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