Imported Upstream version 2.4.4p3
[debian/amanda] / contrib / sst / sst.c
1 /*
2  * Copyright (c) 1997, by Sun Microsystems, Inc.
3  * All Rights Reserved
4  */
5
6 /*
7  * sst.c - Simple SCSI Target driver; a template character SCSA target
8  *         driver for Solaris 2.x.
9  *
10  * This file is a product of Sun Microsystems, Inc. and is provided for
11  * unrestricted use provided that this legend is included on all tape
12  * media and as a part of the software program in whole or part.  Users
13  * may copy or modify this file without charge, but are not authorized to
14  * license or distribute it to anyone else except as part of a product
15  * or program developed by the user.
16  *
17  * THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
18  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
20  *
21  * This file is provided with no support and without any obligation on the
22  * part of Sun Microsystems, Inc. to assist in its use, correction,
23  * modification or enhancement.
24  *
25  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
26  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE
27  * OR ANY PART THEREOF.
28  *
29  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
30  * or profits or other special, indirect and consequential damages, even
31  * if Sun has been advised of the possibility of such damages.
32  *
33  * Sun Microsystems, Inc.
34  * 901 San Antonio Rd
35  * Palo Alto, California  94303
36  *
37  * ------------------------------------------------------------------
38  *
39  * This driver is intended as an example of programming a SCSA
40  * target driver for Solaris 2.X; it is not intended for any particular
41  * device.
42  *
43  * Areas where you may need to change this code or add your own to
44  * deal with your specific device are marked "Note".
45  * Other warnings are marked "WARNING"
46  *
47  * To compile:
48  *      % cc -D_KERNEL -c sst.c
49  *      % ld -r sst.o -o sst
50  *
51  * To install:
52  * 1. Copy the module (sst) and config file (sst.conf) into /usr/kernel/drv
53  * 2. Add an entry to /etc/devlink.tab, of the form:
54  *      "type=sample_driver;name=sst;minor=character        rsst\A1"
55  *    This will cause devlinks(1M) to create link(s) to /devices with
56  *    names of the form "/dev/rsstX" where X is the SCSI target number.
57  * 3. Run add_drv(1M).
58  *
59  * Setting variables
60  *      Variables can be explicitly set from the /etc/system file, by
61  *      adding an entry of the form
62  *              "set sst:<variable name>=<value>"
63  *      Alternatively, you can use adb to set variables and debug as
64  *      follows:
65  *              # adb -kw /dev/ksyms /dev/mem
66  *      The /etc/system file is read only once at boot time, if you change
67  *      it you must reboot for the change to take effect.
68  */
69
70 #pragma ident   "@(#)sst.c 1.23 97/10/03 SMI"
71
72 /*
73  * Includes, Declarations and Local Data
74  */
75
76 #include <sys/scsi/scsi.h>
77 #include <sys/file.h>
78 #ifdef __GNUC__
79 #include <stdarg.h>
80 #endif
81
82 #include "sst_def.h"
83
84 /*
85  * Local Function Prototypes
86  */
87 static int sst_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
88 static int sst_close(dev_t dev, int flag, int otyp, cred_t *cred_p);
89 static int sst_read(dev_t dev, struct uio *uio, cred_t *cred_p);
90 static int sst_write(dev_t dev, struct uio *uio, cred_t *cred_p);
91 static int sst_aread(dev_t dev, struct aio_req *aio, cred_t *credp);
92 static int sst_awrite(dev_t dev, struct aio_req *aio, cred_t *credp);
93 static int sst_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
94
95 static int sst_strategy(struct buf *bp);
96 static int sst_ioctl_cmd(dev_t, struct uscsi_cmd *, enum uio_seg,
97     enum uio_seg, int mode);
98
99 static int sst_unit_ready(dev_t dev);
100 static void sst_done(struct scsi_target *targ, struct buf *bp);
101 static void sst_restart(caddr_t arg);
102 static void sst_callback(struct scsi_pkt *pkt);
103 static struct scsi_pkt *sst_make_cmd(struct scsi_target *targ, struct buf *bp,
104     struct scsi_pkt *pktp);
105 static void sst_fill_cdb(struct scsi_pkt *pkt, struct scsi_target *targ,
106     struct buf *bp, u_int flags);
107 static int sst_handle_incomplete(struct scsi_target *targ);
108 static int sst_handle_arq(struct scsi_pkt *pktp, struct scsi_target *targ,
109     struct buf *bp);
110 static int sst_handle_sense(struct scsi_target *targ, struct buf *bp);
111 static int sst_check_error(struct scsi_target *targ, struct buf *bp);
112 static void sst_log(struct scsi_device *devp, int level, const char *fmt, ...);
113 static void hex_print(char *msg, void *cptr, int len);
114 static void sst_dump_cdb(struct scsi_target *tgt, struct scsi_pkt *pkt,
115     int cdblen);
116
117 /*
118  * Local Static Data
119  */
120 static int sst_io_time          = SST_IO_TIME;
121 static int sst_retry_count      = SST_RETRY_COUNT;
122 static void *sst_state;
123
124 /*
125  * Errors at or above this level will be reported
126  */
127 static int32_t sst_error_reporting = SCSI_ERR_RETRYABLE;
128
129 /*
130  * Enable the driver debugging code if DEBUG is defined (DEBUG also
131  * enables other debugging code, e.g. ASSERT statements).
132  */
133 #ifdef  DEBUG
134 #define SST_DEBUG
135 #endif  DEBUG
136
137
138 /*
139  * Debug message control
140  * Debug Levels:
141  *      0 = no messages
142  *      1 = Errors
143  *      2 = Subroutine calls & control flow
144  *      3 = I/O Data (verbose!)
145  * Can be set with adb or in the /etc/system file with
146  * "set sst:sst_debug=<value>"
147  * turn on diagnostics if DEBUG is defined (DEBUG also enables
148  * other debugging code, e.g. ASSERT statements).
149  */
150
151 #ifdef  SST_DEBUG
152 static int sst_debug = 3;
153 static int sst_debug_cdb = 1;
154 #else
155 static int sst_debug = 0;
156 static int sst_debug_cdb = 0;
157 #endif /* SST_DEBUG */
158
159 #define SST_DUMP_CDB(tgt, pkt, cdblen)                          \
160         if (sst_debug_cdb) {                                    \
161                 sst_dump_cdb((tgt), (pkt), (cdblen));           \
162         }
163
164 /*
165  * Array of commands supported by the device, suitable for
166  * scsi_errmsg(9f)
167  * Note: Add or remove commands here as appropriate for
168  *       your device.
169  */
170 static struct scsi_key_strings sst_cmds[] = {
171         0x00, "test unit ready",
172         0x01, "rezero/rewind",
173         0x03, "request sense",
174         0x04, "format",
175         0x05, "read block limits",
176         0x07, "reassign",
177         0x08, "read",
178         0x0a, "write",
179         0x0b, "seek",
180         0x0f, "read reverse",
181         0x10, "write file mark",
182         0x12, "inquiry",
183         0x13, "verify",
184         0x14, "recover buffered data",
185         0x15, "mode select",
186         0x16, "reserve",
187         0x17, "release",
188         0x18, "copy",
189         0x19, "erase tape",
190         0x1a, "mode sense",
191         0x1b, "start/stop/load",
192         0x1e, "door lock",
193         0x37, "read defect data",
194         -1, NULL,
195 };
196
197 /*
198  *      Module Loading/Unloading and Autoconfiguration Routines
199  */
200
201 /*
202  * Device driver ops vector - cb_ops(9s) structure
203  * Device switch table fields (equivalent to the old 4.x cdevsw and bdevsw).
204  * Unsupported entry points (e.g. for xxprint() and xxdump()) are set to
205  * nodev, except for the poll routine, which is set to nochpoll(), a
206  * routine that returns ENXIO.
207  *
208  * Note: This uses ddi_prop_op for the prop_op(9e) routine. If your device
209  * has its own properties, you should implement a sst_prop_op() routine
210  * to manage them.
211  */
212 static struct cb_ops sst_cb_ops = {
213         sst_open,               /* b/c open */
214         sst_close,              /* b/c close */
215         sst_strategy,           /* b strategy */
216         nodev,                  /* b print */
217         nodev,                  /* b dump */
218         sst_read,               /* c read */
219         sst_write,              /* c write */
220         sst_ioctl,              /* c ioctl */
221         nodev,                  /* c devmap */
222         nodev,                  /* c mmap */
223         nodev,                  /* c segmap */
224         nochpoll,               /* c poll */
225         ddi_prop_op,            /* cb_prop_op */
226         0,                      /* streamtab  */
227         D_MP | D_NEW,           /* Driver compatibility flag */
228         CB_REV,                 /* cb_ops revision number */
229         sst_aread,              /* c aread */
230         sst_awrite              /* c awrite */
231 };
232
233
234 /*
235  * dev_ops(9S) structure, defined in sys/devops.h.
236  * Device Operations table, for autoconfiguration
237  *
238  * Note: If you replace the sst_detach entry here with "nulldev", it
239  * implies that the detach is always successful. We need a real detach to
240  * free the sense packet and the unit structure. If you don't want the
241  * driver to ever be unloaded, replace the sst_detach entry with "nodev"
242  * (which always fails).
243  */
244 static int sst_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
245                 void **result);
246 static int sst_probe(dev_info_t *dip);
247 static int sst_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
248 static int sst_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
249 static int sst_power(dev_info_t *dip, int component, int level);
250
251 static int sst_doattach(dev_info_t *dip);
252 static int sst_dodetach(dev_info_t *dip);
253
254 static struct dev_ops sst_ops = {
255         DEVO_REV,               /* devo_rev, */
256         0,                      /* refcnt  */
257         sst_info,               /* info */
258         nulldev,                /* identify */
259         sst_probe,              /* probe */
260         sst_attach,             /* attach */
261         sst_detach,             /* detach */
262         nodev,                  /* reset */
263         &sst_cb_ops,            /* driver operations */
264         (struct bus_ops *)0,    /* bus operations */
265         sst_power               /* power */
266 };
267
268 /*
269  * Module Loading and Unloading
270  * See modctl.h for external structure definitions.
271  */
272
273 static struct modldrv modldrv = {
274         &mod_driverops,                 /* Type of module (driver). */
275         "SCSI Simple Target Driver Version 1.23",  /* Description */
276         &sst_ops,                       /* driver ops */
277 };
278
279 static struct modlinkage modlinkage = {
280         MODREV_1, (void *)&modldrv, NULL
281 };
282
283 /*
284  * Tell the system that we depend on the general scsi support routines,
285  * i.e the scsi "misc" module must be loaded
286  */
287 char _depends_on[] = "misc/scsi";
288
289 /*
290  * _init(9E) - module Installation
291  *
292  * Install the driver and "pre-allocate" space for INIT_UNITS units,
293  * i.e. instances of the driver.
294  */
295 int
296 _init(void)
297 {
298         int e;
299
300         if ((e = ddi_soft_state_init(&sst_state,
301             sizeof (struct scsi_target), 0)) != 0) {
302                 SST_LOG(0, SST_CE_DEBUG2,
303                         "_init, ddi_soft_state_init failed: 0x%x\n", e);
304                 return (e);
305         }
306
307         if ((e = mod_install(&modlinkage)) != 0) {
308                 SST_LOG(0, SST_CE_DEBUG2,
309                                 "_init, mod_install failed: 0x%x\n", e);
310                 ddi_soft_state_fini(&sst_state);
311         }
312
313         SST_LOG(0, SST_CE_DEBUG2, "_init succeeded\n");
314         return (e);
315 }
316
317
318 /*
319  * _fini(9E) - module removal
320  */
321 int
322 _fini(void)
323 {
324         int e;
325
326         if ((e = mod_remove(&modlinkage)) != 0) {
327                 SST_LOG(0, SST_CE_DEBUG1,
328                     "_fini mod_remove failed, 0x%x\n", e);
329                 return (e);
330         }
331
332         ddi_soft_state_fini(&sst_state);
333
334         SST_LOG(0, SST_CE_DEBUG2, "_fini succeeded\n");
335         return (e);
336 }
337
338 /*
339  * _info(9E) - return module info
340  */
341 int
342 _info(struct modinfo *modinfop)
343 {
344         SST_LOG(0, SST_CE_DEBUG2, "_info\n");
345         return (mod_info(&modlinkage, modinfop));
346 }
347
348 /*
349  * Autoconfiguration Routines
350  */
351
352 /*
353  * probe(9e)
354  *
355  * Check that we're talking to the right device on the SCSI bus.
356  * Calls scsi_probe(9f) to see if there's a device at our target id.
357  * If there is, scsi_probe will fill in the sd_inq struct (in the devp
358  * scsi device struct) with the inquiry data. Validate the data here,
359  * allocate a request sense packet, and start filling in the private data
360  * structure.
361  *
362  * Probe should be stateless, ie it should have no side-effects. Just
363  * check that we have the right device, don't set up any data or
364  * initialize the device here.  Also, it's a Sun convention to probe
365  * quietly; send messages to the log file only, not to the console.
366  *
367  * The host adapter driver sets up the scsi_device structure and puts
368  * it into the dev_info structure with ddi_set_driver_private().
369  *
370  * No need to allow for probing/attaching in the open() routine because
371  * of the loadability - the first reference to the device will auto-load
372  * it, i.e. will call this routine.
373  */
374
375 #define VIDSZ           8       /* Vendor Id length in Inquiry Data */
376 #define PIDSZ           16      /* Product Id length in Inquiry Data */
377
378 static int
379 sst_probe(dev_info_t *dip)
380 {
381         register struct scsi_device *devp;
382         int     err, rval = DDI_PROBE_FAILURE;
383         int     tgt, lun;
384         char    vpid[VIDSZ+PIDSZ+1];
385
386         devp = (struct scsi_device *)ddi_get_driver_private(dip);
387         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
388                 "target", -1);
389         lun = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
390                 "lun", -1);
391
392         SST_LOG(devp, SST_CE_DEBUG2, "sst_probe: target %d, lun %d\n",
393             tgt, lun);
394
395         /*
396          * Call the routine scsi_probe to do some of the dirty work.
397          * This routine uses the SCSI Inquiry command to  test for the
398          * presence of the device. If it's successful, it will fill in
399          * the sd_inq field in the scsi_device structure.
400          */
401         switch (err = scsi_probe(devp, SLEEP_FUNC)) {
402         case SCSIPROBE_NORESP:
403                 sst_log(devp, CE_CONT, "No response from target %d, lun %d\n",
404                     tgt, lun);
405                 rval = DDI_PROBE_FAILURE;
406                 break;
407
408         case SCSIPROBE_NONCCS:
409         case SCSIPROBE_NOMEM:
410         case SCSIPROBE_FAILURE:
411         default:
412                 SST_LOG(devp, SST_CE_DEBUG1,
413                     "sst_probe: scsi_slave failed, 0x%x\n", err);
414                 rval = DDI_PROBE_FAILURE;
415                 break;
416
417         case SCSIPROBE_EXISTS:
418                 /*
419                  * Inquiry succeeded, devp->sd_inq is now filled in
420                  * Note: Check inq_dtype, inq_vid, inq_pid and any other
421                  *       fields to make sure the target/unit is what's
422                  *       expected (sd_inq is a struct scsi_inquiry,
423                  *       defined in scsi/generic/inquiry.h).
424                  * Note: Put device-specific checking into the appropriate
425                  *       case statement, and delete the rest.
426                  * Note: The DTYPE_* defines are from <scsi/generic/inquiry.h>,
427                  *       this is the full list as of "now", check it for new
428                  *       types.
429                  */
430                 switch (devp->sd_inq->inq_dtype) {
431                 case DTYPE_PROCESSOR:
432                 case DTYPE_OPTICAL:
433                 case DTYPE_DIRECT:
434                 case DTYPE_SEQUENTIAL:
435                 case DTYPE_PRINTER:
436                 case DTYPE_WORM:
437                 case DTYPE_RODIRECT:
438                 case DTYPE_SCANNER:
439                 case DTYPE_CHANGER:
440                 case DTYPE_COMM:
441                         /*
442                          * Print what was found on the console. For your
443                          * device, you should send the 'found' message to
444                          * the system log file only, by inserting an
445                          * exclamation point, "!", as the first character of
446                          * the message - see cmn_err(9f).
447                          */
448                         sst_log(devp, CE_CONT,
449                             "found %s device at tgt%d, lun%d\n",
450                             scsi_dname((int)devp->sd_inq->inq_dtype), tgt, lun);
451                         bcopy(devp->sd_inq->inq_vid, vpid, VIDSZ);
452                         bcopy(devp->sd_inq->inq_pid, vpid+VIDSZ, PIDSZ);
453                         vpid[VIDSZ+PIDSZ] = 0;
454                         sst_log(devp, CE_CONT, "Vendor/Product ID = %s\n",
455                             vpid);
456                         rval = DDI_PROBE_SUCCESS;
457                         break;
458
459                 case DTYPE_NOTPRESENT:
460                         sst_log(devp, CE_NOTE,
461                             "Target reports no device present\n");
462                         rval = DDI_PROBE_FAILURE;
463                         break;
464
465                 default:
466                         sst_log(devp, CE_NOTE,
467                             "Unrecognized device type: 0x%x\n",
468                             devp->sd_inq->inq_dtype);
469                         rval = DDI_PROBE_FAILURE;
470                         break;
471                 }
472         }
473
474         /*
475          * scsi_unprobe() must be called even if scsi_probe() failed
476          */
477         scsi_unprobe(devp);
478
479         return (rval);
480 }
481
482
483
484 /*
485  * Attach(9E)
486  */
487
488 static int
489 sst_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
490 {
491         int                     instance;
492         struct scsi_target      *targ;
493         struct scsi_device      *devp;
494
495         switch (cmd) {
496         case DDI_ATTACH:
497                 return (sst_doattach(dip));
498
499         case DDI_RESUME:
500                 /*
501                  * Suspend/Resume
502                  *
503                  * When the driver suspended, there were no
504                  * outstanding cmds and therefore we only need to
505                  * reset the suspended flag and do a cv_broadcast
506                  * on the suspend_cv to wake up any blocked
507                  * threads
508                  */
509                 instance = ddi_get_instance(dip);
510                 targ = ddi_get_soft_state(sst_state, instance);
511                 if (targ == NULL)
512                         return (DDI_FAILURE);
513                 mutex_enter(SST_MUTEX(targ));
514                 targ->targ_suspended = 0;
515
516                 /* wake up threads blocked in sst_strategy */
517                 cv_broadcast(&targ->targ_suspend_cv);
518
519                 mutex_exit(SST_MUTEX(targ));
520
521                 return (DDI_SUCCESS);
522
523         case DDI_PM_RESUME:
524                 /*
525                  * Power Management suspend
526                  *
527                  * Sinc we have no h/w state to restore, the code to
528                  * handle DDI_PM_RESUME is similar to DDI_RESUME,
529                  * except the driver uses the targ_pm_suspend flag.
530                  */
531                 instance = ddi_get_instance(dip);
532                 targ = ddi_get_soft_state(sst_state, instance);
533                 if (targ == NULL)
534                         return (DDI_FAILURE);
535                 mutex_enter(SST_MUTEX(targ));
536
537                 targ->targ_pm_suspended = 0;
538                 cv_broadcast(&targ->targ_suspend_cv);
539
540                 mutex_exit(SST_MUTEX(targ));
541
542                 return (DDI_SUCCESS);
543
544         default:
545                 SST_LOG(0, SST_CE_DEBUG1,
546                         "sst_attach: unsupported cmd 0x%x\n", cmd);
547                 return (DDI_FAILURE);
548         }
549 }
550
551
552 /*
553  * Attach(9E) - DDI_ATTACH handling
554  *
555  *      - create minor device nodes
556  *      - initialize per-instance mutex's & condition variables
557  *      - device-specific initialization (e.g. read disk label)
558  */
559 static int
560 sst_doattach(dev_info_t *dip)
561 {
562         int                     instance;
563         struct  scsi_pkt        *rqpkt;
564         struct scsi_target      *targ;
565         struct scsi_device      *devp;
566         struct buf              *bp;
567
568         instance = ddi_get_instance(dip);
569         devp = (struct scsi_device *)ddi_get_driver_private(dip);
570         SST_LOG(devp, SST_CE_DEBUG2, "sst_attach: instance %d\n", instance);
571
572         /*
573          * Re-probe the device to get the Inquiry data; it's used
574          * elsewhere in the driver. The Inquiry data was validated in
575          * sst_probe so there's no need to look at it again here.
576          */
577         if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
578                 SST_LOG(0, SST_CE_DEBUG1, "sst_attach: re-probe failed\n");
579                 scsi_unprobe(devp);
580                 return (DDI_FAILURE);
581         }
582
583         if (ddi_soft_state_zalloc(sst_state, instance) != DDI_SUCCESS) {
584                 scsi_unprobe(devp);
585                 return (DDI_FAILURE);
586         }
587
588         targ = ddi_get_soft_state(sst_state, instance);
589         devp->sd_private = (opaque_t)targ;
590
591         targ->targ_sbufp = getrbuf(KM_SLEEP);
592         if (targ->targ_sbufp == NULL) {
593                 goto error;
594         }
595
596         targ->targ_devp = devp;
597
598         /*
599          * Set auto-rqsense, per-target; record whether it's allowed
600          * in targ_arq
601          */
602         targ->targ_arq = (scsi_ifsetcap(ROUTE(targ),
603             "auto-rqsense", 1, 1) == 1) ? 1 : 0;
604         SST_LOG(devp, SST_CE_DEBUG2, "sst_attach: Auto Sensing %s\n",
605             targ->targ_arq ? "enabled" : "disabled");
606
607         if (!targ->targ_arq) {
608                 /*
609                  * Allocate a Request Sense packet
610                  */
611                 bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
612                     SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
613                 if (!bp) {
614                         goto error;
615                 }
616
617                 rqpkt = scsi_init_pkt(&devp->sd_address, NULL, bp, CDB_GROUP0,
618                     targ->targ_arq ? sizeof (struct scsi_arq_status) : 1,
619                     sizeof (struct sst_private),
620                     PKT_CONSISTENT, SLEEP_FUNC, NULL);
621                 if (!rqpkt) {
622                         goto error;
623                 }
624                 devp->sd_sense = (struct scsi_extended_sense *)bp->b_un.b_addr;
625
626                 (void) scsi_setup_cdb((union scsi_cdb *)rqpkt->pkt_cdbp,
627                         SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
628
629                 rqpkt->pkt_comp = sst_callback;
630                 rqpkt->pkt_time = sst_io_time;
631                 rqpkt->pkt_flags |= FLAG_SENSING;
632                 targ->targ_rqs = rqpkt;
633                 targ->targ_rqbp = bp;
634         }
635
636         /*
637          * Create the minor node(s), see the man page
638          * for ddi_create_minor_node(9f).
639          * The 2nd parameter is the minor node name; drvconfig(1M)
640          * appends it to the /devices entry, after the colon.
641          * The 4th parameter ('instance') is the actual minor number,
642          * put into the /devices entry's inode and passed to the driver.
643          * The 5th parameter ("sample_driver") is the node type; it's used
644          * by devlinks to match an entry in /etc/devlink.tab to create
645          * the link from /dev to /devices. The #defines are in <sys/sunddi.h>.
646          */
647         if (ddi_create_minor_node(dip, "character", S_IFCHR, instance,
648             "sample_driver", 0) == DDI_FAILURE) {
649                 SST_LOG(0, SST_CE_DEBUG1, "Create Minor Failed\n");
650                 goto error;
651         }
652
653         /*
654          * Initialize power management bookkeeping.
655          */
656         if (pm_create_components(dip, 1) == DDI_SUCCESS) {
657                 if (pm_idle_component(dip, 0) == DDI_FAILURE) {
658                         SST_LOG(devp, SST_CE_DEBUG1,
659                                 "pm_idle_component() failed\n");
660                         goto error;
661                 }
662                 pm_set_normal_power(dip, 0, 1);
663                 targ->targ_power_level = 1;
664         } else {
665                 SST_LOG(devp, SST_CE_DEBUG1,
666                     "pm_create_component() failed\n");
667                 ddi_remove_minor_node(dip, NULL);
668                 goto error;
669         }
670
671         /*
672          * Since this driver manages devices with "remote" hardware,
673          * i.e. the devices themselves have no "reg" properties,
674          * the SUSPEND/RESUME commands in detach/attach will not be
675          * called by the power management framework unless we request
676          * it by creating a "pm-hardware-state" property and setting it
677          * to value "needs-suspend-resume".
678          */
679         if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
680             "pm-hardware-state", "needs-suspend-resume") !=
681                         DDI_PROP_SUCCESS) {
682                 SST_LOG(devp, SST_CE_DEBUG1,
683                     "ddi_prop_update(\"pm-hardware-state\") failed\n");
684                 pm_destroy_components(dip);
685                 ddi_remove_minor_node(dip, NULL);
686                 goto error;
687         }
688
689         /*
690          * Initialize the condition variables.
691          * Note: We don't need to initialize the mutex (SST_MUTEX)
692          *       because it's actually devp->sd_mutex (in the struct
693          *       scsi_device) and is initialized by our parent, the
694          *       host adapter driver.
695          * Note: We don't really need targ_sbuf_cv, we could just wait
696          *       for targ_pkt_cv instead, but it's clearer this way.
697          */
698         cv_init(&targ->targ_sbuf_cv, "targ_sbuf_cv", CV_DRIVER, NULL);
699         cv_init(&targ->targ_pkt_cv, "targ_pkt_cv", CV_DRIVER, NULL);
700         cv_init(&targ->targ_suspend_cv, "targ_suspend_cv", CV_DRIVER,
701                                                         NULL);
702
703         /*
704          * Note: Do any other pre-open target initialization here,
705          *       e.g. read/verify the disk label for a fixed-disk drive.
706          */
707
708         ddi_report_dev(dip);
709         SST_LOG(devp, SST_CE_DEBUG2, "Attached sst driver\n");
710         targ->targ_state = SST_STATE_CLOSED;
711         return (DDI_SUCCESS);
712
713 error:
714         if (bp) {
715                 scsi_free_consistent_buf(bp);
716         }
717         if (rqpkt) {
718                 scsi_destroy_pkt(rqpkt);
719         }
720         if (targ->targ_sbufp) {
721                 freerbuf(targ->targ_sbufp);
722         }
723         ddi_soft_state_free(sst_state, instance);
724         devp->sd_private = (opaque_t)0;
725         devp->sd_sense = (struct scsi_extended_sense *)0;
726         scsi_unprobe(devp);
727         return (DDI_FAILURE);
728 }
729
730
731 /*
732  * Detach(9E)
733  */
734 static int
735 sst_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
736 {
737         int                     instance;
738         struct scsi_device      *devp;
739         struct scsi_target      *targ;
740
741         switch (cmd) {
742
743         case DDI_DETACH:
744                 return (sst_dodetach(dip));
745
746         case DDI_SUSPEND:
747                 /*
748                  * Suspend/Resume
749                  *
750                  * To process DDI_SUSPEND, we must do the following:
751                  *
752                  *      - wait until outstanding operations complete
753                  *      - block new operations
754                  *      - cancel pending timeouts
755                  *      - save h/w state
756                  *
757                  * We don't have any h/w state in this driver, so
758                  * all we really need to do is to wait for any
759                  * outstanding requests to complete.  Once completed,
760                  * we will have no pending timeouts either.
761                  */
762                 instance = ddi_get_instance(dip);
763                 targ = ddi_get_soft_state(sst_state, instance);
764                 if (targ == NULL) {
765                         return (DDI_FAILURE);
766                 }
767
768                 mutex_enter(SST_MUTEX(targ));
769                 targ->targ_suspended = 1;
770
771                 /*
772                  * Wait here till outstanding operations complete
773                  */
774                 while (targ->targ_pkt_busy) {
775                         cv_wait(&targ->targ_pkt_cv, SST_MUTEX(targ));
776                 }
777
778                 mutex_exit(SST_MUTEX(targ));
779                 return (DDI_SUCCESS);
780
781         case DDI_PM_SUSPEND:
782                 /*
783                  * Power Management suspend
784                  *
785                  * To process DDI_PM_SUSPEND, we must do the following:
786                  *
787                  *      - if busy, fail DDI_PM_SUSPEND
788                  *      - save h/w state
789                  *      - cancel pending timeouts
790                  *
791                  * Since we have no h/w state in this driver, we
792                  * only have to check our current state.  Once idle,
793                  * we have no pending timeouts.
794                  */
795                 instance = ddi_get_instance(dip);
796                 targ = ddi_get_soft_state(sst_state, instance);
797                 if (targ == NULL) {
798                         return (DDI_FAILURE);
799                 }
800
801                 mutex_enter(SST_MUTEX(targ));
802                 ASSERT(targ->targ_suspended == 0);
803
804                 /*
805                  * If we have outstanding operations, fail the
806                  * DDI_PM_SUSPEND.  The PM framework will retry after
807                  * the device has been idle for its threshold time.
808                  */
809                 if (targ->targ_pkt_busy) {
810                         mutex_exit(SST_MUTEX(targ));
811                         return (DDI_FAILURE);
812                 }
813
814                 targ->targ_pm_suspended = 1;
815
816                 mutex_exit(SST_MUTEX(targ));
817                 return (DDI_SUCCESS);
818
819
820         default:
821                 SST_LOG(0, SST_CE_DEBUG1,
822                         "sst_detach: bad cmd 0x%x\n", cmd);
823                 return (DDI_FAILURE);
824         }
825 }
826
827 /*
828  * detach(9E) DDI_DETACH handling
829  *
830  * Free resources allocated in sst_attach
831  * Note: If you implement a timeout routine in this driver, cancel it
832  *       here. Note that scsi_init_pkt is called with SLEEP_FUNC, so it
833  *       will wait for resources if none are available. Changing it to
834  *       a callback constitutes a timeout; however this detach routine
835  *       will be called only if the driver is not open, and there should be
836  *       no outstanding scsi_init_pkt's if we're closed.
837  */
838 static int
839 sst_dodetach(dev_info_t *dip)
840 {
841         int                     instance;
842         struct scsi_device      *devp;
843         struct scsi_target      *targ;
844
845         devp = (struct scsi_device *)ddi_get_driver_private(dip);
846         instance = ddi_get_instance(dip);
847         SST_LOG(devp, SST_CE_DEBUG2, "sst_detach: unit %d\n", instance);
848
849         if ((targ = ddi_get_soft_state(sst_state, instance)) == NULL) {
850                 SST_LOG(devp, CE_WARN, "No Target Struct for sst%d\n",
851                     instance);
852                 return (DDI_SUCCESS);
853         }
854
855         /*
856          * Note: Do unit-specific detaching here; e.g. shut down the device
857          */
858
859         /*
860          * Remove other data structures allocated in sst_attach()
861          */
862         cv_destroy(&targ->targ_sbuf_cv);
863         cv_destroy(&targ->targ_pkt_cv);
864         cv_destroy(&targ->targ_suspend_cv);
865
866         if (targ->targ_rqbp) {
867                 scsi_free_consistent_buf(targ->targ_rqbp);
868         }
869         if (targ->targ_rqs) {
870                 scsi_destroy_pkt(targ->targ_rqs);
871         }
872         if (targ->targ_sbufp) {
873                 freerbuf(targ->targ_sbufp);
874         }
875         pm_destroy_components(dip);
876         ddi_soft_state_free(sst_state, instance);
877         devp->sd_private = (opaque_t)0;
878         devp->sd_sense = (struct scsi_extended_sense *)0;
879         ddi_remove_minor_node(dip, NULL);
880         scsi_unprobe(devp);
881         return (DDI_SUCCESS);
882 }
883
884 /*
885  * Power(9E)
886  *
887  * The system calls power(9E) either directly or as a result of
888  * ddi_dev_is_needed(9F) when the system determines that a
889  * component's current power level needs to be changed.
890  *
891  * Since we don't control any h/w with this driver, this code
892  * only serves as a place-holder for a real power(9E) implementation
893  * when the driver is adapted for a real device.
894  */
895 static int
896 sst_power(dev_info_t *dip, int component, int level)
897 {
898         int                     instance;
899         struct scsi_device      *devp;
900         struct scsi_target      *targ;
901
902         devp = (struct scsi_device *)ddi_get_driver_private(dip);
903         instance = ddi_get_instance(dip);
904         SST_LOG(devp, SST_CE_DEBUG2, "sst_detach: unit %d\n", instance);
905
906         if ((targ = ddi_get_soft_state(sst_state, instance)) == NULL) {
907                 SST_LOG(devp, CE_WARN, "No Target Struct for sst%d\n",
908                     instance);
909                 return (DDI_FAILURE);
910         }
911
912         mutex_enter(SST_MUTEX(targ));
913
914         if (targ->targ_power_level == level) {
915                 mutex_exit(SST_MUTEX(targ));
916                 return (DDI_SUCCESS);
917         }
918
919         /*
920          * Set device's power level to 'level'
921          */
922         SST_LOG(devp, SST_CE_DEBUG2, "sst_power: %d -> %d\n",
923                 instance, targ->targ_power_level, level);
924         targ->targ_power_level = level;
925
926         mutex_exit(SST_MUTEX(targ));
927         return (DDI_SUCCESS);
928 }
929
930
931 /*
932  * Entry Points
933  */
934
935
936 /*
937  * open(9e)
938  *
939  * Called for each open(2) call on the device.
940  * Make sure the device is present and correct. Do any initialization that's
941  * needed (start it up, load media, etc).
942  * Note: Credp can be used to restrict access to root, by calling drv_priv(9f);
943  *       see also cred(9s).
944  *       Flag shows the access mode (read/write). Check it if the device is
945  *       read or write only, or has modes where this is relevant.
946  *       Otyp is an open type flag, see open.h.
947  *
948  * WARNING: Unfortunately there is a bug in SunOS 5.0 that affects driver
949  * open(). The file's reference count is incremented before the driver's
950  * open is called. This means that if another process closes (last close)
951  * the device while we're in the open routine, the close routine will not
952  * be called. If the open then fails (e.g. it's an exclusive open device),
953  * the file's ref count is decremented again, but the device is now in an
954  * inconsistent state - the driver thinks it's open (close was never called),
955  * but it's really closed. If it is an exclusive open device, it's now
956  * unusable. This is a particular problem if you want to block in the open
957  * routine until someone closes the device - close will never be called!
958  */
959 /*ARGSUSED*/
960 static int
961 sst_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
962 {
963         register dev_t                  dev = *dev_p;
964         register struct scsi_target     *targ;
965
966         SST_LOG(0, SST_CE_DEBUG2, "sst_open\n");
967
968         /*
969          * Test the open type flag
970          */
971         if (otyp != OTYP_CHR) {
972                 SST_LOG(0, SST_CE_DEBUG1,
973                     "Unsupported open type %d\n", otyp);
974                 return (EINVAL);
975         }
976
977         targ = ddi_get_soft_state(sst_state, getminor(dev));
978         if (targ == NULL) {
979                 return (ENXIO);         /* invalid minor number */
980         }
981
982         /*
983          * This is an exclusive open device; fail if it's not closed.
984          * Otherwise, set the state to open - this will lock out any
985          * other access attempts. We need the mutex here because the
986          * state must not change between the test for closed and the
987          * set to open.
988          * Note With an exclusive open device, we know that no one else
989          *      will access our data structures so strictly mutexes are
990          *      not required. However since this is an example driver, and
991          *      you may want shared access, the rest of the driver is
992          *      coded with mutexes.
993          * Note If you need shared access, it's more complex. See bst.c
994          */
995         /* SST_MUTEX(targ) returns sd_mutex in the scsi_device struct */
996         mutex_enter(SST_MUTEX(targ));           /* LOCK the targ data struct */
997         if (targ->targ_state != SST_STATE_CLOSED) {
998                 mutex_exit(SST_MUTEX(targ));
999                 return (EBUSY);
1000         }
1001         targ->targ_state = SST_STATE_OPEN;      /* lock out other accesses */
1002         mutex_exit(SST_MUTEX(targ));            /* UNLOCK targ data struct */
1003
1004         /*LINTED*/
1005         _NOTE(NO_COMPETING_THREADS_NOW);
1006
1007         /*
1008          * Test to make sure unit still is powered on and is ready
1009          * by sending the SCSI Test Unit Ready command.
1010          *
1011          * If this is, for instance, a CD-ROM, we may get an error on the
1012          * first TUR if the disk has never been accessed (a Check Condition
1013          * with extended sense data to tell us why) so do one sst_unit_ready
1014          * and ignore the result.
1015          */
1016
1017 /* Makes no sense for chg-scsi, test unit ready is done by chg-scsi */
1018 /*
1019
1020         (void) sst_unit_ready(dev);
1021
1022         if (sst_unit_ready(dev) == 0) {
1023                 SST_LOG(0, SST_CE_DEBUG1, "sst%d_open: not ready\n",
1024                     getminor(dev));
1025                 targ->targ_state = SST_STATE_CLOSED;
1026 */
1027                 /*LINTED*/
1028 /*
1029                 _NOTE(COMPETING_THREADS_NOW);
1030
1031                 return (EIO);
1032         }
1033 */
1034         /*
1035          * Do any other initalization work here, e.g. send
1036          * Mode Sense/Select commands to get the target in the
1037          * right state.
1038          */
1039         /*LINTED*/
1040         _NOTE(COMPETING_THREADS_NOW);
1041
1042         return (0);
1043 }
1044
1045
1046 /*
1047  * close(9e)
1048  * Called on final close only, i.e. the last close(2) call.
1049  * Shut down the device, and mark it closed in the unit structure.
1050  */
1051 /*ARGSUSED*/
1052 static int
1053 sst_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
1054 {
1055         register struct scsi_target     *targ;
1056
1057         SST_LOG(0, SST_CE_DEBUG2, "sst_close\n");
1058
1059         targ = ddi_get_soft_state(sst_state, getminor(dev));
1060         if (targ == NULL) {
1061                 return (ENXIO);
1062         }
1063
1064         /*LINTED*/
1065         _NOTE(NO_COMPETING_THREADS_NOW);
1066
1067         /*
1068          * Note: Close processing here, eg rewind if it's a tape;
1069          *       mark offline if removable media, etc.
1070          * WARNING: Since this is an exclusive open device, other
1071          *      accesses will be locked out until the state is set
1072          *      to closed. If you make the device shareable, you
1073          *      need to cope with the open routine being called while
1074          *      we're in the close routine, and vice-versa. You could
1075          *      serialize open/close calls with a semaphore or with
1076          *      a condition variable. See bst.c.
1077          */
1078
1079         targ->targ_state = SST_STATE_CLOSED;
1080
1081         /*LINTED*/
1082         _NOTE(COMPETING_THREADS_NOW);
1083
1084         return (0);
1085 }
1086
1087
1088 /*
1089  * getinfo(9E)
1090  *
1091  * Device Configuration Routine
1092  * link instance number (unit) with dev_info structure
1093  */
1094 /*ARGSUSED*/
1095 static int
1096 sst_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1097 {
1098         dev_t                   dev;
1099         struct scsi_target      *targ;
1100         int                     instance, error;
1101
1102         SST_LOG(0, SST_CE_DEBUG2, "sst_info\n");
1103
1104         switch (infocmd) {
1105         case DDI_INFO_DEVT2DEVINFO:
1106                 dev = (dev_t)arg;
1107                 instance = getminor(dev);
1108                 targ = ddi_get_soft_state(sst_state, instance);
1109                 if (targ == NULL)
1110                         return (DDI_FAILURE);
1111                 *result = (void *) targ->targ_devp->sd_dev;
1112                 error = DDI_SUCCESS;
1113                 break;
1114         case DDI_INFO_DEVT2INSTANCE:
1115                 *result = (void *)getminor((dev_t)arg);
1116                 error = DDI_SUCCESS;
1117                 break;
1118         default:
1119                 error = DDI_FAILURE;
1120         }
1121         return (error);
1122 }
1123
1124
1125 /*
1126  * read(9E)
1127  *
1128  * Character (raw) read and write routines, called via read(2) and
1129  * write(2). These routines perform "raw" (i.e. unbuffered) i/o.
1130  * Just and call strategy via physio. Physio(9f) will take care of
1131  * address mapping and locking, and will split the transfer if ncessary,
1132  * based on minphys, possibly calling the strategy routine multiple times.
1133  */
1134 /* ARGSUSED2 */
1135 static int
1136 sst_read(dev_t dev, struct uio *uio, cred_t *cred_p)
1137 {
1138         SST_LOG(0, SST_CE_DEBUG2, "Read\n");
1139         return (physio(sst_strategy, (struct buf *)0, dev, B_READ,
1140             minphys, uio));
1141 }
1142
1143 /*
1144  * write(9E)
1145  */
1146 /* ARGSUSED2 */
1147 static int
1148 sst_write(dev_t dev, struct uio *uio, cred_t *cred_p)
1149 {
1150         SST_LOG(0, SST_CE_DEBUG2, "Write\n");
1151         return (physio(sst_strategy, (struct buf *)0, dev, B_WRITE,
1152             minphys, uio));
1153 }
1154
1155
1156 /*
1157  * aread(9E) - asynchronous read
1158  */
1159 /*ARGSUSED2*/
1160 static int
1161 sst_aread(dev_t dev, struct aio_req *aio, cred_t *credp)
1162 {
1163         SST_LOG(0, SST_CE_DEBUG2, "aread\n");
1164         return (aphysio(sst_strategy, anocancel, dev, B_READ,
1165             minphys, aio));
1166 }
1167
1168 /*
1169  * awrite(9E) - asynchronous write
1170  */
1171 /*ARGSUSED2*/
1172 static int
1173 sst_awrite(dev_t dev, struct aio_req *aio, cred_t *credp)
1174 {
1175         SST_LOG(0, SST_CE_DEBUG2, "awrite\n");
1176         return (aphysio(sst_strategy, anocancel, dev, B_WRITE,
1177             minphys, aio));
1178 }
1179
1180
1181 /*
1182  * strategy(9E)
1183  *
1184  * Main routine for commands to the device. since this is a character
1185  * device, this routine is called only from the read/write routines above,
1186  * and sst_ioctl_cmd for the pass through ioctl (USCSICMD).
1187  * The cv_wait prevents this routine from being called simultaneously by
1188  * two threads.
1189  */
1190 static int
1191 sst_strategy(struct buf *bp)
1192 {
1193         struct scsi_target      *targ;
1194
1195         targ = ddi_get_soft_state(sst_state, getminor(bp->b_edev));
1196         if (targ == NULL) {
1197                 bp->b_resid = bp->b_bcount;
1198                 bioerror(bp, ENXIO);
1199                 biodone(bp);
1200                 return (0);
1201         }
1202
1203         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_strategy\n");
1204
1205         /*
1206          * Mark component busy so we won't get powered down
1207          * while trying to resume.
1208          */
1209         if (pm_busy_component(SST_DEVINFO(targ), 0) != DDI_SUCCESS) {
1210                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1211                     "pm_busy_component() failed\n");
1212         }
1213
1214         mutex_enter(SST_MUTEX(targ));
1215
1216         /*
1217          * if we are suspended, wait till we are resumed again
1218          */
1219         do {
1220                 /*
1221                  * Block commands while suspended via DDI_SUSPEND
1222                  */
1223                 while (targ->targ_suspended) {
1224                         cv_wait(&targ->targ_suspend_cv, SST_MUTEX(targ));
1225                 }
1226
1227                 /*
1228                  * Raise power level back to operational if needed.
1229                  * Since ddi_dev_is_needed() will result in our
1230                  * power(9E) entry point being called, we must
1231                  * drop the mutex.
1232                  */
1233                 if (targ->targ_power_level == 0) {
1234                         mutex_exit(SST_MUTEX(targ));
1235                         if (ddi_dev_is_needed(SST_DEVINFO(targ), 0, 1) !=
1236                             DDI_SUCCESS) {
1237                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1238                                     "ddi_dev_is_needed() failed\n");
1239                         }
1240                         mutex_enter(SST_MUTEX(targ));
1241                 }
1242
1243                 /*
1244                  * We need to continue to block commands if still
1245                  * suspended via DDI_PM_SUSPEND.  Because we dropped
1246                  * the mutex to call ddi_dev_is_needed(), we may be
1247                  * executing in a thread that came in after the power
1248                  * level was raised but before attach was called with
1249                  * DDI_PM_RESUME.
1250                  */
1251                 while (targ->targ_pm_suspended) {
1252                         cv_wait(&targ->targ_suspend_cv, SST_MUTEX(targ));
1253                 }
1254         } while (targ->targ_suspended || (targ->targ_power_level == 0));
1255
1256         /*
1257          * Wait for the current request to finish. We can safely release
1258          * the mutex once we have the pkt, because anyone else calling
1259          * strategy will wait here until we release it with a cv_signal.
1260          */
1261         while (targ->targ_pkt_busy) {
1262                 cv_wait(&targ->targ_pkt_cv, SST_MUTEX(targ));
1263         }
1264         targ->targ_pkt_busy =  1;               /* mark busy */
1265         mutex_exit(SST_MUTEX(targ));
1266
1267         if (((targ->targ_pkt = sst_make_cmd(targ, bp,
1268             (struct scsi_pkt *)NULL))) == NULL) {
1269                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1270                     "Unable to create SCSI command\n");
1271                 bp->b_resid = bp->b_bcount;
1272                 bioerror(bp, EIO);
1273                 biodone(bp);
1274                 pm_idle_component(SST_DEVINFO(targ), 0);
1275                 return (0);
1276         }
1277
1278         bp->b_resid = 0;
1279
1280         SST_DUMP_CDB(targ, targ->targ_pkt, CDB_GROUP0);
1281
1282         if (scsi_transport(targ->targ_pkt) != TRAN_ACCEPT) {
1283                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1284                     "Command transport failed\n");
1285                 bp->b_resid = bp->b_bcount;
1286                 bioerror(bp, EIO);
1287                 biodone(bp);
1288                 pm_idle_component(SST_DEVINFO(targ), 0);
1289         }
1290
1291         return (0);
1292 }
1293
1294
1295 /*
1296  * ioctl calls.
1297  * Ioctls are device specific. Three are provided here as examples:
1298  * SSTIOC_READY: Send a Test Unit Ready command to the device. This fills
1299  *               in the uscsi_cmd structure in the unit struct. The actual
1300  *               cdb to be sent to the device is created here; the union
1301  *               scsi_cdb is defined in /usr/include/sys/scsi/impl/commands.h.
1302  * SST_ERRLEV:  Change the error reporting level.
1303  * USCSICMD:    Pass through. Send the user-supplied command to the device. Very
1304  *              little checking is done - it's left up to the caller to supply
1305  *              a valid cdb.
1306  */
1307 /* ARGSUSED3 */
1308 static int
1309 sst_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1310         cred_t *cred_p, int *rval_p)
1311 {
1312         int                     err = 0;
1313         struct scsi_target      *targ;
1314         struct uscsi_cmd        uscsi_cmd;
1315         struct uscsi_cmd        *ucmd = &uscsi_cmd;
1316         char                    cmdblk[CDB_GROUP0];
1317
1318 #ifdef _MULTI_DATAMODEL
1319         /*
1320          * For use when a 32-bit app makes an ioctl into a 64-bit driver
1321          */
1322         struct sst_uscsi_cmd32  uscsi_cmd32;
1323         struct sst_uscsi_cmd32  *ucmd32 = &uscsi_cmd32;
1324         model_t                 model;
1325 #endif /* _MULTI_DATAMODEL */
1326
1327
1328         targ = ddi_get_soft_state(sst_state, getminor(dev));
1329         if (targ == NULL) {
1330                 return (ENXIO);         /* invalid minor number */
1331         }
1332
1333         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_ioctl: cmd = 0x%x\n", cmd);
1334
1335         bzero(ucmd, sizeof (struct uscsi_cmd));
1336
1337         switch (cmd) {
1338         case SSTIOC_READY:              /* Send a Test Unit Ready command */
1339                 ucmd->uscsi_bufaddr = 0;
1340                 ucmd->uscsi_buflen = 0;
1341                 bzero(cmdblk, CDB_GROUP0);
1342                 cmdblk[0] = (char)SCMD_TEST_UNIT_READY;
1343                 ucmd->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_WRITE;
1344                 ucmd->uscsi_cdb = cmdblk;
1345                 ucmd->uscsi_cdblen = CDB_GROUP0;
1346                 ucmd->uscsi_timeout = sst_io_time;
1347
1348                 err = sst_ioctl_cmd(dev, ucmd,
1349                         UIO_SYSSPACE, UIO_SYSSPACE, mode);
1350                 break;
1351
1352         case SSTIOC_ERRLEV:             /* Set the error reporting level */
1353                 if (ddi_copyin((void *)arg, &sst_error_reporting,
1354                     sizeof (int32_t), mode)) {
1355                         return (EFAULT);
1356                 }
1357                 break;
1358
1359         case USCSICMD:
1360                 /*
1361                  * Run a generic ucsi.h command.
1362                  */
1363
1364 #if 0 /* allow non-root to do this */
1365                 /*
1366                  * Check root permissions
1367                  */
1368                 if (drv_priv(cred_p) != 0) {
1369                         return (EPERM);
1370                 }
1371 #endif
1372
1373 #ifdef _MULTI_DATAMODEL
1374                 switch (model = ddi_model_convert_from(mode & FMODELS)) {
1375                 case DDI_MODEL_ILP32:
1376                 {
1377                         if (ddi_copyin((void *)arg, ucmd32,
1378                             sizeof (*ucmd32), mode)) {
1379                                 return (EFAULT);
1380                         }
1381                         /*
1382                          * Convert 32-bit ILP32 application's uscsi cmd
1383                          * into 64-bit LP64 equivalent for internal use
1384                          */
1385                         sst_uscsi_cmd32touscsi_cmd(ucmd32, ucmd);
1386                         break;
1387                 }
1388                 case DDI_MODEL_NONE:
1389                         if (ddi_copyin((void *)arg, ucmd,
1390                             sizeof (*ucmd), mode)) {
1391                                 return (EFAULT);
1392                         }
1393                         break;
1394                 }
1395
1396 #else /* ! _MULTI_DATAMODEL */
1397                 if (ddi_copyin((void *)arg, ucmd,
1398                     sizeof (*ucmd), mode)) {
1399                         return (EFAULT);
1400                 }
1401 #endif /* _MULTI_DATAMODEL */
1402
1403                 err = sst_ioctl_cmd(dev, ucmd,
1404                         UIO_USERSPACE, UIO_USERSPACE, mode);
1405
1406 #ifdef _MULTI_DATAMODEL
1407                 switch (model) {
1408                 case DDI_MODEL_ILP32:
1409                         /*
1410                          * Convert LP64 back to IPL32 before copyout
1411                          */
1412                         sst_uscsi_cmdtouscsi_cmd32(ucmd, ucmd32);
1413
1414                         if (ddi_copyout(ucmd32, (void *)arg,
1415                             sizeof (*ucmd32), mode)) {
1416                                 return (EFAULT);
1417                         }
1418                         break;
1419
1420                 case DDI_MODEL_NONE:
1421                         if (ddi_copyout(ucmd, (void *)arg,
1422                             sizeof (*ucmd), mode)) {
1423                                 return (EFAULT);
1424                         }
1425                         break;
1426                 }
1427 #else /* ! _MULTI_DATAMODE */
1428                 if (ddi_copyout(ucmd, (void *)arg,
1429                     sizeof (*ucmd), mode)) {
1430                         return (EFAULT);
1431                 }
1432 #endif /* _MULTI_DATAMODE */
1433                 break;
1434
1435
1436         default:
1437                 err = ENOTTY;
1438                 break;
1439         }
1440         return (err);
1441 }
1442
1443
1444 /*
1445  * Run a command for user (from sst_ioctl) or from someone else in the driver.
1446  *
1447  * cdbspace is for address space of cdb; dataspace is for address space
1448  * of the buffer - user or kernel.
1449  */
1450 static int
1451 sst_ioctl_cmd(dev_t dev, struct uscsi_cmd *scmd,
1452     enum uio_seg cdbspace, enum uio_seg dataspace,
1453     int mode)
1454 {
1455         caddr_t                 cdb, user_cdbp;
1456         int                     err, rw;
1457         struct buf              *bp;
1458         struct scsi_target      *targ;
1459         int                     flag;
1460
1461         targ = ddi_get_soft_state(sst_state, getminor(dev));
1462         if (targ == NULL) {
1463                 return (ENXIO);
1464         }
1465
1466         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_ioctl_cmd\n");
1467
1468         /*
1469          * The uscsi structure itself is already in kernel space (copied
1470          * in by sst_ioctl, or declared there by our caller); but
1471          * we need to copy in the cdb here.
1472          */
1473         cdb = kmem_zalloc((size_t)scmd->uscsi_cdblen, KM_SLEEP);
1474         flag = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : mode;
1475         if (ddi_copyin(scmd->uscsi_cdb, cdb,
1476             (u_int)scmd->uscsi_cdblen, flag)) {
1477                 kmem_free(cdb, (size_t)scmd->uscsi_cdblen);
1478                 return (EFAULT);
1479         }
1480
1481         /*
1482          * The cdb pointer in the structure passed by the user is pointing
1483          * to user space. We've just copied the cdb into a local buffer,
1484          * so point uscsi_cdb to it now. We'll restore the user's pointer
1485          * at the end.
1486          */
1487         user_cdbp = scmd->uscsi_cdb;    /* save the user's pointer */
1488         scmd->uscsi_cdb = cdb;          /* point to the local cdb buffer */
1489         rw = (scmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE;
1490
1491         /*
1492          * Get the 'special command' buffer.
1493          * First lock the targ struct; if the buffer's busy, wait for
1494          * it to become free. Once we get the buffer, mark it busy to
1495          * lock out other requests for it.
1496          * Note cv_wait will release the mutex, allowing other parts
1497          *      of the driver to acquire it.
1498          * Note Once we have the special buffer, we can safely release
1499          *      the mutex; the buffer is now busy and another thread will
1500          *      block in the cv_wait until we release it. All the code
1501          *      from here until we unset the busy flag is non-reentrant,
1502          *      including the physio/strategy/start/callback/done thread.
1503          */
1504         mutex_enter(SST_MUTEX(targ));
1505         while (targ->targ_sbuf_busy) {
1506                 cv_wait(&targ->targ_sbuf_cv, SST_MUTEX(targ));
1507         }
1508         targ->targ_sbuf_busy = 1;
1509         mutex_exit(SST_MUTEX(targ));
1510
1511         bp = targ->targ_sbufp;
1512
1513         if (scmd->uscsi_buflen) {
1514                 /*
1515                  * We're sending/receiving data; create a uio structure and
1516                  * call physio to do the right things.
1517                  */
1518                 auto struct     iovec   aiov;
1519                 auto struct     uio     auio;
1520                 register struct uio     *uio = &auio;
1521
1522                 bzero(&auio, sizeof (struct uio));
1523                 bzero(&aiov, sizeof (struct iovec));
1524                 aiov.iov_base = scmd->uscsi_bufaddr;
1525                 aiov.iov_len = scmd->uscsi_buflen;
1526                 uio->uio_iov = &aiov;
1527
1528                 uio->uio_iovcnt = 1;
1529                 uio->uio_resid = scmd->uscsi_buflen;
1530                 uio->uio_segflg = dataspace;
1531                 uio->uio_offset = 0;
1532                 uio->uio_fmode = 0;
1533
1534                 /*
1535                  * Let physio do the rest...
1536                  */
1537                 bp->b_private = (void *) scmd;
1538                 err = physio(sst_strategy, bp, dev, rw, minphys, uio);
1539         } else {
1540                 /*
1541                  * No data transfer, we can call sst_strategy directly
1542                  */
1543                 bp->b_private = (void *) scmd;
1544                 bp->b_flags = B_BUSY | rw;
1545                 bp->b_edev = dev;
1546                 bp->b_bcount = bp->b_blkno = 0;
1547                 (void) sst_strategy(bp);
1548                 err = biowait(bp);
1549         }
1550
1551         /*
1552          * get the status block, if any, and
1553          * release any resources that we had.
1554          */
1555         scmd->uscsi_status = 0;
1556         scmd->uscsi_status = SCBP_C(targ->targ_pkt);
1557
1558         /*
1559          * Lock the targ struct. Clearing the 'busy' flag and signalling
1560          * must be made atomic.
1561          */
1562         mutex_enter(SST_MUTEX(targ));           /* LOCK the targ struct */
1563         if (targ->targ_pkt != NULL)
1564                 scsi_destroy_pkt(targ->targ_pkt);
1565         targ->targ_sbuf_busy = 0;
1566         cv_signal(&targ->targ_sbuf_cv);         /* release the special buffer */
1567         targ->targ_pkt_busy = 0;
1568         cv_signal(&targ->targ_pkt_cv);          /* release the packet */
1569         mutex_exit(SST_MUTEX(targ));            /* UNLOCK the targ struct */
1570
1571         kmem_free(scmd->uscsi_cdb, (size_t)scmd->uscsi_cdblen);
1572         scmd->uscsi_cdb = user_cdbp;    /* restore the user's pointer */
1573         return (err);
1574 }
1575
1576
1577 /*
1578  * Check to see if the unit will respond to a Test Unit Ready
1579  * Returns true or false
1580  */
1581 static int
1582 sst_unit_ready(dev_t dev)
1583 {
1584         auto struct uscsi_cmd scmd, *com = &scmd;
1585         auto char       cmdblk[CDB_GROUP0];
1586         auto int        err;
1587
1588         com->uscsi_bufaddr = 0;
1589         com->uscsi_buflen = 0;
1590         bzero(cmdblk, CDB_GROUP0);
1591         cmdblk[0] = (char)SCMD_TEST_UNIT_READY;
1592         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_WRITE;
1593         com->uscsi_cdb = cmdblk;
1594         com->uscsi_cdblen = CDB_GROUP0;
1595         com->uscsi_timeout = sst_io_time;
1596
1597         if (err = sst_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 0)) {
1598                 SST_LOG(0, SST_CE_DEBUG1, "sst_unit_ready failed: 0x%x\n", err);
1599                 return (0);
1600         }
1601         return (1);
1602 }
1603
1604
1605 /*
1606  * Done with a command.
1607  * Start the next command and then call biodone() to tell physio or
1608  * sst_ioctl_cmd that this i/o has completed.
1609  *
1610  */
1611 static void
1612 sst_done(struct scsi_target *targ, register struct buf *bp)
1613 {
1614         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_done\n");
1615
1616         /*
1617          * Lock the targ struct; we don't want targ_pkt to change
1618          * between taking a copy of it and zeroing it.
1619          */
1620         mutex_enter(SST_MUTEX(targ));
1621
1622         /*
1623          * For regular commands (i.e. not using the special buffer),
1624          * free resources and clear the current request. Sst_ioctl_cmd
1625          * will do its own freeing for special commands. We need the lock
1626          * here because targ_pkt must not change during the operations.
1627          */
1628         if (bp != targ->targ_sbufp) {
1629                 scsi_destroy_pkt(targ->targ_pkt);
1630                 targ->targ_pkt_busy = 0;
1631                 cv_signal(&targ->targ_pkt_cv);
1632         }
1633
1634         mutex_exit(SST_MUTEX(targ));            /* UNLOCK the targ struct */
1635
1636         /*
1637          * Tell interested parties that the i/o is done
1638          */
1639         biodone(bp);
1640         pm_idle_component(SST_DEVINFO(targ), 0);
1641 }
1642
1643
1644 /*
1645  * Allocate resources for a SCSI command - call scsi_init_pkt to get
1646  * a packet and allocate DVMA resources, and create the SCSI CDB.
1647  *
1648  * Also used to continue an in-progress packet; simply calls scsi_init_pkt
1649  * to update the DMA resources to the next chunk, and re-creates the
1650  * SCSI command.
1651  *
1652  * Note: There are differences in the DMA subsystem between the SPARC
1653  *      and x86 platforms. For example, on the x86 platform, DMA uses
1654  *      physical rather than virtual addresses as a result the DMA buffer
1655  *      may not be contiguous.
1656  *
1657  *      Because of these differences, the system may not be able to fully
1658  *      satisfy a DMA allocation request from the target driver with resources
1659  *      attached to one packet. As a solution, the scsi_init_pkt() routine
1660  *      should be called with the PKT_DMA _PARTIAL flag to allow the system
1661  *      to break up the DMA request. If a single DMA allocation cannot be
1662  *      done, the HBA driver sets the pkt_resid in the scsi_pkt structure
1663  *      to the number of bytes of DMA resources it was able to  allocate.
1664  *      The target driver needs to modify the SCSI command it places into
1665  *      the packet for transport to request the proper amount of data specified
1666  *      in pkt_resid. When the packet's completion routine is called, the
1667  *      process is repeated again.
1668  *
1669  */
1670 static struct scsi_pkt *
1671 sst_make_cmd(struct scsi_target *targ, struct buf *bp, struct scsi_pkt *pkt)
1672 {
1673         struct uscsi_cmd *scmd = (struct uscsi_cmd *)bp->b_private;
1674         int                     tval, blkcnt;
1675         u_int                   flags;
1676         int                     pktalloc = 0;
1677         struct sst_private      *sstprivp;
1678
1679         SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
1680             "sst_make_cmd: block %d, count=%d\n", bp->b_blkno, bp->b_bcount);
1681
1682         /*
1683          * If called with pkt == NULL, this is first time, and we
1684          * must allocate and initialize the packet
1685          */
1686         if (pkt == (struct scsi_pkt *)NULL)
1687                 pktalloc = 1;
1688
1689         flags = 0;
1690         blkcnt = bp->b_bcount >> DEV_BSHIFT;
1691
1692         if (bp != targ->targ_sbufp) {
1693                 /*
1694                  * Allocate a packet.
1695                  * ROUTE(targ) is shorthand for sd_address
1696                  * Note that if auto request sensing is enabled, we
1697                  * specify the status length to hold sense data if needed
1698                  */
1699                 if (pktalloc) {
1700                         pkt = scsi_init_pkt(ROUTE(targ),
1701                             (struct scsi_pkt *)NULL, bp,
1702                             (bp->b_blkno + blkcnt > 0x1FFFFF || blkcnt > 0xFF) ?
1703                                 CDB_GROUP1 : CDB_GROUP0,
1704                             targ->targ_arq ?
1705                             sizeof (struct scsi_arq_status) : 1,
1706                             sizeof (struct sst_private), PKT_DMA_PARTIAL,
1707                             SLEEP_FUNC, 0);
1708                         if (pkt == (struct scsi_pkt *)0) {
1709                                 return (NULL);
1710                         }
1711                 } else {
1712                         /*
1713                          * Here we're simply calling scsi_init_pkt() for the
1714                          * next section of DMA resource for the current
1715                          * I/O request (in bp); we needn't worry about
1716                          * the associated lengths, or possible failure,
1717                          * since scsi_init_pkt() is just moving the DMA win
1718                          */
1719                         pkt = scsi_init_pkt(ROUTE(targ), pkt, bp, 0,
1720                             targ->targ_arq ?
1721                             sizeof (struct scsi_arq_status) : 1,
1722                             sizeof (struct sst_private),
1723                             PKT_DMA_PARTIAL, NULL_FUNC, 0);
1724                 }
1725
1726                 SST_LOG(targ->targ_devp, SST_CE_DEBUG3,
1727                     "sst_make_cmd: pkt_resid: %d\n", pkt->pkt_resid);
1728
1729                 sstprivp = (struct sst_private *)pkt->pkt_private;
1730
1731                 if (pktalloc) {
1732                         sstprivp->priv_bp = bp;
1733                         sstprivp->priv_amtdone = 0;
1734                         sstprivp->priv_amt = bp->b_bcount - pkt->pkt_resid;
1735                 }
1736
1737                 sst_fill_cdb(pkt, targ, bp, flags);
1738                 pkt->pkt_flags = flags;
1739                 tval = sst_io_time;
1740         } else {
1741                 /*
1742                  * All special command come through sst_ioctl_cmd, which
1743                  * uses the uscsi interface. Just need to get the CDB
1744                  * from scmd and plug it in. Still call scsi_setup_cdb because
1745                  * it fills in some of the pkt field for us. Its cdb
1746                  * manipulations will be overwritten by the bcopy.
1747                  */
1748                 if (scmd->uscsi_flags & USCSI_SILENT)
1749                         flags |= FLAG_SILENT;
1750                 if (scmd->uscsi_flags & USCSI_DIAGNOSE)
1751                         flags |= FLAG_DIAGNOSE;
1752                 if (scmd->uscsi_flags & USCSI_ISOLATE)
1753                         flags |= FLAG_ISOLATE;
1754                 if (scmd->uscsi_flags & USCSI_NODISCON)
1755                         flags |= FLAG_NODISCON;
1756                 if (scmd->uscsi_flags & USCSI_NOPARITY)
1757                         flags |= FLAG_NOPARITY;
1758
1759                 if (pktalloc) {
1760                         pkt = scsi_init_pkt(ROUTE(targ),
1761                             (struct scsi_pkt *)NULL,
1762                             bp->b_bcount ? bp : 0,
1763                             scmd->uscsi_cdblen,
1764                             targ->targ_arq ?
1765                             sizeof (struct scsi_arq_status) : 1,
1766                             sizeof (struct sst_private), PKT_DMA_PARTIAL,
1767                             SLEEP_FUNC, 0);
1768                         if (!pkt) {
1769                                 return (NULL);
1770                         }
1771                 } else {
1772                         pkt = scsi_init_pkt(ROUTE(targ), pkt,
1773                             bp->b_bcount ? bp : 0, 0,
1774                             targ->targ_arq ?
1775                             sizeof (struct scsi_arq_status) : 1,
1776                             sizeof (struct sst_private),
1777                             PKT_DMA_PARTIAL, NULL_FUNC, 0);
1778                 }
1779
1780                 sstprivp = (struct sst_private *)pkt->pkt_private;
1781                 if (pktalloc) {
1782                         sstprivp->priv_bp = bp;
1783                         sstprivp->priv_amtdone = 0;
1784                         sstprivp->priv_amt = bp->b_bcount - pkt->pkt_resid;
1785                 }
1786
1787                 (void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
1788                         scmd->uscsi_cdb[0], 0, 0, 0);
1789
1790                 bcopy(scmd->uscsi_cdb, pkt->pkt_cdbp,
1791                     scmd->uscsi_cdblen);
1792                 tval = scmd->uscsi_timeout;
1793
1794                 /* a timeout of 0 (ie. no timeout) is bad practice */
1795                 if (tval == 0) {
1796                         tval = sst_io_time;
1797                 }
1798         }
1799
1800         pkt->pkt_comp = sst_callback;
1801         pkt->pkt_time = tval;
1802
1803 #ifdef SST_DEBUG
1804         if (sst_debug > 2) {
1805                 hex_print("sst_make_cmd, CDB", pkt->pkt_cdbp, 6);
1806         }
1807 #endif SST_DEBUG
1808
1809         return (pkt);
1810 }
1811
1812
1813 /*
1814  * Stuff CDB with SCSI command for current I/O request
1815  */
1816
1817 static void
1818 sst_fill_cdb(struct scsi_pkt *pkt, struct scsi_target *targ, struct buf *bp,
1819     u_int flags)
1820 {
1821         struct sst_private *sstprivp;
1822         u_int blkno, len;
1823         u_int com;
1824
1825         if ((scsi_options & SCSI_OPTIONS_DR) == 0)
1826                 flags |= FLAG_NODISCON;
1827
1828         sstprivp = (struct sst_private *)(pkt->pkt_private);
1829
1830         blkno = bp->b_blkno + (sstprivp->priv_amtdone >> DEV_BSHIFT);
1831         /* use result of scsi_init_pkt() as I/O length */
1832         len = sstprivp->priv_amt; /* pkt->pkt_resid; */
1833
1834         SST_LOG(targ->targ_devp, SST_CE_DEBUG3,
1835             "sst_fill_cdb: blkno %x len %x\n", blkno, len);
1836         /*
1837          * Note: We use the group 0 Read/Write commands here
1838          *      unless the starting block number or blocks
1839          *      requested is too large.
1840          *      If your device needs different commands for normal
1841          *      data transfer (e.g. Send/Receive, read/write buffer),
1842          *      change it here.
1843          */
1844
1845         if ((blkno + (len >> DEV_BSHIFT) > 0x1FFFFF) ||
1846             ((len >> DEV_BSHIFT) > 0xFF)) {
1847                 SST_LOG(targ->targ_devp, SST_CE_DEBUG3,
1848                     "sst_make_cmd: using g1 command\n");
1849                 com = (bp->b_flags & B_READ) ? SCMD_READ_G1 : SCMD_WRITE_G1;
1850         } else {
1851                 com = (bp->b_flags & B_READ) ? SCMD_READ : SCMD_WRITE;
1852         }
1853
1854         /*
1855          * Note: The CDB creation differs for sequential and
1856          *       direct access devices. We do both here, but
1857          *       you should need only one. For sequential access,
1858          *       we assume a fixed 512 byte block size. If you
1859          *       have a variable block size device, ask for the
1860          *       actual number of bytes wanted, and change the last
1861          *       parameter, 'fixbit' (1) to zero.
1862          */
1863         if (targ->targ_devp->sd_inq->inq_dtype == DTYPE_SEQUENTIAL) {
1864                 blkno = 0;
1865         }
1866
1867         (void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
1868                                 com, blkno, len >> DEV_BSHIFT, 0);
1869         pkt->pkt_flags = flags;
1870
1871         if (targ->targ_devp->sd_inq->inq_dtype == DTYPE_SEQUENTIAL) {
1872                 ((union scsi_cdb *)(pkt->pkt_cdbp))->t_code = 1;
1873         }
1874 }
1875
1876
1877 /*
1878  * -----------------------------------------------------------------------------
1879  * Interrupt Service Routines
1880  */
1881
1882 /*
1883  * Restart a command - the device was either busy or not ready
1884  */
1885 static void
1886 sst_restart(caddr_t arg)
1887 {
1888         struct scsi_target      *targ = (struct scsi_target *)arg;
1889         struct scsi_pkt         *pkt;
1890         struct buf              *bp;
1891
1892         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_restart\n");
1893
1894         /*
1895          * No need to lock the targ structure because any other threads
1896          * will wait in sst_strategy for the packet.
1897          */
1898         pkt = targ->targ_pkt;
1899         bp = ((struct sst_private *)pkt->pkt_private)->priv_bp;
1900
1901         if (bp) {
1902                 struct scsi_pkt *pkt = targ->targ_pkt;
1903
1904                 if (pkt->pkt_flags & FLAG_SENSING) {
1905                         pkt = targ->targ_rqs;
1906                 }
1907
1908                 SST_DUMP_CDB(targ, targ->targ_pkt, CDB_GROUP0);
1909
1910                 if (scsi_transport(pkt) != TRAN_ACCEPT) {
1911                         bp->b_resid = bp->b_bcount;
1912                         bioerror(bp, ENXIO);
1913                         sst_done(targ, bp);
1914                 }
1915         }
1916 }
1917
1918
1919 /*
1920  * Command completion processing, called by the host adapter driver
1921  * when it's done with the command.
1922  * No need for mutexes in this routine - there's only one active command
1923  * at a time, anyone else wanting to send a command will wait in strategy
1924  * until we call sst_done.
1925  */
1926 static void
1927 sst_callback(struct scsi_pkt *pkt)
1928 {
1929         struct scsi_target      *targ;
1930         struct buf              *bp;
1931         int                     action;
1932         struct sst_private      *sstprivp;
1933
1934         sstprivp = (struct sst_private *)pkt->pkt_private;
1935         bp = sstprivp->priv_bp;
1936         targ = ddi_get_soft_state(sst_state, getminor(bp->b_edev));
1937
1938         SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
1939         "sst_callback: pkt_reason = 0x%x, pkt_flags = 0x%x, pkt_state = 0x%x\n",
1940             pkt->pkt_reason, pkt->pkt_flags, pkt->pkt_state);
1941
1942         mutex_enter(SST_MUTEX(targ));
1943
1944         if (pkt->pkt_reason != CMD_CMPLT) {
1945                 /*
1946                  * The command did not complete. Retry if possible.
1947                  */
1948                 action = sst_handle_incomplete(targ);
1949         } else if (targ->targ_arq && pkt->pkt_state & STATE_ARQ_DONE) {
1950                 /*
1951                  * The auto-rqsense happened, and the packet has a
1952                  * filled-in scsi_arq_status structure, pointed to by
1953                  * pkt_scbp.
1954                  */
1955                 action = sst_handle_arq(pkt, targ, bp);
1956         } else if (pkt->pkt_flags & FLAG_SENSING) {
1957                 /*
1958                  * We were running a REQUEST SENSE. Decode the
1959                  * sense data and decide what to do next.
1960                  */
1961                 pkt = targ->targ_pkt;   /* get the pkt for the orig command */
1962                 pkt->pkt_flags &= ~FLAG_SENSING;
1963                 action = sst_handle_sense(targ, bp);
1964         } else {
1965                 /*
1966                  * Command completed and we're not getting sense. Check
1967                  * for errors and decide what to do next.
1968                  */
1969                 action = sst_check_error(targ, bp);
1970         }
1971         mutex_exit(SST_MUTEX(targ));
1972
1973         switch (action) {
1974         case QUE_SENSE:
1975                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Getting Sense\n");
1976                 pkt->pkt_flags |= FLAG_SENSING;
1977                 ((struct sst_private *)targ->targ_rqs->pkt_private)->
1978                                                                 priv_bp = bp;
1979                 bzero(targ->targ_devp->sd_sense, SENSE_LENGTH);
1980                 SST_DUMP_CDB(targ, targ->targ_rqs, CDB_GROUP0);
1981                 if (scsi_transport(targ->targ_rqs) == TRAN_ACCEPT) {
1982                         break;
1983                 }
1984                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1985                     "Request Sense transport failed\n");
1986                 /*FALLTHROUGH*/
1987         case COMMAND_DONE_ERROR:
1988                 bp->b_resid = bp->b_bcount;
1989                 bioerror(bp, ENXIO);
1990                 /*FALLTHROUGH*/
1991         case COMMAND_DONE:
1992                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Command Done\n");
1993                 sst_done(targ, bp);
1994                 break;
1995
1996         case QUE_COMMAND:
1997                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Retrying Command\n");
1998                 SST_DUMP_CDB(targ, targ->targ_pkt, CDB_GROUP0);
1999                 if (scsi_transport(targ->targ_pkt) != TRAN_ACCEPT) {
2000                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2001                             "Retry transport failed\n");
2002                         bp->b_resid = bp->b_bcount;
2003                         bioerror(bp, ENXIO);
2004                         sst_done(targ, bp);
2005                         return;
2006                 }
2007                 break;
2008         case JUST_RETURN:
2009                 break;
2010
2011         case CONTINUE_PKT:
2012                 /*
2013                  * Back from a chunk of a split-up bp.  Do next chunk or
2014                  * finish up.
2015                  */
2016                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Continuing packet\n");
2017                 (void) sst_make_cmd(targ, bp, pkt);
2018                 SST_DUMP_CDB(targ, pkt, CDB_GROUP0);
2019                 if (scsi_transport(pkt) != TRAN_ACCEPT) {
2020                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2021                             "Command transport failedn");
2022                         bp->b_resid = bp->b_bcount;
2023                         bioerror(bp, EIO);
2024                         biodone(bp);
2025                         pm_idle_component(SST_DEVINFO(targ), 0);
2026                 }
2027         }
2028
2029 }
2030
2031
2032 /*
2033  * Incomplete command handling. Figure out what to do based on
2034  * how far the command did get.
2035  */
2036 static int
2037 sst_handle_incomplete(struct scsi_target *targ)
2038 {
2039         register int    rval = COMMAND_DONE_ERROR;
2040         register struct scsi_pkt        *pkt = targ->targ_pkt;
2041
2042         if (!targ->targ_arq && pkt->pkt_flags & FLAG_SENSING) {
2043                 pkt = targ->targ_rqs;
2044         }
2045
2046         /*
2047          * The target may still be running the  command,
2048          * so try and reset it, to get it into a known state.
2049          * Note: This is forcible, there may be a more polite
2050          *       method for your device.
2051          */
2052         if ((pkt->pkt_statistics &
2053             (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
2054                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1, "Aborting Command\n");
2055                 mutex_exit(SST_MUTEX(targ));
2056                 if (!(scsi_abort(ROUTE(targ), (struct scsi_pkt *)0))) {
2057                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2058                             "Resetting Target\n");
2059                         if (!(scsi_reset(ROUTE(targ), RESET_TARGET))) {
2060                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2061                                     "Resetting SCSI Bus\n");
2062                                 if (!scsi_reset(ROUTE(targ), RESET_ALL)) {
2063                                         sst_log(targ->targ_devp, CE_WARN,
2064                                             "SCSI bus reset failed\n");
2065                                 }
2066                                 mutex_enter(SST_MUTEX(targ));
2067                                 return (COMMAND_DONE_ERROR);
2068                         }
2069                 }
2070                 mutex_enter(SST_MUTEX(targ));
2071         }
2072
2073         /*
2074          * If we were running a request sense, try it again if possible.
2075          * Some devices can handle retries, others will not.
2076          */
2077         if (pkt->pkt_flags & FLAG_SENSING) {
2078                 if (targ->targ_retry_ct++ < sst_retry_count) {
2079                         rval = QUE_SENSE;
2080                 }
2081         } else if (targ->targ_retry_ct++ < sst_retry_count) {
2082                 rval = QUE_COMMAND;
2083         } else {
2084                 rval = COMMAND_DONE_ERROR;
2085         }
2086
2087         SST_LOG(targ->targ_devp, SST_CE_DEBUG1, "Cmd incomplete, %s\n",
2088             (rval == COMMAND_DONE_ERROR) ? "giving up" : "retrying");
2089
2090         return (rval);
2091 }
2092
2093
2094 /*
2095  * Decode sense data
2096  */
2097 static int
2098 sst_handle_sense(struct scsi_target *targ, struct buf *bp)
2099 {
2100         struct scsi_pkt *rqpkt = targ->targ_rqs;
2101         register        rval = COMMAND_DONE_ERROR;
2102         int             level, amt;
2103
2104
2105         if (SCBP(rqpkt)->sts_busy) {
2106                 if (targ->targ_retry_ct++ < sst_retry_count) {
2107                         (void) timeout(sst_restart, (caddr_t)targ,
2108                             SST_BSY_TIMEOUT);
2109                         rval = JUST_RETURN;
2110                 }
2111                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1, "Target Busy, %s\n",
2112                     (rval == JUST_RETURN) ? "restarting" : "giving up");
2113                 return (rval);
2114         }
2115
2116         if (SCBP(rqpkt)->sts_chk) {
2117                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2118                     "Check Condition on Request Sense!\n");
2119                 return (rval);
2120         }
2121
2122         amt = SENSE_LENGTH - rqpkt->pkt_resid;
2123         if ((rqpkt->pkt_state & STATE_XFERRED_DATA) == 0 || amt == 0) {
2124                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "no sense data\n");
2125                 return (rval);
2126         }
2127
2128         /*
2129          * Now, check to see whether we got enough sense data to make any
2130          * sense out if it (heh-heh).
2131          */
2132         if (amt < SUN_MIN_SENSE_LENGTH) {
2133                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2134                     "not enough sense data\n");
2135                 return (rval);
2136         }
2137
2138         if (sst_debug > 2) {
2139                 hex_print("sst Sense Data", targ->targ_devp->sd_sense,
2140                     SENSE_LENGTH);
2141         }
2142
2143         /*
2144          * Decode the sense data
2145          * Note: We only looking at the sense key here. Most devices
2146          *       have unique additional sense codes & qualifiers, so
2147          *       it's often more useful to look at them instead.
2148          *
2149          */
2150         switch (targ->targ_devp->sd_sense->es_key) {
2151         case KEY_NOT_READY:
2152                 /*
2153                  * If we get a not-ready indication, wait a bit and
2154                  * try it again, unless this is a special command with
2155                  * the 'fail on error' (FLAG_DIAGNOSE) option set.
2156                  */
2157                 if ((bp == targ->targ_sbufp) &&
2158                     (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2159                         rval = COMMAND_DONE_ERROR;
2160                         level = SCSI_ERR_FATAL;
2161                 } else if (targ->targ_retry_ct++ < sst_retry_count) {
2162                         (void) timeout(sst_restart, (caddr_t)targ,
2163                             SST_BSY_TIMEOUT);
2164                         rval = JUST_RETURN;
2165                         level = SCSI_ERR_RETRYABLE;
2166                 } else {
2167                         rval = COMMAND_DONE_ERROR;
2168                         level = SCSI_ERR_FATAL;
2169                 }
2170                 break;
2171
2172         case KEY_ABORTED_COMMAND:
2173         case KEY_UNIT_ATTENTION:
2174                 rval = QUE_COMMAND;
2175                 level = SCSI_ERR_INFO;
2176                 break;
2177         case KEY_RECOVERABLE_ERROR:
2178         case KEY_NO_SENSE:
2179                 rval = COMMAND_DONE;
2180                 level = SCSI_ERR_RECOVERED;
2181                 break;
2182         case KEY_HARDWARE_ERROR:
2183         case KEY_MEDIUM_ERROR:
2184         case KEY_MISCOMPARE:
2185         case KEY_VOLUME_OVERFLOW:
2186         case KEY_WRITE_PROTECT:
2187         case KEY_BLANK_CHECK:
2188         case KEY_ILLEGAL_REQUEST:
2189         default:
2190                 rval = COMMAND_DONE_ERROR;
2191                 level = SCSI_ERR_FATAL;
2192                 break;
2193         }
2194
2195         /*
2196          * If this was for a special command, check the options
2197          */
2198         if (bp == targ->targ_sbufp) {
2199                 if ((rval == QUE_COMMAND) &&
2200                     (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2201                         rval = COMMAND_DONE_ERROR;
2202                 }
2203                 if (((targ->targ_pkt->pkt_flags & FLAG_SILENT) == 0) ||
2204                     sst_debug) {
2205                         scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2206                             level, bp->b_blkno, 0, sst_cmds,
2207                             targ->targ_devp->sd_sense);
2208                 }
2209         } else if ((level >= sst_error_reporting) || sst_debug) {
2210                 scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2211                     level, bp->b_blkno, 0, sst_cmds, targ->targ_devp->sd_sense);
2212         }
2213
2214         return (rval);
2215 }
2216
2217
2218 #define ARQP(pktp)      ((struct scsi_arq_status *)((pktp)->pkt_scbp))
2219
2220 /*
2221  * Decode auto-rqsense data
2222  */
2223 static int
2224 sst_handle_arq(struct scsi_pkt *pktp, struct scsi_target *targ, struct buf *bp)
2225 {
2226         int     level, amt, rval = COMMAND_DONE_ERROR;
2227
2228         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Auto Request Sense done\n");
2229
2230         if (ARQP(pktp)->sts_rqpkt_status.sts_chk) {
2231                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2232                     "Check Condition on Auto Request Sense!\n");
2233                 return (rval);
2234         }
2235
2236         amt = SENSE_LENGTH - ARQP(pktp)->sts_rqpkt_resid;
2237         if ((ARQP(pktp)->sts_rqpkt_state & STATE_XFERRED_DATA) == 0 ||
2238             amt == 0) {
2239                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "no auto sense data\n");
2240                 return (rval);
2241         }
2242
2243         /*
2244          * Stuff the sense data pointer into sd_sense
2245          */
2246         targ->targ_devp->sd_sense = &(ARQP(pktp)->sts_sensedata);
2247
2248         /*
2249          * Now, check to see whether we got enough sense data to make any
2250          * sense out if it (heh-heh).
2251          */
2252         if (amt < SUN_MIN_SENSE_LENGTH) {
2253                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2254                     "not enough auto sense data\n");
2255                 return (rval);
2256         }
2257
2258         if (sst_debug > 2) {
2259                 hex_print("sst Auto Sense Data",
2260                     &(ARQP(pktp)->sts_sensedata), SENSE_LENGTH);
2261         }
2262
2263         /*
2264          * Decode the sense data
2265          * Note: I'm only looking at the sense key here. Most devices
2266          *       have unique additional sense codes & qualifiers, so
2267          *       it's often more useful to look at them instead.
2268          *
2269          */
2270         switch (ARQP(pktp)->sts_sensedata.es_key) {
2271         case KEY_NOT_READY:
2272                 /*
2273                  * If we get a not-ready indication, wait a bit and
2274                  * try it again, unless this is a special command with
2275                  * the 'fail on error' (FLAG_DIAGNOSE) option set.
2276                  */
2277                 if ((bp == targ->targ_sbufp) &&
2278                     (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2279                         rval = COMMAND_DONE_ERROR;
2280                         level = SCSI_ERR_FATAL;
2281                 } else if (targ->targ_retry_ct++ < sst_retry_count) {
2282                         (void) timeout(sst_restart, (caddr_t)targ,
2283                             SST_BSY_TIMEOUT);
2284                         rval = JUST_RETURN;
2285                         level = SCSI_ERR_RETRYABLE;
2286                 } else {
2287                         rval = COMMAND_DONE_ERROR;
2288                         level = SCSI_ERR_FATAL;
2289                 }
2290                 break;
2291
2292         case KEY_ABORTED_COMMAND:
2293         case KEY_UNIT_ATTENTION:
2294                 rval = QUE_COMMAND;
2295                 level = SCSI_ERR_INFO;
2296                 break;
2297         case KEY_RECOVERABLE_ERROR:
2298         case KEY_NO_SENSE:
2299                 rval = COMMAND_DONE;
2300                 level = SCSI_ERR_RECOVERED;
2301                 break;
2302         case KEY_HARDWARE_ERROR:
2303         case KEY_MEDIUM_ERROR:
2304         case KEY_MISCOMPARE:
2305         case KEY_VOLUME_OVERFLOW:
2306         case KEY_WRITE_PROTECT:
2307         case KEY_BLANK_CHECK:
2308         case KEY_ILLEGAL_REQUEST:
2309         default:
2310                 rval = COMMAND_DONE_ERROR;
2311                 level = SCSI_ERR_FATAL;
2312                 break;
2313         }
2314
2315         /*
2316          * If this was for a special command, check the options
2317          */
2318         if (bp == targ->targ_sbufp) {
2319                 if ((rval == QUE_COMMAND) &&
2320                     (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2321                         rval = COMMAND_DONE_ERROR;
2322                 }
2323                 if (((targ->targ_pkt->pkt_flags & FLAG_SILENT) == 0) ||
2324                     sst_debug) {
2325                         scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2326                             level, bp->b_blkno, 0, sst_cmds,
2327                             targ->targ_devp->sd_sense);
2328                 }
2329         } else if ((level >= sst_error_reporting) || sst_debug) {
2330                 scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2331                     level, bp->b_blkno, 0, sst_cmds, targ->targ_devp->sd_sense);
2332         }
2333
2334         return (rval);
2335 }
2336
2337
2338 /*
2339  * Command completion routine. Check the returned status of the
2340  * command
2341  */
2342 static int
2343 sst_check_error(struct scsi_target *targ, struct buf *bp)
2344 {
2345         struct scsi_pkt *pkt = targ->targ_pkt;
2346         int             action;
2347         struct sst_private *sstprivp = (struct sst_private *)(pkt->pkt_private);
2348
2349         if (SCBP(pkt)->sts_busy) {
2350                 /*
2351                  * Target was busy. If we're not out of retries, call
2352                  * timeout to restart in a bit; otherwise give up and
2353                  * reset the target. If the fail on error flag is
2354                  * set, give up immediately.
2355                  */
2356                 int tval = (pkt->pkt_flags & FLAG_DIAGNOSE) ? 0
2357                                                             : SST_BSY_TIMEOUT;
2358
2359                 if (SCBP(pkt)->sts_is) {
2360                         /*
2361                          * Implicit assumption here is that a device
2362                          * will only be reserved long enough to
2363                          * permit a single i/o operation to complete.
2364                          */
2365                         tval = sst_io_time * drv_usectohz(1000000);
2366                 }
2367
2368                 if ((int)targ->targ_retry_ct++ < sst_retry_count) {
2369                         if (tval) {
2370                                 (void) timeout(sst_restart, (caddr_t)targ,
2371                                     tval);
2372                                 action = JUST_RETURN;
2373                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2374                                     "Target busy, retrying\n");
2375                         } else {
2376                                 action = COMMAND_DONE_ERROR;
2377                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2378                                     "Target busy, no retries\n");
2379                         }
2380                 } else {
2381                         /*
2382                          * WARNING: See the warning in sst_handle_incomplete
2383                          */
2384                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2385                             "Resetting Target\n");
2386                         if (!scsi_reset(ROUTE(targ), RESET_TARGET)) {
2387 #ifdef SCSI_BUS_RESET
2388                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2389                                     "Resetting SCSI Bus\n");
2390                                 if (!scsi_reset(ROUTE(targ), RESET_ALL)) {
2391                                         sst_log(targ->targ_devp, CE_WARN,
2392                                             "SCSI bus reset failed\n");
2393                                 }
2394 #else SCSI_BUS_RESET
2395                                 sst_log(targ->targ_devp, CE_WARN,
2396                                     "Reset Target failed\n");
2397 #endif SCSI_BUS_RESET
2398                         }
2399                         action = COMMAND_DONE_ERROR;
2400                 }
2401         } else if (SCBP(pkt)->sts_chk) {
2402                 action = QUE_SENSE;     /* check condition - get sense */
2403                 if (targ->targ_arq) {
2404                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2405                             "Check Condition with Auto Sense enabled!\n");
2406                 } else {
2407                         SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2408                             "Check Condition\n");
2409                 }
2410         } else {
2411                 targ->targ_retry_ct = 0;
2412                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Command Complete\n");
2413                 /*
2414                  * pkt_resid will reflect, at this point, a residual
2415                  * of how many bytes were not transferred; a non-zero
2416                  * pkt_resid is an error.
2417                  */
2418                 if (pkt->pkt_resid) {
2419                         action = COMMAND_DONE;
2420                         bp->b_resid += pkt->pkt_resid;
2421                 } else {
2422                         sstprivp->priv_amtdone += sstprivp->priv_amt;
2423                         if (sstprivp->priv_amtdone < bp->b_bcount)
2424                                 action = CONTINUE_PKT;
2425                         else
2426                                 action = COMMAND_DONE;
2427                 }
2428         }
2429         return (action);
2430 }
2431
2432
2433 /*
2434  * -----------------------------------------------------------------------------
2435  *      Error Message Data and Routines
2436  */
2437
2438 /*
2439  * Log a message to the console and/or syslog with cmn_err
2440  */
2441 /*ARGSUSED*/
2442 static void
2443 sst_log(struct scsi_device *devp, int level, const char *fmt, ...)
2444 {
2445         auto char name[16];
2446         auto char buf[256];
2447         va_list ap;
2448
2449         if (devp) {
2450                 (void) sprintf(name, "%s%d", ddi_get_name(devp->sd_dev),
2451                     ddi_get_instance(devp->sd_dev));
2452         } else {
2453                 (void) sprintf(name, "sst");
2454         }
2455
2456         va_start(ap, fmt);
2457         (void) vsprintf(buf, fmt, ap);
2458         va_end(ap);
2459
2460         switch (level) {
2461         case CE_CONT:
2462         case CE_NOTE:
2463         case CE_WARN:
2464         case CE_PANIC:
2465                 cmn_err(level, "%s:\t%s", name, buf);
2466                 break;
2467
2468         case SST_CE_DEBUG4: if (sst_debug < 4) break;
2469         /*FALLTHROUGH*/
2470         case SST_CE_DEBUG3: if (sst_debug < 3) break;
2471         /*FALLTHROUGH*/
2472         case SST_CE_DEBUG2: if (sst_debug < 2) break;
2473         /*FALLTHROUGH*/
2474         case SST_CE_DEBUG1:
2475         /*FALLTHROUGH*/
2476         default:
2477                 cmn_err(CE_CONT, "^%s:\t%s", name, buf);
2478                 break;
2479         }
2480 }
2481
2482 /*
2483  * Print a readable error message
2484  * Note: This uses the arrays 'sst_cmds' and 'sst_errors' defined in
2485  *       this file.
2486  */
2487
2488 #define BUFSZ   256
2489
2490 /*
2491  * Print a buffer readably
2492  */
2493 static void
2494 hex_print(char *msg, void *cptr, int len)
2495 {
2496         int i = 0, j;
2497         char    buf[BUFSZ];
2498         char    *cp = cptr;
2499
2500         bzero(buf, BUFSZ);
2501         for (i = 0; i < len; i++) {
2502                 /*
2503                  * make sure there's room for longest %x (i.e., 8 for
2504                  * a negative number) plus space (1) plus zero (1)
2505                  */
2506                 if ((j = strlen(buf)) >= (BUFSZ - 10)) {
2507                         buf[BUFSZ-2] = '>';
2508                         buf[BUFSZ-1] = 0;
2509                         break;          /* cp too long, give up */
2510                 }
2511                 (void) sprintf(&buf[j], "%x ", cp[i]);
2512         }
2513         cmn_err(CE_CONT, "^%s: %s\n", msg, buf);
2514 }
2515
2516 static void
2517 sst_dump_cdb(struct scsi_target *tgt, struct scsi_pkt *pkt, int cdblen)
2518 {
2519         static char     hex[] = "0123456789abcdef";
2520         char            buf [256];
2521         u_char          *cdb;
2522         char            *p;
2523         int             i;
2524
2525         (void) sprintf(buf, "CDB = [");
2526         p = &buf[strlen(buf)];
2527         cdb = pkt->pkt_cdbp;
2528         for (i = 0; i < cdblen; i++, cdb++) {
2529                 if (i > 0)
2530                         *p++ = ' ';
2531                 *p++ = hex[(*cdb >> 4) & 0x0f];
2532                 *p++ = hex[*cdb & 0x0f];
2533         }
2534         *p++ = ']';
2535         *p++ = '\n';
2536         *p = 0;
2537         sst_log(tgt->targ_devp, CE_CONT, buf);
2538 }