2 * Copyright (c) 1997, by Sun Microsystems, Inc.
7 * sst.c - Simple SCSI Target driver; a template character SCSA target
8 * driver for Solaris 2.x.
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.
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.
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.
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.
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.
33 * Sun Microsystems, Inc.
35 * Palo Alto, California 94303
37 * ------------------------------------------------------------------
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
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"
48 * % cc -D_KERNEL -c sst.c
49 * % ld -r sst.o -o sst
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.
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
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.
70 #pragma ident "@(#)sst.c 1.23 97/10/03 SMI"
73 * Includes, Declarations and Local Data
76 #include <sys/scsi/scsi.h>
85 * Local Function Prototypes
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 *);
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);
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,
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,
120 static int sst_io_time = SST_IO_TIME;
121 static int sst_retry_count = SST_RETRY_COUNT;
122 static void *sst_state;
125 * Errors at or above this level will be reported
127 static int32_t sst_error_reporting = SCSI_ERR_RETRYABLE;
130 * Enable the driver debugging code if DEBUG is defined (DEBUG also
131 * enables other debugging code, e.g. ASSERT statements).
139 * Debug message control
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).
152 static int sst_debug = 3;
153 static int sst_debug_cdb = 1;
155 static int sst_debug = 0;
156 static int sst_debug_cdb = 0;
157 #endif /* SST_DEBUG */
159 #define SST_DUMP_CDB(tgt, pkt, cdblen) \
160 if (sst_debug_cdb) { \
161 sst_dump_cdb((tgt), (pkt), (cdblen)); \
165 * Array of commands supported by the device, suitable for
167 * Note: Add or remove commands here as appropriate for
170 static struct scsi_key_strings sst_cmds[] = {
171 0x00, "test unit ready",
172 0x01, "rezero/rewind",
173 0x03, "request sense",
175 0x05, "read block limits",
180 0x0f, "read reverse",
181 0x10, "write file mark",
184 0x14, "recover buffered data",
191 0x1b, "start/stop/load",
193 0x37, "read defect data",
198 * Module Loading/Unloading and Autoconfiguration Routines
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.
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
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 */
218 sst_read, /* c read */
219 sst_write, /* c write */
220 sst_ioctl, /* c ioctl */
221 nodev, /* c devmap */
223 nodev, /* c segmap */
224 nochpoll, /* c poll */
225 ddi_prop_op, /* cb_prop_op */
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 */
235 * dev_ops(9S) structure, defined in sys/devops.h.
236 * Device Operations table, for autoconfiguration
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).
244 static int sst_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
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);
251 static int sst_doattach(dev_info_t *dip);
252 static int sst_dodetach(dev_info_t *dip);
254 static struct dev_ops sst_ops = {
255 DEVO_REV, /* devo_rev, */
258 nulldev, /* identify */
259 sst_probe, /* probe */
260 sst_attach, /* attach */
261 sst_detach, /* detach */
263 &sst_cb_ops, /* driver operations */
264 (struct bus_ops *)0, /* bus operations */
265 sst_power /* power */
269 * Module Loading and Unloading
270 * See modctl.h for external structure definitions.
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 */
279 static struct modlinkage modlinkage = {
280 MODREV_1, (void *)&modldrv, NULL
284 * Tell the system that we depend on the general scsi support routines,
285 * i.e the scsi "misc" module must be loaded
287 char _depends_on[] = "misc/scsi";
290 * _init(9E) - module Installation
292 * Install the driver and "pre-allocate" space for INIT_UNITS units,
293 * i.e. instances of the driver.
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);
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);
313 SST_LOG(0, SST_CE_DEBUG2, "_init succeeded\n");
319 * _fini(9E) - module removal
326 if ((e = mod_remove(&modlinkage)) != 0) {
327 SST_LOG(0, SST_CE_DEBUG1,
328 "_fini mod_remove failed, 0x%x\n", e);
332 ddi_soft_state_fini(&sst_state);
334 SST_LOG(0, SST_CE_DEBUG2, "_fini succeeded\n");
339 * _info(9E) - return module info
342 _info(struct modinfo *modinfop)
344 SST_LOG(0, SST_CE_DEBUG2, "_info\n");
345 return (mod_info(&modlinkage, modinfop));
349 * Autoconfiguration Routines
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
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.
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().
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.
375 #define VIDSZ 8 /* Vendor Id length in Inquiry Data */
376 #define PIDSZ 16 /* Product Id length in Inquiry Data */
379 sst_probe(dev_info_t *dip)
381 register struct scsi_device *devp;
382 int err, rval = DDI_PROBE_FAILURE;
384 char vpid[VIDSZ+PIDSZ+1];
386 devp = (struct scsi_device *)ddi_get_driver_private(dip);
387 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
389 lun = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
392 SST_LOG(devp, SST_CE_DEBUG2, "sst_probe: target %d, lun %d\n",
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.
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",
405 rval = DDI_PROBE_FAILURE;
408 case SCSIPROBE_NONCCS:
409 case SCSIPROBE_NOMEM:
410 case SCSIPROBE_FAILURE:
412 SST_LOG(devp, SST_CE_DEBUG1,
413 "sst_probe: scsi_slave failed, 0x%x\n", err);
414 rval = DDI_PROBE_FAILURE;
417 case SCSIPROBE_EXISTS:
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
430 switch (devp->sd_inq->inq_dtype) {
431 case DTYPE_PROCESSOR:
434 case DTYPE_SEQUENTIAL:
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).
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",
456 rval = DDI_PROBE_SUCCESS;
459 case DTYPE_NOTPRESENT:
460 sst_log(devp, CE_NOTE,
461 "Target reports no device present\n");
462 rval = DDI_PROBE_FAILURE;
466 sst_log(devp, CE_NOTE,
467 "Unrecognized device type: 0x%x\n",
468 devp->sd_inq->inq_dtype);
469 rval = DDI_PROBE_FAILURE;
475 * scsi_unprobe() must be called even if scsi_probe() failed
489 sst_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
492 struct scsi_target *targ;
493 struct scsi_device *devp;
497 return (sst_doattach(dip));
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
509 instance = ddi_get_instance(dip);
510 targ = ddi_get_soft_state(sst_state, instance);
512 return (DDI_FAILURE);
513 mutex_enter(SST_MUTEX(targ));
514 targ->targ_suspended = 0;
516 /* wake up threads blocked in sst_strategy */
517 cv_broadcast(&targ->targ_suspend_cv);
519 mutex_exit(SST_MUTEX(targ));
521 return (DDI_SUCCESS);
525 * Power Management suspend
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.
531 instance = ddi_get_instance(dip);
532 targ = ddi_get_soft_state(sst_state, instance);
534 return (DDI_FAILURE);
535 mutex_enter(SST_MUTEX(targ));
537 targ->targ_pm_suspended = 0;
538 cv_broadcast(&targ->targ_suspend_cv);
540 mutex_exit(SST_MUTEX(targ));
542 return (DDI_SUCCESS);
545 SST_LOG(0, SST_CE_DEBUG1,
546 "sst_attach: unsupported cmd 0x%x\n", cmd);
547 return (DDI_FAILURE);
553 * Attach(9E) - DDI_ATTACH handling
555 * - create minor device nodes
556 * - initialize per-instance mutex's & condition variables
557 * - device-specific initialization (e.g. read disk label)
560 sst_doattach(dev_info_t *dip)
563 struct scsi_pkt *rqpkt;
564 struct scsi_target *targ;
565 struct scsi_device *devp;
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);
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.
577 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
578 SST_LOG(0, SST_CE_DEBUG1, "sst_attach: re-probe failed\n");
580 return (DDI_FAILURE);
583 if (ddi_soft_state_zalloc(sst_state, instance) != DDI_SUCCESS) {
585 return (DDI_FAILURE);
588 targ = ddi_get_soft_state(sst_state, instance);
589 devp->sd_private = (opaque_t)targ;
591 targ->targ_sbufp = getrbuf(KM_SLEEP);
592 if (targ->targ_sbufp == NULL) {
596 targ->targ_devp = devp;
599 * Set auto-rqsense, per-target; record whether it's allowed
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");
607 if (!targ->targ_arq) {
609 * Allocate a Request Sense packet
611 bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
612 SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
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);
624 devp->sd_sense = (struct scsi_extended_sense *)bp->b_un.b_addr;
626 (void) scsi_setup_cdb((union scsi_cdb *)rqpkt->pkt_cdbp,
627 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
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;
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>.
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");
654 * Initialize power management bookkeeping.
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");
662 pm_set_normal_power(dip, 0, 1);
663 targ->targ_power_level = 1;
665 SST_LOG(devp, SST_CE_DEBUG1,
666 "pm_create_component() failed\n");
667 ddi_remove_minor_node(dip, NULL);
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".
679 if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
680 "pm-hardware-state", "needs-suspend-resume") !=
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);
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.
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,
704 * Note: Do any other pre-open target initialization here,
705 * e.g. read/verify the disk label for a fixed-disk drive.
709 SST_LOG(devp, SST_CE_DEBUG2, "Attached sst driver\n");
710 targ->targ_state = SST_STATE_CLOSED;
711 return (DDI_SUCCESS);
715 scsi_free_consistent_buf(bp);
718 scsi_destroy_pkt(rqpkt);
720 if (targ->targ_sbufp) {
721 freerbuf(targ->targ_sbufp);
723 ddi_soft_state_free(sst_state, instance);
724 devp->sd_private = (opaque_t)0;
725 devp->sd_sense = (struct scsi_extended_sense *)0;
727 return (DDI_FAILURE);
735 sst_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
738 struct scsi_device *devp;
739 struct scsi_target *targ;
744 return (sst_dodetach(dip));
750 * To process DDI_SUSPEND, we must do the following:
752 * - wait until outstanding operations complete
753 * - block new operations
754 * - cancel pending timeouts
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.
762 instance = ddi_get_instance(dip);
763 targ = ddi_get_soft_state(sst_state, instance);
765 return (DDI_FAILURE);
768 mutex_enter(SST_MUTEX(targ));
769 targ->targ_suspended = 1;
772 * Wait here till outstanding operations complete
774 while (targ->targ_pkt_busy) {
775 cv_wait(&targ->targ_pkt_cv, SST_MUTEX(targ));
778 mutex_exit(SST_MUTEX(targ));
779 return (DDI_SUCCESS);
783 * Power Management suspend
785 * To process DDI_PM_SUSPEND, we must do the following:
787 * - if busy, fail DDI_PM_SUSPEND
789 * - cancel pending timeouts
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.
795 instance = ddi_get_instance(dip);
796 targ = ddi_get_soft_state(sst_state, instance);
798 return (DDI_FAILURE);
801 mutex_enter(SST_MUTEX(targ));
802 ASSERT(targ->targ_suspended == 0);
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.
809 if (targ->targ_pkt_busy) {
810 mutex_exit(SST_MUTEX(targ));
811 return (DDI_FAILURE);
814 targ->targ_pm_suspended = 1;
816 mutex_exit(SST_MUTEX(targ));
817 return (DDI_SUCCESS);
821 SST_LOG(0, SST_CE_DEBUG1,
822 "sst_detach: bad cmd 0x%x\n", cmd);
823 return (DDI_FAILURE);
828 * detach(9E) DDI_DETACH handling
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.
839 sst_dodetach(dev_info_t *dip)
842 struct scsi_device *devp;
843 struct scsi_target *targ;
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);
849 if ((targ = ddi_get_soft_state(sst_state, instance)) == NULL) {
850 SST_LOG(devp, CE_WARN, "No Target Struct for sst%d\n",
852 return (DDI_SUCCESS);
856 * Note: Do unit-specific detaching here; e.g. shut down the device
860 * Remove other data structures allocated in sst_attach()
862 cv_destroy(&targ->targ_sbuf_cv);
863 cv_destroy(&targ->targ_pkt_cv);
864 cv_destroy(&targ->targ_suspend_cv);
866 if (targ->targ_rqbp) {
867 scsi_free_consistent_buf(targ->targ_rqbp);
869 if (targ->targ_rqs) {
870 scsi_destroy_pkt(targ->targ_rqs);
872 if (targ->targ_sbufp) {
873 freerbuf(targ->targ_sbufp);
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);
881 return (DDI_SUCCESS);
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.
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.
896 sst_power(dev_info_t *dip, int component, int level)
899 struct scsi_device *devp;
900 struct scsi_target *targ;
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);
906 if ((targ = ddi_get_soft_state(sst_state, instance)) == NULL) {
907 SST_LOG(devp, CE_WARN, "No Target Struct for sst%d\n",
909 return (DDI_FAILURE);
912 mutex_enter(SST_MUTEX(targ));
914 if (targ->targ_power_level == level) {
915 mutex_exit(SST_MUTEX(targ));
916 return (DDI_SUCCESS);
920 * Set device's power level to 'level'
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;
926 mutex_exit(SST_MUTEX(targ));
927 return (DDI_SUCCESS);
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);
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.
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!
961 sst_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
963 register dev_t dev = *dev_p;
964 register struct scsi_target *targ;
966 SST_LOG(0, SST_CE_DEBUG2, "sst_open\n");
969 * Test the open type flag
971 if (otyp != OTYP_CHR) {
972 SST_LOG(0, SST_CE_DEBUG1,
973 "Unsupported open type %d\n", otyp);
977 targ = ddi_get_soft_state(sst_state, getminor(dev));
979 return (ENXIO); /* invalid minor number */
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
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
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));
1001 targ->targ_state = SST_STATE_OPEN; /* lock out other accesses */
1002 mutex_exit(SST_MUTEX(targ)); /* UNLOCK targ data struct */
1005 _NOTE(NO_COMPETING_THREADS_NOW);
1008 * Test to make sure unit still is powered on and is ready
1009 * by sending the SCSI Test Unit Ready command.
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.
1016 (void) sst_unit_ready(dev);
1018 if (sst_unit_ready(dev) == 0) {
1019 SST_LOG(0, SST_CE_DEBUG1, "sst%d_open: not ready\n",
1021 targ->targ_state = SST_STATE_CLOSED;
1024 _NOTE(COMPETING_THREADS_NOW);
1030 * Do any other initalization work here, e.g. send
1031 * Mode Sense/Select commands to get the target in the
1035 _NOTE(COMPETING_THREADS_NOW);
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.
1048 sst_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
1050 register struct scsi_target *targ;
1052 SST_LOG(0, SST_CE_DEBUG2, "sst_close\n");
1054 targ = ddi_get_soft_state(sst_state, getminor(dev));
1060 _NOTE(NO_COMPETING_THREADS_NOW);
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.
1074 targ->targ_state = SST_STATE_CLOSED;
1077 _NOTE(COMPETING_THREADS_NOW);
1086 * Device Configuration Routine
1087 * link instance number (unit) with dev_info structure
1091 sst_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1094 struct scsi_target *targ;
1095 int instance, error;
1097 SST_LOG(0, SST_CE_DEBUG2, "sst_info\n");
1100 case DDI_INFO_DEVT2DEVINFO:
1102 instance = getminor(dev);
1103 targ = ddi_get_soft_state(sst_state, instance);
1105 return (DDI_FAILURE);
1106 *result = (void *) targ->targ_devp->sd_dev;
1107 error = DDI_SUCCESS;
1109 case DDI_INFO_DEVT2INSTANCE:
1110 *result = (void *)getminor((dev_t)arg);
1111 error = DDI_SUCCESS;
1114 error = DDI_FAILURE;
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.
1131 sst_read(dev_t dev, struct uio *uio, cred_t *cred_p)
1133 SST_LOG(0, SST_CE_DEBUG2, "Read\n");
1134 return (physio(sst_strategy, (struct buf *)0, dev, B_READ,
1143 sst_write(dev_t dev, struct uio *uio, cred_t *cred_p)
1145 SST_LOG(0, SST_CE_DEBUG2, "Write\n");
1146 return (physio(sst_strategy, (struct buf *)0, dev, B_WRITE,
1152 * aread(9E) - asynchronous read
1156 sst_aread(dev_t dev, struct aio_req *aio, cred_t *credp)
1158 SST_LOG(0, SST_CE_DEBUG2, "aread\n");
1159 return (aphysio(sst_strategy, anocancel, dev, B_READ,
1164 * awrite(9E) - asynchronous write
1168 sst_awrite(dev_t dev, struct aio_req *aio, cred_t *credp)
1170 SST_LOG(0, SST_CE_DEBUG2, "awrite\n");
1171 return (aphysio(sst_strategy, anocancel, dev, B_WRITE,
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
1186 sst_strategy(struct buf *bp)
1188 struct scsi_target *targ;
1190 targ = ddi_get_soft_state(sst_state, getminor(bp->b_edev));
1192 bp->b_resid = bp->b_bcount;
1193 bioerror(bp, ENXIO);
1198 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_strategy\n");
1201 * Mark component busy so we won't get powered down
1202 * while trying to resume.
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");
1209 mutex_enter(SST_MUTEX(targ));
1212 * if we are suspended, wait till we are resumed again
1216 * Block commands while suspended via DDI_SUSPEND
1218 while (targ->targ_suspended) {
1219 cv_wait(&targ->targ_suspend_cv, SST_MUTEX(targ));
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
1228 if (targ->targ_power_level == 0) {
1229 mutex_exit(SST_MUTEX(targ));
1230 if (ddi_dev_is_needed(SST_DEVINFO(targ), 0, 1) !=
1232 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1233 "ddi_dev_is_needed() failed\n");
1235 mutex_enter(SST_MUTEX(targ));
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
1246 while (targ->targ_pm_suspended) {
1247 cv_wait(&targ->targ_suspend_cv, SST_MUTEX(targ));
1249 } while (targ->targ_suspended || (targ->targ_power_level == 0));
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.
1256 while (targ->targ_pkt_busy) {
1257 cv_wait(&targ->targ_pkt_cv, SST_MUTEX(targ));
1259 targ->targ_pkt_busy = 1; /* mark busy */
1260 mutex_exit(SST_MUTEX(targ));
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;
1269 pm_idle_component(SST_DEVINFO(targ), 0);
1275 SST_DUMP_CDB(targ, targ->targ_pkt, CDB_GROUP0);
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;
1283 pm_idle_component(SST_DEVINFO(targ), 0);
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
1304 sst_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1305 cred_t *cred_p, int *rval_p)
1308 struct scsi_target *targ;
1309 struct uscsi_cmd uscsi_cmd;
1310 struct uscsi_cmd *ucmd = &uscsi_cmd;
1311 char cmdblk[CDB_GROUP0];
1313 #ifdef _MULTI_DATAMODEL
1315 * For use when a 32-bit app makes an ioctl into a 64-bit driver
1317 struct sst_uscsi_cmd32 uscsi_cmd32;
1318 struct sst_uscsi_cmd32 *ucmd32 = &uscsi_cmd32;
1320 #endif /* _MULTI_DATAMODEL */
1323 targ = ddi_get_soft_state(sst_state, getminor(dev));
1325 return (ENXIO); /* invalid minor number */
1328 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_ioctl: cmd = 0x%x\n", cmd);
1330 bzero(ucmd, sizeof (struct uscsi_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;
1343 err = sst_ioctl_cmd(dev, ucmd,
1344 UIO_SYSSPACE, UIO_SYSSPACE, mode);
1347 case SSTIOC_ERRLEV: /* Set the error reporting level */
1348 if (ddi_copyin((void *)arg, &sst_error_reporting,
1349 sizeof (int32_t), mode)) {
1356 * Run a generic ucsi.h command.
1359 #if 0 /* allow non-root to do this */
1361 * Check root permissions
1363 if (drv_priv(cred_p) != 0) {
1368 #ifdef _MULTI_DATAMODEL
1369 switch (model = ddi_model_convert_from(mode & FMODELS)) {
1370 case DDI_MODEL_ILP32:
1372 if (ddi_copyin((void *)arg, ucmd32,
1373 sizeof (*ucmd32), mode)) {
1377 * Convert 32-bit ILP32 application's uscsi cmd
1378 * into 64-bit LP64 equivalent for internal use
1380 sst_uscsi_cmd32touscsi_cmd(ucmd32, ucmd);
1383 case DDI_MODEL_NONE:
1384 if (ddi_copyin((void *)arg, ucmd,
1385 sizeof (*ucmd), mode)) {
1391 #else /* ! _MULTI_DATAMODEL */
1392 if (ddi_copyin((void *)arg, ucmd,
1393 sizeof (*ucmd), mode)) {
1396 #endif /* _MULTI_DATAMODEL */
1398 err = sst_ioctl_cmd(dev, ucmd,
1399 UIO_USERSPACE, UIO_USERSPACE, mode);
1401 #ifdef _MULTI_DATAMODEL
1403 case DDI_MODEL_ILP32:
1405 * Convert LP64 back to IPL32 before copyout
1407 sst_uscsi_cmdtouscsi_cmd32(ucmd, ucmd32);
1409 if (ddi_copyout(ucmd32, (void *)arg,
1410 sizeof (*ucmd32), mode)) {
1415 case DDI_MODEL_NONE:
1416 if (ddi_copyout(ucmd, (void *)arg,
1417 sizeof (*ucmd), mode)) {
1422 #else /* ! _MULTI_DATAMODE */
1423 if (ddi_copyout(ucmd, (void *)arg,
1424 sizeof (*ucmd), mode)) {
1427 #endif /* _MULTI_DATAMODE */
1440 * Run a command for user (from sst_ioctl) or from someone else in the driver.
1442 * cdbspace is for address space of cdb; dataspace is for address space
1443 * of the buffer - user or kernel.
1446 sst_ioctl_cmd(dev_t dev, struct uscsi_cmd *scmd,
1447 enum uio_seg cdbspace, enum uio_seg dataspace,
1450 caddr_t cdb, user_cdbp;
1453 struct scsi_target *targ;
1456 targ = ddi_get_soft_state(sst_state, getminor(dev));
1461 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_ioctl_cmd\n");
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.
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);
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
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;
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.
1499 mutex_enter(SST_MUTEX(targ));
1500 while (targ->targ_sbuf_busy) {
1501 cv_wait(&targ->targ_sbuf_cv, SST_MUTEX(targ));
1503 targ->targ_sbuf_busy = 1;
1504 mutex_exit(SST_MUTEX(targ));
1506 bp = targ->targ_sbufp;
1508 if (scmd->uscsi_buflen) {
1510 * We're sending/receiving data; create a uio structure and
1511 * call physio to do the right things.
1513 auto struct iovec aiov;
1514 auto struct uio auio;
1515 register struct uio *uio = &auio;
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;
1523 uio->uio_iovcnt = 1;
1524 uio->uio_resid = scmd->uscsi_buflen;
1525 uio->uio_segflg = dataspace;
1526 uio->uio_offset = 0;
1530 * Let physio do the rest...
1532 bp->b_private = (void *) scmd;
1533 err = physio(sst_strategy, bp, dev, rw, minphys, uio);
1536 * No data transfer, we can call sst_strategy directly
1538 bp->b_private = (void *) scmd;
1539 bp->b_flags = B_BUSY | rw;
1541 bp->b_bcount = bp->b_blkno = 0;
1542 (void) sst_strategy(bp);
1547 * get the status block, if any, and
1548 * release any resources that we had.
1550 scmd->uscsi_status = 0;
1551 scmd->uscsi_status = SCBP_C(targ->targ_pkt);
1554 * Lock the targ struct. Clearing the 'busy' flag and signalling
1555 * must be made atomic.
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 */
1566 kmem_free(scmd->uscsi_cdb, (size_t)scmd->uscsi_cdblen);
1567 scmd->uscsi_cdb = user_cdbp; /* restore the user's pointer */
1573 * Check to see if the unit will respond to a Test Unit Ready
1574 * Returns true or false
1577 sst_unit_ready(dev_t dev)
1579 auto struct uscsi_cmd scmd, *com = &scmd;
1580 auto char cmdblk[CDB_GROUP0];
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;
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);
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.
1607 sst_done(struct scsi_target *targ, register struct buf *bp)
1609 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_done\n");
1612 * Lock the targ struct; we don't want targ_pkt to change
1613 * between taking a copy of it and zeroing it.
1615 mutex_enter(SST_MUTEX(targ));
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.
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);
1629 mutex_exit(SST_MUTEX(targ)); /* UNLOCK the targ struct */
1632 * Tell interested parties that the i/o is done
1635 pm_idle_component(SST_DEVINFO(targ), 0);
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.
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
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.
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.
1665 static struct scsi_pkt *
1666 sst_make_cmd(struct scsi_target *targ, struct buf *bp, struct scsi_pkt *pkt)
1668 struct uscsi_cmd *scmd = (struct uscsi_cmd *)bp->b_private;
1672 struct sst_private *sstprivp;
1674 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
1675 "sst_make_cmd: block %d, count=%d\n", bp->b_blkno, bp->b_bcount);
1678 * If called with pkt == NULL, this is first time, and we
1679 * must allocate and initialize the packet
1681 if (pkt == (struct scsi_pkt *)NULL)
1685 blkcnt = bp->b_bcount >> DEV_BSHIFT;
1687 if (bp != targ->targ_sbufp) {
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
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,
1700 sizeof (struct scsi_arq_status) : 1,
1701 sizeof (struct sst_private), PKT_DMA_PARTIAL,
1703 if (pkt == (struct scsi_pkt *)0) {
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
1714 pkt = scsi_init_pkt(ROUTE(targ), pkt, bp, 0,
1716 sizeof (struct scsi_arq_status) : 1,
1717 sizeof (struct sst_private),
1718 PKT_DMA_PARTIAL, NULL_FUNC, 0);
1721 SST_LOG(targ->targ_devp, SST_CE_DEBUG3,
1722 "sst_make_cmd: pkt_resid: %d\n", pkt->pkt_resid);
1724 sstprivp = (struct sst_private *)pkt->pkt_private;
1727 sstprivp->priv_bp = bp;
1728 sstprivp->priv_amtdone = 0;
1729 sstprivp->priv_amt = bp->b_bcount - pkt->pkt_resid;
1732 sst_fill_cdb(pkt, targ, bp, flags);
1733 pkt->pkt_flags = flags;
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.
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;
1755 pkt = scsi_init_pkt(ROUTE(targ),
1756 (struct scsi_pkt *)NULL,
1757 bp->b_bcount ? bp : 0,
1760 sizeof (struct scsi_arq_status) : 1,
1761 sizeof (struct sst_private), PKT_DMA_PARTIAL,
1767 pkt = scsi_init_pkt(ROUTE(targ), pkt,
1768 bp->b_bcount ? bp : 0, 0,
1770 sizeof (struct scsi_arq_status) : 1,
1771 sizeof (struct sst_private),
1772 PKT_DMA_PARTIAL, NULL_FUNC, 0);
1775 sstprivp = (struct sst_private *)pkt->pkt_private;
1777 sstprivp->priv_bp = bp;
1778 sstprivp->priv_amtdone = 0;
1779 sstprivp->priv_amt = bp->b_bcount - pkt->pkt_resid;
1782 (void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
1783 scmd->uscsi_cdb[0], 0, 0, 0);
1785 bcopy(scmd->uscsi_cdb, pkt->pkt_cdbp,
1786 scmd->uscsi_cdblen);
1787 tval = scmd->uscsi_timeout;
1789 /* a timeout of 0 (ie. no timeout) is bad practice */
1795 pkt->pkt_comp = sst_callback;
1796 pkt->pkt_time = tval;
1799 if (sst_debug > 2) {
1800 hex_print("sst_make_cmd, CDB", pkt->pkt_cdbp, 6);
1809 * Stuff CDB with SCSI command for current I/O request
1813 sst_fill_cdb(struct scsi_pkt *pkt, struct scsi_target *targ, struct buf *bp,
1816 struct sst_private *sstprivp;
1820 if ((scsi_options & SCSI_OPTIONS_DR) == 0)
1821 flags |= FLAG_NODISCON;
1823 sstprivp = (struct sst_private *)(pkt->pkt_private);
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; */
1829 SST_LOG(targ->targ_devp, SST_CE_DEBUG3,
1830 "sst_fill_cdb: blkno %x len %x\n", blkno, len);
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),
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;
1846 com = (bp->b_flags & B_READ) ? SCMD_READ : SCMD_WRITE;
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.
1858 if (targ->targ_devp->sd_inq->inq_dtype == DTYPE_SEQUENTIAL) {
1862 (void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
1863 com, blkno, len >> DEV_BSHIFT, 0);
1864 pkt->pkt_flags = flags;
1866 if (targ->targ_devp->sd_inq->inq_dtype == DTYPE_SEQUENTIAL) {
1867 ((union scsi_cdb *)(pkt->pkt_cdbp))->t_code = 1;
1873 * -----------------------------------------------------------------------------
1874 * Interrupt Service Routines
1878 * Restart a command - the device was either busy or not ready
1881 sst_restart(caddr_t arg)
1883 struct scsi_target *targ = (struct scsi_target *)arg;
1884 struct scsi_pkt *pkt;
1887 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "sst_restart\n");
1890 * No need to lock the targ structure because any other threads
1891 * will wait in sst_strategy for the packet.
1893 pkt = targ->targ_pkt;
1894 bp = ((struct sst_private *)pkt->pkt_private)->priv_bp;
1897 struct scsi_pkt *pkt = targ->targ_pkt;
1899 if (pkt->pkt_flags & FLAG_SENSING) {
1900 pkt = targ->targ_rqs;
1903 SST_DUMP_CDB(targ, targ->targ_pkt, CDB_GROUP0);
1905 if (scsi_transport(pkt) != TRAN_ACCEPT) {
1906 bp->b_resid = bp->b_bcount;
1907 bioerror(bp, ENXIO);
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.
1922 sst_callback(struct scsi_pkt *pkt)
1924 struct scsi_target *targ;
1927 struct sst_private *sstprivp;
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));
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);
1937 mutex_enter(SST_MUTEX(targ));
1939 if (pkt->pkt_reason != CMD_CMPLT) {
1941 * The command did not complete. Retry if possible.
1943 action = sst_handle_incomplete(targ);
1944 } else if (targ->targ_arq && pkt->pkt_state & STATE_ARQ_DONE) {
1946 * The auto-rqsense happened, and the packet has a
1947 * filled-in scsi_arq_status structure, pointed to by
1950 action = sst_handle_arq(pkt, targ, bp);
1951 } else if (pkt->pkt_flags & FLAG_SENSING) {
1953 * We were running a REQUEST SENSE. Decode the
1954 * sense data and decide what to do next.
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);
1961 * Command completed and we're not getting sense. Check
1962 * for errors and decide what to do next.
1964 action = sst_check_error(targ, bp);
1966 mutex_exit(SST_MUTEX(targ));
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)->
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) {
1979 SST_LOG(targ->targ_devp, SST_CE_DEBUG1,
1980 "Request Sense transport failed\n");
1982 case COMMAND_DONE_ERROR:
1983 bp->b_resid = bp->b_bcount;
1984 bioerror(bp, ENXIO);
1987 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Command Done\n");
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);
2008 * Back from a chunk of a split-up bp. Do next chunk or
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;
2020 pm_idle_component(SST_DEVINFO(targ), 0);
2028 * Incomplete command handling. Figure out what to do based on
2029 * how far the command did get.
2032 sst_handle_incomplete(struct scsi_target *targ)
2034 register int rval = COMMAND_DONE_ERROR;
2035 register struct scsi_pkt *pkt = targ->targ_pkt;
2037 if (!targ->targ_arq && pkt->pkt_flags & FLAG_SENSING) {
2038 pkt = targ->targ_rqs;
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.
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");
2061 mutex_enter(SST_MUTEX(targ));
2062 return (COMMAND_DONE_ERROR);
2065 mutex_enter(SST_MUTEX(targ));
2069 * If we were running a request sense, try it again if possible.
2070 * Some devices can handle retries, others will not.
2072 if (pkt->pkt_flags & FLAG_SENSING) {
2073 if (targ->targ_retry_ct++ < sst_retry_count) {
2076 } else if (targ->targ_retry_ct++ < sst_retry_count) {
2079 rval = COMMAND_DONE_ERROR;
2082 SST_LOG(targ->targ_devp, SST_CE_DEBUG1, "Cmd incomplete, %s\n",
2083 (rval == COMMAND_DONE_ERROR) ? "giving up" : "retrying");
2093 sst_handle_sense(struct scsi_target *targ, struct buf *bp)
2095 struct scsi_pkt *rqpkt = targ->targ_rqs;
2096 register rval = COMMAND_DONE_ERROR;
2100 if (SCBP(rqpkt)->sts_busy) {
2101 if (targ->targ_retry_ct++ < sst_retry_count) {
2102 (void) timeout(sst_restart, (caddr_t)targ,
2106 SST_LOG(targ->targ_devp, SST_CE_DEBUG1, "Target Busy, %s\n",
2107 (rval == JUST_RETURN) ? "restarting" : "giving up");
2111 if (SCBP(rqpkt)->sts_chk) {
2112 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2113 "Check Condition on Request Sense!\n");
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");
2124 * Now, check to see whether we got enough sense data to make any
2125 * sense out if it (heh-heh).
2127 if (amt < SUN_MIN_SENSE_LENGTH) {
2128 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2129 "not enough sense data\n");
2133 if (sst_debug > 2) {
2134 hex_print("sst Sense Data", targ->targ_devp->sd_sense,
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.
2145 switch (targ->targ_devp->sd_sense->es_key) {
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.
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,
2160 level = SCSI_ERR_RETRYABLE;
2162 rval = COMMAND_DONE_ERROR;
2163 level = SCSI_ERR_FATAL;
2167 case KEY_ABORTED_COMMAND:
2168 case KEY_UNIT_ATTENTION:
2170 level = SCSI_ERR_INFO;
2172 case KEY_RECOVERABLE_ERROR:
2174 rval = COMMAND_DONE;
2175 level = SCSI_ERR_RECOVERED;
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:
2185 rval = COMMAND_DONE_ERROR;
2186 level = SCSI_ERR_FATAL;
2191 * If this was for a special command, check the options
2193 if (bp == targ->targ_sbufp) {
2194 if ((rval == QUE_COMMAND) &&
2195 (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2196 rval = COMMAND_DONE_ERROR;
2198 if (((targ->targ_pkt->pkt_flags & FLAG_SILENT) == 0) ||
2200 scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2201 level, bp->b_blkno, 0, sst_cmds,
2202 targ->targ_devp->sd_sense);
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);
2213 #define ARQP(pktp) ((struct scsi_arq_status *)((pktp)->pkt_scbp))
2216 * Decode auto-rqsense data
2219 sst_handle_arq(struct scsi_pkt *pktp, struct scsi_target *targ, struct buf *bp)
2221 int level, amt, rval = COMMAND_DONE_ERROR;
2223 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Auto Request Sense done\n");
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");
2231 amt = SENSE_LENGTH - ARQP(pktp)->sts_rqpkt_resid;
2232 if ((ARQP(pktp)->sts_rqpkt_state & STATE_XFERRED_DATA) == 0 ||
2234 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "no auto sense data\n");
2239 * Stuff the sense data pointer into sd_sense
2241 targ->targ_devp->sd_sense = &(ARQP(pktp)->sts_sensedata);
2244 * Now, check to see whether we got enough sense data to make any
2245 * sense out if it (heh-heh).
2247 if (amt < SUN_MIN_SENSE_LENGTH) {
2248 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2249 "not enough auto sense data\n");
2253 if (sst_debug > 2) {
2254 hex_print("sst Auto Sense Data",
2255 &(ARQP(pktp)->sts_sensedata), SENSE_LENGTH);
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.
2265 switch (ARQP(pktp)->sts_sensedata.es_key) {
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.
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,
2280 level = SCSI_ERR_RETRYABLE;
2282 rval = COMMAND_DONE_ERROR;
2283 level = SCSI_ERR_FATAL;
2287 case KEY_ABORTED_COMMAND:
2288 case KEY_UNIT_ATTENTION:
2290 level = SCSI_ERR_INFO;
2292 case KEY_RECOVERABLE_ERROR:
2294 rval = COMMAND_DONE;
2295 level = SCSI_ERR_RECOVERED;
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:
2305 rval = COMMAND_DONE_ERROR;
2306 level = SCSI_ERR_FATAL;
2311 * If this was for a special command, check the options
2313 if (bp == targ->targ_sbufp) {
2314 if ((rval == QUE_COMMAND) &&
2315 (targ->targ_pkt->pkt_flags & FLAG_DIAGNOSE)) {
2316 rval = COMMAND_DONE_ERROR;
2318 if (((targ->targ_pkt->pkt_flags & FLAG_SILENT) == 0) ||
2320 scsi_errmsg(targ->targ_devp, targ->targ_pkt, "sst",
2321 level, bp->b_blkno, 0, sst_cmds,
2322 targ->targ_devp->sd_sense);
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);
2334 * Command completion routine. Check the returned status of the
2338 sst_check_error(struct scsi_target *targ, struct buf *bp)
2340 struct scsi_pkt *pkt = targ->targ_pkt;
2342 struct sst_private *sstprivp = (struct sst_private *)(pkt->pkt_private);
2344 if (SCBP(pkt)->sts_busy) {
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.
2351 int tval = (pkt->pkt_flags & FLAG_DIAGNOSE) ? 0
2354 if (SCBP(pkt)->sts_is) {
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.
2360 tval = sst_io_time * drv_usectohz(1000000);
2363 if ((int)targ->targ_retry_ct++ < sst_retry_count) {
2365 (void) timeout(sst_restart, (caddr_t)targ,
2367 action = JUST_RETURN;
2368 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2369 "Target busy, retrying\n");
2371 action = COMMAND_DONE_ERROR;
2372 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2373 "Target busy, no retries\n");
2377 * WARNING: See the warning in sst_handle_incomplete
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");
2389 #else SCSI_BUS_RESET
2390 sst_log(targ->targ_devp, CE_WARN,
2391 "Reset Target failed\n");
2392 #endif SCSI_BUS_RESET
2394 action = COMMAND_DONE_ERROR;
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");
2402 SST_LOG(targ->targ_devp, SST_CE_DEBUG2,
2403 "Check Condition\n");
2406 targ->targ_retry_ct = 0;
2407 SST_LOG(targ->targ_devp, SST_CE_DEBUG2, "Command Complete\n");
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.
2413 if (pkt->pkt_resid) {
2414 action = COMMAND_DONE;
2415 bp->b_resid += pkt->pkt_resid;
2417 sstprivp->priv_amtdone += sstprivp->priv_amt;
2418 if (sstprivp->priv_amtdone < bp->b_bcount)
2419 action = CONTINUE_PKT;
2421 action = COMMAND_DONE;
2429 * -----------------------------------------------------------------------------
2430 * Error Message Data and Routines
2434 * Log a message to the console and/or syslog with cmn_err
2438 sst_log(struct scsi_device *devp, int level, const char *fmt, ...)
2445 (void) sprintf(name, "%s%d", ddi_get_name(devp->sd_dev),
2446 ddi_get_instance(devp->sd_dev));
2448 (void) sprintf(name, "sst");
2452 (void) vsprintf(buf, fmt, ap);
2460 cmn_err(level, "%s:\t%s", name, buf);
2463 case SST_CE_DEBUG4: if (sst_debug < 4) break;
2465 case SST_CE_DEBUG3: if (sst_debug < 3) break;
2467 case SST_CE_DEBUG2: if (sst_debug < 2) break;
2472 cmn_err(CE_CONT, "^%s:\t%s", name, buf);
2478 * Print a readable error message
2479 * Note: This uses the arrays 'sst_cmds' and 'sst_errors' defined in
2486 * Print a buffer readably
2489 hex_print(char *msg, void *cptr, int len)
2496 for (i = 0; i < len; i++) {
2498 * make sure there's room for longest %x (i.e., 8 for
2499 * a negative number) plus space (1) plus zero (1)
2501 if ((j = strlen(buf)) >= (BUFSZ - 10)) {
2504 break; /* cp too long, give up */
2506 (void) sprintf(&buf[j], "%x ", cp[i]);
2508 cmn_err(CE_CONT, "^%s: %s\n", msg, buf);
2512 sst_dump_cdb(struct scsi_target *tgt, struct scsi_pkt *pkt, int cdblen)
2514 static char hex[] = "0123456789abcdef";
2520 (void) sprintf(buf, "CDB = [");
2521 p = &buf[strlen(buf)];
2522 cdb = pkt->pkt_cdbp;
2523 for (i = 0; i < cdblen; i++, cdb++) {
2526 *p++ = hex[(*cdb >> 4) & 0x0f];
2527 *p++ = hex[*cdb & 0x0f];
2532 sst_log(tgt->targ_devp, CE_CONT, buf);