Import 2.3.15pre2
[davej-history.git] / drivers / block / cpqarray.c
blob544a2958358fbab0257ff0bf0e824869ec655541
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/module.h>
26 #include <linux/version.h>
27 #include <linux/types.h>
28 #include <linux/pci.h>
29 #include <linux/kernel.h>
30 #include <linux/malloc.h>
31 #include <linux/delay.h>
32 #include <linux/major.h>
33 #include <linux/fs.h>
34 #include <linux/blkpg.h>
35 #include <linux/timer.h>
36 #include <linux/proc_fs.h>
37 #include <linux/hdreg.h>
38 #include <asm/uaccess.h>
39 #include <asm/spinlock.h>
40 #include <asm/io.h>
43 #define SMART2_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
45 #define DRIVER_NAME "Compaq SMART2 Driver (v 1.0.4)"
46 #define DRIVER_VERSION SMART2_DRIVER_VERSION(1,0,4)
47 #define MAJOR_NR COMPAQ_SMART2_MAJOR
48 #include <linux/blk.h>
49 #include <linux/blkdev.h>
50 #include <linux/genhd.h>
52 #include "cpqarray.h"
53 #include "ida_cmd.h"
54 #include "smart1,2.h"
55 #include "ida_ioctl.h"
57 #define READ_AHEAD 128
58 #define NR_CMDS 128 /* This could probably go as high as ~400 */
60 #define MAX_CTLR 8
61 #define CTLR_SHIFT 8
63 static int nr_ctlr = 0;
64 static ctlr_info_t *hba[MAX_CTLR] = { 0, 0, 0, 0, 0, 0, 0, 0 };
66 static int eisa[8] = { 0, 0 ,0 ,0, 0, 0 ,0 ,0 };
68 #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
70 /* board_id = Subsystem Device ID & Vendor ID
71 * product = Marketing Name for the board
72 * access = Address of the struct of function pointers
74 struct board_type products[] = {
75 { 0x0040110E, "IDA", &smart1_access },
76 { 0x0140110E, "IDA-2", &smart1_access },
77 { 0x1040110E, "IAES", &smart1_access },
78 { 0x2040110E, "SMART", &smart1_access },
79 { 0x3040110E, "SMART-2/E", &smart2e_access },
80 { 0x40300E11, "SMART-2/P", &smart2_access },
81 { 0x40310E11, "SMART-2SL", &smart2_access },
82 { 0x40320E11, "Smart Array 3200", &smart2_access },
83 { 0x40330E11, "Smart Array 3100ES", &smart2_access },
84 { 0x40340E11, "Smart Array 221", &smart2_access },
85 { 0x40400E11, "Integrated Array", &smart4_access },
86 { 0x40500E11, "Smart Array 4200", &smart4_access },
87 { 0x40510E11, "Smart Array 4250ES", &smart4_access },
90 static struct hd_struct * ida;
91 static int * ida_sizes;
92 static int * ida_blocksizes;
93 static int * ida_hardsizes;
94 static struct gendisk ida_gendisk[MAX_CTLR];
96 struct proc_dir_entry *proc_array = NULL;
98 /* Debug... */
99 #define DBG(s) do { s } while(0)
100 /* Debug (general info)... */
101 #define DBGINFO(s) do { } while(0)
102 /* Debug Paranoid... */
103 #define DBGP(s) do { } while(0)
104 /* Debug Extra Paranoid... */
105 #define DBGPX(s) do { } while(0)
107 void cpqarray_init(void);
108 static int cpqarray_pci_detect(void);
109 static int cpqarray_pci_init(ctlr_info_t *c, unchar bus, unchar device_fn);
110 static ulong remap_pci_mem(ulong base, ulong size);
111 static int cpqarray_eisa_detect(void);
112 static int pollcomplete(int ctlr);
113 static void getgeometry(int ctlr);
114 static void start_fwbk(int ctlr);
116 static cmdlist_t * cmd_alloc(ctlr_info_t *h);
117 static void cmd_free(ctlr_info_t *h, cmdlist_t *c);
119 static int sendcmd(
120 __u8 cmd,
121 int ctlr,
122 void *buff,
123 size_t size,
124 unsigned int blk,
125 unsigned int blkcnt,
126 unsigned int log_unit );
128 static int ida_open(struct inode *inode, struct file *filep);
129 static int ida_release(struct inode *inode, struct file *filep);
130 static int ida_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);
131 static int ida_ctlr_ioctl(int ctlr, int dsk, ida_ioctl_t *io);
133 static void do_ida_request(int i);
135 * This is a hack. This driver eats a major number for each controller, and
136 * sets blkdev[xxx].request_fn to each one of these so the real request
137 * function knows what controller its working with.
139 #define DO_IDA_REQUEST(x) { do_ida_request(x); }
141 static void do_ida_request0(void) DO_IDA_REQUEST(0);
142 static void do_ida_request1(void) DO_IDA_REQUEST(1);
143 static void do_ida_request2(void) DO_IDA_REQUEST(2);
144 static void do_ida_request3(void) DO_IDA_REQUEST(3);
145 static void do_ida_request4(void) DO_IDA_REQUEST(4);
146 static void do_ida_request5(void) DO_IDA_REQUEST(5);
147 static void do_ida_request6(void) DO_IDA_REQUEST(6);
148 static void do_ida_request7(void) DO_IDA_REQUEST(7);
150 static void start_io(ctlr_info_t *h);
152 static inline void addQ(cmdlist_t **Qptr, cmdlist_t *c);
153 static inline cmdlist_t *removeQ(cmdlist_t **Qptr, cmdlist_t *c);
154 static inline void complete_buffers(struct buffer_head *bh, int ok);
155 static inline void complete_command(cmdlist_t *cmd, int timeout);
157 static void do_ida_intr(int irq, void *dev_id, struct pt_regs * regs);
158 static void ida_timer(unsigned long tdata);
159 static int frevalidate_logvol(kdev_t dev);
160 static int revalidate_logvol(kdev_t dev, int maxusage);
161 static int revalidate_allvol(kdev_t dev);
163 static void ida_procinit(int i);
164 static int ida_proc_get_info(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
166 static void ida_geninit(struct gendisk *g)
168 int ctlr = g-ida_gendisk;
169 int i,j;
170 drv_info_t *drv;
172 for(i=0; i<NWD; i++) {
173 drv = &hba[ctlr]->drv[i];
174 if (!drv->nr_blks)
175 continue;
176 ida[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)].nr_sects =
177 ida_sizes[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)] =
178 drv->nr_blks;
180 for(j=0; j<16; j++) {
181 ida_blocksizes[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)+j] =
182 1024;
183 ida_hardsizes[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)+j] =
184 drv->blk_size;
186 ida_gendisk[ctlr].nr_real++;
191 struct file_operations ida_fops = {
192 NULL, /* lseek - default */
193 block_read, /* read - general block-dev read */
194 block_write, /* write - general block-dev write */
195 NULL, /* readdir - bad */
196 NULL, /* select */
197 ida_ioctl, /* ioctl */
198 NULL, /* mmap */
199 ida_open, /* open code */
200 NULL,
201 ida_release, /* release */
202 block_fsync, /* fsync */
203 NULL, /* fasync */
204 NULL, /* Disk change */
205 frevalidate_logvol, /* revalidate */
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 ida_procinit(int i)
215 struct proc_dir_entry *pd;
217 if (proc_array == NULL) {
218 proc_array = create_proc_entry("array", S_IFDIR|S_IRUGO|S_IXUGO,
219 &proc_root);
220 if (!proc_array) return;
223 pd = create_proc_entry(hba[i]->devname, S_IFREG|S_IRUGO, proc_array);
224 if (!pd) return;
225 pd->read_proc = ida_proc_get_info;
226 pd->data = hba[i];
230 * Report information about this controller.
232 static int ida_proc_get_info(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
234 off_t pos = 0;
235 off_t len = 0;
236 int size, i, ctlr;
237 ctlr_info_t *h = (ctlr_info_t*)data;
238 drv_info_t *drv;
239 #ifdef CPQ_PROC_PRINT_QUEUES
240 cmdlist_t *c;
241 #endif
243 ctlr = h->ctlr;
244 size = sprintf(buffer, "%s: Compaq %s Controller\n"
245 " Board ID: %08lx\n"
246 " Firmware Revision: %c%c%c%c\n"
247 " Controller Sig: %08lx\n"
248 " Memory Address: %08lx\n"
249 " I/O Port: %04x\n"
250 " IRQ: %x\n"
251 " Logical drives: %d\n"
252 " Physical drives: %d\n\n"
253 " Current Q depth: %d\n"
254 " Max Q depth since init: %d\n\n",
255 h->devname,
256 h->product_name,
257 (unsigned long)h->board_id,
258 h->firm_rev[0], h->firm_rev[1], h->firm_rev[2], h->firm_rev[3],
259 (unsigned long)h->ctlr_sig, (unsigned long)h->vaddr,
260 (unsigned int) h->ioaddr, (unsigned int)h->intr,
261 h->log_drives, h->phys_drives,
262 h->Qdepth, h->maxQsinceinit);
264 pos += size; len += size;
266 size = sprintf(buffer+len, "Logical Drive Info:\n");
267 pos += size; len += size;
269 for(i=0; i<h->log_drives; i++) {
270 drv = &h->drv[i];
271 size = sprintf(buffer+len, "ida/c%dd%d: blksz=%d nr_blks=%d\n",
272 ctlr, i, drv->blk_size, drv->nr_blks);
273 pos += size; len += size;
276 #ifdef CPQ_PROC_PRINT_QUEUES
277 size = sprintf(buffer+len, "\nCurrent Queues:\n");
278 pos += size; len += size;
280 c = h->reqQ;
281 size = sprintf(buffer+len, "reqQ = %p", c); pos += size; len += size;
282 if (c) c=c->next;
283 while(c && c != h->reqQ) {
284 size = sprintf(buffer+len, "->%p", c);
285 pos += size; len += size;
286 c=c->next;
289 c = h->cmpQ;
290 size = sprintf(buffer+len, "\ncmpQ = %p", c); pos += size; len += size;
291 if (c) c=c->next;
292 while(c && c != h->cmpQ) {
293 size = sprintf(buffer+len, "->%p", c);
294 pos += size; len += size;
295 c=c->next;
298 size = sprintf(buffer+len, "\n"); pos += size; len += size;
299 #endif
300 size = sprintf(buffer+len, "nr_allocs = %d\nnr_frees = %d\n",
301 h->nr_allocs, h->nr_frees);
302 pos += size; len += size;
304 *eof = 1;
305 *start = buffer+offset;
306 len -= offset;
307 if (len>length)
308 len = length;
309 return len;
312 #ifdef MODULE
314 MODULE_PARM(eisa, "1-8i");
315 EXPORT_NO_SYMBOLS;
317 /* This is a bit of a hack... */
318 int init_module(void)
320 int i, j;
321 cpqarray_init();
322 if (nr_ctlr == 0)
323 return -EIO;
325 for(i=0; i<nr_ctlr; i++) {
326 ida_geninit(&ida_gendisk[i]);
327 for(j=0; j<NWD; j++)
328 if (ida_sizes[(i<<CTLR_SHIFT) + (j<<NWD_SHIFT)])
329 resetup_one_dev(&ida_gendisk[i], j);
331 return 0;
333 void cleanup_module(void)
335 int i;
336 struct gendisk *g;
338 for(i=0; i<nr_ctlr; i++) {
339 hba[i]->access.set_intr_mask(hba[i], 0);
340 free_irq(hba[i]->intr, hba[i]);
341 iounmap((void*)hba[i]->vaddr);
342 unregister_blkdev(MAJOR_NR+i, hba[i]->devname);
343 del_timer(&hba[i]->timer);
344 remove_proc_entry(hba[i]->devname, proc_array);
345 kfree(hba[i]->cmd_pool);
346 kfree(hba[i]->cmd_pool_bits);
348 if (gendisk_head == &ida_gendisk[i]) {
349 gendisk_head = ida_gendisk[i].next;
350 } else {
351 for(g=gendisk_head; g; g=g->next) {
352 if (g->next == &ida_gendisk[i]) {
353 g->next = ida_gendisk[i].next;
354 break;
359 remove_proc_entry("array", &proc_root);
360 kfree(ida);
361 kfree(ida_sizes);
362 kfree(ida_hardsizes);
363 kfree(ida_blocksizes);
367 #endif /* MODULE */
370 * This is it. Find all the controllers and register them. I really hate
371 * stealing all these major device numbers.
373 void cpqarray_init(void)
375 void (*request_fns[MAX_CTLR])(void) = {
376 do_ida_request0, do_ida_request1,
377 do_ida_request2, do_ida_request3,
378 do_ida_request4, do_ida_request5,
379 do_ida_request6, do_ida_request7,
381 int i;
383 /* detect controllers */
384 cpqarray_pci_detect();
385 cpqarray_eisa_detect();
387 if (nr_ctlr == 0)
388 return;
390 printk(DRIVER_NAME "\n");
391 printk("Found %d controller(s)\n", nr_ctlr);
393 /* allocate space for disk structs */
394 ida = kmalloc(sizeof(struct hd_struct)*nr_ctlr*NWD*16, GFP_KERNEL);
396 if(ida==NULL)
397 goto bail;
398 ida_sizes = kmalloc(sizeof(int)*nr_ctlr*NWD*16, GFP_KERNEL);
399 if(ida_sizes==NULL)
400 goto bail2;
401 ida_blocksizes = kmalloc(sizeof(int)*nr_ctlr*NWD*16, GFP_KERNEL);
402 if(ida_blocksizes==NULL)
403 goto bail3;
404 ida_hardsizes = kmalloc(sizeof(int)*nr_ctlr*NWD*16, GFP_KERNEL);
405 if(ida_hardsizes==NULL)
406 goto bail4;
408 memset(ida, 0, sizeof(struct hd_struct)*nr_ctlr*NWD*16);
409 memset(ida_sizes, 0, sizeof(int)*nr_ctlr*NWD*16);
410 memset(ida_blocksizes, 0, sizeof(int)*nr_ctlr*NWD*16);
411 memset(ida_hardsizes, 0, sizeof(int)*nr_ctlr*NWD*16);
412 memset(ida_gendisk, 0, sizeof(struct gendisk)*MAX_CTLR);
415 * register block devices
416 * Find disks and fill in structs
417 * Get an interrupt, set the Q depth and get into /proc
419 for(i=0; i< nr_ctlr; i++) {
420 hba[i]->access.set_intr_mask(hba[i], 0);
421 if (request_irq(hba[i]->intr, do_ida_intr,
422 SA_INTERRUPT|SA_SHIRQ, hba[i]->devname, hba[i])) {
424 printk("Unable to get irq %d for %s\n",
425 hba[i]->intr, hba[i]->devname);
426 continue;
428 if (register_blkdev(MAJOR_NR+i, hba[i]->devname, &ida_fops)) {
429 printk("Unable to get major number %d for ida\n",
430 MAJOR_NR+i);
431 continue;
434 hba[i]->cmd_pool = (cmdlist_t *)kmalloc(
435 NR_CMDS * sizeof(cmdlist_t), GFP_KERNEL);
436 hba[i]->cmd_pool_bits = (__u32*)kmalloc(
437 ((NR_CMDS+31)/32)*sizeof(__u32), GFP_KERNEL);
439 if(hba[i]->cmd_pool_bits == NULL || hba[i]->cmd_pool == NULL)
441 int j;
442 if(hba[i]->cmd_pool_bits)
443 kfree(hba[i]->cmd_pool_bits);
444 if(hba[i]->cmd_pool)
445 kfree(hba[i]->cmd_pool);
446 for(j=0;i<i;j++)
448 free_irq(hba[j]->intr, hba[j]);
449 unregister_blkdev(MAJOR_NR+j, hba[j]->devname);
450 kfree(hba[j]->cmd_pool_bits);
451 kfree(hba[j]->cmd_pool);
453 free_irq(hba[i]->intr, hba[i]);
454 unregister_blkdev(MAJOR_NR+i, hba[i]->devname);
455 goto bail5;
457 memset(hba[i]->cmd_pool, 0, NR_CMDS * sizeof(cmdlist_t));
458 memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+31)/32)*sizeof(__u32));
459 printk("Finding drives on %s", hba[i]->devname);
460 getgeometry(i);
461 start_fwbk(i);
463 hba[i]->access.set_intr_mask(hba[i], FIFO_NOT_EMPTY);
465 ida_procinit(i);
466 ida_gendisk[i].major = MAJOR_NR + i;
467 ida_gendisk[i].major_name = "ida";
468 ida_gendisk[i].minor_shift = NWD_SHIFT;
469 ida_gendisk[i].max_p = 16;
470 ida_gendisk[i].max_nr = 16;
471 ida_gendisk[i].init = ida_geninit;
472 ida_gendisk[i].part = ida + (i*256);
473 ida_gendisk[i].sizes = ida_sizes + (i*256);
474 /* ida_gendisk[i].nr_real is handled by getgeometry */
476 blk_dev[MAJOR_NR+i].request_fn = request_fns[i];
477 blksize_size[MAJOR_NR+i] = ida_blocksizes + (i*256);
478 hardsect_size[MAJOR_NR+i] = ida_hardsizes + (i*256);
479 read_ahead[MAJOR_NR+i] = READ_AHEAD;
481 /* Get on the disk list */
482 ida_gendisk[i].next = gendisk_head;
483 gendisk_head = &ida_gendisk[i];
485 init_timer(&hba[i]->timer);
486 hba[i]->timer.expires = jiffies + IDA_TIMER;
487 hba[i]->timer.data = (unsigned long)hba[i];
488 hba[i]->timer.function = ida_timer;
489 add_timer(&hba[i]->timer);
492 /* done ! */
493 return;
494 bail5:
495 kfree(ida_hardsizes);
496 bail4:
497 kfree(ida_blocksizes);
498 bail3:
499 kfree(ida_sizes);
500 bail2:
501 kfree(ida);
502 bail:
503 printk(KERN_ERR "cpqarray: out of memory.\n");
507 * Find the controller and initialize it
508 * Cannot use the class code to search, because older array controllers use
509 * 0x018000 and new ones use 0x010400. So I might as well search for each
510 * each device IDs, being there are only going to be three of them.
512 static int cpqarray_pci_detect(void)
514 int index;
515 unchar bus=0, dev_fn=0;
517 /* This seems dumb, surely we could use an array of types to match ?? */
519 for(index=0; ; index++) {
520 if (pcibios_find_device(PCI_VENDOR_ID_DEC,
521 PCI_DEVICE_ID_COMPAQ_42XX, index, &bus, &dev_fn))
522 break;
523 printk(KERN_DEBUG "42XX Device has been found at %x %x\n",
524 bus, dev_fn);
525 if (index == 1000000) break;
526 if (nr_ctlr == 8) {
527 printk("This driver supports a maximum of "
528 "8 controllers.\n");
529 break;
532 hba[nr_ctlr] = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
533 if(hba[nr_ctlr]==NULL)
535 printk(KERN_ERR "cpqarray: out of memory.\n");
536 continue;
538 memset(hba[nr_ctlr], 0, sizeof(ctlr_info_t));
539 if (cpqarray_pci_init(hba[nr_ctlr], bus, dev_fn) != 0)
540 continue;
541 sprintf(hba[nr_ctlr]->devname, "ida%d", nr_ctlr);
542 hba[nr_ctlr]->ctlr = nr_ctlr;
543 nr_ctlr++;
546 for(index=0; ; index++) {
547 unsigned short subvendor=0;
549 if (pcibios_find_device(PCI_VENDOR_ID_NCR,
550 PCI_DEVICE_ID_NCR_53C1510, index, &bus, &dev_fn))
551 break;
552 printk(KERN_DEBUG "Integrated RAID Chip has been found at %x %x\n",
553 bus, dev_fn);
554 if(pcibios_read_config_word(bus, dev_fn,
555 PCI_SUBSYSTEM_VENDOR_ID, &subvendor))
557 printk(KERN_DEBUG "cpqarray failed to read subvendor\n");
558 break;
560 if(subvendor != PCI_VENDOR_ID_COMPAQ)
561 break;
562 printk(KERN_DEBUG "Its a compaq RAID Chip\n");
563 if (index == 1000000) break;
564 if (nr_ctlr == 8) {
565 printk("This driver supports a maximum of "
566 "8 controllers.\n");
567 break;
570 hba[nr_ctlr] = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
571 if(hba[nr_ctlr]==NULL)
573 printk(KERN_ERR "cpqarray: out of memory.\n");
574 continue;
576 memset(hba[nr_ctlr], 0, sizeof(ctlr_info_t));
577 /* DOESNT THIS LEAK MEMORY ?????? - AC */
578 if (cpqarray_pci_init(hba[nr_ctlr], bus, dev_fn) != 0)
579 continue;
580 sprintf(hba[nr_ctlr]->devname, "ida%d", nr_ctlr);
581 hba[nr_ctlr]->ctlr = nr_ctlr;
582 nr_ctlr++;
585 for(index=0; ; index++) {
586 if (pcibios_find_device(PCI_VENDOR_ID_COMPAQ,
587 PCI_DEVICE_ID_COMPAQ_SMART2P, index, &bus, &dev_fn))
588 break;
590 if (index == 1000000) break;
591 if (nr_ctlr == 8) {
592 printk("This driver supports a maximum of "
593 "8 controllers.\n");
594 break;
597 hba[nr_ctlr] = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
598 if(hba[nr_ctlr]==NULL)
600 printk(KERN_ERR "cpqarray: out of memory.\n");
601 continue;
603 memset(hba[nr_ctlr], 0, sizeof(ctlr_info_t));
604 if (cpqarray_pci_init(hba[nr_ctlr], bus, dev_fn) != 0)
605 continue;
606 sprintf(hba[nr_ctlr]->devname, "ida%d", nr_ctlr);
607 hba[nr_ctlr]->ctlr = nr_ctlr;
608 nr_ctlr++;
611 return nr_ctlr;
614 * Find the IO address of the controller, its IRQ and so forth. Fill
615 * in some basic stuff into the ctlr_info_t structure.
617 static int cpqarray_pci_init(ctlr_info_t *c, unchar bus, unchar device_fn)
619 ushort vendor_id, device_id, command;
620 unchar cache_line_size, latency_timer;
621 unchar irq, revision;
622 uint addr[6];
623 __u32 board_id;
624 struct pci_dev *pdev;
626 int i;
628 pdev = pci_find_slot(bus, device_fn);
629 vendor_id = pdev->vendor;
630 device_id = pdev->device;
631 irq = pdev->irq;
633 for(i=0; i<6; i++)
634 addr[i] = pdev->resource[i].flags;
636 (void) pcibios_read_config_word(bus, device_fn,
637 PCI_COMMAND,&command);
638 (void) pcibios_read_config_byte(bus, device_fn,
639 PCI_CLASS_REVISION,&revision);
640 (void) pcibios_read_config_byte(bus, device_fn,
641 PCI_CACHE_LINE_SIZE, &cache_line_size);
642 (void) pcibios_read_config_byte(bus, device_fn,
643 PCI_LATENCY_TIMER, &latency_timer);
645 (void) pcibios_read_config_dword(bus, device_fn, 0x2c, &board_id);
647 DBGINFO(
648 printk("vendor_id = %x\n", vendor_id);
649 printk("device_id = %x\n", device_id);
650 printk("command = %x\n", command);
651 for(i=0; i<6; i++)
652 printk("addr[%d] = %x\n", i, addr[i]);
653 printk("revision = %x\n", revision);
654 printk("irq = %x\n", irq);
655 printk("cache_line_size = %x\n", cache_line_size);
656 printk("latency_timer = %x\n", latency_timer);
657 printk("board_id = %x\n", board_id);
660 c->intr = irq;
661 c->ioaddr = addr[0] & ~0x1;
664 * Memory base addr is first addr with the first bit _not_ set
666 for(i=0; i<6; i++)
667 if (!(addr[i] & 0x1)) {
668 c->paddr = pdev->resource[i].start;
669 break;
671 c->vaddr = remap_pci_mem(c->paddr, 128);
672 c->board_id = board_id;
674 for(i=0; i<NR_PRODUCTS; i++) {
675 if (board_id == products[i].board_id) {
676 c->product_name = products[i].product_name;
677 c->access = *(products[i].access);
678 break;
681 if (i == NR_PRODUCTS) {
682 printk("Sorry, I don't know how to access the SMART Array"
683 " controller %08lx\n", (unsigned long)board_id);
684 return -1;
687 return 0;
691 * Map (physical) PCI mem into (virtual) kernel space
693 static ulong remap_pci_mem(ulong base, ulong size)
695 ulong page_base = ((ulong) base) & PAGE_MASK;
696 ulong page_offs = ((ulong) base) - page_base;
697 ulong page_remapped = (ulong) ioremap(page_base, page_offs+size);
699 return (ulong) (page_remapped ? (page_remapped + page_offs) : 0UL);
703 * Copy the contents of the ints[] array passed to us by init.
705 void cpqarray_setup(char *str, int *ints)
707 int i;
708 for(i=0; i<ints[0] && i<8; i++)
709 eisa[i] = ints[i+1];
713 * Find an EISA controller's signature. Set up an hba if we find it.
715 static int cpqarray_eisa_detect(void)
717 int i=0, j;
718 __u32 board_id;
719 int intr;
721 while(i<8 && eisa[i]) {
722 if (nr_ctlr == 8) {
723 printk("This driver supports a maximum of "
724 "8 controllers.\n");
725 break;
727 board_id = inl(eisa[i]+0xC80);
728 for(j=0; j < NR_PRODUCTS; j++)
729 if (board_id == products[j].board_id)
730 break;
732 if (j == NR_PRODUCTS) {
733 printk("Sorry, I don't know how to access the SMART"
734 " Array controller %08lx\n", (unsigned long)board_id);
735 continue;
737 hba[nr_ctlr] = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
738 if(hba[nr_ctlr]==NULL)
740 printk(KERN_ERR "cpqarray: out of memory.\n");
741 continue;
743 memset(hba[nr_ctlr], 0, sizeof(ctlr_info_t));
744 hba[nr_ctlr]->ioaddr = eisa[i];
747 * Read the config register to find our interrupt
749 intr = inb(eisa[i]+0xCC0) >> 4;
750 if (intr & 1) intr = 11;
751 else if (intr & 2) intr = 10;
752 else if (intr & 4) intr = 14;
753 else if (intr & 8) intr = 15;
755 hba[nr_ctlr]->intr = intr;
756 sprintf(hba[nr_ctlr]->devname, "ida%d", nr_ctlr);
757 hba[nr_ctlr]->product_name = products[j].product_name;
758 hba[nr_ctlr]->access = *(products[j].access);
759 hba[nr_ctlr]->ctlr = nr_ctlr;
760 hba[nr_ctlr]->board_id = board_id;
762 DBGINFO(
763 printk("i = %d, j = %d\n", i, j);
764 printk("irq = %x\n", intr);
765 printk("product name = %s\n", products[j].product_name);
766 printk("board_id = %x\n", board_id);
769 nr_ctlr++;
770 i++;
773 return nr_ctlr;
778 * Open. Make sure the device is really there.
780 static int ida_open(struct inode *inode, struct file *filep)
782 int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
783 int dsk = MINOR(inode->i_rdev) >> NWD_SHIFT;
785 DBGINFO(printk("ida_open %x (%x:%x)\n", inode->i_rdev, ctlr, dsk) );
786 if (ctlr > MAX_CTLR || hba[ctlr] == NULL)
787 return -ENXIO;
789 if (!suser() && ida_sizes[(ctlr << CTLR_SHIFT) +
790 MINOR(inode->i_rdev)] == 0)
791 return -ENXIO;
794 * Root is allowed to open raw volume zero even if its not configured
795 * so array config can still work. I don't think I really like this,
796 * but I'm already using way to many device nodes to claim another one
797 * for "raw controller".
799 if (suser()
800 && ida_sizes[(ctlr << CTLR_SHIFT) + MINOR(inode->i_rdev)] == 0
801 && MINOR(inode->i_rdev) != 0)
802 return -ENXIO;
804 hba[ctlr]->drv[dsk].usage_count++;
805 hba[ctlr]->usage_count++;
806 MOD_INC_USE_COUNT;
807 return 0;
811 * Close. Sync first.
813 static int ida_release(struct inode *inode, struct file *filep)
815 int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
816 int dsk = MINOR(inode->i_rdev) >> NWD_SHIFT;
818 DBGINFO(printk("ida_release %x (%x:%x)\n", inode->i_rdev, ctlr, dsk) );
819 fsync_dev(inode->i_rdev);
821 hba[ctlr]->drv[dsk].usage_count--;
822 hba[ctlr]->usage_count--;
823 MOD_DEC_USE_COUNT;
824 return 0;
828 * Enqueuing and dequeuing functions for cmdlists.
830 static inline void addQ(cmdlist_t **Qptr, cmdlist_t *c)
832 if (*Qptr == NULL) {
833 *Qptr = c;
834 c->next = c->prev = c;
835 } else {
836 c->prev = (*Qptr)->prev;
837 c->next = (*Qptr);
838 (*Qptr)->prev->next = c;
839 (*Qptr)->prev = c;
843 static inline cmdlist_t *removeQ(cmdlist_t **Qptr, cmdlist_t *c)
845 if (c && c->next != c) {
846 if (*Qptr == c) *Qptr = c->next;
847 c->prev->next = c->next;
848 c->next->prev = c->prev;
849 } else {
850 *Qptr = NULL;
852 return c;
856 * Get a request and submit it to the controller.
857 * This routine needs to grab all the requests it possibly can from the
858 * req Q and submit them. Interrupts are off (and need to be off) when you
859 * are in here (either via the dummy do_ida_request functions or by being
860 * called from the interrupt handler
862 static void do_ida_request(int ctlr)
864 ctlr_info_t *h = hba[ctlr];
865 cmdlist_t *c;
866 int seg, sect;
867 char *lastdataend;
868 struct buffer_head *bh;
869 struct request *creq;
871 creq = blk_dev[MAJOR_NR+ctlr].current_request;
872 if (creq == NULL || creq->rq_status == RQ_INACTIVE)
873 goto doreq_done;
875 if (ctlr != MAJOR(creq->rq_dev)-MAJOR_NR ||
876 ctlr > nr_ctlr || h == NULL) {
877 printk("doreq cmd for %d, %x at %p\n",
878 ctlr, creq->rq_dev, creq);
879 complete_buffers(creq->bh, 0);
880 goto doreq_done;
883 if ((c = cmd_alloc(h)) == NULL)
884 goto doreq_done;
886 bh = creq->bh;
888 c->ctlr = ctlr;
889 c->hdr.unit = MINOR(creq->rq_dev) >> NWD_SHIFT;
890 c->hdr.size = sizeof(rblk_t) >> 2;
891 c->size += sizeof(rblk_t);
893 c->req.hdr.blk = ida[(ctlr<<CTLR_SHIFT) + MINOR(creq->rq_dev)].start_sect + creq->sector;
894 c->bh = bh;
895 DBGPX(
896 if (bh == NULL)
897 panic("bh == NULL?");
899 printk("sector=%d, nr_sectors=%d\n", creq->sector, creq->nr_sectors);
901 seg = 0; lastdataend = NULL;
902 sect = 0;
903 while(bh) {
904 sect += bh->b_size/512;
905 DBGPX(
906 if (bh->b_size % 512) {
907 printk("Oh damn. %d+%d, size = %d\n", creq->sector, sect, bh->b_size);
908 panic("b_size %% 512 != 0");
911 if (bh->b_data == lastdataend) {
912 c->req.sg[seg-1].size += bh->b_size;
913 lastdataend += bh->b_size;
914 } else {
915 c->req.sg[seg].size = bh->b_size;
916 c->req.sg[seg].addr = (__u32)virt_to_bus(bh->b_data);
917 lastdataend = bh->b_data + bh->b_size;
918 if (++seg == SG_MAX)
919 break;
921 bh = bh->b_reqnext;
923 DBGPX( printk("Submitting %d sectors in %d segments\n", sect, seg); );
924 c->req.hdr.sg_cnt = seg;
925 c->req.hdr.blk_cnt = sect;
927 creq->sector += sect;
928 creq->nr_sectors -= sect;
930 /* Ready the next request:
931 * Fix up creq if we still have more buffers in the buffer chain, or
932 * mark this request as done and ready the next one.
934 if (creq->nr_sectors) {
935 DBGPX(
936 if (bh==NULL) {
937 printk("sector=%d, nr_sectors=%d, sect=%d, seg=%d\n",
938 creq->sector, creq->nr_sectors, sect, seg);
939 panic("mother...");
942 creq->bh = bh->b_reqnext;
943 bh->b_reqnext = NULL;
944 DBGPX( printk("More to do on same request %p\n", creq); );
945 } else {
946 DBGPX( printk("Done with %p, queueing %p\n", creq, creq->next); );
947 creq->rq_status = RQ_INACTIVE;
948 blk_dev[MAJOR_NR+ctlr].current_request = creq->next;
949 wake_up(&wait_for_request);
952 c->req.hdr.cmd = (creq->cmd == READ) ? IDA_READ : IDA_WRITE;
953 c->type = CMD_RWREQ;
955 /* Put the request on the tail of the request queue */
956 addQ(&h->reqQ, c);
957 h->Qdepth++;
958 if (h->Qdepth > h->maxQsinceinit) h->maxQsinceinit = h->Qdepth;
960 doreq_done:
961 start_io(h);
965 * start_io submits everything on a controller's request queue
966 * and moves it to the completion queue.
968 * Interrupts had better be off if you're in here
970 static void start_io(ctlr_info_t *h)
972 cmdlist_t *c;
974 while((c = h->reqQ) != NULL) {
975 /* Can't do anything if we're busy */
976 if (h->access.fifo_full(h) == 0)
977 return;
979 /* Get the first entry from the request Q */
980 removeQ(&h->reqQ, c);
981 h->Qdepth--;
983 /* Tell the controller to do our bidding */
984 h->access.submit_command(h, c);
986 /* Get onto the completion Q */
987 addQ(&h->cmpQ, c);
991 static inline void complete_buffers(struct buffer_head *bh, int ok)
993 struct buffer_head *xbh;
994 while(bh) {
995 xbh = bh->b_reqnext;
996 bh->b_reqnext = NULL;
998 bh->b_end_io(bh, ok);
1000 bh = xbh;
1004 * Mark all buffers that cmd was responsible for
1006 static inline void complete_command(cmdlist_t *cmd, int timeout)
1008 char buf[80];
1009 int ok=1;
1011 if (cmd->req.hdr.rcode & RCODE_NONFATAL &&
1012 (hba[cmd->ctlr]->misc_tflags & MISC_NONFATAL_WARN) == 0) {
1013 sprintf(buf, "Non Fatal error on ida/c%dd%d\n",
1014 cmd->ctlr, cmd->hdr.unit);
1015 console_print(buf);
1016 hba[cmd->ctlr]->misc_tflags |= MISC_NONFATAL_WARN;
1018 if (cmd->req.hdr.rcode & RCODE_FATAL) {
1019 sprintf(buf, "Fatal error on ida/c%dd%d\n",
1020 cmd->ctlr, cmd->hdr.unit);
1021 console_print(buf);
1022 ok = 0;
1024 if (cmd->req.hdr.rcode & RCODE_INVREQ) {
1025 sprintf(buf, "Invalid request on ida/c%dd%d = (cmd=%x sect=%d cnt=%d sg=%d ret=%x)\n",
1026 cmd->ctlr, cmd->hdr.unit, cmd->req.hdr.cmd,
1027 cmd->req.hdr.blk, cmd->req.hdr.blk_cnt,
1028 cmd->req.hdr.sg_cnt, cmd->req.hdr.rcode);
1029 console_print(buf);
1030 ok = 0;
1032 if (timeout) ok = 0;
1033 complete_buffers(cmd->bh, ok);
1037 * The controller will interrupt us upon completion of commands.
1038 * Find the command on the completion queue, remove it, tell the OS and
1039 * try to queue up more IO
1041 static void do_ida_intr(int irq, void *dev_id, struct pt_regs *regs)
1043 ctlr_info_t *h = dev_id;
1044 cmdlist_t *c;
1045 unsigned long istat;
1046 unsigned long flags;
1047 __u32 a,a1;
1050 istat = h->access.intr_pending(h);
1051 /* Is this interrupt for us? */
1052 if (istat == 0)
1053 return;
1056 * If there are completed commands in the completion queue,
1057 * we had better do something about it.
1059 spin_lock_irqsave(&io_request_lock, flags);
1060 if (istat & FIFO_NOT_EMPTY) {
1061 while((a = h->access.command_completed(h))) {
1062 a1 = a; a &= ~3;
1063 if ((c = h->cmpQ) == NULL) goto bad_completion;
1064 while(c->busaddr != a) {
1065 c = c->next;
1066 if (c == h->cmpQ) break;
1069 * If we've found the command, take it off the
1070 * completion Q and free it
1072 if (c->busaddr == a) {
1073 removeQ(&h->cmpQ, c);
1074 if (c->type == CMD_RWREQ) {
1075 complete_command(c, 0);
1076 cmd_free(h, c);
1077 } else if (c->type == CMD_IOCTL_PEND) {
1078 c->type = CMD_IOCTL_DONE;
1080 continue;
1082 bad_completion:
1083 printk("Completion of %08lx ignored\n", (unsigned long)a1);
1088 * See if we can queue up some more IO
1090 do_ida_request(h->ctlr);
1091 spin_unlock_irqrestore(&io_request_lock, flags);
1095 * This timer was for timing out requests that haven't happened after
1096 * IDA_TIMEOUT. That wasn't such a good idea. This timer is used to
1097 * reset a flags structure so we don't flood the user with
1098 * "Non-Fatal error" messages.
1100 static void ida_timer(unsigned long tdata)
1102 ctlr_info_t *h = (ctlr_info_t*)tdata;
1104 h->timer.expires = jiffies + IDA_TIMER;
1105 add_timer(&h->timer);
1106 h->misc_tflags = 0;
1110 * ida_ioctl does some miscellaneous stuff like reporting drive geometry,
1111 * setting readahead and submitting commands from userspace to the controller.
1113 static int ida_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
1115 int ctlr = MAJOR(inode->i_rdev) - MAJOR_NR;
1116 int dsk = MINOR(inode->i_rdev) >> NWD_SHIFT;
1117 int error;
1118 int diskinfo[4];
1119 struct hd_geometry *geo = (struct hd_geometry *)arg;
1120 ida_ioctl_t *io = (ida_ioctl_t*)arg;
1121 ida_ioctl_t my_io;
1123 switch(cmd) {
1124 case HDIO_GETGEO:
1125 if (hba[ctlr]->drv[dsk].cylinders) {
1126 diskinfo[0] = hba[ctlr]->drv[dsk].heads;
1127 diskinfo[1] = hba[ctlr]->drv[dsk].sectors;
1128 diskinfo[2] = hba[ctlr]->drv[dsk].cylinders;
1129 } else {
1130 diskinfo[0] = 0xff;
1131 diskinfo[1] = 0x3f;
1132 diskinfo[2] = hba[ctlr]->drv[dsk].nr_blks / (0xff*0x3f);
1134 put_user(diskinfo[0], &geo->heads);
1135 put_user(diskinfo[1], &geo->sectors);
1136 put_user(diskinfo[2], &geo->cylinders);
1137 put_user(ida[(ctlr<<CTLR_SHIFT)+MINOR(inode->i_rdev)].start_sect, &geo->start);
1138 return 0;
1139 case IDAGETDRVINFO:
1140 return copy_to_user(&io->c.drv,&hba[ctlr]->drv[dsk],sizeof(drv_info_t));
1141 case BLKGETSIZE:
1142 if (!arg) return -EINVAL;
1143 put_user(ida[(ctlr<<CTLR_SHIFT)+MINOR(inode->i_rdev)].nr_sects, (long*)arg);
1144 return 0;
1145 case BLKRRPART:
1146 return revalidate_logvol(inode->i_rdev, 1);
1147 case IDAPASSTHRU:
1148 if (!suser()) return -EPERM;
1149 error = copy_from_user(&my_io, io, sizeof(my_io));
1150 if (error) return error;
1151 error = ida_ctlr_ioctl(ctlr, dsk, &my_io);
1152 if (error) return error;
1153 error = copy_to_user(io, &my_io, sizeof(my_io));
1154 return error;
1155 case IDAGETCTLRSIG:
1156 if (!arg) return -EINVAL;
1157 put_user(hba[ctlr]->ctlr_sig, (int*)arg);
1158 return 0;
1159 case IDAREVALIDATEVOLS:
1160 return revalidate_allvol(inode->i_rdev);
1161 case IDADRIVERVERSION:
1162 if (!arg) return -EINVAL;
1163 put_user(DRIVER_VERSION, (unsigned long*)arg);
1164 return 0;
1166 case BLKFLSBUF:
1167 case BLKROSET:
1168 case BLKROGET:
1169 case BLKRASET:
1170 case BLKRAGET:
1171 case BLKPG:
1172 return blk_ioctl(inode->i_rdev, cmd, arg);
1174 default:
1175 return -EBADRQC;
1180 * ida_ctlr_ioctl is for passing commands to the controller from userspace.
1181 * The command block (io) has already been copied to kernel space for us,
1182 * however, any elements in the sglist need to be copied to kernel space
1183 * or copied back to userspace.
1185 * Only root may perform a controller passthru command, however I'm not doing
1186 * any serious sanity checking on the arguments. Doing an IDA_WRITE_MEDIA and
1187 * putting a 64M buffer in the sglist is probably a *bad* idea.
1189 static int ida_ctlr_ioctl(int ctlr, int dsk, ida_ioctl_t *io)
1191 ctlr_info_t *h = hba[ctlr];
1192 cmdlist_t *c;
1193 void *p = NULL;
1194 unsigned long flags;
1195 int error;
1197 if ((c = cmd_alloc(NULL)) == NULL)
1198 return -ENOMEM;
1199 c->ctlr = ctlr;
1200 c->hdr.unit = (io->unit & UNITVALID) ? (io->unit & ~UNITVALID) : dsk;
1201 c->hdr.size = sizeof(rblk_t) >> 2;
1202 c->size += sizeof(rblk_t);
1204 c->req.hdr.cmd = io->cmd;
1205 c->req.hdr.blk = io->blk;
1206 c->req.hdr.blk_cnt = io->blk_cnt;
1207 c->type = CMD_IOCTL_PEND;
1209 /* Pre submit processing */
1210 switch(io->cmd) {
1211 case PASSTHRU_A:
1212 p = kmalloc(io->sg[0].size, GFP_KERNEL);
1213 if (!p) { error = -ENOMEM; goto ioctl_err_exit; }
1214 copy_from_user(p, (void*)io->sg[0].addr, io->sg[0].size);
1215 c->req.bp = virt_to_bus(&(io->c));
1216 c->req.sg[0].size = io->sg[0].size;
1217 c->req.sg[0].addr = virt_to_bus(p);
1218 c->req.hdr.sg_cnt = 1;
1219 break;
1220 case IDA_READ:
1221 p = kmalloc(io->sg[0].size, GFP_KERNEL);
1222 if (!p) { error = -ENOMEM; goto ioctl_err_exit; }
1223 c->req.sg[0].size = io->sg[0].size;
1224 c->req.sg[0].addr = virt_to_bus(p);
1225 c->req.hdr.sg_cnt = 1;
1226 break;
1227 case IDA_WRITE:
1228 case IDA_WRITE_MEDIA:
1229 case DIAG_PASS_THRU:
1230 p = kmalloc(io->sg[0].size, GFP_KERNEL);
1231 if (!p) { error = -ENOMEM; goto ioctl_err_exit; }
1232 copy_from_user(p, (void*)io->sg[0].addr, io->sg[0].size);
1233 c->req.sg[0].size = io->sg[0].size;
1234 c->req.sg[0].addr = virt_to_bus(p);
1235 c->req.hdr.sg_cnt = 1;
1236 break;
1237 default:
1238 c->req.sg[0].size = sizeof(io->c);
1239 c->req.sg[0].addr = virt_to_bus(&io->c);
1240 c->req.hdr.sg_cnt = 1;
1243 /* Put the request on the tail of the request queue */
1244 spin_lock_irqsave(&io_request_lock, flags);
1245 addQ(&h->reqQ, c);
1246 h->Qdepth++;
1247 start_io(h);
1248 spin_unlock_irqrestore(&io_request_lock, flags);
1250 /* Wait for completion */
1251 while(c->type != CMD_IOCTL_DONE)
1252 schedule();
1254 /* Post submit processing */
1255 switch(io->cmd) {
1256 case PASSTHRU_A:
1257 case IDA_READ:
1258 case DIAG_PASS_THRU:
1259 copy_to_user((void*)io->sg[0].addr, p, io->sg[0].size);
1260 /* fall through and free p */
1261 case IDA_WRITE:
1262 case IDA_WRITE_MEDIA:
1263 kfree(p);
1264 break;
1265 default:
1266 /* Nothing to do */
1269 io->rcode = c->req.hdr.rcode;
1270 error = 0;
1271 ioctl_err_exit:
1272 cmd_free(NULL, c);
1273 return error;
1277 * Commands are pre-allocated in a large block. Here we use a simple bitmap
1278 * scheme to suballocte them to the driver. Operations that are not time
1279 * critical (and can wait for kmalloc and possibly sleep) can pass in NULL
1280 * as the first argument to get a new command.
1282 static cmdlist_t * cmd_alloc(ctlr_info_t *h)
1284 cmdlist_t * c;
1285 int i;
1287 if (h == NULL) {
1288 c = (cmdlist_t*)kmalloc(sizeof(cmdlist_t), GFP_KERNEL);
1289 if(c==NULL)
1290 return NULL;
1291 } else {
1292 do {
1293 i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
1294 if (i == NR_CMDS)
1295 return NULL;
1296 } while(test_and_set_bit(i%32, h->cmd_pool_bits+(i/32)) != 0);
1297 c = h->cmd_pool + i;
1298 h->nr_allocs++;
1301 memset(c, 0, sizeof(cmdlist_t));
1302 c->busaddr = virt_to_bus(c);
1303 return c;
1306 static void cmd_free(ctlr_info_t *h, cmdlist_t *c)
1308 int i;
1310 if (h == NULL) {
1311 kfree(c);
1312 } else {
1313 i = c - h->cmd_pool;
1314 clear_bit(i%32, h->cmd_pool_bits+(i/32));
1315 h->nr_frees++;
1319 /***********************************************************************
1320 name: sendcmd
1321 Send a command to an IDA using the memory mapped FIFO interface
1322 and wait for it to complete.
1323 This routine should only be called at init time.
1324 ***********************************************************************/
1325 static int sendcmd(
1326 __u8 cmd,
1327 int ctlr,
1328 void *buff,
1329 size_t size,
1330 unsigned int blk,
1331 unsigned int blkcnt,
1332 unsigned int log_unit )
1334 cmdlist_t *c;
1335 int complete;
1336 unsigned long temp;
1337 unsigned long i;
1338 ctlr_info_t *info_p = hba[ctlr];
1340 c = cmd_alloc(info_p);
1341 c->ctlr = ctlr;
1342 c->hdr.unit = log_unit;
1343 c->hdr.prio = 0;
1344 c->hdr.size = sizeof(rblk_t) >> 2;
1345 c->size += sizeof(rblk_t);
1347 /* The request information. */
1348 c->req.hdr.next = 0;
1349 c->req.hdr.rcode = 0;
1350 c->req.bp = 0;
1351 c->req.hdr.sg_cnt = 1;
1352 c->req.hdr.reserved = 0;
1354 if (size == 0)
1355 c->req.sg[0].size = 512;
1356 else
1357 c->req.sg[0].size = size;
1359 c->req.hdr.blk = blk;
1360 c->req.hdr.blk_cnt = blkcnt;
1361 c->req.hdr.cmd = (unsigned char) cmd;
1362 c->req.sg[0].addr = (__u32) virt_to_bus(buff);
1364 * Disable interrupt
1366 info_p->access.set_intr_mask(info_p, 0);
1367 /* Make sure there is room in the command FIFO */
1368 /* Actually it should be completely empty at this time. */
1369 for (i = 200000; i > 0; i--) {
1370 temp = info_p->access.fifo_full(info_p);
1371 if (temp != 0) {
1372 break;
1374 udelay(10);
1375 DBG(
1376 printk("ida%d: idaSendPciCmd FIFO full, waiting!\n",
1377 ctlr);
1381 * Send the cmd
1383 info_p->access.submit_command(info_p, c);
1384 complete = pollcomplete(ctlr);
1385 if (complete != 1) {
1386 if (complete != c->busaddr) {
1387 printk(
1388 "ida%d: idaSendPciCmd "
1389 "Invalid command list address returned! (%08lx)\n",
1390 ctlr, (unsigned long)complete);
1391 cmd_free(info_p, c);
1392 return (IO_ERROR);
1394 } else {
1395 printk(
1396 "ida%d: idaSendPciCmd Timeout out, "
1397 "No command list address returned!\n",
1398 ctlr);
1399 cmd_free(info_p, c);
1400 return (IO_ERROR);
1403 if (c->req.hdr.rcode & 0x00FE) {
1404 if (!(c->req.hdr.rcode & BIG_PROBLEM)) {
1405 printk(
1406 "ida%d: idaSendPciCmd, error: Controller failed "
1407 "at init time "
1408 "cmd: 0x%x, return code = 0x%x\n",
1409 ctlr, c->req.hdr.cmd, c->req.hdr.rcode);
1411 cmd_free(info_p, c);
1412 return (IO_ERROR);
1415 cmd_free(info_p, c);
1416 return (IO_OK);
1419 static int frevalidate_logvol(kdev_t dev)
1421 return revalidate_logvol(dev, 0);
1425 * revalidate_allvol is for online array config utilities. After a
1426 * utility reconfigures the drives in the array, it can use this function
1427 * (through an ioctl) to make the driver zap any previous disk structs for
1428 * that controller and get new ones.
1430 * Right now I'm using the getgeometry() function to do this, but this
1431 * function should probably be finer grained and allow you to revalidate one
1432 * particualar logical volume (instead of all of them on a particular
1433 * controller).
1435 static int revalidate_allvol(kdev_t dev)
1437 int ctlr, i;
1438 unsigned long flags;
1440 ctlr = MAJOR(dev) - MAJOR_NR;
1441 if (MINOR(dev) != 0)
1442 return -ENXIO;
1444 spin_lock_irqsave(&io_request_lock, flags);
1445 if (hba[ctlr]->usage_count > 1) {
1446 spin_unlock_irqrestore(&io_request_lock, flags);
1447 printk("Device busy for volume revalidation (usage=%d)\n",
1448 hba[ctlr]->usage_count);
1449 return -EBUSY;
1451 spin_unlock_irqrestore(&io_request_lock, flags);
1452 hba[ctlr]->usage_count++;
1455 * Set the partition and block size structures for all volumes
1456 * on this controller to zero. We will reread all of this data
1458 memset(ida+(ctlr*256), 0, sizeof(struct hd_struct)*NWD*16);
1459 memset(ida_sizes+(ctlr*256), 0, sizeof(int)*NWD*16);
1460 memset(ida_blocksizes+(ctlr*256), 0, sizeof(int)*NWD*16);
1461 memset(ida_hardsizes+(ctlr*256), 0, sizeof(int)*NWD*16);
1462 memset(hba[ctlr]->drv, 0, sizeof(drv_info_t)*NWD);
1463 ida_gendisk[ctlr].nr_real = 0;
1466 * Tell the array controller not to give us any interupts while
1467 * we check the new geometry. Then turn interrupts back on when
1468 * we're done.
1470 hba[ctlr]->access.set_intr_mask(hba[ctlr], 0);
1471 getgeometry(ctlr);
1472 hba[ctlr]->access.set_intr_mask(hba[ctlr], FIFO_NOT_EMPTY);
1474 ida_geninit(&ida_gendisk[ctlr]);
1475 for(i=0; i<NWD; i++)
1476 if (ida_sizes[(ctlr<<CTLR_SHIFT) + (i<<NWD_SHIFT)])
1477 revalidate_logvol(dev+(i<<NWD_SHIFT), 2);
1479 hba[ctlr]->usage_count--;
1480 return 0;
1483 /* Borrowed and adapted from sd.c */
1484 static int revalidate_logvol(kdev_t dev, int maxusage)
1486 int ctlr, target;
1487 struct gendisk *gdev;
1488 unsigned long flags;
1489 int max_p;
1490 int start;
1491 int i;
1493 target = DEVICE_NR(dev);
1494 ctlr = MAJOR(dev) - MAJOR_NR;
1495 gdev = &ida_gendisk[ctlr];
1497 spin_lock_irqsave(&io_request_lock, flags);
1498 if (hba[ctlr]->drv[target].usage_count > maxusage) {
1499 spin_unlock_irqrestore(&io_request_lock, flags);
1500 printk("Device busy for revalidation (usage=%d)\n",
1501 hba[ctlr]->drv[target].usage_count);
1502 return -EBUSY;
1505 hba[ctlr]->drv[target].usage_count++;
1506 spin_unlock_irqrestore(&io_request_lock, flags);
1508 max_p = gdev->max_p;
1509 start = target << gdev->minor_shift;
1511 for(i=max_p; i>=0; i--) {
1512 int minor = start+i;
1513 kdev_t devi = MKDEV(MAJOR_NR + ctlr, minor);
1514 struct super_block *sb = get_super(devi);
1515 sync_dev(devi);
1516 if (sb) invalidate_inodes(sb);
1517 invalidate_buffers(devi);
1518 gdev->part[minor].start_sect = 0;
1519 gdev->part[minor].nr_sects = 0;
1521 /* reset the blocksize so we can read the partition table */
1522 blksize_size[MAJOR_NR+ctlr][minor] = 1024;
1525 gdev->part[start].nr_sects = hba[ctlr]->drv[target].nr_blks;
1526 resetup_one_dev(gdev, target);
1527 hba[ctlr]->drv[target].usage_count--;
1528 return 0;
1532 /********************************************************************
1533 name: pollcomplete
1534 Wait polling for a command to complete.
1535 The memory mapped FIFO is polled for the completion.
1536 Used only at init time, interrupts disabled.
1537 ********************************************************************/
1538 static int pollcomplete(int ctlr)
1540 int done;
1541 int i;
1543 /* Wait (up to 2 seconds) for a command to complete */
1545 for (i = 200000; i > 0; i--) {
1546 done = hba[ctlr]->access.command_completed(hba[ctlr]);
1547 if (done == 0) {
1548 udelay(10); /* a short fixed delay */
1549 } else
1550 return (done);
1552 /* Invalid address to tell caller we ran out of time */
1553 return 1;
1555 /*****************************************************************
1556 start_fwbk
1557 Starts controller firmwares background processing.
1558 Currently only the Integrated Raid controller needs this done.
1559 If the PCI mem address registers are written to after this,
1560 data corruption may occur
1561 *****************************************************************/
1562 static void start_fwbk(int ctlr)
1564 id_ctlr_t *id_ctlr_buf;
1565 int ret_code;
1567 if( hba[ctlr]->board_id != 0x40400E11)
1568 /* Not a Integrated Raid, so there is nothing for us to do */
1569 return;
1570 printk(KERN_DEBUG "Starting firmware's background processing\n");
1571 /* Command does not return anything, but idasend command needs a
1572 buffer */
1573 id_ctlr_buf = (id_ctlr_t *)kmalloc(sizeof(id_ctlr_t), GFP_KERNEL);
1574 if(id_ctlr_buf==NULL)
1576 printk(KERN_WARNING "Out of memory. Unable to start background processing.\n");
1577 return;
1579 ret_code = sendcmd(RESUME_BACKGROUND_ACTIVITY, ctlr,
1580 id_ctlr_buf, 0, 0, 0, 0);
1581 if(ret_code != IO_OK)
1582 printk(KERN_WARNING "Unable to start background processing\n");
1583 kfree(id_ctlr_buf);
1585 /*****************************************************************
1586 getgeometry
1587 Get ida logical volume geometry from the controller
1588 This is a large bit of code which once existed in two flavors,
1589 It is used only at init time.
1590 *****************************************************************/
1591 static void getgeometry(int ctlr)
1593 id_log_drv_t *id_ldrive;
1594 id_ctlr_t *id_ctlr_buf;
1595 sense_log_drv_stat_t *id_lstatus_buf;
1596 config_t *sense_config_buf;
1597 unsigned int log_unit, log_index;
1598 int ret_code, size;
1599 drv_info_t *drv;
1600 ctlr_info_t *info_p = hba[ctlr];
1602 info_p->log_drv_map = 0;
1604 id_ldrive = (id_log_drv_t *)kmalloc(sizeof(id_log_drv_t), GFP_KERNEL);
1605 if(id_ldrive == NULL)
1606 return;
1607 id_ctlr_buf = (id_ctlr_t *)kmalloc(sizeof(id_ctlr_t), GFP_KERNEL);
1608 if(id_ctlr_buf == NULL)
1609 goto bail2;
1610 id_lstatus_buf = (sense_log_drv_stat_t *)kmalloc(sizeof(sense_log_drv_stat_t), GFP_KERNEL);
1611 if(id_lstatus_buf == NULL)
1612 goto bail3;
1613 sense_config_buf = (config_t *)kmalloc(sizeof(config_t), GFP_KERNEL);
1614 if(sense_config_buf == NULL)
1615 goto bail4;
1616 memset(id_ldrive, 0, sizeof(id_log_drv_t));
1617 memset(id_ctlr_buf, 0, sizeof(id_ctlr_t));
1618 memset(id_lstatus_buf, 0, sizeof(sense_log_drv_stat_t));
1619 memset(sense_config_buf, 0, sizeof(config_t));
1621 info_p->phys_drives = 0;
1622 info_p->log_drv_map = 0;
1623 info_p->drv_assign_map = 0;
1624 info_p->drv_spare_map = 0;
1625 info_p->mp_failed_drv_map = 0; /* only initialized here */
1626 /* Get controllers info for this logical drive */
1627 ret_code = sendcmd(ID_CTLR, ctlr, id_ctlr_buf, 0, 0, 0, 0);
1628 if (ret_code == IO_ERROR) {
1630 * If can't get controller info, set the logical drive map to 0,
1631 * so the idastubopen will fail on all logical drives
1632 * on the controller.
1634 goto geo_ret; /* release the buf and return */
1636 info_p->log_drives = id_ctlr_buf->nr_drvs;;
1637 *(__u32*)(info_p->firm_rev) = *(__u32*)(id_ctlr_buf->firm_rev);
1638 info_p->ctlr_sig = id_ctlr_buf->cfg_sig;
1640 printk(" (%s)\n", info_p->product_name);
1642 * Initialize logical drive map to zero
1644 log_index = 0;
1646 * Get drive geometry for all logical drives
1648 if (id_ctlr_buf->nr_drvs > 16)
1649 printk("ida%d: This driver supports 16 logical drives "
1650 "per controller.\n. Additional drives will not be "
1651 "detected\n", ctlr);
1653 for (log_unit = 0;
1654 (log_index < id_ctlr_buf->nr_drvs)
1655 && (log_unit < NWD);
1656 log_unit++) {
1658 size = sizeof(sense_log_drv_stat_t);
1661 Send "Identify logical drive status" cmd
1663 ret_code = sendcmd(SENSE_LOG_DRV_STAT,
1664 ctlr, id_lstatus_buf, size, 0, 0, log_unit);
1665 if (ret_code == IO_ERROR) {
1667 If can't get logical drive status, set
1668 the logical drive map to 0, so the
1669 idastubopen will fail for all logical drives
1670 on the controller.
1672 info_p->log_drv_map = 0;
1673 printk(
1674 "ida%d: idaGetGeometry - Controller failed "
1675 "to report status of logical drive %d\n"
1676 "Access to this controller has been disabled\n",
1677 ctlr, log_unit);
1678 goto geo_ret; /* release the buf and return */
1682 Make sure the logical drive is configured
1684 if (id_lstatus_buf->status != LOG_NOT_CONF) {
1685 ret_code = sendcmd(ID_LOG_DRV, ctlr, id_ldrive,
1686 sizeof(id_log_drv_t), 0, 0, log_unit);
1688 If error, the bit for this
1689 logical drive won't be set and
1690 idastubopen will return error.
1692 if (ret_code != IO_ERROR) {
1693 drv = &info_p->drv[log_unit];
1694 drv->blk_size = id_ldrive->blk_size;
1695 drv->nr_blks = id_ldrive->nr_blks;
1696 drv->cylinders = id_ldrive->drv.cyl;
1697 drv->heads = id_ldrive->drv.heads;
1698 drv->sectors = id_ldrive->drv.sect_per_track;
1699 info_p->log_drv_map |= (1 << log_unit);
1701 printk("ida/c%dd%d: blksz=%d nr_blks=%d\n",
1702 ctlr, log_unit, drv->blk_size, drv->nr_blks);
1703 ret_code = sendcmd(SENSE_CONFIG,
1704 ctlr, sense_config_buf,
1705 sizeof(config_t), 0, 0, log_unit);
1706 if (ret_code == IO_ERROR) {
1707 info_p->log_drv_map = 0;
1708 goto geo_ret; /* release the buf and return */
1710 info_p->phys_drives =
1711 sense_config_buf->ctlr_phys_drv;
1712 info_p->drv_assign_map
1713 |= sense_config_buf->drv_asgn_map;
1714 info_p->drv_assign_map
1715 |= sense_config_buf->spare_asgn_map;
1716 info_p->drv_spare_map
1717 |= sense_config_buf->spare_asgn_map;
1718 } /* end of if no error on id_ldrive */
1719 log_index = log_index + 1;
1720 } /* end of if logical drive configured */
1721 } /* end of for log_unit */
1722 geo_ret:
1723 kfree(sense_config_buf);
1724 bail4:
1725 kfree(id_ldrive);
1726 bail3:
1727 kfree(id_lstatus_buf);
1728 bail2:
1729 kfree(id_ctlr_buf);