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