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