Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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