Linux Audio

Check our new training course

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