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