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