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