perf hist: Fix missing getline declaration
[safe/jmp/linux-2.6] / drivers / scsi / megaraid.c
1 /*
2  *
3  *                      Linux MegaRAID device driver
4  *
5  * Copyright (c) 2002  LSI Logic Corporation.
6  *
7  *         This program is free software; you can redistribute it and/or
8  *         modify it under the terms of the GNU General Public License
9  *         as published by the Free Software Foundation; either version
10  *         2 of the License, or (at your option) any later version.
11  *
12  * Copyright (c) 2002  Red Hat, Inc. All rights reserved.
13  *        - fixes
14  *        - speed-ups (list handling fixes, issued_list, optimizations.)
15  *        - lots of cleanups.
16  *
17  * Copyright (c) 2003  Christoph Hellwig  <hch@lst.de>
18  *        - new-style, hotplug-aware pci probing and scsi registration
19  *
20  * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
21  *                                              <Seokmann.Ju@lsil.com>
22  *
23  * Description: Linux device driver for LSI Logic MegaRAID controller
24  *
25  * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
26  *                                      518, 520, 531, 532
27  *
28  * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
29  * and others. Please send updates to the mailing list
30  * linux-scsi@vger.kernel.org .
31  *
32  */
33
34 #include <linux/mm.h>
35 #include <linux/fs.h>
36 #include <linux/blkdev.h>
37 #include <asm/uaccess.h>
38 #include <asm/io.h>
39 #include <linux/completion.h>
40 #include <linux/delay.h>
41 #include <linux/proc_fs.h>
42 #include <linux/reboot.h>
43 #include <linux/module.h>
44 #include <linux/list.h>
45 #include <linux/interrupt.h>
46 #include <linux/pci.h>
47 #include <linux/init.h>
48 #include <linux/dma-mapping.h>
49 #include <linux/smp_lock.h>
50 #include <linux/slab.h>
51 #include <scsi/scsicam.h>
52
53 #include "scsi.h"
54 #include <scsi/scsi_host.h>
55
56 #include "megaraid.h"
57
58 #define MEGARAID_MODULE_VERSION "2.00.4"
59
60 MODULE_AUTHOR ("sju@lsil.com");
61 MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
62 MODULE_LICENSE ("GPL");
63 MODULE_VERSION(MEGARAID_MODULE_VERSION);
64
65 static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
66 module_param(max_cmd_per_lun, uint, 0);
67 MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
68
69 static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
70 module_param(max_sectors_per_io, ushort, 0);
71 MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
72
73
74 static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
75 module_param(max_mbox_busy_wait, ushort, 0);
76 MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
77
78 #define RDINDOOR(adapter)       readl((adapter)->mmio_base + 0x20)
79 #define RDOUTDOOR(adapter)      readl((adapter)->mmio_base + 0x2C)
80 #define WRINDOOR(adapter,value)  writel(value, (adapter)->mmio_base + 0x20)
81 #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
82
83 /*
84  * Global variables
85  */
86
87 static int hba_count;
88 static adapter_t *hba_soft_state[MAX_CONTROLLERS];
89 static struct proc_dir_entry *mega_proc_dir_entry;
90
91 /* For controller re-ordering */
92 static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
93
94 /*
95  * The File Operations structure for the serial/ioctl interface of the driver
96  */
97 static const struct file_operations megadev_fops = {
98         .owner          = THIS_MODULE,
99         .ioctl          = megadev_ioctl,
100         .open           = megadev_open,
101 };
102
103 /*
104  * Array to structures for storing the information about the controllers. This
105  * information is sent to the user level applications, when they do an ioctl
106  * for this information.
107  */
108 static struct mcontroller mcontroller[MAX_CONTROLLERS];
109
110 /* The current driver version */
111 static u32 driver_ver = 0x02000000;
112
113 /* major number used by the device for character interface */
114 static int major;
115
116 #define IS_RAID_CH(hba, ch)     (((hba)->mega_ch_class >> (ch)) & 0x01)
117
118
119 /*
120  * Debug variable to print some diagnostic messages
121  */
122 static int trace_level;
123
124 /**
125  * mega_setup_mailbox()
126  * @adapter - pointer to our soft state
127  *
128  * Allocates a 8 byte aligned memory for the handshake mailbox.
129  */
130 static int
131 mega_setup_mailbox(adapter_t *adapter)
132 {
133         unsigned long   align;
134
135         adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
136                         sizeof(mbox64_t), &adapter->una_mbox64_dma);
137
138         if( !adapter->una_mbox64 ) return -1;
139                 
140         adapter->mbox = &adapter->una_mbox64->mbox;
141
142         adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
143                         (~0UL ^ 0xFUL));
144
145         adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
146
147         align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
148
149         adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
150
151         /*
152          * Register the mailbox if the controller is an io-mapped controller
153          */
154         if( adapter->flag & BOARD_IOMAP ) {
155
156                 outb(adapter->mbox_dma & 0xFF,
157                                 adapter->host->io_port + MBOX_PORT0);
158
159                 outb((adapter->mbox_dma >> 8) & 0xFF,
160                                 adapter->host->io_port + MBOX_PORT1);
161
162                 outb((adapter->mbox_dma >> 16) & 0xFF,
163                                 adapter->host->io_port + MBOX_PORT2);
164
165                 outb((adapter->mbox_dma >> 24) & 0xFF,
166                                 adapter->host->io_port + MBOX_PORT3);
167
168                 outb(ENABLE_MBOX_BYTE,
169                                 adapter->host->io_port + ENABLE_MBOX_REGION);
170
171                 irq_ack(adapter);
172
173                 irq_enable(adapter);
174         }
175
176         return 0;
177 }
178
179
180 /*
181  * mega_query_adapter()
182  * @adapter - pointer to our soft state
183  *
184  * Issue the adapter inquiry commands to the controller and find out
185  * information and parameter about the devices attached
186  */
187 static int
188 mega_query_adapter(adapter_t *adapter)
189 {
190         dma_addr_t      prod_info_dma_handle;
191         mega_inquiry3   *inquiry3;
192         u8      raw_mbox[sizeof(struct mbox_out)];
193         mbox_t  *mbox;
194         int     retval;
195
196         /* Initialize adapter inquiry mailbox */
197
198         mbox = (mbox_t *)raw_mbox;
199
200         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
201         memset(&mbox->m_out, 0, sizeof(raw_mbox));
202
203         /*
204          * Try to issue Inquiry3 command
205          * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
206          * update enquiry3 structure
207          */
208         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
209
210         inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
211
212         raw_mbox[0] = FC_NEW_CONFIG;            /* i.e. mbox->cmd=0xA1 */
213         raw_mbox[2] = NC_SUBOP_ENQUIRY3;        /* i.e. 0x0F */
214         raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;  /* i.e. 0x02 */
215
216         /* Issue a blocking command to the card */
217         if ((retval = issue_scb_block(adapter, raw_mbox))) {
218                 /* the adapter does not support 40ld */
219
220                 mraid_ext_inquiry       *ext_inq;
221                 mraid_inquiry           *inq;
222                 dma_addr_t              dma_handle;
223
224                 ext_inq = pci_alloc_consistent(adapter->dev,
225                                 sizeof(mraid_ext_inquiry), &dma_handle);
226
227                 if( ext_inq == NULL ) return -1;
228
229                 inq = &ext_inq->raid_inq;
230
231                 mbox->m_out.xferaddr = (u32)dma_handle;
232
233                 /*issue old 0x04 command to adapter */
234                 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
235
236                 issue_scb_block(adapter, raw_mbox);
237
238                 /*
239                  * update Enquiry3 and ProductInfo structures with
240                  * mraid_inquiry structure
241                  */
242                 mega_8_to_40ld(inq, inquiry3,
243                                 (mega_product_info *)&adapter->product_info);
244
245                 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
246                                 ext_inq, dma_handle);
247
248         } else {                /*adapter supports 40ld */
249                 adapter->flag |= BOARD_40LD;
250
251                 /*
252                  * get product_info, which is static information and will be
253                  * unchanged
254                  */
255                 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
256                                 &adapter->product_info,
257                                 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
258
259                 mbox->m_out.xferaddr = prod_info_dma_handle;
260
261                 raw_mbox[0] = FC_NEW_CONFIG;    /* i.e. mbox->cmd=0xA1 */
262                 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;    /* i.e. 0x0E */
263
264                 if ((retval = issue_scb_block(adapter, raw_mbox)))
265                         printk(KERN_WARNING
266                         "megaraid: Product_info cmd failed with error: %d\n",
267                                 retval);
268
269                 pci_unmap_single(adapter->dev, prod_info_dma_handle,
270                                 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
271         }
272
273
274         /*
275          * kernel scans the channels from 0 to <= max_channel
276          */
277         adapter->host->max_channel =
278                 adapter->product_info.nchannels + NVIRT_CHAN -1;
279
280         adapter->host->max_id = 16;     /* max targets per channel */
281
282         adapter->host->max_lun = 7;     /* Upto 7 luns for non disk devices */
283
284         adapter->host->cmd_per_lun = max_cmd_per_lun;
285
286         adapter->numldrv = inquiry3->num_ldrv;
287
288         adapter->max_cmds = adapter->product_info.max_commands;
289
290         if(adapter->max_cmds > MAX_COMMANDS)
291                 adapter->max_cmds = MAX_COMMANDS;
292
293         adapter->host->can_queue = adapter->max_cmds - 1;
294
295         /*
296          * Get the maximum number of scatter-gather elements supported by this
297          * firmware
298          */
299         mega_get_max_sgl(adapter);
300
301         adapter->host->sg_tablesize = adapter->sglen;
302
303
304         /* use HP firmware and bios version encoding */
305         if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
306                 sprintf (adapter->fw_version, "%c%d%d.%d%d",
307                          adapter->product_info.fw_version[2],
308                          adapter->product_info.fw_version[1] >> 8,
309                          adapter->product_info.fw_version[1] & 0x0f,
310                          adapter->product_info.fw_version[0] >> 8,
311                          adapter->product_info.fw_version[0] & 0x0f);
312                 sprintf (adapter->bios_version, "%c%d%d.%d%d",
313                          adapter->product_info.bios_version[2],
314                          adapter->product_info.bios_version[1] >> 8,
315                          adapter->product_info.bios_version[1] & 0x0f,
316                          adapter->product_info.bios_version[0] >> 8,
317                          adapter->product_info.bios_version[0] & 0x0f);
318         } else {
319                 memcpy(adapter->fw_version,
320                                 (char *)adapter->product_info.fw_version, 4);
321                 adapter->fw_version[4] = 0;
322
323                 memcpy(adapter->bios_version,
324                                 (char *)adapter->product_info.bios_version, 4);
325
326                 adapter->bios_version[4] = 0;
327         }
328
329         printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
330                 adapter->fw_version, adapter->bios_version, adapter->numldrv);
331
332         /*
333          * Do we support extended (>10 bytes) cdbs
334          */
335         adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
336         if (adapter->support_ext_cdb)
337                 printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
338
339
340         return 0;
341 }
342
343 /**
344  * mega_runpendq()
345  * @adapter - pointer to our soft state
346  *
347  * Runs through the list of pending requests.
348  */
349 static inline void
350 mega_runpendq(adapter_t *adapter)
351 {
352         if(!list_empty(&adapter->pending_list))
353                 __mega_runpendq(adapter);
354 }
355
356 /*
357  * megaraid_queue()
358  * @scmd - Issue this scsi command
359  * @done - the callback hook into the scsi mid-layer
360  *
361  * The command queuing entry point for the mid-layer.
362  */
363 static int
364 megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
365 {
366         adapter_t       *adapter;
367         scb_t   *scb;
368         int     busy=0;
369         unsigned long flags;
370
371         adapter = (adapter_t *)scmd->device->host->hostdata;
372
373         scmd->scsi_done = done;
374
375
376         /*
377          * Allocate and build a SCB request
378          * busy flag will be set if mega_build_cmd() command could not
379          * allocate scb. We will return non-zero status in that case.
380          * NOTE: scb can be null even though certain commands completed
381          * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
382          * return 0 in that case.
383          */
384
385         spin_lock_irqsave(&adapter->lock, flags);
386         scb = mega_build_cmd(adapter, scmd, &busy);
387         if (!scb)
388                 goto out;
389
390         scb->state |= SCB_PENDQ;
391         list_add_tail(&scb->list, &adapter->pending_list);
392
393         /*
394          * Check if the HBA is in quiescent state, e.g., during a
395          * delete logical drive opertion. If it is, don't run
396          * the pending_list.
397          */
398         if (atomic_read(&adapter->quiescent) == 0)
399                 mega_runpendq(adapter);
400
401         busy = 0;
402  out:
403         spin_unlock_irqrestore(&adapter->lock, flags);
404         return busy;
405 }
406
407 /**
408  * mega_allocate_scb()
409  * @adapter - pointer to our soft state
410  * @cmd - scsi command from the mid-layer
411  *
412  * Allocate a SCB structure. This is the central structure for controller
413  * commands.
414  */
415 static inline scb_t *
416 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
417 {
418         struct list_head *head = &adapter->free_list;
419         scb_t   *scb;
420
421         /* Unlink command from Free List */
422         if( !list_empty(head) ) {
423
424                 scb = list_entry(head->next, scb_t, list);
425
426                 list_del_init(head->next);
427
428                 scb->state = SCB_ACTIVE;
429                 scb->cmd = cmd;
430                 scb->dma_type = MEGA_DMA_TYPE_NONE;
431
432                 return scb;
433         }
434
435         return NULL;
436 }
437
438 /**
439  * mega_get_ldrv_num()
440  * @adapter - pointer to our soft state
441  * @cmd - scsi mid layer command
442  * @channel - channel on the controller
443  *
444  * Calculate the logical drive number based on the information in scsi command
445  * and the channel number.
446  */
447 static inline int
448 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
449 {
450         int             tgt;
451         int             ldrv_num;
452
453         tgt = cmd->device->id;
454         
455         if ( tgt > adapter->this_id )
456                 tgt--;  /* we do not get inquires for initiator id */
457
458         ldrv_num = (channel * 15) + tgt;
459
460
461         /*
462          * If we have a logical drive with boot enabled, project it first
463          */
464         if( adapter->boot_ldrv_enabled ) {
465                 if( ldrv_num == 0 ) {
466                         ldrv_num = adapter->boot_ldrv;
467                 }
468                 else {
469                         if( ldrv_num <= adapter->boot_ldrv ) {
470                                 ldrv_num--;
471                         }
472                 }
473         }
474
475         /*
476          * If "delete logical drive" feature is enabled on this controller.
477          * Do only if at least one delete logical drive operation was done.
478          *
479          * Also, after logical drive deletion, instead of logical drive number,
480          * the value returned should be 0x80+logical drive id.
481          *
482          * These is valid only for IO commands.
483          */
484
485         if (adapter->support_random_del && adapter->read_ldidmap )
486                 switch (cmd->cmnd[0]) {
487                 case READ_6:    /* fall through */
488                 case WRITE_6:   /* fall through */
489                 case READ_10:   /* fall through */
490                 case WRITE_10:
491                         ldrv_num += 0x80;
492                 }
493
494         return ldrv_num;
495 }
496
497 /**
498  * mega_build_cmd()
499  * @adapter - pointer to our soft state
500  * @cmd - Prepare using this scsi command
501  * @busy - busy flag if no resources
502  *
503  * Prepares a command and scatter gather list for the controller. This routine
504  * also finds out if the commands is intended for a logical drive or a
505  * physical device and prepares the controller command accordingly.
506  *
507  * We also re-order the logical drives and physical devices based on their
508  * boot settings.
509  */
510 static scb_t *
511 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
512 {
513         mega_ext_passthru       *epthru;
514         mega_passthru   *pthru;
515         scb_t   *scb;
516         mbox_t  *mbox;
517         long    seg;
518         char    islogical;
519         int     max_ldrv_num;
520         int     channel = 0;
521         int     target = 0;
522         int     ldrv_num = 0;   /* logical drive number */
523
524
525         /*
526          * filter the internal and ioctl commands
527          */
528         if((cmd->cmnd[0] == MEGA_INTERNAL_CMD))
529                 return (scb_t *)cmd->host_scribble;
530
531         /*
532          * We know what channels our logical drives are on - mega_find_card()
533          */
534         islogical = adapter->logdrv_chan[cmd->device->channel];
535
536         /*
537          * The theory: If physical drive is chosen for boot, all the physical
538          * devices are exported before the logical drives, otherwise physical
539          * devices are pushed after logical drives, in which case - Kernel sees
540          * the physical devices on virtual channel which is obviously converted
541          * to actual channel on the HBA.
542          */
543         if( adapter->boot_pdrv_enabled ) {
544                 if( islogical ) {
545                         /* logical channel */
546                         channel = cmd->device->channel -
547                                 adapter->product_info.nchannels;
548                 }
549                 else {
550                         /* this is physical channel */
551                         channel = cmd->device->channel; 
552                         target = cmd->device->id;
553
554                         /*
555                          * boot from a physical disk, that disk needs to be
556                          * exposed first IF both the channels are SCSI, then
557                          * booting from the second channel is not allowed.
558                          */
559                         if( target == 0 ) {
560                                 target = adapter->boot_pdrv_tgt;
561                         }
562                         else if( target == adapter->boot_pdrv_tgt ) {
563                                 target = 0;
564                         }
565                 }
566         }
567         else {
568                 if( islogical ) {
569                         /* this is the logical channel */
570                         channel = cmd->device->channel; 
571                 }
572                 else {
573                         /* physical channel */
574                         channel = cmd->device->channel - NVIRT_CHAN;    
575                         target = cmd->device->id;
576                 }
577         }
578
579
580         if(islogical) {
581
582                 /* have just LUN 0 for each target on virtual channels */
583                 if (cmd->device->lun) {
584                         cmd->result = (DID_BAD_TARGET << 16);
585                         cmd->scsi_done(cmd);
586                         return NULL;
587                 }
588
589                 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
590
591
592                 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
593                         MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
594
595                 /*
596                  * max_ldrv_num increases by 0x80 if some logical drive was
597                  * deleted.
598                  */
599                 if(adapter->read_ldidmap)
600                         max_ldrv_num += 0x80;
601
602                 if(ldrv_num > max_ldrv_num ) {
603                         cmd->result = (DID_BAD_TARGET << 16);
604                         cmd->scsi_done(cmd);
605                         return NULL;
606                 }
607
608         }
609         else {
610                 if( cmd->device->lun > 7) {
611                         /*
612                          * Do not support lun >7 for physically accessed
613                          * devices
614                          */
615                         cmd->result = (DID_BAD_TARGET << 16);
616                         cmd->scsi_done(cmd);
617                         return NULL;
618                 }
619         }
620
621         /*
622          *
623          * Logical drive commands
624          *
625          */
626         if(islogical) {
627                 switch (cmd->cmnd[0]) {
628                 case TEST_UNIT_READY:
629 #if MEGA_HAVE_CLUSTERING
630                         /*
631                          * Do we support clustering and is the support enabled
632                          * If no, return success always
633                          */
634                         if( !adapter->has_cluster ) {
635                                 cmd->result = (DID_OK << 16);
636                                 cmd->scsi_done(cmd);
637                                 return NULL;
638                         }
639
640                         if(!(scb = mega_allocate_scb(adapter, cmd))) {
641                                 *busy = 1;
642                                 return NULL;
643                         }
644
645                         scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
646                         scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
647                         scb->raw_mbox[3] = ldrv_num;
648
649                         scb->dma_direction = PCI_DMA_NONE;
650
651                         return scb;
652 #else
653                         cmd->result = (DID_OK << 16);
654                         cmd->scsi_done(cmd);
655                         return NULL;
656 #endif
657
658                 case MODE_SENSE: {
659                         char *buf;
660                         struct scatterlist *sg;
661
662                         sg = scsi_sglist(cmd);
663                         buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
664
665                         memset(buf, 0, cmd->cmnd[4]);
666                         kunmap_atomic(buf - sg->offset, KM_IRQ0);
667
668                         cmd->result = (DID_OK << 16);
669                         cmd->scsi_done(cmd);
670                         return NULL;
671                 }
672
673                 case READ_CAPACITY:
674                 case INQUIRY:
675
676                         if(!(adapter->flag & (1L << cmd->device->channel))) {
677
678                                 printk(KERN_NOTICE
679                                         "scsi%d: scanning scsi channel %d ",
680                                                 adapter->host->host_no,
681                                                 cmd->device->channel);
682                                 printk("for logical drives.\n");
683
684                                 adapter->flag |= (1L << cmd->device->channel);
685                         }
686
687                         /* Allocate a SCB and initialize passthru */
688                         if(!(scb = mega_allocate_scb(adapter, cmd))) {
689                                 *busy = 1;
690                                 return NULL;
691                         }
692                         pthru = scb->pthru;
693
694                         mbox = (mbox_t *)scb->raw_mbox;
695                         memset(mbox, 0, sizeof(scb->raw_mbox));
696                         memset(pthru, 0, sizeof(mega_passthru));
697
698                         pthru->timeout = 0;
699                         pthru->ars = 1;
700                         pthru->reqsenselen = 14;
701                         pthru->islogical = 1;
702                         pthru->logdrv = ldrv_num;
703                         pthru->cdblen = cmd->cmd_len;
704                         memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
705
706                         if( adapter->has_64bit_addr ) {
707                                 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
708                         }
709                         else {
710                                 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
711                         }
712
713                         scb->dma_direction = PCI_DMA_FROMDEVICE;
714
715                         pthru->numsgelements = mega_build_sglist(adapter, scb,
716                                 &pthru->dataxferaddr, &pthru->dataxferlen);
717
718                         mbox->m_out.xferaddr = scb->pthru_dma_addr;
719
720                         return scb;
721
722                 case READ_6:
723                 case WRITE_6:
724                 case READ_10:
725                 case WRITE_10:
726                 case READ_12:
727                 case WRITE_12:
728
729                         /* Allocate a SCB and initialize mailbox */
730                         if(!(scb = mega_allocate_scb(adapter, cmd))) {
731                                 *busy = 1;
732                                 return NULL;
733                         }
734                         mbox = (mbox_t *)scb->raw_mbox;
735
736                         memset(mbox, 0, sizeof(scb->raw_mbox));
737                         mbox->m_out.logdrv = ldrv_num;
738
739                         /*
740                          * A little hack: 2nd bit is zero for all scsi read
741                          * commands and is set for all scsi write commands
742                          */
743                         if( adapter->has_64bit_addr ) {
744                                 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
745                                         MEGA_MBOXCMD_LWRITE64:
746                                         MEGA_MBOXCMD_LREAD64 ;
747                         }
748                         else {
749                                 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
750                                         MEGA_MBOXCMD_LWRITE:
751                                         MEGA_MBOXCMD_LREAD ;
752                         }
753
754                         /*
755                          * 6-byte READ(0x08) or WRITE(0x0A) cdb
756                          */
757                         if( cmd->cmd_len == 6 ) {
758                                 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
759                                 mbox->m_out.lba =
760                                         ((u32)cmd->cmnd[1] << 16) |
761                                         ((u32)cmd->cmnd[2] << 8) |
762                                         (u32)cmd->cmnd[3];
763
764                                 mbox->m_out.lba &= 0x1FFFFF;
765
766 #if MEGA_HAVE_STATS
767                                 /*
768                                  * Take modulo 0x80, since the logical drive
769                                  * number increases by 0x80 when a logical
770                                  * drive was deleted
771                                  */
772                                 if (*cmd->cmnd == READ_6) {
773                                         adapter->nreads[ldrv_num%0x80]++;
774                                         adapter->nreadblocks[ldrv_num%0x80] +=
775                                                 mbox->m_out.numsectors;
776                                 } else {
777                                         adapter->nwrites[ldrv_num%0x80]++;
778                                         adapter->nwriteblocks[ldrv_num%0x80] +=
779                                                 mbox->m_out.numsectors;
780                                 }
781 #endif
782                         }
783
784                         /*
785                          * 10-byte READ(0x28) or WRITE(0x2A) cdb
786                          */
787                         if( cmd->cmd_len == 10 ) {
788                                 mbox->m_out.numsectors =
789                                         (u32)cmd->cmnd[8] |
790                                         ((u32)cmd->cmnd[7] << 8);
791                                 mbox->m_out.lba =
792                                         ((u32)cmd->cmnd[2] << 24) |
793                                         ((u32)cmd->cmnd[3] << 16) |
794                                         ((u32)cmd->cmnd[4] << 8) |
795                                         (u32)cmd->cmnd[5];
796
797 #if MEGA_HAVE_STATS
798                                 if (*cmd->cmnd == READ_10) {
799                                         adapter->nreads[ldrv_num%0x80]++;
800                                         adapter->nreadblocks[ldrv_num%0x80] +=
801                                                 mbox->m_out.numsectors;
802                                 } else {
803                                         adapter->nwrites[ldrv_num%0x80]++;
804                                         adapter->nwriteblocks[ldrv_num%0x80] +=
805                                                 mbox->m_out.numsectors;
806                                 }
807 #endif
808                         }
809
810                         /*
811                          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
812                          */
813                         if( cmd->cmd_len == 12 ) {
814                                 mbox->m_out.lba =
815                                         ((u32)cmd->cmnd[2] << 24) |
816                                         ((u32)cmd->cmnd[3] << 16) |
817                                         ((u32)cmd->cmnd[4] << 8) |
818                                         (u32)cmd->cmnd[5];
819
820                                 mbox->m_out.numsectors =
821                                         ((u32)cmd->cmnd[6] << 24) |
822                                         ((u32)cmd->cmnd[7] << 16) |
823                                         ((u32)cmd->cmnd[8] << 8) |
824                                         (u32)cmd->cmnd[9];
825
826 #if MEGA_HAVE_STATS
827                                 if (*cmd->cmnd == READ_12) {
828                                         adapter->nreads[ldrv_num%0x80]++;
829                                         adapter->nreadblocks[ldrv_num%0x80] +=
830                                                 mbox->m_out.numsectors;
831                                 } else {
832                                         adapter->nwrites[ldrv_num%0x80]++;
833                                         adapter->nwriteblocks[ldrv_num%0x80] +=
834                                                 mbox->m_out.numsectors;
835                                 }
836 #endif
837                         }
838
839                         /*
840                          * If it is a read command
841                          */
842                         if( (*cmd->cmnd & 0x0F) == 0x08 ) {
843                                 scb->dma_direction = PCI_DMA_FROMDEVICE;
844                         }
845                         else {
846                                 scb->dma_direction = PCI_DMA_TODEVICE;
847                         }
848
849                         /* Calculate Scatter-Gather info */
850                         mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
851                                         (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
852
853                         return scb;
854
855 #if MEGA_HAVE_CLUSTERING
856                 case RESERVE:   /* Fall through */
857                 case RELEASE:
858
859                         /*
860                          * Do we support clustering and is the support enabled
861                          */
862                         if( ! adapter->has_cluster ) {
863
864                                 cmd->result = (DID_BAD_TARGET << 16);
865                                 cmd->scsi_done(cmd);
866                                 return NULL;
867                         }
868
869                         /* Allocate a SCB and initialize mailbox */
870                         if(!(scb = mega_allocate_scb(adapter, cmd))) {
871                                 *busy = 1;
872                                 return NULL;
873                         }
874
875                         scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
876                         scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
877                                 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
878
879                         scb->raw_mbox[3] = ldrv_num;
880
881                         scb->dma_direction = PCI_DMA_NONE;
882
883                         return scb;
884 #endif
885
886                 default:
887                         cmd->result = (DID_BAD_TARGET << 16);
888                         cmd->scsi_done(cmd);
889                         return NULL;
890                 }
891         }
892
893         /*
894          * Passthru drive commands
895          */
896         else {
897                 /* Allocate a SCB and initialize passthru */
898                 if(!(scb = mega_allocate_scb(adapter, cmd))) {
899                         *busy = 1;
900                         return NULL;
901                 }
902
903                 mbox = (mbox_t *)scb->raw_mbox;
904                 memset(mbox, 0, sizeof(scb->raw_mbox));
905
906                 if( adapter->support_ext_cdb ) {
907
908                         epthru = mega_prepare_extpassthru(adapter, scb, cmd,
909                                         channel, target);
910
911                         mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
912
913                         mbox->m_out.xferaddr = scb->epthru_dma_addr;
914
915                 }
916                 else {
917
918                         pthru = mega_prepare_passthru(adapter, scb, cmd,
919                                         channel, target);
920
921                         /* Initialize mailbox */
922                         if( adapter->has_64bit_addr ) {
923                                 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
924                         }
925                         else {
926                                 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
927                         }
928
929                         mbox->m_out.xferaddr = scb->pthru_dma_addr;
930
931                 }
932                 return scb;
933         }
934         return NULL;
935 }
936
937
938 /**
939  * mega_prepare_passthru()
940  * @adapter - pointer to our soft state
941  * @scb - our scsi control block
942  * @cmd - scsi command from the mid-layer
943  * @channel - actual channel on the controller
944  * @target - actual id on the controller.
945  *
946  * prepare a command for the scsi physical devices.
947  */
948 static mega_passthru *
949 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
950                 int channel, int target)
951 {
952         mega_passthru *pthru;
953
954         pthru = scb->pthru;
955         memset(pthru, 0, sizeof (mega_passthru));
956
957         /* 0=6sec/1=60sec/2=10min/3=3hrs */
958         pthru->timeout = 2;
959
960         pthru->ars = 1;
961         pthru->reqsenselen = 14;
962         pthru->islogical = 0;
963
964         pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
965
966         pthru->target = (adapter->flag & BOARD_40LD) ?
967                 (channel << 4) | target : target;
968
969         pthru->cdblen = cmd->cmd_len;
970         pthru->logdrv = cmd->device->lun;
971
972         memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
973
974         /* Not sure about the direction */
975         scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
976
977         /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
978         switch (cmd->cmnd[0]) {
979         case INQUIRY:
980         case READ_CAPACITY:
981                 if(!(adapter->flag & (1L << cmd->device->channel))) {
982
983                         printk(KERN_NOTICE
984                                 "scsi%d: scanning scsi channel %d [P%d] ",
985                                         adapter->host->host_no,
986                                         cmd->device->channel, channel);
987                         printk("for physical devices.\n");
988
989                         adapter->flag |= (1L << cmd->device->channel);
990                 }
991                 /* Fall through */
992         default:
993                 pthru->numsgelements = mega_build_sglist(adapter, scb,
994                                 &pthru->dataxferaddr, &pthru->dataxferlen);
995                 break;
996         }
997         return pthru;
998 }
999
1000
1001 /**
1002  * mega_prepare_extpassthru()
1003  * @adapter - pointer to our soft state
1004  * @scb - our scsi control block
1005  * @cmd - scsi command from the mid-layer
1006  * @channel - actual channel on the controller
1007  * @target - actual id on the controller.
1008  *
1009  * prepare a command for the scsi physical devices. This rountine prepares
1010  * commands for devices which can take extended CDBs (>10 bytes)
1011  */
1012 static mega_ext_passthru *
1013 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1014                 int channel, int target)
1015 {
1016         mega_ext_passthru       *epthru;
1017
1018         epthru = scb->epthru;
1019         memset(epthru, 0, sizeof(mega_ext_passthru));
1020
1021         /* 0=6sec/1=60sec/2=10min/3=3hrs */
1022         epthru->timeout = 2;
1023
1024         epthru->ars = 1;
1025         epthru->reqsenselen = 14;
1026         epthru->islogical = 0;
1027
1028         epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1029         epthru->target = (adapter->flag & BOARD_40LD) ?
1030                 (channel << 4) | target : target;
1031
1032         epthru->cdblen = cmd->cmd_len;
1033         epthru->logdrv = cmd->device->lun;
1034
1035         memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1036
1037         /* Not sure about the direction */
1038         scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1039
1040         switch(cmd->cmnd[0]) {
1041         case INQUIRY:
1042         case READ_CAPACITY:
1043                 if(!(adapter->flag & (1L << cmd->device->channel))) {
1044
1045                         printk(KERN_NOTICE
1046                                 "scsi%d: scanning scsi channel %d [P%d] ",
1047                                         adapter->host->host_no,
1048                                         cmd->device->channel, channel);
1049                         printk("for physical devices.\n");
1050
1051                         adapter->flag |= (1L << cmd->device->channel);
1052                 }
1053                 /* Fall through */
1054         default:
1055                 epthru->numsgelements = mega_build_sglist(adapter, scb,
1056                                 &epthru->dataxferaddr, &epthru->dataxferlen);
1057                 break;
1058         }
1059
1060         return epthru;
1061 }
1062
1063 static void
1064 __mega_runpendq(adapter_t *adapter)
1065 {
1066         scb_t *scb;
1067         struct list_head *pos, *next;
1068
1069         /* Issue any pending commands to the card */
1070         list_for_each_safe(pos, next, &adapter->pending_list) {
1071
1072                 scb = list_entry(pos, scb_t, list);
1073
1074                 if( !(scb->state & SCB_ISSUED) ) {
1075
1076                         if( issue_scb(adapter, scb) != 0 )
1077                                 return;
1078                 }
1079         }
1080
1081         return;
1082 }
1083
1084
1085 /**
1086  * issue_scb()
1087  * @adapter - pointer to our soft state
1088  * @scb - scsi control block
1089  *
1090  * Post a command to the card if the mailbox is available, otherwise return
1091  * busy. We also take the scb from the pending list if the mailbox is
1092  * available.
1093  */
1094 static int
1095 issue_scb(adapter_t *adapter, scb_t *scb)
1096 {
1097         volatile mbox64_t       *mbox64 = adapter->mbox64;
1098         volatile mbox_t         *mbox = adapter->mbox;
1099         unsigned int    i = 0;
1100
1101         if(unlikely(mbox->m_in.busy)) {
1102                 do {
1103                         udelay(1);
1104                         i++;
1105                 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1106
1107                 if(mbox->m_in.busy) return -1;
1108         }
1109
1110         /* Copy mailbox data into host structure */
1111         memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox, 
1112                         sizeof(struct mbox_out));
1113
1114         mbox->m_out.cmdid = scb->idx;   /* Set cmdid */
1115         mbox->m_in.busy = 1;            /* Set busy */
1116
1117
1118         /*
1119          * Increment the pending queue counter
1120          */
1121         atomic_inc(&adapter->pend_cmds);
1122
1123         switch (mbox->m_out.cmd) {
1124         case MEGA_MBOXCMD_LREAD64:
1125         case MEGA_MBOXCMD_LWRITE64:
1126         case MEGA_MBOXCMD_PASSTHRU64:
1127         case MEGA_MBOXCMD_EXTPTHRU:
1128                 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1129                 mbox64->xfer_segment_hi = 0;
1130                 mbox->m_out.xferaddr = 0xFFFFFFFF;
1131                 break;
1132         default:
1133                 mbox64->xfer_segment_lo = 0;
1134                 mbox64->xfer_segment_hi = 0;
1135         }
1136
1137         /*
1138          * post the command
1139          */
1140         scb->state |= SCB_ISSUED;
1141
1142         if( likely(adapter->flag & BOARD_MEMMAP) ) {
1143                 mbox->m_in.poll = 0;
1144                 mbox->m_in.ack = 0;
1145                 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1146         }
1147         else {
1148                 irq_enable(adapter);
1149                 issue_command(adapter);
1150         }
1151
1152         return 0;
1153 }
1154
1155 /*
1156  * Wait until the controller's mailbox is available
1157  */
1158 static inline int
1159 mega_busywait_mbox (adapter_t *adapter)
1160 {
1161         if (adapter->mbox->m_in.busy)
1162                 return __mega_busywait_mbox(adapter);
1163         return 0;
1164 }
1165
1166 /**
1167  * issue_scb_block()
1168  * @adapter - pointer to our soft state
1169  * @raw_mbox - the mailbox
1170  *
1171  * Issue a scb in synchronous and non-interrupt mode
1172  */
1173 static int
1174 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1175 {
1176         volatile mbox64_t *mbox64 = adapter->mbox64;
1177         volatile mbox_t *mbox = adapter->mbox;
1178         u8      byte;
1179
1180         /* Wait until mailbox is free */
1181         if(mega_busywait_mbox (adapter))
1182                 goto bug_blocked_mailbox;
1183
1184         /* Copy mailbox data into host structure */
1185         memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1186         mbox->m_out.cmdid = 0xFE;
1187         mbox->m_in.busy = 1;
1188
1189         switch (raw_mbox[0]) {
1190         case MEGA_MBOXCMD_LREAD64:
1191         case MEGA_MBOXCMD_LWRITE64:
1192         case MEGA_MBOXCMD_PASSTHRU64:
1193         case MEGA_MBOXCMD_EXTPTHRU:
1194                 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1195                 mbox64->xfer_segment_hi = 0;
1196                 mbox->m_out.xferaddr = 0xFFFFFFFF;
1197                 break;
1198         default:
1199                 mbox64->xfer_segment_lo = 0;
1200                 mbox64->xfer_segment_hi = 0;
1201         }
1202
1203         if( likely(adapter->flag & BOARD_MEMMAP) ) {
1204                 mbox->m_in.poll = 0;
1205                 mbox->m_in.ack = 0;
1206                 mbox->m_in.numstatus = 0xFF;
1207                 mbox->m_in.status = 0xFF;
1208                 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1209
1210                 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1211                         cpu_relax();
1212
1213                 mbox->m_in.numstatus = 0xFF;
1214
1215                 while( (volatile u8)mbox->m_in.poll != 0x77 )
1216                         cpu_relax();
1217
1218                 mbox->m_in.poll = 0;
1219                 mbox->m_in.ack = 0x77;
1220
1221                 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1222
1223                 while(RDINDOOR(adapter) & 0x2)
1224                         cpu_relax();
1225         }
1226         else {
1227                 irq_disable(adapter);
1228                 issue_command(adapter);
1229
1230                 while (!((byte = irq_state(adapter)) & INTR_VALID))
1231                         cpu_relax();
1232
1233                 set_irq_state(adapter, byte);
1234                 irq_enable(adapter);
1235                 irq_ack(adapter);
1236         }
1237
1238         return mbox->m_in.status;
1239
1240 bug_blocked_mailbox:
1241         printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1242         udelay (1000);
1243         return -1;
1244 }
1245
1246
1247 /**
1248  * megaraid_isr_iomapped()
1249  * @irq - irq
1250  * @devp - pointer to our soft state
1251  *
1252  * Interrupt service routine for io-mapped controllers.
1253  * Find out if our device is interrupting. If yes, acknowledge the interrupt
1254  * and service the completed commands.
1255  */
1256 static irqreturn_t
1257 megaraid_isr_iomapped(int irq, void *devp)
1258 {
1259         adapter_t       *adapter = devp;
1260         unsigned long   flags;
1261         u8      status;
1262         u8      nstatus;
1263         u8      completed[MAX_FIRMWARE_STATUS];
1264         u8      byte;
1265         int     handled = 0;
1266
1267
1268         /*
1269          * loop till F/W has more commands for us to complete.
1270          */
1271         spin_lock_irqsave(&adapter->lock, flags);
1272
1273         do {
1274                 /* Check if a valid interrupt is pending */
1275                 byte = irq_state(adapter);
1276                 if( (byte & VALID_INTR_BYTE) == 0 ) {
1277                         /*
1278                          * No more pending commands
1279                          */
1280                         goto out_unlock;
1281                 }
1282                 set_irq_state(adapter, byte);
1283
1284                 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1285                                 == 0xFF)
1286                         cpu_relax();
1287                 adapter->mbox->m_in.numstatus = 0xFF;
1288
1289                 status = adapter->mbox->m_in.status;
1290
1291                 /*
1292                  * decrement the pending queue counter
1293                  */
1294                 atomic_sub(nstatus, &adapter->pend_cmds);
1295
1296                 memcpy(completed, (void *)adapter->mbox->m_in.completed, 
1297                                 nstatus);
1298
1299                 /* Acknowledge interrupt */
1300                 irq_ack(adapter);
1301
1302                 mega_cmd_done(adapter, completed, nstatus, status);
1303
1304                 mega_rundoneq(adapter);
1305
1306                 handled = 1;
1307
1308                 /* Loop through any pending requests */
1309                 if(atomic_read(&adapter->quiescent) == 0) {
1310                         mega_runpendq(adapter);
1311                 }
1312
1313         } while(1);
1314
1315  out_unlock:
1316
1317         spin_unlock_irqrestore(&adapter->lock, flags);
1318
1319         return IRQ_RETVAL(handled);
1320 }
1321
1322
1323 /**
1324  * megaraid_isr_memmapped()
1325  * @irq - irq
1326  * @devp - pointer to our soft state
1327  *
1328  * Interrupt service routine for memory-mapped controllers.
1329  * Find out if our device is interrupting. If yes, acknowledge the interrupt
1330  * and service the completed commands.
1331  */
1332 static irqreturn_t
1333 megaraid_isr_memmapped(int irq, void *devp)
1334 {
1335         adapter_t       *adapter = devp;
1336         unsigned long   flags;
1337         u8      status;
1338         u32     dword = 0;
1339         u8      nstatus;
1340         u8      completed[MAX_FIRMWARE_STATUS];
1341         int     handled = 0;
1342
1343
1344         /*
1345          * loop till F/W has more commands for us to complete.
1346          */
1347         spin_lock_irqsave(&adapter->lock, flags);
1348
1349         do {
1350                 /* Check if a valid interrupt is pending */
1351                 dword = RDOUTDOOR(adapter);
1352                 if(dword != 0x10001234) {
1353                         /*
1354                          * No more pending commands
1355                          */
1356                         goto out_unlock;
1357                 }
1358                 WROUTDOOR(adapter, 0x10001234);
1359
1360                 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1361                                 == 0xFF) {
1362                         cpu_relax();
1363                 }
1364                 adapter->mbox->m_in.numstatus = 0xFF;
1365
1366                 status = adapter->mbox->m_in.status;
1367
1368                 /*
1369                  * decrement the pending queue counter
1370                  */
1371                 atomic_sub(nstatus, &adapter->pend_cmds);
1372
1373                 memcpy(completed, (void *)adapter->mbox->m_in.completed, 
1374                                 nstatus);
1375
1376                 /* Acknowledge interrupt */
1377                 WRINDOOR(adapter, 0x2);
1378
1379                 handled = 1;
1380
1381                 while( RDINDOOR(adapter) & 0x02 )
1382                         cpu_relax();
1383
1384                 mega_cmd_done(adapter, completed, nstatus, status);
1385
1386                 mega_rundoneq(adapter);
1387
1388                 /* Loop through any pending requests */
1389                 if(atomic_read(&adapter->quiescent) == 0) {
1390                         mega_runpendq(adapter);
1391                 }
1392
1393         } while(1);
1394
1395  out_unlock:
1396
1397         spin_unlock_irqrestore(&adapter->lock, flags);
1398
1399         return IRQ_RETVAL(handled);
1400 }
1401 /**
1402  * mega_cmd_done()
1403  * @adapter - pointer to our soft state
1404  * @completed - array of ids of completed commands
1405  * @nstatus - number of completed commands
1406  * @status - status of the last command completed
1407  *
1408  * Complete the comamnds and call the scsi mid-layer callback hooks.
1409  */
1410 static void
1411 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1412 {
1413         mega_ext_passthru       *epthru = NULL;
1414         struct scatterlist      *sgl;
1415         Scsi_Cmnd       *cmd = NULL;
1416         mega_passthru   *pthru = NULL;
1417         mbox_t  *mbox = NULL;
1418         u8      c;
1419         scb_t   *scb;
1420         int     islogical;
1421         int     cmdid;
1422         int     i;
1423
1424         /*
1425          * for all the commands completed, call the mid-layer callback routine
1426          * and free the scb.
1427          */
1428         for( i = 0; i < nstatus; i++ ) {
1429
1430                 cmdid = completed[i];
1431
1432                 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1433                         scb = &adapter->int_scb;
1434                         cmd = scb->cmd;
1435                         mbox = (mbox_t *)scb->raw_mbox;
1436
1437                         /*
1438                          * Internal command interface do not fire the extended
1439                          * passthru or 64-bit passthru
1440                          */
1441                         pthru = scb->pthru;
1442
1443                 }
1444                 else {
1445                         scb = &adapter->scb_list[cmdid];
1446
1447                         /*
1448                          * Make sure f/w has completed a valid command
1449                          */
1450                         if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1451                                 printk(KERN_CRIT
1452                                         "megaraid: invalid command ");
1453                                 printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1454                                         cmdid, scb->state, scb->cmd);
1455
1456                                 continue;
1457                         }
1458
1459                         /*
1460                          * Was a abort issued for this command
1461                          */
1462                         if( scb->state & SCB_ABORT ) {
1463
1464                                 printk(KERN_WARNING
1465                                 "megaraid: aborted cmd %lx[%x] complete.\n",
1466                                         scb->cmd->serial_number, scb->idx);
1467
1468                                 scb->cmd->result = (DID_ABORT << 16);
1469
1470                                 list_add_tail(SCSI_LIST(scb->cmd),
1471                                                 &adapter->completed_list);
1472
1473                                 mega_free_scb(adapter, scb);
1474
1475                                 continue;
1476                         }
1477
1478                         /*
1479                          * Was a reset issued for this command
1480                          */
1481                         if( scb->state & SCB_RESET ) {
1482
1483                                 printk(KERN_WARNING
1484                                 "megaraid: reset cmd %lx[%x] complete.\n",
1485                                         scb->cmd->serial_number, scb->idx);
1486
1487                                 scb->cmd->result = (DID_RESET << 16);
1488
1489                                 list_add_tail(SCSI_LIST(scb->cmd),
1490                                                 &adapter->completed_list);
1491
1492                                 mega_free_scb (adapter, scb);
1493
1494                                 continue;
1495                         }
1496
1497                         cmd = scb->cmd;
1498                         pthru = scb->pthru;
1499                         epthru = scb->epthru;
1500                         mbox = (mbox_t *)scb->raw_mbox;
1501
1502 #if MEGA_HAVE_STATS
1503                         {
1504
1505                         int     logdrv = mbox->m_out.logdrv;
1506
1507                         islogical = adapter->logdrv_chan[cmd->channel];
1508                         /*
1509                          * Maintain an error counter for the logical drive.
1510                          * Some application like SNMP agent need such
1511                          * statistics
1512                          */
1513                         if( status && islogical && (cmd->cmnd[0] == READ_6 ||
1514                                                 cmd->cmnd[0] == READ_10 ||
1515                                                 cmd->cmnd[0] == READ_12)) {
1516                                 /*
1517                                  * Logical drive number increases by 0x80 when
1518                                  * a logical drive is deleted
1519                                  */
1520                                 adapter->rd_errors[logdrv%0x80]++;
1521                         }
1522
1523                         if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
1524                                                 cmd->cmnd[0] == WRITE_10 ||
1525                                                 cmd->cmnd[0] == WRITE_12)) {
1526                                 /*
1527                                  * Logical drive number increases by 0x80 when
1528                                  * a logical drive is deleted
1529                                  */
1530                                 adapter->wr_errors[logdrv%0x80]++;
1531                         }
1532
1533                         }
1534 #endif
1535                 }
1536
1537                 /*
1538                  * Do not return the presence of hard disk on the channel so,
1539                  * inquiry sent, and returned data==hard disk or removable
1540                  * hard disk and not logical, request should return failure! -
1541                  * PJ
1542                  */
1543                 islogical = adapter->logdrv_chan[cmd->device->channel];
1544                 if( cmd->cmnd[0] == INQUIRY && !islogical ) {
1545
1546                         sgl = scsi_sglist(cmd);
1547                         if( sg_page(sgl) ) {
1548                                 c = *(unsigned char *) sg_virt(&sgl[0]);
1549                         } else {
1550                                 printk(KERN_WARNING
1551                                        "megaraid: invalid sg.\n");
1552                                 c = 0;
1553                         }
1554
1555                         if(IS_RAID_CH(adapter, cmd->device->channel) &&
1556                                         ((c & 0x1F ) == TYPE_DISK)) {
1557                                 status = 0xF0;
1558                         }
1559                 }
1560
1561                 /* clear result; otherwise, success returns corrupt value */
1562                 cmd->result = 0;
1563
1564                 /* Convert MegaRAID status to Linux error code */
1565                 switch (status) {
1566                 case 0x00:      /* SUCCESS , i.e. SCSI_STATUS_GOOD */
1567                         cmd->result |= (DID_OK << 16);
1568                         break;
1569
1570                 case 0x02:      /* ERROR_ABORTED, i.e.
1571                                    SCSI_STATUS_CHECK_CONDITION */
1572
1573                         /* set sense_buffer and result fields */
1574                         if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
1575                                 mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
1576
1577                                 memcpy(cmd->sense_buffer, pthru->reqsensearea,
1578                                                 14);
1579
1580                                 cmd->result = (DRIVER_SENSE << 24) |
1581                                         (DID_OK << 16) |
1582                                         (CHECK_CONDITION << 1);
1583                         }
1584                         else {
1585                                 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1586
1587                                         memcpy(cmd->sense_buffer,
1588                                                 epthru->reqsensearea, 14);
1589
1590                                         cmd->result = (DRIVER_SENSE << 24) |
1591                                                 (DID_OK << 16) |
1592                                                 (CHECK_CONDITION << 1);
1593                                 } else {
1594                                         cmd->sense_buffer[0] = 0x70;
1595                                         cmd->sense_buffer[2] = ABORTED_COMMAND;
1596                                         cmd->result |= (CHECK_CONDITION << 1);
1597                                 }
1598                         }
1599                         break;
1600
1601                 case 0x08:      /* ERR_DEST_DRIVE_FAILED, i.e.
1602                                    SCSI_STATUS_BUSY */
1603                         cmd->result |= (DID_BUS_BUSY << 16) | status;
1604                         break;
1605
1606                 default:
1607 #if MEGA_HAVE_CLUSTERING
1608                         /*
1609                          * If TEST_UNIT_READY fails, we know
1610                          * MEGA_RESERVATION_STATUS failed
1611                          */
1612                         if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1613                                 cmd->result |= (DID_ERROR << 16) |
1614                                         (RESERVATION_CONFLICT << 1);
1615                         }
1616                         else
1617                         /*
1618                          * Error code returned is 1 if Reserve or Release
1619                          * failed or the input parameter is invalid
1620                          */
1621                         if( status == 1 &&
1622                                 (cmd->cmnd[0] == RESERVE ||
1623                                          cmd->cmnd[0] == RELEASE) ) {
1624
1625                                 cmd->result |= (DID_ERROR << 16) |
1626                                         (RESERVATION_CONFLICT << 1);
1627                         }
1628                         else
1629 #endif
1630                                 cmd->result |= (DID_BAD_TARGET << 16)|status;
1631                 }
1632
1633                 /*
1634                  * Only free SCBs for the commands coming down from the
1635                  * mid-layer, not for which were issued internally
1636                  *
1637                  * For internal command, restore the status returned by the
1638                  * firmware so that user can interpret it.
1639                  */
1640                 if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1641                         cmd->result = status;
1642
1643                         /*
1644                          * Remove the internal command from the pending list
1645                          */
1646                         list_del_init(&scb->list);
1647                         scb->state = SCB_FREE;
1648                 }
1649                 else {
1650                         mega_free_scb(adapter, scb);
1651                 }
1652
1653                 /* Add Scsi_Command to end of completed queue */
1654                 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1655         }
1656 }
1657
1658
1659 /*
1660  * mega_runpendq()
1661  *
1662  * Run through the list of completed requests and finish it
1663  */
1664 static void
1665 mega_rundoneq (adapter_t *adapter)
1666 {
1667         Scsi_Cmnd *cmd;
1668         struct list_head *pos;
1669
1670         list_for_each(pos, &adapter->completed_list) {
1671
1672                 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1673
1674                 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1675                 cmd->scsi_done(cmd);
1676         }
1677
1678         INIT_LIST_HEAD(&adapter->completed_list);
1679 }
1680
1681
1682 /*
1683  * Free a SCB structure
1684  * Note: We assume the scsi commands associated with this scb is not free yet.
1685  */
1686 static void
1687 mega_free_scb(adapter_t *adapter, scb_t *scb)
1688 {
1689         switch( scb->dma_type ) {
1690
1691         case MEGA_DMA_TYPE_NONE:
1692                 break;
1693
1694         case MEGA_SGLIST:
1695                 scsi_dma_unmap(scb->cmd);
1696                 break;
1697         default:
1698                 break;
1699         }
1700
1701         /*
1702          * Remove from the pending list
1703          */
1704         list_del_init(&scb->list);
1705
1706         /* Link the scb back into free list */
1707         scb->state = SCB_FREE;
1708         scb->cmd = NULL;
1709
1710         list_add(&scb->list, &adapter->free_list);
1711 }
1712
1713
1714 static int
1715 __mega_busywait_mbox (adapter_t *adapter)
1716 {
1717         volatile mbox_t *mbox = adapter->mbox;
1718         long counter;
1719
1720         for (counter = 0; counter < 10000; counter++) {
1721                 if (!mbox->m_in.busy)
1722                         return 0;
1723                 udelay(100);
1724                 cond_resched();
1725         }
1726         return -1;              /* give up after 1 second */
1727 }
1728
1729 /*
1730  * Copies data to SGLIST
1731  * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1732  */
1733 static int
1734 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1735 {
1736         struct scatterlist *sg;
1737         Scsi_Cmnd       *cmd;
1738         int     sgcnt;
1739         int     idx;
1740
1741         cmd = scb->cmd;
1742
1743         /*
1744          * Copy Scatter-Gather list info into controller structure.
1745          *
1746          * The number of sg elements returned must not exceed our limit
1747          */
1748         sgcnt = scsi_dma_map(cmd);
1749
1750         scb->dma_type = MEGA_SGLIST;
1751
1752         BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1753
1754         *len = 0;
1755
1756         if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1757                 sg = scsi_sglist(cmd);
1758                 scb->dma_h_bulkdata = sg_dma_address(sg);
1759                 *buf = (u32)scb->dma_h_bulkdata;
1760                 *len = sg_dma_len(sg);
1761                 return 0;
1762         }
1763
1764         scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1765                 if (adapter->has_64bit_addr) {
1766                         scb->sgl64[idx].address = sg_dma_address(sg);
1767                         *len += scb->sgl64[idx].length = sg_dma_len(sg);
1768                 } else {
1769                         scb->sgl[idx].address = sg_dma_address(sg);
1770                         *len += scb->sgl[idx].length = sg_dma_len(sg);
1771                 }
1772         }
1773
1774         /* Reset pointer and length fields */
1775         *buf = scb->sgl_dma_addr;
1776
1777         /* Return count of SG requests */
1778         return sgcnt;
1779 }
1780
1781
1782 /*
1783  * mega_8_to_40ld()
1784  *
1785  * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1786  * Enquiry3 structures for later use
1787  */
1788 static void
1789 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1790                 mega_product_info *product_info)
1791 {
1792         int i;
1793
1794         product_info->max_commands = inquiry->adapter_info.max_commands;
1795         enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1796         product_info->nchannels = inquiry->adapter_info.nchannels;
1797
1798         for (i = 0; i < 4; i++) {
1799                 product_info->fw_version[i] =
1800                         inquiry->adapter_info.fw_version[i];
1801
1802                 product_info->bios_version[i] =
1803                         inquiry->adapter_info.bios_version[i];
1804         }
1805         enquiry3->cache_flush_interval =
1806                 inquiry->adapter_info.cache_flush_interval;
1807
1808         product_info->dram_size = inquiry->adapter_info.dram_size;
1809
1810         enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1811
1812         for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1813                 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1814                 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1815                 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1816         }
1817
1818         for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1819                 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1820 }
1821
1822 static inline void
1823 mega_free_sgl(adapter_t *adapter)
1824 {
1825         scb_t   *scb;
1826         int     i;
1827
1828         for(i = 0; i < adapter->max_cmds; i++) {
1829
1830                 scb = &adapter->scb_list[i];
1831
1832                 if( scb->sgl64 ) {
1833                         pci_free_consistent(adapter->dev,
1834                                 sizeof(mega_sgl64) * adapter->sglen,
1835                                 scb->sgl64,
1836                                 scb->sgl_dma_addr);
1837
1838                         scb->sgl64 = NULL;
1839                 }
1840
1841                 if( scb->pthru ) {
1842                         pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1843                                 scb->pthru, scb->pthru_dma_addr);
1844
1845                         scb->pthru = NULL;
1846                 }
1847
1848                 if( scb->epthru ) {
1849                         pci_free_consistent(adapter->dev,
1850                                 sizeof(mega_ext_passthru),
1851                                 scb->epthru, scb->epthru_dma_addr);
1852
1853                         scb->epthru = NULL;
1854                 }
1855
1856         }
1857 }
1858
1859
1860 /*
1861  * Get information about the card/driver
1862  */
1863 const char *
1864 megaraid_info(struct Scsi_Host *host)
1865 {
1866         static char buffer[512];
1867         adapter_t *adapter;
1868
1869         adapter = (adapter_t *)host->hostdata;
1870
1871         sprintf (buffer,
1872                  "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1873                  adapter->fw_version, adapter->product_info.max_commands,
1874                  adapter->host->max_id, adapter->host->max_channel,
1875                  adapter->host->max_lun);
1876         return buffer;
1877 }
1878
1879 /*
1880  * Abort a previous SCSI request. Only commands on the pending list can be
1881  * aborted. All the commands issued to the F/W must complete.
1882  */
1883 static int
1884 megaraid_abort(Scsi_Cmnd *cmd)
1885 {
1886         adapter_t       *adapter;
1887         int             rval;
1888
1889         adapter = (adapter_t *)cmd->device->host->hostdata;
1890
1891         rval =  megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1892
1893         /*
1894          * This is required here to complete any completed requests
1895          * to be communicated over to the mid layer.
1896          */
1897         mega_rundoneq(adapter);
1898
1899         return rval;
1900 }
1901
1902
1903 static int
1904 megaraid_reset(struct scsi_cmnd *cmd)
1905 {
1906         adapter_t       *adapter;
1907         megacmd_t       mc;
1908         int             rval;
1909
1910         adapter = (adapter_t *)cmd->device->host->hostdata;
1911
1912 #if MEGA_HAVE_CLUSTERING
1913         mc.cmd = MEGA_CLUSTER_CMD;
1914         mc.opcode = MEGA_RESET_RESERVATIONS;
1915
1916         if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1917                 printk(KERN_WARNING
1918                                 "megaraid: reservation reset failed.\n");
1919         }
1920         else {
1921                 printk(KERN_INFO "megaraid: reservation reset.\n");
1922         }
1923 #endif
1924
1925         spin_lock_irq(&adapter->lock);
1926
1927         rval =  megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1928
1929         /*
1930          * This is required here to complete any completed requests
1931          * to be communicated over to the mid layer.
1932          */
1933         mega_rundoneq(adapter);
1934         spin_unlock_irq(&adapter->lock);
1935
1936         return rval;
1937 }
1938
1939 /**
1940  * megaraid_abort_and_reset()
1941  * @adapter - megaraid soft state
1942  * @cmd - scsi command to be aborted or reset
1943  * @aor - abort or reset flag
1944  *
1945  * Try to locate the scsi command in the pending queue. If found and is not
1946  * issued to the controller, abort/reset it. Otherwise return failure
1947  */
1948 static int
1949 megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1950 {
1951         struct list_head        *pos, *next;
1952         scb_t                   *scb;
1953
1954         printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
1955              (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
1956              cmd->cmnd[0], cmd->device->channel, 
1957              cmd->device->id, cmd->device->lun);
1958
1959         if(list_empty(&adapter->pending_list))
1960                 return FALSE;
1961
1962         list_for_each_safe(pos, next, &adapter->pending_list) {
1963
1964                 scb = list_entry(pos, scb_t, list);
1965
1966                 if (scb->cmd == cmd) { /* Found command */
1967
1968                         scb->state |= aor;
1969
1970                         /*
1971                          * Check if this command has firmware ownership. If
1972                          * yes, we cannot reset this command. Whenever f/w
1973                          * completes this command, we will return appropriate
1974                          * status from ISR.
1975                          */
1976                         if( scb->state & SCB_ISSUED ) {
1977
1978                                 printk(KERN_WARNING
1979                                         "megaraid: %s-%lx[%x], fw owner.\n",
1980                                         (aor==SCB_ABORT) ? "ABORTING":"RESET",
1981                                         cmd->serial_number, scb->idx);
1982
1983                                 return FALSE;
1984                         }
1985                         else {
1986
1987                                 /*
1988                                  * Not yet issued! Remove from the pending
1989                                  * list
1990                                  */
1991                                 printk(KERN_WARNING
1992                                         "megaraid: %s-%lx[%x], driver owner.\n",
1993                                         (aor==SCB_ABORT) ? "ABORTING":"RESET",
1994                                         cmd->serial_number, scb->idx);
1995
1996                                 mega_free_scb(adapter, scb);
1997
1998                                 if( aor == SCB_ABORT ) {
1999                                         cmd->result = (DID_ABORT << 16);
2000                                 }
2001                                 else {
2002                                         cmd->result = (DID_RESET << 16);
2003                                 }
2004
2005                                 list_add_tail(SCSI_LIST(cmd),
2006                                                 &adapter->completed_list);
2007
2008                                 return TRUE;
2009                         }
2010                 }
2011         }
2012
2013         return FALSE;
2014 }
2015
2016 static inline int
2017 make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2018 {
2019         *pdev = alloc_pci_dev();
2020
2021         if( *pdev == NULL ) return -1;
2022
2023         memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2024
2025         if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
2026                 kfree(*pdev);
2027                 return -1;
2028         }
2029
2030         return 0;
2031 }
2032
2033 static inline void
2034 free_local_pdev(struct pci_dev *pdev)
2035 {
2036         kfree(pdev);
2037 }
2038
2039 /**
2040  * mega_allocate_inquiry()
2041  * @dma_handle - handle returned for dma address
2042  * @pdev - handle to pci device
2043  *
2044  * allocates memory for inquiry structure
2045  */
2046 static inline void *
2047 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2048 {
2049         return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2050 }
2051
2052
2053 static inline void
2054 mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2055 {
2056         pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2057 }
2058
2059
2060 #ifdef CONFIG_PROC_FS
2061 /* Following code handles /proc fs  */
2062
2063 #define CREATE_READ_PROC(string, func)  create_proc_read_entry(string,  \
2064                                         S_IRUSR | S_IFREG,              \
2065                                         controller_proc_dir_entry,      \
2066                                         func, adapter)
2067
2068 /**
2069  * mega_create_proc_entry()
2070  * @index - index in soft state array
2071  * @parent - parent node for this /proc entry
2072  *
2073  * Creates /proc entries for our controllers.
2074  */
2075 static void
2076 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2077 {
2078         struct proc_dir_entry   *controller_proc_dir_entry = NULL;
2079         u8              string[64] = { 0 };
2080         adapter_t       *adapter = hba_soft_state[index];
2081
2082         sprintf(string, "hba%d", adapter->host->host_no);
2083
2084         controller_proc_dir_entry =
2085                 adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
2086
2087         if(!controller_proc_dir_entry) {
2088                 printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2089                 return;
2090         }
2091         adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
2092         adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
2093         adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
2094 #if MEGA_HAVE_ENH_PROC
2095         adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
2096         adapter->proc_battery = CREATE_READ_PROC("battery-status",
2097                         proc_battery);
2098
2099         /*
2100          * Display each physical drive on its channel
2101          */
2102         adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
2103                                         proc_pdrv_ch0);
2104         adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
2105                                         proc_pdrv_ch1);
2106         adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
2107                                         proc_pdrv_ch2);
2108         adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
2109                                         proc_pdrv_ch3);
2110
2111         /*
2112          * Display a set of up to 10 logical drive through each of following
2113          * /proc entries
2114          */
2115         adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
2116                                         proc_rdrv_10);
2117         adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
2118                                         proc_rdrv_20);
2119         adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
2120                                         proc_rdrv_30);
2121         adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
2122                                         proc_rdrv_40);
2123 #endif
2124 }
2125
2126
2127 /**
2128  * proc_read_config()
2129  * @page - buffer to write the data in
2130  * @start - where the actual data has been written in page
2131  * @offset - same meaning as the read system call
2132  * @count - same meaning as the read system call
2133  * @eof - set if no more data needs to be returned
2134  * @data - pointer to our soft state
2135  *
2136  * Display configuration information about the controller.
2137  */
2138 static int
2139 proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
2140                 void *data)
2141 {
2142
2143         adapter_t *adapter = (adapter_t *)data;
2144         int len = 0;
2145
2146         len += sprintf(page+len, "%s", MEGARAID_VERSION);
2147
2148         if(adapter->product_info.product_name[0])
2149                 len += sprintf(page+len, "%s\n",
2150                                 adapter->product_info.product_name);
2151
2152         len += sprintf(page+len, "Controller Type: ");
2153
2154         if( adapter->flag & BOARD_MEMMAP ) {
2155                 len += sprintf(page+len,
2156                         "438/466/467/471/493/518/520/531/532\n");
2157         }
2158         else {
2159                 len += sprintf(page+len,
2160                         "418/428/434\n");
2161         }
2162
2163         if(adapter->flag & BOARD_40LD) {
2164                 len += sprintf(page+len,
2165                                 "Controller Supports 40 Logical Drives\n");
2166         }
2167
2168         if(adapter->flag & BOARD_64BIT) {
2169                 len += sprintf(page+len,
2170                 "Controller capable of 64-bit memory addressing\n");
2171         }
2172         if( adapter->has_64bit_addr ) {
2173                 len += sprintf(page+len,
2174                         "Controller using 64-bit memory addressing\n");
2175         }
2176         else {
2177                 len += sprintf(page+len,
2178                         "Controller is not using 64-bit memory addressing\n");
2179         }
2180
2181         len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
2182                         adapter->host->irq);
2183
2184         len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
2185                         adapter->numldrv, adapter->product_info.nchannels);
2186
2187         len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
2188                         adapter->fw_version, adapter->bios_version,
2189                         adapter->product_info.dram_size);
2190
2191         len += sprintf(page+len,
2192                 "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2193                 adapter->product_info.max_commands, adapter->max_cmds);
2194
2195         len += sprintf(page+len, "support_ext_cdb    = %d\n",
2196                         adapter->support_ext_cdb);
2197         len += sprintf(page+len, "support_random_del = %d\n",
2198                         adapter->support_random_del);
2199         len += sprintf(page+len, "boot_ldrv_enabled  = %d\n",
2200                         adapter->boot_ldrv_enabled);
2201         len += sprintf(page+len, "boot_ldrv          = %d\n",
2202                         adapter->boot_ldrv);
2203         len += sprintf(page+len, "boot_pdrv_enabled  = %d\n",
2204                         adapter->boot_pdrv_enabled);
2205         len += sprintf(page+len, "boot_pdrv_ch       = %d\n",
2206                         adapter->boot_pdrv_ch);
2207         len += sprintf(page+len, "boot_pdrv_tgt      = %d\n",
2208                         adapter->boot_pdrv_tgt);
2209         len += sprintf(page+len, "quiescent          = %d\n",
2210                         atomic_read(&adapter->quiescent));
2211         len += sprintf(page+len, "has_cluster        = %d\n",
2212                         adapter->has_cluster);
2213
2214         len += sprintf(page+len, "\nModule Parameters:\n");
2215         len += sprintf(page+len, "max_cmd_per_lun    = %d\n",
2216                         max_cmd_per_lun);
2217         len += sprintf(page+len, "max_sectors_per_io = %d\n",
2218                         max_sectors_per_io);
2219
2220         *eof = 1;
2221
2222         return len;
2223 }
2224
2225
2226
2227 /**
2228  * proc_read_stat()
2229  * @page - buffer to write the data in
2230  * @start - where the actual data has been written in page
2231  * @offset - same meaning as the read system call
2232  * @count - same meaning as the read system call
2233  * @eof - set if no more data needs to be returned
2234  * @data - pointer to our soft state
2235  *
2236  * Diaplay statistical information about the I/O activity.
2237  */
2238 static int
2239 proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
2240                 void *data)
2241 {
2242         adapter_t       *adapter;
2243         int     len;
2244         int     i;
2245
2246         i = 0;  /* avoid compilation warnings */
2247         len = 0;
2248         adapter = (adapter_t *)data;
2249
2250         len = sprintf(page, "Statistical Information for this controller\n");
2251         len += sprintf(page+len, "pend_cmds = %d\n",
2252                         atomic_read(&adapter->pend_cmds));
2253 #if MEGA_HAVE_STATS
2254         for(i = 0; i < adapter->numldrv; i++) {
2255                 len += sprintf(page+len, "Logical Drive %d:\n", i);
2256
2257                 len += sprintf(page+len,
2258                         "\tReads Issued = %lu, Writes Issued = %lu\n",
2259                         adapter->nreads[i], adapter->nwrites[i]);
2260
2261                 len += sprintf(page+len,
2262                         "\tSectors Read = %lu, Sectors Written = %lu\n",
2263                         adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2264
2265                 len += sprintf(page+len,
2266                         "\tRead errors = %lu, Write errors = %lu\n\n",
2267                         adapter->rd_errors[i], adapter->wr_errors[i]);
2268         }
2269 #else
2270         len += sprintf(page+len,
2271                         "IO and error counters not compiled in driver.\n");
2272 #endif
2273
2274         *eof = 1;
2275
2276         return len;
2277 }
2278
2279
2280 /**
2281  * proc_read_mbox()
2282  * @page - buffer to write the data in
2283  * @start - where the actual data has been written in page
2284  * @offset - same meaning as the read system call
2285  * @count - same meaning as the read system call
2286  * @eof - set if no more data needs to be returned
2287  * @data - pointer to our soft state
2288  *
2289  * Display mailbox information for the last command issued. This information
2290  * is good for debugging.
2291  */
2292 static int
2293 proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
2294                 void *data)
2295 {
2296
2297         adapter_t       *adapter = (adapter_t *)data;
2298         volatile mbox_t *mbox = adapter->mbox;
2299         int     len = 0;
2300
2301         len = sprintf(page, "Contents of Mail Box Structure\n");
2302         len += sprintf(page+len, "  Fw Command   = 0x%02x\n", 
2303                         mbox->m_out.cmd);
2304         len += sprintf(page+len, "  Cmd Sequence = 0x%02x\n", 
2305                         mbox->m_out.cmdid);
2306         len += sprintf(page+len, "  No of Sectors= %04d\n", 
2307                         mbox->m_out.numsectors);
2308         len += sprintf(page+len, "  LBA          = 0x%02x\n", 
2309                         mbox->m_out.lba);
2310         len += sprintf(page+len, "  DTA          = 0x%08x\n", 
2311                         mbox->m_out.xferaddr);
2312         len += sprintf(page+len, "  Logical Drive= 0x%02x\n", 
2313                         mbox->m_out.logdrv);
2314         len += sprintf(page+len, "  No of SG Elmt= 0x%02x\n",
2315                         mbox->m_out.numsgelements);
2316         len += sprintf(page+len, "  Busy         = %01x\n", 
2317                         mbox->m_in.busy);
2318         len += sprintf(page+len, "  Status       = 0x%02x\n", 
2319                         mbox->m_in.status);
2320
2321         *eof = 1;
2322
2323         return len;
2324 }
2325
2326
2327 /**
2328  * proc_rebuild_rate()
2329  * @page - buffer to write the data in
2330  * @start - where the actual data has been written in page
2331  * @offset - same meaning as the read system call
2332  * @count - same meaning as the read system call
2333  * @eof - set if no more data needs to be returned
2334  * @data - pointer to our soft state
2335  *
2336  * Display current rebuild rate
2337  */
2338 static int
2339 proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
2340                 void *data)
2341 {
2342         adapter_t       *adapter = (adapter_t *)data;
2343         dma_addr_t      dma_handle;
2344         caddr_t         inquiry;
2345         struct pci_dev  *pdev;
2346         int     len = 0;
2347
2348         if( make_local_pdev(adapter, &pdev) != 0 ) {
2349                 *eof = 1;
2350                 return len;
2351         }
2352
2353         if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2354                 free_local_pdev(pdev);
2355                 *eof = 1;
2356                 return len;
2357         }
2358
2359         if( mega_adapinq(adapter, dma_handle) != 0 ) {
2360
2361                 len = sprintf(page, "Adapter inquiry failed.\n");
2362
2363                 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2364
2365                 mega_free_inquiry(inquiry, dma_handle, pdev);
2366
2367                 free_local_pdev(pdev);
2368
2369                 *eof = 1;
2370
2371                 return len;
2372         }
2373
2374         if( adapter->flag & BOARD_40LD ) {
2375                 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2376                         ((mega_inquiry3 *)inquiry)->rebuild_rate);
2377         }
2378         else {
2379                 len = sprintf(page, "Rebuild Rate: [%d%%]\n",
2380                         ((mraid_ext_inquiry *)
2381                         inquiry)->raid_inq.adapter_info.rebuild_rate);
2382         }
2383
2384
2385         mega_free_inquiry(inquiry, dma_handle, pdev);
2386
2387         free_local_pdev(pdev);
2388
2389         *eof = 1;
2390
2391         return len;
2392 }
2393
2394
2395 /**
2396  * proc_battery()
2397  * @page - buffer to write the data in
2398  * @start - where the actual data has been written in page
2399  * @offset - same meaning as the read system call
2400  * @count - same meaning as the read system call
2401  * @eof - set if no more data needs to be returned
2402  * @data - pointer to our soft state
2403  *
2404  * Display information about the battery module on the controller.
2405  */
2406 static int
2407 proc_battery(char *page, char **start, off_t offset, int count, int *eof,
2408                 void *data)
2409 {
2410         adapter_t       *adapter = (adapter_t *)data;
2411         dma_addr_t      dma_handle;
2412         caddr_t         inquiry;
2413         struct pci_dev  *pdev;
2414         u8      battery_status = 0;
2415         char    str[256];
2416         int     len = 0;
2417
2418         if( make_local_pdev(adapter, &pdev) != 0 ) {
2419                 *eof = 1;
2420                 return len;
2421         }
2422
2423         if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2424                 free_local_pdev(pdev);
2425                 *eof = 1;
2426                 return len;
2427         }
2428
2429         if( mega_adapinq(adapter, dma_handle) != 0 ) {
2430
2431                 len = sprintf(page, "Adapter inquiry failed.\n");
2432
2433                 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2434
2435                 mega_free_inquiry(inquiry, dma_handle, pdev);
2436
2437                 free_local_pdev(pdev);
2438
2439                 *eof = 1;
2440
2441                 return len;
2442         }
2443
2444         if( adapter->flag & BOARD_40LD ) {
2445                 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2446         }
2447         else {
2448                 battery_status = ((mraid_ext_inquiry *)inquiry)->
2449                         raid_inq.adapter_info.battery_status;
2450         }
2451
2452         /*
2453          * Decode the battery status
2454          */
2455         sprintf(str, "Battery Status:[%d]", battery_status);
2456
2457         if(battery_status == MEGA_BATT_CHARGE_DONE)
2458                 strcat(str, " Charge Done");
2459
2460         if(battery_status & MEGA_BATT_MODULE_MISSING)
2461                 strcat(str, " Module Missing");
2462         
2463         if(battery_status & MEGA_BATT_LOW_VOLTAGE)
2464                 strcat(str, " Low Voltage");
2465         
2466         if(battery_status & MEGA_BATT_TEMP_HIGH)
2467                 strcat(str, " Temperature High");
2468         
2469         if(battery_status & MEGA_BATT_PACK_MISSING)
2470                 strcat(str, " Pack Missing");
2471         
2472         if(battery_status & MEGA_BATT_CHARGE_INPROG)
2473                 strcat(str, " Charge In-progress");
2474         
2475         if(battery_status & MEGA_BATT_CHARGE_FAIL)
2476                 strcat(str, " Charge Fail");
2477         
2478         if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
2479                 strcat(str, " Cycles Exceeded");
2480
2481         len = sprintf(page, "%s\n", str);
2482
2483
2484         mega_free_inquiry(inquiry, dma_handle, pdev);
2485
2486         free_local_pdev(pdev);
2487
2488         *eof = 1;
2489
2490         return len;
2491 }
2492
2493
2494 /**
2495  * proc_pdrv_ch0()
2496  * @page - buffer to write the data in
2497  * @start - where the actual data has been written in page
2498  * @offset - same meaning as the read system call
2499  * @count - same meaning as the read system call
2500  * @eof - set if no more data needs to be returned
2501  * @data - pointer to our soft state
2502  *
2503  * Display information about the physical drives on physical channel 0.
2504  */
2505 static int
2506 proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
2507                 void *data)
2508 {
2509         adapter_t *adapter = (adapter_t *)data;
2510
2511         *eof = 1;
2512
2513         return (proc_pdrv(adapter, page, 0));
2514 }
2515
2516
2517 /**
2518  * proc_pdrv_ch1()
2519  * @page - buffer to write the data in
2520  * @start - where the actual data has been written in page
2521  * @offset - same meaning as the read system call
2522  * @count - same meaning as the read system call
2523  * @eof - set if no more data needs to be returned
2524  * @data - pointer to our soft state
2525  *
2526  * Display information about the physical drives on physical channel 1.
2527  */
2528 static int
2529 proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
2530                 void *data)
2531 {
2532         adapter_t *adapter = (adapter_t *)data;
2533
2534         *eof = 1;
2535
2536         return (proc_pdrv(adapter, page, 1));
2537 }
2538
2539
2540 /**
2541  * proc_pdrv_ch2()
2542  * @page - buffer to write the data in
2543  * @start - where the actual data has been written in page
2544  * @offset - same meaning as the read system call
2545  * @count - same meaning as the read system call
2546  * @eof - set if no more data needs to be returned
2547  * @data - pointer to our soft state
2548  *
2549  * Display information about the physical drives on physical channel 2.
2550  */
2551 static int
2552 proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
2553                 void *data)
2554 {
2555         adapter_t *adapter = (adapter_t *)data;
2556
2557         *eof = 1;
2558
2559         return (proc_pdrv(adapter, page, 2));
2560 }
2561
2562
2563 /**
2564  * proc_pdrv_ch3()
2565  * @page - buffer to write the data in
2566  * @start - where the actual data has been written in page
2567  * @offset - same meaning as the read system call
2568  * @count - same meaning as the read system call
2569  * @eof - set if no more data needs to be returned
2570  * @data - pointer to our soft state
2571  *
2572  * Display information about the physical drives on physical channel 3.
2573  */
2574 static int
2575 proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
2576                 void *data)
2577 {
2578         adapter_t *adapter = (adapter_t *)data;
2579
2580         *eof = 1;
2581
2582         return (proc_pdrv(adapter, page, 3));
2583 }
2584
2585
2586 /**
2587  * proc_pdrv()
2588  * @page - buffer to write the data in
2589  * @adapter - pointer to our soft state
2590  *
2591  * Display information about the physical drives.
2592  */
2593 static int
2594 proc_pdrv(adapter_t *adapter, char *page, int channel)
2595 {
2596         dma_addr_t      dma_handle;
2597         char            *scsi_inq;
2598         dma_addr_t      scsi_inq_dma_handle;
2599         caddr_t         inquiry;
2600         struct pci_dev  *pdev;
2601         u8      *pdrv_state;
2602         u8      state;
2603         int     tgt;
2604         int     max_channels;
2605         int     len = 0;
2606         char    str[80];
2607         int     i;
2608
2609         if( make_local_pdev(adapter, &pdev) != 0 ) {
2610                 return len;
2611         }
2612
2613         if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2614                 goto free_pdev;
2615         }
2616
2617         if( mega_adapinq(adapter, dma_handle) != 0 ) {
2618                 len = sprintf(page, "Adapter inquiry failed.\n");
2619
2620                 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2621
2622                 goto free_inquiry;
2623         }
2624
2625
2626         scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
2627
2628         if( scsi_inq == NULL ) {
2629                 len = sprintf(page, "memory not available for scsi inq.\n");
2630
2631                 goto free_inquiry;
2632         }
2633
2634         if( adapter->flag & BOARD_40LD ) {
2635                 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2636         }
2637         else {
2638                 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2639                         raid_inq.pdrv_info.pdrv_state;
2640         }
2641
2642         max_channels = adapter->product_info.nchannels;
2643
2644         if( channel >= max_channels ) {
2645                 goto free_pci;
2646         }
2647
2648         for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2649
2650                 i = channel*16 + tgt;
2651
2652                 state = *(pdrv_state + i);
2653
2654                 switch( state & 0x0F ) {
2655
2656                 case PDRV_ONLINE:
2657                         sprintf(str,
2658                         "Channel:%2d Id:%2d State: Online",
2659                                 channel, tgt);
2660                         break;
2661
2662                 case PDRV_FAILED:
2663                         sprintf(str,
2664                         "Channel:%2d Id:%2d State: Failed",
2665                                 channel, tgt);
2666                         break;
2667
2668                 case PDRV_RBLD:
2669                         sprintf(str,
2670                         "Channel:%2d Id:%2d State: Rebuild",
2671                                 channel, tgt);
2672                         break;
2673
2674                 case PDRV_HOTSPARE:
2675                         sprintf(str,
2676                         "Channel:%2d Id:%2d State: Hot spare",
2677                                 channel, tgt);
2678                         break;
2679
2680                 default:
2681                         sprintf(str,
2682                         "Channel:%2d Id:%2d State: Un-configured",
2683                                 channel, tgt);
2684                         break;
2685
2686                 }
2687
2688                 /*
2689                  * This interface displays inquiries for disk drives
2690                  * only. Inquries for logical drives and non-disk
2691                  * devices are available through /proc/scsi/scsi
2692                  */
2693                 memset(scsi_inq, 0, 256);
2694                 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2695                                 scsi_inq_dma_handle) ||
2696                                 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2697                         continue;
2698                 }
2699
2700                 /*
2701                  * Check for overflow. We print less than 240
2702                  * characters for inquiry
2703                  */
2704                 if( (len + 240) >= PAGE_SIZE ) break;
2705
2706                 len += sprintf(page+len, "%s.\n", str);
2707
2708                 len += mega_print_inquiry(page+len, scsi_inq);
2709         }
2710
2711 free_pci:
2712         pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2713 free_inquiry:
2714         mega_free_inquiry(inquiry, dma_handle, pdev);
2715 free_pdev:
2716         free_local_pdev(pdev);
2717
2718         return len;
2719 }
2720
2721
2722 /*
2723  * Display scsi inquiry
2724  */
2725 static int
2726 mega_print_inquiry(char *page, char *scsi_inq)
2727 {
2728         int     len = 0;
2729         int     i;
2730
2731         len = sprintf(page, "  Vendor: ");
2732         for( i = 8; i < 16; i++ ) {
2733                 len += sprintf(page+len, "%c", scsi_inq[i]);
2734         }
2735
2736         len += sprintf(page+len, "  Model: ");
2737
2738         for( i = 16; i < 32; i++ ) {
2739                 len += sprintf(page+len, "%c", scsi_inq[i]);
2740         }
2741
2742         len += sprintf(page+len, "  Rev: ");
2743
2744         for( i = 32; i < 36; i++ ) {
2745                 len += sprintf(page+len, "%c", scsi_inq[i]);
2746         }
2747
2748         len += sprintf(page+len, "\n");
2749
2750         i = scsi_inq[0] & 0x1f;
2751
2752         len += sprintf(page+len, "  Type:   %s ", scsi_device_type(i));
2753
2754         len += sprintf(page+len,
2755         "                 ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
2756
2757         if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2758                 len += sprintf(page+len, " CCS\n");
2759         else
2760                 len += sprintf(page+len, "\n");
2761
2762         return len;
2763 }
2764
2765
2766 /**
2767  * proc_rdrv_10()
2768  * @page - buffer to write the data in
2769  * @start - where the actual data has been written in page
2770  * @offset - same meaning as the read system call
2771  * @count - same meaning as the read system call
2772  * @eof - set if no more data needs to be returned
2773  * @data - pointer to our soft state
2774  *
2775  * Display real time information about the logical drives 0 through 9.
2776  */
2777 static int
2778 proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
2779                 void *data)
2780 {
2781         adapter_t *adapter = (adapter_t *)data;
2782
2783         *eof = 1;
2784
2785         return (proc_rdrv(adapter, page, 0, 9));
2786 }
2787
2788
2789 /**
2790  * proc_rdrv_20()
2791  * @page - buffer to write the data in
2792  * @start - where the actual data has been written in page
2793  * @offset - same meaning as the read system call
2794  * @count - same meaning as the read system call
2795  * @eof - set if no more data needs to be returned
2796  * @data - pointer to our soft state
2797  *
2798  * Display real time information about the logical drives 0 through 9.
2799  */
2800 static int
2801 proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
2802                 void *data)
2803 {
2804         adapter_t *adapter = (adapter_t *)data;
2805
2806         *eof = 1;
2807
2808         return (proc_rdrv(adapter, page, 10, 19));
2809 }
2810
2811
2812 /**
2813  * proc_rdrv_30()
2814  * @page - buffer to write the data in
2815  * @start - where the actual data has been written in page
2816  * @offset - same meaning as the read system call
2817  * @count - same meaning as the read system call
2818  * @eof - set if no more data needs to be returned
2819  * @data - pointer to our soft state
2820  *
2821  * Display real time information about the logical drives 0 through 9.
2822  */
2823 static int
2824 proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
2825                 void *data)
2826 {
2827         adapter_t *adapter = (adapter_t *)data;
2828
2829         *eof = 1;
2830
2831         return (proc_rdrv(adapter, page, 20, 29));
2832 }
2833
2834
2835 /**
2836  * proc_rdrv_40()
2837  * @page - buffer to write the data in
2838  * @start - where the actual data has been written in page
2839  * @offset - same meaning as the read system call
2840  * @count - same meaning as the read system call
2841  * @eof - set if no more data needs to be returned
2842  * @data - pointer to our soft state
2843  *
2844  * Display real time information about the logical drives 0 through 9.
2845  */
2846 static int
2847 proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
2848                 void *data)
2849 {
2850         adapter_t *adapter = (adapter_t *)data;
2851
2852         *eof = 1;
2853
2854         return (proc_rdrv(adapter, page, 30, 39));
2855 }
2856
2857
2858 /**
2859  * proc_rdrv()
2860  * @page - buffer to write the data in
2861  * @adapter - pointer to our soft state
2862  * @start - starting logical drive to display
2863  * @end - ending logical drive to display
2864  *
2865  * We do not print the inquiry information since its already available through
2866  * /proc/scsi/scsi interface
2867  */
2868 static int
2869 proc_rdrv(adapter_t *adapter, char *page, int start, int end )
2870 {
2871         dma_addr_t      dma_handle;
2872         logdrv_param    *lparam;
2873         megacmd_t       mc;
2874         char            *disk_array;
2875         dma_addr_t      disk_array_dma_handle;
2876         caddr_t         inquiry;
2877         struct pci_dev  *pdev;
2878         u8      *rdrv_state;
2879         int     num_ldrv;
2880         u32     array_sz;
2881         int     len = 0;
2882         int     i;
2883
2884         if( make_local_pdev(adapter, &pdev) != 0 ) {
2885                 return len;
2886         }
2887
2888         if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
2889                 free_local_pdev(pdev);
2890                 return len;
2891         }
2892
2893         if( mega_adapinq(adapter, dma_handle) != 0 ) {
2894
2895                 len = sprintf(page, "Adapter inquiry failed.\n");
2896
2897                 printk(KERN_WARNING "megaraid: inquiry failed.\n");
2898
2899                 mega_free_inquiry(inquiry, dma_handle, pdev);
2900
2901                 free_local_pdev(pdev);
2902
2903                 return len;
2904         }
2905
2906         memset(&mc, 0, sizeof(megacmd_t));
2907
2908         if( adapter->flag & BOARD_40LD ) {
2909                 array_sz = sizeof(disk_array_40ld);
2910
2911                 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2912
2913                 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2914         }
2915         else {
2916                 array_sz = sizeof(disk_array_8ld);
2917
2918                 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2919                         raid_inq.logdrv_info.ldrv_state;
2920
2921                 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2922                         raid_inq.logdrv_info.num_ldrv;
2923         }
2924
2925         disk_array = pci_alloc_consistent(pdev, array_sz,
2926                         &disk_array_dma_handle);
2927
2928         if( disk_array == NULL ) {
2929                 len = sprintf(page, "memory not available.\n");
2930
2931                 mega_free_inquiry(inquiry, dma_handle, pdev);
2932
2933                 free_local_pdev(pdev);
2934
2935                 return len;
2936         }
2937
2938         mc.xferaddr = (u32)disk_array_dma_handle;
2939
2940         if( adapter->flag & BOARD_40LD ) {
2941                 mc.cmd = FC_NEW_CONFIG;
2942                 mc.opcode = OP_DCMD_READ_CONFIG;
2943
2944                 if( mega_internal_command(adapter, &mc, NULL) ) {
2945
2946                         len = sprintf(page, "40LD read config failed.\n");
2947
2948                         mega_free_inquiry(inquiry, dma_handle, pdev);
2949
2950                         pci_free_consistent(pdev, array_sz, disk_array,
2951                                         disk_array_dma_handle);
2952
2953                         free_local_pdev(pdev);
2954
2955                         return len;
2956                 }
2957
2958         }
2959         else {
2960                 mc.cmd = NEW_READ_CONFIG_8LD;
2961
2962                 if( mega_internal_command(adapter, &mc, NULL) ) {
2963
2964                         mc.cmd = READ_CONFIG_8LD;
2965
2966                         if( mega_internal_command(adapter, &mc,
2967                                                 NULL) ){
2968
2969                                 len = sprintf(page,
2970                                         "8LD read config failed.\n");
2971
2972                                 mega_free_inquiry(inquiry, dma_handle, pdev);
2973
2974                                 pci_free_consistent(pdev, array_sz,
2975                                                 disk_array,
2976                                                 disk_array_dma_handle);
2977
2978                                 free_local_pdev(pdev);
2979
2980                                 return len;
2981                         }
2982                 }
2983         }
2984
2985         for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2986
2987                 if( adapter->flag & BOARD_40LD ) {
2988                         lparam =
2989                         &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2990                 }
2991                 else {
2992                         lparam =
2993                         &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2994                 }
2995
2996                 /*
2997                  * Check for overflow. We print less than 240 characters for
2998                  * information about each logical drive.
2999                  */
3000                 if( (len + 240) >= PAGE_SIZE ) break;
3001
3002                 len += sprintf(page+len, "Logical drive:%2d:, ", i);
3003
3004                 switch( rdrv_state[i] & 0x0F ) {
3005                 case RDRV_OFFLINE:
3006                         len += sprintf(page+len, "state: offline");
3007                         break;
3008
3009                 case RDRV_DEGRADED:
3010                         len += sprintf(page+len, "state: degraded");
3011                         break;
3012
3013                 case RDRV_OPTIMAL:
3014                         len += sprintf(page+len, "state: optimal");
3015                         break;
3016
3017                 case RDRV_DELETED:
3018                         len += sprintf(page+len, "state: deleted");
3019                         break;
3020
3021                 default:
3022                         len += sprintf(page+len, "state: unknown");
3023                         break;
3024                 }
3025
3026                 /*
3027                  * Check if check consistency or initialization is going on
3028                  * for this logical drive.
3029                  */
3030                 if( (rdrv_state[i] & 0xF0) == 0x20 ) {
3031                         len += sprintf(page+len,
3032                                         ", check-consistency in progress");
3033                 }
3034                 else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
3035                         len += sprintf(page+len,
3036                                         ", initialization in progress");
3037                 }
3038                 
3039                 len += sprintf(page+len, "\n");
3040
3041                 len += sprintf(page+len, "Span depth:%3d, ",
3042                                 lparam->span_depth);
3043
3044                 len += sprintf(page+len, "RAID level:%3d, ",
3045                                 lparam->level);
3046
3047                 len += sprintf(page+len, "Stripe size:%3d, ",
3048                                 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
3049
3050                 len += sprintf(page+len, "Row size:%3d\n",
3051                                 lparam->row_size);
3052
3053
3054                 len += sprintf(page+len, "Read Policy: ");
3055
3056                 switch(lparam->read_ahead) {
3057
3058                 case NO_READ_AHEAD:
3059                         len += sprintf(page+len, "No read ahead, ");
3060                         break;
3061
3062                 case READ_AHEAD:
3063                         len += sprintf(page+len, "Read ahead, ");
3064                         break;
3065
3066                 case ADAP_READ_AHEAD:
3067                         len += sprintf(page+len, "Adaptive, ");
3068                         break;
3069
3070                 }
3071
3072                 len += sprintf(page+len, "Write Policy: ");
3073
3074                 switch(lparam->write_mode) {
3075
3076                 case WRMODE_WRITE_THRU:
3077                         len += sprintf(page+len, "Write thru, ");
3078                         break;
3079
3080                 case WRMODE_WRITE_BACK:
3081                         len += sprintf(page+len, "Write back, ");
3082                         break;
3083                 }
3084
3085                 len += sprintf(page+len, "Cache Policy: ");
3086
3087                 switch(lparam->direct_io) {
3088
3089                 case CACHED_IO:
3090                         len += sprintf(page+len, "Cached IO\n\n");
3091                         break;
3092
3093                 case DIRECT_IO:
3094                         len += sprintf(page+len, "Direct IO\n\n");
3095                         break;
3096                 }
3097         }
3098
3099         mega_free_inquiry(inquiry, dma_handle, pdev);
3100
3101         pci_free_consistent(pdev, array_sz, disk_array,
3102                         disk_array_dma_handle);
3103
3104         free_local_pdev(pdev);
3105
3106         return len;
3107 }
3108 #else
3109 static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
3110 {
3111 }
3112 #endif
3113
3114
3115 /**
3116  * megaraid_biosparam()
3117  *
3118  * Return the disk geometry for a particular disk
3119  */
3120 static int
3121 megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
3122                     sector_t capacity, int geom[])
3123 {
3124         adapter_t       *adapter;
3125         unsigned char   *bh;
3126         int     heads;
3127         int     sectors;
3128         int     cylinders;
3129         int     rval;
3130
3131         /* Get pointer to host config structure */
3132         adapter = (adapter_t *)sdev->host->hostdata;
3133
3134         if (IS_RAID_CH(adapter, sdev->channel)) {
3135                         /* Default heads (64) & sectors (32) */
3136                         heads = 64;
3137                         sectors = 32;
3138                         cylinders = (ulong)capacity / (heads * sectors);
3139
3140                         /*
3141                          * Handle extended translation size for logical drives
3142                          * > 1Gb
3143                          */
3144                         if ((ulong)capacity >= 0x200000) {
3145                                 heads = 255;
3146                                 sectors = 63;
3147                                 cylinders = (ulong)capacity / (heads * sectors);
3148                         }
3149
3150                         /* return result */
3151                         geom[0] = heads;
3152                         geom[1] = sectors;
3153                         geom[2] = cylinders;
3154         }
3155         else {
3156                 bh = scsi_bios_ptable(bdev);
3157
3158                 if( bh ) {
3159                         rval = scsi_partsize(bh, capacity,
3160                                             &geom[2], &geom[0], &geom[1]);
3161                         kfree(bh);
3162                         if( rval != -1 )
3163                                 return rval;
3164                 }
3165
3166                 printk(KERN_INFO
3167                 "megaraid: invalid partition on this disk on channel %d\n",
3168                                 sdev->channel);
3169
3170                 /* Default heads (64) & sectors (32) */
3171                 heads = 64;
3172                 sectors = 32;
3173                 cylinders = (ulong)capacity / (heads * sectors);
3174
3175                 /* Handle extended translation size for logical drives > 1Gb */
3176                 if ((ulong)capacity >= 0x200000) {
3177                         heads = 255;
3178                         sectors = 63;
3179                         cylinders = (ulong)capacity / (heads * sectors);
3180                 }
3181
3182                 /* return result */
3183                 geom[0] = heads;
3184                 geom[1] = sectors;
3185                 geom[2] = cylinders;
3186         }
3187
3188         return 0;
3189 }
3190
3191 /**
3192  * mega_init_scb()
3193  * @adapter - pointer to our soft state
3194  *
3195  * Allocate memory for the various pointers in the scb structures:
3196  * scatter-gather list pointer, passthru and extended passthru structure
3197  * pointers.
3198  */
3199 static int
3200 mega_init_scb(adapter_t *adapter)
3201 {
3202         scb_t   *scb;
3203         int     i;
3204
3205         for( i = 0; i < adapter->max_cmds; i++ ) {
3206
3207                 scb = &adapter->scb_list[i];
3208
3209                 scb->sgl64 = NULL;
3210                 scb->sgl = NULL;
3211                 scb->pthru = NULL;
3212                 scb->epthru = NULL;
3213         }
3214
3215         for( i = 0; i < adapter->max_cmds; i++ ) {
3216
3217                 scb = &adapter->scb_list[i];
3218
3219                 scb->idx = i;
3220
3221                 scb->sgl64 = pci_alloc_consistent(adapter->dev,
3222                                 sizeof(mega_sgl64) * adapter->sglen,
3223                                 &scb->sgl_dma_addr);
3224
3225                 scb->sgl = (mega_sglist *)scb->sgl64;
3226
3227                 if( !scb->sgl ) {
3228                         printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
3229                         mega_free_sgl(adapter);
3230                         return -1;
3231                 }
3232
3233                 scb->pthru = pci_alloc_consistent(adapter->dev,
3234                                 sizeof(mega_passthru),
3235                                 &scb->pthru_dma_addr);
3236
3237                 if( !scb->pthru ) {
3238                         printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
3239                         mega_free_sgl(adapter);
3240                         return -1;
3241                 }
3242
3243                 scb->epthru = pci_alloc_consistent(adapter->dev,
3244                                 sizeof(mega_ext_passthru),
3245                                 &scb->epthru_dma_addr);
3246
3247                 if( !scb->epthru ) {
3248                         printk(KERN_WARNING
3249                                 "Can't allocate extended passthru.\n");
3250                         mega_free_sgl(adapter);
3251                         return -1;
3252                 }
3253
3254
3255                 scb->dma_type = MEGA_DMA_TYPE_NONE;
3256
3257                 /*
3258                  * Link to free list
3259                  * lock not required since we are loading the driver, so no
3260                  * commands possible right now.
3261                  */
3262                 scb->state = SCB_FREE;
3263                 scb->cmd = NULL;
3264                 list_add(&scb->list, &adapter->free_list);
3265         }
3266
3267         return 0;
3268 }
3269
3270
3271 /**
3272  * megadev_open()
3273  * @inode - unused
3274  * @filep - unused
3275  *
3276  * Routines for the character/ioctl interface to the driver. Find out if this
3277  * is a valid open. 
3278  */
3279 static int
3280 megadev_open (struct inode *inode, struct file *filep)
3281 {
3282         cycle_kernel_lock();
3283         /*
3284          * Only allow superuser to access private ioctl interface
3285          */
3286         if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
3287
3288         return 0;
3289 }
3290
3291
3292 /**
3293  * megadev_ioctl()
3294  * @inode - Our device inode
3295  * @filep - unused
3296  * @cmd - ioctl command
3297  * @arg - user buffer
3298  *
3299  * ioctl entry point for our private ioctl interface. We move the data in from
3300  * the user space, prepare the command (if necessary, convert the old MIMD
3301  * ioctl to new ioctl command), and issue a synchronous command to the
3302  * controller.
3303  */
3304 static int
3305 megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
3306                 unsigned long arg)
3307 {
3308         adapter_t       *adapter;
3309         nitioctl_t      uioc;
3310         int             adapno;
3311         int             rval;
3312         mega_passthru   __user *upthru; /* user address for passthru */
3313         mega_passthru   *pthru;         /* copy user passthru here */
3314         dma_addr_t      pthru_dma_hndl;
3315         void            *data = NULL;   /* data to be transferred */
3316         dma_addr_t      data_dma_hndl;  /* dma handle for data xfer area */
3317         megacmd_t       mc;
3318         megastat_t      __user *ustats;
3319         int             num_ldrv;
3320         u32             uxferaddr = 0;
3321         struct pci_dev  *pdev;
3322
3323         ustats = NULL; /* avoid compilation warnings */
3324         num_ldrv = 0;
3325
3326         /*
3327          * Make sure only USCSICMD are issued through this interface.
3328          * MIMD application would still fire different command.
3329          */
3330         if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3331                 return -EINVAL;
3332         }
3333
3334         /*
3335          * Check and convert a possible MIMD command to NIT command.
3336          * mega_m_to_n() copies the data from the user space, so we do not
3337          * have to do it here.
3338          * NOTE: We will need some user address to copyout the data, therefore
3339          * the inteface layer will also provide us with the required user
3340          * addresses.
3341          */
3342         memset(&uioc, 0, sizeof(nitioctl_t));
3343         if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3344                 return rval;
3345
3346
3347         switch( uioc.opcode ) {
3348
3349         case GET_DRIVER_VER:
3350                 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3351                         return (-EFAULT);
3352
3353                 break;
3354
3355         case GET_N_ADAP:
3356                 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3357                         return (-EFAULT);
3358
3359                 /*
3360                  * Shucks. MIMD interface returns a positive value for number
3361                  * of adapters. TODO: Change it to return 0 when there is no
3362                  * applicatio using mimd interface.
3363                  */
3364                 return hba_count;
3365
3366         case GET_ADAP_INFO:
3367
3368                 /*
3369                  * Which adapter
3370                  */
3371                 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3372                         return (-ENODEV);
3373
3374                 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3375                                 sizeof(struct mcontroller)) )
3376                         return (-EFAULT);
3377                 break;
3378
3379 #if MEGA_HAVE_STATS
3380
3381         case GET_STATS:
3382                 /*
3383                  * Which adapter
3384                  */
3385                 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3386                         return (-ENODEV);
3387
3388                 adapter = hba_soft_state[adapno];
3389
3390                 ustats = uioc.uioc_uaddr;
3391
3392                 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3393                         return (-EFAULT);
3394
3395                 /*
3396                  * Check for the validity of the logical drive number
3397                  */
3398                 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3399
3400                 if( copy_to_user(ustats->nreads, adapter->nreads,
3401                                         num_ldrv*sizeof(u32)) )
3402                         return -EFAULT;
3403
3404                 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3405                                         num_ldrv*sizeof(u32)) )
3406                         return -EFAULT;
3407
3408                 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3409                                         num_ldrv*sizeof(u32)) )
3410                         return -EFAULT;
3411
3412                 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3413                                         num_ldrv*sizeof(u32)) )
3414                         return -EFAULT;
3415
3416                 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3417                                         num_ldrv*sizeof(u32)) )
3418                         return -EFAULT;
3419
3420                 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3421                                         num_ldrv*sizeof(u32)) )
3422                         return -EFAULT;
3423
3424                 return 0;
3425
3426 #endif
3427         case MBOX_CMD:
3428
3429                 /*
3430                  * Which adapter
3431                  */
3432                 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3433                         return (-ENODEV);
3434
3435                 adapter = hba_soft_state[adapno];
3436
3437                 /*
3438                  * Deletion of logical drive is a special case. The adapter
3439                  * should be quiescent before this command is issued.
3440                  */
3441                 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3442                                 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3443
3444                         /*
3445                          * Do we support this feature
3446                          */
3447                         if( !adapter->support_random_del ) {
3448                                 printk(KERN_WARNING "megaraid: logdrv ");
3449                                 printk("delete on non-supporting F/W.\n");
3450
3451                                 return (-EINVAL);
3452                         }
3453
3454                         rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3455
3456                         if( rval == 0 ) {
3457                                 memset(&mc, 0, sizeof(megacmd_t));
3458
3459                                 mc.status = rval;
3460
3461                                 rval = mega_n_to_m((void __user *)arg, &mc);
3462                         }
3463
3464                         return rval;
3465                 }
3466                 /*
3467                  * This interface only support the regular passthru commands.
3468                  * Reject extended passthru and 64-bit passthru
3469                  */
3470                 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3471                         uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3472
3473                         printk(KERN_WARNING "megaraid: rejected passthru.\n");
3474
3475                         return (-EINVAL);
3476                 }
3477
3478                 /*
3479                  * For all internal commands, the buffer must be allocated in
3480                  * <4GB address range
3481                  */
3482                 if( make_local_pdev(adapter, &pdev) != 0 )
3483                         return -EIO;
3484
3485                 /* Is it a passthru command or a DCMD */
3486                 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3487                         /* Passthru commands */
3488
3489                         pthru = pci_alloc_consistent(pdev,
3490                                         sizeof(mega_passthru),
3491                                         &pthru_dma_hndl);
3492
3493                         if( pthru == NULL ) {
3494                                 free_local_pdev(pdev);
3495                                 return (-ENOMEM);
3496                         }
3497
3498                         /*
3499                          * The user passthru structure
3500                          */
3501                         upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
3502
3503                         /*
3504                          * Copy in the user passthru here.
3505                          */
3506                         if( copy_from_user(pthru, upthru,
3507                                                 sizeof(mega_passthru)) ) {
3508
3509                                 pci_free_consistent(pdev,
3510                                                 sizeof(mega_passthru), pthru,
3511                                                 pthru_dma_hndl);
3512
3513                                 free_local_pdev(pdev);
3514
3515                                 return (-EFAULT);
3516                         }
3517
3518                         /*
3519                          * Is there a data transfer
3520                          */
3521                         if( pthru->dataxferlen ) {
3522                                 data = pci_alloc_consistent(pdev,
3523                                                 pthru->dataxferlen,
3524                                                 &data_dma_hndl);
3525
3526                                 if( data == NULL ) {
3527                                         pci_free_consistent(pdev,
3528                                                         sizeof(mega_passthru),
3529                                                         pthru,
3530                                                         pthru_dma_hndl);
3531
3532                                         free_local_pdev(pdev);
3533
3534                                         return (-ENOMEM);
3535                                 }
3536
3537                                 /*
3538                                  * Save the user address and point the kernel
3539                                  * address at just allocated memory
3540                                  */
3541                                 uxferaddr = pthru->dataxferaddr;
3542                                 pthru->dataxferaddr = data_dma_hndl;
3543                         }
3544
3545
3546                         /*
3547                          * Is data coming down-stream
3548                          */
3549                         if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3550                                 /*
3551                                  * Get the user data
3552                                  */
3553                                 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3554                                                         pthru->dataxferlen) ) {
3555                                         rval = (-EFAULT);
3556                                         goto freemem_and_return;
3557                                 }
3558                         }
3559
3560                         memset(&mc, 0, sizeof(megacmd_t));
3561
3562                         mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3563                         mc.xferaddr = (u32)pthru_dma_hndl;
3564
3565                         /*
3566                          * Issue the command
3567                          */
3568                         mega_internal_command(adapter, &mc, pthru);
3569
3570                         rval = mega_n_to_m((void __user *)arg, &mc);
3571
3572                         if( rval ) goto freemem_and_return;
3573
3574
3575                         /*
3576                          * Is data going up-stream
3577                          */
3578                         if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
3579                                 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3580                                                         pthru->dataxferlen) ) {
3581                                         rval = (-EFAULT);
3582                                 }
3583                         }
3584
3585                         /*
3586                          * Send the request sense data also, irrespective of
3587                          * whether the user has asked for it or not.
3588                          */
3589                         if (copy_to_user(upthru->reqsensearea,
3590                                         pthru->reqsensearea, 14))
3591                                 rval = -EFAULT;
3592
3593 freemem_and_return:
3594                         if( pthru->dataxferlen ) {
3595                                 pci_free_consistent(pdev,
3596                                                 pthru->dataxferlen, data,
3597                                                 data_dma_hndl);
3598                         }
3599
3600                         pci_free_consistent(pdev, sizeof(mega_passthru),
3601                                         pthru, pthru_dma_hndl);
3602
3603                         free_local_pdev(pdev);
3604
3605                         return rval;
3606                 }
3607                 else {
3608                         /* DCMD commands */
3609
3610                         /*
3611                          * Is there a data transfer
3612                          */
3613                         if( uioc.xferlen ) {
3614                                 data = pci_alloc_consistent(pdev,
3615                                                 uioc.xferlen, &data_dma_hndl);
3616
3617                                 if( data == NULL ) {
3618                                         free_local_pdev(pdev);
3619                                         return (-ENOMEM);
3620                                 }
3621
3622                                 uxferaddr = MBOX(uioc)->xferaddr;
3623                         }
3624
3625                         /*
3626                          * Is data coming down-stream
3627                          */
3628                         if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3629                                 /*
3630                                  * Get the user data
3631                                  */
3632                                 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
3633                                                         uioc.xferlen) ) {
3634
3635                                         pci_free_consistent(pdev,
3636                                                         uioc.xferlen,
3637                                                         data, data_dma_hndl);
3638
3639                                         free_local_pdev(pdev);
3640
3641                                         return (-EFAULT);
3642                                 }
3643                         }
3644
3645                         memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3646
3647                         mc.xferaddr = (u32)data_dma_hndl;
3648
3649                         /*
3650                          * Issue the command
3651                          */
3652                         mega_internal_command(adapter, &mc, NULL);
3653
3654                         rval = mega_n_to_m((void __user *)arg, &mc);
3655
3656                         if( rval ) {
3657                                 if( uioc.xferlen ) {
3658                                         pci_free_consistent(pdev,
3659                                                         uioc.xferlen, data,
3660                                                         data_dma_hndl);
3661                                 }
3662
3663                                 free_local_pdev(pdev);
3664
3665                                 return rval;
3666                         }
3667
3668                         /*
3669                          * Is data going up-stream
3670                          */
3671                         if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
3672                                 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
3673                                                         uioc.xferlen) ) {
3674
3675                                         rval = (-EFAULT);
3676                                 }
3677                         }
3678
3679                         if( uioc.xferlen ) {
3680                                 pci_free_consistent(pdev,
3681                                                 uioc.xferlen, data,
3682                                                 data_dma_hndl);
3683                         }
3684
3685                         free_local_pdev(pdev);
3686
3687                         return rval;
3688                 }
3689
3690         default:
3691                 return (-EINVAL);
3692         }
3693
3694         return 0;
3695 }
3696
3697 /**
3698  * mega_m_to_n()
3699  * @arg - user address
3700  * @uioc - new ioctl structure
3701  *
3702  * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3703  * structure
3704  *
3705  * Converts the older mimd ioctl structure to newer NIT structure
3706  */
3707 static int
3708 mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3709 {
3710         struct uioctl_t uioc_mimd;
3711         char    signature[8] = {0};
3712         u8      opcode;
3713         u8      subopcode;
3714
3715
3716         /*
3717          * check is the application conforms to NIT. We do not have to do much
3718          * in that case.
3719          * We exploit the fact that the signature is stored in the very
3720          * begining of the structure.
3721          */
3722
3723         if( copy_from_user(signature, arg, 7) )
3724                 return (-EFAULT);
3725
3726         if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3727
3728                 /*
3729                  * NOTE NOTE: The nit ioctl is still under flux because of
3730                  * change of mailbox definition, in HPE. No applications yet
3731                  * use this interface and let's not have applications use this
3732                  * interface till the new specifitions are in place.
3733                  */
3734                 return -EINVAL;
3735 #if 0
3736                 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3737                         return (-EFAULT);
3738                 return 0;
3739 #endif
3740         }
3741
3742         /*
3743          * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3744          *
3745          * Get the user ioctl structure
3746          */
3747         if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3748                 return (-EFAULT);
3749
3750
3751         /*
3752          * Get the opcode and subopcode for the commands
3753          */
3754         opcode = uioc_mimd.ui.fcs.opcode;
3755         subopcode = uioc_mimd.ui.fcs.subopcode;
3756
3757         switch (opcode) {
3758         case 0x82:
3759
3760                 switch (subopcode) {
3761
3762                 case MEGAIOC_QDRVRVER:  /* Query driver version */
3763                         uioc->opcode = GET_DRIVER_VER;
3764                         uioc->uioc_uaddr = uioc_mimd.data;
3765                         break;
3766
3767                 case MEGAIOC_QNADAP:    /* Get # of adapters */
3768                         uioc->opcode = GET_N_ADAP;
3769                         uioc->uioc_uaddr = uioc_mimd.data;
3770                         break;
3771
3772                 case MEGAIOC_QADAPINFO: /* Get adapter information */
3773                         uioc->opcode = GET_ADAP_INFO;
3774                         uioc->adapno = uioc_mimd.ui.fcs.adapno;
3775                         uioc->uioc_uaddr = uioc_mimd.data;
3776                         break;
3777
3778                 default:
3779                         return(-EINVAL);
3780                 }
3781
3782                 break;
3783
3784
3785         case 0x81:
3786
3787                 uioc->opcode = MBOX_CMD;
3788                 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3789
3790                 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3791
3792                 uioc->xferlen = uioc_mimd.ui.fcs.length;
3793
3794                 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3795                 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3796
3797                 break;
3798
3799         case 0x80:
3800
3801                 uioc->opcode = MBOX_CMD;
3802                 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3803
3804                 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3805
3806                 /*
3807                  * Choose the xferlen bigger of input and output data
3808                  */
3809                 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3810                         uioc_mimd.outlen : uioc_mimd.inlen;
3811
3812                 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3813                 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3814
3815                 break;
3816
3817         default:
3818                 return (-EINVAL);
3819
3820         }
3821
3822         return 0;
3823 }
3824
3825 /*
3826  * mega_n_to_m()
3827  * @arg - user address
3828  * @mc - mailbox command
3829  *
3830  * Updates the status information to the application, depending on application
3831  * conforms to older mimd ioctl interface or newer NIT ioctl interface
3832  */
3833 static int
3834 mega_n_to_m(void __user *arg, megacmd_t *mc)
3835 {
3836         nitioctl_t      __user *uiocp;
3837         megacmd_t       __user *umc;
3838         mega_passthru   __user *upthru;
3839         struct uioctl_t __user *uioc_mimd;
3840         char    signature[8] = {0};
3841
3842         /*
3843          * check is the application conforms to NIT.
3844          */
3845         if( copy_from_user(signature, arg, 7) )
3846                 return -EFAULT;
3847
3848         if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3849
3850                 uiocp = arg;
3851
3852                 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3853                         return (-EFAULT);
3854
3855                 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3856
3857                         umc = MBOX_P(uiocp);
3858
3859                         if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3860                                 return -EFAULT;
3861
3862                         if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3863                                 return (-EFAULT);
3864                 }
3865         }
3866         else {
3867                 uioc_mimd = arg;
3868
3869                 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3870                         return (-EFAULT);
3871
3872                 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3873
3874                         umc = (megacmd_t __user *)uioc_mimd->mbox;
3875
3876                         if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3877                                 return (-EFAULT);
3878
3879                         if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3880                                 return (-EFAULT);
3881                 }
3882         }
3883
3884         return 0;
3885 }
3886
3887
3888 /*
3889  * MEGARAID 'FW' commands.
3890  */
3891
3892 /**
3893  * mega_is_bios_enabled()
3894  * @adapter - pointer to our soft state
3895  *
3896  * issue command to find out if the BIOS is enabled for this controller
3897  */
3898 static int
3899 mega_is_bios_enabled(adapter_t *adapter)
3900 {
3901         unsigned char   raw_mbox[sizeof(struct mbox_out)];
3902         mbox_t  *mbox;
3903         int     ret;
3904
3905         mbox = (mbox_t *)raw_mbox;
3906
3907         memset(&mbox->m_out, 0, sizeof(raw_mbox));
3908
3909         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3910
3911         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3912
3913         raw_mbox[0] = IS_BIOS_ENABLED;
3914         raw_mbox[2] = GET_BIOS;
3915
3916
3917         ret = issue_scb_block(adapter, raw_mbox);
3918
3919         return *(char *)adapter->mega_buffer;
3920 }
3921
3922
3923 /**
3924  * mega_enum_raid_scsi()
3925  * @adapter - pointer to our soft state
3926  *
3927  * Find out what channels are RAID/SCSI. This information is used to
3928  * differentiate the virtual channels and physical channels and to support
3929  * ROMB feature and non-disk devices.
3930  */
3931 static void
3932 mega_enum_raid_scsi(adapter_t *adapter)
3933 {
3934         unsigned char raw_mbox[sizeof(struct mbox_out)];
3935         mbox_t *mbox;
3936         int i;
3937
3938         mbox = (mbox_t *)raw_mbox;
3939
3940         memset(&mbox->m_out, 0, sizeof(raw_mbox));
3941
3942         /*
3943          * issue command to find out what channels are raid/scsi
3944          */
3945         raw_mbox[0] = CHNL_CLASS;
3946         raw_mbox[2] = GET_CHNL_CLASS;
3947
3948         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3949
3950         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3951
3952         /*
3953          * Non-ROMB firmware fail this command, so all channels
3954          * must be shown RAID
3955          */
3956         adapter->mega_ch_class = 0xFF;
3957
3958         if(!issue_scb_block(adapter, raw_mbox)) {
3959                 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3960
3961         }
3962
3963         for( i = 0; i < adapter->product_info.nchannels; i++ ) { 
3964                 if( (adapter->mega_ch_class >> i) & 0x01 ) {
3965                         printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
3966                                         i);
3967                 }
3968                 else {
3969                         printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
3970                                         i);
3971                 }
3972         }
3973
3974         return;
3975 }
3976
3977
3978 /**
3979  * mega_get_boot_drv()
3980  * @adapter - pointer to our soft state
3981  *
3982  * Find out which device is the boot device. Note, any logical drive or any
3983  * phyical device (e.g., a CDROM) can be designated as a boot device.
3984  */
3985 static void
3986 mega_get_boot_drv(adapter_t *adapter)
3987 {
3988         struct private_bios_data        *prv_bios_data;
3989         unsigned char   raw_mbox[sizeof(struct mbox_out)];
3990         mbox_t  *mbox;
3991         u16     cksum = 0;
3992         u8      *cksum_p;
3993         u8      boot_pdrv;
3994         int     i;
3995
3996         mbox = (mbox_t *)raw_mbox;
3997
3998         memset(&mbox->m_out, 0, sizeof(raw_mbox));
3999
4000         raw_mbox[0] = BIOS_PVT_DATA;
4001         raw_mbox[2] = GET_BIOS_PVT_DATA;
4002
4003         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4004
4005         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4006
4007         adapter->boot_ldrv_enabled = 0;
4008         adapter->boot_ldrv = 0;
4009
4010         adapter->boot_pdrv_enabled = 0;
4011         adapter->boot_pdrv_ch = 0;
4012         adapter->boot_pdrv_tgt = 0;
4013
4014         if(issue_scb_block(adapter, raw_mbox) == 0) {
4015                 prv_bios_data =
4016                         (struct private_bios_data *)adapter->mega_buffer;
4017
4018                 cksum = 0;
4019                 cksum_p = (char *)prv_bios_data;
4020                 for (i = 0; i < 14; i++ ) {
4021                         cksum += (u16)(*cksum_p++);
4022                 }
4023
4024                 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
4025
4026                         /*
4027                          * If MSB is set, a physical drive is set as boot
4028                          * device
4029                          */
4030                         if( prv_bios_data->boot_drv & 0x80 ) {
4031                                 adapter->boot_pdrv_enabled = 1;
4032                                 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
4033                                 adapter->boot_pdrv_ch = boot_pdrv / 16;
4034                                 adapter->boot_pdrv_tgt = boot_pdrv % 16;
4035                         }
4036                         else {
4037                                 adapter->boot_ldrv_enabled = 1;
4038                                 adapter->boot_ldrv = prv_bios_data->boot_drv;
4039                         }
4040                 }
4041         }
4042
4043 }
4044
4045 /**
4046  * mega_support_random_del()
4047  * @adapter - pointer to our soft state
4048  *
4049  * Find out if this controller supports random deletion and addition of
4050  * logical drives
4051  */
4052 static int
4053 mega_support_random_del(adapter_t *adapter)
4054 {
4055         unsigned char raw_mbox[sizeof(struct mbox_out)];
4056         mbox_t *mbox;
4057         int rval;
4058
4059         mbox = (mbox_t *)raw_mbox;
4060
4061         memset(&mbox->m_out, 0, sizeof(raw_mbox));
4062
4063         /*
4064          * issue command
4065          */
4066         raw_mbox[0] = FC_DEL_LOGDRV;
4067         raw_mbox[2] = OP_SUP_DEL_LOGDRV;
4068
4069         rval = issue_scb_block(adapter, raw_mbox);
4070
4071         return !rval;
4072 }
4073
4074
4075 /**
4076  * mega_support_ext_cdb()
4077  * @adapter - pointer to our soft state
4078  *
4079  * Find out if this firmware support cdblen > 10
4080  */
4081 static int
4082 mega_support_ext_cdb(adapter_t *adapter)
4083 {
4084         unsigned char raw_mbox[sizeof(struct mbox_out)];
4085         mbox_t *mbox;
4086         int rval;
4087
4088         mbox = (mbox_t *)raw_mbox;
4089
4090         memset(&mbox->m_out, 0, sizeof(raw_mbox));
4091         /*
4092          * issue command to find out if controller supports extended CDBs.
4093          */
4094         raw_mbox[0] = 0xA4;
4095         raw_mbox[2] = 0x16;
4096
4097         rval = issue_scb_block(adapter, raw_mbox);
4098
4099         return !rval;
4100 }
4101
4102
4103 /**
4104  * mega_del_logdrv()
4105  * @adapter - pointer to our soft state
4106  * @logdrv - logical drive to be deleted
4107  *
4108  * Delete the specified logical drive. It is the responsibility of the user
4109  * app to let the OS know about this operation.
4110  */
4111 static int
4112 mega_del_logdrv(adapter_t *adapter, int logdrv)
4113 {
4114         unsigned long flags;
4115         scb_t *scb;
4116         int rval;
4117
4118         /*
4119          * Stop sending commands to the controller, queue them internally.
4120          * When deletion is complete, ISR will flush the queue.
4121          */
4122         atomic_set(&adapter->quiescent, 1);
4123
4124         /*
4125          * Wait till all the issued commands are complete and there are no
4126          * commands in the pending queue
4127          */
4128         while (atomic_read(&adapter->pend_cmds) > 0 ||
4129                !list_empty(&adapter->pending_list))
4130                 msleep(1000);   /* sleep for 1s */
4131
4132         rval = mega_do_del_logdrv(adapter, logdrv);
4133
4134         spin_lock_irqsave(&adapter->lock, flags);
4135
4136         /*
4137          * If delete operation was successful, add 0x80 to the logical drive
4138          * ids for commands in the pending queue.
4139          */
4140         if (adapter->read_ldidmap) {
4141                 struct list_head *pos;
4142                 list_for_each(pos, &adapter->pending_list) {
4143                         scb = list_entry(pos, scb_t, list);
4144                         if (scb->pthru->logdrv < 0x80 )
4145                                 scb->pthru->logdrv += 0x80;
4146                 }
4147         }
4148
4149         atomic_set(&adapter->quiescent, 0);
4150
4151         mega_runpendq(adapter);
4152
4153         spin_unlock_irqrestore(&adapter->lock, flags);
4154
4155         return rval;
4156 }
4157
4158
4159 static int
4160 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
4161 {
4162         megacmd_t       mc;
4163         int     rval;
4164
4165         memset( &mc, 0, sizeof(megacmd_t));
4166
4167         mc.cmd = FC_DEL_LOGDRV;
4168         mc.opcode = OP_DEL_LOGDRV;
4169         mc.subopcode = logdrv;
4170
4171         rval = mega_internal_command(adapter, &mc, NULL);
4172
4173         /* log this event */
4174         if(rval) {
4175                 printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
4176                 return rval;
4177         }
4178
4179         /*
4180          * After deleting first logical drive, the logical drives must be
4181          * addressed by adding 0x80 to the logical drive id.
4182          */
4183         adapter->read_ldidmap = 1;
4184
4185         return rval;
4186 }
4187
4188
4189 /**
4190  * mega_get_max_sgl()
4191  * @adapter - pointer to our soft state
4192  *
4193  * Find out the maximum number of scatter-gather elements supported by this
4194  * version of the firmware
4195  */
4196 static void
4197 mega_get_max_sgl(adapter_t *adapter)
4198 {
4199         unsigned char   raw_mbox[sizeof(struct mbox_out)];
4200         mbox_t  *mbox;
4201
4202         mbox = (mbox_t *)raw_mbox;
4203
4204         memset(mbox, 0, sizeof(raw_mbox));
4205
4206         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4207
4208         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4209
4210         raw_mbox[0] = MAIN_MISC_OPCODE;
4211         raw_mbox[2] = GET_MAX_SG_SUPPORT;
4212
4213
4214         if( issue_scb_block(adapter, raw_mbox) ) {
4215                 /*
4216                  * f/w does not support this command. Choose the default value
4217                  */
4218                 adapter->sglen = MIN_SGLIST;
4219         }
4220         else {
4221                 adapter->sglen = *((char *)adapter->mega_buffer);
4222                 
4223                 /*
4224                  * Make sure this is not more than the resources we are
4225                  * planning to allocate
4226                  */
4227                 if ( adapter->sglen > MAX_SGLIST )
4228                         adapter->sglen = MAX_SGLIST;
4229         }
4230
4231         return;
4232 }
4233
4234
4235 /**
4236  * mega_support_cluster()
4237  * @adapter - pointer to our soft state
4238  *
4239  * Find out if this firmware support cluster calls.
4240  */
4241 static int
4242 mega_support_cluster(adapter_t *adapter)
4243 {
4244         unsigned char   raw_mbox[sizeof(struct mbox_out)];
4245         mbox_t  *mbox;
4246
4247         mbox = (mbox_t *)raw_mbox;
4248
4249         memset(mbox, 0, sizeof(raw_mbox));
4250
4251         memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4252
4253         mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
4254
4255         /*
4256          * Try to get the initiator id. This command will succeed iff the
4257          * clustering is available on this HBA.
4258          */
4259         raw_mbox[0] = MEGA_GET_TARGET_ID;
4260
4261         if( issue_scb_block(adapter, raw_mbox) == 0 ) {
4262
4263                 /*
4264                  * Cluster support available. Get the initiator target id.
4265                  * Tell our id to mid-layer too.
4266                  */
4267                 adapter->this_id = *(u32 *)adapter->mega_buffer;
4268                 adapter->host->this_id = adapter->this_id;
4269
4270                 return 1;
4271         }
4272
4273         return 0;
4274 }
4275
4276 #ifdef CONFIG_PROC_FS
4277 /**
4278  * mega_adapinq()
4279  * @adapter - pointer to our soft state
4280  * @dma_handle - DMA address of the buffer
4281  *
4282  * Issue internal comamnds while interrupts are available.
4283  * We only issue direct mailbox commands from within the driver. ioctl()
4284  * interface using these routines can issue passthru commands.
4285  */
4286 static int
4287 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
4288 {
4289         megacmd_t       mc;
4290
4291         memset(&mc, 0, sizeof(megacmd_t));
4292
4293         if( adapter->flag & BOARD_40LD ) {
4294                 mc.cmd = FC_NEW_CONFIG;
4295                 mc.opcode = NC_SUBOP_ENQUIRY3;
4296                 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
4297         }
4298         else {
4299                 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
4300         }
4301
4302         mc.xferaddr = (u32)dma_handle;
4303
4304         if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
4305                 return -1;
4306         }
4307
4308         return 0;
4309 }
4310
4311
4312 /** mega_internal_dev_inquiry()
4313  * @adapter - pointer to our soft state
4314  * @ch - channel for this device
4315  * @tgt - ID of this device
4316  * @buf_dma_handle - DMA address of the buffer
4317  *
4318  * Issue the scsi inquiry for the specified device.
4319  */
4320 static int
4321 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4322                 dma_addr_t buf_dma_handle)
4323 {
4324         mega_passthru   *pthru;
4325         dma_addr_t      pthru_dma_handle;
4326         megacmd_t       mc;
4327         int             rval;
4328         struct pci_dev  *pdev;
4329
4330
4331         /*
4332          * For all internal commands, the buffer must be allocated in <4GB
4333          * address range
4334          */
4335         if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4336
4337         pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4338                         &pthru_dma_handle);
4339
4340         if( pthru == NULL ) {
4341                 free_local_pdev(pdev);
4342                 return -1;
4343         }
4344
4345         pthru->timeout = 2;
4346         pthru->ars = 1;
4347         pthru->reqsenselen = 14;
4348         pthru->islogical = 0;
4349
4350         pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4351
4352         pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4353
4354         pthru->cdblen = 6;
4355
4356         pthru->cdb[0] = INQUIRY;
4357         pthru->cdb[1] = 0;
4358         pthru->cdb[2] = 0;
4359         pthru->cdb[3] = 0;
4360         pthru->cdb[4] = 255;
4361         pthru->cdb[5] = 0;
4362
4363
4364         pthru->dataxferaddr = (u32)buf_dma_handle;
4365         pthru->dataxferlen = 256;
4366
4367         memset(&mc, 0, sizeof(megacmd_t));
4368
4369         mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4370         mc.xferaddr = (u32)pthru_dma_handle;
4371
4372         rval = mega_internal_command(adapter, &mc, pthru);
4373
4374         pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4375                         pthru_dma_handle);
4376
4377         free_local_pdev(pdev);
4378
4379         return rval;
4380 }
4381 #endif
4382
4383 /**
4384  * mega_internal_command()
4385  * @adapter - pointer to our soft state
4386  * @mc - the mailbox command
4387  * @pthru - Passthru structure for DCDB commands
4388  *
4389  * Issue the internal commands in interrupt mode.
4390  * The last argument is the address of the passthru structure if the command
4391  * to be fired is a passthru command
4392  *
4393  * lockscope specifies whether the caller has already acquired the lock. Of
4394  * course, the caller must know which lock we are talking about.
4395  *
4396  * Note: parameter 'pthru' is null for non-passthru commands.
4397  */
4398 static int
4399 mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
4400 {
4401         Scsi_Cmnd       *scmd;
4402         struct  scsi_device *sdev;
4403         scb_t   *scb;
4404         int     rval;
4405
4406         scmd = scsi_allocate_command(GFP_KERNEL);
4407         if (!scmd)
4408                 return -ENOMEM;
4409
4410         /*
4411          * The internal commands share one command id and hence are
4412          * serialized. This is so because we want to reserve maximum number of
4413          * available command ids for the I/O commands.
4414          */
4415         mutex_lock(&adapter->int_mtx);
4416
4417         scb = &adapter->int_scb;
4418         memset(scb, 0, sizeof(scb_t));
4419
4420         sdev = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
4421         scmd->device = sdev;
4422
4423         memset(adapter->int_cdb, 0, sizeof(adapter->int_cdb));
4424         scmd->cmnd = adapter->int_cdb;
4425         scmd->device->host = adapter->host;
4426         scmd->host_scribble = (void *)scb;
4427         scmd->cmnd[0] = MEGA_INTERNAL_CMD;
4428
4429         scb->state |= SCB_ACTIVE;
4430         scb->cmd = scmd;
4431
4432         memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4433
4434         /*
4435          * Is it a passthru command
4436          */
4437         if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4438
4439                 scb->pthru = pthru;
4440         }
4441
4442         scb->idx = CMDID_INT_CMDS;
4443
4444         megaraid_queue(scmd, mega_internal_done);
4445
4446         wait_for_completion(&adapter->int_waitq);
4447
4448         rval = scmd->result;
4449         mc->status = scmd->result;
4450         kfree(sdev);
4451
4452         /*
4453          * Print a debug message for all failed commands. Applications can use
4454          * this information.
4455          */
4456         if( scmd->result && trace_level ) {
4457                 printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
4458                         mc->cmd, mc->opcode, mc->subopcode, scmd->result);
4459         }
4460
4461         mutex_unlock(&adapter->int_mtx);
4462
4463         scsi_free_command(GFP_KERNEL, scmd);
4464
4465         return rval;
4466 }
4467
4468
4469 /**
4470  * mega_internal_done()
4471  * @scmd - internal scsi command
4472  *
4473  * Callback routine for internal commands.
4474  */
4475 static void
4476 mega_internal_done(Scsi_Cmnd *scmd)
4477 {
4478         adapter_t       *adapter;
4479
4480         adapter = (adapter_t *)scmd->device->host->hostdata;
4481
4482         complete(&adapter->int_waitq);
4483
4484 }
4485
4486
4487 static struct scsi_host_template megaraid_template = {
4488         .module                         = THIS_MODULE,
4489         .name                           = "MegaRAID",
4490         .proc_name                      = "megaraid_legacy",
4491         .info                           = megaraid_info,
4492         .queuecommand                   = megaraid_queue,       
4493         .bios_param                     = megaraid_biosparam,
4494         .max_sectors                    = MAX_SECTORS_PER_IO,
4495         .can_queue                      = MAX_COMMANDS,
4496         .this_id                        = DEFAULT_INITIATOR_ID,
4497         .sg_tablesize                   = MAX_SGLIST,
4498         .cmd_per_lun                    = DEF_CMD_PER_LUN,
4499         .use_clustering                 = ENABLE_CLUSTERING,
4500         .eh_abort_handler               = megaraid_abort,
4501         .eh_device_reset_handler        = megaraid_reset,
4502         .eh_bus_reset_handler           = megaraid_reset,
4503         .eh_host_reset_handler          = megaraid_reset,
4504 };
4505
4506 static int __devinit
4507 megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4508 {
4509         struct Scsi_Host *host;
4510         adapter_t *adapter;
4511         unsigned long mega_baseport, tbase, flag = 0;
4512         u16 subsysid, subsysvid;
4513         u8 pci_bus, pci_dev_func;
4514         int irq, i, j;
4515         int error = -ENODEV;
4516
4517         if (pci_enable_device(pdev))
4518                 goto out;
4519         pci_set_master(pdev);
4520
4521         pci_bus = pdev->bus->number;
4522         pci_dev_func = pdev->devfn;
4523
4524         /*
4525          * The megaraid3 stuff reports the ID of the Intel part which is not
4526          * remotely specific to the megaraid
4527          */
4528         if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4529                 u16 magic;
4530                 /*
4531                  * Don't fall over the Compaq management cards using the same
4532                  * PCI identifier
4533                  */
4534                 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4535                     pdev->subsystem_device == 0xC000)
4536                         return -ENODEV;
4537                 /* Now check the magic signature byte */
4538                 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4539                 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4540                         return -ENODEV;
4541                 /* Ok it is probably a megaraid */
4542         }
4543
4544         /*
4545          * For these vendor and device ids, signature offsets are not
4546          * valid and 64 bit is implicit
4547          */
4548         if (id->driver_data & BOARD_64BIT)
4549                 flag |= BOARD_64BIT;
4550         else {
4551                 u32 magic64;
4552
4553                 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4554                 if (magic64 == HBA_SIGNATURE_64BIT)
4555                         flag |= BOARD_64BIT;
4556         }
4557
4558         subsysvid = pdev->subsystem_vendor;
4559         subsysid = pdev->subsystem_device;
4560
4561         printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
4562                 id->vendor, id->device, pci_bus);
4563
4564         printk("slot %d:func %d\n",
4565                 PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
4566
4567         /* Read the base port and IRQ from PCI */
4568         mega_baseport = pci_resource_start(pdev, 0);
4569         irq = pdev->irq;
4570
4571         tbase = mega_baseport;
4572         if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4573                 flag |= BOARD_MEMMAP;
4574
4575                 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
4576                         printk(KERN_WARNING "megaraid: mem region busy!\n");
4577                         goto out_disable_device;
4578                 }
4579
4580                 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4581                 if (!mega_baseport) {
4582                         printk(KERN_WARNING
4583                                "megaraid: could not map hba memory\n");
4584                         goto out_release_region;
4585                 }
4586         } else {
4587                 flag |= BOARD_IOMAP;
4588                 mega_baseport += 0x10;
4589
4590                 if (!request_region(mega_baseport, 16, "megaraid"))
4591                         goto out_disable_device;
4592         }
4593
4594         /* Initialize SCSI Host structure */
4595         host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4596         if (!host)
4597                 goto out_iounmap;
4598
4599         adapter = (adapter_t *)host->hostdata;
4600         memset(adapter, 0, sizeof(adapter_t));
4601
4602         printk(KERN_NOTICE
4603                 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4604                 host->host_no, mega_baseport, irq);
4605
4606         adapter->base = mega_baseport;
4607         if (flag & BOARD_MEMMAP)
4608                 adapter->mmio_base = (void __iomem *) mega_baseport;
4609
4610         INIT_LIST_HEAD(&adapter->free_list);
4611         INIT_LIST_HEAD(&adapter->pending_list);
4612         INIT_LIST_HEAD(&adapter->completed_list);
4613
4614         adapter->flag = flag;
4615         spin_lock_init(&adapter->lock);
4616
4617         host->cmd_per_lun = max_cmd_per_lun;
4618         host->max_sectors = max_sectors_per_io;
4619
4620         adapter->dev = pdev;
4621         adapter->host = host;
4622
4623         adapter->host->irq = irq;
4624
4625         if (flag & BOARD_MEMMAP)
4626                 adapter->host->base = tbase;
4627         else {
4628                 adapter->host->io_port = tbase;
4629                 adapter->host->n_io_port = 16;
4630         }
4631
4632         adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4633
4634         /*
4635          * Allocate buffer to issue internal commands.
4636          */
4637         adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4638                 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4639         if (!adapter->mega_buffer) {
4640                 printk(KERN_WARNING "megaraid: out of RAM.\n");
4641                 goto out_host_put;
4642         }
4643
4644         adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4645         if (!adapter->scb_list) {
4646                 printk(KERN_WARNING "megaraid: out of RAM.\n");
4647                 goto out_free_cmd_buffer;
4648         }
4649
4650         if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4651                                 megaraid_isr_memmapped : megaraid_isr_iomapped,
4652                                         IRQF_SHARED, "megaraid", adapter)) {
4653                 printk(KERN_WARNING
4654                         "megaraid: Couldn't register IRQ %d!\n", irq);
4655                 goto out_free_scb_list;
4656         }
4657
4658         if (mega_setup_mailbox(adapter))
4659                 goto out_free_irq;
4660
4661         if (mega_query_adapter(adapter))
4662                 goto out_free_mbox;
4663
4664         /*
4665          * Have checks for some buggy f/w
4666          */
4667         if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4668                 /*
4669                  * Which firmware
4670                  */
4671                 if (!strcmp(adapter->fw_version, "3.00") ||
4672                                 !strcmp(adapter->fw_version, "3.01")) {
4673
4674                         printk( KERN_WARNING
4675                                 "megaraid: Your  card is a Dell PERC "
4676                                 "2/SC RAID controller with  "
4677                                 "firmware\nmegaraid: 3.00 or 3.01.  "
4678                                 "This driver is known to have "
4679                                 "corruption issues\nmegaraid: with "
4680                                 "those firmware versions on this "
4681                                 "specific card.  In order\nmegaraid: "
4682                                 "to protect your data, please upgrade "
4683                                 "your firmware to version\nmegaraid: "
4684                                 "3.10 or later, available from the "
4685                                 "Dell Technical Support web\n"
4686                                 "megaraid: site at\nhttp://support."
4687                                 "dell.com/us/en/filelib/download/"
4688                                 "index.asp?fileid=2940\n"
4689                         );
4690                 }
4691         }
4692
4693         /*
4694          * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4695          * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4696          * support, since this firmware cannot handle 64 bit
4697          * addressing
4698          */
4699         if ((subsysvid == HP_SUBSYS_VID) &&
4700             ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4701                 /*
4702                  * which firmware
4703                  */
4704                 if (!strcmp(adapter->fw_version, "H01.07") ||
4705                     !strcmp(adapter->fw_version, "H01.08") ||
4706                     !strcmp(adapter->fw_version, "H01.09") ) {
4707                         printk(KERN_WARNING
4708                                 "megaraid: Firmware H.01.07, "
4709                                 "H.01.08, and H.01.09 on 1M/2M "
4710                                 "controllers\n"
4711                                 "megaraid: do not support 64 bit "
4712                                 "addressing.\nmegaraid: DISABLING "
4713                                 "64 bit support.\n");
4714                         adapter->flag &= ~BOARD_64BIT;
4715                 }
4716         }
4717
4718         if (mega_is_bios_enabled(adapter))
4719                 mega_hbas[hba_count].is_bios_enabled = 1;
4720         mega_hbas[hba_count].hostdata_addr = adapter;
4721
4722         /*
4723          * Find out which channel is raid and which is scsi. This is
4724          * for ROMB support.
4725          */
4726         mega_enum_raid_scsi(adapter);
4727
4728         /*
4729          * Find out if a logical drive is set as the boot drive. If
4730          * there is one, will make that as the first logical drive.
4731          * ROMB: Do we have to boot from a physical drive. Then all
4732          * the physical drives would appear before the logical disks.
4733          * Else, all the physical drives would be exported to the mid
4734          * layer after logical drives.
4735          */
4736         mega_get_boot_drv(adapter);
4737
4738         if (adapter->boot_pdrv_enabled) {
4739                 j = adapter->product_info.nchannels;
4740                 for( i = 0; i < j; i++ )
4741                         adapter->logdrv_chan[i] = 0;
4742                 for( i = j; i < NVIRT_CHAN + j; i++ )
4743                         adapter->logdrv_chan[i] = 1;
4744         } else {
4745                 for (i = 0; i < NVIRT_CHAN; i++)
4746                         adapter->logdrv_chan[i] = 1;
4747                 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4748                         adapter->logdrv_chan[i] = 0;
4749                 adapter->mega_ch_class <<= NVIRT_CHAN;
4750         }
4751
4752         /*
4753          * Do we support random deletion and addition of logical
4754          * drives
4755          */
4756         adapter->read_ldidmap = 0;      /* set it after first logdrv
4757                                                    delete cmd */
4758         adapter->support_random_del = mega_support_random_del(adapter);
4759
4760         /* Initialize SCBs */
4761         if (mega_init_scb(adapter))
4762                 goto out_free_mbox;
4763
4764         /*
4765          * Reset the pending commands counter
4766          */
4767         atomic_set(&adapter->pend_cmds, 0);
4768
4769         /*
4770          * Reset the adapter quiescent flag
4771          */
4772         atomic_set(&adapter->quiescent, 0);
4773
4774         hba_soft_state[hba_count] = adapter;
4775
4776         /*
4777          * Fill in the structure which needs to be passed back to the
4778          * application when it does an ioctl() for controller related
4779          * information.
4780          */
4781         i = hba_count;
4782
4783         mcontroller[i].base = mega_baseport;
4784         mcontroller[i].irq = irq;
4785         mcontroller[i].numldrv = adapter->numldrv;
4786         mcontroller[i].pcibus = pci_bus;
4787         mcontroller[i].pcidev = id->device;
4788         mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4789         mcontroller[i].pciid = -1;
4790         mcontroller[i].pcivendor = id->vendor;
4791         mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4792         mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4793
4794
4795         /* Set the Mode of addressing to 64 bit if we can */
4796         if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
4797                 pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4798                 adapter->has_64bit_addr = 1;
4799         } else  {
4800                 pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4801                 adapter->has_64bit_addr = 0;
4802         }
4803                 
4804         mutex_init(&adapter->int_mtx);
4805         init_completion(&adapter->int_waitq);
4806
4807         adapter->this_id = DEFAULT_INITIATOR_ID;
4808         adapter->host->this_id = DEFAULT_INITIATOR_ID;
4809
4810 #if MEGA_HAVE_CLUSTERING
4811         /*
4812          * Is cluster support enabled on this controller
4813          * Note: In a cluster the HBAs ( the initiators ) will have
4814          * different target IDs and we cannot assume it to be 7. Call
4815          * to mega_support_cluster() will get the target ids also if
4816          * the cluster support is available
4817          */
4818         adapter->has_cluster = mega_support_cluster(adapter);
4819         if (adapter->has_cluster) {
4820                 printk(KERN_NOTICE
4821                         "megaraid: Cluster driver, initiator id:%d\n",
4822                         adapter->this_id);
4823         }
4824 #endif
4825
4826         pci_set_drvdata(pdev, host);
4827
4828         mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4829
4830         error = scsi_add_host(host, &pdev->dev);
4831         if (error)
4832                 goto out_free_mbox;
4833
4834         scsi_scan_host(host);
4835         hba_count++;
4836         return 0;
4837
4838  out_free_mbox:
4839         pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4840                         adapter->una_mbox64, adapter->una_mbox64_dma);
4841  out_free_irq:
4842         free_irq(adapter->host->irq, adapter);
4843  out_free_scb_list:
4844         kfree(adapter->scb_list);
4845  out_free_cmd_buffer:
4846         pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4847                         adapter->mega_buffer, adapter->buf_dma_handle);
4848  out_host_put:
4849         scsi_host_put(host);
4850  out_iounmap:
4851         if (flag & BOARD_MEMMAP)
4852                 iounmap((void *)mega_baseport);
4853  out_release_region:
4854         if (flag & BOARD_MEMMAP)
4855                 release_mem_region(tbase, 128);
4856         else
4857                 release_region(mega_baseport, 16);
4858  out_disable_device:
4859         pci_disable_device(pdev);
4860  out:
4861         return error;
4862 }
4863
4864 static void
4865 __megaraid_shutdown(adapter_t *adapter)
4866 {
4867         u_char  raw_mbox[sizeof(struct mbox_out)];
4868         mbox_t  *mbox = (mbox_t *)raw_mbox;
4869         int     i;
4870
4871         /* Flush adapter cache */
4872         memset(&mbox->m_out, 0, sizeof(raw_mbox));
4873         raw_mbox[0] = FLUSH_ADAPTER;
4874
4875         free_irq(adapter->host->irq, adapter);
4876
4877         /* Issue a blocking (interrupts disabled) command to the card */
4878         issue_scb_block(adapter, raw_mbox);
4879
4880         /* Flush disks cache */
4881         memset(&mbox->m_out, 0, sizeof(raw_mbox));
4882         raw_mbox[0] = FLUSH_SYSTEM;
4883
4884         /* Issue a blocking (interrupts disabled) command to the card */
4885         issue_scb_block(adapter, raw_mbox);
4886         
4887         if (atomic_read(&adapter->pend_cmds) > 0)
4888                 printk(KERN_WARNING "megaraid: pending commands!!\n");
4889
4890         /*
4891          * Have a delibrate delay to make sure all the caches are
4892          * actually flushed.
4893          */
4894         for (i = 0; i <= 10; i++)
4895                 mdelay(1000);
4896 }
4897
4898 static void __devexit
4899 megaraid_remove_one(struct pci_dev *pdev)
4900 {
4901         struct Scsi_Host *host = pci_get_drvdata(pdev);
4902         adapter_t *adapter = (adapter_t *)host->hostdata;
4903
4904         scsi_remove_host(host);
4905
4906         __megaraid_shutdown(adapter);
4907
4908         /* Free our resources */
4909         if (adapter->flag & BOARD_MEMMAP) {
4910                 iounmap((void *)adapter->base);
4911                 release_mem_region(adapter->host->base, 128);
4912         } else
4913                 release_region(adapter->base, 16);
4914
4915         mega_free_sgl(adapter);
4916
4917 #ifdef CONFIG_PROC_FS
4918         if (adapter->controller_proc_dir_entry) {
4919                 remove_proc_entry("stat", adapter->controller_proc_dir_entry);
4920                 remove_proc_entry("config",
4921                                 adapter->controller_proc_dir_entry);
4922                 remove_proc_entry("mailbox",
4923                                 adapter->controller_proc_dir_entry);
4924 #if MEGA_HAVE_ENH_PROC
4925                 remove_proc_entry("rebuild-rate",
4926                                 adapter->controller_proc_dir_entry);
4927                 remove_proc_entry("battery-status",
4928                                 adapter->controller_proc_dir_entry);
4929
4930                 remove_proc_entry("diskdrives-ch0",
4931                                 adapter->controller_proc_dir_entry);
4932                 remove_proc_entry("diskdrives-ch1",
4933                                 adapter->controller_proc_dir_entry);
4934                 remove_proc_entry("diskdrives-ch2",
4935                                 adapter->controller_proc_dir_entry);
4936                 remove_proc_entry("diskdrives-ch3",
4937                                 adapter->controller_proc_dir_entry);
4938
4939                 remove_proc_entry("raiddrives-0-9",
4940                                 adapter->controller_proc_dir_entry);
4941                 remove_proc_entry("raiddrives-10-19",
4942                                 adapter->controller_proc_dir_entry);
4943                 remove_proc_entry("raiddrives-20-29",
4944                                 adapter->controller_proc_dir_entry);
4945                 remove_proc_entry("raiddrives-30-39",
4946                                 adapter->controller_proc_dir_entry);
4947 #endif
4948                 {
4949                         char    buf[12] = { 0 };
4950                         sprintf(buf, "hba%d", adapter->host->host_no);
4951                         remove_proc_entry(buf, mega_proc_dir_entry);
4952                 }
4953         }
4954 #endif
4955
4956         pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4957                         adapter->mega_buffer, adapter->buf_dma_handle);
4958         kfree(adapter->scb_list);
4959         pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4960                         adapter->una_mbox64, adapter->una_mbox64_dma);
4961
4962         scsi_host_put(host);
4963         pci_disable_device(pdev);
4964
4965         hba_count--;
4966 }
4967
4968 static void
4969 megaraid_shutdown(struct pci_dev *pdev)
4970 {
4971         struct Scsi_Host *host = pci_get_drvdata(pdev);
4972         adapter_t *adapter = (adapter_t *)host->hostdata;
4973
4974         __megaraid_shutdown(adapter);
4975 }
4976
4977 static struct pci_device_id megaraid_pci_tbl[] = {
4978         {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4979                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4980         {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4981                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4982         {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4983                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4984         {0,}
4985 };
4986 MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4987
4988 static struct pci_driver megaraid_pci_driver = {
4989         .name           = "megaraid_legacy",
4990         .id_table       = megaraid_pci_tbl,
4991         .probe          = megaraid_probe_one,
4992         .remove         = __devexit_p(megaraid_remove_one),
4993         .shutdown       = megaraid_shutdown,
4994 };
4995
4996 static int __init megaraid_init(void)
4997 {
4998         int error;
4999
5000         if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
5001                 max_cmd_per_lun = MAX_CMD_PER_LUN;
5002         if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
5003                 max_mbox_busy_wait = MBOX_BUSY_WAIT;
5004
5005 #ifdef CONFIG_PROC_FS
5006         mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
5007         if (!mega_proc_dir_entry) {
5008                 printk(KERN_WARNING
5009                                 "megaraid: failed to create megaraid root\n");
5010         }
5011 #endif
5012         error = pci_register_driver(&megaraid_pci_driver);
5013         if (error) {
5014 #ifdef CONFIG_PROC_FS
5015                 remove_proc_entry("megaraid", NULL);
5016 #endif
5017                 return error;
5018         }
5019
5020         /*
5021          * Register the driver as a character device, for applications
5022          * to access it for ioctls.
5023          * First argument (major) to register_chrdev implies a dynamic
5024          * major number allocation.
5025          */
5026         major = register_chrdev(0, "megadev_legacy", &megadev_fops);
5027         if (!major) {
5028                 printk(KERN_WARNING
5029                                 "megaraid: failed to register char device\n");
5030         }
5031
5032         return 0;
5033 }
5034
5035 static void __exit megaraid_exit(void)
5036 {
5037         /*
5038          * Unregister the character device interface to the driver.
5039          */
5040         unregister_chrdev(major, "megadev_legacy");
5041
5042         pci_unregister_driver(&megaraid_pci_driver);
5043
5044 #ifdef CONFIG_PROC_FS
5045         remove_proc_entry("megaraid", NULL);
5046 #endif
5047 }
5048
5049 module_init(megaraid_init);
5050 module_exit(megaraid_exit);
5051
5052 /* vi: set ts=8 sw=8 tw=78: */