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