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,
61 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
63 unsigned int use_unit_num, /* 0: address the controller,
64 1: address logical volume log_unit,
65 2: periph device address is scsi3addr */
66 unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr,
69 static int sendcmd_core(ctlr_info_t *h, CommandList_struct *c);
71 static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool);
72 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool);
74 static int cciss_scsi_proc_info(
76 char *buffer, /* data buffer */
77 char **start, /* where data in buffer starts */
78 off_t offset, /* offset from start of imaginary file */
79 int length, /* length of data in buffer */
80 int func); /* 0 == read, 1 == write */
82 static int cciss_scsi_queue_command (struct scsi_cmnd *cmd,
83 void (* done)(struct scsi_cmnd *));
84 static int cciss_eh_device_reset_handler(struct scsi_cmnd *);
85 static int cciss_eh_abort_handler(struct scsi_cmnd *);
87 static struct cciss_scsi_hba_t ccissscsi[MAX_CTLR] = {
88 { .name = "cciss0", .ndevices = 0 },
89 { .name = "cciss1", .ndevices = 0 },
90 { .name = "cciss2", .ndevices = 0 },
91 { .name = "cciss3", .ndevices = 0 },
92 { .name = "cciss4", .ndevices = 0 },
93 { .name = "cciss5", .ndevices = 0 },
94 { .name = "cciss6", .ndevices = 0 },
95 { .name = "cciss7", .ndevices = 0 },
98 static struct scsi_host_template cciss_driver_template = {
99 .module = THIS_MODULE,
101 .proc_name = "cciss",
102 .proc_info = cciss_scsi_proc_info,
103 .queuecommand = cciss_scsi_queue_command,
104 .can_queue = SCSI_CCISS_CAN_QUEUE,
106 .sg_tablesize = MAXSGENTRIES,
108 .use_clustering = DISABLE_CLUSTERING,
109 /* Can't have eh_bus_reset_handler or eh_host_reset_handler for cciss */
110 .eh_device_reset_handler= cciss_eh_device_reset_handler,
111 .eh_abort_handler = cciss_eh_abort_handler,
115 struct cciss_scsi_cmd_stack_elem_t {
116 CommandList_struct cmd;
117 ErrorInfo_struct Err;
124 #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
125 CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
126 // plus two for init time usage
129 struct cciss_scsi_cmd_stack_t {
130 struct cciss_scsi_cmd_stack_elem_t *pool;
131 struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];
132 dma_addr_t cmd_pool_handle;
137 struct cciss_scsi_adapter_data_t {
138 struct Scsi_Host *scsi_host;
139 struct cciss_scsi_cmd_stack_t cmd_stack;
141 spinlock_t lock; // to protect ccissscsi[ctlr];
144 #define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \
145 &(((struct cciss_scsi_adapter_data_t *) \
146 hba[ctlr]->scsi_ctlr)->lock), flags);
147 #define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \
148 &(((struct cciss_scsi_adapter_data_t *) \
149 hba[ctlr]->scsi_ctlr)->lock), flags);
151 static CommandList_struct *
152 scsi_cmd_alloc(ctlr_info_t *h)
154 /* assume only one process in here at a time, locking done by caller. */
155 /* use CCISS_LOCK(ctlr) */
156 /* might be better to rewrite how we allocate scsi commands in a way that */
157 /* needs no locking at all. */
159 /* take the top memory chunk off the stack and return it, if any. */
160 struct cciss_scsi_cmd_stack_elem_t *c;
161 struct cciss_scsi_adapter_data_t *sa;
162 struct cciss_scsi_cmd_stack_t *stk;
165 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
166 stk = &sa->cmd_stack;
170 c = stk->elem[stk->top];
171 /* memset(c, 0, sizeof(*c)); */
172 memset(&c->cmd, 0, sizeof(c->cmd));
173 memset(&c->Err, 0, sizeof(c->Err));
174 /* set physical addr of cmd and addr of scsi parameters */
175 c->cmd.busaddr = c->busaddr;
176 /* (__u32) (stk->cmd_pool_handle +
177 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top)); */
179 temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));
180 /* (__u64) (stk->cmd_pool_handle +
181 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top) +
182 sizeof(CommandList_struct)); */
184 c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;
185 c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;
186 c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);
188 c->cmd.ctlr = h->ctlr;
189 c->cmd.err_info = &c->Err;
191 return (CommandList_struct *) c;
195 scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
197 /* assume only one process in here at a time, locking done by caller. */
198 /* use CCISS_LOCK(ctlr) */
199 /* drop the free memory chunk on top of the stack. */
201 struct cciss_scsi_adapter_data_t *sa;
202 struct cciss_scsi_cmd_stack_t *stk;
204 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
205 stk = &sa->cmd_stack;
206 if (stk->top >= CMD_STACK_SIZE) {
207 printk("cciss: scsi_cmd_free called too many times.\n");
211 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
215 scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
218 struct cciss_scsi_cmd_stack_t *stk;
221 stk = &sa->cmd_stack;
222 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
224 // pci_alloc_consistent guarantees 32-bit DMA address will
227 stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
228 pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
230 if (stk->pool == NULL) {
231 printk("stk->pool is null\n");
235 for (i=0; i<CMD_STACK_SIZE; i++) {
236 stk->elem[i] = &stk->pool[i];
237 stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle +
238 (sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
240 stk->top = CMD_STACK_SIZE-1;
245 scsi_cmd_stack_free(int ctlr)
247 struct cciss_scsi_adapter_data_t *sa;
248 struct cciss_scsi_cmd_stack_t *stk;
251 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
252 stk = &sa->cmd_stack;
253 if (stk->top != CMD_STACK_SIZE-1) {
254 printk( "cciss: %d scsi commands are still outstanding.\n",
255 CMD_STACK_SIZE - stk->top);
257 printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);
259 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
261 pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
266 static int xmargin=8;
267 static int amargin=60;
270 print_bytes (unsigned char *c, int len, int hex, int ascii)
281 if ((i % xmargin) == 0 && i>0) printk("\n");
282 if ((i % xmargin) == 0) printk("0x%04x:", i);
293 if ((i % amargin) == 0 && i>0) printk("\n");
294 if ((i % amargin) == 0) printk("0x%04x:", i);
295 if (*x > 26 && *x < 128) printk("%c", *x);
304 print_cmd(CommandList_struct *cp)
306 printk("queue:%d\n", cp->Header.ReplyQueue);
307 printk("sglist:%d\n", cp->Header.SGList);
308 printk("sgtot:%d\n", cp->Header.SGTotal);
309 printk("Tag:0x%08x/0x%08x\n", cp->Header.Tag.upper,
310 cp->Header.Tag.lower);
311 printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
312 cp->Header.LUN.LunAddrBytes[0],
313 cp->Header.LUN.LunAddrBytes[1],
314 cp->Header.LUN.LunAddrBytes[2],
315 cp->Header.LUN.LunAddrBytes[3],
316 cp->Header.LUN.LunAddrBytes[4],
317 cp->Header.LUN.LunAddrBytes[5],
318 cp->Header.LUN.LunAddrBytes[6],
319 cp->Header.LUN.LunAddrBytes[7]);
320 printk("CDBLen:%d\n", cp->Request.CDBLen);
321 printk("Type:%d\n",cp->Request.Type.Type);
322 printk("Attr:%d\n",cp->Request.Type.Attribute);
323 printk(" Dir:%d\n",cp->Request.Type.Direction);
324 printk("Timeout:%d\n",cp->Request.Timeout);
325 printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"
326 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
327 cp->Request.CDB[0], cp->Request.CDB[1],
328 cp->Request.CDB[2], cp->Request.CDB[3],
329 cp->Request.CDB[4], cp->Request.CDB[5],
330 cp->Request.CDB[6], cp->Request.CDB[7],
331 cp->Request.CDB[8], cp->Request.CDB[9],
332 cp->Request.CDB[10], cp->Request.CDB[11],
333 cp->Request.CDB[12], cp->Request.CDB[13],
334 cp->Request.CDB[14], cp->Request.CDB[15]),
335 printk("edesc.Addr: 0x%08x/0%08x, Len = %d\n",
336 cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower,
338 printk("sgs..........Errorinfo:\n");
339 printk("scsistatus:%d\n", cp->err_info->ScsiStatus);
340 printk("senselen:%d\n", cp->err_info->SenseLen);
341 printk("cmd status:%d\n", cp->err_info->CommandStatus);
342 printk("resid cnt:%d\n", cp->err_info->ResidualCnt);
343 printk("offense size:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);
344 printk("offense byte:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);
345 printk("offense value:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
352 find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
354 /* finds an unused bus, target, lun for a new device */
355 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
357 unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
359 memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
361 target_taken[SELF_SCSI_ID] = 1;
362 for (i=0;i<ccissscsi[ctlr].ndevices;i++)
363 target_taken[ccissscsi[ctlr].dev[i].target] = 1;
365 for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {
366 if (!target_taken[i]) {
367 *bus = 0; *target=i; *lun = 0; found=1;
375 int bus, target, lun;
379 cciss_scsi_add_entry(int ctlr, int hostno,
380 struct cciss_scsi_dev_t *device,
381 struct scsi2map *added, int *nadded)
383 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
384 int n = ccissscsi[ctlr].ndevices;
385 struct cciss_scsi_dev_t *sd;
386 int i, bus, target, lun;
387 unsigned char addr1[8], addr2[8];
389 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
390 printk("cciss%d: Too many devices, "
391 "some will be inaccessible.\n", ctlr);
397 /* Is this device a non-zero lun of a multi-lun device */
398 /* byte 4 of the 8-byte LUN addr will contain the logical unit no. */
399 if (device->scsi3addr[4] != 0) {
400 /* Search through our list and find the device which */
401 /* has the same 8 byte LUN address, excepting byte 4. */
402 /* Assign the same bus and target for this new LUN. */
403 /* Use the logical unit number from the firmware. */
404 memcpy(addr1, device->scsi3addr, 8);
406 for (i = 0; i < n; i++) {
407 sd = &ccissscsi[ctlr].dev[i];
408 memcpy(addr2, sd->scsi3addr, 8);
410 /* differ only in byte 4? */
411 if (memcmp(addr1, addr2, 8) == 0) {
414 lun = device->scsi3addr[4];
420 sd = &ccissscsi[ctlr].dev[n];
422 if (find_bus_target_lun(ctlr,
423 &sd->bus, &sd->target, &sd->lun) != 0)
430 added[*nadded].bus = sd->bus;
431 added[*nadded].target = sd->target;
432 added[*nadded].lun = sd->lun;
435 memcpy(sd->scsi3addr, device->scsi3addr, 8);
436 memcpy(sd->vendor, device->vendor, sizeof(sd->vendor));
437 memcpy(sd->revision, device->revision, sizeof(sd->revision));
438 memcpy(sd->device_id, device->device_id, sizeof(sd->device_id));
439 sd->devtype = device->devtype;
441 ccissscsi[ctlr].ndevices++;
443 /* initially, (before registering with scsi layer) we don't
444 know our hostno and we don't want to print anything first
445 time anyway (the scsi layer's inquiries will show that info) */
447 printk("cciss%d: %s device c%db%dt%dl%d added.\n",
448 ctlr, scsi_device_type(sd->devtype), hostno,
449 sd->bus, sd->target, sd->lun);
454 cciss_scsi_remove_entry(int ctlr, int hostno, int entry,
455 struct scsi2map *removed, int *nremoved)
457 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
459 struct cciss_scsi_dev_t sd;
461 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
462 sd = ccissscsi[ctlr].dev[entry];
463 removed[*nremoved].bus = sd.bus;
464 removed[*nremoved].target = sd.target;
465 removed[*nremoved].lun = sd.lun;
467 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
468 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
469 ccissscsi[ctlr].ndevices--;
470 printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
471 ctlr, scsi_device_type(sd.devtype), hostno,
472 sd.bus, sd.target, sd.lun);
476 #define SCSI3ADDR_EQ(a,b) ( \
477 (a)[7] == (b)[7] && \
478 (a)[6] == (b)[6] && \
479 (a)[5] == (b)[5] && \
480 (a)[4] == (b)[4] && \
481 (a)[3] == (b)[3] && \
482 (a)[2] == (b)[2] && \
483 (a)[1] == (b)[1] && \
486 static void fixup_botched_add(int ctlr, char *scsi3addr)
488 /* called when scsi_add_device fails in order to re-adjust */
489 /* ccissscsi[] to match the mid layer's view. */
492 CPQ_TAPE_LOCK(ctlr, flags);
493 for (i = 0; i < ccissscsi[ctlr].ndevices; i++) {
494 if (memcmp(scsi3addr,
495 ccissscsi[ctlr].dev[i].scsi3addr, 8) == 0) {
496 for (j = i; j < ccissscsi[ctlr].ndevices-1; j++)
497 ccissscsi[ctlr].dev[j] =
498 ccissscsi[ctlr].dev[j+1];
499 ccissscsi[ctlr].ndevices--;
503 CPQ_TAPE_UNLOCK(ctlr, flags);
506 static int device_is_the_same(struct cciss_scsi_dev_t *dev1,
507 struct cciss_scsi_dev_t *dev2)
509 return dev1->devtype == dev2->devtype &&
510 memcmp(dev1->scsi3addr, dev2->scsi3addr,
511 sizeof(dev1->scsi3addr)) == 0 &&
512 memcmp(dev1->device_id, dev2->device_id,
513 sizeof(dev1->device_id)) == 0 &&
514 memcmp(dev1->vendor, dev2->vendor,
515 sizeof(dev1->vendor)) == 0 &&
516 memcmp(dev1->model, dev2->model,
517 sizeof(dev1->model)) == 0 &&
518 memcmp(dev1->revision, dev2->revision,
519 sizeof(dev1->revision)) == 0;
523 adjust_cciss_scsi_table(int ctlr, int hostno,
524 struct cciss_scsi_dev_t sd[], int nsds)
526 /* sd contains scsi3 addresses and devtypes, but
527 bus target and lun are not filled in. This funciton
528 takes what's in sd to be the current and adjusts
529 ccissscsi[] to be in line with what's in sd. */
531 int i,j, found, changes=0;
532 struct cciss_scsi_dev_t *csd;
534 struct scsi2map *added, *removed;
535 int nadded, nremoved;
536 struct Scsi_Host *sh = NULL;
538 added = kzalloc(sizeof(*added) * CCISS_MAX_SCSI_DEVS_PER_HBA,
540 removed = kzalloc(sizeof(*removed) * CCISS_MAX_SCSI_DEVS_PER_HBA,
543 if (!added || !removed) {
544 printk(KERN_WARNING "cciss%d: Out of memory in "
545 "adjust_cciss_scsi_table\n", ctlr);
549 CPQ_TAPE_LOCK(ctlr, flags);
551 if (hostno != -1) /* if it's not the first time... */
552 sh = ((struct cciss_scsi_adapter_data_t *)
553 hba[ctlr]->scsi_ctlr)->scsi_host;
555 /* find any devices in ccissscsi[] that are not in
556 sd[] and remove them from ccissscsi[] */
561 while(i<ccissscsi[ctlr].ndevices) {
562 csd = &ccissscsi[ctlr].dev[i];
564 for (j=0;j<nsds;j++) {
565 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
567 if (device_is_the_same(&sd[j], csd))
575 if (found == 0) { /* device no longer present. */
577 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
578 ctlr, scsi_device_type(csd->devtype), hostno,
579 csd->bus, csd->target, csd->lun); */
580 cciss_scsi_remove_entry(ctlr, hostno, i,
582 /* remove ^^^, hence i not incremented */
583 } else if (found == 1) { /* device is different in some way */
585 printk("cciss%d: device c%db%dt%dl%d has changed.\n",
586 ctlr, hostno, csd->bus, csd->target, csd->lun);
587 cciss_scsi_remove_entry(ctlr, hostno, i,
589 /* remove ^^^, hence i not incremented */
590 if (cciss_scsi_add_entry(ctlr, hostno, &sd[j],
591 added, &nadded) != 0)
592 /* we just removed one, so add can't fail. */
594 csd->devtype = sd[j].devtype;
595 memcpy(csd->device_id, sd[j].device_id,
596 sizeof(csd->device_id));
597 memcpy(csd->vendor, sd[j].vendor,
598 sizeof(csd->vendor));
599 memcpy(csd->model, sd[j].model,
601 memcpy(csd->revision, sd[j].revision,
602 sizeof(csd->revision));
603 } else /* device is same as it ever was, */
604 i++; /* so just move along. */
607 /* Now, make sure every device listed in sd[] is also
608 listed in ccissscsi[], adding them if they aren't found */
610 for (i=0;i<nsds;i++) {
612 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
613 csd = &ccissscsi[ctlr].dev[j];
614 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
616 if (device_is_the_same(&sd[i], csd))
617 found=2; /* found device */
619 found=1; /* found a bug. */
625 if (cciss_scsi_add_entry(ctlr, hostno, &sd[i],
626 added, &nadded) != 0)
628 } else if (found == 1) {
629 /* should never happen... */
631 printk(KERN_WARNING "cciss%d: device "
632 "unexpectedly changed\n", ctlr);
633 /* but if it does happen, we just ignore that device */
636 CPQ_TAPE_UNLOCK(ctlr, flags);
638 /* Don't notify scsi mid layer of any changes the first time through */
639 /* (or if there are no changes) scsi_scan_host will do it later the */
640 /* first time through. */
641 if (hostno == -1 || !changes)
644 /* Notify scsi mid layer of any removed devices */
645 for (i = 0; i < nremoved; i++) {
646 struct scsi_device *sdev =
647 scsi_device_lookup(sh, removed[i].bus,
648 removed[i].target, removed[i].lun);
650 scsi_remove_device(sdev);
651 scsi_device_put(sdev);
653 /* We don't expect to get here. */
654 /* future cmds to this device will get selection */
655 /* timeout as if the device was gone. */
656 printk(KERN_WARNING "cciss%d: didn't find "
657 "c%db%dt%dl%d\n for removal.",
658 ctlr, hostno, removed[i].bus,
659 removed[i].target, removed[i].lun);
663 /* Notify scsi mid layer of any added devices */
664 for (i = 0; i < nadded; i++) {
666 rc = scsi_add_device(sh, added[i].bus,
667 added[i].target, added[i].lun);
670 printk(KERN_WARNING "cciss%d: scsi_add_device "
671 "c%db%dt%dl%d failed, device not added.\n",
673 added[i].bus, added[i].target, added[i].lun);
674 /* now we have to remove it from ccissscsi, */
675 /* since it didn't get added to scsi mid layer */
676 fixup_botched_add(ctlr, added[i].scsi3addr);
686 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
689 struct cciss_scsi_dev_t *sd;
692 CPQ_TAPE_LOCK(ctlr, flags);
693 for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
694 sd = &ccissscsi[ctlr].dev[i];
695 if (sd->bus == bus &&
696 sd->target == target &&
698 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
699 CPQ_TAPE_UNLOCK(ctlr, flags);
703 CPQ_TAPE_UNLOCK(ctlr, flags);
708 cciss_scsi_setup(int cntl_num)
710 struct cciss_scsi_adapter_data_t * shba;
712 ccissscsi[cntl_num].ndevices = 0;
713 shba = (struct cciss_scsi_adapter_data_t *)
714 kmalloc(sizeof(*shba), GFP_KERNEL);
717 shba->scsi_host = NULL;
718 spin_lock_init(&shba->lock);
719 shba->registered = 0;
720 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
724 hba[cntl_num]->scsi_ctlr = (void *) shba;
729 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
731 struct scsi_cmnd *cmd;
733 ErrorInfo_struct *ei;
737 /* First, see if it was a message rather than a command */
738 if (cp->Request.Type.Type == TYPE_MSG) {
739 cp->cmd_type = CMD_MSG_DONE;
743 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
744 ctlr = hba[cp->ctlr];
748 cmd->result = (DID_OK << 16); /* host byte */
749 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
750 /* cmd->result |= (GOOD < 1); */ /* status byte */
752 cmd->result |= (ei->ScsiStatus);
753 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
755 /* copy the sense data whether we need to or not. */
757 memcpy(cmd->sense_buffer, ei->SenseInfo,
758 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
759 SCSI_SENSE_BUFFERSIZE :
761 scsi_set_resid(cmd, ei->ResidualCnt);
763 if(ei->CommandStatus != 0)
764 { /* an error has occurred */
765 switch(ei->CommandStatus)
767 case CMD_TARGET_STATUS:
768 /* Pass it up to the upper layers... */
772 printk(KERN_WARNING "cciss: cmd %p "
773 "has SCSI Status = %x\n",
777 cmd->result |= (ei->ScsiStatus < 1);
779 else { /* scsi status is zero??? How??? */
781 /* Ordinarily, this case should never happen, but there is a bug
782 in some released firmware revisions that allows it to happen
783 if, for example, a 4100 backplane loses power and the tape
784 drive is in it. We assume that it's a fatal error of some
785 kind because we can't show that it wasn't. We will make it
786 look like selection timeout since that is the most common
787 reason for this to occur, and it's severe enough. */
789 cmd->result = DID_NO_CONNECT << 16;
792 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
794 case CMD_DATA_OVERRUN:
795 printk(KERN_WARNING "cciss: cp %p has"
796 " completed with data overrun "
800 /* print_bytes(cp, sizeof(*cp), 1, 0);
802 /* We get CMD_INVALID if you address a non-existent tape drive instead
803 of a selection timeout (no response). You will see this if you yank
804 out a tape drive, then try to access it. This is kind of a shame
805 because it means that any other CMD_INVALID (e.g. driver bug) will
806 get interpreted as a missing target. */
807 cmd->result = DID_NO_CONNECT << 16;
810 case CMD_PROTOCOL_ERR:
811 printk(KERN_WARNING "cciss: cp %p has "
812 "protocol error \n", cp);
814 case CMD_HARDWARE_ERR:
815 cmd->result = DID_ERROR << 16;
816 printk(KERN_WARNING "cciss: cp %p had "
817 " hardware error\n", cp);
819 case CMD_CONNECTION_LOST:
820 cmd->result = DID_ERROR << 16;
821 printk(KERN_WARNING "cciss: cp %p had "
822 "connection lost\n", cp);
825 cmd->result = DID_ABORT << 16;
826 printk(KERN_WARNING "cciss: cp %p was "
829 case CMD_ABORT_FAILED:
830 cmd->result = DID_ERROR << 16;
831 printk(KERN_WARNING "cciss: cp %p reports "
832 "abort failed\n", cp);
834 case CMD_UNSOLICITED_ABORT:
835 cmd->result = DID_ABORT << 16;
836 printk(KERN_WARNING "cciss: cp %p aborted "
837 "do to an unsolicited abort\n", cp);
840 cmd->result = DID_TIME_OUT << 16;
841 printk(KERN_WARNING "cciss: cp %p timedout\n",
845 cmd->result = DID_ERROR << 16;
846 printk(KERN_WARNING "cciss: cp %p returned "
847 "unknown status %x\n", cp,
851 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
852 // cmd->target, cmd->lun);
854 scsi_cmd_free(ctlr, cp);
858 cciss_scsi_detect(int ctlr)
860 struct Scsi_Host *sh;
863 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
866 sh->io_port = 0; // good enough? FIXME,
867 sh->n_io_port = 0; // I don't think we use these two...
868 sh->this_id = SELF_SCSI_ID;
870 ((struct cciss_scsi_adapter_data_t *)
871 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
872 sh->hostdata[0] = (unsigned long) hba[ctlr];
873 sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT];
874 sh->unique_id = sh->irq;
875 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
888 cciss_unmap_one(struct pci_dev *pdev,
889 CommandList_struct *cp,
895 addr64.val32.lower = cp->SG[0].Addr.lower;
896 addr64.val32.upper = cp->SG[0].Addr.upper;
897 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
901 cciss_map_one(struct pci_dev *pdev,
902 CommandList_struct *cp,
909 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
910 cp->SG[0].Addr.lower =
911 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
912 cp->SG[0].Addr.upper =
913 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
914 cp->SG[0].Len = buflen;
915 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
916 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
920 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
921 CommandList_struct *cp,
922 unsigned char *scsi3addr,
924 unsigned char cdblen,
925 unsigned char *buf, int bufsize,
929 DECLARE_COMPLETION_ONSTACK(wait);
931 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
933 cp->Header.ReplyQueue = 0; // unused in simple mode
934 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
935 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
936 // Fill in the request block...
938 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
939 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
940 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
942 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
943 memcpy(cp->Request.CDB, cdb, cdblen);
944 cp->Request.Timeout = 0;
945 cp->Request.CDBLen = cdblen;
946 cp->Request.Type.Type = TYPE_CMD;
947 cp->Request.Type.Attribute = ATTR_SIMPLE;
948 cp->Request.Type.Direction = direction;
950 /* Fill in the SG list and do dma mapping */
951 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
952 bufsize, DMA_FROM_DEVICE);
956 /* Put the request on the tail of the request queue */
957 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
961 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
963 wait_for_completion(&wait);
965 /* undo the dma mapping */
966 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
971 cciss_scsi_interpret_error(CommandList_struct *cp)
973 ErrorInfo_struct *ei;
976 switch(ei->CommandStatus)
978 case CMD_TARGET_STATUS:
979 printk(KERN_WARNING "cciss: cmd %p has "
980 "completed with errors\n", cp);
981 printk(KERN_WARNING "cciss: cmd %p "
982 "has SCSI Status = %x\n",
985 if (ei->ScsiStatus == 0)
987 "cciss:SCSI status is abnormally zero. "
988 "(probably indicates selection timeout "
989 "reported incorrectly due to a known "
990 "firmware bug, circa July, 2001.)\n");
992 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
993 printk("UNDERRUN\n");
995 case CMD_DATA_OVERRUN:
996 printk(KERN_WARNING "cciss: cp %p has"
997 " completed with data overrun "
1001 /* controller unfortunately reports SCSI passthru's */
1002 /* to non-existent targets as invalid commands. */
1003 printk(KERN_WARNING "cciss: cp %p is "
1004 "reported invalid (probably means "
1005 "target device no longer present)\n",
1007 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1011 case CMD_PROTOCOL_ERR:
1012 printk(KERN_WARNING "cciss: cp %p has "
1013 "protocol error \n", cp);
1015 case CMD_HARDWARE_ERR:
1016 /* cmd->result = DID_ERROR << 16; */
1017 printk(KERN_WARNING "cciss: cp %p had "
1018 " hardware error\n", cp);
1020 case CMD_CONNECTION_LOST:
1021 printk(KERN_WARNING "cciss: cp %p had "
1022 "connection lost\n", cp);
1025 printk(KERN_WARNING "cciss: cp %p was "
1028 case CMD_ABORT_FAILED:
1029 printk(KERN_WARNING "cciss: cp %p reports "
1030 "abort failed\n", cp);
1032 case CMD_UNSOLICITED_ABORT:
1033 printk(KERN_WARNING "cciss: cp %p aborted "
1034 "do to an unsolicited abort\n", cp);
1037 printk(KERN_WARNING "cciss: cp %p timedout\n",
1041 printk(KERN_WARNING "cciss: cp %p returned "
1042 "unknown status %x\n", cp,
1048 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
1049 unsigned char page, unsigned char *buf,
1050 unsigned char bufsize)
1053 CommandList_struct *cp;
1055 ErrorInfo_struct *ei;
1056 unsigned long flags;
1058 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1059 cp = scsi_cmd_alloc(c);
1060 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1062 if (cp == NULL) { /* trouble... */
1063 printk("cmd_alloc returned NULL!\n");
1069 cdb[0] = CISS_INQUIRY;
1070 cdb[1] = (page != 0);
1075 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
1076 6, buf, bufsize, XFER_READ);
1078 if (rc != 0) return rc; /* something went wrong */
1080 if (ei->CommandStatus != 0 &&
1081 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1082 cciss_scsi_interpret_error(cp);
1085 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1086 scsi_cmd_free(c, cp);
1087 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1091 /* Get the device id from inquiry page 0x83 */
1092 static int cciss_scsi_get_device_id(ctlr_info_t *c, unsigned char *scsi3addr,
1093 unsigned char *device_id, int buflen)
1100 buf = kzalloc(64, GFP_KERNEL);
1103 rc = cciss_scsi_do_inquiry(c, scsi3addr, 0x83, buf, 64);
1105 memcpy(device_id, &buf[8], buflen);
1111 cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
1112 ReportLunData_struct *buf, int bufsize)
1115 CommandList_struct *cp;
1116 unsigned char cdb[12];
1117 unsigned char scsi3addr[8];
1118 ErrorInfo_struct *ei;
1119 unsigned long flags;
1121 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1122 cp = scsi_cmd_alloc(c);
1123 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1124 if (cp == NULL) { /* trouble... */
1125 printk("cmd_alloc returned NULL!\n");
1129 memset(&scsi3addr[0], 0, 8); /* address the controller */
1130 cdb[0] = CISS_REPORT_PHYS;
1136 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
1137 cdb[7] = (bufsize >> 16) & 0xFF;
1138 cdb[8] = (bufsize >> 8) & 0xFF;
1139 cdb[9] = bufsize & 0xFF;
1143 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
1145 (unsigned char *) buf,
1146 bufsize, XFER_READ);
1148 if (rc != 0) return rc; /* something went wrong */
1151 if (ei->CommandStatus != 0 &&
1152 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1153 cciss_scsi_interpret_error(cp);
1156 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1157 scsi_cmd_free(c, cp);
1158 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1163 cciss_update_non_disk_devices(int cntl_num, int hostno)
1165 /* the idea here is we could get notified from /proc
1166 that some devices have changed, so we do a report
1167 physical luns cmd, and adjust our list of devices
1168 accordingly. (We can't rely on the scsi-mid layer just
1169 doing inquiries, because the "busses" that the scsi
1170 mid-layer probes are totally fabricated by this driver,
1171 so new devices wouldn't show up.
1173 the scsi3addr's of devices won't change so long as the
1174 adapter is not reset. That means we can rescan and
1175 tell which devices we already know about, vs. new
1176 devices, vs. disappearing devices.
1178 Also, if you yank out a tape drive, then put in a disk
1179 in it's place, (say, a configured volume from another
1180 array controller for instance) _don't_ poke this driver
1181 (so it thinks it's still a tape, but _do_ poke the scsi
1182 mid layer, so it does an inquiry... the scsi mid layer
1183 will see the physical disk. This would be bad. Need to
1184 think about how to prevent that. One idea would be to
1185 snoop all scsi responses and if an inquiry repsonse comes
1186 back that reports a disk, chuck it an return selection
1187 timeout instead and adjust our table... Not sure i like
1191 #define OBDR_TAPE_INQ_SIZE 49
1192 #define OBDR_TAPE_SIG "$DR-10"
1193 ReportLunData_struct *ld_buff;
1194 unsigned char *inq_buff;
1195 unsigned char scsi3addr[8];
1199 struct cciss_scsi_dev_t *currentsd, *this_device;
1201 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1204 c = (ctlr_info_t *) hba[cntl_num];
1205 ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
1206 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1207 currentsd = kzalloc(sizeof(*currentsd) *
1208 (CCISS_MAX_SCSI_DEVS_PER_HBA+1), GFP_KERNEL);
1209 if (ld_buff == NULL || inq_buff == NULL || currentsd == NULL) {
1210 printk(KERN_ERR "cciss: out of memory\n");
1213 this_device = ¤tsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1214 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1215 ch = &ld_buff->LUNListLength[0];
1216 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1217 if (num_luns > CISS_MAX_PHYS_LUN) {
1219 "cciss: Maximum physical LUNs (%d) exceeded. "
1220 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1221 num_luns - CISS_MAX_PHYS_LUN);
1222 num_luns = CISS_MAX_PHYS_LUN;
1226 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1231 /* adjust our table of devices */
1232 for (i = 0; i < num_luns; i++) {
1233 /* for each physical lun, do an inquiry */
1234 if (ld_buff->LUN[i][3] & 0xC0) continue;
1235 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1236 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1238 if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, 0, inq_buff,
1239 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0)
1240 /* Inquiry failed (msg printed already) */
1241 continue; /* so we will skip this device. */
1243 this_device->devtype = (inq_buff[0] & 0x1f);
1244 this_device->bus = -1;
1245 this_device->target = -1;
1246 this_device->lun = -1;
1247 memcpy(this_device->scsi3addr, scsi3addr, 8);
1248 memcpy(this_device->vendor, &inq_buff[8],
1249 sizeof(this_device->vendor));
1250 memcpy(this_device->model, &inq_buff[16],
1251 sizeof(this_device->model));
1252 memcpy(this_device->revision, &inq_buff[32],
1253 sizeof(this_device->revision));
1254 memset(this_device->device_id, 0,
1255 sizeof(this_device->device_id));
1256 cciss_scsi_get_device_id(hba[cntl_num], scsi3addr,
1257 this_device->device_id, sizeof(this_device->device_id));
1259 switch (this_device->devtype)
1261 case 0x05: /* CD-ROM */ {
1263 /* We don't *really* support actual CD-ROM devices,
1264 * just this "One Button Disaster Recovery" tape drive
1265 * which temporarily pretends to be a CD-ROM drive.
1266 * So we check that the device is really an OBDR tape
1267 * device by checking for "$DR-10" in bytes 43-48 of
1272 strncpy(obdr_sig, &inq_buff[43], 6);
1274 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1275 /* Not OBDR device, ignore it. */
1278 /* fall through . . . */
1279 case 0x01: /* sequential access, (tape) */
1280 case 0x08: /* medium changer */
1281 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1282 printk(KERN_INFO "cciss%d: %s ignored, "
1283 "too many devices.\n", cntl_num,
1284 scsi_device_type(this_device->devtype));
1287 currentsd[ncurrent] = *this_device;
1295 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1304 is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1306 int verb_len = strlen(verb);
1307 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1314 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1318 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1319 cciss_update_non_disk_devices(ctlr, hostno);
1327 cciss_scsi_proc_info(struct Scsi_Host *sh,
1328 char *buffer, /* data buffer */
1329 char **start, /* where data in buffer starts */
1330 off_t offset, /* offset from start of imaginary file */
1331 int length, /* length of data in buffer */
1332 int func) /* 0 == read, 1 == write */
1335 int buflen, datalen;
1341 ci = (ctlr_info_t *) sh->hostdata[0];
1342 if (ci == NULL) /* This really shouldn't ever happen. */
1345 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1347 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1348 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n",
1349 cntl_num, sh->host_no);
1351 /* this information is needed by apps to know which cciss
1352 device corresponds to which scsi host number without
1353 having to open a scsi target device node. The device
1354 information is not a duplicate of /proc/scsi/scsi because
1355 the two may be out of sync due to scsi hotplug, rather
1356 this info is for an app to be able to use to know how to
1357 get them back in sync. */
1359 for (i=0;i<ccissscsi[cntl_num].ndevices;i++) {
1360 struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i];
1361 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
1362 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1363 sh->host_no, sd->bus, sd->target, sd->lun,
1365 sd->scsi3addr[0], sd->scsi3addr[1],
1366 sd->scsi3addr[2], sd->scsi3addr[3],
1367 sd->scsi3addr[4], sd->scsi3addr[5],
1368 sd->scsi3addr[6], sd->scsi3addr[7]);
1370 datalen = buflen - offset;
1371 if (datalen < 0) { /* they're reading past EOF. */
1373 *start = buffer+buflen;
1375 *start = buffer + offset;
1377 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1378 return cciss_scsi_user_command(cntl_num, sh->host_no,
1382 /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1383 dma mapping and fills in the scatter gather entries of the
1384 cciss command, cp. */
1387 cciss_scatter_gather(struct pci_dev *pdev,
1388 CommandList_struct *cp,
1389 struct scsi_cmnd *cmd)
1392 struct scatterlist *sg;
1396 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1398 use_sg = scsi_dma_map(cmd);
1399 if (use_sg) { /* not too many addrs? */
1400 scsi_for_each_sg(cmd, sg, use_sg, i) {
1401 addr64 = (__u64) sg_dma_address(sg);
1402 len = sg_dma_len(sg);
1403 cp->SG[i].Addr.lower =
1404 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1405 cp->SG[i].Addr.upper =
1406 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1407 cp->SG[i].Len = len;
1408 cp->SG[i].Ext = 0; // we are not chaining
1412 cp->Header.SGList = (__u8) use_sg; /* no. SGs contig in this cmd */
1413 cp->Header.SGTotal = (__u16) use_sg; /* total sgs in this cmd list */
1419 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1423 unsigned char scsi3addr[8];
1424 CommandList_struct *cp;
1425 unsigned long flags;
1427 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1428 // We violate cmd->host privacy here. (Is there another way?)
1429 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1432 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1433 cmd->device->lun, scsi3addr);
1435 /* the scsi nexus does not match any that we presented... */
1436 /* pretend to mid layer that we got selection timeout */
1437 cmd->result = DID_NO_CONNECT << 16;
1439 /* we might want to think about registering controller itself
1440 as a processor device on the bus so sg binds to it. */
1444 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1445 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1446 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1447 // cmd->target, cmd->lun);
1449 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1450 see what the device thinks of it. */
1452 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1453 cp = scsi_cmd_alloc(*c);
1454 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1455 if (cp == NULL) { /* trouble... */
1456 printk("scsi_cmd_alloc returned NULL!\n");
1457 /* FIXME: next 3 lines are -> BAD! <- */
1458 cmd->result = DID_NO_CONNECT << 16;
1463 // Fill in the command list header
1465 cmd->scsi_done = done; // save this for use by completion code
1467 // save cp in case we have to abort it
1468 cmd->host_scribble = (unsigned char *) cp;
1470 cp->cmd_type = CMD_SCSI;
1472 cp->Header.ReplyQueue = 0; // unused in simple mode
1473 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1474 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1476 // Fill in the request block...
1478 cp->Request.Timeout = 0;
1479 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
1480 BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB));
1481 cp->Request.CDBLen = cmd->cmd_len;
1482 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1483 cp->Request.Type.Type = TYPE_CMD;
1484 cp->Request.Type.Attribute = ATTR_SIMPLE;
1485 switch(cmd->sc_data_direction)
1487 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1488 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1489 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1490 case DMA_BIDIRECTIONAL:
1491 // This can happen if a buggy application does a scsi passthru
1492 // and sets both inlen and outlen to non-zero. ( see
1493 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1495 cp->Request.Type.Direction = XFER_RSVD;
1496 // This is technically wrong, and cciss controllers should
1497 // reject it with CMD_INVALID, which is the most correct
1498 // response, but non-fibre backends appear to let it
1499 // slide by, and give the same results as if this field
1500 // were set correctly. Either way is acceptable for
1501 // our purposes here.
1506 printk("cciss: unknown data direction: %d\n",
1507 cmd->sc_data_direction);
1512 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1514 /* Put the request on the tail of the request queue */
1516 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1517 addQ(&(*c)->reqQ, cp);
1520 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1522 /* the cmd'll come back via intr handler in complete_scsi_command() */
1527 cciss_unregister_scsi(int ctlr)
1529 struct cciss_scsi_adapter_data_t *sa;
1530 struct cciss_scsi_cmd_stack_t *stk;
1531 unsigned long flags;
1533 /* we are being forcibly unloaded, and may not refuse. */
1535 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1536 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1537 stk = &sa->cmd_stack;
1539 /* if we weren't ever actually registered, don't unregister */
1540 if (sa->registered) {
1541 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1542 scsi_remove_host(sa->scsi_host);
1543 scsi_host_put(sa->scsi_host);
1544 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1547 /* set scsi_host to NULL so our detect routine will
1548 find us on register */
1549 sa->scsi_host = NULL;
1550 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1551 scsi_cmd_stack_free(ctlr);
1556 cciss_engage_scsi(int ctlr)
1558 struct cciss_scsi_adapter_data_t *sa;
1559 struct cciss_scsi_cmd_stack_t *stk;
1560 unsigned long flags;
1562 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1563 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1564 stk = &sa->cmd_stack;
1566 if (sa->registered) {
1567 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1568 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1572 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1573 cciss_update_non_disk_devices(ctlr, -1);
1574 cciss_scsi_detect(ctlr);
1579 cciss_seq_tape_report(struct seq_file *seq, int ctlr)
1581 unsigned long flags;
1583 CPQ_TAPE_LOCK(ctlr, flags);
1585 "Sequential access devices: %d\n\n",
1586 ccissscsi[ctlr].ndevices);
1587 CPQ_TAPE_UNLOCK(ctlr, flags);
1590 static int wait_for_device_to_become_ready(ctlr_info_t *h,
1591 unsigned char lunaddr[])
1596 CommandList_struct *c;
1598 c = cmd_alloc(h, 1);
1600 printk(KERN_WARNING "cciss%d: out of memory in "
1601 "wait_for_device_to_become_ready.\n", h->ctlr);
1605 /* Send test unit ready until device ready, or give up. */
1606 while (count < 20) {
1608 /* Wait for a bit. do this first, because if we send
1609 * the TUR right away, the reset will just abort it.
1611 set_current_state(TASK_INTERRUPTIBLE);
1612 schedule_timeout(waittime);
1615 /* Increase wait time with each try, up to a point. */
1616 if (waittime < (HZ * 30))
1617 waittime = waittime * 2;
1619 /* Send the Test Unit Ready */
1620 rc = fill_cmd(c, TEST_UNIT_READY, h->ctlr, NULL, 0, 0, 0, 0,
1623 rc = sendcmd_core(h, c);
1624 /* sendcmd turned off interrupts, turn 'em back on. */
1625 h->access.set_intr_mask(h, CCISS_INTR_ON);
1628 if (rc == 0 && c->err_info->CommandStatus == CMD_SUCCESS)
1632 c->err_info->CommandStatus == CMD_TARGET_STATUS &&
1633 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
1634 (c->err_info->SenseInfo[2] == NO_SENSE ||
1635 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
1638 printk(KERN_WARNING "cciss%d: Waiting %d secs "
1639 "for device to become ready.\n",
1640 h->ctlr, waittime / HZ);
1641 rc = 1; /* device not ready. */
1645 printk("cciss%d: giving up on device.\n", h->ctlr);
1647 printk(KERN_WARNING "cciss%d: device is ready.\n", h->ctlr);
1653 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
1654 * complaining. Doing a host- or bus-reset can't do anything good here.
1655 * Despite what it might say in scsi_error.c, there may well be commands
1656 * on the controller, as the cciss driver registers twice, once as a block
1657 * device for the logical drives, and once as a scsi device, for any tape
1658 * drives. So we know there are no commands out on the tape drives, but we
1659 * don't know there are no commands on the controller, and it is likely
1660 * that there probably are, as the cciss block device is most commonly used
1661 * as a boot device (embedded controller on HP/Compaq systems.)
1664 static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1667 CommandList_struct *cmd_in_trouble;
1668 unsigned char lunaddr[8];
1672 /* find the controller to which the command to be aborted was sent */
1673 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1674 if (c == NULL) /* paranoia */
1677 printk(KERN_WARNING "cciss%d: resetting tape drive or medium changer.\n", ctlr);
1678 /* find the command that's giving us trouble */
1679 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
1680 if (cmd_in_trouble == NULL) /* paranoia */
1682 memcpy(lunaddr, &cmd_in_trouble->Header.LUN.LunAddrBytes[0], 8);
1683 /* send a reset to the SCSI LUN which the command was sent to */
1684 rc = sendcmd(CCISS_RESET_MSG, ctlr, NULL, 0, 2, 0, 0, lunaddr,
1686 /* sendcmd turned off interrupts on the board, turn 'em back on. */
1687 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1688 if (rc == 0 && wait_for_device_to_become_ready(*c, lunaddr) == 0)
1690 printk(KERN_WARNING "cciss%d: resetting device failed.\n", ctlr);
1694 static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1697 CommandList_struct *cmd_to_abort;
1701 /* find the controller to which the command to be aborted was sent */
1702 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1703 if (c == NULL) /* paranoia */
1706 printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd\n", ctlr);
1708 /* find the command to be aborted */
1709 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
1710 if (cmd_to_abort == NULL) /* paranoia */
1712 rc = sendcmd(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag,
1714 (unsigned char *) &cmd_to_abort->Header.LUN.LunAddrBytes[0],
1716 /* sendcmd turned off interrupts on the board, turn 'em back on. */
1717 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1724 #else /* no CONFIG_CISS_SCSI_TAPE */
1726 /* If no tape support, then these become defined out of existence */
1728 #define cciss_scsi_setup(cntl_num)
1730 #endif /* CONFIG_CISS_SCSI_TAPE */