2 * Disk Array driver for HP Smart Array controllers, SCSI Tape module.
3 * (C) Copyright 2001, 2007 Hewlett-Packard Development Company, L.P.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 300, Boston, MA
19 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
21 * Author: Stephen M. Cameron
23 #ifdef CONFIG_CISS_SCSI_TAPE
25 /* Here we have code to present the driver as a scsi driver
26 as it is simultaneously presented as a block driver. The
27 reason for doing this is to allow access to SCSI tape drives
28 through the array controller. Note in particular, neither
29 physical nor logical disks are presented through the scsi layer. */
31 #include <linux/timer.h>
32 #include <linux/completion.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
36 #include <asm/atomic.h>
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_host.h>
42 #include "cciss_scsi.h"
44 #define CCISS_ABORT_MSG 0x00
45 #define CCISS_RESET_MSG 0x01
47 /* some prototypes... */
53 unsigned int use_unit_num, /* 0: address the controller,
54 1: address logical volume log_unit,
55 2: address is in scsi3addr */
56 unsigned int log_unit,
58 unsigned char *scsi3addr,
62 static int cciss_scsi_proc_info(
64 char *buffer, /* data buffer */
65 char **start, /* where data in buffer starts */
66 off_t offset, /* offset from start of imaginary file */
67 int length, /* length of data in buffer */
68 int func); /* 0 == read, 1 == write */
70 static int cciss_scsi_queue_command (struct scsi_cmnd *cmd,
71 void (* done)(struct scsi_cmnd *));
72 static int cciss_eh_device_reset_handler(struct scsi_cmnd *);
73 static int cciss_eh_abort_handler(struct scsi_cmnd *);
75 static struct cciss_scsi_hba_t ccissscsi[MAX_CTLR] = {
76 { .name = "cciss0", .ndevices = 0 },
77 { .name = "cciss1", .ndevices = 0 },
78 { .name = "cciss2", .ndevices = 0 },
79 { .name = "cciss3", .ndevices = 0 },
80 { .name = "cciss4", .ndevices = 0 },
81 { .name = "cciss5", .ndevices = 0 },
82 { .name = "cciss6", .ndevices = 0 },
83 { .name = "cciss7", .ndevices = 0 },
86 static struct scsi_host_template cciss_driver_template = {
87 .module = THIS_MODULE,
90 .proc_info = cciss_scsi_proc_info,
91 .queuecommand = cciss_scsi_queue_command,
92 .can_queue = SCSI_CCISS_CAN_QUEUE,
94 .sg_tablesize = MAXSGENTRIES,
96 .use_clustering = DISABLE_CLUSTERING,
97 /* Can't have eh_bus_reset_handler or eh_host_reset_handler for cciss */
98 .eh_device_reset_handler= cciss_eh_device_reset_handler,
99 .eh_abort_handler = cciss_eh_abort_handler,
103 struct cciss_scsi_cmd_stack_elem_t {
104 CommandList_struct cmd;
105 ErrorInfo_struct Err;
112 #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
113 CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
114 // plus two for init time usage
117 struct cciss_scsi_cmd_stack_t {
118 struct cciss_scsi_cmd_stack_elem_t *pool;
119 struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];
120 dma_addr_t cmd_pool_handle;
125 struct cciss_scsi_adapter_data_t {
126 struct Scsi_Host *scsi_host;
127 struct cciss_scsi_cmd_stack_t cmd_stack;
129 spinlock_t lock; // to protect ccissscsi[ctlr];
132 #define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \
133 &(((struct cciss_scsi_adapter_data_t *) \
134 hba[ctlr]->scsi_ctlr)->lock), flags);
135 #define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \
136 &(((struct cciss_scsi_adapter_data_t *) \
137 hba[ctlr]->scsi_ctlr)->lock), flags);
139 static CommandList_struct *
140 scsi_cmd_alloc(ctlr_info_t *h)
142 /* assume only one process in here at a time, locking done by caller. */
143 /* use CCISS_LOCK(ctlr) */
144 /* might be better to rewrite how we allocate scsi commands in a way that */
145 /* needs no locking at all. */
147 /* take the top memory chunk off the stack and return it, if any. */
148 struct cciss_scsi_cmd_stack_elem_t *c;
149 struct cciss_scsi_adapter_data_t *sa;
150 struct cciss_scsi_cmd_stack_t *stk;
153 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
154 stk = &sa->cmd_stack;
158 c = stk->elem[stk->top];
159 /* memset(c, 0, sizeof(*c)); */
160 memset(&c->cmd, 0, sizeof(c->cmd));
161 memset(&c->Err, 0, sizeof(c->Err));
162 /* set physical addr of cmd and addr of scsi parameters */
163 c->cmd.busaddr = c->busaddr;
164 /* (__u32) (stk->cmd_pool_handle +
165 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top)); */
167 temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));
168 /* (__u64) (stk->cmd_pool_handle +
169 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top) +
170 sizeof(CommandList_struct)); */
172 c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;
173 c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;
174 c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);
176 c->cmd.ctlr = h->ctlr;
177 c->cmd.err_info = &c->Err;
179 return (CommandList_struct *) c;
183 scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
185 /* assume only one process in here at a time, locking done by caller. */
186 /* use CCISS_LOCK(ctlr) */
187 /* drop the free memory chunk on top of the stack. */
189 struct cciss_scsi_adapter_data_t *sa;
190 struct cciss_scsi_cmd_stack_t *stk;
192 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
193 stk = &sa->cmd_stack;
194 if (stk->top >= CMD_STACK_SIZE) {
195 printk("cciss: scsi_cmd_free called too many times.\n");
199 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
203 scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
206 struct cciss_scsi_cmd_stack_t *stk;
209 stk = &sa->cmd_stack;
210 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
212 // pci_alloc_consistent guarantees 32-bit DMA address will
215 stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
216 pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
218 if (stk->pool == NULL) {
219 printk("stk->pool is null\n");
223 for (i=0; i<CMD_STACK_SIZE; i++) {
224 stk->elem[i] = &stk->pool[i];
225 stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle +
226 (sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
228 stk->top = CMD_STACK_SIZE-1;
233 scsi_cmd_stack_free(int ctlr)
235 struct cciss_scsi_adapter_data_t *sa;
236 struct cciss_scsi_cmd_stack_t *stk;
239 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
240 stk = &sa->cmd_stack;
241 if (stk->top != CMD_STACK_SIZE-1) {
242 printk( "cciss: %d scsi commands are still outstanding.\n",
243 CMD_STACK_SIZE - stk->top);
245 printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);
247 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
249 pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
254 static int xmargin=8;
255 static int amargin=60;
258 print_bytes (unsigned char *c, int len, int hex, int ascii)
269 if ((i % xmargin) == 0 && i>0) printk("\n");
270 if ((i % xmargin) == 0) printk("0x%04x:", i);
281 if ((i % amargin) == 0 && i>0) printk("\n");
282 if ((i % amargin) == 0) printk("0x%04x:", i);
283 if (*x > 26 && *x < 128) printk("%c", *x);
292 print_cmd(CommandList_struct *cp)
294 printk("queue:%d\n", cp->Header.ReplyQueue);
295 printk("sglist:%d\n", cp->Header.SGList);
296 printk("sgtot:%d\n", cp->Header.SGTotal);
297 printk("Tag:0x%08x/0x%08x\n", cp->Header.Tag.upper,
298 cp->Header.Tag.lower);
299 printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
300 cp->Header.LUN.LunAddrBytes[0],
301 cp->Header.LUN.LunAddrBytes[1],
302 cp->Header.LUN.LunAddrBytes[2],
303 cp->Header.LUN.LunAddrBytes[3],
304 cp->Header.LUN.LunAddrBytes[4],
305 cp->Header.LUN.LunAddrBytes[5],
306 cp->Header.LUN.LunAddrBytes[6],
307 cp->Header.LUN.LunAddrBytes[7]);
308 printk("CDBLen:%d\n", cp->Request.CDBLen);
309 printk("Type:%d\n",cp->Request.Type.Type);
310 printk("Attr:%d\n",cp->Request.Type.Attribute);
311 printk(" Dir:%d\n",cp->Request.Type.Direction);
312 printk("Timeout:%d\n",cp->Request.Timeout);
313 printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"
314 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
315 cp->Request.CDB[0], cp->Request.CDB[1],
316 cp->Request.CDB[2], cp->Request.CDB[3],
317 cp->Request.CDB[4], cp->Request.CDB[5],
318 cp->Request.CDB[6], cp->Request.CDB[7],
319 cp->Request.CDB[8], cp->Request.CDB[9],
320 cp->Request.CDB[10], cp->Request.CDB[11],
321 cp->Request.CDB[12], cp->Request.CDB[13],
322 cp->Request.CDB[14], cp->Request.CDB[15]),
323 printk("edesc.Addr: 0x%08x/0%08x, Len = %d\n",
324 cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower,
326 printk("sgs..........Errorinfo:\n");
327 printk("scsistatus:%d\n", cp->err_info->ScsiStatus);
328 printk("senselen:%d\n", cp->err_info->SenseLen);
329 printk("cmd status:%d\n", cp->err_info->CommandStatus);
330 printk("resid cnt:%d\n", cp->err_info->ResidualCnt);
331 printk("offense size:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);
332 printk("offense byte:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);
333 printk("offense value:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
340 find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
342 /* finds an unused bus, target, lun for a new device */
343 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
345 unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
347 memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
349 target_taken[SELF_SCSI_ID] = 1;
350 for (i=0;i<ccissscsi[ctlr].ndevices;i++)
351 target_taken[ccissscsi[ctlr].dev[i].target] = 1;
353 for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {
354 if (!target_taken[i]) {
355 *bus = 0; *target=i; *lun = 0; found=1;
363 int bus, target, lun;
367 cciss_scsi_add_entry(int ctlr, int hostno,
368 struct cciss_scsi_dev_t *device,
369 struct scsi2map *added, int *nadded)
371 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
372 int n = ccissscsi[ctlr].ndevices;
373 struct cciss_scsi_dev_t *sd;
374 int i, bus, target, lun;
375 unsigned char addr1[8], addr2[8];
377 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
378 printk("cciss%d: Too many devices, "
379 "some will be inaccessible.\n", ctlr);
385 /* Is this device a non-zero lun of a multi-lun device */
386 /* byte 4 of the 8-byte LUN addr will contain the logical unit no. */
387 if (device->scsi3addr[4] != 0) {
388 /* Search through our list and find the device which */
389 /* has the same 8 byte LUN address, excepting byte 4. */
390 /* Assign the same bus and target for this new LUN. */
391 /* Use the logical unit number from the firmware. */
392 memcpy(addr1, device->scsi3addr, 8);
394 for (i = 0; i < n; i++) {
395 sd = &ccissscsi[ctlr].dev[i];
396 memcpy(addr2, sd->scsi3addr, 8);
398 /* differ only in byte 4? */
399 if (memcmp(addr1, addr2, 8) == 0) {
402 lun = device->scsi3addr[4];
408 sd = &ccissscsi[ctlr].dev[n];
410 if (find_bus_target_lun(ctlr,
411 &sd->bus, &sd->target, &sd->lun) != 0)
418 added[*nadded].bus = sd->bus;
419 added[*nadded].target = sd->target;
420 added[*nadded].lun = sd->lun;
423 memcpy(sd->scsi3addr, device->scsi3addr, 8);
424 memcpy(sd->vendor, device->vendor, sizeof(sd->vendor));
425 memcpy(sd->revision, device->revision, sizeof(sd->revision));
426 memcpy(sd->device_id, device->device_id, sizeof(sd->device_id));
427 sd->devtype = device->devtype;
429 ccissscsi[ctlr].ndevices++;
431 /* initially, (before registering with scsi layer) we don't
432 know our hostno and we don't want to print anything first
433 time anyway (the scsi layer's inquiries will show that info) */
435 printk("cciss%d: %s device c%db%dt%dl%d added.\n",
436 ctlr, scsi_device_type(sd->devtype), hostno,
437 sd->bus, sd->target, sd->lun);
442 cciss_scsi_remove_entry(int ctlr, int hostno, int entry,
443 struct scsi2map *removed, int *nremoved)
445 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
447 struct cciss_scsi_dev_t sd;
449 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
450 sd = ccissscsi[ctlr].dev[entry];
451 removed[*nremoved].bus = sd.bus;
452 removed[*nremoved].target = sd.target;
453 removed[*nremoved].lun = sd.lun;
455 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
456 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
457 ccissscsi[ctlr].ndevices--;
458 printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
459 ctlr, scsi_device_type(sd.devtype), hostno,
460 sd.bus, sd.target, sd.lun);
464 #define SCSI3ADDR_EQ(a,b) ( \
465 (a)[7] == (b)[7] && \
466 (a)[6] == (b)[6] && \
467 (a)[5] == (b)[5] && \
468 (a)[4] == (b)[4] && \
469 (a)[3] == (b)[3] && \
470 (a)[2] == (b)[2] && \
471 (a)[1] == (b)[1] && \
474 static void fixup_botched_add(int ctlr, char *scsi3addr)
476 /* called when scsi_add_device fails in order to re-adjust */
477 /* ccissscsi[] to match the mid layer's view. */
480 CPQ_TAPE_LOCK(ctlr, flags);
481 for (i = 0; i < ccissscsi[ctlr].ndevices; i++) {
482 if (memcmp(scsi3addr,
483 ccissscsi[ctlr].dev[i].scsi3addr, 8) == 0) {
484 for (j = i; j < ccissscsi[ctlr].ndevices-1; j++)
485 ccissscsi[ctlr].dev[j] =
486 ccissscsi[ctlr].dev[j+1];
487 ccissscsi[ctlr].ndevices--;
491 CPQ_TAPE_UNLOCK(ctlr, flags);
494 static int device_is_the_same(struct cciss_scsi_dev_t *dev1,
495 struct cciss_scsi_dev_t *dev2)
497 return dev1->devtype == dev2->devtype &&
498 memcmp(dev1->scsi3addr, dev2->scsi3addr,
499 sizeof(dev1->scsi3addr)) == 0 &&
500 memcmp(dev1->device_id, dev2->device_id,
501 sizeof(dev1->device_id)) == 0 &&
502 memcmp(dev1->vendor, dev2->vendor,
503 sizeof(dev1->vendor)) == 0 &&
504 memcmp(dev1->model, dev2->model,
505 sizeof(dev1->model)) == 0 &&
506 memcmp(dev1->revision, dev2->revision,
507 sizeof(dev1->revision)) == 0;
511 adjust_cciss_scsi_table(int ctlr, int hostno,
512 struct cciss_scsi_dev_t sd[], int nsds)
514 /* sd contains scsi3 addresses and devtypes, but
515 bus target and lun are not filled in. This funciton
516 takes what's in sd to be the current and adjusts
517 ccissscsi[] to be in line with what's in sd. */
519 int i,j, found, changes=0;
520 struct cciss_scsi_dev_t *csd;
522 struct scsi2map *added, *removed;
523 int nadded, nremoved;
524 struct Scsi_Host *sh = NULL;
526 added = kzalloc(sizeof(*added) * CCISS_MAX_SCSI_DEVS_PER_HBA,
528 removed = kzalloc(sizeof(*removed) * CCISS_MAX_SCSI_DEVS_PER_HBA,
531 if (!added || !removed) {
532 printk(KERN_WARNING "cciss%d: Out of memory in "
533 "adjust_cciss_scsi_table\n", ctlr);
537 CPQ_TAPE_LOCK(ctlr, flags);
539 if (hostno != -1) /* if it's not the first time... */
540 sh = ((struct cciss_scsi_adapter_data_t *)
541 hba[ctlr]->scsi_ctlr)->scsi_host;
543 /* find any devices in ccissscsi[] that are not in
544 sd[] and remove them from ccissscsi[] */
549 while(i<ccissscsi[ctlr].ndevices) {
550 csd = &ccissscsi[ctlr].dev[i];
552 for (j=0;j<nsds;j++) {
553 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
555 if (device_is_the_same(&sd[j], csd))
563 if (found == 0) { /* device no longer present. */
565 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
566 ctlr, scsi_device_type(csd->devtype), hostno,
567 csd->bus, csd->target, csd->lun); */
568 cciss_scsi_remove_entry(ctlr, hostno, i,
570 /* remove ^^^, hence i not incremented */
571 } else if (found == 1) { /* device is different in some way */
573 printk("cciss%d: device c%db%dt%dl%d has changed.\n",
574 ctlr, hostno, csd->bus, csd->target, csd->lun);
575 cciss_scsi_remove_entry(ctlr, hostno, i,
577 /* remove ^^^, hence i not incremented */
578 if (cciss_scsi_add_entry(ctlr, hostno, &sd[j],
579 added, &nadded) != 0)
580 /* we just removed one, so add can't fail. */
582 csd->devtype = sd[j].devtype;
583 memcpy(csd->device_id, sd[j].device_id,
584 sizeof(csd->device_id));
585 memcpy(csd->vendor, sd[j].vendor,
586 sizeof(csd->vendor));
587 memcpy(csd->model, sd[j].model,
589 memcpy(csd->revision, sd[j].revision,
590 sizeof(csd->revision));
591 } else /* device is same as it ever was, */
592 i++; /* so just move along. */
595 /* Now, make sure every device listed in sd[] is also
596 listed in ccissscsi[], adding them if they aren't found */
598 for (i=0;i<nsds;i++) {
600 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
601 csd = &ccissscsi[ctlr].dev[j];
602 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
604 if (device_is_the_same(&sd[i], csd))
605 found=2; /* found device */
607 found=1; /* found a bug. */
613 if (cciss_scsi_add_entry(ctlr, hostno, &sd[i],
614 added, &nadded) != 0)
616 } else if (found == 1) {
617 /* should never happen... */
619 printk(KERN_WARNING "cciss%d: device "
620 "unexpectedly changed\n", ctlr);
621 /* but if it does happen, we just ignore that device */
624 CPQ_TAPE_UNLOCK(ctlr, flags);
626 /* Don't notify scsi mid layer of any changes the first time through */
627 /* (or if there are no changes) scsi_scan_host will do it later the */
628 /* first time through. */
629 if (hostno == -1 || !changes)
632 /* Notify scsi mid layer of any removed devices */
633 for (i = 0; i < nremoved; i++) {
634 struct scsi_device *sdev =
635 scsi_device_lookup(sh, removed[i].bus,
636 removed[i].target, removed[i].lun);
638 scsi_remove_device(sdev);
639 scsi_device_put(sdev);
641 /* We don't expect to get here. */
642 /* future cmds to this device will get selection */
643 /* timeout as if the device was gone. */
644 printk(KERN_WARNING "cciss%d: didn't find "
645 "c%db%dt%dl%d\n for removal.",
646 ctlr, hostno, removed[i].bus,
647 removed[i].target, removed[i].lun);
651 /* Notify scsi mid layer of any added devices */
652 for (i = 0; i < nadded; i++) {
654 rc = scsi_add_device(sh, added[i].bus,
655 added[i].target, added[i].lun);
658 printk(KERN_WARNING "cciss%d: scsi_add_device "
659 "c%db%dt%dl%d failed, device not added.\n",
661 added[i].bus, added[i].target, added[i].lun);
662 /* now we have to remove it from ccissscsi, */
663 /* since it didn't get added to scsi mid layer */
664 fixup_botched_add(ctlr, added[i].scsi3addr);
674 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
677 struct cciss_scsi_dev_t *sd;
680 CPQ_TAPE_LOCK(ctlr, flags);
681 for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
682 sd = &ccissscsi[ctlr].dev[i];
683 if (sd->bus == bus &&
684 sd->target == target &&
686 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
687 CPQ_TAPE_UNLOCK(ctlr, flags);
691 CPQ_TAPE_UNLOCK(ctlr, flags);
696 cciss_scsi_setup(int cntl_num)
698 struct cciss_scsi_adapter_data_t * shba;
700 ccissscsi[cntl_num].ndevices = 0;
701 shba = (struct cciss_scsi_adapter_data_t *)
702 kmalloc(sizeof(*shba), GFP_KERNEL);
705 shba->scsi_host = NULL;
706 spin_lock_init(&shba->lock);
707 shba->registered = 0;
708 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
712 hba[cntl_num]->scsi_ctlr = (void *) shba;
717 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
719 struct scsi_cmnd *cmd;
721 ErrorInfo_struct *ei;
725 /* First, see if it was a message rather than a command */
726 if (cp->Request.Type.Type == TYPE_MSG) {
727 cp->cmd_type = CMD_MSG_DONE;
731 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
732 ctlr = hba[cp->ctlr];
736 cmd->result = (DID_OK << 16); /* host byte */
737 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
738 /* cmd->result |= (GOOD < 1); */ /* status byte */
740 cmd->result |= (ei->ScsiStatus);
741 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
743 /* copy the sense data whether we need to or not. */
745 memcpy(cmd->sense_buffer, ei->SenseInfo,
746 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
747 SCSI_SENSE_BUFFERSIZE :
749 scsi_set_resid(cmd, ei->ResidualCnt);
751 if(ei->CommandStatus != 0)
752 { /* an error has occurred */
753 switch(ei->CommandStatus)
755 case CMD_TARGET_STATUS:
756 /* Pass it up to the upper layers... */
760 printk(KERN_WARNING "cciss: cmd %p "
761 "has SCSI Status = %x\n",
765 cmd->result |= (ei->ScsiStatus < 1);
767 else { /* scsi status is zero??? How??? */
769 /* Ordinarily, this case should never happen, but there is a bug
770 in some released firmware revisions that allows it to happen
771 if, for example, a 4100 backplane loses power and the tape
772 drive is in it. We assume that it's a fatal error of some
773 kind because we can't show that it wasn't. We will make it
774 look like selection timeout since that is the most common
775 reason for this to occur, and it's severe enough. */
777 cmd->result = DID_NO_CONNECT << 16;
780 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
782 case CMD_DATA_OVERRUN:
783 printk(KERN_WARNING "cciss: cp %p has"
784 " completed with data overrun "
788 /* print_bytes(cp, sizeof(*cp), 1, 0);
790 /* We get CMD_INVALID if you address a non-existent tape drive instead
791 of a selection timeout (no response). You will see this if you yank
792 out a tape drive, then try to access it. This is kind of a shame
793 because it means that any other CMD_INVALID (e.g. driver bug) will
794 get interpreted as a missing target. */
795 cmd->result = DID_NO_CONNECT << 16;
798 case CMD_PROTOCOL_ERR:
799 printk(KERN_WARNING "cciss: cp %p has "
800 "protocol error \n", cp);
802 case CMD_HARDWARE_ERR:
803 cmd->result = DID_ERROR << 16;
804 printk(KERN_WARNING "cciss: cp %p had "
805 " hardware error\n", cp);
807 case CMD_CONNECTION_LOST:
808 cmd->result = DID_ERROR << 16;
809 printk(KERN_WARNING "cciss: cp %p had "
810 "connection lost\n", cp);
813 cmd->result = DID_ABORT << 16;
814 printk(KERN_WARNING "cciss: cp %p was "
817 case CMD_ABORT_FAILED:
818 cmd->result = DID_ERROR << 16;
819 printk(KERN_WARNING "cciss: cp %p reports "
820 "abort failed\n", cp);
822 case CMD_UNSOLICITED_ABORT:
823 cmd->result = DID_ABORT << 16;
824 printk(KERN_WARNING "cciss: cp %p aborted "
825 "do to an unsolicited abort\n", cp);
828 cmd->result = DID_TIME_OUT << 16;
829 printk(KERN_WARNING "cciss: cp %p timedout\n",
833 cmd->result = DID_ERROR << 16;
834 printk(KERN_WARNING "cciss: cp %p returned "
835 "unknown status %x\n", cp,
839 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
840 // cmd->target, cmd->lun);
842 scsi_cmd_free(ctlr, cp);
846 cciss_scsi_detect(int ctlr)
848 struct Scsi_Host *sh;
851 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
854 sh->io_port = 0; // good enough? FIXME,
855 sh->n_io_port = 0; // I don't think we use these two...
856 sh->this_id = SELF_SCSI_ID;
858 ((struct cciss_scsi_adapter_data_t *)
859 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
860 sh->hostdata[0] = (unsigned long) hba[ctlr];
861 sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT];
862 sh->unique_id = sh->irq;
863 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
876 cciss_unmap_one(struct pci_dev *pdev,
877 CommandList_struct *cp,
883 addr64.val32.lower = cp->SG[0].Addr.lower;
884 addr64.val32.upper = cp->SG[0].Addr.upper;
885 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
889 cciss_map_one(struct pci_dev *pdev,
890 CommandList_struct *cp,
897 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
898 cp->SG[0].Addr.lower =
899 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
900 cp->SG[0].Addr.upper =
901 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
902 cp->SG[0].Len = buflen;
903 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
904 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
908 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
909 CommandList_struct *cp,
910 unsigned char *scsi3addr,
912 unsigned char cdblen,
913 unsigned char *buf, int bufsize,
917 DECLARE_COMPLETION_ONSTACK(wait);
919 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
921 cp->Header.ReplyQueue = 0; // unused in simple mode
922 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
923 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
924 // Fill in the request block...
926 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
927 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
928 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
930 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
931 memcpy(cp->Request.CDB, cdb, cdblen);
932 cp->Request.Timeout = 0;
933 cp->Request.CDBLen = cdblen;
934 cp->Request.Type.Type = TYPE_CMD;
935 cp->Request.Type.Attribute = ATTR_SIMPLE;
936 cp->Request.Type.Direction = direction;
938 /* Fill in the SG list and do dma mapping */
939 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
940 bufsize, DMA_FROM_DEVICE);
944 /* Put the request on the tail of the request queue */
945 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
949 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
951 wait_for_completion(&wait);
953 /* undo the dma mapping */
954 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
959 cciss_scsi_interpret_error(CommandList_struct *cp)
961 ErrorInfo_struct *ei;
964 switch(ei->CommandStatus)
966 case CMD_TARGET_STATUS:
967 printk(KERN_WARNING "cciss: cmd %p has "
968 "completed with errors\n", cp);
969 printk(KERN_WARNING "cciss: cmd %p "
970 "has SCSI Status = %x\n",
973 if (ei->ScsiStatus == 0)
975 "cciss:SCSI status is abnormally zero. "
976 "(probably indicates selection timeout "
977 "reported incorrectly due to a known "
978 "firmware bug, circa July, 2001.)\n");
980 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
981 printk("UNDERRUN\n");
983 case CMD_DATA_OVERRUN:
984 printk(KERN_WARNING "cciss: cp %p has"
985 " completed with data overrun "
989 /* controller unfortunately reports SCSI passthru's */
990 /* to non-existent targets as invalid commands. */
991 printk(KERN_WARNING "cciss: cp %p is "
992 "reported invalid (probably means "
993 "target device no longer present)\n",
995 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
999 case CMD_PROTOCOL_ERR:
1000 printk(KERN_WARNING "cciss: cp %p has "
1001 "protocol error \n", cp);
1003 case CMD_HARDWARE_ERR:
1004 /* cmd->result = DID_ERROR << 16; */
1005 printk(KERN_WARNING "cciss: cp %p had "
1006 " hardware error\n", cp);
1008 case CMD_CONNECTION_LOST:
1009 printk(KERN_WARNING "cciss: cp %p had "
1010 "connection lost\n", cp);
1013 printk(KERN_WARNING "cciss: cp %p was "
1016 case CMD_ABORT_FAILED:
1017 printk(KERN_WARNING "cciss: cp %p reports "
1018 "abort failed\n", cp);
1020 case CMD_UNSOLICITED_ABORT:
1021 printk(KERN_WARNING "cciss: cp %p aborted "
1022 "do to an unsolicited abort\n", cp);
1025 printk(KERN_WARNING "cciss: cp %p timedout\n",
1029 printk(KERN_WARNING "cciss: cp %p returned "
1030 "unknown status %x\n", cp,
1036 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
1037 unsigned char page, unsigned char *buf,
1038 unsigned char bufsize)
1041 CommandList_struct *cp;
1043 ErrorInfo_struct *ei;
1044 unsigned long flags;
1046 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1047 cp = scsi_cmd_alloc(c);
1048 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1050 if (cp == NULL) { /* trouble... */
1051 printk("cmd_alloc returned NULL!\n");
1057 cdb[0] = CISS_INQUIRY;
1058 cdb[1] = (page != 0);
1063 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
1064 6, buf, bufsize, XFER_READ);
1066 if (rc != 0) return rc; /* something went wrong */
1068 if (ei->CommandStatus != 0 &&
1069 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1070 cciss_scsi_interpret_error(cp);
1073 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1074 scsi_cmd_free(c, cp);
1075 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1079 /* Get the device id from inquiry page 0x83 */
1080 static int cciss_scsi_get_device_id(ctlr_info_t *c, unsigned char *scsi3addr,
1081 unsigned char *device_id, int buflen)
1088 buf = kzalloc(64, GFP_KERNEL);
1091 rc = cciss_scsi_do_inquiry(c, scsi3addr, 0x83, buf, 64);
1093 memcpy(device_id, &buf[8], buflen);
1099 cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
1100 ReportLunData_struct *buf, int bufsize)
1103 CommandList_struct *cp;
1104 unsigned char cdb[12];
1105 unsigned char scsi3addr[8];
1106 ErrorInfo_struct *ei;
1107 unsigned long flags;
1109 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1110 cp = scsi_cmd_alloc(c);
1111 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1112 if (cp == NULL) { /* trouble... */
1113 printk("cmd_alloc returned NULL!\n");
1117 memset(&scsi3addr[0], 0, 8); /* address the controller */
1118 cdb[0] = CISS_REPORT_PHYS;
1124 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
1125 cdb[7] = (bufsize >> 16) & 0xFF;
1126 cdb[8] = (bufsize >> 8) & 0xFF;
1127 cdb[9] = bufsize & 0xFF;
1131 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
1133 (unsigned char *) buf,
1134 bufsize, XFER_READ);
1136 if (rc != 0) return rc; /* something went wrong */
1139 if (ei->CommandStatus != 0 &&
1140 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1141 cciss_scsi_interpret_error(cp);
1144 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1145 scsi_cmd_free(c, cp);
1146 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1151 cciss_update_non_disk_devices(int cntl_num, int hostno)
1153 /* the idea here is we could get notified from /proc
1154 that some devices have changed, so we do a report
1155 physical luns cmd, and adjust our list of devices
1156 accordingly. (We can't rely on the scsi-mid layer just
1157 doing inquiries, because the "busses" that the scsi
1158 mid-layer probes are totally fabricated by this driver,
1159 so new devices wouldn't show up.
1161 the scsi3addr's of devices won't change so long as the
1162 adapter is not reset. That means we can rescan and
1163 tell which devices we already know about, vs. new
1164 devices, vs. disappearing devices.
1166 Also, if you yank out a tape drive, then put in a disk
1167 in it's place, (say, a configured volume from another
1168 array controller for instance) _don't_ poke this driver
1169 (so it thinks it's still a tape, but _do_ poke the scsi
1170 mid layer, so it does an inquiry... the scsi mid layer
1171 will see the physical disk. This would be bad. Need to
1172 think about how to prevent that. One idea would be to
1173 snoop all scsi responses and if an inquiry repsonse comes
1174 back that reports a disk, chuck it an return selection
1175 timeout instead and adjust our table... Not sure i like
1179 #define OBDR_TAPE_INQ_SIZE 49
1180 #define OBDR_TAPE_SIG "$DR-10"
1181 ReportLunData_struct *ld_buff;
1182 unsigned char *inq_buff;
1183 unsigned char scsi3addr[8];
1187 struct cciss_scsi_dev_t *currentsd, *this_device;
1189 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1192 c = (ctlr_info_t *) hba[cntl_num];
1193 ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
1194 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1195 currentsd = kzalloc(sizeof(*currentsd) *
1196 (CCISS_MAX_SCSI_DEVS_PER_HBA+1), GFP_KERNEL);
1197 if (ld_buff == NULL || inq_buff == NULL || currentsd == NULL) {
1198 printk(KERN_ERR "cciss: out of memory\n");
1201 this_device = ¤tsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1202 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1203 ch = &ld_buff->LUNListLength[0];
1204 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1205 if (num_luns > CISS_MAX_PHYS_LUN) {
1207 "cciss: Maximum physical LUNs (%d) exceeded. "
1208 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1209 num_luns - CISS_MAX_PHYS_LUN);
1210 num_luns = CISS_MAX_PHYS_LUN;
1214 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1219 /* adjust our table of devices */
1220 for (i = 0; i < num_luns; i++) {
1221 /* for each physical lun, do an inquiry */
1222 if (ld_buff->LUN[i][3] & 0xC0) continue;
1223 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1224 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1226 if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, 0, inq_buff,
1227 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0)
1228 /* Inquiry failed (msg printed already) */
1229 continue; /* so we will skip this device. */
1231 this_device->devtype = (inq_buff[0] & 0x1f);
1232 this_device->bus = -1;
1233 this_device->target = -1;
1234 this_device->lun = -1;
1235 memcpy(this_device->scsi3addr, scsi3addr, 8);
1236 memcpy(this_device->vendor, &inq_buff[8],
1237 sizeof(this_device->vendor));
1238 memcpy(this_device->model, &inq_buff[16],
1239 sizeof(this_device->model));
1240 memcpy(this_device->revision, &inq_buff[32],
1241 sizeof(this_device->revision));
1242 memset(this_device->device_id, 0,
1243 sizeof(this_device->device_id));
1244 cciss_scsi_get_device_id(hba[cntl_num], scsi3addr,
1245 this_device->device_id, sizeof(this_device->device_id));
1247 switch (this_device->devtype)
1249 case 0x05: /* CD-ROM */ {
1251 /* We don't *really* support actual CD-ROM devices,
1252 * just this "One Button Disaster Recovery" tape drive
1253 * which temporarily pretends to be a CD-ROM drive.
1254 * So we check that the device is really an OBDR tape
1255 * device by checking for "$DR-10" in bytes 43-48 of
1260 strncpy(obdr_sig, &inq_buff[43], 6);
1262 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1263 /* Not OBDR device, ignore it. */
1266 /* fall through . . . */
1267 case 0x01: /* sequential access, (tape) */
1268 case 0x08: /* medium changer */
1269 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1270 printk(KERN_INFO "cciss%d: %s ignored, "
1271 "too many devices.\n", cntl_num,
1272 scsi_device_type(this_device->devtype));
1275 currentsd[ncurrent] = *this_device;
1283 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1292 is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1294 int verb_len = strlen(verb);
1295 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1302 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1306 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1307 cciss_update_non_disk_devices(ctlr, hostno);
1315 cciss_scsi_proc_info(struct Scsi_Host *sh,
1316 char *buffer, /* data buffer */
1317 char **start, /* where data in buffer starts */
1318 off_t offset, /* offset from start of imaginary file */
1319 int length, /* length of data in buffer */
1320 int func) /* 0 == read, 1 == write */
1323 int buflen, datalen;
1329 ci = (ctlr_info_t *) sh->hostdata[0];
1330 if (ci == NULL) /* This really shouldn't ever happen. */
1333 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1335 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1336 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n",
1337 cntl_num, sh->host_no);
1339 /* this information is needed by apps to know which cciss
1340 device corresponds to which scsi host number without
1341 having to open a scsi target device node. The device
1342 information is not a duplicate of /proc/scsi/scsi because
1343 the two may be out of sync due to scsi hotplug, rather
1344 this info is for an app to be able to use to know how to
1345 get them back in sync. */
1347 for (i=0;i<ccissscsi[cntl_num].ndevices;i++) {
1348 struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i];
1349 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
1350 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1351 sh->host_no, sd->bus, sd->target, sd->lun,
1353 sd->scsi3addr[0], sd->scsi3addr[1],
1354 sd->scsi3addr[2], sd->scsi3addr[3],
1355 sd->scsi3addr[4], sd->scsi3addr[5],
1356 sd->scsi3addr[6], sd->scsi3addr[7]);
1358 datalen = buflen - offset;
1359 if (datalen < 0) { /* they're reading past EOF. */
1361 *start = buffer+buflen;
1363 *start = buffer + offset;
1365 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1366 return cciss_scsi_user_command(cntl_num, sh->host_no,
1370 /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1371 dma mapping and fills in the scatter gather entries of the
1372 cciss command, cp. */
1375 cciss_scatter_gather(struct pci_dev *pdev,
1376 CommandList_struct *cp,
1377 struct scsi_cmnd *cmd)
1380 struct scatterlist *sg;
1384 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1386 use_sg = scsi_dma_map(cmd);
1387 if (use_sg) { /* not too many addrs? */
1388 scsi_for_each_sg(cmd, sg, use_sg, i) {
1389 addr64 = (__u64) sg_dma_address(sg);
1390 len = sg_dma_len(sg);
1391 cp->SG[i].Addr.lower =
1392 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1393 cp->SG[i].Addr.upper =
1394 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1395 cp->SG[i].Len = len;
1396 cp->SG[i].Ext = 0; // we are not chaining
1400 cp->Header.SGList = (__u8) use_sg; /* no. SGs contig in this cmd */
1401 cp->Header.SGTotal = (__u16) use_sg; /* total sgs in this cmd list */
1407 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1411 unsigned char scsi3addr[8];
1412 CommandList_struct *cp;
1413 unsigned long flags;
1415 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1416 // We violate cmd->host privacy here. (Is there another way?)
1417 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1420 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1421 cmd->device->lun, scsi3addr);
1423 /* the scsi nexus does not match any that we presented... */
1424 /* pretend to mid layer that we got selection timeout */
1425 cmd->result = DID_NO_CONNECT << 16;
1427 /* we might want to think about registering controller itself
1428 as a processor device on the bus so sg binds to it. */
1432 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1433 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1434 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1435 // cmd->target, cmd->lun);
1437 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1438 see what the device thinks of it. */
1440 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1441 cp = scsi_cmd_alloc(*c);
1442 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1443 if (cp == NULL) { /* trouble... */
1444 printk("scsi_cmd_alloc returned NULL!\n");
1445 /* FIXME: next 3 lines are -> BAD! <- */
1446 cmd->result = DID_NO_CONNECT << 16;
1451 // Fill in the command list header
1453 cmd->scsi_done = done; // save this for use by completion code
1455 // save cp in case we have to abort it
1456 cmd->host_scribble = (unsigned char *) cp;
1458 cp->cmd_type = CMD_SCSI;
1460 cp->Header.ReplyQueue = 0; // unused in simple mode
1461 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1462 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1464 // Fill in the request block...
1466 cp->Request.Timeout = 0;
1467 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
1468 BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB));
1469 cp->Request.CDBLen = cmd->cmd_len;
1470 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1471 cp->Request.Type.Type = TYPE_CMD;
1472 cp->Request.Type.Attribute = ATTR_SIMPLE;
1473 switch(cmd->sc_data_direction)
1475 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1476 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1477 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1478 case DMA_BIDIRECTIONAL:
1479 // This can happen if a buggy application does a scsi passthru
1480 // and sets both inlen and outlen to non-zero. ( see
1481 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1483 cp->Request.Type.Direction = XFER_RSVD;
1484 // This is technically wrong, and cciss controllers should
1485 // reject it with CMD_INVALID, which is the most correct
1486 // response, but non-fibre backends appear to let it
1487 // slide by, and give the same results as if this field
1488 // were set correctly. Either way is acceptable for
1489 // our purposes here.
1494 printk("cciss: unknown data direction: %d\n",
1495 cmd->sc_data_direction);
1500 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1502 /* Put the request on the tail of the request queue */
1504 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1505 addQ(&(*c)->reqQ, cp);
1508 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1510 /* the cmd'll come back via intr handler in complete_scsi_command() */
1515 cciss_unregister_scsi(int ctlr)
1517 struct cciss_scsi_adapter_data_t *sa;
1518 struct cciss_scsi_cmd_stack_t *stk;
1519 unsigned long flags;
1521 /* we are being forcibly unloaded, and may not refuse. */
1523 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1524 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1525 stk = &sa->cmd_stack;
1527 /* if we weren't ever actually registered, don't unregister */
1528 if (sa->registered) {
1529 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1530 scsi_remove_host(sa->scsi_host);
1531 scsi_host_put(sa->scsi_host);
1532 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1535 /* set scsi_host to NULL so our detect routine will
1536 find us on register */
1537 sa->scsi_host = NULL;
1538 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1539 scsi_cmd_stack_free(ctlr);
1544 cciss_engage_scsi(int ctlr)
1546 struct cciss_scsi_adapter_data_t *sa;
1547 struct cciss_scsi_cmd_stack_t *stk;
1548 unsigned long flags;
1550 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1551 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1552 stk = &sa->cmd_stack;
1554 if (sa->registered) {
1555 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1556 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1560 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1561 cciss_update_non_disk_devices(ctlr, -1);
1562 cciss_scsi_detect(ctlr);
1567 cciss_seq_tape_report(struct seq_file *seq, int ctlr)
1569 unsigned long flags;
1571 CPQ_TAPE_LOCK(ctlr, flags);
1573 "Sequential access devices: %d\n\n",
1574 ccissscsi[ctlr].ndevices);
1575 CPQ_TAPE_UNLOCK(ctlr, flags);
1579 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
1580 * complaining. Doing a host- or bus-reset can't do anything good here.
1581 * Despite what it might say in scsi_error.c, there may well be commands
1582 * on the controller, as the cciss driver registers twice, once as a block
1583 * device for the logical drives, and once as a scsi device, for any tape
1584 * drives. So we know there are no commands out on the tape drives, but we
1585 * don't know there are no commands on the controller, and it is likely
1586 * that there probably are, as the cciss block device is most commonly used
1587 * as a boot device (embedded controller on HP/Compaq systems.)
1590 static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1593 CommandList_struct *cmd_in_trouble;
1597 /* find the controller to which the command to be aborted was sent */
1598 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1599 if (c == NULL) /* paranoia */
1602 printk(KERN_WARNING "cciss%d: resetting tape drive or medium changer.\n", ctlr);
1604 /* find the command that's giving us trouble */
1605 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
1606 if (cmd_in_trouble == NULL) { /* paranoia */
1609 /* send a reset to the SCSI LUN which the command was sent to */
1610 rc = sendcmd(CCISS_RESET_MSG, ctlr, NULL, 0, 2, 0, 0,
1611 (unsigned char *) &cmd_in_trouble->Header.LUN.LunAddrBytes[0],
1613 /* sendcmd turned off interrupts on the board, turn 'em back on. */
1614 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1617 printk(KERN_WARNING "cciss%d: resetting device failed.\n", ctlr);
1621 static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1624 CommandList_struct *cmd_to_abort;
1628 /* find the controller to which the command to be aborted was sent */
1629 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1630 if (c == NULL) /* paranoia */
1633 printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd\n", ctlr);
1635 /* find the command to be aborted */
1636 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
1637 if (cmd_to_abort == NULL) /* paranoia */
1639 rc = sendcmd(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag,
1641 (unsigned char *) &cmd_to_abort->Header.LUN.LunAddrBytes[0],
1643 /* sendcmd turned off interrupts on the board, turn 'em back on. */
1644 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1651 #else /* no CONFIG_CISS_SCSI_TAPE */
1653 /* If no tape support, then these become defined out of existence */
1655 #define cciss_scsi_setup(cntl_num)
1657 #endif /* CONFIG_CISS_SCSI_TAPE */