Imported Upstream version 2.5.1
[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         (void) sst_unit_ready(dev);
1017
1018         if (sst_unit_ready(dev) == 0) {
1019                 SST_LOG(0, SST_CE_DEBUG1, "sst%d_open: not ready\n",
1020                     getminor(dev));
1021                 targ->targ_state = SST_STATE_CLOSED;
1022
1023                 /*LINTED*/
1024                 _NOTE(COMPETING_THREADS_NOW);
1025
1026                 return (EIO);
1027         }
1028
1029         /*
1030          * Do any other initalization work here, e.g. send
1031          * Mode Sense/Select commands to get the target in the
1032          * right state.
1033          */
1034         /*LINTED*/
1035         _NOTE(COMPETING_THREADS_NOW);
1036
1037         return (0);
1038 }
1039
1040
1041 /*
1042  * close(9e)
1043  * Called on final close only, i.e. the last close(2) call.
1044  * Shut down the device, and mark it closed in the unit structure.
1045  */
1046 /*ARGSUSED*/
1047 static int
1048 sst_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
1049 {
1050         register struct scsi_target     *targ;
1051
1052         SST_LOG(0, SST_CE_DEBUG2, "sst_close\n");
1053
1054         targ = ddi_get_soft_state(sst_state, getminor(dev));
1055         if (targ == NULL) {
1056                 return (ENXIO);
1057         }
1058
1059         /*LINTED*/
1060         _NOTE(NO_COMPETING_THREADS_NOW);
1061
1062         /*
1063          * Note: Close processing here, eg rewind if it's a tape;
1064          *       mark offline if removable media, etc.
1065          * WARNING: Since this is an exclusive open device, other
1066          *      accesses will be locked out until the state is set
1067          *      to closed. If you make the device shareable, you
1068          *      need to cope with the open routine being called while
1069          *      we're in the close routine, and vice-versa. You could
1070          *      serialize open/close calls with a semaphore or with
1071          *      a condition variable. See bst.c.
1072          */
1073
1074         targ->targ_state = SST_STATE_CLOSED;
1075
1076         /*LINTED*/
1077         _NOTE(COMPETING_THREADS_NOW);
1078
1079         return (0);
1080 }
1081
1082
1083 /*
1084  * getinfo(9E)
1085  *
1086  * Device Configuration Routine
1087  * link instance number (unit) with dev_info structure
1088  */
1089 /*ARGSUSED*/
1090 static int
1091 sst_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1092 {
1093         dev_t                   dev;
1094         struct scsi_target      *targ;
1095         int                     instance, error;
1096
1097         SST_LOG(0, SST_CE_DEBUG2, "sst_info\n");
1098
1099         switch (infocmd) {
1100         case DDI_INFO_DEVT2DEVINFO:
1101                 dev = (dev_t)arg;
1102                 instance = getminor(dev);
1103                 targ = ddi_get_soft_state(sst_state, instance);
1104                 if (targ == NULL)
1105                         return (DDI_FAILURE);
1106                 *result = (void *) targ->targ_devp->sd_dev;
1107                 error = DDI_SUCCESS;
1108                 break;
1109         case DDI_INFO_DEVT2INSTANCE:
1110                 *result = (void *)getminor((dev_t)arg);
1111                 error = DDI_SUCCESS;
1112                 break;
1113         default:
1114                 error = DDI_FAILURE;
1115         }
1116         return (error);
1117 }
1118
1119
1120 /*
1121  * read(9E)
1122  *
1123  * Character (raw) read and write routines, called via read(2) and
1124  * write(2). These routines perform "raw" (i.e. unbuffered) i/o.
1125  * Just and call strategy via physio. Physio(9f) will take care of
1126  * address mapping and locking, and will split the transfer if ncessary,
1127  * based on minphys, possibly calling the strategy routine multiple times.
1128  */
1129 /* ARGSUSED2 */
1130 static int
1131 sst_read(dev_t dev, struct uio *uio, cred_t *cred_p)
1132 {
1133         SST_LOG(0, SST_CE_DEBUG2, "Read\n");
1134         return (physio(sst_strategy, (struct buf *)0, dev, B_READ,
1135             minphys, uio));
1136 }
1137
1138 /*
1139  * write(9E)
1140  */
1141 /* ARGSUSED2 */
1142 static int
1143 sst_write(dev_t dev, struct uio *uio, cred_t *cred_p)
1144 {
1145         SST_LOG(0, SST_CE_DEBUG2, "Write\n");
1146         return (physio(sst_strategy, (struct buf *)0, dev, B_WRITE,
1147             minphys, uio));
1148 }
1149
1150
1151 /*
1152  * aread(9E) - asynchronous read
1153  */
1154 /*ARGSUSED2*/
1155 static int
1156 sst_aread(dev_t dev, struct aio_req *aio, cred_t *credp)
1157 {
1158         SST_LOG(0, SST_CE_DEBUG2, "aread\n");
1159         return (aphysio(sst_strategy, anocancel, dev, B_READ,
1160             minphys, aio));
1161 }
1162
1163 /*
1164  * awrite(9E) - asynchronous write
1165  */
1166 /*ARGSUSED2*/
1167 static int
1168 sst_awrite(dev_t dev, struct aio_req *aio, cred_t *credp)
1169 {
1170         SST_LOG(0, SST_CE_DEBUG2, "awrite\n");
1171         return (aphysio(sst_strategy, anocancel, dev, B_WRITE,
1172             minphys, aio));
1173 }
1174
1175
1176 /*
1177  * strategy(9E)
1178  *
1179  * Main routine for commands to the device. since this is a character
1180  * device, this routine is called only from the read/write routines above,
1181  * and sst_ioctl_cmd for the pass through ioctl (USCSICMD).
1182  * The cv_wait prevents this routine from being called simultaneously by
1183  * two threads.
1184  */
1185 static int
1186 sst_strategy(struct buf *bp)
1187 {
1188         struct scsi_target      *targ;
1189
1190         targ = ddi_get_soft_state(sst_state, getminor(bp->b_edev));
1191         if (targ == NULL) {
1192                 bp->b_resid = bp->b_bcount;
1193                 bioerror(bp, ENXIO);
1194                 biodone(bp);
1195                 return (0);
1196         }
1197
1198         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_strategy\n");
1199
1200         /*
1201          * Mark component busy so we won't get powered down
1202          * while trying to resume.
1203          */
1204         if (pm_busy_component(SST_DEVINFO(targ), 0) != DDI_SUCCESS) {
1205                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1206                     "pm_busy_component() failed\n");
1207         }
1208
1209         mutex_enter(SST_MUTEX(targ));
1210
1211         /*
1212          * if we are suspended, wait till we are resumed again
1213          */
1214         do {
1215                 /*
1216                  * Block commands while suspended via DDI_SUSPEND
1217                  */
1218                 while (targ->targ_suspended) {
1219                         cv_wait(&targ->targ_suspend_cv, SST_MUTEX(targ));
1220                 }
1221
1222                 /*
1223                  * Raise power level back to operational if needed.
1224                  * Since ddi_dev_is_needed() will result in our
1225                  * power(9E) entry point being called, we must
1226                  * drop the mutex.
1227                  */
1228                 if (targ->targ_power_level == 0) {
1229                         mutex_exit(SST_MUTEX(targ));
1230                         if (ddi_dev_is_needed(SST_DEVINFO(targ), 0, 1) !=
1231                             DDI_SUCCESS) {
1232                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1233                                     "ddi_dev_is_needed() failed\n");
1234                         }
1235                         mutex_enter(SST_MUTEX(targ));
1236                 }
1237
1238                 /*
1239                  * We need to continue to block commands if still
1240                  * suspended via DDI_PM_SUSPEND.  Because we dropped
1241                  * the mutex to call ddi_dev_is_needed(), we may be
1242                  * executing in a thread that came in after the power
1243                  * level was raised but before attach was called with
1244                  * DDI_PM_RESUME.
1245                  */
1246                 while (targ->targ_pm_suspended) {
1247                         cv_wait(&targ->targ_suspend_cv, SST_MUTEX(targ));
1248                 }
1249         } while (targ->targ_suspended || (targ->targ_power_level == 0));
1250
1251         /*
1252          * Wait for the current request to finish. We can safely release
1253          * the mutex once we have the pkt, because anyone else calling
1254          * strategy will wait here until we release it with a cv_signal.
1255          */
1256         while (targ->targ_pkt_busy) {
1257                 cv_wait(&targ->targ_pkt_cv, SST_MUTEX(targ));
1258         }
1259         targ->targ_pkt_busy =  1;               /* mark busy */
1260         mutex_exit(SST_MUTEX(targ));
1261
1262         if (((targ->targ_pkt = sst_make_cmd(targ, bp,
1263             (struct scsi_pkt *)NULL))) == NULL) {
1264                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1265                     "Unable to create SCSI command\n");
1266                 bp->b_resid = bp->b_bcount;
1267                 bioerror(bp, EIO);
1268                 biodone(bp);
1269                 pm_idle_component(SST_DEVINFO(targ), 0);
1270                 return (0);
1271         }
1272
1273         bp->b_resid = 0;
1274
1275         SST_DUMP_CDB(targ, targ->targ_pkt, CDB_GROUP0);
1276
1277         if (scsi_transport(targ->targ_pkt) != TRAN_ACCEPT) {
1278                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1279                     "Command transport failed\n");
1280                 bp->b_resid = bp->b_bcount;
1281                 bioerror(bp, EIO);
1282                 biodone(bp);
1283                 pm_idle_component(SST_DEVINFO(targ), 0);
1284         }
1285
1286         return (0);
1287 }
1288
1289
1290 /*
1291  * ioctl calls.
1292  * Ioctls are device specific. Three are provided here as examples:
1293  * SSTIOC_READY: Send a Test Unit Ready command to the device. This fills
1294  *               in the uscsi_cmd structure in the unit struct. The actual
1295  *               cdb to be sent to the device is created here; the union
1296  *               scsi_cdb is defined in /usr/include/sys/scsi/impl/commands.h.
1297  * SST_ERRLEV:  Change the error reporting level.
1298  * USCSICMD:    Pass through. Send the user-supplied command to the device. Very
1299  *              little checking is done - it's left up to the caller to supply
1300  *              a valid cdb.
1301  */
1302 /* ARGSUSED3 */
1303 static int
1304 sst_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1305         cred_t *cred_p, int *rval_p)
1306 {
1307         int                     err = 0;
1308         struct scsi_target      *targ;
1309         struct uscsi_cmd        uscsi_cmd;
1310         struct uscsi_cmd        *ucmd = &uscsi_cmd;
1311         char                    cmdblk[CDB_GROUP0];
1312
1313 #ifdef _MULTI_DATAMODEL
1314         /*
1315          * For use when a 32-bit app makes an ioctl into a 64-bit driver
1316          */
1317         struct sst_uscsi_cmd32  uscsi_cmd32;
1318         struct sst_uscsi_cmd32  *ucmd32 = &uscsi_cmd32;
1319         model_t                 model;
1320 #endif /* _MULTI_DATAMODEL */
1321
1322
1323         targ = ddi_get_soft_state(sst_state, getminor(dev));
1324         if (targ == NULL) {
1325                 return (ENXIO);         /* invalid minor number */
1326         }
1327
1328         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_ioctl: cmd = 0x%x\n", cmd);
1329
1330         bzero(ucmd, sizeof (struct uscsi_cmd));
1331
1332         switch (cmd) {
1333         case SSTIOC_READY:              /* Send a Test Unit Ready command */
1334                 ucmd->uscsi_bufaddr = 0;
1335                 ucmd->uscsi_buflen = 0;
1336                 bzero(cmdblk, CDB_GROUP0);
1337                 cmdblk[0] = (char)SCMD_TEST_UNIT_READY;
1338                 ucmd->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_WRITE;
1339                 ucmd->uscsi_cdb = cmdblk;
1340                 ucmd->uscsi_cdblen = CDB_GROUP0;
1341                 ucmd->uscsi_timeout = sst_io_time;
1342
1343                 err = sst_ioctl_cmd(dev, ucmd,
1344                         UIO_SYSSPACE, UIO_SYSSPACE, mode);
1345                 break;
1346
1347         case SSTIOC_ERRLEV:             /* Set the error reporting level */
1348                 if (ddi_copyin((void *)arg, &sst_error_reporting,
1349                     sizeof (int32_t), mode)) {
1350                         return (EFAULT);
1351                 }
1352                 break;
1353
1354         case USCSICMD:
1355                 /*
1356                  * Run a generic ucsi.h command.
1357                  */
1358
1359 #if 0 /* allow non-root to do this */
1360                 /*
1361                  * Check root permissions
1362                  */
1363                 if (drv_priv(cred_p) != 0) {
1364                         return (EPERM);
1365                 }
1366 #endif
1367
1368 #ifdef _MULTI_DATAMODEL
1369                 switch (model = ddi_model_convert_from(mode & FMODELS)) {
1370                 case DDI_MODEL_ILP32:
1371                 {
1372                         if (ddi_copyin((void *)arg, ucmd32,
1373                             sizeof (*ucmd32), mode)) {
1374                                 return (EFAULT);
1375                         }
1376                         /*
1377                          * Convert 32-bit ILP32 application's uscsi cmd
1378                          * into 64-bit LP64 equivalent for internal use
1379                          */
1380                         sst_uscsi_cmd32touscsi_cmd(ucmd32, ucmd);
1381                         break;
1382                 }
1383                 case DDI_MODEL_NONE:
1384                         if (ddi_copyin((void *)arg, ucmd,
1385                             sizeof (*ucmd), mode)) {
1386                                 return (EFAULT);
1387                         }
1388                         break;
1389                 }
1390
1391 #else /* ! _MULTI_DATAMODEL */
1392                 if (ddi_copyin((void *)arg, ucmd,
1393                     sizeof (*ucmd), mode)) {
1394                         return (EFAULT);
1395                 }
1396 #endif /* _MULTI_DATAMODEL */
1397
1398                 err = sst_ioctl_cmd(dev, ucmd,
1399                         UIO_USERSPACE, UIO_USERSPACE, mode);
1400
1401 #ifdef _MULTI_DATAMODEL
1402                 switch (model) {
1403                 case DDI_MODEL_ILP32:
1404                         /*
1405                          * Convert LP64 back to IPL32 before copyout
1406                          */
1407                         sst_uscsi_cmdtouscsi_cmd32(ucmd, ucmd32);
1408
1409                         if (ddi_copyout(ucmd32, (void *)arg,
1410                             sizeof (*ucmd32), mode)) {
1411                                 return (EFAULT);
1412                         }
1413                         break;
1414
1415                 case DDI_MODEL_NONE:
1416                         if (ddi_copyout(ucmd, (void *)arg,
1417                             sizeof (*ucmd), mode)) {
1418                                 return (EFAULT);
1419                         }
1420                         break;
1421                 }
1422 #else /* ! _MULTI_DATAMODE */
1423                 if (ddi_copyout(ucmd, (void *)arg,
1424                     sizeof (*ucmd), mode)) {
1425                         return (EFAULT);
1426                 }
1427 #endif /* _MULTI_DATAMODE */
1428                 break;
1429
1430
1431         default:
1432                 err = ENOTTY;
1433                 break;
1434         }
1435         return (err);
1436 }
1437
1438
1439 /*
1440  * Run a command for user (from sst_ioctl) or from someone else in the driver.
1441  *
1442  * cdbspace is for address space of cdb; dataspace is for address space
1443  * of the buffer - user or kernel.
1444  */
1445 static int
1446 sst_ioctl_cmd(dev_t dev, struct uscsi_cmd *scmd,
1447     enum uio_seg cdbspace, enum uio_seg dataspace,
1448     int mode)
1449 {
1450         caddr_t                 cdb, user_cdbp;
1451         int                     err, rw;
1452         struct buf              *bp;
1453         struct scsi_target      *targ;
1454         int                     flag;
1455
1456         targ = ddi_get_soft_state(sst_state, getminor(dev));
1457         if (targ == NULL) {
1458                 return (ENXIO);
1459         }
1460
1461         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_ioctl_cmd\n");
1462
1463         /*
1464          * The uscsi structure itself is already in kernel space (copied
1465          * in by sst_ioctl, or declared there by our caller); but
1466          * we need to copy in the cdb here.
1467          */
1468         cdb = kmem_zalloc((size_t)scmd->uscsi_cdblen, KM_SLEEP);
1469         flag = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : mode;
1470         if (ddi_copyin(scmd->uscsi_cdb, cdb,
1471             (u_int)scmd->uscsi_cdblen, flag)) {
1472                 kmem_free(cdb, (size_t)scmd->uscsi_cdblen);
1473                 return (EFAULT);
1474         }
1475
1476         /*
1477          * The cdb pointer in the structure passed by the user is pointing
1478          * to user space. We've just copied the cdb into a local buffer,
1479          * so point uscsi_cdb to it now. We'll restore the user's pointer
1480          * at the end.
1481          */
1482         user_cdbp = scmd->uscsi_cdb;    /* save the user's pointer */
1483         scmd->uscsi_cdb = cdb;          /* point to the local cdb buffer */
1484         rw = (scmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE;
1485
1486         /*
1487          * Get the 'special command' buffer.
1488          * First lock the targ struct; if the buffer's busy, wait for
1489          * it to become free. Once we get the buffer, mark it busy to
1490          * lock out other requests for it.
1491          * Note cv_wait will release the mutex, allowing other parts
1492          *      of the driver to acquire it.
1493          * Note Once we have the special buffer, we can safely release
1494          *      the mutex; the buffer is now busy and another thread will
1495          *      block in the cv_wait until we release it. All the code
1496          *      from here until we unset the busy flag is non-reentrant,
1497          *      including the physio/strategy/start/callback/done thread.
1498          */
1499         mutex_enter(SST_MUTEX(targ));
1500         while (targ->targ_sbuf_busy) {
1501                 cv_wait(&targ->targ_sbuf_cv, SST_MUTEX(targ));
1502         }
1503         targ->targ_sbuf_busy = 1;
1504         mutex_exit(SST_MUTEX(targ));
1505
1506         bp = targ->targ_sbufp;
1507
1508         if (scmd->uscsi_buflen) {
1509                 /*
1510                  * We're sending/receiving data; create a uio structure and
1511                  * call physio to do the right things.
1512                  */
1513                 auto struct     iovec   aiov;
1514                 auto struct     uio     auio;
1515                 register struct uio     *uio = &auio;
1516
1517                 bzero(&auio, sizeof (struct uio));
1518                 bzero(&aiov, sizeof (struct iovec));
1519                 aiov.iov_base = scmd->uscsi_bufaddr;
1520                 aiov.iov_len = scmd->uscsi_buflen;
1521                 uio->uio_iov = &aiov;
1522
1523                 uio->uio_iovcnt = 1;
1524                 uio->uio_resid = scmd->uscsi_buflen;
1525                 uio->uio_segflg = dataspace;
1526                 uio->uio_offset = 0;
1527                 uio->uio_fmode = 0;
1528
1529                 /*
1530                  * Let physio do the rest...
1531                  */
1532                 bp->b_private = (void *) scmd;
1533                 err = physio(sst_strategy, bp, dev, rw, minphys, uio);
1534         } else {
1535                 /*
1536                  * No data transfer, we can call sst_strategy directly
1537                  */
1538                 bp->b_private = (void *) scmd;
1539                 bp->b_flags = B_BUSY | rw;
1540                 bp->b_edev = dev;
1541                 bp->b_bcount = bp->b_blkno = 0;
1542                 (void) sst_strategy(bp);
1543                 err = biowait(bp);
1544         }
1545
1546         /*
1547          * get the status block, if any, and
1548          * release any resources that we had.
1549          */
1550         scmd->uscsi_status = 0;
1551         scmd->uscsi_status = SCBP_C(targ->targ_pkt);
1552
1553         /*
1554          * Lock the targ struct. Clearing the 'busy' flag and signalling
1555          * must be made atomic.
1556          */
1557         mutex_enter(SST_MUTEX(targ));           /* LOCK the targ struct */
1558         if (targ->targ_pkt != NULL)
1559                 scsi_destroy_pkt(targ->targ_pkt);
1560         targ->targ_sbuf_busy = 0;
1561         cv_signal(&targ->targ_sbuf_cv);         /* release the special buffer */
1562         targ->targ_pkt_busy = 0;
1563         cv_signal(&targ->targ_pkt_cv);          /* release the packet */
1564         mutex_exit(SST_MUTEX(targ));            /* UNLOCK the targ struct */
1565
1566         kmem_free(scmd->uscsi_cdb, (size_t)scmd->uscsi_cdblen);
1567         scmd->uscsi_cdb = user_cdbp;    /* restore the user's pointer */
1568         return (err);
1569 }
1570
1571
1572 /*
1573  * Check to see if the unit will respond to a Test Unit Ready
1574  * Returns true or false
1575  */
1576 static int
1577 sst_unit_ready(dev_t dev)
1578 {
1579         auto struct uscsi_cmd scmd, *com = &scmd;
1580         auto char       cmdblk[CDB_GROUP0];
1581         auto int        err;
1582
1583         com->uscsi_bufaddr = 0;
1584         com->uscsi_buflen = 0;
1585         bzero(cmdblk, CDB_GROUP0);
1586         cmdblk[0] = (char)SCMD_TEST_UNIT_READY;
1587         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_WRITE;
1588         com->uscsi_cdb = cmdblk;
1589         com->uscsi_cdblen = CDB_GROUP0;
1590         com->uscsi_timeout = sst_io_time;
1591
1592         if (err = sst_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, 0)) {
1593                 SST_LOG(0, SST_CE_DEBUG1, "sst_unit_ready failed: 0x%x\n", err);
1594                 return (0);
1595         }
1596         return (1);
1597 }
1598
1599
1600 /*
1601  * Done with a command.
1602  * Start the next command and then call biodone() to tell physio or
1603  * sst_ioctl_cmd that this i/o has completed.
1604  *
1605  */
1606 static void
1607 sst_done(struct scsi_target *targ, register struct buf *bp)
1608 {
1609         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_done\n");
1610
1611         /*
1612          * Lock the targ struct; we don't want targ_pkt to change
1613          * between taking a copy of it and zeroing it.
1614          */
1615         mutex_enter(SST_MUTEX(targ));
1616
1617         /*
1618          * For regular commands (i.e. not using the special buffer),
1619          * free resources and clear the current request. Sst_ioctl_cmd
1620          * will do its own freeing for special commands. We need the lock
1621          * here because targ_pkt must not change during the operations.
1622          */
1623         if (bp != targ->targ_sbufp) {
1624                 scsi_destroy_pkt(targ->targ_pkt);
1625                 targ->targ_pkt_busy = 0;
1626                 cv_signal(&targ->targ_pkt_cv);
1627         }
1628
1629         mutex_exit(SST_MUTEX(targ));            /* UNLOCK the targ struct */
1630
1631         /*
1632          * Tell interested parties that the i/o is done
1633          */
1634         biodone(bp);
1635         pm_idle_component(SST_DEVINFO(targ), 0);
1636 }
1637
1638
1639 /*
1640  * Allocate resources for a SCSI command - call scsi_init_pkt to get
1641  * a packet and allocate DVMA resources, and create the SCSI CDB.
1642  *
1643  * Also used to continue an in-progress packet; simply calls scsi_init_pkt
1644  * to update the DMA resources to the next chunk, and re-creates the
1645  * SCSI command.
1646  *
1647  * Note: There are differences in the DMA subsystem between the SPARC
1648  *      and x86 platforms. For example, on the x86 platform, DMA uses
1649  *      physical rather than virtual addresses as a result the DMA buffer
1650  *      may not be contiguous.
1651  *
1652  *      Because of these differences, the system may not be able to fully
1653  *      satisfy a DMA allocation request from the target driver with resources
1654  *      attached to one packet. As a solution, the scsi_init_pkt() routine
1655  *      should be called with the PKT_DMA _PARTIAL flag to allow the system
1656  *      to break up the DMA request. If a single DMA allocation cannot be
1657  *      done, the HBA driver sets the pkt_resid in the scsi_pkt structure
1658  *      to the number of bytes of DMA resources it was able to  allocate.
1659  *      The target driver needs to modify the SCSI command it places into
1660  *      the packet for transport to request the proper amount of data specified
1661  *      in pkt_resid. When the packet's completion routine is called, the
1662  *      process is repeated again.
1663  *
1664  */
1665 static struct scsi_pkt *
1666 sst_make_cmd(struct scsi_target *targ, struct buf *bp, struct scsi_pkt *pkt)
1667 {
1668         struct uscsi_cmd *scmd = (struct uscsi_cmd *)bp->b_private;
1669         int                     tval, blkcnt;
1670         u_int                   flags;
1671         int                     pktalloc = 0;
1672         struct sst_private      *sstprivp;
1673
1674         SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
1675             "sst_make_cmd: block %d, count=%d\n", bp->b_blkno, bp->b_bcount);
1676
1677         /*
1678          * If called with pkt == NULL, this is first time, and we
1679          * must allocate and initialize the packet
1680          */
1681         if (pkt == (struct scsi_pkt *)NULL)
1682                 pktalloc = 1;
1683
1684         flags = 0;
1685         blkcnt = bp->b_bcount >> DEV_BSHIFT;
1686
1687         if (bp != targ->targ_sbufp) {
1688                 /*
1689                  * Allocate a packet.
1690                  * ROUTE(targ) is shorthand for sd_address
1691                  * Note that if auto request sensing is enabled, we
1692                  * specify the status length to hold sense data if needed
1693                  */
1694                 if (pktalloc) {
1695                         pkt = scsi_init_pkt(ROUTE(targ),
1696                             (struct scsi_pkt *)NULL, bp,
1697                             (bp->b_blkno + blkcnt > 0x1FFFFF || blkcnt > 0xFF) ?
1698                                 CDB_GROUP1 : CDB_GROUP0,
1699                             targ->targ_arq ?
1700                             sizeof (struct scsi_arq_status) : 1,
1701                             sizeof (struct sst_private), PKT_DMA_PARTIAL,
1702                             SLEEP_FUNC, 0);
1703                         if (pkt == (struct scsi_pkt *)0) {
1704                                 return (NULL);
1705                         }
1706                 } else {
1707                         /*
1708                          * Here we're simply calling scsi_init_pkt() for the
1709                          * next section of DMA resource for the current
1710                          * I/O request (in bp); we needn't worry about
1711                          * the associated lengths, or possible failure,
1712                          * since scsi_init_pkt() is just moving the DMA win
1713                          */
1714                         pkt = scsi_init_pkt(ROUTE(targ), pkt, bp, 0,
1715                             targ->targ_arq ?
1716                             sizeof (struct scsi_arq_status) : 1,
1717                             sizeof (struct sst_private),
1718                             PKT_DMA_PARTIAL, NULL_FUNC, 0);
1719                 }
1720
1721                 SST_LOG(targ->targ_devp, SST_CE_DEBUG3,
1722                     "sst_make_cmd: pkt_resid: %d\n", pkt->pkt_resid);
1723
1724                 sstprivp = (struct sst_private *)pkt->pkt_private;
1725
1726                 if (pktalloc) {
1727                         sstprivp->priv_bp = bp;
1728                         sstprivp->priv_amtdone = 0;
1729                         sstprivp->priv_amt = bp->b_bcount - pkt->pkt_resid;
1730                 }
1731
1732                 sst_fill_cdb(pkt, targ, bp, flags);
1733                 pkt->pkt_flags = flags;
1734                 tval = sst_io_time;
1735         } else {
1736                 /*
1737                  * All special command come through sst_ioctl_cmd, which
1738                  * uses the uscsi interface. Just need to get the CDB
1739                  * from scmd and plug it in. Still call scsi_setup_cdb because
1740                  * it fills in some of the pkt field for us. Its cdb
1741                  * manipulations will be overwritten by the bcopy.
1742                  */
1743                 if (scmd->uscsi_flags & USCSI_SILENT)
1744                         flags |= FLAG_SILENT;
1745                 if (scmd->uscsi_flags & USCSI_DIAGNOSE)
1746                         flags |= FLAG_DIAGNOSE;
1747                 if (scmd->uscsi_flags & USCSI_ISOLATE)
1748                         flags |= FLAG_ISOLATE;
1749                 if (scmd->uscsi_flags & USCSI_NODISCON)
1750                         flags |= FLAG_NODISCON;
1751                 if (scmd->uscsi_flags & USCSI_NOPARITY)
1752                         flags |= FLAG_NOPARITY;
1753
1754                 if (pktalloc) {
1755                         pkt = scsi_init_pkt(ROUTE(targ),
1756                             (struct scsi_pkt *)NULL,
1757                             bp->b_bcount ? bp : 0,
1758                             scmd->uscsi_cdblen,
1759                             targ->targ_arq ?
1760                             sizeof (struct scsi_arq_status) : 1,
1761                             sizeof (struct sst_private), PKT_DMA_PARTIAL,
1762                             SLEEP_FUNC, 0);
1763                         if (!pkt) {
1764                                 return (NULL);
1765                         }
1766                 } else {
1767                         pkt = scsi_init_pkt(ROUTE(targ), pkt,
1768                             bp->b_bcount ? bp : 0, 0,
1769                             targ->targ_arq ?
1770                             sizeof (struct scsi_arq_status) : 1,
1771                             sizeof (struct sst_private),
1772                             PKT_DMA_PARTIAL, NULL_FUNC, 0);
1773                 }
1774
1775                 sstprivp = (struct sst_private *)pkt->pkt_private;
1776                 if (pktalloc) {
1777                         sstprivp->priv_bp = bp;
1778                         sstprivp->priv_amtdone = 0;
1779                         sstprivp->priv_amt = bp->b_bcount - pkt->pkt_resid;
1780                 }
1781
1782                 (void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
1783                         scmd->uscsi_cdb[0], 0, 0, 0);
1784
1785                 bcopy(scmd->uscsi_cdb, pkt->pkt_cdbp,
1786                     scmd->uscsi_cdblen);
1787                 tval = scmd->uscsi_timeout;
1788
1789                 /* a timeout of 0 (ie. no timeout) is bad practice */
1790                 if (tval == 0) {
1791                         tval = sst_io_time;
1792                 }
1793         }
1794
1795         pkt->pkt_comp = sst_callback;
1796         pkt->pkt_time = tval;
1797
1798 #ifdef SST_DEBUG
1799         if (sst_debug > 2) {
1800                 hex_print("sst_make_cmd, CDB", pkt->pkt_cdbp, 6);
1801         }
1802 #endif SST_DEBUG
1803
1804         return (pkt);
1805 }
1806
1807
1808 /*
1809  * Stuff CDB with SCSI command for current I/O request
1810  */
1811
1812 static void
1813 sst_fill_cdb(struct scsi_pkt *pkt, struct scsi_target *targ, struct buf *bp,
1814     u_int flags)
1815 {
1816         struct sst_private *sstprivp;
1817         u_int blkno, len;
1818         u_int com;
1819
1820         if ((scsi_options & SCSI_OPTIONS_DR) == 0)
1821                 flags |= FLAG_NODISCON;
1822
1823         sstprivp = (struct sst_private *)(pkt->pkt_private);
1824
1825         blkno = bp->b_blkno + (sstprivp->priv_amtdone >> DEV_BSHIFT);
1826         /* use result of scsi_init_pkt() as I/O length */
1827         len = sstprivp->priv_amt; /* pkt->pkt_resid; */
1828
1829         SST_LOG(targ->targ_devp, SST_CE_DEBUG3,
1830             "sst_fill_cdb: blkno %x len %x\n", blkno, len);
1831         /*
1832          * Note: We use the group 0 Read/Write commands here
1833          *      unless the starting block number or blocks
1834          *      requested is too large.
1835          *      If your device needs different commands for normal
1836          *      data transfer (e.g. Send/Receive, read/write buffer),
1837          *      change it here.
1838          */
1839
1840         if ((blkno + (len >> DEV_BSHIFT) > 0x1FFFFF) ||
1841             ((len >> DEV_BSHIFT) > 0xFF)) {
1842                 SST_LOG(targ->targ_devp, SST_CE_DEBUG3,
1843                     "sst_make_cmd: using g1 command\n");
1844                 com = (bp->b_flags & B_READ) ? SCMD_READ_G1 : SCMD_WRITE_G1;
1845         } else {
1846                 com = (bp->b_flags & B_READ) ? SCMD_READ : SCMD_WRITE;
1847         }
1848
1849         /*
1850          * Note: The CDB creation differs for sequential and
1851          *       direct access devices. We do both here, but
1852          *       you should need only one. For sequential access,
1853          *       we assume a fixed 512 byte block size. If you
1854          *       have a variable block size device, ask for the
1855          *       actual number of bytes wanted, and change the last
1856          *       parameter, 'fixbit' (1) to zero.
1857          */
1858         if (targ->targ_devp->sd_inq->inq_dtype == DTYPE_SEQUENTIAL) {
1859                 blkno = 0;
1860         }
1861
1862         (void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
1863                                 com, blkno, len >> DEV_BSHIFT, 0);
1864         pkt->pkt_flags = flags;
1865
1866         if (targ->targ_devp->sd_inq->inq_dtype == DTYPE_SEQUENTIAL) {
1867                 ((union scsi_cdb *)(pkt->pkt_cdbp))->t_code = 1;
1868         }
1869 }
1870
1871
1872 /*
1873  * -----------------------------------------------------------------------------
1874  * Interrupt Service Routines
1875  */
1876
1877 /*
1878  * Restart a command - the device was either busy or not ready
1879  */
1880 static void
1881 sst_restart(caddr_t arg)
1882 {
1883         struct scsi_target      *targ = (struct scsi_target *)arg;
1884         struct scsi_pkt         *pkt;
1885         struct buf              *bp;
1886
1887         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_restart\n");
1888
1889         /*
1890          * No need to lock the targ structure because any other threads
1891          * will wait in sst_strategy for the packet.
1892          */
1893         pkt = targ->targ_pkt;
1894         bp = ((struct sst_private *)pkt->pkt_private)->priv_bp;
1895
1896         if (bp) {
1897                 struct scsi_pkt *pkt = targ->targ_pkt;
1898
1899                 if (pkt->pkt_flags & FLAG_SENSING) {
1900                         pkt = targ->targ_rqs;
1901                 }
1902
1903                 SST_DUMP_CDB(targ, targ->targ_pkt, CDB_GROUP0);
1904
1905                 if (scsi_transport(pkt) != TRAN_ACCEPT) {
1906                         bp->b_resid = bp->b_bcount;
1907                         bioerror(bp, ENXIO);
1908                         sst_done(targ, bp);
1909                 }
1910         }
1911 }
1912
1913
1914 /*
1915  * Command completion processing, called by the host adapter driver
1916  * when it's done with the command.
1917  * No need for mutexes in this routine - there's only one active command
1918  * at a time, anyone else wanting to send a command will wait in strategy
1919  * until we call sst_done.
1920  */
1921 static void
1922 sst_callback(struct scsi_pkt *pkt)
1923 {
1924         struct scsi_target      *targ;
1925         struct buf              *bp;
1926         int                     action;
1927         struct sst_private      *sstprivp;
1928
1929         sstprivp = (struct sst_private *)pkt->pkt_private;
1930         bp = sstprivp->priv_bp;
1931         targ = ddi_get_soft_state(sst_state, getminor(bp->b_edev));
1932
1933         SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
1934         "sst_callback: pkt_reason = 0x%x, pkt_flags = 0x%x, pkt_state = 0x%x\n",
1935             pkt->pkt_reason, pkt->pkt_flags, pkt->pkt_state);
1936
1937         mutex_enter(SST_MUTEX(targ));
1938
1939         if (pkt->pkt_reason != CMD_CMPLT) {
1940                 /*
1941                  * The command did not complete. Retry if possible.
1942                  */
1943                 action = sst_handle_incomplete(targ);
1944         } else if (targ->targ_arq && pkt->pkt_state & STATE_ARQ_DONE) {
1945                 /*
1946                  * The auto-rqsense happened, and the packet has a
1947                  * filled-in scsi_arq_status structure, pointed to by
1948                  * pkt_scbp.
1949                  */
1950                 action = sst_handle_arq(pkt, targ, bp);
1951         } else if (pkt->pkt_flags & FLAG_SENSING) {
1952                 /*
1953                  * We were running a REQUEST SENSE. Decode the
1954                  * sense data and decide what to do next.
1955                  */
1956                 pkt = targ->targ_pkt;   /* get the pkt for the orig command */
1957                 pkt->pkt_flags &= ~FLAG_SENSING;
1958                 action = sst_handle_sense(targ, bp);
1959         } else {
1960                 /*
1961                  * Command completed and we're not getting sense. Check
1962                  * for errors and decide what to do next.
1963                  */
1964                 action = sst_check_error(targ, bp);
1965         }
1966         mutex_exit(SST_MUTEX(targ));
1967
1968         switch (action) {
1969         case QUE_SENSE:
1970                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Getting Sense\n");
1971                 pkt->pkt_flags |= FLAG_SENSING;
1972                 ((struct sst_private *)targ->targ_rqs->pkt_private)->
1973                                                                 priv_bp = bp;
1974                 bzero(targ->targ_devp->sd_sense, SENSE_LENGTH);
1975                 SST_DUMP_CDB(targ, targ->targ_rqs, CDB_GROUP0);
1976                 if (scsi_transport(targ->targ_rqs) == TRAN_ACCEPT) {
1977                         break;
1978                 }
1979                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1980                     "Request Sense transport failed\n");
1981                 /*FALLTHROUGH*/
1982         case COMMAND_DONE_ERROR:
1983                 bp->b_resid = bp->b_bcount;
1984                 bioerror(bp, ENXIO);
1985                 /*FALLTHROUGH*/
1986         case COMMAND_DONE:
1987                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Command Done\n");
1988                 sst_done(targ, bp);
1989                 break;
1990
1991         case QUE_COMMAND:
1992                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Retrying Command\n");
1993                 SST_DUMP_CDB(targ, targ->targ_pkt, CDB_GROUP0);
1994                 if (scsi_transport(targ->targ_pkt) != TRAN_ACCEPT) {
1995                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1996                             "Retry transport failed\n");
1997                         bp->b_resid = bp->b_bcount;
1998                         bioerror(bp, ENXIO);
1999                         sst_done(targ, bp);
2000                         return;
2001                 }
2002                 break;
2003         case JUST_RETURN:
2004                 break;
2005
2006         case CONTINUE_PKT:
2007                 /*
2008                  * Back from a chunk of a split-up bp.  Do next chunk or
2009                  * finish up.
2010                  */
2011                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Continuing packet\n");
2012                 (void) sst_make_cmd(targ, bp, pkt);
2013                 SST_DUMP_CDB(targ, pkt, CDB_GROUP0);
2014                 if (scsi_transport(pkt) != TRAN_ACCEPT) {
2015                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2016                             "Command transport failedn");
2017                         bp->b_resid = bp->b_bcount;
2018                         bioerror(bp, EIO);
2019                         biodone(bp);
2020                         pm_idle_component(SST_DEVINFO(targ), 0);
2021                 }
2022         }
2023
2024 }
2025
2026
2027 /*
2028  * Incomplete command handling. Figure out what to do based on
2029  * how far the command did get.
2030  */
2031 static int
2032 sst_handle_incomplete(struct scsi_target *targ)
2033 {
2034         register int    rval = COMMAND_DONE_ERROR;
2035         register struct scsi_pkt        *pkt = targ->targ_pkt;
2036
2037         if (!targ->targ_arq && pkt->pkt_flags & FLAG_SENSING) {
2038                 pkt = targ->targ_rqs;
2039         }
2040
2041         /*
2042          * The target may still be running the  command,
2043          * so try and reset it, to get it into a known state.
2044          * Note: This is forcible, there may be a more polite
2045          *       method for your device.
2046          */
2047         if ((pkt->pkt_statistics &
2048             (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
2049                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1, "Aborting Command\n");
2050                 mutex_exit(SST_MUTEX(targ));
2051                 if (!(scsi_abort(ROUTE(targ), (struct scsi_pkt *)0))) {
2052                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2053                             "Resetting Target\n");
2054                         if (!(scsi_reset(ROUTE(targ), RESET_TARGET))) {
2055                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2056                                     "Resetting SCSI Bus\n");
2057                                 if (!scsi_reset(ROUTE(targ), RESET_ALL)) {
2058                                         sst_log(targ->targ_devp, CE_WARN,
2059                                             "SCSI bus reset failed\n");
2060                                 }
2061                                 mutex_enter(SST_MUTEX(targ));
2062                                 return (COMMAND_DONE_ERROR);
2063                         }
2064                 }
2065                 mutex_enter(SST_MUTEX(targ));
2066         }
2067
2068         /*
2069          * If we were running a request sense, try it again if possible.
2070          * Some devices can handle retries, others will not.
2071          */
2072         if (pkt->pkt_flags & FLAG_SENSING) {
2073                 if (targ->targ_retry_ct++ < sst_retry_count) {
2074                         rval = QUE_SENSE;
2075                 }
2076         } else if (targ->targ_retry_ct++ < sst_retry_count) {
2077                 rval = QUE_COMMAND;
2078         } else {
2079                 rval = COMMAND_DONE_ERROR;
2080         }
2081
2082         SST_LOG(targ->targ_devp, SST_CE_DEBUG1, "Cmd incomplete, %s\n",
2083             (rval == COMMAND_DONE_ERROR) ? "giving up" : "retrying");
2084
2085         return (rval);
2086 }
2087
2088
2089 /*
2090  * Decode sense data
2091  */
2092 static int
2093 sst_handle_sense(struct scsi_target *targ, struct buf *bp)
2094 {
2095         struct scsi_pkt *rqpkt = targ->targ_rqs;
2096         register        rval = COMMAND_DONE_ERROR;
2097         int             level, amt;
2098
2099
2100         if (SCBP(rqpkt)->sts_busy) {
2101                 if (targ->targ_retry_ct++ < sst_retry_count) {
2102                         (void) timeout(sst_restart, (caddr_t)targ,
2103                             SST_BSY_TIMEOUT);
2104                         rval = JUST_RETURN;
2105                 }
2106                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1, "Target Busy, %s\n",
2107                     (rval == JUST_RETURN) ? "restarting" : "giving up");
2108                 return (rval);
2109         }
2110
2111         if (SCBP(rqpkt)->sts_chk) {
2112                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2113                     "Check Condition on Request Sense!\n");
2114                 return (rval);
2115         }
2116
2117         amt = SENSE_LENGTH - rqpkt->pkt_resid;
2118         if ((rqpkt->pkt_state & STATE_XFERRED_DATA) == 0 || amt == 0) {
2119                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "no sense data\n");
2120                 return (rval);
2121         }
2122
2123         /*
2124          * Now, check to see whether we got enough sense data to make any
2125          * sense out if it (heh-heh).
2126          */
2127         if (amt < SUN_MIN_SENSE_LENGTH) {
2128                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2129                     "not enough sense data\n");
2130                 return (rval);
2131         }
2132
2133         if (sst_debug > 2) {
2134                 hex_print("sst Sense Data", targ->targ_devp->sd_sense,
2135                     SENSE_LENGTH);
2136         }
2137
2138         /*
2139          * Decode the sense data
2140          * Note: We only looking at the sense key here. Most devices
2141          *       have unique additional sense codes & qualifiers, so
2142          *       it's often more useful to look at them instead.
2143          *
2144          */
2145         switch (targ->targ_devp->sd_sense->es_key) {
2146         case KEY_NOT_READY:
2147                 /*
2148                  * If we get a not-ready indication, wait a bit and
2149                  * try it again, unless this is a special command with
2150                  * the 'fail on error' (FLAG_DIAGNOSE) option set.
2151                  */
2152                 if ((bp == targ->targ_sbufp) &&
2153                     (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2154                         rval = COMMAND_DONE_ERROR;
2155                         level = SCSI_ERR_FATAL;
2156                 } else if (targ->targ_retry_ct++ < sst_retry_count) {
2157                         (void) timeout(sst_restart, (caddr_t)targ,
2158                             SST_BSY_TIMEOUT);
2159                         rval = JUST_RETURN;
2160                         level = SCSI_ERR_RETRYABLE;
2161                 } else {
2162                         rval = COMMAND_DONE_ERROR;
2163                         level = SCSI_ERR_FATAL;
2164                 }
2165                 break;
2166
2167         case KEY_ABORTED_COMMAND:
2168         case KEY_UNIT_ATTENTION:
2169                 rval = QUE_COMMAND;
2170                 level = SCSI_ERR_INFO;
2171                 break;
2172         case KEY_RECOVERABLE_ERROR:
2173         case KEY_NO_SENSE:
2174                 rval = COMMAND_DONE;
2175                 level = SCSI_ERR_RECOVERED;
2176                 break;
2177         case KEY_HARDWARE_ERROR:
2178         case KEY_MEDIUM_ERROR:
2179         case KEY_MISCOMPARE:
2180         case KEY_VOLUME_OVERFLOW:
2181         case KEY_WRITE_PROTECT:
2182         case KEY_BLANK_CHECK:
2183         case KEY_ILLEGAL_REQUEST:
2184         default:
2185                 rval = COMMAND_DONE_ERROR;
2186                 level = SCSI_ERR_FATAL;
2187                 break;
2188         }
2189
2190         /*
2191          * If this was for a special command, check the options
2192          */
2193         if (bp == targ->targ_sbufp) {
2194                 if ((rval == QUE_COMMAND) &&
2195                     (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2196                         rval = COMMAND_DONE_ERROR;
2197                 }
2198                 if (((targ->targ_pkt->pkt_flags & FLAG_SILENT) == 0) ||
2199                     sst_debug) {
2200                         scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2201                             level, bp->b_blkno, 0, sst_cmds,
2202                             targ->targ_devp->sd_sense);
2203                 }
2204         } else if ((level >= sst_error_reporting) || sst_debug) {
2205                 scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2206                     level, bp->b_blkno, 0, sst_cmds, targ->targ_devp->sd_sense);
2207         }
2208
2209         return (rval);
2210 }
2211
2212
2213 #define ARQP(pktp)      ((struct scsi_arq_status *)((pktp)->pkt_scbp))
2214
2215 /*
2216  * Decode auto-rqsense data
2217  */
2218 static int
2219 sst_handle_arq(struct scsi_pkt *pktp, struct scsi_target *targ, struct buf *bp)
2220 {
2221         int     level, amt, rval = COMMAND_DONE_ERROR;
2222
2223         SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Auto Request Sense done\n");
2224
2225         if (ARQP(pktp)->sts_rqpkt_status.sts_chk) {
2226                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2227                     "Check Condition on Auto Request Sense!\n");
2228                 return (rval);
2229         }
2230
2231         amt = SENSE_LENGTH - ARQP(pktp)->sts_rqpkt_resid;
2232         if ((ARQP(pktp)->sts_rqpkt_state & STATE_XFERRED_DATA) == 0 ||
2233             amt == 0) {
2234                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "no auto sense data\n");
2235                 return (rval);
2236         }
2237
2238         /*
2239          * Stuff the sense data pointer into sd_sense
2240          */
2241         targ->targ_devp->sd_sense = &(ARQP(pktp)->sts_sensedata);
2242
2243         /*
2244          * Now, check to see whether we got enough sense data to make any
2245          * sense out if it (heh-heh).
2246          */
2247         if (amt < SUN_MIN_SENSE_LENGTH) {
2248                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2249                     "not enough auto sense data\n");
2250                 return (rval);
2251         }
2252
2253         if (sst_debug > 2) {
2254                 hex_print("sst Auto Sense Data",
2255                     &(ARQP(pktp)->sts_sensedata), SENSE_LENGTH);
2256         }
2257
2258         /*
2259          * Decode the sense data
2260          * Note: I'm only looking at the sense key here. Most devices
2261          *       have unique additional sense codes & qualifiers, so
2262          *       it's often more useful to look at them instead.
2263          *
2264          */
2265         switch (ARQP(pktp)->sts_sensedata.es_key) {
2266         case KEY_NOT_READY:
2267                 /*
2268                  * If we get a not-ready indication, wait a bit and
2269                  * try it again, unless this is a special command with
2270                  * the 'fail on error' (FLAG_DIAGNOSE) option set.
2271                  */
2272                 if ((bp == targ->targ_sbufp) &&
2273                     (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2274                         rval = COMMAND_DONE_ERROR;
2275                         level = SCSI_ERR_FATAL;
2276                 } else if (targ->targ_retry_ct++ < sst_retry_count) {
2277                         (void) timeout(sst_restart, (caddr_t)targ,
2278                             SST_BSY_TIMEOUT);
2279                         rval = JUST_RETURN;
2280                         level = SCSI_ERR_RETRYABLE;
2281                 } else {
2282                         rval = COMMAND_DONE_ERROR;
2283                         level = SCSI_ERR_FATAL;
2284                 }
2285                 break;
2286
2287         case KEY_ABORTED_COMMAND:
2288         case KEY_UNIT_ATTENTION:
2289                 rval = QUE_COMMAND;
2290                 level = SCSI_ERR_INFO;
2291                 break;
2292         case KEY_RECOVERABLE_ERROR:
2293         case KEY_NO_SENSE:
2294                 rval = COMMAND_DONE;
2295                 level = SCSI_ERR_RECOVERED;
2296                 break;
2297         case KEY_HARDWARE_ERROR:
2298         case KEY_MEDIUM_ERROR:
2299         case KEY_MISCOMPARE:
2300         case KEY_VOLUME_OVERFLOW:
2301         case KEY_WRITE_PROTECT:
2302         case KEY_BLANK_CHECK:
2303         case KEY_ILLEGAL_REQUEST:
2304         default:
2305                 rval = COMMAND_DONE_ERROR;
2306                 level = SCSI_ERR_FATAL;
2307                 break;
2308         }
2309
2310         /*
2311          * If this was for a special command, check the options
2312          */
2313         if (bp == targ->targ_sbufp) {
2314                 if ((rval == QUE_COMMAND) &&
2315                     (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2316                         rval = COMMAND_DONE_ERROR;
2317                 }
2318                 if (((targ->targ_pkt->pkt_flags & FLAG_SILENT) == 0) ||
2319                     sst_debug) {
2320                         scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2321                             level, bp->b_blkno, 0, sst_cmds,
2322                             targ->targ_devp->sd_sense);
2323                 }
2324         } else if ((level >= sst_error_reporting) || sst_debug) {
2325                 scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2326                     level, bp->b_blkno, 0, sst_cmds, targ->targ_devp->sd_sense);
2327         }
2328
2329         return (rval);
2330 }
2331
2332
2333 /*
2334  * Command completion routine. Check the returned status of the
2335  * command
2336  */
2337 static int
2338 sst_check_error(struct scsi_target *targ, struct buf *bp)
2339 {
2340         struct scsi_pkt *pkt = targ->targ_pkt;
2341         int             action;
2342         struct sst_private *sstprivp = (struct sst_private *)(pkt->pkt_private);
2343
2344         if (SCBP(pkt)->sts_busy) {
2345                 /*
2346                  * Target was busy. If we're not out of retries, call
2347                  * timeout to restart in a bit; otherwise give up and
2348                  * reset the target. If the fail on error flag is
2349                  * set, give up immediately.
2350                  */
2351                 int tval = (pkt->pkt_flags & FLAG_DIAGNOSE) ? 0
2352                                                             : SST_BSY_TIMEOUT;
2353
2354                 if (SCBP(pkt)->sts_is) {
2355                         /*
2356                          * Implicit assumption here is that a device
2357                          * will only be reserved long enough to
2358                          * permit a single i/o operation to complete.
2359                          */
2360                         tval = sst_io_time * drv_usectohz(1000000);
2361                 }
2362
2363                 if ((int)targ->targ_retry_ct++ < sst_retry_count) {
2364                         if (tval) {
2365                                 (void) timeout(sst_restart, (caddr_t)targ,
2366                                     tval);
2367                                 action = JUST_RETURN;
2368                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2369                                     "Target busy, retrying\n");
2370                         } else {
2371                                 action = COMMAND_DONE_ERROR;
2372                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2373                                     "Target busy, no retries\n");
2374                         }
2375                 } else {
2376                         /*
2377                          * WARNING: See the warning in sst_handle_incomplete
2378                          */
2379                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2380                             "Resetting Target\n");
2381                         if (!scsi_reset(ROUTE(targ), RESET_TARGET)) {
2382 #ifdef SCSI_BUS_RESET
2383                                 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2384                                     "Resetting SCSI Bus\n");
2385                                 if (!scsi_reset(ROUTE(targ), RESET_ALL)) {
2386                                         sst_log(targ->targ_devp, CE_WARN,
2387                                             "SCSI bus reset failed\n");
2388                                 }
2389 #else SCSI_BUS_RESET
2390                                 sst_log(targ->targ_devp, CE_WARN,
2391                                     "Reset Target failed\n");
2392 #endif SCSI_BUS_RESET
2393                         }
2394                         action = COMMAND_DONE_ERROR;
2395                 }
2396         } else if (SCBP(pkt)->sts_chk) {
2397                 action = QUE_SENSE;     /* check condition - get sense */
2398                 if (targ->targ_arq) {
2399                         SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
2400                             "Check Condition with Auto Sense enabled!\n");
2401                 } else {
2402                         SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2403                             "Check Condition\n");
2404                 }
2405         } else {
2406                 targ->targ_retry_ct = 0;
2407                 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Command Complete\n");
2408                 /*
2409                  * pkt_resid will reflect, at this point, a residual
2410                  * of how many bytes were not transferred; a non-zero
2411                  * pkt_resid is an error.
2412                  */
2413                 if (pkt->pkt_resid) {
2414                         action = COMMAND_DONE;
2415                         bp->b_resid += pkt->pkt_resid;
2416                 } else {
2417                         sstprivp->priv_amtdone += sstprivp->priv_amt;
2418                         if (sstprivp->priv_amtdone < bp->b_bcount)
2419                                 action = CONTINUE_PKT;
2420                         else
2421                                 action = COMMAND_DONE;
2422                 }
2423         }
2424         return (action);
2425 }
2426
2427
2428 /*
2429  * -----------------------------------------------------------------------------
2430  *      Error Message Data and Routines
2431  */
2432
2433 /*
2434  * Log a message to the console and/or syslog with cmn_err
2435  */
2436 /*ARGSUSED*/
2437 static void
2438 sst_log(struct scsi_device *devp, int level, const char *fmt, ...)
2439 {
2440         auto char name[16];
2441         auto char buf[256];
2442         va_list ap;
2443
2444         if (devp) {
2445                 (void) sprintf(name, "%s%d", ddi_get_name(devp->sd_dev),
2446                     ddi_get_instance(devp->sd_dev));
2447         } else {
2448                 (void) sprintf(name, "sst");
2449         }
2450
2451         va_start(ap, fmt);
2452         (void) vsprintf(buf, fmt, ap);
2453         va_end(ap);
2454
2455         switch (level) {
2456         case CE_CONT:
2457         case CE_NOTE:
2458         case CE_WARN:
2459         case CE_PANIC:
2460                 cmn_err(level, "%s:\t%s", name, buf);
2461                 break;
2462
2463         case SST_CE_DEBUG4: if (sst_debug < 4) break;
2464         /*FALLTHROUGH*/
2465         case SST_CE_DEBUG3: if (sst_debug < 3) break;
2466         /*FALLTHROUGH*/
2467         case SST_CE_DEBUG2: if (sst_debug < 2) break;
2468         /*FALLTHROUGH*/
2469         case SST_CE_DEBUG1:
2470         /*FALLTHROUGH*/
2471         default:
2472                 cmn_err(CE_CONT, "^%s:\t%s", name, buf);
2473                 break;
2474         }
2475 }
2476
2477 /*
2478  * Print a readable error message
2479  * Note: This uses the arrays 'sst_cmds' and 'sst_errors' defined in
2480  *       this file.
2481  */
2482
2483 #define BUFSZ   256
2484
2485 /*
2486  * Print a buffer readably
2487  */
2488 static void
2489 hex_print(char *msg, void *cptr, int len)
2490 {
2491         int i = 0, j;
2492         char    buf[BUFSZ];
2493         char    *cp = cptr;
2494
2495         bzero(buf, BUFSZ);
2496         for (i = 0; i < len; i++) {
2497                 /*
2498                  * make sure there's room for longest %x (i.e., 8 for
2499                  * a negative number) plus space (1) plus zero (1)
2500                  */
2501                 if ((j = strlen(buf)) >= (BUFSZ - 10)) {
2502                         buf[BUFSZ-2] = '>';
2503                         buf[BUFSZ-1] = 0;
2504                         break;          /* cp too long, give up */
2505                 }
2506                 (void) sprintf(&buf[j], "%x ", cp[i]);
2507         }
2508         cmn_err(CE_CONT, "^%s: %s\n", msg, buf);
2509 }
2510
2511 static void
2512 sst_dump_cdb(struct scsi_target *tgt, struct scsi_pkt *pkt, int cdblen)
2513 {
2514         static char     hex[] = "0123456789abcdef";
2515         char            buf [256];
2516         u_char          *cdb;
2517         char            *p;
2518         int             i;
2519
2520         (void) sprintf(buf, "CDB = [");
2521         p = &buf[strlen(buf)];
2522         cdb = pkt->pkt_cdbp;
2523         for (i = 0; i < cdblen; i++, cdb++) {
2524                 if (i > 0)
2525                         *p++ = ' ';
2526                 *p++ = hex[(*cdb >> 4) & 0x0f];
2527                 *p++ = hex[*cdb & 0x0f];
2528         }
2529         *p++ = ']';
2530         *p++ = '\n';
2531         *p = 0;
2532         sst_log(tgt->targ_devp, CE_CONT, buf);
2533 }