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