[PATCH] cciss: direct lookup for command completions
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / block / cciss_scsi.c
blobdf4afacc75d79b99e9defab474b8659b1ba0c51e
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 <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... */
42 static int sendcmd(
43 __u8 cmd,
44 int ctlr,
45 void *buff,
46 size_t size,
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,
51 __u8 page_code,
52 unsigned char *scsi3addr,
53 int cmd_type);
56 static int cciss_scsi_proc_info(
57 struct Scsi_Host *sh,
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,
80 .name = "cciss",
81 .proc_name = "cciss",
82 .proc_info = cciss_scsi_proc_info,
83 .queuecommand = cciss_scsi_queue_command,
84 .can_queue = SCSI_CCISS_CAN_QUEUE,
85 .this_id = 7,
86 .sg_tablesize = MAXSGENTRIES,
87 .cmd_per_lun = 1,
88 .use_clustering = DISABLE_CLUSTERING,
91 #pragma pack(1)
92 struct cciss_scsi_cmd_stack_elem_t {
93 CommandList_struct cmd;
94 ErrorInfo_struct Err;
95 __u32 busaddr;
96 __u32 pad;
99 #pragma pack()
101 #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
102 CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
103 // plus two for init time usage
105 #pragma pack(1)
106 struct cciss_scsi_cmd_stack_t {
107 struct cciss_scsi_cmd_stack_elem_t *pool;
108 struct cciss_scsi_cmd_stack_elem_t *elem[CMD_STACK_SIZE];
109 dma_addr_t cmd_pool_handle;
110 int top;
112 #pragma pack()
114 struct cciss_scsi_adapter_data_t {
115 struct Scsi_Host *scsi_host;
116 struct cciss_scsi_cmd_stack_t cmd_stack;
117 int registered;
118 spinlock_t lock; // to protect ccissscsi[ctlr];
121 #define CPQ_TAPE_LOCK(ctlr, flags) spin_lock_irqsave( \
122 &(((struct cciss_scsi_adapter_data_t *) \
123 hba[ctlr]->scsi_ctlr)->lock), flags);
124 #define CPQ_TAPE_UNLOCK(ctlr, flags) spin_unlock_irqrestore( \
125 &(((struct cciss_scsi_adapter_data_t *) \
126 hba[ctlr]->scsi_ctlr)->lock), flags);
128 static CommandList_struct *
129 scsi_cmd_alloc(ctlr_info_t *h)
131 /* assume only one process in here at a time, locking done by caller. */
132 /* use CCISS_LOCK(ctlr) */
133 /* might be better to rewrite how we allocate scsi commands in a way that */
134 /* needs no locking at all. */
136 /* take the top memory chunk off the stack and return it, if any. */
137 struct cciss_scsi_cmd_stack_elem_t *c;
138 struct cciss_scsi_adapter_data_t *sa;
139 struct cciss_scsi_cmd_stack_t *stk;
140 u64bit temp64;
142 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
143 stk = &sa->cmd_stack;
145 if (stk->top < 0)
146 return NULL;
147 c = stk->elem[stk->top];
148 /* memset(c, 0, sizeof(*c)); */
149 memset(&c->cmd, 0, sizeof(c->cmd));
150 memset(&c->Err, 0, sizeof(c->Err));
151 /* set physical addr of cmd and addr of scsi parameters */
152 c->cmd.busaddr = c->busaddr;
153 /* (__u32) (stk->cmd_pool_handle +
154 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top)); */
156 temp64.val = (__u64) (c->busaddr + sizeof(CommandList_struct));
157 /* (__u64) (stk->cmd_pool_handle +
158 (sizeof(struct cciss_scsi_cmd_stack_elem_t)*stk->top) +
159 sizeof(CommandList_struct)); */
160 stk->top--;
161 c->cmd.ErrDesc.Addr.lower = temp64.val32.lower;
162 c->cmd.ErrDesc.Addr.upper = temp64.val32.upper;
163 c->cmd.ErrDesc.Len = sizeof(ErrorInfo_struct);
165 c->cmd.ctlr = h->ctlr;
166 c->cmd.err_info = &c->Err;
168 return (CommandList_struct *) c;
171 static void
172 scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
174 /* assume only one process in here at a time, locking done by caller. */
175 /* use CCISS_LOCK(ctlr) */
176 /* drop the free memory chunk on top of the stack. */
178 struct cciss_scsi_adapter_data_t *sa;
179 struct cciss_scsi_cmd_stack_t *stk;
181 sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
182 stk = &sa->cmd_stack;
183 if (stk->top >= CMD_STACK_SIZE) {
184 printk("cciss: scsi_cmd_free called too many times.\n");
185 BUG();
187 stk->top++;
188 stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
191 static int
192 scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
194 int i;
195 struct cciss_scsi_cmd_stack_t *stk;
196 size_t size;
198 stk = &sa->cmd_stack;
199 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
201 // pci_alloc_consistent guarantees 32-bit DMA address will
202 // be used
204 stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
205 pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
207 if (stk->pool == NULL) {
208 printk("stk->pool is null\n");
209 return -1;
212 for (i=0; i<CMD_STACK_SIZE; i++) {
213 stk->elem[i] = &stk->pool[i];
214 stk->elem[i]->busaddr = (__u32) (stk->cmd_pool_handle +
215 (sizeof(struct cciss_scsi_cmd_stack_elem_t) * i));
217 stk->top = CMD_STACK_SIZE-1;
218 return 0;
221 static void
222 scsi_cmd_stack_free(int ctlr)
224 struct cciss_scsi_adapter_data_t *sa;
225 struct cciss_scsi_cmd_stack_t *stk;
226 size_t size;
228 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
229 stk = &sa->cmd_stack;
230 if (stk->top != CMD_STACK_SIZE-1) {
231 printk( "cciss: %d scsi commands are still outstanding.\n",
232 CMD_STACK_SIZE - stk->top);
233 // BUG();
234 printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);
236 size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
238 pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
239 stk->pool = NULL;
242 /* scsi_device_types comes from scsi.h */
243 #define DEVICETYPE(n) (n<0 || n>MAX_SCSI_DEVICE_CODE) ? \
244 "Unknown" : scsi_device_types[n]
246 #if 0
247 static int xmargin=8;
248 static int amargin=60;
250 static void
251 print_bytes (unsigned char *c, int len, int hex, int ascii)
254 int i;
255 unsigned char *x;
257 if (hex)
259 x = c;
260 for (i=0;i<len;i++)
262 if ((i % xmargin) == 0 && i>0) printk("\n");
263 if ((i % xmargin) == 0) printk("0x%04x:", i);
264 printk(" %02x", *x);
265 x++;
267 printk("\n");
269 if (ascii)
271 x = c;
272 for (i=0;i<len;i++)
274 if ((i % amargin) == 0 && i>0) printk("\n");
275 if ((i % amargin) == 0) printk("0x%04x:", i);
276 if (*x > 26 && *x < 128) printk("%c", *x);
277 else printk(".");
278 x++;
280 printk("\n");
284 static void
285 print_cmd(CommandList_struct *cp)
287 printk("queue:%d\n", cp->Header.ReplyQueue);
288 printk("sglist:%d\n", cp->Header.SGList);
289 printk("sgtot:%d\n", cp->Header.SGTotal);
290 printk("Tag:0x%08x/0x%08x\n", cp->Header.Tag.upper,
291 cp->Header.Tag.lower);
292 printk("LUN:0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
293 cp->Header.LUN.LunAddrBytes[0],
294 cp->Header.LUN.LunAddrBytes[1],
295 cp->Header.LUN.LunAddrBytes[2],
296 cp->Header.LUN.LunAddrBytes[3],
297 cp->Header.LUN.LunAddrBytes[4],
298 cp->Header.LUN.LunAddrBytes[5],
299 cp->Header.LUN.LunAddrBytes[6],
300 cp->Header.LUN.LunAddrBytes[7]);
301 printk("CDBLen:%d\n", cp->Request.CDBLen);
302 printk("Type:%d\n",cp->Request.Type.Type);
303 printk("Attr:%d\n",cp->Request.Type.Attribute);
304 printk(" Dir:%d\n",cp->Request.Type.Direction);
305 printk("Timeout:%d\n",cp->Request.Timeout);
306 printk( "CDB: %02x %02x %02x %02x %02x %02x %02x %02x"
307 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
308 cp->Request.CDB[0], cp->Request.CDB[1],
309 cp->Request.CDB[2], cp->Request.CDB[3],
310 cp->Request.CDB[4], cp->Request.CDB[5],
311 cp->Request.CDB[6], cp->Request.CDB[7],
312 cp->Request.CDB[8], cp->Request.CDB[9],
313 cp->Request.CDB[10], cp->Request.CDB[11],
314 cp->Request.CDB[12], cp->Request.CDB[13],
315 cp->Request.CDB[14], cp->Request.CDB[15]),
316 printk("edesc.Addr: 0x%08x/0%08x, Len = %d\n",
317 cp->ErrDesc.Addr.upper, cp->ErrDesc.Addr.lower,
318 cp->ErrDesc.Len);
319 printk("sgs..........Errorinfo:\n");
320 printk("scsistatus:%d\n", cp->err_info->ScsiStatus);
321 printk("senselen:%d\n", cp->err_info->SenseLen);
322 printk("cmd status:%d\n", cp->err_info->CommandStatus);
323 printk("resid cnt:%d\n", cp->err_info->ResidualCnt);
324 printk("offense size:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_size);
325 printk("offense byte:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_num);
326 printk("offense value:%d\n", cp->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
330 #endif
332 static int
333 find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
335 /* finds an unused bus, target, lun for a new device */
336 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
337 int i, found=0;
338 unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
340 memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
342 target_taken[SELF_SCSI_ID] = 1;
343 for (i=0;i<ccissscsi[ctlr].ndevices;i++)
344 target_taken[ccissscsi[ctlr].dev[i].target] = 1;
346 for (i=0;i<CCISS_MAX_SCSI_DEVS_PER_HBA;i++) {
347 if (!target_taken[i]) {
348 *bus = 0; *target=i; *lun = 0; found=1;
349 break;
352 return (!found);
355 static int
356 cciss_scsi_add_entry(int ctlr, int hostno,
357 unsigned char *scsi3addr, int devtype)
359 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
360 int n = ccissscsi[ctlr].ndevices;
361 struct cciss_scsi_dev_t *sd;
363 if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
364 printk("cciss%d: Too many devices, "
365 "some will be inaccessible.\n", ctlr);
366 return -1;
368 sd = &ccissscsi[ctlr].dev[n];
369 if (find_bus_target_lun(ctlr, &sd->bus, &sd->target, &sd->lun) != 0)
370 return -1;
371 memcpy(&sd->scsi3addr[0], scsi3addr, 8);
372 sd->devtype = devtype;
373 ccissscsi[ctlr].ndevices++;
375 /* initially, (before registering with scsi layer) we don't
376 know our hostno and we don't want to print anything first
377 time anyway (the scsi layer's inquiries will show that info) */
378 if (hostno != -1)
379 printk("cciss%d: %s device c%db%dt%dl%d added.\n",
380 ctlr, DEVICETYPE(sd->devtype), hostno,
381 sd->bus, sd->target, sd->lun);
382 return 0;
385 static void
386 cciss_scsi_remove_entry(int ctlr, int hostno, int entry)
388 /* assumes hba[ctlr]->scsi_ctlr->lock is held */
389 int i;
390 struct cciss_scsi_dev_t sd;
392 if (entry < 0 || entry >= CCISS_MAX_SCSI_DEVS_PER_HBA) return;
393 sd = ccissscsi[ctlr].dev[entry];
394 for (i=entry;i<ccissscsi[ctlr].ndevices-1;i++)
395 ccissscsi[ctlr].dev[i] = ccissscsi[ctlr].dev[i+1];
396 ccissscsi[ctlr].ndevices--;
397 printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
398 ctlr, DEVICETYPE(sd.devtype), hostno,
399 sd.bus, sd.target, sd.lun);
403 #define SCSI3ADDR_EQ(a,b) ( \
404 (a)[7] == (b)[7] && \
405 (a)[6] == (b)[6] && \
406 (a)[5] == (b)[5] && \
407 (a)[4] == (b)[4] && \
408 (a)[3] == (b)[3] && \
409 (a)[2] == (b)[2] && \
410 (a)[1] == (b)[1] && \
411 (a)[0] == (b)[0])
413 static int
414 adjust_cciss_scsi_table(int ctlr, int hostno,
415 struct cciss_scsi_dev_t sd[], int nsds)
417 /* sd contains scsi3 addresses and devtypes, but
418 bus target and lun are not filled in. This funciton
419 takes what's in sd to be the current and adjusts
420 ccissscsi[] to be in line with what's in sd. */
422 int i,j, found, changes=0;
423 struct cciss_scsi_dev_t *csd;
424 unsigned long flags;
426 CPQ_TAPE_LOCK(ctlr, flags);
428 /* find any devices in ccissscsi[] that are not in
429 sd[] and remove them from ccissscsi[] */
431 i = 0;
432 while(i<ccissscsi[ctlr].ndevices) {
433 csd = &ccissscsi[ctlr].dev[i];
434 found=0;
435 for (j=0;j<nsds;j++) {
436 if (SCSI3ADDR_EQ(sd[j].scsi3addr,
437 csd->scsi3addr)) {
438 if (sd[j].devtype == csd->devtype)
439 found=2;
440 else
441 found=1;
442 break;
446 if (found == 0) { /* device no longer present. */
447 changes++;
448 /* printk("cciss%d: %s device c%db%dt%dl%d removed.\n",
449 ctlr, DEVICETYPE(csd->devtype), hostno,
450 csd->bus, csd->target, csd->lun); */
451 cciss_scsi_remove_entry(ctlr, hostno, i);
452 /* note, i not incremented */
454 else if (found == 1) { /* device is different kind */
455 changes++;
456 printk("cciss%d: device c%db%dt%dl%d type changed "
457 "(device type now %s).\n",
458 ctlr, hostno, csd->bus, csd->target, csd->lun,
459 DEVICETYPE(csd->devtype));
460 csd->devtype = sd[j].devtype;
461 i++; /* so just move along. */
462 } else /* device is same as it ever was, */
463 i++; /* so just move along. */
466 /* Now, make sure every device listed in sd[] is also
467 listed in ccissscsi[], adding them if they aren't found */
469 for (i=0;i<nsds;i++) {
470 found=0;
471 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
472 csd = &ccissscsi[ctlr].dev[j];
473 if (SCSI3ADDR_EQ(sd[i].scsi3addr,
474 csd->scsi3addr)) {
475 if (sd[i].devtype == csd->devtype)
476 found=2; /* found device */
477 else
478 found=1; /* found a bug. */
479 break;
482 if (!found) {
483 changes++;
484 if (cciss_scsi_add_entry(ctlr, hostno,
485 &sd[i].scsi3addr[0], sd[i].devtype) != 0)
486 break;
487 } else if (found == 1) {
488 /* should never happen... */
489 changes++;
490 printk("cciss%d: device unexpectedly changed type\n",
491 ctlr);
492 /* but if it does happen, we just ignore that device */
495 CPQ_TAPE_UNLOCK(ctlr, flags);
497 if (!changes)
498 printk("cciss%d: No device changes detected.\n", ctlr);
500 return 0;
503 static int
504 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
506 int i;
507 struct cciss_scsi_dev_t *sd;
508 unsigned long flags;
510 CPQ_TAPE_LOCK(ctlr, flags);
511 for (i=0;i<ccissscsi[ctlr].ndevices;i++) {
512 sd = &ccissscsi[ctlr].dev[i];
513 if (sd->bus == bus &&
514 sd->target == target &&
515 sd->lun == lun) {
516 memcpy(scsi3addr, &sd->scsi3addr[0], 8);
517 CPQ_TAPE_UNLOCK(ctlr, flags);
518 return 0;
521 CPQ_TAPE_UNLOCK(ctlr, flags);
522 return -1;
525 static void
526 cciss_scsi_setup(int cntl_num)
528 struct cciss_scsi_adapter_data_t * shba;
530 ccissscsi[cntl_num].ndevices = 0;
531 shba = (struct cciss_scsi_adapter_data_t *)
532 kmalloc(sizeof(*shba), GFP_KERNEL);
533 if (shba == NULL)
534 return;
535 shba->scsi_host = NULL;
536 spin_lock_init(&shba->lock);
537 shba->registered = 0;
538 if (scsi_cmd_stack_setup(cntl_num, shba) != 0) {
539 kfree(shba);
540 shba = NULL;
542 hba[cntl_num]->scsi_ctlr = (void *) shba;
543 return;
546 static void
547 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
549 struct scsi_cmnd *cmd;
550 ctlr_info_t *ctlr;
551 u64bit addr64;
552 ErrorInfo_struct *ei;
554 ei = cp->err_info;
556 /* First, see if it was a message rather than a command */
557 if (cp->Request.Type.Type == TYPE_MSG) {
558 cp->cmd_type = CMD_MSG_DONE;
559 return;
562 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
563 ctlr = hba[cp->ctlr];
565 /* undo the DMA mappings */
567 if (cmd->use_sg) {
568 pci_unmap_sg(ctlr->pdev,
569 cmd->buffer, cmd->use_sg,
570 cmd->sc_data_direction);
572 else if (cmd->request_bufflen) {
573 addr64.val32.lower = cp->SG[0].Addr.lower;
574 addr64.val32.upper = cp->SG[0].Addr.upper;
575 pci_unmap_single(ctlr->pdev, (dma_addr_t) addr64.val,
576 cmd->request_bufflen,
577 cmd->sc_data_direction);
580 cmd->result = (DID_OK << 16); /* host byte */
581 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
582 /* cmd->result |= (GOOD < 1); */ /* status byte */
584 cmd->result |= (ei->ScsiStatus);
585 /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus); */
587 /* copy the sense data whether we need to or not. */
589 memcpy(cmd->sense_buffer, ei->SenseInfo,
590 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
591 SCSI_SENSE_BUFFERSIZE :
592 ei->SenseLen);
593 cmd->resid = ei->ResidualCnt;
595 if(ei->CommandStatus != 0)
596 { /* an error has occurred */
597 switch(ei->CommandStatus)
599 case CMD_TARGET_STATUS:
600 /* Pass it up to the upper layers... */
601 if( ei->ScsiStatus)
603 #if 0
604 printk(KERN_WARNING "cciss: cmd %p "
605 "has SCSI Status = %x\n",
606 cp,
607 ei->ScsiStatus);
608 #endif
609 cmd->result |= (ei->ScsiStatus < 1);
611 else { /* scsi status is zero??? How??? */
613 /* Ordinarily, this case should never happen, but there is a bug
614 in some released firmware revisions that allows it to happen
615 if, for example, a 4100 backplane loses power and the tape
616 drive is in it. We assume that it's a fatal error of some
617 kind because we can't show that it wasn't. We will make it
618 look like selection timeout since that is the most common
619 reason for this to occur, and it's severe enough. */
621 cmd->result = DID_NO_CONNECT << 16;
623 break;
624 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
625 break;
626 case CMD_DATA_OVERRUN:
627 printk(KERN_WARNING "cciss: cp %p has"
628 " completed with data overrun "
629 "reported\n", cp);
630 break;
631 case CMD_INVALID: {
632 /* print_bytes(cp, sizeof(*cp), 1, 0);
633 print_cmd(cp); */
634 /* We get CMD_INVALID if you address a non-existent tape drive instead
635 of a selection timeout (no response). You will see this if you yank
636 out a tape drive, then try to access it. This is kind of a shame
637 because it means that any other CMD_INVALID (e.g. driver bug) will
638 get interpreted as a missing target. */
639 cmd->result = DID_NO_CONNECT << 16;
641 break;
642 case CMD_PROTOCOL_ERR:
643 printk(KERN_WARNING "cciss: cp %p has "
644 "protocol error \n", cp);
645 break;
646 case CMD_HARDWARE_ERR:
647 cmd->result = DID_ERROR << 16;
648 printk(KERN_WARNING "cciss: cp %p had "
649 " hardware error\n", cp);
650 break;
651 case CMD_CONNECTION_LOST:
652 cmd->result = DID_ERROR << 16;
653 printk(KERN_WARNING "cciss: cp %p had "
654 "connection lost\n", cp);
655 break;
656 case CMD_ABORTED:
657 cmd->result = DID_ABORT << 16;
658 printk(KERN_WARNING "cciss: cp %p was "
659 "aborted\n", cp);
660 break;
661 case CMD_ABORT_FAILED:
662 cmd->result = DID_ERROR << 16;
663 printk(KERN_WARNING "cciss: cp %p reports "
664 "abort failed\n", cp);
665 break;
666 case CMD_UNSOLICITED_ABORT:
667 cmd->result = DID_ABORT << 16;
668 printk(KERN_WARNING "cciss: cp %p aborted "
669 "do to an unsolicited abort\n", cp);
670 break;
671 case CMD_TIMEOUT:
672 cmd->result = DID_TIME_OUT << 16;
673 printk(KERN_WARNING "cciss: cp %p timedout\n",
674 cp);
675 break;
676 default:
677 cmd->result = DID_ERROR << 16;
678 printk(KERN_WARNING "cciss: cp %p returned "
679 "unknown status %x\n", cp,
680 ei->CommandStatus);
683 // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel,
684 // cmd->target, cmd->lun);
685 cmd->scsi_done(cmd);
686 scsi_cmd_free(ctlr, cp);
689 static int
690 cciss_scsi_detect(int ctlr)
692 struct Scsi_Host *sh;
693 int error;
695 sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
696 if (sh == NULL)
697 goto fail;
698 sh->io_port = 0; // good enough? FIXME,
699 sh->n_io_port = 0; // I don't think we use these two...
700 sh->this_id = SELF_SCSI_ID;
702 ((struct cciss_scsi_adapter_data_t *)
703 hba[ctlr]->scsi_ctlr)->scsi_host = (void *) sh;
704 sh->hostdata[0] = (unsigned long) hba[ctlr];
705 sh->irq = hba[ctlr]->intr;
706 sh->unique_id = sh->irq;
707 error = scsi_add_host(sh, &hba[ctlr]->pdev->dev);
708 if (error)
709 goto fail_host_put;
710 scsi_scan_host(sh);
711 return 1;
713 fail_host_put:
714 scsi_host_put(sh);
715 fail:
716 return 0;
719 static void
720 cciss_unmap_one(struct pci_dev *pdev,
721 CommandList_struct *cp,
722 size_t buflen,
723 int data_direction)
725 u64bit addr64;
727 addr64.val32.lower = cp->SG[0].Addr.lower;
728 addr64.val32.upper = cp->SG[0].Addr.upper;
729 pci_unmap_single(pdev, (dma_addr_t) addr64.val, buflen, data_direction);
732 static void
733 cciss_map_one(struct pci_dev *pdev,
734 CommandList_struct *cp,
735 unsigned char *buf,
736 size_t buflen,
737 int data_direction)
739 __u64 addr64;
741 addr64 = (__u64) pci_map_single(pdev, buf, buflen, data_direction);
742 cp->SG[0].Addr.lower =
743 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
744 cp->SG[0].Addr.upper =
745 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
746 cp->SG[0].Len = buflen;
747 cp->Header.SGList = (__u8) 1; /* no. SGs contig in this cmd */
748 cp->Header.SGTotal = (__u16) 1; /* total sgs in this cmd list */
751 static int
752 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
753 CommandList_struct *cp,
754 unsigned char *scsi3addr,
755 unsigned char *cdb,
756 unsigned char cdblen,
757 unsigned char *buf, int bufsize,
758 int direction)
760 unsigned long flags;
761 DECLARE_COMPLETION(wait);
763 cp->cmd_type = CMD_IOCTL_PEND; // treat this like an ioctl
764 cp->scsi_cmd = NULL;
765 cp->Header.ReplyQueue = 0; // unused in simple mode
766 memcpy(&cp->Header.LUN, scsi3addr, sizeof(cp->Header.LUN));
767 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
768 // Fill in the request block...
770 /* printk("Using scsi3addr 0x%02x%0x2%0x2%0x2%0x2%0x2%0x2%0x2\n",
771 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
772 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]); */
774 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
775 memcpy(cp->Request.CDB, cdb, cdblen);
776 cp->Request.Timeout = 0;
777 cp->Request.CDBLen = cdblen;
778 cp->Request.Type.Type = TYPE_CMD;
779 cp->Request.Type.Attribute = ATTR_SIMPLE;
780 cp->Request.Type.Direction = direction;
782 /* Fill in the SG list and do dma mapping */
783 cciss_map_one(c->pdev, cp, (unsigned char *) buf,
784 bufsize, DMA_FROM_DEVICE);
786 cp->waiting = &wait;
788 /* Put the request on the tail of the request queue */
789 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
790 addQ(&c->reqQ, cp);
791 c->Qdepth++;
792 start_io(c);
793 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
795 wait_for_completion(&wait);
797 /* undo the dma mapping */
798 cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
799 return(0);
802 static void
803 cciss_scsi_interpret_error(CommandList_struct *cp)
805 ErrorInfo_struct *ei;
807 ei = cp->err_info;
808 switch(ei->CommandStatus)
810 case CMD_TARGET_STATUS:
811 printk(KERN_WARNING "cciss: cmd %p has "
812 "completed with errors\n", cp);
813 printk(KERN_WARNING "cciss: cmd %p "
814 "has SCSI Status = %x\n",
815 cp,
816 ei->ScsiStatus);
817 if (ei->ScsiStatus == 0)
818 printk(KERN_WARNING
819 "cciss:SCSI status is abnormally zero. "
820 "(probably indicates selection timeout "
821 "reported incorrectly due to a known "
822 "firmware bug, circa July, 2001.)\n");
823 break;
824 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
825 printk("UNDERRUN\n");
826 break;
827 case CMD_DATA_OVERRUN:
828 printk(KERN_WARNING "cciss: cp %p has"
829 " completed with data overrun "
830 "reported\n", cp);
831 break;
832 case CMD_INVALID: {
833 /* controller unfortunately reports SCSI passthru's */
834 /* to non-existent targets as invalid commands. */
835 printk(KERN_WARNING "cciss: cp %p is "
836 "reported invalid (probably means "
837 "target device no longer present)\n",
838 cp);
839 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
840 print_cmd(cp); */
842 break;
843 case CMD_PROTOCOL_ERR:
844 printk(KERN_WARNING "cciss: cp %p has "
845 "protocol error \n", cp);
846 break;
847 case CMD_HARDWARE_ERR:
848 /* cmd->result = DID_ERROR << 16; */
849 printk(KERN_WARNING "cciss: cp %p had "
850 " hardware error\n", cp);
851 break;
852 case CMD_CONNECTION_LOST:
853 printk(KERN_WARNING "cciss: cp %p had "
854 "connection lost\n", cp);
855 break;
856 case CMD_ABORTED:
857 printk(KERN_WARNING "cciss: cp %p was "
858 "aborted\n", cp);
859 break;
860 case CMD_ABORT_FAILED:
861 printk(KERN_WARNING "cciss: cp %p reports "
862 "abort failed\n", cp);
863 break;
864 case CMD_UNSOLICITED_ABORT:
865 printk(KERN_WARNING "cciss: cp %p aborted "
866 "do to an unsolicited abort\n", cp);
867 break;
868 case CMD_TIMEOUT:
869 printk(KERN_WARNING "cciss: cp %p timedout\n",
870 cp);
871 break;
872 default:
873 printk(KERN_WARNING "cciss: cp %p returned "
874 "unknown status %x\n", cp,
875 ei->CommandStatus);
879 static int
880 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr,
881 InquiryData_struct *buf)
883 int rc;
884 CommandList_struct *cp;
885 char cdb[6];
886 ErrorInfo_struct *ei;
887 unsigned long flags;
889 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
890 cp = scsi_cmd_alloc(c);
891 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
893 if (cp == NULL) { /* trouble... */
894 printk("cmd_alloc returned NULL!\n");
895 return -1;
898 ei = cp->err_info;
900 cdb[0] = CISS_INQUIRY;
901 cdb[1] = 0;
902 cdb[2] = 0;
903 cdb[3] = 0;
904 cdb[4] = sizeof(*buf) & 0xff;
905 cdb[5] = 0;
906 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb,
907 6, (unsigned char *) buf,
908 sizeof(*buf), XFER_READ);
910 if (rc != 0) return rc; /* something went wrong */
912 if (ei->CommandStatus != 0 &&
913 ei->CommandStatus != CMD_DATA_UNDERRUN) {
914 cciss_scsi_interpret_error(cp);
915 rc = -1;
917 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
918 scsi_cmd_free(c, cp);
919 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
920 return rc;
923 static int
924 cciss_scsi_do_report_phys_luns(ctlr_info_t *c,
925 ReportLunData_struct *buf, int bufsize)
927 int rc;
928 CommandList_struct *cp;
929 unsigned char cdb[12];
930 unsigned char scsi3addr[8];
931 ErrorInfo_struct *ei;
932 unsigned long flags;
934 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
935 cp = scsi_cmd_alloc(c);
936 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
937 if (cp == NULL) { /* trouble... */
938 printk("cmd_alloc returned NULL!\n");
939 return -1;
942 memset(&scsi3addr[0], 0, 8); /* address the controller */
943 cdb[0] = CISS_REPORT_PHYS;
944 cdb[1] = 0;
945 cdb[2] = 0;
946 cdb[3] = 0;
947 cdb[4] = 0;
948 cdb[5] = 0;
949 cdb[6] = (bufsize >> 24) & 0xFF; //MSB
950 cdb[7] = (bufsize >> 16) & 0xFF;
951 cdb[8] = (bufsize >> 8) & 0xFF;
952 cdb[9] = bufsize & 0xFF;
953 cdb[10] = 0;
954 cdb[11] = 0;
956 rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr,
957 cdb, 12,
958 (unsigned char *) buf,
959 bufsize, XFER_READ);
961 if (rc != 0) return rc; /* something went wrong */
963 ei = cp->err_info;
964 if (ei->CommandStatus != 0 &&
965 ei->CommandStatus != CMD_DATA_UNDERRUN) {
966 cciss_scsi_interpret_error(cp);
967 rc = -1;
969 spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
970 scsi_cmd_free(c, cp);
971 spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
972 return rc;
975 static void
976 cciss_update_non_disk_devices(int cntl_num, int hostno)
978 /* the idea here is we could get notified from /proc
979 that some devices have changed, so we do a report
980 physical luns cmd, and adjust our list of devices
981 accordingly. (We can't rely on the scsi-mid layer just
982 doing inquiries, because the "busses" that the scsi
983 mid-layer probes are totally fabricated by this driver,
984 so new devices wouldn't show up.
986 the scsi3addr's of devices won't change so long as the
987 adapter is not reset. That means we can rescan and
988 tell which devices we already know about, vs. new
989 devices, vs. disappearing devices.
991 Also, if you yank out a tape drive, then put in a disk
992 in it's place, (say, a configured volume from another
993 array controller for instance) _don't_ poke this driver
994 (so it thinks it's still a tape, but _do_ poke the scsi
995 mid layer, so it does an inquiry... the scsi mid layer
996 will see the physical disk. This would be bad. Need to
997 think about how to prevent that. One idea would be to
998 snoop all scsi responses and if an inquiry repsonse comes
999 back that reports a disk, chuck it an return selection
1000 timeout instead and adjust our table... Not sure i like
1001 that though.
1005 ReportLunData_struct *ld_buff;
1006 InquiryData_struct *inq_buff;
1007 unsigned char scsi3addr[8];
1008 ctlr_info_t *c;
1009 __u32 num_luns=0;
1010 unsigned char *ch;
1011 /* unsigned char found[CCISS_MAX_SCSI_DEVS_PER_HBA]; */
1012 struct cciss_scsi_dev_t currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1013 int ncurrent=0;
1014 int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1015 int i;
1017 c = (ctlr_info_t *) hba[cntl_num];
1018 ld_buff = kmalloc(reportlunsize, GFP_KERNEL);
1019 if (ld_buff == NULL) {
1020 printk(KERN_ERR "cciss: out of memory\n");
1021 return;
1023 memset(ld_buff, 0, reportlunsize);
1024 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1025 if (inq_buff == NULL) {
1026 printk(KERN_ERR "cciss: out of memory\n");
1027 kfree(ld_buff);
1028 return;
1031 if (cciss_scsi_do_report_phys_luns(c, ld_buff, reportlunsize) == 0) {
1032 ch = &ld_buff->LUNListLength[0];
1033 num_luns = ((ch[0]<<24) | (ch[1]<<16) | (ch[2]<<8) | ch[3]) / 8;
1034 if (num_luns > CISS_MAX_PHYS_LUN) {
1035 printk(KERN_WARNING
1036 "cciss: Maximum physical LUNs (%d) exceeded. "
1037 "%d LUNs ignored.\n", CISS_MAX_PHYS_LUN,
1038 num_luns - CISS_MAX_PHYS_LUN);
1039 num_luns = CISS_MAX_PHYS_LUN;
1042 else {
1043 printk(KERN_ERR "cciss: Report physical LUNs failed.\n");
1044 goto out;
1048 /* adjust our table of devices */
1049 for(i=0; i<num_luns; i++)
1051 int devtype;
1053 /* for each physical lun, do an inquiry */
1054 if (ld_buff->LUN[i][3] & 0xC0) continue;
1055 memset(inq_buff, 0, sizeof(InquiryData_struct));
1056 memcpy(&scsi3addr[0], &ld_buff->LUN[i][0], 8);
1058 if (cciss_scsi_do_inquiry(hba[cntl_num],
1059 scsi3addr, inq_buff) != 0)
1061 /* Inquiry failed (msg printed already) */
1062 devtype = 0; /* so we will skip this device. */
1063 } else /* what kind of device is this? */
1064 devtype = (inq_buff->data_byte[0] & 0x1f);
1066 switch (devtype)
1068 case 0x01: /* sequential access, (tape) */
1069 case 0x08: /* medium changer */
1070 if (ncurrent >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
1071 printk(KERN_INFO "cciss%d: %s ignored, "
1072 "too many devices.\n", cntl_num,
1073 DEVICETYPE(devtype));
1074 break;
1076 memcpy(&currentsd[ncurrent].scsi3addr[0],
1077 &scsi3addr[0], 8);
1078 currentsd[ncurrent].devtype = devtype;
1079 currentsd[ncurrent].bus = -1;
1080 currentsd[ncurrent].target = -1;
1081 currentsd[ncurrent].lun = -1;
1082 ncurrent++;
1083 break;
1084 default:
1085 break;
1089 adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1090 out:
1091 kfree(inq_buff);
1092 kfree(ld_buff);
1093 return;
1096 static int
1097 is_keyword(char *ptr, int len, char *verb) // Thanks to ncr53c8xx.c
1099 int verb_len = strlen(verb);
1100 if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1101 return verb_len;
1102 else
1103 return 0;
1106 static int
1107 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1109 int arg_len;
1111 if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1112 cciss_update_non_disk_devices(ctlr, hostno);
1113 else
1114 return -EINVAL;
1115 return length;
1119 static int
1120 cciss_scsi_proc_info(struct Scsi_Host *sh,
1121 char *buffer, /* data buffer */
1122 char **start, /* where data in buffer starts */
1123 off_t offset, /* offset from start of imaginary file */
1124 int length, /* length of data in buffer */
1125 int func) /* 0 == read, 1 == write */
1128 int buflen, datalen;
1129 ctlr_info_t *ci;
1130 int cntl_num;
1133 ci = (ctlr_info_t *) sh->hostdata[0];
1134 if (ci == NULL) /* This really shouldn't ever happen. */
1135 return -EINVAL;
1137 cntl_num = ci->ctlr; /* Get our index into the hba[] array */
1139 if (func == 0) { /* User is reading from /proc/scsi/ciss*?/?* */
1140 buflen = sprintf(buffer, "hostnum=%d\n", sh->host_no);
1142 datalen = buflen - offset;
1143 if (datalen < 0) { /* they're reading past EOF. */
1144 datalen = 0;
1145 *start = buffer+buflen;
1146 } else
1147 *start = buffer + offset;
1148 return(datalen);
1149 } else /* User is writing to /proc/scsi/cciss*?/?* ... */
1150 return cciss_scsi_user_command(cntl_num, sh->host_no,
1151 buffer, length);
1154 /* cciss_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
1155 dma mapping and fills in the scatter gather entries of the
1156 cciss command, cp. */
1158 static void
1159 cciss_scatter_gather(struct pci_dev *pdev,
1160 CommandList_struct *cp,
1161 struct scsi_cmnd *cmd)
1163 unsigned int use_sg, nsegs=0, len;
1164 struct scatterlist *scatter = (struct scatterlist *) cmd->buffer;
1165 __u64 addr64;
1167 /* is it just one virtual address? */
1168 if (!cmd->use_sg) {
1169 if (cmd->request_bufflen) { /* anything to xfer? */
1171 addr64 = (__u64) pci_map_single(pdev,
1172 cmd->request_buffer,
1173 cmd->request_bufflen,
1174 cmd->sc_data_direction);
1176 cp->SG[0].Addr.lower =
1177 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1178 cp->SG[0].Addr.upper =
1179 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1180 cp->SG[0].Len = cmd->request_bufflen;
1181 nsegs=1;
1183 } /* else, must be a list of virtual addresses.... */
1184 else if (cmd->use_sg <= MAXSGENTRIES) { /* not too many addrs? */
1186 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg,
1187 cmd->sc_data_direction);
1189 for (nsegs=0; nsegs < use_sg; nsegs++) {
1190 addr64 = (__u64) sg_dma_address(&scatter[nsegs]);
1191 len = sg_dma_len(&scatter[nsegs]);
1192 cp->SG[nsegs].Addr.lower =
1193 (__u32) (addr64 & (__u64) 0x00000000FFFFFFFF);
1194 cp->SG[nsegs].Addr.upper =
1195 (__u32) ((addr64 >> 32) & (__u64) 0x00000000FFFFFFFF);
1196 cp->SG[nsegs].Len = len;
1197 cp->SG[nsegs].Ext = 0; // we are not chaining
1199 } else BUG();
1201 cp->Header.SGList = (__u8) nsegs; /* no. SGs contig in this cmd */
1202 cp->Header.SGTotal = (__u16) nsegs; /* total sgs in this cmd list */
1203 return;
1207 static int
1208 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1210 ctlr_info_t **c;
1211 int ctlr, rc;
1212 unsigned char scsi3addr[8];
1213 CommandList_struct *cp;
1214 unsigned long flags;
1216 // Get the ptr to our adapter structure (hba[i]) out of cmd->host.
1217 // We violate cmd->host privacy here. (Is there another way?)
1218 c = (ctlr_info_t **) &cmd->device->host->hostdata[0];
1219 ctlr = (*c)->ctlr;
1221 rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id,
1222 cmd->device->lun, scsi3addr);
1223 if (rc != 0) {
1224 /* the scsi nexus does not match any that we presented... */
1225 /* pretend to mid layer that we got selection timeout */
1226 cmd->result = DID_NO_CONNECT << 16;
1227 done(cmd);
1228 /* we might want to think about registering controller itself
1229 as a processor device on the bus so sg binds to it. */
1230 return 0;
1233 /* printk("cciss_queue_command, p=%p, cmd=0x%02x, c%db%dt%dl%d\n",
1234 cmd, cmd->cmnd[0], ctlr, cmd->channel, cmd->target, cmd->lun);*/
1235 // printk("q:%p:c%db%dt%dl%d ", cmd, ctlr, cmd->channel,
1236 // cmd->target, cmd->lun);
1238 /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1239 see what the device thinks of it. */
1241 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1242 cp = scsi_cmd_alloc(*c);
1243 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1244 if (cp == NULL) { /* trouble... */
1245 printk("scsi_cmd_alloc returned NULL!\n");
1246 /* FIXME: next 3 lines are -> BAD! <- */
1247 cmd->result = DID_NO_CONNECT << 16;
1248 done(cmd);
1249 return 0;
1252 // Fill in the command list header
1254 cmd->scsi_done = done; // save this for use by completion code
1256 // save cp in case we have to abort it
1257 cmd->host_scribble = (unsigned char *) cp;
1259 cp->cmd_type = CMD_SCSI;
1260 cp->scsi_cmd = cmd;
1261 cp->Header.ReplyQueue = 0; // unused in simple mode
1262 memcpy(&cp->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
1263 cp->Header.Tag.lower = cp->busaddr; // Use k. address of cmd as tag
1265 // Fill in the request block...
1267 cp->Request.Timeout = 0;
1268 memset(cp->Request.CDB, 0, sizeof(cp->Request.CDB));
1269 if (cmd->cmd_len > sizeof(cp->Request.CDB)) BUG();
1270 cp->Request.CDBLen = cmd->cmd_len;
1271 memcpy(cp->Request.CDB, cmd->cmnd, cmd->cmd_len);
1272 cp->Request.Type.Type = TYPE_CMD;
1273 cp->Request.Type.Attribute = ATTR_SIMPLE;
1274 switch(cmd->sc_data_direction)
1276 case DMA_TO_DEVICE: cp->Request.Type.Direction = XFER_WRITE; break;
1277 case DMA_FROM_DEVICE: cp->Request.Type.Direction = XFER_READ; break;
1278 case DMA_NONE: cp->Request.Type.Direction = XFER_NONE; break;
1279 case DMA_BIDIRECTIONAL:
1280 // This can happen if a buggy application does a scsi passthru
1281 // and sets both inlen and outlen to non-zero. ( see
1282 // ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
1284 cp->Request.Type.Direction = XFER_RSVD;
1285 // This is technically wrong, and cciss controllers should
1286 // reject it with CMD_INVALID, which is the most correct
1287 // response, but non-fibre backends appear to let it
1288 // slide by, and give the same results as if this field
1289 // were set correctly. Either way is acceptable for
1290 // our purposes here.
1292 break;
1294 default:
1295 printk("cciss: unknown data direction: %d\n",
1296 cmd->sc_data_direction);
1297 BUG();
1298 break;
1301 cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1303 /* Put the request on the tail of the request queue */
1305 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1306 addQ(&(*c)->reqQ, cp);
1307 (*c)->Qdepth++;
1308 start_io(*c);
1309 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1311 /* the cmd'll come back via intr handler in complete_scsi_command() */
1312 return 0;
1315 static void
1316 cciss_unregister_scsi(int ctlr)
1318 struct cciss_scsi_adapter_data_t *sa;
1319 struct cciss_scsi_cmd_stack_t *stk;
1320 unsigned long flags;
1322 /* we are being forcibly unloaded, and may not refuse. */
1324 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1325 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1326 stk = &sa->cmd_stack;
1328 /* if we weren't ever actually registered, don't unregister */
1329 if (sa->registered) {
1330 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1331 scsi_remove_host(sa->scsi_host);
1332 scsi_host_put(sa->scsi_host);
1333 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1336 /* set scsi_host to NULL so our detect routine will
1337 find us on register */
1338 sa->scsi_host = NULL;
1339 scsi_cmd_stack_free(ctlr);
1340 kfree(sa);
1341 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1344 static int
1345 cciss_register_scsi(int ctlr)
1347 unsigned long flags;
1349 CPQ_TAPE_LOCK(ctlr, flags);
1351 /* Since this is really a block driver, the SCSI core may not be
1352 initialized at init time, in which case, calling scsi_register_host
1353 would hang. Instead, we do it later, via /proc filesystem
1354 and rc scripts, when we know SCSI core is good to go. */
1356 /* Only register if SCSI devices are detected. */
1357 if (ccissscsi[ctlr].ndevices != 0) {
1358 ((struct cciss_scsi_adapter_data_t *)
1359 hba[ctlr]->scsi_ctlr)->registered = 1;
1360 CPQ_TAPE_UNLOCK(ctlr, flags);
1361 return cciss_scsi_detect(ctlr);
1363 CPQ_TAPE_UNLOCK(ctlr, flags);
1364 printk(KERN_INFO
1365 "cciss%d: No appropriate SCSI device detected, "
1366 "SCSI subsystem not engaged.\n", ctlr);
1367 return 0;
1370 static int
1371 cciss_engage_scsi(int ctlr)
1373 struct cciss_scsi_adapter_data_t *sa;
1374 struct cciss_scsi_cmd_stack_t *stk;
1375 unsigned long flags;
1377 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1378 sa = (struct cciss_scsi_adapter_data_t *) hba[ctlr]->scsi_ctlr;
1379 stk = &sa->cmd_stack;
1381 if (((struct cciss_scsi_adapter_data_t *)
1382 hba[ctlr]->scsi_ctlr)->registered) {
1383 printk("cciss%d: SCSI subsystem already engaged.\n", ctlr);
1384 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1385 return ENXIO;
1387 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1388 cciss_update_non_disk_devices(ctlr, -1);
1389 cciss_register_scsi(ctlr);
1390 return 0;
1393 static void
1394 cciss_proc_tape_report(int ctlr, unsigned char *buffer, off_t *pos, off_t *len)
1396 unsigned long flags;
1397 int size;
1399 *pos = *pos -1; *len = *len - 1; // cut off the last trailing newline
1401 CPQ_TAPE_LOCK(ctlr, flags);
1402 size = sprintf(buffer + *len,
1403 " Sequential access devices: %d\n\n",
1404 ccissscsi[ctlr].ndevices);
1405 CPQ_TAPE_UNLOCK(ctlr, flags);
1406 *pos += size; *len += size;
1409 #else /* no CONFIG_CISS_SCSI_TAPE */
1411 /* If no tape support, then these become defined out of existence */
1413 #define cciss_scsi_setup(cntl_num)
1414 #define cciss_unregister_scsi(ctlr)
1415 #define cciss_register_scsi(ctlr)
1416 #define cciss_proc_tape_report(ctlr, buffer, pos, len)
1418 #endif /* CONFIG_CISS_SCSI_TAPE */