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 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
49 __u8 page_code, unsigned char *scsi3addr,
52 static CommandList_struct *cmd_alloc(ctlr_info_t *h, int get_from_pool);
53 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool);
55 static int cciss_scsi_proc_info(
57 char *buffer, /* data buffer */
58 char **start, /* where data in buffer starts */
59 off_t offset, /* offset from start of imaginary file */
60 int length, /* length of data in buffer */
61 int func); /* 0 == read, 1 == write */
63 static int cciss_scsi_queue_command (struct scsi_cmnd *cmd,
64 void (* done)(struct scsi_cmnd *));
65 static int cciss_eh_device_reset_handler(struct scsi_cmnd *);
66 static int cciss_eh_abort_handler(struct scsi_cmnd *);
68 static struct cciss_scsi_hba_t ccissscsi[MAX_CTLR] = {
69 { .name = "cciss0", .ndevices = 0 },
70 { .name = "cciss1", .ndevices = 0 },
71 { .name = "cciss2", .ndevices = 0 },
72 { .name = "cciss3", .ndevices = 0 },
73 { .name = "cciss4", .ndevices = 0 },
74 { .name = "cciss5", .ndevices = 0 },
75 { .name = "cciss6", .ndevices = 0 },
76 { .name = "cciss7", .ndevices = 0 },
79 static struct scsi_host_template cciss_driver_template = {
80 .module = THIS_MODULE,
83 .proc_info = cciss_scsi_proc_info,
84 .queuecommand = cciss_scsi_queue_command,
85 .can_queue = SCSI_CCISS_CAN_QUEUE,
87 .sg_tablesize = MAXSGENTRIES,
89 .use_clustering = DISABLE_CLUSTERING,
90 /* Can't have eh_bus_reset_handler or eh_host_reset_handler for cciss */
91 .eh_device_reset_handler= cciss_eh_device_reset_handler,
92 .eh_abort_handler = cciss_eh_abort_handler,
100 struct cciss_scsi_cmd_stack_elem_t {
101 CommandList_struct cmd;
102 ErrorInfo_struct Err;
104 u8 pad[IS_32_BIT * SCSI_PAD_32 + IS_64_BIT * SCSI_PAD_64];
109 #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
110 CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
111 // plus two for init time usage
114 struct cciss_scsi_cmd_stack_t {
115 struct cciss_scsi_cmd_stack_elem_t *pool;
116 struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];
117 dma_addr_t cmd_pool_handle;
122 struct cciss_scsi_adapter_data_t {
123 struct Scsi_Host *scsi_host;
124 struct cciss_scsi_cmd_stack_t cmd_stack;
126 spinlock_t lock; // to protect ccissscsi[ctlr];
129 #define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \
130 &(((struct cciss_scsi_adapter_data_t *) \
131 hba[ctlr]->scsi_ctlr)->lock), flags);
132 #define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \
133 &(((struct cciss_scsi_adapter_data_t *) \
134 hba[ctlr]->scsi_ctlr)->lock), flags);
136 static CommandList_struct *
137 scsi_cmd_alloc(ctlr_info_t *h)
139 /* assume only one process in here at a time, locking done by caller. */
140 /* use CCISS_LOCK(ctlr) */
141 /* might be better to rewrite how we allocate scsi commands in a way that */
142 /* needs no locking at all. */
144 /* take the top memory chunk off the stack and return it, if any. */
145 struct cciss_scsi_cmd_stack_elem_t *c;
146 struct cciss_scsi_adapter_data_t *sa;
147 struct cciss_scsi_cmd_stack_t *stk;
150 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
151 stk = &sa->cmd_stack;
155 c = stk->elem[stk->top];
156 /* memset(c, 0, sizeof(*c)); */
157 memset(&c->cmd, 0, sizeof(c->cmd));
158 memset(&c->Err, 0, sizeof(c->Err));
159 /* set physical addr of cmd and addr of scsi parameters */
160 c->cmd.busaddr = c->busaddr;
161 /* (__u32) (stk->cmd_pool_handle +
162 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top)); */
164 temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));
165 /* (__u64) (stk->cmd_pool_handle +
166 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top) +
167 sizeof(CommandList_struct)); */
169 c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;
170 c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;
171 c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);
173 c->cmd.ctlr = h->ctlr;
174 c->cmd.err_info = &c->Err;
176 return (CommandList_struct *) c;
180 scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
182 /* assume only one process in here at a time, locking done by caller. */
183 /* use CCISS_LOCK(ctlr) */
184 /* drop the free memory chunk on top of the stack. */
186 struct cciss_scsi_adapter_data_t *sa;
187 struct cciss_scsi_cmd_stack_t *stk;
189 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
190 stk = &sa->cmd_stack;
191 if (stk->top >= CMD_STACK_SIZE) {
192 printk("cciss: scsi_cmd_free called too many times.\n");
196 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
200 scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
203 struct cciss_scsi_cmd_stack_t *stk;
206 stk = &sa->cmd_stack;
207 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
209 /* Check alignment, see cciss_cmd.h near CommandList_struct def. */
210 BUILD_BUG_ON((sizeof(*stk->pool) % COMMANDLIST_ALIGNMENT) != 0);
211 /* pci_alloc_consistent guarantees 32-bit DMA address will be used */
212 stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
213 pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
215 if (stk->pool == NULL) {
216 printk("stk->pool is null\n");
220 for (i=0; i<CMD_STACK_SIZE; i++) {
221 stk->elem[i] = &stk->pool[i];
222 stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle +
223 (sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
225 stk->top = CMD_STACK_SIZE-1;
230 scsi_cmd_stack_free(int ctlr)
232 struct cciss_scsi_adapter_data_t *sa;
233 struct cciss_scsi_cmd_stack_t *stk;
236 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
237 stk = &sa->cmd_stack;
238 if (stk->top != CMD_STACK_SIZE-1) {
239 printk( "cciss: %d scsi commands are still outstanding.\n",
240 CMD_STACK_SIZE - stk->top);
242 printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);
244 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
246 pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
251 static int xmargin=8;
252 static int amargin=60;
255 print_bytes (unsigned char *c, int len, int hex, int ascii)
266 if ((i % xmargin) == 0 && i>0) printk("\n");
267 if ((i % xmargin) == 0) printk("0x%04x:", i);
278 if ((i % amargin) == 0 && i>0) printk("\n");
279 if ((i % amargin) == 0) printk("0x%04x:", i);
280 if (*x > 26 && *x < 128) printk("%c", *x);
289 print_cmd(CommandList_struct *cp)
291 printk("queue:%d\n", cp->Header.ReplyQueue);
292 printk("sglist:%d\n", cp->Header.SGList);
293 printk("sgtot:%d\n", cp->Header.SGTotal);
294 printk("Tag:0x%08x/0x%08x\n", cp->Header.Tag.upper,
295 cp->Header.Tag.lower);
296 printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
297 cp->Header.LUN.LunAddrBytes[0],
298 cp->Header.LUN.LunAddrBytes[1],
299 cp->Header.LUN.LunAddrBytes[2],
300 cp->Header.LUN.LunAddrBytes[3],
301 cp->Header.LUN.LunAddrBytes[4],
302 cp->Header.LUN.LunAddrBytes[5],
303 cp->Header.LUN.LunAddrBytes[6],
304 cp->Header.LUN.LunAddrBytes[7]);
305 printk("CDBLen:%d\n", cp->Request.CDBLen);
306 printk("Type:%d\n",cp->Request.Type.Type);
307 printk("Attr:%d\n",cp->Request.Type.Attribute);
308 printk(" Dir:%d\n",cp->Request.Type.Direction);
309 printk("Timeout:%d\n",cp->Request.Timeout);
310 printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"
311 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
312 cp->Request.CDB[0], cp->Request.CDB[1],
313 cp->Request.CDB[2], cp->Request.CDB[3],
314 cp->Request.CDB[4], cp->Request.CDB[5],
315 cp->Request.CDB[6], cp->Request.CDB[7],
316 cp->Request.CDB[8], cp->Request.CDB[9],
317 cp->Request.CDB[10], cp->Request.CDB[11],
318 cp->Request.CDB[12], cp->Request.CDB[13],
319 cp->Request.CDB[14], cp->Request.CDB[15]),
320 printk("edesc.Addr: 0x%08x/0%08x, Len = %d\n",
321 cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower,
323 printk("sgs..........Errorinfo:\n");
324 printk("scsistatus:%d\n", cp->err_info->ScsiStatus);
325 printk("senselen:%d\n", cp->err_info->SenseLen);
326 printk("cmd status:%d\n", cp->err_info->CommandStatus);
327 printk("resid cnt:%d\n", cp->err_info->ResidualCnt);
328 printk("offense size:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);
329 printk("offense byte:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);
330 printk("offense value:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
337 find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
339 /* finds an unused bus, target, lun for a new device */
340 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
342 unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
344 memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
346 target_taken[SELF_SCSI_ID] = 1;
347 for (i=0;i<ccissscsi[ctlr].ndevices;i++)
348 target_taken[ccissscsi[ctlr].dev[i].target] = 1;
350 for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {
351 if (!target_taken[i]) {
352 *bus = 0; *target=i; *lun = 0; found=1;
360 int bus, target, lun;
364 cciss_scsi_add_entry(int ctlr, int hostno,
365 struct cciss_scsi_dev_t *device,
366 struct scsi2map *added, int *nadded)
368 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
369 int n = ccissscsi[ctlr].ndevices;
370 struct cciss_scsi_dev_t *sd;
371 int i, bus, target, lun;
372 unsigned char addr1[8], addr2[8];
374 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
375 printk("cciss%d: Too many devices, "
376 "some will be inaccessible.\n", ctlr);
382 /* Is this device a non-zero lun of a multi-lun device */
383 /* byte 4 of the 8-byte LUN addr will contain the logical unit no. */
384 if (device->scsi3addr[4] != 0) {
385 /* Search through our list and find the device which */
386 /* has the same 8 byte LUN address, excepting byte 4. */
387 /* Assign the same bus and target for this new LUN. */
388 /* Use the logical unit number from the firmware. */
389 memcpy(addr1, device->scsi3addr, 8);
391 for (i = 0; i < n; i++) {
392 sd = &ccissscsi[ctlr].dev[i];
393 memcpy(addr2, sd->scsi3addr, 8);
395 /* differ only in byte 4? */
396 if (memcmp(addr1, addr2, 8) == 0) {
399 lun = device->scsi3addr[4];
405 sd = &ccissscsi[ctlr].dev[n];
407 if (find_bus_target_lun(ctlr,
408 &sd->bus, &sd->target, &sd->lun) != 0)
415 added[*nadded].bus = sd->bus;
416 added[*nadded].target = sd->target;
417 added[*nadded].lun = sd->lun;
420 memcpy(sd->scsi3addr, device->scsi3addr, 8);
421 memcpy(sd->vendor, device->vendor, sizeof(sd->vendor));
422 memcpy(sd->revision, device->revision, sizeof(sd->revision));
423 memcpy(sd->device_id, device->device_id, sizeof(sd->device_id));
424 sd->devtype = device->devtype;
426 ccissscsi[ctlr].ndevices++;
428 /* initially, (before registering with scsi layer) we don't
429 know our hostno and we don't want to print anything first
430 time anyway (the scsi layer's inquiries will show that info) */
432 printk("cciss%d: %s device c%db%dt%dl%d added.\n",
433 ctlr, scsi_device_type(sd->devtype), hostno,
434 sd->bus, sd->target, sd->lun);
439 cciss_scsi_remove_entry(int ctlr, int hostno, int entry,
440 struct scsi2map *removed, int *nremoved)
442 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
444 struct cciss_scsi_dev_t sd;
446 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
447 sd = ccissscsi[ctlr].dev[entry];
448 removed[*nremoved].bus = sd.bus;
449 removed[*nremoved].target = sd.target;
450 removed[*nremoved].lun = sd.lun;
452 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
453 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
454 ccissscsi[ctlr].ndevices--;
455 printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
456 ctlr, scsi_device_type(sd.devtype), hostno,
457 sd.bus, sd.target, sd.lun);
461 #define SCSI3ADDR_EQ(a,b) ( \
462 (a)[7] == (b)[7] && \
463 (a)[6] == (b)[6] && \
464 (a)[5] == (b)[5] && \
465 (a)[4] == (b)[4] && \
466 (a)[3] == (b)[3] && \
467 (a)[2] == (b)[2] && \
468 (a)[1] == (b)[1] && \
471 static void fixup_botched_add(int ctlr, char *scsi3addr)
473 /* called when scsi_add_device fails in order to re-adjust */
474 /* ccissscsi[] to match the mid layer's view. */
477 CPQ_TAPE_LOCK(ctlr, flags);
478 for (i = 0; i < ccissscsi[ctlr].ndevices; i++) {
479 if (memcmp(scsi3addr,
480 ccissscsi[ctlr].dev[i].scsi3addr, 8) == 0) {
481 for (j = i; j < ccissscsi[ctlr].ndevices-1; j++)
482 ccissscsi[ctlr].dev[j] =
483 ccissscsi[ctlr].dev[j+1];
484 ccissscsi[ctlr].ndevices--;
488 CPQ_TAPE_UNLOCK(ctlr, flags);
491 static int device_is_the_same(struct cciss_scsi_dev_t *dev1,
492 struct cciss_scsi_dev_t *dev2)
494 return dev1->devtype == dev2->devtype &&
495 memcmp(dev1->scsi3addr, dev2->scsi3addr,
496 sizeof(dev1->scsi3addr)) == 0 &&
497 memcmp(dev1->device_id, dev2->device_id,
498 sizeof(dev1->device_id)) == 0 &&
499 memcmp(dev1->vendor, dev2->vendor,
500 sizeof(dev1->vendor)) == 0 &&
501 memcmp(dev1->model, dev2->model,
502 sizeof(dev1->model)) == 0 &&
503 memcmp(dev1->revision, dev2->revision,
504 sizeof(dev1->revision)) == 0;
508 adjust_cciss_scsi_table(int ctlr, int hostno,
509 struct cciss_scsi_dev_t sd[], int nsds)
511 /* sd contains scsi3 addresses and devtypes, but
512 bus target and lun are not filled in. This funciton
513 takes what's in sd to be the current and adjusts
514 ccissscsi[] to be in line with what's in sd. */
516 int i,j, found, changes=0;
517 struct cciss_scsi_dev_t *csd;
519 struct scsi2map *added, *removed;
520 int nadded, nremoved;
521 struct Scsi_Host *sh = NULL;
523 added = kzalloc(sizeof(*added) * CCISS_MAX_SCSI_DEVS_PER_HBA,
525 removed = kzalloc(sizeof(*removed) * CCISS_MAX_SCSI_DEVS_PER_HBA,
528 if (!added || !removed) {
529 printk(KERN_WARNING "cciss%d: Out of memory in "
530 "adjust_cciss_scsi_table\n", ctlr);
534 CPQ_TAPE_LOCK(ctlr, flags);
536 if (hostno != -1) /* if it's not the first time... */
537 sh = ((struct cciss_scsi_adapter_data_t *)
538 hba[ctlr]->scsi_ctlr)->scsi_host;
540 /* find any devices in ccissscsi[] that are not in
541 sd[] and remove them from ccissscsi[] */
546 while(i<ccissscsi[ctlr].ndevices) {
547 csd = &ccissscsi[ctlr].dev[i];
549 for (j=0;j<nsds;j++) {
550 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
552 if (device_is_the_same(&sd[j], csd))
560 if (found == 0) { /* device no longer present. */
562 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
563 ctlr, scsi_device_type(csd->devtype), hostno,
564 csd->bus, csd->target, csd->lun); */
565 cciss_scsi_remove_entry(ctlr, hostno, i,
567 /* remove ^^^, hence i not incremented */
568 } else if (found == 1) { /* device is different in some way */
570 printk("cciss%d: device c%db%dt%dl%d has changed.\n",
571 ctlr, hostno, csd->bus, csd->target, csd->lun);
572 cciss_scsi_remove_entry(ctlr, hostno, i,
574 /* remove ^^^, hence i not incremented */
575 if (cciss_scsi_add_entry(ctlr, hostno, &sd[j],
576 added, &nadded) != 0)
577 /* we just removed one, so add can't fail. */
579 csd->devtype = sd[j].devtype;
580 memcpy(csd->device_id, sd[j].device_id,
581 sizeof(csd->device_id));
582 memcpy(csd->vendor, sd[j].vendor,
583 sizeof(csd->vendor));
584 memcpy(csd->model, sd[j].model,
586 memcpy(csd->revision, sd[j].revision,
587 sizeof(csd->revision));
588 } else /* device is same as it ever was, */
589 i++; /* so just move along. */
592 /* Now, make sure every device listed in sd[] is also
593 listed in ccissscsi[], adding them if they aren't found */
595 for (i=0;i<nsds;i++) {
597 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
598 csd = &ccissscsi[ctlr].dev[j];
599 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
601 if (device_is_the_same(&sd[i], csd))
602 found=2; /* found device */
604 found=1; /* found a bug. */
610 if (cciss_scsi_add_entry(ctlr, hostno, &sd[i],
611 added, &nadded) != 0)
613 } else if (found == 1) {
614 /* should never happen... */
616 printk(KERN_WARNING "cciss%d: device "
617 "unexpectedly changed\n", ctlr);
618 /* but if it does happen, we just ignore that device */
621 CPQ_TAPE_UNLOCK(ctlr, flags);
623 /* Don't notify scsi mid layer of any changes the first time through */
624 /* (or if there are no changes) scsi_scan_host will do it later the */
625 /* first time through. */
626 if (hostno == -1 || !changes)
629 /* Notify scsi mid layer of any removed devices */
630 for (i = 0; i < nremoved; i++) {
631 struct scsi_device *sdev =
632 scsi_device_lookup(sh, removed[i].bus,
633 removed[i].target, removed[i].lun);
635 scsi_remove_device(sdev);
636 scsi_device_put(sdev);
638 /* We don't expect to get here. */
639 /* future cmds to this device will get selection */
640 /* timeout as if the device was gone. */
641 printk(KERN_WARNING "cciss%d: didn't find "
642 "c%db%dt%dl%d\n for removal.",
643 ctlr, hostno, removed[i].bus,
644 removed[i].target, removed[i].lun);
648 /* Notify scsi mid layer of any added devices */
649 for (i = 0; i < nadded; i++) {
651 rc = scsi_add_device(sh, added[i].bus,
652 added[i].target, added[i].lun);
655 printk(KERN_WARNING "cciss%d: scsi_add_device "
656 "c%db%dt%dl%d failed, device not added.\n",
658 added[i].bus, added[i].target, added[i].lun);
659 /* now we have to remove it from ccissscsi, */
660 /* since it didn't get added to scsi mid layer */
661 fixup_botched_add(ctlr, added[i].scsi3addr);
671 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
674 struct cciss_scsi_dev_t *sd;
677 CPQ_TAPE_LOCK(ctlr, flags);
678 for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
679 sd = &ccissscsi[ctlr].dev[i];
680 if (sd->bus == bus &&
681 sd->target == target &&
683 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
684 CPQ_TAPE_UNLOCK(ctlr, flags);
688 CPQ_TAPE_UNLOCK(ctlr, flags);
693 cciss_scsi_setup(int cntl_num)
695 struct cciss_scsi_adapter_data_t * shba;
697 ccissscsi[cntl_num].ndevices = 0;
698 shba = (struct cciss_scsi_adapter_data_t *)
699 kmalloc(sizeof(*shba), GFP_KERNEL);
702 shba->scsi_host = NULL;
703 spin_lock_init(&shba->lock);
704 shba->registered = 0;
705 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
709 hba[cntl_num]->scsi_ctlr = (void *) shba;
714 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
716 struct scsi_cmnd *cmd;
718 ErrorInfo_struct *ei;
722 /* First, see if it was a message rather than a command */
723 if (cp->Request.Type.Type == TYPE_MSG) {
724 cp->cmd_type = CMD_MSG_DONE;
728 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
729 ctlr = hba[cp->ctlr];
733 cmd->result = (DID_OK << 16); /* host byte */
734 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
735 /* cmd->result |= (GOOD < 1); */ /* status byte */
737 cmd->result |= (ei->ScsiStatus);
738 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
740 /* copy the sense data whether we need to or not. */
742 memcpy(cmd->sense_buffer, ei->SenseInfo,
743 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
744 SCSI_SENSE_BUFFERSIZE :
746 scsi_set_resid(cmd, ei->ResidualCnt);
748 if(ei->CommandStatus != 0)
749 { /* an error has occurred */
750 switch(ei->CommandStatus)
752 case CMD_TARGET_STATUS:
753 /* Pass it up to the upper layers... */
757 printk(KERN_WARNING "cciss: cmd %p "
758 "has SCSI Status = %x\n",
762 cmd->result |= (ei->ScsiStatus << 1);
764 else { /* scsi status is zero??? How??? */
766 /* Ordinarily, this case should never happen, but there is a bug
767 in some released firmware revisions that allows it to happen
768 if, for example, a 4100 backplane loses power and the tape
769 drive is in it. We assume that it's a fatal error of some
770 kind because we can't show that it wasn't. We will make it
771 look like selection timeout since that is the most common
772 reason for this to occur, and it's severe enough. */
774 cmd->result = DID_NO_CONNECT << 16;
777 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
779 case CMD_DATA_OVERRUN:
780 printk(KERN_WARNING "cciss: cp %p has"
781 " completed with data overrun "
785 /* print_bytes(cp, sizeof(*cp), 1, 0);
787 /* We get CMD_INVALID if you address a non-existent tape drive instead
788 of a selection timeout (no response). You will see this if you yank
789 out a tape drive, then try to access it. This is kind of a shame
790 because it means that any other CMD_INVALID (e.g. driver bug) will
791 get interpreted as a missing target. */
792 cmd->result = DID_NO_CONNECT << 16;
795 case CMD_PROTOCOL_ERR:
796 printk(KERN_WARNING "cciss: cp %p has "
797 "protocol error \n", cp);
799 case CMD_HARDWARE_ERR:
800 cmd->result = DID_ERROR << 16;
801 printk(KERN_WARNING "cciss: cp %p had "
802 " hardware error\n", cp);
804 case CMD_CONNECTION_LOST:
805 cmd->result = DID_ERROR << 16;
806 printk(KERN_WARNING "cciss: cp %p had "
807 "connection lost\n", cp);
810 cmd->result = DID_ABORT << 16;
811 printk(KERN_WARNING "cciss: cp %p was "
814 case CMD_ABORT_FAILED:
815 cmd->result = DID_ERROR << 16;
816 printk(KERN_WARNING "cciss: cp %p reports "
817 "abort failed\n", cp);
819 case CMD_UNSOLICITED_ABORT:
820 cmd->result = DID_ABORT << 16;
821 printk(KERN_WARNING "cciss: cp %p aborted "
822 "do to an unsolicited abort\n", cp);
825 cmd->result = DID_TIME_OUT << 16;
826 printk(KERN_WARNING "cciss: cp %p timedout\n",
830 cmd->result = DID_ERROR << 16;
831 printk(KERN_WARNING "cciss: cp %p returned "
832 "unknown status %x\n", cp,
836 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
837 // cmd->target, cmd->lun);
839 scsi_cmd_free(ctlr, cp);
843 cciss_scsi_detect(int ctlr)
845 struct Scsi_Host *sh;
848 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
851 sh->io_port = 0; // good enough? FIXME,
852 sh->n_io_port = 0; // I don't think we use these two...
853 sh->this_id = SELF_SCSI_ID;
855 ((struct cciss_scsi_adapter_data_t *)
856 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
857 sh->hostdata[0] = (unsigned long) hba[ctlr];
858 sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT];
859 sh->unique_id = sh->irq;
860 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
873 cciss_unmap_one(struct pci_dev *pdev,
874 CommandList_struct *cp,
880 addr64.val32.lower = cp->SG[0].Addr.lower;
881 addr64.val32.upper = cp->SG[0].Addr.upper;
882 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
886 cciss_map_one(struct pci_dev *pdev,
887 CommandList_struct *cp,
894 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
895 cp->SG[0].Addr.lower =
896 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
897 cp->SG[0].Addr.upper =
898 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
899 cp->SG[0].Len = buflen;
900 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
901 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
905 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
906 CommandList_struct *cp,
907 unsigned char *scsi3addr,
909 unsigned char cdblen,
910 unsigned char *buf, int bufsize,
914 DECLARE_COMPLETION_ONSTACK(wait);
916 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
918 cp->Header.ReplyQueue = 0; // unused in simple mode
919 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
920 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
921 // Fill in the request block...
923 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
924 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
925 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
927 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
928 memcpy(cp->Request.CDB, cdb, cdblen);
929 cp->Request.Timeout = 0;
930 cp->Request.CDBLen = cdblen;
931 cp->Request.Type.Type = TYPE_CMD;
932 cp->Request.Type.Attribute = ATTR_SIMPLE;
933 cp->Request.Type.Direction = direction;
935 /* Fill in the SG list and do dma mapping */
936 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
937 bufsize, DMA_FROM_DEVICE);
941 /* Put the request on the tail of the request queue */
942 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
946 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
948 wait_for_completion(&wait);
950 /* undo the dma mapping */
951 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
956 cciss_scsi_interpret_error(CommandList_struct *cp)
958 ErrorInfo_struct *ei;
961 switch(ei->CommandStatus)
963 case CMD_TARGET_STATUS:
964 printk(KERN_WARNING "cciss: cmd %p has "
965 "completed with errors\n", cp);
966 printk(KERN_WARNING "cciss: cmd %p "
967 "has SCSI Status = %x\n",
970 if (ei->ScsiStatus == 0)
972 "cciss:SCSI status is abnormally zero. "
973 "(probably indicates selection timeout "
974 "reported incorrectly due to a known "
975 "firmware bug, circa July, 2001.)\n");
977 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
978 printk("UNDERRUN\n");
980 case CMD_DATA_OVERRUN:
981 printk(KERN_WARNING "cciss: cp %p has"
982 " completed with data overrun "
986 /* controller unfortunately reports SCSI passthru's */
987 /* to non-existent targets as invalid commands. */
988 printk(KERN_WARNING "cciss: cp %p is "
989 "reported invalid (probably means "
990 "target device no longer present)\n",
992 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
996 case CMD_PROTOCOL_ERR:
997 printk(KERN_WARNING "cciss: cp %p has "
998 "protocol error \n", cp);
1000 case CMD_HARDWARE_ERR:
1001 /* cmd->result = DID_ERROR << 16; */
1002 printk(KERN_WARNING "cciss: cp %p had "
1003 " hardware error\n", cp);
1005 case CMD_CONNECTION_LOST:
1006 printk(KERN_WARNING "cciss: cp %p had "
1007 "connection lost\n", cp);
1010 printk(KERN_WARNING "cciss: cp %p was "
1013 case CMD_ABORT_FAILED:
1014 printk(KERN_WARNING "cciss: cp %p reports "
1015 "abort failed\n", cp);
1017 case CMD_UNSOLICITED_ABORT:
1018 printk(KERN_WARNING "cciss: cp %p aborted "
1019 "do to an unsolicited abort\n", cp);
1022 printk(KERN_WARNING "cciss: cp %p timedout\n",
1026 printk(KERN_WARNING "cciss: cp %p returned "
1027 "unknown status %x\n", cp,
1033 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
1034 unsigned char page, unsigned char *buf,
1035 unsigned char bufsize)
1038 CommandList_struct *cp;
1040 ErrorInfo_struct *ei;
1041 unsigned long flags;
1043 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1044 cp = scsi_cmd_alloc(c);
1045 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1047 if (cp == NULL) { /* trouble... */
1048 printk("cmd_alloc returned NULL!\n");
1054 cdb[0] = CISS_INQUIRY;
1055 cdb[1] = (page != 0);
1060 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
1061 6, buf, bufsize, XFER_READ);
1063 if (rc != 0) return rc; /* something went wrong */
1065 if (ei->CommandStatus != 0 &&
1066 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1067 cciss_scsi_interpret_error(cp);
1070 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1071 scsi_cmd_free(c, cp);
1072 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1076 /* Get the device id from inquiry page 0x83 */
1077 static int cciss_scsi_get_device_id(ctlr_info_t *c, unsigned char *scsi3addr,
1078 unsigned char *device_id, int buflen)
1085 buf = kzalloc(64, GFP_KERNEL);
1088 rc = cciss_scsi_do_inquiry(c, scsi3addr, 0x83, buf, 64);
1090 memcpy(device_id, &buf[8], buflen);
1096 cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
1097 ReportLunData_struct *buf, int bufsize)
1100 CommandList_struct *cp;
1101 unsigned char cdb[12];
1102 unsigned char scsi3addr[8];
1103 ErrorInfo_struct *ei;
1104 unsigned long flags;
1106 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1107 cp = scsi_cmd_alloc(c);
1108 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1109 if (cp == NULL) { /* trouble... */
1110 printk("cmd_alloc returned NULL!\n");
1114 memset(&scsi3addr[0], 0, 8); /* address the controller */
1115 cdb[0] = CISS_REPORT_PHYS;
1121 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
1122 cdb[7] = (bufsize >> 16) & 0xFF;
1123 cdb[8] = (bufsize >> 8) & 0xFF;
1124 cdb[9] = bufsize & 0xFF;
1128 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
1130 (unsigned char *) buf,
1131 bufsize, XFER_READ);
1133 if (rc != 0) return rc; /* something went wrong */
1136 if (ei->CommandStatus != 0 &&
1137 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1138 cciss_scsi_interpret_error(cp);
1141 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1142 scsi_cmd_free(c, cp);
1143 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1148 cciss_update_non_disk_devices(int cntl_num, int hostno)
1150 /* the idea here is we could get notified from /proc
1151 that some devices have changed, so we do a report
1152 physical luns cmd, and adjust our list of devices
1153 accordingly. (We can't rely on the scsi-mid layer just
1154 doing inquiries, because the "busses" that the scsi
1155 mid-layer probes are totally fabricated by this driver,
1156 so new devices wouldn't show up.
1158 the scsi3addr's of devices won't change so long as the
1159 adapter is not reset. That means we can rescan and
1160 tell which devices we already know about, vs. new
1161 devices, vs. disappearing devices.
1163 Also, if you yank out a tape drive, then put in a disk
1164 in it's place, (say, a configured volume from another
1165 array controller for instance) _don't_ poke this driver
1166 (so it thinks it's still a tape, but _do_ poke the scsi
1167 mid layer, so it does an inquiry... the scsi mid layer
1168 will see the physical disk. This would be bad. Need to
1169 think about how to prevent that. One idea would be to
1170 snoop all scsi responses and if an inquiry repsonse comes
1171 back that reports a disk, chuck it an return selection
1172 timeout instead and adjust our table... Not sure i like
1176 #define OBDR_TAPE_INQ_SIZE 49
1177 #define OBDR_TAPE_SIG "$DR-10"
1178 ReportLunData_struct *ld_buff;
1179 unsigned char *inq_buff;
1180 unsigned char scsi3addr[8];
1184 struct cciss_scsi_dev_t *currentsd, *this_device;
1186 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1189 c = (ctlr_info_t *) hba[cntl_num];
1190 ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
1191 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1192 currentsd = kzalloc(sizeof(*currentsd) *
1193 (CCISS_MAX_SCSI_DEVS_PER_HBA+1), GFP_KERNEL);
1194 if (ld_buff == NULL || inq_buff == NULL || currentsd == NULL) {
1195 printk(KERN_ERR "cciss: out of memory\n");
1198 this_device = ¤tsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1199 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1200 ch = &ld_buff->LUNListLength[0];
1201 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1202 if (num_luns > CISS_MAX_PHYS_LUN) {
1204 "cciss: Maximum physical LUNs (%d) exceeded. "
1205 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1206 num_luns - CISS_MAX_PHYS_LUN);
1207 num_luns = CISS_MAX_PHYS_LUN;
1211 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1216 /* adjust our table of devices */
1217 for (i = 0; i < num_luns; i++) {
1218 /* for each physical lun, do an inquiry */
1219 if (ld_buff->LUN[i][3] & 0xC0) continue;
1220 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1221 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1223 if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, 0, inq_buff,
1224 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0)
1225 /* Inquiry failed (msg printed already) */
1226 continue; /* so we will skip this device. */
1228 this_device->devtype = (inq_buff[0] & 0x1f);
1229 this_device->bus = -1;
1230 this_device->target = -1;
1231 this_device->lun = -1;
1232 memcpy(this_device->scsi3addr, scsi3addr, 8);
1233 memcpy(this_device->vendor, &inq_buff[8],
1234 sizeof(this_device->vendor));
1235 memcpy(this_device->model, &inq_buff[16],
1236 sizeof(this_device->model));
1237 memcpy(this_device->revision, &inq_buff[32],
1238 sizeof(this_device->revision));
1239 memset(this_device->device_id, 0,
1240 sizeof(this_device->device_id));
1241 cciss_scsi_get_device_id(hba[cntl_num], scsi3addr,
1242 this_device->device_id, sizeof(this_device->device_id));
1244 switch (this_device->devtype)
1246 case 0x05: /* CD-ROM */ {
1248 /* We don't *really* support actual CD-ROM devices,
1249 * just this "One Button Disaster Recovery" tape drive
1250 * which temporarily pretends to be a CD-ROM drive.
1251 * So we check that the device is really an OBDR tape
1252 * device by checking for "$DR-10" in bytes 43-48 of
1257 strncpy(obdr_sig, &inq_buff[43], 6);
1259 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1260 /* Not OBDR device, ignore it. */
1263 /* fall through . . . */
1264 case 0x01: /* sequential access, (tape) */
1265 case 0x08: /* medium changer */
1266 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1267 printk(KERN_INFO "cciss%d: %s ignored, "
1268 "too many devices.\n", cntl_num,
1269 scsi_device_type(this_device->devtype));
1272 currentsd[ncurrent] = *this_device;
1280 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1289 is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1291 int verb_len = strlen(verb);
1292 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1299 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1303 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1304 cciss_update_non_disk_devices(ctlr, hostno);
1312 cciss_scsi_proc_info(struct Scsi_Host *sh,
1313 char *buffer, /* data buffer */
1314 char **start, /* where data in buffer starts */
1315 off_t offset, /* offset from start of imaginary file */
1316 int length, /* length of data in buffer */
1317 int func) /* 0 == read, 1 == write */
1320 int buflen, datalen;
1326 ci = (ctlr_info_t *) sh->hostdata[0];
1327 if (ci == NULL) /* This really shouldn't ever happen. */
1330 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1332 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1333 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n",
1334 cntl_num, sh->host_no);
1336 /* this information is needed by apps to know which cciss
1337 device corresponds to which scsi host number without
1338 having to open a scsi target device node. The device
1339 information is not a duplicate of /proc/scsi/scsi because
1340 the two may be out of sync due to scsi hotplug, rather
1341 this info is for an app to be able to use to know how to
1342 get them back in sync. */
1344 for (i=0;i<ccissscsi[cntl_num].ndevices;i++) {
1345 struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i];
1346 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
1347 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1348 sh->host_no, sd->bus, sd->target, sd->lun,
1350 sd->scsi3addr[0], sd->scsi3addr[1],
1351 sd->scsi3addr[2], sd->scsi3addr[3],
1352 sd->scsi3addr[4], sd->scsi3addr[5],
1353 sd->scsi3addr[6], sd->scsi3addr[7]);
1355 datalen = buflen - offset;
1356 if (datalen < 0) { /* they're reading past EOF. */
1358 *start = buffer+buflen;
1360 *start = buffer + offset;
1362 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1363 return cciss_scsi_user_command(cntl_num, sh->host_no,
1367 /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1368 dma mapping and fills in the scatter gather entries of the
1369 cciss command, cp. */
1372 cciss_scatter_gather(struct pci_dev *pdev,
1373 CommandList_struct *cp,
1374 struct scsi_cmnd *cmd)
1377 struct scatterlist *sg;
1381 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1383 use_sg = scsi_dma_map(cmd);
1384 if (use_sg) { /* not too many addrs? */
1385 scsi_for_each_sg(cmd, sg, use_sg, i) {
1386 addr64 = (__u64) sg_dma_address(sg);
1387 len = sg_dma_len(sg);
1388 cp->SG[i].Addr.lower =
1389 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1390 cp->SG[i].Addr.upper =
1391 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1392 cp->SG[i].Len = len;
1393 cp->SG[i].Ext = 0; // we are not chaining
1397 cp->Header.SGList = (__u8) use_sg; /* no. SGs contig in this cmd */
1398 cp->Header.SGTotal = (__u16) use_sg; /* total sgs in this cmd list */
1404 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1408 unsigned char scsi3addr[8];
1409 CommandList_struct *cp;
1410 unsigned long flags;
1412 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1413 // We violate cmd->host privacy here. (Is there another way?)
1414 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1417 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1418 cmd->device->lun, scsi3addr);
1420 /* the scsi nexus does not match any that we presented... */
1421 /* pretend to mid layer that we got selection timeout */
1422 cmd->result = DID_NO_CONNECT << 16;
1424 /* we might want to think about registering controller itself
1425 as a processor device on the bus so sg binds to it. */
1429 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1430 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1431 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1432 // cmd->target, cmd->lun);
1434 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1435 see what the device thinks of it. */
1437 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1438 cp = scsi_cmd_alloc(*c);
1439 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1440 if (cp == NULL) { /* trouble... */
1441 printk("scsi_cmd_alloc returned NULL!\n");
1442 /* FIXME: next 3 lines are -> BAD! <- */
1443 cmd->result = DID_NO_CONNECT << 16;
1448 // Fill in the command list header
1450 cmd->scsi_done = done; // save this for use by completion code
1452 // save cp in case we have to abort it
1453 cmd->host_scribble = (unsigned char *) cp;
1455 cp->cmd_type = CMD_SCSI;
1457 cp->Header.ReplyQueue = 0; // unused in simple mode
1458 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1459 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1461 // Fill in the request block...
1463 cp->Request.Timeout = 0;
1464 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
1465 BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB));
1466 cp->Request.CDBLen = cmd->cmd_len;
1467 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1468 cp->Request.Type.Type = TYPE_CMD;
1469 cp->Request.Type.Attribute = ATTR_SIMPLE;
1470 switch(cmd->sc_data_direction)
1472 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1473 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1474 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1475 case DMA_BIDIRECTIONAL:
1476 // This can happen if a buggy application does a scsi passthru
1477 // and sets both inlen and outlen to non-zero. ( see
1478 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1480 cp->Request.Type.Direction = XFER_RSVD;
1481 // This is technically wrong, and cciss controllers should
1482 // reject it with CMD_INVALID, which is the most correct
1483 // response, but non-fibre backends appear to let it
1484 // slide by, and give the same results as if this field
1485 // were set correctly. Either way is acceptable for
1486 // our purposes here.
1491 printk("cciss: unknown data direction: %d\n",
1492 cmd->sc_data_direction);
1497 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1499 /* Put the request on the tail of the request queue */
1501 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1502 addQ(&(*c)->reqQ, cp);
1505 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1507 /* the cmd'll come back via intr handler in complete_scsi_command() */
1512 cciss_unregister_scsi(int ctlr)
1514 struct cciss_scsi_adapter_data_t *sa;
1515 struct cciss_scsi_cmd_stack_t *stk;
1516 unsigned long flags;
1518 /* we are being forcibly unloaded, and may not refuse. */
1520 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1521 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1522 stk = &sa->cmd_stack;
1524 /* if we weren't ever actually registered, don't unregister */
1525 if (sa->registered) {
1526 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1527 scsi_remove_host(sa->scsi_host);
1528 scsi_host_put(sa->scsi_host);
1529 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1532 /* set scsi_host to NULL so our detect routine will
1533 find us on register */
1534 sa->scsi_host = NULL;
1535 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1536 scsi_cmd_stack_free(ctlr);
1541 cciss_engage_scsi(int ctlr)
1543 struct cciss_scsi_adapter_data_t *sa;
1544 struct cciss_scsi_cmd_stack_t *stk;
1545 unsigned long flags;
1547 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1548 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1549 stk = &sa->cmd_stack;
1551 if (sa->registered) {
1552 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1553 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1557 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1558 cciss_update_non_disk_devices(ctlr, -1);
1559 cciss_scsi_detect(ctlr);
1564 cciss_seq_tape_report(struct seq_file *seq, int ctlr)
1566 unsigned long flags;
1568 CPQ_TAPE_LOCK(ctlr, flags);
1570 "Sequential access devices: %d\n\n",
1571 ccissscsi[ctlr].ndevices);
1572 CPQ_TAPE_UNLOCK(ctlr, flags);
1575 static int wait_for_device_to_become_ready(ctlr_info_t *h,
1576 unsigned char lunaddr[])
1581 CommandList_struct *c;
1583 c = cmd_alloc(h, 1);
1585 printk(KERN_WARNING "cciss%d: out of memory in "
1586 "wait_for_device_to_become_ready.\n", h->ctlr);
1590 /* Send test unit ready until device ready, or give up. */
1591 while (count < 20) {
1593 /* Wait for a bit. do this first, because if we send
1594 * the TUR right away, the reset will just abort it.
1596 schedule_timeout_uninterruptible(waittime);
1599 /* Increase wait time with each try, up to a point. */
1600 if (waittime < (HZ * 30))
1601 waittime = waittime * 2;
1603 /* Send the Test Unit Ready */
1604 rc = fill_cmd(c, TEST_UNIT_READY, h->ctlr, NULL, 0, 0,
1607 rc = sendcmd_withirq_core(h, c, 0);
1609 (void) process_sendcmd_error(h, c);
1614 if (c->err_info->CommandStatus == CMD_SUCCESS)
1617 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
1618 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
1619 if (c->err_info->SenseInfo[2] == NO_SENSE)
1621 if (c->err_info->SenseInfo[2] == UNIT_ATTENTION) {
1623 asc = c->err_info->SenseInfo[12];
1624 check_for_unit_attention(h, c);
1625 if (asc == POWER_OR_RESET)
1630 printk(KERN_WARNING "cciss%d: Waiting %d secs "
1631 "for device to become ready.\n",
1632 h->ctlr, waittime / HZ);
1633 rc = 1; /* device not ready. */
1637 printk("cciss%d: giving up on device.\n", h->ctlr);
1639 printk(KERN_WARNING "cciss%d: device is ready.\n", h->ctlr);
1645 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
1646 * complaining. Doing a host- or bus-reset can't do anything good here.
1647 * Despite what it might say in scsi_error.c, there may well be commands
1648 * on the controller, as the cciss driver registers twice, once as a block
1649 * device for the logical drives, and once as a scsi device, for any tape
1650 * drives. So we know there are no commands out on the tape drives, but we
1651 * don't know there are no commands on the controller, and it is likely
1652 * that there probably are, as the cciss block device is most commonly used
1653 * as a boot device (embedded controller on HP/Compaq systems.)
1656 static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1659 CommandList_struct *cmd_in_trouble;
1660 unsigned char lunaddr[8];
1664 /* find the controller to which the command to be aborted was sent */
1665 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1666 if (c == NULL) /* paranoia */
1669 printk(KERN_WARNING "cciss%d: resetting tape drive or medium changer.\n", ctlr);
1670 /* find the command that's giving us trouble */
1671 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
1672 if (cmd_in_trouble == NULL) /* paranoia */
1674 memcpy(lunaddr, &cmd_in_trouble->Header.LUN.LunAddrBytes[0], 8);
1675 /* send a reset to the SCSI LUN which the command was sent to */
1676 rc = sendcmd_withirq(CCISS_RESET_MSG, ctlr, NULL, 0, 0, lunaddr,
1678 if (rc == 0 && wait_for_device_to_become_ready(*c, lunaddr) == 0)
1680 printk(KERN_WARNING "cciss%d: resetting device failed.\n", ctlr);
1684 static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1687 CommandList_struct *cmd_to_abort;
1688 unsigned char lunaddr[8];
1692 /* find the controller to which the command to be aborted was sent */
1693 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1694 if (c == NULL) /* paranoia */
1697 printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd\n", ctlr);
1699 /* find the command to be aborted */
1700 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
1701 if (cmd_to_abort == NULL) /* paranoia */
1703 memcpy(lunaddr, &cmd_to_abort->Header.LUN.LunAddrBytes[0], 8);
1704 rc = sendcmd_withirq(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag,
1705 0, 0, lunaddr, TYPE_MSG);
1712 #else /* no CONFIG_CISS_SCSI_TAPE */
1714 /* If no tape support, then these become defined out of existence */
1716 #define cciss_scsi_setup(cntl_num)
1718 #endif /* CONFIG_CISS_SCSI_TAPE */