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