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