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