Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

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