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