/
scsi_base.c
2817 lines (2480 loc) · 80.5 KB
/
scsi_base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $OpenBSD: scsi_base.c,v 1.265 2020/03/12 19:21:01 krw Exp $ */
/* $NetBSD: scsi_base.c,v 1.43 1997/04/02 02:29:36 mycroft Exp $ */
/*
* Copyright (c) 1994, 1995, 1997 Charles M. Hannum. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Charles M. Hannum.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Originally written by Julian Elischer (julian@dialix.oz.au)
* Detailed SCSI error printing Copyright 1997 by Matthew Jacob.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/uio.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/pool.h>
#include <sys/task.h>
#include <scsi/scsi_all.h>
#include <scsi/scsi_disk.h>
#include <scsi/scsiconf.h>
static __inline void asc2ascii(u_int8_t, u_int8_t ascq, char *result,
size_t len);
int scsi_xs_error(struct scsi_xfer *);
char *scsi_decode_sense(struct scsi_sense_data *, int);
void scsi_xs_sync_done(struct scsi_xfer *);
/* Values for flag parameter to scsi_decode_sense. */
#define DECODE_SENSE_KEY 1
#define DECODE_ASC_ASCQ 2
#define DECODE_SKSV 3
struct pool scsi_xfer_pool;
struct pool scsi_plug_pool;
struct scsi_plug {
struct task task;
struct scsibus_softc *sb;
int target;
int lun;
int how;
};
void scsi_plug_probe(void *);
void scsi_plug_detach(void *);
struct scsi_xfer * scsi_xs_io(struct scsi_link *, void *, int);
int scsi_ioh_pending(struct scsi_iopool *);
struct scsi_iohandler * scsi_ioh_deq(struct scsi_iopool *);
void scsi_xsh_runqueue(struct scsi_link *);
void scsi_xsh_ioh(void *, void *);
int scsi_link_open(struct scsi_link *);
void scsi_link_close(struct scsi_link *);
void * scsi_iopool_get(struct scsi_iopool *);
void scsi_iopool_put(struct scsi_iopool *, void *);
/* Various helper functions for scsi_do_mode_sense() */
int scsi_mode_sense(struct scsi_link *, int,
union scsi_mode_sense_buf *, int);
int scsi_mode_sense_big(struct scsi_link *, int,
union scsi_mode_sense_buf *, int);
void * scsi_mode_sense_page(struct scsi_mode_header *, int,
int);
void * scsi_mode_sense_big_page(struct scsi_mode_header_big *,
int, int);
/* ioh/xsh queue state */
#define RUNQ_IDLE 0
#define RUNQ_LINKQ 1
#define RUNQ_POOLQ 2
/* synchronous api for allocating an io. */
struct scsi_io_mover {
struct mutex mtx;
void *io;
u_int done;
};
#define SCSI_IO_MOVER_INITIALIZER { MUTEX_INITIALIZER(IPL_BIO), NULL, 0 }
void scsi_move(struct scsi_io_mover *);
void scsi_move_done(void *, void *);
void scsi_io_get_done(void *, void *);
void scsi_xs_get_done(void *, void *);
/*
* Called when a scsibus is attached to initialize global data.
*/
void
scsi_init(void)
{
static int scsi_init_done;
if (scsi_init_done)
return;
scsi_init_done = 1;
#if defined(SCSI_DELAY) && SCSI_DELAY > 0
/* Historical. Older buses may need a moment to stabilize. */
delay(1000000 * SCSI_DELAY);
#endif /* SCSI_DELAY && SCSI_DELAY > 0 */
/* Initialize the scsi_xfer pool. */
pool_init(&scsi_xfer_pool, sizeof(struct scsi_xfer), 0, IPL_BIO, 0,
"scxspl", NULL);
pool_init(&scsi_plug_pool, sizeof(struct scsi_plug), 0, IPL_BIO, 0,
"scsiplug", NULL);
}
int
scsi_req_probe(struct scsibus_softc *sb, int target, int lun)
{
struct scsi_plug *p;
p = pool_get(&scsi_plug_pool, PR_NOWAIT);
if (p == NULL)
return ENOMEM;
task_set(&p->task, scsi_plug_probe, p);
p->sb = sb;
p->target = target;
p->lun = lun;
task_add(systq, &p->task);
return 0;
}
int
scsi_req_detach(struct scsibus_softc *sb, int target, int lun, int how)
{
struct scsi_plug *p;
p = pool_get(&scsi_plug_pool, PR_NOWAIT);
if (p == NULL)
return ENOMEM;
task_set(&p->task, scsi_plug_detach, p);
p->sb = sb;
p->target = target;
p->lun = lun;
p->how = how;
task_add(systq, &p->task);
return 0;
}
void
scsi_plug_probe(void *xp)
{
struct scsi_plug *p = xp;
struct scsibus_softc *sb = p->sb;
int target = p->target, lun = p->lun;
pool_put(&scsi_plug_pool, p);
scsi_probe(sb, target, lun);
}
void
scsi_plug_detach(void *xp)
{
struct scsi_plug *p = xp;
struct scsibus_softc *sb = p->sb;
int target = p->target, lun = p->lun;
int how = p->how;
pool_put(&scsi_plug_pool, p);
scsi_detach(sb, target, lun, how);
}
int
scsi_pending_start(struct mutex *mtx, u_int *running)
{
int rv = 1;
mtx_enter(mtx);
(*running)++;
if ((*running) > 1)
rv = 0;
mtx_leave(mtx);
return rv;
}
int
scsi_pending_finish(struct mutex *mtx, u_int *running)
{
int rv = 1;
mtx_enter(mtx);
(*running)--;
if ((*running) > 0) {
(*running) = 1;
rv = 0;
}
mtx_leave(mtx);
return rv;
}
void
scsi_iopool_init(struct scsi_iopool *iopl, void *iocookie,
void *(*io_get)(void *), void (*io_put)(void *, void *))
{
iopl->iocookie = iocookie;
iopl->io_get = io_get;
iopl->io_put = io_put;
TAILQ_INIT(&iopl->queue);
iopl->running = 0;
mtx_init(&iopl->mtx, IPL_BIO);
}
void *
scsi_iopool_get(struct scsi_iopool *iopl)
{
void *io;
KERNEL_LOCK();
io = iopl->io_get(iopl->iocookie);
KERNEL_UNLOCK();
return io;
}
void
scsi_iopool_put(struct scsi_iopool *iopl, void *io)
{
KERNEL_LOCK();
iopl->io_put(iopl->iocookie, io);
KERNEL_UNLOCK();
}
void
scsi_iopool_destroy(struct scsi_iopool *iopl)
{
struct scsi_runq sleepers = TAILQ_HEAD_INITIALIZER(sleepers);
struct scsi_iohandler *ioh = NULL;
mtx_enter(&iopl->mtx);
while ((ioh = TAILQ_FIRST(&iopl->queue)) != NULL) {
TAILQ_REMOVE(&iopl->queue, ioh, q_entry);
ioh->q_state = RUNQ_IDLE;
if (ioh->handler == scsi_io_get_done)
TAILQ_INSERT_TAIL(&sleepers, ioh, q_entry);
#ifdef DIAGNOSTIC
else
panic("scsi_iopool_destroy: scsi_iohandler on pool");
#endif /* DIAGNOSTIC */
}
mtx_leave(&iopl->mtx);
while ((ioh = TAILQ_FIRST(&sleepers)) != NULL) {
TAILQ_REMOVE(&sleepers, ioh, q_entry);
ioh->handler(ioh->cookie, NULL);
}
}
void *
scsi_default_get(void *iocookie)
{
return SCSI_IOPOOL_POISON;
}
void
scsi_default_put(void *iocookie, void *io)
{
#ifdef DIAGNOSTIC
if (io != SCSI_IOPOOL_POISON)
panic("unexpected opening returned");
#endif /* DIAGNOSTIC */
}
/*
* public interface to the ioh api.
*/
void
scsi_ioh_set(struct scsi_iohandler *ioh, struct scsi_iopool *iopl,
void (*handler)(void *, void *), void *cookie)
{
ioh->q_state = RUNQ_IDLE;
ioh->pool = iopl;
ioh->handler = handler;
ioh->cookie = cookie;
}
int
scsi_ioh_add(struct scsi_iohandler *ioh)
{
struct scsi_iopool *iopl = ioh->pool;
int rv = 0;
mtx_enter(&iopl->mtx);
switch (ioh->q_state) {
case RUNQ_IDLE:
TAILQ_INSERT_TAIL(&iopl->queue, ioh, q_entry);
ioh->q_state = RUNQ_POOLQ;
rv = 1;
break;
#ifdef DIAGNOSTIC
case RUNQ_POOLQ:
break;
default:
panic("scsi_ioh_add: unexpected state %u", ioh->q_state);
#endif /* DIAGNOSTIC */
}
mtx_leave(&iopl->mtx);
/* lets get some io up in the air */
scsi_iopool_run(iopl);
return rv;
}
int
scsi_ioh_del(struct scsi_iohandler *ioh)
{
struct scsi_iopool *iopl = ioh->pool;
int rv = 0;
mtx_enter(&iopl->mtx);
switch (ioh->q_state) {
case RUNQ_POOLQ:
TAILQ_REMOVE(&iopl->queue, ioh, q_entry);
ioh->q_state = RUNQ_IDLE;
rv = 1;
break;
#ifdef DIAGNOSTIC
case RUNQ_IDLE:
break;
default:
panic("scsi_ioh_del: unexpected state %u", ioh->q_state);
#endif /* DIAGNOSTIC */
}
mtx_leave(&iopl->mtx);
return rv;
}
/*
* internal iopool runqueue handling.
*/
struct scsi_iohandler *
scsi_ioh_deq(struct scsi_iopool *iopl)
{
struct scsi_iohandler *ioh = NULL;
mtx_enter(&iopl->mtx);
ioh = TAILQ_FIRST(&iopl->queue);
if (ioh != NULL) {
TAILQ_REMOVE(&iopl->queue, ioh, q_entry);
ioh->q_state = RUNQ_IDLE;
}
mtx_leave(&iopl->mtx);
return ioh;
}
int
scsi_ioh_pending(struct scsi_iopool *iopl)
{
int rv;
mtx_enter(&iopl->mtx);
rv = !TAILQ_EMPTY(&iopl->queue);
mtx_leave(&iopl->mtx);
return rv;
}
void
scsi_iopool_run(struct scsi_iopool *iopl)
{
struct scsi_iohandler *ioh;
void *io;
if (!scsi_pending_start(&iopl->mtx, &iopl->running))
return;
do {
while (scsi_ioh_pending(iopl)) {
io = scsi_iopool_get(iopl);
if (io == NULL)
break;
ioh = scsi_ioh_deq(iopl);
if (ioh == NULL) {
scsi_iopool_put(iopl, io);
break;
}
ioh->handler(ioh->cookie, io);
}
} while (!scsi_pending_finish(&iopl->mtx, &iopl->running));
}
/*
* move an io from a runq to a proc thats waiting for an io.
*/
void
scsi_move(struct scsi_io_mover *m)
{
mtx_enter(&m->mtx);
while (!m->done)
msleep_nsec(m, &m->mtx, PRIBIO, "scsiiomv", INFSLP);
mtx_leave(&m->mtx);
}
void
scsi_move_done(void *cookie, void *io)
{
struct scsi_io_mover *m = cookie;
mtx_enter(&m->mtx);
m->io = io;
m->done = 1;
wakeup_one(m);
mtx_leave(&m->mtx);
}
/*
* synchronous api for allocating an io.
*/
void *
scsi_io_get(struct scsi_iopool *iopl, int flags)
{
struct scsi_io_mover m = SCSI_IO_MOVER_INITIALIZER;
struct scsi_iohandler ioh;
void *io;
/* try and sneak an io off the backend immediately */
io = scsi_iopool_get(iopl);
if (io != NULL)
return io;
else if (ISSET(flags, SCSI_NOSLEEP))
return NULL;
/* otherwise sleep until we get one */
scsi_ioh_set(&ioh, iopl, scsi_io_get_done, &m);
scsi_ioh_add(&ioh);
scsi_move(&m);
return m.io;
}
void
scsi_io_get_done(void *cookie, void *io)
{
scsi_move_done(cookie, io);
}
void
scsi_io_put(struct scsi_iopool *iopl, void *io)
{
scsi_iopool_put(iopl, io);
scsi_iopool_run(iopl);
}
/*
* public interface to the xsh api.
*/
void
scsi_xsh_set(struct scsi_xshandler *xsh, struct scsi_link *link,
void (*handler)(struct scsi_xfer *))
{
scsi_ioh_set(&xsh->ioh, link->pool, scsi_xsh_ioh, xsh);
xsh->link = link;
xsh->handler = handler;
}
int
scsi_xsh_add(struct scsi_xshandler *xsh)
{
struct scsi_link *link = xsh->link;
int rv = 0;
if (ISSET(link->state, SDEV_S_DYING))
return 0;
mtx_enter(&link->pool->mtx);
if (xsh->ioh.q_state == RUNQ_IDLE) {
TAILQ_INSERT_TAIL(&link->queue, &xsh->ioh, q_entry);
xsh->ioh.q_state = RUNQ_LINKQ;
rv = 1;
}
mtx_leave(&link->pool->mtx);
/* lets get some io up in the air */
scsi_xsh_runqueue(link);
return rv;
}
int
scsi_xsh_del(struct scsi_xshandler *xsh)
{
struct scsi_link *link = xsh->link;
int rv = 1;
mtx_enter(&link->pool->mtx);
switch (xsh->ioh.q_state) {
case RUNQ_IDLE:
rv = 0;
break;
case RUNQ_LINKQ:
TAILQ_REMOVE(&link->queue, &xsh->ioh, q_entry);
break;
case RUNQ_POOLQ:
TAILQ_REMOVE(&link->pool->queue, &xsh->ioh, q_entry);
link->pending--;
if (ISSET(link->state, SDEV_S_DYING) && link->pending == 0)
wakeup_one(&link->pending);
break;
default:
panic("unexpected xsh state %u", xsh->ioh.q_state);
}
xsh->ioh.q_state = RUNQ_IDLE;
mtx_leave(&link->pool->mtx);
return rv;
}
/*
* internal xs runqueue handling.
*/
void
scsi_xsh_runqueue(struct scsi_link *link)
{
struct scsi_iohandler *ioh;
int runq;
if (!scsi_pending_start(&link->pool->mtx, &link->running))
return;
do {
runq = 0;
mtx_enter(&link->pool->mtx);
while (!ISSET(link->state, SDEV_S_DYING) &&
link->pending < link->openings &&
((ioh = TAILQ_FIRST(&link->queue)) != NULL)) {
link->pending++;
TAILQ_REMOVE(&link->queue, ioh, q_entry);
TAILQ_INSERT_TAIL(&link->pool->queue, ioh, q_entry);
ioh->q_state = RUNQ_POOLQ;
runq = 1;
}
mtx_leave(&link->pool->mtx);
if (runq)
scsi_iopool_run(link->pool);
} while (!scsi_pending_finish(&link->pool->mtx, &link->running));
}
void
scsi_xsh_ioh(void *cookie, void *io)
{
struct scsi_xshandler *xsh = cookie;
struct scsi_xfer *xs;
xs = scsi_xs_io(xsh->link, io, SCSI_NOSLEEP);
if (xs == NULL) {
/*
* in this situation we should queue things waiting for an
* xs and then give them xses when they were supposed be to
* returned to the pool.
*/
printf("scsi_xfer pool exhausted!\n");
scsi_xsh_add(xsh);
return;
}
xsh->handler(xs);
}
/*
* Get a scsi transfer structure for the caller.
* Go to the iopool backend for an "opening" and then attach an xs to it.
*/
struct scsi_xfer *
scsi_xs_get(struct scsi_link *link, int flags)
{
struct scsi_xshandler xsh;
struct scsi_io_mover m = SCSI_IO_MOVER_INITIALIZER;
struct scsi_iopool *iopl = link->pool;
void *io;
if (ISSET(link->state, SDEV_S_DYING))
return NULL;
/* really custom xs handler to avoid scsi_xsh_ioh */
scsi_ioh_set(&xsh.ioh, iopl, scsi_xs_get_done, &m);
xsh.link = link;
if (!scsi_link_open(link)) {
if (ISSET(flags, SCSI_NOSLEEP))
return NULL;
scsi_xsh_add(&xsh);
scsi_move(&m);
if (m.io == NULL)
return NULL;
io = m.io;
} else if ((io = scsi_iopool_get(iopl)) == NULL) {
if (ISSET(flags, SCSI_NOSLEEP)) {
scsi_link_close(link);
return NULL;
}
scsi_ioh_add(&xsh.ioh);
scsi_move(&m);
if (m.io == NULL)
return NULL;
io = m.io;
}
return scsi_xs_io(link, io, flags);
}
void
scsi_xs_get_done(void *cookie, void *io)
{
scsi_move_done(cookie, io);
}
void
scsi_link_shutdown(struct scsi_link *link)
{
struct scsi_runq sleepers = TAILQ_HEAD_INITIALIZER(sleepers);
struct scsi_iopool *iopl = link->pool;
struct scsi_iohandler *ioh;
struct scsi_xshandler *xsh;
mtx_enter(&iopl->mtx);
while ((ioh = TAILQ_FIRST(&link->queue)) != NULL) {
TAILQ_REMOVE(&link->queue, ioh, q_entry);
ioh->q_state = RUNQ_IDLE;
if (ioh->handler == scsi_xs_get_done)
TAILQ_INSERT_TAIL(&sleepers, ioh, q_entry);
#ifdef DIAGNOSTIC
else
panic("scsi_link_shutdown: scsi_xshandler on link");
#endif /* DIAGNOSTIC */
}
ioh = TAILQ_FIRST(&iopl->queue);
while (ioh != NULL) {
xsh = (struct scsi_xshandler *)ioh;
ioh = TAILQ_NEXT(ioh, q_entry);
#ifdef DIAGNOSTIC
if (xsh->ioh.handler == scsi_xsh_ioh &&
xsh->link == link)
panic("scsi_link_shutdown: scsi_xshandler on pool");
#endif /* DIAGNOSTIC */
if (xsh->ioh.handler == scsi_xs_get_done &&
xsh->link == link) {
TAILQ_REMOVE(&iopl->queue, &xsh->ioh, q_entry);
xsh->ioh.q_state = RUNQ_IDLE;
link->pending--;
TAILQ_INSERT_TAIL(&sleepers, &xsh->ioh, q_entry);
}
}
while (link->pending > 0)
msleep_nsec(&link->pending, &iopl->mtx, PRIBIO, "pendxs",
INFSLP);
mtx_leave(&iopl->mtx);
while ((ioh = TAILQ_FIRST(&sleepers)) != NULL) {
TAILQ_REMOVE(&sleepers, ioh, q_entry);
ioh->handler(ioh->cookie, NULL);
}
}
int
scsi_link_open(struct scsi_link *link)
{
int open = 0;
mtx_enter(&link->pool->mtx);
if (link->pending < link->openings) {
link->pending++;
open = 1;
}
mtx_leave(&link->pool->mtx);
return open;
}
void
scsi_link_close(struct scsi_link *link)
{
mtx_enter(&link->pool->mtx);
link->pending--;
if (ISSET(link->state, SDEV_S_DYING) && link->pending == 0)
wakeup_one(&link->pending);
mtx_leave(&link->pool->mtx);
scsi_xsh_runqueue(link);
}
struct scsi_xfer *
scsi_xs_io(struct scsi_link *link, void *io, int flags)
{
struct scsi_xfer *xs;
xs = pool_get(&scsi_xfer_pool, PR_ZERO |
(ISSET(flags, SCSI_NOSLEEP) ? PR_NOWAIT : PR_WAITOK));
if (xs == NULL) {
scsi_io_put(link->pool, io);
scsi_link_close(link);
} else {
xs->flags = flags;
xs->sc_link = link;
xs->retries = SCSI_RETRIES;
xs->timeout = 10000;
xs->cmd = &xs->cmdstore;
xs->io = io;
}
return xs;
}
void
scsi_xs_put(struct scsi_xfer *xs)
{
struct scsi_link *link = xs->sc_link;
void *io = xs->io;
pool_put(&scsi_xfer_pool, xs);
scsi_io_put(link->pool, io);
scsi_link_close(link);
}
/*
* Get scsi driver to send a "are you ready?" command
*/
int
scsi_test_unit_ready(struct scsi_link *link, int retries, int flags)
{
struct scsi_test_unit_ready *cmd;
struct scsi_xfer *xs;
int error;
xs = scsi_xs_get(link, flags);
if (xs == NULL)
return ENOMEM;
xs->cmdlen = sizeof(*cmd);
xs->retries = retries;
xs->timeout = 10000;
cmd = (struct scsi_test_unit_ready *)xs->cmd;
cmd->opcode = TEST_UNIT_READY;
error = scsi_xs_sync(xs);
scsi_xs_put(xs);
return error;
}
void
scsi_init_inquiry(struct scsi_xfer *xs, u_int8_t flags, u_int8_t pagecode,
void *data, size_t len)
{
struct scsi_inquiry *cmd;
cmd = (struct scsi_inquiry *)xs->cmd;
cmd->opcode = INQUIRY;
cmd->flags = flags;
cmd->pagecode = pagecode;
_lto2b(len, cmd->length);
xs->cmdlen = sizeof(*cmd);
SET(xs->flags, SCSI_DATA_IN);
xs->data = data;
xs->datalen = len;
}
/*
* Do a scsi operation asking a device what it is.
* Use the scsi_cmd routine in the switch table.
*/
int
scsi_inquire(struct scsi_link *link, struct scsi_inquiry_data *inqbuf,
int flags)
{
struct scsi_xfer *xs;
size_t bytes;
int error;
/*
* Start by asking for only the basic 36 bytes of SCSI2 inquiry
* information. This avoids problems with devices that choke trying to
* supply more.
*/
bytes = 36;
#ifdef SCSIDEBUG
again:
#endif /* SCSIDEBUG */
xs = scsi_xs_get(link, flags);
if (xs == NULL)
return EBUSY;
scsi_init_inquiry(xs, 0, 0, inqbuf, bytes);
bzero(inqbuf, sizeof(*inqbuf));
memset(&inqbuf->vendor, ' ', sizeof inqbuf->vendor);
memset(&inqbuf->product, ' ', sizeof inqbuf->product);
memset(&inqbuf->revision, ' ', sizeof inqbuf->revision);
memset(&inqbuf->extra, ' ', sizeof inqbuf->extra);
error = scsi_xs_sync(xs);
scsi_xs_put(xs);
#ifdef SCSIDEBUG
sc_print_addr(link);
if (bytes > inqbuf->additional_length + 4)
bytes = inqbuf->additional_length + 4;
printf("got %zu of %u bytes of inquiry data:\n",
bytes, inqbuf->additional_length + 4);
scsi_show_mem((u_char *)inqbuf, bytes);
if (bytes == 36 && bytes < inqbuf->additional_length + 4) {
bytes = inqbuf->additional_length + 4;
if (bytes > sizeof(*inqbuf))
bytes = sizeof(*inqbuf);
goto again;
}
#endif /* SCSIDEBUG */
return error;
}
/*
* Query a VPD inquiry page
*/
int
scsi_inquire_vpd(struct scsi_link *link, void *buf, u_int buflen,
u_int8_t page, int flags)
{
struct scsi_xfer *xs;
int error;
#ifdef SCSIDEBUG
u_int32_t bytes;
#endif /* SCSIDEBUG */
if (ISSET(link->flags, SDEV_UMASS))
return EJUSTRETURN;
xs = scsi_xs_get(link, flags | SCSI_DATA_IN | SCSI_SILENT);
if (xs == NULL)
return ENOMEM;
xs->retries = 2;
xs->timeout = 10000;
scsi_init_inquiry(xs, SI_EVPD, page, buf, buflen);
error = scsi_xs_sync(xs);
scsi_xs_put(xs);
#ifdef SCSIDEBUG
sc_print_addr(link);
if (error == 0) {
bytes = sizeof(struct scsi_vpd_hdr) +
_2btol(((struct scsi_vpd_hdr *)buf)->page_length);
if (bytes < buflen)
buflen = bytes;
printf("got %u of %u bytes of VPD inquiry page %u data:\n",
buflen, bytes, page);
scsi_show_mem(buf, buflen);
} else {
printf("VPD inquiry page %u not available\n", page);
}
#endif /* SCSIDEBUG */
return error;
}
int
scsi_read_cap_10(struct scsi_link *link, struct scsi_read_cap_data *rdcap,
int flags)
{
struct scsi_read_capacity cdb;
struct scsi_xfer *xs;
int rv;
xs = scsi_xs_get(link, flags | SCSI_DATA_IN | SCSI_SILENT);
if (xs == NULL)
return ENOMEM;
memset(&cdb, 0, sizeof(cdb));
cdb.opcode = READ_CAPACITY;
memcpy(xs->cmd, &cdb, sizeof(cdb));
xs->cmdlen = sizeof(cdb);
xs->data = (void *)rdcap;
xs->datalen = sizeof(*rdcap);
xs->timeout = 20000;
rv = scsi_xs_sync(xs);
scsi_xs_put(xs);
#ifdef SCSIDEBUG
if (rv == 0) {
sc_print_addr(link);
printf("read capacity 10 data:\n");
scsi_show_mem((u_char *)rdcap, sizeof(*rdcap));
}
#endif /* SCSIDEBUG */
return rv;
}
int
scsi_read_cap_16(struct scsi_link *link, struct scsi_read_cap_data_16 *rdcap,
int flags)
{
struct scsi_read_capacity_16 cdb;
struct scsi_xfer *xs;
int rv;
xs = scsi_xs_get(link, flags | SCSI_DATA_IN | SCSI_SILENT);
if (xs == NULL)
return ENOMEM;
memset(&cdb, 0, sizeof(cdb));
cdb.opcode = READ_CAPACITY_16;
cdb.byte2 = SRC16_SERVICE_ACTION;
_lto4b(sizeof(*rdcap), cdb.length);
memcpy(xs->cmd, &cdb, sizeof(cdb));
xs->cmdlen = sizeof(cdb);
xs->data = (void *)rdcap;
xs->datalen = sizeof(*rdcap);
xs->timeout = 20000;
rv = scsi_xs_sync(xs);
scsi_xs_put(xs);
#ifdef SCSIDEBUG
if (rv == 0) {
sc_print_addr(link);
printf("read capacity 16 data:\n");
scsi_show_mem((u_char *)rdcap, sizeof(*rdcap));
}