Merge with 2.4.0-test3-pre4.
[linux-2.6/linux-mips.git] / drivers / block / cpqarray.c
blob9ae6fa94c59642a69648c98c3a78505e7378e118
1 /*
2 * Disk Array driver for Compaq SMART2 Controllers
3 * Copyright 1998 Compaq Computer Corporation
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 arrays@compaq.com
21 * If you want to make changes, improve or add functionality to this
22 * driver, you'll probably need the Compaq Array Controller Interface
23 * Specificiation (Document number ECG086/1198)
25 #include <linux/config.h> /* CONFIG_PROC_FS */
26 #include <linux/module.h>
27 #include <linux/version.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/kernel.h>
31 #include <linux/malloc.h>
32 #include <linux/delay.h>
33 #include <linux/major.h>
34 #include <linux/fs.h>
35 #include <linux/blkpg.h>
36 #include <linux/timer.h>
37 #include <linux/proc_fs.h>
38 #include <linux/init.h>
39 #include <linux/hdreg.h>
40 #include <linux/spinlock.h>
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
45 #define SMART2_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
47 #define DRIVER_NAME "Compaq SMART2 Driver (v 2.4.0)"
48 #define DRIVER_VERSION SMART2_DRIVER_VERSION(2,4,0)
49 #define MAJOR_NR COMPAQ_SMART2_MAJOR
50 #include <linux/blk.h>
51 #include <linux/blkdev.h>
52 #include <linux/genhd.h>
54 #include "cpqarray.h"
55 #include "ida_cmd.h"
56 #include "smart1,2.h"
57 #include "ida_ioctl.h"
59 #define READ_AHEAD 128
60 #define NR_CMDS 128 /* This could probably go as high as ~400 */
62 #define MAX_CTLR 8
63 #define CTLR_SHIFT 8
65 static int nr_ctlr = 0;
66 static ctlr_info_t *hba[MAX_CTLR] = { 0, 0, 0, 0, 0, 0, 0, 0 };
68 static int eisa[8] = { 0, 0 ,0 ,0, 0, 0 ,0 ,0 };
70 #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
72 /* board_id = Subsystem Device ID & Vendor ID
73 * product = Marketing Name for the board
74 * access = Address of the struct of function pointers
76 static struct board_type products[] = {
77 { 0x0040110E, "IDA", &smart1_access },
78 { 0x0140110E, "IDA-2", &smart1_access },
79 { 0x1040110E, "IAES", &smart1_access },
80 { 0x2040110E, "SMART", &smart1_access },
81 { 0x3040110E, "SMART-2/E", &smart2e_access },
82 { 0x40300E11, "SMART-2/P", &smart2_access },
83 { 0x40310E11, "SMART-2SL", &smart2_access },
84 { 0x40320E11, "Smart Array 3200", &smart2_access },
85 { 0x40330E11, "Smart Array 3100ES", &smart2_access },
86 { 0x40340E11, "Smart Array 221", &smart2_access },
87 { 0x40400E11, "Integrated Array", &smart4_access },
88 { 0x40500E11, "Smart Array 4200", &smart4_access },
89 { 0x40510E11, "Smart Array 4250ES", &smart4_access },
90 { 0x40580E11, "Smart Array 431", &smart4_access },
93 static struct hd_struct * ida;
94 static int * ida_sizes;
95 static int * ida_blocksizes;
96 static int * ida_hardsizes;
97 static struct gendisk ida_gendisk[MAX_CTLR];
99 static struct proc_dir_entry *proc_array = NULL;
101 /* Debug... */
102 #define DBG(s) do { s } while(0)
103 /* Debug (general info)... */
104 #define DBGINFO(s) do { } while(0)
105 /* Debug Paranoid... */
106 #define DBGP(s) do { } while(0)
107 /* Debug Extra Paranoid... */
108 #define DBGPX(s) do { } while(0)
110 int cpqarray_init(void);
111 static int cpqarray_pci_detect(void);
112 static int cpqarray_pci_init(ctlr_info_t *c, unchar bus, unchar device_fn);
113 static ulong remap_pci_mem(ulong base, ulong size);
114 static int cpqarray_eisa_detect(void);
115 static int pollcomplete(int ctlr);
116 static void getgeometry(int ctlr);
117 static void start_fwbk(int ctlr);
119 static cmdlist_t * cmd_alloc(ctlr_info_t *h);
120 static void cmd_free(ctlr_info_t *h, cmdlist_t *c);
122 static int sendcmd(
123 __u8 cmd,
124 int ctlr,
125 void *buff,
126 size_t size,
127 unsigned int blk,
128 unsigned int blkcnt,
129 unsigned int log_unit );
131 static int ida_open(struct inode *inode, struct file *filep);
132 static int ida_release(struct inode *inode, struct file *filep);
133 static int ida_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);
134 static int ida_ctlr_ioctl(int ctlr, int dsk, ida_ioctl_t *io);
136 static void do_ida_request(int i);
138 * This is a hack. This driver eats a major number for each controller, and
139 * sets blkdev[xxx].request_fn to each one of these so the real request
140 * function knows what controller its working with.
142 #define DO_IDA_REQUEST(x) { do_ida_request(x); }
144 static void do_ida_request0(request_queue_t * q) DO_IDA_REQUEST(0);
145 static void do_ida_request1(request_queue_t * q) DO_IDA_REQUEST(1);
146 static void do_ida_request2(request_queue_t * q) DO_IDA_REQUEST(2);
147 static void do_ida_request3(request_queue_t * q) DO_IDA_REQUEST(3);
148 static void do_ida_request4(request_queue_t * q) DO_IDA_REQUEST(4);
149 static void do_ida_request5(request_queue_t * q) DO_IDA_REQUEST(5);
150 static void do_ida_request6(request_queue_t * q) DO_IDA_REQUEST(6);
151 static void do_ida_request7(request_queue_t * q) DO_IDA_REQUEST(7);
153 static void start_io(ctlr_info_t *h);
155 static inline void addQ(cmdlist_t **Qptr, cmdlist_t *c);
156 static inline cmdlist_t *removeQ(cmdlist_t **Qptr, cmdlist_t *c);
157 static inline void complete_buffers(struct buffer_head *bh, int ok);
158 static inline void complete_command(cmdlist_t *cmd, int timeout);
160 static void do_ida_intr(int irq, void *dev_id, struct pt_regs * regs);
161 static void ida_timer(unsigned long tdata);
162 static int frevalidate_logvol(kdev_t dev);
163 static int revalidate_logvol(kdev_t dev, int maxusage);
164 static int revalidate_allvol(kdev_t dev);
166 #ifdef CONFIG_PROC_FS
167 static void ida_procinit(int i);
168 static int ida_proc_get_info(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
169 #else
170 static void ida_procinit(int i) {}
171 static int ida_proc_get_info(char *buffer, char **start, off_t offset,
172 int length, int *eof, void *data) {}
173 #endif
175 static void ida_geninit(int ctlr)
177 int i,j;
178 drv_info_t *drv;
180 for(i=0; i<NWD; i++) {
181 drv = &hba[ctlr]->drv[i];
182 if (!drv->nr_blks)
183 continue;
184 ida[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)].nr_sects =
185 ida_sizes[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)] =
186 drv->nr_blks;
188 for(j=0; j<16; j++) {
189 ida_blocksizes[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)+j] =
190 1024;
191 ida_hardsizes[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)+j] =
192 drv->blk_size;
194 ida_gendisk[ctlr].nr_real++;
199 static struct block_device_operations ida_fops = {
200 open: ida_open,
201 release: ida_release,
202 ioctl: ida_ioctl,
203 revalidate: frevalidate_logvol,
207 #ifdef CONFIG_PROC_FS
210 * Get us a file in /proc/array that says something about each controller.
211 * Create /proc/array if it doesn't exist yet.
213 static void __init ida_procinit(int i)
215 if (proc_array == NULL) {
216 proc_array = proc_mkdir("driver/array", NULL);
217 if (!proc_array) return;
220 create_proc_read_entry(hba[i]->devname, 0, proc_array,
221 ida_proc_get_info, hba[i]);
225 * Report information about this controller.
227 static int ida_proc_get_info(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
229 off_t pos = 0;
230 off_t len = 0;
231 int size, i, ctlr;
232 ctlr_info_t *h = (ctlr_info_t*)data;
233 drv_info_t *drv;
234 #ifdef CPQ_PROC_PRINT_QUEUES
235 cmdlist_t *c;
236 #endif
238 ctlr = h->ctlr;
239 size = sprintf(buffer, "%s: Compaq %s Controller\n"
240 " Board ID: %08lx\n"
241 " Firmware Revision: %c%c%c%c\n"
242 " Controller Sig: %08lx\n"
243 " Memory Address: %08lx\n"
244 " I/O Port: %04x\n"
245 " IRQ: %x\n"
246 " Logical drives: %d\n"
247 " Physical drives: %d\n\n"
248 " Current Q depth: %d\n"
249 " Max Q depth since init: %d\n\n",
250 h->devname,
251 h->product_name,
252 (unsigned long)h->board_id,
253 h->firm_rev[0], h->firm_rev[1], h->firm_rev[2], h->firm_rev[3],
254 (unsigned long)h->ctlr_sig, (unsigned long)h->vaddr,
255 (unsigned int) h->ioaddr, (unsigned int)h->intr,
256 h->log_drives, h->phys_drives,
257 h->Qdepth, h->maxQsinceinit);
259 pos += size; len += size;
261 size = sprintf(buffer+len, "Logical Drive Info:\n");
262 pos += size; len += size;
264 for(i=0; i<h->log_drives; i++) {
265 drv = &h->drv[i];
266 size = sprintf(buffer+len, "ida/c%dd%d: blksz=%d nr_blks=%d\n",
267 ctlr, i, drv->blk_size, drv->nr_blks);
268 pos += size; len += size;
271 #ifdef CPQ_PROC_PRINT_QUEUES
272 size = sprintf(buffer+len, "\nCurrent Queues:\n");
273 pos += size; len += size;
275 c = h->reqQ;
276 size = sprintf(buffer+len, "reqQ = %p", c); pos += size; len += size;
277 if (c) c=c->next;
278 while(c && c != h->reqQ) {
279 size = sprintf(buffer+len, "->%p", c);
280 pos += size; len += size;
281 c=c->next;
284 c = h->cmpQ;
285 size = sprintf(buffer+len, "\ncmpQ = %p", c); pos += size; len += size;
286 if (c) c=c->next;
287 while(c && c != h->cmpQ) {
288 size = sprintf(buffer+len, "->%p", c);
289 pos += size; len += size;
290 c=c->next;
293 size = sprintf(buffer+len, "\n"); pos += size; len += size;
294 #endif
295 size = sprintf(buffer+len, "nr_allocs = %d\nnr_frees = %d\n",
296 h->nr_allocs, h->nr_frees);
297 pos += size; len += size;
299 *eof = 1;
300 *start = buffer+offset;
301 len -= offset;
302 if (len>length)
303 len = length;
304 return len;
306 #endif /* CONFIG_PROC_FS */
308 #ifdef MODULE
310 MODULE_PARM(eisa, "1-8i");
311 EXPORT_NO_SYMBOLS;
313 /* This is a bit of a hack... */
314 int __init init_module(void)
316 if (cpqarray_init() == 0) /* all the block dev numbers already used */
317 return -EIO; /* or no controllers were found */
318 return 0;
321 void cleanup_module(void)
323 int i;
324 struct gendisk *g;
326 remove_proc_entry("driver/array", NULL);
328 for(i=0; i<nr_ctlr; i++) {
329 hba[i]->access.set_intr_mask(hba[i], 0);
330 free_irq(hba[i]->intr, hba[i]);
331 iounmap((void*)hba[i]->vaddr);
332 unregister_blkdev(MAJOR_NR+i, hba[i]->devname);
333 del_timer(&hba[i]->timer);
334 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR + i));
335 remove_proc_entry(hba[i]->devname, proc_array);
336 kfree(hba[i]->cmd_pool);
337 kfree(hba[i]->cmd_pool_bits);
339 if (gendisk_head == &ida_gendisk[i]) {
340 gendisk_head = ida_gendisk[i].next;
341 } else {
342 for(g=gendisk_head; g; g=g->next) {
343 if (g->next == &ida_gendisk[i]) {
344 g->next = ida_gendisk[i].next;
345 break;
351 kfree(ida);
352 kfree(ida_sizes);
353 kfree(ida_hardsizes);
354 kfree(ida_blocksizes);
356 #endif /* MODULE */
359 * This is it. Find all the controllers and register them. I really hate
360 * stealing all these major device numbers.
361 * returns the number of block devices registered.
363 int __init cpqarray_init(void)
365 void (*request_fns[MAX_CTLR])(request_queue_t *) = {
366 do_ida_request0, do_ida_request1,
367 do_ida_request2, do_ida_request3,
368 do_ida_request4, do_ida_request5,
369 do_ida_request6, do_ida_request7,
371 int i,j;
372 int num_cntlrs_reg = 0;
374 /* detect controllers */
375 cpqarray_pci_detect();
376 cpqarray_eisa_detect();
378 if (nr_ctlr == 0)
379 return(num_cntlrs_reg);
381 printk(DRIVER_NAME "\n");
382 printk("Found %d controller(s)\n", nr_ctlr);
384 /* allocate space for disk structs */
385 ida = kmalloc(sizeof(struct hd_struct)*nr_ctlr*NWD*16, GFP_KERNEL);
386 if(ida==NULL)
388 printk( KERN_ERR "cpqarray: out of memory");
389 return(num_cntlrs_reg);
392 ida_sizes = kmalloc(sizeof(int)*nr_ctlr*NWD*16, GFP_KERNEL);
393 if(ida_sizes==NULL)
395 kfree(ida);
396 printk( KERN_ERR "cpqarray: out of memory");
397 return(num_cntlrs_reg);
400 ida_blocksizes = kmalloc(sizeof(int)*nr_ctlr*NWD*16, GFP_KERNEL);
401 if(ida_blocksizes==NULL)
403 kfree(ida);
404 kfree(ida_sizes);
405 printk( KERN_ERR "cpqarray: out of memory");
406 return(num_cntlrs_reg);
409 ida_hardsizes = kmalloc(sizeof(int)*nr_ctlr*NWD*16, GFP_KERNEL);
410 if(ida_hardsizes==NULL)
412 kfree(ida);
413 kfree(ida_sizes);
414 kfree(ida_blocksizes);
415 printk( KERN_ERR "cpqarray: out of memory");
416 return(num_cntlrs_reg);
419 memset(ida, 0, sizeof(struct hd_struct)*nr_ctlr*NWD*16);
420 memset(ida_sizes, 0, sizeof(int)*nr_ctlr*NWD*16);
421 memset(ida_blocksizes, 0, sizeof(int)*nr_ctlr*NWD*16);
422 memset(ida_hardsizes, 0, sizeof(int)*nr_ctlr*NWD*16);
423 memset(ida_gendisk, 0, sizeof(struct gendisk)*MAX_CTLR);
426 * register block devices
427 * Find disks and fill in structs
428 * Get an interrupt, set the Q depth and get into /proc
430 for(i=0; i< nr_ctlr; i++) {
431 /* If this successful it should insure that we are the only */
432 /* instance of the driver */
433 if (register_blkdev(MAJOR_NR+i, hba[i]->devname, &ida_fops)) {
434 printk(KERN_ERR "cpqarray: Unable to get major number %d for ida\n",
435 MAJOR_NR+i);
436 continue;
440 hba[i]->access.set_intr_mask(hba[i], 0);
441 if (request_irq(hba[i]->intr, do_ida_intr,
442 SA_INTERRUPT|SA_SHIRQ, hba[i]->devname, hba[i])) {
444 printk(KERN_ERR "cpqarray: Unable to get irq %d for %s\n",
445 hba[i]->intr, hba[i]->devname);
446 unregister_blkdev(MAJOR_NR+i, hba[i]->devname);
447 continue;
449 num_cntlrs_reg++;
450 hba[i]->cmd_pool = (cmdlist_t *)kmalloc(
451 NR_CMDS * sizeof(cmdlist_t), GFP_KERNEL);
452 hba[i]->cmd_pool_bits = (__u32*)kmalloc(
453 ((NR_CMDS+31)/32)*sizeof(__u32), GFP_KERNEL);
455 if(hba[i]->cmd_pool_bits == NULL || hba[i]->cmd_pool == NULL)
457 nr_ctlr = i;
458 if(hba[i]->cmd_pool_bits)
459 kfree(hba[i]->cmd_pool_bits);
460 if(hba[i]->cmd_pool)
461 kfree(hba[i]->cmd_pool);
462 free_irq(hba[i]->intr, hba[i]);
463 unregister_blkdev(MAJOR_NR+i, hba[i]->devname);
464 num_cntlrs_reg--;
465 printk( KERN_ERR "cpqarray: out of memory");
467 /* If num_cntlrs_reg == 0, no controllers worked.
468 * init_module will fail, so clean up global
469 * memory that clean_module would do.
472 if (num_cntlrs_reg == 0)
474 kfree(ida);
475 kfree(ida_sizes);
476 kfree(ida_hardsizes);
477 kfree(ida_blocksizes);
479 return(num_cntlrs_reg);
482 memset(hba[i]->cmd_pool, 0, NR_CMDS * sizeof(cmdlist_t));
483 memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+31)/32)*sizeof(__u32));
484 printk(KERN_INFO "cpqarray: Finding drives on %s",
485 hba[i]->devname);
486 getgeometry(i);
487 start_fwbk(i);
489 hba[i]->access.set_intr_mask(hba[i], FIFO_NOT_EMPTY);
492 ida_procinit(i);
494 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR + i),
495 request_fns[i]);
496 blk_queue_headactive(BLK_DEFAULT_QUEUE(MAJOR_NR + i), 0);
497 blksize_size[MAJOR_NR+i] = ida_blocksizes + (i*256);
498 hardsect_size[MAJOR_NR+i] = ida_hardsizes + (i*256);
499 read_ahead[MAJOR_NR+i] = READ_AHEAD;
501 ida_gendisk[i].major = MAJOR_NR + i;
502 ida_gendisk[i].major_name = "ida";
503 ida_gendisk[i].minor_shift = NWD_SHIFT;
504 ida_gendisk[i].max_p = 16;
505 ida_gendisk[i].part = ida + (i*256);
506 ida_gendisk[i].sizes = ida_sizes + (i*256);
507 ida_gendisk[i].nr_real = 0;
509 /* Get on the disk list */
510 ida_gendisk[i].next = gendisk_head;
511 gendisk_head = &ida_gendisk[i];
513 init_timer(&hba[i]->timer);
514 hba[i]->timer.expires = jiffies + IDA_TIMER;
515 hba[i]->timer.data = (unsigned long)hba[i];
516 hba[i]->timer.function = ida_timer;
517 add_timer(&hba[i]->timer);
519 ida_geninit(i);
520 for(j=0; j<NWD; j++)
521 register_disk(&ida_gendisk[i],
522 MKDEV(MAJOR_NR+i,j<<4),
523 16, &ida_fops, hba[i]->drv[j].nr_blks);
526 /* done ! */
527 return(num_cntlrs_reg);
531 * Find the controller and initialize it
532 * Cannot use the class code to search, because older array controllers use
533 * 0x018000 and new ones use 0x010400. So I might as well search for each
534 * each device IDs, being there are only going to be three of them.
536 static int cpqarray_pci_detect(void)
538 int index;
539 unchar bus=0, dev_fn=0;
541 #define IDA_BOARD_TYPES 3
542 static int ida_vendor_id[IDA_BOARD_TYPES] = { PCI_VENDOR_ID_DEC,
543 PCI_VENDOR_ID_NCR, PCI_VENDOR_ID_COMPAQ };
544 static int ida_device_id[IDA_BOARD_TYPES] = { PCI_DEVICE_ID_COMPAQ_42XX, PCI_DEVICE_ID_NCR_53C1510, PCI_DEVICE_ID_COMPAQ_SMART2P };
545 int brdtype;
547 /* search for all PCI board types that could be for this driver */
548 for(brdtype=0; brdtype<IDA_BOARD_TYPES; brdtype++)
550 for(index=0; ; index++) {
551 if (pcibios_find_device(ida_vendor_id[brdtype],
552 ida_device_id[brdtype], index, &bus, &dev_fn))
553 break;
554 printk(KERN_DEBUG "cpqarray: Device %x has been found at %x %x\n",
555 ida_vendor_id[brdtype], bus, dev_fn);
556 if (index == 1000000) break;
557 if (nr_ctlr == 8) {
558 printk(KERN_WARNING "cpqarray: This driver"
559 " supports a maximum of 8 controllers.\n");
560 break;
563 /* if it is a PCI_DEVICE_ID_NCR_53C1510, make sure it's the Compaq version of the chip */
565 if (ida_device_id[brdtype] == PCI_DEVICE_ID_NCR_53C1510) {
566 unsigned short subvendor=0;
567 if(pcibios_read_config_word(bus, dev_fn,
568 PCI_SUBSYSTEM_VENDOR_ID, &subvendor))
570 printk(KERN_DEBUG "cpqarray: failed to read subvendor\n");
571 continue;
573 if(subvendor != PCI_VENDOR_ID_COMPAQ)
575 printk(KERN_DEBUG
576 "cpqarray: not a Compaq integrated array controller\n");
577 continue;
581 hba[nr_ctlr] = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL); if(hba[nr_ctlr]==NULL)
583 printk(KERN_ERR "cpqarray: out of memory.\n");
584 continue;
586 memset(hba[nr_ctlr], 0, sizeof(ctlr_info_t));
587 if (cpqarray_pci_init(hba[nr_ctlr], bus, dev_fn) != 0)
589 kfree(hba[nr_ctlr]);
590 continue;
592 sprintf(hba[nr_ctlr]->devname, "ida%d", nr_ctlr);
593 hba[nr_ctlr]->ctlr = nr_ctlr;
594 nr_ctlr++;
599 return nr_ctlr;
603 * Find the IO address of the controller, its IRQ and so forth. Fill
604 * in some basic stuff into the ctlr_info_t structure.
606 static int cpqarray_pci_init(ctlr_info_t *c, unchar bus, unchar device_fn)
608 ushort vendor_id, device_id, command;
609 unchar cache_line_size, latency_timer;
610 unchar irq, revision;
611 uint addr[6];
612 __u32 board_id;
613 struct pci_dev *pdev;
615 int i;
617 pdev = pci_find_slot(bus, device_fn);
618 vendor_id = pdev->vendor;
619 device_id = pdev->device;
620 irq = pdev->irq;
622 for(i=0; i<6; i++)
623 addr[i] = pdev->resource[i].flags;
625 if (pci_enable_device(pdev))
626 return -1;
628 pci_read_config_word(pdev, PCI_COMMAND, &command);
629 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
630 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_line_size);
631 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency_timer);
633 pci_read_config_dword(pdev, 0x2c, &board_id);
635 DBGINFO(
636 printk("vendor_id = %x\n", vendor_id);
637 printk("device_id = %x\n", device_id);
638 printk("command = %x\n", command);
639 for(i=0; i<6; i++)
640 printk("addr[%d] = %x\n", i, addr[i]);
641 printk("revision = %x\n", revision);
642 printk("irq = %x\n", irq);
643 printk("cache_line_size = %x\n", cache_line_size);
644 printk("latency_timer = %x\n", latency_timer);
645 printk("board_id = %x\n", board_id);
648 c->intr = irq;
649 c->ioaddr = addr[0] & ~0x1;
652 * Memory base addr is first addr with the first bit _not_ set
654 for(i=0; i<6; i++)
655 if (!(addr[i] & 0x1)) {
656 c->paddr = pci_resource_start (pdev, i);
657 break;
659 c->vaddr = remap_pci_mem(c->paddr, 128);
660 c->board_id = board_id;
662 for(i=0; i<NR_PRODUCTS; i++) {
663 if (board_id == products[i].board_id) {
664 c->product_name = products[i].product_name;
665 c->access = *(products[i].access);
666 break;
669 if (i == NR_PRODUCTS) {
670 printk(KERN_WARNING "cpqarray: Sorry, I don't know how"
671 " to access the SMART Array controller %08lx\n",
672 (unsigned long)board_id);
673 return -1;
676 return 0;
680 * Map (physical) PCI mem into (virtual) kernel space
682 static ulong remap_pci_mem(ulong base, ulong size)
684 ulong page_base = ((ulong) base) & PAGE_MASK;
685 ulong page_offs = ((ulong) base) - page_base;
686 ulong page_remapped = (ulong) ioremap(page_base, page_offs+size);
688 return (ulong) (page_remapped ? (page_remapped + page_offs) : 0UL);
691 #ifndef MODULE
692 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,13)
694 * Config string is a comma seperated set of i/o addresses of EISA cards.
696 static int cpqarray_setup(char *str)
698 int i, ints[9];
700 (void)get_options(str, ARRAY_SIZE(ints), ints);
702 for(i=0; i<ints[0] && i<8; i++)
703 eisa[i] = ints[i+1];
704 return 1;
707 __setup("smart2=", cpqarray_setup);
709 #else
712 * Copy the contents of the ints[] array passed to us by init.
714 void cpqarray_setup(char *str, int *ints)
716 int i;
717 for(i=0; i<ints[0] && i<8; i++)
718 eisa[i] = ints[i+1];
720 #endif
721 #endif
724 * Find an EISA controller's signature. Set up an hba if we find it.
726 static int cpqarray_eisa_detect(void)
728 int i=0, j;
729 __u32 board_id;
730 int intr;
732 while(i<8 && eisa[i]) {
733 if (nr_ctlr == 8) {
734 printk(KERN_WARNING "cpqarray: This driver supports"
735 " a maximum of 8 controllers.\n");
736 break;
738 board_id = inl(eisa[i]+0xC80);
739 for(j=0; j < NR_PRODUCTS; j++)
740 if (board_id == products[j].board_id)
741 break;
743 if (j == NR_PRODUCTS) {
744 printk(KERN_WARNING "cpqarray: Sorry, I don't know how"
745 " to access the SMART Array controller %08lx\n", (unsigned long)board_id);
746 continue;
748 hba[nr_ctlr] = (ctlr_info_t *) kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
749 if(hba[nr_ctlr]==NULL)
751 printk(KERN_ERR "cpqarray: out of memory.\n");
752 continue;
754 memset(hba[nr_ctlr], 0, sizeof(ctlr_info_t));
755 hba[nr_ctlr]->ioaddr = eisa[i];
758 * Read the config register to find our interrupt
760 intr = inb(eisa[i]+0xCC0) >> 4;
761 if (intr & 1) intr = 11;
762 else if (intr & 2) intr = 10;
763 else if (intr & 4) intr = 14;
764 else if (intr & 8) intr = 15;
766 hba[nr_ctlr]->intr = intr;
767 sprintf(hba[nr_ctlr]->devname, "ida%d", nr_ctlr);
768 hba[nr_ctlr]->product_name = products[j].product_name;
769 hba[nr_ctlr]->access = *(products[j].access);
770 hba[nr_ctlr]->ctlr = nr_ctlr;
771 hba[nr_ctlr]->board_id = board_id;
773 DBGINFO(
774 printk("i = %d, j = %d\n", i, j);
775 printk("irq = %x\n", intr);
776 printk("product name = %s\n", products[j].product_name);
777 printk("board_id = %x\n", board_id);
780 nr_ctlr++;
781 i++;
784 return nr_ctlr;
789 * Open. Make sure the device is really there.
791 static int ida_open(struct inode *inode, struct file *filep)
793 int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
794 int dsk = MINOR(inode->i_rdev) >> NWD_SHIFT;
796 DBGINFO(printk("ida_open %x (%x:%x)\n", inode->i_rdev, ctlr, dsk) );
797 if (ctlr > MAX_CTLR || hba[ctlr] == NULL)
798 return -ENXIO;
800 if (!suser() && ida_sizes[(ctlr << CTLR_SHIFT) +
801 MINOR(inode->i_rdev)] == 0)
802 return -ENXIO;
805 * Root is allowed to open raw volume zero even if its not configured
806 * so array config can still work. I don't think I really like this,
807 * but I'm already using way to many device nodes to claim another one
808 * for "raw controller".
810 if (suser()
811 && ida_sizes[(ctlr << CTLR_SHIFT) + MINOR(inode->i_rdev)] == 0
812 && MINOR(inode->i_rdev) != 0)
813 return -ENXIO;
815 hba[ctlr]->drv[dsk].usage_count++;
816 hba[ctlr]->usage_count++;
817 MOD_INC_USE_COUNT;
818 return 0;
822 * Close. Sync first.
824 static int ida_release(struct inode *inode, struct file *filep)
826 int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
827 int dsk = MINOR(inode->i_rdev) >> NWD_SHIFT;
829 DBGINFO(printk("ida_release %x (%x:%x)\n", inode->i_rdev, ctlr, dsk) );
831 hba[ctlr]->drv[dsk].usage_count--;
832 hba[ctlr]->usage_count--;
833 MOD_DEC_USE_COUNT;
834 return 0;
838 * Enqueuing and dequeuing functions for cmdlists.
840 static inline void addQ(cmdlist_t **Qptr, cmdlist_t *c)
842 if (*Qptr == NULL) {
843 *Qptr = c;
844 c->next = c->prev = c;
845 } else {
846 c->prev = (*Qptr)->prev;
847 c->next = (*Qptr);
848 (*Qptr)->prev->next = c;
849 (*Qptr)->prev = c;
853 static inline cmdlist_t *removeQ(cmdlist_t **Qptr, cmdlist_t *c)
855 if (c && c->next != c) {
856 if (*Qptr == c) *Qptr = c->next;
857 c->prev->next = c->next;
858 c->next->prev = c->prev;
859 } else {
860 *Qptr = NULL;
862 return c;
866 * Get a request and submit it to the controller.
867 * This routine needs to grab all the requests it possibly can from the
868 * req Q and submit them. Interrupts are off (and need to be off) when you
869 * are in here (either via the dummy do_ida_request functions or by being
870 * called from the interrupt handler
872 static void do_ida_request(int ctlr)
874 ctlr_info_t *h = hba[ctlr];
875 cmdlist_t *c;
876 int seg, sect;
877 char *lastdataend;
878 struct list_head * queue_head;
879 struct buffer_head *bh;
880 struct request *creq;
882 queue_head = &blk_dev[MAJOR_NR+ctlr].request_queue.queue_head;
884 if (list_empty(queue_head))
886 start_io(h);
887 return;
890 creq = blkdev_entry_next_request(queue_head);
891 if (creq->rq_status == RQ_INACTIVE)
893 start_io(h);
894 return;
898 if (ctlr != MAJOR(creq->rq_dev)-MAJOR_NR ||
899 ctlr > nr_ctlr || h == NULL)
901 printk("doreq cmd for %d, %x at %p\n",
902 ctlr, creq->rq_dev, creq);
903 complete_buffers(creq->bh, 0);
904 start_io(h);
905 return;
908 if ((c = cmd_alloc(h)) == NULL)
910 start_io(h);
911 return;
914 bh = creq->bh;
916 c->ctlr = ctlr;
917 c->hdr.unit = MINOR(creq->rq_dev) >> NWD_SHIFT;
918 c->hdr.size = sizeof(rblk_t) >> 2;
919 c->size += sizeof(rblk_t);
921 c->req.hdr.blk = ida[(ctlr<<CTLR_SHIFT) + MINOR(creq->rq_dev)].start_sect + creq->sector;
922 c->bh = bh;
923 DBGPX(
924 if (bh == NULL)
925 panic("bh == NULL?");
927 printk("sector=%d, nr_sectors=%d\n", creq->sector, creq->nr_sectors);
929 seg = 0; lastdataend = NULL;
930 sect = 0;
931 while(bh) {
932 sect += bh->b_size/512;
933 DBGPX(
934 if (bh->b_size % 512) {
935 printk("Oh damn. %d+%d, size = %d\n", creq->sector, sect, bh->b_size);
936 panic("b_size %% 512 != 0");
939 if (bh->b_data == lastdataend) {
940 c->req.sg[seg-1].size += bh->b_size;
941 lastdataend += bh->b_size;
942 } else {
943 c->req.sg[seg].size = bh->b_size;
944 c->req.sg[seg].addr = (__u32)virt_to_bus(bh->b_data);
945 lastdataend = bh->b_data + bh->b_size;
946 if (++seg == SG_MAX)
947 break;
949 bh = bh->b_reqnext;
951 DBGPX( printk("Submitting %d sectors in %d segments\n", sect, seg); );
952 c->req.hdr.sg_cnt = seg;
953 c->req.hdr.blk_cnt = sect;
955 creq->sector += sect;
956 creq->nr_sectors -= sect;
958 /* Ready the next request:
959 * Fix up creq if we still have more buffers in the buffer chain, or
960 * mark this request as done and ready the next one.
962 if (creq->nr_sectors) {
963 DBGPX(
964 if (bh==NULL) {
965 printk("sector=%d, nr_sectors=%d, sect=%d, seg=%d\n",
966 creq->sector, creq->nr_sectors, sect, seg);
967 panic("mother...");
970 creq->bh = bh->b_reqnext;
971 bh->b_reqnext = NULL;
972 DBGPX( printk("More to do on same request %p\n", creq); );
973 } else {
974 DBGPX( printk("Done with %p\n", creq); );
975 blkdev_dequeue_request(creq);
976 end_that_request_last(creq);
979 c->req.hdr.cmd = (creq->cmd == READ) ? IDA_READ : IDA_WRITE;
980 c->type = CMD_RWREQ;
982 /* Put the request on the tail of the request queue */
983 addQ(&h->reqQ, c);
984 h->Qdepth++;
985 if (h->Qdepth > h->maxQsinceinit)
986 h->maxQsinceinit = h->Qdepth;
988 start_io(h);
992 * start_io submits everything on a controller's request queue
993 * and moves it to the completion queue.
995 * Interrupts had better be off if you're in here
997 static void start_io(ctlr_info_t *h)
999 cmdlist_t *c;
1001 while((c = h->reqQ) != NULL) {
1002 /* Can't do anything if we're busy */
1003 if (h->access.fifo_full(h) == 0)
1004 return;
1006 /* Get the first entry from the request Q */
1007 removeQ(&h->reqQ, c);
1008 h->Qdepth--;
1010 /* Tell the controller to do our bidding */
1011 h->access.submit_command(h, c);
1013 /* Get onto the completion Q */
1014 addQ(&h->cmpQ, c);
1018 static inline void complete_buffers(struct buffer_head *bh, int ok)
1020 struct buffer_head *xbh;
1021 while(bh) {
1022 xbh = bh->b_reqnext;
1023 bh->b_reqnext = NULL;
1025 bh->b_end_io(bh, ok);
1027 bh = xbh;
1031 * Mark all buffers that cmd was responsible for
1033 static inline void complete_command(cmdlist_t *cmd, int timeout)
1035 int ok=1;
1037 if (cmd->req.hdr.rcode & RCODE_NONFATAL &&
1038 (hba[cmd->ctlr]->misc_tflags & MISC_NONFATAL_WARN) == 0) {
1039 printk(KERN_WARNING "Non Fatal error on ida/c%dd%d\n",
1040 cmd->ctlr, cmd->hdr.unit);
1041 hba[cmd->ctlr]->misc_tflags |= MISC_NONFATAL_WARN;
1043 if (cmd->req.hdr.rcode & RCODE_FATAL) {
1044 printk(KERN_WARNING "Fatal error on ida/c%dd%d\n",
1045 cmd->ctlr, cmd->hdr.unit);
1046 ok = 0;
1048 if (cmd->req.hdr.rcode & RCODE_INVREQ) {
1049 printk(KERN_WARNING "Invalid request on ida/c%dd%d = (cmd=%x sect=%d cnt=%d sg=%d ret=%x)\n",
1050 cmd->ctlr, cmd->hdr.unit, cmd->req.hdr.cmd,
1051 cmd->req.hdr.blk, cmd->req.hdr.blk_cnt,
1052 cmd->req.hdr.sg_cnt, cmd->req.hdr.rcode);
1053 ok = 0;
1055 if (timeout) ok = 0;
1056 complete_buffers(cmd->bh, ok);
1060 * The controller will interrupt us upon completion of commands.
1061 * Find the command on the completion queue, remove it, tell the OS and
1062 * try to queue up more IO
1064 static void do_ida_intr(int irq, void *dev_id, struct pt_regs *regs)
1066 ctlr_info_t *h = dev_id;
1067 cmdlist_t *c;
1068 unsigned long istat;
1069 unsigned long flags;
1070 __u32 a,a1;
1073 istat = h->access.intr_pending(h);
1074 /* Is this interrupt for us? */
1075 if (istat == 0)
1076 return;
1079 * If there are completed commands in the completion queue,
1080 * we had better do something about it.
1082 spin_lock_irqsave(&io_request_lock, flags);
1083 if (istat & FIFO_NOT_EMPTY) {
1084 while((a = h->access.command_completed(h))) {
1085 a1 = a; a &= ~3;
1086 if ((c = h->cmpQ) == NULL)
1088 printk(KERN_WARNING "cpqarray: Completion of %08lx ignored\n", (unsigned long)a1);
1089 continue;
1091 while(c->busaddr != a) {
1092 c = c->next;
1093 if (c == h->cmpQ)
1094 break;
1097 * If we've found the command, take it off the
1098 * completion Q and free it
1100 if (c->busaddr == a) {
1101 removeQ(&h->cmpQ, c);
1102 if (c->type == CMD_RWREQ) {
1103 complete_command(c, 0);
1104 cmd_free(h, c);
1105 } else if (c->type == CMD_IOCTL_PEND) {
1106 c->type = CMD_IOCTL_DONE;
1108 continue;
1114 * See if we can queue up some more IO
1116 do_ida_request(h->ctlr);
1117 spin_unlock_irqrestore(&io_request_lock, flags);
1121 * This timer was for timing out requests that haven't happened after
1122 * IDA_TIMEOUT. That wasn't such a good idea. This timer is used to
1123 * reset a flags structure so we don't flood the user with
1124 * "Non-Fatal error" messages.
1126 static void ida_timer(unsigned long tdata)
1128 ctlr_info_t *h = (ctlr_info_t*)tdata;
1130 h->timer.expires = jiffies + IDA_TIMER;
1131 add_timer(&h->timer);
1132 h->misc_tflags = 0;
1136 * ida_ioctl does some miscellaneous stuff like reporting drive geometry,
1137 * setting readahead and submitting commands from userspace to the controller.
1139 static int ida_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
1141 int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
1142 int dsk = MINOR(inode->i_rdev) >> NWD_SHIFT;
1143 int error;
1144 int diskinfo[4];
1145 struct hd_geometry *geo = (struct hd_geometry *)arg;
1146 ida_ioctl_t *io = (ida_ioctl_t*)arg;
1147 ida_ioctl_t my_io;
1149 switch(cmd) {
1150 case HDIO_GETGEO:
1151 if (hba[ctlr]->drv[dsk].cylinders) {
1152 diskinfo[0] = hba[ctlr]->drv[dsk].heads;
1153 diskinfo[1] = hba[ctlr]->drv[dsk].sectors;
1154 diskinfo[2] = hba[ctlr]->drv[dsk].cylinders;
1155 } else {
1156 diskinfo[0] = 0xff;
1157 diskinfo[1] = 0x3f;
1158 diskinfo[2] = hba[ctlr]->drv[dsk].nr_blks / (0xff*0x3f);
1160 put_user(diskinfo[0], &geo->heads);
1161 put_user(diskinfo[1], &geo->sectors);
1162 put_user(diskinfo[2], &geo->cylinders);
1163 put_user(ida[(ctlr<<CTLR_SHIFT)+MINOR(inode->i_rdev)].start_sect, &geo->start);
1164 return 0;
1165 case IDAGETDRVINFO:
1166 return copy_to_user(&io->c.drv,&hba[ctlr]->drv[dsk],sizeof(drv_info_t));
1167 case BLKGETSIZE:
1168 if (!arg) return -EINVAL;
1169 put_user(ida[(ctlr<<CTLR_SHIFT)+MINOR(inode->i_rdev)].nr_sects, (long*)arg);
1170 return 0;
1171 case BLKRRPART:
1172 return revalidate_logvol(inode->i_rdev, 1);
1173 case IDAPASSTHRU:
1174 if (!suser()) return -EPERM;
1175 error = copy_from_user(&my_io, io, sizeof(my_io));
1176 if (error) return error;
1177 error = ida_ctlr_ioctl(ctlr, dsk, &my_io);
1178 if (error) return error;
1179 error = copy_to_user(io, &my_io, sizeof(my_io));
1180 return error;
1181 case IDAGETCTLRSIG:
1182 if (!arg) return -EINVAL;
1183 put_user(hba[ctlr]->ctlr_sig, (int*)arg);
1184 return 0;
1185 case IDAREVALIDATEVOLS:
1186 return revalidate_allvol(inode->i_rdev);
1187 case IDADRIVERVERSION:
1188 if (!arg) return -EINVAL;
1189 put_user(DRIVER_VERSION, (unsigned long*)arg);
1190 return 0;
1192 case BLKFLSBUF:
1193 case BLKROSET:
1194 case BLKROGET:
1195 case BLKRASET:
1196 case BLKRAGET:
1197 case BLKPG:
1198 return blk_ioctl(inode->i_rdev, cmd, arg);
1200 default:
1201 return -EBADRQC;
1206 * ida_ctlr_ioctl is for passing commands to the controller from userspace.
1207 * The command block (io) has already been copied to kernel space for us,
1208 * however, any elements in the sglist need to be copied to kernel space
1209 * or copied back to userspace.
1211 * Only root may perform a controller passthru command, however I'm not doing
1212 * any serious sanity checking on the arguments. Doing an IDA_WRITE_MEDIA and
1213 * putting a 64M buffer in the sglist is probably a *bad* idea.
1215 static int ida_ctlr_ioctl(int ctlr, int dsk, ida_ioctl_t *io)
1217 ctlr_info_t *h = hba[ctlr];
1218 cmdlist_t *c;
1219 void *p = NULL;
1220 unsigned long flags;
1221 int error;
1223 if ((c = cmd_alloc(NULL)) == NULL)
1224 return -ENOMEM;
1225 c->ctlr = ctlr;
1226 c->hdr.unit = (io->unit & UNITVALID) ? (io->unit & ~UNITVALID) : dsk;
1227 c->hdr.size = sizeof(rblk_t) >> 2;
1228 c->size += sizeof(rblk_t);
1230 c->req.hdr.cmd = io->cmd;
1231 c->req.hdr.blk = io->blk;
1232 c->req.hdr.blk_cnt = io->blk_cnt;
1233 c->type = CMD_IOCTL_PEND;
1235 /* Pre submit processing */
1236 switch(io->cmd) {
1237 case PASSTHRU_A:
1238 p = kmalloc(io->sg[0].size, GFP_KERNEL);
1239 if (!p)
1241 error = -ENOMEM;
1242 cmd_free(NULL, c);
1243 return(error);
1245 copy_from_user(p, (void*)io->sg[0].addr, io->sg[0].size);
1246 c->req.hdr.blk = virt_to_bus(&(io->c));
1247 c->req.sg[0].size = io->sg[0].size;
1248 c->req.sg[0].addr = virt_to_bus(p);
1249 c->req.hdr.sg_cnt = 1;
1250 break;
1251 case IDA_READ:
1252 p = kmalloc(io->sg[0].size, GFP_KERNEL);
1253 if (!p)
1255 error = -ENOMEM;
1256 cmd_free(NULL, c);
1257 return(error);
1260 c->req.sg[0].size = io->sg[0].size;
1261 c->req.sg[0].addr = virt_to_bus(p);
1262 c->req.hdr.sg_cnt = 1;
1263 break;
1264 case IDA_WRITE:
1265 case IDA_WRITE_MEDIA:
1266 case DIAG_PASS_THRU:
1267 p = kmalloc(io->sg[0].size, GFP_KERNEL);
1268 if (!p)
1270 error = -ENOMEM;
1271 cmd_free(NULL, c);
1272 return(error);
1274 copy_from_user(p, (void*)io->sg[0].addr, io->sg[0].size);
1275 c->req.sg[0].size = io->sg[0].size;
1276 c->req.sg[0].addr = virt_to_bus(p);
1277 c->req.hdr.sg_cnt = 1;
1278 break;
1279 default:
1280 c->req.sg[0].size = sizeof(io->c);
1281 c->req.sg[0].addr = virt_to_bus(&io->c);
1282 c->req.hdr.sg_cnt = 1;
1285 /* Put the request on the tail of the request queue */
1286 spin_lock_irqsave(&io_request_lock, flags);
1287 addQ(&h->reqQ, c);
1288 h->Qdepth++;
1289 start_io(h);
1290 spin_unlock_irqrestore(&io_request_lock, flags);
1292 /* Wait for completion */
1293 while(c->type != CMD_IOCTL_DONE)
1294 schedule();
1296 /* Post submit processing */
1297 switch(io->cmd) {
1298 case PASSTHRU_A:
1299 case IDA_READ:
1300 case DIAG_PASS_THRU:
1301 copy_to_user((void*)io->sg[0].addr, p, io->sg[0].size);
1302 /* fall through and free p */
1303 case IDA_WRITE:
1304 case IDA_WRITE_MEDIA:
1305 kfree(p);
1306 break;
1307 default:
1308 /* Nothing to do */
1311 io->rcode = c->req.hdr.rcode;
1312 cmd_free(NULL, c);
1313 return(0);
1317 * Commands are pre-allocated in a large block. Here we use a simple bitmap
1318 * scheme to suballocte them to the driver. Operations that are not time
1319 * critical (and can wait for kmalloc and possibly sleep) can pass in NULL
1320 * as the first argument to get a new command.
1322 static cmdlist_t * cmd_alloc(ctlr_info_t *h)
1324 cmdlist_t * c;
1325 int i;
1327 if (h == NULL) {
1328 c = (cmdlist_t*)kmalloc(sizeof(cmdlist_t), GFP_KERNEL);
1329 if(c==NULL)
1330 return NULL;
1331 } else {
1332 do {
1333 i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
1334 if (i == NR_CMDS)
1335 return NULL;
1336 } while(test_and_set_bit(i%32, h->cmd_pool_bits+(i/32)) != 0);
1337 c = h->cmd_pool + i;
1338 h->nr_allocs++;
1341 memset(c, 0, sizeof(cmdlist_t));
1342 c->busaddr = virt_to_bus(c);
1343 return c;
1346 static void cmd_free(ctlr_info_t *h, cmdlist_t *c)
1348 int i;
1350 if (h == NULL) {
1351 kfree(c);
1352 } else {
1353 i = c - h->cmd_pool;
1354 clear_bit(i%32, h->cmd_pool_bits+(i/32));
1355 h->nr_frees++;
1359 /***********************************************************************
1360 name: sendcmd
1361 Send a command to an IDA using the memory mapped FIFO interface
1362 and wait for it to complete.
1363 This routine should only be called at init time.
1364 ***********************************************************************/
1365 static int sendcmd(
1366 __u8 cmd,
1367 int ctlr,
1368 void *buff,
1369 size_t size,
1370 unsigned int blk,
1371 unsigned int blkcnt,
1372 unsigned int log_unit )
1374 cmdlist_t *c;
1375 int complete;
1376 unsigned long temp;
1377 unsigned long i;
1378 ctlr_info_t *info_p = hba[ctlr];
1380 c = cmd_alloc(info_p);
1381 c->ctlr = ctlr;
1382 c->hdr.unit = log_unit;
1383 c->hdr.prio = 0;
1384 c->hdr.size = sizeof(rblk_t) >> 2;
1385 c->size += sizeof(rblk_t);
1387 /* The request information. */
1388 c->req.hdr.next = 0;
1389 c->req.hdr.rcode = 0;
1390 c->req.bp = 0;
1391 c->req.hdr.sg_cnt = 1;
1392 c->req.hdr.reserved = 0;
1394 if (size == 0)
1395 c->req.sg[0].size = 512;
1396 else
1397 c->req.sg[0].size = size;
1399 c->req.hdr.blk = blk;
1400 c->req.hdr.blk_cnt = blkcnt;
1401 c->req.hdr.cmd = (unsigned char) cmd;
1402 c->req.sg[0].addr = (__u32) virt_to_bus(buff);
1404 * Disable interrupt
1406 info_p->access.set_intr_mask(info_p, 0);
1407 /* Make sure there is room in the command FIFO */
1408 /* Actually it should be completely empty at this time. */
1409 for (i = 200000; i > 0; i--) {
1410 temp = info_p->access.fifo_full(info_p);
1411 if (temp != 0) {
1412 break;
1414 udelay(10);
1415 DBG(
1416 printk(KERN_WARNING "cpqarray ida%d: idaSendPciCmd FIFO full,"
1417 " waiting!\n", ctlr);
1421 * Send the cmd
1423 info_p->access.submit_command(info_p, c);
1424 complete = pollcomplete(ctlr);
1425 if (complete != 1) {
1426 if (complete != c->busaddr) {
1427 printk( KERN_WARNING
1428 "cpqarray ida%d: idaSendPciCmd "
1429 "Invalid command list address returned! (%08lx)\n",
1430 ctlr, (unsigned long)complete);
1431 cmd_free(info_p, c);
1432 return (IO_ERROR);
1434 } else {
1435 printk( KERN_WARNING
1436 "cpqarray ida%d: idaSendPciCmd Timeout out, "
1437 "No command list address returned!\n",
1438 ctlr);
1439 cmd_free(info_p, c);
1440 return (IO_ERROR);
1443 if (c->req.hdr.rcode & 0x00FE) {
1444 if (!(c->req.hdr.rcode & BIG_PROBLEM)) {
1445 printk( KERN_WARNING
1446 "cpqarray ida%d: idaSendPciCmd, error: "
1447 "Controller failed at init time "
1448 "cmd: 0x%x, return code = 0x%x\n",
1449 ctlr, c->req.hdr.cmd, c->req.hdr.rcode);
1451 cmd_free(info_p, c);
1452 return (IO_ERROR);
1455 cmd_free(info_p, c);
1456 return (IO_OK);
1459 static int frevalidate_logvol(kdev_t dev)
1461 return revalidate_logvol(dev, 0);
1465 * revalidate_allvol is for online array config utilities. After a
1466 * utility reconfigures the drives in the array, it can use this function
1467 * (through an ioctl) to make the driver zap any previous disk structs for
1468 * that controller and get new ones.
1470 * Right now I'm using the getgeometry() function to do this, but this
1471 * function should probably be finer grained and allow you to revalidate one
1472 * particualar logical volume (instead of all of them on a particular
1473 * controller).
1475 static int revalidate_allvol(kdev_t dev)
1477 int ctlr, i;
1478 unsigned long flags;
1480 ctlr = MAJOR(dev) - MAJOR_NR;
1481 if (MINOR(dev) != 0)
1482 return -ENXIO;
1484 spin_lock_irqsave(&io_request_lock, flags);
1485 if (hba[ctlr]->usage_count > 1) {
1486 spin_unlock_irqrestore(&io_request_lock, flags);
1487 printk(KERN_WARNING "cpqarray: Device busy for volume"
1488 " revalidation (usage=%d)\n", hba[ctlr]->usage_count);
1489 return -EBUSY;
1491 spin_unlock_irqrestore(&io_request_lock, flags);
1492 hba[ctlr]->usage_count++;
1495 * Set the partition and block size structures for all volumes
1496 * on this controller to zero. We will reread all of this data
1498 memset(ida+(ctlr*256), 0, sizeof(struct hd_struct)*NWD*16);
1499 memset(ida_sizes+(ctlr*256), 0, sizeof(int)*NWD*16);
1500 memset(ida_blocksizes+(ctlr*256), 0, sizeof(int)*NWD*16);
1501 memset(ida_hardsizes+(ctlr*256), 0, sizeof(int)*NWD*16);
1502 memset(hba[ctlr]->drv, 0, sizeof(drv_info_t)*NWD);
1503 ida_gendisk[ctlr].nr_real = 0;
1506 * Tell the array controller not to give us any interupts while
1507 * we check the new geometry. Then turn interrupts back on when
1508 * we're done.
1510 hba[ctlr]->access.set_intr_mask(hba[ctlr], 0);
1511 getgeometry(ctlr);
1512 hba[ctlr]->access.set_intr_mask(hba[ctlr], FIFO_NOT_EMPTY);
1514 ida_geninit(ctlr);
1515 for(i=0; i<NWD; i++)
1516 if (ida_sizes[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)])
1517 revalidate_logvol(dev+(i<<NWD_SHIFT), 2);
1519 hba[ctlr]->usage_count--;
1520 return 0;
1523 /* Borrowed and adapted from sd.c */
1524 static int revalidate_logvol(kdev_t dev, int maxusage)
1526 int ctlr, target;
1527 struct gendisk *gdev;
1528 unsigned long flags;
1529 int max_p;
1530 int start;
1531 int i;
1533 target = DEVICE_NR(dev);
1534 ctlr = MAJOR(dev) - MAJOR_NR;
1535 gdev = &ida_gendisk[ctlr];
1537 spin_lock_irqsave(&io_request_lock, flags);
1538 if (hba[ctlr]->drv[target].usage_count > maxusage) {
1539 spin_unlock_irqrestore(&io_request_lock, flags);
1540 printk(KERN_WARNING "cpqarray: Device busy for "
1541 "revalidation (usage=%d)\n",
1542 hba[ctlr]->drv[target].usage_count);
1543 return -EBUSY;
1546 hba[ctlr]->drv[target].usage_count++;
1547 spin_unlock_irqrestore(&io_request_lock, flags);
1549 max_p = gdev->max_p;
1550 start = target << gdev->minor_shift;
1552 for(i=max_p; i>=0; i--) {
1553 int minor = start+i;
1554 kdev_t devi = MKDEV(MAJOR_NR + ctlr, minor);
1555 struct super_block *sb = get_super(devi);
1556 sync_dev(devi);
1557 if (sb) invalidate_inodes(sb);
1558 invalidate_buffers(devi);
1559 gdev->part[minor].start_sect = 0;
1560 gdev->part[minor].nr_sects = 0;
1562 /* reset the blocksize so we can read the partition table */
1563 blksize_size[MAJOR_NR+ctlr][minor] = 1024;
1566 /* 16 minors per disk... */
1567 grok_partitions(gdev, target, 16, hba[ctlr]->drv[target].nr_blks);
1568 hba[ctlr]->drv[target].usage_count--;
1569 return 0;
1573 /********************************************************************
1574 name: pollcomplete
1575 Wait polling for a command to complete.
1576 The memory mapped FIFO is polled for the completion.
1577 Used only at init time, interrupts disabled.
1578 ********************************************************************/
1579 static int pollcomplete(int ctlr)
1581 int done;
1582 int i;
1584 /* Wait (up to 2 seconds) for a command to complete */
1586 for (i = 200000; i > 0; i--) {
1587 done = hba[ctlr]->access.command_completed(hba[ctlr]);
1588 if (done == 0) {
1589 udelay(10); /* a short fixed delay */
1590 } else
1591 return (done);
1593 /* Invalid address to tell caller we ran out of time */
1594 return 1;
1596 /*****************************************************************
1597 start_fwbk
1598 Starts controller firmwares background processing.
1599 Currently only the Integrated Raid controller needs this done.
1600 If the PCI mem address registers are written to after this,
1601 data corruption may occur
1602 *****************************************************************/
1603 static void start_fwbk(int ctlr)
1605 id_ctlr_t *id_ctlr_buf;
1606 int ret_code;
1608 if( (hba[ctlr]->board_id != 0x40400E11)
1609 && (hba[ctlr]->board_id != 0x40480E11) )
1611 /* Not a Integrated Raid, so there is nothing for us to do */
1612 return;
1613 printk(KERN_DEBUG "cpqarray: Starting firmware's background"
1614 " processing\n");
1615 /* Command does not return anything, but idasend command needs a
1616 buffer */
1617 id_ctlr_buf = (id_ctlr_t *)kmalloc(sizeof(id_ctlr_t), GFP_KERNEL);
1618 if(id_ctlr_buf==NULL)
1620 printk(KERN_WARNING "cpqarray: Out of memory. "
1621 "Unable to start background processing.\n");
1622 return;
1624 ret_code = sendcmd(RESUME_BACKGROUND_ACTIVITY, ctlr,
1625 id_ctlr_buf, 0, 0, 0, 0);
1626 if(ret_code != IO_OK)
1627 printk(KERN_WARNING "cpqarray: Unable to start"
1628 " background processing\n");
1630 kfree(id_ctlr_buf);
1632 /*****************************************************************
1633 getgeometry
1634 Get ida logical volume geometry from the controller
1635 This is a large bit of code which once existed in two flavors,
1636 It is used only at init time.
1637 *****************************************************************/
1638 static void getgeometry(int ctlr)
1640 id_log_drv_t *id_ldrive;
1641 id_ctlr_t *id_ctlr_buf;
1642 sense_log_drv_stat_t *id_lstatus_buf;
1643 config_t *sense_config_buf;
1644 unsigned int log_unit, log_index;
1645 int ret_code, size;
1646 drv_info_t *drv;
1647 ctlr_info_t *info_p = hba[ctlr];
1649 info_p->log_drv_map = 0;
1651 id_ldrive = (id_log_drv_t *)kmalloc(sizeof(id_log_drv_t), GFP_KERNEL);
1652 if(id_ldrive == NULL)
1654 printk( KERN_ERR "cpqarray: out of memory.\n");
1655 return;
1658 id_ctlr_buf = (id_ctlr_t *)kmalloc(sizeof(id_ctlr_t), GFP_KERNEL);
1659 if(id_ctlr_buf == NULL)
1661 kfree(id_ldrive);
1662 printk( KERN_ERR "cpqarray: out of memory.\n");
1663 return;
1666 id_lstatus_buf = (sense_log_drv_stat_t *)kmalloc(sizeof(sense_log_drv_stat_t), GFP_KERNEL);
1667 if(id_lstatus_buf == NULL)
1669 kfree(id_ctlr_buf);
1670 kfree(id_ldrive);
1671 printk( KERN_ERR "cpqarray: out of memory.\n");
1672 return;
1675 sense_config_buf = (config_t *)kmalloc(sizeof(config_t), GFP_KERNEL);
1676 if(sense_config_buf == NULL)
1678 kfree(id_lstatus_buf);
1679 kfree(id_ctlr_buf);
1680 kfree(id_ldrive);
1681 printk( KERN_ERR "cpqarray: out of memory.\n");
1682 return;
1685 memset(id_ldrive, 0, sizeof(id_log_drv_t));
1686 memset(id_ctlr_buf, 0, sizeof(id_ctlr_t));
1687 memset(id_lstatus_buf, 0, sizeof(sense_log_drv_stat_t));
1688 memset(sense_config_buf, 0, sizeof(config_t));
1690 info_p->phys_drives = 0;
1691 info_p->log_drv_map = 0;
1692 info_p->drv_assign_map = 0;
1693 info_p->drv_spare_map = 0;
1694 info_p->mp_failed_drv_map = 0; /* only initialized here */
1695 /* Get controllers info for this logical drive */
1696 ret_code = sendcmd(ID_CTLR, ctlr, id_ctlr_buf, 0, 0, 0, 0);
1697 if (ret_code == IO_ERROR) {
1699 * If can't get controller info, set the logical drive map to 0,
1700 * so the idastubopen will fail on all logical drives
1701 * on the controller.
1703 /* Free all the buffers and return */
1704 printk(KERN_ERR "cpqarray: error sending ID controller\n");
1705 kfree(sense_config_buf);
1706 kfree(id_lstatus_buf);
1707 kfree(id_ctlr_buf);
1708 kfree(id_ldrive);
1709 return;
1712 info_p->log_drives = id_ctlr_buf->nr_drvs;;
1713 *(__u32*)(info_p->firm_rev) = *(__u32*)(id_ctlr_buf->firm_rev);
1714 info_p->ctlr_sig = id_ctlr_buf->cfg_sig;
1716 printk(" (%s)\n", info_p->product_name);
1718 * Initialize logical drive map to zero
1720 log_index = 0;
1722 * Get drive geometry for all logical drives
1724 if (id_ctlr_buf->nr_drvs > 16)
1725 printk(KERN_WARNING "cpqarray ida%d: This driver supports "
1726 "16 logical drives per controller.\n. "
1727 " Additional drives will not be "
1728 "detected\n", ctlr);
1730 for (log_unit = 0;
1731 (log_index < id_ctlr_buf->nr_drvs)
1732 && (log_unit < NWD);
1733 log_unit++) {
1735 size = sizeof(sense_log_drv_stat_t);
1738 Send "Identify logical drive status" cmd
1740 ret_code = sendcmd(SENSE_LOG_DRV_STAT,
1741 ctlr, id_lstatus_buf, size, 0, 0, log_unit);
1742 if (ret_code == IO_ERROR) {
1744 If can't get logical drive status, set
1745 the logical drive map to 0, so the
1746 idastubopen will fail for all logical drives
1747 on the controller.
1749 info_p->log_drv_map = 0;
1750 printk( KERN_WARNING
1751 "cpqarray ida%d: idaGetGeometry - Controller"
1752 " failed to report status of logical drive %d\n"
1753 "Access to this controller has been disabled\n",
1754 ctlr, log_unit);
1755 /* Free all the buffers and return */
1756 kfree(sense_config_buf);
1757 kfree(id_lstatus_buf);
1758 kfree(id_ctlr_buf);
1759 kfree(id_ldrive);
1760 return;
1763 Make sure the logical drive is configured
1765 if (id_lstatus_buf->status != LOG_NOT_CONF) {
1766 ret_code = sendcmd(ID_LOG_DRV, ctlr, id_ldrive,
1767 sizeof(id_log_drv_t), 0, 0, log_unit);
1769 If error, the bit for this
1770 logical drive won't be set and
1771 idastubopen will return error.
1773 if (ret_code != IO_ERROR) {
1774 drv = &info_p->drv[log_unit];
1775 drv->blk_size = id_ldrive->blk_size;
1776 drv->nr_blks = id_ldrive->nr_blks;
1777 drv->cylinders = id_ldrive->drv.cyl;
1778 drv->heads = id_ldrive->drv.heads;
1779 drv->sectors = id_ldrive->drv.sect_per_track;
1780 info_p->log_drv_map |= (1 << log_unit);
1782 printk(KERN_INFO "cpqarray ida/c%dd%d: blksz=%d nr_blks=%d\n",
1783 ctlr, log_unit, drv->blk_size, drv->nr_blks);
1784 ret_code = sendcmd(SENSE_CONFIG,
1785 ctlr, sense_config_buf,
1786 sizeof(config_t), 0, 0, log_unit);
1787 if (ret_code == IO_ERROR) {
1788 info_p->log_drv_map = 0;
1789 /* Free all the buffers and return */
1790 printk(KERN_ERR "cpqarray: error sending sense config\n");
1791 kfree(sense_config_buf);
1792 kfree(id_lstatus_buf);
1793 kfree(id_ctlr_buf);
1794 kfree(id_ldrive);
1795 return;
1798 info_p->phys_drives =
1799 sense_config_buf->ctlr_phys_drv;
1800 info_p->drv_assign_map
1801 |= sense_config_buf->drv_asgn_map;
1802 info_p->drv_assign_map
1803 |= sense_config_buf->spare_asgn_map;
1804 info_p->drv_spare_map
1805 |= sense_config_buf->spare_asgn_map;
1806 } /* end of if no error on id_ldrive */
1807 log_index = log_index + 1;
1808 } /* end of if logical drive configured */
1809 } /* end of for log_unit */
1810 kfree(sense_config_buf);
1811 kfree(id_ldrive);
1812 kfree(id_lstatus_buf);
1813 kfree(id_ctlr_buf);
1814 return;