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