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 <linux/compat.h>
41 #include <asm/uaccess.h>
44 #include <linux/blkdev.h>
45 #include <linux/genhd.h>
46 #include <linux/completion.h>
48 #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
49 #define DRIVER_NAME "HP CISS Driver (v 2.6.4)"
50 #define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,4)
52 /* Embedded module documentation macros - see modules.h */
53 MODULE_AUTHOR("Hewlett-Packard Company");
54 MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 2.6.4");
55 MODULE_VERSION("2.6.4");
56 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
58 MODULE_LICENSE("GPL");
60 #include "cciss_cmd.h"
62 #include <linux/cciss_ioctl.h>
64 /* define the PCI info for the cards we can control */
65 const struct pci_device_id cciss_pci_device_id[] = {
66 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS,
67 0x0E11, 0x4070, 0, 0, 0},
68 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
69 0x0E11, 0x4080, 0, 0, 0},
70 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
71 0x0E11, 0x4082, 0, 0, 0},
72 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
73 0x0E11, 0x4083, 0, 0, 0},
74 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
75 0x0E11, 0x409A, 0, 0, 0},
76 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
77 0x0E11, 0x409B, 0, 0, 0},
78 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
79 0x0E11, 0x409C, 0, 0, 0},
80 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
81 0x0E11, 0x409D, 0, 0, 0},
82 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
83 0x0E11, 0x4091, 0, 0, 0},
84 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA,
85 0x103C, 0x3225, 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 { 0x3225103C, "Smart Array P600", &SA5_access},
109 /* How long to wait (in millesconds) for board to go into simple mode */
110 #define MAX_CONFIG_WAIT 30000
111 #define MAX_IOCTL_CONFIG_WAIT 1000
113 /*define how many times we will try a command because of bus resets */
114 #define MAX_CMD_RETRIES 3
116 #define READ_AHEAD 1024
117 #define NR_CMDS 384 /* #commands that can be outstanding */
120 #define CCISS_DMA_MASK 0xFFFFFFFF /* 32 bit DMA */
122 static ctlr_info_t *hba[MAX_CTLR];
124 static void do_cciss_request(request_queue_t *q);
125 static int cciss_open(struct inode *inode, struct file *filep);
126 static int cciss_release(struct inode *inode, struct file *filep);
127 static int cciss_ioctl(struct inode *inode, struct file *filep,
128 unsigned int cmd, unsigned long arg);
130 static int revalidate_allvol(ctlr_info_t *host);
131 static int cciss_revalidate(struct gendisk *disk);
132 static int deregister_disk(struct gendisk *disk);
133 static int register_new_disk(ctlr_info_t *h);
135 static void cciss_getgeometry(int cntl_num);
137 static void start_io( ctlr_info_t *h);
138 static int sendcmd( __u8 cmd, int ctlr, void *buff, size_t size,
139 unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
140 unsigned char *scsi3addr, int cmd_type);
142 #ifdef CONFIG_PROC_FS
143 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
144 int length, int *eof, void *data);
145 static void cciss_procinit(int i);
147 static void cciss_procinit(int i) {}
148 #endif /* CONFIG_PROC_FS */
150 static struct block_device_operations cciss_fops = {
151 .owner = THIS_MODULE,
153 .release = cciss_release,
154 .ioctl = cciss_ioctl,
155 .revalidate_disk= cciss_revalidate,
159 * Enqueuing and dequeuing functions for cmdlists.
161 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
165 c->next = c->prev = c;
167 c->prev = (*Qptr)->prev;
169 (*Qptr)->prev->next = c;
174 static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
175 CommandList_struct *c)
177 if (c && c->next != c) {
178 if (*Qptr == c) *Qptr = c->next;
179 c->prev->next = c->next;
180 c->next->prev = c->prev;
187 #include "cciss_scsi.c" /* For SCSI tape support */
189 #ifdef CONFIG_PROC_FS
192 * Report information about this controller.
194 #define ENG_GIG 1000000000
195 #define ENG_GIG_FACTOR (ENG_GIG/512)
196 #define RAID_UNKNOWN 6
197 static const char *raid_label[] = {"0","4","1(1+0)","5","5+1","ADG",
200 static struct proc_dir_entry *proc_cciss;
202 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
203 int length, int *eof, void *data)
208 ctlr_info_t *h = (ctlr_info_t*)data;
209 drive_info_struct *drv;
211 sector_t vol_sz, vol_sz_frac;
215 /* prevent displaying bogus info during configuration
216 * or deconfiguration of a logical volume
218 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
219 if (h->busy_configuring) {
220 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
223 h->busy_configuring = 1;
224 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
226 size = sprintf(buffer, "%s: HP %s Controller\n"
227 "Board ID: 0x%08lx\n"
228 "Firmware Version: %c%c%c%c\n"
230 "Logical drives: %d\n"
231 "Current Q depth: %d\n"
232 "Current # commands on controller: %d\n"
233 "Max Q depth since init: %d\n"
234 "Max # commands on controller since init: %d\n"
235 "Max SG entries since init: %d\n\n",
238 (unsigned long)h->board_id,
239 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
240 (unsigned int)h->intr,
242 h->Qdepth, h->commands_outstanding,
243 h->maxQsinceinit, h->max_outstanding, h->maxSG);
245 pos += size; len += size;
246 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
247 for(i=0; i<=h->highest_lun; i++) {
250 if (drv->block_size == 0)
253 vol_sz = drv->nr_blocks;
254 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
256 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
258 if (drv->raid_level > 5)
259 drv->raid_level = RAID_UNKNOWN;
260 size = sprintf(buffer+len, "cciss/c%dd%d:"
261 "\t%4u.%02uGB\tRAID %s\n",
262 ctlr, i, (int)vol_sz, (int)vol_sz_frac,
263 raid_label[drv->raid_level]);
264 pos += size; len += size;
268 *start = buffer+offset;
272 h->busy_configuring = 0;
277 cciss_proc_write(struct file *file, const char __user *buffer,
278 unsigned long count, void *data)
280 unsigned char cmd[80];
282 #ifdef CONFIG_CISS_SCSI_TAPE
283 ctlr_info_t *h = (ctlr_info_t *) data;
287 if (count > sizeof(cmd)-1) return -EINVAL;
288 if (copy_from_user(cmd, buffer, count)) return -EFAULT;
290 len = strlen(cmd); // above 3 lines ensure safety
291 if (cmd[len-1] == '\n')
293 # ifdef CONFIG_CISS_SCSI_TAPE
294 if (strcmp("engage scsi", cmd)==0) {
295 rc = cciss_engage_scsi(h->ctlr);
296 if (rc != 0) return -rc;
299 /* might be nice to have "disengage" too, but it's not
300 safely possible. (only 1 module use count, lock issues.) */
306 * Get us a file in /proc/cciss that says something about each controller.
307 * Create /proc/cciss if it doesn't exist yet.
309 static void __devinit cciss_procinit(int i)
311 struct proc_dir_entry *pde;
313 if (proc_cciss == NULL) {
314 proc_cciss = proc_mkdir("cciss", proc_root_driver);
319 pde = create_proc_read_entry(hba[i]->devname,
320 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
321 proc_cciss, cciss_proc_get_info, hba[i]);
322 pde->write_proc = cciss_proc_write;
324 #endif /* CONFIG_PROC_FS */
327 * For operations that cannot sleep, a command block is allocated at init,
328 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
329 * which ones are free or in use. For operations that can wait for kmalloc
330 * to possible sleep, this routine can be called with get_from_pool set to 0.
331 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
333 static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
335 CommandList_struct *c;
338 dma_addr_t cmd_dma_handle, err_dma_handle;
342 c = (CommandList_struct *) pci_alloc_consistent(
343 h->pdev, sizeof(CommandList_struct), &cmd_dma_handle);
346 memset(c, 0, sizeof(CommandList_struct));
348 c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
349 h->pdev, sizeof(ErrorInfo_struct),
352 if (c->err_info == NULL)
354 pci_free_consistent(h->pdev,
355 sizeof(CommandList_struct), c, cmd_dma_handle);
358 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
359 } else /* get it out of the controllers pool */
362 i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
365 } while(test_and_set_bit(i & (BITS_PER_LONG - 1), h->cmd_pool_bits+(i/BITS_PER_LONG)) != 0);
367 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
370 memset(c, 0, sizeof(CommandList_struct));
371 cmd_dma_handle = h->cmd_pool_dhandle
372 + i*sizeof(CommandList_struct);
373 c->err_info = h->errinfo_pool + i;
374 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
375 err_dma_handle = h->errinfo_pool_dhandle
376 + i*sizeof(ErrorInfo_struct);
380 c->busaddr = (__u32) cmd_dma_handle;
381 temp64.val = (__u64) err_dma_handle;
382 c->ErrDesc.Addr.lower = temp64.val32.lower;
383 c->ErrDesc.Addr.upper = temp64.val32.upper;
384 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
393 * Frees a command block that was previously allocated with cmd_alloc().
395 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
402 temp64.val32.lower = c->ErrDesc.Addr.lower;
403 temp64.val32.upper = c->ErrDesc.Addr.upper;
404 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
405 c->err_info, (dma_addr_t) temp64.val);
406 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
407 c, (dma_addr_t) c->busaddr);
411 clear_bit(i&(BITS_PER_LONG-1), h->cmd_pool_bits+(i/BITS_PER_LONG));
416 static inline ctlr_info_t *get_host(struct gendisk *disk)
418 return disk->queue->queuedata;
421 static inline drive_info_struct *get_drv(struct gendisk *disk)
423 return disk->private_data;
427 * Open. Make sure the device is really there.
429 static int cciss_open(struct inode *inode, struct file *filep)
431 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
432 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
435 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
436 #endif /* CCISS_DEBUG */
439 * Root is allowed to open raw volume zero even if it's not configured
440 * so array config can still work. Root is also allowed to open any
441 * volume that has a LUN ID, so it can issue IOCTL to reread the
442 * disk information. I don't think I really like this
443 * but I'm already using way to many device nodes to claim another one
444 * for "raw controller".
446 if (drv->nr_blocks == 0) {
447 if (iminor(inode) != 0) { /* not node 0? */
448 /* if not node 0 make sure it is a partition = 0 */
449 if (iminor(inode) & 0x0f) {
451 /* if it is, make sure we have a LUN ID */
452 } else if (drv->LunID == 0) {
456 if (!capable(CAP_SYS_ADMIN))
466 static int cciss_release(struct inode *inode, struct file *filep)
468 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
469 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
472 printk(KERN_DEBUG "cciss_release %s\n", inode->i_bdev->bd_disk->disk_name);
473 #endif /* CCISS_DEBUG */
481 /* for AMD 64 bit kernel compatibility with 32-bit userland ioctls */
482 extern long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
484 register_ioctl32_conversion(unsigned int cmd, int (*handler)(unsigned int,
485 unsigned int, unsigned long, struct file *));
486 extern int unregister_ioctl32_conversion(unsigned int cmd);
488 static int cciss_ioctl32_passthru(unsigned int fd, unsigned cmd, unsigned long arg, struct file *file);
489 static int cciss_ioctl32_big_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
492 typedef int (*handler_type) (unsigned int, unsigned int, unsigned long, struct file *);
494 static struct ioctl32_map {
496 handler_type handler;
498 } cciss_ioctl32_map[] = {
499 { CCISS_GETPCIINFO, (handler_type) sys_ioctl, 0 },
500 { CCISS_GETINTINFO, (handler_type) sys_ioctl, 0 },
501 { CCISS_SETINTINFO, (handler_type) sys_ioctl, 0 },
502 { CCISS_GETNODENAME, (handler_type) sys_ioctl, 0 },
503 { CCISS_SETNODENAME, (handler_type) sys_ioctl, 0 },
504 { CCISS_GETHEARTBEAT, (handler_type) sys_ioctl, 0 },
505 { CCISS_GETBUSTYPES, (handler_type) sys_ioctl, 0 },
506 { CCISS_GETFIRMVER, (handler_type) sys_ioctl, 0 },
507 { CCISS_GETDRIVVER, (handler_type) sys_ioctl, 0 },
508 { CCISS_REVALIDVOLS, (handler_type) sys_ioctl, 0 },
509 { CCISS_PASSTHRU32, cciss_ioctl32_passthru, 0 },
510 { CCISS_DEREGDISK, (handler_type) sys_ioctl, 0 },
511 { CCISS_REGNEWDISK, (handler_type) sys_ioctl, 0 },
512 { CCISS_REGNEWD, (handler_type) sys_ioctl, 0 },
513 { CCISS_RESCANDISK, (handler_type) sys_ioctl, 0 },
514 { CCISS_GETLUNINFO, (handler_type) sys_ioctl, 0 },
515 { CCISS_BIG_PASSTHRU32, cciss_ioctl32_big_passthru, 0 },
517 #define NCCISS_IOCTL32_ENTRIES (sizeof(cciss_ioctl32_map) / sizeof(cciss_ioctl32_map[0]))
518 static void register_cciss_ioctl32(void)
522 for (i=0; i < NCCISS_IOCTL32_ENTRIES; i++) {
523 rc = register_ioctl32_conversion(
524 cciss_ioctl32_map[i].cmd,
525 cciss_ioctl32_map[i].handler);
527 printk(KERN_WARNING "cciss: failed to register "
528 "32 bit compatible ioctl 0x%08x\n",
529 cciss_ioctl32_map[i].cmd);
530 cciss_ioctl32_map[i].registered = 0;
532 cciss_ioctl32_map[i].registered = 1;
535 static void unregister_cciss_ioctl32(void)
539 for (i=0; i < NCCISS_IOCTL32_ENTRIES; i++) {
540 if (!cciss_ioctl32_map[i].registered)
542 rc = unregister_ioctl32_conversion(
543 cciss_ioctl32_map[i].cmd);
545 cciss_ioctl32_map[i].registered = 0;
548 printk(KERN_WARNING "cciss: failed to unregister "
549 "32 bit compatible ioctl 0x%08x\n",
550 cciss_ioctl32_map[i].cmd);
553 int cciss_ioctl32_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
556 IOCTL32_Command_struct __user *arg32 =
557 (IOCTL32_Command_struct __user *) arg;
558 IOCTL_Command_struct arg64;
559 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
564 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
565 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
566 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
567 err |= get_user(arg64.buf_size, &arg32->buf_size);
568 err |= get_user(cp, &arg32->buf);
569 arg64.buf = compat_ptr(cp);
570 err |= copy_to_user(p, &arg64, sizeof(arg64));
575 err = sys_ioctl(fd, CCISS_PASSTHRU, (unsigned long) p);
578 err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
584 int cciss_ioctl32_big_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
587 BIG_IOCTL32_Command_struct __user *arg32 =
588 (BIG_IOCTL32_Command_struct __user *) arg;
589 BIG_IOCTL_Command_struct arg64;
590 BIG_IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
595 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
596 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
597 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
598 err |= get_user(arg64.buf_size, &arg32->buf_size);
599 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
600 err |= get_user(cp, &arg32->buf);
601 arg64.buf = compat_ptr(cp);
602 err |= copy_to_user(p, &arg64, sizeof(arg64));
607 err = sys_ioctl(fd, CCISS_BIG_PASSTHRU, (unsigned long) p);
610 err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
616 static inline void register_cciss_ioctl32(void) {}
617 static inline void unregister_cciss_ioctl32(void) {}
622 static int cciss_ioctl(struct inode *inode, struct file *filep,
623 unsigned int cmd, unsigned long arg)
625 struct block_device *bdev = inode->i_bdev;
626 struct gendisk *disk = bdev->bd_disk;
627 ctlr_info_t *host = get_host(disk);
628 drive_info_struct *drv = get_drv(disk);
629 int ctlr = host->ctlr;
630 void __user *argp = (void __user *)arg;
633 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
634 #endif /* CCISS_DEBUG */
639 struct hd_geometry driver_geo;
640 if (drv->cylinders) {
641 driver_geo.heads = drv->heads;
642 driver_geo.sectors = drv->sectors;
643 driver_geo.cylinders = drv->cylinders;
646 driver_geo.start= get_start_sect(inode->i_bdev);
647 if (copy_to_user(argp, &driver_geo, sizeof(struct hd_geometry)))
652 case CCISS_GETPCIINFO:
654 cciss_pci_info_struct pciinfo;
656 if (!arg) return -EINVAL;
657 pciinfo.bus = host->pdev->bus->number;
658 pciinfo.dev_fn = host->pdev->devfn;
659 pciinfo.board_id = host->board_id;
660 if (copy_to_user(argp, &pciinfo, sizeof( cciss_pci_info_struct )))
664 case CCISS_GETINTINFO:
666 cciss_coalint_struct intinfo;
667 if (!arg) return -EINVAL;
668 intinfo.delay = readl(&host->cfgtable->HostWrite.CoalIntDelay);
669 intinfo.count = readl(&host->cfgtable->HostWrite.CoalIntCount);
670 if (copy_to_user(argp, &intinfo, sizeof( cciss_coalint_struct )))
674 case CCISS_SETINTINFO:
676 cciss_coalint_struct intinfo;
680 if (!arg) return -EINVAL;
681 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
682 if (copy_from_user(&intinfo, argp, sizeof( cciss_coalint_struct)))
684 if ( (intinfo.delay == 0 ) && (intinfo.count == 0))
687 // printk("cciss_ioctl: delay and count cannot be 0\n");
690 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
691 /* Update the field, and then ring the doorbell */
692 writel( intinfo.delay,
693 &(host->cfgtable->HostWrite.CoalIntDelay));
694 writel( intinfo.count,
695 &(host->cfgtable->HostWrite.CoalIntCount));
696 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
698 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
699 if (!(readl(host->vaddr + SA5_DOORBELL)
702 /* delay and try again */
705 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
706 if (i >= MAX_IOCTL_CONFIG_WAIT)
710 case CCISS_GETNODENAME:
712 NodeName_type NodeName;
715 if (!arg) return -EINVAL;
717 NodeName[i] = readb(&host->cfgtable->ServerName[i]);
718 if (copy_to_user(argp, NodeName, sizeof( NodeName_type)))
722 case CCISS_SETNODENAME:
724 NodeName_type NodeName;
728 if (!arg) return -EINVAL;
729 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
731 if (copy_from_user(NodeName, argp, sizeof( NodeName_type)))
734 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
736 /* Update the field, and then ring the doorbell */
738 writeb( NodeName[i], &host->cfgtable->ServerName[i]);
740 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
742 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
743 if (!(readl(host->vaddr + SA5_DOORBELL)
746 /* delay and try again */
749 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
750 if (i >= MAX_IOCTL_CONFIG_WAIT)
755 case CCISS_GETHEARTBEAT:
757 Heartbeat_type heartbeat;
759 if (!arg) return -EINVAL;
760 heartbeat = readl(&host->cfgtable->HeartBeat);
761 if (copy_to_user(argp, &heartbeat, sizeof( Heartbeat_type)))
765 case CCISS_GETBUSTYPES:
767 BusTypes_type BusTypes;
769 if (!arg) return -EINVAL;
770 BusTypes = readl(&host->cfgtable->BusTypes);
771 if (copy_to_user(argp, &BusTypes, sizeof( BusTypes_type) ))
775 case CCISS_GETFIRMVER:
777 FirmwareVer_type firmware;
779 if (!arg) return -EINVAL;
780 memcpy(firmware, host->firm_ver, 4);
782 if (copy_to_user(argp, firmware, sizeof( FirmwareVer_type)))
786 case CCISS_GETDRIVVER:
788 DriverVer_type DriverVer = DRIVER_VERSION;
790 if (!arg) return -EINVAL;
792 if (copy_to_user(argp, &DriverVer, sizeof( DriverVer_type) ))
797 case CCISS_REVALIDVOLS:
798 if (bdev != bdev->bd_contains || drv != host->drv)
800 return revalidate_allvol(host);
802 case CCISS_GETLUNINFO: {
803 LogvolInfo_struct luninfo;
806 luninfo.LunID = drv->LunID;
807 luninfo.num_opens = drv->usage_count;
808 luninfo.num_parts = 0;
809 /* count partitions 1 to 15 with sizes > 0 */
810 for (i = 0; i < MAX_PART - 1; i++) {
813 if (disk->part[i]->nr_sects != 0)
816 if (copy_to_user(argp, &luninfo,
817 sizeof(LogvolInfo_struct)))
821 case CCISS_DEREGDISK:
822 return deregister_disk(disk);
825 return register_new_disk(host);
829 IOCTL_Command_struct iocommand;
830 CommandList_struct *c;
834 DECLARE_COMPLETION(wait);
836 if (!arg) return -EINVAL;
838 if (!capable(CAP_SYS_RAWIO)) return -EPERM;
840 if (copy_from_user(&iocommand, argp, sizeof( IOCTL_Command_struct) ))
842 if((iocommand.buf_size < 1) &&
843 (iocommand.Request.Type.Direction != XFER_NONE))
847 #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
848 /* Check kmalloc limits */
849 if(iocommand.buf_size > 128000)
852 if(iocommand.buf_size > 0)
854 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
858 if (iocommand.Request.Type.Direction == XFER_WRITE)
860 /* Copy the data into the buffer we created */
861 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
867 memset(buff, 0, iocommand.buf_size);
869 if ((c = cmd_alloc(host , 0)) == NULL)
874 // Fill in the command type
875 c->cmd_type = CMD_IOCTL_PEND;
876 // Fill in Command Header
877 c->Header.ReplyQueue = 0; // unused in simple mode
878 if( iocommand.buf_size > 0) // buffer to fill
880 c->Header.SGList = 1;
881 c->Header.SGTotal= 1;
882 } else // no buffers to fill
884 c->Header.SGList = 0;
885 c->Header.SGTotal= 0;
887 c->Header.LUN = iocommand.LUN_info;
888 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
890 // Fill in Request block
891 c->Request = iocommand.Request;
893 // Fill in the scatter gather information
894 if (iocommand.buf_size > 0 )
896 temp64.val = pci_map_single( host->pdev, buff,
898 PCI_DMA_BIDIRECTIONAL);
899 c->SG[0].Addr.lower = temp64.val32.lower;
900 c->SG[0].Addr.upper = temp64.val32.upper;
901 c->SG[0].Len = iocommand.buf_size;
902 c->SG[0].Ext = 0; // we are not chaining
906 /* Put the request on the tail of the request queue */
907 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
908 addQ(&host->reqQ, c);
911 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
913 wait_for_completion(&wait);
915 /* unlock the buffers from DMA */
916 temp64.val32.lower = c->SG[0].Addr.lower;
917 temp64.val32.upper = c->SG[0].Addr.upper;
918 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
919 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
921 /* Copy the error information out */
922 iocommand.error_info = *(c->err_info);
923 if ( copy_to_user(argp, &iocommand, sizeof( IOCTL_Command_struct) ) )
926 cmd_free(host, c, 0);
930 if (iocommand.Request.Type.Direction == XFER_READ)
932 /* Copy the data out of the buffer we created */
933 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size))
936 cmd_free(host, c, 0);
941 cmd_free(host, c, 0);
944 case CCISS_BIG_PASSTHRU: {
945 BIG_IOCTL_Command_struct *ioc;
946 CommandList_struct *c;
947 unsigned char **buff = NULL;
948 int *buff_size = NULL;
954 DECLARE_COMPLETION(wait);
957 BYTE __user *data_ptr;
961 if (!capable(CAP_SYS_RAWIO))
963 ioc = (BIG_IOCTL_Command_struct *)
964 kmalloc(sizeof(*ioc), GFP_KERNEL);
969 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
973 if ((ioc->buf_size < 1) &&
974 (ioc->Request.Type.Direction != XFER_NONE)) {
978 /* Check kmalloc limits using all SGs */
979 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
983 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
987 buff = (unsigned char **) kmalloc(MAXSGENTRIES *
988 sizeof(char *), GFP_KERNEL);
993 memset(buff, 0, MAXSGENTRIES);
994 buff_size = (int *) kmalloc(MAXSGENTRIES * sizeof(int),
1000 left = ioc->buf_size;
1001 data_ptr = ioc->buf;
1003 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
1004 buff_size[sg_used] = sz;
1005 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1006 if (buff[sg_used] == NULL) {
1010 if (ioc->Request.Type.Direction == XFER_WRITE &&
1011 copy_from_user(buff[sg_used], data_ptr, sz)) {
1015 memset(buff[sg_used], 0, sz);
1021 if ((c = cmd_alloc(host , 0)) == NULL) {
1025 c->cmd_type = CMD_IOCTL_PEND;
1026 c->Header.ReplyQueue = 0;
1028 if( ioc->buf_size > 0) {
1029 c->Header.SGList = sg_used;
1030 c->Header.SGTotal= sg_used;
1032 c->Header.SGList = 0;
1033 c->Header.SGTotal= 0;
1035 c->Header.LUN = ioc->LUN_info;
1036 c->Header.Tag.lower = c->busaddr;
1038 c->Request = ioc->Request;
1039 if (ioc->buf_size > 0 ) {
1041 for(i=0; i<sg_used; i++) {
1042 temp64.val = pci_map_single( host->pdev, buff[i],
1044 PCI_DMA_BIDIRECTIONAL);
1045 c->SG[i].Addr.lower = temp64.val32.lower;
1046 c->SG[i].Addr.upper = temp64.val32.upper;
1047 c->SG[i].Len = buff_size[i];
1048 c->SG[i].Ext = 0; /* we are not chaining */
1052 /* Put the request on the tail of the request queue */
1053 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1054 addQ(&host->reqQ, c);
1057 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1058 wait_for_completion(&wait);
1059 /* unlock the buffers from DMA */
1060 for(i=0; i<sg_used; i++) {
1061 temp64.val32.lower = c->SG[i].Addr.lower;
1062 temp64.val32.upper = c->SG[i].Addr.upper;
1063 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
1064 buff_size[i], PCI_DMA_BIDIRECTIONAL);
1066 /* Copy the error information out */
1067 ioc->error_info = *(c->err_info);
1068 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1069 cmd_free(host, c, 0);
1073 if (ioc->Request.Type.Direction == XFER_READ) {
1074 /* Copy the data out of the buffer we created */
1075 BYTE __user *ptr = ioc->buf;
1076 for(i=0; i< sg_used; i++) {
1077 if (copy_to_user(ptr, buff[i], buff_size[i])) {
1078 cmd_free(host, c, 0);
1082 ptr += buff_size[i];
1085 cmd_free(host, c, 0);
1089 for(i=0; i<sg_used; i++)
1107 * revalidate_allvol is for online array config utilities. After a
1108 * utility reconfigures the drives in the array, it can use this function
1109 * (through an ioctl) to make the driver zap any previous disk structs for
1110 * that controller and get new ones.
1112 * Right now I'm using the getgeometry() function to do this, but this
1113 * function should probably be finer grained and allow you to revalidate one
1114 * particualar logical volume (instead of all of them on a particular
1117 static int revalidate_allvol(ctlr_info_t *host)
1119 int ctlr = host->ctlr, i;
1120 unsigned long flags;
1122 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1123 if (host->usage_count > 1) {
1124 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1125 printk(KERN_WARNING "cciss: Device busy for volume"
1126 " revalidation (usage=%d)\n", host->usage_count);
1129 host->usage_count++;
1130 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1132 for(i=0; i< NWD; i++) {
1133 struct gendisk *disk = host->gendisk[i];
1134 if (disk->flags & GENHD_FL_UP)
1139 * Set the partition and block size structures for all volumes
1140 * on this controller to zero. We will reread all of this data
1142 memset(host->drv, 0, sizeof(drive_info_struct)
1145 * Tell the array controller not to give us any interrupts while
1146 * we check the new geometry. Then turn interrupts back on when
1149 host->access.set_intr_mask(host, CCISS_INTR_OFF);
1150 cciss_getgeometry(ctlr);
1151 host->access.set_intr_mask(host, CCISS_INTR_ON);
1153 /* Loop through each real device */
1154 for (i = 0; i < NWD; i++) {
1155 struct gendisk *disk = host->gendisk[i];
1156 drive_info_struct *drv = &(host->drv[i]);
1157 /* we must register the controller even if no disks exist */
1158 /* this is for the online array utilities */
1159 if (!drv->heads && i)
1161 blk_queue_hardsect_size(host->queue, drv->block_size);
1162 set_capacity(disk, drv->nr_blocks);
1165 host->usage_count--;
1169 static int deregister_disk(struct gendisk *disk)
1171 unsigned long flags;
1172 ctlr_info_t *h = get_host(disk);
1173 drive_info_struct *drv = get_drv(disk);
1176 if (!capable(CAP_SYS_RAWIO))
1179 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1180 /* make sure logical volume is NOT is use */
1181 if( drv->usage_count > 1) {
1182 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1186 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1188 /* invalidate the devices and deregister the disk */
1189 if (disk->flags & GENHD_FL_UP)
1191 /* check to see if it was the last disk */
1192 if (drv == h->drv + h->highest_lun) {
1193 /* if so, find the new hightest lun */
1194 int i, newhighest =-1;
1195 for(i=0; i<h->highest_lun; i++) {
1196 /* if the disk has size > 0, it is available */
1197 if (h->drv[i].nr_blocks)
1200 h->highest_lun = newhighest;
1204 /* zero out the disk size info */
1206 drv->block_size = 0;
1211 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
1213 unsigned int use_unit_num, /* 0: address the controller,
1214 1: address logical volume log_unit,
1215 2: periph device address is scsi3addr */
1216 unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr,
1219 ctlr_info_t *h= hba[ctlr];
1220 u64bit buff_dma_handle;
1223 c->cmd_type = CMD_IOCTL_PEND;
1224 c->Header.ReplyQueue = 0;
1226 c->Header.SGList = 1;
1227 c->Header.SGTotal= 1;
1229 c->Header.SGList = 0;
1230 c->Header.SGTotal= 0;
1232 c->Header.Tag.lower = c->busaddr;
1234 c->Request.Type.Type = cmd_type;
1235 if (cmd_type == TYPE_CMD) {
1238 /* If the logical unit number is 0 then, this is going
1239 to controller so It's a physical command
1240 mode = 0 target = 0. So we have nothing to write.
1241 otherwise, if use_unit_num == 1,
1242 mode = 1(volume set addressing) target = LUNID
1243 otherwise, if use_unit_num == 2,
1244 mode = 0(periph dev addr) target = scsi3addr */
1245 if (use_unit_num == 1) {
1246 c->Header.LUN.LogDev.VolId=
1247 h->drv[log_unit].LunID;
1248 c->Header.LUN.LogDev.Mode = 1;
1249 } else if (use_unit_num == 2) {
1250 memcpy(c->Header.LUN.LunAddrBytes,scsi3addr,8);
1251 c->Header.LUN.LogDev.Mode = 0;
1253 /* are we trying to read a vital product page */
1254 if(page_code != 0) {
1255 c->Request.CDB[1] = 0x01;
1256 c->Request.CDB[2] = page_code;
1258 c->Request.CDBLen = 6;
1259 c->Request.Type.Attribute = ATTR_SIMPLE;
1260 c->Request.Type.Direction = XFER_READ;
1261 c->Request.Timeout = 0;
1262 c->Request.CDB[0] = CISS_INQUIRY;
1263 c->Request.CDB[4] = size & 0xFF;
1265 case CISS_REPORT_LOG:
1266 case CISS_REPORT_PHYS:
1267 /* Talking to controller so It's a physical command
1268 mode = 00 target = 0. Nothing to write.
1270 c->Request.CDBLen = 12;
1271 c->Request.Type.Attribute = ATTR_SIMPLE;
1272 c->Request.Type.Direction = XFER_READ;
1273 c->Request.Timeout = 0;
1274 c->Request.CDB[0] = cmd;
1275 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
1276 c->Request.CDB[7] = (size >> 16) & 0xFF;
1277 c->Request.CDB[8] = (size >> 8) & 0xFF;
1278 c->Request.CDB[9] = size & 0xFF;
1281 case CCISS_READ_CAPACITY:
1282 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1283 c->Header.LUN.LogDev.Mode = 1;
1284 c->Request.CDBLen = 10;
1285 c->Request.Type.Attribute = ATTR_SIMPLE;
1286 c->Request.Type.Direction = XFER_READ;
1287 c->Request.Timeout = 0;
1288 c->Request.CDB[0] = cmd;
1290 case CCISS_CACHE_FLUSH:
1291 c->Request.CDBLen = 12;
1292 c->Request.Type.Attribute = ATTR_SIMPLE;
1293 c->Request.Type.Direction = XFER_WRITE;
1294 c->Request.Timeout = 0;
1295 c->Request.CDB[0] = BMIC_WRITE;
1296 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
1300 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
1303 } else if (cmd_type == TYPE_MSG) {
1305 case 3: /* No-Op message */
1306 c->Request.CDBLen = 1;
1307 c->Request.Type.Attribute = ATTR_SIMPLE;
1308 c->Request.Type.Direction = XFER_WRITE;
1309 c->Request.Timeout = 0;
1310 c->Request.CDB[0] = cmd;
1314 "cciss%d: unknown message type %d\n",
1320 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
1323 /* Fill in the scatter gather information */
1325 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
1326 buff, size, PCI_DMA_BIDIRECTIONAL);
1327 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1328 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1329 c->SG[0].Len = size;
1330 c->SG[0].Ext = 0; /* we are not chaining */
1334 static int sendcmd_withirq(__u8 cmd,
1338 unsigned int use_unit_num,
1339 unsigned int log_unit,
1343 ctlr_info_t *h = hba[ctlr];
1344 CommandList_struct *c;
1345 u64bit buff_dma_handle;
1346 unsigned long flags;
1348 DECLARE_COMPLETION(wait);
1350 if ((c = cmd_alloc(h , 0)) == NULL)
1352 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1353 log_unit, page_code, NULL, cmd_type);
1354 if (return_status != IO_OK) {
1356 return return_status;
1361 /* Put the request on the tail of the queue and send it */
1362 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1366 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1368 wait_for_completion(&wait);
1370 if(c->err_info->CommandStatus != 0)
1371 { /* an error has occurred */
1372 switch(c->err_info->CommandStatus)
1374 case CMD_TARGET_STATUS:
1375 printk(KERN_WARNING "cciss: cmd %p has "
1376 " completed with errors\n", c);
1377 if( c->err_info->ScsiStatus)
1379 printk(KERN_WARNING "cciss: cmd %p "
1380 "has SCSI Status = %x\n",
1382 c->err_info->ScsiStatus);
1386 case CMD_DATA_UNDERRUN:
1387 case CMD_DATA_OVERRUN:
1388 /* expected for inquire and report lun commands */
1391 printk(KERN_WARNING "cciss: Cmd %p is "
1392 "reported invalid\n", c);
1393 return_status = IO_ERROR;
1395 case CMD_PROTOCOL_ERR:
1396 printk(KERN_WARNING "cciss: cmd %p has "
1397 "protocol error \n", c);
1398 return_status = IO_ERROR;
1400 case CMD_HARDWARE_ERR:
1401 printk(KERN_WARNING "cciss: cmd %p had "
1402 " hardware error\n", c);
1403 return_status = IO_ERROR;
1405 case CMD_CONNECTION_LOST:
1406 printk(KERN_WARNING "cciss: cmd %p had "
1407 "connection lost\n", c);
1408 return_status = IO_ERROR;
1411 printk(KERN_WARNING "cciss: cmd %p was "
1413 return_status = IO_ERROR;
1415 case CMD_ABORT_FAILED:
1416 printk(KERN_WARNING "cciss: cmd %p reports "
1417 "abort failed\n", c);
1418 return_status = IO_ERROR;
1420 case CMD_UNSOLICITED_ABORT:
1422 "cciss%d: unsolicited abort %p\n",
1424 if (c->retry_count < MAX_CMD_RETRIES) {
1426 "cciss%d: retrying %p\n",
1429 /* erase the old error information */
1430 memset(c->err_info, 0,
1431 sizeof(ErrorInfo_struct));
1432 return_status = IO_OK;
1433 INIT_COMPLETION(wait);
1436 return_status = IO_ERROR;
1439 printk(KERN_WARNING "cciss: cmd %p returned "
1440 "unknown status %x\n", c,
1441 c->err_info->CommandStatus);
1442 return_status = IO_ERROR;
1445 /* unlock the buffers from DMA */
1446 pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
1447 size, PCI_DMA_BIDIRECTIONAL);
1449 return(return_status);
1452 static void cciss_geometry_inquiry(int ctlr, int logvol,
1453 int withirq, unsigned int total_size,
1454 unsigned int block_size, InquiryData_struct *inq_buff,
1455 drive_info_struct *drv)
1458 memset(inq_buff, 0, sizeof(InquiryData_struct));
1460 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
1461 inq_buff, sizeof(*inq_buff), 1, logvol ,0xC1, TYPE_CMD);
1463 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
1464 sizeof(*inq_buff), 1, logvol ,0xC1, NULL, TYPE_CMD);
1465 if (return_code == IO_OK) {
1466 if(inq_buff->data_byte[8] == 0xFF) {
1468 "cciss: reading geometry failed, volume "
1469 "does not support reading geometry\n");
1470 drv->block_size = block_size;
1471 drv->nr_blocks = total_size;
1473 drv->sectors = 32; // Sectors per track
1474 drv->cylinders = total_size / 255 / 32;
1478 drv->block_size = block_size;
1479 drv->nr_blocks = total_size;
1480 drv->heads = inq_buff->data_byte[6];
1481 drv->sectors = inq_buff->data_byte[7];
1482 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1483 drv->cylinders += inq_buff->data_byte[5];
1484 drv->raid_level = inq_buff->data_byte[8];
1485 t = drv->heads * drv->sectors;
1487 drv->cylinders = total_size/t;
1490 } else { /* Get geometry failed */
1491 printk(KERN_WARNING "cciss: reading geometry failed\n");
1493 printk(KERN_INFO " heads= %d, sectors= %d, cylinders= %d\n\n",
1494 drv->heads, drv->sectors, drv->cylinders);
1497 cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
1498 int withirq, unsigned int *total_size, unsigned int *block_size)
1501 memset(buf, 0, sizeof(*buf));
1503 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
1504 ctlr, buf, sizeof(*buf), 1, logvol, 0, TYPE_CMD);
1506 return_code = sendcmd(CCISS_READ_CAPACITY,
1507 ctlr, buf, sizeof(*buf), 1, logvol, 0, NULL, TYPE_CMD);
1508 if (return_code == IO_OK) {
1509 *total_size = be32_to_cpu(*((__be32 *) &buf->total_size[0]))+1;
1510 *block_size = be32_to_cpu(*((__be32 *) &buf->block_size[0]));
1511 } else { /* read capacity command failed */
1512 printk(KERN_WARNING "cciss: read capacity failed\n");
1514 *block_size = BLOCK_SIZE;
1516 printk(KERN_INFO " blocks= %u block_size= %d\n",
1517 *total_size, *block_size);
1521 static int register_new_disk(ctlr_info_t *h)
1523 struct gendisk *disk;
1528 int new_lun_found = 0;
1529 int new_lun_index = 0;
1530 int free_index_found = 0;
1532 ReportLunData_struct *ld_buff = NULL;
1533 ReadCapdata_struct *size_buff = NULL;
1534 InquiryData_struct *inq_buff = NULL;
1538 unsigned int block_size;
1539 unsigned int total_size;
1541 if (!capable(CAP_SYS_RAWIO))
1543 /* if we have no space in our disk array left to add anything */
1544 if( h->num_luns >= CISS_MAX_LUN)
1547 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1548 if (ld_buff == NULL)
1550 memset(ld_buff, 0, sizeof(ReportLunData_struct));
1551 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1552 if (size_buff == NULL)
1554 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1555 if (inq_buff == NULL)
1558 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
1559 sizeof(ReportLunData_struct), 0, 0, 0, TYPE_CMD);
1561 if( return_code == IO_OK)
1564 // printk("LUN Data\n--------------------------\n");
1566 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
1567 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
1568 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
1569 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
1570 } else /* reading number of logical volumes failed */
1572 printk(KERN_WARNING "cciss: report logical volume"
1573 " command failed\n");
1577 num_luns = listlength / 8; // 8 bytes pre entry
1578 if (num_luns > CISS_MAX_LUN)
1580 num_luns = CISS_MAX_LUN;
1583 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
1584 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
1585 ld_buff->LUNListLength[3], num_luns);
1587 for(i=0; i< num_luns; i++)
1590 int lunID_found = 0;
1592 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
1593 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
1594 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
1595 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1597 /* check to see if this is a new lun */
1598 for(j=0; j <= h->highest_lun; j++)
1601 printk("Checking %d %x against %x\n", j,h->drv[j].LunID,
1603 #endif /* CCISS_DEBUG */
1604 if (h->drv[j].LunID == lunid)
1611 if( lunID_found == 1)
1614 { /* It is the new lun we have been looking for */
1616 printk("new lun found at %d\n", i);
1617 #endif /* CCISS_DEBUG */
1625 printk(KERN_WARNING "cciss: New Logical Volume not found\n");
1628 /* Now find the free index */
1629 for(i=0; i <CISS_MAX_LUN; i++)
1632 printk("Checking Index %d\n", i);
1633 #endif /* CCISS_DEBUG */
1634 if(h->drv[i].LunID == 0)
1637 printk("free index found at %d\n", i);
1638 #endif /* CCISS_DEBUG */
1639 free_index_found = 1;
1644 if (!free_index_found)
1646 printk(KERN_WARNING "cciss: unable to find free slot for disk\n");
1650 logvol = free_index;
1651 h->drv[logvol].LunID = lunid;
1652 /* there could be gaps in lun numbers, track hightest */
1653 if(h->highest_lun < lunid)
1654 h->highest_lun = logvol;
1655 cciss_read_capacity(ctlr, logvol, size_buff, 1,
1656 &total_size, &block_size);
1657 cciss_geometry_inquiry(ctlr, logvol, 1, total_size, block_size,
1658 inq_buff, &h->drv[logvol]);
1659 h->drv[logvol].usage_count = 0;
1661 /* setup partitions per disk */
1662 disk = h->gendisk[logvol];
1663 set_capacity(disk, h->drv[logvol].nr_blocks);
1664 /* if it's the controller it's already added */
1673 printk(KERN_ERR "cciss: out of memory\n");
1679 static int cciss_revalidate(struct gendisk *disk)
1681 ctlr_info_t *h = get_host(disk);
1682 drive_info_struct *drv = get_drv(disk);
1685 unsigned int block_size;
1686 unsigned int total_size;
1687 ReadCapdata_struct *size_buff = NULL;
1688 InquiryData_struct *inq_buff = NULL;
1690 for(logvol=0; logvol < CISS_MAX_LUN; logvol++)
1692 if(h->drv[logvol].LunID == drv->LunID) {
1698 if (!FOUND) return 1;
1700 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1701 if (size_buff == NULL)
1703 printk(KERN_WARNING "cciss: out of memory\n");
1706 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1707 if (inq_buff == NULL)
1709 printk(KERN_WARNING "cciss: out of memory\n");
1714 cciss_read_capacity(h->ctlr, logvol, size_buff, 1, &total_size, &block_size);
1715 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size, inq_buff, drv);
1717 blk_queue_hardsect_size(h->queue, drv->block_size);
1718 set_capacity(disk, drv->nr_blocks);
1726 * Wait polling for a command to complete.
1727 * The memory mapped FIFO is polled for the completion.
1728 * Used only at init time, interrupts from the HBA are disabled.
1730 static unsigned long pollcomplete(int ctlr)
1735 /* Wait (up to 20 seconds) for a command to complete */
1737 for (i = 20 * HZ; i > 0; i--) {
1738 done = hba[ctlr]->access.command_completed(hba[ctlr]);
1739 if (done == FIFO_EMPTY) {
1740 set_current_state(TASK_UNINTERRUPTIBLE);
1741 schedule_timeout(1);
1745 /* Invalid address to tell caller we ran out of time */
1749 * Send a command to the controller, and wait for it to complete.
1750 * Only used at init time.
1757 unsigned int use_unit_num, /* 0: address the controller,
1758 1: address logical volume log_unit,
1759 2: periph device address is scsi3addr */
1760 unsigned int log_unit,
1762 unsigned char *scsi3addr,
1765 CommandList_struct *c;
1767 unsigned long complete;
1768 ctlr_info_t *info_p= hba[ctlr];
1769 u64bit buff_dma_handle;
1772 if ((c = cmd_alloc(info_p, 1)) == NULL) {
1773 printk(KERN_WARNING "cciss: unable to get memory");
1776 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1777 log_unit, page_code, scsi3addr, cmd_type);
1778 if (status != IO_OK) {
1779 cmd_free(info_p, c, 1);
1787 printk(KERN_DEBUG "cciss: turning intr off\n");
1788 #endif /* CCISS_DEBUG */
1789 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
1791 /* Make sure there is room in the command FIFO */
1792 /* Actually it should be completely empty at this time. */
1793 for (i = 200000; i > 0; i--)
1795 /* if fifo isn't full go */
1796 if (!(info_p->access.fifo_full(info_p)))
1802 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
1803 " waiting!\n", ctlr);
1808 info_p->access.submit_command(info_p, c);
1809 complete = pollcomplete(ctlr);
1812 printk(KERN_DEBUG "cciss: command completed\n");
1813 #endif /* CCISS_DEBUG */
1815 if (complete != 1) {
1816 if ( (complete & CISS_ERROR_BIT)
1817 && (complete & ~CISS_ERROR_BIT) == c->busaddr)
1819 /* if data overrun or underun on Report command
1822 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
1823 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
1824 (c->Request.CDB[0] == CISS_INQUIRY)) &&
1825 ((c->err_info->CommandStatus ==
1826 CMD_DATA_OVERRUN) ||
1827 (c->err_info->CommandStatus ==
1831 complete = c->busaddr;
1833 if (c->err_info->CommandStatus ==
1834 CMD_UNSOLICITED_ABORT) {
1835 printk(KERN_WARNING "cciss%d: "
1836 "unsolicited abort %p\n",
1838 if (c->retry_count < MAX_CMD_RETRIES) {
1840 "cciss%d: retrying %p\n",
1843 /* erase the old error */
1845 memset(c->err_info, 0,
1846 sizeof(ErrorInfo_struct));
1850 "cciss%d: retried %p too "
1851 "many times\n", ctlr, c);
1856 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1857 " Error %x \n", ctlr,
1858 c->err_info->CommandStatus);
1859 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1861 " size %x\n num %x value %x\n", ctlr,
1862 c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
1863 c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
1864 c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
1869 if (complete != c->busaddr) {
1870 printk( KERN_WARNING "cciss cciss%d: SendCmd "
1871 "Invalid command list address returned! (%lx)\n",
1877 printk( KERN_WARNING
1878 "cciss cciss%d: SendCmd Timeout out, "
1879 "No command list address returned!\n",
1885 /* unlock the data buffer from DMA */
1886 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
1887 size, PCI_DMA_BIDIRECTIONAL);
1888 cmd_free(info_p, c, 1);
1892 * Map (physical) PCI mem into (virtual) kernel space
1894 static void __iomem *remap_pci_mem(ulong base, ulong size)
1896 ulong page_base = ((ulong) base) & PAGE_MASK;
1897 ulong page_offs = ((ulong) base) - page_base;
1898 void __iomem *page_remapped = ioremap(page_base, page_offs+size);
1900 return page_remapped ? (page_remapped + page_offs) : NULL;
1904 * Takes jobs of the Q and sends them to the hardware, then puts it on
1905 * the Q to wait for completion.
1907 static void start_io( ctlr_info_t *h)
1909 CommandList_struct *c;
1911 while(( c = h->reqQ) != NULL )
1913 /* can't do anything if fifo is full */
1914 if ((h->access.fifo_full(h))) {
1915 printk(KERN_WARNING "cciss: fifo full\n");
1919 /* Get the frist entry from the Request Q */
1920 removeQ(&(h->reqQ), c);
1923 /* Tell the controller execute command */
1924 h->access.submit_command(h, c);
1926 /* Put job onto the completed Q */
1927 addQ (&(h->cmpQ), c);
1931 static inline void complete_buffers(struct bio *bio, int status)
1934 struct bio *xbh = bio->bi_next;
1935 int nr_sectors = bio_sectors(bio);
1937 bio->bi_next = NULL;
1938 blk_finished_io(len);
1939 bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
1944 /* Assumes that CCISS_LOCK(h->ctlr) is held. */
1945 /* Zeros out the error record and then resends the command back */
1946 /* to the controller */
1947 static inline void resend_cciss_cmd( ctlr_info_t *h, CommandList_struct *c)
1949 /* erase the old error information */
1950 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
1952 /* add it to software queue and then send it to the controller */
1955 if(h->Qdepth > h->maxQsinceinit)
1956 h->maxQsinceinit = h->Qdepth;
1960 /* checks the status of the job and calls complete buffers to mark all
1961 * buffers for the completed job.
1963 static inline void complete_command( ctlr_info_t *h, CommandList_struct *cmd,
1974 if(cmd->err_info->CommandStatus != 0)
1975 { /* an error has occurred */
1976 switch(cmd->err_info->CommandStatus)
1978 unsigned char sense_key;
1979 case CMD_TARGET_STATUS:
1982 if( cmd->err_info->ScsiStatus == 0x02)
1984 printk(KERN_WARNING "cciss: cmd %p "
1985 "has CHECK CONDITION "
1986 " byte 2 = 0x%x\n", cmd,
1987 cmd->err_info->SenseInfo[2]
1989 /* check the sense key */
1991 cmd->err_info->SenseInfo[2];
1992 /* no status or recovered error */
1993 if((sense_key == 0x0) ||
2000 printk(KERN_WARNING "cciss: cmd %p "
2001 "has SCSI Status 0x%x\n",
2002 cmd, cmd->err_info->ScsiStatus);
2005 case CMD_DATA_UNDERRUN:
2006 printk(KERN_WARNING "cciss: cmd %p has"
2007 " completed with data underrun "
2010 case CMD_DATA_OVERRUN:
2011 printk(KERN_WARNING "cciss: cmd %p has"
2012 " completed with data overrun "
2016 printk(KERN_WARNING "cciss: cmd %p is "
2017 "reported invalid\n", cmd);
2020 case CMD_PROTOCOL_ERR:
2021 printk(KERN_WARNING "cciss: cmd %p has "
2022 "protocol error \n", cmd);
2025 case CMD_HARDWARE_ERR:
2026 printk(KERN_WARNING "cciss: cmd %p had "
2027 " hardware error\n", cmd);
2030 case CMD_CONNECTION_LOST:
2031 printk(KERN_WARNING "cciss: cmd %p had "
2032 "connection lost\n", cmd);
2036 printk(KERN_WARNING "cciss: cmd %p was "
2040 case CMD_ABORT_FAILED:
2041 printk(KERN_WARNING "cciss: cmd %p reports "
2042 "abort failed\n", cmd);
2045 case CMD_UNSOLICITED_ABORT:
2046 printk(KERN_WARNING "cciss%d: unsolicited "
2047 "abort %p\n", h->ctlr, cmd);
2048 if (cmd->retry_count < MAX_CMD_RETRIES) {
2051 "cciss%d: retrying %p\n",
2056 "cciss%d: %p retried too "
2057 "many times\n", h->ctlr, cmd);
2061 printk(KERN_WARNING "cciss: cmd %p timedout\n",
2066 printk(KERN_WARNING "cciss: cmd %p returned "
2067 "unknown status %x\n", cmd,
2068 cmd->err_info->CommandStatus);
2072 /* We need to return this command */
2074 resend_cciss_cmd(h,cmd);
2077 /* command did not need to be retried */
2078 /* unmap the DMA mapping for all the scatter gather elements */
2079 for(i=0; i<cmd->Header.SGList; i++) {
2080 temp64.val32.lower = cmd->SG[i].Addr.lower;
2081 temp64.val32.upper = cmd->SG[i].Addr.upper;
2082 pci_unmap_page(hba[cmd->ctlr]->pdev,
2083 temp64.val, cmd->SG[i].Len,
2084 (cmd->Request.Type.Direction == XFER_READ) ?
2085 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
2087 complete_buffers(cmd->rq->bio, status);
2090 printk("Done with %p\n", cmd->rq);
2091 #endif /* CCISS_DEBUG */
2093 end_that_request_last(cmd->rq);
2098 * Get a request and submit it to the controller.
2100 static void do_cciss_request(request_queue_t *q)
2102 ctlr_info_t *h= q->queuedata;
2103 CommandList_struct *c;
2105 struct request *creq;
2107 struct scatterlist tmp_sg[MAXSGENTRIES];
2108 drive_info_struct *drv;
2111 if (blk_queue_plugged(q))
2115 creq = elv_next_request(q);
2119 if (creq->nr_phys_segments > MAXSGENTRIES)
2122 if (( c = cmd_alloc(h, 1)) == NULL)
2125 blkdev_dequeue_request(creq);
2127 spin_unlock_irq(q->queue_lock);
2129 c->cmd_type = CMD_RWREQ;
2132 /* fill in the request */
2133 drv = creq->rq_disk->private_data;
2134 c->Header.ReplyQueue = 0; // unused in simple mode
2135 c->Header.Tag.lower = c->busaddr; // use the physical address the cmd block for tag
2136 c->Header.LUN.LogDev.VolId= drv->LunID;
2137 c->Header.LUN.LogDev.Mode = 1;
2138 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2139 c->Request.Type.Type = TYPE_CMD; // It is a command.
2140 c->Request.Type.Attribute = ATTR_SIMPLE;
2141 c->Request.Type.Direction =
2142 (rq_data_dir(creq) == READ) ? XFER_READ: XFER_WRITE;
2143 c->Request.Timeout = 0; // Don't time out
2144 c->Request.CDB[0] = (rq_data_dir(creq) == READ) ? CCISS_READ : CCISS_WRITE;
2145 start_blk = creq->sector;
2147 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
2148 (int) creq->nr_sectors);
2149 #endif /* CCISS_DEBUG */
2151 seg = blk_rq_map_sg(q, creq, tmp_sg);
2153 /* get the DMA records for the setup */
2154 if (c->Request.Type.Direction == XFER_READ)
2155 dir = PCI_DMA_FROMDEVICE;
2157 dir = PCI_DMA_TODEVICE;
2159 for (i=0; i<seg; i++)
2161 c->SG[i].Len = tmp_sg[i].length;
2162 temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
2163 tmp_sg[i].offset, tmp_sg[i].length,
2165 c->SG[i].Addr.lower = temp64.val32.lower;
2166 c->SG[i].Addr.upper = temp64.val32.upper;
2167 c->SG[i].Ext = 0; // we are not chaining
2169 /* track how many SG entries we are using */
2174 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", creq->nr_sectors, seg);
2175 #endif /* CCISS_DEBUG */
2177 c->Header.SGList = c->Header.SGTotal = seg;
2178 c->Request.CDB[1]= 0;
2179 c->Request.CDB[2]= (start_blk >> 24) & 0xff; //MSB
2180 c->Request.CDB[3]= (start_blk >> 16) & 0xff;
2181 c->Request.CDB[4]= (start_blk >> 8) & 0xff;
2182 c->Request.CDB[5]= start_blk & 0xff;
2183 c->Request.CDB[6]= 0; // (sect >> 24) & 0xff; MSB
2184 c->Request.CDB[7]= (creq->nr_sectors >> 8) & 0xff;
2185 c->Request.CDB[8]= creq->nr_sectors & 0xff;
2186 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2188 spin_lock_irq(q->queue_lock);
2192 if(h->Qdepth > h->maxQsinceinit)
2193 h->maxQsinceinit = h->Qdepth;
2202 static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
2204 ctlr_info_t *h = dev_id;
2205 CommandList_struct *c;
2206 unsigned long flags;
2210 /* Is this interrupt for us? */
2211 if (( h->access.intr_pending(h) == 0) || (h->interrupts_enabled == 0))
2215 * If there are completed commands in the completion queue,
2216 * we had better do something about it.
2218 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2219 while( h->access.intr_pending(h))
2221 while((a = h->access.command_completed(h)) != FIFO_EMPTY)
2225 if ((c = h->cmpQ) == NULL)
2227 printk(KERN_WARNING "cciss: Completion of %08lx ignored\n", (unsigned long)a1);
2230 while(c->busaddr != a) {
2236 * If we've found the command, take it off the
2237 * completion Q and free it
2239 if (c->busaddr == a) {
2240 removeQ(&h->cmpQ, c);
2241 if (c->cmd_type == CMD_RWREQ) {
2242 complete_command(h, c, 0);
2243 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2244 complete(c->waiting);
2246 # ifdef CONFIG_CISS_SCSI_TAPE
2247 else if (c->cmd_type == CMD_SCSI)
2248 complete_scsi_command(c, 0, a1);
2256 * See if we can queue up some more IO
2258 blk_start_queue(h->queue);
2259 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2263 * We cannot read the structure directly, for portablity we must use
2265 * This is for debug only.
2268 static void print_cfg_table( CfgTable_struct *tb)
2273 printk("Controller Configuration information\n");
2274 printk("------------------------------------\n");
2276 temp_name[i] = readb(&(tb->Signature[i]));
2278 printk(" Signature = %s\n", temp_name);
2279 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
2280 printk(" Transport methods supported = 0x%x\n",
2281 readl(&(tb-> TransportSupport)));
2282 printk(" Transport methods active = 0x%x\n",
2283 readl(&(tb->TransportActive)));
2284 printk(" Requested transport Method = 0x%x\n",
2285 readl(&(tb->HostWrite.TransportRequest)));
2286 printk(" Coalese Interrupt Delay = 0x%x\n",
2287 readl(&(tb->HostWrite.CoalIntDelay)));
2288 printk(" Coalese Interrupt Count = 0x%x\n",
2289 readl(&(tb->HostWrite.CoalIntCount)));
2290 printk(" Max outstanding commands = 0x%d\n",
2291 readl(&(tb->CmdsOutMax)));
2292 printk(" Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
2294 temp_name[i] = readb(&(tb->ServerName[i]));
2295 temp_name[16] = '\0';
2296 printk(" Server Name = %s\n", temp_name);
2297 printk(" Heartbeat Counter = 0x%x\n\n\n",
2298 readl(&(tb->HeartBeat)));
2300 #endif /* CCISS_DEBUG */
2302 static void release_io_mem(ctlr_info_t *c)
2304 /* if IO mem was not protected do nothing */
2305 if( c->io_mem_addr == 0)
2307 release_region(c->io_mem_addr, c->io_mem_length);
2309 c->io_mem_length = 0;
2312 static int find_PCI_BAR_index(struct pci_dev *pdev,
2313 unsigned long pci_bar_addr)
2315 int i, offset, mem_type, bar_type;
2316 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
2319 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
2320 bar_type = pci_resource_flags(pdev, i) &
2321 PCI_BASE_ADDRESS_SPACE;
2322 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2325 mem_type = pci_resource_flags(pdev, i) &
2326 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
2328 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2329 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2330 offset += 4; /* 32 bit */
2332 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2335 default: /* reserved in PCI 2.2 */
2336 printk(KERN_WARNING "Base address is invalid\n");
2341 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2347 static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2349 ushort subsystem_vendor_id, subsystem_device_id, command;
2350 __u32 board_id, scratchpad = 0;
2352 __u32 cfg_base_addr;
2353 __u64 cfg_base_addr_index;
2356 /* check to see if controller has been disabled */
2357 /* BEFORE trying to enable it */
2358 (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
2359 if(!(command & 0x02))
2361 printk(KERN_WARNING "cciss: controller appears to be disabled\n");
2365 if (pci_enable_device(pdev))
2367 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
2370 if (pci_set_dma_mask(pdev, CCISS_DMA_MASK ) != 0)
2372 printk(KERN_ERR "cciss: Unable to set DMA mask\n");
2376 subsystem_vendor_id = pdev->subsystem_vendor;
2377 subsystem_device_id = pdev->subsystem_device;
2378 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
2379 subsystem_vendor_id);
2381 /* search for our IO range so we can protect it */
2382 for(i=0; i<DEVICE_COUNT_RESOURCE; i++)
2384 /* is this an IO range */
2385 if( pci_resource_flags(pdev, i) & 0x01 ) {
2386 c->io_mem_addr = pci_resource_start(pdev, i);
2387 c->io_mem_length = pci_resource_end(pdev, i) -
2388 pci_resource_start(pdev, i) +1;
2390 printk("IO value found base_addr[%d] %lx %lx\n", i,
2391 c->io_mem_addr, c->io_mem_length);
2392 #endif /* CCISS_DEBUG */
2393 /* register the IO range */
2394 if(!request_region( c->io_mem_addr,
2395 c->io_mem_length, "cciss"))
2397 printk(KERN_WARNING "cciss I/O memory range already in use addr=%lx length=%ld\n",
2398 c->io_mem_addr, c->io_mem_length);
2400 c->io_mem_length = 0;
2407 printk("command = %x\n", command);
2408 printk("irq = %x\n", pdev->irq);
2409 printk("board_id = %x\n", board_id);
2410 #endif /* CCISS_DEBUG */
2412 c->intr = pdev->irq;
2415 * Memory base addr is first addr , the second points to the config
2419 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
2421 printk("address 0 = %x\n", c->paddr);
2422 #endif /* CCISS_DEBUG */
2423 c->vaddr = remap_pci_mem(c->paddr, 200);
2425 /* Wait for the board to become ready. (PCI hotplug needs this.)
2426 * We poll for up to 120 secs, once per 100ms. */
2427 for (i=0; i < 1200; i++) {
2428 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
2429 if (scratchpad == CCISS_FIRMWARE_READY)
2431 set_current_state(TASK_INTERRUPTIBLE);
2432 schedule_timeout(HZ / 10); /* wait 100ms */
2434 if (scratchpad != CCISS_FIRMWARE_READY) {
2435 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
2439 /* get the address index number */
2440 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2441 cfg_base_addr &= (__u32) 0x0000ffff;
2443 printk("cfg base address = %x\n", cfg_base_addr);
2444 #endif /* CCISS_DEBUG */
2445 cfg_base_addr_index =
2446 find_PCI_BAR_index(pdev, cfg_base_addr);
2448 printk("cfg base address index = %x\n", cfg_base_addr_index);
2449 #endif /* CCISS_DEBUG */
2450 if (cfg_base_addr_index == -1) {
2451 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
2456 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2458 printk("cfg offset = %x\n", cfg_offset);
2459 #endif /* CCISS_DEBUG */
2460 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
2461 cfg_base_addr_index) + cfg_offset,
2462 sizeof(CfgTable_struct));
2463 c->board_id = board_id;
2466 print_cfg_table(c->cfgtable);
2467 #endif /* CCISS_DEBUG */
2469 for(i=0; i<NR_PRODUCTS; i++) {
2470 if (board_id == products[i].board_id) {
2471 c->product_name = products[i].product_name;
2472 c->access = *(products[i].access);
2476 if (i == NR_PRODUCTS) {
2477 printk(KERN_WARNING "cciss: Sorry, I don't know how"
2478 " to access the Smart Array controller %08lx\n",
2479 (unsigned long)board_id);
2482 if ( (readb(&c->cfgtable->Signature[0]) != 'C') ||
2483 (readb(&c->cfgtable->Signature[1]) != 'I') ||
2484 (readb(&c->cfgtable->Signature[2]) != 'S') ||
2485 (readb(&c->cfgtable->Signature[3]) != 'S') )
2487 printk("Does not appear to be a valid CISS config table\n");
2493 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
2495 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
2497 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
2502 printk("Trying to put board into Simple mode\n");
2503 #endif /* CCISS_DEBUG */
2504 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
2505 /* Update the field, and then ring the doorbell */
2506 writel( CFGTBL_Trans_Simple,
2507 &(c->cfgtable->HostWrite.TransportRequest));
2508 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
2510 /* under certain very rare conditions, this can take awhile.
2511 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
2512 * as we enter this code.) */
2513 for(i=0;i<MAX_CONFIG_WAIT;i++) {
2514 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
2516 /* delay and try again */
2517 set_current_state(TASK_INTERRUPTIBLE);
2518 schedule_timeout(10);
2522 printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
2523 #endif /* CCISS_DEBUG */
2525 print_cfg_table(c->cfgtable);
2526 #endif /* CCISS_DEBUG */
2528 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
2530 printk(KERN_WARNING "cciss: unable to get board into"
2539 * Gets information about the local volumes attached to the controller.
2541 static void cciss_getgeometry(int cntl_num)
2543 ReportLunData_struct *ld_buff;
2544 ReadCapdata_struct *size_buff;
2545 InquiryData_struct *inq_buff;
2553 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
2554 if (ld_buff == NULL)
2556 printk(KERN_ERR "cciss: out of memory\n");
2559 memset(ld_buff, 0, sizeof(ReportLunData_struct));
2560 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
2561 if (size_buff == NULL)
2563 printk(KERN_ERR "cciss: out of memory\n");
2567 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
2568 if (inq_buff == NULL)
2570 printk(KERN_ERR "cciss: out of memory\n");
2575 /* Get the firmware version */
2576 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
2577 sizeof(InquiryData_struct), 0, 0 ,0, NULL, TYPE_CMD);
2578 if (return_code == IO_OK)
2580 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
2581 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
2582 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
2583 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
2584 } else /* send command failed */
2586 printk(KERN_WARNING "cciss: unable to determine firmware"
2587 " version of controller\n");
2589 /* Get the number of logical volumes */
2590 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
2591 sizeof(ReportLunData_struct), 0, 0, 0, NULL, TYPE_CMD);
2593 if( return_code == IO_OK)
2596 printk("LUN Data\n--------------------------\n");
2597 #endif /* CCISS_DEBUG */
2599 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
2600 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
2601 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
2602 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
2603 } else /* reading number of logical volumes failed */
2605 printk(KERN_WARNING "cciss: report logical volume"
2606 " command failed\n");
2609 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
2610 if (hba[cntl_num]->num_luns > CISS_MAX_LUN)
2612 printk(KERN_ERR "ciss: only %d number of logical volumes supported\n",
2614 hba[cntl_num]->num_luns = CISS_MAX_LUN;
2617 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
2618 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
2619 ld_buff->LUNListLength[3], hba[cntl_num]->num_luns);
2620 #endif /* CCISS_DEBUG */
2622 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
2623 for(i=0; i< hba[cntl_num]->num_luns; i++)
2626 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
2627 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
2628 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
2629 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
2631 hba[cntl_num]->drv[i].LunID = lunid;
2635 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
2636 ld_buff->LUN[i][0], ld_buff->LUN[i][1],ld_buff->LUN[i][2],
2637 ld_buff->LUN[i][3], hba[cntl_num]->drv[i].LunID);
2638 #endif /* CCISS_DEBUG */
2639 cciss_read_capacity(cntl_num, i, size_buff, 0,
2640 &total_size, &block_size);
2641 cciss_geometry_inquiry(cntl_num, i, 0, total_size, block_size,
2642 inq_buff, &hba[cntl_num]->drv[i]);
2649 /* Function to find the first free pointer into our hba[] array */
2650 /* Returns -1 if no free entries are left. */
2651 static int alloc_cciss_hba(void)
2653 struct gendisk *disk[NWD];
2655 for (n = 0; n < NWD; n++) {
2656 disk[n] = alloc_disk(1 << NWD_SHIFT);
2661 for(i=0; i< MAX_CTLR; i++) {
2664 p = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
2667 memset(p, 0, sizeof(ctlr_info_t));
2668 for (n = 0; n < NWD; n++)
2669 p->gendisk[n] = disk[n];
2674 printk(KERN_WARNING "cciss: This driver supports a maximum"
2675 " of 8 controllers.\n");
2678 printk(KERN_ERR "cciss: out of memory.\n");
2685 static void free_hba(int i)
2687 ctlr_info_t *p = hba[i];
2691 for (n = 0; n < NWD; n++)
2692 put_disk(p->gendisk[n]);
2697 * This is it. Find all the controllers and register them. I really hate
2698 * stealing all these major device numbers.
2699 * returns the number of block devices registered.
2701 static int __devinit cciss_init_one(struct pci_dev *pdev,
2702 const struct pci_device_id *ent)
2708 printk(KERN_DEBUG "cciss: Device 0x%x has been found at"
2709 " bus %d dev %d func %d\n",
2710 pdev->device, pdev->bus->number, PCI_SLOT(pdev->devfn),
2711 PCI_FUNC(pdev->devfn));
2712 i = alloc_cciss_hba();
2715 if (cciss_pci_init(hba[i], pdev) != 0)
2718 sprintf(hba[i]->devname, "cciss%d", i);
2720 hba[i]->pdev = pdev;
2722 /* configure PCI DMA stuff */
2723 if (!pci_set_dma_mask(pdev, 0xffffffffffffffffULL))
2724 printk("cciss: using DAC cycles\n");
2725 else if (!pci_set_dma_mask(pdev, 0xffffffff))
2726 printk("cciss: not using DAC cycles\n");
2728 printk("cciss: no suitable DMA available\n");
2732 if (register_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname)) {
2733 printk(KERN_ERR "cciss: Unable to register device %s\n",
2738 /* make sure the board interrupts are off */
2739 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
2740 if( request_irq(hba[i]->intr, do_cciss_intr,
2741 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
2742 hba[i]->devname, hba[i])) {
2743 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
2744 hba[i]->intr, hba[i]->devname);
2747 hba[i]->cmd_pool_bits = kmalloc(((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long), GFP_KERNEL);
2748 hba[i]->cmd_pool = (CommandList_struct *)pci_alloc_consistent(
2749 hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2750 &(hba[i]->cmd_pool_dhandle));
2751 hba[i]->errinfo_pool = (ErrorInfo_struct *)pci_alloc_consistent(
2752 hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2753 &(hba[i]->errinfo_pool_dhandle));
2754 if((hba[i]->cmd_pool_bits == NULL)
2755 || (hba[i]->cmd_pool == NULL)
2756 || (hba[i]->errinfo_pool == NULL)) {
2757 printk( KERN_ERR "cciss: out of memory");
2761 spin_lock_init(&hba[i]->lock);
2762 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
2766 q->backing_dev_info.ra_pages = READ_AHEAD;
2768 q->queuedata = hba[i];
2770 /* Initialize the pdev driver private data.
2771 have it point to hba[i]. */
2772 pci_set_drvdata(pdev, hba[i]);
2773 /* command and error info recs zeroed out before
2775 memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long));
2778 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
2779 #endif /* CCISS_DEBUG */
2781 cciss_getgeometry(i);
2783 cciss_scsi_setup(i);
2785 /* Turn the interrupts on so we can service requests */
2786 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
2790 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
2792 /* This is a hardware imposed limit. */
2793 blk_queue_max_hw_segments(q, MAXSGENTRIES);
2795 /* This is a limit in the driver and could be eliminated. */
2796 blk_queue_max_phys_segments(q, MAXSGENTRIES);
2798 blk_queue_max_sectors(q, 512);
2801 for(j=0; j<NWD; j++) {
2802 drive_info_struct *drv = &(hba[i]->drv[j]);
2803 struct gendisk *disk = hba[i]->gendisk[j];
2805 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
2806 sprintf(disk->devfs_name, "cciss/host%d/target%d", i, j);
2807 disk->major = COMPAQ_CISS_MAJOR + i;
2808 disk->first_minor = j << NWD_SHIFT;
2809 disk->fops = &cciss_fops;
2810 disk->queue = hba[i]->queue;
2811 disk->private_data = drv;
2812 /* we must register the controller even if no disks exist */
2813 /* this is for the online array utilities */
2814 if(!drv->heads && j)
2816 blk_queue_hardsect_size(hba[i]->queue, drv->block_size);
2817 set_capacity(disk, drv->nr_blocks);
2823 if(hba[i]->cmd_pool_bits)
2824 kfree(hba[i]->cmd_pool_bits);
2825 if(hba[i]->cmd_pool)
2826 pci_free_consistent(hba[i]->pdev,
2827 NR_CMDS * sizeof(CommandList_struct),
2828 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2829 if(hba[i]->errinfo_pool)
2830 pci_free_consistent(hba[i]->pdev,
2831 NR_CMDS * sizeof( ErrorInfo_struct),
2832 hba[i]->errinfo_pool,
2833 hba[i]->errinfo_pool_dhandle);
2834 free_irq(hba[i]->intr, hba[i]);
2836 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2838 release_io_mem(hba[i]);
2843 static void __devexit cciss_remove_one (struct pci_dev *pdev)
2845 ctlr_info_t *tmp_ptr;
2850 if (pci_get_drvdata(pdev) == NULL)
2852 printk( KERN_ERR "cciss: Unable to remove device \n");
2855 tmp_ptr = pci_get_drvdata(pdev);
2859 printk(KERN_ERR "cciss: device appears to "
2860 "already be removed \n");
2863 /* Turn board interrupts off and send the flush cache command */
2864 /* sendcmd will turn off interrupt, and send the flush...
2865 * To write all data in the battery backed cache to disks */
2866 memset(flush_buf, 0, 4);
2867 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
2869 if(return_code != IO_OK)
2871 printk(KERN_WARNING "Error Flushing cache on controller %d\n",
2874 free_irq(hba[i]->intr, hba[i]);
2875 pci_set_drvdata(pdev, NULL);
2876 iounmap(hba[i]->vaddr);
2877 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
2878 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2879 remove_proc_entry(hba[i]->devname, proc_cciss);
2881 /* remove it from the disk list */
2882 for (j = 0; j < NWD; j++) {
2883 struct gendisk *disk = hba[i]->gendisk[j];
2884 if (disk->flags & GENHD_FL_UP)
2888 blk_cleanup_queue(hba[i]->queue);
2889 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2890 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2891 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2892 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
2893 kfree(hba[i]->cmd_pool_bits);
2894 release_io_mem(hba[i]);
2898 static struct pci_driver cciss_pci_driver = {
2900 .probe = cciss_init_one,
2901 .remove = __devexit_p(cciss_remove_one),
2902 .id_table = cciss_pci_device_id, /* id_table */
2906 * This is it. Register the PCI driver information for the cards we control
2907 * the OS will call our registered routines when it finds one of our cards.
2909 int __init cciss_init(void)
2911 printk(KERN_INFO DRIVER_NAME "\n");
2913 /* Register for our PCI devices */
2914 return pci_module_init(&cciss_pci_driver);
2917 static int __init init_cciss_module(void)
2919 register_cciss_ioctl32();
2920 return ( cciss_init());
2923 static void __exit cleanup_cciss_module(void)
2927 unregister_cciss_ioctl32();
2928 pci_unregister_driver(&cciss_pci_driver);
2929 /* double check that all controller entrys have been removed */
2930 for (i=0; i< MAX_CTLR; i++)
2934 printk(KERN_WARNING "cciss: had to remove"
2935 " controller %d\n", i);
2936 cciss_remove_one(hba[i]->pdev);
2939 remove_proc_entry("cciss", proc_root_driver);
2942 module_init(init_cciss_module);
2943 module_exit(cleanup_cciss_module);