Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / block / cciss_scsi.c
blob597c007fe81b421614635eaf3cb8ae00347000fe
1 /*
2 * Disk Array driver for Compaq SA53xx Controllers, SCSI Tape module
3 * Copyright 2001 Compaq Computer Corporation
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; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_host.h>
43 #include "cciss_scsi.h"
45 #define CCISS_ABORT_MSG 0x00
46 #define CCISS_RESET_MSG 0x01
48 /* some prototypes... */
49 static int sendcmd(
50 __u8 cmd,
51 int ctlr,
52 void *buff,
53 size_t size,
54 unsigned int use_unit_num, /* 0: address the controller,
55 1: address logical volume log_unit,
56 2: address is in scsi3addr */
57 unsigned int log_unit,
58 __u8 page_code,
59 unsigned char *scsi3addr,
60 int cmd_type);
63 static int cciss_scsi_proc_info(
64 struct Scsi_Host *sh,
65 char *buffer, /* data buffer */
66 char **start, /* where data in buffer starts */
67 off_t offset, /* offset from start of imaginary file */
68 int length, /* length of data in buffer */
69 int func); /* 0 == read, 1 == write */
71 static int cciss_scsi_queue_command (struct scsi_cmnd *cmd,
72 void (* done)(struct scsi_cmnd *));
73 static int cciss_eh_device_reset_handler(struct scsi_cmnd *);
74 static int cciss_eh_abort_handler(struct scsi_cmnd *);
76 static struct cciss_scsi_hba_t ccissscsi[MAX_CTLR] = {
77 { .name = "cciss0", .ndevices = 0 },
78 { .name = "cciss1", .ndevices = 0 },
79 { .name = "cciss2", .ndevices = 0 },
80 { .name = "cciss3", .ndevices = 0 },
81 { .name = "cciss4", .ndevices = 0 },
82 { .name = "cciss5", .ndevices = 0 },
83 { .name = "cciss6", .ndevices = 0 },
84 { .name = "cciss7", .ndevices = 0 },
87 static struct scsi_host_template cciss_driver_template = {
88 .module = THIS_MODULE,
89 .name = "cciss",
90 .proc_name = "cciss",
91 .proc_info = cciss_scsi_proc_info,
92 .queuecommand = cciss_scsi_queue_command,
93 .can_queue = SCSI_CCISS_CAN_QUEUE,
94 .this_id = 7,
95 .sg_tablesize = MAXSGENTRIES,
96 .cmd_per_lun = 1,
97 .use_clustering = DISABLE_CLUSTERING,
98 /* Can't have eh_bus_reset_handler or eh_host_reset_handler for cciss */
99 .eh_device_reset_handler= cciss_eh_device_reset_handler,
100 .eh_abort_handler = cciss_eh_abort_handler,
103 #pragma pack(1)
104 struct cciss_scsi_cmd_stack_elem_t {
105 CommandList_struct cmd;
106 ErrorInfo_struct Err;
107 __u32 busaddr;
108 __u32 pad;
111 #pragma pack()
113 #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
114 CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
115 // plus two for init time usage
117 #pragma pack(1)
118 struct cciss_scsi_cmd_stack_t {
119 struct cciss_scsi_cmd_stack_elem_t *pool;
120 struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];
121 dma_addr_t cmd_pool_handle;
122 int top;
124 #pragma pack()
126 struct cciss_scsi_adapter_data_t {
127 struct Scsi_Host *scsi_host;
128 struct cciss_scsi_cmd_stack_t cmd_stack;
129 int registered;
130 spinlock_t lock; // to protect ccissscsi[ctlr];
133 #define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \
134 &(((struct cciss_scsi_adapter_data_t *) \
135 hba[ctlr]->scsi_ctlr)->lock), flags);
136 #define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \
137 &(((struct cciss_scsi_adapter_data_t *) \
138 hba[ctlr]->scsi_ctlr)->lock), flags);
140 static CommandList_struct *
141 scsi_cmd_alloc(ctlr_info_t *h)
143 /* assume only one process in here at a time, locking done by caller. */
144 /* use CCISS_LOCK(ctlr) */
145 /* might be better to rewrite how we allocate scsi commands in a way that */
146 /* needs no locking at all. */
148 /* take the top memory chunk off the stack and return it, if any. */
149 struct cciss_scsi_cmd_stack_elem_t *c;
150 struct cciss_scsi_adapter_data_t *sa;
151 struct cciss_scsi_cmd_stack_t *stk;
152 u64bit temp64;
154 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
155 stk = &sa->cmd_stack;
157 if (stk->top < 0)
158 return NULL;
159 c = stk->elem[stk->top];
160 /* memset(c, 0, sizeof(*c)); */
161 memset(&c->cmd, 0, sizeof(c->cmd));
162 memset(&c->Err, 0, sizeof(c->Err));
163 /* set physical addr of cmd and addr of scsi parameters */
164 c->cmd.busaddr = c->busaddr;
165 /* (__u32) (stk->cmd_pool_handle +
166 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top)); */
168 temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));
169 /* (__u64) (stk->cmd_pool_handle +
170 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top) +
171 sizeof(CommandList_struct)); */
172 stk->top--;
173 c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;
174 c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;
175 c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);
177 c->cmd.ctlr = h->ctlr;
178 c->cmd.err_info = &c->Err;
180 return (CommandList_struct *) c;
183 static void
184 scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
186 /* assume only one process in here at a time, locking done by caller. */
187 /* use CCISS_LOCK(ctlr) */
188 /* drop the free memory chunk on top of the stack. */
190 struct cciss_scsi_adapter_data_t *sa;
191 struct cciss_scsi_cmd_stack_t *stk;
193 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
194 stk = &sa->cmd_stack;
195 if (stk->top >= CMD_STACK_SIZE) {
196 printk("cciss: scsi_cmd_free called too many times.\n");
197 BUG();
199 stk->top++;
200 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
203 static int
204 scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
206 int i;
207 struct cciss_scsi_cmd_stack_t *stk;
208 size_t size;
210 stk = &sa->cmd_stack;
211 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
213 // pci_alloc_consistent guarantees 32-bit DMA address will
214 // be used
216 stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
217 pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
219 if (stk->pool == NULL) {
220 printk("stk->pool is null\n");
221 return -1;
224 for (i=0; i<CMD_STACK_SIZE; i++) {
225 stk->elem[i] = &stk->pool[i];
226 stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle +
227 (sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
229 stk->top = CMD_STACK_SIZE-1;
230 return 0;
233 static void
234 scsi_cmd_stack_free(int ctlr)
236 struct cciss_scsi_adapter_data_t *sa;
237 struct cciss_scsi_cmd_stack_t *stk;
238 size_t size;
240 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
241 stk = &sa->cmd_stack;
242 if (stk->top != CMD_STACK_SIZE-1) {
243 printk( "cciss: %d scsi commands are still outstanding.\n",
244 CMD_STACK_SIZE - stk->top);
245 // BUG();
246 printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);
248 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
250 pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
251 stk->pool = NULL;
254 /* scsi_device_types comes from scsi.h */
255 #define DEVICETYPE(n) (n<0 || n>MAX_SCSI_DEVICE_CODE) ? \
256 "Unknown" : scsi_device_types[n]
258 #if 0
259 static int xmargin=8;
260 static int amargin=60;
262 static void
263 print_bytes (unsigned char *c, int len, int hex, int ascii)
266 int i;
267 unsigned char *x;
269 if (hex)
271 x = c;
272 for (i=0;i<len;i++)
274 if ((i % xmargin) == 0 && i>0) printk("\n");
275 if ((i % xmargin) == 0) printk("0x%04x:", i);
276 printk(" %02x", *x);
277 x++;
279 printk("\n");
281 if (ascii)
283 x = c;
284 for (i=0;i<len;i++)
286 if ((i % amargin) == 0 && i>0) printk("\n");
287 if ((i % amargin) == 0) printk("0x%04x:", i);
288 if (*x > 26 && *x < 128) printk("%c", *x);
289 else printk(".");
290 x++;
292 printk("\n");
296 static void
297 print_cmd(CommandList_struct *cp)
299 printk("queue:%d\n", cp->Header.ReplyQueue);
300 printk("sglist:%d\n", cp->Header.SGList);
301 printk("sgtot:%d\n", cp->Header.SGTotal);
302 printk("Tag:0x%08x/0x%08x\n", cp->Header.Tag.upper,
303 cp->Header.Tag.lower);
304 printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
305 cp->Header.LUN.LunAddrBytes[0],
306 cp->Header.LUN.LunAddrBytes[1],
307 cp->Header.LUN.LunAddrBytes[2],
308 cp->Header.LUN.LunAddrBytes[3],
309 cp->Header.LUN.LunAddrBytes[4],
310 cp->Header.LUN.LunAddrBytes[5],
311 cp->Header.LUN.LunAddrBytes[6],
312 cp->Header.LUN.LunAddrBytes[7]);
313 printk("CDBLen:%d\n", cp->Request.CDBLen);
314 printk("Type:%d\n",cp->Request.Type.Type);
315 printk("Attr:%d\n",cp->Request.Type.Attribute);
316 printk(" Dir:%d\n",cp->Request.Type.Direction);
317 printk("Timeout:%d\n",cp->Request.Timeout);
318 printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"
319 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
320 cp->Request.CDB[0], cp->Request.CDB[1],
321 cp->Request.CDB[2], cp->Request.CDB[3],
322 cp->Request.CDB[4], cp->Request.CDB[5],
323 cp->Request.CDB[6], cp->Request.CDB[7],
324 cp->Request.CDB[8], cp->Request.CDB[9],
325 cp->Request.CDB[10], cp->Request.CDB[11],
326 cp->Request.CDB[12], cp->Request.CDB[13],
327 cp->Request.CDB[14], cp->Request.CDB[15]),
328 printk("edesc.Addr: 0x%08x/0%08x, Len = %d\n",
329 cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower,
330 cp->ErrDesc.Len);
331 printk("sgs..........Errorinfo:\n");
332 printk("scsistatus:%d\n", cp->err_info->ScsiStatus);
333 printk("senselen:%d\n", cp->err_info->SenseLen);
334 printk("cmd status:%d\n", cp->err_info->CommandStatus);
335 printk("resid cnt:%d\n", cp->err_info->ResidualCnt);
336 printk("offense size:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);
337 printk("offense byte:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);
338 printk("offense value:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
342 #endif
344 static int
345 find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
347 /* finds an unused bus, target, lun for a new device */
348 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
349 int i, found=0;
350 unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
352 memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
354 target_taken[SELF_SCSI_ID] = 1;
355 for (i=0;i<ccissscsi[ctlr].ndevices;i++)
356 target_taken[ccissscsi[ctlr].dev[i].target] = 1;
358 for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {
359 if (!target_taken[i]) {
360 *bus = 0; *target=i; *lun = 0; found=1;
361 break;
364 return (!found);
367 static int
368 cciss_scsi_add_entry(int ctlr, int hostno,
369 unsigned char *scsi3addr, int devtype)
371 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
372 int n = ccissscsi[ctlr].ndevices;
373 struct cciss_scsi_dev_t *sd;
375 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
376 printk("cciss%d: Too many devices, "
377 "some will be inaccessible.\n", ctlr);
378 return -1;
380 sd = &ccissscsi[ctlr].dev[n];
381 if (find_bus_target_lun(ctlr, &sd->bus, &sd->target, &sd->lun) != 0)
382 return -1;
383 memcpy(&sd->scsi3addr[0], scsi3addr, 8);
384 sd->devtype = devtype;
385 ccissscsi[ctlr].ndevices++;
387 /* initially, (before registering with scsi layer) we don't
388 know our hostno and we don't want to print anything first
389 time anyway (the scsi layer's inquiries will show that info) */
390 if (hostno != -1)
391 printk("cciss%d: %s device c%db%dt%dl%d added.\n",
392 ctlr, DEVICETYPE(sd->devtype), hostno,
393 sd->bus, sd->target, sd->lun);
394 return 0;
397 static void
398 cciss_scsi_remove_entry(int ctlr, int hostno, int entry)
400 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
401 int i;
402 struct cciss_scsi_dev_t sd;
404 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
405 sd = ccissscsi[ctlr].dev[entry];
406 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
407 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
408 ccissscsi[ctlr].ndevices--;
409 printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
410 ctlr, DEVICETYPE(sd.devtype), hostno,
411 sd.bus, sd.target, sd.lun);
415 #define SCSI3ADDR_EQ(a,b) ( \
416 (a)[7] == (b)[7] && \
417 (a)[6] == (b)[6] && \
418 (a)[5] == (b)[5] && \
419 (a)[4] == (b)[4] && \
420 (a)[3] == (b)[3] && \
421 (a)[2] == (b)[2] && \
422 (a)[1] == (b)[1] && \
423 (a)[0] == (b)[0])
425 static int
426 adjust_cciss_scsi_table(int ctlr, int hostno,
427 struct cciss_scsi_dev_t sd[], int nsds)
429 /* sd contains scsi3 addresses and devtypes, but
430 bus target and lun are not filled in. This funciton
431 takes what's in sd to be the current and adjusts
432 ccissscsi[] to be in line with what's in sd. */
434 int i,j, found, changes=0;
435 struct cciss_scsi_dev_t *csd;
436 unsigned long flags;
438 CPQ_TAPE_LOCK(ctlr, flags);
440 /* find any devices in ccissscsi[] that are not in
441 sd[] and remove them from ccissscsi[] */
443 i = 0;
444 while(i<ccissscsi[ctlr].ndevices) {
445 csd = &ccissscsi[ctlr].dev[i];
446 found=0;
447 for (j=0;j<nsds;j++) {
448 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
449 csd->scsi3addr)) {
450 if (sd[j].devtype == csd->devtype)
451 found=2;
452 else
453 found=1;
454 break;
458 if (found == 0) { /* device no longer present. */
459 changes++;
460 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
461 ctlr, DEVICETYPE(csd->devtype), hostno,
462 csd->bus, csd->target, csd->lun); */
463 cciss_scsi_remove_entry(ctlr, hostno, i);
464 /* note, i not incremented */
466 else if (found == 1) { /* device is different kind */
467 changes++;
468 printk("cciss%d: device c%db%dt%dl%d type changed "
469 "(device type now %s).\n",
470 ctlr, hostno, csd->bus, csd->target, csd->lun,
471 DEVICETYPE(csd->devtype));
472 csd->devtype = sd[j].devtype;
473 i++; /* so just move along. */
474 } else /* device is same as it ever was, */
475 i++; /* so just move along. */
478 /* Now, make sure every device listed in sd[] is also
479 listed in ccissscsi[], adding them if they aren't found */
481 for (i=0;i<nsds;i++) {
482 found=0;
483 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
484 csd = &ccissscsi[ctlr].dev[j];
485 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
486 csd->scsi3addr)) {
487 if (sd[i].devtype == csd->devtype)
488 found=2; /* found device */
489 else
490 found=1; /* found a bug. */
491 break;
494 if (!found) {
495 changes++;
496 if (cciss_scsi_add_entry(ctlr, hostno,
497 &sd[i].scsi3addr[0], sd[i].devtype) != 0)
498 break;
499 } else if (found == 1) {
500 /* should never happen... */
501 changes++;
502 printk("cciss%d: device unexpectedly changed type\n",
503 ctlr);
504 /* but if it does happen, we just ignore that device */
507 CPQ_TAPE_UNLOCK(ctlr, flags);
509 if (!changes)
510 printk("cciss%d: No device changes detected.\n", ctlr);
512 return 0;
515 static int
516 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
518 int i;
519 struct cciss_scsi_dev_t *sd;
520 unsigned long flags;
522 CPQ_TAPE_LOCK(ctlr, flags);
523 for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
524 sd = &ccissscsi[ctlr].dev[i];
525 if (sd->bus == bus &&
526 sd->target == target &&
527 sd->lun == lun) {
528 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
529 CPQ_TAPE_UNLOCK(ctlr, flags);
530 return 0;
533 CPQ_TAPE_UNLOCK(ctlr, flags);
534 return -1;
537 static void
538 cciss_scsi_setup(int cntl_num)
540 struct cciss_scsi_adapter_data_t * shba;
542 ccissscsi[cntl_num].ndevices = 0;
543 shba = (struct cciss_scsi_adapter_data_t *)
544 kmalloc(sizeof(*shba), GFP_KERNEL);
545 if (shba == NULL)
546 return;
547 shba->scsi_host = NULL;
548 spin_lock_init(&shba->lock);
549 shba->registered = 0;
550 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
551 kfree(shba);
552 shba = NULL;
554 hba[cntl_num]->scsi_ctlr = (void *) shba;
555 return;
558 static void
559 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
561 struct scsi_cmnd *cmd;
562 ctlr_info_t *ctlr;
563 u64bit addr64;
564 ErrorInfo_struct *ei;
566 ei = cp->err_info;
568 /* First, see if it was a message rather than a command */
569 if (cp->Request.Type.Type == TYPE_MSG) {
570 cp->cmd_type = CMD_MSG_DONE;
571 return;
574 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
575 ctlr = hba[cp->ctlr];
577 /* undo the DMA mappings */
579 if (cmd->use_sg) {
580 pci_unmap_sg(ctlr->pdev,
581 cmd->buffer, cmd->use_sg,
582 cmd->sc_data_direction);
584 else if (cmd->request_bufflen) {
585 addr64.val32.lower = cp->SG[0].Addr.lower;
586 addr64.val32.upper = cp->SG[0].Addr.upper;
587 pci_unmap_single(ctlr->pdev, (dma_addr_t) addr64.val,
588 cmd->request_bufflen,
589 cmd->sc_data_direction);
592 cmd->result = (DID_OK << 16); /* host byte */
593 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
594 /* cmd->result |= (GOOD < 1); */ /* status byte */
596 cmd->result |= (ei->ScsiStatus);
597 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
599 /* copy the sense data whether we need to or not. */
601 memcpy(cmd->sense_buffer, ei->SenseInfo,
602 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
603 SCSI_SENSE_BUFFERSIZE :
604 ei->SenseLen);
605 cmd->resid = ei->ResidualCnt;
607 if(ei->CommandStatus != 0)
608 { /* an error has occurred */
609 switch(ei->CommandStatus)
611 case CMD_TARGET_STATUS:
612 /* Pass it up to the upper layers... */
613 if( ei->ScsiStatus)
615 #if 0
616 printk(KERN_WARNING "cciss: cmd %p "
617 "has SCSI Status = %x\n",
618 cp,
619 ei->ScsiStatus);
620 #endif
621 cmd->result |= (ei->ScsiStatus < 1);
623 else { /* scsi status is zero??? How??? */
625 /* Ordinarily, this case should never happen, but there is a bug
626 in some released firmware revisions that allows it to happen
627 if, for example, a 4100 backplane loses power and the tape
628 drive is in it. We assume that it's a fatal error of some
629 kind because we can't show that it wasn't. We will make it
630 look like selection timeout since that is the most common
631 reason for this to occur, and it's severe enough. */
633 cmd->result = DID_NO_CONNECT << 16;
635 break;
636 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
637 break;
638 case CMD_DATA_OVERRUN:
639 printk(KERN_WARNING "cciss: cp %p has"
640 " completed with data overrun "
641 "reported\n", cp);
642 break;
643 case CMD_INVALID: {
644 /* print_bytes(cp, sizeof(*cp), 1, 0);
645 print_cmd(cp); */
646 /* We get CMD_INVALID if you address a non-existent tape drive instead
647 of a selection timeout (no response). You will see this if you yank
648 out a tape drive, then try to access it. This is kind of a shame
649 because it means that any other CMD_INVALID (e.g. driver bug) will
650 get interpreted as a missing target. */
651 cmd->result = DID_NO_CONNECT << 16;
653 break;
654 case CMD_PROTOCOL_ERR:
655 printk(KERN_WARNING "cciss: cp %p has "
656 "protocol error \n", cp);
657 break;
658 case CMD_HARDWARE_ERR:
659 cmd->result = DID_ERROR << 16;
660 printk(KERN_WARNING "cciss: cp %p had "
661 " hardware error\n", cp);
662 break;
663 case CMD_CONNECTION_LOST:
664 cmd->result = DID_ERROR << 16;
665 printk(KERN_WARNING "cciss: cp %p had "
666 "connection lost\n", cp);
667 break;
668 case CMD_ABORTED:
669 cmd->result = DID_ABORT << 16;
670 printk(KERN_WARNING "cciss: cp %p was "
671 "aborted\n", cp);
672 break;
673 case CMD_ABORT_FAILED:
674 cmd->result = DID_ERROR << 16;
675 printk(KERN_WARNING "cciss: cp %p reports "
676 "abort failed\n", cp);
677 break;
678 case CMD_UNSOLICITED_ABORT:
679 cmd->result = DID_ABORT << 16;
680 printk(KERN_WARNING "cciss: cp %p aborted "
681 "do to an unsolicited abort\n", cp);
682 break;
683 case CMD_TIMEOUT:
684 cmd->result = DID_TIME_OUT << 16;
685 printk(KERN_WARNING "cciss: cp %p timedout\n",
686 cp);
687 break;
688 default:
689 cmd->result = DID_ERROR << 16;
690 printk(KERN_WARNING "cciss: cp %p returned "
691 "unknown status %x\n", cp,
692 ei->CommandStatus);
695 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
696 // cmd->target, cmd->lun);
697 cmd->scsi_done(cmd);
698 scsi_cmd_free(ctlr, cp);
701 static int
702 cciss_scsi_detect(int ctlr)
704 struct Scsi_Host *sh;
705 int error;
707 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
708 if (sh == NULL)
709 goto fail;
710 sh->io_port = 0; // good enough? FIXME,
711 sh->n_io_port = 0; // I don't think we use these two...
712 sh->this_id = SELF_SCSI_ID;
714 ((struct cciss_scsi_adapter_data_t *)
715 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
716 sh->hostdata[0] = (unsigned long) hba[ctlr];
717 sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT];
718 sh->unique_id = sh->irq;
719 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
720 if (error)
721 goto fail_host_put;
722 scsi_scan_host(sh);
723 return 1;
725 fail_host_put:
726 scsi_host_put(sh);
727 fail:
728 return 0;
731 static void
732 cciss_unmap_one(struct pci_dev *pdev,
733 CommandList_struct *cp,
734 size_t buflen,
735 int data_direction)
737 u64bit addr64;
739 addr64.val32.lower = cp->SG[0].Addr.lower;
740 addr64.val32.upper = cp->SG[0].Addr.upper;
741 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
744 static void
745 cciss_map_one(struct pci_dev *pdev,
746 CommandList_struct *cp,
747 unsigned char *buf,
748 size_t buflen,
749 int data_direction)
751 __u64 addr64;
753 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
754 cp->SG[0].Addr.lower =
755 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
756 cp->SG[0].Addr.upper =
757 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
758 cp->SG[0].Len = buflen;
759 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
760 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
763 static int
764 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
765 CommandList_struct *cp,
766 unsigned char *scsi3addr,
767 unsigned char *cdb,
768 unsigned char cdblen,
769 unsigned char *buf, int bufsize,
770 int direction)
772 unsigned long flags;
773 DECLARE_COMPLETION(wait);
775 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
776 cp->scsi_cmd = NULL;
777 cp->Header.ReplyQueue = 0; // unused in simple mode
778 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
779 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
780 // Fill in the request block...
782 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
783 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
784 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
786 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
787 memcpy(cp->Request.CDB, cdb, cdblen);
788 cp->Request.Timeout = 0;
789 cp->Request.CDBLen = cdblen;
790 cp->Request.Type.Type = TYPE_CMD;
791 cp->Request.Type.Attribute = ATTR_SIMPLE;
792 cp->Request.Type.Direction = direction;
794 /* Fill in the SG list and do dma mapping */
795 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
796 bufsize, DMA_FROM_DEVICE);
798 cp->waiting = &wait;
800 /* Put the request on the tail of the request queue */
801 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
802 addQ(&c->reqQ, cp);
803 c->Qdepth++;
804 start_io(c);
805 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
807 wait_for_completion(&wait);
809 /* undo the dma mapping */
810 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
811 return(0);
814 static void
815 cciss_scsi_interpret_error(CommandList_struct *cp)
817 ErrorInfo_struct *ei;
819 ei = cp->err_info;
820 switch(ei->CommandStatus)
822 case CMD_TARGET_STATUS:
823 printk(KERN_WARNING "cciss: cmd %p has "
824 "completed with errors\n", cp);
825 printk(KERN_WARNING "cciss: cmd %p "
826 "has SCSI Status = %x\n",
827 cp,
828 ei->ScsiStatus);
829 if (ei->ScsiStatus == 0)
830 printk(KERN_WARNING
831 "cciss:SCSI status is abnormally zero. "
832 "(probably indicates selection timeout "
833 "reported incorrectly due to a known "
834 "firmware bug, circa July, 2001.)\n");
835 break;
836 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
837 printk("UNDERRUN\n");
838 break;
839 case CMD_DATA_OVERRUN:
840 printk(KERN_WARNING "cciss: cp %p has"
841 " completed with data overrun "
842 "reported\n", cp);
843 break;
844 case CMD_INVALID: {
845 /* controller unfortunately reports SCSI passthru's */
846 /* to non-existent targets as invalid commands. */
847 printk(KERN_WARNING "cciss: cp %p is "
848 "reported invalid (probably means "
849 "target device no longer present)\n",
850 cp);
851 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
852 print_cmd(cp); */
854 break;
855 case CMD_PROTOCOL_ERR:
856 printk(KERN_WARNING "cciss: cp %p has "
857 "protocol error \n", cp);
858 break;
859 case CMD_HARDWARE_ERR:
860 /* cmd->result = DID_ERROR << 16; */
861 printk(KERN_WARNING "cciss: cp %p had "
862 " hardware error\n", cp);
863 break;
864 case CMD_CONNECTION_LOST:
865 printk(KERN_WARNING "cciss: cp %p had "
866 "connection lost\n", cp);
867 break;
868 case CMD_ABORTED:
869 printk(KERN_WARNING "cciss: cp %p was "
870 "aborted\n", cp);
871 break;
872 case CMD_ABORT_FAILED:
873 printk(KERN_WARNING "cciss: cp %p reports "
874 "abort failed\n", cp);
875 break;
876 case CMD_UNSOLICITED_ABORT:
877 printk(KERN_WARNING "cciss: cp %p aborted "
878 "do to an unsolicited abort\n", cp);
879 break;
880 case CMD_TIMEOUT:
881 printk(KERN_WARNING "cciss: cp %p timedout\n",
882 cp);
883 break;
884 default:
885 printk(KERN_WARNING "cciss: cp %p returned "
886 "unknown status %x\n", cp,
887 ei->CommandStatus);
891 static int
892 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
893 unsigned char *buf, unsigned char bufsize)
895 int rc;
896 CommandList_struct *cp;
897 char cdb[6];
898 ErrorInfo_struct *ei;
899 unsigned long flags;
901 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
902 cp = scsi_cmd_alloc(c);
903 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
905 if (cp == NULL) { /* trouble... */
906 printk("cmd_alloc returned NULL!\n");
907 return -1;
910 ei = cp->err_info;
912 cdb[0] = CISS_INQUIRY;
913 cdb[1] = 0;
914 cdb[2] = 0;
915 cdb[3] = 0;
916 cdb[4] = bufsize;
917 cdb[5] = 0;
918 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
919 6, buf, bufsize, XFER_READ);
921 if (rc != 0) return rc; /* something went wrong */
923 if (ei->CommandStatus != 0 &&
924 ei->CommandStatus != CMD_DATA_UNDERRUN) {
925 cciss_scsi_interpret_error(cp);
926 rc = -1;
928 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
929 scsi_cmd_free(c, cp);
930 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
931 return rc;
934 static int
935 cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
936 ReportLunData_struct *buf, int bufsize)
938 int rc;
939 CommandList_struct *cp;
940 unsigned char cdb[12];
941 unsigned char scsi3addr[8];
942 ErrorInfo_struct *ei;
943 unsigned long flags;
945 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
946 cp = scsi_cmd_alloc(c);
947 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
948 if (cp == NULL) { /* trouble... */
949 printk("cmd_alloc returned NULL!\n");
950 return -1;
953 memset(&scsi3addr[0], 0, 8); /* address the controller */
954 cdb[0] = CISS_REPORT_PHYS;
955 cdb[1] = 0;
956 cdb[2] = 0;
957 cdb[3] = 0;
958 cdb[4] = 0;
959 cdb[5] = 0;
960 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
961 cdb[7] = (bufsize >> 16) & 0xFF;
962 cdb[8] = (bufsize >> 8) & 0xFF;
963 cdb[9] = bufsize & 0xFF;
964 cdb[10] = 0;
965 cdb[11] = 0;
967 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
968 cdb, 12,
969 (unsigned char *) buf,
970 bufsize, XFER_READ);
972 if (rc != 0) return rc; /* something went wrong */
974 ei = cp->err_info;
975 if (ei->CommandStatus != 0 &&
976 ei->CommandStatus != CMD_DATA_UNDERRUN) {
977 cciss_scsi_interpret_error(cp);
978 rc = -1;
980 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
981 scsi_cmd_free(c, cp);
982 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
983 return rc;
986 static void
987 cciss_update_non_disk_devices(int cntl_num, int hostno)
989 /* the idea here is we could get notified from /proc
990 that some devices have changed, so we do a report
991 physical luns cmd, and adjust our list of devices
992 accordingly. (We can't rely on the scsi-mid layer just
993 doing inquiries, because the "busses" that the scsi
994 mid-layer probes are totally fabricated by this driver,
995 so new devices wouldn't show up.
997 the scsi3addr's of devices won't change so long as the
998 adapter is not reset. That means we can rescan and
999 tell which devices we already know about, vs. new
1000 devices, vs. disappearing devices.
1002 Also, if you yank out a tape drive, then put in a disk
1003 in it's place, (say, a configured volume from another
1004 array controller for instance) _don't_ poke this driver
1005 (so it thinks it's still a tape, but _do_ poke the scsi
1006 mid layer, so it does an inquiry... the scsi mid layer
1007 will see the physical disk. This would be bad. Need to
1008 think about how to prevent that. One idea would be to
1009 snoop all scsi responses and if an inquiry repsonse comes
1010 back that reports a disk, chuck it an return selection
1011 timeout instead and adjust our table... Not sure i like
1012 that though.
1015 #define OBDR_TAPE_INQ_SIZE 49
1016 #define OBDR_TAPE_SIG "$DR-10"
1017 ReportLunData_struct *ld_buff;
1018 unsigned char *inq_buff;
1019 unsigned char scsi3addr[8];
1020 ctlr_info_t *c;
1021 __u32 num_luns=0;
1022 unsigned char *ch;
1023 /* unsigned char found[CCISS_MAX_SCSI_DEVS_PER_HBA]; */
1024 struct cciss_scsi_dev_t currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1025 int ncurrent=0;
1026 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1027 int i;
1029 c = (ctlr_info_t *) hba[cntl_num];
1030 ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
1031 if (ld_buff == NULL) {
1032 printk(KERN_ERR "cciss: out of memory\n");
1033 return;
1035 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1036 if (inq_buff == NULL) {
1037 printk(KERN_ERR "cciss: out of memory\n");
1038 kfree(ld_buff);
1039 return;
1042 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1043 ch = &ld_buff->LUNListLength[0];
1044 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1045 if (num_luns > CISS_MAX_PHYS_LUN) {
1046 printk(KERN_WARNING
1047 "cciss: Maximum physical LUNs (%d) exceeded. "
1048 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1049 num_luns - CISS_MAX_PHYS_LUN);
1050 num_luns = CISS_MAX_PHYS_LUN;
1053 else {
1054 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1055 goto out;
1059 /* adjust our table of devices */
1060 for(i=0; i<num_luns; i++)
1062 int devtype;
1064 /* for each physical lun, do an inquiry */
1065 if (ld_buff->LUN[i][3] & 0xC0) continue;
1066 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1067 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1069 if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, inq_buff,
1070 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1071 /* Inquiry failed (msg printed already) */
1072 devtype = 0; /* so we will skip this device. */
1073 } else /* what kind of device is this? */
1074 devtype = (inq_buff[0] & 0x1f);
1076 switch (devtype)
1078 case 0x05: /* CD-ROM */ {
1080 /* We don't *really* support actual CD-ROM devices,
1081 * just this "One Button Disaster Recovery" tape drive
1082 * which temporarily pretends to be a CD-ROM drive.
1083 * So we check that the device is really an OBDR tape
1084 * device by checking for "$DR-10" in bytes 43-48 of
1085 * the inquiry data.
1087 char obdr_sig[7];
1089 strncpy(obdr_sig, &inq_buff[43], 6);
1090 obdr_sig[6] = '\0';
1091 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1092 /* Not OBDR device, ignore it. */
1093 break;
1095 /* fall through . . . */
1096 case 0x01: /* sequential access, (tape) */
1097 case 0x08: /* medium changer */
1098 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1099 printk(KERN_INFO "cciss%d: %s ignored, "
1100 "too many devices.\n", cntl_num,
1101 DEVICETYPE(devtype));
1102 break;
1104 memcpy(&currentsd[ncurrent].scsi3addr[0],
1105 &scsi3addr[0], 8);
1106 currentsd[ncurrent].devtype = devtype;
1107 currentsd[ncurrent].bus = -1;
1108 currentsd[ncurrent].target = -1;
1109 currentsd[ncurrent].lun = -1;
1110 ncurrent++;
1111 break;
1112 default:
1113 break;
1117 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1118 out:
1119 kfree(inq_buff);
1120 kfree(ld_buff);
1121 return;
1124 static int
1125 is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1127 int verb_len = strlen(verb);
1128 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1129 return verb_len;
1130 else
1131 return 0;
1134 static int
1135 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1137 int arg_len;
1139 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1140 cciss_update_non_disk_devices(ctlr, hostno);
1141 else
1142 return -EINVAL;
1143 return length;
1147 static int
1148 cciss_scsi_proc_info(struct Scsi_Host *sh,
1149 char *buffer, /* data buffer */
1150 char **start, /* where data in buffer starts */
1151 off_t offset, /* offset from start of imaginary file */
1152 int length, /* length of data in buffer */
1153 int func) /* 0 == read, 1 == write */
1156 int buflen, datalen;
1157 ctlr_info_t *ci;
1158 int i;
1159 int cntl_num;
1162 ci = (ctlr_info_t *) sh->hostdata[0];
1163 if (ci == NULL) /* This really shouldn't ever happen. */
1164 return -EINVAL;
1166 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1168 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1169 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n",
1170 cntl_num, sh->host_no);
1172 /* this information is needed by apps to know which cciss
1173 device corresponds to which scsi host number without
1174 having to open a scsi target device node. The device
1175 information is not a duplicate of /proc/scsi/scsi because
1176 the two may be out of sync due to scsi hotplug, rather
1177 this info is for an app to be able to use to know how to
1178 get them back in sync. */
1180 for (i=0;i<ccissscsi[cntl_num].ndevices;i++) {
1181 struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i];
1182 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
1183 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1184 sh->host_no, sd->bus, sd->target, sd->lun,
1185 sd->devtype,
1186 sd->scsi3addr[0], sd->scsi3addr[1],
1187 sd->scsi3addr[2], sd->scsi3addr[3],
1188 sd->scsi3addr[4], sd->scsi3addr[5],
1189 sd->scsi3addr[6], sd->scsi3addr[7]);
1191 datalen = buflen - offset;
1192 if (datalen < 0) { /* they're reading past EOF. */
1193 datalen = 0;
1194 *start = buffer+buflen;
1195 } else
1196 *start = buffer + offset;
1197 return(datalen);
1198 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1199 return cciss_scsi_user_command(cntl_num, sh->host_no,
1200 buffer, length);
1203 /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1204 dma mapping and fills in the scatter gather entries of the
1205 cciss command, cp. */
1207 static void
1208 cciss_scatter_gather(struct pci_dev *pdev,
1209 CommandList_struct *cp,
1210 struct scsi_cmnd *cmd)
1212 unsigned int use_sg, nsegs=0, len;
1213 struct scatterlist *scatter = (struct scatterlist *) cmd->buffer;
1214 __u64 addr64;
1216 /* is it just one virtual address? */
1217 if (!cmd->use_sg) {
1218 if (cmd->request_bufflen) { /* anything to xfer? */
1220 addr64 = (__u64) pci_map_single(pdev,
1221 cmd->request_buffer,
1222 cmd->request_bufflen,
1223 cmd->sc_data_direction);
1225 cp->SG[0].Addr.lower =
1226 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1227 cp->SG[0].Addr.upper =
1228 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1229 cp->SG[0].Len = cmd->request_bufflen;
1230 nsegs=1;
1232 } /* else, must be a list of virtual addresses.... */
1233 else if (cmd->use_sg <= MAXSGENTRIES) { /* not too many addrs? */
1235 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg,
1236 cmd->sc_data_direction);
1238 for (nsegs=0; nsegs < use_sg; nsegs++) {
1239 addr64 = (__u64) sg_dma_address(&scatter[nsegs]);
1240 len = sg_dma_len(&scatter[nsegs]);
1241 cp->SG[nsegs].Addr.lower =
1242 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1243 cp->SG[nsegs].Addr.upper =
1244 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1245 cp->SG[nsegs].Len = len;
1246 cp->SG[nsegs].Ext = 0; // we are not chaining
1248 } else BUG();
1250 cp->Header.SGList = (__u8) nsegs; /* no. SGs contig in this cmd */
1251 cp->Header.SGTotal = (__u16) nsegs; /* total sgs in this cmd list */
1252 return;
1256 static int
1257 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1259 ctlr_info_t **c;
1260 int ctlr, rc;
1261 unsigned char scsi3addr[8];
1262 CommandList_struct *cp;
1263 unsigned long flags;
1265 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1266 // We violate cmd->host privacy here. (Is there another way?)
1267 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1268 ctlr = (*c)->ctlr;
1270 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1271 cmd->device->lun, scsi3addr);
1272 if (rc != 0) {
1273 /* the scsi nexus does not match any that we presented... */
1274 /* pretend to mid layer that we got selection timeout */
1275 cmd->result = DID_NO_CONNECT << 16;
1276 done(cmd);
1277 /* we might want to think about registering controller itself
1278 as a processor device on the bus so sg binds to it. */
1279 return 0;
1282 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1283 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1284 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1285 // cmd->target, cmd->lun);
1287 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1288 see what the device thinks of it. */
1290 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1291 cp = scsi_cmd_alloc(*c);
1292 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1293 if (cp == NULL) { /* trouble... */
1294 printk("scsi_cmd_alloc returned NULL!\n");
1295 /* FIXME: next 3 lines are -> BAD! <- */
1296 cmd->result = DID_NO_CONNECT << 16;
1297 done(cmd);
1298 return 0;
1301 // Fill in the command list header
1303 cmd->scsi_done = done; // save this for use by completion code
1305 // save cp in case we have to abort it
1306 cmd->host_scribble = (unsigned char *) cp;
1308 cp->cmd_type = CMD_SCSI;
1309 cp->scsi_cmd = cmd;
1310 cp->Header.ReplyQueue = 0; // unused in simple mode
1311 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1312 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1314 // Fill in the request block...
1316 cp->Request.Timeout = 0;
1317 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
1318 BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB));
1319 cp->Request.CDBLen = cmd->cmd_len;
1320 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1321 cp->Request.Type.Type = TYPE_CMD;
1322 cp->Request.Type.Attribute = ATTR_SIMPLE;
1323 switch(cmd->sc_data_direction)
1325 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1326 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1327 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1328 case DMA_BIDIRECTIONAL:
1329 // This can happen if a buggy application does a scsi passthru
1330 // and sets both inlen and outlen to non-zero. ( see
1331 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1333 cp->Request.Type.Direction = XFER_RSVD;
1334 // This is technically wrong, and cciss controllers should
1335 // reject it with CMD_INVALID, which is the most correct
1336 // response, but non-fibre backends appear to let it
1337 // slide by, and give the same results as if this field
1338 // were set correctly. Either way is acceptable for
1339 // our purposes here.
1341 break;
1343 default:
1344 printk("cciss: unknown data direction: %d\n",
1345 cmd->sc_data_direction);
1346 BUG();
1347 break;
1350 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1352 /* Put the request on the tail of the request queue */
1354 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1355 addQ(&(*c)->reqQ, cp);
1356 (*c)->Qdepth++;
1357 start_io(*c);
1358 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1360 /* the cmd'll come back via intr handler in complete_scsi_command() */
1361 return 0;
1364 static void
1365 cciss_unregister_scsi(int ctlr)
1367 struct cciss_scsi_adapter_data_t *sa;
1368 struct cciss_scsi_cmd_stack_t *stk;
1369 unsigned long flags;
1371 /* we are being forcibly unloaded, and may not refuse. */
1373 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1374 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1375 stk = &sa->cmd_stack;
1377 /* if we weren't ever actually registered, don't unregister */
1378 if (sa->registered) {
1379 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1380 scsi_remove_host(sa->scsi_host);
1381 scsi_host_put(sa->scsi_host);
1382 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1385 /* set scsi_host to NULL so our detect routine will
1386 find us on register */
1387 sa->scsi_host = NULL;
1388 scsi_cmd_stack_free(ctlr);
1389 kfree(sa);
1390 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1393 static int
1394 cciss_register_scsi(int ctlr)
1396 unsigned long flags;
1398 CPQ_TAPE_LOCK(ctlr, flags);
1400 /* Since this is really a block driver, the SCSI core may not be
1401 initialized at init time, in which case, calling scsi_register_host
1402 would hang. Instead, we do it later, via /proc filesystem
1403 and rc scripts, when we know SCSI core is good to go. */
1405 /* Only register if SCSI devices are detected. */
1406 if (ccissscsi[ctlr].ndevices != 0) {
1407 ((struct cciss_scsi_adapter_data_t *)
1408 hba[ctlr]->scsi_ctlr)->registered = 1;
1409 CPQ_TAPE_UNLOCK(ctlr, flags);
1410 return cciss_scsi_detect(ctlr);
1412 CPQ_TAPE_UNLOCK(ctlr, flags);
1413 printk(KERN_INFO
1414 "cciss%d: No appropriate SCSI device detected, "
1415 "SCSI subsystem not engaged.\n", ctlr);
1416 return 0;
1419 static int
1420 cciss_engage_scsi(int ctlr)
1422 struct cciss_scsi_adapter_data_t *sa;
1423 struct cciss_scsi_cmd_stack_t *stk;
1424 unsigned long flags;
1426 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1427 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1428 stk = &sa->cmd_stack;
1430 if (((struct cciss_scsi_adapter_data_t *)
1431 hba[ctlr]->scsi_ctlr)->registered) {
1432 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1433 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1434 return ENXIO;
1436 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1437 cciss_update_non_disk_devices(ctlr, -1);
1438 cciss_register_scsi(ctlr);
1439 return 0;
1442 static void
1443 cciss_proc_tape_report(int ctlr, unsigned char *buffer, off_t *pos, off_t *len)
1445 unsigned long flags;
1446 int size;
1448 *pos = *pos -1; *len = *len - 1; // cut off the last trailing newline
1450 CPQ_TAPE_LOCK(ctlr, flags);
1451 size = sprintf(buffer + *len,
1452 "Sequential access devices: %d\n\n",
1453 ccissscsi[ctlr].ndevices);
1454 CPQ_TAPE_UNLOCK(ctlr, flags);
1455 *pos += size; *len += size;
1458 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
1459 * complaining. Doing a host- or bus-reset can't do anything good here.
1460 * Despite what it might say in scsi_error.c, there may well be commands
1461 * on the controller, as the cciss driver registers twice, once as a block
1462 * device for the logical drives, and once as a scsi device, for any tape
1463 * drives. So we know there are no commands out on the tape drives, but we
1464 * don't know there are no commands on the controller, and it is likely
1465 * that there probably are, as the cciss block device is most commonly used
1466 * as a boot device (embedded controller on HP/Compaq systems.)
1469 static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1471 int rc;
1472 CommandList_struct *cmd_in_trouble;
1473 ctlr_info_t **c;
1474 int ctlr;
1476 /* find the controller to which the command to be aborted was sent */
1477 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1478 if (c == NULL) /* paranoia */
1479 return FAILED;
1480 ctlr = (*c)->ctlr;
1481 printk(KERN_WARNING "cciss%d: resetting tape drive or medium changer.\n", ctlr);
1483 /* find the command that's giving us trouble */
1484 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
1485 if (cmd_in_trouble == NULL) { /* paranoia */
1486 return FAILED;
1488 /* send a reset to the SCSI LUN which the command was sent to */
1489 rc = sendcmd(CCISS_RESET_MSG, ctlr, NULL, 0, 2, 0, 0,
1490 (unsigned char *) &cmd_in_trouble->Header.LUN.LunAddrBytes[0],
1491 TYPE_MSG);
1492 /* sendcmd turned off interrputs on the board, turn 'em back on. */
1493 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1494 if (rc == 0)
1495 return SUCCESS;
1496 printk(KERN_WARNING "cciss%d: resetting device failed.\n", ctlr);
1497 return FAILED;
1500 static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1502 int rc;
1503 CommandList_struct *cmd_to_abort;
1504 ctlr_info_t **c;
1505 int ctlr;
1507 /* find the controller to which the command to be aborted was sent */
1508 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1509 if (c == NULL) /* paranoia */
1510 return FAILED;
1511 ctlr = (*c)->ctlr;
1512 printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd\n", ctlr);
1514 /* find the command to be aborted */
1515 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
1516 if (cmd_to_abort == NULL) /* paranoia */
1517 return FAILED;
1518 rc = sendcmd(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag,
1519 0, 2, 0, 0,
1520 (unsigned char *) &cmd_to_abort->Header.LUN.LunAddrBytes[0],
1521 TYPE_MSG);
1522 /* sendcmd turned off interrputs on the board, turn 'em back on. */
1523 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1524 if (rc == 0)
1525 return SUCCESS;
1526 return FAILED;
1530 #else /* no CONFIG_CISS_SCSI_TAPE */
1532 /* If no tape support, then these become defined out of existence */
1534 #define cciss_scsi_setup(cntl_num)
1535 #define cciss_unregister_scsi(ctlr)
1536 #define cciss_register_scsi(ctlr)
1537 #define cciss_proc_tape_report(ctlr, buffer, pos, len)
1539 #endif /* CONFIG_CISS_SCSI_TAPE */