PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / block / cciss_scsi.c
blobe4bf9a11ca0d0b1c0cc6a619b4d1fa59bedd5508
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);
362 static int
363 cciss_scsi_add_entry(int ctlr, int hostno,
364 unsigned char *scsi3addr, int devtype)
366 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
367 int n = ccissscsi[ctlr].ndevices;
368 struct cciss_scsi_dev_t *sd;
370 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
371 printk("cciss%d: Too many devices, "
372 "some will be inaccessible.\n", ctlr);
373 return -1;
375 sd = &ccissscsi[ctlr].dev[n];
376 if (find_bus_target_lun(ctlr, &sd->bus, &sd->target, &sd->lun) != 0)
377 return -1;
378 memcpy(&sd->scsi3addr[0], scsi3addr, 8);
379 sd->devtype = devtype;
380 ccissscsi[ctlr].ndevices++;
382 /* initially, (before registering with scsi layer) we don't
383 know our hostno and we don't want to print anything first
384 time anyway (the scsi layer's inquiries will show that info) */
385 if (hostno != -1)
386 printk("cciss%d: %s device c%db%dt%dl%d added.\n",
387 ctlr, scsi_device_type(sd->devtype), hostno,
388 sd->bus, sd->target, sd->lun);
389 return 0;
392 static void
393 cciss_scsi_remove_entry(int ctlr, int hostno, int entry)
395 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
396 int i;
397 struct cciss_scsi_dev_t sd;
399 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
400 sd = ccissscsi[ctlr].dev[entry];
401 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
402 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
403 ccissscsi[ctlr].ndevices--;
404 printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
405 ctlr, scsi_device_type(sd.devtype), hostno,
406 sd.bus, sd.target, sd.lun);
410 #define SCSI3ADDR_EQ(a,b) ( \
411 (a)[7] == (b)[7] && \
412 (a)[6] == (b)[6] && \
413 (a)[5] == (b)[5] && \
414 (a)[4] == (b)[4] && \
415 (a)[3] == (b)[3] && \
416 (a)[2] == (b)[2] && \
417 (a)[1] == (b)[1] && \
418 (a)[0] == (b)[0])
420 static int
421 adjust_cciss_scsi_table(int ctlr, int hostno,
422 struct cciss_scsi_dev_t sd[], int nsds)
424 /* sd contains scsi3 addresses and devtypes, but
425 bus target and lun are not filled in. This funciton
426 takes what's in sd to be the current and adjusts
427 ccissscsi[] to be in line with what's in sd. */
429 int i,j, found, changes=0;
430 struct cciss_scsi_dev_t *csd;
431 unsigned long flags;
433 CPQ_TAPE_LOCK(ctlr, flags);
435 /* find any devices in ccissscsi[] that are not in
436 sd[] and remove them from ccissscsi[] */
438 i = 0;
439 while(i<ccissscsi[ctlr].ndevices) {
440 csd = &ccissscsi[ctlr].dev[i];
441 found=0;
442 for (j=0;j<nsds;j++) {
443 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
444 csd->scsi3addr)) {
445 if (sd[j].devtype == csd->devtype)
446 found=2;
447 else
448 found=1;
449 break;
453 if (found == 0) { /* device no longer present. */
454 changes++;
455 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
456 ctlr, scsi_device_type(csd->devtype), hostno,
457 csd->bus, csd->target, csd->lun); */
458 cciss_scsi_remove_entry(ctlr, hostno, i);
459 /* note, i not incremented */
461 else if (found == 1) { /* device is different kind */
462 changes++;
463 printk("cciss%d: device c%db%dt%dl%d type changed "
464 "(device type now %s).\n",
465 ctlr, hostno, csd->bus, csd->target, csd->lun,
466 scsi_device_type(csd->devtype));
467 csd->devtype = sd[j].devtype;
468 i++; /* so just move along. */
469 } else /* device is same as it ever was, */
470 i++; /* so just move along. */
473 /* Now, make sure every device listed in sd[] is also
474 listed in ccissscsi[], adding them if they aren't found */
476 for (i=0;i<nsds;i++) {
477 found=0;
478 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
479 csd = &ccissscsi[ctlr].dev[j];
480 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
481 csd->scsi3addr)) {
482 if (sd[i].devtype == csd->devtype)
483 found=2; /* found device */
484 else
485 found=1; /* found a bug. */
486 break;
489 if (!found) {
490 changes++;
491 if (cciss_scsi_add_entry(ctlr, hostno,
492 &sd[i].scsi3addr[0], sd[i].devtype) != 0)
493 break;
494 } else if (found == 1) {
495 /* should never happen... */
496 changes++;
497 printk("cciss%d: device unexpectedly changed type\n",
498 ctlr);
499 /* but if it does happen, we just ignore that device */
502 CPQ_TAPE_UNLOCK(ctlr, flags);
504 if (!changes)
505 printk("cciss%d: No device changes detected.\n", ctlr);
507 return 0;
510 static int
511 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
513 int i;
514 struct cciss_scsi_dev_t *sd;
515 unsigned long flags;
517 CPQ_TAPE_LOCK(ctlr, flags);
518 for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
519 sd = &ccissscsi[ctlr].dev[i];
520 if (sd->bus == bus &&
521 sd->target == target &&
522 sd->lun == lun) {
523 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
524 CPQ_TAPE_UNLOCK(ctlr, flags);
525 return 0;
528 CPQ_TAPE_UNLOCK(ctlr, flags);
529 return -1;
532 static void
533 cciss_scsi_setup(int cntl_num)
535 struct cciss_scsi_adapter_data_t * shba;
537 ccissscsi[cntl_num].ndevices = 0;
538 shba = (struct cciss_scsi_adapter_data_t *)
539 kmalloc(sizeof(*shba), GFP_KERNEL);
540 if (shba == NULL)
541 return;
542 shba->scsi_host = NULL;
543 spin_lock_init(&shba->lock);
544 shba->registered = 0;
545 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
546 kfree(shba);
547 shba = NULL;
549 hba[cntl_num]->scsi_ctlr = (void *) shba;
550 return;
553 static void
554 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
556 struct scsi_cmnd *cmd;
557 ctlr_info_t *ctlr;
558 ErrorInfo_struct *ei;
560 ei = cp->err_info;
562 /* First, see if it was a message rather than a command */
563 if (cp->Request.Type.Type == TYPE_MSG) {
564 cp->cmd_type = CMD_MSG_DONE;
565 return;
568 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
569 ctlr = hba[cp->ctlr];
571 scsi_dma_unmap(cmd);
573 cmd->result = (DID_OK << 16); /* host byte */
574 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
575 /* cmd->result |= (GOOD < 1); */ /* status byte */
577 cmd->result |= (ei->ScsiStatus);
578 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
580 /* copy the sense data whether we need to or not. */
582 memcpy(cmd->sense_buffer, ei->SenseInfo,
583 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
584 SCSI_SENSE_BUFFERSIZE :
585 ei->SenseLen);
586 scsi_set_resid(cmd, ei->ResidualCnt);
588 if(ei->CommandStatus != 0)
589 { /* an error has occurred */
590 switch(ei->CommandStatus)
592 case CMD_TARGET_STATUS:
593 /* Pass it up to the upper layers... */
594 if( ei->ScsiStatus)
596 #if 0
597 printk(KERN_WARNING "cciss: cmd %p "
598 "has SCSI Status = %x\n",
599 cp,
600 ei->ScsiStatus);
601 #endif
602 cmd->result |= (ei->ScsiStatus < 1);
604 else { /* scsi status is zero??? How??? */
606 /* Ordinarily, this case should never happen, but there is a bug
607 in some released firmware revisions that allows it to happen
608 if, for example, a 4100 backplane loses power and the tape
609 drive is in it. We assume that it's a fatal error of some
610 kind because we can't show that it wasn't. We will make it
611 look like selection timeout since that is the most common
612 reason for this to occur, and it's severe enough. */
614 cmd->result = DID_NO_CONNECT << 16;
616 break;
617 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
618 break;
619 case CMD_DATA_OVERRUN:
620 printk(KERN_WARNING "cciss: cp %p has"
621 " completed with data overrun "
622 "reported\n", cp);
623 break;
624 case CMD_INVALID: {
625 /* print_bytes(cp, sizeof(*cp), 1, 0);
626 print_cmd(cp); */
627 /* We get CMD_INVALID if you address a non-existent tape drive instead
628 of a selection timeout (no response). You will see this if you yank
629 out a tape drive, then try to access it. This is kind of a shame
630 because it means that any other CMD_INVALID (e.g. driver bug) will
631 get interpreted as a missing target. */
632 cmd->result = DID_NO_CONNECT << 16;
634 break;
635 case CMD_PROTOCOL_ERR:
636 printk(KERN_WARNING "cciss: cp %p has "
637 "protocol error \n", cp);
638 break;
639 case CMD_HARDWARE_ERR:
640 cmd->result = DID_ERROR << 16;
641 printk(KERN_WARNING "cciss: cp %p had "
642 " hardware error\n", cp);
643 break;
644 case CMD_CONNECTION_LOST:
645 cmd->result = DID_ERROR << 16;
646 printk(KERN_WARNING "cciss: cp %p had "
647 "connection lost\n", cp);
648 break;
649 case CMD_ABORTED:
650 cmd->result = DID_ABORT << 16;
651 printk(KERN_WARNING "cciss: cp %p was "
652 "aborted\n", cp);
653 break;
654 case CMD_ABORT_FAILED:
655 cmd->result = DID_ERROR << 16;
656 printk(KERN_WARNING "cciss: cp %p reports "
657 "abort failed\n", cp);
658 break;
659 case CMD_UNSOLICITED_ABORT:
660 cmd->result = DID_ABORT << 16;
661 printk(KERN_WARNING "cciss: cp %p aborted "
662 "do to an unsolicited abort\n", cp);
663 break;
664 case CMD_TIMEOUT:
665 cmd->result = DID_TIME_OUT << 16;
666 printk(KERN_WARNING "cciss: cp %p timedout\n",
667 cp);
668 break;
669 default:
670 cmd->result = DID_ERROR << 16;
671 printk(KERN_WARNING "cciss: cp %p returned "
672 "unknown status %x\n", cp,
673 ei->CommandStatus);
676 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
677 // cmd->target, cmd->lun);
678 cmd->scsi_done(cmd);
679 scsi_cmd_free(ctlr, cp);
682 static int
683 cciss_scsi_detect(int ctlr)
685 struct Scsi_Host *sh;
686 int error;
688 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
689 if (sh == NULL)
690 goto fail;
691 sh->io_port = 0; // good enough? FIXME,
692 sh->n_io_port = 0; // I don't think we use these two...
693 sh->this_id = SELF_SCSI_ID;
695 ((struct cciss_scsi_adapter_data_t *)
696 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
697 sh->hostdata[0] = (unsigned long) hba[ctlr];
698 sh->irq = hba[ctlr]->intr[SIMPLE_MODE_INT];
699 sh->unique_id = sh->irq;
700 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
701 if (error)
702 goto fail_host_put;
703 scsi_scan_host(sh);
704 return 1;
706 fail_host_put:
707 scsi_host_put(sh);
708 fail:
709 return 0;
712 static void
713 cciss_unmap_one(struct pci_dev *pdev,
714 CommandList_struct *cp,
715 size_t buflen,
716 int data_direction)
718 u64bit addr64;
720 addr64.val32.lower = cp->SG[0].Addr.lower;
721 addr64.val32.upper = cp->SG[0].Addr.upper;
722 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
725 static void
726 cciss_map_one(struct pci_dev *pdev,
727 CommandList_struct *cp,
728 unsigned char *buf,
729 size_t buflen,
730 int data_direction)
732 __u64 addr64;
734 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
735 cp->SG[0].Addr.lower =
736 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
737 cp->SG[0].Addr.upper =
738 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
739 cp->SG[0].Len = buflen;
740 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
741 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
744 static int
745 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
746 CommandList_struct *cp,
747 unsigned char *scsi3addr,
748 unsigned char *cdb,
749 unsigned char cdblen,
750 unsigned char *buf, int bufsize,
751 int direction)
753 unsigned long flags;
754 DECLARE_COMPLETION_ONSTACK(wait);
756 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
757 cp->scsi_cmd = NULL;
758 cp->Header.ReplyQueue = 0; // unused in simple mode
759 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
760 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
761 // Fill in the request block...
763 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
764 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
765 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
767 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
768 memcpy(cp->Request.CDB, cdb, cdblen);
769 cp->Request.Timeout = 0;
770 cp->Request.CDBLen = cdblen;
771 cp->Request.Type.Type = TYPE_CMD;
772 cp->Request.Type.Attribute = ATTR_SIMPLE;
773 cp->Request.Type.Direction = direction;
775 /* Fill in the SG list and do dma mapping */
776 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
777 bufsize, DMA_FROM_DEVICE);
779 cp->waiting = &wait;
781 /* Put the request on the tail of the request queue */
782 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
783 addQ(&c->reqQ, cp);
784 c->Qdepth++;
785 start_io(c);
786 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
788 wait_for_completion(&wait);
790 /* undo the dma mapping */
791 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
792 return(0);
795 static void
796 cciss_scsi_interpret_error(CommandList_struct *cp)
798 ErrorInfo_struct *ei;
800 ei = cp->err_info;
801 switch(ei->CommandStatus)
803 case CMD_TARGET_STATUS:
804 printk(KERN_WARNING "cciss: cmd %p has "
805 "completed with errors\n", cp);
806 printk(KERN_WARNING "cciss: cmd %p "
807 "has SCSI Status = %x\n",
808 cp,
809 ei->ScsiStatus);
810 if (ei->ScsiStatus == 0)
811 printk(KERN_WARNING
812 "cciss:SCSI status is abnormally zero. "
813 "(probably indicates selection timeout "
814 "reported incorrectly due to a known "
815 "firmware bug, circa July, 2001.)\n");
816 break;
817 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
818 printk("UNDERRUN\n");
819 break;
820 case CMD_DATA_OVERRUN:
821 printk(KERN_WARNING "cciss: cp %p has"
822 " completed with data overrun "
823 "reported\n", cp);
824 break;
825 case CMD_INVALID: {
826 /* controller unfortunately reports SCSI passthru's */
827 /* to non-existent targets as invalid commands. */
828 printk(KERN_WARNING "cciss: cp %p is "
829 "reported invalid (probably means "
830 "target device no longer present)\n",
831 cp);
832 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
833 print_cmd(cp); */
835 break;
836 case CMD_PROTOCOL_ERR:
837 printk(KERN_WARNING "cciss: cp %p has "
838 "protocol error \n", cp);
839 break;
840 case CMD_HARDWARE_ERR:
841 /* cmd->result = DID_ERROR << 16; */
842 printk(KERN_WARNING "cciss: cp %p had "
843 " hardware error\n", cp);
844 break;
845 case CMD_CONNECTION_LOST:
846 printk(KERN_WARNING "cciss: cp %p had "
847 "connection lost\n", cp);
848 break;
849 case CMD_ABORTED:
850 printk(KERN_WARNING "cciss: cp %p was "
851 "aborted\n", cp);
852 break;
853 case CMD_ABORT_FAILED:
854 printk(KERN_WARNING "cciss: cp %p reports "
855 "abort failed\n", cp);
856 break;
857 case CMD_UNSOLICITED_ABORT:
858 printk(KERN_WARNING "cciss: cp %p aborted "
859 "do to an unsolicited abort\n", cp);
860 break;
861 case CMD_TIMEOUT:
862 printk(KERN_WARNING "cciss: cp %p timedout\n",
863 cp);
864 break;
865 default:
866 printk(KERN_WARNING "cciss: cp %p returned "
867 "unknown status %x\n", cp,
868 ei->CommandStatus);
872 static int
873 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
874 unsigned char *buf, unsigned char bufsize)
876 int rc;
877 CommandList_struct *cp;
878 char cdb[6];
879 ErrorInfo_struct *ei;
880 unsigned long flags;
882 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
883 cp = scsi_cmd_alloc(c);
884 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
886 if (cp == NULL) { /* trouble... */
887 printk("cmd_alloc returned NULL!\n");
888 return -1;
891 ei = cp->err_info;
893 cdb[0] = CISS_INQUIRY;
894 cdb[1] = 0;
895 cdb[2] = 0;
896 cdb[3] = 0;
897 cdb[4] = bufsize;
898 cdb[5] = 0;
899 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
900 6, buf, bufsize, XFER_READ);
902 if (rc != 0) return rc; /* something went wrong */
904 if (ei->CommandStatus != 0 &&
905 ei->CommandStatus != CMD_DATA_UNDERRUN) {
906 cciss_scsi_interpret_error(cp);
907 rc = -1;
909 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
910 scsi_cmd_free(c, cp);
911 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
912 return rc;
915 static int
916 cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
917 ReportLunData_struct *buf, int bufsize)
919 int rc;
920 CommandList_struct *cp;
921 unsigned char cdb[12];
922 unsigned char scsi3addr[8];
923 ErrorInfo_struct *ei;
924 unsigned long flags;
926 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
927 cp = scsi_cmd_alloc(c);
928 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
929 if (cp == NULL) { /* trouble... */
930 printk("cmd_alloc returned NULL!\n");
931 return -1;
934 memset(&scsi3addr[0], 0, 8); /* address the controller */
935 cdb[0] = CISS_REPORT_PHYS;
936 cdb[1] = 0;
937 cdb[2] = 0;
938 cdb[3] = 0;
939 cdb[4] = 0;
940 cdb[5] = 0;
941 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
942 cdb[7] = (bufsize >> 16) & 0xFF;
943 cdb[8] = (bufsize >> 8) & 0xFF;
944 cdb[9] = bufsize & 0xFF;
945 cdb[10] = 0;
946 cdb[11] = 0;
948 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
949 cdb, 12,
950 (unsigned char *) buf,
951 bufsize, XFER_READ);
953 if (rc != 0) return rc; /* something went wrong */
955 ei = cp->err_info;
956 if (ei->CommandStatus != 0 &&
957 ei->CommandStatus != CMD_DATA_UNDERRUN) {
958 cciss_scsi_interpret_error(cp);
959 rc = -1;
961 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
962 scsi_cmd_free(c, cp);
963 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
964 return rc;
967 static void
968 cciss_update_non_disk_devices(int cntl_num, int hostno)
970 /* the idea here is we could get notified from /proc
971 that some devices have changed, so we do a report
972 physical luns cmd, and adjust our list of devices
973 accordingly. (We can't rely on the scsi-mid layer just
974 doing inquiries, because the "busses" that the scsi
975 mid-layer probes are totally fabricated by this driver,
976 so new devices wouldn't show up.
978 the scsi3addr's of devices won't change so long as the
979 adapter is not reset. That means we can rescan and
980 tell which devices we already know about, vs. new
981 devices, vs. disappearing devices.
983 Also, if you yank out a tape drive, then put in a disk
984 in it's place, (say, a configured volume from another
985 array controller for instance) _don't_ poke this driver
986 (so it thinks it's still a tape, but _do_ poke the scsi
987 mid layer, so it does an inquiry... the scsi mid layer
988 will see the physical disk. This would be bad. Need to
989 think about how to prevent that. One idea would be to
990 snoop all scsi responses and if an inquiry repsonse comes
991 back that reports a disk, chuck it an return selection
992 timeout instead and adjust our table... Not sure i like
993 that though.
996 #define OBDR_TAPE_INQ_SIZE 49
997 #define OBDR_TAPE_SIG "$DR-10"
998 ReportLunData_struct *ld_buff;
999 unsigned char *inq_buff;
1000 unsigned char scsi3addr[8];
1001 ctlr_info_t *c;
1002 __u32 num_luns=0;
1003 unsigned char *ch;
1004 /* unsigned char found[CCISS_MAX_SCSI_DEVS_PER_HBA]; */
1005 struct cciss_scsi_dev_t currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1006 int ncurrent=0;
1007 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1008 int i;
1010 c = (ctlr_info_t *) hba[cntl_num];
1011 ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
1012 if (ld_buff == NULL) {
1013 printk(KERN_ERR "cciss: out of memory\n");
1014 return;
1016 inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1017 if (inq_buff == NULL) {
1018 printk(KERN_ERR "cciss: out of memory\n");
1019 kfree(ld_buff);
1020 return;
1023 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1024 ch = &ld_buff->LUNListLength[0];
1025 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1026 if (num_luns > CISS_MAX_PHYS_LUN) {
1027 printk(KERN_WARNING
1028 "cciss: Maximum physical LUNs (%d) exceeded. "
1029 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1030 num_luns - CISS_MAX_PHYS_LUN);
1031 num_luns = CISS_MAX_PHYS_LUN;
1034 else {
1035 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1036 goto out;
1040 /* adjust our table of devices */
1041 for(i=0; i<num_luns; i++)
1043 int devtype;
1045 /* for each physical lun, do an inquiry */
1046 if (ld_buff->LUN[i][3] & 0xC0) continue;
1047 memset(inq_buff, 0, OBDR_TAPE_INQ_SIZE);
1048 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1050 if (cciss_scsi_do_inquiry(hba[cntl_num], scsi3addr, inq_buff,
1051 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1052 /* Inquiry failed (msg printed already) */
1053 devtype = 0; /* so we will skip this device. */
1054 } else /* what kind of device is this? */
1055 devtype = (inq_buff[0] & 0x1f);
1057 switch (devtype)
1059 case 0x05: /* CD-ROM */ {
1061 /* We don't *really* support actual CD-ROM devices,
1062 * just this "One Button Disaster Recovery" tape drive
1063 * which temporarily pretends to be a CD-ROM drive.
1064 * So we check that the device is really an OBDR tape
1065 * device by checking for "$DR-10" in bytes 43-48 of
1066 * the inquiry data.
1068 char obdr_sig[7];
1070 strncpy(obdr_sig, &inq_buff[43], 6);
1071 obdr_sig[6] = '\0';
1072 if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
1073 /* Not OBDR device, ignore it. */
1074 break;
1076 /* fall through . . . */
1077 case 0x01: /* sequential access, (tape) */
1078 case 0x08: /* medium changer */
1079 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1080 printk(KERN_INFO "cciss%d: %s ignored, "
1081 "too many devices.\n", cntl_num,
1082 scsi_device_type(devtype));
1083 break;
1085 memcpy(&currentsd[ncurrent].scsi3addr[0],
1086 &scsi3addr[0], 8);
1087 currentsd[ncurrent].devtype = devtype;
1088 currentsd[ncurrent].bus = -1;
1089 currentsd[ncurrent].target = -1;
1090 currentsd[ncurrent].lun = -1;
1091 ncurrent++;
1092 break;
1093 default:
1094 break;
1098 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1099 out:
1100 kfree(inq_buff);
1101 kfree(ld_buff);
1102 return;
1105 static int
1106 is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1108 int verb_len = strlen(verb);
1109 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1110 return verb_len;
1111 else
1112 return 0;
1115 static int
1116 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1118 int arg_len;
1120 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1121 cciss_update_non_disk_devices(ctlr, hostno);
1122 else
1123 return -EINVAL;
1124 return length;
1128 static int
1129 cciss_scsi_proc_info(struct Scsi_Host *sh,
1130 char *buffer, /* data buffer */
1131 char **start, /* where data in buffer starts */
1132 off_t offset, /* offset from start of imaginary file */
1133 int length, /* length of data in buffer */
1134 int func) /* 0 == read, 1 == write */
1137 int buflen, datalen;
1138 ctlr_info_t *ci;
1139 int i;
1140 int cntl_num;
1143 ci = (ctlr_info_t *) sh->hostdata[0];
1144 if (ci == NULL) /* This really shouldn't ever happen. */
1145 return -EINVAL;
1147 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1149 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1150 buflen = sprintf(buffer, "cciss%d: SCSI host: %d\n",
1151 cntl_num, sh->host_no);
1153 /* this information is needed by apps to know which cciss
1154 device corresponds to which scsi host number without
1155 having to open a scsi target device node. The device
1156 information is not a duplicate of /proc/scsi/scsi because
1157 the two may be out of sync due to scsi hotplug, rather
1158 this info is for an app to be able to use to know how to
1159 get them back in sync. */
1161 for (i=0;i<ccissscsi[cntl_num].ndevices;i++) {
1162 struct cciss_scsi_dev_t *sd = &ccissscsi[cntl_num].dev[i];
1163 buflen += sprintf(&buffer[buflen], "c%db%dt%dl%d %02d "
1164 "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1165 sh->host_no, sd->bus, sd->target, sd->lun,
1166 sd->devtype,
1167 sd->scsi3addr[0], sd->scsi3addr[1],
1168 sd->scsi3addr[2], sd->scsi3addr[3],
1169 sd->scsi3addr[4], sd->scsi3addr[5],
1170 sd->scsi3addr[6], sd->scsi3addr[7]);
1172 datalen = buflen - offset;
1173 if (datalen < 0) { /* they're reading past EOF. */
1174 datalen = 0;
1175 *start = buffer+buflen;
1176 } else
1177 *start = buffer + offset;
1178 return(datalen);
1179 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1180 return cciss_scsi_user_command(cntl_num, sh->host_no,
1181 buffer, length);
1184 /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1185 dma mapping and fills in the scatter gather entries of the
1186 cciss command, cp. */
1188 static void
1189 cciss_scatter_gather(struct pci_dev *pdev,
1190 CommandList_struct *cp,
1191 struct scsi_cmnd *cmd)
1193 unsigned int len;
1194 struct scatterlist *sg;
1195 __u64 addr64;
1196 int use_sg, i;
1198 BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
1200 use_sg = scsi_dma_map(cmd);
1201 if (use_sg) { /* not too many addrs? */
1202 scsi_for_each_sg(cmd, sg, use_sg, i) {
1203 addr64 = (__u64) sg_dma_address(sg);
1204 len = sg_dma_len(sg);
1205 cp->SG[i].Addr.lower =
1206 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1207 cp->SG[i].Addr.upper =
1208 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1209 cp->SG[i].Len = len;
1210 cp->SG[i].Ext = 0; // we are not chaining
1214 cp->Header.SGList = (__u8) use_sg; /* no. SGs contig in this cmd */
1215 cp->Header.SGTotal = (__u16) use_sg; /* total sgs in this cmd list */
1216 return;
1220 static int
1221 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1223 ctlr_info_t **c;
1224 int ctlr, rc;
1225 unsigned char scsi3addr[8];
1226 CommandList_struct *cp;
1227 unsigned long flags;
1229 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1230 // We violate cmd->host privacy here. (Is there another way?)
1231 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1232 ctlr = (*c)->ctlr;
1234 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1235 cmd->device->lun, scsi3addr);
1236 if (rc != 0) {
1237 /* the scsi nexus does not match any that we presented... */
1238 /* pretend to mid layer that we got selection timeout */
1239 cmd->result = DID_NO_CONNECT << 16;
1240 done(cmd);
1241 /* we might want to think about registering controller itself
1242 as a processor device on the bus so sg binds to it. */
1243 return 0;
1246 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1247 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1248 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1249 // cmd->target, cmd->lun);
1251 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1252 see what the device thinks of it. */
1254 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1255 cp = scsi_cmd_alloc(*c);
1256 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1257 if (cp == NULL) { /* trouble... */
1258 printk("scsi_cmd_alloc returned NULL!\n");
1259 /* FIXME: next 3 lines are -> BAD! <- */
1260 cmd->result = DID_NO_CONNECT << 16;
1261 done(cmd);
1262 return 0;
1265 // Fill in the command list header
1267 cmd->scsi_done = done; // save this for use by completion code
1269 // save cp in case we have to abort it
1270 cmd->host_scribble = (unsigned char *) cp;
1272 cp->cmd_type = CMD_SCSI;
1273 cp->scsi_cmd = cmd;
1274 cp->Header.ReplyQueue = 0; // unused in simple mode
1275 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1276 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1278 // Fill in the request block...
1280 cp->Request.Timeout = 0;
1281 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
1282 BUG_ON(cmd->cmd_len > sizeof(cp->Request.CDB));
1283 cp->Request.CDBLen = cmd->cmd_len;
1284 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1285 cp->Request.Type.Type = TYPE_CMD;
1286 cp->Request.Type.Attribute = ATTR_SIMPLE;
1287 switch(cmd->sc_data_direction)
1289 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1290 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1291 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1292 case DMA_BIDIRECTIONAL:
1293 // This can happen if a buggy application does a scsi passthru
1294 // and sets both inlen and outlen to non-zero. ( see
1295 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1297 cp->Request.Type.Direction = XFER_RSVD;
1298 // This is technically wrong, and cciss controllers should
1299 // reject it with CMD_INVALID, which is the most correct
1300 // response, but non-fibre backends appear to let it
1301 // slide by, and give the same results as if this field
1302 // were set correctly. Either way is acceptable for
1303 // our purposes here.
1305 break;
1307 default:
1308 printk("cciss: unknown data direction: %d\n",
1309 cmd->sc_data_direction);
1310 BUG();
1311 break;
1314 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1316 /* Put the request on the tail of the request queue */
1318 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1319 addQ(&(*c)->reqQ, cp);
1320 (*c)->Qdepth++;
1321 start_io(*c);
1322 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1324 /* the cmd'll come back via intr handler in complete_scsi_command() */
1325 return 0;
1328 static void
1329 cciss_unregister_scsi(int ctlr)
1331 struct cciss_scsi_adapter_data_t *sa;
1332 struct cciss_scsi_cmd_stack_t *stk;
1333 unsigned long flags;
1335 /* we are being forcibly unloaded, and may not refuse. */
1337 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1338 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1339 stk = &sa->cmd_stack;
1341 /* if we weren't ever actually registered, don't unregister */
1342 if (sa->registered) {
1343 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1344 scsi_remove_host(sa->scsi_host);
1345 scsi_host_put(sa->scsi_host);
1346 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1349 /* set scsi_host to NULL so our detect routine will
1350 find us on register */
1351 sa->scsi_host = NULL;
1352 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1353 scsi_cmd_stack_free(ctlr);
1354 kfree(sa);
1357 static int
1358 cciss_register_scsi(int ctlr)
1360 unsigned long flags;
1362 CPQ_TAPE_LOCK(ctlr, flags);
1364 /* Since this is really a block driver, the SCSI core may not be
1365 initialized at init time, in which case, calling scsi_register_host
1366 would hang. Instead, we do it later, via /proc filesystem
1367 and rc scripts, when we know SCSI core is good to go. */
1369 /* Only register if SCSI devices are detected. */
1370 if (ccissscsi[ctlr].ndevices != 0) {
1371 ((struct cciss_scsi_adapter_data_t *)
1372 hba[ctlr]->scsi_ctlr)->registered = 1;
1373 CPQ_TAPE_UNLOCK(ctlr, flags);
1374 return cciss_scsi_detect(ctlr);
1376 CPQ_TAPE_UNLOCK(ctlr, flags);
1377 printk(KERN_INFO
1378 "cciss%d: No appropriate SCSI device detected, "
1379 "SCSI subsystem not engaged.\n", ctlr);
1380 return 0;
1383 static int
1384 cciss_engage_scsi(int ctlr)
1386 struct cciss_scsi_adapter_data_t *sa;
1387 struct cciss_scsi_cmd_stack_t *stk;
1388 unsigned long flags;
1390 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1391 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1392 stk = &sa->cmd_stack;
1394 if (((struct cciss_scsi_adapter_data_t *)
1395 hba[ctlr]->scsi_ctlr)->registered) {
1396 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1397 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1398 return ENXIO;
1400 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1401 cciss_update_non_disk_devices(ctlr, -1);
1402 cciss_register_scsi(ctlr);
1403 return 0;
1406 static void
1407 cciss_seq_tape_report(struct seq_file *seq, int ctlr)
1409 unsigned long flags;
1411 CPQ_TAPE_LOCK(ctlr, flags);
1412 seq_printf(seq,
1413 "Sequential access devices: %d\n\n",
1414 ccissscsi[ctlr].ndevices);
1415 CPQ_TAPE_UNLOCK(ctlr, flags);
1419 /* Need at least one of these error handlers to keep ../scsi/hosts.c from
1420 * complaining. Doing a host- or bus-reset can't do anything good here.
1421 * Despite what it might say in scsi_error.c, there may well be commands
1422 * on the controller, as the cciss driver registers twice, once as a block
1423 * device for the logical drives, and once as a scsi device, for any tape
1424 * drives. So we know there are no commands out on the tape drives, but we
1425 * don't know there are no commands on the controller, and it is likely
1426 * that there probably are, as the cciss block device is most commonly used
1427 * as a boot device (embedded controller on HP/Compaq systems.)
1430 static int cciss_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
1432 int rc;
1433 CommandList_struct *cmd_in_trouble;
1434 ctlr_info_t **c;
1435 int ctlr;
1437 /* find the controller to which the command to be aborted was sent */
1438 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1439 if (c == NULL) /* paranoia */
1440 return FAILED;
1441 ctlr = (*c)->ctlr;
1442 printk(KERN_WARNING "cciss%d: resetting tape drive or medium changer.\n", ctlr);
1444 /* find the command that's giving us trouble */
1445 cmd_in_trouble = (CommandList_struct *) scsicmd->host_scribble;
1446 if (cmd_in_trouble == NULL) { /* paranoia */
1447 return FAILED;
1449 /* send a reset to the SCSI LUN which the command was sent to */
1450 rc = sendcmd(CCISS_RESET_MSG, ctlr, NULL, 0, 2, 0, 0,
1451 (unsigned char *) &cmd_in_trouble->Header.LUN.LunAddrBytes[0],
1452 TYPE_MSG);
1453 /* sendcmd turned off interrupts on the board, turn 'em back on. */
1454 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1455 if (rc == 0)
1456 return SUCCESS;
1457 printk(KERN_WARNING "cciss%d: resetting device failed.\n", ctlr);
1458 return FAILED;
1461 static int cciss_eh_abort_handler(struct scsi_cmnd *scsicmd)
1463 int rc;
1464 CommandList_struct *cmd_to_abort;
1465 ctlr_info_t **c;
1466 int ctlr;
1468 /* find the controller to which the command to be aborted was sent */
1469 c = (ctlr_info_t **) &scsicmd->device->host->hostdata[0];
1470 if (c == NULL) /* paranoia */
1471 return FAILED;
1472 ctlr = (*c)->ctlr;
1473 printk(KERN_WARNING "cciss%d: aborting tardy SCSI cmd\n", ctlr);
1475 /* find the command to be aborted */
1476 cmd_to_abort = (CommandList_struct *) scsicmd->host_scribble;
1477 if (cmd_to_abort == NULL) /* paranoia */
1478 return FAILED;
1479 rc = sendcmd(CCISS_ABORT_MSG, ctlr, &cmd_to_abort->Header.Tag,
1480 0, 2, 0, 0,
1481 (unsigned char *) &cmd_to_abort->Header.LUN.LunAddrBytes[0],
1482 TYPE_MSG);
1483 /* sendcmd turned off interrupts on the board, turn 'em back on. */
1484 (*c)->access.set_intr_mask(*c, CCISS_INTR_ON);
1485 if (rc == 0)
1486 return SUCCESS;
1487 return FAILED;
1491 #else /* no CONFIG_CISS_SCSI_TAPE */
1493 /* If no tape support, then these become defined out of existence */
1495 #define cciss_scsi_setup(cntl_num)
1496 #define cciss_unregister_scsi(ctlr)
1497 #define cciss_register_scsi(ctlr)
1499 #endif /* CONFIG_CISS_SCSI_TAPE */