~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/block/cciss.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/malloc.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.4.0)"
 48 #define DRIVER_VERSION CCISS_DRIVER_VERSION(2,4,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 5300");
 53 
 54 #include "cciss_cmd.h"
 55 #include "cciss.h"
 56 #include <linux/cciss_ioctl.h>
 57 
 58 #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
 59 
 60 /*  board_id = Subsystem Device ID & Vendor ID
 61  *  product = Marketing Name for the board
 62  *  access = Address of the struct of function pointers 
 63  */
 64 static struct board_type products[] = {
 65         { 0x40700E11, "Smart Array 5300",       &SA5_access },
 66 };
 67 
 68 /* How long to wait (in millesconds) for board to go into simple mode */
 69 #define MAX_CONFIG_WAIT 1000 
 70 
 71 #define READ_AHEAD       128
 72 #define NR_CMDS          128 /* #commands that can be outstanding */
 73 #define MAX_CTLR 8
 74 static int nr_ctlr; 
 75 static ctlr_info_t *hba[MAX_CTLR];
 76 
 77 static struct proc_dir_entry *proc_cciss;
 78 
 79 static void do_cciss_request(int i);
 80 /*
 81  * This is a hack.  This driver eats a major number for each controller, and
 82  * sets blkdev[xxx].request_fn to each one of these so the real request
 83  * function knows what controller its working with.
 84  */
 85 #define DO_CCISS_REQUEST(x) { do_cciss_request(x); }
 86 
 87 static void do_cciss_request0(request_queue_t * q) DO_CCISS_REQUEST(0);
 88 static void do_cciss_request1(request_queue_t * q) DO_CCISS_REQUEST(1);
 89 static void do_cciss_request2(request_queue_t * q) DO_CCISS_REQUEST(2);
 90 static void do_cciss_request3(request_queue_t * q) DO_CCISS_REQUEST(3);
 91 static void do_cciss_request4(request_queue_t * q) DO_CCISS_REQUEST(4);
 92 static void do_cciss_request5(request_queue_t * q) DO_CCISS_REQUEST(5);
 93 static void do_cciss_request6(request_queue_t * q) DO_CCISS_REQUEST(6);
 94 static void do_cciss_request7(request_queue_t * q) DO_CCISS_REQUEST(7);
 95 
 96 static int cciss_open(struct inode *inode, struct file *filep);
 97 static int cciss_release(struct inode *inode, struct file *filep);
 98 static int cciss_ioctl(struct inode *inode, struct file *filep, 
 99                 unsigned int cmd, unsigned long arg);
100 
101 static int revalidate_allvol(kdev_t dev);
102 static int revalidate_logvol(kdev_t dev, int maxusage);
103 static int frevalidate_logvol(kdev_t dev);
104 
105 static void cciss_getgeometry(int cntl_num);
106 
107 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c);
108 static void start_io( ctlr_info_t *h);
109 
110 #ifdef CONFIG_PROC_FS
111 static int cciss_proc_get_info(char *buffer, char **start, off_t offset, 
112                 int length, int *eof, void *data);
113 static void cciss_procinit(int i);
114 #else
115 static int cciss_proc_get_info(char *buffer, char **start, off_t offset, 
116                 int length, int *eof, void *data) { return 0;}
117 static void cciss_procinit(int i) {}
118 #endif /* CONFIG_PROC_FS */
119 
120 static struct block_device_operations cciss_fops  = {
121         open:                   cciss_open, 
122         release:                cciss_release,
123         ioctl:                  cciss_ioctl,
124         revalidate:             frevalidate_logvol,
125 };
126 
127 /*
128  * Report information about this controller.
129  */
130 #ifdef CONFIG_PROC_FS
131 static int cciss_proc_get_info(char *buffer, char **start, off_t offset, 
132                 int length, int *eof, void *data)
133 {
134         off_t pos = 0;
135         off_t len = 0;
136         int size, i, ctlr;
137         ctlr_info_t *h = (ctlr_info_t*)data;
138         drive_info_struct *drv;
139 
140         ctlr = h->ctlr;
141         size = sprintf(buffer, "%s:  Compaq %s Controller\n"
142                 "       Board ID: %08lx\n"
143                 "       Firmware Version: %c%c%c%c\n"
144                 "       Memory Address: %08lx\n"
145                 "       IRQ: 0x%x\n"
146                 "       Logical drives: %d\n"
147                 "       Current Q depth: %d\n"
148                 "       Current # commands on controller %d\n"
149                 "       Max Q depth since init: %d\n"
150                 "       Max # commands on controller since init: %d\n"
151                 "       Max SG entries since init: %d\n\n",
152                 h->devname,
153                 h->product_name,
154                 (unsigned long)h->board_id,
155                 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
156                 (unsigned long)h->vaddr,
157                 (unsigned int)h->intr,
158                 h->num_luns, 
159                 h->Qdepth, h->commands_outstanding,
160                 h->maxQsinceinit, h->max_outstanding, h->maxSG);
161 
162         pos += size; len += size;
163         for(i=0; i<h->num_luns; i++) {
164                 drv = &h->drv[i];
165                 size = sprintf(buffer+len, "cciss/c%dd%d: blksz=%d nr_blocks=%d\n",
166                                 ctlr, i, drv->block_size, drv->nr_blocks);
167                 pos += size; len += size;
168         }
169 
170         size = sprintf(buffer+len, "nr_allocs = %d\nnr_frees = %d\n",
171                         h->nr_allocs, h->nr_frees);
172         pos += size; len += size;
173 
174         *eof = 1;
175         *start = buffer+offset;
176         len -= offset;
177         if (len>length)
178                 len = length;
179         return len;
180 }
181 
182 /*
183  * Get us a file in /proc/cciss that says something about each controller.
184  * Create /proc/cciss if it doesn't exist yet.
185  */
186 static void __init cciss_procinit(int i)
187 {
188         if (proc_cciss == NULL) {
189                 proc_cciss = proc_mkdir("driver/cciss", NULL);
190                 if (!proc_cciss) 
191                         return;
192         }
193 
194         create_proc_read_entry(hba[i]->devname, 0, proc_cciss,
195                         cciss_proc_get_info, hba[i]);
196 }
197 #endif /* CONFIG_PROC_FS */
198 
199 /* 
200  * For operations that cannot sleep, a command block is allocated at init, 
201  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
202  * which ones are free or in use.  For operations that can wait for kmalloc 
203  * to possible sleep, this routine can be called with a NULL pointer. 
204  * cmd_free() MUST be called with a NULL pointer if cmd_alloc was. 
205  */ 
206 static CommandList_struct * cmd_alloc(ctlr_info_t *h)
207 {
208         CommandList_struct *c;
209         int i; 
210         u64bit temp64;
211 
212         if (h == NULL)
213         {
214                 c = (CommandList_struct *)kmalloc(sizeof(CommandList_struct), 
215                         GFP_KERNEL);
216                 if(c==NULL)
217                         return NULL;
218                 memset(c, 0, sizeof(CommandList_struct));
219 
220                 c->err_info = (ErrorInfo_struct *)kmalloc(
221                                         sizeof(ErrorInfo_struct), GFP_KERNEL);
222         
223                 if (c->err_info == NULL)
224                 {
225                         kfree(c);
226                         return NULL;
227                 }
228                 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
229         } else /* get it out of the controllers pool */ 
230         {
231                 do {
232                         i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
233                         if (i == NR_CMDS)
234                                 return NULL;
235                 } while(test_and_set_bit(i%32, h->cmd_pool_bits+(i/32)) != 0);
236 #ifdef CCISS_DEBUG
237                 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
238 #endif
239                 c = h->cmd_pool + i;
240                 memset(c, 0, sizeof(CommandList_struct));                       
241                 c->err_info = h->errinfo_pool + i;
242                 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
243                 h->nr_allocs++;
244         }
245 
246 
247         temp64.val = (__u64) virt_to_bus(c->err_info);
248         c->ErrDesc.Addr.lower = temp64.val32.lower;
249         c->ErrDesc.Addr.upper = temp64.val32.upper;
250         c->ErrDesc.Len = sizeof(ErrorInfo_struct);
251         c->busaddr = virt_to_bus(c);
252         return c;
253 
254 
255 }
256 
257 /* 
258  * Frees a command block that was previously allocated with cmd_alloc(). 
259  */
260 static void cmd_free(ctlr_info_t *h, CommandList_struct *c)
261 {
262         int i;
263 
264         if( h == NULL)
265         { 
266                 kfree(c->err_info);
267                 kfree(c);       
268         } else 
269         {
270                 i = c - h->cmd_pool;
271                 clear_bit(i%32, h->cmd_pool_bits+(i/32));
272                 h->nr_frees++;
273         }
274 }
275 
276 /*  
277  * fills in the disk information. 
278  */
279 static void cciss_geninit( int ctlr)
280 {
281         drive_info_struct *drv;
282         int i,j;
283         
284         /* Loop through each real device */ 
285         hba[ctlr]->gendisk.nr_real = 0; 
286         for(i=0; i< NWD; i++)
287         {
288                 drv = &(hba[ctlr]->drv[i]);
289                 if( !(drv->nr_blocks))
290                         continue;
291                 hba[ctlr]->hd[i << NWD_SHIFT].nr_sects = 
292                 hba[ctlr]->sizes[i << NWD_SHIFT] = drv->nr_blocks;
293 
294                 /* for each partition */ 
295                 for(j=0; j<MAX_PART; j++)
296                 {
297                         hba[ctlr]->blocksizes[(i<<NWD_SHIFT) + j] = 1024; 
298 
299                         hba[ctlr]->hardsizes[ (i<<NWD_SHIFT) + j] = 
300                                 drv->block_size;
301                 }
302                 hba[ctlr]->gendisk.nr_real++;
303         }
304 }
305 /*
306  * Open.  Make sure the device is really there.
307  */
308 static int cciss_open(struct inode *inode, struct file *filep)
309 {
310         int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
311         int dsk  = MINOR(inode->i_rdev) >> NWD_SHIFT;
312 
313 #ifdef CCISS_DEBUG
314         printk(KERN_DEBUG "cciss_open %x (%x:%x)\n", inode->i_rdev, ctlr, dsk);
315 #endif /* CCISS_DEBUG */ 
316 
317         if (ctlr > MAX_CTLR || hba[ctlr] == NULL)
318                 return -ENXIO;
319 
320         if (!suser() && hba[ctlr]->sizes[ MINOR(inode->i_rdev)] == 0)
321                 return -ENXIO;
322 
323         /*
324          * Root is allowed to open raw volume zero even if its not configured
325          * so array config can still work.  I don't think I really like this,
326          * but I'm already using way to many device nodes to claim another one
327          * for "raw controller".
328          */
329         if (suser()
330                 && (hba[ctlr]->sizes[MINOR(inode->i_rdev)] == 0) 
331                 && (MINOR(inode->i_rdev)!= 0))
332                 return -ENXIO;
333 
334         hba[ctlr]->drv[dsk].usage_count++;
335         hba[ctlr]->usage_count++;
336         MOD_INC_USE_COUNT;
337         return 0;
338 }
339 /*
340  * Close.  Sync first.
341  */
342 static int cciss_release(struct inode *inode, struct file *filep)
343 {
344         int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
345         int dsk  = MINOR(inode->i_rdev) >> NWD_SHIFT;
346 
347 #ifdef CCISS_DEBUG
348         printk(KERN_DEBUG "cciss_release %x (%x:%x)\n", inode->i_rdev, ctlr, dsk);
349 #endif /* CCISS_DEBUG */
350 
351         /* fsync_dev(inode->i_rdev); */
352 
353         hba[ctlr]->drv[dsk].usage_count--;
354         hba[ctlr]->usage_count--;
355         MOD_DEC_USE_COUNT;
356         return 0;
357 }
358 
359 /*
360  * ioctl 
361  */
362 static int cciss_ioctl(struct inode *inode, struct file *filep, 
363                 unsigned int cmd, unsigned long arg)
364 {
365         int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
366         int dsk  = MINOR(inode->i_rdev) >> NWD_SHIFT;
367         int diskinfo[4];
368         struct hd_geometry *geo = (struct hd_geometry *)arg;
369 
370 #ifdef CCISS_DEBUG
371         printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
372 #endif /* CCISS_DEBUG */ 
373         
374         switch(cmd) {
375         case HDIO_GETGEO:
376                 if (hba[ctlr]->drv[dsk].cylinders) {
377                         diskinfo[0] = hba[ctlr]->drv[dsk].heads;
378                         diskinfo[1] = hba[ctlr]->drv[dsk].sectors;
379                         diskinfo[2] = hba[ctlr]->drv[dsk].cylinders;
380                 } else {
381                         diskinfo[0] = 0xff;
382                         diskinfo[1] = 0x3f;
383                         diskinfo[2] = hba[ctlr]->drv[dsk].nr_blocks / (0xff*0x3f);              }
384                 put_user(diskinfo[0], &geo->heads);
385                 put_user(diskinfo[1], &geo->sectors);
386                 put_user(diskinfo[2], &geo->cylinders);
387                 put_user(hba[ctlr]->hd[MINOR(inode->i_rdev)].start_sect, &geo->start);
388                 return 0;
389         case BLKGETSIZE:
390                 if (!arg) return -EINVAL;
391                 put_user(hba[ctlr]->hd[MINOR(inode->i_rdev)].nr_sects, (long*)arg);
392                 return 0;
393         case BLKRRPART:
394                 return revalidate_logvol(inode->i_rdev, 1);
395         case BLKFLSBUF:
396         case BLKROSET:
397         case BLKROGET:
398         case BLKRASET:
399         case BLKRAGET:
400         case BLKPG:
401                 return( blk_ioctl(inode->i_rdev, cmd, arg));
402         case CCISS_GETPCIINFO:
403         {
404                 cciss_pci_info_struct pciinfo;
405 
406                 if (!arg) return -EINVAL;
407                 pciinfo.bus = hba[ctlr]->pci_bus;
408                 pciinfo.dev_fn = hba[ctlr]->pci_dev_fn;
409                 pciinfo.board_id = hba[ctlr]->board_id;
410                 if (copy_to_user((void *) arg, &pciinfo,  sizeof( cciss_pci_info_struct )))
411                         return  -EFAULT;
412                 return(0);
413         }       
414         case CCISS_GETINTINFO:
415         {
416                 cciss_coalint_struct intinfo;
417                 ctlr_info_t *c = hba[ctlr];
418 
419                 if (!arg) return -EINVAL;
420                 intinfo.delay = readl(&c->cfgtable->HostWrite.CoalIntDelay);
421                 intinfo.count = readl(&c->cfgtable->HostWrite.CoalIntCount);
422                 if (copy_to_user((void *) arg, &intinfo, sizeof( cciss_coalint_struct )))
423                         return -EFAULT;
424                 return(0);
425         }
426         case CCISS_SETINTINFO:
427         {
428                 cciss_coalint_struct intinfo;
429                 ctlr_info_t *c = hba[ctlr];
430                 unsigned long flags;
431                 int i;
432 
433                 if (!arg) return -EINVAL;       
434                 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
435                 if (copy_from_user(&intinfo, (void *) arg, sizeof( cciss_coalint_struct)))
436                         return -EFAULT;
437                 if ( (intinfo.delay == 0 ) && (intinfo.count == 0))
438 
439                 {
440 //                      printk("cciss_ioctl: delay and count cannot be 0\n");
441                         return( -EINVAL);
442                 }
443                 spin_lock_irqsave(&io_request_lock, flags);
444                 /* Can only safely update if no commands outstanding */ 
445                 if (c->commands_outstanding > 0 )
446                 {
447 //                      printk("cciss_ioctl: cannot change coalasing "
448 //                              "%d commands outstanding on controller\n", 
449 //                                      c->commands_outstanding);
450                         spin_unlock_irqrestore(&io_request_lock, flags);
451                         return(-EINVAL);
452                 }
453                 /* Update the field, and then ring the doorbell */ 
454                 writel( intinfo.delay, 
455                         &(c->cfgtable->HostWrite.CoalIntDelay));
456                 writel( intinfo.count, 
457                         &(c->cfgtable->HostWrite.CoalIntCount));
458                 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
459 
460                 for(i=0;i<MAX_CONFIG_WAIT;i++)
461                 {
462                         if (!(readl(c->vaddr + SA5_DOORBELL) 
463                                         & CFGTBL_ChangeReq))
464                                 break;
465                         /* delay and try again */
466                         udelay(1000);
467                 }       
468                 spin_unlock_irqrestore(&io_request_lock, flags);
469                 if (i >= MAX_CONFIG_WAIT)
470                         return( -EFAULT);
471                 return(0);
472         }
473         case CCISS_GETNODENAME:
474         {
475                 NodeName_type NodeName;
476                 ctlr_info_t *c = hba[ctlr];
477                 int i; 
478 
479                 if (!arg) return -EINVAL;
480                 for(i=0;i<16;i++)
481                         NodeName[i] = readb(&c->cfgtable->ServerName[i]);
482                 if (copy_to_user((void *) arg, NodeName, sizeof( NodeName_type)))
483                         return  -EFAULT;
484                 return(0);
485         }
486         case CCISS_SETNODENAME:
487         {
488                 NodeName_type NodeName;
489                 ctlr_info_t *c = hba[ctlr];
490                 unsigned long flags;
491                 int i;
492 
493                 if (!arg) return -EINVAL;
494                 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
495                 
496                 if (copy_from_user(NodeName, (void *) arg, sizeof( NodeName_type)))
497                         return -EFAULT;
498 
499                 spin_lock_irqsave(&io_request_lock, flags);
500 
501                         /* Update the field, and then ring the doorbell */ 
502                 for(i=0;i<16;i++)
503                         writeb( NodeName[i], &c->cfgtable->ServerName[i]);
504                         
505                 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
506 
507                 for(i=0;i<MAX_CONFIG_WAIT;i++)
508                 {
509                         if (!(readl(c->vaddr + SA5_DOORBELL) 
510                                         & CFGTBL_ChangeReq))
511                                 break;
512                         /* delay and try again */
513                         udelay(1000);
514                 }       
515                 spin_unlock_irqrestore(&io_request_lock, flags);
516                 if (i >= MAX_CONFIG_WAIT)
517                         return( -EFAULT);
518                 return(0);
519         }
520 
521         case CCISS_GETHEARTBEAT:
522         {
523                 Heartbeat_type heartbeat;
524                 ctlr_info_t *c = hba[ctlr];
525 
526                 if (!arg) return -EINVAL;
527                 heartbeat = readl(&c->cfgtable->HeartBeat);
528                 if (copy_to_user((void *) arg, &heartbeat, sizeof( Heartbeat_type)))
529                         return -EFAULT;
530                 return(0);
531         }
532         case CCISS_GETBUSTYPES:
533         {
534                 BusTypes_type BusTypes;
535                 ctlr_info_t *c = hba[ctlr];
536 
537                 if (!arg) return -EINVAL;
538                 BusTypes = readl(&c->cfgtable->BusTypes);
539                 if (copy_to_user((void *) arg, &BusTypes, sizeof( BusTypes_type) ))
540                         return  -EFAULT;
541                 return(0);
542         }
543         case CCISS_GETFIRMVER:
544         {
545                 FirmwareVer_type firmware;
546 
547                 if (!arg) return -EINVAL;
548                 memcpy(firmware, hba[ctlr]->firm_ver, 4);
549 
550                 if (copy_to_user((void *) arg, firmware, sizeof( FirmwareVer_type)))
551                         return -EFAULT;
552                 return(0);
553         }
554         case CCISS_GETDRIVVER:
555         {
556                 DriverVer_type DriverVer = DRIVER_VERSION;
557 
558                 if (!arg) return -EINVAL;
559 
560                 if (copy_to_user((void *) arg, &DriverVer, sizeof( DriverVer_type) ))
561                         return -EFAULT;
562                 return(0);
563         }
564 
565         case CCISS_REVALIDVOLS:
566                 return( revalidate_allvol(inode->i_rdev));
567         
568         case CCISS_PASSTHRU:
569         {
570                 IOCTL_Command_struct iocommand;
571                 ctlr_info_t *h = hba[ctlr];
572                 CommandList_struct *c;
573                 char    *buff = NULL;
574                 u64bit  temp64;
575                 unsigned long flags;
576 
577                 if (!arg) return -EINVAL;
578         
579                 if (!capable(CAP_SYS_RAWIO)) return -EPERM;
580 
581                 if (copy_from_user(&iocommand, (void *) arg, sizeof( IOCTL_Command_struct) ))
582                         return -EFAULT;
583                 if((iocommand.buf_size < 1) && 
584                                 (iocommand.Request.Type.Direction != XFER_NONE))
585                 {       
586                         return -EINVAL;
587                 } 
588                 /* Check kmalloc limits */
589                 if(iocommand.buf_size > 128000)
590                         return -EINVAL;
591                 if(iocommand.buf_size > 0)
592                 {
593                         buff =  kmalloc(iocommand.buf_size, GFP_KERNEL);
594                         if( buff == NULL) 
595                                 return -EFAULT;
596                 }
597                 if (iocommand.Request.Type.Direction == XFER_WRITE)
598                 {
599                         /* Copy the data into the buffer we created */ 
600                         if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
601                                 return -EFAULT;
602                 }
603                 if ((c = cmd_alloc(NULL)) == NULL)
604                 {
605                         if(buff!=NULL)
606                                 kfree(buff);
607                         return -ENOMEM;
608                 }
609                         // Fill in the command type 
610                 c->cmd_type = CMD_IOCTL_PEND;
611                         // Fill in Command Header 
612                 c->Header.ReplyQueue = 0;  // unused in simple mode
613                 if( iocommand.buf_size > 0)     // buffer to fill 
614                 {
615                         c->Header.SGList = 1;
616                         c->Header.SGTotal= 1;
617                 } else  // no buffers to fill  
618                 {
619                         c->Header.SGList = 0;
620                         c->Header.SGTotal= 0;
621                 }
622                 c->Header.LUN = iocommand.LUN_info;
623                 c->Header.Tag.lower = c->busaddr;  // use the kernel address the cmd block for tag
624                 
625                 // Fill in Request block 
626                 c->Request = iocommand.Request; 
627         
628                 // Fill in the scatter gather information
629                 if (iocommand.buf_size > 0 ) 
630                 {       
631                         temp64.val = (__u64) virt_to_bus(buff);
632                         c->SG[0].Addr.lower = temp64.val32.lower;
633                         c->SG[0].Addr.upper = temp64.val32.upper;
634                         c->SG[0].Len = iocommand.buf_size;
635                         c->SG[0].Ext = 0;  // we are not chaining
636                 }
637                 /* Put the request on the tail of the request queue */
638                 spin_lock_irqsave(&io_request_lock, flags);
639                 addQ(&h->reqQ, c);
640                 h->Qdepth++;
641                 start_io(h);
642                 spin_unlock_irqrestore(&io_request_lock, flags);
643 
644                 /* Wait for completion */
645                 while(c->cmd_type != CMD_IOCTL_DONE)
646                         schedule_timeout(1);
647 
648                 /* Copy the error information out */ 
649                 iocommand.error_info = *(c->err_info);
650                 if ( copy_to_user((void *) arg, &iocommand, sizeof( IOCTL_Command_struct) ) )
651                 {
652                         cmd_free(NULL, c);
653                         if (buff != NULL) 
654                                 kfree(buff);
655                         return( -EFAULT);       
656                 }       
657 
658                 if (iocommand.Request.Type.Direction == XFER_READ)
659                 {
660                         /* Copy the data out of the buffer we created */
661                         if (copy_to_user(iocommand.buf, buff, iocommand.buf_size))
662                         {
663                                      cmd_free(NULL, c);
664                                      kfree(buff);
665                         }
666                 }
667                 cmd_free(NULL, c);
668                 if (buff != NULL)
669                         kfree(buff);
670                 return(0);
671         } 
672 
673         default:
674                 return -EBADRQC;
675         }
676         
677 }
678 
679 /* Borrowed and adapted from sd.c */
680 static int revalidate_logvol(kdev_t dev, int maxusage)
681 {
682         int ctlr, target;
683         struct gendisk *gdev;
684         unsigned long flags;
685         int max_p;
686         int start;
687         int i;
688 
689         target = MINOR(dev) >> NWD_SHIFT;
690         ctlr = MAJOR(dev) - MAJOR_NR;
691         gdev = &(hba[ctlr]->gendisk);
692 
693         spin_lock_irqsave(&io_request_lock, flags);
694         if (hba[ctlr]->drv[target].usage_count > maxusage) {
695                 spin_unlock_irqrestore(&io_request_lock, flags);
696                 printk(KERN_WARNING "cpqarray: Device busy for "
697                         "revalidation (usage=%d)\n",
698                         hba[ctlr]->drv[target].usage_count);
699                 return -EBUSY;
700         }
701         hba[ctlr]->drv[target].usage_count++;
702         spin_unlock_irqrestore(&io_request_lock, flags);
703 
704         max_p = gdev->max_p;
705         start = target << gdev->minor_shift;
706 
707         for(i=max_p; i>=0; i--) {
708                 int minor = start+i;
709                 kdev_t devi = MKDEV(MAJOR_NR + ctlr, minor);
710                 struct super_block *sb = get_super(devi);
711                 sync_dev(devi);
712                 if (sb) invalidate_inodes(sb);
713                 invalidate_buffers(devi);
714                 gdev->part[minor].start_sect = 0;
715                 gdev->part[minor].nr_sects = 0;
716 
717                 /* reset the blocksize so we can read the partition table */
718                 blksize_size[MAJOR_NR+ctlr][minor] = 1024;
719         }
720         /* setup partitions per disk */
721         grok_partitions(gdev, target, MAX_PART, 
722                         hba[ctlr]->drv[target].nr_blocks);
723         hba[ctlr]->drv[target].usage_count--;
724         return 0;
725 }
726 
727 static int frevalidate_logvol(kdev_t dev)
728 {
729 #ifdef CCISS_DEBUG
730         printk(KERN_DEBUG "cciss: frevalidate has been called\n");
731 #endif /* CCISS_DEBUG */ 
732         return revalidate_logvol(dev, 0);
733 }
734 
735 /*
736  * revalidate_allvol is for online array config utilities.  After a
737  * utility reconfigures the drives in the array, it can use this function
738  * (through an ioctl) to make the driver zap any previous disk structs for
739  * that controller and get new ones.
740  *
741  * Right now I'm using the getgeometry() function to do this, but this
742  * function should probably be finer grained and allow you to revalidate one
743  * particualar logical volume (instead of all of them on a particular
744  * controller).
745  */
746 static int revalidate_allvol(kdev_t dev)
747 {
748         int ctlr, i;
749         unsigned long flags;
750 
751         ctlr = MAJOR(dev) - MAJOR_NR;
752         if (MINOR(dev) != 0)
753                 return -ENXIO;
754 
755         spin_lock_irqsave(&io_request_lock, flags);
756         if (hba[ctlr]->usage_count > 1) {
757                 spin_unlock_irqrestore(&io_request_lock, flags);
758                 printk(KERN_WARNING "cciss: Device busy for volume"
759                         " revalidation (usage=%d)\n", hba[ctlr]->usage_count);
760                 return -EBUSY;
761         }
762         spin_unlock_irqrestore(&io_request_lock, flags);
763         hba[ctlr]->usage_count++;
764 
765         /*
766          * Set the partition and block size structures for all volumes
767          * on this controller to zero.  We will reread all of this data
768          */
769         memset(hba[ctlr]->hd,         0, sizeof(struct hd_struct) * 256);
770         memset(hba[ctlr]->sizes,      0, sizeof(int) * 256);
771         memset(hba[ctlr]->blocksizes, 0, sizeof(int) * 256);
772         memset(hba[ctlr]->hardsizes,  0, sizeof(int) * 256);
773         memset(hba[ctlr]->drv,        0, sizeof(drive_info_struct)
774                                                 * CISS_MAX_LUN);
775         hba[ctlr]->gendisk.nr_real = 0;
776 
777         /*
778          * Tell the array controller not to give us any interupts while
779          * we check the new geometry.  Then turn interrupts back on when
780          * we're done.
781          */
782         hba[ctlr]->access.set_intr_mask(hba[ctlr], CCISS_INTR_OFF);
783         cciss_getgeometry(ctlr);
784         hba[ctlr]->access.set_intr_mask(hba[ctlr], CCISS_INTR_ON);
785 
786         cciss_geninit(ctlr);
787         for(i=0; i<NWD; i++)
788                 if (hba[ctlr]->sizes[ i<<NWD_SHIFT ])
789                         revalidate_logvol(dev+(i<<NWD_SHIFT), 2);
790 
791         hba[ctlr]->usage_count--;
792         return 0;
793 }
794 
795 
796 
797 /*
798  *   Wait polling for a command to complete.
799  *   The memory mapped FIFO is polled for the completion.
800  *   Used only at init time, interrupts disabled.
801  */
802 static unsigned long pollcomplete(int ctlr)
803 {
804         unsigned long done;
805         int i;
806 
807         /* Wait (up to 2 seconds) for a command to complete */
808 
809         for (i = 200000; i > 0; i--) {
810                 done = hba[ctlr]->access.command_completed(hba[ctlr]);
811                 if (done == FIFO_EMPTY) {
812                         udelay(10);     /* a short fixed delay */
813                 } else
814                         return (done);
815         }
816         /* Invalid address to tell caller we ran out of time */
817         return 1;
818 }
819 /*
820  * Send a command to the controller, and wait for it to complete.  
821  * Only used at init time. 
822  */
823 static int sendcmd(
824         __u8    cmd,
825         int     ctlr,
826         void    *buff,
827         size_t  size,
828         unsigned int use_unit_num,
829         unsigned int log_unit,
830         __u8    page_code )
831 {
832         CommandList_struct *c;
833         int i;
834         unsigned long complete;
835         ctlr_info_t *info_p= hba[ctlr];
836         u64bit temp64;
837 
838         c = cmd_alloc(info_p);
839         if (c == NULL)
840         {
841                 printk(KERN_WARNING "cciss: unable to get memory");
842                 return(IO_ERROR);
843         }
844         // Fill in Command Header 
845         c->Header.ReplyQueue = 0;  // unused in simple mode
846         if( buff != NULL)       // buffer to fill 
847         {
848                 c->Header.SGList = 1;
849                 c->Header.SGTotal= 1;
850         } else  // no buffers to fill  
851         {
852                 c->Header.SGList = 0;
853                 c->Header.SGTotal= 0;
854         }
855         c->Header.Tag.lower = c->busaddr;  // use the kernel address the cmd block for tag
856         // Fill in Request block        
857         switch(cmd)
858         {
859                 case  CISS_INQUIRY:
860                         /* If the logical unit number is 0 then, this is going
861                                 to controller so It's a physical command
862                                 mode = 0 target = 0.
863                                 So we have nothing to write. 
864                                 Otherwise 
865                                 mode = 1  target = LUNID
866                         */
867                         if(use_unit_num != 0)
868                         {
869                                 c->Header.LUN.LogDev.VolId=
870                                         hba[ctlr]->drv[log_unit].LunID;
871                                 c->Header.LUN.LogDev.Mode = 1;
872                         }
873                         /* are we trying to read a vital product page */
874                         if(page_code != 0)
875                         {
876                                 c->Request.CDB[1] = 0x01;
877                                 c->Request.CDB[2] = page_code;
878                         }
879                         c->Request.CDBLen = 6;
880                         c->Request.Type.Type =  TYPE_CMD; // It is a command. 
881                         c->Request.Type.Attribute = ATTR_SIMPLE;  
882                         c->Request.Type.Direction = XFER_READ; // Read 
883                         c->Request.Timeout = 0; // Don't time out 
884                         c->Request.CDB[0] =  CISS_INQUIRY;
885                         c->Request.CDB[4] = size  & 0xFF;  
886                 break;
887                 case CISS_REPORT_LOG:
888                         /* Talking to controller so It's a physical command
889                                 mode = 00 target = 0.
890                                 So we have nothing to write.
891                         */
892                         c->Request.CDBLen = 12;
893                         c->Request.Type.Type =  TYPE_CMD; // It is a command.
894                         c->Request.Type.Attribute = ATTR_SIMPLE; 
895                         c->Request.Type.Direction = XFER_READ; // Read
896                         c->Request.Timeout = 0; // Don't time out
897                         c->Request.CDB[0] = CISS_REPORT_LOG;
898                         c->Request.CDB[6] = (size >> 24) & 0xFF;  //MSB
899                         c->Request.CDB[7] = (size >> 16) & 0xFF;
900                         c->Request.CDB[8] = (size >> 8) & 0xFF;
901                         c->Request.CDB[9] = size & 0xFF;
902                 break;
903 
904                 case CCISS_READ_CAPACITY:
905                         c->Header.LUN.LogDev.VolId= 
906                                 hba[ctlr]->drv[log_unit].LunID;
907                         c->Header.LUN.LogDev.Mode = 1;
908                         c->Request.CDBLen = 10;
909                         c->Request.Type.Type =  TYPE_CMD; // It is a command.
910                         c->Request.Type.Attribute = ATTR_SIMPLE; 
911                         c->Request.Type.Direction = XFER_READ; // Read
912                         c->Request.Timeout = 0; // Don't time out
913                         c->Request.CDB[0] = CCISS_READ_CAPACITY;
914                 break;
915                 default:
916                         printk(KERN_WARNING
917                                 "cciss:  Unknown Command 0x%c sent attempted\n",
918                                   cmd);
919                         cmd_free(info_p, c);
920                         return(IO_ERROR);
921         };
922         // Fill in the scatter gather information
923         if (size > 0 ) 
924         {
925                 temp64.val = (__u64) virt_to_bus(buff);
926                 c->SG[0].Addr.lower = temp64.val32.lower;
927                 c->SG[0].Addr.upper = temp64.val32.upper;
928                 c->SG[0].Len = size;
929                 c->SG[0].Ext = 0;  // we are not chaining
930         }
931         /*
932          * Disable interrupt
933          */
934 #ifdef CCISS_DEBUG
935         printk(KERN_DEBUG "cciss: turning intr off\n");
936 #endif /* CCISS_DEBUG */ 
937         info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
938         
939         /* Make sure there is room in the command FIFO */
940         /* Actually it should be completely empty at this time. */
941         for (i = 200000; i > 0; i--) 
942         {
943                 /* if fifo isn't full go */
944                 if (!(info_p->access.fifo_full(info_p))) 
945                 {
946                         
947                         break;
948                 }
949                 udelay(10);
950                 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
951                         " waiting!\n", ctlr);
952         }
953         /*
954          * Send the cmd
955          */
956         info_p->access.submit_command(info_p, c);
957         complete = pollcomplete(ctlr);
958 
959 #ifdef CCISS_DEBUG
960         printk(KERN_DEBUG "cciss: command completed\n");
961 #endif /* CCISS_DEBUG */
962 
963         if (complete != 1) {
964                 if ( (complete & CISS_ERROR_BIT)
965                      && (complete & ~CISS_ERROR_BIT) == c->busaddr)
966                      {
967                         /* if data overrun or underun on Report command 
968                                 ignore it 
969                         */
970                         if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
971                              (c->Request.CDB[0] == CISS_INQUIRY)) &&
972                                 ((c->err_info->CommandStatus == 
973                                         CMD_DATA_OVERRUN) || 
974                                  (c->err_info->CommandStatus == 
975                                         CMD_DATA_UNDERRUN)
976                                 ))
977                         {
978                                 complete = c->busaddr;
979                         } else
980                         {
981                                 printk(KERN_WARNING "ciss ciss%d: sendcmd"
982                                 " Error %x \n", ctlr, 
983                                         c->err_info->CommandStatus); 
984                                 printk(KERN_WARNING "ciss ciss%d: sendcmd"
985                                 " offensive info\n"
986                                 "  size %x\n   num %x   value %x\n", ctlr,
987                                   c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
988                                   c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
989                                   c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
990                                 cmd_free(info_p,c);
991                                 return(IO_ERROR);
992                         }
993                 }
994                 if (complete != c->busaddr) {
995                         printk( KERN_WARNING "cciss cciss%d: SendCmd "
996                       "Invalid command list address returned! (%lx)\n",
997                                 ctlr, complete);
998                         cmd_free(info_p, c);
999                         return (IO_ERROR);
1000                 }
1001         } else {
1002                 printk( KERN_WARNING
1003                         "cciss cciss%d: SendCmd Timeout out, "
1004                         "No command list address returned!\n",
1005                         ctlr);
1006                 cmd_free(info_p, c);
1007                 return (IO_ERROR);
1008         }
1009         cmd_free(info_p, c);
1010         return (IO_OK);
1011 } 
1012 /*
1013  * Map (physical) PCI mem into (virtual) kernel space
1014  */
1015 static ulong remap_pci_mem(ulong base, ulong size)
1016 {
1017         ulong page_base        = ((ulong) base) & PAGE_MASK;
1018         ulong page_offs        = ((ulong) base) - page_base;
1019         ulong page_remapped    = (ulong) ioremap(page_base, page_offs+size);
1020 
1021         return (ulong) (page_remapped ? (page_remapped + page_offs) : 0UL);
1022 }
1023 
1024 /*
1025  * Enqueuing and dequeuing functions for cmdlists.
1026  */
1027 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
1028 {
1029         if (*Qptr == NULL) {
1030                 *Qptr = c;
1031                 c->next = c->prev = c;
1032         } else {
1033                 c->prev = (*Qptr)->prev;
1034                 c->next = (*Qptr);
1035                 (*Qptr)->prev->next = c;
1036                 (*Qptr)->prev = c;
1037         }
1038 }
1039 
1040 static inline CommandList_struct *removeQ(CommandList_struct **Qptr, 
1041                                                 CommandList_struct *c)
1042 {
1043         if (c && c->next != c) {
1044                 if (*Qptr == c) *Qptr = c->next;
1045                 c->prev->next = c->next;
1046                 c->next->prev = c->prev;
1047         } else {
1048                 *Qptr = NULL;
1049         }
1050         return c;
1051 }
1052 
1053 /* 
1054  * Takes jobs of the Q and sends them to the hardware, then puts it on 
1055  * the Q to wait for completion. 
1056  */ 
1057 static void start_io( ctlr_info_t *h)
1058 {
1059         CommandList_struct *c;
1060         
1061         while(( c = h->reqQ) != NULL )
1062         {
1063                 /* can't do anything if fifo is full */
1064                 if ((h->access.fifo_full(h)))
1065                 {
1066                         printk(KERN_WARNING "cciss: fifo full \n");
1067                         return;
1068                 }
1069                 /* Get the frist entry from the Request Q */ 
1070                 removeQ(&(h->reqQ), c);
1071                 h->Qdepth--;
1072         
1073                 /* Tell the controller execute command */ 
1074                 h->access.submit_command(h, c);
1075                 
1076                 /* Put job onto the completed Q */ 
1077                 addQ (&(h->cmpQ), c); 
1078         }
1079 }
1080 
1081 static inline void complete_buffers( struct buffer_head *bh, int status)
1082 {
1083         struct buffer_head *xbh;
1084         
1085         while(bh)
1086         {
1087                 xbh = bh->b_reqnext; 
1088                 bh->b_reqnext = NULL; 
1089                 blk_finished_io(bh->b_size >> 9);
1090                 bh->b_end_io(bh, status);
1091                 bh = xbh;
1092         }
1093 } 
1094 /* checks the status of the job and calls complete buffers to mark all 
1095  * buffers for the completed job. 
1096  */ 
1097 static inline void complete_command( CommandList_struct *cmd, int timeout)
1098 {
1099         int status = 1;
1100         
1101         if (timeout)
1102                 status = 0; 
1103         if(cmd->err_info->CommandStatus != 0) 
1104         { /* an error has occured */ 
1105                 switch(cmd->err_info->CommandStatus)
1106                 {
1107                         case CMD_TARGET_STATUS:
1108                                 printk(KERN_WARNING "cciss: cmd %p has "
1109                                         " completed with errors\n", cmd);
1110                                 if( cmd->err_info->ScsiStatus)
1111                                 {
1112                                         printk(KERN_WARNING "cciss: cmd %p "
1113                                         "has SCSI Status = %x\n",
1114                                                 cmd,  
1115                                                 cmd->err_info->ScsiStatus);
1116                                 }
1117 
1118                         break;
1119                         case CMD_DATA_UNDERRUN:
1120                                 printk(KERN_WARNING "cciss: cmd %p has"
1121                                         " completed with data underrun "
1122                                         "reported\n", cmd);
1123                         break;
1124                         case CMD_DATA_OVERRUN:
1125                                 printk(KERN_WARNING "cciss: cmd %p has"
1126                                         " completed with data overrun "
1127                                         "reported\n", cmd);
1128                         break;
1129                         case CMD_INVALID:
1130                                 printk(KERN_WARNING "cciss: cmd %p is "
1131                                         "reported invalid\n", cmd);
1132                                 status = 0;
1133                         break;
1134                         case CMD_PROTOCOL_ERR:
1135                                 printk(KERN_WARNING "cciss: cmd %p has "
1136                                         "protocol error \n", cmd);
1137                                 status = 0;
1138                         break;
1139                         case CMD_HARDWARE_ERR:
1140                                 printk(KERN_WARNING "cciss: cmd %p had " 
1141                                         " hardware error\n", cmd);
1142                                 status = 0;
1143                         break;
1144                         case CMD_CONNECTION_LOST:
1145                                 printk(KERN_WARNING "cciss: cmd %p had "
1146                                         "connection lost\n", cmd);
1147                                 status=0;
1148                         break;
1149                         case CMD_ABORTED:
1150                                 printk(KERN_WARNING "cciss: cmd %p was "
1151                                         "aborted\n", cmd);
1152                                 status=0;
1153                         break;
1154                         case CMD_ABORT_FAILED:
1155                                 printk(KERN_WARNING "cciss: cmd %p reports "
1156                                         "abort failed\n", cmd);
1157                                 status=0;
1158                         break;
1159                         case CMD_UNSOLICITED_ABORT:
1160                                 printk(KERN_WARNING "cciss: cmd %p aborted "
1161                                         "do to an unsolicited abort\n", cmd);
1162                                 status=0;
1163                         break;
1164                         case CMD_TIMEOUT:
1165                                 printk(KERN_WARNING "cciss: cmd %p timedout\n",
1166                                         cmd);
1167                                 status=0;
1168                         break;
1169                         default:
1170                                 printk(KERN_WARNING "cciss: cmd %p returned "
1171                                         "unknown status %x\n", cmd, 
1172                                                 cmd->err_info->CommandStatus); 
1173                                 status=0;
1174                 }
1175         }
1176         complete_buffers(cmd->bh, status);
1177 }
1178 /* 
1179  * Get a request and submit it to the controller. 
1180  * Currently we do one request at a time.  Ideally we would like to send
1181  * everything to the controller on the first call, but there is a danger
1182  * of holding the io_request_lock for to long.  
1183  */
1184 static void do_cciss_request(int ctlr)
1185 {
1186         ctlr_info_t *h= hba[ctlr];
1187         CommandList_struct *c;
1188         int log_unit, start_blk, seg, sect;
1189         char *lastdataend;
1190         struct buffer_head *bh;
1191         struct list_head *queue_head;
1192         struct request *creq;
1193         u64bit temp64;
1194 
1195         queue_head = &blk_dev[MAJOR_NR+ctlr].request_queue.queue_head;  
1196         if (list_empty(queue_head))
1197         {
1198                 /* nothing to do... */
1199                 start_io(h);
1200                 return;
1201         }
1202         creq =  blkdev_entry_next_request(queue_head); 
1203         if ((creq == NULL) || (creq->rq_status == RQ_INACTIVE))
1204         {
1205                 /* nothing to do... restart processing and return */ 
1206                 start_io(h);
1207                 return;
1208         }
1209         if ((ctlr != (MAJOR(creq->rq_dev)-MAJOR_NR)) || (ctlr > nr_ctlr)
1210                 || (h == NULL))
1211         {
1212 #ifdef CCISS_DEBUG
1213                 printk(KERN_WARNING "cciss: doreq cmd of %d, %x at %p\n",
1214                         ctlr, creq->rq_dev, creq);
1215 #endif /* CCISS_DEBUG */
1216                 complete_buffers(creq->bh, 0);
1217                 start_io(h); 
1218                 return;
1219         }
1220         if (( c = cmd_alloc(h)) == NULL)
1221         {
1222                 start_io(h);
1223                 return;
1224         }
1225         c->cmd_type = CMD_RWREQ;      
1226         bh = c->bh = creq->bh;
1227         
1228         /* fill in the request */ 
1229         log_unit = MINOR(creq->rq_dev) >> NWD_SHIFT; 
1230         c->Header.ReplyQueue = 0;  // unused in simple mode
1231         c->Header.Tag.lower = c->busaddr;  // use the physical address the cmd block for tag
1232         c->Header.LUN.LogDev.VolId= hba[ctlr]->drv[log_unit].LunID;
1233         c->Header.LUN.LogDev.Mode = 1;
1234         c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
1235         c->Request.Type.Type =  TYPE_CMD; // It is a command. 
1236         c->Request.Type.Attribute = ATTR_SIMPLE; 
1237         c->Request.Type.Direction = 
1238                 (creq->cmd == READ) ? XFER_READ: XFER_WRITE; 
1239         c->Request.Timeout = 0; // Don't time out       
1240         c->Request.CDB[0] = (creq->cmd == READ) ? CCISS_READ : CCISS_WRITE;
1241         start_blk = hba[ctlr]->hd[MINOR(creq->rq_dev)].start_sect + creq->sector;
1242         if (bh == NULL)
1243                 panic("cciss: bh== NULL?");
1244 #ifdef CCISS_DEBUG
1245         printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
1246                 (int) creq->nr_sectors);        
1247 #endif /* CCISS_DEBUG */
1248         seg = 0; 
1249         lastdataend = NULL;
1250         sect = 0;
1251         while(bh)
1252         {
1253                 sect += bh->b_size/512;
1254                 if (bh->b_size % 512)
1255                 {
1256                         printk(KERN_CRIT "cciss:  Oh Man.  %d+%d, size=%d\n", 
1257                                 (int) creq->sector, sect, (int) bh->b_size);
1258                         panic("b_size 512 != 0\n");
1259                 }
1260                 if (bh->b_data == lastdataend)
1261                 {  // tack it on to the last segment 
1262                         c->SG[seg-1].Len +=bh->b_size;
1263                         lastdataend += bh->b_size;
1264                 } else
1265                 {
1266                         c->SG[seg].Len = bh->b_size;
1267                         temp64.val = (__u64) virt_to_bus(bh->b_data);
1268                         c->SG[seg].Addr.lower = temp64.val32.lower;
1269                         c->SG[seg].Addr.upper = temp64.val32.upper;
1270                         c->SG[0].Ext = 0;  // we are not chaining
1271                         lastdataend = bh->b_data + bh->b_size;
1272                         if( ++seg == MAXSGENTRIES)
1273                         {
1274                                 break; 
1275                         }
1276                 }
1277                 bh = bh->b_reqnext;
1278         }
1279         /* track how many SG entries we are using */ 
1280         if( seg > h->maxSG)
1281                 h->maxSG = seg; 
1282 
1283         /* adjusting the remaining request, if any */ 
1284         creq-> sector+= sect;
1285         creq->nr_sectors -= sect; 
1286 
1287 #ifdef CCISS_DEBUG
1288         printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", sect, seg);
1289 #endif /* CCISS_DEBUG */
1290 
1291         c->Header.SGList = c->Header.SGTotal = seg;
1292         c->Request.CDB[1]= 0;
1293         c->Request.CDB[2]= (start_blk >> 24) & 0xff;    //MSB
1294         c->Request.CDB[3]= (start_blk >> 16) & 0xff;
1295         c->Request.CDB[4]= (start_blk >>  8) & 0xff;
1296         c->Request.CDB[5]= start_blk & 0xff;
1297         c->Request.CDB[6]= 0; // (sect >> 24) & 0xff; MSB
1298         // c->Request.CDB[7]= (sect >> 16) & 0xff; 
1299         c->Request.CDB[7]= (sect >>  8) & 0xff; 
1300         c->Request.CDB[8]= sect & 0xff; 
1301         c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
1302 
1303         /* check to see if we going to complete the entire request */ 
1304         /* if so, mark this request as Done and ready the next one */ 
1305         if (creq->nr_sectors)
1306         {
1307 #ifdef CCISS_DEBUG
1308                 printk(KERN_DEBUG "cciss: More to do on the same request %p %ld\n", 
1309                         creq, creq->nr_sectors);
1310 #endif /* CCISS_DEBUG */
1311 
1312                 creq->bh = bh->b_reqnext;
1313                 bh->b_reqnext = NULL;
1314         } else
1315         {
1316 #ifdef CCISS_DEBUG
1317                 printk("cciss: Done with %p, queueing %p\n", creq);
1318 #endif /* CCISS_DEBUG */
1319                         
1320                 blkdev_dequeue_request(creq);
1321                 end_that_request_last(creq);
1322         }
1323         addQ(&(h->reqQ),c);
1324         h->Qdepth++;
1325         if(h->Qdepth > h->maxQsinceinit)
1326                 h->maxQsinceinit = h->Qdepth; 
1327         start_io(h);
1328 }
1329 
1330 static void do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
1331 {
1332         ctlr_info_t *h = dev_id;
1333         CommandList_struct *c;
1334         unsigned long flags;
1335         __u32 a, a1;
1336 
1337 
1338         /* Is this interrupt for us? */
1339         if ( h->access.intr_pending(h) == 0)
1340                 return;
1341 
1342         /*
1343          * If there are completed commands in the completion queue,
1344          * we had better do something about it.
1345          */
1346         spin_lock_irqsave(&io_request_lock, flags);
1347         while( h->access.intr_pending(h))
1348         {
1349                 while((a = h->access.command_completed(h)) != FIFO_EMPTY) 
1350                 {
1351                         a1 = a;
1352                         a &= ~3;
1353                         if ((c = h->cmpQ) == NULL)
1354                         {  
1355                                 printk(KERN_WARNING "cpqarray: Completion of %08lx ignored\n", (unsigned long)a1);
1356                                 continue;       
1357                         } 
1358                         while(c->busaddr != a) {
1359                                 c = c->next;
1360                                 if (c == h->cmpQ) 
1361                                         break;
1362                         }
1363                         /*
1364                          * If we've found the command, take it off the
1365                          * completion Q and free it
1366                          */
1367                          if (c->busaddr == a) {
1368                                 removeQ(&h->cmpQ, c);
1369                                 if (c->cmd_type == CMD_RWREQ) {
1370                                         complete_command(c, 0);
1371                                         cmd_free(h, c);
1372                                 } else if (c->cmd_type == CMD_IOCTL_PEND) {
1373                                         c->cmd_type = CMD_IOCTL_DONE;
1374                                 }
1375                                 continue;
1376                         }
1377                 }
1378         }
1379         /*
1380          * See if we can queue up some more IO
1381          */
1382         do_cciss_request(h->ctlr);
1383         spin_unlock_irqrestore(&io_request_lock, flags);
1384 }
1385 /* 
1386  *  We cannot read the structure directly, for portablity we must use 
1387  *   the io functions.
1388  *   This is for debug only. 
1389  */
1390 #ifdef CCISS_DEBUG
1391 static void print_cfg_table( CfgTable_struct *tb)
1392 {
1393         int i;
1394         char temp_name[17];
1395 
1396         printk("Controller Configuration information\n");
1397         printk("------------------------------------\n");
1398         for(i=0;i<4;i++)
1399                 temp_name[i] = readb(&(tb->Signature[i]));
1400         temp_name[4]='\0';
1401         printk("   Signature = %s\n", temp_name); 
1402         printk("   Spec Number = %d\n", readl(&(tb->SpecValence)));
1403         printk("   Transport methods supported = 0x%x\n", 
1404                                 readl(&(tb-> TransportSupport)));
1405         printk("   Transport methods active = 0x%x\n", 
1406                                 readl(&(tb->TransportActive)));
1407         printk("   Requested transport Method = 0x%x\n", 
1408                         readl(&(tb->HostWrite.TransportRequest)));
1409         printk("   Coalese Interrupt Delay = 0x%x\n", 
1410                         readl(&(tb->HostWrite.CoalIntDelay)));
1411         printk("   Coalese Interrupt Count = 0x%x\n", 
1412                         readl(&(tb->HostWrite.CoalIntCount)));
1413         printk("   Max outstanding commands = 0x%d\n", 
1414                         readl(&(tb->CmdsOutMax)));
1415         printk("   Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
1416         for(i=0;i<16;i++)
1417                 temp_name[i] = readb(&(tb->ServerName[i]));
1418         temp_name[16] = '\0';
1419         printk("   Server Name = %s\n", temp_name);
1420         printk("   Heartbeat Counter = 0x%x\n\n\n", 
1421                         readl(&(tb->HeartBeat)));
1422 }
1423 #endif /* CCISS_DEBUG */
1424 
1425 static int cciss_pci_init(ctlr_info_t *c, unchar bus, unchar device_fn)
1426 {
1427         ushort vendor_id, device_id, command;
1428         unchar cache_line_size, latency_timer;
1429         unchar irq, revision;
1430         uint addr[6];
1431         __u32 board_id;
1432         struct pci_dev *pdev;
1433 
1434         int i;
1435 
1436         pdev = pci_find_slot(bus, device_fn);
1437         vendor_id = pdev->vendor;
1438         device_id = pdev->device;
1439         irq = pdev->irq;
1440 
1441         for(i=0; i<6; i++)
1442                 addr[i] = pdev->resource[i].start;
1443 
1444         if (pci_enable_device(pdev))
1445                 return( -1);
1446         
1447         (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
1448         (void) pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1449         (void) pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE,
1450                                                 &cache_line_size);
1451         (void) pci_read_config_byte(pdev, PCI_LATENCY_TIMER,
1452                                                 &latency_timer);
1453 
1454         (void) pci_read_config_dword(pdev, PCI_SUBSYSTEM_VENDOR_ID, 
1455                                                 &board_id);
1456 
1457 #ifdef CCISS_DEBUG
1458         printk("vendor_id = %x\n", vendor_id);
1459         printk("device_id = %x\n", device_id);
1460         printk("command = %x\n", command);
1461         for(i=0; i<6; i++)
1462                 printk("addr[%d] = %x\n", i, addr[i]);
1463         printk("revision = %x\n", revision);
1464         printk("irq = %x\n", irq);
1465         printk("cache_line_size = %x\n", cache_line_size);
1466         printk("latency_timer = %x\n", latency_timer);
1467         printk("board_id = %x\n", board_id);
1468 #endif /* CCISS_DEBUG */ 
1469 
1470         c->intr = irq;
1471 
1472         /*
1473          * Memory base addr is first addr , the second points to the config
1474          *   table
1475          */
1476         c->paddr = pci_resource_start(pdev, 0);
1477         c->vaddr = remap_pci_mem(c->paddr, 128);
1478         c->cfgtable = (CfgTable_struct *) remap_pci_mem(addr[1], 
1479                                                 sizeof(CfgTable_struct));
1480         c->board_id = board_id;
1481 
1482 #ifdef CCISS_DEBUG
1483         print_cfg_table(c->cfgtable); 
1484 #endif /* CCISS_DEBUG */
1485         for(i=0; i<NR_PRODUCTS; i++) {
1486                 if (board_id == products[i].board_id) {
1487                         c->product_name = products[i].product_name;
1488                         c->access = *(products[i].access);
1489                         break;
1490                 }
1491         }
1492         if (i == NR_PRODUCTS) {
1493                 printk(KERN_WARNING "cciss: Sorry, I don't know how"
1494                         " to access the Smart Array controller %08lx\n", 
1495                                 (unsigned long)board_id);
1496                 return -1;
1497         }
1498 #ifdef CCISS_DEBUG
1499         printk("Trying to put board into Simple mode\n");
1500 #endif /* CCISS_DEBUG */ 
1501         c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
1502         /* Update the field, and then ring the doorbell */ 
1503         writel( CFGTBL_Trans_Simple, 
1504                 &(c->cfgtable->HostWrite.TransportRequest));
1505         writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
1506 
1507         for(i=0;i<MAX_CONFIG_WAIT;i++)
1508         {
1509                 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
1510                         break;
1511                 /* delay and try again */
1512                 udelay(1000);
1513         }       
1514 
1515 #ifdef CCISS_DEBUG
1516         printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
1517 #endif /* CCISS_DEBUG */
1518 #ifdef CCISS_DEBUG
1519         print_cfg_table(c->cfgtable);   
1520 #endif /* CCISS_DEBUG */ 
1521 
1522         if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
1523         {
1524                 printk(KERN_WARNING "cciss: unable to get board into"
1525                                         " simple mode\n");
1526                 return -1;
1527         }
1528         return 0;
1529 
1530 }
1531 /* 
1532  * Scans PCI space for any controllers that this driver can control. 
1533  */
1534 static int cciss_pci_detect(void)
1535 {
1536 
1537         int index;
1538         unchar bus=0, dev_fn=0;
1539         
1540                 for(index=0; ; index++) {
1541                         if (pcibios_find_device(PCI_VENDOR_ID_COMPAQ,
1542                                 PCI_DEVICE_ID_COMPAQ_CISS, 
1543                                         index, &bus, &dev_fn))
1544                                 break;
1545                         printk(KERN_DEBUG "cciss: Device %x has been found at %x %x\n",
1546                                 PCI_DEVICE_ID_COMPAQ_CISS, bus, dev_fn);
1547                         if (index == 1000000) break;
1548                         if (nr_ctlr == 8) {
1549                                 printk(KERN_WARNING "cciss: This driver"
1550                                 " supports a maximum of 8 controllers.\n");
1551                                 break;
1552                         }
1553                         hba[nr_ctlr] = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
1554                         if(hba[nr_ctlr]==NULL)
1555                         {
1556                                 printk(KERN_ERR "cciss: out of memory.\n");
1557                                 continue;
1558                         }
1559                         memset(hba[nr_ctlr], 0, sizeof(ctlr_info_t));
1560                         if (cciss_pci_init(hba[nr_ctlr], bus, dev_fn) != 0)
1561                         {
1562                                 kfree(hba[nr_ctlr]);
1563                                 continue;
1564                         }
1565                         sprintf(hba[nr_ctlr]->devname, "cciss%d", nr_ctlr);
1566                         hba[nr_ctlr]->ctlr = nr_ctlr;
1567                         hba[nr_ctlr]->pci_bus = bus;
1568                         hba[nr_ctlr]->pci_dev_fn = dev_fn;
1569                         nr_ctlr++;
1570 
1571                 }
1572         return nr_ctlr;
1573 
1574 }
1575 
1576 /* 
1577  * Gets information about the local volumes attached to the controller. 
1578  */ 
1579 static void cciss_getgeometry(int cntl_num)
1580 {
1581         ReportLunData_struct *ld_buff;
1582         ReadCapdata_struct *size_buff;
1583         InquiryData_struct *inq_buff;
1584         int return_code;
1585         int i;
1586         int listlength = 0;
1587         int lunid = 0;
1588         int block_size;
1589         int total_size; 
1590 
1591         ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1592         if (ld_buff == NULL)
1593         {
1594                 printk(KERN_ERR "cciss: out of memory\n");
1595                 return;
1596         }
1597         memset(ld_buff, 0, sizeof(ReportLunData_struct));
1598         size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1599         if (size_buff == NULL)
1600         {
1601                 printk(KERN_ERR "cciss: out of memory\n");
1602                 kfree(ld_buff);
1603                 return;
1604         }
1605         inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1606         if (inq_buff == NULL)
1607         {
1608                 printk(KERN_ERR "cciss: out of memory\n");
1609                 kfree(ld_buff);
1610                 kfree(size_buff);
1611                 return;
1612         }
1613         /* Get the firmware version */ 
1614         return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff, 
1615                 sizeof(InquiryData_struct), 0, 0 ,0 );
1616         if (return_code == IO_OK)
1617         {
1618                 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
1619                 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
1620                 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
1621                 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
1622         } else /* send command failed */
1623         {
1624                 printk(KERN_WARNING "cciss: unable to determine firmware"
1625                         " version of controller\n");
1626         }
1627         /* Get the number of logical volumes */ 
1628         return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff, 
1629                         sizeof(ReportLunData_struct), 0, 0, 0 );
1630 
1631         if( return_code == IO_OK)
1632         {
1633 #ifdef CCISS_DEBUG
1634                 printk("LUN Data\n--------------------------\n");
1635 #endif /* CCISS_DEBUG */ 
1636 
1637                 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
1638                 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
1639                 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;  
1640                 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
1641         } else /* reading number of logical volumes failed */
1642         {
1643                 printk(KERN_WARNING "cciss: report logical volume"
1644                         " command failed\n");
1645                 listlength = 0;
1646         }
1647         hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
1648         if (hba[cntl_num]->num_luns > CISS_MAX_LUN)
1649         {
1650                 printk(KERN_ERR "ciss:  only %d number of logical volumes supported\n",
1651                         CISS_MAX_LUN);
1652                 hba[cntl_num]->num_luns = CISS_MAX_LUN;
1653         }
1654 #ifdef CCISS_DEBUG
1655         printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
1656                 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
1657                 ld_buff->LUNListLength[3],  hba[cntl_num]->num_luns);
1658 #endif /* CCISS_DEBUG */
1659         for(i=0; i<  hba[cntl_num]->num_luns ; i++)
1660         {
1661                 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
1662                 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
1663                 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
1664                 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1665                 hba[cntl_num]->drv[i].LunID = lunid;
1666 
1667 #ifdef CCISS_DEBUG
1668                 printk(KERN_DEBUG "LUN[%d]:  %x %x %x %x = %x\n", i, 
1669                 ld_buff->LUN[i][0], ld_buff->LUN[i][1],ld_buff->LUN[i][2], 
1670                 ld_buff->LUN[i][3], hba[cntl_num]->drv[i].LunID);
1671 #endif /* CCISS_DEBUG */
1672 
1673                 memset(size_buff, 0, sizeof(ReadCapdata_struct));
1674                 return_code = sendcmd(CCISS_READ_CAPACITY, cntl_num, size_buff, 
1675                                 sizeof( ReadCapdata_struct), 1, i, 0 );
1676                 if (return_code == IO_OK)
1677                 {
1678                         total_size = (0xff & 
1679                                 (unsigned int)(size_buff->total_size[0])) << 24;
1680                         total_size |= (0xff & 
1681                                 (unsigned int)(size_buff->total_size[1])) << 16;
1682                         total_size |= (0xff & 
1683                                 (unsigned int)(size_buff->total_size[2])) << 8;
1684                         total_size |= (0xff & (unsigned int)
1685                                 (size_buff->total_size[3])); 
1686                         total_size++; // command returns highest block address
1687 
1688                         block_size = (0xff & 
1689                                 (unsigned int)(size_buff->block_size[0])) << 24;
1690                         block_size |= (0xff & 
1691                                 (unsigned int)(size_buff->block_size[1])) << 16;
1692                         block_size |= (0xff & 
1693                                 (unsigned int)(size_buff->block_size[2])) << 8;
1694                         block_size |= (0xff & 
1695                                 (unsigned int)(size_buff->block_size[3]));
1696                 } else /* read capacity command failed */ 
1697                 {
1698                         printk(KERN_WARNING "cciss: read capacity failed\n");
1699                         total_size = block_size = 0; 
1700                 }       
1701                 printk("      blocks= %d block_size= %d\n", total_size,
1702                                         block_size);
1703 
1704                 /* Execute the command to read the disk geometry */
1705                 memset(inq_buff, 0, sizeof(InquiryData_struct));
1706                 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
1707                         sizeof(InquiryData_struct), 1, i ,0xC1 );
1708                 if (return_code == IO_OK)
1709                 {
1710                         if(inq_buff->data_byte[8] == 0xFF)
1711                         {
1712                            printk(KERN_WARNING "cciss: reading geometry failed, volume does not support reading geometry\n");
1713 
1714                            hba[cntl_num]->drv[i].block_size = block_size;
1715                            hba[cntl_num]->drv[i].nr_blocks = total_size;
1716                            hba[cntl_num]->drv[i].heads = 255;
1717                            hba[cntl_num]->drv[i].sectors = 32; // Sectors per track
1718                            hba[cntl_num]->drv[i].cylinders = total_size / 255 / 32;                     } else
1719                         {
1720 
1721                            hba[cntl_num]->drv[i].block_size = block_size;
1722                            hba[cntl_num]->drv[i].nr_blocks = total_size;
1723                            hba[cntl_num]->drv[i].heads = 
1724                                         inq_buff->data_byte[6]; 
1725                            hba[cntl_num]->drv[i].sectors = 
1726                                         inq_buff->data_byte[7]; 
1727                            hba[cntl_num]->drv[i].cylinders = 
1728                                         (inq_buff->data_byte[4] & 0xff) << 8;
1729                            hba[cntl_num]->drv[i].cylinders += 
1730                                         inq_buff->data_byte[5];
1731                         }
1732                 }
1733                 else /* Get geometry failed */
1734                 {
1735                         printk(KERN_WARNING "cciss: reading geometry failed, continuing with default geometry\n"); 
1736 
1737                         hba[cntl_num]->drv[i].block_size = block_size;
1738                         hba[cntl_num]->drv[i].nr_blocks = total_size;
1739                         hba[cntl_num]->drv[i].heads = 255;
1740                         hba[cntl_num]->drv[i].sectors = 32; // Sectors per track 
1741                         hba[cntl_num]->drv[i].cylinders = total_size / 255 / 32;
1742                 }
1743                 printk(KERN_INFO "      heads= %d, sectors= %d, cylinders= %d\n\n",
1744                         hba[cntl_num]->drv[i].heads, 
1745                         hba[cntl_num]->drv[i].sectors,
1746                         hba[cntl_num]->drv[i].cylinders);
1747 
1748         }
1749         kfree(ld_buff);
1750         kfree(size_buff);
1751 }       
1752 
1753 /*
1754  *  This is it.  Find all the controllers and register them.  I really hate
1755  *  stealing all these major device numbers.
1756  *  returns the number of block devices registered.
1757  */
1758 int __init cciss_init(void)
1759 {
1760         int num_cntlrs_reg = 0;
1761         int i,j;
1762 
1763         void (*request_fns[MAX_CTLR])(request_queue_t *) = {
1764                 do_cciss_request0, do_cciss_request1,
1765                 do_cciss_request2, do_cciss_request3,
1766                 do_cciss_request4, do_cciss_request5,
1767                 do_cciss_request6, do_cciss_request7,
1768         };
1769 
1770         /* detect controllers */
1771         cciss_pci_detect();
1772 
1773         if (nr_ctlr == 0)
1774                 return(num_cntlrs_reg);
1775 
1776         printk(KERN_INFO DRIVER_NAME "\n");
1777         printk(KERN_INFO "Found %d controller(s)\n", nr_ctlr);
1778         for(i=0;i<nr_ctlr;i++)
1779         {
1780                 if( register_blkdev(MAJOR_NR+i, hba[i]->devname, &cciss_fops))
1781                 {
1782                         printk(KERN_ERR "cciss:  Unable to get major number "
1783                                 "%d for %s\n", MAJOR_NR+i, hba[i]->devname);
1784                         continue;
1785                 }
1786                 /* make sure the board interrupts are off */
1787                 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
1788                 if( request_irq(hba[i]->intr, do_cciss_intr, SA_INTERRUPT|SA_SHIRQ, hba[i]->devname, hba[i]))
1789                 {
1790                         printk(KERN_ERR "ciss: Unable to get irq %d for %s\n",
1791                                 hba[i]->intr, hba[i]->devname);
1792                         unregister_blkdev( MAJOR_NR+i, hba[i]->devname);
1793                         continue;
1794                 }
1795                 num_cntlrs_reg++;
1796                 hba[i]->cmd_pool_bits = (__u32*)kmalloc(
1797                                 ((NR_CMDS+31)/32)*sizeof(__u32), GFP_KERNEL);
1798                 hba[i]->cmd_pool = (CommandList_struct *)kmalloc(
1799                                 NR_CMDS * sizeof(CommandList_struct), 
1800                                         GFP_KERNEL);
1801                 hba[i]->errinfo_pool = (ErrorInfo_struct *)kmalloc(
1802                                 NR_CMDS * sizeof( ErrorInfo_struct), 
1803                                         GFP_KERNEL);
1804                 if((hba[i]->cmd_pool_bits == NULL) 
1805                         || (hba[i]->cmd_pool == NULL)
1806                         || (hba[i]->errinfo_pool == NULL))
1807                 {
1808                         nr_ctlr = i;
1809                         if(hba[i]->cmd_pool_bits)
1810                                 kfree(hba[i]->cmd_pool_bits);
1811                         if(hba[i]->cmd_pool)
1812                                 kfree(hba[i]->cmd_pool);
1813                         if(hba[i]->errinfo_pool)
1814                                 kfree(hba[i]->errinfo_pool);
1815                         free_irq(hba[i]->intr, hba[i]);
1816                         unregister_blkdev(MAJOR_NR+i, hba[i]->devname);
1817                         num_cntlrs_reg--;
1818                         printk( KERN_ERR "cciss: out of memory");
1819                         return(num_cntlrs_reg);
1820                 }
1821 
1822                 /* command and error info recs zeroed out before 
1823                         they are used */
1824                 memset(hba[i]->cmd_pool_bits, 0, 
1825                         ((NR_CMDS+31)/32)*sizeof(__u32));
1826 
1827 #ifdef CCISS_DEBUG      
1828                 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
1829 #endif /* CCISS_DEBUG */
1830 
1831                 cciss_getgeometry(i);
1832 
1833                 /* Turn the interrupts on so we can service requests */
1834                 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
1835 
1836                 cciss_procinit(i);
1837                 
1838                 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR+i),
1839                                 request_fns[i]);
1840                 blk_queue_headactive(BLK_DEFAULT_QUEUE(MAJOR_NR+i), 0);
1841 
1842                 /* fill in the other Kernel structs */
1843                 blksize_size[MAJOR_NR+i] = hba[i]->blocksizes;
1844                 hardsect_size[MAJOR_NR+i] = hba[i]->hardsizes;
1845                 read_ahead[MAJOR_NR+i] = READ_AHEAD;
1846 
1847                 /* Fill in the gendisk data */  
1848                 hba[i]->gendisk.major = MAJOR_NR + i;
1849                 hba[i]->gendisk.major_name = "cciss";
1850                 hba[i]->gendisk.minor_shift = NWD_SHIFT;
1851                 hba[i]->gendisk.max_p = MAX_PART;
1852                 hba[i]->gendisk.part = hba[i]->hd;
1853                 hba[i]->gendisk.sizes = hba[i]->sizes;
1854                 hba[i]->gendisk.nr_real = hba[i]->num_luns;
1855 
1856                 /* Get on the disk list */ 
1857                 hba[i]->gendisk.next = gendisk_head;
1858                 gendisk_head = &(hba[i]->gendisk); 
1859 
1860                 cciss_geninit(i);
1861                 for(j=0; j<NWD; j++)
1862                         register_disk(&(hba[i]->gendisk),
1863                                 MKDEV(MAJOR_NR+i, j <<4), 
1864                                 MAX_PART, &cciss_fops, 
1865                                 hba[i]->drv[j].nr_blocks);
1866         }
1867         return(nr_ctlr);
1868 }
1869 
1870 EXPORT_NO_SYMBOLS;
1871 
1872 /* This is a bit of a hack... */
1873 static int __init init_cciss_module(void)
1874 {
1875 
1876         if (cciss_init() == 0) /* all the block dev numbers already used */
1877                 return -EIO;      /* or no controllers were found */
1878         return 0;
1879 }
1880 
1881 static void __exit cleanup_cciss_module(void)
1882 {
1883         int i;
1884         struct gendisk *g;
1885 
1886         for(i=0; i<nr_ctlr; i++)
1887         {
1888                 /* Turn board interrupts off */ 
1889                 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
1890                 free_irq(hba[i]->intr, hba[i]);
1891                 iounmap((void*)hba[i]->vaddr);
1892                 unregister_blkdev(MAJOR_NR+i, hba[i]->devname);
1893                 remove_proc_entry(hba[i]->devname, proc_cciss); 
1894 
1895                 /* remove it from the disk list */ 
1896                 if (gendisk_head == &(hba[i]->gendisk))
1897                 {
1898                         gendisk_head = hba[i]->gendisk.next;
1899                 } else 
1900                 {
1901                         for(g=gendisk_head; g ; g=g->next)
1902                         {
1903                                 if(g->next == &(hba[i]->gendisk))
1904                                 {
1905                                         g->next = hba[i]->gendisk.next;
1906                                 }
1907                         }
1908                 }
1909                 remove_proc_entry("driver/cciss", &proc_root);
1910                 kfree(hba[i]->cmd_pool);
1911                 kfree(hba[i]->errinfo_pool);
1912                 kfree(hba[i]->cmd_pool_bits);
1913                 kfree(hba[i]);
1914         }
1915 }
1916 
1917 module_init(init_cciss_module);
1918 module_exit(cleanup_cciss_module);
1919 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.