usb-storage: automatically recognize bad residues
[linux-2.6/mini2440.git] / drivers / block / cciss_scsi.c
blobe1233aabda771493718b5f37f9228e00301098e3
1 /*
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
17 * 02111-1307, USA.
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... */
48 static int sendcmd(
49 __u8 cmd,
50 int ctlr,
51 void *buff,
52 size_t size,
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,
57 __u8 page_code,
58 unsigned char *scsi3addr,
59 int cmd_type);
62 static int cciss_scsi_proc_info(
63 struct Scsi_Host *sh,
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,
88 .name = "cciss",
89 .proc_name = "cciss",
90 .proc_info = cciss_scsi_proc_info,
91 .queuecommand = cciss_scsi_queue_command,
92 .can_queue = SCSI_CCISS_CAN_QUEUE,
93 .this_id = 7,
94 .sg_tablesize = MAXSGENTRIES,
95 .cmd_per_lun = 1,
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,
102 #pragma pack(1)
103 struct cciss_scsi_cmd_stack_elem_t {
104 CommandList_struct cmd;
105 ErrorInfo_struct Err;
106 __u32 busaddr;
107 __u32 pad;
110 #pragma pack()
112 #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
113 CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
114 // plus two for init time usage
116 #pragma pack(1)
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;
121 int top;
123 #pragma pack()
125 struct cciss_scsi_adapter_data_t {
126 struct Scsi_Host *scsi_host;
127 struct cciss_scsi_cmd_stack_t cmd_stack;
128 int registered;
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;
151 u64bit temp64;
153 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
154 stk = &sa->cmd_stack;
156 if (stk->top < 0)
157 return NULL;
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)); */
171 stk->top--;
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;
182 static void
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");
196 BUG();
198 stk->top++;
199 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
202 static int
203 scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
205 int i;
206 struct cciss_scsi_cmd_stack_t *stk;
207 size_t size;
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
213 // be used
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");
220 return -1;
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;
229 return 0;
232 static void
233 scsi_cmd_stack_free(int ctlr)
235 struct cciss_scsi_adapter_data_t *sa;
236 struct cciss_scsi_cmd_stack_t *stk;
237 size_t size;
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);
244 // BUG();
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);
250 stk->pool = NULL;
253 #if 0
254 static int xmargin=8;
255 static int amargin=60;
257 static void
258 print_bytes (unsigned char *c, int len, int hex, int ascii)
261 int i;
262 unsigned char *x;
264 if (hex)
266 x = c;
267 for (i=0;i<len;i++)
269 if ((i % xmargin) == 0 && i>0) printk("\n");
270 if ((i % xmargin) == 0) printk("0x%04x:", i);
271 printk(" %02x", *x);
272 x++;
274 printk("\n");
276 if (ascii)
278 x = c;
279 for (i=0;i<len;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);
284 else printk(".");
285 x++;
287 printk("\n");
291 static void
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,
325 cp->ErrDesc.Len);
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);
337 #endif
339 static int
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 */
344 int i, found=0;
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;
356 break;
359 return (!found);
361 struct scsi2map {
362 char scsi3addr[8];
363 int bus, target, lun;
366 static int
367 cciss_scsi_add_entry(int ctlr, int hostno,
368 unsigned char *scsi3addr, int devtype,
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);
380 return -1;
383 bus = target = -1;
384 lun = 0;
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 (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, scsi3addr, 8);
393 addr1[4] = 0;
394 for (i = 0; i < n; i++) {
395 sd = &ccissscsi[ctlr].dev[i];
396 memcpy(addr2, sd->scsi3addr, 8);
397 addr2[4] = 0;
398 /* differ only in byte 4? */
399 if (memcmp(addr1, addr2, 8) == 0) {
400 bus = sd->bus;
401 target = sd->target;
402 lun = scsi3addr[4];
403 break;
408 sd = &ccissscsi[ctlr].dev[n];
409 if (lun == 0) {
410 if (find_bus_target_lun(ctlr,
411 &sd->bus, &sd->target, &sd->lun) != 0)
412 return -1;
413 } else {
414 sd->bus = bus;
415 sd->target = target;
416 sd->lun = lun;
418 added[*nadded].bus = sd->bus;
419 added[*nadded].target = sd->target;
420 added[*nadded].lun = sd->lun;
421 (*nadded)++;
423 memcpy(&sd->scsi3addr[0], scsi3addr, 8);
424 sd->devtype = devtype;
425 ccissscsi[ctlr].ndevices++;
427 /* initially, (before registering with scsi layer) we don't
428 know our hostno and we don't want to print anything first
429 time anyway (the scsi layer's inquiries will show that info) */
430 if (hostno != -1)
431 printk("cciss%d: %s device c%db%dt%dl%d added.\n",
432 ctlr, scsi_device_type(sd->devtype), hostno,
433 sd->bus, sd->target, sd->lun);
434 return 0;
437 static void
438 cciss_scsi_remove_entry(int ctlr, int hostno, int entry,
439 struct scsi2map *removed, int *nremoved)
441 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
442 int i;
443 struct cciss_scsi_dev_t sd;
445 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
446 sd = ccissscsi[ctlr].dev[entry];
447 removed[*nremoved].bus = sd.bus;
448 removed[*nremoved].target = sd.target;
449 removed[*nremoved].lun = sd.lun;
450 (*nremoved)++;
451 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
452 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
453 ccissscsi[ctlr].ndevices--;
454 printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
455 ctlr, scsi_device_type(sd.devtype), hostno,
456 sd.bus, sd.target, sd.lun);
460 #define SCSI3ADDR_EQ(a,b) ( \
461 (a)[7] == (b)[7] && \
462 (a)[6] == (b)[6] && \
463 (a)[5] == (b)[5] && \
464 (a)[4] == (b)[4] && \
465 (a)[3] == (b)[3] && \
466 (a)[2] == (b)[2] && \
467 (a)[1] == (b)[1] && \
468 (a)[0] == (b)[0])
470 static void fixup_botched_add(int ctlr, char *scsi3addr)
472 /* called when scsi_add_device fails in order to re-adjust */
473 /* ccissscsi[] to match the mid layer's view. */
474 unsigned long flags;
475 int i, j;
476 CPQ_TAPE_LOCK(ctlr, flags);
477 for (i = 0; i < ccissscsi[ctlr].ndevices; i++) {
478 if (memcmp(scsi3addr,
479 ccissscsi[ctlr].dev[i].scsi3addr, 8) == 0) {
480 for (j = i; j < ccissscsi[ctlr].ndevices-1; j++)
481 ccissscsi[ctlr].dev[j] =
482 ccissscsi[ctlr].dev[j+1];
483 ccissscsi[ctlr].ndevices--;
484 break;
487 CPQ_TAPE_UNLOCK(ctlr, flags);
490 static int
491 adjust_cciss_scsi_table(int ctlr, int hostno,
492 struct cciss_scsi_dev_t sd[], int nsds)
494 /* sd contains scsi3 addresses and devtypes, but
495 bus target and lun are not filled in. This funciton
496 takes what's in sd to be the current and adjusts
497 ccissscsi[] to be in line with what's in sd. */
499 int i,j, found, changes=0;
500 struct cciss_scsi_dev_t *csd;
501 unsigned long flags;
502 struct scsi2map *added, *removed;
503 int nadded, nremoved;
504 struct Scsi_Host *sh = NULL;
506 added = kzalloc(sizeof(*added) * CCISS_MAX_SCSI_DEVS_PER_HBA,
507 GFP_KERNEL);
508 removed = kzalloc(sizeof(*removed) * CCISS_MAX_SCSI_DEVS_PER_HBA,
509 GFP_KERNEL);
511 if (!added || !removed) {
512 printk(KERN_WARNING "cciss%d: Out of memory in "
513 "adjust_cciss_scsi_table\n", ctlr);
514 goto free_and_out;
517 CPQ_TAPE_LOCK(ctlr, flags);
519 if (hostno != -1) /* if it's not the first time... */
520 sh = ((struct cciss_scsi_adapter_data_t *)
521 hba[ctlr]->scsi_ctlr)->scsi_host;
523 /* find any devices in ccissscsi[] that are not in
524 sd[] and remove them from ccissscsi[] */
526 i = 0;
527 nremoved = 0;
528 nadded = 0;
529 while(i<ccissscsi[ctlr].ndevices) {
530 csd = &ccissscsi[ctlr].dev[i];
531 found=0;
532 for (j=0;j<nsds;j++) {
533 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
534 csd->scsi3addr)) {
535 if (sd[j].devtype == csd->devtype)
536 found=2;
537 else
538 found=1;
539 break;
543 if (found == 0) { /* device no longer present. */
544 changes++;
545 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
546 ctlr, scsi_device_type(csd->devtype), hostno,
547 csd->bus, csd->target, csd->lun); */
548 cciss_scsi_remove_entry(ctlr, hostno, i,
549 removed, &nremoved);
550 /* remove ^^^, hence i not incremented */
552 else if (found == 1) { /* device is different kind */
553 changes++;
554 printk("cciss%d: device c%db%dt%dl%d type changed "
555 "(device type now %s).\n",
556 ctlr, hostno, csd->bus, csd->target, csd->lun,
557 scsi_device_type(csd->devtype));
558 cciss_scsi_remove_entry(ctlr, hostno, i,
559 removed, &nremoved);
560 /* remove ^^^, hence i not incremented */
561 if (cciss_scsi_add_entry(ctlr, hostno,
562 &sd[j].scsi3addr[0], sd[j].devtype,
563 added, &nadded) != 0)
564 /* we just removed one, so add can't fail. */
565 BUG();
566 csd->devtype = sd[j].devtype;
567 } else /* device is same as it ever was, */
568 i++; /* so just move along. */
571 /* Now, make sure every device listed in sd[] is also
572 listed in ccissscsi[], adding them if they aren't found */
574 for (i=0;i<nsds;i++) {
575 found=0;
576 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
577 csd = &ccissscsi[ctlr].dev[j];
578 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
579 csd->scsi3addr)) {
580 if (sd[i].devtype == csd->devtype)
581 found=2; /* found device */
582 else
583 found=1; /* found a bug. */
584 break;
587 if (!found) {
588 changes++;
589 if (cciss_scsi_add_entry(ctlr, hostno,
591 &sd[i].scsi3addr[0], sd[i].devtype,
592 added, &nadded) != 0)
593 break;
594 } else if (found == 1) {
595 /* should never happen... */
596 changes++;
597 printk("cciss%d: device unexpectedly changed type\n",
598 ctlr);
599 /* but if it does happen, we just ignore that device */
602 CPQ_TAPE_UNLOCK(ctlr, flags);
604 /* Don't notify scsi mid layer of any changes the first time through */
605 /* (or if there are no changes) scsi_scan_host will do it later the */
606 /* first time through. */
607 if (hostno == -1 || !changes)
608 goto free_and_out;
610 /* Notify scsi mid layer of any removed devices */
611 for (i = 0; i < nremoved; i++) {
612 struct scsi_device *sdev =
613 scsi_device_lookup(sh, removed[i].bus,
614 removed[i].target, removed[i].lun);
615 if (sdev != NULL) {
616 scsi_remove_device(sdev);
617 scsi_device_put(sdev);
618 } else {
619 /* We don't expect to get here. */
620 /* future cmds to this device will get selection */
621 /* timeout as if the device was gone. */
622 printk(KERN_WARNING "cciss%d: didn't find "
623 "c%db%dt%dl%d\n for removal.",
624 ctlr, hostno, removed[i].bus,
625 removed[i].target, removed[i].lun);
629 /* Notify scsi mid layer of any added devices */
630 for (i = 0; i < nadded; i++) {
631 int rc;
632 rc = scsi_add_device(sh, added[i].bus,
633 added[i].target, added[i].lun);
634 if (rc == 0)
635 continue;
636 printk(KERN_WARNING "cciss%d: scsi_add_device "
637 "c%db%dt%dl%d failed, device not added.\n",
638 ctlr, hostno,
639 added[i].bus, added[i].target, added[i].lun);
640 /* now we have to remove it from ccissscsi, */
641 /* since it didn't get added to scsi mid layer */
642 fixup_botched_add(ctlr, added[i].scsi3addr);
645 free_and_out:
646 kfree(added);
647 kfree(removed);
648 return 0;
651 static int
652 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
654 int i;
655 struct cciss_scsi_dev_t *sd;
656 unsigned long flags;
658 CPQ_TAPE_LOCK(ctlr, flags);
659 for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
660 sd = &ccissscsi[ctlr].dev[i];
661 if (sd->bus == bus &&
662 sd->target == target &&
663 sd->lun == lun) {
664 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
665 CPQ_TAPE_UNLOCK(ctlr, flags);
666 return 0;
669 CPQ_TAPE_UNLOCK(ctlr, flags);
670 return -1;
673 static void
674 cciss_scsi_setup(int cntl_num)
676 struct cciss_scsi_adapter_data_t * shba;
678 ccissscsi[cntl_num].ndevices = 0;
679 shba = (struct cciss_scsi_adapter_data_t *)
680 kmalloc(sizeof(*shba), GFP_KERNEL);
681 if (shba == NULL)
682 return;
683 shba->scsi_host = NULL;
684 spin_lock_init(&shba->lock);
685 shba->registered = 0;
686 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
687 kfree(shba);
688 shba = NULL;
690 hba[cntl_num]->scsi_ctlr = (void *) shba;
691 return;
694 static void
695 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
697 struct scsi_cmnd *cmd;
698 ctlr_info_t *ctlr;
699 ErrorInfo_struct *ei;
701 ei = cp->err_info;
703 /* First, see if it was a message rather than a command */
704 if (cp->Request.Type.Type == TYPE_MSG) {
705 cp->cmd_type = CMD_MSG_DONE;
706 return;
709 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
710 ctlr = hba[cp->ctlr];
712 scsi_dma_unmap(cmd);
714 cmd->result = (DID_OK << 16); /* host byte */
715 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
716 /* cmd->result |= (GOOD < 1); */ /* status byte */
718 cmd->result |= (ei->ScsiStatus);
719 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
721 /* copy the sense data whether we need to or not. */
723 memcpy(cmd->sense_buffer, ei->SenseInfo,
724 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
725 SCSI_SENSE_BUFFERSIZE :
726 ei->SenseLen);
727 scsi_set_resid(cmd, ei->ResidualCnt);
729 if(ei->CommandStatus != 0)
730 { /* an error has occurred */
731 switch(ei->CommandStatus)
733 case CMD_TARGET_STATUS:
734 /* Pass it up to the upper layers... */
735 if( ei->ScsiStatus)
737 #if 0
738 printk(KERN_WARNING "cciss: cmd %p "
739 "has SCSI Status = %x\n",
740 cp,
741 ei->ScsiStatus);
742 #endif
743 cmd->result |= (ei->ScsiStatus < 1);
745 else { /* scsi status is zero??? How??? */
747 /* Ordinarily, this case should never happen, but there is a bug
748 in some released firmware revisions that allows it to happen
749 if, for example, a 4100 backplane loses power and the tape
750 drive is in it. We assume that it's a fatal error of some
751 kind because we can't show that it wasn't. We will make it
752 look like selection timeout since that is the most common
753 reason for this to occur, and it's severe enough. */
755 cmd->result = DID_NO_CONNECT << 16;
757 break;
758 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
759 break;
760 case CMD_DATA_OVERRUN:
761 printk(KERN_WARNING "cciss: cp %p has"
762 " completed with data overrun "
763 "reported\n", cp);
764 break;
765 case CMD_INVALID: {
766 /* print_bytes(cp, sizeof(*cp), 1, 0);
767 print_cmd(cp); */
768 /* We get CMD_INVALID if you address a non-existent tape drive instead
769 of a selection timeout (no response). You will see this if you yank
770 out a tape drive, then try to access it. This is kind of a shame
771 because it means that any other CMD_INVALID (e.g. driver bug) will
772 get interpreted as a missing target. */
773 cmd->result = DID_NO_CONNECT << 16;
775 break;
776 case CMD_PROTOCOL_ERR:
777 printk(KERN_WARNING "cciss: cp %p has "
778 "protocol error \n", cp);
779 break;
780 case CMD_HARDWARE_ERR:
781 cmd->result = DID_ERROR << 16;
782 printk(KERN_WARNING "cciss: cp %p had "
783 " hardware error\n", cp);
784 break;
785 case CMD_CONNECTION_LOST:
786 cmd->result = DID_ERROR << 16;
787 printk(KERN_WARNING "cciss: cp %p had "
788 "connection lost\n", cp);
789 break;
790 case CMD_ABORTED:
791 cmd->result = DID_ABORT << 16;
792 printk(KERN_WARNING "cciss: cp %p was "
793 "aborted\n", cp);
794 break;
795 case CMD_ABORT_FAILED:
796 cmd->result = DID_ERROR << 16;
797 printk(KERN_WARNING "cciss: cp %p reports "
798 "abort failed\n", cp);
799 break;
800 case CMD_UNSOLICITED_ABORT:
801 cmd->result = DID_ABORT << 16;
802 printk(KERN_WARNING "cciss: cp %p aborted "
803 "do to an unsolicited abort\n", cp);
804 break;
805 case CMD_TIMEOUT:
806 cmd->result = DID_TIME_OUT << 16;
807 printk(KERN_WARNING "cciss: cp %p timedout\n",
808 cp);
809 break;
810 default:
811 cmd->result = DID_ERROR << 16;
812 printk(KERN_WARNING "cciss: cp %p returned "
813 "unknown status %x\n", cp,
814 ei->CommandStatus);
817 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
818 // cmd->target, cmd->lun);
819 cmd->scsi_done(cmd);
820 scsi_cmd_free(ctlr, cp);
823 static int
824 cciss_scsi_detect(int ctlr)
826 struct Scsi_Host *sh;
827 int error;
829 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
830 if (sh == NULL)
831 goto fail;
832 sh->io_port = 0; // good enough? FIXME,
833 sh->n_io_port = 0; // I don't think we use these two...
834 sh->this_id = SELF_SCSI_ID;
836 ((struct cciss_scsi_adapter_data_t *)
837 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
838 sh->hostdata[0] = (unsigned long) hba[ctlr];
839 sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT];
840 sh->unique_id = sh->irq;
841 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
842 if (error)
843 goto fail_host_put;
844 scsi_scan_host(sh);
845 return 1;
847 fail_host_put:
848 scsi_host_put(sh);
849 fail:
850 return 0;
853 static void
854 cciss_unmap_one(struct pci_dev *pdev,
855 CommandList_struct *cp,
856 size_t buflen,
857 int data_direction)
859 u64bit addr64;
861 addr64.val32.lower = cp->SG[0].Addr.lower;
862 addr64.val32.upper = cp->SG[0].Addr.upper;
863 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
866 static void
867 cciss_map_one(struct pci_dev *pdev,
868 CommandList_struct *cp,
869 unsigned char *buf,
870 size_t buflen,
871 int data_direction)
873 __u64 addr64;
875 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
876 cp->SG[0].Addr.lower =
877 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
878 cp->SG[0].Addr.upper =
879 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
880 cp->SG[0].Len = buflen;
881 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
882 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
885 static int
886 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
887 CommandList_struct *cp,
888 unsigned char *scsi3addr,
889 unsigned char *cdb,
890 unsigned char cdblen,
891 unsigned char *buf, int bufsize,
892 int direction)
894 unsigned long flags;
895 DECLARE_COMPLETION_ONSTACK(wait);
897 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
898 cp->scsi_cmd = NULL;
899 cp->Header.ReplyQueue = 0; // unused in simple mode
900 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
901 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
902 // Fill in the request block...
904 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
905 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
906 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
908 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
909 memcpy(cp->Request.CDB, cdb, cdblen);
910 cp->Request.Timeout = 0;
911 cp->Request.CDBLen = cdblen;
912 cp->Request.Type.Type = TYPE_CMD;
913 cp->Request.Type.Attribute = ATTR_SIMPLE;
914 cp->Request.Type.Direction = direction;
916 /* Fill in the SG list and do dma mapping */
917 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
918 bufsize, DMA_FROM_DEVICE);
920 cp->waiting = &wait;
922 /* Put the request on the tail of the request queue */
923 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
924 addQ(&c->reqQ, cp);
925 c->Qdepth++;
926 start_io(c);
927 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
929 wait_for_completion(&wait);
931 /* undo the dma mapping */
932 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
933 return(0);
936 static void
937 cciss_scsi_interpret_error(CommandList_struct *cp)
939 ErrorInfo_struct *ei;
941 ei = cp->err_info;
942 switch(ei->CommandStatus)
944 case CMD_TARGET_STATUS:
945 printk(KERN_WARNING "cciss: cmd %p has "
946 "completed with errors\n", cp);
947 printk(KERN_WARNING "cciss: cmd %p "
948 "has SCSI Status = %x\n",
949 cp,
950 ei->ScsiStatus);
951 if (ei->ScsiStatus == 0)
952 printk(KERN_WARNING
953 "cciss:SCSI status is abnormally zero. "
954 "(probably indicates selection timeout "
955 "reported incorrectly due to a known "
956 "firmware bug, circa July, 2001.)\n");
957 break;
958 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
959 printk("UNDERRUN\n");
960 break;
961 case CMD_DATA_OVERRUN:
962 printk(KERN_WARNING "cciss: cp %p has"
963 " completed with data overrun "
964 "reported\n", cp);
965 break;
966 case CMD_INVALID: {
967 /* controller unfortunately reports SCSI passthru's */
968 /* to non-existent targets as invalid commands. */
969 printk(KERN_WARNING "cciss: cp %p is "
970 "reported invalid (probably means "
971 "target device no longer present)\n",
972 cp);
973 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
974 print_cmd(cp); */
976 break;
977 case CMD_PROTOCOL_ERR:
978 printk(KERN_WARNING "cciss: cp %p has "
979 "protocol error \n", cp);
980 break;
981 case CMD_HARDWARE_ERR:
982 /* cmd->result = DID_ERROR << 16; */
983 printk(KERN_WARNING "cciss: cp %p had "
984 " hardware error\n", cp);
985 break;
986 case CMD_CONNECTION_LOST:
987 printk(KERN_WARNING "cciss: cp %p had "
988 "connection lost\n", cp);
989 break;
990 case CMD_ABORTED:
991 printk(KERN_WARNING "cciss: cp %p was "
992 "aborted\n", cp);
993 break;
994 case CMD_ABORT_FAILED:
995 printk(KERN_WARNING "cciss: cp %p reports "
996 "abort failed\n", cp);
997 break;
998 case CMD_UNSOLICITED_ABORT:
999 printk(KERN_WARNING "cciss: cp %p aborted "
1000 "do to an unsolicited abort\n", cp);
1001 break;
1002 case CMD_TIMEOUT:
1003 printk(KERN_WARNING "cciss: cp %p timedout\n",
1004 cp);
1005 break;
1006 default:
1007 printk(KERN_WARNING "cciss: cp %p returned "
1008 "unknown status %x\n", cp,
1009 ei->CommandStatus);
1013 static int
1014 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
1015 unsigned char *buf, unsigned char bufsize)
1017 int rc;
1018 CommandList_struct *cp;
1019 char cdb[6];
1020 ErrorInfo_struct *ei;
1021 unsigned long flags;
1023 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1024 cp = scsi_cmd_alloc(c);
1025 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1027 if (cp == NULL) { /* trouble... */
1028 printk("cmd_alloc returned NULL!\n");
1029 return -1;
1032 ei = cp->err_info;
1034 cdb[0] = CISS_INQUIRY;
1035 cdb[1] = 0;
1036 cdb[2] = 0;
1037 cdb[3] = 0;
1038 cdb[4] = bufsize;
1039 cdb[5] = 0;
1040 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
1041 6, buf, bufsize, XFER_READ);
1043 if (rc != 0) return rc; /* something went wrong */
1045 if (ei->CommandStatus != 0 &&
1046 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1047 cciss_scsi_interpret_error(cp);
1048 rc = -1;
1050 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1051 scsi_cmd_free(c, cp);
1052 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1053 return rc;
1056 static int
1057 cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
1058 ReportLunData_struct *buf, int bufsize)
1060 int rc;
1061 CommandList_struct *cp;
1062 unsigned char cdb[12];
1063 unsigned char scsi3addr[8];
1064 ErrorInfo_struct *ei;
1065 unsigned long flags;
1067 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1068 cp = scsi_cmd_alloc(c);
1069 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1070 if (cp == NULL) { /* trouble... */
1071 printk("cmd_alloc returned NULL!\n");
1072 return -1;
1075 memset(&scsi3addr[0], 0, 8); /* address the controller */
1076 cdb[0] = CISS_REPORT_PHYS;
1077 cdb[1] = 0;
1078 cdb[2] = 0;
1079 cdb[3] = 0;
1080 cdb[4] = 0;
1081 cdb[5] = 0;
1082 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
1083 cdb[7] = (bufsize >> 16) & 0xFF;
1084 cdb[8] = (bufsize >> 8) & 0xFF;
1085 cdb[9] = bufsize & 0xFF;
1086 cdb[10] = 0;
1087 cdb[11] = 0;
1089 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
1090 cdb, 12,
1091 (unsigned char *) buf,
1092 bufsize, XFER_READ);
1094 if (rc != 0) return rc; /* something went wrong */
1096 ei = cp->err_info;
1097 if (ei->CommandStatus != 0 &&
1098 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1099 cciss_scsi_interpret_error(cp);
1100 rc = -1;
1102 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
1103 scsi_cmd_free(c, cp);
1104 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
1105 return rc;
1108 static void
1109 cciss_update_non_disk_devices(int cntl_num, int hostno)
1111 /* the idea here is we could get notified from /proc
1112 that some devices have changed, so we do a report
1113 physical luns cmd, and adjust our list of devices
1114 accordingly. (We can't rely on the scsi-mid layer just
1115 doing inquiries, because the "busses" that the scsi
1116 mid-layer probes are totally fabricated by this driver,
1117 so new devices wouldn't show up.
1119 the scsi3addr's of devices won't change so long as the
1120 adapter is not reset. That means we can rescan and
1121 tell which devices we already know about, vs. new
1122 devices, vs. disappearing devices.
1124 Also, if you yank out a tape drive, then put in a disk
1125 in it's place, (say, a configured volume from another
1126 array controller for instance) _don't_ poke this driver
1127 (so it thinks it's still a tape, but _do_ poke the scsi
1128 mid layer, so it does an inquiry... the scsi mid layer
1129 will see the physical disk. This would be bad. Need to
1130 think about how to prevent that. One idea would be to
1131 snoop all scsi responses and if an inquiry repsonse comes
1132 back that reports a disk, chuck it an return selection
1133 timeout instead and adjust our table... Not sure i like
1134 that though.
1137 #define OBDR_TAPE_INQ_SIZE 49
1138 #define OBDR_TAPE_SIG "$DR-10"
1139 ReportLunData_struct *ld_buff;
1140 unsigned char *inq_buff;
1141 unsigned char scsi3addr[8];
1142 ctlr_info_t *c;
1143 __u32 num_luns=0;
1144 unsigned char *ch;
1145 /* unsigned char found[CCISS_MAX_SCSI_DEVS_PER_HBA]; */
1146 struct cciss_scsi_dev_t currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1147 int ncurrent=0;
1148 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1149 int i;
1151 c = (ctlr_info_t *) hba[cntl_num];
1152 ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
1153 if (ld_buff == NULL) {
1154 printk(KERN_ERR "cciss: out of memory\n");
1155 return;
1157 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1158 if (inq_buff == NULL) {
1159 printk(KERN_ERR "cciss: out of memory\n");
1160 kfree(ld_buff);
1161 return;
1164 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1165 ch = &ld_buff->LUNListLength[0];
1166 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1167 if (num_luns > CISS_MAX_PHYS_LUN) {
1168 printk(KERN_WARNING
1169 "cciss: Maximum physical LUNs (%d) exceeded. "
1170 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1171 num_luns - CISS_MAX_PHYS_LUN);
1172 num_luns = CISS_MAX_PHYS_LUN;
1175 else {
1176 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1177 goto out;
1181 /* adjust our table of devices */
1182 for(i=0; i<num_luns; i++)
1184 int devtype;
1186 /* for each physical lun, do an inquiry */
1187 if (ld_buff->LUN[i][3] & 0xC0) continue;
1188 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1189 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1191 if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, inq_buff,
1192 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1193 /* Inquiry failed (msg printed already) */
1194 devtype = 0; /* so we will skip this device. */
1195 } else /* what kind of device is this? */
1196 devtype = (inq_buff[0] & 0x1f);
1198 switch (devtype)
1200 case 0x05: /* CD-ROM */ {
1202 /* We don't *really* support actual CD-ROM devices,
1203 * just this "One Button Disaster Recovery" tape drive
1204 * which temporarily pretends to be a CD-ROM drive.
1205 * So we check that the device is really an OBDR tape
1206 * device by checking for "$DR-10" in bytes 43-48 of
1207 * the inquiry data.
1209 char obdr_sig[7];
1211 strncpy(obdr_sig, &inq_buff[43], 6);
1212 obdr_sig[6] = '\0';
1213 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1214 /* Not OBDR device, ignore it. */
1215 break;
1217 /* fall through . . . */
1218 case 0x01: /* sequential access, (tape) */
1219 case 0x08: /* medium changer */
1220 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1221 printk(KERN_INFO "cciss%d: %s ignored, "
1222 "too many devices.\n", cntl_num,
1223 scsi_device_type(devtype));
1224 break;
1226 memcpy(&currentsd[ncurrent].scsi3addr[0],
1227 &scsi3addr[0], 8);
1228 currentsd[ncurrent].devtype = devtype;
1229 currentsd[ncurrent].bus = -1;
1230 currentsd[ncurrent].target = -1;
1231 currentsd[ncurrent].lun = -1;
1232 ncurrent++;
1233 break;
1234 default:
1235 break;
1239 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1240 out:
1241 kfree(inq_buff);
1242 kfree(ld_buff);
1243 return;
1246 static int
1247 is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1249 int verb_len = strlen(verb);
1250 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1251 return verb_len;
1252 else
1253 return 0;
1256 static int
1257 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1259 int arg_len;
1261 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1262 cciss_update_non_disk_devices(ctlr, hostno);
1263 else
1264 return -EINVAL;
1265 return length;
1269 static int
1270 cciss_scsi_proc_info(struct Scsi_Host *sh,
1271 char *buffer, /* data buffer */
1272 char **start, /* where data in buffer starts */
1273 off_t offset, /* offset from start of imaginary file */
1274 int length, /* length of data in buffer */
1275 int func) /* 0 == read, 1 == write */
1278 int buflen, datalen;
1279 ctlr_info_t *ci;
1280 int i;
1281 int cntl_num;
1284 ci = (ctlr_info_t *) sh->hostdata[0];
1285 if (ci == NULL) /* This really shouldn't ever happen. */
1286 return -EINVAL;
1288 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1290 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1291 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n",
1292 cntl_num, sh->host_no);
1294 /* this information is needed by apps to know which cciss
1295 device corresponds to which scsi host number without
1296 having to open a scsi target device node. The device
1297 information is not a duplicate of /proc/scsi/scsi because
1298 the two may be out of sync due to scsi hotplug, rather
1299 this info is for an app to be able to use to know how to
1300 get them back in sync. */
1302 for (i=0;i<ccissscsi[cntl_num].ndevices;i++) {
1303 struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i];
1304 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
1305 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1306 sh->host_no, sd->bus, sd->target, sd->lun,
1307 sd->devtype,
1308 sd->scsi3addr[0], sd->scsi3addr[1],
1309 sd->scsi3addr[2], sd->scsi3addr[3],
1310 sd->scsi3addr[4], sd->scsi3addr[5],
1311 sd->scsi3addr[6], sd->scsi3addr[7]);
1313 datalen = buflen - offset;
1314 if (datalen < 0) { /* they're reading past EOF. */
1315 datalen = 0;
1316 *start = buffer+buflen;
1317 } else
1318 *start = buffer + offset;
1319 return(datalen);
1320 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1321 return cciss_scsi_user_command(cntl_num, sh->host_no,
1322 buffer, length);
1325 /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1326 dma mapping and fills in the scatter gather entries of the
1327 cciss command, cp. */
1329 static void
1330 cciss_scatter_gather(struct pci_dev *pdev,
1331 CommandList_struct *cp,
1332 struct scsi_cmnd *cmd)
1334 unsigned int len;
1335 struct scatterlist *sg;
1336 __u64 addr64;
1337 int use_sg, i;
1339 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1341 use_sg = scsi_dma_map(cmd);
1342 if (use_sg) { /* not too many addrs? */
1343 scsi_for_each_sg(cmd, sg, use_sg, i) {
1344 addr64 = (__u64) sg_dma_address(sg);
1345 len = sg_dma_len(sg);
1346 cp->SG[i].Addr.lower =
1347 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1348 cp->SG[i].Addr.upper =
1349 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1350 cp->SG[i].Len = len;
1351 cp->SG[i].Ext = 0; // we are not chaining
1355 cp->Header.SGList = (__u8) use_sg; /* no. SGs contig in this cmd */
1356 cp->Header.SGTotal = (__u16) use_sg; /* total sgs in this cmd list */
1357 return;
1361 static int
1362 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1364 ctlr_info_t **c;
1365 int ctlr, rc;
1366 unsigned char scsi3addr[8];
1367 CommandList_struct *cp;
1368 unsigned long flags;
1370 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1371 // We violate cmd->host privacy here. (Is there another way?)
1372 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1373 ctlr = (*c)->ctlr;
1375 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1376 cmd->device->lun, scsi3addr);
1377 if (rc != 0) {
1378 /* the scsi nexus does not match any that we presented... */
1379 /* pretend to mid layer that we got selection timeout */
1380 cmd->result = DID_NO_CONNECT << 16;
1381 done(cmd);
1382 /* we might want to think about registering controller itself
1383 as a processor device on the bus so sg binds to it. */
1384 return 0;
1387 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1388 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1389 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1390 // cmd->target, cmd->lun);
1392 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1393 see what the device thinks of it. */
1395 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1396 cp = scsi_cmd_alloc(*c);
1397 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1398 if (cp == NULL) { /* trouble... */
1399 printk("scsi_cmd_alloc returned NULL!\n");
1400 /* FIXME: next 3 lines are -> BAD! <- */
1401 cmd->result = DID_NO_CONNECT << 16;
1402 done(cmd);
1403 return 0;
1406 // Fill in the command list header
1408 cmd->scsi_done = done; // save this for use by completion code
1410 // save cp in case we have to abort it
1411 cmd->host_scribble = (unsigned char *) cp;
1413 cp->cmd_type = CMD_SCSI;
1414 cp->scsi_cmd = cmd;
1415 cp->Header.ReplyQueue = 0; // unused in simple mode
1416 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1417 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1419 // Fill in the request block...
1421 cp->Request.Timeout = 0;
1422 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
1423 BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB));
1424 cp->Request.CDBLen = cmd->cmd_len;
1425 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1426 cp->Request.Type.Type = TYPE_CMD;
1427 cp->Request.Type.Attribute = ATTR_SIMPLE;
1428 switch(cmd->sc_data_direction)
1430 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1431 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1432 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1433 case DMA_BIDIRECTIONAL:
1434 // This can happen if a buggy application does a scsi passthru
1435 // and sets both inlen and outlen to non-zero. ( see
1436 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1438 cp->Request.Type.Direction = XFER_RSVD;
1439 // This is technically wrong, and cciss controllers should
1440 // reject it with CMD_INVALID, which is the most correct
1441 // response, but non-fibre backends appear to let it
1442 // slide by, and give the same results as if this field
1443 // were set correctly. Either way is acceptable for
1444 // our purposes here.
1446 break;
1448 default:
1449 printk("cciss: unknown data direction: %d\n",
1450 cmd->sc_data_direction);
1451 BUG();
1452 break;
1455 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1457 /* Put the request on the tail of the request queue */
1459 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1460 addQ(&(*c)->reqQ, cp);
1461 (*c)->Qdepth++;
1462 start_io(*c);
1463 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1465 /* the cmd'll come back via intr handler in complete_scsi_command() */
1466 return 0;
1469 static void
1470 cciss_unregister_scsi(int ctlr)
1472 struct cciss_scsi_adapter_data_t *sa;
1473 struct cciss_scsi_cmd_stack_t *stk;
1474 unsigned long flags;
1476 /* we are being forcibly unloaded, and may not refuse. */
1478 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1479 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1480 stk = &sa->cmd_stack;
1482 /* if we weren't ever actually registered, don't unregister */
1483 if (sa->registered) {
1484 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1485 scsi_remove_host(sa->scsi_host);
1486 scsi_host_put(sa->scsi_host);
1487 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1490 /* set scsi_host to NULL so our detect routine will
1491 find us on register */
1492 sa->scsi_host = NULL;
1493 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1494 scsi_cmd_stack_free(ctlr);
1495 kfree(sa);
1498 static int
1499 cciss_engage_scsi(int ctlr)
1501 struct cciss_scsi_adapter_data_t *sa;
1502 struct cciss_scsi_cmd_stack_t *stk;
1503 unsigned long flags;
1505 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1506 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1507 stk = &sa->cmd_stack;
1509 if (sa->registered) {
1510 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1511 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1512 return ENXIO;
1514 sa->registered = 1;
1515 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1516 cciss_update_non_disk_devices(ctlr, -1);
1517 cciss_scsi_detect(ctlr);
1518 return 0;
1521 static void
1522 cciss_seq_tape_report(struct seq_file *seq, int ctlr)
1524 unsigned long flags;
1526 CPQ_TAPE_LOCK(ctlr, flags);
1527 seq_printf(seq,
1528 "Sequential access devices: %d\n\n",
1529 ccissscsi[ctlr].ndevices);
1530 CPQ_TAPE_UNLOCK(ctlr, flags);
1534 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
1535 * complaining. Doing a host- or bus-reset can't do anything good here.
1536 * Despite what it might say in scsi_error.c, there may well be commands
1537 * on the controller, as the cciss driver registers twice, once as a block
1538 * device for the logical drives, and once as a scsi device, for any tape
1539 * drives. So we know there are no commands out on the tape drives, but we
1540 * don't know there are no commands on the controller, and it is likely
1541 * that there probably are, as the cciss block device is most commonly used
1542 * as a boot device (embedded controller on HP/Compaq systems.)
1545 static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1547 int rc;
1548 CommandList_struct *cmd_in_trouble;
1549 ctlr_info_t **c;
1550 int ctlr;
1552 /* find the controller to which the command to be aborted was sent */
1553 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1554 if (c == NULL) /* paranoia */
1555 return FAILED;
1556 ctlr = (*c)->ctlr;
1557 printk(KERN_WARNING "cciss%d: resetting tape drive or medium changer.\n", ctlr);
1559 /* find the command that's giving us trouble */
1560 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
1561 if (cmd_in_trouble == NULL) { /* paranoia */
1562 return FAILED;
1564 /* send a reset to the SCSI LUN which the command was sent to */
1565 rc = sendcmd(CCISS_RESET_MSG, ctlr, NULL, 0, 2, 0, 0,
1566 (unsigned char *) &cmd_in_trouble->Header.LUN.LunAddrBytes[0],
1567 TYPE_MSG);
1568 /* sendcmd turned off interrupts on the board, turn 'em back on. */
1569 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1570 if (rc == 0)
1571 return SUCCESS;
1572 printk(KERN_WARNING "cciss%d: resetting device failed.\n", ctlr);
1573 return FAILED;
1576 static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1578 int rc;
1579 CommandList_struct *cmd_to_abort;
1580 ctlr_info_t **c;
1581 int ctlr;
1583 /* find the controller to which the command to be aborted was sent */
1584 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1585 if (c == NULL) /* paranoia */
1586 return FAILED;
1587 ctlr = (*c)->ctlr;
1588 printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd\n", ctlr);
1590 /* find the command to be aborted */
1591 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
1592 if (cmd_to_abort == NULL) /* paranoia */
1593 return FAILED;
1594 rc = sendcmd(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag,
1595 0, 2, 0, 0,
1596 (unsigned char *) &cmd_to_abort->Header.LUN.LunAddrBytes[0],
1597 TYPE_MSG);
1598 /* sendcmd turned off interrupts on the board, turn 'em back on. */
1599 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1600 if (rc == 0)
1601 return SUCCESS;
1602 return FAILED;
1606 #else /* no CONFIG_CISS_SCSI_TAPE */
1608 /* If no tape support, then these become defined out of existence */
1610 #define cciss_scsi_setup(cntl_num)
1612 #endif /* CONFIG_CISS_SCSI_TAPE */