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