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