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 "Compaq CISS Driver (v 2.6.2)"
50 #define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,2)
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.2");
55 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
57 MODULE_LICENSE("GPL");
59 #include "cciss_cmd.h"
61 #include <linux/cciss_ioctl.h>
63 /* define the PCI info for the cards we can control */
64 const struct pci_device_id cciss_pci_device_id[] = {
65 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS,
66 0x0E11, 0x4070, 0, 0, 0},
67 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
68 0x0E11, 0x4080, 0, 0, 0},
69 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
70 0x0E11, 0x4082, 0, 0, 0},
71 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
72 0x0E11, 0x4083, 0, 0, 0},
73 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
74 0x0E11, 0x409A, 0, 0, 0},
75 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
76 0x0E11, 0x409B, 0, 0, 0},
77 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
78 0x0E11, 0x409C, 0, 0, 0},
79 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
80 0x0E11, 0x409D, 0, 0, 0},
81 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
82 0x0E11, 0x4091, 0, 0, 0},
83 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
84 0x0E11, 0x409E, 0, 0, 0},
85 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
86 0x103C, 0x3211, 0, 0, 0},
89 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
91 #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
93 /* board_id = Subsystem Device ID & Vendor ID
94 * product = Marketing Name for the board
95 * access = Address of the struct of function pointers
97 static struct board_type products[] = {
98 { 0x40700E11, "Smart Array 5300", &SA5_access },
99 { 0x40800E11, "Smart Array 5i", &SA5B_access},
100 { 0x40820E11, "Smart Array 532", &SA5B_access},
101 { 0x40830E11, "Smart Array 5312", &SA5B_access},
102 { 0x409A0E11, "Smart Array 641", &SA5_access},
103 { 0x409B0E11, "Smart Array 642", &SA5_access},
104 { 0x409C0E11, "Smart Array 6400", &SA5_access},
105 { 0x409D0E11, "Smart Array 6400 EM", &SA5_access},
106 { 0x40910E11, "Smart Array 6i", &SA5_access},
107 { 0x409E0E11, "Smart Array 6422", &SA5_access},
108 { 0x3211103C, "Smart Array V100", &SA5_access},
111 /* How long to wait (in millesconds) for board to go into simple mode */
112 #define MAX_CONFIG_WAIT 30000
113 #define MAX_IOCTL_CONFIG_WAIT 1000
115 /*define how many times we will try a command because of bus resets */
116 #define MAX_CMD_RETRIES 3
118 #define READ_AHEAD 256
119 #define NR_CMDS 384 /* #commands that can be outstanding */
122 #define CCISS_DMA_MASK 0xFFFFFFFF /* 32 bit DMA */
124 static ctlr_info_t *hba[MAX_CTLR];
126 static void do_cciss_request(request_queue_t *q);
127 static int cciss_open(struct inode *inode, struct file *filep);
128 static int cciss_release(struct inode *inode, struct file *filep);
129 static int cciss_ioctl(struct inode *inode, struct file *filep,
130 unsigned int cmd, unsigned long arg);
132 static int revalidate_allvol(ctlr_info_t *host);
133 static int cciss_revalidate(struct gendisk *disk);
134 static int deregister_disk(struct gendisk *disk);
135 static int register_new_disk(ctlr_info_t *h);
137 static void cciss_getgeometry(int cntl_num);
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,
161 * Enqueuing and dequeuing functions for cmdlists.
163 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
167 c->next = c->prev = c;
169 c->prev = (*Qptr)->prev;
171 (*Qptr)->prev->next = c;
176 static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
177 CommandList_struct *c)
179 if (c && c->next != c) {
180 if (*Qptr == c) *Qptr = c->next;
181 c->prev->next = c->next;
182 c->next->prev = c->prev;
188 #ifdef CONFIG_PROC_FS
190 #include "cciss_scsi.c" /* For SCSI tape support */
193 * Report information about this controller.
195 #define ENG_GIG 1048576000
196 #define ENG_GIG_FACTOR (ENG_GIG/512)
197 #define RAID_UNKNOWN 6
198 static const char *raid_label[] = {"0","4","1(0+1)","5","5+1","ADG",
201 static struct proc_dir_entry *proc_cciss;
203 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
204 int length, int *eof, void *data)
209 ctlr_info_t *h = (ctlr_info_t*)data;
210 drive_info_struct *drv;
212 sector_t vol_sz, vol_sz_frac;
216 /* prevent displaying bogus info during configuration
217 * or deconfiguration of a logical volume
219 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
220 if (h->busy_configuring) {
221 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
224 h->busy_configuring = 1;
225 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
227 size = sprintf(buffer, "%s: HP %s Controller\n"
228 "Board ID: 0x%08lx\n"
229 "Firmware Version: %c%c%c%c\n"
231 "Logical drives: %d\n"
232 "Current Q depth: %d\n"
233 "Current # commands on controller: %d\n"
234 "Max Q depth since init: %d\n"
235 "Max # commands on controller since init: %d\n"
236 "Max SG entries since init: %d\n\n",
239 (unsigned long)h->board_id,
240 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
241 (unsigned int)h->intr,
243 h->Qdepth, h->commands_outstanding,
244 h->maxQsinceinit, h->max_outstanding, h->maxSG);
246 pos += size; len += size;
247 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
248 for(i=0; i<=h->highest_lun; i++) {
252 if (drv->block_size == 0)
254 vol_sz = drv->nr_blocks;
255 sector_div(vol_sz, ENG_GIG_FACTOR);
260 * (drv->nr_blocks%ENG_GIG_FACTOR)*100/ENG_GIG_FACTOR;
262 tmp = drv->nr_blocks;
263 vol_sz_frac = sector_div(tmp, ENG_GIG_FACTOR);
265 /* Now, vol_sz_frac = (drv->nr_blocks%ENG_GIG_FACTOR) */
268 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
270 if (drv->raid_level > 5)
271 drv->raid_level = RAID_UNKNOWN;
272 size = sprintf(buffer+len, "cciss/c%dd%d:"
273 "\t%4d.%02dGB\tRAID %s\n",
274 ctlr, i, vol_sz,vol_sz_frac,
275 raid_label[drv->raid_level]);
276 pos += size; len += size;
280 *start = buffer+offset;
284 h->busy_configuring = 0;
289 cciss_proc_write(struct file *file, const char __user *buffer,
290 unsigned long count, void *data)
292 unsigned char cmd[80];
294 #ifdef CONFIG_CISS_SCSI_TAPE
295 ctlr_info_t *h = (ctlr_info_t *) data;
299 if (count > sizeof(cmd)-1) return -EINVAL;
300 if (copy_from_user(cmd, buffer, count)) return -EFAULT;
302 len = strlen(cmd); // above 3 lines ensure safety
303 if (cmd[len-1] == '\n')
305 # ifdef CONFIG_CISS_SCSI_TAPE
306 if (strcmp("engage scsi", cmd)==0) {
307 rc = cciss_engage_scsi(h->ctlr);
308 if (rc != 0) return -rc;
311 /* might be nice to have "disengage" too, but it's not
312 safely possible. (only 1 module use count, lock issues.) */
318 * Get us a file in /proc/cciss that says something about each controller.
319 * Create /proc/cciss if it doesn't exist yet.
321 static void __devinit cciss_procinit(int i)
323 struct proc_dir_entry *pde;
325 if (proc_cciss == NULL) {
326 proc_cciss = proc_mkdir("cciss", proc_root_driver);
331 pde = create_proc_read_entry(hba[i]->devname,
332 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
333 proc_cciss, cciss_proc_get_info, hba[i]);
334 pde->write_proc = cciss_proc_write;
336 #endif /* CONFIG_PROC_FS */
339 * For operations that cannot sleep, a command block is allocated at init,
340 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
341 * which ones are free or in use. For operations that can wait for kmalloc
342 * to possible sleep, this routine can be called with get_from_pool set to 0.
343 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
345 static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
347 CommandList_struct *c;
350 dma_addr_t cmd_dma_handle, err_dma_handle;
354 c = (CommandList_struct *) pci_alloc_consistent(
355 h->pdev, sizeof(CommandList_struct), &cmd_dma_handle);
358 memset(c, 0, sizeof(CommandList_struct));
360 c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
361 h->pdev, sizeof(ErrorInfo_struct),
364 if (c->err_info == NULL)
366 pci_free_consistent(h->pdev,
367 sizeof(CommandList_struct), c, cmd_dma_handle);
370 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
371 } else /* get it out of the controllers pool */
374 i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
377 } while(test_and_set_bit(i & (BITS_PER_LONG - 1), h->cmd_pool_bits+(i/BITS_PER_LONG)) != 0);
379 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
382 memset(c, 0, sizeof(CommandList_struct));
383 cmd_dma_handle = h->cmd_pool_dhandle
384 + i*sizeof(CommandList_struct);
385 c->err_info = h->errinfo_pool + i;
386 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
387 err_dma_handle = h->errinfo_pool_dhandle
388 + i*sizeof(ErrorInfo_struct);
392 c->busaddr = (__u32) cmd_dma_handle;
393 temp64.val = (__u64) err_dma_handle;
394 c->ErrDesc.Addr.lower = temp64.val32.lower;
395 c->ErrDesc.Addr.upper = temp64.val32.upper;
396 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
405 * Frees a command block that was previously allocated with cmd_alloc().
407 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
414 temp64.val32.lower = c->ErrDesc.Addr.lower;
415 temp64.val32.upper = c->ErrDesc.Addr.upper;
416 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
417 c->err_info, (dma_addr_t) temp64.val);
418 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
419 c, (dma_addr_t) c->busaddr);
423 clear_bit(i&(BITS_PER_LONG-1), h->cmd_pool_bits+(i/BITS_PER_LONG));
428 static inline ctlr_info_t *get_host(struct gendisk *disk)
430 return disk->queue->queuedata;
433 static inline drive_info_struct *get_drv(struct gendisk *disk)
435 return disk->private_data;
439 * Open. Make sure the device is really there.
441 static int cciss_open(struct inode *inode, struct file *filep)
443 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
444 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
447 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
448 #endif /* CCISS_DEBUG */
451 * Root is allowed to open raw volume zero even if it's not configured
452 * so array config can still work. I don't think I really like this,
453 * but I'm already using way to many device nodes to claim another one
454 * for "raw controller".
456 if (drv->nr_blocks == 0) {
457 if (iminor(inode) != 0)
459 if (!capable(CAP_SYS_ADMIN))
469 static int cciss_release(struct inode *inode, struct file *filep)
471 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
472 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
475 printk(KERN_DEBUG "cciss_release %s\n", inode->i_bdev->bd_disk->disk_name);
476 #endif /* CCISS_DEBUG */
484 /* for AMD 64 bit kernel compatibility with 32-bit userland ioctls */
485 extern long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
487 register_ioctl32_conversion(unsigned int cmd, int (*handler)(unsigned int,
488 unsigned int, unsigned long, struct file *));
489 extern int unregister_ioctl32_conversion(unsigned int cmd);
491 static int cciss_ioctl32_passthru(unsigned int fd, unsigned cmd, unsigned long arg, struct file *file);
492 static int cciss_ioctl32_big_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
495 typedef int (*handler_type) (unsigned int, unsigned int, unsigned long, struct file *);
497 static struct ioctl32_map {
499 handler_type handler;
501 } cciss_ioctl32_map[] = {
502 { CCISS_GETPCIINFO, (handler_type) sys_ioctl, 0 },
503 { CCISS_GETINTINFO, (handler_type) sys_ioctl, 0 },
504 { CCISS_SETINTINFO, (handler_type) sys_ioctl, 0 },
505 { CCISS_GETNODENAME, (handler_type) sys_ioctl, 0 },
506 { CCISS_SETNODENAME, (handler_type) sys_ioctl, 0 },
507 { CCISS_GETHEARTBEAT, (handler_type) sys_ioctl, 0 },
508 { CCISS_GETBUSTYPES, (handler_type) sys_ioctl, 0 },
509 { CCISS_GETFIRMVER, (handler_type) sys_ioctl, 0 },
510 { CCISS_GETDRIVVER, (handler_type) sys_ioctl, 0 },
511 { CCISS_REVALIDVOLS, (handler_type) sys_ioctl, 0 },
512 { CCISS_PASSTHRU32, cciss_ioctl32_passthru, 0 },
513 { CCISS_DEREGDISK, (handler_type) sys_ioctl, 0 },
514 { CCISS_REGNEWDISK, (handler_type) sys_ioctl, 0 },
515 { CCISS_REGNEWD, (handler_type) sys_ioctl, 0 },
516 { CCISS_RESCANDISK, (handler_type) sys_ioctl, 0 },
517 { CCISS_GETLUNINFO, (handler_type) sys_ioctl, 0 },
518 { CCISS_BIG_PASSTHRU32, cciss_ioctl32_big_passthru, 0 },
520 #define NCCISS_IOCTL32_ENTRIES (sizeof(cciss_ioctl32_map) / sizeof(cciss_ioctl32_map[0]))
521 static void register_cciss_ioctl32(void)
525 for (i=0; i < NCCISS_IOCTL32_ENTRIES; i++) {
526 rc = register_ioctl32_conversion(
527 cciss_ioctl32_map[i].cmd,
528 cciss_ioctl32_map[i].handler);
530 printk(KERN_WARNING "cciss: failed to register "
531 "32 bit compatible ioctl 0x%08x\n",
532 cciss_ioctl32_map[i].cmd);
533 cciss_ioctl32_map[i].registered = 0;
535 cciss_ioctl32_map[i].registered = 1;
538 static void unregister_cciss_ioctl32(void)
542 for (i=0; i < NCCISS_IOCTL32_ENTRIES; i++) {
543 if (!cciss_ioctl32_map[i].registered)
545 rc = unregister_ioctl32_conversion(
546 cciss_ioctl32_map[i].cmd);
548 cciss_ioctl32_map[i].registered = 0;
551 printk(KERN_WARNING "cciss: failed to unregister "
552 "32 bit compatible ioctl 0x%08x\n",
553 cciss_ioctl32_map[i].cmd);
556 int cciss_ioctl32_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
559 IOCTL32_Command_struct __user *arg32 =
560 (IOCTL32_Command_struct __user *) arg;
561 IOCTL_Command_struct arg64;
562 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
567 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
568 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
569 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
570 err |= get_user(arg64.buf_size, &arg32->buf_size);
571 err |= get_user(cp, &arg32->buf);
572 arg64.buf = compat_ptr(cp);
573 err |= copy_to_user(p, &arg64, sizeof(arg64));
578 err = sys_ioctl(fd, CCISS_PASSTHRU, (unsigned long) p);
581 err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(&arg32->error_info));
587 int cciss_ioctl32_big_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
590 BIG_IOCTL32_Command_struct __user *arg32 =
591 (BIG_IOCTL32_Command_struct __user *) arg;
592 BIG_IOCTL_Command_struct arg64;
593 BIG_IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
598 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
599 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
600 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
601 err |= get_user(arg64.buf_size, &arg32->buf_size);
602 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
603 err |= get_user(cp, &arg32->buf);
604 arg64.buf = compat_ptr(cp);
605 err |= copy_to_user(p, &arg64, sizeof(arg64));
610 err = sys_ioctl(fd, CCISS_BIG_PASSTHRU, (unsigned long) p);
613 err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(&arg32->error_info));
619 static inline void register_cciss_ioctl32(void) {}
620 static inline void unregister_cciss_ioctl32(void) {}
625 static int cciss_ioctl(struct inode *inode, struct file *filep,
626 unsigned int cmd, unsigned long arg)
628 struct block_device *bdev = inode->i_bdev;
629 struct gendisk *disk = bdev->bd_disk;
630 ctlr_info_t *host = get_host(disk);
631 drive_info_struct *drv = get_drv(disk);
632 int ctlr = host->ctlr;
633 void __user *argp = (void __user *)arg;
636 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
637 #endif /* CCISS_DEBUG */
642 struct hd_geometry driver_geo;
643 if (drv->cylinders) {
644 driver_geo.heads = drv->heads;
645 driver_geo.sectors = drv->sectors;
646 driver_geo.cylinders = drv->cylinders;
649 driver_geo.start= get_start_sect(inode->i_bdev);
650 if (copy_to_user(argp, &driver_geo, sizeof(struct hd_geometry)))
655 case CCISS_GETPCIINFO:
657 cciss_pci_info_struct pciinfo;
659 if (!arg) return -EINVAL;
660 pciinfo.bus = host->pdev->bus->number;
661 pciinfo.dev_fn = host->pdev->devfn;
662 pciinfo.board_id = host->board_id;
663 if (copy_to_user(argp, &pciinfo, sizeof( cciss_pci_info_struct )))
667 case CCISS_GETINTINFO:
669 cciss_coalint_struct intinfo;
670 if (!arg) return -EINVAL;
671 intinfo.delay = readl(&host->cfgtable->HostWrite.CoalIntDelay);
672 intinfo.count = readl(&host->cfgtable->HostWrite.CoalIntCount);
673 if (copy_to_user(argp, &intinfo, sizeof( cciss_coalint_struct )))
677 case CCISS_SETINTINFO:
679 cciss_coalint_struct intinfo;
683 if (!arg) return -EINVAL;
684 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
685 if (copy_from_user(&intinfo, argp, sizeof( cciss_coalint_struct)))
687 if ( (intinfo.delay == 0 ) && (intinfo.count == 0))
690 // printk("cciss_ioctl: delay and count cannot be 0\n");
693 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
694 /* Update the field, and then ring the doorbell */
695 writel( intinfo.delay,
696 &(host->cfgtable->HostWrite.CoalIntDelay));
697 writel( intinfo.count,
698 &(host->cfgtable->HostWrite.CoalIntCount));
699 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
701 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
702 if (!(readl(host->vaddr + SA5_DOORBELL)
705 /* delay and try again */
708 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
709 if (i >= MAX_IOCTL_CONFIG_WAIT)
713 case CCISS_GETNODENAME:
715 NodeName_type NodeName;
718 if (!arg) return -EINVAL;
720 NodeName[i] = readb(&host->cfgtable->ServerName[i]);
721 if (copy_to_user(argp, NodeName, sizeof( NodeName_type)))
725 case CCISS_SETNODENAME:
727 NodeName_type NodeName;
731 if (!arg) return -EINVAL;
732 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
734 if (copy_from_user(NodeName, argp, sizeof( NodeName_type)))
737 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
739 /* Update the field, and then ring the doorbell */
741 writeb( NodeName[i], &host->cfgtable->ServerName[i]);
743 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
745 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
746 if (!(readl(host->vaddr + SA5_DOORBELL)
749 /* delay and try again */
752 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
753 if (i >= MAX_IOCTL_CONFIG_WAIT)
758 case CCISS_GETHEARTBEAT:
760 Heartbeat_type heartbeat;
762 if (!arg) return -EINVAL;
763 heartbeat = readl(&host->cfgtable->HeartBeat);
764 if (copy_to_user(argp, &heartbeat, sizeof( Heartbeat_type)))
768 case CCISS_GETBUSTYPES:
770 BusTypes_type BusTypes;
772 if (!arg) return -EINVAL;
773 BusTypes = readl(&host->cfgtable->BusTypes);
774 if (copy_to_user(argp, &BusTypes, sizeof( BusTypes_type) ))
778 case CCISS_GETFIRMVER:
780 FirmwareVer_type firmware;
782 if (!arg) return -EINVAL;
783 memcpy(firmware, host->firm_ver, 4);
785 if (copy_to_user(argp, firmware, sizeof( FirmwareVer_type)))
789 case CCISS_GETDRIVVER:
791 DriverVer_type DriverVer = DRIVER_VERSION;
793 if (!arg) return -EINVAL;
795 if (copy_to_user(argp, &DriverVer, sizeof( DriverVer_type) ))
800 case CCISS_REVALIDVOLS:
801 if (bdev != bdev->bd_contains || drv != host->drv)
803 return revalidate_allvol(host);
805 case CCISS_GETLUNINFO: {
806 LogvolInfo_struct luninfo;
809 luninfo.LunID = drv->LunID;
810 luninfo.num_opens = drv->usage_count;
811 luninfo.num_parts = 0;
812 /* count partitions 1 to 15 with sizes > 0 */
813 for(i=1; i <MAX_PART; i++) {
816 if (disk->part[i]->nr_sects != 0)
819 if (copy_to_user(argp, &luninfo,
820 sizeof(LogvolInfo_struct)))
824 case CCISS_DEREGDISK:
825 return deregister_disk(disk);
828 return register_new_disk(host);
832 IOCTL_Command_struct iocommand;
833 CommandList_struct *c;
837 DECLARE_COMPLETION(wait);
839 if (!arg) return -EINVAL;
841 if (!capable(CAP_SYS_RAWIO)) return -EPERM;
843 if (copy_from_user(&iocommand, argp, sizeof( IOCTL_Command_struct) ))
845 if((iocommand.buf_size < 1) &&
846 (iocommand.Request.Type.Direction != XFER_NONE))
850 #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
851 /* Check kmalloc limits */
852 if(iocommand.buf_size > 128000)
855 if(iocommand.buf_size > 0)
857 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
861 if (iocommand.Request.Type.Direction == XFER_WRITE)
863 /* Copy the data into the buffer we created */
864 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
870 if ((c = cmd_alloc(host , 0)) == NULL)
875 // Fill in the command type
876 c->cmd_type = CMD_IOCTL_PEND;
877 // Fill in Command Header
878 c->Header.ReplyQueue = 0; // unused in simple mode
879 if( iocommand.buf_size > 0) // buffer to fill
881 c->Header.SGList = 1;
882 c->Header.SGTotal= 1;
883 } else // no buffers to fill
885 c->Header.SGList = 0;
886 c->Header.SGTotal= 0;
888 c->Header.LUN = iocommand.LUN_info;
889 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
891 // Fill in Request block
892 c->Request = iocommand.Request;
894 // Fill in the scatter gather information
895 if (iocommand.buf_size > 0 )
897 temp64.val = pci_map_single( host->pdev, buff,
899 PCI_DMA_BIDIRECTIONAL);
900 c->SG[0].Addr.lower = temp64.val32.lower;
901 c->SG[0].Addr.upper = temp64.val32.upper;
902 c->SG[0].Len = iocommand.buf_size;
903 c->SG[0].Ext = 0; // we are not chaining
907 /* Put the request on the tail of the request queue */
908 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
909 addQ(&host->reqQ, c);
912 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
914 wait_for_completion(&wait);
916 /* unlock the buffers from DMA */
917 temp64.val32.lower = c->SG[0].Addr.lower;
918 temp64.val32.upper = c->SG[0].Addr.upper;
919 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
920 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
922 /* Copy the error information out */
923 iocommand.error_info = *(c->err_info);
924 if ( copy_to_user(argp, &iocommand, sizeof( IOCTL_Command_struct) ) )
927 cmd_free(host, c, 0);
931 if (iocommand.Request.Type.Direction == XFER_READ)
933 /* Copy the data out of the buffer we created */
934 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size))
937 cmd_free(host, c, 0);
942 cmd_free(host, c, 0);
945 case CCISS_BIG_PASSTHRU: {
946 BIG_IOCTL_Command_struct *ioc;
947 CommandList_struct *c;
948 unsigned char **buff = NULL;
949 int *buff_size = NULL;
955 DECLARE_COMPLETION(wait);
958 BYTE __user *data_ptr;
962 if (!capable(CAP_SYS_RAWIO))
964 ioc = (BIG_IOCTL_Command_struct *)
965 kmalloc(sizeof(*ioc), GFP_KERNEL);
970 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
974 if ((ioc->buf_size < 1) &&
975 (ioc->Request.Type.Direction != XFER_NONE)) {
979 /* Check kmalloc limits using all SGs */
980 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
984 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
988 buff = (unsigned char **) kmalloc(MAXSGENTRIES *
989 sizeof(char *), GFP_KERNEL);
994 memset(buff, 0, MAXSGENTRIES);
995 buff_size = (int *) kmalloc(MAXSGENTRIES * sizeof(int),
1001 left = ioc->buf_size;
1002 data_ptr = ioc->buf;
1004 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
1005 buff_size[sg_used] = sz;
1006 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1007 if (buff[sg_used] == NULL) {
1011 if (ioc->Request.Type.Direction == XFER_WRITE &&
1012 copy_from_user(buff[sg_used], data_ptr, sz)) {
1020 if ((c = cmd_alloc(host , 0)) == NULL) {
1024 c->cmd_type = CMD_IOCTL_PEND;
1025 c->Header.ReplyQueue = 0;
1027 if( ioc->buf_size > 0) {
1028 c->Header.SGList = sg_used;
1029 c->Header.SGTotal= sg_used;
1031 c->Header.SGList = 0;
1032 c->Header.SGTotal= 0;
1034 c->Header.LUN = ioc->LUN_info;
1035 c->Header.Tag.lower = c->busaddr;
1037 c->Request = ioc->Request;
1038 if (ioc->buf_size > 0 ) {
1040 for(i=0; i<sg_used; i++) {
1041 temp64.val = pci_map_single( host->pdev, buff[i],
1043 PCI_DMA_BIDIRECTIONAL);
1044 c->SG[i].Addr.lower = temp64.val32.lower;
1045 c->SG[i].Addr.upper = temp64.val32.upper;
1046 c->SG[i].Len = buff_size[i];
1047 c->SG[i].Ext = 0; /* we are not chaining */
1051 /* Put the request on the tail of the request queue */
1052 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1053 addQ(&host->reqQ, c);
1056 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1057 wait_for_completion(&wait);
1058 /* unlock the buffers from DMA */
1059 for(i=0; i<sg_used; i++) {
1060 temp64.val32.lower = c->SG[i].Addr.lower;
1061 temp64.val32.upper = c->SG[i].Addr.upper;
1062 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
1063 buff_size[i], PCI_DMA_BIDIRECTIONAL);
1065 /* Copy the error information out */
1066 ioc->error_info = *(c->err_info);
1067 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
1068 cmd_free(host, c, 0);
1072 if (ioc->Request.Type.Direction == XFER_READ) {
1073 /* Copy the data out of the buffer we created */
1074 BYTE __user *ptr = ioc->buf;
1075 for(i=0; i< sg_used; i++) {
1076 if (copy_to_user(ptr, buff[i], buff_size[i])) {
1077 cmd_free(host, c, 0);
1081 ptr += buff_size[i];
1084 cmd_free(host, c, 0);
1088 for(i=0; i<sg_used; i++)
1105 static int cciss_revalidate(struct gendisk *disk)
1107 drive_info_struct *drv = disk->private_data;
1108 set_capacity(disk, drv->nr_blocks);
1113 * revalidate_allvol is for online array config utilities. After a
1114 * utility reconfigures the drives in the array, it can use this function
1115 * (through an ioctl) to make the driver zap any previous disk structs for
1116 * that controller and get new ones.
1118 * Right now I'm using the getgeometry() function to do this, but this
1119 * function should probably be finer grained and allow you to revalidate one
1120 * particualar logical volume (instead of all of them on a particular
1123 static int revalidate_allvol(ctlr_info_t *host)
1125 int ctlr = host->ctlr, i;
1126 unsigned long flags;
1128 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1129 if (host->usage_count > 1) {
1130 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1131 printk(KERN_WARNING "cciss: Device busy for volume"
1132 " revalidation (usage=%d)\n", host->usage_count);
1135 host->usage_count++;
1136 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1138 for(i=0; i< NWD; i++) {
1139 struct gendisk *disk = host->gendisk[i];
1140 if (disk->flags & GENHD_FL_UP)
1145 * Set the partition and block size structures for all volumes
1146 * on this controller to zero. We will reread all of this data
1148 memset(host->drv, 0, sizeof(drive_info_struct)
1151 * Tell the array controller not to give us any interrupts while
1152 * we check the new geometry. Then turn interrupts back on when
1155 host->access.set_intr_mask(host, CCISS_INTR_OFF);
1156 cciss_getgeometry(ctlr);
1157 host->access.set_intr_mask(host, CCISS_INTR_ON);
1159 /* Loop through each real device */
1160 for (i = 0; i < NWD; i++) {
1161 struct gendisk *disk = host->gendisk[i];
1162 drive_info_struct *drv = &(host->drv[i]);
1163 if (!drv->nr_blocks)
1165 blk_queue_hardsect_size(host->queue, drv->block_size);
1166 set_capacity(disk, drv->nr_blocks);
1169 host->usage_count--;
1173 static int deregister_disk(struct gendisk *disk)
1175 unsigned long flags;
1176 ctlr_info_t *h = get_host(disk);
1177 drive_info_struct *drv = get_drv(disk);
1180 if (!capable(CAP_SYS_RAWIO))
1183 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1184 /* make sure logical volume is NOT is use */
1185 if( drv->usage_count > 1) {
1186 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1190 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1192 /* invalidate the devices and deregister the disk */
1193 if (disk->flags & GENHD_FL_UP)
1195 /* check to see if it was the last disk */
1196 if (drv == h->drv + h->highest_lun) {
1197 /* if so, find the new hightest lun */
1198 int i, newhighest =-1;
1199 for(i=0; i<h->highest_lun; i++) {
1200 /* if the disk has size > 0, it is available */
1201 if (h->drv[i].nr_blocks)
1204 h->highest_lun = newhighest;
1208 /* zero out the disk size info */
1210 drv->block_size = 0;
1215 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
1217 unsigned int use_unit_num, /* 0: address the controller,
1218 1: address logical volume log_unit,
1219 2: periph device address is scsi3addr */
1220 unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr,
1223 ctlr_info_t *h= hba[ctlr];
1224 u64bit buff_dma_handle;
1227 c->cmd_type = CMD_IOCTL_PEND;
1228 c->Header.ReplyQueue = 0;
1230 c->Header.SGList = 1;
1231 c->Header.SGTotal= 1;
1233 c->Header.SGList = 0;
1234 c->Header.SGTotal= 0;
1236 c->Header.Tag.lower = c->busaddr;
1238 c->Request.Type.Type = cmd_type;
1239 if (cmd_type == TYPE_CMD) {
1242 /* If the logical unit number is 0 then, this is going
1243 to controller so It's a physical command
1244 mode = 0 target = 0. So we have nothing to write.
1245 otherwise, if use_unit_num == 1,
1246 mode = 1(volume set addressing) target = LUNID
1247 otherwise, if use_unit_num == 2,
1248 mode = 0(periph dev addr) target = scsi3addr */
1249 if (use_unit_num == 1) {
1250 c->Header.LUN.LogDev.VolId=
1251 h->drv[log_unit].LunID;
1252 c->Header.LUN.LogDev.Mode = 1;
1253 } else if (use_unit_num == 2) {
1254 memcpy(c->Header.LUN.LunAddrBytes,scsi3addr,8);
1255 c->Header.LUN.LogDev.Mode = 0;
1257 /* are we trying to read a vital product page */
1258 if(page_code != 0) {
1259 c->Request.CDB[1] = 0x01;
1260 c->Request.CDB[2] = page_code;
1262 c->Request.CDBLen = 6;
1263 c->Request.Type.Attribute = ATTR_SIMPLE;
1264 c->Request.Type.Direction = XFER_READ;
1265 c->Request.Timeout = 0;
1266 c->Request.CDB[0] = CISS_INQUIRY;
1267 c->Request.CDB[4] = size & 0xFF;
1269 case CISS_REPORT_LOG:
1270 case CISS_REPORT_PHYS:
1271 /* Talking to controller so It's a physical command
1272 mode = 00 target = 0. Nothing to write.
1274 c->Request.CDBLen = 12;
1275 c->Request.Type.Attribute = ATTR_SIMPLE;
1276 c->Request.Type.Direction = XFER_READ;
1277 c->Request.Timeout = 0;
1278 c->Request.CDB[0] = cmd;
1279 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
1280 c->Request.CDB[7] = (size >> 16) & 0xFF;
1281 c->Request.CDB[8] = (size >> 8) & 0xFF;
1282 c->Request.CDB[9] = size & 0xFF;
1285 case CCISS_READ_CAPACITY:
1286 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1287 c->Header.LUN.LogDev.Mode = 1;
1288 c->Request.CDBLen = 10;
1289 c->Request.Type.Attribute = ATTR_SIMPLE;
1290 c->Request.Type.Direction = XFER_READ;
1291 c->Request.Timeout = 0;
1292 c->Request.CDB[0] = cmd;
1294 case CCISS_CACHE_FLUSH:
1295 c->Request.CDBLen = 12;
1296 c->Request.Type.Attribute = ATTR_SIMPLE;
1297 c->Request.Type.Direction = XFER_WRITE;
1298 c->Request.Timeout = 0;
1299 c->Request.CDB[0] = BMIC_WRITE;
1300 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
1304 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
1307 } else if (cmd_type == TYPE_MSG) {
1309 case 3: /* No-Op message */
1310 c->Request.CDBLen = 1;
1311 c->Request.Type.Attribute = ATTR_SIMPLE;
1312 c->Request.Type.Direction = XFER_WRITE;
1313 c->Request.Timeout = 0;
1314 c->Request.CDB[0] = cmd;
1318 "cciss%d: unknown message type %d\n",
1324 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
1327 /* Fill in the scatter gather information */
1329 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
1330 buff, size, PCI_DMA_BIDIRECTIONAL);
1331 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1332 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1333 c->SG[0].Len = size;
1334 c->SG[0].Ext = 0; /* we are not chaining */
1338 static int sendcmd_withirq(__u8 cmd,
1342 unsigned int use_unit_num,
1343 unsigned int log_unit,
1347 ctlr_info_t *h = hba[ctlr];
1348 CommandList_struct *c;
1349 u64bit buff_dma_handle;
1350 unsigned long flags;
1352 DECLARE_COMPLETION(wait);
1354 if ((c = cmd_alloc(h , 0)) == NULL)
1356 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1357 log_unit, page_code, NULL, cmd_type);
1358 if (return_status != IO_OK) {
1360 return return_status;
1365 /* Put the request on the tail of the queue and send it */
1366 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1370 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1372 wait_for_completion(&wait);
1374 if(c->err_info->CommandStatus != 0)
1375 { /* an error has occurred */
1376 switch(c->err_info->CommandStatus)
1378 case CMD_TARGET_STATUS:
1379 printk(KERN_WARNING "cciss: cmd %p has "
1380 " completed with errors\n", c);
1381 if( c->err_info->ScsiStatus)
1383 printk(KERN_WARNING "cciss: cmd %p "
1384 "has SCSI Status = %x\n",
1386 c->err_info->ScsiStatus);
1390 case CMD_DATA_UNDERRUN:
1391 case CMD_DATA_OVERRUN:
1392 /* expected for inquire and report lun commands */
1395 printk(KERN_WARNING "cciss: Cmd %p is "
1396 "reported invalid\n", c);
1397 return_status = IO_ERROR;
1399 case CMD_PROTOCOL_ERR:
1400 printk(KERN_WARNING "cciss: cmd %p has "
1401 "protocol error \n", c);
1402 return_status = IO_ERROR;
1404 case CMD_HARDWARE_ERR:
1405 printk(KERN_WARNING "cciss: cmd %p had "
1406 " hardware error\n", c);
1407 return_status = IO_ERROR;
1409 case CMD_CONNECTION_LOST:
1410 printk(KERN_WARNING "cciss: cmd %p had "
1411 "connection lost\n", c);
1412 return_status = IO_ERROR;
1415 printk(KERN_WARNING "cciss: cmd %p was "
1417 return_status = IO_ERROR;
1419 case CMD_ABORT_FAILED:
1420 printk(KERN_WARNING "cciss: cmd %p reports "
1421 "abort failed\n", c);
1422 return_status = IO_ERROR;
1424 case CMD_UNSOLICITED_ABORT:
1426 "cciss%d: unsolicited abort %p\n",
1428 if (c->retry_count < MAX_CMD_RETRIES) {
1430 "cciss%d: retrying %p\n",
1433 /* erase the old error information */
1434 memset(c->err_info, 0,
1435 sizeof(ErrorInfo_struct));
1436 return_status = IO_OK;
1437 INIT_COMPLETION(wait);
1440 return_status = IO_ERROR;
1443 printk(KERN_WARNING "cciss: cmd %p returned "
1444 "unknown status %x\n", c,
1445 c->err_info->CommandStatus);
1446 return_status = IO_ERROR;
1449 /* unlock the buffers from DMA */
1450 pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
1451 size, PCI_DMA_BIDIRECTIONAL);
1453 return(return_status);
1456 static void cciss_geometry_inquiry(int ctlr, int logvol,
1457 int withirq, unsigned int total_size,
1458 unsigned int block_size, InquiryData_struct *inq_buff,
1459 drive_info_struct *drv)
1462 memset(inq_buff, 0, sizeof(InquiryData_struct));
1464 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
1465 inq_buff, sizeof(*inq_buff), 1, logvol ,0xC1, TYPE_CMD);
1467 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
1468 sizeof(*inq_buff), 1, logvol ,0xC1, NULL, TYPE_CMD);
1469 if (return_code == IO_OK) {
1470 if(inq_buff->data_byte[8] == 0xFF) {
1472 "cciss: reading geometry failed, volume "
1473 "does not support reading geometry\n");
1474 drv->block_size = block_size;
1475 drv->nr_blocks = total_size;
1477 drv->sectors = 32; // Sectors per track
1478 drv->cylinders = total_size / 255 / 32;
1480 drv->block_size = block_size;
1481 drv->nr_blocks = total_size;
1482 drv->heads = inq_buff->data_byte[6];
1483 drv->sectors = inq_buff->data_byte[7];
1484 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1485 drv->cylinders += inq_buff->data_byte[5];
1487 } else { /* Get geometry failed */
1488 printk(KERN_WARNING "cciss: reading geometry failed, "
1489 "continuing with default geometry\n");
1490 drv->block_size = block_size;
1491 drv->nr_blocks = total_size;
1493 drv->sectors = 32; // Sectors per track
1494 drv->cylinders = total_size / 255 / 32;
1496 printk(KERN_INFO " heads= %d, sectors= %d, cylinders= %d\n\n",
1497 drv->heads, drv->sectors, drv->cylinders);
1500 cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
1501 int withirq, unsigned int *total_size, unsigned int *block_size)
1504 memset(buf, 0, sizeof(*buf));
1506 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
1507 ctlr, buf, sizeof(*buf), 1, logvol, 0, TYPE_CMD);
1509 return_code = sendcmd(CCISS_READ_CAPACITY,
1510 ctlr, buf, sizeof(*buf), 1, logvol, 0, NULL, TYPE_CMD);
1511 if (return_code == IO_OK) {
1512 *total_size = be32_to_cpu(*((__u32 *) &buf->total_size[0]))+1;
1513 *block_size = be32_to_cpu(*((__u32 *) &buf->block_size[0]));
1514 } else { /* read capacity command failed */
1515 printk(KERN_WARNING "cciss: read capacity failed\n");
1517 *block_size = BLOCK_SIZE;
1519 printk(KERN_INFO " blocks= %u block_size= %d\n",
1520 *total_size, *block_size);
1523 static int register_new_disk(ctlr_info_t *h)
1525 struct gendisk *disk;
1530 int new_lun_found = 0;
1531 int new_lun_index = 0;
1532 int free_index_found = 0;
1534 ReportLunData_struct *ld_buff = NULL;
1535 ReadCapdata_struct *size_buff = NULL;
1536 InquiryData_struct *inq_buff = NULL;
1540 unsigned int block_size;
1541 unsigned int total_size;
1543 if (!capable(CAP_SYS_RAWIO))
1545 /* if we have no space in our disk array left to add anything */
1546 if( h->num_luns >= CISS_MAX_LUN)
1549 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1550 if (ld_buff == NULL)
1552 memset(ld_buff, 0, sizeof(ReportLunData_struct));
1553 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1554 if (size_buff == NULL)
1556 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1557 if (inq_buff == NULL)
1560 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
1561 sizeof(ReportLunData_struct), 0, 0, 0, TYPE_CMD);
1563 if( return_code == IO_OK)
1566 // printk("LUN Data\n--------------------------\n");
1568 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
1569 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
1570 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
1571 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
1572 } else /* reading number of logical volumes failed */
1574 printk(KERN_WARNING "cciss: report logical volume"
1575 " command failed\n");
1579 num_luns = listlength / 8; // 8 bytes pre entry
1580 if (num_luns > CISS_MAX_LUN)
1582 num_luns = CISS_MAX_LUN;
1585 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
1586 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
1587 ld_buff->LUNListLength[3], num_luns);
1589 for(i=0; i< num_luns; i++)
1592 int lunID_found = 0;
1594 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
1595 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
1596 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
1597 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1599 /* check to see if this is a new lun */
1600 for(j=0; j <= h->highest_lun; j++)
1603 printk("Checking %d %x against %x\n", j,h->drv[j].LunID,
1605 #endif /* CCISS_DEBUG */
1606 if (h->drv[j].LunID == lunid)
1613 if( lunID_found == 1)
1616 { /* It is the new lun we have been looking for */
1618 printk("new lun found at %d\n", i);
1619 #endif /* CCISS_DEBUG */
1627 printk(KERN_WARNING "cciss: New Logical Volume not found\n");
1630 /* Now find the free index */
1631 for(i=0; i <CISS_MAX_LUN; i++)
1634 printk("Checking Index %d\n", i);
1635 #endif /* CCISS_DEBUG */
1636 if(h->drv[i].LunID == 0)
1639 printk("free index found at %d\n", i);
1640 #endif /* CCISS_DEBUG */
1641 free_index_found = 1;
1646 if (!free_index_found)
1648 printk(KERN_WARNING "cciss: unable to find free slot for disk\n");
1652 logvol = free_index;
1653 h->drv[logvol].LunID = lunid;
1654 /* there could be gaps in lun numbers, track hightest */
1655 if(h->highest_lun < lunid)
1656 h->highest_lun = logvol;
1657 cciss_read_capacity(ctlr, logvol, size_buff, 1,
1658 &total_size, &block_size);
1659 cciss_geometry_inquiry(ctlr, logvol, 1, total_size, block_size,
1660 inq_buff, &h->drv[logvol]);
1661 h->drv[logvol].usage_count = 0;
1663 /* setup partitions per disk */
1664 disk = h->gendisk[logvol];
1665 set_capacity(disk, h->drv[logvol].nr_blocks);
1673 printk(KERN_ERR "cciss: out of memory\n");
1679 * Wait polling for a command to complete.
1680 * The memory mapped FIFO is polled for the completion.
1681 * Used only at init time, interrupts from the HBA are disabled.
1683 static unsigned long pollcomplete(int ctlr)
1688 /* Wait (up to 20 seconds) for a command to complete */
1690 for (i = 20 * HZ; i > 0; i--) {
1691 done = hba[ctlr]->access.command_completed(hba[ctlr]);
1692 if (done == FIFO_EMPTY) {
1693 set_current_state(TASK_UNINTERRUPTIBLE);
1694 schedule_timeout(1);
1698 /* Invalid address to tell caller we ran out of time */
1702 * Send a command to the controller, and wait for it to complete.
1703 * Only used at init time.
1710 unsigned int use_unit_num, /* 0: address the controller,
1711 1: address logical volume log_unit,
1712 2: periph device address is scsi3addr */
1713 unsigned int log_unit,
1715 unsigned char *scsi3addr,
1718 CommandList_struct *c;
1720 unsigned long complete;
1721 ctlr_info_t *info_p= hba[ctlr];
1722 u64bit buff_dma_handle;
1725 if ((c = cmd_alloc(info_p, 1)) == NULL) {
1726 printk(KERN_WARNING "cciss: unable to get memory");
1729 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1730 log_unit, page_code, scsi3addr, cmd_type);
1731 if (status != IO_OK) {
1732 cmd_free(info_p, c, 1);
1740 printk(KERN_DEBUG "cciss: turning intr off\n");
1741 #endif /* CCISS_DEBUG */
1742 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
1744 /* Make sure there is room in the command FIFO */
1745 /* Actually it should be completely empty at this time. */
1746 for (i = 200000; i > 0; i--)
1748 /* if fifo isn't full go */
1749 if (!(info_p->access.fifo_full(info_p)))
1755 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
1756 " waiting!\n", ctlr);
1761 info_p->access.submit_command(info_p, c);
1762 complete = pollcomplete(ctlr);
1765 printk(KERN_DEBUG "cciss: command completed\n");
1766 #endif /* CCISS_DEBUG */
1768 if (complete != 1) {
1769 if ( (complete & CISS_ERROR_BIT)
1770 && (complete & ~CISS_ERROR_BIT) == c->busaddr)
1772 /* if data overrun or underun on Report command
1775 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
1776 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
1777 (c->Request.CDB[0] == CISS_INQUIRY)) &&
1778 ((c->err_info->CommandStatus ==
1779 CMD_DATA_OVERRUN) ||
1780 (c->err_info->CommandStatus ==
1784 complete = c->busaddr;
1786 if (c->err_info->CommandStatus ==
1787 CMD_UNSOLICITED_ABORT) {
1788 printk(KERN_WARNING "cciss%d: "
1789 "unsolicited abort %p\n",
1791 if (c->retry_count < MAX_CMD_RETRIES) {
1793 "cciss%d: retrying %p\n",
1796 /* erase the old error */
1798 memset(c->err_info, 0,
1799 sizeof(ErrorInfo_struct));
1803 "cciss%d: retried %p too "
1804 "many times\n", ctlr, c);
1809 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1810 " Error %x \n", ctlr,
1811 c->err_info->CommandStatus);
1812 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1814 " size %x\n num %x value %x\n", ctlr,
1815 c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
1816 c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
1817 c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
1822 if (complete != c->busaddr) {
1823 printk( KERN_WARNING "cciss cciss%d: SendCmd "
1824 "Invalid command list address returned! (%lx)\n",
1830 printk( KERN_WARNING
1831 "cciss cciss%d: SendCmd Timeout out, "
1832 "No command list address returned!\n",
1838 /* unlock the data buffer from DMA */
1839 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
1840 size, PCI_DMA_BIDIRECTIONAL);
1841 cmd_free(info_p, c, 1);
1845 * Map (physical) PCI mem into (virtual) kernel space
1847 static ulong remap_pci_mem(ulong base, ulong size)
1849 ulong page_base = ((ulong) base) & PAGE_MASK;
1850 ulong page_offs = ((ulong) base) - page_base;
1851 ulong page_remapped = (ulong) ioremap(page_base, page_offs+size);
1853 return (ulong) (page_remapped ? (page_remapped + page_offs) : 0UL);
1857 * Takes jobs of the Q and sends them to the hardware, then puts it on
1858 * the Q to wait for completion.
1860 static void start_io( ctlr_info_t *h)
1862 CommandList_struct *c;
1864 while(( c = h->reqQ) != NULL )
1866 /* can't do anything if fifo is full */
1867 if ((h->access.fifo_full(h))) {
1868 printk(KERN_WARNING "cciss: fifo full\n");
1872 /* Get the frist entry from the Request Q */
1873 removeQ(&(h->reqQ), c);
1876 /* Tell the controller execute command */
1877 h->access.submit_command(h, c);
1879 /* Put job onto the completed Q */
1880 addQ (&(h->cmpQ), c);
1884 static inline void complete_buffers(struct bio *bio, int status)
1887 struct bio *xbh = bio->bi_next;
1888 int nr_sectors = bio_sectors(bio);
1890 bio->bi_next = NULL;
1891 blk_finished_io(len);
1892 bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
1897 /* Assumes that CCISS_LOCK(h->ctlr) is held. */
1898 /* Zeros out the error record and then resends the command back */
1899 /* to the controller */
1900 static inline void resend_cciss_cmd( ctlr_info_t *h, CommandList_struct *c)
1902 /* erase the old error information */
1903 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
1905 /* add it to software queue and then send it to the controller */
1908 if(h->Qdepth > h->maxQsinceinit)
1909 h->maxQsinceinit = h->Qdepth;
1913 /* checks the status of the job and calls complete buffers to mark all
1914 * buffers for the completed job.
1916 static inline void complete_command( ctlr_info_t *h, CommandList_struct *cmd,
1927 if(cmd->err_info->CommandStatus != 0)
1928 { /* an error has occurred */
1929 switch(cmd->err_info->CommandStatus)
1931 unsigned char sense_key;
1932 case CMD_TARGET_STATUS:
1935 if( cmd->err_info->ScsiStatus == 0x02)
1937 printk(KERN_WARNING "cciss: cmd %p "
1938 "has CHECK CONDITION "
1939 " byte 2 = 0x%x\n", cmd,
1940 cmd->err_info->SenseInfo[2]
1942 /* check the sense key */
1944 cmd->err_info->SenseInfo[2];
1945 /* no status or recovered error */
1946 if((sense_key == 0x0) ||
1953 printk(KERN_WARNING "cciss: cmd %p "
1954 "has SCSI Status 0x%x\n",
1955 cmd, cmd->err_info->ScsiStatus);
1958 case CMD_DATA_UNDERRUN:
1959 printk(KERN_WARNING "cciss: cmd %p has"
1960 " completed with data underrun "
1963 case CMD_DATA_OVERRUN:
1964 printk(KERN_WARNING "cciss: cmd %p has"
1965 " completed with data overrun "
1969 printk(KERN_WARNING "cciss: cmd %p is "
1970 "reported invalid\n", cmd);
1973 case CMD_PROTOCOL_ERR:
1974 printk(KERN_WARNING "cciss: cmd %p has "
1975 "protocol error \n", cmd);
1978 case CMD_HARDWARE_ERR:
1979 printk(KERN_WARNING "cciss: cmd %p had "
1980 " hardware error\n", cmd);
1983 case CMD_CONNECTION_LOST:
1984 printk(KERN_WARNING "cciss: cmd %p had "
1985 "connection lost\n", cmd);
1989 printk(KERN_WARNING "cciss: cmd %p was "
1993 case CMD_ABORT_FAILED:
1994 printk(KERN_WARNING "cciss: cmd %p reports "
1995 "abort failed\n", cmd);
1998 case CMD_UNSOLICITED_ABORT:
1999 printk(KERN_WARNING "cciss%d: unsolicited "
2000 "abort %p\n", h->ctlr, cmd);
2001 if (cmd->retry_count < MAX_CMD_RETRIES) {
2004 "cciss%d: retrying %p\n",
2009 "cciss%d: %p retried too "
2010 "many times\n", h->ctlr, cmd);
2014 printk(KERN_WARNING "cciss: cmd %p timedout\n",
2019 printk(KERN_WARNING "cciss: cmd %p returned "
2020 "unknown status %x\n", cmd,
2021 cmd->err_info->CommandStatus);
2025 /* We need to return this command */
2027 resend_cciss_cmd(h,cmd);
2030 /* command did not need to be retried */
2031 /* unmap the DMA mapping for all the scatter gather elements */
2032 for(i=0; i<cmd->Header.SGList; i++) {
2033 temp64.val32.lower = cmd->SG[i].Addr.lower;
2034 temp64.val32.upper = cmd->SG[i].Addr.upper;
2035 pci_unmap_page(hba[cmd->ctlr]->pdev,
2036 temp64.val, cmd->SG[i].Len,
2037 (cmd->Request.Type.Direction == XFER_READ) ?
2038 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
2040 complete_buffers(cmd->rq->bio, status);
2043 printk("Done with %p\n", cmd->rq);
2044 #endif /* CCISS_DEBUG */
2046 end_that_request_last(cmd->rq);
2051 * Get a request and submit it to the controller.
2053 static void do_cciss_request(request_queue_t *q)
2055 ctlr_info_t *h= q->queuedata;
2056 CommandList_struct *c;
2058 struct request *creq;
2060 struct scatterlist tmp_sg[MAXSGENTRIES];
2061 drive_info_struct *drv;
2064 if (blk_queue_plugged(q))
2068 creq = elv_next_request(q);
2072 if (creq->nr_phys_segments > MAXSGENTRIES)
2075 if (( c = cmd_alloc(h, 1)) == NULL)
2078 blkdev_dequeue_request(creq);
2080 spin_unlock_irq(q->queue_lock);
2082 c->cmd_type = CMD_RWREQ;
2085 /* fill in the request */
2086 drv = creq->rq_disk->private_data;
2087 c->Header.ReplyQueue = 0; // unused in simple mode
2088 c->Header.Tag.lower = c->busaddr; // use the physical address the cmd block for tag
2089 c->Header.LUN.LogDev.VolId= drv->LunID;
2090 c->Header.LUN.LogDev.Mode = 1;
2091 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2092 c->Request.Type.Type = TYPE_CMD; // It is a command.
2093 c->Request.Type.Attribute = ATTR_SIMPLE;
2094 c->Request.Type.Direction =
2095 (rq_data_dir(creq) == READ) ? XFER_READ: XFER_WRITE;
2096 c->Request.Timeout = 0; // Don't time out
2097 c->Request.CDB[0] = (rq_data_dir(creq) == READ) ? CCISS_READ : CCISS_WRITE;
2098 start_blk = creq->sector;
2100 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
2101 (int) creq->nr_sectors);
2102 #endif /* CCISS_DEBUG */
2104 seg = blk_rq_map_sg(q, creq, tmp_sg);
2106 /* get the DMA records for the setup */
2107 if (c->Request.Type.Direction == XFER_READ)
2108 dir = PCI_DMA_FROMDEVICE;
2110 dir = PCI_DMA_TODEVICE;
2112 for (i=0; i<seg; i++)
2114 c->SG[i].Len = tmp_sg[i].length;
2115 temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
2116 tmp_sg[i].offset, tmp_sg[i].length,
2118 c->SG[i].Addr.lower = temp64.val32.lower;
2119 c->SG[i].Addr.upper = temp64.val32.upper;
2120 c->SG[i].Ext = 0; // we are not chaining
2122 /* track how many SG entries we are using */
2127 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", creq->nr_sectors, seg);
2128 #endif /* CCISS_DEBUG */
2130 c->Header.SGList = c->Header.SGTotal = seg;
2131 c->Request.CDB[1]= 0;
2132 c->Request.CDB[2]= (start_blk >> 24) & 0xff; //MSB
2133 c->Request.CDB[3]= (start_blk >> 16) & 0xff;
2134 c->Request.CDB[4]= (start_blk >> 8) & 0xff;
2135 c->Request.CDB[5]= start_blk & 0xff;
2136 c->Request.CDB[6]= 0; // (sect >> 24) & 0xff; MSB
2137 c->Request.CDB[7]= (creq->nr_sectors >> 8) & 0xff;
2138 c->Request.CDB[8]= creq->nr_sectors & 0xff;
2139 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2141 spin_lock_irq(q->queue_lock);
2145 if(h->Qdepth > h->maxQsinceinit)
2146 h->maxQsinceinit = h->Qdepth;
2155 static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
2157 ctlr_info_t *h = dev_id;
2158 CommandList_struct *c;
2159 unsigned long flags;
2163 /* Is this interrupt for us? */
2164 if (( h->access.intr_pending(h) == 0) || (h->interrupts_enabled == 0))
2168 * If there are completed commands in the completion queue,
2169 * we had better do something about it.
2171 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2172 while( h->access.intr_pending(h))
2174 while((a = h->access.command_completed(h)) != FIFO_EMPTY)
2178 if ((c = h->cmpQ) == NULL)
2180 printk(KERN_WARNING "cciss: Completion of %08lx ignored\n", (unsigned long)a1);
2183 while(c->busaddr != a) {
2189 * If we've found the command, take it off the
2190 * completion Q and free it
2192 if (c->busaddr == a) {
2193 removeQ(&h->cmpQ, c);
2194 if (c->cmd_type == CMD_RWREQ) {
2195 complete_command(h, c, 0);
2196 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2197 complete(c->waiting);
2199 # ifdef CONFIG_CISS_SCSI_TAPE
2200 else if (c->cmd_type == CMD_SCSI)
2201 complete_scsi_command(c, 0, a1);
2209 * See if we can queue up some more IO
2211 blk_start_queue(h->queue);
2212 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2216 * We cannot read the structure directly, for portablity we must use
2218 * This is for debug only.
2221 static void print_cfg_table( CfgTable_struct *tb)
2226 printk("Controller Configuration information\n");
2227 printk("------------------------------------\n");
2229 temp_name[i] = readb(&(tb->Signature[i]));
2231 printk(" Signature = %s\n", temp_name);
2232 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
2233 printk(" Transport methods supported = 0x%x\n",
2234 readl(&(tb-> TransportSupport)));
2235 printk(" Transport methods active = 0x%x\n",
2236 readl(&(tb->TransportActive)));
2237 printk(" Requested transport Method = 0x%x\n",
2238 readl(&(tb->HostWrite.TransportRequest)));
2239 printk(" Coalese Interrupt Delay = 0x%x\n",
2240 readl(&(tb->HostWrite.CoalIntDelay)));
2241 printk(" Coalese Interrupt Count = 0x%x\n",
2242 readl(&(tb->HostWrite.CoalIntCount)));
2243 printk(" Max outstanding commands = 0x%d\n",
2244 readl(&(tb->CmdsOutMax)));
2245 printk(" Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
2247 temp_name[i] = readb(&(tb->ServerName[i]));
2248 temp_name[16] = '\0';
2249 printk(" Server Name = %s\n", temp_name);
2250 printk(" Heartbeat Counter = 0x%x\n\n\n",
2251 readl(&(tb->HeartBeat)));
2253 #endif /* CCISS_DEBUG */
2255 static void release_io_mem(ctlr_info_t *c)
2257 /* if IO mem was not protected do nothing */
2258 if( c->io_mem_addr == 0)
2260 release_region(c->io_mem_addr, c->io_mem_length);
2262 c->io_mem_length = 0;
2265 static int find_PCI_BAR_index(struct pci_dev *pdev,
2266 unsigned long pci_bar_addr)
2268 int i, offset, mem_type, bar_type;
2269 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
2272 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
2273 bar_type = pci_resource_flags(pdev, i) &
2274 PCI_BASE_ADDRESS_SPACE;
2275 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2278 mem_type = pci_resource_flags(pdev, i) &
2279 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
2281 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2282 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2283 offset += 4; /* 32 bit */
2285 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2288 default: /* reserved in PCI 2.2 */
2289 printk(KERN_WARNING "Base address is invalid\n");
2294 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2300 static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2302 ushort subsystem_vendor_id, subsystem_device_id, command;
2303 unchar irq = pdev->irq;
2304 __u32 board_id, scratchpad = 0;
2306 __u32 cfg_base_addr;
2307 __u64 cfg_base_addr_index;
2310 /* check to see if controller has been disabled */
2311 /* BEFORE trying to enable it */
2312 (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
2313 if(!(command & 0x02))
2315 printk(KERN_WARNING "cciss: controller appears to be disabled\n");
2319 if (pci_enable_device(pdev))
2321 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
2324 if (pci_set_dma_mask(pdev, CCISS_DMA_MASK ) != 0)
2326 printk(KERN_ERR "cciss: Unable to set DMA mask\n");
2330 subsystem_vendor_id = pdev->subsystem_vendor;
2331 subsystem_device_id = pdev->subsystem_device;
2332 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
2333 subsystem_vendor_id);
2335 /* search for our IO range so we can protect it */
2336 for(i=0; i<DEVICE_COUNT_RESOURCE; i++)
2338 /* is this an IO range */
2339 if( pci_resource_flags(pdev, i) & 0x01 ) {
2340 c->io_mem_addr = pci_resource_start(pdev, i);
2341 c->io_mem_length = pci_resource_end(pdev, i) -
2342 pci_resource_start(pdev, i) +1;
2344 printk("IO value found base_addr[%d] %lx %lx\n", i,
2345 c->io_mem_addr, c->io_mem_length);
2346 #endif /* CCISS_DEBUG */
2347 /* register the IO range */
2348 if(!request_region( c->io_mem_addr,
2349 c->io_mem_length, "cciss"))
2351 printk(KERN_WARNING "cciss I/O memory range already in use addr=%lx length=%ld\n",
2352 c->io_mem_addr, c->io_mem_length);
2354 c->io_mem_length = 0;
2361 printk("command = %x\n", command);
2362 printk("irq = %x\n", irq);
2363 printk("board_id = %x\n", board_id);
2364 #endif /* CCISS_DEBUG */
2369 * Memory base addr is first addr , the second points to the config
2373 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
2375 printk("address 0 = %x\n", c->paddr);
2376 #endif /* CCISS_DEBUG */
2377 c->vaddr = remap_pci_mem(c->paddr, 200);
2379 /* Wait for the board to become ready. (PCI hotplug needs this.)
2380 * We poll for up to 120 secs, once per 100ms. */
2381 for (i=0; i < 1200; i++) {
2382 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
2383 if (scratchpad == CCISS_FIRMWARE_READY)
2385 set_current_state(TASK_INTERRUPTIBLE);
2386 schedule_timeout(HZ / 10); /* wait 100ms */
2388 if (scratchpad != CCISS_FIRMWARE_READY) {
2389 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
2393 /* get the address index number */
2394 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2395 cfg_base_addr &= (__u32) 0x0000ffff;
2397 printk("cfg base address = %x\n", cfg_base_addr);
2398 #endif /* CCISS_DEBUG */
2399 cfg_base_addr_index =
2400 find_PCI_BAR_index(pdev, cfg_base_addr);
2402 printk("cfg base address index = %x\n", cfg_base_addr_index);
2403 #endif /* CCISS_DEBUG */
2404 if (cfg_base_addr_index == -1) {
2405 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
2410 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2412 printk("cfg offset = %x\n", cfg_offset);
2413 #endif /* CCISS_DEBUG */
2414 c->cfgtable = (CfgTable_struct *)
2415 remap_pci_mem(pci_resource_start(pdev, cfg_base_addr_index)
2416 + cfg_offset, sizeof(CfgTable_struct));
2417 c->board_id = board_id;
2420 print_cfg_table(c->cfgtable);
2421 #endif /* CCISS_DEBUG */
2423 for(i=0; i<NR_PRODUCTS; i++) {
2424 if (board_id == products[i].board_id) {
2425 c->product_name = products[i].product_name;
2426 c->access = *(products[i].access);
2430 if (i == NR_PRODUCTS) {
2431 printk(KERN_WARNING "cciss: Sorry, I don't know how"
2432 " to access the Smart Array controller %08lx\n",
2433 (unsigned long)board_id);
2436 if ( (readb(&c->cfgtable->Signature[0]) != 'C') ||
2437 (readb(&c->cfgtable->Signature[1]) != 'I') ||
2438 (readb(&c->cfgtable->Signature[2]) != 'S') ||
2439 (readb(&c->cfgtable->Signature[3]) != 'S') )
2441 printk("Does not appear to be a valid CISS config table\n");
2447 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
2449 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
2451 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
2456 printk("Trying to put board into Simple mode\n");
2457 #endif /* CCISS_DEBUG */
2458 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
2459 /* Update the field, and then ring the doorbell */
2460 writel( CFGTBL_Trans_Simple,
2461 &(c->cfgtable->HostWrite.TransportRequest));
2462 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
2464 /* under certain very rare conditions, this can take awhile.
2465 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
2466 * as we enter this code.) */
2467 for(i=0;i<MAX_CONFIG_WAIT;i++) {
2468 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
2470 /* delay and try again */
2471 set_current_state(TASK_INTERRUPTIBLE);
2472 schedule_timeout(10);
2476 printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
2477 #endif /* CCISS_DEBUG */
2479 print_cfg_table(c->cfgtable);
2480 #endif /* CCISS_DEBUG */
2482 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
2484 printk(KERN_WARNING "cciss: unable to get board into"
2493 * Gets information about the local volumes attached to the controller.
2495 static void cciss_getgeometry(int cntl_num)
2497 ReportLunData_struct *ld_buff;
2498 ReadCapdata_struct *size_buff;
2499 InquiryData_struct *inq_buff;
2507 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
2508 if (ld_buff == NULL)
2510 printk(KERN_ERR "cciss: out of memory\n");
2513 memset(ld_buff, 0, sizeof(ReportLunData_struct));
2514 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
2515 if (size_buff == NULL)
2517 printk(KERN_ERR "cciss: out of memory\n");
2521 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
2522 if (inq_buff == NULL)
2524 printk(KERN_ERR "cciss: out of memory\n");
2529 /* Get the firmware version */
2530 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
2531 sizeof(InquiryData_struct), 0, 0 ,0, NULL, TYPE_CMD);
2532 if (return_code == IO_OK)
2534 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
2535 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
2536 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
2537 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
2538 } else /* send command failed */
2540 printk(KERN_WARNING "cciss: unable to determine firmware"
2541 " version of controller\n");
2543 /* Get the number of logical volumes */
2544 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
2545 sizeof(ReportLunData_struct), 0, 0, 0, NULL, TYPE_CMD);
2547 if( return_code == IO_OK)
2550 printk("LUN Data\n--------------------------\n");
2551 #endif /* CCISS_DEBUG */
2553 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
2554 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
2555 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
2556 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
2557 } else /* reading number of logical volumes failed */
2559 printk(KERN_WARNING "cciss: report logical volume"
2560 " command failed\n");
2563 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
2564 if (hba[cntl_num]->num_luns > CISS_MAX_LUN)
2566 printk(KERN_ERR "ciss: only %d number of logical volumes supported\n",
2568 hba[cntl_num]->num_luns = CISS_MAX_LUN;
2571 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
2572 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
2573 ld_buff->LUNListLength[3], hba[cntl_num]->num_luns);
2574 #endif /* CCISS_DEBUG */
2576 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
2577 for(i=0; i< hba[cntl_num]->num_luns; i++)
2580 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
2581 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
2582 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
2583 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
2585 hba[cntl_num]->drv[i].LunID = lunid;
2589 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
2590 ld_buff->LUN[i][0], ld_buff->LUN[i][1],ld_buff->LUN[i][2],
2591 ld_buff->LUN[i][3], hba[cntl_num]->drv[i].LunID);
2592 #endif /* CCISS_DEBUG */
2593 cciss_read_capacity(cntl_num, i, size_buff, 0,
2594 &total_size, &block_size);
2595 cciss_geometry_inquiry(cntl_num, i, 0, total_size, block_size,
2596 inq_buff, &hba[cntl_num]->drv[i]);
2603 /* Function to find the first free pointer into our hba[] array */
2604 /* Returns -1 if no free entries are left. */
2605 static int alloc_cciss_hba(void)
2607 struct gendisk *disk[NWD];
2609 for (n = 0; n < NWD; n++) {
2610 disk[n] = alloc_disk(1 << NWD_SHIFT);
2615 for(i=0; i< MAX_CTLR; i++) {
2618 p = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
2621 memset(p, 0, sizeof(ctlr_info_t));
2622 for (n = 0; n < NWD; n++)
2623 p->gendisk[n] = disk[n];
2628 printk(KERN_WARNING "cciss: This driver supports a maximum"
2629 " of 8 controllers.\n");
2632 printk(KERN_ERR "cciss: out of memory.\n");
2639 static void free_hba(int i)
2641 ctlr_info_t *p = hba[i];
2645 for (n = 0; n < NWD; n++)
2646 put_disk(p->gendisk[n]);
2651 * This is it. Find all the controllers and register them. I really hate
2652 * stealing all these major device numbers.
2653 * returns the number of block devices registered.
2655 static int __devinit cciss_init_one(struct pci_dev *pdev,
2656 const struct pci_device_id *ent)
2662 printk(KERN_DEBUG "cciss: Device 0x%x has been found at"
2663 " bus %d dev %d func %d\n",
2664 pdev->device, pdev->bus->number, PCI_SLOT(pdev->devfn),
2665 PCI_FUNC(pdev->devfn));
2666 i = alloc_cciss_hba();
2669 if (cciss_pci_init(hba[i], pdev) != 0)
2672 sprintf(hba[i]->devname, "cciss%d", i);
2674 hba[i]->pdev = pdev;
2676 /* configure PCI DMA stuff */
2677 if (!pci_set_dma_mask(pdev, 0xffffffffffffffffULL))
2678 printk("cciss: using DAC cycles\n");
2679 else if (!pci_set_dma_mask(pdev, 0xffffffff))
2680 printk("cciss: not using DAC cycles\n");
2682 printk("cciss: no suitable DMA available\n");
2686 if (register_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname)) {
2687 printk(KERN_ERR "cciss: Unable to register device %s\n",
2692 /* make sure the board interrupts are off */
2693 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
2694 if( request_irq(hba[i]->intr, do_cciss_intr,
2695 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
2696 hba[i]->devname, hba[i])) {
2697 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
2698 hba[i]->intr, hba[i]->devname);
2701 hba[i]->cmd_pool_bits = kmalloc(((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long), GFP_KERNEL);
2702 hba[i]->cmd_pool = (CommandList_struct *)pci_alloc_consistent(
2703 hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2704 &(hba[i]->cmd_pool_dhandle));
2705 hba[i]->errinfo_pool = (ErrorInfo_struct *)pci_alloc_consistent(
2706 hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2707 &(hba[i]->errinfo_pool_dhandle));
2708 if((hba[i]->cmd_pool_bits == NULL)
2709 || (hba[i]->cmd_pool == NULL)
2710 || (hba[i]->errinfo_pool == NULL)) {
2711 printk( KERN_ERR "cciss: out of memory");
2715 spin_lock_init(&hba[i]->lock);
2716 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
2720 q->backing_dev_info.ra_pages = READ_AHEAD;
2722 q->queuedata = hba[i];
2724 /* Initialize the pdev driver private data.
2725 have it point to hba[i]. */
2726 pci_set_drvdata(pdev, hba[i]);
2727 /* command and error info recs zeroed out before
2729 memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long));
2732 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
2733 #endif /* CCISS_DEBUG */
2735 cciss_getgeometry(i);
2737 cciss_scsi_setup(i);
2739 /* Turn the interrupts on so we can service requests */
2740 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
2744 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
2746 /* This is a hardware imposed limit. */
2747 blk_queue_max_hw_segments(q, MAXSGENTRIES);
2749 /* This is a limit in the driver and could be eliminated. */
2750 blk_queue_max_phys_segments(q, MAXSGENTRIES);
2752 blk_queue_max_sectors(q, 512);
2755 for(j=0; j<NWD; j++) {
2756 drive_info_struct *drv = &(hba[i]->drv[j]);
2757 struct gendisk *disk = hba[i]->gendisk[j];
2759 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
2760 sprintf(disk->devfs_name, "cciss/host%d/target%d", i, j);
2761 disk->major = COMPAQ_CISS_MAJOR + i;
2762 disk->first_minor = j << NWD_SHIFT;
2763 disk->fops = &cciss_fops;
2764 disk->queue = hba[i]->queue;
2765 disk->private_data = drv;
2766 if( !(drv->nr_blocks))
2768 blk_queue_hardsect_size(hba[i]->queue, drv->block_size);
2769 set_capacity(disk, drv->nr_blocks);
2775 if(hba[i]->cmd_pool_bits)
2776 kfree(hba[i]->cmd_pool_bits);
2777 if(hba[i]->cmd_pool)
2778 pci_free_consistent(hba[i]->pdev,
2779 NR_CMDS * sizeof(CommandList_struct),
2780 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2781 if(hba[i]->errinfo_pool)
2782 pci_free_consistent(hba[i]->pdev,
2783 NR_CMDS * sizeof( ErrorInfo_struct),
2784 hba[i]->errinfo_pool,
2785 hba[i]->errinfo_pool_dhandle);
2786 free_irq(hba[i]->intr, hba[i]);
2788 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2790 release_io_mem(hba[i]);
2795 static void __devexit cciss_remove_one (struct pci_dev *pdev)
2797 ctlr_info_t *tmp_ptr;
2802 if (pci_get_drvdata(pdev) == NULL)
2804 printk( KERN_ERR "cciss: Unable to remove device \n");
2807 tmp_ptr = pci_get_drvdata(pdev);
2811 printk(KERN_ERR "cciss: device appears to "
2812 "already be removed \n");
2815 /* Turn board interrupts off and send the flush cache command */
2816 /* sendcmd will turn off interrupt, and send the flush...
2817 * To write all data in the battery backed cache to disks */
2818 memset(flush_buf, 0, 4);
2819 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
2821 if(return_code != IO_OK)
2823 printk(KERN_WARNING "Error Flushing cache on controller %d\n",
2826 free_irq(hba[i]->intr, hba[i]);
2827 pci_set_drvdata(pdev, NULL);
2828 iounmap((void*)hba[i]->vaddr);
2829 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
2830 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2831 remove_proc_entry(hba[i]->devname, proc_cciss);
2833 /* remove it from the disk list */
2834 for (j = 0; j < NWD; j++) {
2835 struct gendisk *disk = hba[i]->gendisk[j];
2836 if (disk->flags & GENHD_FL_UP)
2840 blk_cleanup_queue(hba[i]->queue);
2841 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2842 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2843 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2844 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
2845 kfree(hba[i]->cmd_pool_bits);
2846 release_io_mem(hba[i]);
2850 static struct pci_driver cciss_pci_driver = {
2852 .probe = cciss_init_one,
2853 .remove = __devexit_p(cciss_remove_one),
2854 .id_table = cciss_pci_device_id, /* id_table */
2858 * This is it. Register the PCI driver information for the cards we control
2859 * the OS will call our registered routines when it finds one of our cards.
2861 int __init cciss_init(void)
2863 printk(KERN_INFO DRIVER_NAME "\n");
2865 /* Register for our PCI devices */
2866 return pci_module_init(&cciss_pci_driver);
2869 static int __init init_cciss_module(void)
2871 register_cciss_ioctl32();
2872 return ( cciss_init());
2875 static void __exit cleanup_cciss_module(void)
2879 unregister_cciss_ioctl32();
2880 pci_unregister_driver(&cciss_pci_driver);
2881 /* double check that all controller entrys have been removed */
2882 for (i=0; i< MAX_CTLR; i++)
2886 printk(KERN_WARNING "cciss: had to remove"
2887 " controller %d\n", i);
2888 cciss_remove_one(hba[i]->pdev);
2891 remove_proc_entry("cciss", proc_root_driver);
2894 module_init(init_cciss_module);
2895 module_exit(cleanup_cciss_module);