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