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