2 * Disk Array driver for HP SA 5xxx and 6xxx Controllers
3 * Copyright 2000, 2002 Hewlett-Packard Development Company, L.P.
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.
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.
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.
19 * Questions/Comments/Bugfixes to Cciss-discuss@lists.sourceforge.net
23 #include <linux/config.h> /* CONFIG_PROC_FS */
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/types.h>
27 #include <linux/pci.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/major.h>
33 #include <linux/bio.h>
34 #include <linux/blkpg.h>
35 #include <linux/timer.h>
36 #include <linux/proc_fs.h>
37 #include <linux/init.h>
38 #include <linux/hdreg.h>
39 #include <linux/spinlock.h>
40 #include <asm/uaccess.h>
43 #include <linux/blkdev.h>
44 #include <linux/genhd.h>
45 #include <linux/completion.h>
47 #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
48 #define DRIVER_NAME "Compaq CISS Driver (v 2.6.2)"
49 #define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,2)
51 /* Embedded module documentation macros - see modules.h */
52 MODULE_AUTHOR("Hewlett-Packard Company");
53 MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 2.6.2");
54 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
56 MODULE_LICENSE("GPL");
58 #include "cciss_cmd.h"
60 #include <linux/cciss_ioctl.h>
62 /* define the PCI info for the cards we can control */
63 const struct pci_device_id cciss_pci_device_id[] = {
64 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS,
65 0x0E11, 0x4070, 0, 0, 0},
66 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
67 0x0E11, 0x4080, 0, 0, 0},
68 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
69 0x0E11, 0x4082, 0, 0, 0},
70 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
71 0x0E11, 0x4083, 0, 0, 0},
72 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
73 0x0E11, 0x409A, 0, 0, 0},
74 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
75 0x0E11, 0x409B, 0, 0, 0},
76 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
77 0x0E11, 0x409C, 0, 0, 0},
78 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
79 0x0E11, 0x409D, 0, 0, 0},
80 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
81 0x0E11, 0x4091, 0, 0, 0},
82 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
83 0x0E11, 0x409E, 0, 0, 0},
84 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
85 0x103C, 0x3211, 0, 0, 0},
88 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
90 #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
92 /* board_id = Subsystem Device ID & Vendor ID
93 * product = Marketing Name for the board
94 * access = Address of the struct of function pointers
96 static struct board_type products[] = {
97 { 0x40700E11, "Smart Array 5300", &SA5_access },
98 { 0x40800E11, "Smart Array 5i", &SA5B_access},
99 { 0x40820E11, "Smart Array 532", &SA5B_access},
100 { 0x40830E11, "Smart Array 5312", &SA5B_access},
101 { 0x409A0E11, "Smart Array 641", &SA5_access},
102 { 0x409B0E11, "Smart Array 642", &SA5_access},
103 { 0x409C0E11, "Smart Array 6400", &SA5_access},
104 { 0x409D0E11, "Smart Array 6400 EM", &SA5_access},
105 { 0x40910E11, "Smart Array 6i", &SA5_access},
106 { 0x409E0E11, "Smart Array 6422", &SA5_access},
107 { 0x3211103C, "Smart Array V100", &SA5_access},
110 /* How long to wait (in millesconds) for board to go into simple mode */
111 #define MAX_CONFIG_WAIT 30000
112 #define MAX_IOCTL_CONFIG_WAIT 1000
114 /*define how many times we will try a command because of bus resets */
115 #define MAX_CMD_RETRIES 3
117 #define READ_AHEAD 256
118 #define NR_CMDS 384 /* #commands that can be outstanding */
121 #define CCISS_DMA_MASK 0xFFFFFFFF /* 32 bit DMA */
123 static ctlr_info_t *hba[MAX_CTLR];
125 static void do_cciss_request(request_queue_t *q);
126 static int cciss_open(struct inode *inode, struct file *filep);
127 static int cciss_release(struct inode *inode, struct file *filep);
128 static int cciss_ioctl(struct inode *inode, struct file *filep,
129 unsigned int cmd, unsigned long arg);
131 static int revalidate_allvol(ctlr_info_t *host);
132 static int cciss_revalidate(struct gendisk *disk);
133 static int deregister_disk(struct gendisk *disk);
134 static int register_new_disk(ctlr_info_t *h);
136 static void cciss_getgeometry(int cntl_num);
138 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c);
139 static void start_io( ctlr_info_t *h);
140 static int sendcmd( __u8 cmd, int ctlr, void *buff, size_t size,
141 unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
142 unsigned char *scsi3addr, int cmd_type);
144 #ifdef CONFIG_PROC_FS
145 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
146 int length, int *eof, void *data);
147 static void cciss_procinit(int i);
149 static void cciss_procinit(int i) {}
150 #endif /* CONFIG_PROC_FS */
152 static struct block_device_operations cciss_fops = {
153 .owner = THIS_MODULE,
155 .release = cciss_release,
156 .ioctl = cciss_ioctl,
157 .revalidate_disk= cciss_revalidate,
160 #include "cciss_scsi.c" /* For SCSI tape support */
163 * Report information about this controller.
165 #define ENG_GIG 1048576000
166 #define ENG_GIG_FACTOR (ENG_GIG/512)
167 #define RAID_UNKNOWN 6
168 static const char *raid_label[] = {"0","4","1(0+1)","5","5+1","ADG",
170 #ifdef CONFIG_PROC_FS
172 static struct proc_dir_entry *proc_cciss;
174 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
175 int length, int *eof, void *data)
180 ctlr_info_t *h = (ctlr_info_t*)data;
181 drive_info_struct *drv;
183 unsigned int vol_sz, vol_sz_frac;
187 /* prevent displaying bogus info during configuration
188 * or deconfiguration of a logical volume
190 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
191 if (h->busy_configuring) {
192 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
195 h->busy_configuring = 1;
196 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
198 size = sprintf(buffer, "%s: HP %s Controller\n"
199 "Board ID: 0x%08lx\n"
200 "Firmware Version: %c%c%c%c\n"
202 "Logical drives: %d\n"
203 "Current Q depth: %d\n"
204 "Current # commands on controller: %d\n"
205 "Max Q depth since init: %d\n"
206 "Max # commands on controller since init: %d\n"
207 "Max SG entries since init: %d\n\n",
210 (unsigned long)h->board_id,
211 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
212 (unsigned int)h->intr,
214 h->Qdepth, h->commands_outstanding,
215 h->maxQsinceinit, h->max_outstanding, h->maxSG);
217 pos += size; len += size;
218 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
219 for(i=0; i<=h->highest_lun; i++) {
223 if (drv->block_size == 0)
225 vol_sz = drv->nr_blocks;
226 sector_div(vol_sz, ENG_GIG_FACTOR);
231 * (drv->nr_blocks%ENG_GIG_FACTOR)*100/ENG_GIG_FACTOR;
233 tmp = drv->nr_blocks;
234 vol_sz_frac = sector_div(tmp, ENG_GIG_FACTOR);
236 /* Now, vol_sz_frac = (drv->nr_blocks%ENG_GIG_FACTOR) */
239 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
241 if (drv->raid_level > 5)
242 drv->raid_level = RAID_UNKNOWN;
243 size = sprintf(buffer+len, "cciss/c%dd%d:"
244 "\t%4d.%02dGB\tRAID %s\n",
245 ctlr, i, vol_sz,vol_sz_frac,
246 raid_label[drv->raid_level]);
247 pos += size; len += size;
251 *start = buffer+offset;
255 h->busy_configuring = 0;
260 cciss_proc_write(struct file *file, const char *buffer,
261 unsigned long count, void *data)
263 unsigned char cmd[80];
265 #ifdef CONFIG_CISS_SCSI_TAPE
266 ctlr_info_t *h = (ctlr_info_t *) data;
270 if (count > sizeof(cmd)-1) return -EINVAL;
271 if (copy_from_user(cmd, buffer, count)) return -EFAULT;
273 len = strlen(cmd); // above 3 lines ensure safety
274 if (cmd[len-1] == '\n')
276 # ifdef CONFIG_CISS_SCSI_TAPE
277 if (strcmp("engage scsi", cmd)==0) {
278 rc = cciss_engage_scsi(h->ctlr);
279 if (rc != 0) return -rc;
282 /* might be nice to have "disengage" too, but it's not
283 safely possible. (only 1 module use count, lock issues.) */
289 * Get us a file in /proc/cciss that says something about each controller.
290 * Create /proc/cciss if it doesn't exist yet.
292 static void __devinit cciss_procinit(int i)
294 struct proc_dir_entry *pde;
296 if (proc_cciss == NULL) {
297 proc_cciss = proc_mkdir("cciss", proc_root_driver);
302 pde = create_proc_read_entry(hba[i]->devname,
303 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
304 proc_cciss, cciss_proc_get_info, hba[i]);
305 pde->write_proc = cciss_proc_write;
307 #endif /* CONFIG_PROC_FS */
310 * For operations that cannot sleep, a command block is allocated at init,
311 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
312 * which ones are free or in use. For operations that can wait for kmalloc
313 * to possible sleep, this routine can be called with get_from_pool set to 0.
314 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
316 static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
318 CommandList_struct *c;
321 dma_addr_t cmd_dma_handle, err_dma_handle;
325 c = (CommandList_struct *) pci_alloc_consistent(
326 h->pdev, sizeof(CommandList_struct), &cmd_dma_handle);
329 memset(c, 0, sizeof(CommandList_struct));
331 c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
332 h->pdev, sizeof(ErrorInfo_struct),
335 if (c->err_info == NULL)
337 pci_free_consistent(h->pdev,
338 sizeof(CommandList_struct), c, cmd_dma_handle);
341 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
342 } else /* get it out of the controllers pool */
345 i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
348 } while(test_and_set_bit(i & (BITS_PER_LONG - 1), h->cmd_pool_bits+(i/BITS_PER_LONG)) != 0);
350 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
353 memset(c, 0, sizeof(CommandList_struct));
354 cmd_dma_handle = h->cmd_pool_dhandle
355 + i*sizeof(CommandList_struct);
356 c->err_info = h->errinfo_pool + i;
357 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
358 err_dma_handle = h->errinfo_pool_dhandle
359 + i*sizeof(ErrorInfo_struct);
363 c->busaddr = (__u32) cmd_dma_handle;
364 temp64.val = (__u64) err_dma_handle;
365 c->ErrDesc.Addr.lower = temp64.val32.lower;
366 c->ErrDesc.Addr.upper = temp64.val32.upper;
367 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
376 * Frees a command block that was previously allocated with cmd_alloc().
378 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
385 temp64.val32.lower = c->ErrDesc.Addr.lower;
386 temp64.val32.upper = c->ErrDesc.Addr.upper;
387 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
388 c->err_info, (dma_addr_t) temp64.val);
389 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
390 c, (dma_addr_t) c->busaddr);
394 clear_bit(i&(BITS_PER_LONG-1), h->cmd_pool_bits+(i/BITS_PER_LONG));
399 static inline ctlr_info_t *get_host(struct gendisk *disk)
401 return disk->queue->queuedata;
404 static inline drive_info_struct *get_drv(struct gendisk *disk)
406 return disk->private_data;
410 * Open. Make sure the device is really there.
412 static int cciss_open(struct inode *inode, struct file *filep)
414 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
415 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
418 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
419 #endif /* CCISS_DEBUG */
422 * Root is allowed to open raw volume zero even if it's not configured
423 * so array config can still work. I don't think I really like this,
424 * but I'm already using way to many device nodes to claim another one
425 * for "raw controller".
427 if (drv->nr_blocks == 0) {
428 if (iminor(inode) != 0)
430 if (!capable(CAP_SYS_ADMIN))
440 static int cciss_release(struct inode *inode, struct file *filep)
442 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
443 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
446 printk(KERN_DEBUG "cciss_release %s\n", inode->i_bdev->bd_disk->disk_name);
447 #endif /* CCISS_DEBUG */
457 static int cciss_ioctl(struct inode *inode, struct file *filep,
458 unsigned int cmd, unsigned long arg)
460 struct block_device *bdev = inode->i_bdev;
461 struct gendisk *disk = bdev->bd_disk;
462 ctlr_info_t *host = get_host(disk);
463 drive_info_struct *drv = get_drv(disk);
464 int ctlr = host->ctlr;
467 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
468 #endif /* CCISS_DEBUG */
473 struct hd_geometry driver_geo;
474 if (drv->cylinders) {
475 driver_geo.heads = drv->heads;
476 driver_geo.sectors = drv->sectors;
477 driver_geo.cylinders = drv->cylinders;
480 driver_geo.start= get_start_sect(inode->i_bdev);
481 if (copy_to_user((void *) arg, &driver_geo,
482 sizeof( struct hd_geometry)))
487 case CCISS_GETPCIINFO:
489 cciss_pci_info_struct pciinfo;
491 if (!arg) return -EINVAL;
492 pciinfo.bus = host->pdev->bus->number;
493 pciinfo.dev_fn = host->pdev->devfn;
494 pciinfo.board_id = host->board_id;
495 if (copy_to_user((void *) arg, &pciinfo, sizeof( cciss_pci_info_struct )))
499 case CCISS_GETINTINFO:
501 cciss_coalint_struct intinfo;
502 if (!arg) return -EINVAL;
503 intinfo.delay = readl(&host->cfgtable->HostWrite.CoalIntDelay);
504 intinfo.count = readl(&host->cfgtable->HostWrite.CoalIntCount);
505 if (copy_to_user((void *) arg, &intinfo, sizeof( cciss_coalint_struct )))
509 case CCISS_SETINTINFO:
511 cciss_coalint_struct intinfo;
515 if (!arg) return -EINVAL;
516 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
517 if (copy_from_user(&intinfo, (void *) arg, sizeof( cciss_coalint_struct)))
519 if ( (intinfo.delay == 0 ) && (intinfo.count == 0))
522 // printk("cciss_ioctl: delay and count cannot be 0\n");
525 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
526 /* Update the field, and then ring the doorbell */
527 writel( intinfo.delay,
528 &(host->cfgtable->HostWrite.CoalIntDelay));
529 writel( intinfo.count,
530 &(host->cfgtable->HostWrite.CoalIntCount));
531 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
533 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
534 if (!(readl(host->vaddr + SA5_DOORBELL)
537 /* delay and try again */
540 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
541 if (i >= MAX_IOCTL_CONFIG_WAIT)
545 case CCISS_GETNODENAME:
547 NodeName_type NodeName;
550 if (!arg) return -EINVAL;
552 NodeName[i] = readb(&host->cfgtable->ServerName[i]);
553 if (copy_to_user((void *) arg, NodeName, sizeof( NodeName_type)))
557 case CCISS_SETNODENAME:
559 NodeName_type NodeName;
563 if (!arg) return -EINVAL;
564 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
566 if (copy_from_user(NodeName, (void *) arg, sizeof( NodeName_type)))
569 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
571 /* Update the field, and then ring the doorbell */
573 writeb( NodeName[i], &host->cfgtable->ServerName[i]);
575 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
577 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
578 if (!(readl(host->vaddr + SA5_DOORBELL)
581 /* delay and try again */
584 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
585 if (i >= MAX_IOCTL_CONFIG_WAIT)
590 case CCISS_GETHEARTBEAT:
592 Heartbeat_type heartbeat;
594 if (!arg) return -EINVAL;
595 heartbeat = readl(&host->cfgtable->HeartBeat);
596 if (copy_to_user((void *) arg, &heartbeat, sizeof( Heartbeat_type)))
600 case CCISS_GETBUSTYPES:
602 BusTypes_type BusTypes;
604 if (!arg) return -EINVAL;
605 BusTypes = readl(&host->cfgtable->BusTypes);
606 if (copy_to_user((void *) arg, &BusTypes, sizeof( BusTypes_type) ))
610 case CCISS_GETFIRMVER:
612 FirmwareVer_type firmware;
614 if (!arg) return -EINVAL;
615 memcpy(firmware, host->firm_ver, 4);
617 if (copy_to_user((void *) arg, firmware, sizeof( FirmwareVer_type)))
621 case CCISS_GETDRIVVER:
623 DriverVer_type DriverVer = DRIVER_VERSION;
625 if (!arg) return -EINVAL;
627 if (copy_to_user((void *) arg, &DriverVer, sizeof( DriverVer_type) ))
632 case CCISS_REVALIDVOLS:
633 if (bdev != bdev->bd_contains || drv != host->drv)
635 return revalidate_allvol(host);
637 case CCISS_GETLUNINFO: {
638 LogvolInfo_struct luninfo;
641 luninfo.LunID = drv->LunID;
642 luninfo.num_opens = drv->usage_count;
643 luninfo.num_parts = 0;
644 /* count partitions 1 to 15 with sizes > 0 */
645 for(i=1; i <MAX_PART; i++) {
648 if (disk->part[i]->nr_sects != 0)
651 if (copy_to_user((void *) arg, &luninfo,
652 sizeof(LogvolInfo_struct)))
656 case CCISS_DEREGDISK:
657 return deregister_disk(disk);
660 return register_new_disk(host);
664 IOCTL_Command_struct iocommand;
665 CommandList_struct *c;
669 DECLARE_COMPLETION(wait);
671 if (!arg) return -EINVAL;
673 if (!capable(CAP_SYS_RAWIO)) return -EPERM;
675 if (copy_from_user(&iocommand, (void *) arg, sizeof( IOCTL_Command_struct) ))
677 if((iocommand.buf_size < 1) &&
678 (iocommand.Request.Type.Direction != XFER_NONE))
682 #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
683 /* Check kmalloc limits */
684 if(iocommand.buf_size > 128000)
687 if(iocommand.buf_size > 0)
689 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
693 if (iocommand.Request.Type.Direction == XFER_WRITE)
695 /* Copy the data into the buffer we created */
696 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
702 if ((c = cmd_alloc(host , 0)) == NULL)
707 // Fill in the command type
708 c->cmd_type = CMD_IOCTL_PEND;
709 // Fill in Command Header
710 c->Header.ReplyQueue = 0; // unused in simple mode
711 if( iocommand.buf_size > 0) // buffer to fill
713 c->Header.SGList = 1;
714 c->Header.SGTotal= 1;
715 } else // no buffers to fill
717 c->Header.SGList = 0;
718 c->Header.SGTotal= 0;
720 c->Header.LUN = iocommand.LUN_info;
721 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
723 // Fill in Request block
724 c->Request = iocommand.Request;
726 // Fill in the scatter gather information
727 if (iocommand.buf_size > 0 )
729 temp64.val = pci_map_single( host->pdev, buff,
731 PCI_DMA_BIDIRECTIONAL);
732 c->SG[0].Addr.lower = temp64.val32.lower;
733 c->SG[0].Addr.upper = temp64.val32.upper;
734 c->SG[0].Len = iocommand.buf_size;
735 c->SG[0].Ext = 0; // we are not chaining
739 /* Put the request on the tail of the request queue */
740 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
741 addQ(&host->reqQ, c);
744 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
746 wait_for_completion(&wait);
748 /* unlock the buffers from DMA */
749 temp64.val32.lower = c->SG[0].Addr.lower;
750 temp64.val32.upper = c->SG[0].Addr.upper;
751 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
752 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
754 /* Copy the error information out */
755 iocommand.error_info = *(c->err_info);
756 if ( copy_to_user((void *) arg, &iocommand, sizeof( IOCTL_Command_struct) ) )
759 cmd_free(host, c, 0);
763 if (iocommand.Request.Type.Direction == XFER_READ)
765 /* Copy the data out of the buffer we created */
766 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size))
769 cmd_free(host, c, 0);
774 cmd_free(host, c, 0);
777 case CCISS_BIG_PASSTHRU: {
778 BIG_IOCTL_Command_struct *ioc;
779 CommandList_struct *c;
780 unsigned char **buff = NULL;
781 int *buff_size = NULL;
787 DECLARE_COMPLETION(wait);
794 if (!capable(CAP_SYS_RAWIO))
796 ioc = (BIG_IOCTL_Command_struct *)
797 kmalloc(sizeof(*ioc), GFP_KERNEL);
802 if (copy_from_user(ioc, (void *) arg, sizeof(*ioc))) {
806 if ((ioc->buf_size < 1) &&
807 (ioc->Request.Type.Direction != XFER_NONE)) {
811 /* Check kmalloc limits using all SGs */
812 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
816 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
820 buff = (unsigned char **) kmalloc(MAXSGENTRIES *
821 sizeof(char *), GFP_KERNEL);
826 memset(buff, 0, MAXSGENTRIES);
827 buff_size = (int *) kmalloc(MAXSGENTRIES * sizeof(int),
833 left = ioc->buf_size;
834 data_ptr = (BYTE *) ioc->buf;
836 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
837 buff_size[sg_used] = sz;
838 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
839 if (buff[sg_used] == NULL) {
843 if (ioc->Request.Type.Direction == XFER_WRITE &&
844 copy_from_user(buff[sg_used], data_ptr, sz)) {
852 if ((c = cmd_alloc(host , 0)) == NULL) {
856 c->cmd_type = CMD_IOCTL_PEND;
857 c->Header.ReplyQueue = 0;
859 if( ioc->buf_size > 0) {
860 c->Header.SGList = sg_used;
861 c->Header.SGTotal= sg_used;
863 c->Header.SGList = 0;
864 c->Header.SGTotal= 0;
866 c->Header.LUN = ioc->LUN_info;
867 c->Header.Tag.lower = c->busaddr;
869 c->Request = ioc->Request;
870 if (ioc->buf_size > 0 ) {
872 for(i=0; i<sg_used; i++) {
873 temp64.val = pci_map_single( host->pdev, buff[i],
875 PCI_DMA_BIDIRECTIONAL);
876 c->SG[i].Addr.lower = temp64.val32.lower;
877 c->SG[i].Addr.upper = temp64.val32.upper;
878 c->SG[i].Len = buff_size[i];
879 c->SG[i].Ext = 0; /* we are not chaining */
883 /* Put the request on the tail of the request queue */
884 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
885 addQ(&host->reqQ, c);
888 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
889 wait_for_completion(&wait);
890 /* unlock the buffers from DMA */
891 for(i=0; i<sg_used; i++) {
892 temp64.val32.lower = c->SG[i].Addr.lower;
893 temp64.val32.upper = c->SG[i].Addr.upper;
894 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
895 buff_size[i], PCI_DMA_BIDIRECTIONAL);
897 /* Copy the error information out */
898 ioc->error_info = *(c->err_info);
899 if (copy_to_user((void *) arg, ioc, sizeof(*ioc))) {
900 cmd_free(host, c, 0);
904 if (ioc->Request.Type.Direction == XFER_READ) {
905 /* Copy the data out of the buffer we created */
906 BYTE *ptr = (BYTE *) ioc->buf;
907 for(i=0; i< sg_used; i++) {
908 if (copy_to_user(ptr, buff[i], buff_size[i])) {
909 cmd_free(host, c, 0);
916 cmd_free(host, c, 0);
920 for(i=0; i<sg_used; i++)
937 static int cciss_revalidate(struct gendisk *disk)
939 drive_info_struct *drv = disk->private_data;
940 set_capacity(disk, drv->nr_blocks);
945 * revalidate_allvol is for online array config utilities. After a
946 * utility reconfigures the drives in the array, it can use this function
947 * (through an ioctl) to make the driver zap any previous disk structs for
948 * that controller and get new ones.
950 * Right now I'm using the getgeometry() function to do this, but this
951 * function should probably be finer grained and allow you to revalidate one
952 * particualar logical volume (instead of all of them on a particular
955 static int revalidate_allvol(ctlr_info_t *host)
957 int ctlr = host->ctlr, i;
960 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
961 if (host->usage_count > 1) {
962 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
963 printk(KERN_WARNING "cciss: Device busy for volume"
964 " revalidation (usage=%d)\n", host->usage_count);
968 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
970 for(i=0; i< NWD; i++) {
971 struct gendisk *disk = host->gendisk[i];
972 if (disk->flags & GENHD_FL_UP)
977 * Set the partition and block size structures for all volumes
978 * on this controller to zero. We will reread all of this data
980 memset(host->drv, 0, sizeof(drive_info_struct)
983 * Tell the array controller not to give us any interrupts while
984 * we check the new geometry. Then turn interrupts back on when
987 host->access.set_intr_mask(host, CCISS_INTR_OFF);
988 cciss_getgeometry(ctlr);
989 host->access.set_intr_mask(host, CCISS_INTR_ON);
991 /* Loop through each real device */
992 for (i = 0; i < NWD; i++) {
993 struct gendisk *disk = host->gendisk[i];
994 drive_info_struct *drv = &(host->drv[i]);
997 blk_queue_hardsect_size(host->queue, drv->block_size);
998 set_capacity(disk, drv->nr_blocks);
1001 host->usage_count--;
1005 static int deregister_disk(struct gendisk *disk)
1007 unsigned long flags;
1008 ctlr_info_t *h = get_host(disk);
1009 drive_info_struct *drv = get_drv(disk);
1012 if (!capable(CAP_SYS_RAWIO))
1015 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1016 /* make sure logical volume is NOT is use */
1017 if( drv->usage_count > 1) {
1018 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1022 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1024 /* invalidate the devices and deregister the disk */
1025 if (disk->flags & GENHD_FL_UP)
1027 /* check to see if it was the last disk */
1028 if (drv == h->drv + h->highest_lun) {
1029 /* if so, find the new hightest lun */
1030 int i, newhighest =-1;
1031 for(i=0; i<h->highest_lun; i++) {
1032 /* if the disk has size > 0, it is available */
1033 if (h->drv[i].nr_blocks)
1036 h->highest_lun = newhighest;
1040 /* zero out the disk size info */
1042 drv->block_size = 0;
1047 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
1049 unsigned int use_unit_num, /* 0: address the controller,
1050 1: address logical volume log_unit,
1051 2: periph device address is scsi3addr */
1052 unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr,
1055 ctlr_info_t *h= hba[ctlr];
1056 u64bit buff_dma_handle;
1059 c->cmd_type = CMD_IOCTL_PEND;
1060 c->Header.ReplyQueue = 0;
1062 c->Header.SGList = 1;
1063 c->Header.SGTotal= 1;
1065 c->Header.SGList = 0;
1066 c->Header.SGTotal= 0;
1068 c->Header.Tag.lower = c->busaddr;
1070 c->Request.Type.Type = cmd_type;
1071 if (cmd_type == TYPE_CMD) {
1074 /* If the logical unit number is 0 then, this is going
1075 to controller so It's a physical command
1076 mode = 0 target = 0. So we have nothing to write.
1077 otherwise, if use_unit_num == 1,
1078 mode = 1(volume set addressing) target = LUNID
1079 otherwise, if use_unit_num == 2,
1080 mode = 0(periph dev addr) target = scsi3addr */
1081 if (use_unit_num == 1) {
1082 c->Header.LUN.LogDev.VolId=
1083 h->drv[log_unit].LunID;
1084 c->Header.LUN.LogDev.Mode = 1;
1085 } else if (use_unit_num == 2) {
1086 memcpy(c->Header.LUN.LunAddrBytes,scsi3addr,8);
1087 c->Header.LUN.LogDev.Mode = 0;
1089 /* are we trying to read a vital product page */
1090 if(page_code != 0) {
1091 c->Request.CDB[1] = 0x01;
1092 c->Request.CDB[2] = page_code;
1094 c->Request.CDBLen = 6;
1095 c->Request.Type.Attribute = ATTR_SIMPLE;
1096 c->Request.Type.Direction = XFER_READ;
1097 c->Request.Timeout = 0;
1098 c->Request.CDB[0] = CISS_INQUIRY;
1099 c->Request.CDB[4] = size & 0xFF;
1101 case CISS_REPORT_LOG:
1102 case CISS_REPORT_PHYS:
1103 /* Talking to controller so It's a physical command
1104 mode = 00 target = 0. Nothing to write.
1106 c->Request.CDBLen = 12;
1107 c->Request.Type.Attribute = ATTR_SIMPLE;
1108 c->Request.Type.Direction = XFER_READ;
1109 c->Request.Timeout = 0;
1110 c->Request.CDB[0] = cmd;
1111 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
1112 c->Request.CDB[7] = (size >> 16) & 0xFF;
1113 c->Request.CDB[8] = (size >> 8) & 0xFF;
1114 c->Request.CDB[9] = size & 0xFF;
1117 case CCISS_READ_CAPACITY:
1118 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1119 c->Header.LUN.LogDev.Mode = 1;
1120 c->Request.CDBLen = 10;
1121 c->Request.Type.Attribute = ATTR_SIMPLE;
1122 c->Request.Type.Direction = XFER_READ;
1123 c->Request.Timeout = 0;
1124 c->Request.CDB[0] = cmd;
1126 case CCISS_CACHE_FLUSH:
1127 c->Request.CDBLen = 12;
1128 c->Request.Type.Attribute = ATTR_SIMPLE;
1129 c->Request.Type.Direction = XFER_WRITE;
1130 c->Request.Timeout = 0;
1131 c->Request.CDB[0] = BMIC_WRITE;
1132 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
1136 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
1139 } else if (cmd_type == TYPE_MSG) {
1141 case 3: /* No-Op message */
1142 c->Request.CDBLen = 1;
1143 c->Request.Type.Attribute = ATTR_SIMPLE;
1144 c->Request.Type.Direction = XFER_WRITE;
1145 c->Request.Timeout = 0;
1146 c->Request.CDB[0] = cmd;
1150 "cciss%d: unknown message type %d\n",
1156 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
1159 /* Fill in the scatter gather information */
1161 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
1162 buff, size, PCI_DMA_BIDIRECTIONAL);
1163 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1164 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1165 c->SG[0].Len = size;
1166 c->SG[0].Ext = 0; /* we are not chaining */
1170 static int sendcmd_withirq(__u8 cmd,
1174 unsigned int use_unit_num,
1175 unsigned int log_unit,
1179 ctlr_info_t *h = hba[ctlr];
1180 CommandList_struct *c;
1181 u64bit buff_dma_handle;
1182 unsigned long flags;
1184 DECLARE_COMPLETION(wait);
1186 if ((c = cmd_alloc(h , 0)) == NULL)
1188 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1189 log_unit, page_code, NULL, cmd_type);
1190 if (return_status != IO_OK) {
1192 return return_status;
1197 /* Put the request on the tail of the queue and send it */
1198 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1202 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1204 wait_for_completion(&wait);
1206 if(c->err_info->CommandStatus != 0)
1207 { /* an error has occurred */
1208 switch(c->err_info->CommandStatus)
1210 case CMD_TARGET_STATUS:
1211 printk(KERN_WARNING "cciss: cmd %p has "
1212 " completed with errors\n", c);
1213 if( c->err_info->ScsiStatus)
1215 printk(KERN_WARNING "cciss: cmd %p "
1216 "has SCSI Status = %x\n",
1218 c->err_info->ScsiStatus);
1222 case CMD_DATA_UNDERRUN:
1223 case CMD_DATA_OVERRUN:
1224 /* expected for inquire and report lun commands */
1227 printk(KERN_WARNING "cciss: Cmd %p is "
1228 "reported invalid\n", c);
1229 return_status = IO_ERROR;
1231 case CMD_PROTOCOL_ERR:
1232 printk(KERN_WARNING "cciss: cmd %p has "
1233 "protocol error \n", c);
1234 return_status = IO_ERROR;
1236 case CMD_HARDWARE_ERR:
1237 printk(KERN_WARNING "cciss: cmd %p had "
1238 " hardware error\n", c);
1239 return_status = IO_ERROR;
1241 case CMD_CONNECTION_LOST:
1242 printk(KERN_WARNING "cciss: cmd %p had "
1243 "connection lost\n", c);
1244 return_status = IO_ERROR;
1247 printk(KERN_WARNING "cciss: cmd %p was "
1249 return_status = IO_ERROR;
1251 case CMD_ABORT_FAILED:
1252 printk(KERN_WARNING "cciss: cmd %p reports "
1253 "abort failed\n", c);
1254 return_status = IO_ERROR;
1256 case CMD_UNSOLICITED_ABORT:
1258 "cciss%d: unsolicited abort %p\n",
1260 if (c->retry_count < MAX_CMD_RETRIES) {
1262 "cciss%d: retrying %p\n",
1265 /* erase the old error information */
1266 memset(c->err_info, 0,
1267 sizeof(ErrorInfo_struct));
1268 return_status = IO_OK;
1269 INIT_COMPLETION(wait);
1272 return_status = IO_ERROR;
1275 printk(KERN_WARNING "cciss: cmd %p returned "
1276 "unknown status %x\n", c,
1277 c->err_info->CommandStatus);
1278 return_status = IO_ERROR;
1281 /* unlock the buffers from DMA */
1282 pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
1283 size, PCI_DMA_BIDIRECTIONAL);
1285 return(return_status);
1288 static void cciss_geometry_inquiry(int ctlr, int logvol,
1289 int withirq, unsigned int total_size,
1290 unsigned int block_size, InquiryData_struct *inq_buff,
1291 drive_info_struct *drv)
1294 memset(inq_buff, 0, sizeof(InquiryData_struct));
1296 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
1297 inq_buff, sizeof(*inq_buff), 1, logvol ,0xC1, TYPE_CMD);
1299 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
1300 sizeof(*inq_buff), 1, logvol ,0xC1, NULL, TYPE_CMD);
1301 if (return_code == IO_OK) {
1302 if(inq_buff->data_byte[8] == 0xFF) {
1304 "cciss: reading geometry failed, volume "
1305 "does not support reading geometry\n");
1306 drv->block_size = block_size;
1307 drv->nr_blocks = total_size;
1309 drv->sectors = 32; // Sectors per track
1310 drv->cylinders = total_size / 255 / 32;
1312 drv->block_size = block_size;
1313 drv->nr_blocks = total_size;
1314 drv->heads = inq_buff->data_byte[6];
1315 drv->sectors = inq_buff->data_byte[7];
1316 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1317 drv->cylinders += inq_buff->data_byte[5];
1319 } else { /* Get geometry failed */
1320 printk(KERN_WARNING "cciss: reading geometry failed, "
1321 "continuing with default geometry\n");
1322 drv->block_size = block_size;
1323 drv->nr_blocks = total_size;
1325 drv->sectors = 32; // Sectors per track
1326 drv->cylinders = total_size / 255 / 32;
1328 printk(KERN_INFO " heads= %d, sectors= %d, cylinders= %d\n\n",
1329 drv->heads, drv->sectors, drv->cylinders);
1332 cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
1333 int withirq, unsigned int *total_size, unsigned int *block_size)
1336 memset(buf, 0, sizeof(*buf));
1338 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
1339 ctlr, buf, sizeof(*buf), 1, logvol, 0, TYPE_CMD);
1341 return_code = sendcmd(CCISS_READ_CAPACITY,
1342 ctlr, buf, sizeof(*buf), 1, logvol, 0, NULL, TYPE_CMD);
1343 if (return_code == IO_OK) {
1344 *total_size = be32_to_cpu(*((__u32 *) &buf->total_size[0]))+1;
1345 *block_size = be32_to_cpu(*((__u32 *) &buf->block_size[0]));
1346 } else { /* read capacity command failed */
1347 printk(KERN_WARNING "cciss: read capacity failed\n");
1349 *block_size = BLOCK_SIZE;
1351 printk(KERN_INFO " blocks= %u block_size= %d\n",
1352 *total_size, *block_size);
1355 static int register_new_disk(ctlr_info_t *h)
1357 struct gendisk *disk;
1362 int new_lun_found = 0;
1363 int new_lun_index = 0;
1364 int free_index_found = 0;
1366 ReportLunData_struct *ld_buff = NULL;
1367 ReadCapdata_struct *size_buff = NULL;
1368 InquiryData_struct *inq_buff = NULL;
1372 unsigned int block_size;
1373 unsigned int total_size;
1375 if (!capable(CAP_SYS_RAWIO))
1377 /* if we have no space in our disk array left to add anything */
1378 if( h->num_luns >= CISS_MAX_LUN)
1381 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1382 if (ld_buff == NULL)
1384 memset(ld_buff, 0, sizeof(ReportLunData_struct));
1385 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1386 if (size_buff == NULL)
1388 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1389 if (inq_buff == NULL)
1392 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
1393 sizeof(ReportLunData_struct), 0, 0, 0, TYPE_CMD);
1395 if( return_code == IO_OK)
1398 // printk("LUN Data\n--------------------------\n");
1400 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
1401 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
1402 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
1403 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
1404 } else /* reading number of logical volumes failed */
1406 printk(KERN_WARNING "cciss: report logical volume"
1407 " command failed\n");
1411 num_luns = listlength / 8; // 8 bytes pre entry
1412 if (num_luns > CISS_MAX_LUN)
1414 num_luns = CISS_MAX_LUN;
1417 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
1418 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
1419 ld_buff->LUNListLength[3], num_luns);
1421 for(i=0; i< num_luns; i++)
1424 int lunID_found = 0;
1426 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
1427 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
1428 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
1429 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1431 /* check to see if this is a new lun */
1432 for(j=0; j <= h->highest_lun; j++)
1435 printk("Checking %d %x against %x\n", j,h->drv[j].LunID,
1437 #endif /* CCISS_DEBUG */
1438 if (h->drv[j].LunID == lunid)
1445 if( lunID_found == 1)
1448 { /* It is the new lun we have been looking for */
1450 printk("new lun found at %d\n", i);
1451 #endif /* CCISS_DEBUG */
1459 printk(KERN_WARNING "cciss: New Logical Volume not found\n");
1462 /* Now find the free index */
1463 for(i=0; i <CISS_MAX_LUN; i++)
1466 printk("Checking Index %d\n", i);
1467 #endif /* CCISS_DEBUG */
1468 if(h->drv[i].LunID == 0)
1471 printk("free index found at %d\n", i);
1472 #endif /* CCISS_DEBUG */
1473 free_index_found = 1;
1478 if (!free_index_found)
1480 printk(KERN_WARNING "cciss: unable to find free slot for disk\n");
1484 logvol = free_index;
1485 h->drv[logvol].LunID = lunid;
1486 /* there could be gaps in lun numbers, track hightest */
1487 if(h->highest_lun < lunid)
1488 h->highest_lun = logvol;
1489 cciss_read_capacity(ctlr, logvol, size_buff, 1,
1490 &total_size, &block_size);
1491 cciss_geometry_inquiry(ctlr, logvol, 1, total_size, block_size,
1492 inq_buff, &h->drv[logvol]);
1493 h->drv[logvol].usage_count = 0;
1495 /* setup partitions per disk */
1496 disk = h->gendisk[logvol];
1497 set_capacity(disk, h->drv[logvol].nr_blocks);
1505 printk(KERN_ERR "cciss: out of memory\n");
1511 * Wait polling for a command to complete.
1512 * The memory mapped FIFO is polled for the completion.
1513 * Used only at init time, interrupts from the HBA are disabled.
1515 static unsigned long pollcomplete(int ctlr)
1520 /* Wait (up to 20 seconds) for a command to complete */
1522 for (i = 20 * HZ; i > 0; i--) {
1523 done = hba[ctlr]->access.command_completed(hba[ctlr]);
1524 if (done == FIFO_EMPTY) {
1525 set_current_state(TASK_UNINTERRUPTIBLE);
1526 schedule_timeout(1);
1530 /* Invalid address to tell caller we ran out of time */
1534 * Send a command to the controller, and wait for it to complete.
1535 * Only used at init time.
1542 unsigned int use_unit_num, /* 0: address the controller,
1543 1: address logical volume log_unit,
1544 2: periph device address is scsi3addr */
1545 unsigned int log_unit,
1547 unsigned char *scsi3addr,
1550 CommandList_struct *c;
1552 unsigned long complete;
1553 ctlr_info_t *info_p= hba[ctlr];
1554 u64bit buff_dma_handle;
1557 if ((c = cmd_alloc(info_p, 1)) == NULL) {
1558 printk(KERN_WARNING "cciss: unable to get memory");
1561 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1562 log_unit, page_code, scsi3addr, cmd_type);
1563 if (status != IO_OK) {
1564 cmd_free(info_p, c, 1);
1572 printk(KERN_DEBUG "cciss: turning intr off\n");
1573 #endif /* CCISS_DEBUG */
1574 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
1576 /* Make sure there is room in the command FIFO */
1577 /* Actually it should be completely empty at this time. */
1578 for (i = 200000; i > 0; i--)
1580 /* if fifo isn't full go */
1581 if (!(info_p->access.fifo_full(info_p)))
1587 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
1588 " waiting!\n", ctlr);
1593 info_p->access.submit_command(info_p, c);
1594 complete = pollcomplete(ctlr);
1597 printk(KERN_DEBUG "cciss: command completed\n");
1598 #endif /* CCISS_DEBUG */
1600 if (complete != 1) {
1601 if ( (complete & CISS_ERROR_BIT)
1602 && (complete & ~CISS_ERROR_BIT) == c->busaddr)
1604 /* if data overrun or underun on Report command
1607 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
1608 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
1609 (c->Request.CDB[0] == CISS_INQUIRY)) &&
1610 ((c->err_info->CommandStatus ==
1611 CMD_DATA_OVERRUN) ||
1612 (c->err_info->CommandStatus ==
1616 complete = c->busaddr;
1618 if (c->err_info->CommandStatus ==
1619 CMD_UNSOLICITED_ABORT) {
1620 printk(KERN_WARNING "cciss%d: "
1621 "unsolicited abort %p\n",
1623 if (c->retry_count < MAX_CMD_RETRIES) {
1625 "cciss%d: retrying %p\n",
1628 /* erase the old error */
1630 memset(c->err_info, 0,
1631 sizeof(ErrorInfo_struct));
1635 "cciss%d: retried %p too "
1636 "many times\n", ctlr, c);
1641 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1642 " Error %x \n", ctlr,
1643 c->err_info->CommandStatus);
1644 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1646 " size %x\n num %x value %x\n", ctlr,
1647 c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
1648 c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
1649 c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
1654 if (complete != c->busaddr) {
1655 printk( KERN_WARNING "cciss cciss%d: SendCmd "
1656 "Invalid command list address returned! (%lx)\n",
1662 printk( KERN_WARNING
1663 "cciss cciss%d: SendCmd Timeout out, "
1664 "No command list address returned!\n",
1670 /* unlock the data buffer from DMA */
1671 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
1672 size, PCI_DMA_BIDIRECTIONAL);
1673 cmd_free(info_p, c, 1);
1677 * Map (physical) PCI mem into (virtual) kernel space
1679 static ulong remap_pci_mem(ulong base, ulong size)
1681 ulong page_base = ((ulong) base) & PAGE_MASK;
1682 ulong page_offs = ((ulong) base) - page_base;
1683 ulong page_remapped = (ulong) ioremap(page_base, page_offs+size);
1685 return (ulong) (page_remapped ? (page_remapped + page_offs) : 0UL);
1689 * Enqueuing and dequeuing functions for cmdlists.
1691 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
1693 if (*Qptr == NULL) {
1695 c->next = c->prev = c;
1697 c->prev = (*Qptr)->prev;
1699 (*Qptr)->prev->next = c;
1704 static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
1705 CommandList_struct *c)
1707 if (c && c->next != c) {
1708 if (*Qptr == c) *Qptr = c->next;
1709 c->prev->next = c->next;
1710 c->next->prev = c->prev;
1718 * Takes jobs of the Q and sends them to the hardware, then puts it on
1719 * the Q to wait for completion.
1721 static void start_io( ctlr_info_t *h)
1723 CommandList_struct *c;
1725 while(( c = h->reqQ) != NULL )
1727 /* can't do anything if fifo is full */
1728 if ((h->access.fifo_full(h))) {
1729 printk(KERN_WARNING "cciss: fifo full\n");
1733 /* Get the frist entry from the Request Q */
1734 removeQ(&(h->reqQ), c);
1737 /* Tell the controller execute command */
1738 h->access.submit_command(h, c);
1740 /* Put job onto the completed Q */
1741 addQ (&(h->cmpQ), c);
1745 static inline void complete_buffers(struct bio *bio, int status)
1748 struct bio *xbh = bio->bi_next;
1749 int nr_sectors = bio_sectors(bio);
1751 bio->bi_next = NULL;
1752 blk_finished_io(len);
1753 bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
1758 /* Assumes that CCISS_LOCK(h->ctlr) is held. */
1759 /* Zeros out the error record and then resends the command back */
1760 /* to the controller */
1761 static inline void resend_cciss_cmd( ctlr_info_t *h, CommandList_struct *c)
1763 /* erase the old error information */
1764 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
1766 /* add it to software queue and then send it to the controller */
1769 if(h->Qdepth > h->maxQsinceinit)
1770 h->maxQsinceinit = h->Qdepth;
1774 /* checks the status of the job and calls complete buffers to mark all
1775 * buffers for the completed job.
1777 static inline void complete_command( ctlr_info_t *h, CommandList_struct *cmd,
1788 if(cmd->err_info->CommandStatus != 0)
1789 { /* an error has occurred */
1790 switch(cmd->err_info->CommandStatus)
1792 unsigned char sense_key;
1793 case CMD_TARGET_STATUS:
1796 if( cmd->err_info->ScsiStatus == 0x02)
1798 printk(KERN_WARNING "cciss: cmd %p "
1799 "has CHECK CONDITION "
1800 " byte 2 = 0x%x\n", cmd,
1801 cmd->err_info->SenseInfo[2]
1803 /* check the sense key */
1805 cmd->err_info->SenseInfo[2];
1806 /* no status or recovered error */
1807 if((sense_key == 0x0) ||
1814 printk(KERN_WARNING "cciss: cmd %p "
1815 "has SCSI Status 0x%x\n",
1816 cmd, cmd->err_info->ScsiStatus);
1819 case CMD_DATA_UNDERRUN:
1820 printk(KERN_WARNING "cciss: cmd %p has"
1821 " completed with data underrun "
1824 case CMD_DATA_OVERRUN:
1825 printk(KERN_WARNING "cciss: cmd %p has"
1826 " completed with data overrun "
1830 printk(KERN_WARNING "cciss: cmd %p is "
1831 "reported invalid\n", cmd);
1834 case CMD_PROTOCOL_ERR:
1835 printk(KERN_WARNING "cciss: cmd %p has "
1836 "protocol error \n", cmd);
1839 case CMD_HARDWARE_ERR:
1840 printk(KERN_WARNING "cciss: cmd %p had "
1841 " hardware error\n", cmd);
1844 case CMD_CONNECTION_LOST:
1845 printk(KERN_WARNING "cciss: cmd %p had "
1846 "connection lost\n", cmd);
1850 printk(KERN_WARNING "cciss: cmd %p was "
1854 case CMD_ABORT_FAILED:
1855 printk(KERN_WARNING "cciss: cmd %p reports "
1856 "abort failed\n", cmd);
1859 case CMD_UNSOLICITED_ABORT:
1860 printk(KERN_WARNING "cciss%d: unsolicited "
1861 "abort %p\n", h->ctlr, cmd);
1862 if (cmd->retry_count < MAX_CMD_RETRIES) {
1865 "cciss%d: retrying %p\n",
1870 "cciss%d: %p retried too "
1871 "many times\n", h->ctlr, cmd);
1875 printk(KERN_WARNING "cciss: cmd %p timedout\n",
1880 printk(KERN_WARNING "cciss: cmd %p returned "
1881 "unknown status %x\n", cmd,
1882 cmd->err_info->CommandStatus);
1886 /* We need to return this command */
1888 resend_cciss_cmd(h,cmd);
1891 /* command did not need to be retried */
1892 /* unmap the DMA mapping for all the scatter gather elements */
1893 for(i=0; i<cmd->Header.SGList; i++) {
1894 temp64.val32.lower = cmd->SG[i].Addr.lower;
1895 temp64.val32.upper = cmd->SG[i].Addr.upper;
1896 pci_unmap_page(hba[cmd->ctlr]->pdev,
1897 temp64.val, cmd->SG[i].Len,
1898 (cmd->Request.Type.Direction == XFER_READ) ?
1899 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
1901 complete_buffers(cmd->rq->bio, status);
1904 printk("Done with %p\n", cmd->rq);
1905 #endif /* CCISS_DEBUG */
1907 end_that_request_last(cmd->rq);
1912 * Get a request and submit it to the controller.
1914 static void do_cciss_request(request_queue_t *q)
1916 ctlr_info_t *h= q->queuedata;
1917 CommandList_struct *c;
1919 struct request *creq;
1921 struct scatterlist tmp_sg[MAXSGENTRIES];
1922 drive_info_struct *drv;
1925 if (blk_queue_plugged(q))
1929 creq = elv_next_request(q);
1933 if (creq->nr_phys_segments > MAXSGENTRIES)
1936 if (( c = cmd_alloc(h, 1)) == NULL)
1939 blkdev_dequeue_request(creq);
1941 spin_unlock_irq(q->queue_lock);
1943 c->cmd_type = CMD_RWREQ;
1946 /* fill in the request */
1947 drv = creq->rq_disk->private_data;
1948 c->Header.ReplyQueue = 0; // unused in simple mode
1949 c->Header.Tag.lower = c->busaddr; // use the physical address the cmd block for tag
1950 c->Header.LUN.LogDev.VolId= drv->LunID;
1951 c->Header.LUN.LogDev.Mode = 1;
1952 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
1953 c->Request.Type.Type = TYPE_CMD; // It is a command.
1954 c->Request.Type.Attribute = ATTR_SIMPLE;
1955 c->Request.Type.Direction =
1956 (rq_data_dir(creq) == READ) ? XFER_READ: XFER_WRITE;
1957 c->Request.Timeout = 0; // Don't time out
1958 c->Request.CDB[0] = (rq_data_dir(creq) == READ) ? CCISS_READ : CCISS_WRITE;
1959 start_blk = creq->sector;
1961 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
1962 (int) creq->nr_sectors);
1963 #endif /* CCISS_DEBUG */
1965 seg = blk_rq_map_sg(q, creq, tmp_sg);
1967 /* get the DMA records for the setup */
1968 if (c->Request.Type.Direction == XFER_READ)
1969 dir = PCI_DMA_FROMDEVICE;
1971 dir = PCI_DMA_TODEVICE;
1973 for (i=0; i<seg; i++)
1975 c->SG[i].Len = tmp_sg[i].length;
1976 temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
1977 tmp_sg[i].offset, tmp_sg[i].length,
1979 c->SG[i].Addr.lower = temp64.val32.lower;
1980 c->SG[i].Addr.upper = temp64.val32.upper;
1981 c->SG[i].Ext = 0; // we are not chaining
1983 /* track how many SG entries we are using */
1988 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", creq->nr_sectors, seg);
1989 #endif /* CCISS_DEBUG */
1991 c->Header.SGList = c->Header.SGTotal = seg;
1992 c->Request.CDB[1]= 0;
1993 c->Request.CDB[2]= (start_blk >> 24) & 0xff; //MSB
1994 c->Request.CDB[3]= (start_blk >> 16) & 0xff;
1995 c->Request.CDB[4]= (start_blk >> 8) & 0xff;
1996 c->Request.CDB[5]= start_blk & 0xff;
1997 c->Request.CDB[6]= 0; // (sect >> 24) & 0xff; MSB
1998 c->Request.CDB[7]= (creq->nr_sectors >> 8) & 0xff;
1999 c->Request.CDB[8]= creq->nr_sectors & 0xff;
2000 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2002 spin_lock_irq(q->queue_lock);
2006 if(h->Qdepth > h->maxQsinceinit)
2007 h->maxQsinceinit = h->Qdepth;
2016 static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
2018 ctlr_info_t *h = dev_id;
2019 CommandList_struct *c;
2020 unsigned long flags;
2024 /* Is this interrupt for us? */
2025 if (( h->access.intr_pending(h) == 0) || (h->interrupts_enabled == 0))
2029 * If there are completed commands in the completion queue,
2030 * we had better do something about it.
2032 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2033 while( h->access.intr_pending(h))
2035 while((a = h->access.command_completed(h)) != FIFO_EMPTY)
2039 if ((c = h->cmpQ) == NULL)
2041 printk(KERN_WARNING "cciss: Completion of %08lx ignored\n", (unsigned long)a1);
2044 while(c->busaddr != a) {
2050 * If we've found the command, take it off the
2051 * completion Q and free it
2053 if (c->busaddr == a) {
2054 removeQ(&h->cmpQ, c);
2055 if (c->cmd_type == CMD_RWREQ) {
2056 complete_command(h, c, 0);
2057 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2058 complete(c->waiting);
2060 # ifdef CONFIG_CISS_SCSI_TAPE
2061 else if (c->cmd_type == CMD_SCSI)
2062 complete_scsi_command(c, 0, a1);
2070 * See if we can queue up some more IO
2072 blk_start_queue(h->queue);
2073 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2077 * We cannot read the structure directly, for portablity we must use
2079 * This is for debug only.
2082 static void print_cfg_table( CfgTable_struct *tb)
2087 printk("Controller Configuration information\n");
2088 printk("------------------------------------\n");
2090 temp_name[i] = readb(&(tb->Signature[i]));
2092 printk(" Signature = %s\n", temp_name);
2093 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
2094 printk(" Transport methods supported = 0x%x\n",
2095 readl(&(tb-> TransportSupport)));
2096 printk(" Transport methods active = 0x%x\n",
2097 readl(&(tb->TransportActive)));
2098 printk(" Requested transport Method = 0x%x\n",
2099 readl(&(tb->HostWrite.TransportRequest)));
2100 printk(" Coalese Interrupt Delay = 0x%x\n",
2101 readl(&(tb->HostWrite.CoalIntDelay)));
2102 printk(" Coalese Interrupt Count = 0x%x\n",
2103 readl(&(tb->HostWrite.CoalIntCount)));
2104 printk(" Max outstanding commands = 0x%d\n",
2105 readl(&(tb->CmdsOutMax)));
2106 printk(" Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
2108 temp_name[i] = readb(&(tb->ServerName[i]));
2109 temp_name[16] = '\0';
2110 printk(" Server Name = %s\n", temp_name);
2111 printk(" Heartbeat Counter = 0x%x\n\n\n",
2112 readl(&(tb->HeartBeat)));
2114 #endif /* CCISS_DEBUG */
2116 static void release_io_mem(ctlr_info_t *c)
2118 /* if IO mem was not protected do nothing */
2119 if( c->io_mem_addr == 0)
2121 release_region(c->io_mem_addr, c->io_mem_length);
2123 c->io_mem_length = 0;
2126 static int find_PCI_BAR_index(struct pci_dev *pdev,
2127 unsigned long pci_bar_addr)
2129 int i, offset, mem_type, bar_type;
2130 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
2133 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
2134 bar_type = pci_resource_flags(pdev, i) &
2135 PCI_BASE_ADDRESS_SPACE;
2136 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2139 mem_type = pci_resource_flags(pdev, i) &
2140 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
2142 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2143 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2144 offset += 4; /* 32 bit */
2146 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2149 default: /* reserved in PCI 2.2 */
2150 printk(KERN_WARNING "Base address is invalid\n");
2155 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2161 static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2163 ushort subsystem_vendor_id, subsystem_device_id, command;
2164 unchar irq = pdev->irq;
2165 __u32 board_id, scratchpad = 0;
2167 __u32 cfg_base_addr;
2168 __u64 cfg_base_addr_index;
2171 /* check to see if controller has been disabled */
2172 /* BEFORE trying to enable it */
2173 (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
2174 if(!(command & 0x02))
2176 printk(KERN_WARNING "cciss: controller appears to be disabled\n");
2180 if (pci_enable_device(pdev))
2182 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
2185 if (pci_set_dma_mask(pdev, CCISS_DMA_MASK ) != 0)
2187 printk(KERN_ERR "cciss: Unable to set DMA mask\n");
2191 subsystem_vendor_id = pdev->subsystem_vendor;
2192 subsystem_device_id = pdev->subsystem_device;
2193 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
2194 subsystem_vendor_id);
2196 /* search for our IO range so we can protect it */
2197 for(i=0; i<DEVICE_COUNT_RESOURCE; i++)
2199 /* is this an IO range */
2200 if( pci_resource_flags(pdev, i) & 0x01 ) {
2201 c->io_mem_addr = pci_resource_start(pdev, i);
2202 c->io_mem_length = pci_resource_end(pdev, i) -
2203 pci_resource_start(pdev, i) +1;
2205 printk("IO value found base_addr[%d] %lx %lx\n", i,
2206 c->io_mem_addr, c->io_mem_length);
2207 #endif /* CCISS_DEBUG */
2208 /* register the IO range */
2209 if(!request_region( c->io_mem_addr,
2210 c->io_mem_length, "cciss"))
2212 printk(KERN_WARNING "cciss I/O memory range already in use addr=%lx length=%ld\n",
2213 c->io_mem_addr, c->io_mem_length);
2215 c->io_mem_length = 0;
2222 printk("command = %x\n", command);
2223 printk("irq = %x\n", irq);
2224 printk("board_id = %x\n", board_id);
2225 #endif /* CCISS_DEBUG */
2230 * Memory base addr is first addr , the second points to the config
2234 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
2236 printk("address 0 = %x\n", c->paddr);
2237 #endif /* CCISS_DEBUG */
2238 c->vaddr = remap_pci_mem(c->paddr, 200);
2240 /* Wait for the board to become ready. (PCI hotplug needs this.)
2241 * We poll for up to 120 secs, once per 100ms. */
2242 for (i=0; i < 1200; i++) {
2243 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
2244 if (scratchpad == CCISS_FIRMWARE_READY)
2246 set_current_state(TASK_INTERRUPTIBLE);
2247 schedule_timeout(HZ / 10); /* wait 100ms */
2249 if (scratchpad != CCISS_FIRMWARE_READY) {
2250 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
2254 /* get the address index number */
2255 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2256 cfg_base_addr &= (__u32) 0x0000ffff;
2258 printk("cfg base address = %x\n", cfg_base_addr);
2259 #endif /* CCISS_DEBUG */
2260 cfg_base_addr_index =
2261 find_PCI_BAR_index(pdev, cfg_base_addr);
2263 printk("cfg base address index = %x\n", cfg_base_addr_index);
2264 #endif /* CCISS_DEBUG */
2265 if (cfg_base_addr_index == -1) {
2266 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
2271 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2273 printk("cfg offset = %x\n", cfg_offset);
2274 #endif /* CCISS_DEBUG */
2275 c->cfgtable = (CfgTable_struct *)
2276 remap_pci_mem(pci_resource_start(pdev, cfg_base_addr_index)
2277 + cfg_offset, sizeof(CfgTable_struct));
2278 c->board_id = board_id;
2281 print_cfg_table(c->cfgtable);
2282 #endif /* CCISS_DEBUG */
2284 for(i=0; i<NR_PRODUCTS; i++) {
2285 if (board_id == products[i].board_id) {
2286 c->product_name = products[i].product_name;
2287 c->access = *(products[i].access);
2291 if (i == NR_PRODUCTS) {
2292 printk(KERN_WARNING "cciss: Sorry, I don't know how"
2293 " to access the Smart Array controller %08lx\n",
2294 (unsigned long)board_id);
2297 if ( (readb(&c->cfgtable->Signature[0]) != 'C') ||
2298 (readb(&c->cfgtable->Signature[1]) != 'I') ||
2299 (readb(&c->cfgtable->Signature[2]) != 'S') ||
2300 (readb(&c->cfgtable->Signature[3]) != 'S') )
2302 printk("Does not appear to be a valid CISS config table\n");
2308 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
2310 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
2312 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
2317 printk("Trying to put board into Simple mode\n");
2318 #endif /* CCISS_DEBUG */
2319 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
2320 /* Update the field, and then ring the doorbell */
2321 writel( CFGTBL_Trans_Simple,
2322 &(c->cfgtable->HostWrite.TransportRequest));
2323 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
2325 /* under certain very rare conditions, this can take awhile.
2326 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
2327 * as we enter this code.) */
2328 for(i=0;i<MAX_CONFIG_WAIT;i++) {
2329 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
2331 /* delay and try again */
2332 set_current_state(TASK_INTERRUPTIBLE);
2333 schedule_timeout(10);
2337 printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
2338 #endif /* CCISS_DEBUG */
2340 print_cfg_table(c->cfgtable);
2341 #endif /* CCISS_DEBUG */
2343 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
2345 printk(KERN_WARNING "cciss: unable to get board into"
2354 * Gets information about the local volumes attached to the controller.
2356 static void cciss_getgeometry(int cntl_num)
2358 ReportLunData_struct *ld_buff;
2359 ReadCapdata_struct *size_buff;
2360 InquiryData_struct *inq_buff;
2368 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
2369 if (ld_buff == NULL)
2371 printk(KERN_ERR "cciss: out of memory\n");
2374 memset(ld_buff, 0, sizeof(ReportLunData_struct));
2375 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
2376 if (size_buff == NULL)
2378 printk(KERN_ERR "cciss: out of memory\n");
2382 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
2383 if (inq_buff == NULL)
2385 printk(KERN_ERR "cciss: out of memory\n");
2390 /* Get the firmware version */
2391 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
2392 sizeof(InquiryData_struct), 0, 0 ,0, NULL, TYPE_CMD);
2393 if (return_code == IO_OK)
2395 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
2396 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
2397 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
2398 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
2399 } else /* send command failed */
2401 printk(KERN_WARNING "cciss: unable to determine firmware"
2402 " version of controller\n");
2404 /* Get the number of logical volumes */
2405 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
2406 sizeof(ReportLunData_struct), 0, 0, 0, NULL, TYPE_CMD);
2408 if( return_code == IO_OK)
2411 printk("LUN Data\n--------------------------\n");
2412 #endif /* CCISS_DEBUG */
2414 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
2415 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
2416 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
2417 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
2418 } else /* reading number of logical volumes failed */
2420 printk(KERN_WARNING "cciss: report logical volume"
2421 " command failed\n");
2424 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
2425 if (hba[cntl_num]->num_luns > CISS_MAX_LUN)
2427 printk(KERN_ERR "ciss: only %d number of logical volumes supported\n",
2429 hba[cntl_num]->num_luns = CISS_MAX_LUN;
2432 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
2433 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
2434 ld_buff->LUNListLength[3], hba[cntl_num]->num_luns);
2435 #endif /* CCISS_DEBUG */
2437 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
2438 for(i=0; i< hba[cntl_num]->num_luns; i++)
2441 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
2442 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
2443 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
2444 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
2446 hba[cntl_num]->drv[i].LunID = lunid;
2450 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
2451 ld_buff->LUN[i][0], ld_buff->LUN[i][1],ld_buff->LUN[i][2],
2452 ld_buff->LUN[i][3], hba[cntl_num]->drv[i].LunID);
2453 #endif /* CCISS_DEBUG */
2454 cciss_read_capacity(cntl_num, i, size_buff, 0,
2455 &total_size, &block_size);
2456 cciss_geometry_inquiry(cntl_num, i, 0, total_size, block_size,
2457 inq_buff, &hba[cntl_num]->drv[i]);
2464 /* Function to find the first free pointer into our hba[] array */
2465 /* Returns -1 if no free entries are left. */
2466 static int alloc_cciss_hba(void)
2468 struct gendisk *disk[NWD];
2470 for (n = 0; n < NWD; n++) {
2471 disk[n] = alloc_disk(1 << NWD_SHIFT);
2476 for(i=0; i< MAX_CTLR; i++) {
2479 p = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
2482 memset(p, 0, sizeof(ctlr_info_t));
2483 for (n = 0; n < NWD; n++)
2484 p->gendisk[n] = disk[n];
2489 printk(KERN_WARNING "cciss: This driver supports a maximum"
2490 " of 8 controllers.\n");
2493 printk(KERN_ERR "cciss: out of memory.\n");
2500 static void free_hba(int i)
2502 ctlr_info_t *p = hba[i];
2506 for (n = 0; n < NWD; n++)
2507 put_disk(p->gendisk[n]);
2512 * This is it. Find all the controllers and register them. I really hate
2513 * stealing all these major device numbers.
2514 * returns the number of block devices registered.
2516 static int __devinit cciss_init_one(struct pci_dev *pdev,
2517 const struct pci_device_id *ent)
2523 printk(KERN_DEBUG "cciss: Device 0x%x has been found at"
2524 " bus %d dev %d func %d\n",
2525 pdev->device, pdev->bus->number, PCI_SLOT(pdev->devfn),
2526 PCI_FUNC(pdev->devfn));
2527 i = alloc_cciss_hba();
2530 if (cciss_pci_init(hba[i], pdev) != 0)
2533 sprintf(hba[i]->devname, "cciss%d", i);
2535 hba[i]->pdev = pdev;
2537 /* configure PCI DMA stuff */
2538 if (!pci_set_dma_mask(pdev, 0xffffffffffffffffULL))
2539 printk("cciss: using DAC cycles\n");
2540 else if (!pci_set_dma_mask(pdev, 0xffffffff))
2541 printk("cciss: not using DAC cycles\n");
2543 printk("cciss: no suitable DMA available\n");
2547 if (register_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname)) {
2548 printk(KERN_ERR "cciss: Unable to register device %s\n",
2553 /* make sure the board interrupts are off */
2554 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
2555 if( request_irq(hba[i]->intr, do_cciss_intr,
2556 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
2557 hba[i]->devname, hba[i])) {
2558 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
2559 hba[i]->intr, hba[i]->devname);
2562 hba[i]->cmd_pool_bits = kmalloc(((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long), GFP_KERNEL);
2563 hba[i]->cmd_pool = (CommandList_struct *)pci_alloc_consistent(
2564 hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2565 &(hba[i]->cmd_pool_dhandle));
2566 hba[i]->errinfo_pool = (ErrorInfo_struct *)pci_alloc_consistent(
2567 hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2568 &(hba[i]->errinfo_pool_dhandle));
2569 if((hba[i]->cmd_pool_bits == NULL)
2570 || (hba[i]->cmd_pool == NULL)
2571 || (hba[i]->errinfo_pool == NULL)) {
2572 printk( KERN_ERR "cciss: out of memory");
2576 spin_lock_init(&hba[i]->lock);
2577 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
2581 q->backing_dev_info.ra_pages = READ_AHEAD;
2583 q->queuedata = hba[i];
2585 /* Initialize the pdev driver private data.
2586 have it point to hba[i]. */
2587 pci_set_drvdata(pdev, hba[i]);
2588 /* command and error info recs zeroed out before
2590 memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long));
2593 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
2594 #endif /* CCISS_DEBUG */
2596 cciss_getgeometry(i);
2598 cciss_scsi_setup(i);
2600 /* Turn the interrupts on so we can service requests */
2601 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
2605 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
2607 /* This is a hardware imposed limit. */
2608 blk_queue_max_hw_segments(q, MAXSGENTRIES);
2610 /* This is a limit in the driver and could be eliminated. */
2611 blk_queue_max_phys_segments(q, MAXSGENTRIES);
2613 blk_queue_max_sectors(q, 512);
2616 for(j=0; j<NWD; j++) {
2617 drive_info_struct *drv = &(hba[i]->drv[j]);
2618 struct gendisk *disk = hba[i]->gendisk[j];
2620 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
2621 sprintf(disk->devfs_name, "cciss/host%d/target%d", i, j);
2622 disk->major = COMPAQ_CISS_MAJOR + i;
2623 disk->first_minor = j << NWD_SHIFT;
2624 disk->fops = &cciss_fops;
2625 disk->queue = hba[i]->queue;
2626 disk->private_data = drv;
2627 if( !(drv->nr_blocks))
2629 blk_queue_hardsect_size(hba[i]->queue, drv->block_size);
2630 set_capacity(disk, drv->nr_blocks);
2636 if(hba[i]->cmd_pool_bits)
2637 kfree(hba[i]->cmd_pool_bits);
2638 if(hba[i]->cmd_pool)
2639 pci_free_consistent(hba[i]->pdev,
2640 NR_CMDS * sizeof(CommandList_struct),
2641 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2642 if(hba[i]->errinfo_pool)
2643 pci_free_consistent(hba[i]->pdev,
2644 NR_CMDS * sizeof( ErrorInfo_struct),
2645 hba[i]->errinfo_pool,
2646 hba[i]->errinfo_pool_dhandle);
2647 free_irq(hba[i]->intr, hba[i]);
2649 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2651 release_io_mem(hba[i]);
2656 static void __devexit cciss_remove_one (struct pci_dev *pdev)
2658 ctlr_info_t *tmp_ptr;
2663 if (pci_get_drvdata(pdev) == NULL)
2665 printk( KERN_ERR "cciss: Unable to remove device \n");
2668 tmp_ptr = pci_get_drvdata(pdev);
2672 printk(KERN_ERR "cciss: device appears to "
2673 "already be removed \n");
2676 /* Turn board interrupts off and send the flush cache command */
2677 /* sendcmd will turn off interrupt, and send the flush...
2678 * To write all data in the battery backed cache to disks */
2679 memset(flush_buf, 0, 4);
2680 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
2682 if(return_code != IO_OK)
2684 printk(KERN_WARNING "Error Flushing cache on controller %d\n",
2687 free_irq(hba[i]->intr, hba[i]);
2688 pci_set_drvdata(pdev, NULL);
2689 iounmap((void*)hba[i]->vaddr);
2690 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
2691 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2692 remove_proc_entry(hba[i]->devname, proc_cciss);
2694 /* remove it from the disk list */
2695 for (j = 0; j < NWD; j++) {
2696 struct gendisk *disk = hba[i]->gendisk[j];
2697 if (disk->flags & GENHD_FL_UP)
2701 blk_cleanup_queue(hba[i]->queue);
2702 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2703 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2704 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2705 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
2706 kfree(hba[i]->cmd_pool_bits);
2707 release_io_mem(hba[i]);
2711 static struct pci_driver cciss_pci_driver = {
2713 .probe = cciss_init_one,
2714 .remove = __devexit_p(cciss_remove_one),
2715 .id_table = cciss_pci_device_id, /* id_table */
2719 * This is it. Register the PCI driver information for the cards we control
2720 * the OS will call our registered routines when it finds one of our cards.
2722 int __init cciss_init(void)
2724 printk(KERN_INFO DRIVER_NAME "\n");
2726 /* Register for our PCI devices */
2727 return pci_module_init(&cciss_pci_driver);
2730 static int __init init_cciss_module(void)
2732 return ( cciss_init());
2735 static void __exit cleanup_cciss_module(void)
2739 pci_unregister_driver(&cciss_pci_driver);
2740 /* double check that all controller entrys have been removed */
2741 for (i=0; i< MAX_CTLR; i++)
2745 printk(KERN_WARNING "cciss: had to remove"
2746 " controller %d\n", i);
2747 cciss_remove_one(hba[i]->pdev);
2750 remove_proc_entry("cciss", proc_root_driver);
2753 module_init(init_cciss_module);
2754 module_exit(cleanup_cciss_module);