[SCSI] remove scsi_set_device
[linux-2.6] / drivers / block / cciss_scsi.c
1 /*
2  *    Disk Array driver for Compaq SA53xx Controllers, SCSI Tape module
3  *    Copyright 2001 Compaq Computer Corporation
4  *
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.
9  *
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.
14  *
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.
18  *
19  *    Questions/Comments/Bugfixes to iss_storagedev@hp.com
20  *    
21  *    Author: Stephen M. Cameron
22  */
23 #ifdef CONFIG_CISS_SCSI_TAPE
24
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. */
30
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>
38
39 #include "cciss_scsi.h"
40
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);
54
55
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 */
63
64 static int cciss_scsi_queue_command (struct scsi_cmnd *cmd,
65                 void (* done)(struct scsi_cmnd *));
66
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 },
76 };
77
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,
89 };
90
91 #pragma pack(1)
92 struct cciss_scsi_cmd_stack_elem_t {
93         CommandList_struct cmd;
94         ErrorInfo_struct Err;
95         __u32 busaddr;
96 };
97
98 #pragma pack()
99
100 #define CMD_STACK_SIZE (SCSI_CCISS_CAN_QUEUE * \
101                 CCISS_MAX_SCSI_DEVS_PER_HBA + 2)
102                         // plus two for init time usage
103
104 #pragma pack(1)
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;
109         int top;
110 };
111 #pragma pack()
112
113 struct cciss_scsi_adapter_data_t {
114         struct Scsi_Host *scsi_host;
115         struct cciss_scsi_cmd_stack_t cmd_stack;
116         int registered;
117         spinlock_t lock; // to protect ccissscsi[ctlr]; 
118 };
119
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);
126
127 static CommandList_struct *
128 scsi_cmd_alloc(ctlr_info_t *h)
129 {
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. */
134
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;
139         u64bit temp64;
140
141         sa = (struct cciss_scsi_adapter_data_t *) h->scsi_ctlr;
142         stk = &sa->cmd_stack; 
143
144         if (stk->top < 0) 
145                 return NULL;
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)); */
154
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)); */
159         stk->top--;
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);
163         
164         c->cmd.ctlr = h->ctlr;
165         c->cmd.err_info = &c->Err;
166
167         return (CommandList_struct *) c;
168 }
169
170 static void 
171 scsi_cmd_free(ctlr_info_t *h, CommandList_struct *cmd)
172 {
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. */
176
177         struct cciss_scsi_adapter_data_t *sa;
178         struct cciss_scsi_cmd_stack_t *stk;
179
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");
184                 BUG();
185         }
186         stk->top++;
187         stk->elem[stk->top] = (struct cciss_scsi_cmd_stack_elem_t *) cmd;
188 }
189
190 static int
191 scsi_cmd_stack_setup(int ctlr, struct cciss_scsi_adapter_data_t *sa)
192 {
193         int i;
194         struct cciss_scsi_cmd_stack_t *stk;
195         size_t size;
196
197         stk = &sa->cmd_stack; 
198         size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
199
200         // pci_alloc_consistent guarantees 32-bit DMA address will
201         // be used
202
203         stk->pool = (struct cciss_scsi_cmd_stack_elem_t *)
204                 pci_alloc_consistent(hba[ctlr]->pdev, size, &stk->cmd_pool_handle);
205
206         if (stk->pool == NULL) {
207                 printk("stk->pool is null\n");
208                 return -1;
209         }
210
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));
215         }
216         stk->top = CMD_STACK_SIZE-1;
217         return 0;
218 }
219
220 static void
221 scsi_cmd_stack_free(int ctlr)
222 {
223         struct cciss_scsi_adapter_data_t *sa;
224         struct cciss_scsi_cmd_stack_t *stk;
225         size_t size;
226
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);
232                 // BUG();
233                 printk("WE HAVE A BUG HERE!!! stk=0x%p\n", stk);
234         }
235         size = sizeof(struct cciss_scsi_cmd_stack_elem_t) * CMD_STACK_SIZE;
236
237         pci_free_consistent(hba[ctlr]->pdev, size, stk->pool, stk->cmd_pool_handle);
238         stk->pool = NULL;
239 }
240
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]
244
245 #if 0
246 static int xmargin=8;
247 static int amargin=60;
248
249 static void
250 print_bytes (unsigned char *c, int len, int hex, int ascii)
251 {
252
253         int i;
254         unsigned char *x;
255
256         if (hex)
257         {
258                 x = c;
259                 for (i=0;i<len;i++)
260                 {
261                         if ((i % xmargin) == 0 && i>0) printk("\n");
262                         if ((i % xmargin) == 0) printk("0x%04x:", i);
263                         printk(" %02x", *x);
264                         x++;
265                 }
266                 printk("\n");
267         }
268         if (ascii)
269         {
270                 x = c;
271                 for (i=0;i<len;i++)
272                 {
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);
276                         else printk(".");
277                         x++;
278                 }
279                 printk("\n");
280         }
281 }
282
283 static void
284 print_cmd(CommandList_struct *cp)
285 {
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, 
317                         cp->ErrDesc.Len);
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);
326                         
327 }
328
329 #endif
330
331 static int 
332 find_bus_target_lun(int ctlr, int *bus, int *target, int *lun)
333 {
334         /* finds an unused bus, target, lun for a new device */
335         /* assumes hba[ctlr]->scsi_ctlr->lock is held */ 
336         int i, found=0;
337         unsigned char target_taken[CCISS_MAX_SCSI_DEVS_PER_HBA];
338
339         memset(&target_taken[0], 0, CCISS_MAX_SCSI_DEVS_PER_HBA);
340
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;
344         
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;
348                         break;
349                 }
350         }
351         return (!found);        
352 }
353
354 static int 
355 cciss_scsi_add_entry(int ctlr, int hostno, 
356                 unsigned char *scsi3addr, int devtype)
357 {
358         /* assumes hba[ctlr]->scsi_ctlr->lock is held */ 
359         int n = ccissscsi[ctlr].ndevices;
360         struct cciss_scsi_dev_t *sd;
361
362         if (n >= CCISS_MAX_SCSI_DEVS_PER_HBA) {
363                 printk("cciss%d: Too many devices, "
364                         "some will be inaccessible.\n", ctlr);
365                 return -1;
366         }
367         sd = &ccissscsi[ctlr].dev[n];
368         if (find_bus_target_lun(ctlr, &sd->bus, &sd->target, &sd->lun) != 0)
369                 return -1;
370         memcpy(&sd->scsi3addr[0], scsi3addr, 8);
371         sd->devtype = devtype;
372         ccissscsi[ctlr].ndevices++;
373
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) */
377         if (hostno != -1)
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);
381         return 0;
382 }
383
384 static void
385 cciss_scsi_remove_entry(int ctlr, int hostno, int entry)
386 {
387         /* assumes hba[ctlr]->scsi_ctlr->lock is held */ 
388         int i;
389         struct cciss_scsi_dev_t sd;
390
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);
399 }
400
401
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] && \
410         (a)[0] == (b)[0])
411
412 static int
413 adjust_cciss_scsi_table(int ctlr, int hostno,
414         struct cciss_scsi_dev_t sd[], int nsds)
415 {
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. */ 
420
421         int i,j, found, changes=0;
422         struct cciss_scsi_dev_t *csd;
423         unsigned long flags;
424
425         CPQ_TAPE_LOCK(ctlr, flags);
426
427         /* find any devices in ccissscsi[] that are not in 
428            sd[] and remove them from ccissscsi[] */
429
430         i = 0;
431         while(i<ccissscsi[ctlr].ndevices) {
432                 csd = &ccissscsi[ctlr].dev[i];
433                 found=0;
434                 for (j=0;j<nsds;j++) {
435                         if (SCSI3ADDR_EQ(sd[j].scsi3addr,
436                                 csd->scsi3addr)) {
437                                 if (sd[j].devtype == csd->devtype)
438                                         found=2;
439                                 else
440                                         found=1;
441                                 break;
442                         }
443                 }
444
445                 if (found == 0) { /* device no longer present. */ 
446                         changes++;
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 */
452                 } 
453                 else if (found == 1) { /* device is different kind */
454                         changes++;
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. */
463         }
464
465         /* Now, make sure every device listed in sd[] is also
466            listed in ccissscsi[], adding them if they aren't found */
467
468         for (i=0;i<nsds;i++) {
469                 found=0;
470                 for (j=0;j<ccissscsi[ctlr].ndevices;j++) {
471                         csd = &ccissscsi[ctlr].dev[j];
472                         if (SCSI3ADDR_EQ(sd[i].scsi3addr,
473                                 csd->scsi3addr)) {
474                                 if (sd[i].devtype == csd->devtype)
475                                         found=2;        /* found device */
476                                 else
477                                         found=1;        /* found a bug. */
478                                 break;
479                         }
480                 }
481                 if (!found) {
482                         changes++;
483                         if (cciss_scsi_add_entry(ctlr, hostno, 
484                                 &sd[i].scsi3addr[0], sd[i].devtype) != 0)
485                                 break;
486                 } else if (found == 1) {
487                         /* should never happen... */
488                         changes++;
489                         printk("cciss%d: device unexpectedly changed type\n",
490                                 ctlr);
491                         /* but if it does happen, we just ignore that device */
492                 }
493         }
494         CPQ_TAPE_UNLOCK(ctlr, flags);
495
496         if (!changes) 
497                 printk("cciss%d: No device changes detected.\n", ctlr);
498
499         return 0;
500 }
501
502 static int
503 lookup_scsi3addr(int ctlr, int bus, int target, int lun, char *scsi3addr)
504 {
505         int i;
506         struct cciss_scsi_dev_t *sd;
507         unsigned long flags;
508
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 &&
514                     sd->lun == lun) {
515                         memcpy(scsi3addr, &sd->scsi3addr[0], 8);
516                         CPQ_TAPE_UNLOCK(ctlr, flags);
517                         return 0;
518                 }
519         }
520         CPQ_TAPE_UNLOCK(ctlr, flags);
521         return -1;
522 }
523
524 static void 
525 cciss_scsi_setup(int cntl_num)
526 {
527         struct cciss_scsi_adapter_data_t * shba;
528
529         ccissscsi[cntl_num].ndevices = 0;
530         shba = (struct cciss_scsi_adapter_data_t *)
531                 kmalloc(sizeof(*shba), GFP_KERNEL);     
532         if (shba == NULL)
533                 return;
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) {
538                 kfree(shba);
539                 shba = NULL;
540         }
541         hba[cntl_num]->scsi_ctlr = (void *) shba;
542         return;
543 }
544
545 static void
546 complete_scsi_command( CommandList_struct *cp, int timeout, __u32 tag)
547 {
548         struct scsi_cmnd *cmd;
549         ctlr_info_t *ctlr;
550         u64bit addr64;
551         ErrorInfo_struct *ei;
552
553         ei = cp->err_info;
554
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;
558                 return;
559         }
560
561         cmd = (struct scsi_cmnd *) cp->scsi_cmd;        
562         ctlr = hba[cp->ctlr];
563
564         /* undo the DMA mappings */
565
566         if (cmd->use_sg) {
567                 pci_unmap_sg(ctlr->pdev,
568                         cmd->buffer, cmd->use_sg,
569                                 cmd->sc_data_direction); 
570         }
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);
577         }
578
579         cmd->result = (DID_OK << 16);           /* host byte */
580         cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
581         /* cmd->result |= (GOOD < 1); */                /* status byte */
582
583         cmd->result |= (ei->ScsiStatus);
584         /* printk("Scsistatus is 0x%02x\n", ei->ScsiStatus);  */
585
586         /* copy the sense data whether we need to or not. */
587
588         memcpy(cmd->sense_buffer, ei->SenseInfo, 
589                 ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
590                         SCSI_SENSE_BUFFERSIZE : 
591                         ei->SenseLen);
592         cmd->resid = ei->ResidualCnt;
593
594         if(ei->CommandStatus != 0) 
595         { /* an error has occurred */ 
596                 switch(ei->CommandStatus)
597                 {
598                         case CMD_TARGET_STATUS:
599                                 /* Pass it up to the upper layers... */
600                                 if( ei->ScsiStatus)
601                                 {
602 #if 0
603                                         printk(KERN_WARNING "cciss: cmd %p "
604                                         "has SCSI Status = %x\n",
605                                                 cp,  
606                                                 ei->ScsiStatus); 
607 #endif
608                                         cmd->result |= (ei->ScsiStatus < 1);
609                                 }
610                                 else {  /* scsi status is zero??? How??? */
611                                         
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. */
619
620                                         cmd->result = DID_NO_CONNECT << 16;
621                                 }
622                         break;
623                         case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
624                         break;
625                         case CMD_DATA_OVERRUN:
626                                 printk(KERN_WARNING "cciss: cp %p has"
627                                         " completed with data overrun "
628                                         "reported\n", cp);
629                         break;
630                         case CMD_INVALID: {
631                                 /* print_bytes(cp, sizeof(*cp), 1, 0);
632                                 print_cmd(cp); */
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;
639                                 }
640                         break;
641                         case CMD_PROTOCOL_ERR:
642                                 printk(KERN_WARNING "cciss: cp %p has "
643                                         "protocol error \n", cp);
644                         break;
645                         case CMD_HARDWARE_ERR:
646                                 cmd->result = DID_ERROR << 16;
647                                 printk(KERN_WARNING "cciss: cp %p had " 
648                                         " hardware error\n", cp);
649                         break;
650                         case CMD_CONNECTION_LOST:
651                                 cmd->result = DID_ERROR << 16;
652                                 printk(KERN_WARNING "cciss: cp %p had "
653                                         "connection lost\n", cp);
654                         break;
655                         case CMD_ABORTED:
656                                 cmd->result = DID_ABORT << 16;
657                                 printk(KERN_WARNING "cciss: cp %p was "
658                                         "aborted\n", cp);
659                         break;
660                         case CMD_ABORT_FAILED:
661                                 cmd->result = DID_ERROR << 16;
662                                 printk(KERN_WARNING "cciss: cp %p reports "
663                                         "abort failed\n", cp);
664                         break;
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);
669                         break;
670                         case CMD_TIMEOUT:
671                                 cmd->result = DID_TIME_OUT << 16;
672                                 printk(KERN_WARNING "cciss: cp %p timedout\n",
673                                         cp);
674                         break;
675                         default:
676                                 cmd->result = DID_ERROR << 16;
677                                 printk(KERN_WARNING "cciss: cp %p returned "
678                                         "unknown status %x\n", cp, 
679                                                 ei->CommandStatus); 
680                 }
681         }
682         // printk("c:%p:c%db%dt%dl%d ", cmd, ctlr->ctlr, cmd->channel, 
683         //      cmd->target, cmd->lun);
684         cmd->scsi_done(cmd);
685         scsi_cmd_free(ctlr, cp);
686 }
687
688 static int
689 cciss_scsi_detect(int ctlr)
690 {
691         struct Scsi_Host *sh;
692         int error;
693
694         sh = scsi_host_alloc(&cciss_driver_template, sizeof(struct ctlr_info *));
695         if (sh == NULL)
696                 goto fail;
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;  
700
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);
707         if (error)
708                 goto fail_host_put;
709         scsi_scan_host(sh);
710         return 1;
711
712  fail_host_put:
713         scsi_host_put(sh);
714  fail:
715         return 0;
716 }
717
718 static void
719 cciss_unmap_one(struct pci_dev *pdev,
720                 CommandList_struct *cp,
721                 size_t buflen,
722                 int data_direction)
723 {
724         u64bit addr64;
725
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);
729 }
730
731 static void
732 cciss_map_one(struct pci_dev *pdev,
733                 CommandList_struct *cp,
734                 unsigned char *buf,
735                 size_t buflen,
736                 int data_direction)
737 {
738         __u64 addr64;
739
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 */
748 }
749
750 static int
751 cciss_scsi_do_simple_cmd(ctlr_info_t *c,
752                         CommandList_struct *cp,
753                         unsigned char *scsi3addr, 
754                         unsigned char *cdb,
755                         unsigned char cdblen,
756                         unsigned char *buf, int bufsize,
757                         int direction)
758 {
759         unsigned long flags;
760         DECLARE_COMPLETION(wait);
761
762         cp->cmd_type = CMD_IOCTL_PEND;          // treat this like an ioctl 
763         cp->scsi_cmd = NULL;
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...
768
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]); */
772
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;
780
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); 
784
785         cp->waiting = &wait;
786
787         /* Put the request on the tail of the request queue */
788         spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
789         addQ(&c->reqQ, cp);
790         c->Qdepth++;
791         start_io(c);
792         spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
793
794         wait_for_completion(&wait);
795
796         /* undo the dma mapping */
797         cciss_unmap_one(c->pdev, cp, bufsize, DMA_FROM_DEVICE);
798         return(0);
799 }
800
801 static void 
802 cciss_scsi_interpret_error(CommandList_struct *cp)
803 {
804         ErrorInfo_struct *ei;
805
806         ei = cp->err_info; 
807         switch(ei->CommandStatus)
808         {
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",
814                                         cp,  
815                                         ei->ScsiStatus);
816                         if (ei->ScsiStatus == 0)
817                                 printk(KERN_WARNING 
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");
822                 break;
823                 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
824                         printk("UNDERRUN\n");
825                 break;
826                 case CMD_DATA_OVERRUN:
827                         printk(KERN_WARNING "cciss: cp %p has"
828                                 " completed with data overrun "
829                                 "reported\n", cp);
830                 break;
831                 case CMD_INVALID: {
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", 
837                                 cp); 
838                         /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
839                         print_cmd(cp);  */
840                         }
841                 break;
842                 case CMD_PROTOCOL_ERR:
843                         printk(KERN_WARNING "cciss: cp %p has "
844                                 "protocol error \n", cp);
845                 break;
846                 case CMD_HARDWARE_ERR:
847                         /* cmd->result = DID_ERROR << 16; */
848                         printk(KERN_WARNING "cciss: cp %p had " 
849                                 " hardware error\n", cp);
850                 break;
851                 case CMD_CONNECTION_LOST:
852                         printk(KERN_WARNING "cciss: cp %p had "
853                                 "connection lost\n", cp);
854                 break;
855                 case CMD_ABORTED:
856                         printk(KERN_WARNING "cciss: cp %p was "
857                                 "aborted\n", cp);
858                 break;
859                 case CMD_ABORT_FAILED:
860                         printk(KERN_WARNING "cciss: cp %p reports "
861                                 "abort failed\n", cp);
862                 break;
863                 case CMD_UNSOLICITED_ABORT:
864                         printk(KERN_WARNING "cciss: cp %p aborted "
865                                 "do to an unsolicited abort\n", cp);
866                 break;
867                 case CMD_TIMEOUT:
868                         printk(KERN_WARNING "cciss: cp %p timedout\n",
869                                 cp);
870                 break;
871                 default:
872                         printk(KERN_WARNING "cciss: cp %p returned "
873                                 "unknown status %x\n", cp, 
874                                         ei->CommandStatus); 
875         }
876 }
877
878 static int
879 cciss_scsi_do_inquiry(ctlr_info_t *c, unsigned char *scsi3addr, 
880                  InquiryData_struct *buf)
881 {
882         int rc;
883         CommandList_struct *cp;
884         char cdb[6];
885         ErrorInfo_struct *ei;
886         unsigned long flags;
887
888         spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
889         cp = scsi_cmd_alloc(c);
890         spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
891
892         if (cp == NULL) {                       /* trouble... */
893                 printk("cmd_alloc returned NULL!\n");
894                 return -1;
895         }
896
897         ei = cp->err_info; 
898
899         cdb[0] = CISS_INQUIRY;
900         cdb[1] = 0;
901         cdb[2] = 0;
902         cdb[3] = 0;
903         cdb[4] = sizeof(*buf) & 0xff;
904         cdb[5] = 0;
905         rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, cdb, 
906                                 6, (unsigned char *) buf, 
907                                 sizeof(*buf), XFER_READ);
908
909         if (rc != 0) return rc; /* something went wrong */
910
911         if (ei->CommandStatus != 0 && 
912             ei->CommandStatus != CMD_DATA_UNDERRUN) {
913                 cciss_scsi_interpret_error(cp);
914                 rc = -1;
915         }
916         spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
917         scsi_cmd_free(c, cp);
918         spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
919         return rc;      
920 }
921
922 static int
923 cciss_scsi_do_report_phys_luns(ctlr_info_t *c, 
924                 ReportLunData_struct *buf, int bufsize)
925 {
926         int rc;
927         CommandList_struct *cp;
928         unsigned char cdb[12];
929         unsigned char scsi3addr[8]; 
930         ErrorInfo_struct *ei;
931         unsigned long flags;
932
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");
938                 return -1;
939         }
940
941         memset(&scsi3addr[0], 0, 8); /* address the controller */
942         cdb[0] = CISS_REPORT_PHYS;
943         cdb[1] = 0;
944         cdb[2] = 0;
945         cdb[3] = 0;
946         cdb[4] = 0;
947         cdb[5] = 0;
948         cdb[6] = (bufsize >> 24) & 0xFF;  //MSB
949         cdb[7] = (bufsize >> 16) & 0xFF;
950         cdb[8] = (bufsize >> 8) & 0xFF;
951         cdb[9] = bufsize & 0xFF;
952         cdb[10] = 0;
953         cdb[11] = 0;
954
955         rc = cciss_scsi_do_simple_cmd(c, cp, scsi3addr, 
956                                 cdb, 12, 
957                                 (unsigned char *) buf, 
958                                 bufsize, XFER_READ);
959
960         if (rc != 0) return rc; /* something went wrong */
961
962         ei = cp->err_info; 
963         if (ei->CommandStatus != 0 && 
964             ei->CommandStatus != CMD_DATA_UNDERRUN) {
965                 cciss_scsi_interpret_error(cp);
966                 rc = -1;
967         }
968         spin_lock_irqsave(CCISS_LOCK(c->ctlr), flags);
969         scsi_cmd_free(c, cp);
970         spin_unlock_irqrestore(CCISS_LOCK(c->ctlr), flags);
971         return rc;      
972 }
973
974 static void
975 cciss_update_non_disk_devices(int cntl_num, int hostno)
976 {
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.
984
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.
989
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
1000            that though.  
1001
1002          */
1003
1004         ReportLunData_struct *ld_buff;
1005         InquiryData_struct *inq_buff;
1006         unsigned char scsi3addr[8];
1007         ctlr_info_t *c;
1008         __u32 num_luns=0;
1009         unsigned char *ch;
1010         /* unsigned char found[CCISS_MAX_SCSI_DEVS_PER_HBA]; */
1011         struct cciss_scsi_dev_t currentsd[CCISS_MAX_SCSI_DEVS_PER_HBA];
1012         int ncurrent=0;
1013         int reportlunsize = sizeof(*ld_buff) + CISS_MAX_PHYS_LUN * 8;
1014         int i;
1015
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");
1020                 return;
1021         }
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");
1026                 kfree(ld_buff);
1027                 return;
1028         }
1029
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) {
1034                         printk(KERN_WARNING 
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;
1039                 }
1040         }
1041         else {
1042                 printk(KERN_ERR  "cciss: Report physical LUNs failed.\n");
1043                 goto out;
1044         }
1045
1046
1047         /* adjust our table of devices */       
1048         for(i=0; i<num_luns; i++)
1049         {
1050                 int devtype;
1051
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);
1056
1057                 if (cciss_scsi_do_inquiry(hba[cntl_num], 
1058                         scsi3addr, inq_buff) != 0)
1059                 {
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);
1064
1065                 switch (devtype)
1066                 {
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));
1073                                 break;
1074                         }
1075                         memcpy(&currentsd[ncurrent].scsi3addr[0], 
1076                                 &scsi3addr[0], 8);
1077                         currentsd[ncurrent].devtype = devtype;
1078                         currentsd[ncurrent].bus = -1;
1079                         currentsd[ncurrent].target = -1;
1080                         currentsd[ncurrent].lun = -1;
1081                         ncurrent++;
1082                         break;
1083                   default: 
1084                         break;
1085                 }
1086         }
1087
1088         adjust_cciss_scsi_table(cntl_num, hostno, currentsd, ncurrent);
1089 out:
1090         kfree(inq_buff);
1091         kfree(ld_buff);
1092         return;
1093 }
1094
1095 static int
1096 is_keyword(char *ptr, int len, char *verb)  // Thanks to ncr53c8xx.c
1097 {
1098         int verb_len = strlen(verb);
1099         if (len >= verb_len && !memcmp(verb,ptr,verb_len))
1100                 return verb_len;
1101         else
1102                 return 0;
1103 }
1104
1105 static int
1106 cciss_scsi_user_command(int ctlr, int hostno, char *buffer, int length)
1107 {
1108         int arg_len;
1109
1110         if ((arg_len = is_keyword(buffer, length, "rescan")) != 0)
1111                 cciss_update_non_disk_devices(ctlr, hostno);
1112         else
1113                 return -EINVAL;
1114         return length;
1115 }
1116
1117
1118 static int
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 */
1125 {
1126
1127         int buflen, datalen;
1128         ctlr_info_t *ci;
1129         int cntl_num;
1130
1131
1132         ci = (ctlr_info_t *) sh->hostdata[0];
1133         if (ci == NULL)  /* This really shouldn't ever happen. */
1134                 return -EINVAL;
1135
1136         cntl_num = ci->ctlr;    /* Get our index into the hba[] array */
1137
1138         if (func == 0) {        /* User is reading from /proc/scsi/ciss*?/?*  */
1139                 buflen = sprintf(buffer, "hostnum=%d\n", sh->host_no);  
1140
1141                 datalen = buflen - offset;
1142                 if (datalen < 0) {      /* they're reading past EOF. */
1143                         datalen = 0;
1144                         *start = buffer+buflen; 
1145                 } else
1146                         *start = buffer + offset;
1147                 return(datalen);
1148         } else  /* User is writing to /proc/scsi/cciss*?/?*  ... */
1149                 return cciss_scsi_user_command(cntl_num, sh->host_no,
1150                         buffer, length);        
1151
1152
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. */
1156
1157 static void
1158 cciss_scatter_gather(struct pci_dev *pdev, 
1159                 CommandList_struct *cp, 
1160                 struct scsi_cmnd *cmd)
1161 {
1162         unsigned int use_sg, nsegs=0, len;
1163         struct scatterlist *scatter = (struct scatterlist *) cmd->buffer;
1164         __u64 addr64;
1165
1166         /* is it just one virtual address? */   
1167         if (!cmd->use_sg) {
1168                 if (cmd->request_bufflen) {     /* anything to xfer? */
1169
1170                         addr64 = (__u64) pci_map_single(pdev, 
1171                                 cmd->request_buffer, 
1172                                 cmd->request_bufflen, 
1173                                 cmd->sc_data_direction); 
1174         
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;
1180                         nsegs=1;
1181                 }
1182         } /* else, must be a list of virtual addresses.... */
1183         else if (cmd->use_sg <= MAXSGENTRIES) { /* not too many addrs? */
1184
1185                 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, 
1186                         cmd->sc_data_direction);
1187
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
1197                 }
1198         } else BUG();
1199
1200         cp->Header.SGList = (__u8) nsegs;   /* no. SGs contig in this cmd */
1201         cp->Header.SGTotal = (__u16) nsegs; /* total sgs in this cmd list */
1202         return;
1203 }
1204
1205
1206 static int
1207 cciss_scsi_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
1208 {
1209         ctlr_info_t **c;
1210         int ctlr, rc;
1211         unsigned char scsi3addr[8];
1212         CommandList_struct *cp;
1213         unsigned long flags;
1214
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];   
1218         ctlr = (*c)->ctlr;
1219
1220         rc = lookup_scsi3addr(ctlr, cmd->device->channel, cmd->device->id, 
1221                         cmd->device->lun, scsi3addr);
1222         if (rc != 0) {
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;
1226                 done(cmd);
1227                 /* we might want to think about registering controller itself
1228                    as a processor device on the bus so sg binds to it. */
1229                 return 0;
1230         }
1231
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);
1236
1237         /* Ok, we have a reasonable scsi nexus, so send the cmd down, and
1238            see what the device thinks of it. */
1239
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;
1247                 done(cmd);
1248                 return 0;
1249         }
1250
1251         // Fill in the command list header
1252
1253         cmd->scsi_done = done;    // save this for use by completion code 
1254
1255         // save cp in case we have to abort it 
1256         cmd->host_scribble = (unsigned char *) cp; 
1257
1258         cp->cmd_type = CMD_SCSI;
1259         cp->scsi_cmd = cmd;
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
1263         
1264         // Fill in the request block...
1265
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)
1274         {
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() )
1282
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.
1290
1291                 break;
1292
1293           default: 
1294                 printk("cciss: unknown data direction: %d\n", 
1295                         cmd->sc_data_direction);
1296                 BUG();
1297                 break;
1298         }
1299
1300         cciss_scatter_gather((*c)->pdev, cp, cmd); // Fill the SG list
1301
1302         /* Put the request on the tail of the request queue */
1303
1304         spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1305         addQ(&(*c)->reqQ, cp);
1306         (*c)->Qdepth++;
1307         start_io(*c);
1308         spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1309
1310         /* the cmd'll come back via intr handler in complete_scsi_command()  */
1311         return 0;
1312 }
1313
1314 static void 
1315 cciss_unregister_scsi(int ctlr)
1316 {
1317         struct cciss_scsi_adapter_data_t *sa;
1318         struct cciss_scsi_cmd_stack_t *stk;
1319         unsigned long flags;
1320
1321         /* we are being forcibly unloaded, and may not refuse. */
1322
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; 
1326
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);
1333         }
1334
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);
1339         kfree(sa);
1340         spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1341 }
1342
1343 static int 
1344 cciss_register_scsi(int ctlr)
1345 {
1346         unsigned long flags;
1347
1348         CPQ_TAPE_LOCK(ctlr, flags);
1349
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. */
1354
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);
1361         }
1362         CPQ_TAPE_UNLOCK(ctlr, flags);
1363         printk(KERN_INFO 
1364                 "cciss%d: No appropriate SCSI device detected, "
1365                 "SCSI subsystem not engaged.\n", ctlr);
1366         return 0;
1367 }
1368
1369 static int 
1370 cciss_engage_scsi(int ctlr)
1371 {
1372         struct cciss_scsi_adapter_data_t *sa;
1373         struct cciss_scsi_cmd_stack_t *stk;
1374         unsigned long flags;
1375
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; 
1379
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);
1384                 return ENXIO;
1385         }
1386         spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1387         cciss_update_non_disk_devices(ctlr, -1);
1388         cciss_register_scsi(ctlr);
1389         return 0;
1390 }
1391
1392 static void
1393 cciss_proc_tape_report(int ctlr, unsigned char *buffer, off_t *pos, off_t *len)
1394 {
1395         unsigned long flags;
1396         int size;
1397
1398         *pos = *pos -1; *len = *len - 1; // cut off the last trailing newline
1399
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;
1406 }
1407
1408 #else /* no CONFIG_CISS_SCSI_TAPE */
1409
1410 /* If no tape support, then these become defined out of existence */
1411
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)
1416
1417 #endif /* CONFIG_CISS_SCSI_TAPE */