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 <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <asm/atomic.h>
36 #include <linux/timer.h>
37 #include <linux/completion.h>
39 #include "cciss_scsi.h"
41 /* some prototypes... */
47 unsigned int use_unit_num, /* 0: address the controller,
48 1: address logical volume log_unit,
49 2: address is in scsi3addr */
50 unsigned int log_unit,
52 unsigned char *scsi3addr,
56 static int cciss_scsi_proc_info(
58 char *buffer, /* data buffer */
59 char **start, /* where data in buffer starts */
60 off_t offset, /* offset from start of imaginary file */
61 int length, /* length of data in buffer */
62 int func); /* 0 == read, 1 == write */
64 static int cciss_scsi_queue_command (struct scsi_cmnd *cmd,
65 void (* done)(struct scsi_cmnd *));
67 static struct cciss_scsi_hba_t ccissscsi[MAX_CTLR] = {
68 { .name = "cciss0", .ndevices = 0 },
69 { .name = "cciss1", .ndevices = 0 },
70 { .name = "cciss2", .ndevices = 0 },
71 { .name = "cciss3", .ndevices = 0 },
72 { .name = "cciss4", .ndevices = 0 },
73 { .name = "cciss5", .ndevices = 0 },
74 { .name = "cciss6", .ndevices = 0 },
75 { .name = "cciss7", .ndevices = 0 },
78 static struct scsi_host_template cciss_driver_template = {
79 .module = THIS_MODULE,
82 .proc_info = cciss_scsi_proc_info,
83 .queuecommand = cciss_scsi_queue_command,
84 .can_queue = SCSI_CCISS_CAN_QUEUE,
86 .sg_tablesize = MAXSGENTRIES,
88 .use_clustering = DISABLE_CLUSTERING,
92 struct cciss_scsi_cmd_stack_elem_t {
93 CommandList_struct cmd;
100 #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
101 CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
102 // plus two for init time usage
105 struct cciss_scsi_cmd_stack_t {
106 struct cciss_scsi_cmd_stack_elem_t *pool;
107 struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];
108 dma_addr_t cmd_pool_handle;
113 struct cciss_scsi_adapter_data_t {
114 struct Scsi_Host *scsi_host;
115 struct cciss_scsi_cmd_stack_t cmd_stack;
117 spinlock_t lock; // to protect ccissscsi[ctlr];
120 #define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \
121 &(((struct cciss_scsi_adapter_data_t *) \
122 hba[ctlr]->scsi_ctlr)->lock), flags);
123 #define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \
124 &(((struct cciss_scsi_adapter_data_t *) \
125 hba[ctlr]->scsi_ctlr)->lock), flags);
127 static CommandList_struct *
128 scsi_cmd_alloc(ctlr_info_t *h)
130 /* assume only one process in here at a time, locking done by caller. */
131 /* use CCISS_LOCK(ctlr) */
132 /* might be better to rewrite how we allocate scsi commands in a way that */
133 /* needs no locking at all. */
135 /* take the top memory chunk off the stack and return it, if any. */
136 struct cciss_scsi_cmd_stack_elem_t *c;
137 struct cciss_scsi_adapter_data_t *sa;
138 struct cciss_scsi_cmd_stack_t *stk;
141 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
142 stk = &sa->cmd_stack;
146 c = stk->elem[stk->top];
147 /* memset(c, 0, sizeof(*c)); */
148 memset(&c->cmd, 0, sizeof(c->cmd));
149 memset(&c->Err, 0, sizeof(c->Err));
150 /* set physical addr of cmd and addr of scsi parameters */
151 c->cmd.busaddr = c->busaddr;
152 /* (__u32) (stk->cmd_pool_handle +
153 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top)); */
155 temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));
156 /* (__u64) (stk->cmd_pool_handle +
157 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top) +
158 sizeof(CommandList_struct)); */
160 c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;
161 c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;
162 c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);
164 c->cmd.ctlr = h->ctlr;
165 c->cmd.err_info = &c->Err;
167 return (CommandList_struct *) c;
171 scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
173 /* assume only one process in here at a time, locking done by caller. */
174 /* use CCISS_LOCK(ctlr) */
175 /* drop the free memory chunk on top of the stack. */
177 struct cciss_scsi_adapter_data_t *sa;
178 struct cciss_scsi_cmd_stack_t *stk;
180 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
181 stk = &sa->cmd_stack;
182 if (stk->top >= CMD_STACK_SIZE) {
183 printk("cciss: scsi_cmd_free called too many times.\n");
187 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
191 scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
194 struct cciss_scsi_cmd_stack_t *stk;
197 stk = &sa->cmd_stack;
198 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
200 // pci_alloc_consistent guarantees 32-bit DMA address will
203 stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
204 pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
206 if (stk->pool == NULL) {
207 printk("stk->pool is null\n");
211 for (i=0; i<CMD_STACK_SIZE; i++) {
212 stk->elem[i] = &stk->pool[i];
213 stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle +
214 (sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
216 stk->top = CMD_STACK_SIZE-1;
221 scsi_cmd_stack_free(int ctlr)
223 struct cciss_scsi_adapter_data_t *sa;
224 struct cciss_scsi_cmd_stack_t *stk;
227 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
228 stk = &sa->cmd_stack;
229 if (stk->top != CMD_STACK_SIZE-1) {
230 printk( "cciss: %d scsi commands are still outstanding.\n",
231 CMD_STACK_SIZE - stk->top);
233 printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);
235 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
237 pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
241 /* scsi_device_types comes from scsi.h */
242 #define DEVICETYPE(n) (n<0 || n>MAX_SCSI_DEVICE_CODE) ? \
243 "Unknown" : scsi_device_types[n]
246 static int xmargin=8;
247 static int amargin=60;
250 print_bytes (unsigned char *c, int len, int hex, int ascii)
261 if ((i % xmargin) == 0 && i>0) printk("\n");
262 if ((i % xmargin) == 0) printk("0x%04x:", i);
273 if ((i % amargin) == 0 && i>0) printk("\n");
274 if ((i % amargin) == 0) printk("0x%04x:", i);
275 if (*x > 26 && *x < 128) printk("%c", *x);
284 print_cmd(CommandList_struct *cp)
286 printk("queue:%d\n", cp->Header.ReplyQueue);
287 printk("sglist:%d\n", cp->Header.SGList);
288 printk("sgtot:%d\n", cp->Header.SGTotal);
289 printk("Tag:0x%08x/0x%08x\n", cp->Header.Tag.upper,
290 cp->Header.Tag.lower);
291 printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
292 cp->Header.LUN.LunAddrBytes[0],
293 cp->Header.LUN.LunAddrBytes[1],
294 cp->Header.LUN.LunAddrBytes[2],
295 cp->Header.LUN.LunAddrBytes[3],
296 cp->Header.LUN.LunAddrBytes[4],
297 cp->Header.LUN.LunAddrBytes[5],
298 cp->Header.LUN.LunAddrBytes[6],
299 cp->Header.LUN.LunAddrBytes[7]);
300 printk("CDBLen:%d\n", cp->Request.CDBLen);
301 printk("Type:%d\n",cp->Request.Type.Type);
302 printk("Attr:%d\n",cp->Request.Type.Attribute);
303 printk(" Dir:%d\n",cp->Request.Type.Direction);
304 printk("Timeout:%d\n",cp->Request.Timeout);
305 printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"
306 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
307 cp->Request.CDB[0], cp->Request.CDB[1],
308 cp->Request.CDB[2], cp->Request.CDB[3],
309 cp->Request.CDB[4], cp->Request.CDB[5],
310 cp->Request.CDB[6], cp->Request.CDB[7],
311 cp->Request.CDB[8], cp->Request.CDB[9],
312 cp->Request.CDB[10], cp->Request.CDB[11],
313 cp->Request.CDB[12], cp->Request.CDB[13],
314 cp->Request.CDB[14], cp->Request.CDB[15]),
315 printk("edesc.Addr: 0x%08x/0%08x, Len = %d\n",
316 cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower,
318 printk("sgs..........Errorinfo:\n");
319 printk("scsistatus:%d\n", cp->err_info->ScsiStatus);
320 printk("senselen:%d\n", cp->err_info->SenseLen);
321 printk("cmd status:%d\n", cp->err_info->CommandStatus);
322 printk("resid cnt:%d\n", cp->err_info->ResidualCnt);
323 printk("offense size:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);
324 printk("offense byte:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);
325 printk("offense value:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
332 find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
334 /* finds an unused bus, target, lun for a new device */
335 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
337 unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
339 memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
341 target_taken[SELF_SCSI_ID] = 1;
342 for (i=0;i<ccissscsi[ctlr].ndevices;i++)
343 target_taken[ccissscsi[ctlr].dev[i].target] = 1;
345 for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {
346 if (!target_taken[i]) {
347 *bus = 0; *target=i; *lun = 0; found=1;
355 cciss_scsi_add_entry(int ctlr, int hostno,
356 unsigned char *scsi3addr, int devtype)
358 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
359 int n = ccissscsi[ctlr].ndevices;
360 struct cciss_scsi_dev_t *sd;
362 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
363 printk("cciss%d: Too many devices, "
364 "some will be inaccessible.\n", ctlr);
367 sd = &ccissscsi[ctlr].dev[n];
368 if (find_bus_target_lun(ctlr, &sd->bus, &sd->target, &sd->lun) != 0)
370 memcpy(&sd->scsi3addr[0], scsi3addr, 8);
371 sd->devtype = devtype;
372 ccissscsi[ctlr].ndevices++;
374 /* initially, (before registering with scsi layer) we don't
375 know our hostno and we don't want to print anything first
376 time anyway (the scsi layer's inquiries will show that info) */
378 printk("cciss%d: %s device c%db%dt%dl%d added.\n",
379 ctlr, DEVICETYPE(sd->devtype), hostno,
380 sd->bus, sd->target, sd->lun);
385 cciss_scsi_remove_entry(int ctlr, int hostno, int entry)
387 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
389 struct cciss_scsi_dev_t sd;
391 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
392 sd = ccissscsi[ctlr].dev[entry];
393 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
394 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
395 ccissscsi[ctlr].ndevices--;
396 printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
397 ctlr, DEVICETYPE(sd.devtype), hostno,
398 sd.bus, sd.target, sd.lun);
402 #define SCSI3ADDR_EQ(a,b) ( \
403 (a)[7] == (b)[7] && \
404 (a)[6] == (b)[6] && \
405 (a)[5] == (b)[5] && \
406 (a)[4] == (b)[4] && \
407 (a)[3] == (b)[3] && \
408 (a)[2] == (b)[2] && \
409 (a)[1] == (b)[1] && \
413 adjust_cciss_scsi_table(int ctlr, int hostno,
414 struct cciss_scsi_dev_t sd[], int nsds)
416 /* sd contains scsi3 addresses and devtypes, but
417 bus target and lun are not filled in. This funciton
418 takes what's in sd to be the current and adjusts
419 ccissscsi[] to be in line with what's in sd. */
421 int i,j, found, changes=0;
422 struct cciss_scsi_dev_t *csd;
425 CPQ_TAPE_LOCK(ctlr, flags);
427 /* find any devices in ccissscsi[] that are not in
428 sd[] and remove them from ccissscsi[] */
431 while(i<ccissscsi[ctlr].ndevices) {
432 csd = &ccissscsi[ctlr].dev[i];
434 for (j=0;j<nsds;j++) {
435 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
437 if (sd[j].devtype == csd->devtype)
445 if (found == 0) { /* device no longer present. */
447 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
448 ctlr, DEVICETYPE(csd->devtype), hostno,
449 csd->bus, csd->target, csd->lun); */
450 cciss_scsi_remove_entry(ctlr, hostno, i);
451 /* note, i not incremented */
453 else if (found == 1) { /* device is different kind */
455 printk("cciss%d: device c%db%dt%dl%d type changed "
456 "(device type now %s).\n",
457 ctlr, hostno, csd->bus, csd->target, csd->lun,
458 DEVICETYPE(csd->devtype));
459 csd->devtype = sd[j].devtype;
460 i++; /* so just move along. */
461 } else /* device is same as it ever was, */
462 i++; /* so just move along. */
465 /* Now, make sure every device listed in sd[] is also
466 listed in ccissscsi[], adding them if they aren't found */
468 for (i=0;i<nsds;i++) {
470 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
471 csd = &ccissscsi[ctlr].dev[j];
472 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
474 if (sd[i].devtype == csd->devtype)
475 found=2; /* found device */
477 found=1; /* found a bug. */
483 if (cciss_scsi_add_entry(ctlr, hostno,
484 &sd[i].scsi3addr[0], sd[i].devtype) != 0)
486 } else if (found == 1) {
487 /* should never happen... */
489 printk("cciss%d: device unexpectedly changed type\n",
491 /* but if it does happen, we just ignore that device */
494 CPQ_TAPE_UNLOCK(ctlr, flags);
497 printk("cciss%d: No device changes detected.\n", ctlr);
503 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
506 struct cciss_scsi_dev_t *sd;
509 CPQ_TAPE_LOCK(ctlr, flags);
510 for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
511 sd = &ccissscsi[ctlr].dev[i];
512 if (sd->bus == bus &&
513 sd->target == target &&
515 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
516 CPQ_TAPE_UNLOCK(ctlr, flags);
520 CPQ_TAPE_UNLOCK(ctlr, flags);
525 cciss_scsi_setup(int cntl_num)
527 struct cciss_scsi_adapter_data_t * shba;
529 ccissscsi[cntl_num].ndevices = 0;
530 shba = (struct cciss_scsi_adapter_data_t *)
531 kmalloc(sizeof(*shba), GFP_KERNEL);
534 shba->scsi_host = NULL;
535 spin_lock_init(&shba->lock);
536 shba->registered = 0;
537 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
541 hba[cntl_num]->scsi_ctlr = (void *) shba;
546 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
548 struct scsi_cmnd *cmd;
551 ErrorInfo_struct *ei;
555 /* First, see if it was a message rather than a command */
556 if (cp->Request.Type.Type == TYPE_MSG) {
557 cp->cmd_type = CMD_MSG_DONE;
561 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
562 ctlr = hba[cp->ctlr];
564 /* undo the DMA mappings */
567 pci_unmap_sg(ctlr->pdev,
568 cmd->buffer, cmd->use_sg,
569 cmd->sc_data_direction);
571 else if (cmd->request_bufflen) {
572 addr64.val32.lower = cp->SG[0].Addr.lower;
573 addr64.val32.upper = cp->SG[0].Addr.upper;
574 pci_unmap_single(ctlr->pdev, (dma_addr_t) addr64.val,
575 cmd->request_bufflen,
576 cmd->sc_data_direction);
579 cmd->result = (DID_OK << 16); /* host byte */
580 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
581 /* cmd->result |= (GOOD < 1); */ /* status byte */
583 cmd->result |= (ei->ScsiStatus);
584 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
586 /* copy the sense data whether we need to or not. */
588 memcpy(cmd->sense_buffer, ei->SenseInfo,
589 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
590 SCSI_SENSE_BUFFERSIZE :
592 cmd->resid = ei->ResidualCnt;
594 if(ei->CommandStatus != 0)
595 { /* an error has occurred */
596 switch(ei->CommandStatus)
598 case CMD_TARGET_STATUS:
599 /* Pass it up to the upper layers... */
603 printk(KERN_WARNING "cciss: cmd %p "
604 "has SCSI Status = %x\n",
608 cmd->result |= (ei->ScsiStatus < 1);
610 else { /* scsi status is zero??? How??? */
612 /* Ordinarily, this case should never happen, but there is a bug
613 in some released firmware revisions that allows it to happen
614 if, for example, a 4100 backplane loses power and the tape
615 drive is in it. We assume that it's a fatal error of some
616 kind because we can't show that it wasn't. We will make it
617 look like selection timeout since that is the most common
618 reason for this to occur, and it's severe enough. */
620 cmd->result = DID_NO_CONNECT << 16;
623 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
625 case CMD_DATA_OVERRUN:
626 printk(KERN_WARNING "cciss: cp %p has"
627 " completed with data overrun "
631 /* print_bytes(cp, sizeof(*cp), 1, 0);
633 /* We get CMD_INVALID if you address a non-existent tape drive instead
634 of a selection timeout (no response). You will see this if you yank
635 out a tape drive, then try to access it. This is kind of a shame
636 because it means that any other CMD_INVALID (e.g. driver bug) will
637 get interpreted as a missing target. */
638 cmd->result = DID_NO_CONNECT << 16;
641 case CMD_PROTOCOL_ERR:
642 printk(KERN_WARNING "cciss: cp %p has "
643 "protocol error \n", cp);
645 case CMD_HARDWARE_ERR:
646 cmd->result = DID_ERROR << 16;
647 printk(KERN_WARNING "cciss: cp %p had "
648 " hardware error\n", cp);
650 case CMD_CONNECTION_LOST:
651 cmd->result = DID_ERROR << 16;
652 printk(KERN_WARNING "cciss: cp %p had "
653 "connection lost\n", cp);
656 cmd->result = DID_ABORT << 16;
657 printk(KERN_WARNING "cciss: cp %p was "
660 case CMD_ABORT_FAILED:
661 cmd->result = DID_ERROR << 16;
662 printk(KERN_WARNING "cciss: cp %p reports "
663 "abort failed\n", cp);
665 case CMD_UNSOLICITED_ABORT:
666 cmd->result = DID_ABORT << 16;
667 printk(KERN_WARNING "cciss: cp %p aborted "
668 "do to an unsolicited abort\n", cp);
671 cmd->result = DID_TIME_OUT << 16;
672 printk(KERN_WARNING "cciss: cp %p timedout\n",
676 cmd->result = DID_ERROR << 16;
677 printk(KERN_WARNING "cciss: cp %p returned "
678 "unknown status %x\n", cp,
682 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
683 // cmd->target, cmd->lun);
685 scsi_cmd_free(ctlr, cp);
689 cciss_scsi_detect(int ctlr)
691 struct Scsi_Host *sh;
694 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
697 sh->io_port = 0; // good enough? FIXME,
698 sh->n_io_port = 0; // I don't think we use these two...
699 sh->this_id = SELF_SCSI_ID;
701 ((struct cciss_scsi_adapter_data_t *)
702 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
703 sh->hostdata[0] = (unsigned long) hba[ctlr];
704 sh->irq = hba[ctlr]->intr;
705 sh->unique_id = sh->irq;
706 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
719 cciss_unmap_one(struct pci_dev *pdev,
720 CommandList_struct *cp,
726 addr64.val32.lower = cp->SG[0].Addr.lower;
727 addr64.val32.upper = cp->SG[0].Addr.upper;
728 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
732 cciss_map_one(struct pci_dev *pdev,
733 CommandList_struct *cp,
740 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
741 cp->SG[0].Addr.lower =
742 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
743 cp->SG[0].Addr.upper =
744 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
745 cp->SG[0].Len = buflen;
746 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
747 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
751 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
752 CommandList_struct *cp,
753 unsigned char *scsi3addr,
755 unsigned char cdblen,
756 unsigned char *buf, int bufsize,
760 DECLARE_COMPLETION(wait);
762 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
764 cp->Header.ReplyQueue = 0; // unused in simple mode
765 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
766 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
767 // Fill in the request block...
769 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
770 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
771 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
773 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
774 memcpy(cp->Request.CDB, cdb, cdblen);
775 cp->Request.Timeout = 0;
776 cp->Request.CDBLen = cdblen;
777 cp->Request.Type.Type = TYPE_CMD;
778 cp->Request.Type.Attribute = ATTR_SIMPLE;
779 cp->Request.Type.Direction = direction;
781 /* Fill in the SG list and do dma mapping */
782 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
783 bufsize, DMA_FROM_DEVICE);
787 /* Put the request on the tail of the request queue */
788 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
792 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
794 wait_for_completion(&wait);
796 /* undo the dma mapping */
797 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
802 cciss_scsi_interpret_error(CommandList_struct *cp)
804 ErrorInfo_struct *ei;
807 switch(ei->CommandStatus)
809 case CMD_TARGET_STATUS:
810 printk(KERN_WARNING "cciss: cmd %p has "
811 "completed with errors\n", cp);
812 printk(KERN_WARNING "cciss: cmd %p "
813 "has SCSI Status = %x\n",
816 if (ei->ScsiStatus == 0)
818 "cciss:SCSI status is abnormally zero. "
819 "(probably indicates selection timeout "
820 "reported incorrectly due to a known "
821 "firmware bug, circa July, 2001.)\n");
823 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
824 printk("UNDERRUN\n");
826 case CMD_DATA_OVERRUN:
827 printk(KERN_WARNING "cciss: cp %p has"
828 " completed with data overrun "
832 /* controller unfortunately reports SCSI passthru's */
833 /* to non-existent targets as invalid commands. */
834 printk(KERN_WARNING "cciss: cp %p is "
835 "reported invalid (probably means "
836 "target device no longer present)\n",
838 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
842 case CMD_PROTOCOL_ERR:
843 printk(KERN_WARNING "cciss: cp %p has "
844 "protocol error \n", cp);
846 case CMD_HARDWARE_ERR:
847 /* cmd->result = DID_ERROR << 16; */
848 printk(KERN_WARNING "cciss: cp %p had "
849 " hardware error\n", cp);
851 case CMD_CONNECTION_LOST:
852 printk(KERN_WARNING "cciss: cp %p had "
853 "connection lost\n", cp);
856 printk(KERN_WARNING "cciss: cp %p was "
859 case CMD_ABORT_FAILED:
860 printk(KERN_WARNING "cciss: cp %p reports "
861 "abort failed\n", cp);
863 case CMD_UNSOLICITED_ABORT:
864 printk(KERN_WARNING "cciss: cp %p aborted "
865 "do to an unsolicited abort\n", cp);
868 printk(KERN_WARNING "cciss: cp %p timedout\n",
872 printk(KERN_WARNING "cciss: cp %p returned "
873 "unknown status %x\n", cp,
879 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
880 InquiryData_struct *buf)
883 CommandList_struct *cp;
885 ErrorInfo_struct *ei;
888 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
889 cp = scsi_cmd_alloc(c);
890 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
892 if (cp == NULL) { /* trouble... */
893 printk("cmd_alloc returned NULL!\n");
899 cdb[0] = CISS_INQUIRY;
903 cdb[4] = sizeof(*buf) & 0xff;
905 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
906 6, (unsigned char *) buf,
907 sizeof(*buf), XFER_READ);
909 if (rc != 0) return rc; /* something went wrong */
911 if (ei->CommandStatus != 0 &&
912 ei->CommandStatus != CMD_DATA_UNDERRUN) {
913 cciss_scsi_interpret_error(cp);
916 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
917 scsi_cmd_free(c, cp);
918 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
923 cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
924 ReportLunData_struct *buf, int bufsize)
927 CommandList_struct *cp;
928 unsigned char cdb[12];
929 unsigned char scsi3addr[8];
930 ErrorInfo_struct *ei;
933 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
934 cp = scsi_cmd_alloc(c);
935 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
936 if (cp == NULL) { /* trouble... */
937 printk("cmd_alloc returned NULL!\n");
941 memset(&scsi3addr[0], 0, 8); /* address the controller */
942 cdb[0] = CISS_REPORT_PHYS;
948 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
949 cdb[7] = (bufsize >> 16) & 0xFF;
950 cdb[8] = (bufsize >> 8) & 0xFF;
951 cdb[9] = bufsize & 0xFF;
955 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
957 (unsigned char *) buf,
960 if (rc != 0) return rc; /* something went wrong */
963 if (ei->CommandStatus != 0 &&
964 ei->CommandStatus != CMD_DATA_UNDERRUN) {
965 cciss_scsi_interpret_error(cp);
968 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
969 scsi_cmd_free(c, cp);
970 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
975 cciss_update_non_disk_devices(int cntl_num, int hostno)
977 /* the idea here is we could get notified from /proc
978 that some devices have changed, so we do a report
979 physical luns cmd, and adjust our list of devices
980 accordingly. (We can't rely on the scsi-mid layer just
981 doing inquiries, because the "busses" that the scsi
982 mid-layer probes are totally fabricated by this driver,
983 so new devices wouldn't show up.
985 the scsi3addr's of devices won't change so long as the
986 adapter is not reset. That means we can rescan and
987 tell which devices we already know about, vs. new
988 devices, vs. disappearing devices.
990 Also, if you yank out a tape drive, then put in a disk
991 in it's place, (say, a configured volume from another
992 array controller for instance) _don't_ poke this driver
993 (so it thinks it's still a tape, but _do_ poke the scsi
994 mid layer, so it does an inquiry... the scsi mid layer
995 will see the physical disk. This would be bad. Need to
996 think about how to prevent that. One idea would be to
997 snoop all scsi responses and if an inquiry repsonse comes
998 back that reports a disk, chuck it an return selection
999 timeout instead and adjust our table... Not sure i like
1004 ReportLunData_struct *ld_buff;
1005 InquiryData_struct *inq_buff;
1006 unsigned char scsi3addr[8];
1010 /* unsigned char found[CCISS_MAX_SCSI_DEVS_PER_HBA]; */
1011 struct cciss_scsi_dev_t currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1013 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1016 c = (ctlr_info_t *) hba[cntl_num];
1017 ld_buff = kmalloc(reportlunsize, GFP_KERNEL);
1018 if (ld_buff == NULL) {
1019 printk(KERN_ERR "cciss: out of memory\n");
1022 memset(ld_buff, 0, reportlunsize);
1023 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1024 if (inq_buff == NULL) {
1025 printk(KERN_ERR "cciss: out of memory\n");
1030 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1031 ch = &ld_buff->LUNListLength[0];
1032 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1033 if (num_luns > CISS_MAX_PHYS_LUN) {
1035 "cciss: Maximum physical LUNs (%d) exceeded. "
1036 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1037 num_luns - CISS_MAX_PHYS_LUN);
1038 num_luns = CISS_MAX_PHYS_LUN;
1042 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1047 /* adjust our table of devices */
1048 for(i=0; i<num_luns; i++)
1052 /* for each physical lun, do an inquiry */
1053 if (ld_buff->LUN[i][3] & 0xC0) continue;
1054 memset(inq_buff, 0, sizeof(InquiryData_struct));
1055 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1057 if (cciss_scsi_do_inquiry(hba[cntl_num],
1058 scsi3addr, inq_buff) != 0)
1060 /* Inquiry failed (msg printed already) */
1061 devtype = 0; /* so we will skip this device. */
1062 } else /* what kind of device is this? */
1063 devtype = (inq_buff->data_byte[0] & 0x1f);
1067 case 0x01: /* sequential access, (tape) */
1068 case 0x08: /* medium changer */
1069 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1070 printk(KERN_INFO "cciss%d: %s ignored, "
1071 "too many devices.\n", cntl_num,
1072 DEVICETYPE(devtype));
1075 memcpy(¤tsd[ncurrent].scsi3addr[0],
1077 currentsd[ncurrent].devtype = devtype;
1078 currentsd[ncurrent].bus = -1;
1079 currentsd[ncurrent].target = -1;
1080 currentsd[ncurrent].lun = -1;
1088 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1096 is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1098 int verb_len = strlen(verb);
1099 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1106 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1110 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1111 cciss_update_non_disk_devices(ctlr, hostno);
1119 cciss_scsi_proc_info(struct Scsi_Host *sh,
1120 char *buffer, /* data buffer */
1121 char **start, /* where data in buffer starts */
1122 off_t offset, /* offset from start of imaginary file */
1123 int length, /* length of data in buffer */
1124 int func) /* 0 == read, 1 == write */
1127 int buflen, datalen;
1132 ci = (ctlr_info_t *) sh->hostdata[0];
1133 if (ci == NULL) /* This really shouldn't ever happen. */
1136 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1138 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1139 buflen = sprintf(buffer, "hostnum=%d\n", sh->host_no);
1141 datalen = buflen - offset;
1142 if (datalen < 0) { /* they're reading past EOF. */
1144 *start = buffer+buflen;
1146 *start = buffer + offset;
1148 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1149 return cciss_scsi_user_command(cntl_num, sh->host_no,
1153 /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1154 dma mapping and fills in the scatter gather entries of the
1155 cciss command, cp. */
1158 cciss_scatter_gather(struct pci_dev *pdev,
1159 CommandList_struct *cp,
1160 struct scsi_cmnd *cmd)
1162 unsigned int use_sg, nsegs=0, len;
1163 struct scatterlist *scatter = (struct scatterlist *) cmd->buffer;
1166 /* is it just one virtual address? */
1168 if (cmd->request_bufflen) { /* anything to xfer? */
1170 addr64 = (__u64) pci_map_single(pdev,
1171 cmd->request_buffer,
1172 cmd->request_bufflen,
1173 cmd->sc_data_direction);
1175 cp->SG[0].Addr.lower =
1176 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1177 cp->SG[0].Addr.upper =
1178 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1179 cp->SG[0].Len = cmd->request_bufflen;
1182 } /* else, must be a list of virtual addresses.... */
1183 else if (cmd->use_sg <= MAXSGENTRIES) { /* not too many addrs? */
1185 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg,
1186 cmd->sc_data_direction);
1188 for (nsegs=0; nsegs < use_sg; nsegs++) {
1189 addr64 = (__u64) sg_dma_address(&scatter[nsegs]);
1190 len = sg_dma_len(&scatter[nsegs]);
1191 cp->SG[nsegs].Addr.lower =
1192 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1193 cp->SG[nsegs].Addr.upper =
1194 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1195 cp->SG[nsegs].Len = len;
1196 cp->SG[nsegs].Ext = 0; // we are not chaining
1200 cp->Header.SGList = (__u8) nsegs; /* no. SGs contig in this cmd */
1201 cp->Header.SGTotal = (__u16) nsegs; /* total sgs in this cmd list */
1207 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1211 unsigned char scsi3addr[8];
1212 CommandList_struct *cp;
1213 unsigned long flags;
1215 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1216 // We violate cmd->host privacy here. (Is there another way?)
1217 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1220 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1221 cmd->device->lun, scsi3addr);
1223 /* the scsi nexus does not match any that we presented... */
1224 /* pretend to mid layer that we got selection timeout */
1225 cmd->result = DID_NO_CONNECT << 16;
1227 /* we might want to think about registering controller itself
1228 as a processor device on the bus so sg binds to it. */
1232 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1233 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1234 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1235 // cmd->target, cmd->lun);
1237 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1238 see what the device thinks of it. */
1240 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1241 cp = scsi_cmd_alloc(*c);
1242 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1243 if (cp == NULL) { /* trouble... */
1244 printk("scsi_cmd_alloc returned NULL!\n");
1245 /* FIXME: next 3 lines are -> BAD! <- */
1246 cmd->result = DID_NO_CONNECT << 16;
1251 // Fill in the command list header
1253 cmd->scsi_done = done; // save this for use by completion code
1255 // save cp in case we have to abort it
1256 cmd->host_scribble = (unsigned char *) cp;
1258 cp->cmd_type = CMD_SCSI;
1260 cp->Header.ReplyQueue = 0; // unused in simple mode
1261 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1262 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1264 // Fill in the request block...
1266 cp->Request.Timeout = 0;
1267 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
1268 if (cmd->cmd_len > sizeof(cp->Request.CDB)) BUG();
1269 cp->Request.CDBLen = cmd->cmd_len;
1270 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1271 cp->Request.Type.Type = TYPE_CMD;
1272 cp->Request.Type.Attribute = ATTR_SIMPLE;
1273 switch(cmd->sc_data_direction)
1275 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1276 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1277 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1278 case DMA_BIDIRECTIONAL:
1279 // This can happen if a buggy application does a scsi passthru
1280 // and sets both inlen and outlen to non-zero. ( see
1281 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1283 cp->Request.Type.Direction = XFER_RSVD;
1284 // This is technically wrong, and cciss controllers should
1285 // reject it with CMD_INVALID, which is the most correct
1286 // response, but non-fibre backends appear to let it
1287 // slide by, and give the same results as if this field
1288 // were set correctly. Either way is acceptable for
1289 // our purposes here.
1294 printk("cciss: unknown data direction: %d\n",
1295 cmd->sc_data_direction);
1300 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1302 /* Put the request on the tail of the request queue */
1304 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1305 addQ(&(*c)->reqQ, cp);
1308 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1310 /* the cmd'll come back via intr handler in complete_scsi_command() */
1315 cciss_unregister_scsi(int ctlr)
1317 struct cciss_scsi_adapter_data_t *sa;
1318 struct cciss_scsi_cmd_stack_t *stk;
1319 unsigned long flags;
1321 /* we are being forcibly unloaded, and may not refuse. */
1323 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1324 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1325 stk = &sa->cmd_stack;
1327 /* if we weren't ever actually registered, don't unregister */
1328 if (sa->registered) {
1329 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1330 scsi_remove_host(sa->scsi_host);
1331 scsi_host_put(sa->scsi_host);
1332 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1335 /* set scsi_host to NULL so our detect routine will
1336 find us on register */
1337 sa->scsi_host = NULL;
1338 scsi_cmd_stack_free(ctlr);
1340 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1344 cciss_register_scsi(int ctlr)
1346 unsigned long flags;
1348 CPQ_TAPE_LOCK(ctlr, flags);
1350 /* Since this is really a block driver, the SCSI core may not be
1351 initialized at init time, in which case, calling scsi_register_host
1352 would hang. Instead, we do it later, via /proc filesystem
1353 and rc scripts, when we know SCSI core is good to go. */
1355 /* Only register if SCSI devices are detected. */
1356 if (ccissscsi[ctlr].ndevices != 0) {
1357 ((struct cciss_scsi_adapter_data_t *)
1358 hba[ctlr]->scsi_ctlr)->registered = 1;
1359 CPQ_TAPE_UNLOCK(ctlr, flags);
1360 return cciss_scsi_detect(ctlr);
1362 CPQ_TAPE_UNLOCK(ctlr, flags);
1364 "cciss%d: No appropriate SCSI device detected, "
1365 "SCSI subsystem not engaged.\n", ctlr);
1370 cciss_engage_scsi(int ctlr)
1372 struct cciss_scsi_adapter_data_t *sa;
1373 struct cciss_scsi_cmd_stack_t *stk;
1374 unsigned long flags;
1376 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1377 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1378 stk = &sa->cmd_stack;
1380 if (((struct cciss_scsi_adapter_data_t *)
1381 hba[ctlr]->scsi_ctlr)->registered) {
1382 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1383 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1386 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1387 cciss_update_non_disk_devices(ctlr, -1);
1388 cciss_register_scsi(ctlr);
1393 cciss_proc_tape_report(int ctlr, unsigned char *buffer, off_t *pos, off_t *len)
1395 unsigned long flags;
1398 *pos = *pos -1; *len = *len - 1; // cut off the last trailing newline
1400 CPQ_TAPE_LOCK(ctlr, flags);
1401 size = sprintf(buffer + *len,
1402 " Sequential access devices: %d\n\n",
1403 ccissscsi[ctlr].ndevices);
1404 CPQ_TAPE_UNLOCK(ctlr, flags);
1405 *pos += size; *len += size;
1408 #else /* no CONFIG_CISS_SCSI_TAPE */
1410 /* If no tape support, then these become defined out of existence */
1412 #define cciss_scsi_setup(cntl_num)
1413 #define cciss_unregister_scsi(ctlr)
1414 #define cciss_register_scsi(ctlr)
1415 #define cciss_proc_tape_report(ctlr, buffer, pos, len)
1417 #endif /* CONFIG_CISS_SCSI_TAPE */