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 iss_storagedev@hp.com
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_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_HP, PCI_DEVICE_ID_HP_CISSA,
84 0x103C, 0x3225, 0, 0, 0},
87 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
89 #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
91 /* board_id = Subsystem Device ID & Vendor ID
92 * product = Marketing Name for the board
93 * access = Address of the struct of function pointers
95 static struct board_type products[] = {
96 { 0x40700E11, "Smart Array 5300", &SA5_access },
97 { 0x40800E11, "Smart Array 5i", &SA5B_access},
98 { 0x40820E11, "Smart Array 532", &SA5B_access},
99 { 0x40830E11, "Smart Array 5312", &SA5B_access},
100 { 0x409A0E11, "Smart Array 641", &SA5_access},
101 { 0x409B0E11, "Smart Array 642", &SA5_access},
102 { 0x409C0E11, "Smart Array 6400", &SA5_access},
103 { 0x409D0E11, "Smart Array 6400 EM", &SA5_access},
104 { 0x40910E11, "Smart Array 6i", &SA5_access},
105 { 0x3225103C, "Smart Array P600", &SA5_access},
108 /* How long to wait (in millesconds) for board to go into simple mode */
109 #define MAX_CONFIG_WAIT 30000
110 #define MAX_IOCTL_CONFIG_WAIT 1000
112 /*define how many times we will try a command because of bus resets */
113 #define MAX_CMD_RETRIES 3
115 #define READ_AHEAD 1024
116 #define NR_CMDS 384 /* #commands that can be outstanding */
119 #define CCISS_DMA_MASK 0xFFFFFFFF /* 32 bit DMA */
121 static ctlr_info_t *hba[MAX_CTLR];
123 static void do_cciss_request(request_queue_t *q);
124 static int cciss_open(struct inode *inode, struct file *filep);
125 static int cciss_release(struct inode *inode, struct file *filep);
126 static int cciss_ioctl(struct inode *inode, struct file *filep,
127 unsigned int cmd, unsigned long arg);
129 static int revalidate_allvol(ctlr_info_t *host);
130 static int cciss_revalidate(struct gendisk *disk);
131 static int deregister_disk(struct gendisk *disk);
132 static int register_new_disk(ctlr_info_t *h);
134 static void cciss_getgeometry(int cntl_num);
136 static void start_io( ctlr_info_t *h);
137 static int sendcmd( __u8 cmd, int ctlr, void *buff, size_t size,
138 unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
139 unsigned char *scsi3addr, int cmd_type);
141 #ifdef CONFIG_PROC_FS
142 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
143 int length, int *eof, void *data);
144 static void cciss_procinit(int i);
146 static void cciss_procinit(int i) {}
147 #endif /* CONFIG_PROC_FS */
149 static struct block_device_operations cciss_fops = {
150 .owner = THIS_MODULE,
152 .release = cciss_release,
153 .ioctl = cciss_ioctl,
154 .revalidate_disk= cciss_revalidate,
158 * Enqueuing and dequeuing functions for cmdlists.
160 static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
164 c->next = c->prev = c;
166 c->prev = (*Qptr)->prev;
168 (*Qptr)->prev->next = c;
173 static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
174 CommandList_struct *c)
176 if (c && c->next != c) {
177 if (*Qptr == c) *Qptr = c->next;
178 c->prev->next = c->next;
179 c->next->prev = c->prev;
186 #include "cciss_scsi.c" /* For SCSI tape support */
188 #ifdef CONFIG_PROC_FS
191 * Report information about this controller.
193 #define ENG_GIG 1000000000
194 #define ENG_GIG_FACTOR (ENG_GIG/512)
195 #define RAID_UNKNOWN 6
196 static const char *raid_label[] = {"0","4","1(1+0)","5","5+1","ADG",
199 static struct proc_dir_entry *proc_cciss;
201 static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
202 int length, int *eof, void *data)
207 ctlr_info_t *h = (ctlr_info_t*)data;
208 drive_info_struct *drv;
210 sector_t vol_sz, vol_sz_frac;
214 /* prevent displaying bogus info during configuration
215 * or deconfiguration of a logical volume
217 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
218 if (h->busy_configuring) {
219 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
222 h->busy_configuring = 1;
223 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
225 size = sprintf(buffer, "%s: HP %s Controller\n"
226 "Board ID: 0x%08lx\n"
227 "Firmware Version: %c%c%c%c\n"
229 "Logical drives: %d\n"
230 "Current Q depth: %d\n"
231 "Current # commands on controller: %d\n"
232 "Max Q depth since init: %d\n"
233 "Max # commands on controller since init: %d\n"
234 "Max SG entries since init: %d\n\n",
237 (unsigned long)h->board_id,
238 h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
239 (unsigned int)h->intr,
241 h->Qdepth, h->commands_outstanding,
242 h->maxQsinceinit, h->max_outstanding, h->maxSG);
244 pos += size; len += size;
245 cciss_proc_tape_report(ctlr, buffer, &pos, &len);
246 for(i=0; i<=h->highest_lun; i++) {
249 if (drv->block_size == 0)
252 vol_sz = drv->nr_blocks;
253 vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
255 sector_div(vol_sz_frac, ENG_GIG_FACTOR);
257 if (drv->raid_level > 5)
258 drv->raid_level = RAID_UNKNOWN;
259 size = sprintf(buffer+len, "cciss/c%dd%d:"
260 "\t%4u.%02uGB\tRAID %s\n",
261 ctlr, i, (int)vol_sz, (int)vol_sz_frac,
262 raid_label[drv->raid_level]);
263 pos += size; len += size;
267 *start = buffer+offset;
271 h->busy_configuring = 0;
276 cciss_proc_write(struct file *file, const char __user *buffer,
277 unsigned long count, void *data)
279 unsigned char cmd[80];
281 #ifdef CONFIG_CISS_SCSI_TAPE
282 ctlr_info_t *h = (ctlr_info_t *) data;
286 if (count > sizeof(cmd)-1) return -EINVAL;
287 if (copy_from_user(cmd, buffer, count)) return -EFAULT;
289 len = strlen(cmd); // above 3 lines ensure safety
290 if (cmd[len-1] == '\n')
292 # ifdef CONFIG_CISS_SCSI_TAPE
293 if (strcmp("engage scsi", cmd)==0) {
294 rc = cciss_engage_scsi(h->ctlr);
295 if (rc != 0) return -rc;
298 /* might be nice to have "disengage" too, but it's not
299 safely possible. (only 1 module use count, lock issues.) */
305 * Get us a file in /proc/cciss that says something about each controller.
306 * Create /proc/cciss if it doesn't exist yet.
308 static void __devinit cciss_procinit(int i)
310 struct proc_dir_entry *pde;
312 if (proc_cciss == NULL) {
313 proc_cciss = proc_mkdir("cciss", proc_root_driver);
318 pde = create_proc_read_entry(hba[i]->devname,
319 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
320 proc_cciss, cciss_proc_get_info, hba[i]);
321 pde->write_proc = cciss_proc_write;
323 #endif /* CONFIG_PROC_FS */
326 * For operations that cannot sleep, a command block is allocated at init,
327 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
328 * which ones are free or in use. For operations that can wait for kmalloc
329 * to possible sleep, this routine can be called with get_from_pool set to 0.
330 * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
332 static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
334 CommandList_struct *c;
337 dma_addr_t cmd_dma_handle, err_dma_handle;
341 c = (CommandList_struct *) pci_alloc_consistent(
342 h->pdev, sizeof(CommandList_struct), &cmd_dma_handle);
345 memset(c, 0, sizeof(CommandList_struct));
347 c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
348 h->pdev, sizeof(ErrorInfo_struct),
351 if (c->err_info == NULL)
353 pci_free_consistent(h->pdev,
354 sizeof(CommandList_struct), c, cmd_dma_handle);
357 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
358 } else /* get it out of the controllers pool */
361 i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
364 } while(test_and_set_bit(i & (BITS_PER_LONG - 1), h->cmd_pool_bits+(i/BITS_PER_LONG)) != 0);
366 printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
369 memset(c, 0, sizeof(CommandList_struct));
370 cmd_dma_handle = h->cmd_pool_dhandle
371 + i*sizeof(CommandList_struct);
372 c->err_info = h->errinfo_pool + i;
373 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
374 err_dma_handle = h->errinfo_pool_dhandle
375 + i*sizeof(ErrorInfo_struct);
379 c->busaddr = (__u32) cmd_dma_handle;
380 temp64.val = (__u64) err_dma_handle;
381 c->ErrDesc.Addr.lower = temp64.val32.lower;
382 c->ErrDesc.Addr.upper = temp64.val32.upper;
383 c->ErrDesc.Len = sizeof(ErrorInfo_struct);
392 * Frees a command block that was previously allocated with cmd_alloc().
394 static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
401 temp64.val32.lower = c->ErrDesc.Addr.lower;
402 temp64.val32.upper = c->ErrDesc.Addr.upper;
403 pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
404 c->err_info, (dma_addr_t) temp64.val);
405 pci_free_consistent(h->pdev, sizeof(CommandList_struct),
406 c, (dma_addr_t) c->busaddr);
410 clear_bit(i&(BITS_PER_LONG-1), h->cmd_pool_bits+(i/BITS_PER_LONG));
415 static inline ctlr_info_t *get_host(struct gendisk *disk)
417 return disk->queue->queuedata;
420 static inline drive_info_struct *get_drv(struct gendisk *disk)
422 return disk->private_data;
426 * Open. Make sure the device is really there.
428 static int cciss_open(struct inode *inode, struct file *filep)
430 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
431 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
434 printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
435 #endif /* CCISS_DEBUG */
438 * Root is allowed to open raw volume zero even if it's not configured
439 * so array config can still work. Root is also allowed to open any
440 * volume that has a LUN ID, so it can issue IOCTL to reread the
441 * disk information. I don't think I really like this
442 * but I'm already using way to many device nodes to claim another one
443 * for "raw controller".
445 if (drv->nr_blocks == 0) {
446 if (iminor(inode) != 0) { /* not node 0? */
447 /* if not node 0 make sure it is a partition = 0 */
448 if (iminor(inode) & 0x0f) {
450 /* if it is, make sure we have a LUN ID */
451 } else if (drv->LunID == 0) {
455 if (!capable(CAP_SYS_ADMIN))
465 static int cciss_release(struct inode *inode, struct file *filep)
467 ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
468 drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
471 printk(KERN_DEBUG "cciss_release %s\n", inode->i_bdev->bd_disk->disk_name);
472 #endif /* CCISS_DEBUG */
480 /* for AMD 64 bit kernel compatibility with 32-bit userland ioctls */
481 extern long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
483 register_ioctl32_conversion(unsigned int cmd, int (*handler)(unsigned int,
484 unsigned int, unsigned long, struct file *));
485 extern int unregister_ioctl32_conversion(unsigned int cmd);
487 static int cciss_ioctl32_passthru(unsigned int fd, unsigned cmd, unsigned long arg, struct file *file);
488 static int cciss_ioctl32_big_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
491 typedef int (*handler_type) (unsigned int, unsigned int, unsigned long, struct file *);
493 static struct ioctl32_map {
495 handler_type handler;
497 } cciss_ioctl32_map[] = {
498 { CCISS_GETPCIINFO, (handler_type) sys_ioctl, 0 },
499 { CCISS_GETINTINFO, (handler_type) sys_ioctl, 0 },
500 { CCISS_SETINTINFO, (handler_type) sys_ioctl, 0 },
501 { CCISS_GETNODENAME, (handler_type) sys_ioctl, 0 },
502 { CCISS_SETNODENAME, (handler_type) sys_ioctl, 0 },
503 { CCISS_GETHEARTBEAT, (handler_type) sys_ioctl, 0 },
504 { CCISS_GETBUSTYPES, (handler_type) sys_ioctl, 0 },
505 { CCISS_GETFIRMVER, (handler_type) sys_ioctl, 0 },
506 { CCISS_GETDRIVVER, (handler_type) sys_ioctl, 0 },
507 { CCISS_REVALIDVOLS, (handler_type) sys_ioctl, 0 },
508 { CCISS_PASSTHRU32, cciss_ioctl32_passthru, 0 },
509 { CCISS_DEREGDISK, (handler_type) sys_ioctl, 0 },
510 { CCISS_REGNEWDISK, (handler_type) sys_ioctl, 0 },
511 { CCISS_REGNEWD, (handler_type) sys_ioctl, 0 },
512 { CCISS_RESCANDISK, (handler_type) sys_ioctl, 0 },
513 { CCISS_GETLUNINFO, (handler_type) sys_ioctl, 0 },
514 { CCISS_BIG_PASSTHRU32, cciss_ioctl32_big_passthru, 0 },
516 #define NCCISS_IOCTL32_ENTRIES (sizeof(cciss_ioctl32_map) / sizeof(cciss_ioctl32_map[0]))
517 static void register_cciss_ioctl32(void)
521 for (i=0; i < NCCISS_IOCTL32_ENTRIES; i++) {
522 rc = register_ioctl32_conversion(
523 cciss_ioctl32_map[i].cmd,
524 cciss_ioctl32_map[i].handler);
526 printk(KERN_WARNING "cciss: failed to register "
527 "32 bit compatible ioctl 0x%08x\n",
528 cciss_ioctl32_map[i].cmd);
529 cciss_ioctl32_map[i].registered = 0;
531 cciss_ioctl32_map[i].registered = 1;
534 static void unregister_cciss_ioctl32(void)
538 for (i=0; i < NCCISS_IOCTL32_ENTRIES; i++) {
539 if (!cciss_ioctl32_map[i].registered)
541 rc = unregister_ioctl32_conversion(
542 cciss_ioctl32_map[i].cmd);
544 cciss_ioctl32_map[i].registered = 0;
547 printk(KERN_WARNING "cciss: failed to unregister "
548 "32 bit compatible ioctl 0x%08x\n",
549 cciss_ioctl32_map[i].cmd);
552 int cciss_ioctl32_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
555 IOCTL32_Command_struct __user *arg32 =
556 (IOCTL32_Command_struct __user *) arg;
557 IOCTL_Command_struct arg64;
558 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
563 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
564 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
565 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
566 err |= get_user(arg64.buf_size, &arg32->buf_size);
567 err |= get_user(cp, &arg32->buf);
568 arg64.buf = compat_ptr(cp);
569 err |= copy_to_user(p, &arg64, sizeof(arg64));
574 err = sys_ioctl(fd, CCISS_PASSTHRU, (unsigned long) p);
577 err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
583 int cciss_ioctl32_big_passthru(unsigned int fd, unsigned cmd, unsigned long arg,
586 BIG_IOCTL32_Command_struct __user *arg32 =
587 (BIG_IOCTL32_Command_struct __user *) arg;
588 BIG_IOCTL_Command_struct arg64;
589 BIG_IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
594 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
595 err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
596 err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
597 err |= get_user(arg64.buf_size, &arg32->buf_size);
598 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
599 err |= get_user(cp, &arg32->buf);
600 arg64.buf = compat_ptr(cp);
601 err |= copy_to_user(p, &arg64, sizeof(arg64));
606 err = sys_ioctl(fd, CCISS_BIG_PASSTHRU, (unsigned long) p);
609 err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
615 static inline void register_cciss_ioctl32(void) {}
616 static inline void unregister_cciss_ioctl32(void) {}
621 static int cciss_ioctl(struct inode *inode, struct file *filep,
622 unsigned int cmd, unsigned long arg)
624 struct block_device *bdev = inode->i_bdev;
625 struct gendisk *disk = bdev->bd_disk;
626 ctlr_info_t *host = get_host(disk);
627 drive_info_struct *drv = get_drv(disk);
628 int ctlr = host->ctlr;
629 void __user *argp = (void __user *)arg;
632 printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
633 #endif /* CCISS_DEBUG */
638 struct hd_geometry driver_geo;
639 if (drv->cylinders) {
640 driver_geo.heads = drv->heads;
641 driver_geo.sectors = drv->sectors;
642 driver_geo.cylinders = drv->cylinders;
645 driver_geo.start= get_start_sect(inode->i_bdev);
646 if (copy_to_user(argp, &driver_geo, sizeof(struct hd_geometry)))
651 case CCISS_GETPCIINFO:
653 cciss_pci_info_struct pciinfo;
655 if (!arg) return -EINVAL;
656 pciinfo.bus = host->pdev->bus->number;
657 pciinfo.dev_fn = host->pdev->devfn;
658 pciinfo.board_id = host->board_id;
659 if (copy_to_user(argp, &pciinfo, sizeof( cciss_pci_info_struct )))
663 case CCISS_GETINTINFO:
665 cciss_coalint_struct intinfo;
666 if (!arg) return -EINVAL;
667 intinfo.delay = readl(&host->cfgtable->HostWrite.CoalIntDelay);
668 intinfo.count = readl(&host->cfgtable->HostWrite.CoalIntCount);
669 if (copy_to_user(argp, &intinfo, sizeof( cciss_coalint_struct )))
673 case CCISS_SETINTINFO:
675 cciss_coalint_struct intinfo;
679 if (!arg) return -EINVAL;
680 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
681 if (copy_from_user(&intinfo, argp, sizeof( cciss_coalint_struct)))
683 if ( (intinfo.delay == 0 ) && (intinfo.count == 0))
686 // printk("cciss_ioctl: delay and count cannot be 0\n");
689 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
690 /* Update the field, and then ring the doorbell */
691 writel( intinfo.delay,
692 &(host->cfgtable->HostWrite.CoalIntDelay));
693 writel( intinfo.count,
694 &(host->cfgtable->HostWrite.CoalIntCount));
695 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
697 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
698 if (!(readl(host->vaddr + SA5_DOORBELL)
701 /* delay and try again */
704 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
705 if (i >= MAX_IOCTL_CONFIG_WAIT)
709 case CCISS_GETNODENAME:
711 NodeName_type NodeName;
714 if (!arg) return -EINVAL;
716 NodeName[i] = readb(&host->cfgtable->ServerName[i]);
717 if (copy_to_user(argp, NodeName, sizeof( NodeName_type)))
721 case CCISS_SETNODENAME:
723 NodeName_type NodeName;
727 if (!arg) return -EINVAL;
728 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
730 if (copy_from_user(NodeName, argp, sizeof( NodeName_type)))
733 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
735 /* Update the field, and then ring the doorbell */
737 writeb( NodeName[i], &host->cfgtable->ServerName[i]);
739 writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
741 for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
742 if (!(readl(host->vaddr + SA5_DOORBELL)
745 /* delay and try again */
748 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
749 if (i >= MAX_IOCTL_CONFIG_WAIT)
754 case CCISS_GETHEARTBEAT:
756 Heartbeat_type heartbeat;
758 if (!arg) return -EINVAL;
759 heartbeat = readl(&host->cfgtable->HeartBeat);
760 if (copy_to_user(argp, &heartbeat, sizeof( Heartbeat_type)))
764 case CCISS_GETBUSTYPES:
766 BusTypes_type BusTypes;
768 if (!arg) return -EINVAL;
769 BusTypes = readl(&host->cfgtable->BusTypes);
770 if (copy_to_user(argp, &BusTypes, sizeof( BusTypes_type) ))
774 case CCISS_GETFIRMVER:
776 FirmwareVer_type firmware;
778 if (!arg) return -EINVAL;
779 memcpy(firmware, host->firm_ver, 4);
781 if (copy_to_user(argp, firmware, sizeof( FirmwareVer_type)))
785 case CCISS_GETDRIVVER:
787 DriverVer_type DriverVer = DRIVER_VERSION;
789 if (!arg) return -EINVAL;
791 if (copy_to_user(argp, &DriverVer, sizeof( DriverVer_type) ))
796 case CCISS_REVALIDVOLS:
797 if (bdev != bdev->bd_contains || drv != host->drv)
799 return revalidate_allvol(host);
801 case CCISS_GETLUNINFO: {
802 LogvolInfo_struct luninfo;
805 luninfo.LunID = drv->LunID;
806 luninfo.num_opens = drv->usage_count;
807 luninfo.num_parts = 0;
808 /* count partitions 1 to 15 with sizes > 0 */
809 for (i = 0; i < MAX_PART - 1; i++) {
812 if (disk->part[i]->nr_sects != 0)
815 if (copy_to_user(argp, &luninfo,
816 sizeof(LogvolInfo_struct)))
820 case CCISS_DEREGDISK:
821 return deregister_disk(disk);
824 return register_new_disk(host);
828 IOCTL_Command_struct iocommand;
829 CommandList_struct *c;
833 DECLARE_COMPLETION(wait);
835 if (!arg) return -EINVAL;
837 if (!capable(CAP_SYS_RAWIO)) return -EPERM;
839 if (copy_from_user(&iocommand, argp, sizeof( IOCTL_Command_struct) ))
841 if((iocommand.buf_size < 1) &&
842 (iocommand.Request.Type.Direction != XFER_NONE))
846 #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
847 /* Check kmalloc limits */
848 if(iocommand.buf_size > 128000)
851 if(iocommand.buf_size > 0)
853 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
857 if (iocommand.Request.Type.Direction == XFER_WRITE)
859 /* Copy the data into the buffer we created */
860 if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
866 memset(buff, 0, iocommand.buf_size);
868 if ((c = cmd_alloc(host , 0)) == NULL)
873 // Fill in the command type
874 c->cmd_type = CMD_IOCTL_PEND;
875 // Fill in Command Header
876 c->Header.ReplyQueue = 0; // unused in simple mode
877 if( iocommand.buf_size > 0) // buffer to fill
879 c->Header.SGList = 1;
880 c->Header.SGTotal= 1;
881 } else // no buffers to fill
883 c->Header.SGList = 0;
884 c->Header.SGTotal= 0;
886 c->Header.LUN = iocommand.LUN_info;
887 c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
889 // Fill in Request block
890 c->Request = iocommand.Request;
892 // Fill in the scatter gather information
893 if (iocommand.buf_size > 0 )
895 temp64.val = pci_map_single( host->pdev, buff,
897 PCI_DMA_BIDIRECTIONAL);
898 c->SG[0].Addr.lower = temp64.val32.lower;
899 c->SG[0].Addr.upper = temp64.val32.upper;
900 c->SG[0].Len = iocommand.buf_size;
901 c->SG[0].Ext = 0; // we are not chaining
905 /* Put the request on the tail of the request queue */
906 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
907 addQ(&host->reqQ, c);
910 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
912 wait_for_completion(&wait);
914 /* unlock the buffers from DMA */
915 temp64.val32.lower = c->SG[0].Addr.lower;
916 temp64.val32.upper = c->SG[0].Addr.upper;
917 pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
918 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
920 /* Copy the error information out */
921 iocommand.error_info = *(c->err_info);
922 if ( copy_to_user(argp, &iocommand, sizeof( IOCTL_Command_struct) ) )
925 cmd_free(host, c, 0);
929 if (iocommand.Request.Type.Direction == XFER_READ)
931 /* Copy the data out of the buffer we created */
932 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size))
935 cmd_free(host, c, 0);
940 cmd_free(host, c, 0);
943 case CCISS_BIG_PASSTHRU: {
944 BIG_IOCTL_Command_struct *ioc;
945 CommandList_struct *c;
946 unsigned char **buff = NULL;
947 int *buff_size = NULL;
953 DECLARE_COMPLETION(wait);
956 BYTE __user *data_ptr;
960 if (!capable(CAP_SYS_RAWIO))
962 ioc = (BIG_IOCTL_Command_struct *)
963 kmalloc(sizeof(*ioc), GFP_KERNEL);
968 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
972 if ((ioc->buf_size < 1) &&
973 (ioc->Request.Type.Direction != XFER_NONE)) {
977 /* Check kmalloc limits using all SGs */
978 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
982 if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
986 buff = (unsigned char **) kmalloc(MAXSGENTRIES *
987 sizeof(char *), GFP_KERNEL);
992 memset(buff, 0, MAXSGENTRIES);
993 buff_size = (int *) kmalloc(MAXSGENTRIES * sizeof(int),
999 left = ioc->buf_size;
1000 data_ptr = ioc->buf;
1002 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
1003 buff_size[sg_used] = sz;
1004 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
1005 if (buff[sg_used] == NULL) {
1009 if (ioc->Request.Type.Direction == XFER_WRITE &&
1010 copy_from_user(buff[sg_used], data_ptr, sz)) {
1014 memset(buff[sg_used], 0, 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++)
1106 * revalidate_allvol is for online array config utilities. After a
1107 * utility reconfigures the drives in the array, it can use this function
1108 * (through an ioctl) to make the driver zap any previous disk structs for
1109 * that controller and get new ones.
1111 * Right now I'm using the getgeometry() function to do this, but this
1112 * function should probably be finer grained and allow you to revalidate one
1113 * particualar logical volume (instead of all of them on a particular
1116 static int revalidate_allvol(ctlr_info_t *host)
1118 int ctlr = host->ctlr, i;
1119 unsigned long flags;
1121 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1122 if (host->usage_count > 1) {
1123 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1124 printk(KERN_WARNING "cciss: Device busy for volume"
1125 " revalidation (usage=%d)\n", host->usage_count);
1128 host->usage_count++;
1129 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1131 for(i=0; i< NWD; i++) {
1132 struct gendisk *disk = host->gendisk[i];
1133 if (disk->flags & GENHD_FL_UP)
1138 * Set the partition and block size structures for all volumes
1139 * on this controller to zero. We will reread all of this data
1141 memset(host->drv, 0, sizeof(drive_info_struct)
1144 * Tell the array controller not to give us any interrupts while
1145 * we check the new geometry. Then turn interrupts back on when
1148 host->access.set_intr_mask(host, CCISS_INTR_OFF);
1149 cciss_getgeometry(ctlr);
1150 host->access.set_intr_mask(host, CCISS_INTR_ON);
1152 /* Loop through each real device */
1153 for (i = 0; i < NWD; i++) {
1154 struct gendisk *disk = host->gendisk[i];
1155 drive_info_struct *drv = &(host->drv[i]);
1156 /* we must register the controller even if no disks exist */
1157 /* this is for the online array utilities */
1158 if (!drv->heads && i)
1160 blk_queue_hardsect_size(host->queue, drv->block_size);
1161 set_capacity(disk, drv->nr_blocks);
1164 host->usage_count--;
1168 static int deregister_disk(struct gendisk *disk)
1170 unsigned long flags;
1171 ctlr_info_t *h = get_host(disk);
1172 drive_info_struct *drv = get_drv(disk);
1175 if (!capable(CAP_SYS_RAWIO))
1178 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1179 /* make sure logical volume is NOT is use */
1180 if( drv->usage_count > 1) {
1181 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1185 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1187 /* invalidate the devices and deregister the disk */
1188 if (disk->flags & GENHD_FL_UP)
1190 /* check to see if it was the last disk */
1191 if (drv == h->drv + h->highest_lun) {
1192 /* if so, find the new hightest lun */
1193 int i, newhighest =-1;
1194 for(i=0; i<h->highest_lun; i++) {
1195 /* if the disk has size > 0, it is available */
1196 if (h->drv[i].nr_blocks)
1199 h->highest_lun = newhighest;
1203 /* zero out the disk size info */
1205 drv->block_size = 0;
1210 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
1212 unsigned int use_unit_num, /* 0: address the controller,
1213 1: address logical volume log_unit,
1214 2: periph device address is scsi3addr */
1215 unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr,
1218 ctlr_info_t *h= hba[ctlr];
1219 u64bit buff_dma_handle;
1222 c->cmd_type = CMD_IOCTL_PEND;
1223 c->Header.ReplyQueue = 0;
1225 c->Header.SGList = 1;
1226 c->Header.SGTotal= 1;
1228 c->Header.SGList = 0;
1229 c->Header.SGTotal= 0;
1231 c->Header.Tag.lower = c->busaddr;
1233 c->Request.Type.Type = cmd_type;
1234 if (cmd_type == TYPE_CMD) {
1237 /* If the logical unit number is 0 then, this is going
1238 to controller so It's a physical command
1239 mode = 0 target = 0. So we have nothing to write.
1240 otherwise, if use_unit_num == 1,
1241 mode = 1(volume set addressing) target = LUNID
1242 otherwise, if use_unit_num == 2,
1243 mode = 0(periph dev addr) target = scsi3addr */
1244 if (use_unit_num == 1) {
1245 c->Header.LUN.LogDev.VolId=
1246 h->drv[log_unit].LunID;
1247 c->Header.LUN.LogDev.Mode = 1;
1248 } else if (use_unit_num == 2) {
1249 memcpy(c->Header.LUN.LunAddrBytes,scsi3addr,8);
1250 c->Header.LUN.LogDev.Mode = 0;
1252 /* are we trying to read a vital product page */
1253 if(page_code != 0) {
1254 c->Request.CDB[1] = 0x01;
1255 c->Request.CDB[2] = page_code;
1257 c->Request.CDBLen = 6;
1258 c->Request.Type.Attribute = ATTR_SIMPLE;
1259 c->Request.Type.Direction = XFER_READ;
1260 c->Request.Timeout = 0;
1261 c->Request.CDB[0] = CISS_INQUIRY;
1262 c->Request.CDB[4] = size & 0xFF;
1264 case CISS_REPORT_LOG:
1265 case CISS_REPORT_PHYS:
1266 /* Talking to controller so It's a physical command
1267 mode = 00 target = 0. Nothing to write.
1269 c->Request.CDBLen = 12;
1270 c->Request.Type.Attribute = ATTR_SIMPLE;
1271 c->Request.Type.Direction = XFER_READ;
1272 c->Request.Timeout = 0;
1273 c->Request.CDB[0] = cmd;
1274 c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
1275 c->Request.CDB[7] = (size >> 16) & 0xFF;
1276 c->Request.CDB[8] = (size >> 8) & 0xFF;
1277 c->Request.CDB[9] = size & 0xFF;
1280 case CCISS_READ_CAPACITY:
1281 c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
1282 c->Header.LUN.LogDev.Mode = 1;
1283 c->Request.CDBLen = 10;
1284 c->Request.Type.Attribute = ATTR_SIMPLE;
1285 c->Request.Type.Direction = XFER_READ;
1286 c->Request.Timeout = 0;
1287 c->Request.CDB[0] = cmd;
1289 case CCISS_CACHE_FLUSH:
1290 c->Request.CDBLen = 12;
1291 c->Request.Type.Attribute = ATTR_SIMPLE;
1292 c->Request.Type.Direction = XFER_WRITE;
1293 c->Request.Timeout = 0;
1294 c->Request.CDB[0] = BMIC_WRITE;
1295 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
1299 "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
1302 } else if (cmd_type == TYPE_MSG) {
1304 case 3: /* No-Op message */
1305 c->Request.CDBLen = 1;
1306 c->Request.Type.Attribute = ATTR_SIMPLE;
1307 c->Request.Type.Direction = XFER_WRITE;
1308 c->Request.Timeout = 0;
1309 c->Request.CDB[0] = cmd;
1313 "cciss%d: unknown message type %d\n",
1319 "cciss%d: unknown command type %d\n", ctlr, cmd_type);
1322 /* Fill in the scatter gather information */
1324 buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
1325 buff, size, PCI_DMA_BIDIRECTIONAL);
1326 c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
1327 c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
1328 c->SG[0].Len = size;
1329 c->SG[0].Ext = 0; /* we are not chaining */
1333 static int sendcmd_withirq(__u8 cmd,
1337 unsigned int use_unit_num,
1338 unsigned int log_unit,
1342 ctlr_info_t *h = hba[ctlr];
1343 CommandList_struct *c;
1344 u64bit buff_dma_handle;
1345 unsigned long flags;
1347 DECLARE_COMPLETION(wait);
1349 if ((c = cmd_alloc(h , 0)) == NULL)
1351 return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1352 log_unit, page_code, NULL, cmd_type);
1353 if (return_status != IO_OK) {
1355 return return_status;
1360 /* Put the request on the tail of the queue and send it */
1361 spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
1365 spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
1367 wait_for_completion(&wait);
1369 if(c->err_info->CommandStatus != 0)
1370 { /* an error has occurred */
1371 switch(c->err_info->CommandStatus)
1373 case CMD_TARGET_STATUS:
1374 printk(KERN_WARNING "cciss: cmd %p has "
1375 " completed with errors\n", c);
1376 if( c->err_info->ScsiStatus)
1378 printk(KERN_WARNING "cciss: cmd %p "
1379 "has SCSI Status = %x\n",
1381 c->err_info->ScsiStatus);
1385 case CMD_DATA_UNDERRUN:
1386 case CMD_DATA_OVERRUN:
1387 /* expected for inquire and report lun commands */
1390 printk(KERN_WARNING "cciss: Cmd %p is "
1391 "reported invalid\n", c);
1392 return_status = IO_ERROR;
1394 case CMD_PROTOCOL_ERR:
1395 printk(KERN_WARNING "cciss: cmd %p has "
1396 "protocol error \n", c);
1397 return_status = IO_ERROR;
1399 case CMD_HARDWARE_ERR:
1400 printk(KERN_WARNING "cciss: cmd %p had "
1401 " hardware error\n", c);
1402 return_status = IO_ERROR;
1404 case CMD_CONNECTION_LOST:
1405 printk(KERN_WARNING "cciss: cmd %p had "
1406 "connection lost\n", c);
1407 return_status = IO_ERROR;
1410 printk(KERN_WARNING "cciss: cmd %p was "
1412 return_status = IO_ERROR;
1414 case CMD_ABORT_FAILED:
1415 printk(KERN_WARNING "cciss: cmd %p reports "
1416 "abort failed\n", c);
1417 return_status = IO_ERROR;
1419 case CMD_UNSOLICITED_ABORT:
1421 "cciss%d: unsolicited abort %p\n",
1423 if (c->retry_count < MAX_CMD_RETRIES) {
1425 "cciss%d: retrying %p\n",
1428 /* erase the old error information */
1429 memset(c->err_info, 0,
1430 sizeof(ErrorInfo_struct));
1431 return_status = IO_OK;
1432 INIT_COMPLETION(wait);
1435 return_status = IO_ERROR;
1438 printk(KERN_WARNING "cciss: cmd %p returned "
1439 "unknown status %x\n", c,
1440 c->err_info->CommandStatus);
1441 return_status = IO_ERROR;
1444 /* unlock the buffers from DMA */
1445 pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
1446 size, PCI_DMA_BIDIRECTIONAL);
1448 return(return_status);
1451 static void cciss_geometry_inquiry(int ctlr, int logvol,
1452 int withirq, unsigned int total_size,
1453 unsigned int block_size, InquiryData_struct *inq_buff,
1454 drive_info_struct *drv)
1457 memset(inq_buff, 0, sizeof(InquiryData_struct));
1459 return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
1460 inq_buff, sizeof(*inq_buff), 1, logvol ,0xC1, TYPE_CMD);
1462 return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
1463 sizeof(*inq_buff), 1, logvol ,0xC1, NULL, TYPE_CMD);
1464 if (return_code == IO_OK) {
1465 if(inq_buff->data_byte[8] == 0xFF) {
1467 "cciss: reading geometry failed, volume "
1468 "does not support reading geometry\n");
1469 drv->block_size = block_size;
1470 drv->nr_blocks = total_size;
1472 drv->sectors = 32; // Sectors per track
1473 drv->cylinders = total_size / 255 / 32;
1477 drv->block_size = block_size;
1478 drv->nr_blocks = total_size;
1479 drv->heads = inq_buff->data_byte[6];
1480 drv->sectors = inq_buff->data_byte[7];
1481 drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
1482 drv->cylinders += inq_buff->data_byte[5];
1483 drv->raid_level = inq_buff->data_byte[8];
1484 t = drv->heads * drv->sectors;
1486 drv->cylinders = total_size/t;
1489 } else { /* Get geometry failed */
1490 printk(KERN_WARNING "cciss: reading geometry failed\n");
1492 printk(KERN_INFO " heads= %d, sectors= %d, cylinders= %d\n\n",
1493 drv->heads, drv->sectors, drv->cylinders);
1496 cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
1497 int withirq, unsigned int *total_size, unsigned int *block_size)
1500 memset(buf, 0, sizeof(*buf));
1502 return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
1503 ctlr, buf, sizeof(*buf), 1, logvol, 0, TYPE_CMD);
1505 return_code = sendcmd(CCISS_READ_CAPACITY,
1506 ctlr, buf, sizeof(*buf), 1, logvol, 0, NULL, TYPE_CMD);
1507 if (return_code == IO_OK) {
1508 *total_size = be32_to_cpu(*((__be32 *) &buf->total_size[0]))+1;
1509 *block_size = be32_to_cpu(*((__be32 *) &buf->block_size[0]));
1510 } else { /* read capacity command failed */
1511 printk(KERN_WARNING "cciss: read capacity failed\n");
1513 *block_size = BLOCK_SIZE;
1515 printk(KERN_INFO " blocks= %u block_size= %d\n",
1516 *total_size, *block_size);
1520 static int register_new_disk(ctlr_info_t *h)
1522 struct gendisk *disk;
1527 int new_lun_found = 0;
1528 int new_lun_index = 0;
1529 int free_index_found = 0;
1531 ReportLunData_struct *ld_buff = NULL;
1532 ReadCapdata_struct *size_buff = NULL;
1533 InquiryData_struct *inq_buff = NULL;
1537 unsigned int block_size;
1538 unsigned int total_size;
1540 if (!capable(CAP_SYS_RAWIO))
1542 /* if we have no space in our disk array left to add anything */
1543 if( h->num_luns >= CISS_MAX_LUN)
1546 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
1547 if (ld_buff == NULL)
1549 memset(ld_buff, 0, sizeof(ReportLunData_struct));
1550 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1551 if (size_buff == NULL)
1553 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1554 if (inq_buff == NULL)
1557 return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
1558 sizeof(ReportLunData_struct), 0, 0, 0, TYPE_CMD);
1560 if( return_code == IO_OK)
1563 // printk("LUN Data\n--------------------------\n");
1565 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
1566 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
1567 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
1568 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
1569 } else /* reading number of logical volumes failed */
1571 printk(KERN_WARNING "cciss: report logical volume"
1572 " command failed\n");
1576 num_luns = listlength / 8; // 8 bytes pre entry
1577 if (num_luns > CISS_MAX_LUN)
1579 num_luns = CISS_MAX_LUN;
1582 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
1583 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
1584 ld_buff->LUNListLength[3], num_luns);
1586 for(i=0; i< num_luns; i++)
1589 int lunID_found = 0;
1591 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
1592 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
1593 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
1594 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
1596 /* check to see if this is a new lun */
1597 for(j=0; j <= h->highest_lun; j++)
1600 printk("Checking %d %x against %x\n", j,h->drv[j].LunID,
1602 #endif /* CCISS_DEBUG */
1603 if (h->drv[j].LunID == lunid)
1610 if( lunID_found == 1)
1613 { /* It is the new lun we have been looking for */
1615 printk("new lun found at %d\n", i);
1616 #endif /* CCISS_DEBUG */
1624 printk(KERN_WARNING "cciss: New Logical Volume not found\n");
1627 /* Now find the free index */
1628 for(i=0; i <CISS_MAX_LUN; i++)
1631 printk("Checking Index %d\n", i);
1632 #endif /* CCISS_DEBUG */
1633 if(h->drv[i].LunID == 0)
1636 printk("free index found at %d\n", i);
1637 #endif /* CCISS_DEBUG */
1638 free_index_found = 1;
1643 if (!free_index_found)
1645 printk(KERN_WARNING "cciss: unable to find free slot for disk\n");
1649 logvol = free_index;
1650 h->drv[logvol].LunID = lunid;
1651 /* there could be gaps in lun numbers, track hightest */
1652 if(h->highest_lun < lunid)
1653 h->highest_lun = logvol;
1654 cciss_read_capacity(ctlr, logvol, size_buff, 1,
1655 &total_size, &block_size);
1656 cciss_geometry_inquiry(ctlr, logvol, 1, total_size, block_size,
1657 inq_buff, &h->drv[logvol]);
1658 h->drv[logvol].usage_count = 0;
1660 /* setup partitions per disk */
1661 disk = h->gendisk[logvol];
1662 set_capacity(disk, h->drv[logvol].nr_blocks);
1663 /* if it's the controller it's already added */
1672 printk(KERN_ERR "cciss: out of memory\n");
1678 static int cciss_revalidate(struct gendisk *disk)
1680 ctlr_info_t *h = get_host(disk);
1681 drive_info_struct *drv = get_drv(disk);
1684 unsigned int block_size;
1685 unsigned int total_size;
1686 ReadCapdata_struct *size_buff = NULL;
1687 InquiryData_struct *inq_buff = NULL;
1689 for(logvol=0; logvol < CISS_MAX_LUN; logvol++)
1691 if(h->drv[logvol].LunID == drv->LunID) {
1697 if (!FOUND) return 1;
1699 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
1700 if (size_buff == NULL)
1702 printk(KERN_WARNING "cciss: out of memory\n");
1705 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
1706 if (inq_buff == NULL)
1708 printk(KERN_WARNING "cciss: out of memory\n");
1713 cciss_read_capacity(h->ctlr, logvol, size_buff, 1, &total_size, &block_size);
1714 cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size, inq_buff, drv);
1716 blk_queue_hardsect_size(h->queue, drv->block_size);
1717 set_capacity(disk, drv->nr_blocks);
1725 * Wait polling for a command to complete.
1726 * The memory mapped FIFO is polled for the completion.
1727 * Used only at init time, interrupts from the HBA are disabled.
1729 static unsigned long pollcomplete(int ctlr)
1734 /* Wait (up to 20 seconds) for a command to complete */
1736 for (i = 20 * HZ; i > 0; i--) {
1737 done = hba[ctlr]->access.command_completed(hba[ctlr]);
1738 if (done == FIFO_EMPTY) {
1739 set_current_state(TASK_UNINTERRUPTIBLE);
1740 schedule_timeout(1);
1744 /* Invalid address to tell caller we ran out of time */
1748 * Send a command to the controller, and wait for it to complete.
1749 * Only used at init time.
1756 unsigned int use_unit_num, /* 0: address the controller,
1757 1: address logical volume log_unit,
1758 2: periph device address is scsi3addr */
1759 unsigned int log_unit,
1761 unsigned char *scsi3addr,
1764 CommandList_struct *c;
1766 unsigned long complete;
1767 ctlr_info_t *info_p= hba[ctlr];
1768 u64bit buff_dma_handle;
1771 if ((c = cmd_alloc(info_p, 1)) == NULL) {
1772 printk(KERN_WARNING "cciss: unable to get memory");
1775 status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
1776 log_unit, page_code, scsi3addr, cmd_type);
1777 if (status != IO_OK) {
1778 cmd_free(info_p, c, 1);
1786 printk(KERN_DEBUG "cciss: turning intr off\n");
1787 #endif /* CCISS_DEBUG */
1788 info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
1790 /* Make sure there is room in the command FIFO */
1791 /* Actually it should be completely empty at this time. */
1792 for (i = 200000; i > 0; i--)
1794 /* if fifo isn't full go */
1795 if (!(info_p->access.fifo_full(info_p)))
1801 printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
1802 " waiting!\n", ctlr);
1807 info_p->access.submit_command(info_p, c);
1808 complete = pollcomplete(ctlr);
1811 printk(KERN_DEBUG "cciss: command completed\n");
1812 #endif /* CCISS_DEBUG */
1814 if (complete != 1) {
1815 if ( (complete & CISS_ERROR_BIT)
1816 && (complete & ~CISS_ERROR_BIT) == c->busaddr)
1818 /* if data overrun or underun on Report command
1821 if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
1822 (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
1823 (c->Request.CDB[0] == CISS_INQUIRY)) &&
1824 ((c->err_info->CommandStatus ==
1825 CMD_DATA_OVERRUN) ||
1826 (c->err_info->CommandStatus ==
1830 complete = c->busaddr;
1832 if (c->err_info->CommandStatus ==
1833 CMD_UNSOLICITED_ABORT) {
1834 printk(KERN_WARNING "cciss%d: "
1835 "unsolicited abort %p\n",
1837 if (c->retry_count < MAX_CMD_RETRIES) {
1839 "cciss%d: retrying %p\n",
1842 /* erase the old error */
1844 memset(c->err_info, 0,
1845 sizeof(ErrorInfo_struct));
1849 "cciss%d: retried %p too "
1850 "many times\n", ctlr, c);
1855 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1856 " Error %x \n", ctlr,
1857 c->err_info->CommandStatus);
1858 printk(KERN_WARNING "ciss ciss%d: sendcmd"
1860 " size %x\n num %x value %x\n", ctlr,
1861 c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
1862 c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
1863 c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
1868 if (complete != c->busaddr) {
1869 printk( KERN_WARNING "cciss cciss%d: SendCmd "
1870 "Invalid command list address returned! (%lx)\n",
1876 printk( KERN_WARNING
1877 "cciss cciss%d: SendCmd Timeout out, "
1878 "No command list address returned!\n",
1884 /* unlock the data buffer from DMA */
1885 pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
1886 size, PCI_DMA_BIDIRECTIONAL);
1887 cmd_free(info_p, c, 1);
1891 * Map (physical) PCI mem into (virtual) kernel space
1893 static void __iomem *remap_pci_mem(ulong base, ulong size)
1895 ulong page_base = ((ulong) base) & PAGE_MASK;
1896 ulong page_offs = ((ulong) base) - page_base;
1897 void __iomem *page_remapped = ioremap(page_base, page_offs+size);
1899 return page_remapped ? (page_remapped + page_offs) : NULL;
1903 * Takes jobs of the Q and sends them to the hardware, then puts it on
1904 * the Q to wait for completion.
1906 static void start_io( ctlr_info_t *h)
1908 CommandList_struct *c;
1910 while(( c = h->reqQ) != NULL )
1912 /* can't do anything if fifo is full */
1913 if ((h->access.fifo_full(h))) {
1914 printk(KERN_WARNING "cciss: fifo full\n");
1918 /* Get the frist entry from the Request Q */
1919 removeQ(&(h->reqQ), c);
1922 /* Tell the controller execute command */
1923 h->access.submit_command(h, c);
1925 /* Put job onto the completed Q */
1926 addQ (&(h->cmpQ), c);
1930 static inline void complete_buffers(struct bio *bio, int status)
1933 struct bio *xbh = bio->bi_next;
1934 int nr_sectors = bio_sectors(bio);
1936 bio->bi_next = NULL;
1937 blk_finished_io(len);
1938 bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
1943 /* Assumes that CCISS_LOCK(h->ctlr) is held. */
1944 /* Zeros out the error record and then resends the command back */
1945 /* to the controller */
1946 static inline void resend_cciss_cmd( ctlr_info_t *h, CommandList_struct *c)
1948 /* erase the old error information */
1949 memset(c->err_info, 0, sizeof(ErrorInfo_struct));
1951 /* add it to software queue and then send it to the controller */
1954 if(h->Qdepth > h->maxQsinceinit)
1955 h->maxQsinceinit = h->Qdepth;
1959 /* checks the status of the job and calls complete buffers to mark all
1960 * buffers for the completed job.
1962 static inline void complete_command( ctlr_info_t *h, CommandList_struct *cmd,
1973 if(cmd->err_info->CommandStatus != 0)
1974 { /* an error has occurred */
1975 switch(cmd->err_info->CommandStatus)
1977 unsigned char sense_key;
1978 case CMD_TARGET_STATUS:
1981 if( cmd->err_info->ScsiStatus == 0x02)
1983 printk(KERN_WARNING "cciss: cmd %p "
1984 "has CHECK CONDITION "
1985 " byte 2 = 0x%x\n", cmd,
1986 cmd->err_info->SenseInfo[2]
1988 /* check the sense key */
1990 cmd->err_info->SenseInfo[2];
1991 /* no status or recovered error */
1992 if((sense_key == 0x0) ||
1999 printk(KERN_WARNING "cciss: cmd %p "
2000 "has SCSI Status 0x%x\n",
2001 cmd, cmd->err_info->ScsiStatus);
2004 case CMD_DATA_UNDERRUN:
2005 printk(KERN_WARNING "cciss: cmd %p has"
2006 " completed with data underrun "
2009 case CMD_DATA_OVERRUN:
2010 printk(KERN_WARNING "cciss: cmd %p has"
2011 " completed with data overrun "
2015 printk(KERN_WARNING "cciss: cmd %p is "
2016 "reported invalid\n", cmd);
2019 case CMD_PROTOCOL_ERR:
2020 printk(KERN_WARNING "cciss: cmd %p has "
2021 "protocol error \n", cmd);
2024 case CMD_HARDWARE_ERR:
2025 printk(KERN_WARNING "cciss: cmd %p had "
2026 " hardware error\n", cmd);
2029 case CMD_CONNECTION_LOST:
2030 printk(KERN_WARNING "cciss: cmd %p had "
2031 "connection lost\n", cmd);
2035 printk(KERN_WARNING "cciss: cmd %p was "
2039 case CMD_ABORT_FAILED:
2040 printk(KERN_WARNING "cciss: cmd %p reports "
2041 "abort failed\n", cmd);
2044 case CMD_UNSOLICITED_ABORT:
2045 printk(KERN_WARNING "cciss%d: unsolicited "
2046 "abort %p\n", h->ctlr, cmd);
2047 if (cmd->retry_count < MAX_CMD_RETRIES) {
2050 "cciss%d: retrying %p\n",
2055 "cciss%d: %p retried too "
2056 "many times\n", h->ctlr, cmd);
2060 printk(KERN_WARNING "cciss: cmd %p timedout\n",
2065 printk(KERN_WARNING "cciss: cmd %p returned "
2066 "unknown status %x\n", cmd,
2067 cmd->err_info->CommandStatus);
2071 /* We need to return this command */
2073 resend_cciss_cmd(h,cmd);
2076 /* command did not need to be retried */
2077 /* unmap the DMA mapping for all the scatter gather elements */
2078 for(i=0; i<cmd->Header.SGList; i++) {
2079 temp64.val32.lower = cmd->SG[i].Addr.lower;
2080 temp64.val32.upper = cmd->SG[i].Addr.upper;
2081 pci_unmap_page(hba[cmd->ctlr]->pdev,
2082 temp64.val, cmd->SG[i].Len,
2083 (cmd->Request.Type.Direction == XFER_READ) ?
2084 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
2086 complete_buffers(cmd->rq->bio, status);
2089 printk("Done with %p\n", cmd->rq);
2090 #endif /* CCISS_DEBUG */
2092 end_that_request_last(cmd->rq);
2097 * Get a request and submit it to the controller.
2099 static void do_cciss_request(request_queue_t *q)
2101 ctlr_info_t *h= q->queuedata;
2102 CommandList_struct *c;
2104 struct request *creq;
2106 struct scatterlist tmp_sg[MAXSGENTRIES];
2107 drive_info_struct *drv;
2110 if (blk_queue_plugged(q))
2114 creq = elv_next_request(q);
2118 if (creq->nr_phys_segments > MAXSGENTRIES)
2121 if (( c = cmd_alloc(h, 1)) == NULL)
2124 blkdev_dequeue_request(creq);
2126 spin_unlock_irq(q->queue_lock);
2128 c->cmd_type = CMD_RWREQ;
2131 /* fill in the request */
2132 drv = creq->rq_disk->private_data;
2133 c->Header.ReplyQueue = 0; // unused in simple mode
2134 c->Header.Tag.lower = c->busaddr; // use the physical address the cmd block for tag
2135 c->Header.LUN.LogDev.VolId= drv->LunID;
2136 c->Header.LUN.LogDev.Mode = 1;
2137 c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
2138 c->Request.Type.Type = TYPE_CMD; // It is a command.
2139 c->Request.Type.Attribute = ATTR_SIMPLE;
2140 c->Request.Type.Direction =
2141 (rq_data_dir(creq) == READ) ? XFER_READ: XFER_WRITE;
2142 c->Request.Timeout = 0; // Don't time out
2143 c->Request.CDB[0] = (rq_data_dir(creq) == READ) ? CCISS_READ : CCISS_WRITE;
2144 start_blk = creq->sector;
2146 printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
2147 (int) creq->nr_sectors);
2148 #endif /* CCISS_DEBUG */
2150 seg = blk_rq_map_sg(q, creq, tmp_sg);
2152 /* get the DMA records for the setup */
2153 if (c->Request.Type.Direction == XFER_READ)
2154 dir = PCI_DMA_FROMDEVICE;
2156 dir = PCI_DMA_TODEVICE;
2158 for (i=0; i<seg; i++)
2160 c->SG[i].Len = tmp_sg[i].length;
2161 temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
2162 tmp_sg[i].offset, tmp_sg[i].length,
2164 c->SG[i].Addr.lower = temp64.val32.lower;
2165 c->SG[i].Addr.upper = temp64.val32.upper;
2166 c->SG[i].Ext = 0; // we are not chaining
2168 /* track how many SG entries we are using */
2173 printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", creq->nr_sectors, seg);
2174 #endif /* CCISS_DEBUG */
2176 c->Header.SGList = c->Header.SGTotal = seg;
2177 c->Request.CDB[1]= 0;
2178 c->Request.CDB[2]= (start_blk >> 24) & 0xff; //MSB
2179 c->Request.CDB[3]= (start_blk >> 16) & 0xff;
2180 c->Request.CDB[4]= (start_blk >> 8) & 0xff;
2181 c->Request.CDB[5]= start_blk & 0xff;
2182 c->Request.CDB[6]= 0; // (sect >> 24) & 0xff; MSB
2183 c->Request.CDB[7]= (creq->nr_sectors >> 8) & 0xff;
2184 c->Request.CDB[8]= creq->nr_sectors & 0xff;
2185 c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
2187 spin_lock_irq(q->queue_lock);
2191 if(h->Qdepth > h->maxQsinceinit)
2192 h->maxQsinceinit = h->Qdepth;
2201 static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
2203 ctlr_info_t *h = dev_id;
2204 CommandList_struct *c;
2205 unsigned long flags;
2209 /* Is this interrupt for us? */
2210 if (( h->access.intr_pending(h) == 0) || (h->interrupts_enabled == 0))
2214 * If there are completed commands in the completion queue,
2215 * we had better do something about it.
2217 spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
2218 while( h->access.intr_pending(h))
2220 while((a = h->access.command_completed(h)) != FIFO_EMPTY)
2224 if ((c = h->cmpQ) == NULL)
2226 printk(KERN_WARNING "cciss: Completion of %08lx ignored\n", (unsigned long)a1);
2229 while(c->busaddr != a) {
2235 * If we've found the command, take it off the
2236 * completion Q and free it
2238 if (c->busaddr == a) {
2239 removeQ(&h->cmpQ, c);
2240 if (c->cmd_type == CMD_RWREQ) {
2241 complete_command(h, c, 0);
2242 } else if (c->cmd_type == CMD_IOCTL_PEND) {
2243 complete(c->waiting);
2245 # ifdef CONFIG_CISS_SCSI_TAPE
2246 else if (c->cmd_type == CMD_SCSI)
2247 complete_scsi_command(c, 0, a1);
2255 * See if we can queue up some more IO
2257 blk_start_queue(h->queue);
2258 spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
2262 * We cannot read the structure directly, for portablity we must use
2264 * This is for debug only.
2267 static void print_cfg_table( CfgTable_struct *tb)
2272 printk("Controller Configuration information\n");
2273 printk("------------------------------------\n");
2275 temp_name[i] = readb(&(tb->Signature[i]));
2277 printk(" Signature = %s\n", temp_name);
2278 printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
2279 printk(" Transport methods supported = 0x%x\n",
2280 readl(&(tb-> TransportSupport)));
2281 printk(" Transport methods active = 0x%x\n",
2282 readl(&(tb->TransportActive)));
2283 printk(" Requested transport Method = 0x%x\n",
2284 readl(&(tb->HostWrite.TransportRequest)));
2285 printk(" Coalese Interrupt Delay = 0x%x\n",
2286 readl(&(tb->HostWrite.CoalIntDelay)));
2287 printk(" Coalese Interrupt Count = 0x%x\n",
2288 readl(&(tb->HostWrite.CoalIntCount)));
2289 printk(" Max outstanding commands = 0x%d\n",
2290 readl(&(tb->CmdsOutMax)));
2291 printk(" Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
2293 temp_name[i] = readb(&(tb->ServerName[i]));
2294 temp_name[16] = '\0';
2295 printk(" Server Name = %s\n", temp_name);
2296 printk(" Heartbeat Counter = 0x%x\n\n\n",
2297 readl(&(tb->HeartBeat)));
2299 #endif /* CCISS_DEBUG */
2301 static void release_io_mem(ctlr_info_t *c)
2303 /* if IO mem was not protected do nothing */
2304 if( c->io_mem_addr == 0)
2306 release_region(c->io_mem_addr, c->io_mem_length);
2308 c->io_mem_length = 0;
2311 static int find_PCI_BAR_index(struct pci_dev *pdev,
2312 unsigned long pci_bar_addr)
2314 int i, offset, mem_type, bar_type;
2315 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
2318 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
2319 bar_type = pci_resource_flags(pdev, i) &
2320 PCI_BASE_ADDRESS_SPACE;
2321 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
2324 mem_type = pci_resource_flags(pdev, i) &
2325 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
2327 case PCI_BASE_ADDRESS_MEM_TYPE_32:
2328 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
2329 offset += 4; /* 32 bit */
2331 case PCI_BASE_ADDRESS_MEM_TYPE_64:
2334 default: /* reserved in PCI 2.2 */
2335 printk(KERN_WARNING "Base address is invalid\n");
2340 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
2346 static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
2348 ushort subsystem_vendor_id, subsystem_device_id, command;
2349 __u32 board_id, scratchpad = 0;
2351 __u32 cfg_base_addr;
2352 __u64 cfg_base_addr_index;
2355 /* check to see if controller has been disabled */
2356 /* BEFORE trying to enable it */
2357 (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
2358 if(!(command & 0x02))
2360 printk(KERN_WARNING "cciss: controller appears to be disabled\n");
2364 if (pci_enable_device(pdev))
2366 printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
2369 if (pci_set_dma_mask(pdev, CCISS_DMA_MASK ) != 0)
2371 printk(KERN_ERR "cciss: Unable to set DMA mask\n");
2375 subsystem_vendor_id = pdev->subsystem_vendor;
2376 subsystem_device_id = pdev->subsystem_device;
2377 board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
2378 subsystem_vendor_id);
2380 /* search for our IO range so we can protect it */
2381 for(i=0; i<DEVICE_COUNT_RESOURCE; i++)
2383 /* is this an IO range */
2384 if( pci_resource_flags(pdev, i) & 0x01 ) {
2385 c->io_mem_addr = pci_resource_start(pdev, i);
2386 c->io_mem_length = pci_resource_end(pdev, i) -
2387 pci_resource_start(pdev, i) +1;
2389 printk("IO value found base_addr[%d] %lx %lx\n", i,
2390 c->io_mem_addr, c->io_mem_length);
2391 #endif /* CCISS_DEBUG */
2392 /* register the IO range */
2393 if(!request_region( c->io_mem_addr,
2394 c->io_mem_length, "cciss"))
2396 printk(KERN_WARNING "cciss I/O memory range already in use addr=%lx length=%ld\n",
2397 c->io_mem_addr, c->io_mem_length);
2399 c->io_mem_length = 0;
2406 printk("command = %x\n", command);
2407 printk("irq = %x\n", pdev->irq);
2408 printk("board_id = %x\n", board_id);
2409 #endif /* CCISS_DEBUG */
2411 c->intr = pdev->irq;
2414 * Memory base addr is first addr , the second points to the config
2418 c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
2420 printk("address 0 = %x\n", c->paddr);
2421 #endif /* CCISS_DEBUG */
2422 c->vaddr = remap_pci_mem(c->paddr, 200);
2424 /* Wait for the board to become ready. (PCI hotplug needs this.)
2425 * We poll for up to 120 secs, once per 100ms. */
2426 for (i=0; i < 1200; i++) {
2427 scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
2428 if (scratchpad == CCISS_FIRMWARE_READY)
2430 set_current_state(TASK_INTERRUPTIBLE);
2431 schedule_timeout(HZ / 10); /* wait 100ms */
2433 if (scratchpad != CCISS_FIRMWARE_READY) {
2434 printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
2438 /* get the address index number */
2439 cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
2440 cfg_base_addr &= (__u32) 0x0000ffff;
2442 printk("cfg base address = %x\n", cfg_base_addr);
2443 #endif /* CCISS_DEBUG */
2444 cfg_base_addr_index =
2445 find_PCI_BAR_index(pdev, cfg_base_addr);
2447 printk("cfg base address index = %x\n", cfg_base_addr_index);
2448 #endif /* CCISS_DEBUG */
2449 if (cfg_base_addr_index == -1) {
2450 printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
2455 cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
2457 printk("cfg offset = %x\n", cfg_offset);
2458 #endif /* CCISS_DEBUG */
2459 c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
2460 cfg_base_addr_index) + cfg_offset,
2461 sizeof(CfgTable_struct));
2462 c->board_id = board_id;
2465 print_cfg_table(c->cfgtable);
2466 #endif /* CCISS_DEBUG */
2468 for(i=0; i<NR_PRODUCTS; i++) {
2469 if (board_id == products[i].board_id) {
2470 c->product_name = products[i].product_name;
2471 c->access = *(products[i].access);
2475 if (i == NR_PRODUCTS) {
2476 printk(KERN_WARNING "cciss: Sorry, I don't know how"
2477 " to access the Smart Array controller %08lx\n",
2478 (unsigned long)board_id);
2481 if ( (readb(&c->cfgtable->Signature[0]) != 'C') ||
2482 (readb(&c->cfgtable->Signature[1]) != 'I') ||
2483 (readb(&c->cfgtable->Signature[2]) != 'S') ||
2484 (readb(&c->cfgtable->Signature[3]) != 'S') )
2486 printk("Does not appear to be a valid CISS config table\n");
2492 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
2494 prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
2496 writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
2501 printk("Trying to put board into Simple mode\n");
2502 #endif /* CCISS_DEBUG */
2503 c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
2504 /* Update the field, and then ring the doorbell */
2505 writel( CFGTBL_Trans_Simple,
2506 &(c->cfgtable->HostWrite.TransportRequest));
2507 writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
2509 /* under certain very rare conditions, this can take awhile.
2510 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
2511 * as we enter this code.) */
2512 for(i=0;i<MAX_CONFIG_WAIT;i++) {
2513 if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
2515 /* delay and try again */
2516 set_current_state(TASK_INTERRUPTIBLE);
2517 schedule_timeout(10);
2521 printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
2522 #endif /* CCISS_DEBUG */
2524 print_cfg_table(c->cfgtable);
2525 #endif /* CCISS_DEBUG */
2527 if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
2529 printk(KERN_WARNING "cciss: unable to get board into"
2538 * Gets information about the local volumes attached to the controller.
2540 static void cciss_getgeometry(int cntl_num)
2542 ReportLunData_struct *ld_buff;
2543 ReadCapdata_struct *size_buff;
2544 InquiryData_struct *inq_buff;
2552 ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
2553 if (ld_buff == NULL)
2555 printk(KERN_ERR "cciss: out of memory\n");
2558 memset(ld_buff, 0, sizeof(ReportLunData_struct));
2559 size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
2560 if (size_buff == NULL)
2562 printk(KERN_ERR "cciss: out of memory\n");
2566 inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
2567 if (inq_buff == NULL)
2569 printk(KERN_ERR "cciss: out of memory\n");
2574 /* Get the firmware version */
2575 return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
2576 sizeof(InquiryData_struct), 0, 0 ,0, NULL, TYPE_CMD);
2577 if (return_code == IO_OK)
2579 hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
2580 hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
2581 hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
2582 hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
2583 } else /* send command failed */
2585 printk(KERN_WARNING "cciss: unable to determine firmware"
2586 " version of controller\n");
2588 /* Get the number of logical volumes */
2589 return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
2590 sizeof(ReportLunData_struct), 0, 0, 0, NULL, TYPE_CMD);
2592 if( return_code == IO_OK)
2595 printk("LUN Data\n--------------------------\n");
2596 #endif /* CCISS_DEBUG */
2598 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
2599 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
2600 listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
2601 listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
2602 } else /* reading number of logical volumes failed */
2604 printk(KERN_WARNING "cciss: report logical volume"
2605 " command failed\n");
2608 hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
2609 if (hba[cntl_num]->num_luns > CISS_MAX_LUN)
2611 printk(KERN_ERR "ciss: only %d number of logical volumes supported\n",
2613 hba[cntl_num]->num_luns = CISS_MAX_LUN;
2616 printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
2617 ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
2618 ld_buff->LUNListLength[3], hba[cntl_num]->num_luns);
2619 #endif /* CCISS_DEBUG */
2621 hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
2622 for(i=0; i< hba[cntl_num]->num_luns; i++)
2625 lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
2626 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
2627 lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
2628 lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
2630 hba[cntl_num]->drv[i].LunID = lunid;
2634 printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
2635 ld_buff->LUN[i][0], ld_buff->LUN[i][1],ld_buff->LUN[i][2],
2636 ld_buff->LUN[i][3], hba[cntl_num]->drv[i].LunID);
2637 #endif /* CCISS_DEBUG */
2638 cciss_read_capacity(cntl_num, i, size_buff, 0,
2639 &total_size, &block_size);
2640 cciss_geometry_inquiry(cntl_num, i, 0, total_size, block_size,
2641 inq_buff, &hba[cntl_num]->drv[i]);
2648 /* Function to find the first free pointer into our hba[] array */
2649 /* Returns -1 if no free entries are left. */
2650 static int alloc_cciss_hba(void)
2652 struct gendisk *disk[NWD];
2654 for (n = 0; n < NWD; n++) {
2655 disk[n] = alloc_disk(1 << NWD_SHIFT);
2660 for(i=0; i< MAX_CTLR; i++) {
2663 p = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
2666 memset(p, 0, sizeof(ctlr_info_t));
2667 for (n = 0; n < NWD; n++)
2668 p->gendisk[n] = disk[n];
2673 printk(KERN_WARNING "cciss: This driver supports a maximum"
2674 " of 8 controllers.\n");
2677 printk(KERN_ERR "cciss: out of memory.\n");
2684 static void free_hba(int i)
2686 ctlr_info_t *p = hba[i];
2690 for (n = 0; n < NWD; n++)
2691 put_disk(p->gendisk[n]);
2696 * This is it. Find all the controllers and register them. I really hate
2697 * stealing all these major device numbers.
2698 * returns the number of block devices registered.
2700 static int __devinit cciss_init_one(struct pci_dev *pdev,
2701 const struct pci_device_id *ent)
2707 printk(KERN_DEBUG "cciss: Device 0x%x has been found at"
2708 " bus %d dev %d func %d\n",
2709 pdev->device, pdev->bus->number, PCI_SLOT(pdev->devfn),
2710 PCI_FUNC(pdev->devfn));
2711 i = alloc_cciss_hba();
2714 if (cciss_pci_init(hba[i], pdev) != 0)
2717 sprintf(hba[i]->devname, "cciss%d", i);
2719 hba[i]->pdev = pdev;
2721 /* configure PCI DMA stuff */
2722 if (!pci_set_dma_mask(pdev, 0xffffffffffffffffULL))
2723 printk("cciss: using DAC cycles\n");
2724 else if (!pci_set_dma_mask(pdev, 0xffffffff))
2725 printk("cciss: not using DAC cycles\n");
2727 printk("cciss: no suitable DMA available\n");
2731 if (register_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname)) {
2732 printk(KERN_ERR "cciss: Unable to register device %s\n",
2737 /* make sure the board interrupts are off */
2738 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
2739 if( request_irq(hba[i]->intr, do_cciss_intr,
2740 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
2741 hba[i]->devname, hba[i])) {
2742 printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
2743 hba[i]->intr, hba[i]->devname);
2746 hba[i]->cmd_pool_bits = kmalloc(((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long), GFP_KERNEL);
2747 hba[i]->cmd_pool = (CommandList_struct *)pci_alloc_consistent(
2748 hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2749 &(hba[i]->cmd_pool_dhandle));
2750 hba[i]->errinfo_pool = (ErrorInfo_struct *)pci_alloc_consistent(
2751 hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2752 &(hba[i]->errinfo_pool_dhandle));
2753 if((hba[i]->cmd_pool_bits == NULL)
2754 || (hba[i]->cmd_pool == NULL)
2755 || (hba[i]->errinfo_pool == NULL)) {
2756 printk( KERN_ERR "cciss: out of memory");
2760 spin_lock_init(&hba[i]->lock);
2761 q = blk_init_queue(do_cciss_request, &hba[i]->lock);
2765 q->backing_dev_info.ra_pages = READ_AHEAD;
2767 q->queuedata = hba[i];
2769 /* Initialize the pdev driver private data.
2770 have it point to hba[i]. */
2771 pci_set_drvdata(pdev, hba[i]);
2772 /* command and error info recs zeroed out before
2774 memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long));
2777 printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
2778 #endif /* CCISS_DEBUG */
2780 cciss_getgeometry(i);
2782 cciss_scsi_setup(i);
2784 /* Turn the interrupts on so we can service requests */
2785 hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
2789 blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
2791 /* This is a hardware imposed limit. */
2792 blk_queue_max_hw_segments(q, MAXSGENTRIES);
2794 /* This is a limit in the driver and could be eliminated. */
2795 blk_queue_max_phys_segments(q, MAXSGENTRIES);
2797 blk_queue_max_sectors(q, 512);
2800 for(j=0; j<NWD; j++) {
2801 drive_info_struct *drv = &(hba[i]->drv[j]);
2802 struct gendisk *disk = hba[i]->gendisk[j];
2804 sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
2805 sprintf(disk->devfs_name, "cciss/host%d/target%d", i, j);
2806 disk->major = COMPAQ_CISS_MAJOR + i;
2807 disk->first_minor = j << NWD_SHIFT;
2808 disk->fops = &cciss_fops;
2809 disk->queue = hba[i]->queue;
2810 disk->private_data = drv;
2811 /* we must register the controller even if no disks exist */
2812 /* this is for the online array utilities */
2813 if(!drv->heads && j)
2815 blk_queue_hardsect_size(hba[i]->queue, drv->block_size);
2816 set_capacity(disk, drv->nr_blocks);
2822 if(hba[i]->cmd_pool_bits)
2823 kfree(hba[i]->cmd_pool_bits);
2824 if(hba[i]->cmd_pool)
2825 pci_free_consistent(hba[i]->pdev,
2826 NR_CMDS * sizeof(CommandList_struct),
2827 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2828 if(hba[i]->errinfo_pool)
2829 pci_free_consistent(hba[i]->pdev,
2830 NR_CMDS * sizeof( ErrorInfo_struct),
2831 hba[i]->errinfo_pool,
2832 hba[i]->errinfo_pool_dhandle);
2833 free_irq(hba[i]->intr, hba[i]);
2835 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2837 release_io_mem(hba[i]);
2842 static void __devexit cciss_remove_one (struct pci_dev *pdev)
2844 ctlr_info_t *tmp_ptr;
2849 if (pci_get_drvdata(pdev) == NULL)
2851 printk( KERN_ERR "cciss: Unable to remove device \n");
2854 tmp_ptr = pci_get_drvdata(pdev);
2858 printk(KERN_ERR "cciss: device appears to "
2859 "already be removed \n");
2862 /* Turn board interrupts off and send the flush cache command */
2863 /* sendcmd will turn off interrupt, and send the flush...
2864 * To write all data in the battery backed cache to disks */
2865 memset(flush_buf, 0, 4);
2866 return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
2868 if(return_code != IO_OK)
2870 printk(KERN_WARNING "Error Flushing cache on controller %d\n",
2873 free_irq(hba[i]->intr, hba[i]);
2874 pci_set_drvdata(pdev, NULL);
2875 iounmap(hba[i]->vaddr);
2876 cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
2877 unregister_blkdev(COMPAQ_CISS_MAJOR+i, hba[i]->devname);
2878 remove_proc_entry(hba[i]->devname, proc_cciss);
2880 /* remove it from the disk list */
2881 for (j = 0; j < NWD; j++) {
2882 struct gendisk *disk = hba[i]->gendisk[j];
2883 if (disk->flags & GENHD_FL_UP)
2887 blk_cleanup_queue(hba[i]->queue);
2888 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
2889 hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
2890 pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
2891 hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
2892 kfree(hba[i]->cmd_pool_bits);
2893 release_io_mem(hba[i]);
2897 static struct pci_driver cciss_pci_driver = {
2899 .probe = cciss_init_one,
2900 .remove = __devexit_p(cciss_remove_one),
2901 .id_table = cciss_pci_device_id, /* id_table */
2905 * This is it. Register the PCI driver information for the cards we control
2906 * the OS will call our registered routines when it finds one of our cards.
2908 int __init cciss_init(void)
2910 printk(KERN_INFO DRIVER_NAME "\n");
2912 /* Register for our PCI devices */
2913 return pci_module_init(&cciss_pci_driver);
2916 static int __init init_cciss_module(void)
2918 register_cciss_ioctl32();
2919 return ( cciss_init());
2922 static void __exit cleanup_cciss_module(void)
2926 unregister_cciss_ioctl32();
2927 pci_unregister_driver(&cciss_pci_driver);
2928 /* double check that all controller entrys have been removed */
2929 for (i=0; i< MAX_CTLR; i++)
2933 printk(KERN_WARNING "cciss: had to remove"
2934 " controller %d\n", i);
2935 cciss_remove_one(hba[i]->pdev);
2938 remove_proc_entry("cciss", proc_root_driver);
2941 module_init(init_cciss_module);
2942 module_exit(cleanup_cciss_module);