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