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