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