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