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