added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.git] / drivers / block / cciss.c
1 /*
2  *    Disk Array driver for HP SA 5xxx and 6xxx Controllers
3  *    Copyright 2000, 2002 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; 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 Cciss-discuss@lists.sourceforge.net
20  *
21  */
22
23 #include <linux/config.h>       /* CONFIG_PROC_FS */
24 #include <linux/module.h>
25 #include <linux/version.h>
26 #include <linux/types.h>
27 #include <linux/pci.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/major.h>
32 #include <linux/fs.h>
33 #include <linux/blkpg.h>
34 #include <linux/timer.h>
35 #include <linux/proc_fs.h>
36 #include <linux/init.h> 
37 #include <linux/hdreg.h>
38 #include <linux/spinlock.h>
39 #include <asm/uaccess.h>
40 #include <asm/io.h>
41
42 #include <linux/blk.h>
43 #include <linux/blkdev.h>
44 #include <linux/genhd.h>
45
46 #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
47 #define DRIVER_NAME "HP CISS Driver (v 2.4.42)"
48 #define DRIVER_VERSION CCISS_DRIVER_VERSION(2,4,42)
49
50 /* Embedded module documentation macros - see modules.h */
51 MODULE_AUTHOR("Charles M. White III - Hewlett-Packard Company");
52 MODULE_DESCRIPTION("Driver for HP SA5xxx SA6xxx Controllers version 2.4.42");
53 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"); 
54 MODULE_LICENSE("GPL");
55
56 #include "cciss_cmd.h"
57 #include "cciss.h"
58 #include <linux/cciss_ioctl.h>
59
60 /* define the PCI info for the cards we can control */
61 const struct pci_device_id cciss_pci_device_id[] = {
62         { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS,
63                         0x0E11, 0x4070, 0, 0, 0},
64         { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
65                         0x0E11, 0x4080, 0, 0, 0},
66         { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
67                         0x0E11, 0x4082, 0, 0, 0},
68         { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
69                         0x0E11, 0x4083, 0, 0, 0},
70         { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
71                         0x0E11, 0x409A, 0, 0, 0},
72         { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
73                         0x0E11, 0x409B, 0, 0, 0},
74         { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
75                         0x0E11, 0x409C, 0, 0, 0},
76         {0,}
77 };
78 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
79
80 #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
81
82 /*  board_id = Subsystem Device ID & Vendor ID
83  *  product = Marketing Name for the board
84  *  access = Address of the struct of function pointers 
85  */
86 static struct board_type products[] = {
87         { 0x40700E11, "Smart Array 5300", &SA5_access},
88         { 0x40800E11, "Smart Array 5i", &SA5B_access},
89         { 0x40820E11, "Smart Array 532", &SA5B_access},
90         { 0x40830E11, "Smart Array 5312", &SA5B_access},
91         { 0x409A0E11, "Smart Array 641", &SA5_access},
92         { 0x409B0E11, "Smart Array 642", &SA5_access},
93         { 0x409C0E11, "Smart Array 6400", &SA5_access},
94 };
95
96 /* How long to wait (in millesconds) for board to go into simple mode */
97 #define MAX_CONFIG_WAIT 1000 
98
99 /*define how many times we will try a command because of bus resets */
100 #define MAX_CMD_RETRIES 3
101
102 #define READ_AHEAD       128
103 #define NR_CMDS          128 /* #commands that can be outstanding */
104 #define MAX_CTLR 8
105
106 #define CCISS_DMA_MASK  0xFFFFFFFF      /* 32 bit DMA */
107
108 static ctlr_info_t *hba[MAX_CTLR];
109
110 static struct proc_dir_entry *proc_cciss;
111
112 static void do_cciss_request(request_queue_t *q);
113 static int cciss_open(struct inode *inode, struct file *filep);
114 static int cciss_release(struct inode *inode, struct file *filep);
115 static int cciss_ioctl(struct inode *inode, struct file *filep, 
116                 unsigned int cmd, unsigned long arg);
117
118 static int revalidate_logvol(kdev_t dev, int maxusage);
119 static int frevalidate_logvol(kdev_t dev);
120 static int deregister_disk(int ctlr, int logvol);
121 static int register_new_disk(int cltr, int opened_vol, __u64 requested_lun);
122 static int cciss_rescan_disk(int cltr, int logvol);
123
124 static void cciss_getgeometry(int cntl_num);
125
126 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c);
127 static void start_io( ctlr_info_t *h);
128
129 #ifdef CONFIG_PROC_FS
130 static int cciss_proc_get_info(char *buffer, char **start, off_t offset, 
131                 int length, int *eof, void *data);
132 static void cciss_procinit(int i);
133 #else
134 static int cciss_proc_get_info(char *buffer, char **start, off_t offset, 
135                 int length, int *eof, void *data) { return 0;}
136 static void cciss_procinit(int i) {}
137 #endif /* CONFIG_PROC_FS */
138
139 static struct block_device_operations cciss_fops  = {
140         owner:                  THIS_MODULE,
141         open:                   cciss_open, 
142         release:                cciss_release,
143         ioctl:                  cciss_ioctl,
144         revalidate:             frevalidate_logvol,
145 };
146
147 #include "cciss_scsi.c"         /* For SCSI tape support */
148
149 #define ENG_GIG 1048576000
150 #define ENG_GIG_FACTOR (ENG_GIG/512)
151 #define RAID_UNKNOWN 6
152 static const char *raid_label[] = {"0","4","1(0+1)","5","5+1","ADG",
153                                    "UNKNOWN"};
154 /*
155  * Report information about this controller.
156  */
157 #ifdef CONFIG_PROC_FS
158 static int cciss_proc_get_info(char *buffer, char **start, off_t offset, 
159                 int length, int *eof, void *data)
160 {
161         off_t pos = 0;
162         off_t len = 0;
163         int size, i, ctlr;
164         ctlr_info_t *h = (ctlr_info_t*)data;
165         drive_info_struct *drv;
166         unsigned long flags;
167         unsigned int vol_sz, vol_sz_frac;
168
169         spin_lock_irqsave(&io_request_lock, flags);
170         if (h->busy_configuring) {
171                 spin_unlock_irqrestore(&io_request_lock, flags);
172                 return -EBUSY;
173         }
174         h->busy_configuring = 1;
175         spin_unlock_irqrestore(&io_request_lock, flags);
176                 
177         ctlr = h->ctlr;
178         size = sprintf(buffer, "%s: HP %s Controller\n"
179                 "Board ID: 0x%08lx\n"
180                 "Firmware Version: %c%c%c%c\n"
181                 "IRQ: %d\n"
182                 "Logical drives: %d\n"
183                 "Current Q depth: %d\n"
184                 "Current # commands on controller: %d\n"
185                 "Max Q depth since init: %d\n"
186                 "Max # commands on controller since init: %d\n"
187                 "Max SG entries since init: %d\n\n",
188                 h->devname,
189                 h->product_name,
190                 (unsigned long)h->board_id,
191                 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
192                 (unsigned int)h->intr,
193                 h->num_luns, 
194                 h->Qdepth, h->commands_outstanding,
195                 h->maxQsinceinit, h->max_outstanding, h->maxSG);
196   
197         pos += size; len += size;
198         cciss_proc_tape_report(ctlr, buffer, &pos, &len);
199         for(i=0; i<=h->highest_lun; i++) {
200                 drv = &h->drv[i];
201                 if (drv->nr_blocks == 0)
202                         continue;
203                 vol_sz = drv->nr_blocks/ENG_GIG_FACTOR; 
204                 vol_sz_frac = (drv->nr_blocks%ENG_GIG_FACTOR)*100/ENG_GIG_FACTOR;
205
206                 if (drv->raid_level > 5)
207                         drv->raid_level = RAID_UNKNOWN;
208                 size = sprintf(buffer+len, "cciss/c%dd%d:"
209                                 "\t%4d.%02dGB\tRAID %s\n",
210                                  ctlr, i, vol_sz,vol_sz_frac,
211                                  raid_label[drv->raid_level]);
212                 pos += size, len += size;
213         }
214
215         *eof = 1;
216         *start = buffer+offset;
217         len -= offset;
218         if (len>length)
219                 len = length;
220         h->busy_configuring = 0;
221         return len;
222 }
223
224 static int
225 cciss_proc_write(struct file *file, const char *buffer,
226                         unsigned long count, void *data)
227 {
228         unsigned char cmd[80];
229         int len;
230 #ifdef CONFIG_CISS_SCSI_TAPE
231         ctlr_info_t *h = (ctlr_info_t *) data;
232         int rc;
233 #endif
234
235         if (count > sizeof(cmd)-1) 
236                 return -EINVAL;
237         if (copy_from_user(cmd, buffer, count)) 
238                 return -EFAULT;
239         cmd[count] = '\0';
240         len = strlen(cmd);      
241         if (cmd[len-1] == '\n')
242                 cmd[--len] = '\0';
243 #       ifdef CONFIG_CISS_SCSI_TAPE
244                 if (strcmp("engage scsi", cmd)==0) {
245                         rc = cciss_engage_scsi(h->ctlr);
246                         if (rc != 0) 
247                                 return -rc;
248                         return count;
249                 }
250                 /* might be nice to have "disengage" too, but it's not
251                    safely possible. (only 1 module use count, lock issues.) */
252 #       endif
253         return -EINVAL;
254 }
255
256 /*
257  * Get us a file in /proc/cciss that says something about each controller.
258  * Create /proc/cciss if it doesn't exist yet.
259  */
260 static void __init cciss_procinit(int i)
261 {
262         struct proc_dir_entry *pde;
263
264         if (proc_cciss == NULL) {
265                 proc_cciss = proc_mkdir("cciss", proc_root_driver);
266                 if (!proc_cciss) {
267                         printk("cciss:  proc_mkdir failed\n");
268                         return;
269                 }
270         }
271
272         pde = create_proc_read_entry(hba[i]->devname,
273                 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
274                 proc_cciss, cciss_proc_get_info, hba[i]);
275         pde->write_proc = cciss_proc_write;
276 }
277 #endif /* CONFIG_PROC_FS */
278
279 /* 
280  * For operations that cannot sleep, a command block is allocated at init, 
281  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
282  * which ones are free or in use.  For operations that can wait for kmalloc 
283  * to possible sleep, this routine can be called with get_from_pool set to 0. 
284  * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was. 
285  */ 
286 static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
287 {
288         CommandList_struct *c;
289         int i; 
290         u64bit temp64;
291         dma_addr_t cmd_dma_handle, err_dma_handle;
292
293         if (!get_from_pool) {
294                 c = (CommandList_struct *) pci_alloc_consistent(
295                         h->pdev, sizeof(CommandList_struct), &cmd_dma_handle); 
296                 if (c==NULL)
297                         return NULL;
298                 memset(c, 0, sizeof(CommandList_struct));
299
300                 c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
301                                         h->pdev, sizeof(ErrorInfo_struct), 
302                                         &err_dma_handle);
303         
304                 if (c->err_info == NULL)
305                 {
306                         pci_free_consistent(h->pdev, 
307                                 sizeof(CommandList_struct), c, cmd_dma_handle);
308                         return NULL;
309                 }
310                 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
311         } else /* get it out of the controllers pool */ 
312         {
313                 do {
314                         i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
315                         if (i == NR_CMDS)
316                                 return NULL;
317                 } while(test_and_set_bit(i%32, h->cmd_pool_bits+(i/32)) != 0);
318 #ifdef CCISS_DEBUG
319                 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
320 #endif
321                 c = h->cmd_pool + i;
322                 memset(c, 0, sizeof(CommandList_struct));
323                 cmd_dma_handle = h->cmd_pool_dhandle 
324                                         + i*sizeof(CommandList_struct);
325                 c->err_info = h->errinfo_pool + i;
326                 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
327                 err_dma_handle = h->errinfo_pool_dhandle 
328                                         + i*sizeof(ErrorInfo_struct);
329                 h->nr_allocs++;
330         }
331
332         c->busaddr = (__u32) cmd_dma_handle;
333         temp64.val = (__u64) err_dma_handle;    
334         c->ErrDesc.Addr.lower = temp64.val32.lower;
335         c->ErrDesc.Addr.upper = temp64.val32.upper;
336         c->ErrDesc.Len = sizeof(ErrorInfo_struct);
337         
338         c->ctlr = h->ctlr;
339         return c;
340
341
342 }
343
344 /* 
345  * Frees a command block that was previously allocated with cmd_alloc(). 
346  */
347 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
348 {
349         int i;
350         u64bit temp64;
351
352         if (!got_from_pool) { 
353                 temp64.val32.lower = c->ErrDesc.Addr.lower;
354                 temp64.val32.upper = c->ErrDesc.Addr.upper;
355                 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct), 
356                         c->err_info, (dma_addr_t) temp64.val);
357                 pci_free_consistent(h->pdev, sizeof(CommandList_struct), 
358                         c, (dma_addr_t) c->busaddr);
359         } else 
360         {
361                 i = c - h->cmd_pool;
362                 clear_bit(i%32, h->cmd_pool_bits+(i/32));
363                 h->nr_frees++;
364         }
365 }
366
367 /*  
368  * fills in the disk information. 
369  */
370 static void cciss_geninit( int ctlr)
371 {
372         drive_info_struct *drv;
373         int i,j;
374         
375         /* Loop through each real device */ 
376         hba[ctlr]->gendisk.nr_real = 0; 
377         for(i=0; i< NWD; i++) {
378                 drv = &(hba[ctlr]->drv[i]);
379                 if (!(drv->nr_blocks))
380                         continue;
381                 hba[ctlr]->hd[i << NWD_SHIFT].nr_sects = 
382                 hba[ctlr]->sizes[i << NWD_SHIFT] = drv->nr_blocks;
383
384                 /* for each partition */ 
385                 for(j=0; j<MAX_PART; j++) {
386                         hba[ctlr]->blocksizes[(i<<NWD_SHIFT) + j] = 1024; 
387
388                         hba[ctlr]->hardsizes[ (i<<NWD_SHIFT) + j] = 
389                                 drv->block_size;
390                 }
391         }
392         hba[ctlr]->gendisk.nr_real = hba[ctlr]->highest_lun+1;
393 }
394 /*
395  * Open.  Make sure the device is really there.
396  */
397 static int cciss_open(struct inode *inode, struct file *filep)
398 {
399         int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
400         int dsk  = MINOR(inode->i_rdev) >> NWD_SHIFT;
401
402 #ifdef CCISS_DEBUG
403         printk(KERN_DEBUG "cciss_open %x (%x:%x)\n", inode->i_rdev, ctlr, dsk);
404 #endif /* CCISS_DEBUG */ 
405
406         if (ctlr > MAX_CTLR || hba[ctlr] == NULL)
407                 return -ENXIO;
408         /*
409          * Root is allowed to open raw volume zero even if its not configured
410          * so array config can still work. Root is also allowed to open any
411          * volume that has a LUN ID, so it can issue IOCTL to reread the
412          * disk information.  I don't think I really like this.
413          * but I'm already using way to many device nodes to claim another one
414          * for "raw controller".
415          */
416         if (hba[ctlr]->sizes[MINOR(inode->i_rdev)] == 0) { /* not online? */
417                 if (MINOR(inode->i_rdev) != 0) {         /* not node 0? */
418                         /* if not node 0 make sure it is a partition = 0 */
419                         if (MINOR(inode->i_rdev) & 0x0f) {
420                                 return -ENXIO;
421                                 /* if it is, make sure we have a LUN ID */
422                         } else if (hba[ctlr]->drv[MINOR(inode->i_rdev)
423                                         >> NWD_SHIFT].LunID == 0) {
424                                 return -ENXIO;
425                         }
426                 }
427                 if (!capable(CAP_SYS_ADMIN))
428                         return -EPERM;
429         }
430
431         hba[ctlr]->drv[dsk].usage_count++;
432         hba[ctlr]->usage_count++;
433         return 0;
434 }
435 /*
436  * Close.  Sync first.
437  */
438 static int cciss_release(struct inode *inode, struct file *filep)
439 {
440         int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
441         int dsk  = MINOR(inode->i_rdev) >> NWD_SHIFT;
442
443 #ifdef CCISS_DEBUG
444         printk(KERN_DEBUG "cciss_release %x (%x:%x)\n", inode->i_rdev, ctlr, dsk);
445 #endif /* CCISS_DEBUG */
446
447         /* fsync_dev(inode->i_rdev); */
448
449         hba[ctlr]->drv[dsk].usage_count--;
450         hba[ctlr]->usage_count--;
451         return 0;
452 }
453
454 /*
455  * ioctl 
456  */
457 static int cciss_ioctl(struct inode *inode, struct file *filep, 
458                 unsigned int cmd, unsigned long arg)
459 {
460         int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
461         int dsk  = MINOR(inode->i_rdev) >> NWD_SHIFT;
462
463 #ifdef CCISS_DEBUG
464         printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
465 #endif /* CCISS_DEBUG */ 
466         
467         switch(cmd) {
468            case HDIO_GETGEO:
469            {
470                 struct hd_geometry driver_geo;
471                 if (hba[ctlr]->drv[dsk].cylinders) {
472                         driver_geo.heads = hba[ctlr]->drv[dsk].heads;
473                         driver_geo.sectors = hba[ctlr]->drv[dsk].sectors;
474                         driver_geo.cylinders = hba[ctlr]->drv[dsk].cylinders;
475                 } else {
476                         driver_geo.heads = 0xff;
477                         driver_geo.sectors = 0x3f;
478                         driver_geo.cylinders = 
479                                 hba[ctlr]->drv[dsk].nr_blocks / (0xff*0x3f);
480                 }
481                 driver_geo.start=
482                         hba[ctlr]->hd[MINOR(inode->i_rdev)].start_sect;
483                 if (copy_to_user((void *) arg, &driver_geo,
484                                 sizeof( struct hd_geometry)))
485                         return  -EFAULT;
486                 return 0;
487            }
488         case HDIO_GETGEO_BIG:
489         {
490                 struct hd_big_geometry driver_geo;
491                 if (hba[ctlr]->drv[dsk].cylinders) {
492                         driver_geo.heads = hba[ctlr]->drv[dsk].heads;
493                         driver_geo.sectors = hba[ctlr]->drv[dsk].sectors;
494                         driver_geo.cylinders = hba[ctlr]->drv[dsk].cylinders;
495                 } else {
496                         driver_geo.heads = 0xff;
497                         driver_geo.sectors = 0x3f;
498                         driver_geo.cylinders = 
499                                 hba[ctlr]->drv[dsk].nr_blocks / (0xff*0x3f);
500                 }
501                 driver_geo.start= 
502                 hba[ctlr]->hd[MINOR(inode->i_rdev)].start_sect;
503                 if (copy_to_user((void *) arg, &driver_geo,  
504                                 sizeof( struct hd_big_geometry)))
505                         return  -EFAULT;
506                 return 0;
507         }
508         case BLKRRPART:
509                 if (!capable(CAP_SYS_ADMIN))
510                         return -EPERM;
511                 return revalidate_logvol(inode->i_rdev, 1);
512         case BLKGETSIZE:
513         case BLKGETSIZE64:
514         case BLKFLSBUF:
515         case BLKBSZSET:
516         case BLKBSZGET:
517         case BLKROSET:
518         case BLKROGET:
519         case BLKRASET:
520         case BLKRAGET:
521         case BLKPG:
522         case BLKELVGET:
523         case BLKELVSET:
524                 return blk_ioctl(inode->i_rdev, cmd, arg);
525         case CCISS_GETPCIINFO:
526         {
527                 cciss_pci_info_struct pciinfo;
528
529                 if (!arg) 
530                         return -EINVAL;
531                 pciinfo.bus = hba[ctlr]->pdev->bus->number;
532                 pciinfo.dev_fn = hba[ctlr]->pdev->devfn;
533                 pciinfo.board_id = hba[ctlr]->board_id;
534                 if (copy_to_user((void *) arg, &pciinfo,  sizeof( cciss_pci_info_struct )))
535                         return  -EFAULT;
536                 return 0;
537         }       
538         case CCISS_GETINTINFO:
539         {
540                 cciss_coalint_struct intinfo;
541                 ctlr_info_t *c = hba[ctlr];
542
543                 if (!arg) 
544                         return -EINVAL;
545                 intinfo.delay = readl(&c->cfgtable->HostWrite.CoalIntDelay);
546                 intinfo.count = readl(&c->cfgtable->HostWrite.CoalIntCount);
547                 if (copy_to_user((void *) arg, &intinfo, sizeof( cciss_coalint_struct )))
548                         return -EFAULT;
549                 return 0;
550         }
551         case CCISS_SETINTINFO:
552         {
553                 cciss_coalint_struct intinfo;
554                 ctlr_info_t *c = hba[ctlr];
555                 unsigned long flags;
556                 int i;
557
558                 if (!arg) 
559                         return -EINVAL; 
560                 if (!capable(CAP_SYS_ADMIN)) 
561                         return -EPERM;
562                 if (copy_from_user(&intinfo, (void *) arg, sizeof( cciss_coalint_struct)))
563                         return -EFAULT;
564                 if ( (intinfo.delay == 0 ) && (intinfo.count == 0)) {
565                         return -EINVAL;
566                 }
567
568                 spin_lock_irqsave(&io_request_lock, flags);
569                 /* Can only safely update if no commands outstanding */ 
570                 if (c->commands_outstanding > 0 ) {
571                         spin_unlock_irqrestore(&io_request_lock, flags);
572                         return -EINVAL;
573                 }
574                 /* Update the field, and then ring the doorbell */ 
575                 writel( intinfo.delay, 
576                         &(c->cfgtable->HostWrite.CoalIntDelay));
577                 writel( intinfo.count, 
578                         &(c->cfgtable->HostWrite.CoalIntCount));
579                 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
580
581                 for(i=0;i<MAX_CONFIG_WAIT;i++) {
582                         if (!(readl(c->vaddr + SA5_DOORBELL) 
583                                         & CFGTBL_ChangeReq))
584                                 break;
585                         /* delay and try again */
586                         udelay(1000);
587                 }       
588                 spin_unlock_irqrestore(&io_request_lock, flags);
589                 if (i >= MAX_CONFIG_WAIT)
590                         return -EFAULT;
591                 return 0;
592         }
593         case CCISS_GETNODENAME:
594         {
595                 NodeName_type NodeName;
596                 ctlr_info_t *c = hba[ctlr];
597                 int i; 
598
599                 if (!arg) 
600                         return -EINVAL;
601                 for(i=0;i<16;i++)
602                         NodeName[i] = readb(&c->cfgtable->ServerName[i]);
603                 if (copy_to_user((void *) arg, NodeName, sizeof( NodeName_type)))
604                         return  -EFAULT;
605                 return 0;
606         }
607         case CCISS_SETNODENAME:
608         {
609                 NodeName_type NodeName;
610                 ctlr_info_t *c = hba[ctlr];
611                 unsigned long flags;
612                 int i;
613
614                 if (!arg) 
615                         return -EINVAL;
616                 if (!capable(CAP_SYS_ADMIN)) 
617                         return -EPERM;
618                 
619                 if (copy_from_user(NodeName, (void *) arg, sizeof( NodeName_type)))
620                         return -EFAULT;
621
622                 spin_lock_irqsave(&io_request_lock, flags);
623
624                         /* Update the field, and then ring the doorbell */ 
625                 for(i=0;i<16;i++)
626                         writeb( NodeName[i], &c->cfgtable->ServerName[i]);
627                         
628                 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
629
630                 for(i=0;i<MAX_CONFIG_WAIT;i++) {
631                         if (!(readl(c->vaddr + SA5_DOORBELL) 
632                                         & CFGTBL_ChangeReq))
633                                 break;
634                         /* delay and try again */
635                         udelay(1000);
636                 }       
637                 spin_unlock_irqrestore(&io_request_lock, flags);
638                 if (i >= MAX_CONFIG_WAIT)
639                         return -EFAULT;
640                 return 0;
641         }
642
643         case CCISS_GETHEARTBEAT:
644         {
645                 Heartbeat_type heartbeat;
646                 ctlr_info_t *c = hba[ctlr];
647
648                 if (!arg) 
649                         return -EINVAL;
650                 heartbeat = readl(&c->cfgtable->HeartBeat);
651                 if (copy_to_user((void *) arg, &heartbeat, sizeof( Heartbeat_type)))
652                         return -EFAULT;
653                 return 0;
654         }
655         case CCISS_GETBUSTYPES:
656         {
657                 BusTypes_type BusTypes;
658                 ctlr_info_t *c = hba[ctlr];
659
660                 if (!arg) 
661                         return -EINVAL;
662                 BusTypes = readl(&c->cfgtable->BusTypes);
663                 if (copy_to_user((void *) arg, &BusTypes, sizeof( BusTypes_type) ))
664                         return  -EFAULT;
665                 return 0;
666         }
667         case CCISS_GETFIRMVER:
668         {
669                 FirmwareVer_type firmware;
670
671                 if (!arg) 
672                         return -EINVAL;
673                 memcpy(firmware, hba[ctlr]->firm_ver, 4);
674
675                 if (copy_to_user((void *) arg, firmware, sizeof( FirmwareVer_type)))
676                         return -EFAULT;
677                 return 0;
678         }
679         case CCISS_GETDRIVVER:
680         {
681                 DriverVer_type DriverVer = DRIVER_VERSION;
682
683                 if (!arg) 
684                         return -EINVAL;
685
686                 if (copy_to_user((void *) arg, &DriverVer, sizeof( DriverVer_type) ))
687                         return -EFAULT;
688                 return 0;
689         }
690         case CCISS_RESCANDISK:
691         {
692                 return cciss_rescan_disk(ctlr, dsk);
693         }
694         case CCISS_DEREGDISK:
695                 return deregister_disk(ctlr,dsk);
696
697         case CCISS_REGNEWD:
698                 return register_new_disk(ctlr, dsk, 0);
699         case CCISS_REGNEWDISK:
700         {
701                 __u64 new_logvol;
702
703                 if (!arg) 
704                         return -EINVAL;
705                 if (copy_from_user(&new_logvol, (void *) arg, 
706                         sizeof( __u64)))
707                         return -EFAULT;
708                 return register_new_disk(ctlr, dsk, new_logvol);
709         }
710         case CCISS_GETLUNINFO:
711         {
712                 LogvolInfo_struct luninfo;
713                 int num_parts = 0;
714                 int i, start;
715
716                 luninfo.LunID = hba[ctlr]->drv[dsk].LunID;
717                 luninfo.num_opens = hba[ctlr]->drv[dsk].usage_count;
718
719                 /* count partitions 1 to 15 with sizes > 0 */
720                 start = (dsk << NWD_SHIFT);
721                 for(i=1; i <MAX_PART; i++) {
722                         int minor = start+i;
723                         if (hba[ctlr]->sizes[minor] != 0)
724                                 num_parts++;
725                 }
726                 luninfo.num_parts = num_parts;
727                 if (copy_to_user((void *) arg, &luninfo,
728                                 sizeof( LogvolInfo_struct) ))
729                         return -EFAULT;
730                 return 0;
731         }
732         case CCISS_PASSTHRU:
733         {
734                 IOCTL_Command_struct iocommand;
735                 ctlr_info_t *h = hba[ctlr];
736                 CommandList_struct *c;
737                 char    *buff = NULL;
738                 u64bit  temp64;
739                 unsigned long flags;
740                 DECLARE_COMPLETION(wait);
741
742                 if (!arg) 
743                         return -EINVAL;
744         
745                 if (!capable(CAP_SYS_RAWIO)) 
746                         return -EPERM;
747
748                 if (copy_from_user(&iocommand, (void *) arg, sizeof( IOCTL_Command_struct) ))
749                         return -EFAULT;
750                 if ((iocommand.buf_size < 1) && 
751                                 (iocommand.Request.Type.Direction 
752                                         != XFER_NONE)) {        
753                         return -EINVAL;
754                 } 
755                 /* Check kmalloc limits */
756                 if (iocommand.buf_size > 128000)
757                         return -EINVAL;
758                 if (iocommand.buf_size > 0) {
759                         buff =  kmalloc(iocommand.buf_size, GFP_KERNEL);
760                         if (buff == NULL) 
761                                 return -ENOMEM;
762                 }
763                 if (iocommand.Request.Type.Direction == XFER_WRITE) {
764                         /* Copy the data into the buffer we created */ 
765                         if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
766                         {
767                                 kfree(buff);
768                                 return -EFAULT;
769                         }
770                 }
771                 if ((c = cmd_alloc(h , 0)) == NULL) {
772                         kfree(buff);
773                         return -ENOMEM;
774                 }
775                         /* Fill in the command type */
776                 c->cmd_type = CMD_IOCTL_PEND;
777                         /* Fill in Command Header */
778                 c->Header.ReplyQueue = 0;  /* unused in simple mode */
779                 if (iocommand.buf_size > 0) {   /* buffer to fill */
780                         c->Header.SGList = 1;
781                         c->Header.SGTotal= 1;
782                 } else  {  /* no buffers to fill  */
783                         c->Header.SGList = 0;
784                         c->Header.SGTotal= 0;
785                 }
786                 c->Header.LUN = iocommand.LUN_info;
787                 c->Header.Tag.lower = c->busaddr;  /* use the kernel address */
788                                                 /* the cmd block for tag */
789                 
790                 /* Fill in Request block */
791                 c->Request = iocommand.Request; 
792         
793                 /* Fill in the scatter gather information */
794                 if (iocommand.buf_size > 0 ) {
795                         temp64.val = pci_map_single( h->pdev, buff,
796                                         iocommand.buf_size, 
797                                 PCI_DMA_BIDIRECTIONAL); 
798                         c->SG[0].Addr.lower = temp64.val32.lower;
799                         c->SG[0].Addr.upper = temp64.val32.upper;
800                         c->SG[0].Len = iocommand.buf_size;
801                         c->SG[0].Ext = 0;  /* we are not chaining */
802                 }
803                 c->waiting = &wait;
804
805                 /* Put the request on the tail of the request queue */
806                 spin_lock_irqsave(&io_request_lock, flags);
807                 addQ(&h->reqQ, c);
808                 h->Qdepth++;
809                 start_io(h);
810                 spin_unlock_irqrestore(&io_request_lock, flags);
811
812                 wait_for_completion(&wait);
813
814                 /* unlock the buffers from DMA */
815                 temp64.val32.lower = c->SG[0].Addr.lower;
816                 temp64.val32.upper = c->SG[0].Addr.upper;
817                 pci_unmap_single( h->pdev, (dma_addr_t) temp64.val,
818                         iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
819
820                 /* Copy the error information out */ 
821                 iocommand.error_info = *(c->err_info);
822                 if (copy_to_user((void *) arg, &iocommand, 
823                                 sizeof( IOCTL_Command_struct) ) ) {
824                         kfree(buff);
825                         cmd_free(h, c, 0);
826                         return( -EFAULT);
827                 }       
828
829                 if (iocommand.Request.Type.Direction == XFER_READ) {
830                         /* Copy the data out of the buffer we created */
831                         if (copy_to_user(iocommand.buf, buff, 
832                                                 iocommand.buf_size)) {
833                                 kfree(buff);
834                                 cmd_free(h, c, 0);
835                                 return -EFAULT;
836                         }
837                 }
838                 kfree(buff);
839                 cmd_free(h, c, 0);
840                 return 0;
841         } 
842         case CCISS_BIG_PASSTHRU:
843         {
844                 BIG_IOCTL_Command_struct iocommand;
845                 ctlr_info_t *h = hba[ctlr];
846                 CommandList_struct *c;
847                 char    *buff[MAXSGENTRIES] = {NULL,};
848                 int     buff_size[MAXSGENTRIES] = {0,};
849                 u64bit  temp64;
850                 unsigned long flags;
851                 BYTE sg_used = 0;
852                 int status = 0;
853                 int i;
854                 DECLARE_COMPLETION(wait);
855
856                 if (!arg) 
857                         return -EINVAL;
858                 
859                 if (!capable(CAP_SYS_RAWIO)) 
860                         return -EPERM;
861
862                 if (copy_from_user(&iocommand, (void *) arg, sizeof( BIG_IOCTL_Command_struct) ))
863                         return -EFAULT;
864                 if ((iocommand.buf_size < 1) && 
865                         (iocommand.Request.Type.Direction != XFER_NONE)) {
866                         return -EINVAL;
867                 } 
868                 /* Check kmalloc limits  using all SGs */
869                 if (iocommand.malloc_size > MAX_KMALLOC_SIZE)
870                         return -EINVAL;
871                 if (iocommand.buf_size > iocommand.malloc_size * MAXSGENTRIES)
872                         return -EINVAL;
873                 if (iocommand.buf_size > 0) {
874                         __u32   size_left_alloc = iocommand.buf_size;
875                         BYTE    *data_ptr = (BYTE *) iocommand.buf;
876                         while (size_left_alloc > 0) {
877                                 buff_size[sg_used] = (size_left_alloc 
878                                                         > iocommand.malloc_size)
879                                         ? iocommand.malloc_size : size_left_alloc;
880                                 buff[sg_used] = kmalloc( buff_size[sg_used], 
881                                                 GFP_KERNEL);
882                                 if (buff[sg_used] == NULL) {
883                                         status = -ENOMEM;
884                                         goto cleanup1;
885                                 }
886                                 if (iocommand.Request.Type.Direction == 
887                                                 XFER_WRITE)
888                                    /* Copy the data into the buffer created */
889                                    if (copy_from_user(buff[sg_used], data_ptr, 
890                                                 buff_size[sg_used])) {
891                                         status = -ENOMEM;
892                                         goto cleanup1;                  
893                                    }
894                                 size_left_alloc -= buff_size[sg_used];
895                                 data_ptr += buff_size[sg_used];
896                                 sg_used++;
897                         }
898                         
899                 }
900                 if ((c = cmd_alloc(h , 0)) == NULL) {
901                         status = -ENOMEM;
902                         goto cleanup1;  
903                 }
904                 /* Fill in the command type */
905                 c->cmd_type = CMD_IOCTL_PEND;
906                 /* Fill in Command Header */
907                 c->Header.ReplyQueue = 0;  /* unused in simple mode */
908                 
909                 if (iocommand.buf_size > 0) {   /* buffer to fill */
910                         c->Header.SGList = sg_used;
911                         c->Header.SGTotal= sg_used;
912                 } else  {       /* no buffers to fill */
913                         c->Header.SGList = 0;
914                         c->Header.SGTotal= 0;
915                 }
916                 c->Header.LUN = iocommand.LUN_info;
917                 c->Header.Tag.lower = c->busaddr;  /* use the kernel address */
918                                                 /* the cmd block for tag */
919                 
920         /* Fill in Request block */
921         c->Request = iocommand.Request; 
922         /* Fill in the scatter gather information */
923         if (iocommand.buf_size > 0 ) {
924                 int i;
925                 for(i=0; i< sg_used; i++) {
926                         temp64.val = pci_map_single( h->pdev, buff[i], 
927                                         buff_size[i], 
928                                         PCI_DMA_BIDIRECTIONAL);
929
930                         c->SG[i].Addr.lower = temp64.val32.lower;
931                         c->SG[i].Addr.upper = temp64.val32.upper;
932                         c->SG[i].Len = buff_size[i];
933                         c->SG[i].Ext = 0;  /* we are not chaining */
934                 }
935         }
936         c->waiting = &wait;
937         /* Put the request on the tail of the request queue */
938         spin_lock_irqsave(&io_request_lock, flags);
939         addQ(&h->reqQ, c);
940         h->Qdepth++;
941         start_io(h);
942         spin_unlock_irqrestore(&io_request_lock, flags);
943         wait_for_completion(&wait);
944         /* unlock the buffers from DMA */
945         for(i=0; i< sg_used; i++) {
946                 temp64.val32.lower = c->SG[i].Addr.lower;
947                 temp64.val32.upper = c->SG[i].Addr.upper;
948                 pci_unmap_single( h->pdev, (dma_addr_t) temp64.val,
949                                 buff_size[i], PCI_DMA_BIDIRECTIONAL);
950         }
951         /* Copy the error information out */
952                 iocommand.error_info = *(c->err_info);
953                 if (copy_to_user((void *) arg, &iocommand, 
954                                         sizeof( IOCTL_Command_struct) ) ) {
955                                 cmd_free(h, c, 0);
956                                 status = -EFAULT;
957                                 goto cleanup1;
958                 }
959                 if (iocommand.Request.Type.Direction == XFER_READ) {
960                 /* Copy the data out of the buffer we created */
961                         BYTE *ptr = (BYTE  *) iocommand.buf;
962                         for(i=0; i< sg_used; i++) {
963                                 if (copy_to_user(ptr, buff[i], buff_size[i])) {
964                                         cmd_free(h, c, 0);
965                                         status = -EFAULT;
966                                         goto cleanup1;
967
968                                 }
969                                 ptr += buff_size[i];
970                         }
971                 }
972                 cmd_free(h, c, 0);
973                 status = 0;
974                 
975
976 cleanup1:
977                 for(i=0; i< sg_used; i++) {
978                         if (buff[i] != NULL)
979                                 kfree(buff[i]);
980                 }
981                 return status;
982         }
983         default:
984                 return -EBADRQC;
985         }
986         
987 }
988
989 /* Borrowed and adapted from sd.c */
990 static int revalidate_logvol(kdev_t dev, int maxusage)
991 {
992         int ctlr, target;
993         struct gendisk *gdev;
994         unsigned long flags;
995         int max_p;
996         int start;
997         int i;
998
999         target = MINOR(dev) >> NWD_SHIFT;
1000         ctlr = MAJOR(dev) - MAJOR_NR;
1001         gdev = &(hba[ctlr]->gendisk);
1002
1003         spin_lock_irqsave(&io_request_lock, flags);
1004         if (hba[ctlr]->drv[target].usage_count > maxusage) {
1005                 spin_unlock_irqrestore(&io_request_lock, flags);
1006                 printk(KERN_WARNING "cciss: Device busy for "
1007                         "revalidation (usage=%d)\n",
1008                         hba[ctlr]->drv[target].usage_count);
1009                 return -EBUSY;
1010         }
1011         hba[ctlr]->drv[target].usage_count++;
1012         spin_unlock_irqrestore(&io_request_lock, flags);
1013
1014         max_p = gdev->max_p;
1015         start = target << gdev->minor_shift;
1016
1017         for(i=max_p-1; i>=0; i--) {
1018                 int minor = start+i;
1019                 invalidate_device(MKDEV(MAJOR_NR + ctlr, minor), 1);
1020                 gdev->part[minor].start_sect = 0;
1021                 gdev->part[minor].nr_sects = 0;
1022
1023                 /* reset the blocksize so we can read the partition table */
1024                 blksize_size[MAJOR_NR+ctlr][minor] = 1024;
1025         }
1026         /* setup partitions per disk */
1027         grok_partitions(gdev, target, MAX_PART, 
1028                         hba[ctlr]->drv[target].nr_blocks);
1029         hba[ctlr]->drv[target].usage_count--;
1030         return 0;
1031 }
1032
1033 static int frevalidate_logvol(kdev_t dev)
1034 {
1035 #ifdef CCISS_DEBUG
1036         printk(KERN_DEBUG "cciss: frevalidate has been called\n");
1037 #endif /* CCISS_DEBUG */ 
1038         return revalidate_logvol(dev, 0);
1039 }
1040 static int deregister_disk(int ctlr, int logvol)
1041 {
1042         unsigned long flags;
1043         struct gendisk *gdev = &(hba[ctlr]->gendisk);
1044         ctlr_info_t  *h = hba[ctlr];
1045         int start, max_p, i;
1046
1047         if (!capable(CAP_SYS_RAWIO))
1048                 return -EPERM;
1049
1050         spin_lock_irqsave(&io_request_lock, flags);
1051         /* make sure logical volume is NOT is use */
1052         if (h->drv[logvol].usage_count > 1 || h->busy_configuring) {
1053                 spin_unlock_irqrestore(&io_request_lock, flags);
1054                 return -EBUSY;
1055         }
1056         h->busy_configuring = 1;
1057         spin_unlock_irqrestore(&io_request_lock, flags);
1058
1059         /* invalidate the devices and deregister the disk */
1060         max_p = gdev->max_p;
1061         start = logvol << gdev->minor_shift;
1062         for (i=max_p-1; i>=0; i--) {
1063                 int minor = start+i;
1064                 /* printk("invalidating( %d %d)\n", ctlr, minor); */
1065                 invalidate_device(MKDEV(MAJOR_NR+ctlr, minor), 1);
1066                 /* so open will now fail */
1067                 h->sizes[minor] = 0;
1068                 /* so it will no longer appear in /proc/partitions */
1069                 gdev->part[minor].start_sect = 0;
1070                 gdev->part[minor].nr_sects = 0;
1071         }
1072         /* check to see if it was the last disk */
1073         if (logvol == h->highest_lun) {
1074                 /* if so, find the new hightest lun */
1075                 int i, newhighest =-1;
1076                 for(i=0; i<h->highest_lun; i++) {
1077                         /* if the disk has size > 0, it is available */
1078                         if (h->sizes[i << gdev->minor_shift] != 0)
1079                                 newhighest = i;
1080                 }
1081                 h->highest_lun = newhighest;
1082
1083         }
1084         --h->num_luns;
1085         gdev->nr_real = h->highest_lun+1;
1086         /* zero out the disk size info */
1087         h->drv[logvol].nr_blocks = 0;
1088         h->drv[logvol].block_size = 0;
1089         h->drv[logvol].cylinders = 0;
1090         h->drv[logvol].LunID = 0;
1091         h->busy_configuring = 0;
1092         return 0;
1093 }
1094 static int sendcmd_withirq(__u8 cmd,
1095         int     ctlr,
1096         void    *buff,
1097         size_t  size,
1098         unsigned int use_unit_num,
1099         unsigned int log_unit,
1100         __u8    page_code )
1101 {
1102         ctlr_info_t *h = hba[ctlr];
1103         CommandList_struct *c;
1104         u64bit  buff_dma_handle;
1105         unsigned long flags;
1106         int return_status = IO_OK;
1107         DECLARE_COMPLETION(wait);
1108
1109         if ((c = cmd_alloc(h , 0)) == NULL)
1110                 return -ENOMEM;
1111         c->cmd_type = CMD_IOCTL_PEND;
1112         /* Fill in Command Header */
1113         c->Header.ReplyQueue = 0;  /* unused in simple mode */
1114         if (buff != NULL) {     /* buffer to fill */
1115                 c->Header.SGList = 1;
1116                 c->Header.SGTotal= 1;
1117         } else {
1118                 /* no buffers to fill */
1119                 c->Header.SGList = 0;
1120                 c->Header.SGTotal= 0;
1121         }
1122         c->Header.Tag.lower = c->busaddr;  /* tag is phys addr of cmd */
1123         /* Fill in Request block */
1124         switch (cmd) {
1125                 case  CISS_INQUIRY:
1126                         /* If the logical unit number is 0 then, this is going
1127                                 to controller so It's a physical command
1128                                 mode = 0 target = 0.
1129                                 So we have nothing to write.
1130                                 Otherwise
1131                                 mode = 1  target = LUNID
1132                         */
1133                         if (use_unit_num != 0) {
1134                                 c->Header.LUN.LogDev.VolId =
1135                                         hba[ctlr]->drv[log_unit].LunID;
1136                                 c->Header.LUN.LogDev.Mode = 1;
1137                         }
1138                         if (page_code != 0) {
1139                                 c->Request.CDB[1] = 0x01;
1140                                 c->Request.CDB[2] = page_code;
1141                         }
1142                         c->Request.CDBLen = 6;
1143                         c->Request.Type.Type =  TYPE_CMD;
1144                         c->Request.Type.Attribute = ATTR_SIMPLE;
1145                         c->Request.Type.Direction = XFER_READ; /* Read */
1146                         c->Request.Timeout = 0; /* Don't time out */
1147                         c->Request.CDB[0] =  CISS_INQUIRY;
1148                         c->Request.CDB[4] = size  & 0xFF;
1149                 break;
1150                 case CISS_REPORT_LOG:
1151                         /* Talking to controller so It's a physical command
1152                                 mode = 00 target = 0.
1153                                 So we have nothing to write.
1154                         */
1155                         c->Request.CDBLen = 12;
1156                         c->Request.Type.Type =  TYPE_CMD;
1157                         c->Request.Type.Attribute = ATTR_SIMPLE;
1158                         c->Request.Type.Direction = XFER_READ; /* Read */
1159                         c->Request.Timeout = 0; /* Don't time out */
1160                         c->Request.CDB[0] = CISS_REPORT_LOG;
1161                         c->Request.CDB[6] = (size >> 24) & 0xFF;  /* MSB */
1162                         c->Request.CDB[7] = (size >> 16) & 0xFF;
1163                         c->Request.CDB[8] = (size >> 8) & 0xFF;
1164                         c->Request.CDB[9] = size & 0xFF;
1165                 break;
1166                 case CCISS_READ_CAPACITY:
1167                         c->Header.LUN.LogDev.VolId=
1168                                 hba[ctlr]->drv[log_unit].LunID;
1169                         c->Header.LUN.LogDev.Mode = 1;
1170                         c->Request.CDBLen = 10;
1171                         c->Request.Type.Type =  TYPE_CMD; /* It is a command. */
1172                         c->Request.Type.Attribute = ATTR_SIMPLE;
1173                         c->Request.Type.Direction = XFER_READ; /* Read */
1174                         c->Request.Timeout = 0; /* Don't time out */
1175                         c->Request.CDB[0] = CCISS_READ_CAPACITY;
1176                 break;
1177                 default:
1178                         printk(KERN_WARNING
1179                                 "cciss:  Unknown Command 0x%x sent attempted\n",                                cmd);
1180                         cmd_free(h, c, 1);
1181                         return IO_ERROR;
1182         };
1183
1184         /* Fill in the scatter gather information */
1185         if (size > 0) {
1186                 buff_dma_handle.val = (__u64) pci_map_single( h->pdev,
1187                         buff, size, PCI_DMA_BIDIRECTIONAL);
1188                 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1189                 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1190                 c->SG[0].Len = size;
1191                 c->SG[0].Ext = 0;  /* we are not chaining */
1192         }
1193
1194 resend_cmd2:
1195         c->waiting = &wait;
1196         /* Put the request on the tail of the queue and send it */
1197         spin_lock_irqsave(&io_request_lock, flags);
1198         addQ(&h->reqQ, c);
1199         h->Qdepth++;
1200         start_io(h);
1201         spin_unlock_irqrestore(&io_request_lock, flags);
1202
1203         wait_for_completion(&wait);
1204
1205
1206         if (c->err_info->CommandStatus != 0) {
1207                 /* an error has occurred */
1208                 switch (c->err_info->CommandStatus) {
1209                         case CMD_TARGET_STATUS:
1210                                 printk(KERN_WARNING "cciss: cmd %p has "
1211                                         " completed with errors\n", c);
1212                                 if (c->err_info->ScsiStatus) {
1213                                         printk(KERN_WARNING "cciss: cmd %p "
1214                                         "has SCSI Status = %x\n", c,
1215                                                 c->err_info->ScsiStatus);
1216                                 }
1217                         break;
1218                         case CMD_DATA_UNDERRUN:
1219                         case CMD_DATA_OVERRUN:
1220                         /* expected for inquire and report lun commands */
1221                         break;
1222                         case CMD_INVALID:
1223                                 printk(KERN_WARNING "cciss: cmd %p is "
1224                                         "reported invalid\n", c);
1225                                 return_status = IO_ERROR;
1226                         break;
1227                         case CMD_PROTOCOL_ERR:
1228                                 printk(KERN_WARNING "cciss: cmd %p has "
1229                                         "protocol error \n", c);
1230                                 return_status = IO_ERROR;
1231                         break;
1232                         case CMD_HARDWARE_ERR:
1233                                 printk(KERN_WARNING "cciss: cmd %p had "
1234                                         " hardware error\n", c);
1235                                 return_status = IO_ERROR;
1236                                 break;
1237                         case CMD_CONNECTION_LOST:
1238                                 printk(KERN_WARNING "cciss: cmd %p had "
1239                                         "connection lost\n", c);
1240                                 return_status = IO_ERROR;
1241                         break;
1242                         case CMD_ABORTED:
1243                                 printk(KERN_WARNING "cciss: cmd %p was "
1244                                         "aborted\n", c);
1245                                 return_status = IO_ERROR;
1246                         break;
1247                         case CMD_ABORT_FAILED:
1248                                 printk(KERN_WARNING "cciss: cmd %p reports "
1249                                         "abort failed\n", c);
1250                                 return_status = IO_ERROR;
1251                         break;
1252                         case CMD_UNSOLICITED_ABORT:
1253                                 printk(KERN_WARNING "cciss: cmd %p aborted "
1254                                         "do to an unsolicited abort\n", c);
1255                                 if (c->retry_count < MAX_CMD_RETRIES) 
1256                                 { 
1257                                         printk(KERN_WARNING "retrying cmd\n"); 
1258                                         c->retry_count++; 
1259                                         /* erase the old error */ 
1260                                         /* information */ 
1261                                         memset(c->err_info, 0, 
1262                                                 sizeof(ErrorInfo_struct)); 
1263                                         return_status = IO_OK;
1264                                         INIT_COMPLETION(wait);
1265                                         goto resend_cmd2;
1266                                         
1267                                 }
1268                                 return_status = IO_ERROR;
1269                         break;
1270                         default:
1271                                 printk(KERN_WARNING "cciss: cmd %p returned "
1272                                         "unknown status %x\n", c,
1273                                                 c->err_info->CommandStatus);
1274                                 return_status = IO_ERROR;
1275                 }
1276         }
1277
1278         /* unlock the buffers from DMA */
1279         pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
1280                         size, PCI_DMA_BIDIRECTIONAL);
1281         cmd_free(h, c, 0);
1282         return return_status;
1283 }
1284 static int register_new_disk(int ctlr, int opened_vol, __u64 requested_lun)
1285 {
1286         struct gendisk *gdev = &(hba[ctlr]->gendisk);
1287         ctlr_info_t  *h = hba[ctlr];
1288         int start, max_p, i;
1289         int num_luns;
1290         int logvol;
1291         int new_lun_found = 0;
1292         int new_lun_index = 0;
1293         int free_index_found = 0;
1294         int free_index = 0;
1295         ReportLunData_struct *ld_buff;
1296         ReadCapdata_struct *size_buff;
1297         InquiryData_struct *inq_buff;
1298         int return_code;
1299         int listlength = 0;
1300         __u32 lunid = 0;
1301         unsigned int block_size;
1302         unsigned int total_size;
1303         unsigned long flags;
1304         int req_lunid = (int) (requested_lun & (__u64) 0xffffffff);
1305
1306         if (!capable(CAP_SYS_RAWIO))
1307                 return -EPERM;
1308         /* if we have no space in our disk array left to add anything */
1309         spin_lock_irqsave(&io_request_lock, flags);
1310         if (h->num_luns >= CISS_MAX_LUN) {
1311                 spin_unlock_irqrestore(&io_request_lock, flags);
1312                 return -EINVAL;
1313         }
1314         if (h->busy_configuring) {
1315                 spin_unlock_irqrestore(&io_request_lock, flags);
1316                 return -EBUSY;
1317         }
1318         h->busy_configuring = 1;
1319         spin_unlock_irqrestore(&io_request_lock, flags);
1320
1321         ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1322         if (ld_buff == NULL) {
1323                 printk(KERN_ERR "cciss: out of memory\n");
1324                 h->busy_configuring = 0;
1325                 return -ENOMEM;
1326         }
1327         memset(ld_buff, 0, sizeof(ReportLunData_struct));
1328         size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1329         if (size_buff == NULL) {
1330                 printk(KERN_ERR "cciss: out of memory\n");
1331                 kfree(ld_buff);
1332                 h->busy_configuring = 0;
1333                 return -ENOMEM;
1334         }
1335         inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1336         if (inq_buff == NULL) {
1337                 printk(KERN_ERR "cciss: out of memory\n");
1338                 kfree(ld_buff);
1339                 kfree(size_buff);
1340                 h->busy_configuring = 0;
1341                 return -ENOMEM;
1342         }
1343
1344         return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
1345                         sizeof(ReportLunData_struct), 0, 0, 0 );
1346
1347         if (return_code == IO_OK) {
1348                 /* printk("LUN Data\n--------------------------\n"); */
1349                 listlength |= (0xff &
1350                         (unsigned int)(ld_buff->LUNListLength[0])) << 24;
1351                 listlength |= (0xff &
1352                         (unsigned int)(ld_buff->LUNListLength[1])) << 16;
1353                 listlength |= (0xff &
1354                         (unsigned int)(ld_buff->LUNListLength[2])) << 8;
1355                 listlength |= 0xff &
1356                         (unsigned int)(ld_buff->LUNListLength[3]);
1357         } else {
1358                 /* reading number of logical volumes failed */
1359                 printk(KERN_WARNING "cciss: report logical volume"
1360                         " command failed\n");
1361                 listlength = 0;
1362                 h->busy_configuring = 0;
1363                 return -1;
1364         }
1365         num_luns = listlength / 8; /* 8 bytes pre entry */
1366         if (num_luns > CISS_MAX_LUN)
1367                 num_luns = CISS_MAX_LUN;
1368
1369 #ifdef CCISS_DEBUG
1370         printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
1371                 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
1372                 ld_buff->LUNListLength[3],  num_luns);
1373 #endif
1374         for(i=0; i<  num_luns; i++) {
1375                 int j;
1376                 int lunID_found = 0;
1377
1378                 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
1379                 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
1380                 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
1381                 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1382
1383                 /* check to see if this is a new lun */
1384                 for(j=0; j <= h->highest_lun; j++) {
1385 #ifdef CCISS_DEBUG
1386                         printk("Checking %d %x against %x\n", j,h->drv[j].LunID,
1387                                                 lunid);
1388 #endif /* CCISS_DEBUG */
1389                         if (h->drv[j].LunID == lunid) {
1390                                 lunID_found = 1;
1391                                 break;
1392                         }
1393
1394                 }
1395                 if (lunID_found == 1)
1396                         continue;
1397                 else {  /* new lun found */
1398                         
1399 #ifdef CCISS_DEBUG
1400                         printk("new lun found at %d\n", i);
1401 #endif /* CCISS_DEBUG */
1402                         if (req_lunid)  /* we are looking for a specific lun */
1403                         {
1404                                 if (lunid != req_lunid)
1405                                 {
1406 #ifdef CCISS_DEBUG
1407                                         printk("new lun %x is not %x\n",
1408                                                         lunid, req_lunid);
1409 #endif /* CCISS_DEBUG */
1410                                         continue;
1411                                 }
1412                         }
1413                         new_lun_index = i;
1414                         new_lun_found = 1;
1415                         break;
1416                 }
1417         }
1418         if (!new_lun_found) {
1419                 printk(KERN_DEBUG "cciss:  New Logical Volume not found\n");
1420                 h->busy_configuring = 0;
1421                 return -1;
1422         }
1423         /* Now find the free index      */
1424         for(i=0; i <CISS_MAX_LUN; i++) {
1425 #ifdef CCISS_DEBUG
1426                 printk("Checking Index %d\n", i);
1427 #endif /* CCISS_DEBUG */
1428                 if (hba[ctlr]->drv[i].LunID == 0) {
1429 #ifdef CCISS_DEBUG
1430                         printk("free index found at %d\n", i);
1431 #endif /* CCISS_DEBUG */
1432                         free_index_found = 1;
1433                         free_index = i;
1434                         break;
1435                 }
1436         }
1437         if (!free_index_found) {
1438                 printk(KERN_WARNING "cciss: unable to find free slot for disk\n");
1439                 h->busy_configuring = 0;
1440                 return -1;
1441         }
1442
1443         logvol = free_index;
1444         hba[ctlr]->drv[logvol].LunID = lunid;
1445                 /* there could be gaps in lun numbers, track hightest */
1446         if (hba[ctlr]->highest_lun < logvol)
1447                 hba[ctlr]->highest_lun = logvol;
1448
1449         memset(size_buff, 0, sizeof(ReadCapdata_struct));
1450         return_code = sendcmd_withirq(CCISS_READ_CAPACITY, ctlr,
1451                         size_buff, sizeof(ReadCapdata_struct), 1,
1452                         logvol, 0 );
1453         if (return_code == IO_OK) {
1454                 total_size = (0xff &
1455                         (unsigned int) size_buff->total_size[0]) << 24;
1456                 total_size |= (0xff &
1457                         (unsigned int) size_buff->total_size[1]) << 16;
1458                 total_size |= (0xff &
1459                         (unsigned int) size_buff->total_size[2]) << 8;
1460                 total_size |= (0xff &
1461                         (unsigned int) size_buff->total_size[3]);
1462                 total_size++; /* command returns highest block address */
1463
1464                 block_size = (0xff &
1465                         (unsigned int) size_buff->block_size[0]) << 24;
1466                 block_size |= (0xff &
1467                         (unsigned int) size_buff->block_size[1]) << 16;
1468                 block_size |= (0xff &
1469                         (unsigned int) size_buff->block_size[2]) << 8;
1470                 block_size |= (0xff &
1471                         (unsigned int) size_buff->block_size[3]);
1472         } else {
1473                 /* read capacity command failed */
1474                 printk(KERN_WARNING "cciss: read capacity failed\n");
1475                 total_size = 0;
1476                 block_size = BLOCK_SIZE;
1477         }
1478         printk(KERN_INFO "      blocks= %d block_size= %d\n",
1479                                         total_size, block_size);
1480         /* Execute the command to read the disk geometry */
1481         memset(inq_buff, 0, sizeof(InquiryData_struct));
1482         return_code = sendcmd_withirq(CISS_INQUIRY, ctlr, inq_buff,
1483                 sizeof(InquiryData_struct), 1, logvol ,0xC1 );
1484         if (return_code == IO_OK) {
1485                 if (inq_buff->data_byte[8] == 0xFF) {
1486                         printk(KERN_WARNING
1487                         "cciss: reading geometry failed, "
1488                         "volume does not support reading geometry\n");
1489
1490                         hba[ctlr]->drv[logvol].block_size = block_size;
1491                         hba[ctlr]->drv[logvol].nr_blocks = total_size;
1492                         hba[ctlr]->drv[logvol].heads = 255;
1493                         hba[ctlr]->drv[logvol].sectors = 32; /* secs/trk */
1494                         hba[ctlr]->drv[logvol].cylinders = total_size / 255 /32;
1495                         hba[ctlr]->drv[logvol].raid_level = RAID_UNKNOWN;
1496                 } else {
1497                         hba[ctlr]->drv[logvol].block_size = block_size;
1498                         hba[ctlr]->drv[logvol].nr_blocks = total_size;
1499                         hba[ctlr]->drv[logvol].heads = inq_buff->data_byte[6];
1500                         hba[ctlr]->drv[logvol].sectors = inq_buff->data_byte[7];
1501                         hba[ctlr]->drv[logvol].cylinders =
1502                                 (inq_buff->data_byte[4] & 0xff) << 8;
1503                         hba[ctlr]->drv[logvol].cylinders +=
1504                                 inq_buff->data_byte[5];
1505                         hba[ctlr]->drv[logvol].raid_level = 
1506                                 inq_buff->data_byte[8];
1507                 }
1508         } else {
1509                 /* Get geometry failed */
1510                 printk(KERN_WARNING "cciss: reading geometry failed, "
1511                         "continuing with default geometry\n");
1512
1513                 hba[ctlr]->drv[logvol].block_size = block_size;
1514                 hba[ctlr]->drv[logvol].nr_blocks = total_size;
1515                 hba[ctlr]->drv[logvol].heads = 255;
1516                 hba[ctlr]->drv[logvol].sectors = 32; /* Sectors per track */
1517                 hba[ctlr]->drv[logvol].cylinders = total_size / 255 / 32;
1518         }
1519         if (hba[ctlr]->drv[logvol].raid_level > 5)
1520                 hba[ctlr]->drv[logvol].raid_level = RAID_UNKNOWN;
1521         printk(KERN_INFO "      heads= %d, sectors= %d, cylinders= %d RAID %s\n\n",
1522                 hba[ctlr]->drv[logvol].heads,
1523                 hba[ctlr]->drv[logvol].sectors,
1524                 hba[ctlr]->drv[logvol].cylinders, 
1525                 raid_label[hba[ctlr]->drv[logvol].raid_level]);
1526
1527         /* special case for c?d0, which may be opened even when
1528            it does not "exist".  In that case, don't mess with usage count.
1529            Also, /dev/c1d1 could be used to re-add c0d0 so we can't just 
1530            check whether logvol == 0, must check logvol != opened_vol */
1531         if (logvol != opened_vol)
1532                 hba[ctlr]->drv[logvol].usage_count = 0;
1533
1534         max_p = gdev->max_p;
1535         start = logvol<< gdev->minor_shift;
1536         hba[ctlr]->hd[start].nr_sects = total_size;
1537         hba[ctlr]->sizes[start] = total_size;
1538
1539         for(i=max_p-1; i>=0; i--) {
1540                 int minor = start+i;
1541                 invalidate_device(MKDEV(MAJOR_NR + ctlr, minor), 1);
1542                 gdev->part[minor].start_sect = 0;
1543                 gdev->part[minor].nr_sects = 0;
1544
1545                 /* reset the blocksize so we can read the partition table */
1546                 blksize_size[MAJOR_NR+ctlr][minor] = block_size;
1547                 hba[ctlr]->hardsizes[minor] = block_size;
1548         }
1549
1550         ++hba[ctlr]->num_luns;
1551         gdev->nr_real = hba[ctlr]->highest_lun + 1;
1552         /* setup partitions per disk */
1553         grok_partitions(gdev, logvol, MAX_PART,
1554                         hba[ctlr]->drv[logvol].nr_blocks);
1555         kfree(ld_buff);
1556         kfree(size_buff);
1557         kfree(inq_buff);
1558         h->busy_configuring = 0;
1559         return logvol;
1560 }
1561
1562 static int cciss_rescan_disk(int ctlr, int logvol)
1563 {
1564         struct gendisk *gdev = &(hba[ctlr]->gendisk);
1565         int start, max_p, i;
1566         ReadCapdata_struct *size_buff;
1567         InquiryData_struct *inq_buff;
1568         int return_code;
1569         unsigned int block_size;
1570         unsigned int total_size;
1571
1572         if (!capable(CAP_SYS_RAWIO))
1573                 return -EPERM;
1574         if (hba[ctlr]->sizes[logvol << NWD_SHIFT] != 0) {
1575                 /* disk is possible on line, return just a warning */
1576                 return 1;
1577         }
1578         size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1579         if (size_buff == NULL) {
1580                 printk(KERN_ERR "cciss: out of memory\n");
1581                 return -1;
1582         }
1583         inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1584         if (inq_buff == NULL) {
1585                 printk(KERN_ERR "cciss: out of memory\n");
1586                 kfree(size_buff);
1587                 return -1;
1588         }
1589         memset(size_buff, 0, sizeof(ReadCapdata_struct));
1590         return_code = sendcmd_withirq(CCISS_READ_CAPACITY, ctlr, size_buff,
1591                                 sizeof( ReadCapdata_struct), 1, logvol, 0 );
1592         if (return_code == IO_OK) {
1593                 total_size = (0xff &
1594                         (unsigned int)(size_buff->total_size[0])) << 24;
1595                 total_size |= (0xff &
1596                                 (unsigned int)(size_buff->total_size[1])) << 16;
1597                 total_size |= (0xff &
1598                                 (unsigned int)(size_buff->total_size[2])) << 8;
1599                 total_size |= (0xff & (unsigned int)
1600                                 (size_buff->total_size[3]));
1601                 total_size++; /* command returns highest block address */
1602
1603                 block_size = (0xff &
1604                                 (unsigned int)(size_buff->block_size[0])) << 24;
1605                 block_size |= (0xff &
1606                                 (unsigned int)(size_buff->block_size[1])) << 16;
1607                 block_size |= (0xff &
1608                                 (unsigned int)(size_buff->block_size[2])) << 8;
1609                 block_size |= (0xff &
1610                                 (unsigned int)(size_buff->block_size[3]));
1611         } else { /* read capacity command failed */
1612                 printk(KERN_WARNING "cciss: read capacity failed\n");
1613                 total_size = block_size = 0;
1614         }
1615         printk(KERN_INFO "      blocks= %d block_size= %d\n",
1616                                         total_size, block_size);
1617         /* Execute the command to read the disk geometry */
1618         memset(inq_buff, 0, sizeof(InquiryData_struct));
1619         return_code = sendcmd_withirq(CISS_INQUIRY, ctlr, inq_buff,
1620                         sizeof(InquiryData_struct), 1, logvol ,0xC1 );
1621         if (return_code == IO_OK) {
1622                 if (inq_buff->data_byte[8] == 0xFF) {
1623                         printk(KERN_WARNING "cciss: reading geometry failed, "
1624                                 "volume does not support reading geometry\n");
1625
1626                         hba[ctlr]->drv[logvol].nr_blocks = total_size;
1627                         hba[ctlr]->drv[logvol].heads = 255;
1628                         hba[ctlr]->drv[logvol].sectors = 32; /* Sectors/track */
1629                         hba[ctlr]->drv[logvol].cylinders = total_size / 255 /32;
1630                 } else {
1631                         hba[ctlr]->drv[logvol].nr_blocks = total_size;
1632                         hba[ctlr]->drv[logvol].heads = inq_buff->data_byte[6];
1633                         hba[ctlr]->drv[logvol].sectors = inq_buff->data_byte[7];
1634                         hba[ctlr]->drv[logvol].cylinders =
1635                                 (inq_buff->data_byte[4] & 0xff) << 8;
1636                         hba[ctlr]->drv[logvol].cylinders +=
1637                                 inq_buff->data_byte[5];
1638                 }
1639         } else { /* Get geometry failed */
1640                 printk(KERN_WARNING "cciss: reading geometry failed, "
1641                                 "continuing with default geometry\n");
1642
1643                 hba[ctlr]->drv[logvol].nr_blocks = total_size;
1644                 hba[ctlr]->drv[logvol].heads = 255;
1645                 hba[ctlr]->drv[logvol].sectors = 32; /* Sectors / track */
1646                 hba[ctlr]->drv[logvol].cylinders = total_size / 255 /32;
1647         }
1648
1649         printk(KERN_INFO "      heads= %d, sectors= %d, cylinders= %d \n\n", 
1650                 hba[ctlr]->drv[logvol].heads,
1651                 hba[ctlr]->drv[logvol].sectors,
1652                 hba[ctlr]->drv[logvol].cylinders);
1653         max_p = gdev->max_p;
1654         start = logvol<< gdev->minor_shift;
1655         hba[ctlr]->hd[start].nr_sects = hba[ctlr]->sizes[start]= total_size;
1656
1657         for (i=max_p-1; i>=0; i--) {
1658                 int minor = start+i;
1659                 invalidate_device(MKDEV(MAJOR_NR + ctlr, minor), 1);
1660                 gdev->part[minor].start_sect = 0;
1661                 gdev->part[minor].nr_sects = 0;
1662
1663                 /* reset the blocksize so we can read the partition table */
1664                 blksize_size[MAJOR_NR+ctlr][minor] = 1024;
1665                 hba[ctlr]->hardsizes[minor] = block_size;
1666         }
1667
1668         /* setup partitions per disk */
1669         grok_partitions(gdev, logvol, MAX_PART,
1670                         hba[ctlr]->drv[logvol].nr_blocks );
1671
1672         kfree(size_buff);
1673         kfree(inq_buff);
1674         return 0;
1675 }
1676 /*
1677  *   Wait polling for a command to complete.
1678  *   The memory mapped FIFO is polled for the completion.
1679  *   Used only at init time, interrupts disabled.
1680  */
1681 static unsigned long pollcomplete(int ctlr)
1682 {
1683         unsigned long done;
1684         int i;
1685
1686         /* Wait (up to 20 seconds) for a command to complete */
1687
1688         for (i = 20 * HZ; i > 0; i--) {
1689                 done = hba[ctlr]->access.command_completed(hba[ctlr]);
1690                 if (done == FIFO_EMPTY) {
1691                         set_current_state(TASK_UNINTERRUPTIBLE);
1692                         schedule_timeout(1);
1693                 } else
1694                         return done;
1695         }
1696         /* Invalid address to tell caller we ran out of time */
1697         return 1;
1698 }
1699 /*
1700  * Send a command to the controller, and wait for it to complete.  
1701  * Only used at init time. 
1702  */
1703 static int sendcmd(
1704         __u8    cmd,
1705         int     ctlr,
1706         void    *buff,
1707         size_t  size,
1708         unsigned int use_unit_num, /* 0: address the controller,
1709                                       1: address logical volume log_unit,
1710                                       2: periph device address is scsi3addr */
1711         unsigned int log_unit,
1712         __u8    page_code,
1713         unsigned char *scsi3addr)
1714 {
1715         CommandList_struct *c;
1716         int i;
1717         unsigned long complete;
1718         ctlr_info_t *info_p= hba[ctlr];
1719         u64bit buff_dma_handle;
1720         int status = IO_OK;
1721
1722         c = cmd_alloc(info_p, 1);
1723         if (c == NULL) {
1724                 printk(KERN_WARNING "cciss: unable to get memory");
1725                 return IO_ERROR;
1726         }
1727         /* Fill in Command Header */
1728         c->Header.ReplyQueue = 0;  /* unused in simple mode */
1729         if (buff != NULL) {     /* buffer to fill */
1730                 c->Header.SGList = 1;
1731                 c->Header.SGTotal= 1;
1732         } else  {       /* no buffers to fill  */
1733                 c->Header.SGList = 0;
1734                 c->Header.SGTotal= 0;
1735         }
1736         c->Header.Tag.lower = c->busaddr;  /* use the kernel address */
1737                                            /* the cmd block for tag */
1738         /* Fill in Request block */
1739         switch (cmd) {
1740                 case  CISS_INQUIRY:
1741                         /* If the logical unit number is 0 then, this is going
1742                                 to controller so It's a physical command
1743                                 mode = 0 target = 0.
1744                                 So we have nothing to write. 
1745                                 otherwise, if use_unit_num == 1,
1746                                 mode = 1(volume set addressing) target = LUNID
1747                                 otherwise, if use_unit_num == 2,
1748                                 mode = 0(periph dev addr) target = scsi3addr
1749                         */
1750                         if (use_unit_num == 1) {
1751                                 c->Header.LUN.LogDev.VolId=
1752                                         hba[ctlr]->drv[log_unit].LunID;
1753                                 c->Header.LUN.LogDev.Mode = 1;
1754                         }
1755                         else if (use_unit_num == 2) {
1756                                 memcpy(c->Header.LUN.LunAddrBytes,scsi3addr,8);
1757                                 c->Header.LUN.LogDev.Mode = 0; 
1758                                                         /* phys dev addr */
1759                         }
1760
1761                         /* are we trying to read a vital product page */
1762                         if (page_code != 0) {
1763                                 c->Request.CDB[1] = 0x01;
1764                                 c->Request.CDB[2] = page_code;
1765                         }
1766                         c->Request.CDBLen = 6;
1767                         c->Request.Type.Type =  TYPE_CMD; /* It is a command. */
1768                         c->Request.Type.Attribute = ATTR_SIMPLE;  
1769                         c->Request.Type.Direction = XFER_READ; /* Read */
1770                         c->Request.Timeout = 0; /* Don't time out */
1771                         c->Request.CDB[0] =  CISS_INQUIRY;
1772                         c->Request.CDB[4] = size  & 0xFF;  
1773                 break;
1774                 case CISS_REPORT_LOG:
1775                 case CISS_REPORT_PHYS:
1776                         /* Talking to controller so It's a physical command
1777                                 mode = 00 target = 0.
1778                                 So we have nothing to write.
1779                         */
1780                         c->Request.CDBLen = 12;
1781                         c->Request.Type.Type =  TYPE_CMD; /* It is a command. */
1782                         c->Request.Type.Attribute = ATTR_SIMPLE; 
1783                         c->Request.Type.Direction = XFER_READ; /* Read */
1784                         c->Request.Timeout = 0; /* Don't time out */
1785                         c->Request.CDB[0] = cmd;
1786                         c->Request.CDB[6] = (size >> 24) & 0xFF;  /* MSB */
1787                         c->Request.CDB[7] = (size >> 16) & 0xFF;
1788                         c->Request.CDB[8] = (size >> 8) & 0xFF;
1789                         c->Request.CDB[9] = size & 0xFF;
1790                 break;
1791
1792                 case CCISS_READ_CAPACITY:
1793                         c->Header.LUN.LogDev.VolId= 
1794                                 hba[ctlr]->drv[log_unit].LunID;
1795                         c->Header.LUN.LogDev.Mode = 1;
1796                         c->Request.CDBLen = 10;
1797                         c->Request.Type.Type =  TYPE_CMD; /* It is a command. */
1798                         c->Request.Type.Attribute = ATTR_SIMPLE; 
1799                         c->Request.Type.Direction = XFER_READ; /* Read */
1800                         c->Request.Timeout = 0; /* Don't time out */
1801                         c->Request.CDB[0] = CCISS_READ_CAPACITY;
1802                 break;
1803                 case CCISS_CACHE_FLUSH:
1804                         c->Request.CDBLen = 12;
1805                         c->Request.Type.Type =  TYPE_CMD; /* It is a command. */
1806                         c->Request.Type.Attribute = ATTR_SIMPLE;
1807                         c->Request.Type.Direction = XFER_WRITE; /* No data */
1808                         c->Request.Timeout = 0; /* Don't time out */
1809                         c->Request.CDB[0] = BMIC_WRITE;  /* BMIC Passthru */
1810                         c->Request.CDB[6] = BMIC_CACHE_FLUSH;
1811                 break;
1812                 default:
1813                         printk(KERN_WARNING
1814                                 "cciss:  Unknown Command 0x%x sent attempted\n",
1815                                   cmd);
1816                         cmd_free(info_p, c, 1);
1817                         return IO_ERROR;
1818         };
1819         /* Fill in the scatter gather information */
1820         if (size > 0) {
1821                 buff_dma_handle.val = (__u64) pci_map_single( info_p->pdev, 
1822                         buff, size, PCI_DMA_BIDIRECTIONAL);
1823                 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1824                 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1825                 c->SG[0].Len = size;
1826                 c->SG[0].Ext = 0;  /* we are not chaining */
1827         }
1828 resend_cmd1:
1829         /*
1830          * Disable interrupt
1831          */
1832 #ifdef CCISS_DEBUG
1833         printk(KERN_DEBUG "cciss: turning intr off\n");
1834 #endif /* CCISS_DEBUG */ 
1835         info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
1836         
1837         /* Make sure there is room in the command FIFO */
1838         /* Actually it should be completely empty at this time. */
1839         for (i = 200000; i > 0; i--) {
1840                 /* if fifo isn't full go */
1841                 if (!(info_p->access.fifo_full(info_p))) {
1842                         
1843                         break;
1844                 }
1845                 udelay(10);
1846                 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
1847                         " waiting!\n", ctlr);
1848         }
1849         /*
1850          * Send the cmd
1851          */
1852         info_p->access.submit_command(info_p, c);
1853         complete = pollcomplete(ctlr);
1854
1855 #ifdef CCISS_DEBUG
1856         printk(KERN_DEBUG "cciss: command completed\n");
1857 #endif /* CCISS_DEBUG */
1858
1859         if (complete != 1) {
1860                 if ( (complete & CISS_ERROR_BIT)
1861                      && (complete & ~CISS_ERROR_BIT) == c->busaddr) {
1862                         /* if data overrun or underun on Report command 
1863                                 ignore it 
1864                         */
1865                         if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
1866                              (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
1867                              (c->Request.CDB[0] == CISS_INQUIRY)) &&
1868                                 ((c->err_info->CommandStatus == 
1869                                         CMD_DATA_OVERRUN) || 
1870                                  (c->err_info->CommandStatus == 
1871                                         CMD_DATA_UNDERRUN)
1872                                 )) {
1873                                 complete = c->busaddr;
1874                         } else {
1875                                 if (c->err_info->CommandStatus == 
1876                                                 CMD_UNSOLICITED_ABORT) {
1877                                         printk(KERN_WARNING "cciss: "
1878                                                 "cmd %p aborted do "
1879                                         "to an unsolicited abort \n", c); 
1880                                         if (c->retry_count < MAX_CMD_RETRIES) {
1881                                                 printk(KERN_WARNING
1882                                                    "retrying cmd\n");
1883                                                 c->retry_count++;
1884                                                 /* erase the old error */
1885                                                 /* information */
1886                                                 memset(c->err_info, 0, 
1887                                                    sizeof(ErrorInfo_struct));
1888                                                 goto resend_cmd1;
1889                                         } else {
1890                                                 printk(KERN_WARNING
1891                                                    "retried to many times\n");
1892                                                 status = IO_ERROR;
1893                                                 goto cleanup1;
1894                                         }
1895                                 }
1896                                 printk(KERN_WARNING "cciss cciss%d: sendcmd"
1897                                 " Error %x \n", ctlr, 
1898                                         c->err_info->CommandStatus); 
1899                                 printk(KERN_WARNING "cciss cciss%d: sendcmd"
1900                                 " offensive info\n"
1901                                 "  size %x\n   num %x   value %x\n", ctlr,
1902                                   c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
1903                                   c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
1904                                   c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
1905                                 status = IO_ERROR;
1906                                 goto cleanup1;
1907                         }
1908                 }
1909                 if (complete != c->busaddr) {
1910                         printk( KERN_WARNING "cciss cciss%d: SendCmd "
1911                       "Invalid command list address returned! (%lx)\n",
1912                                 ctlr, complete);
1913                         status = IO_ERROR;
1914                         goto cleanup1;
1915                 }
1916         } else {
1917                 printk( KERN_WARNING
1918                         "cciss cciss%d: SendCmd Timeout out, "
1919                         "No command list address returned!\n",
1920                         ctlr);
1921                 status = IO_ERROR;
1922         }
1923                 
1924 cleanup1:       
1925         /* unlock the data buffer from DMA */
1926         pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
1927                                 size, PCI_DMA_BIDIRECTIONAL);
1928         cmd_free(info_p, c, 1);
1929         return status;
1930
1931 /*
1932  * Map (physical) PCI mem into (virtual) kernel space
1933  */
1934 static ulong remap_pci_mem(ulong base, ulong size)
1935 {
1936         ulong page_base        = ((ulong) base) & PAGE_MASK;
1937         ulong page_offs        = ((ulong) base) - page_base;
1938         ulong page_remapped    = (ulong) ioremap(page_base, page_offs+size);
1939
1940         return (ulong) (page_remapped ? (page_remapped + page_offs) : 0UL);
1941 }
1942
1943 /*
1944  * Enqueuing and dequeuing functions for cmdlists.
1945  */
1946 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
1947 {
1948         if (*Qptr == NULL) {
1949                 *Qptr = c;
1950                 c->next = c->prev = c;
1951         } else {
1952                 c->prev = (*Qptr)->prev;
1953                 c->next = (*Qptr);
1954                 (*Qptr)->prev->next = c;
1955                 (*Qptr)->prev = c;
1956         }
1957 }
1958
1959 static inline CommandList_struct *removeQ(CommandList_struct **Qptr, 
1960                                                 CommandList_struct *c)
1961 {
1962         if (c && c->next != c) {
1963                 if (*Qptr == c) *Qptr = c->next;
1964                 c->prev->next = c->next;
1965                 c->next->prev = c->prev;
1966         } else {
1967                 *Qptr = NULL;
1968         }
1969         return c;
1970 }
1971
1972 /* 
1973  * Takes jobs of the Q and sends them to the hardware, then puts it on 
1974  * the Q to wait for completion. 
1975  */ 
1976 static void start_io( ctlr_info_t *h)
1977 {
1978         CommandList_struct *c;
1979         
1980         while(( c = h->reqQ) != NULL ) {
1981                 /* can't do anything if fifo is full */
1982                 if ((h->access.fifo_full(h))) {
1983                         printk(KERN_WARNING "cciss: fifo full \n");
1984                         return;
1985                 }
1986                 /* Get the frist entry from the Request Q */ 
1987                 removeQ(&(h->reqQ), c);
1988                 h->Qdepth--;
1989         
1990                 /* Tell the controller execute command */ 
1991                 h->access.submit_command(h, c);
1992                 
1993                 /* Put job onto the completed Q */ 
1994                 addQ (&(h->cmpQ), c); 
1995         }
1996 }
1997
1998 static inline void complete_buffers( struct buffer_head *bh, int status)
1999 {
2000         struct buffer_head *xbh;
2001         
2002         while(bh) {
2003                 xbh = bh->b_reqnext; 
2004                 bh->b_reqnext = NULL; 
2005                 blk_finished_io(bh->b_size >> 9);
2006                 bh->b_end_io(bh, status);
2007                 bh = xbh;
2008         }
2009
2010 /* This code assumes io_request_lock is already held */
2011 /* Zeros out the error record and then resends the command back */
2012 /* to the controller */ 
2013 static inline void resend_cciss_cmd( ctlr_info_t *h, CommandList_struct *c)
2014 {
2015         /* erase the old error information */
2016         memset(c->err_info, 0, sizeof(ErrorInfo_struct));
2017
2018         /* add it to software queue and then send it to the controller */
2019         addQ(&(h->reqQ),c);
2020         h->Qdepth++;
2021         if (h->Qdepth > h->maxQsinceinit)
2022                 h->maxQsinceinit = h->Qdepth; 
2023
2024         start_io(h);
2025 }
2026 /* checks the status of the job and calls complete buffers to mark all 
2027  * buffers for the completed job. 
2028  */ 
2029 static inline void complete_command( ctlr_info_t *h, CommandList_struct *cmd, 
2030                 int timeout)
2031 {
2032         int status = 1;
2033         int retry_cmd = 0;
2034         int i, ddir;
2035         u64bit temp64;
2036                 
2037         if (timeout)
2038                 status = 0; 
2039
2040         if (cmd->err_info->CommandStatus != 0) { 
2041                 /* an error has occurred */ 
2042                 switch (cmd->err_info->CommandStatus) {
2043                         unsigned char sense_key;
2044                         case CMD_TARGET_STATUS:
2045                                 status = 0;
2046                         
2047                                 if (cmd->err_info->ScsiStatus == 0x02) {
2048                                         printk(KERN_WARNING "cciss: cmd %p "
2049                                                 "has CHECK CONDITION,"
2050                                                 " sense key = 0x%x\n", cmd,
2051                                                 cmd->err_info->SenseInfo[2]);
2052                                         /* check the sense key */
2053                                         sense_key = 0xf & 
2054                                                 cmd->err_info->SenseInfo[2];
2055                                         /* recovered error */
2056                                         if ( sense_key == 0x1)
2057                                                 status = 1;
2058                                 } else {
2059                                         printk(KERN_WARNING "cciss: cmd %p "
2060                                                 "has SCSI Status 0x%x\n",
2061                                                 cmd, cmd->err_info->ScsiStatus);
2062                                 }
2063                         break;
2064                         case CMD_DATA_UNDERRUN:
2065                                 printk(KERN_WARNING "cciss: cmd %p has"
2066                                         " completed with data underrun "
2067                                         "reported\n", cmd);
2068                         break;
2069                         case CMD_DATA_OVERRUN:
2070                                 printk(KERN_WARNING "cciss: cmd %p has"
2071                                         " completed with data overrun "
2072                                         "reported\n", cmd);
2073                         break;
2074                         case CMD_INVALID:
2075                                 printk(KERN_WARNING "cciss: cmd %p is "
2076                                         "reported invalid\n", cmd);
2077                                 status = 0;
2078                         break;
2079                         case CMD_PROTOCOL_ERR:
2080                                 printk(KERN_WARNING "cciss: cmd %p has "
2081                                         "protocol error \n", cmd);
2082                                 status = 0;
2083                         break;
2084                         case CMD_HARDWARE_ERR:
2085                                 printk(KERN_WARNING "cciss: cmd %p had " 
2086                                         " hardware error\n", cmd);
2087                                 status = 0;
2088                         break;
2089                         case CMD_CONNECTION_LOST:
2090                                 printk(KERN_WARNING "cciss: cmd %p had "
2091                                         "connection lost\n", cmd);
2092                                 status=0;
2093                         break;
2094                         case CMD_ABORTED:
2095                                 printk(KERN_WARNING "cciss: cmd %p was "
2096                                         "aborted\n", cmd);
2097                                 status=0;
2098                         break;
2099                         case CMD_ABORT_FAILED:
2100                                 printk(KERN_WARNING "cciss: cmd %p reports "
2101                                         "abort failed\n", cmd);
2102                                 status=0;
2103                         break;
2104                         case CMD_UNSOLICITED_ABORT:
2105                                 printk(KERN_WARNING "cciss: cmd %p aborted do "
2106                                         "to an unsolicited abort \n",
2107                                         cmd);
2108                                 if (cmd->retry_count < MAX_CMD_RETRIES) {
2109                                         retry_cmd=1;
2110                                         printk(KERN_WARNING
2111                                                 "retrying cmd\n");
2112                                         cmd->retry_count++;
2113                                 } else {
2114                                         printk(KERN_WARNING
2115                                         "retried to many times\n");
2116                                 }
2117                                 status=0;
2118                         break;
2119                         case CMD_TIMEOUT:
2120                                 printk(KERN_WARNING "cciss: cmd %p timedout\n",
2121                                         cmd);
2122                                 status=0;
2123                         break;
2124                         default:
2125                                 printk(KERN_WARNING "cciss: cmd %p returned "
2126                                         "unknown status %x\n", cmd, 
2127                                                 cmd->err_info->CommandStatus); 
2128                                 status=0;
2129                 }
2130         }
2131         /* We need to return this command */
2132         if (retry_cmd) {
2133                 resend_cciss_cmd(h,cmd);
2134                 return;
2135         }       
2136         /* command did not need to be retried */
2137         /* unmap the DMA mapping for all the scatter gather elements */
2138         if (cmd->Request.Type.Direction == XFER_READ)
2139                 ddir = PCI_DMA_FROMDEVICE;
2140         else
2141                 ddir = PCI_DMA_TODEVICE;
2142         for(i=0; i<cmd->Header.SGList; i++) {
2143                 temp64.val32.lower = cmd->SG[i].Addr.lower;
2144                 temp64.val32.upper = cmd->SG[i].Addr.upper;
2145                 pci_unmap_page(hba[cmd->ctlr]->pdev,
2146                         temp64.val, cmd->SG[i].Len, ddir);
2147         }
2148         complete_buffers(cmd->rq->bh, status);
2149 #ifdef CCISS_DEBUG
2150         printk("Done with %p\n", cmd->rq);
2151 #endif /* CCISS_DEBUG */ 
2152         end_that_request_last(cmd->rq);
2153         cmd_free(h,cmd,1);
2154 }
2155
2156
2157 static inline int cpq_new_segment(request_queue_t *q, struct request *rq,
2158                                   int max_segments)
2159 {
2160         if (rq->nr_segments < MAXSGENTRIES) {
2161                 rq->nr_segments++;
2162                 return 1;
2163         }
2164         return 0;
2165 }
2166
2167 static int cpq_back_merge_fn(request_queue_t *q, struct request *rq,
2168                              struct buffer_head *bh, int max_segments)
2169 {
2170         if (blk_seg_merge_ok(rq->bhtail, bh))   
2171                 return 1;
2172         return cpq_new_segment(q, rq, max_segments);
2173 }
2174
2175 static int cpq_front_merge_fn(request_queue_t *q, struct request *rq,
2176                              struct buffer_head *bh, int max_segments)
2177 {
2178         if (blk_seg_merge_ok(bh, rq->bh))
2179                 return 1;
2180         return cpq_new_segment(q, rq, max_segments);
2181 }
2182
2183 static int cpq_merge_requests_fn(request_queue_t *q, struct request *rq,
2184                                  struct request *nxt, int max_segments)
2185 {
2186         int total_segments = rq->nr_segments + nxt->nr_segments;
2187
2188         if (blk_seg_merge_ok(rq->bhtail, nxt->bh))
2189                 total_segments--;
2190
2191         if (total_segments > MAXSGENTRIES)
2192                 return 0;
2193
2194         rq->nr_segments = total_segments;
2195         return 1;
2196 }
2197
2198 /* 
2199  * Get a request and submit it to the controller. 
2200  * Currently we do one request at a time.  Ideally we would like to send
2201  * everything to the controller on the first call, but there is a danger
2202  * of holding the io_request_lock for to long.  
2203  */
2204 static void do_cciss_request(request_queue_t *q)
2205 {
2206         ctlr_info_t *h= q->queuedata; 
2207         CommandList_struct *c;
2208         int log_unit, start_blk, seg;
2209         unsigned long long lastdataend;
2210         struct buffer_head *bh;
2211         struct list_head *queue_head = &q->queue_head;
2212         struct request *creq;
2213         u64bit temp64;
2214         struct scatterlist tmp_sg[MAXSGENTRIES];
2215         int i, ddir;
2216
2217         if (q->plugged)
2218                 goto startio;
2219
2220 next:
2221         if (list_empty(queue_head))
2222                 goto startio;
2223
2224         creq =  blkdev_entry_next_request(queue_head); 
2225         if (creq->nr_segments > MAXSGENTRIES)
2226                 BUG();
2227
2228         if (h->ctlr != MAJOR(creq->rq_dev)-MAJOR_NR ) {
2229                 printk(KERN_WARNING "doreq cmd for %d, %x at %p\n",
2230                                 h->ctlr, creq->rq_dev, creq);
2231                 blkdev_dequeue_request(creq);
2232                 complete_buffers(creq->bh, 0);
2233                 end_that_request_last(creq);
2234                 goto startio;
2235         }
2236
2237         if (( c = cmd_alloc(h, 1)) == NULL)
2238                 goto startio;
2239
2240         blkdev_dequeue_request(creq);
2241
2242         spin_unlock_irq(&io_request_lock);
2243
2244         c->cmd_type = CMD_RWREQ;      
2245         c->rq = creq;
2246         bh = creq->bh;
2247         
2248         /* fill in the request */ 
2249         log_unit = MINOR(creq->rq_dev) >> NWD_SHIFT; 
2250         c->Header.ReplyQueue = 0;  /* unused in simple mode */
2251         c->Header.Tag.lower = c->busaddr;  /* use the physical address */
2252                                         /* the cmd block for tag */
2253         c->Header.LUN.LogDev.VolId= hba[h->ctlr]->drv[log_unit].LunID;
2254         c->Header.LUN.LogDev.Mode = 1;
2255         c->Request.CDBLen = 10; /* 12 byte commands not in FW yet. */
2256         c->Request.Type.Type =  TYPE_CMD; /* It is a command.  */
2257         c->Request.Type.Attribute = ATTR_SIMPLE; 
2258         c->Request.Type.Direction = 
2259                 (creq->cmd == READ) ? XFER_READ: XFER_WRITE; 
2260         c->Request.Timeout = 0; /* Don't time out */
2261         c->Request.CDB[0] = (creq->cmd == READ) ? CCISS_READ : CCISS_WRITE;
2262         start_blk = hba[h->ctlr]->hd[MINOR(creq->rq_dev)].start_sect + creq->sector;
2263 #ifdef CCISS_DEBUG
2264         if (bh == NULL)
2265                 panic("cciss: bh== NULL?");
2266         printk(KERN_DEBUG "cciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
2267                 (int) creq->nr_sectors);        
2268 #endif /* CCISS_DEBUG */
2269         seg = 0;
2270         lastdataend = ~0ULL;
2271         while(bh) {
2272                 if (bh_phys(bh) == lastdataend)
2273                 {  /* tack it on to the last segment */
2274                         tmp_sg[seg-1].length +=bh->b_size;
2275                         lastdataend += bh->b_size;
2276                 } else {
2277                         if (seg == MAXSGENTRIES)
2278                                 BUG();
2279                         tmp_sg[seg].page = bh->b_page;
2280                         tmp_sg[seg].length = bh->b_size;
2281                         tmp_sg[seg].offset = bh_offset(bh);
2282                         lastdataend = bh_phys(bh) + bh->b_size;
2283                         seg++;
2284                 }
2285                 bh = bh->b_reqnext;
2286         }
2287
2288         /* get the DMA records for the setup */ 
2289         if (c->Request.Type.Direction == XFER_READ)
2290                 ddir = PCI_DMA_FROMDEVICE;
2291         else
2292                 ddir = PCI_DMA_TODEVICE;
2293         for (i=0; i<seg; i++) {
2294                 c->SG[i].Len = tmp_sg[i].length;
2295                 temp64.val = pci_map_page(h->pdev, tmp_sg[i].page,
2296                             tmp_sg[i].offset, tmp_sg[i].length, ddir);
2297                 c->SG[i].Addr.lower = temp64.val32.lower;
2298                 c->SG[i].Addr.upper = temp64.val32.upper;
2299                 c->SG[i].Ext = 0;  /* we are not chaining */
2300         }
2301         /* track how many SG entries we are using */ 
2302         if (seg > h->maxSG)
2303                 h->maxSG = seg; 
2304
2305 #ifdef CCISS_DEBUG
2306         printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", sect, seg);
2307 #endif /* CCISS_DEBUG */
2308
2309         c->Header.SGList = c->Header.SGTotal = seg;
2310         c->Request.CDB[1]= 0;
2311         c->Request.CDB[2]= (start_blk >> 24) & 0xff;    /* MSB */
2312         c->Request.CDB[3]= (start_blk >> 16) & 0xff;
2313         c->Request.CDB[4]= (start_blk >>  8) & 0xff;
2314         c->Request.CDB[5]= start_blk & 0xff;
2315         c->Request.CDB[6]= 0; /* (sect >> 24) & 0xff; MSB */
2316         c->Request.CDB[7]= (creq->nr_sectors >>  8) & 0xff; 
2317         c->Request.CDB[8]= creq->nr_sectors & 0xff; 
2318         c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2319
2320         spin_lock_irq(&io_request_lock);
2321
2322         addQ(&(h->reqQ),c);
2323         h->Qdepth++;
2324         if (h->Qdepth > h->maxQsinceinit)
2325                 h->maxQsinceinit = h->Qdepth; 
2326
2327         goto next;
2328
2329 startio:
2330         start_io(h);
2331 }
2332
2333 static void do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
2334 {
2335         ctlr_info_t *h = dev_id;
2336         CommandList_struct *c;
2337         unsigned long flags;
2338         __u32 a, a1;
2339
2340
2341         /* Is this interrupt for us? */
2342         if (h->access.intr_pending(h) == 0)
2343                 return;
2344
2345         /*
2346          * If there are completed commands in the completion queue,
2347          * we had better do something about it.
2348          */
2349         spin_lock_irqsave(&io_request_lock, flags);
2350         while( h->access.intr_pending(h)) {
2351                 while((a = h->access.command_completed(h)) != FIFO_EMPTY) {
2352                         a1 = a;
2353                         a &= ~3;
2354                         if ((c = h->cmpQ) == NULL) {  
2355                                 printk(KERN_WARNING "cciss: Completion of %08lx ignored\n", (unsigned long)a1);
2356                                 continue;       
2357                         } 
2358                         while(c->busaddr != a) {
2359                                 c = c->next;
2360                                 if (c == h->cmpQ) 
2361                                         break;
2362                         }
2363                         /*
2364                          * If we've found the command, take it off the
2365                          * completion Q and free it
2366                          */
2367                          if (c->busaddr == a) {
2368                                 removeQ(&h->cmpQ, c);
2369                                 if (c->cmd_type == CMD_RWREQ) {
2370                                         complete_command(h, c, 0);
2371                                 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2372                                         complete(c->waiting);
2373                                 }
2374 #                               ifdef CONFIG_CISS_SCSI_TAPE
2375                                 else if (c->cmd_type == CMD_SCSI) {
2376                                         complete_scsi_command(c, 0, a1);
2377                                 }
2378 #                               endif
2379                                 continue;
2380                         }
2381                 }
2382         }
2383         /*
2384          * See if we can queue up some more IO
2385          */
2386         do_cciss_request(BLK_DEFAULT_QUEUE(MAJOR_NR + h->ctlr));
2387         spin_unlock_irqrestore(&io_request_lock, flags);
2388 }
2389 /* 
2390  *  We cannot read the structure directly, for portablity we must use 
2391  *   the io functions.
2392  *   This is for debug only. 
2393  */
2394 #ifdef CCISS_DEBUG
2395 static void print_cfg_table( CfgTable_struct *tb)
2396 {
2397         int i;
2398         char temp_name[17];
2399
2400         printk("Controller Configuration information\n");
2401         printk("------------------------------------\n");
2402         for(i=0;i<4;i++)
2403                 temp_name[i] = readb(&(tb->Signature[i]));
2404         temp_name[4]='\0';
2405         printk("   Signature = %s\n", temp_name); 
2406         printk("   Spec Number = %d\n", readl(&(tb->SpecValence)));
2407         printk("   Transport methods supported = 0x%x\n", 
2408                                 readl(&(tb-> TransportSupport)));
2409         printk("   Transport methods active = 0x%x\n", 
2410                                 readl(&(tb->TransportActive)));
2411         printk("   Requested transport Method = 0x%x\n", 
2412                         readl(&(tb->HostWrite.TransportRequest)));
2413         printk("   Coalese Interrupt Delay = 0x%x\n", 
2414                         readl(&(tb->HostWrite.CoalIntDelay)));
2415         printk("   Coalese Interrupt Count = 0x%x\n", 
2416                         readl(&(tb->HostWrite.CoalIntCount)));
2417         printk("   Max outstanding commands = 0x%d\n", 
2418                         readl(&(tb->CmdsOutMax)));
2419         printk("   Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
2420         for(i=0;i<16;i++)
2421                 temp_name[i] = readb(&(tb->ServerName[i]));
2422         temp_name[16] = '\0';
2423         printk("   Server Name = %s\n", temp_name);
2424         printk("   Heartbeat Counter = 0x%x\n\n\n", 
2425                         readl(&(tb->HeartBeat)));
2426 }
2427 #endif /* CCISS_DEBUG */ 
2428
2429 static void release_io_mem(ctlr_info_t *c)
2430 {
2431         /* if IO mem was not protected do nothing */
2432         if (c->io_mem_addr == 0)
2433                 return;
2434         release_region(c->io_mem_addr, c->io_mem_length);
2435         c->io_mem_addr = 0;
2436         c->io_mem_length = 0;
2437 }
2438 static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2439 {
2440         ushort vendor_id, device_id, command;
2441         unchar cache_line_size, latency_timer;
2442         unchar irq, revision;
2443         uint addr[6];
2444         __u32 board_id;
2445         int cfg_offset;
2446         int cfg_base_addr;
2447         int cfg_base_addr_index;
2448         int i;
2449
2450         vendor_id = pdev->vendor;
2451         device_id = pdev->device;
2452         irq = pdev->irq;
2453
2454         for(i=0; i<6; i++)
2455                 addr[i] = pdev->resource[i].start;
2456
2457         if (pci_enable_device(pdev)) {
2458                 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
2459                 return -1;
2460         }
2461         if (pci_set_dma_mask(pdev, CCISS_DMA_MASK ) != 0) {
2462                 printk(KERN_ERR "cciss:  Unable to set DMA mask\n");
2463                 return -1;
2464         }
2465         
2466         (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
2467         (void) pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
2468         (void) pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE,
2469                                                 &cache_line_size);
2470         (void) pci_read_config_byte(pdev, PCI_LATENCY_TIMER,
2471                                                 &latency_timer);
2472
2473         (void) pci_read_config_dword(pdev, PCI_SUBSYSTEM_VENDOR_ID, 
2474                                                 &board_id);
2475
2476         /* check to see if controller has been disabled */
2477         if (!(command & 0x02)) {
2478                 printk(KERN_WARNING "cciss: controller appears to be disabled\n");
2479                 return -1;
2480         }
2481         /* search for our IO range so we can protect it */
2482         for (i=0; i<6; i++) {
2483                 /* is this an IO range */
2484                 if (pdev->resource[i].flags & 0x01) {
2485                         c->io_mem_addr = pdev->resource[i].start;
2486                         c->io_mem_length = pdev->resource[i].end -
2487                                 pdev->resource[i].start +1; 
2488 #ifdef CCISS_DEBUG
2489                         printk("IO value found base_addr[%d] %lx %lx\n", i,
2490                                 c->io_mem_addr, c->io_mem_length);
2491 #endif /* CCISS_DEBUG */
2492                         /* register the IO range */
2493                         if (!request_region( c->io_mem_addr,
2494                                         c->io_mem_length, "cciss")) {
2495                                 printk(KERN_WARNING 
2496                                         "cciss I/O memory range already in "
2497                                         "use addr=%lx length=%ld\n",
2498                                 c->io_mem_addr, c->io_mem_length);
2499                                 c->io_mem_addr= 0;
2500                                 c->io_mem_length = 0;
2501                         }
2502                         break;
2503                 }
2504         }
2505
2506 #ifdef CCISS_DEBUG
2507         printk("vendor_id = %x\n", vendor_id);
2508         printk("device_id = %x\n", device_id);
2509         printk("command = %x\n", command);
2510         for(i=0; i<6; i++)
2511                 printk("addr[%d] = %x\n", i, addr[i]);
2512         printk("revision = %x\n", revision);
2513         printk("irq = %x\n", irq);
2514         printk("cache_line_size = %x\n", cache_line_size);
2515         printk("latency_timer = %x\n", latency_timer);
2516         printk("board_id = %x\n", board_id);
2517 #endif /* CCISS_DEBUG */ 
2518
2519         c->intr = irq;
2520
2521         /*
2522          * Memory base addr is first addr , the second points to the config
2523          *   table
2524          */
2525
2526         c->paddr = addr[0] ; /* addressing mode bits already removed */
2527 #ifdef CCISS_DEBUG
2528         printk("address 0 = %x\n", c->paddr);
2529 #endif /* CCISS_DEBUG */ 
2530         c->vaddr = remap_pci_mem(c->paddr, 200);
2531
2532         /* get the address index number */
2533         cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2534         /* I am not prepared to deal with a 64 bit address value */
2535         cfg_base_addr &= 0xffff;
2536 #ifdef CCISS_DEBUG
2537         printk("cfg base address = %x\n", cfg_base_addr);
2538 #endif /* CCISS_DEBUG */
2539         cfg_base_addr_index = (cfg_base_addr  - PCI_BASE_ADDRESS_0)/4;
2540 #ifdef CCISS_DEBUG
2541         printk("cfg base address index = %x\n", cfg_base_addr_index);
2542 #endif /* CCISS_DEBUG */
2543
2544         cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2545 #ifdef CCISS_DEBUG
2546         printk("cfg offset = %x\n", cfg_offset);
2547 #endif /* CCISS_DEBUG */
2548         c->cfgtable = (CfgTable_struct *) 
2549                 remap_pci_mem((addr[cfg_base_addr_index] & 0xfffffff0)
2550                                 + cfg_offset, sizeof(CfgTable_struct));
2551         c->board_id = board_id;
2552
2553 #ifdef CCISS_DEBUG
2554         print_cfg_table(c->cfgtable); 
2555 #endif /* CCISS_DEBUG */
2556
2557         for(i=0; i<NR_PRODUCTS; i++) {
2558                 if (board_id == products[i].board_id) {
2559                         c->product_name = products[i].product_name;
2560                         c->access = *(products[i].access);
2561                         break;
2562                 }
2563         }
2564         if (i == NR_PRODUCTS) {
2565                 printk(KERN_WARNING "cciss: Sorry, I don't know how"
2566                         " to access the Smart Array controller %08lx\n", 
2567                                 (unsigned long)board_id);
2568                 return -1;
2569         }
2570         if (  (readb(&c->cfgtable->Signature[0]) != 'C') ||
2571               (readb(&c->cfgtable->Signature[1]) != 'I') ||
2572               (readb(&c->cfgtable->Signature[2]) != 'S') ||
2573               (readb(&c->cfgtable->Signature[3]) != 'S') ) {
2574                 printk("Does not appear to be a valid CISS config table\n");
2575                 return -1;
2576         }
2577 #ifdef CCISS_DEBUG
2578         printk("Trying to put board into Simple mode\n");
2579 #endif /* CCISS_DEBUG */ 
2580         c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
2581         /* Update the field, and then ring the doorbell */ 
2582         writel( CFGTBL_Trans_Simple, 
2583                 &(c->cfgtable->HostWrite.TransportRequest));
2584         writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
2585
2586         for(i=0;i<MAX_CONFIG_WAIT;i++) {
2587                 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
2588                         break;
2589                 /* delay and try again */
2590                 udelay(1000);
2591         }       
2592
2593 #ifdef CCISS_DEBUG
2594         printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
2595 #endif /* CCISS_DEBUG */
2596 #ifdef CCISS_DEBUG
2597         print_cfg_table(c->cfgtable);   
2598 #endif /* CCISS_DEBUG */ 
2599
2600         if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
2601                 printk(KERN_WARNING "cciss: unable to get board into"
2602                                         " simple mode\n");
2603                 return -1;
2604         }
2605         return 0;
2606
2607 }
2608
2609 /* 
2610  * Gets information about the local volumes attached to the controller. 
2611  */ 
2612 static void cciss_getgeometry(int cntl_num)
2613 {
2614         ReportLunData_struct *ld_buff;
2615         ReadCapdata_struct *size_buff;
2616         InquiryData_struct *inq_buff;
2617         int return_code;
2618         int i;
2619         int listlength = 0;
2620         __u32 lunid = 0;
2621         int block_size;
2622         int total_size; 
2623
2624         ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
2625         if (ld_buff == NULL) {
2626                 printk(KERN_ERR "cciss: out of memory\n");
2627                 return;
2628         }
2629         memset(ld_buff, 0, sizeof(ReportLunData_struct));
2630         size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
2631         if (size_buff == NULL) {
2632                 printk(KERN_ERR "cciss: out of memory\n");
2633                 kfree(ld_buff);
2634                 return;
2635         }
2636         inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
2637         if (inq_buff == NULL) {
2638                 printk(KERN_ERR "cciss: out of memory\n");
2639                 kfree(ld_buff);
2640                 kfree(size_buff);
2641                 return;
2642         }
2643         /* Get the firmware version */ 
2644         return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff, 
2645                 sizeof(InquiryData_struct), 0, 0 ,0, NULL);
2646         if (return_code == IO_OK) {
2647                 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
2648                 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
2649                 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
2650                 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
2651         } else  {       /* send command failed */
2652                 printk(KERN_WARNING "cciss: unable to determine firmware"
2653                         " version of controller\n");
2654         }
2655         /* Get the number of logical volumes */ 
2656         return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff, 
2657                         sizeof(ReportLunData_struct), 0, 0, 0, NULL);
2658
2659         if (return_code == IO_OK) {
2660 #ifdef CCISS_DEBUG
2661                 printk("LUN Data\n--------------------------\n");
2662 #endif /* CCISS_DEBUG */ 
2663
2664                 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
2665                 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
2666                 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;  
2667                 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
2668         } else { /* reading number of logical volumes failed */
2669                 printk(KERN_WARNING "cciss: report logical volume"
2670                         " command failed\n");
2671                 listlength = 0;
2672         }
2673         hba[cntl_num]->num_luns = listlength / 8; /* 8 bytes pre entry */
2674         if (hba[cntl_num]->num_luns > CISS_MAX_LUN) {
2675                 printk(KERN_ERR "cciss:  only %d number of logical volumes supported\n",
2676                         CISS_MAX_LUN);
2677                 hba[cntl_num]->num_luns = CISS_MAX_LUN;
2678         }
2679 #ifdef CCISS_DEBUG
2680         printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
2681                 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
2682                 ld_buff->LUNListLength[3],  hba[cntl_num]->num_luns);
2683 #endif /* CCISS_DEBUG */
2684
2685         hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
2686         for(i=0; i<  hba[cntl_num]->num_luns; i++) {
2687                 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
2688                 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
2689                 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
2690                 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
2691                 hba[cntl_num]->drv[i].LunID = lunid;
2692
2693 #ifdef CCISS_DEBUG
2694                 printk(KERN_DEBUG "LUN[%d]:  %x %x %x %x = %x\n", i, 
2695                 ld_buff->LUN[i][0], ld_buff->LUN[i][1],ld_buff->LUN[i][2], 
2696                 ld_buff->LUN[i][3], hba[cntl_num]->drv[i].LunID);
2697 #endif /* CCISS_DEBUG */
2698
2699                 memset(size_buff, 0, sizeof(ReadCapdata_struct));
2700                 return_code = sendcmd(CCISS_READ_CAPACITY, cntl_num, size_buff, 
2701                                 sizeof( ReadCapdata_struct), 1, i, 0, NULL);
2702                 if (return_code == IO_OK) {
2703                         total_size = (0xff & 
2704                                 (unsigned int)(size_buff->total_size[0])) << 24;
2705                         total_size |= (0xff & 
2706                                 (unsigned int)(size_buff->total_size[1])) << 16;
2707                         total_size |= (0xff & 
2708                                 (unsigned int)(size_buff->total_size[2])) << 8;
2709                         total_size |= (0xff & (unsigned int)
2710                                 (size_buff->total_size[3])); 
2711                         total_size++;   /* command returns highest */
2712                                         /* block address */
2713
2714                         block_size = (0xff & 
2715                                 (unsigned int)(size_buff->block_size[0])) << 24;
2716                         block_size |= (0xff & 
2717                                 (unsigned int)(size_buff->block_size[1])) << 16;
2718                         block_size |= (0xff & 
2719                                 (unsigned int)(size_buff->block_size[2])) << 8;
2720                         block_size |= (0xff & 
2721                                 (unsigned int)(size_buff->block_size[3]));
2722                 } else {        /* read capacity command failed */ 
2723                         printk(KERN_WARNING "cciss: read capacity failed\n");
2724                         total_size = block_size = 0; 
2725                 }       
2726                 printk(KERN_INFO "      blocks= %d block_size= %d\n", 
2727                                         total_size, block_size);
2728
2729                 /* Execute the command to read the disk geometry */
2730                 memset(inq_buff, 0, sizeof(InquiryData_struct));
2731                 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
2732                         sizeof(InquiryData_struct), 1, i, 0xC1, NULL );
2733                 if (return_code == IO_OK) {
2734                         if (inq_buff->data_byte[8] == 0xFF) {
2735                            printk(KERN_WARNING "cciss: reading geometry failed, volume does not support reading geometry\n");
2736
2737                            hba[cntl_num]->drv[i].block_size = block_size;
2738                            hba[cntl_num]->drv[i].nr_blocks = total_size;
2739                            hba[cntl_num]->drv[i].heads = 255;
2740                            hba[cntl_num]->drv[i].sectors = 32; /* Sectors */
2741                                                                 /* per track */
2742                            hba[cntl_num]->drv[i].cylinders = total_size 
2743                                                                 / 255 / 32;
2744                         } else {
2745
2746                            hba[cntl_num]->drv[i].block_size = block_size;
2747                            hba[cntl_num]->drv[i].nr_blocks = total_size;
2748                            hba[cntl_num]->drv[i].heads = 
2749                                         inq_buff->data_byte[6]; 
2750                            hba[cntl_num]->drv[i].sectors = 
2751                                         inq_buff->data_byte[7]; 
2752                            hba[cntl_num]->drv[i].cylinders = 
2753                                         (inq_buff->data_byte[4] & 0xff) << 8;
2754                            hba[cntl_num]->drv[i].cylinders += 
2755                                         inq_buff->data_byte[5];
2756                            hba[cntl_num]->drv[i].raid_level = 
2757                                         inq_buff->data_byte[8]; 
2758                         }
2759                 }
2760                 else {  /* Get geometry failed */
2761                         printk(KERN_WARNING "cciss: reading geometry failed, continuing with default geometry\n"); 
2762
2763                         hba[cntl_num]->drv[i].block_size = block_size;
2764                         hba[cntl_num]->drv[i].nr_blocks = total_size;
2765                         hba[cntl_num]->drv[i].heads = 255;
2766                         hba[cntl_num]->drv[i].sectors = 32;     /* Sectors */
2767                                                                 /* per track */
2768                         hba[cntl_num]->drv[i].cylinders = total_size / 255 / 32;
2769                 }
2770                 if (hba[cntl_num]->drv[i].raid_level > 5)
2771                         hba[cntl_num]->drv[i].raid_level = RAID_UNKNOWN;
2772                 printk(KERN_INFO "      heads= %d, sectors= %d, cylinders= %d RAID %s\n\n",
2773                         hba[cntl_num]->drv[i].heads, 
2774                         hba[cntl_num]->drv[i].sectors,
2775                         hba[cntl_num]->drv[i].cylinders,
2776                         raid_label[hba[cntl_num]->drv[i].raid_level]); 
2777         }
2778         kfree(ld_buff);
2779         kfree(size_buff);
2780         kfree(inq_buff);
2781 }       
2782
2783 /* Function to find the first free pointer into our hba[] array */
2784 /* Returns -1 if no free entries are left.  */
2785 static int alloc_cciss_hba(void)
2786 {
2787         int i;
2788         for(i=0; i< MAX_CTLR; i++) {
2789                 if (hba[i] == NULL) {
2790                         hba[i] = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
2791                         if (hba[i]==NULL) {
2792                                 printk(KERN_ERR "cciss: out of memory.\n");
2793                                 return -1;
2794                         }
2795                         return i;
2796                 }
2797         }
2798         printk(KERN_WARNING "cciss: This driver supports a maximum"
2799                 " of 8 controllers.\n");
2800         return -1;
2801 }
2802
2803 static void free_hba(int i)
2804 {
2805         kfree(hba[i]);
2806         hba[i]=NULL;
2807 }
2808
2809 /*
2810  *  This is it.  Find all the controllers and register them.  I really hate
2811  *  stealing all these major device numbers.
2812  *  returns the number of block devices registered.
2813  */
2814 static int __init cciss_init_one(struct pci_dev *pdev,
2815         const struct pci_device_id *ent)
2816 {
2817         request_queue_t *q;
2818         int i;
2819         int j;
2820
2821         printk(KERN_DEBUG "cciss: Device 0x%x has been found at"
2822                         " bus %d dev %d func %d\n",
2823                 pdev->device, pdev->bus->number, PCI_SLOT(pdev->devfn),
2824                         PCI_FUNC(pdev->devfn));
2825         i = alloc_cciss_hba();
2826         if (i < 0 ) 
2827                 return -1;
2828         memset(hba[i], 0, sizeof(ctlr_info_t));
2829         if (cciss_pci_init(hba[i], pdev) != 0) {
2830                 free_hba(i);
2831                 return -1;
2832         }
2833         sprintf(hba[i]->devname, "cciss%d", i);
2834         hba[i]->ctlr = i;
2835         hba[i]->pdev = pdev;
2836
2837         /* configure PCI DMA stuff */
2838         if (!pci_set_dma_mask(pdev, (u64) 0xffffffffffffffff))
2839                 printk("cciss: using DAC cycles\n");
2840         else if (!pci_set_dma_mask(pdev, (u64) 0xffffffff))
2841                 printk("cciss: not using DAC cycles\n");
2842         else {
2843                 printk("cciss: no suitable DMA available\n");
2844                 free_hba(i);
2845                 return -ENODEV;
2846         }
2847                 
2848         if (register_blkdev(MAJOR_NR+i, hba[i]->devname, &cciss_fops)) {
2849                 printk(KERN_ERR "cciss:  Unable to get major number "
2850                         "%d for %s\n", MAJOR_NR+i, hba[i]->devname);
2851                 release_io_mem(hba[i]);
2852                 free_hba(i);
2853                 return -1;
2854         }
2855         /* make sure the board interrupts are off */
2856         hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
2857         if (request_irq(hba[i]->intr, do_cciss_intr, 
2858                 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM, 
2859                         hba[i]->devname, hba[i])) {
2860
2861                 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
2862                         hba[i]->intr, hba[i]->devname);
2863                 unregister_blkdev( MAJOR_NR+i, hba[i]->devname);
2864                 release_io_mem(hba[i]);
2865                 free_hba(i);
2866                 return -1;
2867         }
2868         hba[i]->cmd_pool_bits = (__u32*)kmalloc(
2869                 ((NR_CMDS+31)/32)*sizeof(__u32), GFP_KERNEL);
2870         hba[i]->cmd_pool = (CommandList_struct *)pci_alloc_consistent(
2871                 hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct), 
2872                 &(hba[i]->cmd_pool_dhandle));
2873         hba[i]->errinfo_pool = (ErrorInfo_struct *)pci_alloc_consistent(
2874                 hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct), 
2875                 &(hba[i]->errinfo_pool_dhandle));
2876         if ((hba[i]->cmd_pool_bits == NULL) 
2877                 || (hba[i]->cmd_pool == NULL)
2878                 || (hba[i]->errinfo_pool == NULL)) {
2879
2880                 if (hba[i]->cmd_pool_bits)
2881                         kfree(hba[i]->cmd_pool_bits);
2882                 if (hba[i]->cmd_pool)
2883                         pci_free_consistent(hba[i]->pdev,  
2884                                 NR_CMDS * sizeof(CommandList_struct), 
2885                                 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);    
2886                 if (hba[i]->errinfo_pool)
2887                         pci_free_consistent(hba[i]->pdev,
2888                                 NR_CMDS * sizeof( ErrorInfo_struct),
2889                                 hba[i]->errinfo_pool, 
2890                                 hba[i]->errinfo_pool_dhandle);
2891                 free_irq(hba[i]->intr, hba[i]);
2892                 unregister_blkdev(MAJOR_NR+i, hba[i]->devname);
2893                 release_io_mem(hba[i]);
2894                 free_hba(i);
2895                 printk( KERN_ERR "cciss: out of memory");
2896                 return -1;
2897         }
2898
2899         /* Initialize the pdev driver private data. 
2900                 have it point to hba[i].  */
2901         pci_set_drvdata(pdev, hba[i]);
2902         /* command and error info recs zeroed out before 
2903                         they are used */
2904         memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+31)/32)*sizeof(__u32));
2905
2906 #ifdef CCISS_DEBUG      
2907         printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
2908 #endif /* CCISS_DEBUG */
2909
2910         cciss_getgeometry(i);
2911
2912         cciss_find_non_disk_devices(i); /* find our tape drives, if any */
2913
2914         /* Turn the interrupts on so we can service requests */
2915         hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
2916
2917         cciss_procinit(i);
2918
2919         q = BLK_DEFAULT_QUEUE(MAJOR_NR + i);
2920         q->queuedata = hba[i];
2921         blk_init_queue(q, do_cciss_request);
2922         blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
2923         blk_queue_headactive(q, 0);             
2924
2925         /* fill in the other Kernel structs */
2926         blksize_size[MAJOR_NR+i] = hba[i]->blocksizes;
2927         hardsect_size[MAJOR_NR+i] = hba[i]->hardsizes;
2928         read_ahead[MAJOR_NR+i] = READ_AHEAD;
2929
2930         /* Set the pointers to queue functions */ 
2931         q->back_merge_fn = cpq_back_merge_fn;
2932         q->front_merge_fn = cpq_front_merge_fn;
2933         q->merge_requests_fn = cpq_merge_requests_fn;
2934
2935
2936         /* Fill in the gendisk data */  
2937         hba[i]->gendisk.major = MAJOR_NR + i;
2938         hba[i]->gendisk.major_name = "cciss";
2939         hba[i]->gendisk.minor_shift = NWD_SHIFT;
2940         hba[i]->gendisk.max_p = MAX_PART;
2941         hba[i]->gendisk.part = hba[i]->hd;
2942         hba[i]->gendisk.sizes = hba[i]->sizes;
2943         hba[i]->gendisk.nr_real = hba[i]->highest_lun+1;
2944         hba[i]->gendisk.fops = &cciss_fops;
2945
2946         /* Get on the disk list */ 
2947         add_gendisk(&(hba[i]->gendisk));
2948
2949         cciss_geninit(i);
2950         for(j=0; j<NWD; j++)
2951                 register_disk(&(hba[i]->gendisk),
2952                         MKDEV(MAJOR_NR+i, j <<4), 
2953                         MAX_PART, &cciss_fops, 
2954                         hba[i]->drv[j].nr_blocks);
2955
2956         cciss_register_scsi(i, 1);  /* hook ourself into SCSI subsystem */
2957
2958         return 1;
2959 }
2960
2961 static void __devexit cciss_remove_one (struct pci_dev *pdev)
2962 {
2963         ctlr_info_t *tmp_ptr;
2964         int i;
2965         char flush_buf[4];
2966         int return_code; 
2967
2968         if (pci_get_drvdata(pdev) == NULL) {
2969                 printk( KERN_ERR "cciss: Unable to remove device \n");
2970                 return;
2971         }
2972         tmp_ptr = pci_get_drvdata(pdev);
2973         i = tmp_ptr->ctlr;
2974         if (hba[i] == NULL) {
2975                 printk(KERN_ERR "cciss: device appears to "
2976                         "already be removed \n");
2977                 return;
2978         }
2979         /* Turn board interrupts off  and send the flush cache command */
2980         /* sendcmd will turn off interrupt, and send the flush...
2981          * To write all data in the battery backed cache to disks */
2982         memset(flush_buf, 0, 4);
2983         return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4,0,0,0, NULL);
2984         if (return_code != IO_OK) {
2985                 printk(KERN_WARNING 
2986                         "Error Flushing cache on controller %d\n", i);
2987         }
2988         free_irq(hba[i]->intr, hba[i]);
2989         pci_set_drvdata(pdev, NULL);
2990         iounmap((void*)hba[i]->vaddr);
2991         cciss_unregister_scsi(i);  /* unhook from SCSI subsystem */
2992         unregister_blkdev(MAJOR_NR+i, hba[i]->devname);
2993         remove_proc_entry(hba[i]->devname, proc_cciss); 
2994         
2995
2996         /* remove it from the disk list */
2997         del_gendisk(&(hba[i]->gendisk));
2998
2999         pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct), 
3000                 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
3001         pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
3002                 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
3003         kfree(hba[i]->cmd_pool_bits);
3004         release_io_mem(hba[i]);
3005         free_hba(i);
3006 }       
3007
3008 static struct pci_driver cciss_pci_driver = {
3009          name:   "cciss",
3010         probe:  cciss_init_one,
3011         remove:  __devexit_p(cciss_remove_one),
3012         id_table:  cciss_pci_device_id, /* id_table */
3013 };
3014
3015 /*
3016 *  This is it.  Register the PCI driver information for the cards we control
3017 *  the OS will call our registered routines when it finds one of our cards. 
3018 */
3019 int __init cciss_init(void)
3020 {
3021
3022         printk(KERN_INFO DRIVER_NAME "\n");
3023         /* Register for out PCI devices */
3024         if (pci_register_driver(&cciss_pci_driver) > 0 )
3025                 return 0;
3026         else 
3027                 return -ENODEV;
3028
3029  }
3030
3031 EXPORT_NO_SYMBOLS;
3032 static int __init init_cciss_module(void)
3033 {
3034
3035         return cciss_init();
3036 }
3037
3038 static void __exit cleanup_cciss_module(void)
3039 {
3040         int i;
3041
3042         pci_unregister_driver(&cciss_pci_driver);
3043         /* double check that all controller entrys have been removed */
3044         for (i=0; i< MAX_CTLR; i++) {
3045                 if (hba[i] != NULL) {
3046                         printk(KERN_WARNING "cciss: had to remove"
3047                                         " controller %d\n", i);
3048                         cciss_remove_one(hba[i]->pdev);
3049                 }
3050         }
3051         remove_proc_entry("cciss", proc_root_driver);
3052 }
3053
3054 module_init(init_cciss_module);
3055 module_exit(cleanup_cciss_module);