mptscsih.c 192 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11
/*
 *  linux/drivers/message/fusion/mptscsih.c
 *      High performance SCSI / Fibre Channel SCSI Host device driver.
 *      For use with PCI chip/adapter(s):
 *          LSIFC9xx/LSI409xx Fibre Channel
 *      running LSI Logic Fusion MPT (Message Passing Technology) firmware.
 *
 *  Credits:
 *      This driver would not exist if not for Alan Cox's development
 *      of the linux i2o driver.
 *
Pam Delaney's avatar
Pam Delaney committed
12 13 14 15 16 17
 *      A special thanks to Pamela Delaney (LSI Logic) for tons of work
 *      and countless enhancements while adding support for the 1030
 *      chip family.  Pam has been instrumental in the development of
 *      of the 2.xx.xx series fusion drivers, and her contributions are
 *      far too numerous to hope to list in one place.
 *
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20 21 22 23
 *      A huge debt of gratitude is owed to David S. Miller (DaveM)
 *      for fixing much of the stupid and broken stuff in the early
 *      driver while porting to sparc64 platform.  THANK YOU!
 *
 *      (see mptbase.c)
 *
24
 *  Copyright (c) 1999-2004 LSI Logic Corporation
Linus Torvalds's avatar
Linus Torvalds committed
25
 *  Original author: Steven J. Ralston
Pam Delaney's avatar
Pam Delaney committed
26
 *  (mailto:sjralston1@netscape.net)
27
 *  (mailto:mpt_linux_developer@lsil.com)
Linus Torvalds's avatar
Linus Torvalds committed
28
 *
29
 *  $Id: mptscsih.c,v 1.104 2002/12/03 21:26:34 pdelaney Exp $
Linus Torvalds's avatar
Linus Torvalds committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    NO WARRANTY
    THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
    CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
    LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
    solely responsible for determining the appropriateness of using and
    distributing the Program and assumes all risks associated with its
    exercise of rights under this Agreement, including but not limited to
    the risks and costs of program errors, damage to or loss of data,
    programs or equipment, and unavailability or interruption of operations.

    DISCLAIMER OF LIABILITY
    NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
    USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
    HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/kdev_t.h>
#include <linux/blkdev.h>
Pam Delaney's avatar
Pam Delaney committed
74 75 76
#include <linux/delay.h>	/* for mdelay */
#include <linux/interrupt.h>	/* needed for in_interrupt() proto */
#include <linux/reboot.h>	/* notifier code */
Linus Torvalds's avatar
Linus Torvalds committed
77
#include "../../scsi/scsi.h"
James Bottomley's avatar
James Bottomley committed
78 79
#include <scsi/scsi_host.h>

Linus Torvalds's avatar
Linus Torvalds committed
80 81 82 83 84 85 86 87 88 89 90 91

#include "mptbase.h"
#include "mptscsih.h"
#include "isense.h"

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
#define my_NAME		"Fusion MPT SCSI Host driver"
#define my_VERSION	MPT_LINUX_VERSION_COMMON
#define MYNAM		"mptscsih"

MODULE_AUTHOR(MODULEAUTHOR);
MODULE_DESCRIPTION(my_NAME);
Linus Torvalds's avatar
Linus Torvalds committed
92 93
MODULE_LICENSE("GPL");

Pam Delaney's avatar
Pam Delaney committed
94 95 96 97
/* Set string for command line args from insmod */
#ifdef MODULE
char *mptscsih = 0;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
98 99 100 101

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

typedef struct _BIG_SENSE_BUF {
Pam Delaney's avatar
Pam Delaney committed
102
	u8		data[MPT_SENSE_BUFFER_ALLOC];
Linus Torvalds's avatar
Linus Torvalds committed
103 104
} BIG_SENSE_BUF;

Pam Delaney's avatar
Pam Delaney committed
105 106 107 108 109
#define MPT_SCANDV_GOOD			(0x00000000) /* must be 0 */
#define MPT_SCANDV_DID_RESET		(0x00000001)
#define MPT_SCANDV_SENSE		(0x00000002)
#define MPT_SCANDV_SOME_ERROR		(0x00000004)
#define MPT_SCANDV_SELECTION_TIMEOUT	(0x00000008)
110
#define MPT_SCANDV_ISSUE_SENSE		(0x00000010)
Pam Delaney's avatar
Pam Delaney committed
111 112 113 114 115 116 117

#define MPT_SCANDV_MAX_RETRIES		(10)

#define MPT_ICFLAG_BUF_CAP	0x01	/* ReadBuffer Read Capacity format */
#define MPT_ICFLAG_ECHO		0x02	/* ReadBuffer Echo buffer format */
#define MPT_ICFLAG_PHYS_DISK	0x04	/* Any SCSI IO but do Phys Disk Format */
#define MPT_ICFLAG_TAGGED_CMD	0x08	/* Do tagged IO */
118
#define MPT_ICFLAG_DID_RESET	0x20	/* Bus Reset occurred with this command */
Pam Delaney's avatar
Pam Delaney committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#define MPT_ICFLAG_RESERVED	0x40	/* Reserved has been issued */

typedef struct _internal_cmd {
	char		*data;		/* data pointer */
	dma_addr_t	data_dma;	/* data dma address */
	int		size;		/* transfer size */
	u8		cmd;		/* SCSI Op Code */
	u8		bus;		/* bus number */
	u8		id;		/* SCSI ID (virtual) */
	u8		lun;
	u8		flags;		/* Bit Field - See above */
	u8		physDiskNum;	/* Phys disk number, -1 else */
	u8		rsvd2;
	u8		rsvd;
} INTERNAL_CMD;

typedef struct _negoparms {
	u8 width;
	u8 offset;
	u8 factor;
	u8 flags;
} NEGOPARMS;

typedef struct _dv_parameters {
	NEGOPARMS	 max;
	NEGOPARMS	 now;
	u8		 cmd;
	u8		 id;
	u16		 pad1;
} DVPARAMETERS;

Linus Torvalds's avatar
Linus Torvalds committed
150 151 152 153 154 155 156

/*
 *  Other private/forward protos...
 */
static int	mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
static void	mptscsih_report_queue_full(Scsi_Cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq);
static int	mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
Pam Delaney's avatar
Pam Delaney committed
157 158 159 160 161

static int	mptscsih_AddSGE(MPT_SCSI_HOST *hd, Scsi_Cmnd *SCpnt,
				 SCSIIORequest_t *pReq, int req_idx);
static void	mptscsih_freeChainBuffers(MPT_SCSI_HOST *hd, int req_idx);
static int	mptscsih_initChainBuffers (MPT_SCSI_HOST *hd, int init);
Linus Torvalds's avatar
Linus Torvalds committed
162
static void	copy_sense_data(Scsi_Cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply);
Pam Delaney's avatar
Pam Delaney committed
163 164 165 166 167
static int	mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd);
static u32	SCPNT_TO_LOOKUP_IDX(Scsi_Cmnd *sc);
static MPT_FRAME_HDR *mptscsih_search_pendingQ(MPT_SCSI_HOST *hd, int scpnt_idx);
static void	post_pendingQ_commands(MPT_SCSI_HOST *hd);

168 169
static int	mptscsih_TMHandler(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout, int sleepFlag);
static int	mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout, int sleepFlag);
Linus Torvalds's avatar
Linus Torvalds committed
170

Linus Torvalds's avatar
Linus Torvalds committed
171
static int	mptscsih_ioc_reset(MPT_ADAPTER *ioc, int post_reset);
Linus Torvalds's avatar
Linus Torvalds committed
172 173
static int	mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply);

174 175
static void	mptscsih_initTarget(MPT_SCSI_HOST *hd, int bus_id, int target_id, u8 lun, char *data, int dlen);
void		mptscsih_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtDevice *target, char byte56);
176
static void	mptscsih_set_dvflags(MPT_SCSI_HOST *hd, SCSIIORequest_t *pReq);
Pam Delaney's avatar
Pam Delaney committed
177 178 179
static void	mptscsih_setDevicePage1Flags (u8 width, u8 factor, u8 offset, int *requestedPtr, int *configurationPtr, u8 flags);
static void	mptscsih_no_negotiate(MPT_SCSI_HOST *hd, int target_id);
static int	mptscsih_writeSDP1(MPT_SCSI_HOST *hd, int portnum, int target, int flags);
180
static int	mptscsih_writeIOCPage4(MPT_SCSI_HOST *hd, int target_id, int bus);
Pam Delaney's avatar
Pam Delaney committed
181 182 183
static int	mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
static void	mptscsih_timer_expired(unsigned long data);
static void	mptscsih_taskmgmt_timeout(unsigned long data);
184
static void	mptscsih_schedule_reset(void *hd);
Pam Delaney's avatar
Pam Delaney committed
185 186 187
static int	mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *iocmd);
static int	mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, int portnum);

188 189
static struct mpt_work_struct   mptscsih_rstTask;

James Bottomley's avatar
James Bottomley committed
190
#ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
Pam Delaney's avatar
Pam Delaney committed
191 192 193
static int	mptscsih_do_raid(MPT_SCSI_HOST *hd, u8 action, INTERNAL_CMD *io);
static void	mptscsih_domainValidation(void *hd);
static int	mptscsih_is_phys_disk(MPT_ADAPTER *ioc, int id);
194
static void	mptscsih_qas_check(MPT_SCSI_HOST *hd, int id);
195
static int	mptscsih_doDv(MPT_SCSI_HOST *hd, int channel, int target);
Pam Delaney's avatar
Pam Delaney committed
196 197 198
static void	mptscsih_dv_parms(MPT_SCSI_HOST *hd, DVPARAMETERS *dv,void *pPage);
static void	mptscsih_fillbuf(char *buffer, int size, int index, int width);
#endif
199
#ifdef MODULE
Pam Delaney's avatar
Pam Delaney committed
200
static int	mptscsih_setup(char *str);
201
#endif
James Bottomley's avatar
James Bottomley committed
202
/* module entry point */
203 204
static int  __init   mptscsih_init  (void);
static void __exit   mptscsih_exit  (void);
James Bottomley's avatar
James Bottomley committed
205

206 207
static int  mptscsih_probe (struct pci_dev *, const struct pci_device_id *);
static void mptscsih_remove(struct pci_dev *);
James Bottomley's avatar
James Bottomley committed
208 209 210 211 212 213
static void mptscsih_shutdown(struct device *);
#ifdef CONFIG_PM
static int mptscsih_suspend(struct pci_dev *pdev, u32 state);
static int mptscsih_resume(struct pci_dev *pdev);
#endif

Pam Delaney's avatar
Pam Delaney committed
214 215 216 217

/*
 *	Private data...
 */
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220 221 222

static int	mpt_scsi_hosts = 0;

static int	ScsiDoneCtx = -1;
static int	ScsiTaskCtx = -1;
Pam Delaney's avatar
Pam Delaney committed
223
static int	ScsiScanDvCtx = -1; /* Used only for bus scan and dv */
Linus Torvalds's avatar
Linus Torvalds committed
224

Pam Delaney's avatar
Pam Delaney committed
225
#define SNS_LEN(scp)	sizeof((scp)->sense_buffer)
Linus Torvalds's avatar
Linus Torvalds committed
226

James Bottomley's avatar
James Bottomley committed
227
#ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
Pam Delaney's avatar
Pam Delaney committed
228 229 230 231 232 233
/*
 * Domain Validation task structure
 */
static spinlock_t dvtaskQ_lock = SPIN_LOCK_UNLOCKED;
static int dvtaskQ_active = 0;
static int dvtaskQ_release = 0;
234
static struct mpt_work_struct	mptscsih_dvTask;
Linus Torvalds's avatar
Linus Torvalds committed
235 236
#endif

Pam Delaney's avatar
Pam Delaney committed
237 238 239 240 241 242 243 244 245 246 247
/*
 * Wait Queue setup
 */
static DECLARE_WAIT_QUEUE_HEAD (scandv_waitq);
static int scandv_wait_done = 1;

/* Driver default setup
 */
static struct mptscsih_driver_setup
	driver_setup = MPTSCSIH_DRIVER_SETUP;

248 249 250 251
#ifdef MPTSCSIH_DBG_TIMEOUT
static Scsi_Cmnd *foo_to[8];
#endif

James Bottomley's avatar
James Bottomley committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/* see mptscsih.h */

static struct scsi_host_template driver_template = {
	.proc_name			= "mptscsih",
	.proc_info			= x_scsi_proc_info,
	.name				= "MPT SCSI Host",
	.info				= x_scsi_info,
	.queuecommand			= x_scsi_queuecommand,
	.slave_alloc			= x_scsi_slave_alloc,
	.slave_configure		= x_scsi_slave_configure,
	.slave_destroy			= x_scsi_slave_destroy,
	.eh_abort_handler		= x_scsi_abort,
	.eh_device_reset_handler	= x_scsi_dev_reset,
	.eh_bus_reset_handler		= x_scsi_bus_reset,
	.eh_host_reset_handler		= x_scsi_host_reset,
	.bios_param			= x_scsi_bios_param,
	.can_queue			= MPT_SCSI_CAN_QUEUE,
	.this_id			= -1,
	.sg_tablesize			= MPT_SCSI_SG_DEPTH,
	.max_sectors			= MPT_SCSI_MAX_SECTORS,
	.cmd_per_lun			= MPT_SCSI_CMD_PER_LUN,
	.use_clustering			= ENABLE_CLUSTERING,
};

Linus Torvalds's avatar
Linus Torvalds committed
278 279
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
280 281 282 283 284 285 286 287
 *  Private inline routines...
 */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* 19991030 -sralston
 *  Return absolute SCSI data direction:
 *     1 = _DATA_OUT
 *     0 = _DIR_NONE
 *    -1 = _DATA_IN
Linus Torvalds's avatar
Linus Torvalds committed
288
 *
289 290 291 292 293 294 295
 * Changed: 3-20-2002 pdelaney to use the default data
 * direction and the defines set up in the
 * 2.4 kernel series
 *     1 = _DATA_OUT	changed to SCSI_DATA_WRITE (1)
 *     0 = _DIR_NONE	changed to SCSI_DATA_NONE (3)
 *    -1 = _DATA_IN	changed to SCSI_DATA_READ (2)
 * If the direction is unknown, fall through to original code.
Linus Torvalds's avatar
Linus Torvalds committed
296
 *
297 298 299
 * Mid-layer bug fix(): sg interface generates the wrong data
 * direction in some cases. Set the direction the hard way for
 * the most common commands.
Linus Torvalds's avatar
Linus Torvalds committed
300
 */
301 302
static inline int
mptscsih_io_direction(Scsi_Cmnd *cmd)
Linus Torvalds's avatar
Linus Torvalds committed
303
{
304
	switch (cmd->cmnd[0]) {
James Bottomley's avatar
James Bottomley committed
305 306 307
	case WRITE_6:
	case WRITE_10:
	case WRITE_16:
308 309
		return SCSI_DATA_WRITE;
		break;
James Bottomley's avatar
James Bottomley committed
310 311 312
	case READ_6:
	case READ_10:
	case READ_16:
313 314
		return SCSI_DATA_READ;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
315 316
	}

317 318
	if (cmd->sc_data_direction != SCSI_DATA_UNKNOWN)
		return cmd->sc_data_direction;
319

320 321 322
	switch (cmd->cmnd[0]) {
	/*  _DATA_OUT commands	*/
	case WRITE_6:		case WRITE_10:		case WRITE_12:
James Bottomley's avatar
James Bottomley committed
323
	case WRITE_16:
324 325 326 327 328 329 330 331 332 333 334 335 336
	case WRITE_LONG:	case WRITE_SAME:	case WRITE_BUFFER:
	case WRITE_VERIFY:	case WRITE_VERIFY_12:
	case COMPARE:		case COPY:		case COPY_VERIFY:
	case SEARCH_EQUAL:	case SEARCH_HIGH:	case SEARCH_LOW:
	case SEARCH_EQUAL_12:	case SEARCH_HIGH_12:	case SEARCH_LOW_12:
	case MODE_SELECT:	case MODE_SELECT_10:	case LOG_SELECT:
	case SEND_DIAGNOSTIC:	case CHANGE_DEFINITION: case UPDATE_BLOCK:
	case SET_WINDOW:	case MEDIUM_SCAN:	case SEND_VOLUME_TAG:
	case REASSIGN_BLOCKS:
	case PERSISTENT_RESERVE_OUT:
	case 0xea:
	case 0xa3:
		return SCSI_DATA_WRITE;
Linus Torvalds's avatar
Linus Torvalds committed
337

338 339 340 341 342 343 344
	/*  No data transfer commands  */
	case SEEK_6:		case SEEK_10:
	case RESERVE:		case RELEASE:
	case TEST_UNIT_READY:
	case START_STOP:
	case ALLOW_MEDIUM_REMOVAL:
		return SCSI_DATA_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
345

346 347 348 349 350 351
	/*  Conditional data transfer commands	*/
	case FORMAT_UNIT:
		if (cmd->cmnd[1] & 0x10)	/* FmtData (data out phase)? */
			return SCSI_DATA_WRITE;
		else
			return SCSI_DATA_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
352

353 354 355 356 357
	case VERIFY:
		if (cmd->cmnd[1] & 0x02)	/* VERIFY:BYTCHK (data out phase)? */
			return SCSI_DATA_WRITE;
		else
			return SCSI_DATA_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
358

359 360 361 362 363
	case RESERVE_10:
		if (cmd->cmnd[1] & 0x03)	/* RESERVE:{LongID|Extent} (data out phase)? */
			return SCSI_DATA_WRITE;
		else
			return SCSI_DATA_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
364

365 366 367 368 369
	/*  Must be data _IN!  */
	default:
		return SCSI_DATA_READ;
	}
} /* mptscsih_io_direction() */
Pam Delaney's avatar
Pam Delaney committed
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_add_sge - Place a simple SGE at address pAddr.
 *	@pAddr: virtual address for SGE
 *	@flagslength: SGE flags and data transfer length
 *	@dma_addr: Physical address
 *
 *	This routine places a MPT request frame back on the MPT adapter's
 *	FreeQ.
 */
static inline void
mptscsih_add_sge(char *pAddr, u32 flagslength, dma_addr_t dma_addr)
{
	if (sizeof(dma_addr_t) == sizeof(u64)) {
		SGESimple64_t *pSge = (SGESimple64_t *) pAddr;
		u32 tmp = dma_addr & 0xFFFFFFFF;
Pam Delaney's avatar
Pam Delaney committed
387

388 389 390 391
		pSge->FlagsLength = cpu_to_le32(flagslength);
		pSge->Address.Low = cpu_to_le32(tmp);
		tmp = (u32) ((u64)dma_addr >> 32);
		pSge->Address.High = cpu_to_le32(tmp);
Pam Delaney's avatar
Pam Delaney committed
392 393

	} else {
394 395 396 397 398
		SGESimple32_t *pSge = (SGESimple32_t *) pAddr;
		pSge->FlagsLength = cpu_to_le32(flagslength);
		pSge->Address = cpu_to_le32(dma_addr);
	}
} /* mptscsih_add_sge() */
Linus Torvalds's avatar
Linus Torvalds committed
399

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_add_chain - Place a chain SGE at address pAddr.
 *	@pAddr: virtual address for SGE
 *	@next: nextChainOffset value (u32's)
 *	@length: length of next SGL segment
 *	@dma_addr: Physical address
 *
 *	This routine places a MPT request frame back on the MPT adapter's
 *	FreeQ.
 */
static inline void
mptscsih_add_chain(char *pAddr, u8 next, u16 length, dma_addr_t dma_addr)
{
	if (sizeof(dma_addr_t) == sizeof(u64)) {
		SGEChain64_t *pChain = (SGEChain64_t *) pAddr;
		u32 tmp = dma_addr & 0xFFFFFFFF;
Linus Torvalds's avatar
Linus Torvalds committed
417

418 419
		pChain->Length = cpu_to_le16(length);
		pChain->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT | mpt_addr_size();
Pam Delaney's avatar
Pam Delaney committed
420

421
		pChain->NextChainOffset = next;
Linus Torvalds's avatar
Linus Torvalds committed
422

423 424 425 426 427 428 429 430 431 432 433
		pChain->Address.Low = cpu_to_le32(tmp);
		tmp = (u32) ((u64)dma_addr >> 32);
		pChain->Address.High = cpu_to_le32(tmp);
	} else {
		SGEChain32_t *pChain = (SGEChain32_t *) pAddr;
		pChain->Length = cpu_to_le16(length);
		pChain->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT | mpt_addr_size();
		pChain->NextChainOffset = next;
		pChain->Address = cpu_to_le32(dma_addr);
	}
} /* mptscsih_add_chain() */
Linus Torvalds's avatar
Linus Torvalds committed
434

435 436 437 438 439 440 441 442 443 444 445 446
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_getFreeChainBuffes - Function to get a free chain
 *	from the MPT_SCSI_HOST FreeChainQ.
 *	@hd: Pointer to the MPT_SCSI_HOST instance
 *	@req_idx: Index of the SCSI IO request frame. (output)
 *
 *	return SUCCESS or FAILED
 */
static inline int
mptscsih_getFreeChainBuffer(MPT_SCSI_HOST *hd, int *retIndex)
{
447
	MPT_FRAME_HDR *chainBuf;
448
	unsigned long flags;
449 450
	int rc;
	int chain_idx;
Linus Torvalds's avatar
Linus Torvalds committed
451

452 453
	spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
	if (!Q_IS_EMPTY(&hd->FreeChainQ)) {
Linus Torvalds's avatar
Linus Torvalds committed
454

455
		int offset;
456

457 458 459 460 461 462
		chainBuf = hd->FreeChainQ.head;
		Q_DEL_ITEM(&chainBuf->u.frame.linkage);
		offset = (u8 *)chainBuf - (u8 *)hd->ChainBuffer;
		chain_idx = offset / hd->ioc->req_sz;
		rc = SUCCESS;
	}
463 464 465 466
	else {
		rc = FAILED;
		chain_idx = MPT_HOST_NO_CHAIN;
	}
467
	spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
468

Pam Delaney's avatar
Pam Delaney committed
469

470
	*retIndex = chain_idx;
Pam Delaney's avatar
Pam Delaney committed
471

472 473
	dsgprintk((MYIOC_s_INFO_FMT "getFreeChainBuffer (index %d), got buf=%p\n",
			hd->ioc->name, *retIndex, chainBuf));
Pam Delaney's avatar
Pam Delaney committed
474

475 476
	return rc;
} /* mptscsih_getFreeChainBuffer() */
Pam Delaney's avatar
Pam Delaney committed
477

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_AddSGE - Add a SGE (plus chain buffers) to the
 *	SCSIIORequest_t Message Frame.
 *	@hd: Pointer to MPT_SCSI_HOST structure
 *	@SCpnt: Pointer to Scsi_Cmnd structure
 *	@pReq: Pointer to SCSIIORequest_t structure
 *
 *	Returns ...
 */
static int
mptscsih_AddSGE(MPT_SCSI_HOST *hd, Scsi_Cmnd *SCpnt,
		SCSIIORequest_t *pReq, int req_idx)
{
	char 	*psge;
	char	*chainSge;
	struct scatterlist *sg;
	int	 frm_sz;
	int	 sges_left, sg_done;
	int	 chain_idx = MPT_HOST_NO_CHAIN;
	int	 sgeOffset;
	int	 numSgeSlots, numSgeThisFrame;
	u32	 sgflags, sgdir, thisxfer = 0;
	int	 chain_dma_off = 0;
	int	 newIndex;
	int	 ii;
	dma_addr_t v2;

	sgdir = le32_to_cpu(pReq->Control) & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK;
	if (sgdir == MPI_SCSIIO_CONTROL_WRITE)  {
		sgdir = MPT_TRANSFER_HOST_TO_IOC;
	} else {
		sgdir = MPT_TRANSFER_IOC_TO_HOST;
	}

	psge = (char *) &pReq->SGL;
	frm_sz = hd->ioc->req_sz;

	/* Map the data portion, if any.
	 * sges_left  = 0 if no data transfer.
	 */
519
	if ( (sges_left = SCpnt->use_sg) ) {
520 521 522 523
		sges_left = pci_map_sg(hd->ioc->pcidev,
			       (struct scatterlist *) SCpnt->request_buffer,
			       SCpnt->use_sg,
			       scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
524
		if (sges_left == 0)
525
			return FAILED;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
	} else if (SCpnt->request_bufflen) {
		dma_addr_t	 buf_dma_addr;
		scPrivate	*my_priv;

		buf_dma_addr = pci_map_single(hd->ioc->pcidev,
				      SCpnt->request_buffer,
				      SCpnt->request_bufflen,
				      scsi_to_pci_dma_dir(SCpnt->sc_data_direction));

		/* We hide it here for later unmap. */
		my_priv = (scPrivate *) &SCpnt->SCp;
		my_priv->p1 = (void *)(ulong) buf_dma_addr;

		dsgprintk((MYIOC_s_INFO_FMT "SG: non-SG for %p, len=%d\n",
				hd->ioc->name, SCpnt, SCpnt->request_bufflen));

		mptscsih_add_sge((char *) &pReq->SGL,
			0xD1000000|MPT_SGE_FLAGS_ADDRESSING|sgdir|SCpnt->request_bufflen,
			buf_dma_addr);

		return SUCCESS;
	}

	/* Handle the SG case.
	 */
	sg = (struct scatterlist *) SCpnt->request_buffer;
	sg_done  = 0;
	sgeOffset = sizeof(SCSIIORequest_t) - sizeof(SGE_IO_UNION);
	chainSge = NULL;

	/* Prior to entering this loop - the following must be set
	 * current MF:  sgeOffset (bytes)
	 *              chainSge (Null if original MF is not a chain buffer)
	 *              sg_done (num SGE done for this MF)
	 */

nextSGEset:
	numSgeSlots = ((frm_sz - sgeOffset) / (sizeof(u32) + sizeof(dma_addr_t)) );
	numSgeThisFrame = (sges_left < numSgeSlots) ? sges_left : numSgeSlots;

	sgflags = MPT_SGE_FLAGS_SIMPLE_ELEMENT | MPT_SGE_FLAGS_ADDRESSING | sgdir;

	/* Get first (num - 1) SG elements
	 * Skip any SG entries with a length of 0
	 * NOTE: at finish, sg and psge pointed to NEXT data/location positions
	 */
	for (ii=0; ii < (numSgeThisFrame-1); ii++) {
		thisxfer = sg_dma_len(sg);
		if (thisxfer == 0) {
			sg ++; /* Get next SG element from the OS */
			sg_done++;
			continue;
		}

		v2 = sg_dma_address(sg);
		mptscsih_add_sge(psge, sgflags | thisxfer, v2);

		sg++;		/* Get next SG element from the OS */
		psge += (sizeof(u32) + sizeof(dma_addr_t));
		sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
		sg_done++;
	}

	if (numSgeThisFrame == sges_left) {
		/* Add last element, end of buffer and end of list flags.
		 */
		sgflags |= MPT_SGE_FLAGS_LAST_ELEMENT |
				MPT_SGE_FLAGS_END_OF_BUFFER |
				MPT_SGE_FLAGS_END_OF_LIST;

		/* Add last SGE and set termination flags.
		 * Note: Last SGE may have a length of 0 - which should be ok.
		 */
		thisxfer = sg_dma_len(sg);

		v2 = sg_dma_address(sg);
		mptscsih_add_sge(psge, sgflags | thisxfer, v2);
		/*
		sg++;
		psge += (sizeof(u32) + sizeof(dma_addr_t));
		*/
		sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
		sg_done++;

		if (chainSge) {
			/* The current buffer is a chain buffer,
			 * but there is not another one.
			 * Update the chain element
			 * Offset and Length fields.
			 */
			mptscsih_add_chain((char *)chainSge, 0, sgeOffset, hd->ChainBufferDMA + chain_dma_off);
		} else {
			/* The current buffer is the original MF
			 * and there is no Chain buffer.
			 */
			pReq->ChainOffset = 0;
		}
	} else {
		/* At least one chain buffer is needed.
		 * Complete the first MF
		 *  - last SGE element, set the LastElement bit
		 *  - set ChainOffset (words) for orig MF
		 *             (OR finish previous MF chain buffer)
		 *  - update MFStructPtr ChainIndex
		 *  - Populate chain element
		 * Also
		 * Loop until done.
		 */

		dsgprintk((MYIOC_s_INFO_FMT "SG: Chain Required! sg done %d\n",
				hd->ioc->name, sg_done));

		/* Set LAST_ELEMENT flag for last non-chain element
		 * in the buffer. Since psge points at the NEXT
		 * SGE element, go back one SGE element, update the flags
		 * and reset the pointer. (Note: sgflags & thisxfer are already
		 * set properly).
		 */
		if (sg_done) {
			u32 *ptmp = (u32 *) (psge - (sizeof(u32) + sizeof(dma_addr_t)));
			sgflags = le32_to_cpu(*ptmp);
			sgflags |= MPT_SGE_FLAGS_LAST_ELEMENT;
			*ptmp = cpu_to_le32(sgflags);
		}

		if (chainSge) {
			/* The current buffer is a chain buffer.
			 * chainSge points to the previous Chain Element.
			 * Update its chain element Offset and Length (must
			 * include chain element size) fields.
			 * Old chain element is now complete.
			 */
			u8 nextChain = (u8) (sgeOffset >> 2);
			sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
			mptscsih_add_chain((char *)chainSge, nextChain, sgeOffset, hd->ChainBufferDMA + chain_dma_off);
		} else {
			/* The original MF buffer requires a chain buffer -
			 * set the offset.
			 * Last element in this MF is a chain element.
			 */
			pReq->ChainOffset = (u8) (sgeOffset >> 2);
		}

		sges_left -= sg_done;


		/* NOTE: psge points to the beginning of the chain element
		 * in current buffer. Get a chain buffer.
		 */
		if ((mptscsih_getFreeChainBuffer(hd, &newIndex)) == FAILED)
			return FAILED;

		/* Update the tracking arrays.
		 * If chainSge == NULL, update ReqToChain, else ChainToChain
		 */
		if (chainSge) {
			hd->ChainToChain[chain_idx] = newIndex;
		} else {
			hd->ReqToChain[req_idx] = newIndex;
		}
		chain_idx = newIndex;
		chain_dma_off = hd->ioc->req_sz * chain_idx;

		/* Populate the chainSGE for the current buffer.
		 * - Set chain buffer pointer to psge and fill
		 *   out the Address and Flags fields.
		 */
		chainSge = (char *) psge;
		dsgprintk((KERN_INFO "  Current buff @ %p (index 0x%x)",
				psge, req_idx));

		/* Start the SGE for the next buffer
		 */
		psge = (char *) (hd->ChainBuffer + chain_dma_off);
		sgeOffset = 0;
		sg_done = 0;

		dsgprintk((KERN_INFO "  Chain buff @ %p (index 0x%x)\n",
				psge, chain_idx));

		/* Start the SGE for the next buffer
		 */

		goto nextSGEset;
	}

	return SUCCESS;
} /* mptscsih_AddSGE() */

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_io_done - Main SCSI IO callback routine registered to
 *	Fusion MPT (base) driver
 *	@ioc: Pointer to MPT_ADAPTER structure
 *	@mf: Pointer to original MPT request frame
 *	@r: Pointer to MPT reply frame (NULL if TurboReply)
 *
 *	This routine is called from mpt.c::mpt_interrupt() at the completion
 *	of any SCSI IO request.
 *	This routine is registered with the Fusion MPT (base) driver at driver
 *	load/init time via the mpt_register() API call.
 *
 *	Returns 1 indicating alloc'd request frame ptr should be freed.
 */
static int
mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
{
	Scsi_Cmnd	*sc;
	MPT_SCSI_HOST	*hd;
	SCSIIORequest_t	*pScsiReq;
	SCSIIOReply_t	*pScsiReply;
	u16		 req_idx;

	hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;

	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
	sc = hd->ScsiLookup[req_idx];
	if (sc == NULL) {
744 745
		MPIHeader_t *hdr = (MPIHeader_t *)mf;

746 747 748
		/* Remark: writeSDP1 will use the ScsiDoneCtx
		 * If a SCSI I/O cmd, device disabled by OS and
		 * completion done. Cannot touch sc struct. Just free mem.
749
		 */
750 751 752
		if (hdr->Function == MPI_FUNCTION_SCSI_IO_REQUEST)
			printk(MYIOC_s_ERR_FMT "NULL ScsiCmd ptr!\n",
			ioc->name);
753 754 755 756 757

		mptscsih_freeChainBuffers(hd, req_idx);
		return 1;
	}

758 759 760
	dmfprintk((MYIOC_s_INFO_FMT
		"ScsiDone (mf=%p,mr=%p,sc=%p,idx=%d)\n",
		ioc->name, mf, mr, sc, req_idx));
761 762 763 764 765

	sc->result = DID_OK << 16;		/* Set default reply as OK */
	pScsiReq = (SCSIIORequest_t *) mf;
	pScsiReply = (SCSIIOReply_t *) mr;

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
#ifdef MPTSCSIH_DBG_TIMEOUT
	if (ioc->timeout_cnt > 0) {
		int ii, left = 0;

		for (ii=0; ii < 8; ii++) {
			if (sc == foo_to[ii]) {
				printk(MYIOC_s_INFO_FMT "complete (%p, %ld)\n",
					ioc->name, sc, jiffies);
				foo_to[ii] = NULL;
			}
			if (foo_to[ii] != NULL)
				left++;
		}

		if (left == 0) {
			ioc->timeout_maxcnt = 0;
			ioc->timeout_cnt = 0;
		}
	}
#endif

787 788
	if (pScsiReply == NULL) {
		/* special context reply handling */
789
		;
790 791 792
	} else {
		u32	 xfer_cnt;
		u16	 status;
793
		u8	 scsi_state, scsi_status;
794 795 796 797

		status = le16_to_cpu(pScsiReply->IOCStatus) & MPI_IOCSTATUS_MASK;
		scsi_state = pScsiReply->SCSIState;

798
		dprintk((KERN_NOTICE "  Uh-Oh! (%d:%d:%d) mf=%p, mr=%p, sc=%p\n",
799 800
				ioc->id, pScsiReq->TargetID, pScsiReq->LUN[1],
				mf, mr, sc));
801
		dprintk((KERN_NOTICE "  IOCStatus=%04xh, SCSIState=%02xh"
802 803 804 805 806 807
				", SCSIStatus=%02xh, IOCLogInfo=%08xh\n",
				status, scsi_state, pScsiReply->SCSIStatus,
				le32_to_cpu(pScsiReply->IOCLogInfo)));

		if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID)
			copy_sense_data(sc, hd, mf, pScsiReply);
808 809 810 811 812 813 814 815
                
		/*
		 *  Look for + dump FCP ResponseInfo[]!
		 */
		if (scsi_state & MPI_SCSI_STATE_RESPONSE_INFO_VALID) {
			printk(KERN_NOTICE "  FCP_ResponseInfo=%08xh\n",
			le32_to_cpu(pScsiReply->ResponseInfo));
		}
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840

		switch(status) {
		case MPI_IOCSTATUS_BUSY:			/* 0x0002 */
			/* CHECKME!
			 * Maybe: DRIVER_BUSY | SUGGEST_RETRY | DID_SOFT_ERROR (retry)
			 * But not: DID_BUS_BUSY lest one risk
			 * killing interrupt handler:-(
			 */
			sc->result = STS_BUSY;
			break;

		case MPI_IOCSTATUS_SCSI_INVALID_BUS:		/* 0x0041 */
		case MPI_IOCSTATUS_SCSI_INVALID_TARGETID:	/* 0x0042 */
			sc->result = DID_BAD_TARGET << 16;
			break;

		case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:	/* 0x0043 */
			/* Spoof to SCSI Selection Timeout! */
			sc->result = DID_NO_CONNECT << 16;

			if (hd->sel_timeout[pScsiReq->TargetID] < 0xFFFF)
				hd->sel_timeout[pScsiReq->TargetID]++;
			break;

		case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:	/* 0x0048 */
841 842
		case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:		/* 0x004B */
		case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:		/* 0x004C */
843 844 845 846 847 848 849
			/* Linux handles an unsolicited DID_RESET better
			 * than an unsolicited DID_ABORT.
			 */
			sc->result = DID_RESET << 16;

			/* GEM Workaround. */
			if (hd->is_spi)
850
				mptscsih_no_negotiate(hd, sc->device->id);
851 852 853 854 855 856 857 858
			break;

		case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:	/* 0x0049 */
		case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:		/* 0x0045 */
			/*
			 *  Do upfront check for valid SenseData and give it
			 *  precedence!
			 */
859 860 861 862
			scsi_status = pScsiReply->SCSIStatus;
			sc->result = (DID_OK << 16) | scsi_status;
			xfer_cnt = le32_to_cpu(pScsiReply->TransferCount);
			if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
Pam Delaney's avatar
Pam Delaney committed
863 864 865
				/* Have already saved the status and sense data
				 */
				;
866 867 868 869 870 871 872 873 874 875 876 877 878
			} else {
				if ( (xfer_cnt == 0) || (sc->underflow > xfer_cnt)) {
					sc->result = DID_SOFT_ERROR << 16;
				}
				if (scsi_state & (MPI_SCSI_STATE_AUTOSENSE_FAILED | MPI_SCSI_STATE_NO_SCSI_STATUS)) {
					/* What to do?
				 	*/
					sc->result = DID_SOFT_ERROR << 16;
				}
				else if (scsi_state & MPI_SCSI_STATE_TERMINATED) {
					/*  Not real sure here either...  */
					sc->result = DID_RESET << 16;
				}
Linus Torvalds's avatar
Linus Torvalds committed
879 880
			}

Pam Delaney's avatar
Pam Delaney committed
881 882 883 884 885
			/* Give report and update residual count.
			 */
			dprintk((KERN_NOTICE "  sc->underflow={report ERR if < %02xh bytes xfer'd}\n",
					sc->underflow));
			dprintk((KERN_NOTICE "  ActBytesXferd=%02xh\n", xfer_cnt));
Linus Torvalds's avatar
Linus Torvalds committed
886

Pam Delaney's avatar
Pam Delaney committed
887 888
			sc->resid = sc->request_bufflen - xfer_cnt;
			dprintk((KERN_NOTICE "  SET sc->resid=%02xh\n", sc->resid));
889
			
Pam Delaney's avatar
Pam Delaney committed
890 891
			/* Report Queue Full
			 */
892
			if (scsi_status == MPI_SCSI_STATUS_TASK_SET_FULL)
Pam Delaney's avatar
Pam Delaney committed
893
				mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
894
			
Linus Torvalds's avatar
Linus Torvalds committed
895 896
			break;

Pam Delaney's avatar
Pam Delaney committed
897
		case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:	/* 0x0040 */
Linus Torvalds's avatar
Linus Torvalds committed
898
		case MPI_IOCSTATUS_SUCCESS:			/* 0x0000 */
Pam Delaney's avatar
Pam Delaney committed
899
			sc->result = (DID_OK << 16) | pScsiReply->SCSIStatus;
900 901 902
			if (scsi_state == 0) {
				;
			} else if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
Pam Delaney's avatar
Pam Delaney committed
903
				/*
904
				 * If running against circa 200003dd 909 MPT f/w,
Pam Delaney's avatar
Pam Delaney committed
905 906 907
				 * may get this (AUTOSENSE_VALID) for actual TASK_SET_FULL
				 * (QUEUE_FULL) returned from device! --> get 0x0000?128
				 * and with SenseBytes set to 0.
Linus Torvalds's avatar
Linus Torvalds committed
908 909 910
				 */
				if (pScsiReply->SCSIStatus == MPI_SCSI_STATUS_TASK_SET_FULL)
					mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
Pam Delaney's avatar
Pam Delaney committed
911

Linus Torvalds's avatar
Linus Torvalds committed
912
			}
913
			else if (scsi_state &
Pam Delaney's avatar
Pam Delaney committed
914 915
			         (MPI_SCSI_STATE_AUTOSENSE_FAILED | MPI_SCSI_STATE_NO_SCSI_STATUS)
			   ) {
Linus Torvalds's avatar
Linus Torvalds committed
916
				/*
Pam Delaney's avatar
Pam Delaney committed
917
				 * What to do?
Linus Torvalds's avatar
Linus Torvalds committed
918 919 920
				 */
				sc->result = DID_SOFT_ERROR << 16;
			}
921
			else if (scsi_state & MPI_SCSI_STATE_TERMINATED) {
Linus Torvalds's avatar
Linus Torvalds committed
922
				/*  Not real sure here either...  */
Pam Delaney's avatar
Pam Delaney committed
923 924
				sc->result = DID_RESET << 16;
			}
925
			else if (scsi_state & MPI_SCSI_STATE_QUEUE_TAG_REJECTED) {
Pam Delaney's avatar
Pam Delaney committed
926 927 928 929 930
				/* Device Inq. data indicates that it supports
				 * QTags, but rejects QTag messages.
				 * This command completed OK.
				 *
				 * Not real sure here either so do nothing...  */
Linus Torvalds's avatar
Linus Torvalds committed
931 932 933 934 935
			}

			if (sc->result == MPI_SCSI_STATUS_TASK_SET_FULL)
				mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);

Pam Delaney's avatar
Pam Delaney committed
936 937 938 939 940 941 942 943 944 945 946 947
			/* Add handling of:
			 * Reservation Conflict, Busy,
			 * Command Terminated, CHECK
			 */
			break;

		case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:		/* 0x0047 */
			if (pScsiReply->SCSIState & MPI_SCSI_STATE_TERMINATED) {
				/*  Not real sure here either...  */
				sc->result = DID_RESET << 16;
			} else
				sc->result = DID_SOFT_ERROR << 16;
Linus Torvalds's avatar
Linus Torvalds committed
948 949 950 951 952 953 954 955 956 957 958 959 960 961
			break;

		case MPI_IOCSTATUS_INVALID_FUNCTION:		/* 0x0001 */
		case MPI_IOCSTATUS_INVALID_SGL:			/* 0x0003 */
		case MPI_IOCSTATUS_INTERNAL_ERROR:		/* 0x0004 */
		case MPI_IOCSTATUS_RESERVED:			/* 0x0005 */
		case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES:	/* 0x0006 */
		case MPI_IOCSTATUS_INVALID_FIELD:		/* 0x0007 */
		case MPI_IOCSTATUS_INVALID_STATE:		/* 0x0008 */
		case MPI_IOCSTATUS_SCSI_DATA_OVERRUN:		/* 0x0044 */
		case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:		/* 0x0046 */
		case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED:	/* 0x004A */
		default:
			/*
Pam Delaney's avatar
Pam Delaney committed
962
			 * What to do?
Linus Torvalds's avatar
Linus Torvalds committed
963 964 965 966 967 968
			 */
			sc->result = DID_SOFT_ERROR << 16;
			break;

		}	/* switch(status) */

Pam Delaney's avatar
Pam Delaney committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982
		dprintk((KERN_NOTICE "  sc->result set to %08xh\n", sc->result));
	} /* end of address reply case */

	/* Unmap the DMA buffers, if any. */
	if (sc->use_sg) {
		pci_unmap_sg(ioc->pcidev, (struct scatterlist *) sc->request_buffer,
			    sc->use_sg, scsi_to_pci_dma_dir(sc->sc_data_direction));
	} else if (sc->request_bufflen) {
		scPrivate	*my_priv;

		my_priv = (scPrivate *) &sc->SCp;
		pci_unmap_single(ioc->pcidev, (dma_addr_t)(ulong)my_priv->p1,
			   sc->request_bufflen,
			   scsi_to_pci_dma_dir(sc->sc_data_direction));
Linus Torvalds's avatar
Linus Torvalds committed
983 984
	}

Pam Delaney's avatar
Pam Delaney committed
985
	hd->ScsiLookup[req_idx] = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
986

Pam Delaney's avatar
Pam Delaney committed
987
	sc->scsi_done(sc);		/* Issue the command callback */
Linus Torvalds's avatar
Linus Torvalds committed
988

Pam Delaney's avatar
Pam Delaney committed
989 990
	/* Free Chain buffers */
	mptscsih_freeChainBuffers(hd, req_idx);
Linus Torvalds's avatar
Linus Torvalds committed
991 992 993
	return 1;
}

Pam Delaney's avatar
Pam Delaney committed
994 995 996 997 998 999 1000 1001 1002 1003 1004
/*
 * Flush all commands on the doneQ.
 * Lock Q when deleting/adding members
 * Lock io_request_lock for OS callback.
 */
static void
flush_doneQ(MPT_SCSI_HOST *hd)
{
	MPT_DONE_Q	*buffer;
	Scsi_Cmnd	*SCpnt;
	unsigned long	 flags;
Linus Torvalds's avatar
Linus Torvalds committed
1005

Pam Delaney's avatar
Pam Delaney committed
1006 1007
	/* Flush the doneQ.
	 */
1008
	dtmprintk((KERN_INFO MYNAM ": flush_doneQ called\n"));
Pam Delaney's avatar
Pam Delaney committed
1009 1010 1011 1012 1013
	while (1) {
		spin_lock_irqsave(&hd->freedoneQlock, flags);
		if (Q_IS_EMPTY(&hd->doneQ)) {
			spin_unlock_irqrestore(&hd->freedoneQlock, flags);
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1014 1015
		}

Pam Delaney's avatar
Pam Delaney committed
1016 1017
		buffer = hd->doneQ.head;
		/* Delete from Q
Linus Torvalds's avatar
Linus Torvalds committed
1018
		 */
Pam Delaney's avatar
Pam Delaney committed
1019
		Q_DEL_ITEM(buffer);
Linus Torvalds's avatar
Linus Torvalds committed
1020

Pam Delaney's avatar
Pam Delaney committed
1021 1022 1023 1024
		/* Set the Scsi_Cmnd pointer
		 */
		SCpnt = (Scsi_Cmnd *) buffer->argp;
		buffer->argp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1025

Pam Delaney's avatar
Pam Delaney committed
1026 1027 1028 1029
		/* Add to the freeQ
		 */
		Q_ADD_TAIL(&hd->freeQ.head, buffer, MPT_DONE_Q);
		spin_unlock_irqrestore(&hd->freedoneQlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1030

Pam Delaney's avatar
Pam Delaney committed
1031 1032 1033 1034
		/* Do the OS callback.
		 */
		SCpnt->scsi_done(SCpnt);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1035

Pam Delaney's avatar
Pam Delaney committed
1036 1037
	return;
}
Linus Torvalds's avatar
Linus Torvalds committed
1038

Pam Delaney's avatar
Pam Delaney committed
1039 1040 1041 1042 1043 1044 1045 1046 1047
/*
 * Search the doneQ for a specific command. If found, delete from Q.
 * Calling function will finish processing.
 */
static void
search_doneQ_for_cmd(MPT_SCSI_HOST *hd, Scsi_Cmnd *SCpnt)
{
	unsigned long	 flags;
	MPT_DONE_Q	*buffer;
Linus Torvalds's avatar
Linus Torvalds committed
1048

Pam Delaney's avatar
Pam Delaney committed
1049 1050 1051 1052 1053 1054 1055 1056
	spin_lock_irqsave(&hd->freedoneQlock, flags);
	if (!Q_IS_EMPTY(&hd->doneQ)) {
		buffer = hd->doneQ.head;
		do {
			Scsi_Cmnd *sc = (Scsi_Cmnd *) buffer->argp;
			if (SCpnt == sc) {
				Q_DEL_ITEM(buffer);
				SCpnt->result = sc->result;
Linus Torvalds's avatar
Linus Torvalds committed
1057

Pam Delaney's avatar
Pam Delaney committed
1058 1059 1060
				/* Set the Scsi_Cmnd pointer
				 */
				buffer->argp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1061

Pam Delaney's avatar
Pam Delaney committed
1062 1063 1064 1065 1066 1067
				/* Add to the freeQ
				 */
				Q_ADD_TAIL(&hd->freeQ.head, buffer, MPT_DONE_Q);
				break;
			}
		} while ((buffer = buffer->forw) != (MPT_DONE_Q *) &hd->doneQ);
Linus Torvalds's avatar
Linus Torvalds committed
1068
	}
Pam Delaney's avatar
Pam Delaney committed
1069 1070
	spin_unlock_irqrestore(&hd->freedoneQlock, flags);
	return;
Linus Torvalds's avatar
Linus Torvalds committed
1071 1072
}

Pam Delaney's avatar
Pam Delaney committed
1073 1074 1075 1076 1077
/*
 *	mptscsih_flush_running_cmds - For each command found, search
 *		Scsi_Host instance taskQ and reply to OS.
 *		Called only if recovering from a FW reload.
 *	@hd: Pointer to a SCSI HOST structure
Linus Torvalds's avatar
Linus Torvalds committed
1078
 *
Pam Delaney's avatar
Pam Delaney committed
1079
 *	Returns: None.
Linus Torvalds's avatar
Linus Torvalds committed
1080
 *
Pam Delaney's avatar
Pam Delaney committed
1081
 *	Must be called while new I/Os are being queued.
Linus Torvalds's avatar
Linus Torvalds committed
1082
 */
Pam Delaney's avatar
Pam Delaney committed
1083 1084
static void
mptscsih_flush_running_cmds(MPT_SCSI_HOST *hd)
Linus Torvalds's avatar
Linus Torvalds committed
1085
{
1086 1087 1088
	Scsi_Cmnd	*SCpnt;
	MPT_FRAME_HDR	*mf;
	MPT_DONE_Q	*buffer;
Pam Delaney's avatar
Pam Delaney committed
1089 1090
	int		 ii;
	int		 max = hd->ioc->req_depth;
1091
	unsigned long	 flags;
Pam Delaney's avatar
Pam Delaney committed
1092 1093 1094 1095

	dprintk((KERN_INFO MYNAM ": flush_ScsiLookup called\n"));
	for (ii= 0; ii < max; ii++) {
		if ((SCpnt = hd->ScsiLookup[ii]) != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
1096

Pam Delaney's avatar
Pam Delaney committed
1097 1098
			/* Command found.
			 */
1099 1100 1101 1102 1103

			/* Search pendingQ, if found,
			 * delete from Q.
			 */
			mptscsih_search_pendingQ(hd, ii);
Linus Torvalds's avatar
Linus Torvalds committed
1104

Pam Delaney's avatar
Pam Delaney committed
1105 1106 1107 1108 1109 1110 1111 1112
			/* Null ScsiLookup index
			 */
			hd->ScsiLookup[ii] = NULL;

			mf = MPT_INDEX_2_MFPTR(hd->ioc, ii);
			dmfprintk(( "flush: ScsiDone (mf=%p,sc=%p)\n",
					mf, SCpnt));

1113
			/* Set status, free OS resources (SG DMA buffers)
Pam Delaney's avatar
Pam Delaney committed
1114
			 * Do OS callback
1115
			 * Free driver resources (chain, msg buffers)
Pam Delaney's avatar
Pam Delaney committed
1116
			 */
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
			if (scsi_device_online(SCpnt->device)) {
				if (SCpnt->use_sg) {
					pci_unmap_sg(hd->ioc->pcidev,
						(struct scatterlist *) SCpnt->request_buffer,
						SCpnt->use_sg,
						scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
				} else if (SCpnt->request_bufflen) {
					scPrivate	*my_priv;

					my_priv = (scPrivate *) &SCpnt->SCp;
					pci_unmap_single(hd->ioc->pcidev,
						(dma_addr_t)(ulong)my_priv->p1,
						SCpnt->request_bufflen,
						scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
				}
1132
			}
Pam Delaney's avatar
Pam Delaney committed
1133 1134 1135 1136 1137 1138 1139 1140
			SCpnt->result = DID_RESET << 16;
			SCpnt->host_scribble = NULL;

			/* Free Chain buffers */
			mptscsih_freeChainBuffers(hd, ii);

			/* Free Message frames */
			mpt_free_msg_frame(ScsiDoneCtx, hd->ioc->id, mf);
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

#if 1
			/* Post to doneQ, do not reply until POST phase
			 * of reset handler....prevents new commands from
			 * being queued.
			 */
			spin_lock_irqsave(&hd->freedoneQlock, flags);
			if (!Q_IS_EMPTY(&hd->freeQ)) {
				buffer = hd->freeQ.head;
				Q_DEL_ITEM(buffer);

				/* Set the Scsi_Cmnd pointer
				 */
				buffer->argp = (void *)SCpnt;

				/* Add to the doneQ
				 */
				Q_ADD_TAIL(&hd->doneQ.head, buffer, MPT_DONE_Q);
				spin_unlock_irqrestore(&hd->freedoneQlock, flags);
			} else {
				spin_unlock_irqrestore(&hd->freedoneQlock, flags);
				SCpnt->scsi_done(SCpnt);
			}
#else
			SCpnt->scsi_done(SCpnt);	/* Issue the command callback */
#endif

Linus Torvalds's avatar
Linus Torvalds committed
1168 1169
		}
	}
1170 1171 1172 1173

	return;
}

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
/*
 *	mptscsih_search_running_cmds - Delete any commands associated
 *		with the specified target and lun. Function called only
 *		when a lun is disable by mid-layer.
 *		Do NOT access the referenced Scsi_Cmnd structure or
 *		members. Will cause either a paging or NULL ptr error.
 *	@hd: Pointer to a SCSI HOST structure
 *	@target: target id
 *	@lun: lun
 *
 *	Returns: None.
 *
 *	Called from slave_destroy.
 */
static void
mptscsih_search_running_cmds(MPT_SCSI_HOST *hd, uint target, uint lun)
{
	SCSIIORequest_t	*mf = NULL;
	int		 ii;
	int		 max = hd->ioc->req_depth;

1195 1196
	dsprintk((KERN_INFO MYNAM ": search_running target %d lun %d max %d\n",
			target, lun, max));
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219

	for (ii=0; ii < max; ii++) {
		if (hd->ScsiLookup[ii] != NULL) {

			mf = (SCSIIORequest_t *)MPT_INDEX_2_MFPTR(hd->ioc, ii);

			dsprintk(( "search_running: found (sc=%p, mf = %p) target %d, lun %d \n",
					hd->ScsiLookup[ii], mf, mf->TargetID, mf->LUN[1]));

			if ((mf->TargetID != ((u8)target)) || (mf->LUN[1] != ((u8) lun)))
				continue;

			/* Cleanup
			 */
			hd->ScsiLookup[ii] = NULL;
			mptscsih_freeChainBuffers(hd, ii);
			mpt_free_msg_frame(ScsiDoneCtx, hd->ioc->id, (MPT_FRAME_HDR *)mf);
		}
	}

	return;
}

Pam Delaney's avatar
Pam Delaney committed
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_initChainBuffers - Allocate memory for and initialize
 *	chain buffers, chain buffer control arrays and spinlock.
 *	@hd: Pointer to MPT_SCSI_HOST structure
 *	@init: If set, initialize the spin lock.
 */
static int
mptscsih_initChainBuffers (MPT_SCSI_HOST *hd, int init)
{
	MPT_FRAME_HDR	*chain;
	u8		*mem;
	unsigned long	flags;
1233 1234
	int		sz, ii, num_chain;
	int 		scale, num_sge;
Linus Torvalds's avatar
Linus Torvalds committed
1235

1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	/* chain buffer allocation done from PrimeIocFifos */	
	if (hd->ioc->fifo_pool == NULL) 
		return -1;
	
	hd->ChainBuffer = hd->ioc->chain_alloc;
	hd->ChainBufferDMA = hd->ioc->chain_alloc_dma;
	
	dprintk((KERN_INFO "  ChainBuffer    @ %p(%p), sz=%d\n",
		 hd->ChainBuffer, (void *)(ulong)hd->ChainBufferDMA, hd->ioc->chain_alloc_sz));
		
1246 1247
	/* ReqToChain size must equal the req_depth
	 * index = req_idx
Pam Delaney's avatar
Pam Delaney committed
1248 1249
	 */
	if (hd->ReqToChain == NULL) {
1250
		sz = hd->ioc->req_depth * sizeof(int);
Pam Delaney's avatar
Pam Delaney committed
1251 1252 1253 1254 1255
		mem = kmalloc(sz, GFP_ATOMIC);
		if (mem == NULL)
			return -1;

		hd->ReqToChain = (int *) mem;
Linus Torvalds's avatar
Linus Torvalds committed
1256
	}
1257
	for (ii = 0; ii < hd->ioc->req_depth; ii++)
1258
		hd->ReqToChain[ii] = MPT_HOST_NO_CHAIN;
Linus Torvalds's avatar
Linus Torvalds committed
1259

1260 1261 1262 1263
	/* ChainToChain size must equal the total number
	 * of chain buffers to be allocated.
	 * index = chain_idx
	 *
1264
	 * Calculate the number of chain buffers needed(plus 1) per I/O
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
	 * then multiply the the maximum number of simultaneous cmds
	 *
	 * num_sge = num sge in request frame + last chain buffer
	 * scale = num sge per chain buffer if no chain element
	 */
	scale = hd->ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
	if (sizeof(dma_addr_t) == sizeof(u64))
		num_sge =  scale + (hd->ioc->req_sz - 60) / (sizeof(dma_addr_t) + sizeof(u32));
	else
		num_sge =  1+ scale + (hd->ioc->req_sz - 64) / (sizeof(dma_addr_t) + sizeof(u32));

	num_chain = 1;
	while (hd->max_sge - num_sge > 0) {
		num_chain++;
		num_sge += (scale - 1);
	}
	num_chain++;

	if ((int) hd->ioc->chip_type > (int) FC929)
		num_chain *= MPT_SCSI_CAN_QUEUE;
	else
		num_chain *= MPT_FC_CAN_QUEUE;

	hd->num_chain = num_chain;

	sz = num_chain * sizeof(int);
Pam Delaney's avatar
Pam Delaney committed
1291 1292 1293 1294
	if (hd->ChainToChain == NULL) {
		mem = kmalloc(sz, GFP_ATOMIC);
		if (mem == NULL)
			return -1;
Linus Torvalds's avatar
Linus Torvalds committed
1295

Pam Delaney's avatar
Pam Delaney committed
1296 1297 1298 1299 1300
		hd->ChainToChain = (int *) mem;
	} else {
		mem = (u8 *) hd->ChainToChain;
	}
	memset(mem, 0xFF, sz);
Linus Torvalds's avatar
Linus Torvalds committed
1301

Pam Delaney's avatar
Pam Delaney committed
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314

	/* Initialize the free chain Q.
	 */
	if (init) {
		spin_lock_init(&hd->FreeChainQlock);
	}

	spin_lock_irqsave (&hd->FreeChainQlock, flags);
	Q_INIT(&hd->FreeChainQ, MPT_FRAME_HDR);

	/* Post the chain buffers to the FreeChainQ.
	 */
	mem = (u8 *)hd->ChainBuffer;
1315
	for (ii=0; ii < num_chain; ii++) {
Pam Delaney's avatar
Pam Delaney committed
1316 1317 1318
		chain = (MPT_FRAME_HDR *) mem;
		Q_ADD_TAIL(&hd->FreeChainQ.head, &chain->u.frame.linkage, MPT_FRAME_HDR);
		mem += hd->ioc->req_sz;
Linus Torvalds's avatar
Linus Torvalds committed
1319
	}
Pam Delaney's avatar
Pam Delaney committed
1320
	spin_unlock_irqrestore(&hd->FreeChainQlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1321 1322 1323 1324 1325

	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
/*
 *  Hack! It might be nice to report if a device is returning QUEUE_FULL
 *  but maybe not each and every time...
 */
static long last_queue_full = 0;

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_report_queue_full - Report QUEUE_FULL status returned
 *	from a SCSI target device.
 *	@sc: Pointer to Scsi_Cmnd structure
 *	@pScsiReply: Pointer to SCSIIOReply_t
 *	@pScsiReq: Pointer to original SCSI request
Linus Torvalds's avatar
Linus Torvalds committed
1339
 *
Pam Delaney's avatar
Pam Delaney committed
1340 1341 1342
 *	This routine periodically reports QUEUE_FULL status returned from a
 *	SCSI target device.  It reports this to the console via kernel
 *	printk() API call, not more than once every 10 seconds.
Linus Torvalds's avatar
Linus Torvalds committed
1343
 */
Pam Delaney's avatar
Pam Delaney committed
1344 1345
static void
mptscsih_report_queue_full(Scsi_Cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq)
Linus Torvalds's avatar
Linus Torvalds committed
1346
{
Pam Delaney's avatar
Pam Delaney committed
1347
	long time = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
1348

Pam Delaney's avatar
Pam Delaney committed
1349 1350
	if (time - last_queue_full > 10 * HZ) {
		char *ioc_str = "ioc?";
Linus Torvalds's avatar
Linus Torvalds committed
1351

1352 1353
		if (sc->device && sc->device->host != NULL && sc->device->host->hostdata != NULL)
			ioc_str = ((MPT_SCSI_HOST *)sc->device->host->hostdata)->ioc->name;
1354 1355
		dprintk((MYIOC_s_WARN_FMT "Device (%d:%d:%d) reported QUEUE_FULL!\n",
				ioc_str, 0, sc->device->id, sc->device->lun));
Pam Delaney's avatar
Pam Delaney committed
1356 1357
		last_queue_full = time;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1358 1359 1360
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1361
static char *info_kbuf = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1362 1363

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
James Bottomley's avatar
James Bottomley committed
1364 1365 1366 1367
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_probe - Installs scsi devices per bus.
 *	@pdev: Pointer to pci_dev structure
Linus Torvalds's avatar
Linus Torvalds committed
1368
 *
James Bottomley's avatar
James Bottomley committed
1369
 *	Returns 0 for success, non-zero for failure.
Linus Torvalds's avatar
Linus Torvalds committed
1370 1371
 *
 */
James Bottomley's avatar
James Bottomley committed
1372

1373
static int
James Bottomley's avatar
James Bottomley committed
1374
mptscsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Linus Torvalds's avatar
Linus Torvalds committed
1375
{
1376 1377
	struct Scsi_Host	*sh;
	MPT_SCSI_HOST		*hd;
James Bottomley's avatar
James Bottomley committed
1378
	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
Pam Delaney's avatar
Pam Delaney committed
1379 1380 1381 1382 1383
	MPT_DONE_Q		*freedoneQ;
	unsigned long		 flags;
	int			 sz, ii;
	int			 numSGE = 0;
	int			 scale;
1384
	int			 ioc_cap;
Pam Delaney's avatar
Pam Delaney committed
1385
	u8			*mem;
1386
	int			error=0;
Linus Torvalds's avatar
Linus Torvalds committed
1387 1388


1389 1390 1391 1392 1393 1394 1395 1396 1397
	/* 20010202 -sralston
	 *  Added sanity check on readiness of the MPT adapter.
	 */
	if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
		printk(MYIOC_s_WARN_FMT
		  "Skipping because it's not operational!\n",
		  ioc->name);
		return -ENODEV;
	}
Pam Delaney's avatar
Pam Delaney committed
1398

1399 1400 1401 1402 1403
	if (!ioc->active) {
		printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
		  ioc->name);
		return -ENODEV;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1404

1405 1406 1407 1408 1409 1410 1411 1412
	/*  Sanity check - ensure at least 1 port is INITIATOR capable
	 */
	ioc_cap = 0;
	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
		if (ioc->pfacts[ii].ProtocolFlags &
		    MPI_PORTFACTS_PROTOCOL_INITIATOR)
			ioc_cap ++;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1413

1414 1415 1416 1417 1418 1419
	if (!ioc_cap) {
		printk(MYIOC_s_WARN_FMT
			"Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
			ioc->name, ioc);
		return -ENODEV;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1420

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
	sh = scsi_host_alloc(&driver_template, sizeof(MPT_SCSI_HOST));
        
	if (!sh) {
		printk(MYIOC_s_WARN_FMT
			"Unable to register controller with SCSI subsystem\n",
			ioc->name);
                return -1;
        }
	
	spin_lock_irqsave(&ioc->FreeQlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1431

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	/* Attach the SCSI Host to the IOC structure
	 */
	ioc->sh = sh;

	sh->io_port = 0;
	sh->n_io_port = 0;
	sh->irq = 0;

	/* set 16 byte cdb's */
	sh->max_cmd_len = 16;

	/* Yikes!  This is important!
	 * Otherwise, by default, linux
	 * only scans target IDs 0-7!
	 * pfactsN->MaxDevices unreliable
	 * (not supported in early
	 *	versions of the FW).
	 * max_id = 1 + actual max id,
	 * max_lun = 1 + actual last lun,
	 *	see hosts.h :o(
	 */
	if ((int)ioc->chip_type > (int)FC929) {
		sh->max_id = MPT_MAX_SCSI_DEVICES;
	} else {
	/* For FC, increase the queue depth
	 * from MPT_SCSI_CAN_QUEUE (31)
	 * to MPT_FC_CAN_QUEUE (63).
	 */
		sh->can_queue = MPT_FC_CAN_QUEUE;
		sh->max_id =
		  MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
	}
		
	sh->max_lun = MPT_LAST_LUN + 1;
	sh->max_sectors = MPT_SCSI_MAX_SECTORS;
	sh->max_channel = 0;
	sh->this_id = ioc->pfacts[0].PortSCSIID;
		
	/* Required entry.
	 */
	sh->unique_id = ioc->id;

	/* Verify that we won't exceed the maximum
	 * number of chain buffers
	 * We can optimize:  ZZ = req_sz/sizeof(SGE)
	 * For 32bit SGE's:
	 *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
	 *               + (req_sz - 64)/sizeof(SGE)
	 * A slightly different algorithm is required for
	 * 64bit SGEs.
	 */
	scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
	if (sizeof(dma_addr_t) == sizeof(u64)) {
		numSGE = (scale - 1) *
		  (ioc->facts.MaxChainDepth-1) + scale +
		  (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
		  sizeof(u32));
	} else {
		numSGE = 1 + (scale - 1) *
		  (ioc->facts.MaxChainDepth-1) + scale +
		  (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
		  sizeof(u32));
	}
		
	if (numSGE < sh->sg_tablesize) {
		/* Reset this value */
		dprintk((MYIOC_s_INFO_FMT
		  "Resetting sg_tablesize to %d from %d\n",
		  ioc->name, numSGE, sh->sg_tablesize));
		sh->sg_tablesize = numSGE;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1503

1504 1505 1506
	/* Set the pci device pointer in Scsi_Host structure.
	 */
	scsi_set_device(sh, &ioc->pcidev->dev);
Linus Torvalds's avatar
Linus Torvalds committed
1507

1508
	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1509

1510 1511 1512
	hd = (MPT_SCSI_HOST *) sh->hostdata;
	hd->ioc = ioc;
	hd->max_sge = sh->sg_tablesize;
Linus Torvalds's avatar
Linus Torvalds committed
1513

1514 1515
	if ((int)ioc->chip_type > (int)FC929)
	hd->is_spi = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1516

1517 1518 1519 1520
	if (DmpService && (ioc->chip_type == FC919 ||
	  ioc->chip_type == FC929)) {
		hd->is_multipath = 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1521

1522 1523 1524 1525 1526 1527 1528 1529 1530
	/* SCSI needs Scsi_Cmnd lookup table!
	 * (with size equal to req_depth*PtrSz!)
	 */
	sz = hd->ioc->req_depth * sizeof(void *);
	mem = kmalloc(sz, GFP_ATOMIC);
	if (mem == NULL) {
		error = -ENOMEM;
		goto mptscsih_probe_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1531

1532 1533
	memset(mem, 0, sz);
	hd->ScsiLookup = (struct scsi_cmnd **) mem;
Linus Torvalds's avatar
Linus Torvalds committed
1534

1535 1536 1537 1538 1539 1540 1541
	dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p, sz=%d\n",
		 ioc->name, hd->ScsiLookup, sz));
		
	if (mptscsih_initChainBuffers(hd, 1) < 0) {
		error = -EINVAL;
		goto mptscsih_probe_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1542

1543 1544 1545 1546 1547 1548 1549 1550
	/* Allocate memory for free and doneQ's
	 */
	sz = sh->can_queue * sizeof(MPT_DONE_Q);
	mem = kmalloc(sz, GFP_ATOMIC);
	if (mem == NULL) {
		error = -ENOMEM;
		goto mptscsih_probe_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1551

1552 1553
	memset(mem, 0xFF, sz);
	hd->memQ = mem;
Linus Torvalds's avatar
Linus Torvalds committed
1554

1555 1556 1557 1558 1559 1560
	/* Initialize the free, done and pending Qs.
	 */
	Q_INIT(&hd->freeQ, MPT_DONE_Q);
	Q_INIT(&hd->doneQ, MPT_DONE_Q);
	Q_INIT(&hd->pendingQ, MPT_DONE_Q);
	spin_lock_init(&hd->freedoneQlock);
Linus Torvalds's avatar
Linus Torvalds committed
1561

1562 1563 1564 1565 1566 1567
	mem = hd->memQ;
	for (ii=0; ii < sh->can_queue; ii++) {
		freedoneQ = (MPT_DONE_Q *) mem;
		Q_ADD_TAIL(&hd->freeQ.head, freedoneQ, MPT_DONE_Q);
		mem += sizeof(MPT_DONE_Q);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1568

1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
	/* Initialize this Scsi_Host
	 * internal task Q.
	 */
	Q_INIT(&hd->taskQ, MPT_FRAME_HDR);
	hd->taskQcnt = 0;
		
	/* Allocate memory for the device structures.
	 * A non-Null pointer at an offset
	 * indicates a device exists.
	 * max_id = 1 + maximum id (hosts.h)
	 */
	sz = sh->max_id * sizeof(void *);
	mem = kmalloc(sz, GFP_ATOMIC);
	if (mem == NULL) {
		error = -ENOMEM;
		goto mptscsih_probe_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1586

1587 1588
	memset(mem, 0, sz);
	hd->Targets = (VirtDevice **) mem;
Linus Torvalds's avatar
Linus Torvalds committed
1589

1590 1591
	dprintk((KERN_INFO
	  "  Targets @ %p, sz=%d\n", hd->Targets, sz));
Pam Delaney's avatar
Pam Delaney committed
1592

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
	/* Clear the TM flags
	 */
	hd->tmPending = 0;
	hd->tmState = TM_STATE_NONE;
	hd->resetPending = 0;
	hd->abortSCpnt = NULL;
	hd->tmPtr = NULL;
	hd->numTMrequests = 0;
		
	/* Clear the pointer used to store
	 * single-threaded commands, i.e., those
	 * issued during a bus scan, dv and
	 * configuration pages.
	 */
	hd->cmdPtr = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1608

1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
	/* Initialize this SCSI Hosts' timers
	 * To use, set the timer expires field
	 * and add_timer
	 */
	init_timer(&hd->timer);
	hd->timer.data = (unsigned long) hd;
	hd->timer.function = mptscsih_timer_expired;

	init_timer(&hd->TMtimer);
	hd->TMtimer.data = (unsigned long) hd;
	hd->TMtimer.function = mptscsih_taskmgmt_timeout;
	hd->qtag_tick = jiffies;

	/* Moved Earlier Pam D */
	/* ioc->sh = sh;	*/

#ifdef MPTSCSIH_DBG_TIMEOUT
	hd->ioc->timeout_hard = 0;
	hd->ioc->timeout_delta = 30 * HZ;
	hd->ioc->timeout_maxcnt = 0;
	hd->ioc->timeout_cnt = 0;
	for (ii=0; ii < 8; ii++)
		foo_to[ii] = NULL;
#endif
	if (hd->is_spi) {
		/* Update with the driver setup
		 * values.
		 */
		if (hd->ioc->spi_data.maxBusWidth >
		  driver_setup.max_width) {
			hd->ioc->spi_data.maxBusWidth =
			  driver_setup.max_width;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1642

1643 1644 1645 1646 1647
		if (hd->ioc->spi_data.minSyncFactor <
		  driver_setup.min_sync_fac) {
			hd->ioc->spi_data.minSyncFactor =
			  driver_setup.min_sync_fac;
		}
Pam Delaney's avatar
Pam Delaney committed
1648

1649 1650 1651
		if (hd->ioc->spi_data.minSyncFactor == MPT_ASYNC) {
			hd->ioc->spi_data.maxSyncOffset = 0;
		}
Pam Delaney's avatar
Pam Delaney committed
1652

1653
		hd->ioc->spi_data.Saf_Te = driver_setup.saf_te;
Pam Delaney's avatar
Pam Delaney committed
1654

1655
		hd->negoNvram = 0;
James Bottomley's avatar
James Bottomley committed
1656
#ifndef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
1657
		hd->negoNvram = MPT_SCSICFG_USE_NVRAM;
Linus Torvalds's avatar
Linus Torvalds committed
1658
#endif
1659 1660 1661
		if (driver_setup.dv == 0) {
			hd->negoNvram = MPT_SCSICFG_USE_NVRAM;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1662

1663 1664 1665 1666 1667
		hd->ioc->spi_data.forceDv = 0;
		for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
			hd->ioc->spi_data.dvStatus[ii] =
			  MPT_SCSICFG_NEGOTIATE;
		}
James Bottomley's avatar
James Bottomley committed
1668

1669 1670 1671 1672 1673
		if (hd->negoNvram == 0) {
			for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++)
				hd->ioc->spi_data.dvStatus[ii] |=
				  MPT_SCSICFG_DV_NOT_DONE;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1674

1675 1676 1677 1678 1679 1680 1681
		ddvprintk((MYIOC_s_INFO_FMT
			"dv %x width %x factor %x saf_te %x\n",
			hd->ioc->name, driver_setup.dv,
			driver_setup.max_width,
			driver_setup.min_sync_fac,
			driver_setup.saf_te));
	}
Linus Torvalds's avatar
Linus Torvalds committed
1682

1683
	mpt_scsi_hosts++;
Linus Torvalds's avatar
Linus Torvalds committed
1684

1685 1686 1687 1688 1689 1690
	error = scsi_add_host (sh, &ioc->pcidev->dev);
	if(error) {
		dprintk((KERN_ERR MYNAM
		  "scsi_add_host failed\n"));
		goto mptscsih_probe_failed;
	}
1691

1692 1693
	scsi_scan_host(sh);
	return 0;
1694

James Bottomley's avatar
James Bottomley committed
1695 1696 1697
mptscsih_probe_failed:

	mptscsih_remove(pdev);
1698
	return error;
1699

Linus Torvalds's avatar
Linus Torvalds committed
1700 1701 1702
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
James Bottomley's avatar
James Bottomley committed
1703 1704 1705
/*
 *	mptscsih_remove - Removed scsi devices
 *	@pdev: Pointer to pci_dev structure
Linus Torvalds's avatar
Linus Torvalds committed
1706 1707 1708
 *
 *
 */
1709
static void
James Bottomley's avatar
James Bottomley committed
1710
mptscsih_remove(struct pci_dev *pdev)
Linus Torvalds's avatar
Linus Torvalds committed
1711
{
James Bottomley's avatar
James Bottomley committed
1712 1713 1714 1715 1716 1717 1718 1719
	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
	struct Scsi_Host 	*host = ioc->sh;
	MPT_SCSI_HOST		*hd;
	int 		 	count;
	unsigned long	 	flags;

	if(!host)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1720

James Bottomley's avatar
James Bottomley committed
1721
	scsi_remove_host(host);
Linus Torvalds's avatar
Linus Torvalds committed
1722

James Bottomley's avatar
James Bottomley committed
1723
#ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
Pam Delaney's avatar
Pam Delaney committed
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
	/* Check DV thread active */
	count = 10 * HZ;
	spin_lock_irqsave(&dvtaskQ_lock, flags);
	if (dvtaskQ_active) {
		spin_unlock_irqrestore(&dvtaskQ_lock, flags);
		while(dvtaskQ_active && --count) {
			set_current_state(TASK_INTERRUPTIBLE);
			schedule_timeout(1);
		}
	} else {
		spin_unlock_irqrestore(&dvtaskQ_lock, flags);
	}
	if (!count)
		printk(KERN_ERR MYNAM ": ERROR - DV thread still active!\n");
#if defined(MPT_DEBUG_DV) || defined(MPT_DEBUG_DV_TINY)
	else
		printk(KERN_ERR MYNAM ": DV thread orig %d, count %d\n", 10 * HZ, count);
#endif
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1743

James Bottomley's avatar
James Bottomley committed
1744
	hd = (MPT_SCSI_HOST *)host->hostdata;
Pam Delaney's avatar
Pam Delaney committed
1745 1746
	if (hd != NULL) {
		int sz1, sz2, sz3, sztarget=0;
1747 1748
		int szr2chain = 0;
		int szc2chain = 0;
Pam Delaney's avatar
Pam Delaney committed
1749
		int szQ = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1750

James Bottomley's avatar
James Bottomley committed
1751
		mptscsih_shutdown(&pdev->dev);
Linus Torvalds's avatar
Linus Torvalds committed
1752

Pam Delaney's avatar
Pam Delaney committed
1753
		sz1 = sz2 = sz3 = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1754

Pam Delaney's avatar
Pam Delaney committed
1755 1756 1757 1758
		if (hd->ScsiLookup != NULL) {
			sz1 = hd->ioc->req_depth * sizeof(void *);
			kfree(hd->ScsiLookup);
			hd->ScsiLookup = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1759
		}
Linus Torvalds's avatar
Linus Torvalds committed
1760

Pam Delaney's avatar
Pam Delaney committed
1761
		if (hd->ReqToChain != NULL) {
1762
			szr2chain = hd->ioc->req_depth * sizeof(int);
Pam Delaney's avatar
Pam Delaney committed
1763 1764 1765
			kfree(hd->ReqToChain);
			hd->ReqToChain = NULL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1766

Pam Delaney's avatar
Pam Delaney committed
1767
		if (hd->ChainToChain != NULL) {
1768
			szc2chain = hd->num_chain * sizeof(int);
Pam Delaney's avatar
Pam Delaney committed
1769 1770 1771
			kfree(hd->ChainToChain);
			hd->ChainToChain = NULL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1772

Pam Delaney's avatar
Pam Delaney committed
1773 1774 1775 1776 1777
		if (hd->memQ != NULL) {
			szQ = host->can_queue * sizeof(MPT_DONE_Q);
			kfree(hd->memQ);
			hd->memQ = NULL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1778

Pam Delaney's avatar
Pam Delaney committed
1779 1780
		if (hd->Targets != NULL) {
			int max, ii;
Linus Torvalds's avatar
Linus Torvalds committed
1781

Pam Delaney's avatar
Pam Delaney committed
1782 1783 1784 1785 1786 1787
			/*
			 * Free any target structures that were allocated.
			 */
			if (hd->is_spi) {
				max = MPT_MAX_SCSI_DEVICES;
			} else {
1788
				max = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
Pam Delaney's avatar
Pam Delaney committed
1789 1790 1791 1792 1793 1794 1795 1796
			}
			for (ii=0; ii < max; ii++) {
				if (hd->Targets[ii]) {
					kfree(hd->Targets[ii]);
					hd->Targets[ii] = NULL;
					sztarget += sizeof(VirtDevice);
				}
			}
Linus Torvalds's avatar
Linus Torvalds committed
1797

Pam Delaney's avatar
Pam Delaney committed
1798 1799 1800 1801 1802 1803 1804
			/*
			 * Free pointer array.
			 */
			sz3 = max * sizeof(void *);
			kfree(hd->Targets);
			hd->Targets = NULL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1805

James Bottomley's avatar
James Bottomley committed
1806 1807 1808
		dprintk((MYIOC_s_INFO_FMT
		  "Free'd ScsiLookup (%d), chain (%d) and Target (%d+%d) memory\n",
		  hd->ioc->name, sz1, szchain, sz3, sztarget));
Pam Delaney's avatar
Pam Delaney committed
1809
		dprintk(("Free'd done and free Q (%d) memory\n", szQ));
James Bottomley's avatar
James Bottomley committed
1810 1811 1812 1813

		/* NULL the Scsi_Host pointer
		 */
		hd->ioc->sh = NULL;
Pam Delaney's avatar
Pam Delaney committed
1814
	}
James Bottomley's avatar
James Bottomley committed
1815 1816

	scsi_host_put(host);
1817
	mpt_scsi_hosts--;
James Bottomley's avatar
James Bottomley committed
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838

}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_shutdown - reboot notifier
 *
 */
static void
mptscsih_shutdown(struct device * dev)
{
	MPT_ADAPTER 		*ioc = pci_get_drvdata(to_pci_dev(dev));
	struct Scsi_Host 	*host = ioc->sh;
	MPT_SCSI_HOST		*hd;

	if(!host)
		return;

	hd = (MPT_SCSI_HOST *)host->hostdata;

	/* Flush the cache of this adapter
Pam Delaney's avatar
Pam Delaney committed
1839
	 */
James Bottomley's avatar
James Bottomley committed
1840 1841
	if(hd != NULL)
		mptscsih_synchronize_cache(hd, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1842

James Bottomley's avatar
James Bottomley committed
1843
}
Linus Torvalds's avatar
Linus Torvalds committed
1844

James Bottomley's avatar
James Bottomley committed
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
#ifdef CONFIG_PM
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_suspend - Fusion MPT scsie driver suspend routine.
 *
 *
 */
static int
mptscsih_suspend(struct pci_dev *pdev, u32 state)
{
	mptscsih_shutdown(&pdev->dev);
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1858

James Bottomley's avatar
James Bottomley committed
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_resume - Fusion MPT scsi driver resume routine.
 *
 *
 */
static int
mptscsih_resume(struct pci_dev *pdev)
{
	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
	struct Scsi_Host 	*host = ioc->sh;
	MPT_SCSI_HOST		*hd;
Pam Delaney's avatar
Pam Delaney committed
1871

James Bottomley's avatar
James Bottomley committed
1872 1873
	if(!host)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1874

James Bottomley's avatar
James Bottomley committed
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
	hd = (MPT_SCSI_HOST *)host->hostdata;
	if(!hd)
		return 0;

#ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
	{
	unsigned long lflags;
	spin_lock_irqsave(&dvtaskQ_lock, lflags);
	if (!dvtaskQ_active) {
		dvtaskQ_active = 1;
		spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
		MPT_INIT_WORK(&mptscsih_dvTask,
		  mptscsih_domainValidation, (void *) hd);
		SCHEDULE_TASK(&mptscsih_dvTask);
	} else {
		spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
	}
	}
#endif
Pam Delaney's avatar
Pam Delaney committed
1894
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1895 1896
}

James Bottomley's avatar
James Bottomley committed
1897 1898 1899 1900
#endif

static struct mpt_pci_driver mptscsih_driver = {
	.probe		= mptscsih_probe,
1901
	.remove		= mptscsih_remove,
James Bottomley's avatar
James Bottomley committed
1902 1903 1904 1905 1906 1907 1908 1909 1910
	.shutdown	= mptscsih_shutdown,
#ifdef CONFIG_PM
	.suspend	= mptscsih_suspend,
	.resume		= mptscsih_resume,
#endif
};


/*  SCSI host fops start here...  */
Linus Torvalds's avatar
Linus Torvalds committed
1911 1912
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
James Bottomley's avatar
James Bottomley committed
1913 1914
 *	mptscsih_init - Register MPT adapter(s) as SCSI host(s) with
 *	linux scsi mid-layer.
Linus Torvalds's avatar
Linus Torvalds committed
1915
 *
James Bottomley's avatar
James Bottomley committed
1916
 *	Returns 0 for success, non-zero for failure.
Linus Torvalds's avatar
Linus Torvalds committed
1917
 */
1918 1919
static int __init
mptscsih_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
1920 1921
{

James Bottomley's avatar
James Bottomley committed
1922
	show_mptmod_ver(my_NAME, my_VERSION);
Linus Torvalds's avatar
Linus Torvalds committed
1923

James Bottomley's avatar
James Bottomley committed
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
	ScsiDoneCtx = mpt_register(mptscsih_io_done, MPTSCSIH_DRIVER);
	ScsiTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSCSIH_DRIVER);
	ScsiScanDvCtx = mpt_register(mptscsih_scandv_complete, MPTSCSIH_DRIVER);

	if (mpt_event_register(ScsiDoneCtx, mptscsih_event_process) == 0) {
		dprintk((KERN_INFO MYNAM
		  ": Registered for IOC event notifications\n"));
	}

	if (mpt_reset_register(ScsiDoneCtx, mptscsih_ioc_reset) == 0) {
		dprintk((KERN_INFO MYNAM
		  ": Registered for IOC reset notifications\n"));
	}

#ifdef MODULE
	/* Evaluate the command line arguments, if any */
	if (mptscsih)
		mptscsih_setup(mptscsih);
#endif

1944 1945 1946 1947
	if(mpt_device_driver_register(&mptscsih_driver,
	  MPTSCSIH_DRIVER) != 0 ) {
		dprintk((KERN_INFO MYNAM
		": failed to register dd callbacks\n"));
Linus Torvalds's avatar
Linus Torvalds committed
1948 1949
	}

1950
	return 0;
James Bottomley's avatar
James Bottomley committed
1951 1952 1953 1954 1955 1956 1957 1958 1959

}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_exit - Unregisters MPT adapter(s)
 *
 */
1960 1961
static void __exit
mptscsih_exit(void)
James Bottomley's avatar
James Bottomley committed
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
{
	MPT_ADAPTER	*ioc;

	/* removing devices */
	for(ioc = mpt_adapter_find_first(); ioc != NULL;
	  ioc = mpt_adapter_find_next(ioc)) {
		if ((ioc->last_state != MPI_IOC_STATE_OPERATIONAL) ||
		  (ioc->sh == NULL))
			continue;
		mptscsih_remove(ioc->pcidev);
	}

	mpt_reset_deregister(ScsiDoneCtx);
	dprintk((KERN_INFO MYNAM
	  ": Deregistered for IOC reset notifications\n"));

	mpt_event_deregister(ScsiDoneCtx);
	dprintk((KERN_INFO MYNAM
	  ": Deregistered for IOC event notifications\n"));

	mpt_device_driver_deregister(MPTSCSIH_DRIVER);
	mpt_deregister(ScsiScanDvCtx);
	mpt_deregister(ScsiTaskCtx);
	mpt_deregister(ScsiDoneCtx);

	if (info_kbuf != NULL)
		kfree(info_kbuf);

Pam Delaney's avatar
Pam Delaney committed
1990
}
Linus Torvalds's avatar
Linus Torvalds committed
1991

Pam Delaney's avatar
Pam Delaney committed
1992 1993
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
1994 1995
 *	mptscsih_info - Return information about MPT adapter
 *	@SChost: Pointer to Scsi_Host structure
Linus Torvalds's avatar
Linus Torvalds committed
1996
 *
1997
 *	(linux Scsi_Host_Template.info routine)
Linus Torvalds's avatar
Linus Torvalds committed
1998
 *
1999
 *	Returns pointer to buffer where information was written.
Linus Torvalds's avatar
Linus Torvalds committed
2000
 */
2001 2002
const char *
mptscsih_info(struct Scsi_Host *SChost)
Linus Torvalds's avatar
Linus Torvalds committed
2003
{
2004
	MPT_SCSI_HOST *h;
2005
	int size = 0;
Pam Delaney's avatar
Pam Delaney committed
2006

2007 2008 2009
	if (info_kbuf == NULL)
		if ((info_kbuf = kmalloc(0x1000 /* 4Kb */, GFP_KERNEL)) == NULL)
			return info_kbuf;
Pam Delaney's avatar
Pam Delaney committed
2010

2011 2012 2013 2014 2015 2016
	h = (MPT_SCSI_HOST *)SChost->hostdata;
	info_kbuf[0] = '\0';
	if (h) {
		mpt_print_ioc_summary(h->ioc, info_kbuf, &size, 0, 0);
		info_kbuf[size-1] = '\0';
	}
Pam Delaney's avatar
Pam Delaney committed
2017

2018 2019
	return info_kbuf;
}
Pam Delaney's avatar
Pam Delaney committed
2020

2021 2022 2023 2024 2025 2026
struct info_str {
	char *buffer;
	int   length;
	int   offset;
	int   pos;
};
Pam Delaney's avatar
Pam Delaney committed
2027

2028 2029 2030 2031
static void copy_mem_info(struct info_str *info, char *data, int len)
{
	if (info->pos + len > info->length)
		len = info->length - info->pos;
Pam Delaney's avatar
Pam Delaney committed
2032

2033 2034 2035
	if (info->pos + len < info->offset) {
		info->pos += len;
		return;
Linus Torvalds's avatar
Linus Torvalds committed
2036 2037
	}

2038 2039 2040
	if (info->pos < info->offset) {
	        data += (info->offset - info->pos);
	        len  -= (info->offset - info->pos);
Linus Torvalds's avatar
Linus Torvalds committed
2041 2042
	}

2043 2044 2045
	if (len > 0) {
                memcpy(info->buffer + info->pos, data, len);
                info->pos += len;
Linus Torvalds's avatar
Linus Torvalds committed
2046
	}
2047
}
Linus Torvalds's avatar
Linus Torvalds committed
2048

2049 2050 2051 2052 2053
static int copy_info(struct info_str *info, char *fmt, ...)
{
	va_list args;
	char buf[81];
	int len;
Linus Torvalds's avatar
Linus Torvalds committed
2054

2055 2056 2057
	va_start(args, fmt);
	len = vsprintf(buf, fmt, args);
	va_end(args);
Linus Torvalds's avatar
Linus Torvalds committed
2058

2059 2060 2061
	copy_mem_info(info, buf, len);
	return len;
}
Linus Torvalds's avatar
Linus Torvalds committed
2062

2063 2064 2065
static int mptscsih_host_info(MPT_ADAPTER *ioc, char *pbuf, off_t offset, int len)
{
	struct info_str info;
Linus Torvalds's avatar
Linus Torvalds committed
2066

2067 2068 2069 2070
	info.buffer	= pbuf;
	info.length	= len;
	info.offset	= offset;
	info.pos	= 0;
Linus Torvalds's avatar
Linus Torvalds committed
2071

2072 2073 2074 2075 2076 2077 2078 2079
	copy_info(&info, "%s: %s, ", ioc->name, ioc->prod_name);
	copy_info(&info, "%s%08xh, ", MPT_FW_REV_MAGIC_ID_STRING, ioc->facts.FWVersion.Word);
	copy_info(&info, "Ports=%d, ", ioc->facts.NumberOfPorts);
	copy_info(&info, "MaxQ=%d\n", ioc->req_depth);

	return ((info.pos > info.offset) ? info.pos - info.offset : 0);
}

2080 2081
#ifndef MPTSCSIH_DBG_TIMEOUT
static int mptscsih_user_command(MPT_ADAPTER *ioc, char *pbuf, int len)
2082
{
2083 2084
	/* Not yet implemented */
	return len;
2085
}
2086
#else
2087 2088 2089
#define is_digit(c)	((c) >= '0' && (c) <= '9')
#define digit_to_bin(c)	((c) - '0')
#define is_space(c)	((c) == ' ' || (c) == '\t')
Linus Torvalds's avatar
Linus Torvalds committed
2090

2091 2092 2093
#define UC_DBG_TIMEOUT		0x01
#define UC_DBG_HARDRESET	0x02

2094 2095 2096
static int skip_spaces(char *ptr, int len)
{
	int cnt, c;
Linus Torvalds's avatar
Linus Torvalds committed
2097

2098
	for (cnt = len; cnt > 0 && (c = *ptr++) && is_space(c); cnt --);
Linus Torvalds's avatar
Linus Torvalds committed
2099

2100 2101
	return (len - cnt);
}
Linus Torvalds's avatar
Linus Torvalds committed
2102

2103 2104 2105 2106 2107 2108
static int get_int_arg(char *ptr, int len, ulong *pv)
{
	int cnt, c;
	ulong	v;
	for (v =  0, cnt = len; cnt > 0 && (c=*ptr++) && is_digit(c); cnt --) {
		v = (v * 10) + digit_to_bin(c);
Pam Delaney's avatar
Pam Delaney committed
2109
	}
Linus Torvalds's avatar
Linus Torvalds committed
2110

2111 2112
	if (pv)
		*pv = v;
Linus Torvalds's avatar
Linus Torvalds committed
2113

2114 2115
	return (len - cnt);
}
Pam Delaney's avatar
Pam Delaney committed
2116

Linus Torvalds's avatar
Linus Torvalds committed
2117

2118 2119 2120
static int is_keyword(char *ptr, int len, char *verb)
{
	int verb_len = strlen(verb);
Linus Torvalds's avatar
Linus Torvalds committed
2121

2122 2123 2124 2125 2126
	if (len >= strlen(verb) && !memcmp(verb, ptr, verb_len))
		return verb_len;
	else
		return 0;
}
Pam Delaney's avatar
Pam Delaney committed
2127

2128 2129 2130 2131 2132
#define SKIP_SPACES(min_spaces)						\
	if ((arg_len = skip_spaces(ptr,len)) < (min_spaces))		\
		return -EINVAL;						\
	ptr += arg_len;							\
	len -= arg_len;
Linus Torvalds's avatar
Linus Torvalds committed
2133

2134 2135 2136 2137 2138 2139 2140 2141
#define GET_INT_ARG(v)							\
	if (!(arg_len = get_int_arg(ptr,len, &(v))))			\
		return -EINVAL;						\
	ptr += arg_len;							\
	len -= arg_len;

static int mptscsih_user_command(MPT_ADAPTER *ioc, char *buffer, int length)
{
2142 2143 2144 2145 2146 2147 2148
	char *ptr = buffer;
	char btmp[24];	/* REMOVE */
	int arg_len;
	int len	= length;
	int cmd;
	ulong number = 1;
	ulong delta = 10;
2149 2150 2151

	if ((len > 0) && (ptr[len -1] == '\n'))
		--len;
Pam Delaney's avatar
Pam Delaney committed
2152

2153 2154 2155 2156 2157 2158 2159 2160 2161
	if (len < 22) {
		strncpy(btmp, buffer, len);
		btmp[len+1]='\0';
	} else {
		strncpy(btmp, buffer, 22);
		btmp[23]='\0';
	}
	printk("user_command:  ioc %d, buffer %s, length %d\n",
			ioc->id, btmp, length);
Linus Torvalds's avatar
Linus Torvalds committed
2162

2163 2164 2165 2166 2167
	if ((arg_len = is_keyword(ptr, len, "timeout")) != 0)
		cmd = UC_DBG_TIMEOUT;
	else if ((arg_len = is_keyword(ptr, len, "hardreset")) != 0)
		cmd = UC_DBG_HARDRESET;
	else
2168
		return -EINVAL;
2169

2170 2171
	ptr += arg_len;
	len -= arg_len;
Linus Torvalds's avatar
Linus Torvalds committed
2172

2173 2174 2175 2176
	switch(cmd) {
		case UC_DBG_TIMEOUT:
			SKIP_SPACES(1);
			GET_INT_ARG(number);
2177
			SKIP_SPACES(1);
2178
			GET_INT_ARG(delta);
2179 2180
			break;
	}
2181

2182
	printk("user_command: cnt=%ld delta=%ld\n", number, delta);
Linus Torvalds's avatar
Linus Torvalds committed
2183

2184 2185 2186
	if (len)
		return -EINVAL;
	else {
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
		if (cmd == UC_DBG_HARDRESET) {
			ioc->timeout_hard = 1;
		} else if (cmd == UC_DBG_TIMEOUT) {
			/* process this command ...
			 */
			ioc->timeout_maxcnt = 0;
			ioc->timeout_delta = delta < 2 ? 2 : delta;
			ioc->timeout_cnt = 0;
			ioc->timeout_maxcnt = number < 8 ? number: 8;
		}
2197 2198 2199 2200
	}
	/* Not yet implemented */
	return length;
}
2201
#endif
Pam Delaney's avatar
Pam Delaney committed
2202

2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_proc_info - Return information about MPT adapter
 *
 *	(linux Scsi_Host_Template.info routine)
 *
 * 	buffer: if write, user data; if read, buffer for user
 * 	length: if write, return length;
 * 	offset: if write, 0; if read, the current offset into the buffer from
 * 		the previous read.
 * 	hostno: scsi host number
 *	func:   if write = 1; if read = 0
 */
2216 2217
int mptscsih_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
			int length, int func)
2218
{
2219
	MPT_ADAPTER	*ioc;
2220 2221
	MPT_SCSI_HOST	*hd = NULL;
	int size = 0;
Pam Delaney's avatar
Pam Delaney committed
2222

2223
	dprintk(("Called mptscsih_proc_info: hostno=%d, func=%d\n", host->host_no, func));
2224 2225 2226 2227
	dprintk(("buffer %p, start=%p (%p) offset=%ld length = %d\n",
			buffer, start, *start, offset, length));

	for (ioc = mpt_adapter_find_first(); ioc != NULL; ioc = mpt_adapter_find_next(ioc)) {
2228
		if ((ioc->sh) && (ioc->sh == host)) {
2229 2230
			hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
2231
		}
2232 2233 2234 2235 2236 2237
	}
	if ((ioc == NULL) || (ioc->sh == NULL) || (hd == NULL))
		return 0;

	if (func) {
		size = mptscsih_user_command(ioc, buffer, length);
Pam Delaney's avatar
Pam Delaney committed
2238
	} else {
2239 2240 2241 2242
		if (start)
			*start = buffer;

		size = mptscsih_host_info(ioc, buffer, offset, length);
Linus Torvalds's avatar
Linus Torvalds committed
2243 2244
	}

2245 2246
	return size;
}
Linus Torvalds's avatar
Linus Torvalds committed
2247

Pam Delaney's avatar
Pam Delaney committed
2248

2249 2250 2251
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
#define ADD_INDEX_LOG(req_ent)	do { } while(0)

Linus Torvalds's avatar
Linus Torvalds committed
2252
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2253 2254
/**
 *	mptscsih_qcmd - Primary Fusion MPT SCSI initiator IO start routine.
Pam Delaney's avatar
Pam Delaney committed
2255
 *	@SCpnt: Pointer to Scsi_Cmnd structure
2256
 *	@done: Pointer SCSI mid-layer IO completion function
Linus Torvalds's avatar
Linus Torvalds committed
2257
 *
2258 2259 2260 2261 2262
 *	(linux Scsi_Host_Template.queuecommand routine)
 *	This is the primary SCSI IO start routine.  Create a MPI SCSIIORequest
 *	from a linux Scsi_Cmnd request and send it to the IOC.
 *
 *	Returns 0. (rtn value discarded by linux scsi mid-layer)
Linus Torvalds's avatar
Linus Torvalds committed
2263
 */
2264 2265
int
mptscsih_qcmd(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
Linus Torvalds's avatar
Linus Torvalds committed
2266
{
2267 2268 2269 2270
	MPT_SCSI_HOST		*hd;
	MPT_FRAME_HDR		*mf;
	SCSIIORequest_t		*pScsiReq;
	VirtDevice		*pTarget;
2271
	MPT_DONE_Q		*buffer;
2272 2273 2274 2275 2276 2277 2278 2279 2280
	unsigned long		 flags;
	int	 target;
	int	 lun;
	int	 datadir;
	u32	 datalen;
	u32	 scsictl;
	u32	 scsidir;
	u32	 cmd_len;
	int	 my_idx;
Pam Delaney's avatar
Pam Delaney committed
2281
	int	 ii;
2282 2283 2284
	int	 rc;
	int	 did_errcode;
	int	 issueCmd;
Pam Delaney's avatar
Pam Delaney committed
2285

2286
	did_errcode = 0;
2287
	hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata;
2288 2289
	target = SCpnt->device->id;
	lun = SCpnt->device->lun;
2290
	SCpnt->scsi_done = done;
Linus Torvalds's avatar
Linus Torvalds committed
2291

2292
	pTarget = hd->Targets[target];
Linus Torvalds's avatar
Linus Torvalds committed
2293

2294 2295 2296 2297 2298
	dmfprintk((MYIOC_s_INFO_FMT "qcmd: SCpnt=%p, done()=%p\n",
			(hd && hd->ioc) ? hd->ioc->name : "ioc?", SCpnt, done));

	if (hd->resetPending) {
		/* Prevent new commands from being issued
2299 2300 2301
		 * while reloading the FW. Reset timer to 60 seconds,
		 * as the FW can take some time to come ready.
		 * For New EH, cmds on doneQ posted to FW.
2302 2303
		 */
		did_errcode = 1;
2304 2305 2306
		mod_timer(&SCpnt->eh_timeout, jiffies + (HZ * 60));
		dtmprintk((MYIOC_s_WARN_FMT "qcmd: SCpnt=%p timeout + 60HZ\n",
			(hd && hd->ioc) ? hd->ioc->name : "ioc?", SCpnt));
2307
		goto did_error;
Linus Torvalds's avatar
Linus Torvalds committed
2308 2309
	}

2310 2311
	/*
	 *  Put together a MPT SCSI request...
Pam Delaney's avatar
Pam Delaney committed
2312
	 */
2313 2314 2315 2316 2317 2318
	if ((mf = mpt_get_msg_frame(ScsiDoneCtx, hd->ioc->id)) == NULL) {
		dprintk((MYIOC_s_WARN_FMT "QueueCmd, no msg frames!!\n",
				hd->ioc->name));
		did_errcode = 2;
		goto did_error;
	}
Pam Delaney's avatar
Pam Delaney committed
2319

2320
	pScsiReq = (SCSIIORequest_t *) mf;
Pam Delaney's avatar
Pam Delaney committed
2321

2322
	my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
Pam Delaney's avatar
Pam Delaney committed
2323

2324
	ADD_INDEX_LOG(my_idx);
Pam Delaney's avatar
Pam Delaney committed
2325

2326 2327 2328
	/*
	 *  The scsi layer should be handling this stuff
	 *  (In 2.3.x it does -DaveM)
Pam Delaney's avatar
Pam Delaney committed
2329 2330
	 */

2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
	/*  BUG FIX!  19991030 -sralston
	 *    TUR's being issued with scsictl=0x02000000 (DATA_IN)!
	 *    Seems we may receive a buffer (datalen>0) even when there
	 *    will be no data transfer!  GRRRRR...
	 */
	datadir = mptscsih_io_direction(SCpnt);
	if (datadir == SCSI_DATA_READ) {
		datalen = SCpnt->request_bufflen;
		scsidir = MPI_SCSIIO_CONTROL_READ;	/* DATA IN  (host<--ioc<--dev) */
	} else if (datadir == SCSI_DATA_WRITE) {
		datalen = SCpnt->request_bufflen;
		scsidir = MPI_SCSIIO_CONTROL_WRITE;	/* DATA OUT (host-->ioc-->dev) */
	} else {
		datalen = 0;
		scsidir = MPI_SCSIIO_CONTROL_NODATATRANSFER;
Linus Torvalds's avatar
Linus Torvalds committed
2346 2347
	}

2348 2349 2350
	/* Default to untagged. Once a target structure has been allocated,
	 * use the Inquiry data to determine if device supports tagged.
	 */
2351 2352 2353 2354 2355 2356
	if (   pTarget
	    && (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)
	    && (SCpnt->device->tagged_supported)) {
		scsictl = scsidir | MPI_SCSIIO_CONTROL_SIMPLEQ;
	} else {
		scsictl = scsidir | MPI_SCSIIO_CONTROL_UNTAGGED;
2357
	}
Linus Torvalds's avatar
Linus Torvalds committed
2358

2359 2360
	/* Use the above information to set up the message frame
	 */
2361 2362
	pScsiReq->TargetID = (u8) target;
	pScsiReq->Bus = (u8) SCpnt->device->channel;
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
	pScsiReq->ChainOffset = 0;
	pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
	pScsiReq->CDBLength = SCpnt->cmd_len;
	pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
	pScsiReq->Reserved = 0;
	pScsiReq->MsgFlags = mpt_msg_flags();
	pScsiReq->LUN[0] = 0;
	pScsiReq->LUN[1] = lun;
	pScsiReq->LUN[2] = 0;
	pScsiReq->LUN[3] = 0;
	pScsiReq->LUN[4] = 0;
	pScsiReq->LUN[5] = 0;
	pScsiReq->LUN[6] = 0;
	pScsiReq->LUN[7] = 0;
	pScsiReq->Control = cpu_to_le32(scsictl);
Pam Delaney's avatar
Pam Delaney committed
2378

2379 2380 2381 2382 2383 2384
	/*
	 *  Write SCSI CDB into the message
	 */
	cmd_len = SCpnt->cmd_len;
	for (ii=0; ii < cmd_len; ii++)
		pScsiReq->CDB[ii] = SCpnt->cmnd[ii];
Linus Torvalds's avatar
Linus Torvalds committed
2385

2386 2387 2388
	for (ii=cmd_len; ii < 16; ii++)
		pScsiReq->CDB[ii] = 0;

2389 2390
	/* DataLength */
	pScsiReq->DataLength = cpu_to_le32(datalen);
Pam Delaney's avatar
Pam Delaney committed
2391

2392 2393 2394
	/* SenseBuffer low address */
	pScsiReq->SenseBufferLowAddr = cpu_to_le32(hd->ioc->sense_buf_low_dma
					   + (my_idx * MPT_SENSE_BUFFER_ALLOC));
Pam Delaney's avatar
Pam Delaney committed
2395

2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
	/* Now add the SG list
	 * Always have a SGE even if null length.
	 */
	rc = SUCCESS;
	if (datalen == 0) {
		/* Add a NULL SGE */
		mptscsih_add_sge((char *)&pScsiReq->SGL, MPT_SGE_FLAGS_SSIMPLE_READ | 0,
			(dma_addr_t) -1);
	} else {
		/* Add a 32 or 64 bit SGE */
		rc = mptscsih_AddSGE(hd, SCpnt, pScsiReq, my_idx);
	}
Linus Torvalds's avatar
Linus Torvalds committed
2408 2409


2410 2411 2412
	if (rc == SUCCESS) {
		hd->ScsiLookup[my_idx] = SCpnt;
		SCpnt->host_scribble = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
2413

2414 2415 2416 2417
		/* SCSI specific processing */
		issueCmd = 1;
		if (hd->is_spi) {
			int dvStatus = hd->ioc->spi_data.dvStatus[target];
Pam Delaney's avatar
Pam Delaney committed
2418

2419
			if (dvStatus || hd->ioc->spi_data.forceDv) {
Pam Delaney's avatar
Pam Delaney committed
2420

2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
				/* Write SDP1 on this I/O to this target */
				if (dvStatus & MPT_SCSICFG_NEGOTIATE) {
					mptscsih_writeSDP1(hd, 0, target, hd->negoNvram);
					dvStatus &= ~MPT_SCSICFG_NEGOTIATE;
					hd->ioc->spi_data.dvStatus[target] =  dvStatus;
				} else if (dvStatus & MPT_SCSICFG_BLK_NEGO) {
					mptscsih_writeSDP1(hd, 0, target, MPT_SCSICFG_BLK_NEGO);
					dvStatus &= ~MPT_SCSICFG_BLK_NEGO;
					hd->ioc->spi_data.dvStatus[target] =  dvStatus;
				}
Pam Delaney's avatar
Pam Delaney committed
2431

James Bottomley's avatar
James Bottomley committed
2432
#ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
2433 2434 2435 2436 2437 2438 2439 2440 2441
				if ((dvStatus & MPT_SCSICFG_NEED_DV) ||
					(hd->ioc->spi_data.forceDv & MPT_SCSICFG_NEED_DV)) {
					unsigned long lflags;
					/* Schedule DV if necessary */
					spin_lock_irqsave(&dvtaskQ_lock, lflags);
					if (!dvtaskQ_active) {
						dvtaskQ_active = 1;
						spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
						MPT_INIT_WORK(&mptscsih_dvTask, mptscsih_domainValidation, (void *) hd);
Pam Delaney's avatar
Pam Delaney committed
2442

2443 2444 2445 2446 2447 2448
						SCHEDULE_TASK(&mptscsih_dvTask);
					} else {
						spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
					}
					hd->ioc->spi_data.forceDv &= ~MPT_SCSICFG_NEED_DV;
				}
Pam Delaney's avatar
Pam Delaney committed
2449

2450 2451 2452 2453 2454 2455 2456
				/* Trying to do DV to this target, extend timeout.
				 * Wait to issue intil flag is clear
				 */
				if (dvStatus & MPT_SCSICFG_DV_PENDING) {
					mod_timer(&SCpnt->eh_timeout, jiffies + 40 * HZ);
					issueCmd = 0;
				}
Linus Torvalds's avatar
Linus Torvalds committed
2457

2458 2459 2460 2461 2462 2463 2464
				/* Set the DV flags.
				 */
				if (dvStatus & MPT_SCSICFG_DV_NOT_DONE)
					mptscsih_set_dvflags(hd, pScsiReq);
#endif
			}
		}
Linus Torvalds's avatar
Linus Torvalds committed
2465

2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
#ifdef MPTSCSIH_DBG_TIMEOUT
		if (hd->ioc->timeout_cnt < hd->ioc->timeout_maxcnt) {
			foo_to[hd->ioc->timeout_cnt] = SCpnt;
			hd->ioc->timeout_cnt++;
			//mod_timer(&SCpnt->eh_timeout, jiffies + hd->ioc->timeout_delta);
			issueCmd = 0;
			printk(MYIOC_s_WARN_FMT
				"to pendingQ: (sc=%p, mf=%p, time=%ld)\n",
				hd->ioc->name, SCpnt, mf, jiffies);
		}
#endif

2478
		if (issueCmd) {
2479
			mpt_put_msg_frame(ScsiDoneCtx, hd->ioc->id, mf);
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
			dmfprintk((MYIOC_s_INFO_FMT "Issued SCSI cmd (%p) mf=%p idx=%d\n",
					hd->ioc->name, SCpnt, mf, my_idx));
		} else {
			ddvtprintk((MYIOC_s_INFO_FMT "Pending cmd=%p idx %d\n",
					hd->ioc->name, SCpnt, my_idx));
			/* Place this command on the pendingQ if possible */
			spin_lock_irqsave(&hd->freedoneQlock, flags);
			if (!Q_IS_EMPTY(&hd->freeQ)) {
				buffer = hd->freeQ.head;
				Q_DEL_ITEM(buffer);
Linus Torvalds's avatar
Linus Torvalds committed
2490

2491 2492 2493
				/* Save the mf pointer
				 */
				buffer->argp = (void *)mf;
Linus Torvalds's avatar
Linus Torvalds committed
2494

2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
				/* Add to the pendingQ
				 */
				Q_ADD_TAIL(&hd->pendingQ.head, buffer, MPT_DONE_Q);
				spin_unlock_irqrestore(&hd->freedoneQlock, flags);
			} else {
				spin_unlock_irqrestore(&hd->freedoneQlock, flags);
				SCpnt->result = (DID_BUS_BUSY << 16);
				SCpnt->scsi_done(SCpnt);
			}
		}
	} else {
		mptscsih_freeChainBuffers(hd, my_idx);
		mpt_free_msg_frame(ScsiDoneCtx, hd->ioc->id, mf);
		did_errcode = 3;
		goto did_error;
Pam Delaney's avatar
Pam Delaney committed
2510
	}
Linus Torvalds's avatar
Linus Torvalds committed
2511

2512
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2513

2514 2515 2516 2517 2518 2519 2520
did_error:
	dprintk((MYIOC_s_WARN_FMT "_qcmd did_errcode=%d (sc=%p)\n",
			hd->ioc->name, did_errcode, SCpnt));
	/* Just wish OS to issue a retry */
	SCpnt->result = (DID_BUS_BUSY << 16);
	spin_lock_irqsave(&hd->freedoneQlock, flags);
	if (!Q_IS_EMPTY(&hd->freeQ)) {
2521 2522
		dtmprintk((MYIOC_s_WARN_FMT "SCpnt=%p to doneQ\n",
			(hd && hd->ioc) ? hd->ioc->name : "ioc?", SCpnt));
2523 2524
		buffer = hd->freeQ.head;
		Q_DEL_ITEM(buffer);
Linus Torvalds's avatar
Linus Torvalds committed
2525

2526 2527 2528
		/* Set the Scsi_Cmnd pointer
		 */
		buffer->argp = (void *)SCpnt;
Pam Delaney's avatar
Pam Delaney committed
2529

2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
		/* Add to the doneQ
		 */
		Q_ADD_TAIL(&hd->doneQ.head, buffer, MPT_DONE_Q);
		spin_unlock_irqrestore(&hd->freedoneQlock, flags);
	} else {
		spin_unlock_irqrestore(&hd->freedoneQlock, flags);
		SCpnt->scsi_done(SCpnt);
	}

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2540 2541 2542
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
2543 2544 2545 2546 2547 2548 2549 2550 2551
/*
 *	mptscsih_freeChainBuffers - Function to free chain buffers associated
 *	with a SCSI IO request
 *	@hd: Pointer to the MPT_SCSI_HOST instance
 *	@req_idx: Index of the SCSI IO request frame.
 *
 *	Called if SG chain buffer allocation fails and mptscsih callbacks.
 *	No return.
 */
Linus Torvalds's avatar
Linus Torvalds committed
2552
static void
Pam Delaney's avatar
Pam Delaney committed
2553
mptscsih_freeChainBuffers(MPT_SCSI_HOST *hd, int req_idx)
Linus Torvalds's avatar
Linus Torvalds committed
2554
{
2555
	MPT_FRAME_HDR *chain;
Pam Delaney's avatar
Pam Delaney committed
2556 2557 2558
	unsigned long flags;
	int chain_idx;
	int next;
Linus Torvalds's avatar
Linus Torvalds committed
2559

Pam Delaney's avatar
Pam Delaney committed
2560 2561 2562 2563 2564
	/* Get the first chain index and reset
	 * tracker state.
	 */
	chain_idx = hd->ReqToChain[req_idx];
	hd->ReqToChain[req_idx] = MPT_HOST_NO_CHAIN;
Linus Torvalds's avatar
Linus Torvalds committed
2565

Pam Delaney's avatar
Pam Delaney committed
2566
	while (chain_idx != MPT_HOST_NO_CHAIN) {
Linus Torvalds's avatar
Linus Torvalds committed
2567

Pam Delaney's avatar
Pam Delaney committed
2568 2569
		/* Save the next chain buffer index */
		next = hd->ChainToChain[chain_idx];
Linus Torvalds's avatar
Linus Torvalds committed
2570

Pam Delaney's avatar
Pam Delaney committed
2571 2572 2573 2574
		/* Free this chain buffer and reset
		 * tracker
		 */
		hd->ChainToChain[chain_idx] = MPT_HOST_NO_CHAIN;
Linus Torvalds's avatar
Linus Torvalds committed
2575

Pam Delaney's avatar
Pam Delaney committed
2576 2577 2578 2579 2580 2581
		chain = (MPT_FRAME_HDR *) (hd->ChainBuffer
					+ (chain_idx * hd->ioc->req_sz));
		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
		Q_ADD_TAIL(&hd->FreeChainQ.head,
					&chain->u.frame.linkage, MPT_FRAME_HDR);
		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2582

Pam Delaney's avatar
Pam Delaney committed
2583 2584 2585 2586 2587 2588
		dmfprintk((MYIOC_s_INFO_FMT "FreeChainBuffers (index %d)\n",
				hd->ioc->name, chain_idx));

		/* handle next */
		chain_idx = next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2589 2590 2591 2592
	return;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617
/*
 *	Reset Handling
 */

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_TMHandler - Generic handler for SCSI Task Management.
 *	Fall through to mpt_HardResetHandler if: not operational, too many
 *	failed TM requests or handshake failure.
 *
 *	@ioc: Pointer to MPT_ADAPTER structure
 *	@type: Task Management type
 *	@target: Logical Target ID for reset (if appropriate)
 *	@lun: Logical Unit for reset (if appropriate)
 *	@ctx2abort: Context for the task to be aborted (if appropriate)
 *	@sleepFlag: If set, use udelay instead of schedule in handshake code.
 *
 *	Remark: Currently invoked from a non-interrupt thread (_bh).
 *
 *	Remark: With old EH code, at most 1 SCSI TaskMgmt function per IOC
 *	will be active.
 *
 *	Returns 0 for SUCCESS or -1 if FAILED.
 */
static int
2618
mptscsih_TMHandler(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout, int sleepFlag)
Linus Torvalds's avatar
Linus Torvalds committed
2619
{
2620
	MPT_ADAPTER	*ioc;
Pam Delaney's avatar
Pam Delaney committed
2621 2622 2623 2624
	int		 rc = -1;
	int		 doTask = 1;
	u32		 ioc_raw_state;
	unsigned long	 flags;
Linus Torvalds's avatar
Linus Torvalds committed
2625

Pam Delaney's avatar
Pam Delaney committed
2626 2627 2628 2629 2630
	/* If FW is being reloaded currently, return success to
	 * the calling function.
	 */
	if (hd == NULL)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2631

Pam Delaney's avatar
Pam Delaney committed
2632 2633 2634
	ioc = hd->ioc;
	if (ioc == NULL) {
		printk(KERN_ERR MYNAM " TMHandler" " NULL ioc!\n");
2635
		return FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
2636
	}
2637
	dtmprintk((MYIOC_s_INFO_FMT "TMHandler Entered!\n", ioc->name));
Linus Torvalds's avatar
Linus Torvalds committed
2638

Pam Delaney's avatar
Pam Delaney committed
2639 2640 2641 2642 2643
	// SJR - CHECKME - Can we avoid this here?
	// (mpt_HardResetHandler has this check...)
	spin_lock_irqsave(&ioc->diagLock, flags);
	if ((ioc->diagPending) || (ioc->alt_ioc && ioc->alt_ioc->diagPending)) {
		spin_unlock_irqrestore(&ioc->diagLock, flags);
2644
		return FAILED;
Pam Delaney's avatar
Pam Delaney committed
2645 2646
	}
	spin_unlock_irqrestore(&ioc->diagLock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2647

Pam Delaney's avatar
Pam Delaney committed
2648 2649 2650 2651 2652
	/* Do not do a Task Management if there are
	 * too many failed TMs on this adapter.
	 */
	if (hd->numTMrequests > MPT_HOST_TOO_MANY_TM)
		doTask = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2653

2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684
	/*  Wait a fixed amount of time for the TM pending flag to be cleared.
	 *  If we time out and not bus reset, then we return a FAILED status to the caller.
	 *  The call to mptscsih_tm_pending_wait() will set the pending flag if we are
	 *  successful. Otherwise, reload the FW.
	 */
	if (mptscsih_tm_pending_wait(hd) == FAILED) {
		if (type == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
			dtmprintk((KERN_WARNING MYNAM ": %s: TMHandler abort: "
			   "Timed out waiting for last TM (%d) to complete! \n",
			   hd->ioc->name, hd->tmPending));
			return FAILED;
		} else if (type == MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
			dtmprintk((KERN_WARNING MYNAM ": %s: TMHandler target reset: "
			   "Timed out waiting for last TM (%d) to complete! \n",
			   hd->ioc->name, hd->tmPending));
			return FAILED;
		} else if (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) {
			dtmprintk((KERN_WARNING MYNAM ": %s: TMHandler bus reset: "
			   "Timed out waiting for last TM (%d) to complete! \n",
			   hd->ioc->name, hd->tmPending));
			if (hd->tmPending & (1 << MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS))
				return FAILED;

			doTask = 0;
		}
	} else {
		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
		hd->tmPending |=  (1 << type);
		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
	}

Pam Delaney's avatar
Pam Delaney committed
2685 2686 2687
	/* Is operational?
	 */
	ioc_raw_state = mpt_GetIocState(hd->ioc, 0);
Linus Torvalds's avatar
Linus Torvalds committed
2688

Pam Delaney's avatar
Pam Delaney committed
2689 2690
#ifdef MPT_DEBUG_RESET
	if ((ioc_raw_state & MPI_IOC_STATE_MASK) != MPI_IOC_STATE_OPERATIONAL) {
2691
		printk(MYIOC_s_WARN_FMT
2692
			"TM Handler: IOC Not operational(0x%x)!\n",
Pam Delaney's avatar
Pam Delaney committed
2693 2694
			hd->ioc->name, ioc_raw_state);
	}
Linus Torvalds's avatar
Linus Torvalds committed
2695 2696
#endif

Pam Delaney's avatar
Pam Delaney committed
2697 2698
	if (doTask && ((ioc_raw_state & MPI_IOC_STATE_MASK) == MPI_IOC_STATE_OPERATIONAL)
				&& !(ioc_raw_state & MPI_DOORBELL_ACTIVE)) {
Linus Torvalds's avatar
Linus Torvalds committed
2699

Pam Delaney's avatar
Pam Delaney committed
2700 2701
		/* Isse the Task Mgmt request.
		 */
2702 2703
		if (hd->hard_resets < -1)
			hd->hard_resets++;
2704
		rc = mptscsih_IssueTaskMgmt(hd, type, channel, target, lun, ctx2abort, timeout, sleepFlag);
Pam Delaney's avatar
Pam Delaney committed
2705 2706 2707
		if (rc) {
			printk(MYIOC_s_INFO_FMT "Issue of TaskMgmt failed!\n", hd->ioc->name);
		} else {
2708
			dtmprintk((MYIOC_s_INFO_FMT "Issue of TaskMgmt Successful!\n", hd->ioc->name));
Pam Delaney's avatar
Pam Delaney committed
2709 2710
		}
	}
2711 2712 2713 2714

#ifdef MPTSCSIH_DBG_TIMEOUT
	if (hd->ioc->timeout_hard)
		rc = 1;
Pam Delaney's avatar
Pam Delaney committed
2715
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2716

2717 2718 2719 2720 2721
	/* Only fall through to the HRH if this is a bus reset
	 */
	if ((type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) && (rc ||
		ioc->reload_fw || (ioc->alt_ioc && ioc->alt_ioc->reload_fw))) {
		dtmprintk((MYIOC_s_INFO_FMT "Calling HardReset! \n",
Pam Delaney's avatar
Pam Delaney committed
2722 2723
			 hd->ioc->name));
		rc = mpt_HardResetHandler(hd->ioc, sleepFlag);
Linus Torvalds's avatar
Linus Torvalds committed
2724 2725
	}

Pam Delaney's avatar
Pam Delaney committed
2726 2727 2728
	dtmprintk((MYIOC_s_INFO_FMT "TMHandler rc = %d!\n", hd->ioc->name, rc));

	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
2729 2730
}

Pam Delaney's avatar
Pam Delaney committed
2731

Linus Torvalds's avatar
Linus Torvalds committed
2732
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
/*
 *	mptscsih_IssueTaskMgmt - Generic send Task Management function.
 *	@hd: Pointer to MPT_SCSI_HOST structure
 *	@type: Task Management type
 *	@target: Logical Target ID for reset (if appropriate)
 *	@lun: Logical Unit for reset (if appropriate)
 *	@ctx2abort: Context for the task to be aborted (if appropriate)
 *	@sleepFlag: If set, use udelay instead of schedule in handshake code.
 *
 *	Remark: _HardResetHandler can be invoked from an interrupt thread (timer)
 *	or a non-interrupt thread.  In the former, must not call schedule().
 *
 *	Not all fields are meaningfull for all task types.
 *
 *	Returns 0 for SUCCESS, -999 for "no msg frames",
 *	else other non-zero value returned.
 */
Linus Torvalds's avatar
Linus Torvalds committed
2750
static int
2751
mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout, int sleepFlag)
Linus Torvalds's avatar
Linus Torvalds committed
2752
{
Pam Delaney's avatar
Pam Delaney committed
2753 2754 2755 2756
	MPT_FRAME_HDR	*mf;
	SCSITaskMgmt_t	*pScsiTm;
	int		 ii;
	int		 retval;
Linus Torvalds's avatar
Linus Torvalds committed
2757

Pam Delaney's avatar
Pam Delaney committed
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
	/* Return Fail to calling function if no message frames available.
	 */
	if ((mf = mpt_get_msg_frame(ScsiTaskCtx, hd->ioc->id)) == NULL) {
		dtmprintk((MYIOC_s_WARN_FMT "IssueTaskMgmt, no msg frames!!\n",
				hd->ioc->name));
		//return FAILED;
		return -999;
	}
	dtmprintk((MYIOC_s_INFO_FMT "IssueTaskMgmt request @ %p\n",
			hd->ioc->name, mf));
Linus Torvalds's avatar
Linus Torvalds committed
2768

Pam Delaney's avatar
Pam Delaney committed
2769 2770 2771 2772
	/* Format the Request
	 */
	pScsiTm = (SCSITaskMgmt_t *) mf;
	pScsiTm->TargetID = target;
2773
	pScsiTm->Bus = channel;
Pam Delaney's avatar
Pam Delaney committed
2774 2775
	pScsiTm->ChainOffset = 0;
	pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
Linus Torvalds's avatar
Linus Torvalds committed
2776

Pam Delaney's avatar
Pam Delaney committed
2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
	pScsiTm->Reserved = 0;
	pScsiTm->TaskType = type;
	pScsiTm->Reserved1 = 0;
	pScsiTm->MsgFlags = (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS)
	                    ? MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION : 0;

	for (ii= 0; ii < 8; ii++) {
		pScsiTm->LUN[ii] = 0;
	}
	pScsiTm->LUN[1] = lun;

	for (ii=0; ii < 7; ii++)
		pScsiTm->Reserved2[ii] = 0;

	pScsiTm->TaskMsgContext = ctx2abort;
	dtmprintk((MYIOC_s_INFO_FMT "IssueTaskMgmt, ctx2abort (0x%08x), type (%d)\n",
			hd->ioc->name, ctx2abort, type));

	/* MPI v0.10 requires SCSITaskMgmt requests be sent via Doorbell/handshake
		mpt_put_msg_frame(hd->ioc->id, mf);
	* Save the MF pointer in case the request times out.
	*/
	hd->tmPtr = mf;
	hd->numTMrequests++;
James Bottomley's avatar
James Bottomley committed
2801
	hd->TMtimer.expires = jiffies + timeout;
Pam Delaney's avatar
Pam Delaney committed
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
	add_timer(&hd->TMtimer);

	if ((retval = mpt_send_handshake_request(ScsiTaskCtx, hd->ioc->id,
				sizeof(SCSITaskMgmt_t), (u32*)pScsiTm, sleepFlag))
	!= 0) {
		dtmprintk((MYIOC_s_WARN_FMT "_send_handshake FAILED!"
			" (hd %p, ioc %p, mf %p) \n", hd->ioc->name, hd, hd->ioc, mf));
		hd->numTMrequests--;
		hd->tmPtr = NULL;
		del_timer(&hd->TMtimer);
		mpt_free_msg_frame(ScsiTaskCtx, hd->ioc->id, mf);
	}

	return retval;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_abort - Abort linux Scsi_Cmnd routine, new_eh variant
 *	@SCpnt: Pointer to Scsi_Cmnd structure, IO to be aborted
 *
 *	(linux Scsi_Host_Template.eh_abort_handler routine)
 *
 *	Returns SUCCESS or FAILED.
 */
int
mptscsih_abort(Scsi_Cmnd * SCpnt)
{
	MPT_SCSI_HOST	*hd;
	MPT_FRAME_HDR	*mf;
	u32		 ctx2abort;
	int		 scpnt_idx;
2834
	spinlock_t	*host_lock = SCpnt->device->host->host_lock;
Pam Delaney's avatar
Pam Delaney committed
2835 2836 2837

	/* If we can't locate our host adapter structure, return FAILED status.
	 */
2838
	if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL) {
Pam Delaney's avatar
Pam Delaney committed
2839 2840
		SCpnt->result = DID_RESET << 16;
		SCpnt->scsi_done(SCpnt);
2841
		dtmprintk((KERN_WARNING MYNAM ": mptscsih_abort: "
Pam Delaney's avatar
Pam Delaney committed
2842 2843 2844 2845 2846
			   "Can't locate host! (sc=%p)\n",
			   SCpnt));
		return FAILED;
	}

2847 2848 2849 2850 2851
	if (hd->resetPending)
		return FAILED;

	printk(KERN_WARNING MYNAM ": %s: >> Attempting task abort! (sc=%p)\n",
	       hd->ioc->name, SCpnt);
2852 2853 2854

	if (hd->timeouts < -1)
		hd->timeouts++;
Pam Delaney's avatar
Pam Delaney committed
2855 2856 2857 2858 2859 2860

	/* Find this command
	 */
	if ((scpnt_idx = SCPNT_TO_LOOKUP_IDX(SCpnt)) < 0) {
		/* Cmd not found in ScsiLookup. If found in
		 * doneQ, delete from Q. Do OS callback.
Linus Torvalds's avatar
Linus Torvalds committed
2861
		 */
Pam Delaney's avatar
Pam Delaney committed
2862 2863 2864
		search_doneQ_for_cmd(hd, SCpnt);

		SCpnt->result = DID_RESET << 16;
2865
		dtmprintk((KERN_WARNING MYNAM ": %s: mptscsih_abort: "
Pam Delaney's avatar
Pam Delaney committed
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
			   "Command not in the active list! (sc=%p)\n",
			   hd->ioc->name, SCpnt));
		return SUCCESS;
	}

	/* If this command is pended, then timeout/hang occurred
	 * during DV. Post command and flush pending Q
	 * and then following up with the reset request.
	 */
	if ((mf = mptscsih_search_pendingQ(hd, scpnt_idx)) != NULL) {
2876
		mpt_put_msg_frame(ScsiDoneCtx, hd->ioc->id, mf);
Pam Delaney's avatar
Pam Delaney committed
2877
		post_pendingQ_commands(hd);
2878
		dtmprintk((KERN_WARNING MYNAM ": %s: mptscsih_abort: "
2879
			   "Posting pended cmd! (sc=%p)\n",
Pam Delaney's avatar
Pam Delaney committed
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893
			   hd->ioc->name, SCpnt));
	}

	/* Most important!  Set TaskMsgContext to SCpnt's MsgContext!
	 * (the IO to be ABORT'd)
	 *
	 * NOTE: Since we do not byteswap MsgContext, we do not
	 *	 swap it here either.  It is an opaque cookie to
	 *	 the controller, so it does not matter. -DaveM
	 */
	mf = MPT_INDEX_2_MFPTR(hd->ioc, scpnt_idx);
	ctx2abort = mf->u.frame.hwhdr.msgctxu.MsgContext;

	hd->abortSCpnt = SCpnt;
2894 2895

	spin_unlock_irq(host_lock);
Pam Delaney's avatar
Pam Delaney committed
2896
	if (mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
2897
		SCpnt->device->channel, SCpnt->device->id, SCpnt->device->lun,
James Bottomley's avatar
James Bottomley committed
2898
		ctx2abort, (HZ*2) /* 2 second timeout */,CAN_SLEEP)
2899
		< 0) {
Pam Delaney's avatar
Pam Delaney committed
2900 2901 2902

		/* The TM request failed and the subsequent FW-reload failed!
		 * Fatal error case.
Linus Torvalds's avatar
Linus Torvalds committed
2903
		 */
Pam Delaney's avatar
Pam Delaney committed
2904 2905
		printk(MYIOC_s_WARN_FMT "Error issuing abort task! (sc=%p)\n",
		       hd->ioc->name, SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
2906

Pam Delaney's avatar
Pam Delaney committed
2907 2908 2909 2910 2911
		/* We must clear our pending flag before clearing our state.
		 */
		hd->tmPending = 0;
		hd->tmState = TM_STATE_NONE;

2912
		spin_lock_irq(host_lock);
Pam Delaney's avatar
Pam Delaney committed
2913
		return FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
2914
	}
2915
	spin_lock_irq(host_lock);
2916
	return FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
2917 2918 2919 2920

}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
2921 2922 2923
/**
 *	mptscsih_dev_reset - Perform a SCSI TARGET_RESET!  new_eh variant
 *	@SCpnt: Pointer to Scsi_Cmnd structure, IO which reset is due to
Linus Torvalds's avatar
Linus Torvalds committed
2924
 *
Pam Delaney's avatar
Pam Delaney committed
2925 2926 2927
 *	(linux Scsi_Host_Template.eh_dev_reset_handler routine)
 *
 *	Returns SUCCESS or FAILED.
Linus Torvalds's avatar
Linus Torvalds committed
2928
 */
Pam Delaney's avatar
Pam Delaney committed
2929 2930 2931 2932
int
mptscsih_dev_reset(Scsi_Cmnd * SCpnt)
{
	MPT_SCSI_HOST	*hd;
2933
	spinlock_t	*host_lock = SCpnt->device->host->host_lock;
Linus Torvalds's avatar
Linus Torvalds committed
2934

Pam Delaney's avatar
Pam Delaney committed
2935 2936
	/* If we can't locate our host adapter structure, return FAILED status.
	 */
2937
	if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
2938
		dtmprintk((KERN_WARNING MYNAM ": mptscsih_dev_reset: "
Pam Delaney's avatar
Pam Delaney committed
2939 2940 2941 2942
			   "Can't locate host! (sc=%p)\n",
			   SCpnt));
		return FAILED;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2943

2944 2945 2946 2947 2948
	if (hd->resetPending)
		return FAILED;

	printk(KERN_WARNING MYNAM ": %s: >> Attempting target reset! (sc=%p)\n",
	       hd->ioc->name, SCpnt);
2949

2950
	/* Unsupported for SCSI. Supported for FCP
2951 2952 2953
	 */
	if (hd->is_spi)
		return FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
2954

2955
	spin_unlock_irq(host_lock);
Pam Delaney's avatar
Pam Delaney committed
2956
	if (mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
2957 2958
		SCpnt->device->channel, SCpnt->device->id,
		0, 0, (HZ*5) /* 5 second timeout */, CAN_SLEEP)
Pam Delaney's avatar
Pam Delaney committed
2959 2960 2961 2962 2963 2964 2965 2966
		< 0){
		/* The TM request failed and the subsequent FW-reload failed!
		 * Fatal error case.
		 */
		printk(MYIOC_s_WARN_FMT "Error processing TaskMgmt request (sc=%p)\n",
		 		hd->ioc->name, SCpnt);
		hd->tmPending = 0;
		hd->tmState = TM_STATE_NONE;
2967
		spin_lock_irq(host_lock);
Pam Delaney's avatar
Pam Delaney committed
2968 2969
		return FAILED;
	}
2970
	spin_lock_irq(host_lock);
Pam Delaney's avatar
Pam Delaney committed
2971
	return SUCCESS;
2972

Pam Delaney's avatar
Pam Delaney committed
2973
}
Linus Torvalds's avatar
Linus Torvalds committed
2974 2975

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
2976 2977 2978 2979 2980 2981 2982
/**
 *	mptscsih_bus_reset - Perform a SCSI BUS_RESET!	new_eh variant
 *	@SCpnt: Pointer to Scsi_Cmnd structure, IO which reset is due to
 *
 *	(linux Scsi_Host_Template.eh_bus_reset_handler routine)
 *
 *	Returns SUCCESS or FAILED.
Linus Torvalds's avatar
Linus Torvalds committed
2983
 */
Pam Delaney's avatar
Pam Delaney committed
2984 2985 2986 2987
int
mptscsih_bus_reset(Scsi_Cmnd * SCpnt)
{
	MPT_SCSI_HOST	*hd;
2988
	spinlock_t	*host_lock = SCpnt->device->host->host_lock;
Pam Delaney's avatar
Pam Delaney committed
2989 2990 2991

	/* If we can't locate our host adapter structure, return FAILED status.
	 */
2992
	if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
2993
		dtmprintk((KERN_WARNING MYNAM ": mptscsih_bus_reset: "
Pam Delaney's avatar
Pam Delaney committed
2994 2995 2996 2997 2998
			   "Can't locate host! (sc=%p)\n",
			   SCpnt ) );
		return FAILED;
	}

2999 3000
	printk(KERN_WARNING MYNAM ": %s: >> Attempting bus reset! (sc=%p)\n",
	       hd->ioc->name, SCpnt);
3001 3002 3003

	if (hd->timeouts < -1)
		hd->timeouts++;
Pam Delaney's avatar
Pam Delaney committed
3004 3005

	/* We are now ready to execute the task management request. */
3006 3007 3008 3009
	spin_unlock_irq(host_lock);
//	printk("testing start : mptscsih_schedule_reset\n");
//	mptscsih_schedule_reset(hd);
//	printk("testing end: mptscsih_schedule_reset\n");
Pam Delaney's avatar
Pam Delaney committed
3010
	if (mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
3011
		SCpnt->device->channel, 0, 0, 0, (HZ*5) /* 5 second timeout */, CAN_SLEEP)
Pam Delaney's avatar
Pam Delaney committed
3012 3013 3014 3015 3016
	    < 0){

		/* The TM request failed and the subsequent FW-reload failed!
		 * Fatal error case.
		 */
3017
		printk(MYIOC_s_WARN_FMT
Pam Delaney's avatar
Pam Delaney committed
3018 3019 3020 3021
		       "Error processing TaskMgmt request (sc=%p)\n",
		       hd->ioc->name, SCpnt);
		hd->tmPending = 0;
		hd->tmState = TM_STATE_NONE;
3022
		spin_lock_irq(host_lock);
Pam Delaney's avatar
Pam Delaney committed
3023 3024
		return FAILED;
	}
3025
	spin_lock_irq(host_lock);
Pam Delaney's avatar
Pam Delaney committed
3026 3027
	return SUCCESS;
}
Linus Torvalds's avatar
Linus Torvalds committed
3028 3029

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
3030 3031 3032 3033 3034 3035 3036 3037
/**
 *	mptscsih_host_reset - Perform a SCSI host adapter RESET!
 *	new_eh variant
 *	@SCpnt: Pointer to Scsi_Cmnd structure, IO which reset is due to
 *
 *	(linux Scsi_Host_Template.eh_host_reset_handler routine)
 *
 *	Returns SUCCESS or FAILED.
Linus Torvalds's avatar
Linus Torvalds committed
3038
 */
Pam Delaney's avatar
Pam Delaney committed
3039 3040 3041 3042 3043
int
mptscsih_host_reset(Scsi_Cmnd *SCpnt)
{
	MPT_SCSI_HOST *  hd;
	int              status = SUCCESS;
3044
	spinlock_t	*host_lock = SCpnt->device->host->host_lock;
Pam Delaney's avatar
Pam Delaney committed
3045 3046

	/*  If we can't locate the host to reset, then we failed. */
3047
	if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
3048
		dtmprintk( ( KERN_WARNING MYNAM ": mptscsih_host_reset: "
Pam Delaney's avatar
Pam Delaney committed
3049 3050 3051 3052
			     "Can't locate host! (sc=%p)\n",
			     SCpnt ) );
		return FAILED;
	}
Linus Torvalds's avatar
Linus Torvalds committed
3053

Pam Delaney's avatar
Pam Delaney committed
3054 3055 3056 3057 3058 3059
	printk(KERN_WARNING MYNAM ": %s: >> Attempting host reset! (sc=%p)\n",
	       hd->ioc->name, SCpnt);

	/*  If our attempts to reset the host failed, then return a failed
	 *  status.  The host will be taken off line by the SCSI mid-layer.
	 */
3060 3061
	spin_unlock_irq(host_lock);
	if (mpt_HardResetHandler(hd->ioc, CAN_SLEEP) < 0){
Pam Delaney's avatar
Pam Delaney committed
3062 3063
		status = FAILED;
	} else {
3064 3065
		/*  Make sure TM pending is cleared and TM state is set to
		 *  NONE.
Pam Delaney's avatar
Pam Delaney committed
3066 3067 3068 3069
		 */
		hd->tmPending = 0;
		hd->tmState = TM_STATE_NONE;
	}
3070
	spin_lock_irq(host_lock);
Pam Delaney's avatar
Pam Delaney committed
3071 3072


3073
	dtmprintk( ( KERN_WARNING MYNAM ": mptscsih_host_reset: "
Pam Delaney's avatar
Pam Delaney committed
3074 3075 3076 3077 3078 3079 3080 3081
		     "Status = %s\n",
		     (status == SUCCESS) ? "SUCCESS" : "FAILED" ) );

	return status;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
3082
 *	mptscsih_tm_pending_wait - wait for pending task management request to
Pam Delaney's avatar
Pam Delaney committed
3083 3084 3085 3086 3087 3088 3089 3090 3091
 *		complete.
 *	@hd: Pointer to MPT host structure.
 *
 *	Returns {SUCCESS,FAILED}.
 */
static int
mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd)
{
	unsigned long  flags;
James Bottomley's avatar
James Bottomley committed
3092
	int            loop_count = 10 * 4;  /* Wait 10 seconds */
Pam Delaney's avatar
Pam Delaney committed
3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104
	int            status = FAILED;

	do {
		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
		if (hd->tmState == TM_STATE_NONE) {
			hd->tmState = TM_STATE_IN_PROGRESS;
			hd->tmPending = 1;
			spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
			status = SUCCESS;
			break;
		}
		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
3105 3106
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(HZ/4);
Pam Delaney's avatar
Pam Delaney committed
3107 3108 3109 3110 3111 3112 3113
	} while (--loop_count);

	return status;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
3114 3115 3116 3117
 *	mptscsih_taskmgmt_complete - Registered with Fusion MPT base driver
 *	@ioc: Pointer to MPT_ADAPTER structure
 *	@mf: Pointer to SCSI task mgmt request frame
 *	@mr: Pointer to SCSI task mgmt reply frame
Pam Delaney's avatar
Pam Delaney committed
3118
 *
3119 3120 3121 3122
 *	This routine is called from mptbase.c::mpt_interrupt() at the completion
 *	of any SCSI task management request.
 *	This routine is registered with the MPT (base) driver at driver
 *	load/init time via the mpt_register() API call.
Pam Delaney's avatar
Pam Delaney committed
3123
 *
3124
 *	Returns 1 indicating alloc'd request frame ptr should be freed.
Pam Delaney's avatar
Pam Delaney committed
3125
 */
3126 3127
static int
mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
Pam Delaney's avatar
Pam Delaney committed
3128
{
3129 3130
	SCSITaskMgmtReply_t	*pScsiTmReply;
	SCSITaskMgmt_t		*pScsiTmReq;
3131
	MPT_SCSI_HOST		*hd;
Pam Delaney's avatar
Pam Delaney committed
3132
	unsigned long		 flags;
3133
	u8			 tmType = 0;
Pam Delaney's avatar
Pam Delaney committed
3134

3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
	dtmprintk((MYIOC_s_INFO_FMT "SCSI TaskMgmt completed (mf=%p,r=%p)\n",
			ioc->name, mf, mr));
	if (ioc->sh) {
		/* Depending on the thread, a timer is activated for
		 * the TM request.  Delete this timer on completion of TM.
		 * Decrement count of outstanding TM requests.
		 */
		hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
		if (hd->tmPtr) {
			del_timer(&hd->TMtimer);
		}
		dtmprintk((MYIOC_s_INFO_FMT "taskQcnt (%d)\n",
			ioc->name, hd->taskQcnt));
	} else {
		dtmprintk((MYIOC_s_WARN_FMT "TaskMgmt Complete: NULL Scsi Host Ptr\n",
			ioc->name));
		return 1;
Pam Delaney's avatar
Pam Delaney committed
3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181
	}

	if (mr == NULL) {
		dtmprintk((MYIOC_s_WARN_FMT "ERROR! TaskMgmt Reply: NULL Request %p\n",
			ioc->name, mf));
		return 1;
	} else {
		pScsiTmReply = (SCSITaskMgmtReply_t*)mr;
		pScsiTmReq = (SCSITaskMgmt_t*)mf;

		/* Figure out if this was ABORT_TASK, TARGET_RESET, or BUS_RESET! */
		tmType = pScsiTmReq->TaskType;

		dtmprintk((KERN_INFO "  TaskType = %d, TerminationCount=%d\n",
				tmType, le32_to_cpu(pScsiTmReply->TerminationCount)));

		/* Error?  (anything non-zero?) */
		if (*(u32 *)&pScsiTmReply->Reserved2[0]) {
			u16	 iocstatus;

			iocstatus = le16_to_cpu(pScsiTmReply->IOCStatus) & MPI_IOCSTATUS_MASK;
			dtmprintk((KERN_INFO "  SCSI TaskMgmt (%d) - Oops!\n", tmType));
			dtmprintk((KERN_INFO "  IOCStatus = %04xh\n", iocstatus));
			dtmprintk((KERN_INFO "  IOCLogInfo = %08xh\n",
				 le32_to_cpu(pScsiTmReply->IOCLogInfo)));

			/* clear flags and continue.
			 */
			if (tmType == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
				hd->abortSCpnt = NULL;
3182

Pam Delaney's avatar
Pam Delaney committed
3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
			/* If an internal command is present
			 * or the TM failed - reload the FW.
			 * FC FW may respond FAILED to an ABORT
			 */
			if (tmType == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) {
				if ((hd->cmdPtr) ||
				    (iocstatus == MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED)) {
					if (mpt_HardResetHandler(ioc, NO_SLEEP) < 0) {
						printk((KERN_WARNING
							" Firmware Reload FAILED!!\n"));
					}
				}
			}
		} else {
			dtmprintk((KERN_INFO "  SCSI TaskMgmt SUCCESS!\n"));

			hd->numTMrequests--;
			hd->abortSCpnt = NULL;
			flush_doneQ(hd);

		}
	}

	hd->tmPtr = NULL;
	spin_lock_irqsave(&ioc->FreeQlock, flags);
	hd->tmPending = 0;
	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
3210
	hd->tmState = TM_STATE_NONE;
Pam Delaney's avatar
Pam Delaney committed
3211 3212 3213 3214 3215 3216 3217 3218 3219

	return 1;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	This is anyones guess quite frankly.
 */
int
3220
mptscsih_bios_param(struct scsi_device * sdev, struct block_device *bdev,
James Bottomley's avatar
James Bottomley committed
3221
		sector_t capacity, int geom[])
3222
{
James Bottomley's avatar
James Bottomley committed
3223 3224 3225 3226 3227 3228
	int		heads;
	int		sectors;
	sector_t	cylinders;
#ifdef CONFIG_LBD
	ulong 		dummy;
#endif
Pam Delaney's avatar
Pam Delaney committed
3229

James Bottomley's avatar
James Bottomley committed
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253
	heads = 64;
	sectors = 32;
#ifdef CONFIG_LBD
	dummy = heads * sectors;
	cylinders = capacity;
	sector_div(cylinders,dummy);
#else
	cylinders = (ulong)capacity / (heads * sectors);
#endif

	/*
	 * Handle extended translation size for logical drives
	 * > 1Gb
	 */
	if ((ulong)capacity >= 0x200000) {
		heads = 255;
		sectors = 63;
#ifdef CONFIG_LBD
		dummy = heads * sectors;
		cylinders = capacity;
		sector_div(cylinders,dummy);
#else
		cylinders = (ulong)capacity / (heads * sectors);
#endif
Pam Delaney's avatar
Pam Delaney committed
3254
	}
James Bottomley's avatar
James Bottomley committed
3255 3256 3257 3258 3259 3260 3261 3262 3263 3264

	/* return result */
	geom[0] = heads;
	geom[1] = sectors;
	geom[2] = cylinders;

	dprintk((KERN_NOTICE
		": bios_param: Id=%i Lun=%i Channel=%i CHS=%i/%i/%i\n",
		sdev->id, sdev->lun,sdev->channel,(int)cylinders,heads,sectors));

Pam Delaney's avatar
Pam Delaney committed
3265 3266 3267 3268
	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284
/*
 *	OS entry point to allow host driver to alloc memory
 *	for each scsi device. Called once per device the bus scan.
 *	Return non-zero if allocation fails.
 *	Init memory once per id (not LUN).
 */
int
mptscsih_slave_alloc(Scsi_Device *device)
{
	struct Scsi_Host	*host = device->host;
	MPT_SCSI_HOST		*hd;
	VirtDevice		*vdev;

	hd = (MPT_SCSI_HOST *)host->hostdata;

	if (hd == NULL)
3285
		return -ENODEV;
3286 3287 3288 3289

	if ((vdev = hd->Targets[device->id]) == NULL) {
		if ((vdev = kmalloc(sizeof(VirtDevice), GFP_ATOMIC)) == NULL) {
			printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%d) FAILED!\n",
3290 3291
			hd->ioc->name, (int)sizeof(VirtDevice));
			return -ENOMEM;
3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
		} else {
			memset(vdev, 0, sizeof(VirtDevice));
			rwlock_init(&vdev->VdevLock);
			Q_INIT(&vdev->WaitQ, void);
			Q_INIT(&vdev->SentQ, void);
			Q_INIT(&vdev->DoneQ, void);
			vdev->tflags = 0;
			vdev->ioc_id = hd->ioc->id;
			vdev->target_id = device->id;
			vdev->bus_id = hd->port;

			hd->Targets[device->id] = vdev;
		}
	}
	vdev->num_luns++;

	return 0;
}

/*
 *	OS entry point to allow for host driver to free allocated memory
 *	Called if no device present or device being unloaded
 */
void
mptscsih_slave_destroy(Scsi_Device *device)
{
	struct Scsi_Host	*host = device->host;
	MPT_SCSI_HOST		*hd;
	VirtDevice		*vdev;
3321
	int 			raid_volume=0;
3322 3323

	hd = (MPT_SCSI_HOST *)host->hostdata;
3324

3325 3326 3327 3328 3329 3330 3331 3332 3333 3334
	if (hd == NULL)
		return;

	mptscsih_search_running_cmds(hd, device->id, device->lun);

	/* Free memory and reset all flags for this target
	 */
	if ((vdev = hd->Targets[device->id]) != NULL) {
		vdev->num_luns--;

3335 3336
		if (vdev->luns[0] & (1 << device->lun))
			vdev->luns[0] &= ~(1 << device->lun);
3337 3338 3339 3340 3341 3342 3343

		/* Free device structure only if number of luns is 0.
		 */
		if (vdev->num_luns == 0) {
			kfree(hd->Targets[device->id]);
			hd->Targets[device->id] = NULL;

3344 3345 3346 3347 3348 3349 3350 3351 3352
			if (!hd->is_spi) 
				return;

			if((hd->ioc->spi_data.isRaid) && (hd->ioc->spi_data.pIocPg3)) {
			int i;
				for(i=0;i<hd->ioc->spi_data.pIocPg3->NumPhysDisks &&
					raid_volume==0;i++)
						
					if(device->id == 
3353
					  hd->ioc->spi_data.pIocPg3->PhysDisk[i].PhysDiskID) {
3354 3355 3356 3357 3358 3359 3360
						raid_volume=1;
						hd->ioc->spi_data.forceDv |=
						  MPT_SCSICFG_RELOAD_IOC_PG3;
					}
			}

			if(!raid_volume){
3361 3362
				hd->ioc->spi_data.dvStatus[device->id] =
				MPT_SCSICFG_NEGOTIATE;
3363 3364

				if (hd->negoNvram == 0)
3365 3366
					hd->ioc->spi_data.dvStatus[device->id]
					|= MPT_SCSICFG_DV_NOT_DONE;
3367 3368 3369
			}
		}
	}
3370
	
3371 3372 3373
	return;
}

Pam Delaney's avatar
Pam Delaney committed
3374 3375 3376 3377
/*
 *	OS entry point to adjust the queue_depths on a per-device basis.
 *	Called once per device the bus scan. Use it to force the queue_depth
 *	member to 1 if a device does not support Q tags.
3378
 *	Return non-zero if fails.
Pam Delaney's avatar
Pam Delaney committed
3379
 */
3380 3381 3382 3383 3384 3385
int
mptscsih_slave_configure(Scsi_Device *device)
{
	struct Scsi_Host	*sh = device->host;
	VirtDevice		*pTarget;
	MPT_SCSI_HOST		*hd = (MPT_SCSI_HOST *)sh->hostdata;
3386

3387 3388 3389
	if ((hd == NULL) || (hd->Targets == NULL)) {
		return 0;
	}
3390

3391 3392 3393 3394 3395 3396 3397
	dsprintk((MYIOC_s_INFO_FMT
		"device @ %p, id=%d, LUN=%d, channel=%d\n",
		hd->ioc->name, device, device->id, device->lun, device->channel));
	dsprintk((MYIOC_s_INFO_FMT
		"sdtr %d wdtr %d ppr %d inq length=%d\n",
		hd->ioc->name, device->sdtr, device->wdtr,
		device->ppr, device->inquiry_len));
3398

3399 3400 3401 3402 3403
	if (device->id > sh->max_id) {
		/* error case, should never happen */
		scsi_adjust_queue_depth(device, 0, 1);
		goto slave_configure_exit;
	}
3404

3405
	pTarget = hd->Targets[device->id];
3406

3407 3408 3409 3410
	if (pTarget == NULL) {
		/* error case - don't know about this device */
		scsi_adjust_queue_depth(device, 0, 1);
		goto slave_configure_exit;
3411 3412
	}

3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
	mptscsih_initTarget(hd, device->channel, device->id, device->lun,
		device->inquiry, device->inquiry_len );
	scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
		MPT_SCSI_CMD_PER_DEV_HIGH);
	if ( hd->is_spi ) {
		if (pTarget->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY) {
			if (!(pTarget->tflags & MPT_TARGET_FLAGS_Q_YES))
				scsi_adjust_queue_depth(device, 0, 1);
			else if (((pTarget->inq_data[0] & 0x1f) == 0x00)
			  && (pTarget->minSyncFactor <= MPT_ULTRA160 ))
				scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
					MPT_SCSI_CMD_PER_DEV_HIGH);
			else
				scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
					MPT_SCSI_CMD_PER_DEV_LOW);
		} else {
			/* error case - No Inq. Data */
			scsi_adjust_queue_depth(device, 0, 1);
3431 3432
		}
	}
3433

3434 3435 3436
	dsprintk((MYIOC_s_INFO_FMT
		"Queue depth=%d, tflags=%x\n",
		hd->ioc->name, device->queue_depth, pTarget->tflags));
3437

3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
	dsprintk((MYIOC_s_INFO_FMT
		"negoFlags=%x, maxOffset=%x, SyncFactor=%x\n",
		hd->ioc->name, pTarget->negoFlags, pTarget->maxOffset, pTarget->minSyncFactor));

slave_configure_exit:

	dsprintk((MYIOC_s_INFO_FMT
		"tagged %d, simple %d, ordered %d\n",
		hd->ioc->name,device->tagged_supported, device->simple_tags,
		device->ordered_tags));

	return 0;
3450
}
Pam Delaney's avatar
Pam Delaney committed
3451

3452

Pam Delaney's avatar
Pam Delaney committed
3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  Private routines...
 */

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* Utility function to copy sense data from the scsi_cmnd buffer
 * to the FC and SCSI target structures.
 *
 */
static void
copy_sense_data(Scsi_Cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply)
{
	VirtDevice	*target;
	SCSIIORequest_t	*pReq;
	u32		 sense_count = le32_to_cpu(pScsiReply->SenseCount);
	int		 index;
	char		 devFoo[96];
	IO_Info_t	 thisIo;

	/* Get target structure
	 */
	pReq = (SCSIIORequest_t *) mf;
	index = (int) pReq->TargetID;
	target = hd->Targets[index];
	if (hd->is_multipath && sc->device->hostdata)
		target = (VirtDevice *) sc->device->hostdata;

	if (sense_count) {
		u8 *sense_data;
		int req_index;

		/* Copy the sense received into the scsi command block. */
		req_index = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
		sense_data = ((u8 *)hd->ioc->sense_buf_pool + (req_index * MPT_SENSE_BUFFER_ALLOC));
		memcpy(sc->sense_buffer, sense_data, SNS_LEN(sc));

		/* Log SMART data (asc = 0x5D, non-IM case only) if required.
		 */
		if ((hd->ioc->events) && (hd->ioc->eventTypes & (1 << MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE))) {
			if ((sense_data[12] == 0x5D) && (target->raidVolume == 0)) {
				int idx;
				MPT_ADAPTER *ioc = hd->ioc;

				idx = ioc->eventContext % ioc->eventLogSize;
				ioc->events[idx].event = MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE;
				ioc->events[idx].eventContext = ioc->eventContext;

				ioc->events[idx].data[0] = (pReq->LUN[1] << 24) ||
					(MPI_EVENT_SCSI_DEV_STAT_RC_SMART_DATA << 16) ||
					(pReq->Bus << 8) || pReq->TargetID;

				ioc->events[idx].data[1] = (sense_data[13] << 8) || sense_data[12];

				ioc->eventContext++;
			}
		}

		/* Print an error report for the user.
		 */
		thisIo.cdbPtr = sc->cmnd;
		thisIo.sensePtr = sc->sense_buffer;
		thisIo.SCSIStatus = pScsiReply->SCSIStatus;
		thisIo.DoDisplay = 1;
		if (hd->is_multipath)
3518
			sprintf(devFoo, "%d:%d:%d",
Pam Delaney's avatar
Pam Delaney committed
3519 3520
					hd->ioc->id,
					pReq->TargetID,
3521
					pReq->LUN[1]);
Pam Delaney's avatar
Pam Delaney committed
3522
		else
3523
			sprintf(devFoo, "%d:%d:%d", hd->ioc->id, sc->device->id, sc->device->lun);
Pam Delaney's avatar
Pam Delaney committed
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546
		thisIo.DevIDStr = devFoo;
/* fubar */
		thisIo.dataPtr = NULL;
		thisIo.inqPtr = NULL;
		if (sc->device) {
			thisIo.inqPtr = sc->device->vendor-8;	/* FIXME!!! */
		}
		(void) mpt_ScsiHost_ErrorReport(&thisIo);

	} else {
		dprintk((MYIOC_s_INFO_FMT "Hmmm... SenseData len=0! (?)\n",
				hd->ioc->name));
	}

	return;
}

static u32
SCPNT_TO_LOOKUP_IDX(Scsi_Cmnd *sc)
{
	MPT_SCSI_HOST *hd;
	int i;

3547
	hd = (MPT_SCSI_HOST *) sc->device->host->hostdata;
Pam Delaney's avatar
Pam Delaney committed
3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560

	for (i = 0; i < hd->ioc->req_depth; i++) {
		if (hd->ScsiLookup[i] == sc) {
			return i;
		}
	}

	return -1;
}


/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* Search the pendingQ for a command with specific index.
3561
 * If found, delete and return mf pointer
Pam Delaney's avatar
Pam Delaney committed
3562 3563 3564 3565 3566 3567 3568 3569
 * If not found, return NULL
 */
static MPT_FRAME_HDR *
mptscsih_search_pendingQ(MPT_SCSI_HOST *hd, int scpnt_idx)
{
	unsigned long	 flags;
	MPT_DONE_Q	*buffer;
	MPT_FRAME_HDR	*mf = NULL;
3570
	MPT_FRAME_HDR	*cmdMfPtr;
Pam Delaney's avatar
Pam Delaney committed
3571

3572
	ddvtprintk((MYIOC_s_INFO_FMT ": search_pendingQ ...", hd->ioc->name));
Pam Delaney's avatar
Pam Delaney committed
3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
	cmdMfPtr = MPT_INDEX_2_MFPTR(hd->ioc, scpnt_idx);
	spin_lock_irqsave(&hd->freedoneQlock, flags);
	if (!Q_IS_EMPTY(&hd->pendingQ)) {
		buffer = hd->pendingQ.head;
		do {
			mf = (MPT_FRAME_HDR *) buffer->argp;
			if (mf == cmdMfPtr) {
				Q_DEL_ITEM(buffer);

				/* clear the arg pointer
				 */
				buffer->argp = NULL;

				/* Add to the freeQ
				 */
				Q_ADD_TAIL(&hd->freeQ.head, buffer, MPT_DONE_Q);
				break;
			}
			mf = NULL;
		} while ((buffer = buffer->forw) != (MPT_DONE_Q *) &hd->pendingQ);
	}
	spin_unlock_irqrestore(&hd->freedoneQlock, flags);
	ddvtprintk((" ...return %p\n", mf));
	return mf;
}

/* Post all commands on the pendingQ to the FW.
 * Lock Q when deleting/adding members
 * Lock io_request_lock for OS callback.
 */
static void
post_pendingQ_commands(MPT_SCSI_HOST *hd)
{
	MPT_FRAME_HDR	*mf;
	MPT_DONE_Q	*buffer;
	unsigned long	 flags;

	/* Flush the pendingQ.
	 */
3612
	ddvtprintk((MYIOC_s_INFO_FMT ": post_pendingQ_commands\n", hd->ioc->name));
Pam Delaney's avatar
Pam Delaney committed
3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638
	while (1) {
		spin_lock_irqsave(&hd->freedoneQlock, flags);
		if (Q_IS_EMPTY(&hd->pendingQ)) {
			spin_unlock_irqrestore(&hd->freedoneQlock, flags);
			break;
		}

		buffer = hd->pendingQ.head;
		/* Delete from Q
		 */
		Q_DEL_ITEM(buffer);

		mf = (MPT_FRAME_HDR *) buffer->argp;
		buffer->argp = NULL;

		/* Add to the freeQ
		 */
		Q_ADD_TAIL(&hd->freeQ.head, buffer, MPT_DONE_Q);
		spin_unlock_irqrestore(&hd->freedoneQlock, flags);

		if (!mf) {
			/* This should never happen */
			printk(MYIOC_s_WARN_FMT "post_pendingQ_commands: mf %p\n", hd->ioc->name, (void *) mf);
			continue;
		}

3639
		mpt_put_msg_frame(ScsiDoneCtx, hd->ioc->id, mf);
3640 3641 3642 3643 3644 3645 3646 3647 3648

#if defined(MPT_DEBUG_DV) || defined(MPT_DEBUG_DV_TINY)
		{
			u16		 req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
			Scsi_Cmnd	*sc = hd->ScsiLookup[req_idx];
			printk(MYIOC_s_INFO_FMT "Issued SCSI cmd (sc=%p) idx=%d (mf=%p)\n",
					hd->ioc->name, sc, req_idx, mf);
		}
#endif
Pam Delaney's avatar
Pam Delaney committed
3649 3650 3651 3652 3653 3654 3655 3656 3657
	}

	return;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mptscsih_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
{
3658
	MPT_SCSI_HOST	*hd;
Pam Delaney's avatar
Pam Delaney committed
3659 3660 3661 3662
	unsigned long	 flags;

	dtmprintk((KERN_WARNING MYNAM
			": IOC %s_reset routed to SCSI host driver!\n",
3663 3664
			reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
			reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
Pam Delaney's avatar
Pam Delaney committed
3665 3666 3667 3668 3669 3670 3671 3672 3673 3674

	/* If a FW reload request arrives after base installed but
	 * before all scsi hosts have been attached, then an alt_ioc
	 * may have a NULL sh pointer.
	 */
	if ((ioc->sh == NULL) || (ioc->sh->hostdata == NULL))
		return 0;
	else
		hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;

3675 3676
	if (reset_phase == MPT_IOC_SETUP_RESET) {
		dtmprintk((MYIOC_s_WARN_FMT "Setup-Diag Reset\n", ioc->name));
Pam Delaney's avatar
Pam Delaney committed
3677 3678 3679 3680 3681 3682 3683

		/* Clean Up:
		 * 1. Set Hard Reset Pending Flag
		 * All new commands go to doneQ
		 */
		hd->resetPending = 1;

3684 3685 3686
	} else if (reset_phase == MPT_IOC_PRE_RESET) {
		dtmprintk((MYIOC_s_WARN_FMT "Pre-Diag Reset\n", ioc->name));

Pam Delaney's avatar
Pam Delaney committed
3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
		/* 2. Flush running commands
		 *	Clean drop test code - if compiled
		 *	Clean ScsiLookup (and associated memory)
		 *	AND clean mytaskQ
		 */

		/* 2a. Drop Test Command.
		 */

		/* 2b. Reply to OS all known outstanding I/O commands.
		 */
		mptscsih_flush_running_cmds(hd);

		/* 2c. If there was an internal command that
		 * has not completed, configuration or io request,
		 * free these resources.
		 */
		if (hd->cmdPtr) {
			del_timer(&hd->timer);
			mpt_free_msg_frame(ScsiScanDvCtx, ioc->id, hd->cmdPtr);
		}

		/* 2d. If a task management has not completed,
		 * free resources associated with this request.
		 */
		if (hd->tmPtr) {
			del_timer(&hd->TMtimer);
			mpt_free_msg_frame(ScsiTaskCtx, ioc->id, hd->tmPtr);
		}

3717 3718 3719 3720 3721
#ifdef MPTSCSIH_DBG_TIMEOUT
		ioc->timeout_hard = 0;
#endif

		dtmprintk((MYIOC_s_WARN_FMT "Pre-Reset complete.\n", ioc->name));
Pam Delaney's avatar
Pam Delaney committed
3722 3723

	} else {
3724
		dtmprintk((MYIOC_s_WARN_FMT "Post-Diag Reset\n", ioc->name));
Pam Delaney's avatar
Pam Delaney committed
3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760

		/* Once a FW reload begins, all new OS commands are
		 * redirected to the doneQ w/ a reset status.
		 * Init all control structures.
		 */

		/* ScsiLookup initialization
		 */
		{
			int ii;
			for (ii=0; ii < hd->ioc->req_depth; ii++)
				hd->ScsiLookup[ii] = NULL;
		}

		/* 2. Chain Buffer initialization
		 */
		mptscsih_initChainBuffers(hd, 0);

		/* 3. tmPtr clear
		 */
		if (hd->tmPtr) {
			hd->tmPtr = NULL;
		}

		/* 4. Renegotiate to all devices, if SCSI
		 */
		if (hd->is_spi)
			mptscsih_writeSDP1(hd, 0, 0, MPT_SCSICFG_ALL_IDS | MPT_SCSICFG_USE_NVRAM);

		/* 5. Enable new commands to be posted
		 */
		spin_lock_irqsave(&ioc->FreeQlock, flags);
		hd->tmPending = 0;
		spin_unlock_irqrestore(&ioc->FreeQlock, flags);
		hd->resetPending = 0;
		hd->numTMrequests = 0;
3761
		hd->tmState = TM_STATE_NONE;
Pam Delaney's avatar
Pam Delaney committed
3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780

		/* 6. If there was an internal command,
		 * wake this process up.
		 */
		if (hd->cmdPtr) {
			/*
			 * Wake up the original calling thread
			 */
			hd->pLocal = &hd->localReply;
			hd->pLocal->completion = MPT_SCANDV_DID_RESET;
			scandv_wait_done = 1;
			wake_up(&scandv_waitq);
			hd->cmdPtr = NULL;
		}

		/* 7. Flush doneQ
		 */
		flush_doneQ(hd);

3781 3782
		/* 8. Set flag to force DV and re-read IOC Page 3
		 */
3783 3784 3785 3786
		if (hd->is_spi) {
			ioc->spi_data.forceDv = MPT_SCSICFG_NEED_DV | MPT_SCSICFG_RELOAD_IOC_PG3;
			ddvtprintk(("Set reload IOC Pg3 Flag\n"));
		}
3787

3788 3789
		dtmprintk((MYIOC_s_WARN_FMT "Post-Reset complete.\n", ioc->name));

Pam Delaney's avatar
Pam Delaney committed
3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810
	}

	return 1;		/* currently means nothing really */
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
{
	MPT_SCSI_HOST *hd;
	u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;

	dprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
			ioc->name, event));

	switch (event) {
	case MPI_EVENT_UNIT_ATTENTION:			/* 03 */
		/* FIXME! */
		break;
	case MPI_EVENT_IOC_BUS_RESET:			/* 04 */
	case MPI_EVENT_EXT_BUS_RESET:			/* 05 */
3811 3812 3813 3814 3815 3816
		hd = NULL;
		if (ioc->sh) {
			hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
			if (hd && (hd->is_spi) && (hd->soft_resets < -1))
				hd->soft_resets++;
		}
Pam Delaney's avatar
Pam Delaney committed
3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834
		break;
	case MPI_EVENT_LOGOUT:				/* 09 */
		/* FIXME! */
		break;

		/*
		 *  CHECKME! Don't think we need to do
		 *  anything for these, but...
		 */
	case MPI_EVENT_RESCAN:				/* 06 */
	case MPI_EVENT_LINK_STATUS_CHANGE:		/* 07 */
	case MPI_EVENT_LOOP_STATE_CHANGE:		/* 08 */
		/*
		 *  CHECKME!  Falling thru...
		 */
		break;

	case MPI_EVENT_INTEGRATED_RAID:			/* 0B */
James Bottomley's avatar
James Bottomley committed
3835
#ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
3836
		/* negoNvram set to 0 if DV enabled and to USE_NVRAM if
Pam Delaney's avatar
Pam Delaney committed
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848
		 * if DV disabled. Need to check for target mode.
		 */
		hd = NULL;
		if (ioc->sh)
			hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;

		if (hd && (hd->is_spi) && (hd->negoNvram == 0)) {
			ScsiCfgData	*pSpi;
			Ioc3PhysDisk_t	*pPDisk;
			int		 numPDisk;
			u8		 reason;
			u8		 physDiskNum;
James Bottomley's avatar
James Bottomley committed
3849

Pam Delaney's avatar
Pam Delaney committed
3850 3851
			reason = (le32_to_cpu(pEvReply->Data[0]) & 0x00FF0000) >> 16;
			if (reason == MPI_EVENT_RAID_RC_DOMAIN_VAL_NEEDED) {
3852
				/* New or replaced disk.
Pam Delaney's avatar
Pam Delaney committed
3853 3854 3855 3856
				 * Set DV flag and schedule DV.
				 */
				pSpi = &ioc->spi_data;
				physDiskNum = (le32_to_cpu(pEvReply->Data[0]) & 0xFF000000) >> 24;
3857
				ddvtprintk(("DV requested for phys disk id %d\n", physDiskNum));
Pam Delaney's avatar
Pam Delaney committed
3858 3859 3860 3861 3862 3863
				if (pSpi->pIocPg3) {
					pPDisk =  pSpi->pIocPg3->PhysDisk;
					numPDisk =pSpi->pIocPg3->NumPhysDisks;

					while (numPDisk) {
						if (physDiskNum == pPDisk->PhysDiskNum) {
3864
							pSpi->dvStatus[pPDisk->PhysDiskID] = (MPT_SCSICFG_NEED_DV | MPT_SCSICFG_DV_NOT_DONE);
Pam Delaney's avatar
Pam Delaney committed
3865 3866 3867 3868 3869 3870 3871
							pSpi->forceDv = MPT_SCSICFG_NEED_DV;
							ddvtprintk(("NEED_DV set for phys disk id %d\n", pPDisk->PhysDiskID));
							break;
						}
						pPDisk++;
						numPDisk--;
					}
3872 3873 3874 3875 3876 3877 3878 3879 3880 3881

					if (numPDisk == 0) {
						/* The physical disk that needs DV was not found
						 * in the stored IOC Page 3. The driver must reload
						 * this page. DV routine will set the NEED_DV flag for
						 * all phys disks that have DV_NOT_DONE set.
						 */
						pSpi->forceDv = MPT_SCSICFG_NEED_DV | MPT_SCSICFG_RELOAD_IOC_PG3;
						ddvtprintk(("phys disk %d not found. Setting reload IOC Pg3 Flag\n", physDiskNum));
					}
Pam Delaney's avatar
Pam Delaney committed
3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129
				}
			}
		}
#endif

#if defined(MPT_DEBUG_DV) || defined(MPT_DEBUG_DV_TINY)
		printk("Raid Event RF: ");
		{
			u32 *m = (u32 *)pEvReply;
			int ii;
			int n = (int)pEvReply->MsgLength;
			for (ii=6; ii < n; ii++)
				printk(" %08x", le32_to_cpu(m[ii]));
			printk("\n");
		}
#endif
		break;

	case MPI_EVENT_NONE:				/* 00 */
	case MPI_EVENT_LOG_DATA:			/* 01 */
	case MPI_EVENT_STATE_CHANGE:			/* 02 */
	case MPI_EVENT_EVENT_CHANGE:			/* 0A */
	default:
		dprintk((KERN_INFO "  Ignoring event (=%02Xh)\n", event));
		break;
	}

	return 1;		/* currently means nothing really */
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  Private data...
 */
static ASCQ_Table_t *mptscsih_ASCQ_TablePtr;

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* old symsense.c stuff... */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 * Private data...
 * To protect ourselves against those that would pass us bogus pointers
 */
static u8 dummyInqData[SCSI_STD_INQUIRY_BYTES]
    = { 0x1F, 0x00, 0x00, 0x00,
	0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static u8 dummySenseData[SCSI_STD_SENSE_BYTES]
    = { 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00 };
static u8 dummyCDB[16]
    = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static u8 dummyScsiData[16]
    = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

static char *ScsiStatusString[] = {
	"GOOD",					/* 00h */
	NULL,					/* 01h */
	"CHECK CONDITION",			/* 02h */
	NULL,					/* 03h */
	"CONDITION MET",			/* 04h */
	NULL,					/* 05h */
	NULL,					/* 06h */
	NULL,					/* 07h */
	"BUSY",					/* 08h */
	NULL,					/* 09h */
	NULL,					/* 0Ah */
	NULL,					/* 0Bh */
	NULL,					/* 0Ch */
	NULL,					/* 0Dh */
	NULL,					/* 0Eh */
	NULL,					/* 0Fh */
	"INTERMEDIATE",				/* 10h */
	NULL,					/* 11h */
	NULL,					/* 12h */
	NULL,					/* 13h */
	"INTERMEDIATE-CONDITION MET",		/* 14h */
	NULL,					/* 15h */
	NULL,					/* 16h */
	NULL,					/* 17h */
	"RESERVATION CONFLICT",			/* 18h */
	NULL,					/* 19h */
	NULL,					/* 1Ah */
	NULL,					/* 1Bh */
	NULL,					/* 1Ch */
	NULL,					/* 1Dh */
	NULL,					/* 1Eh */
	NULL,					/* 1Fh */
	NULL,					/* 20h */
	NULL,					/* 21h */
	"COMMAND TERMINATED",			/* 22h */
	NULL,					/* 23h */
	NULL,					/* 24h */
	NULL,					/* 25h */
	NULL,					/* 26h */
	NULL,					/* 27h */
	"TASK SET FULL",			/* 28h */
	NULL,					/* 29h */
	NULL,					/* 2Ah */
	NULL,					/* 2Bh */
	NULL,					/* 2Ch */
	NULL,					/* 2Dh */
	NULL,					/* 2Eh */
	NULL,					/* 2Fh */
	"ACA ACTIVE",				/* 30h */
	NULL
};

static const char *ScsiCommonOpString[] = {
	"TEST UNIT READY",			/* 00h */
	"REZERO UNIT (REWIND)",			/* 01h */
	NULL,					/* 02h */
	"REQUEST_SENSE",			/* 03h */
	"FORMAT UNIT (MEDIUM)",			/* 04h */
	"READ BLOCK LIMITS",			/* 05h */
	NULL,					/* 06h */
	"REASSIGN BLOCKS",			/* 07h */
	"READ(6)",				/* 08h */
	NULL,					/* 09h */
	"WRITE(6)",				/* 0Ah */
	"SEEK(6)",				/* 0Bh */
	NULL,					/* 0Ch */
	NULL,					/* 0Dh */
	NULL,					/* 0Eh */
	"READ REVERSE",				/* 0Fh */
	"WRITE_FILEMARKS",			/* 10h */
	"SPACE(6)",				/* 11h */
	"INQUIRY",				/* 12h */
	NULL
};

static const char *SenseKeyString[] = {
	"NO SENSE",				/* 0h */
	"RECOVERED ERROR",			/* 1h */
	"NOT READY",				/* 2h */
	"MEDIUM ERROR",				/* 3h */
	"HARDWARE ERROR",			/* 4h */
	"ILLEGAL REQUEST",			/* 5h */
	"UNIT ATTENTION",			/* 6h */
	"DATA PROTECT",				/* 7h */
	"BLANK CHECK",				/* 8h */
	"VENDOR-SPECIFIC",			/* 9h */
	"ABORTED COPY",				/* Ah */
	"ABORTED COMMAND",			/* Bh */
	"EQUAL (obsolete)",			/* Ch */
	"VOLUME OVERFLOW",			/* Dh */
	"MISCOMPARE",				/* Eh */
	"RESERVED",				/* Fh */
	NULL
};

#define SPECIAL_ASCQ(c,q) \
	(((c) == 0x40 && (q) != 0x00) || ((c) == 0x4D) || ((c) == 0x70))

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int dump_cdb(char *foo, unsigned char *cdb)
{
	int i, grpCode, cdbLen;
	int l = 0;

	grpCode = cdb[0] >> 5;
	if (grpCode < 1)
		cdbLen = 6;
	else if (grpCode < 3)
		cdbLen = 10;
	else if (grpCode == 5)
		cdbLen = 12;
	else
		cdbLen = 16;

	for (i=0; i < cdbLen; i++)
		l += sprintf(foo+l, " %02X", cdb[i]);

	return l;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*  Do ASC/ASCQ lookup/grindage to English readable string(s)  */
static const char * ascq_set_strings_4max(
		u8 ASC, u8 ASCQ,
		const char **s1, const char **s2, const char **s3, const char **s4)
{
	static const char *asc_04_part1_string = "LOGICAL UNIT ";
	static const char *asc_04_part2a_string = "NOT READY, ";
	static const char *asc_04_part2b_string = "IS ";
	static const char *asc_04_ascq_NN_part3_strings[] = {	/* ASC ASCQ (hex) */
	  "CAUSE NOT REPORTABLE",				/* 04 00 */
	  "IN PROCESS OF BECOMING READY",			/* 04 01 */
	  "INITIALIZING CMD. REQUIRED",				/* 04 02 */
	  "MANUAL INTERVENTION REQUIRED",			/* 04 03 */
	  /* Add	" IN PROGRESS" to all the following... */
	  "FORMAT",						/* 04 04 */
	  "REBUILD",						/* 04 05 */
	  "RECALCULATION",					/* 04 06 */
	  "OPERATION",						/* 04 07 */
	  "LONG WRITE",						/* 04 08 */
	  "SELF-TEST",						/* 04 09 */
	  NULL
	};
	static char *asc_04_part4_string = " IN PROGRESS";

	static char *asc_29_ascq_NN_strings[] = {		/* ASC ASCQ (hex) */
	  "POWER ON, RESET, OR BUS DEVICE RESET OCCURRED",	/* 29 00 */
	  "POWER ON OCCURRED",					/* 29 01 */
	  "SCSI BUS RESET OCCURRED",				/* 29 02 */
	  "BUS DEVICE RESET FUNCTION OCCURRED",			/* 29 03 */
	  "DEVICE INTERNAL RESET",				/* 29 04 */
	  "TRANSCEIVER MODE CHANGED TO SINGLE-ENDED",		/* 29 05 */
	  "TRANSCEIVER MODE CHANGED TO LVD",			/* 29 06 */
	  NULL
	};
	static char *ascq_vendor_uniq = "(Vendor Unique)";
	static char *ascq_noone = "(no matching ASC/ASCQ description found)";
	int idx;

	*s1 = *s2 = *s3 = *s4 = "";		/* set'em all to the empty "" string */

	/* CHECKME! Need lock/sem?
	 *  Update and examine for isense module presense.
	 */
	mptscsih_ASCQ_TablePtr = (ASCQ_Table_t *)mpt_v_ASCQ_TablePtr;

	if (mptscsih_ASCQ_TablePtr == NULL) {
		/* 2nd chances... */
		if (ASC == 0x04 && (ASCQ < sizeof(asc_04_ascq_NN_part3_strings)/sizeof(char*)-1)) {
			*s1 = asc_04_part1_string;
			*s2 = (ASCQ == 0x01) ? asc_04_part2b_string : asc_04_part2a_string;
			*s3 = asc_04_ascq_NN_part3_strings[ASCQ];
			/* check for " IN PROGRESS" ones */
			if (ASCQ >= 0x04)
				*s4 = asc_04_part4_string;
		} else if (ASC == 0x29 && (ASCQ < sizeof(asc_29_ascq_NN_strings)/sizeof(char*)-1))
			*s1 = asc_29_ascq_NN_strings[ASCQ];
		/*
		 *	Else { leave all *s[1-4] values pointing to the empty "" string }
		 */
		return *s1;
	}

	/*
	 * Need to check ASC here; if it is "special," then
	 * the ASCQ is variable, and indicates failed component number.
4130
	 * We must treat the ASCQ as a "don't care" while searching the
Pam Delaney's avatar
Pam Delaney committed
4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152
	 * mptscsih_ASCQ_Table[] by masking it off, and then restoring it later
	 * on when we actually need to identify the failed component.
	 */
	if (SPECIAL_ASCQ(ASC,ASCQ))
		ASCQ = 0xFF;

	/* OK, now search mptscsih_ASCQ_Table[] for a matching entry */
	for (idx = 0; mptscsih_ASCQ_TablePtr && idx < mpt_ASCQ_TableSz; idx++)
		if ((ASC == mptscsih_ASCQ_TablePtr[idx].ASC) && (ASCQ == mptscsih_ASCQ_TablePtr[idx].ASCQ)) {
			*s1 = mptscsih_ASCQ_TablePtr[idx].Description;
			return *s1;
		}

	if ((ASC >= 0x80) || (ASCQ >= 0x80))
		*s1 = ascq_vendor_uniq;
	else
		*s1 = ascq_noone;

	return *s1;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4153 4154 4155 4156 4157 4158 4159 4160 4161
/*
 *  SCSI Information Report; desired output format...
 *---
SCSI Error: (iocnum:target_id:LUN) Status=02h (CHECK CONDITION)
  Key=6h (UNIT ATTENTION); FRU=03h
  ASC/ASCQ=29h/00h, "POWER ON, RESET, OR BUS DEVICE RESET OCCURRED"
  CDB: 00 00 00 00 00 00 - TestUnitReady
 *---
 */
Pam Delaney's avatar
Pam Delaney committed
4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185
/*
 *  SCSI Error Report; desired output format...
 *---
SCSI Error Report =-=-=-=-=-=-=-=-=-=-=-=-=-= (ioc0,scsi0:0)
  SCSI_Status=02h (CHECK CONDITION)
  Original_CDB[]: 00 00 00 00 00 00 - TestUnitReady
  SenseData[12h]: 70 00 06 00 00 00 00 0A 00 00 00 00 29 00 03 00 00 00
  SenseKey=6h (UNIT ATTENTION); FRU=03h
  ASC/ASCQ=29h/00h, "POWER ON, RESET, OR BUS DEVICE RESET OCCURRED"
 *---
 */

int mpt_ScsiHost_ErrorReport(IO_Info_t *ioop)
{
	char		 foo[512];
	char		 buf2[32];
	char		*statstr;
	const char	*opstr;
	int		 sk		= SD_Sense_Key(ioop->sensePtr);
	const char	*skstr		= SenseKeyString[sk];
	unsigned char	 asc		= SD_ASC(ioop->sensePtr);
	unsigned char	 ascq		= SD_ASCQ(ioop->sensePtr);
	int		 l;

4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196
	/* Change the error logging to only report errors on
	 * read and write commands. Ignore errors on other commands.
	 * Should this be configurable via proc?
	 */
	switch (ioop->cdbPtr[0]) {
	case READ_6:
	case WRITE_6:
	case READ_10:
	case WRITE_10:
	case READ_12:
	case WRITE_12:
James Bottomley's avatar
James Bottomley committed
4197 4198
	case READ_16:
	case WRITE_16:
4199 4200 4201 4202 4203
		break;
	default:
		return 0;
	}

Pam Delaney's avatar
Pam Delaney committed
4204 4205 4206 4207 4208 4209 4210 4211 4212 4213
	/*
	 *  More quiet mode.
	 *  Filter out common, repetitive, warning-type errors...  like:
	 *    POWER ON (06,29/00 or 06,29/01),
	 *    SPINNING UP (02,04/01),
	 *    LOGICAL UNIT NOT SUPPORTED (05,25/00), etc.
	 */
	if (sk == SK_NO_SENSE) {
		return 0;
	}
4214

Pam Delaney's avatar
Pam Delaney committed
4215 4216 4217 4218 4219 4220 4221 4222 4223
	if (	(sk==SK_UNIT_ATTENTION	&& asc==0x29 && (ascq==0x00 || ascq==0x01))
	     || (sk==SK_NOT_READY	&& asc==0x04 && (ascq==0x01 || ascq==0x02))
	     || (sk==SK_ILLEGAL_REQUEST && asc==0x25 && ascq==0x00)
	   )
	{
		/* Do nothing! */
		return 0;
	}

4224 4225 4226 4227 4228 4229 4230 4231
	/* Prevent the system from continually writing to the log
	 * if a medium is not found: 02 3A 00
	 * Changer issues: TUR, Read Capacity, Table of Contents continually
	 */
	if (sk==SK_NOT_READY && asc==0x3A) {
		if (ioop->cdbPtr == NULL) {
			return 0;
		} else if ((ioop->cdbPtr[0] == CMD_TestUnitReady) ||
4232
			(ioop->cdbPtr[0] == CMD_ReadCapacity) ||
4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243
			(ioop->cdbPtr[0] == 0x43)) {
			return 0;
		}
	}
	if (sk==SK_UNIT_ATTENTION) {
		if (ioop->cdbPtr == NULL)
			return 0;
		else if (ioop->cdbPtr[0] == CMD_TestUnitReady)
			return 0;
	}

Pam Delaney's avatar
Pam Delaney committed
4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268
	/*
	 *  Protect ourselves...
	 */
	if (ioop->cdbPtr == NULL)
		ioop->cdbPtr = dummyCDB;
	if (ioop->sensePtr == NULL)
		ioop->sensePtr = dummySenseData;
	if (ioop->inqPtr == NULL)
		ioop->inqPtr = dummyInqData;
	if (ioop->dataPtr == NULL)
		ioop->dataPtr = dummyScsiData;

	statstr = NULL;
	if ((ioop->SCSIStatus >= sizeof(ScsiStatusString)/sizeof(char*)-1) ||
	    ((statstr = (char*)ScsiStatusString[ioop->SCSIStatus]) == NULL)) {
		(void) sprintf(buf2, "Bad-Reserved-%02Xh", ioop->SCSIStatus);
		statstr = buf2;
	}

	opstr = NULL;
	if (1+ioop->cdbPtr[0] <= sizeof(ScsiCommonOpString)/sizeof(char*))
		opstr = ScsiCommonOpString[ioop->cdbPtr[0]];
	else if (mpt_ScsiOpcodesPtr)
		opstr = mpt_ScsiOpcodesPtr[ioop->cdbPtr[0]];

4269 4270 4271 4272 4273 4274
	l = sprintf(foo, "SCSI Error: (%s) Status=%02Xh (%s)\n",
			  ioop->DevIDStr,
			  ioop->SCSIStatus,
			  statstr);
	l += sprintf(foo+l, " Key=%Xh (%s); FRU=%02Xh\n ASC/ASCQ=%02Xh/%02Xh",
		  sk, skstr, SD_FRU(ioop->sensePtr), asc, ascq );
Pam Delaney's avatar
Pam Delaney committed
4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285
	{
		const char	*x1, *x2, *x3, *x4;
		x1 = x2 = x3 = x4 = "";
		x1 = ascq_set_strings_4max(asc, ascq, &x1, &x2, &x3, &x4);
		if (x1 != NULL) {
			if (x1[0] != '(')
				l += sprintf(foo+l, " \"%s%s%s%s\"", x1,x2,x3,x4);
			else
				l += sprintf(foo+l, " %s%s%s%s", x1,x2,x3,x4);
		}
	}
4286 4287 4288 4289 4290
	l += sprintf(foo+l, "\n CDB:");
	l += dump_cdb(foo+l, ioop->cdbPtr);
	if (opstr)
		l += sprintf(foo+l, " - \"%s\"", opstr);
	l += sprintf(foo+l, "\n");
Pam Delaney's avatar
Pam Delaney committed
4291 4292 4293 4294 4295 4296

	PrintF(("%s\n", foo));

	return l;
}

4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_initTarget - Target, LUN alloc/free functionality.
 *	@hd: Pointer to MPT_SCSI_HOST structure
 *	@bus_id: Bus number (?)
 *	@target_id: SCSI target id
 *	@lun: SCSI LUN id
 *	@data: Pointer to data
 *	@dlen: Number of INQUIRY bytes
 *
 *	NOTE: It's only SAFE to call this routine if data points to
 *	sane & valid STANDARD INQUIRY data!
 *
 *	Allocate and initialize memory for this target.
 *	Save inquiry data.
 *
 */
static void
mptscsih_initTarget(MPT_SCSI_HOST *hd, int bus_id, int target_id, u8 lun, char *data, int dlen)
{
	int		indexed_lun, lun_index;
	VirtDevice	*vdev;
	char		data_56;

	dprintk((MYIOC_s_INFO_FMT "initTarget bus=%d id=%d lun=%d hd=%p\n",
			hd->ioc->name, bus_id, target_id, lun, hd));

	/* Is LUN supported? If so, upper 3 bits will be 0
	* in first byte of inquiry data.
	*/
	if (data[0] & 0xe0)
		return;

	vdev = hd->Targets[target_id];

	lun_index = (lun >> 5);  /* 32 luns per lun_index */
	indexed_lun = (lun % 32);
	vdev->luns[lun_index] |= (1 << indexed_lun);

	vdev->raidVolume = 0;
	if (hd->is_spi) {
		if (hd->ioc->spi_data.isRaid & (1 << target_id)) {
			vdev->raidVolume = 1;
			ddvtprintk((KERN_INFO "RAID Volume @ id %d\n", target_id));
		}
	}

	if (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY)) {
		if ( dlen > 8 ) {
			memcpy (vdev->inq_data, data, 8);
		} else {
			memcpy (vdev->inq_data, data, dlen);
		}
		vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;

		/* If LUN 0, tape and have not done DV, set the DV flag.
		 */
		if (hd->is_spi && (lun == 0) && (data[0] == SCSI_TYPE_TAPE)) {
			ScsiCfgData *pSpi = &hd->ioc->spi_data;
			if (pSpi->dvStatus[target_id] & MPT_SCSICFG_DV_NOT_DONE)
				pSpi->dvStatus[target_id] |= MPT_SCSICFG_NEED_DV;
		}

		if ( (data[0] == SCSI_TYPE_PROC) &&
			!(vdev->tflags & MPT_TARGET_FLAGS_SAF_TE_ISSUED )) {
			if ( dlen > 49 ) {
				vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
				if ( data[44] == 'S' &&
				     data[45] == 'A' &&
				     data[46] == 'F' &&
				     data[47] == '-' &&
				     data[48] == 'T' &&
				     data[49] == 'E' ) {
					vdev->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
					mptscsih_writeIOCPage4(hd, target_id, bus_id);
				}
			} else {
				/* Treat all Processors as SAF-TE if
				 * command line option is set */
				if ( hd->ioc->spi_data.Saf_Te ) {
					vdev->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
					mptscsih_writeIOCPage4(hd, target_id, bus_id);
				}
			}
		}

		data_56 = 0;
		if (dlen > 56) {
			if ( (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_56))) {
			/* Update the target capabilities
			 */
				data_56 = data[56];
				vdev->tflags |= MPT_TARGET_FLAGS_VALID_56;
			}
		}
		mptscsih_setTargetNegoParms(hd, vdev, data_56);
	}

	dprintk((KERN_INFO "  target = %p\n", vdev));
	return;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  Update the target negotiation parameters based on the
 *  the Inquiry data, adapter capabilities, and NVRAM settings.
 *
 */
void mptscsih_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtDevice *target, char byte56)
{
	ScsiCfgData *pspi_data = &hd->ioc->spi_data;
	int  id = (int) target->target_id;
	int  nvram;
	char canQ = 0;
	VirtDevice	*vdev;
	int ii;
	u8 width = MPT_NARROW;
	u8 factor = MPT_ASYNC;
	u8 offset = 0;
	u8 version, nfactor;
	u8 noQas = 1;

	if (!hd->is_spi) {
		if (target->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY) {
			if (target->inq_data[7] & 0x02)
				target->tflags |= MPT_TARGET_FLAGS_Q_YES;
		}
		return;
	}

	target->negoFlags = pspi_data->noQas;

	/* noQas == 0 => device supports QAS. Need byte 56 of Inq to determine
	 * support. If available, default QAS to off and allow enabling.
	 * If not available, default QAS to on, turn off for non-disks.
	 */

	/* Set flags based on Inquiry data
	 */
	if (target->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY) {
		version = target->inq_data[2] & 0x07;
		if (version < 2) {
			width = 0;
			factor = MPT_ULTRA2;
			offset = pspi_data->maxSyncOffset;
		} else {
			if (target->inq_data[7] & 0x20) {
				width = 1;
			}

			if (target->inq_data[7] & 0x10) {
				/* bits 2 & 3 show DT support
				 */
				if ((byte56 & 0x04) == 0)
					factor = MPT_ULTRA2;
				else if ((byte56 & 0x03) == 0)
					factor = MPT_ULTRA160;
				else
					factor = MPT_ULTRA320;
				offset = pspi_data->maxSyncOffset;

				/* If RAID, never disable QAS
				 * else if non RAID, do not disable
				 *   QAS if bit 1 is set
				 * bit 1 QAS support, non-raid only
				 * bit 0 IU support
				 */
				if ((target->raidVolume == 1) || ((byte56 & 0x02) != 0))
					noQas = 0;
			} else {
				factor = MPT_ASYNC;
				offset = 0;
			}
		}

		if (target->inq_data[7] & 0x02) {
			canQ = 1;
		}

		/* Update tflags based on NVRAM settings. (SCSI only)
		 */
		if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
			nvram = pspi_data->nvram[id];
			nfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;

			if (width)
				width = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;

			if (offset > 0) {
				/* Ensure factor is set to the
				 * maximum of: adapter, nvram, inquiry
				 */
				if (nfactor) {
					if (nfactor < pspi_data->minSyncFactor )
						nfactor = pspi_data->minSyncFactor;

4495
					factor = max(factor, nfactor);
4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558
					if (factor == MPT_ASYNC)
						offset = 0;
				} else {
					offset = 0;
					factor = MPT_ASYNC;
				}
			} else {
				factor = MPT_ASYNC;
			}
		}

		/* Make sure data is consistent
		 */
		if ((!width) && (factor < MPT_ULTRA2)) {
			factor = MPT_ULTRA2;
		}

		/* Save the data to the target structure.
		 */
		target->minSyncFactor = factor;
		target->maxOffset = offset;
		target->maxWidth = width;
		if (canQ) {
			target->tflags |= MPT_TARGET_FLAGS_Q_YES;
		}

		target->tflags |= MPT_TARGET_FLAGS_VALID_NEGO;

		/* Disable unused features.
		 */
		if (!width)
			target->negoFlags |= MPT_TARGET_NO_NEGO_WIDE;

		if (!offset)
			target->negoFlags |= MPT_TARGET_NO_NEGO_SYNC;

		/* GEM, processor WORKAROUND
		 */
		if (((target->inq_data[0] & 0x1F) == 0x03)
			|| ((target->inq_data[0] & 0x1F) > 0x08)) {
			target->negoFlags |= (MPT_TARGET_NO_NEGO_WIDE | MPT_TARGET_NO_NEGO_SYNC);
			pspi_data->dvStatus[id] |= MPT_SCSICFG_BLK_NEGO;
		} else {
			if (noQas && (pspi_data->noQas == 0)) {
				pspi_data->noQas |= MPT_TARGET_NO_NEGO_QAS;
				target->negoFlags |= MPT_TARGET_NO_NEGO_QAS;

				/* Disable QAS in a mixed configuration case
		 		*/

//				ddvtprintk((KERN_INFO "Disabling QAS!\n"));
				for (ii = 0; ii < id; ii++) {
					if ( (vdev = hd->Targets[ii]) ) {
						vdev->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
					}
				}
			}
		}
	}

	return;
}

Pam Delaney's avatar
Pam Delaney committed
4559
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
4560
/* If DV disabled (negoNvram set to USE_NVARM) or if not LUN 0, return.
4561 4562
 * Else set the NEED_DV flag after Read Capacity Issued (disks)
 * or Mode Sense (cdroms).
4563 4564 4565
 *
 * Tapes, initTarget will set this flag on completion of Inquiry command.
 * Called only if DV_NOT_DONE flag is set
Pam Delaney's avatar
Pam Delaney committed
4566
 */
4567
static void mptscsih_set_dvflags(MPT_SCSI_HOST *hd, SCSIIORequest_t *pReq)
Pam Delaney's avatar
Pam Delaney committed
4568
{
4569
	u8 cmd;
4570

4571
	if ((pReq->LUN[1] != 0) || (hd->negoNvram != 0))
Pam Delaney's avatar
Pam Delaney committed
4572 4573
		return;

4574
	cmd = pReq->CDB[0];
Pam Delaney's avatar
Pam Delaney committed
4575

4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588
	if ((cmd == READ_CAPACITY) || (cmd == MODE_SENSE)) {
		ScsiCfgData *pSpi = &hd->ioc->spi_data;
		if ((pSpi->isRaid & (1 << pReq->TargetID)) && pSpi->pIocPg3) {
			/* Set NEED_DV for all hidden disks
			 */
			Ioc3PhysDisk_t *pPDisk =  pSpi->pIocPg3->PhysDisk;
			int		numPDisk = pSpi->pIocPg3->NumPhysDisks;

			while (numPDisk) {
				pSpi->dvStatus[pPDisk->PhysDiskID] |= MPT_SCSICFG_NEED_DV;
				ddvtprintk(("NEED_DV set for phys disk id %d\n", pPDisk->PhysDiskID));
				pPDisk++;
				numPDisk--;
Pam Delaney's avatar
Pam Delaney committed
4589
			}
4590 4591 4592
		}
		pSpi->dvStatus[pReq->TargetID] |= MPT_SCSICFG_NEED_DV;
		ddvtprintk(("NEED_DV set for visible disk id %d\n", pReq->TargetID));
Pam Delaney's avatar
Pam Delaney committed
4593 4594 4595 4596 4597
	}
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
4598 4599
 * If no Target, bus reset on 1st I/O. Set the flag to
 * prevent any future negotiations to this device.
Pam Delaney's avatar
Pam Delaney committed
4600 4601 4602
 */
static void mptscsih_no_negotiate(MPT_SCSI_HOST *hd, int target_id)
{
4603 4604 4605

	if ((hd->Targets) && (hd->Targets[target_id] == NULL))
		hd->ioc->spi_data.dvStatus[target_id] |= MPT_SCSICFG_BLK_NEGO;
Pam Delaney's avatar
Pam Delaney committed
4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677

	return;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  SCSI Config Page functionality ...
 */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*	mptscsih_setDevicePage1Flags  - add Requested and Configuration fields flags
 *	based on width, factor and offset parameters.
 *	@width: bus width
 *	@factor: sync factor
 *	@offset: sync offset
 *	@requestedPtr: pointer to requested values (updated)
 *	@configurationPtr: pointer to configuration values (updated)
 *	@flags: flags to block WDTR or SDTR negotiation
 *
 *	Return: None.
 *
 *	Remark: Called by writeSDP1 and _dv_params
 */
static void
mptscsih_setDevicePage1Flags (u8 width, u8 factor, u8 offset, int *requestedPtr, int *configurationPtr, u8 flags)
{
	u8 nowide = flags & MPT_TARGET_NO_NEGO_WIDE;
	u8 nosync = flags & MPT_TARGET_NO_NEGO_SYNC;

	*configurationPtr = 0;
	*requestedPtr = width ? MPI_SCSIDEVPAGE1_RP_WIDE : 0;
	*requestedPtr |= (offset << 16) | (factor << 8);

	if (width && offset && !nowide && !nosync) {
		if (factor < MPT_ULTRA160) {
			*requestedPtr |= (MPI_SCSIDEVPAGE1_RP_IU + MPI_SCSIDEVPAGE1_RP_DT);
			if ((flags & MPT_TARGET_NO_NEGO_QAS) == 0)
				*requestedPtr |= MPI_SCSIDEVPAGE1_RP_QAS;
		} else if (factor < MPT_ULTRA2) {
			*requestedPtr |= MPI_SCSIDEVPAGE1_RP_DT;
		}
	}

	if (nowide)
		*configurationPtr |= MPI_SCSIDEVPAGE1_CONF_WDTR_DISALLOWED;

	if (nosync)
		*configurationPtr |= MPI_SCSIDEVPAGE1_CONF_SDTR_DISALLOWED;

	return;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*	mptscsih_writeSDP1  - write SCSI Device Page 1
 *	@hd: Pointer to a SCSI Host Strucutre
 *	@portnum: IOC port number
 *	@target_id: writeSDP1 for single ID
 *	@flags: MPT_SCSICFG_ALL_IDS, MPT_SCSICFG_USE_NVRAM, MPT_SCSICFG_BLK_NEGO
 *
 *	Return: -EFAULT if read of config page header fails
 *		or 0 if success.
 *
 *	Remark: If a target has been found, the settings from the
 *		target structure are used, else the device is set
 *		to async/narrow.
 *
 *	Remark: Called during init and after a FW reload.
 *	Remark: We do not wait for a return, write pages sequentially.
 */
static int
mptscsih_writeSDP1(MPT_SCSI_HOST *hd, int portnum, int target_id, int flags)
{
	MPT_ADAPTER		*ioc = hd->ioc;
4678 4679 4680
	Config_t		*pReq;
	SCSIDevicePage1_t	*pData;
	VirtDevice		*pTarget;
Pam Delaney's avatar
Pam Delaney committed
4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692
	MPT_FRAME_HDR		*mf;
	dma_addr_t		 dataDma;
	u16			 req_idx;
	u32			 frameOffset;
	u32			 requested, configuration, flagsLength;
	int			 ii, nvram;
	int			 id = 0, maxid = 0;
	u8			 width;
	u8			 factor;
	u8			 offset;
	u8			 bus = 0;
	u8			 negoFlags;
4693
	u8			 maxwidth, maxoffset, maxfactor;
Pam Delaney's avatar
Pam Delaney committed
4694 4695 4696 4697 4698 4699 4700 4701 4702

	if (ioc->spi_data.sdp1length == 0)
		return 0;

	if (flags & MPT_SCSICFG_ALL_IDS) {
		id = 0;
		maxid = ioc->sh->max_id - 1;
	} else if (ioc->sh) {
		id = target_id;
4703
		maxid = min_t(int, id, ioc->sh->max_id - 1);
Pam Delaney's avatar
Pam Delaney committed
4704 4705 4706
	}

	for (; id <= maxid; id++) {
4707

Pam Delaney's avatar
Pam Delaney committed
4708 4709 4710
		if (id == ioc->pfacts[portnum].PortSCSIID)
			continue;

4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734
		/* Use NVRAM to get adapter and target maximums
		 * Data over-riden by target structure information, if present
		 */
		maxwidth = ioc->spi_data.maxBusWidth;
		maxoffset = ioc->spi_data.maxSyncOffset;
		maxfactor = ioc->spi_data.minSyncFactor;
		if (ioc->spi_data.nvram && (ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
			nvram = ioc->spi_data.nvram[id];

			if (maxwidth)
				maxwidth = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;

			if (maxoffset > 0) {
				maxfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
				if (maxfactor == 0) {
					/* Key for async */
					maxfactor = MPT_ASYNC;
					maxoffset = 0;
				} else if (maxfactor < ioc->spi_data.minSyncFactor) {
					maxfactor = ioc->spi_data.minSyncFactor;
				}
			} else
				maxfactor = MPT_ASYNC;
		}
Pam Delaney's avatar
Pam Delaney committed
4735

4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748
		/* Set the negotiation flags.
		 */
		negoFlags = ioc->spi_data.noQas;
		if (!maxwidth)
			negoFlags |= MPT_TARGET_NO_NEGO_WIDE;

		if (!maxoffset)
			negoFlags |= MPT_TARGET_NO_NEGO_SYNC;

		if (flags & MPT_SCSICFG_USE_NVRAM) {
			width = maxwidth;
			factor = maxfactor;
			offset = maxoffset;
Pam Delaney's avatar
Pam Delaney committed
4749 4750 4751 4752
		} else {
			width = 0;
			factor = MPT_ASYNC;
			offset = 0;
4753 4754
			//negoFlags = 0;
			//negoFlags = MPT_TARGET_NO_NEGO_SYNC;
Pam Delaney's avatar
Pam Delaney committed
4755 4756
		}

James Bottomley's avatar
James Bottomley committed
4757
#ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
4758 4759 4760 4761 4762 4763 4764 4765 4766 4767
		/* Force to async and narrow if DV has not been executed
		 * for this ID
		 */
		if ((hd->ioc->spi_data.dvStatus[id] & MPT_SCSICFG_DV_NOT_DONE) != 0) {
			width = 0;
			factor = MPT_ASYNC;
			offset = 0;
		}
#endif

Pam Delaney's avatar
Pam Delaney committed
4768 4769 4770
		/* If id is not a raid volume, get the updated
		 * transmission settings from the target structure.
		 */
4771
		if (hd->Targets && (pTarget = hd->Targets[id]) && !pTarget->raidVolume) {
Pam Delaney's avatar
Pam Delaney committed
4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791
			width = pTarget->maxWidth;
			factor = pTarget->minSyncFactor;
			offset = pTarget->maxOffset;
			negoFlags = pTarget->negoFlags;
		}

		if (flags & MPT_SCSICFG_BLK_NEGO)
			negoFlags = MPT_TARGET_NO_NEGO_WIDE | MPT_TARGET_NO_NEGO_SYNC;

		mptscsih_setDevicePage1Flags(width, factor, offset,
					&requested, &configuration, negoFlags);

		/* Get a MF for this command.
		 */
		if ((mf = mpt_get_msg_frame(ScsiDoneCtx, ioc->id)) == NULL) {
			dprintk((MYIOC_s_WARN_FMT "write SDP1: no msg frames!\n",
						ioc->name));
			return -EAGAIN;
		}

4792 4793 4794 4795
		ddvprintk((MYIOC_s_INFO_FMT "WriteSDP1 (mf=%p, id=%d, req=0x%x, cfg=0x%x)\n",
			hd->ioc->name, mf, id, requested, configuration));


Pam Delaney's avatar
Pam Delaney committed
4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816
		/* Set the request and the data pointers.
		 * Request takes: 36 bytes (32 bit SGE)
		 * SCSI Device Page 1 requires 16 bytes
		 * 40 + 16 <= size of SCSI IO Request = 56 bytes
		 * and MF size >= 64 bytes.
		 * Place data at end of MF.
		 */
		pReq = (Config_t *)mf;

		req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
		frameOffset = ioc->req_sz - sizeof(SCSIDevicePage1_t);

		pData = (SCSIDevicePage1_t *)((u8 *) mf + frameOffset);
		dataDma = ioc->req_frames_dma + (req_idx * ioc->req_sz) + frameOffset;

		/* Complete the request frame (same for all requests).
		 */
		pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
		pReq->Reserved = 0;
		pReq->ChainOffset = 0;
		pReq->Function = MPI_FUNCTION_CONFIG;
4817 4818
		pReq->ExtPageLength = 0;
		pReq->ExtPageType = 0;
Pam Delaney's avatar
Pam Delaney committed
4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844
		pReq->MsgFlags = 0;
		for (ii=0; ii < 8; ii++) {
			pReq->Reserved2[ii] = 0;
		}
		pReq->Header.PageVersion = ioc->spi_data.sdp1version;
		pReq->Header.PageLength = ioc->spi_data.sdp1length;
		pReq->Header.PageNumber = 1;
		pReq->Header.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
		pReq->PageAddress = cpu_to_le32(id | (bus << 8 ));

		/* Add a SGE to the config request.
		 */
		flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE | ioc->spi_data.sdp1length * 4;

		mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);

		/* Set up the common data portion
		 */
		pData->Header.PageVersion = pReq->Header.PageVersion;
		pData->Header.PageLength = pReq->Header.PageLength;
		pData->Header.PageNumber = pReq->Header.PageNumber;
		pData->Header.PageType = pReq->Header.PageType;
		pData->RequestedParameters = cpu_to_le32(requested);
		pData->Reserved = 0;
		pData->Configuration = cpu_to_le32(configuration);

4845
		dprintk((MYIOC_s_INFO_FMT
Pam Delaney's avatar
Pam Delaney committed
4846
			"write SDP1: id %d pgaddr 0x%x req 0x%x config 0x%x\n",
4847
				ioc->name, id, (id | (bus<<8)),
Pam Delaney's avatar
Pam Delaney committed
4848 4849
				requested, configuration));

4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908
		mpt_put_msg_frame(ScsiDoneCtx, ioc->id, mf);
	}

	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*	mptscsih_writeIOCPage4  - write IOC Page 4
 *	@hd: Pointer to a SCSI Host Structure
 *	@target_id: write IOC Page4 for this ID & Bus
 *
 *	Return: -EAGAIN if unable to obtain a Message Frame
 *		or 0 if success.
 *
 *	Remark: We do not wait for a return, write pages sequentially.
 */
static int
mptscsih_writeIOCPage4(MPT_SCSI_HOST *hd, int target_id, int bus)
{
	MPT_ADAPTER		*ioc = hd->ioc;
	Config_t		*pReq;
	IOCPage4_t		*IOCPage4Ptr;
	MPT_FRAME_HDR		*mf;
	dma_addr_t		 dataDma;
	u16			 req_idx;
	u32			 frameOffset;
	u32			 flagsLength;
	int			 ii;

	/* Get a MF for this command.
	 */
	if ((mf = mpt_get_msg_frame(ScsiDoneCtx, ioc->id)) == NULL) {
		dprintk((MYIOC_s_WARN_FMT "writeIOCPage4 : no msg frames!\n",
					ioc->name));
		return -EAGAIN;
	}

	ddvprintk((MYIOC_s_INFO_FMT "writeIOCPage4 (mf=%p, id=%d)\n",
		ioc->name, mf, target_id));

	/* Set the request and the data pointers.
	 * Place data at end of MF.
	 */
	pReq = (Config_t *)mf;

	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
	frameOffset = ioc->req_sz - sizeof(IOCPage4_t);

	/* Complete the request frame (same for all requests).
	 */
	pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
	pReq->Reserved = 0;
	pReq->ChainOffset = 0;
	pReq->Function = MPI_FUNCTION_CONFIG;
	pReq->ExtPageLength = 0;
	pReq->ExtPageType = 0;
	pReq->MsgFlags = 0;
	for (ii=0; ii < 8; ii++) {
		pReq->Reserved2[ii] = 0;
Pam Delaney's avatar
Pam Delaney committed
4909 4910
	}

4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931
       	IOCPage4Ptr = ioc->spi_data.pIocPg4;
       	dataDma = ioc->spi_data.IocPg4_dma;
       	ii = IOCPage4Ptr->ActiveSEP++;
       	IOCPage4Ptr->SEP[ii].SEPTargetID = target_id;
       	IOCPage4Ptr->SEP[ii].SEPBus = bus;
       	pReq->Header = IOCPage4Ptr->Header;
	pReq->PageAddress = cpu_to_le32(target_id | (bus << 8 ));

	/* Add a SGE to the config request.
	 */
	flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE |
		(IOCPage4Ptr->Header.PageLength + ii) * 4;

	mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);

	dsprintk((MYIOC_s_INFO_FMT
		"writeIOCPage4: pgaddr 0x%x\n",
			ioc->name, (target_id | (bus<<8))));

	mpt_put_msg_frame(ScsiDoneCtx, ioc->id, mf);

Pam Delaney's avatar
Pam Delaney committed
4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956
	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*	mptscsih_taskmgmt_timeout - Call back for timeout on a
 *	task management request.
 *	@data: Pointer to MPT_SCSI_HOST recast as an unsigned long
 *
 */
static void mptscsih_taskmgmt_timeout(unsigned long data)
{
	MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *) data;

	dtmprintk((KERN_WARNING MYNAM ": %s: mptscsih_taskmgmt_timeout: "
		   "TM request timed out!\n", hd->ioc->name));

	/* Delete the timer that triggered this callback.
	 * Remark: del_timer checks to make sure timer is active
	 * before deleting.
	 */
	del_timer(&hd->TMtimer);

	/* Call the reset handler. Already had a TM request
	 * timeout - so issue a diagnostic reset
	 */
4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974
	MPT_INIT_WORK(&mptscsih_rstTask, mptscsih_schedule_reset, (void *)hd);
	SCHEDULE_TASK(&mptscsih_rstTask);
	return;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*	mptscsih_schedule_reset - Call back for timeout on a
 *	task management request.
 *	@data: Pointer to MPT_SCSI_HOST recast as an unsigned long
 *
 */
static void
mptscsih_schedule_reset(void *arg)
{
        MPT_SCSI_HOST           *hd;
        hd = (MPT_SCSI_HOST *) arg;

	if (mpt_HardResetHandler(hd->ioc, CAN_SLEEP) < 0) {
Pam Delaney's avatar
Pam Delaney committed
4975
		printk((KERN_WARNING " Firmware Reload FAILED!!\n"));
4976
	} else {
Pam Delaney's avatar
Pam Delaney committed
4977 4978 4979
		/* Because we have reset the IOC, no TM requests can be
		 * pending.  So let's make sure the tmPending flag is reset.
		 */
4980
		dtmprintk((KERN_WARNING MYNAM
4981
			   ": %s: mptscsih_taskmgmt_timeout\n",
Pam Delaney's avatar
Pam Delaney committed
4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011
			   hd->ioc->name));
		hd->tmPending = 0;
	}

	return;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  Bus Scan and Domain Validation functionality ...
 */

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_scandv_complete - Scan and DV callback routine registered
 *	to Fustion MPT (base) driver.
 *
 *	@ioc: Pointer to MPT_ADAPTER structure
 *	@mf: Pointer to original MPT request frame
 *	@mr: Pointer to MPT reply frame (NULL if TurboReply)
 *
 *	This routine is called from mpt.c::mpt_interrupt() at the completion
 *	of any SCSI IO request.
 *	This routine is registered with the Fusion MPT (base) driver at driver
 *	load/init time via the mpt_register() API call.
 *
 *	Returns 1 indicating alloc'd request frame ptr should be freed.
 *
 *	Remark: Sets a completion code and (possibly) saves sense data
 *	in the IOC member localReply structure.
5012
 *	Used ONLY for DV and other internal commands.
Pam Delaney's avatar
Pam Delaney committed
5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036
 */
static int
mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
{
	MPT_SCSI_HOST	*hd;
	SCSIIORequest_t *pReq;
	int		 completionCode;
	u16		 req_idx;

	if ((mf == NULL) ||
	    (mf >= MPT_INDEX_2_MFPTR(ioc, ioc->req_depth))) {
		printk(MYIOC_s_ERR_FMT
			"ScanDvComplete, %s req frame ptr! (=%p)\n",
				ioc->name, mf?"BAD":"NULL", (void *) mf);
		goto wakeup;
	}

	hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
	del_timer(&hd->timer);
	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
	hd->ScsiLookup[req_idx] = NULL;
	pReq = (SCSIIORequest_t *) mf;

	if (mf != hd->cmdPtr) {
5037 5038
		printk(MYIOC_s_WARN_FMT "ScanDvComplete (mf=%p, cmdPtr=%p, idx=%d)\n",
				hd->ioc->name, (void *)mf, (void *) hd->cmdPtr, req_idx);
Pam Delaney's avatar
Pam Delaney committed
5039 5040 5041
	}
	hd->cmdPtr = NULL;

5042 5043
	ddvprintk((MYIOC_s_INFO_FMT "ScanDvComplete (mf=%p,mr=%p,idx=%d)\n",
			hd->ioc->name, mf, mr, req_idx));
Pam Delaney's avatar
Pam Delaney committed
5044 5045

	hd->pLocal = &hd->localReply;
5046
	hd->pLocal->scsiStatus = 0;
Pam Delaney's avatar
Pam Delaney committed
5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059

	/* If target struct exists, clear sense valid flag.
	 */
	if (mr == NULL) {
		completionCode = MPT_SCANDV_GOOD;
	} else {
		SCSIIOReply_t	*pReply;
		u16		 status;

		pReply = (SCSIIOReply_t *) mr;

		status = le16_to_cpu(pReply->IOCStatus) & MPI_IOCSTATUS_MASK;

5060
		ddvtprintk((KERN_NOTICE "  IOCStatus=%04xh, SCSIState=%02xh, SCSIStatus=%02xh, IOCLogInfo=%08xh\n",
Pam Delaney's avatar
Pam Delaney committed
5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102
			     status, pReply->SCSIState, pReply->SCSIStatus,
			     le32_to_cpu(pReply->IOCLogInfo)));

		switch(status) {

		case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:	/* 0x0043 */
			completionCode = MPT_SCANDV_SELECTION_TIMEOUT;
			break;

		case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:		/* 0x0046 */
		case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:	/* 0x0048 */
		case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:		/* 0x004B */
		case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:		/* 0x004C */
			completionCode = MPT_SCANDV_DID_RESET;
			break;

		case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:		/* 0x0045 */
		case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:	/* 0x0040 */
		case MPI_IOCSTATUS_SUCCESS:			/* 0x0000 */
			if (pReply->Function == MPI_FUNCTION_CONFIG) {
				ConfigReply_t *pr = (ConfigReply_t *)mr;
				completionCode = MPT_SCANDV_GOOD;
				hd->pLocal->header.PageVersion = pr->Header.PageVersion;
				hd->pLocal->header.PageLength = pr->Header.PageLength;
				hd->pLocal->header.PageNumber = pr->Header.PageNumber;
				hd->pLocal->header.PageType = pr->Header.PageType;

			} else if (pReply->Function == MPI_FUNCTION_RAID_ACTION) {
				/* If the RAID Volume request is successful,
				 * return GOOD, else indicate that
				 * some type of error occurred.
				 */
				MpiRaidActionReply_t	*pr = (MpiRaidActionReply_t *)mr;
				if (pr->ActionStatus == MPI_RAID_ACTION_ASTATUS_SUCCESS)
					completionCode = MPT_SCANDV_GOOD;
				else
					completionCode = MPT_SCANDV_SOME_ERROR;

			} else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
				u8		*sense_data;
				int		 sz;

5103
				/* save sense data in global structure
Pam Delaney's avatar
Pam Delaney committed
5104 5105 5106 5107 5108 5109
				 */
				completionCode = MPT_SCANDV_SENSE;
				hd->pLocal->scsiStatus = pReply->SCSIStatus;
				sense_data = ((u8 *)hd->ioc->sense_buf_pool +
					(req_idx * MPT_SENSE_BUFFER_ALLOC));

5110
				sz = min_t(int, pReq->SenseBufferLength,
Pam Delaney's avatar
Pam Delaney committed
5111 5112 5113 5114 5115
							SCSI_STD_SENSE_BYTES);
				memcpy(hd->pLocal->sense, sense_data, sz);

				ddvprintk((KERN_NOTICE "  Check Condition, sense ptr %p\n",
						sense_data));
5116 5117 5118 5119 5120 5121 5122
			} else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_FAILED) {
				if (pReq->CDB[0] == CMD_Inquiry)
					completionCode = MPT_SCANDV_ISSUE_SENSE;
				else
					completionCode = MPT_SCANDV_DID_RESET;
			}
			else if (pReply->SCSIState & MPI_SCSI_STATE_NO_SCSI_STATUS)
Pam Delaney's avatar
Pam Delaney committed
5123
				completionCode = MPT_SCANDV_DID_RESET;
5124
			else if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
Pam Delaney's avatar
Pam Delaney committed
5125
				completionCode = MPT_SCANDV_DID_RESET;
5126
			else {
Pam Delaney's avatar
Pam Delaney committed
5127 5128 5129
				/* If no error, this will be equivalent
				 * to MPT_SCANDV_GOOD
				 */
5130 5131
				completionCode = MPT_SCANDV_GOOD;
				hd->pLocal->scsiStatus = pReply->SCSIStatus;
Pam Delaney's avatar
Pam Delaney committed
5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147
			}
			break;

		case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:		/* 0x0047 */
			if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
				completionCode = MPT_SCANDV_DID_RESET;
			else
				completionCode = MPT_SCANDV_SOME_ERROR;
			break;

		default:
			completionCode = MPT_SCANDV_SOME_ERROR;
			break;

		}	/* switch(status) */

5148
		ddvtprintk((KERN_NOTICE "  completionCode set to %08xh\n",
Pam Delaney's avatar
Pam Delaney committed
5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212
				completionCode));
	} /* end of address reply case */

	hd->pLocal->completion = completionCode;

	/* MF and RF are freed in mpt_interrupt
	 */
wakeup:
	/* Free Chain buffers (will never chain) in scan or dv */
	//mptscsih_freeChainBuffers(hd, req_idx);

	/*
	 * Wake up the original calling thread
	 */
	scandv_wait_done = 1;
	wake_up(&scandv_waitq);

	return 1;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*	mptscsih_timer_expired - Call back for timer process.
 *	Used only for dv functionality.
 *	@data: Pointer to MPT_SCSI_HOST recast as an unsigned long
 *
 */
static void mptscsih_timer_expired(unsigned long data)
{
	MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *) data;

	ddvprintk((MYIOC_s_WARN_FMT "Timer Expired! Cmd %p\n", hd->ioc->name, hd->cmdPtr));

	if (hd->cmdPtr) {
		MPIHeader_t *cmd = (MPIHeader_t *)hd->cmdPtr;

		if (cmd->Function == MPI_FUNCTION_SCSI_IO_REQUEST) {
			/* Desire to issue a task management request here.
			 * TM requests MUST be single threaded.
			 * If old eh code and no TM current, issue request.
			 * If new eh code, do nothing. Wait for OS cmd timeout
			 *	for bus reset.
			 */
			ddvtprintk((MYIOC_s_NOTE_FMT "DV Cmd Timeout: NoOp\n", hd->ioc->name));
		} else {
			/* Perform a FW reload */
			if (mpt_HardResetHandler(hd->ioc, NO_SLEEP) < 0) {
				printk(MYIOC_s_WARN_FMT "Firmware Reload FAILED!\n", hd->ioc->name);
			}
		}
	} else {
		/* This should NEVER happen */
		printk(MYIOC_s_WARN_FMT "Null cmdPtr!!!!\n", hd->ioc->name);
	}

	/* No more processing.
	 * TM call will generate an interrupt for SCSI TM Management.
	 * The FW will reply to all outstanding commands, callback will finish cleanup.
	 * Hard reset clean-up will free all resources.
	 */
	ddvprintk((MYIOC_s_WARN_FMT "Timer Expired Complete!\n", hd->ioc->name));

	return;
}

James Bottomley's avatar
James Bottomley committed
5213
#ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
Pam Delaney's avatar
Pam Delaney committed
5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*	mptscsih_do_raid - Format and Issue a RAID volume request message.
 *	@hd: Pointer to scsi host structure
 *	@action: What do be done.
 *	@id: Logical target id.
 *	@bus: Target locations bus.
 *
 *	Returns: < 0 on a fatal error
 *		0 on success
 *
 *	Remark: Wait to return until reply processed by the ISR.
 */
static int
mptscsih_do_raid(MPT_SCSI_HOST *hd, u8 action, INTERNAL_CMD *io)
{
	MpiRaidActionRequest_t	*pReq;
	MPT_FRAME_HDR		*mf;
	int			in_isr;

	in_isr = in_interrupt();
	if (in_isr) {
		dprintk((MYIOC_s_WARN_FMT "Internal raid request not allowed in ISR context!\n",
       				hd->ioc->name));
		return -EPERM;
	}
Linus Torvalds's avatar
Linus Torvalds committed
5239

Pam Delaney's avatar
Pam Delaney committed
5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259
	/* Get and Populate a free Frame
	 */
	if ((mf = mpt_get_msg_frame(ScsiScanDvCtx, hd->ioc->id)) == NULL) {
		ddvprintk((MYIOC_s_WARN_FMT "_do_raid: no msg frames!\n",
					hd->ioc->name));
		return -EAGAIN;
	}
	pReq = (MpiRaidActionRequest_t *)mf;
	pReq->Action = action;
	pReq->Reserved1 = 0;
	pReq->ChainOffset = 0;
	pReq->Function = MPI_FUNCTION_RAID_ACTION;
	pReq->VolumeID = io->id;
	pReq->VolumeBus = io->bus;
	pReq->PhysDiskNum = io->physDiskNum;
	pReq->MsgFlags = 0;
	pReq->Reserved2 = 0;
	pReq->ActionDataWord = 0; /* Reserved for this action */
	//pReq->ActionDataSGE = 0;

5260
	mpt_add_sge((char *)&pReq->ActionDataSGE,
Pam Delaney's avatar
Pam Delaney committed
5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275
		MPT_SGE_FLAGS_SSIMPLE_READ | 0, (dma_addr_t) -1);

	ddvprintk((MYIOC_s_INFO_FMT "RAID Volume action %x id %d\n",
			hd->ioc->name, action, io->id));

	hd->pLocal = NULL;
	hd->timer.expires = jiffies + HZ*2; /* 2 second timeout */
	scandv_wait_done = 0;

	/* Save cmd pointer, for resource free if timeout or
	 * FW reload occurs
	 */
	hd->cmdPtr = mf;

	add_timer(&hd->timer);
5276
	mpt_put_msg_frame(ScsiScanDvCtx, hd->ioc->id, mf);
Pam Delaney's avatar
Pam Delaney committed
5277 5278 5279 5280 5281 5282 5283
	wait_event(scandv_waitq, scandv_wait_done);

	if ((hd->pLocal == NULL) || (hd->pLocal->completion != MPT_SCANDV_GOOD))
		return -1;

	return 0;
}
James Bottomley's avatar
James Bottomley committed
5284
#endif /* ~MPTSCSIH_ENABLE_DOMAIN_VALIDATION */
Pam Delaney's avatar
Pam Delaney committed
5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_do_cmd - Do internal command.
 *	@hd: MPT_SCSI_HOST pointer
 *	@io: INTERNAL_CMD pointer.
 *
 *	Issue the specified internally generated command and do command
 *	specific cleanup. For bus scan / DV only.
 *	NOTES: If command is Inquiry and status is good,
 *	initialize a target structure, save the data
 *
 *	Remark: Single threaded access only.
 *
 *	Return:
 *		< 0 if an illegal command or no resources
 *
 *		   0 if good
 *
 *		 > 0 if command complete but some type of completion error.
 */
static int
mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *io)
{
	MPT_FRAME_HDR	*mf;
	SCSIIORequest_t	*pScsiReq;
	SCSIIORequest_t	 ReqCopy;
	int		 my_idx, ii, dir;
	int		 rc, cmdTimeout;
	int		in_isr;
	char		 cmdLen;
	char		 CDB[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	char		 cmd = io->cmd;

	in_isr = in_interrupt();
	if (in_isr) {
		dprintk((MYIOC_s_WARN_FMT "Internal SCSI IO request not allowed in ISR context!\n",
       				hd->ioc->name));
		return -EPERM;
	}


	/* Set command specific information
	 */
	switch (cmd) {
	case CMD_Inquiry:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
		CDB[4] = io->size;
		cmdTimeout = 10;
		break;

	case CMD_TestUnitReady:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
		cmdTimeout = 10;
		break;

	case CMD_StartStopUnit:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
		CDB[4] = 1;	/*Spin up the disk */
		cmdTimeout = 15;
		break;

5352 5353 5354 5355 5356 5357 5358 5359
	case CMD_RequestSense:
		cmdLen = 6;
		CDB[0] = cmd;
		CDB[4] = io->size;
		dir = MPI_SCSIIO_CONTROL_READ;
		cmdTimeout = 10;
		break;

Pam Delaney's avatar
Pam Delaney committed
5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463
	case CMD_ReadBuffer:
		cmdLen = 10;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
		if (io->flags & MPT_ICFLAG_ECHO) {
			CDB[1] = 0x0A;
		} else {
			CDB[1] = 0x02;
		}

		if (io->flags & MPT_ICFLAG_BUF_CAP) {
			CDB[1] |= 0x01;
		}
		CDB[6] = (io->size >> 16) & 0xFF;
		CDB[7] = (io->size >>  8) & 0xFF;
		CDB[8] = io->size & 0xFF;
		cmdTimeout = 10;
		break;

	case CMD_WriteBuffer:
		cmdLen = 10;
		dir = MPI_SCSIIO_CONTROL_WRITE;
		CDB[0] = cmd;
		if (io->flags & MPT_ICFLAG_ECHO) {
			CDB[1] = 0x0A;
		} else {
			CDB[1] = 0x02;
		}
		CDB[6] = (io->size >> 16) & 0xFF;
		CDB[7] = (io->size >>  8) & 0xFF;
		CDB[8] = io->size & 0xFF;
		cmdTimeout = 10;
		break;

	case CMD_Reserve6:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
		cmdTimeout = 10;
		break;

	case CMD_Release6:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
		cmdTimeout = 10;
		break;

	case CMD_SynchronizeCache:
		cmdLen = 10;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
//		CDB[1] = 0x02;	/* set immediate bit */
		cmdTimeout = 10;
		break;

	default:
		/* Error Case */
		return -EFAULT;
	}

	/* Get and Populate a free Frame
	 */
	if ((mf = mpt_get_msg_frame(ScsiScanDvCtx, hd->ioc->id)) == NULL) {
		ddvprintk((MYIOC_s_WARN_FMT "No msg frames!\n",
					hd->ioc->name));
		return -EBUSY;
	}

	pScsiReq = (SCSIIORequest_t *) mf;

	/* Get the request index */
	my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
	ADD_INDEX_LOG(my_idx); /* for debug */

	if (io->flags & MPT_ICFLAG_PHYS_DISK) {
		pScsiReq->TargetID = io->physDiskNum;
		pScsiReq->Bus = 0;
		pScsiReq->ChainOffset = 0;
		pScsiReq->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
	} else {
		pScsiReq->TargetID = io->id;
		pScsiReq->Bus = io->bus;
		pScsiReq->ChainOffset = 0;
		pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
	}

	pScsiReq->CDBLength = cmdLen;
	pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;

	pScsiReq->Reserved = 0;

	pScsiReq->MsgFlags = mpt_msg_flags();
	/* MsgContext set in mpt_get_msg_fram call  */

	for (ii=0; ii < 8; ii++)
		pScsiReq->LUN[ii] = 0;
	pScsiReq->LUN[1] = io->lun;

	if (io->flags & MPT_ICFLAG_TAGGED_CMD)
		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_SIMPLEQ);
	else
		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);

5464 5465 5466 5467 5468 5469
	if (cmd == CMD_RequestSense) {
		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
		ddvprintk((MYIOC_s_INFO_FMT "Untagged! 0x%2x\n",
			hd->ioc->name, cmd));
	}

Pam Delaney's avatar
Pam Delaney committed
5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512
	for (ii=0; ii < 16; ii++)
		pScsiReq->CDB[ii] = CDB[ii];

	pScsiReq->DataLength = cpu_to_le32(io->size);
	pScsiReq->SenseBufferLowAddr = cpu_to_le32(hd->ioc->sense_buf_low_dma
					   + (my_idx * MPT_SENSE_BUFFER_ALLOC));

	ddvprintk((MYIOC_s_INFO_FMT "Sending Command 0x%x for (%d:%d:%d)\n",
			hd->ioc->name, cmd, io->bus, io->id, io->lun));

	if (dir == MPI_SCSIIO_CONTROL_READ) {
		mpt_add_sge((char *) &pScsiReq->SGL,
			MPT_SGE_FLAGS_SSIMPLE_READ | io->size,
			io->data_dma);
	} else {
		mpt_add_sge((char *) &pScsiReq->SGL,
			MPT_SGE_FLAGS_SSIMPLE_WRITE | io->size,
			io->data_dma);
	}

	/* The ISR will free the request frame, but we need
	 * the information to initialize the target. Duplicate.
	 */
	memcpy(&ReqCopy, pScsiReq, sizeof(SCSIIORequest_t));

	/* Issue this command after:
	 *	finish init
	 *	add timer
	 * Wait until the reply has been received
	 *  ScsiScanDvCtx callback function will
	 *	set hd->pLocal;
	 *	set scandv_wait_done and call wake_up
	 */
	hd->pLocal = NULL;
	hd->timer.expires = jiffies + HZ*cmdTimeout;
	scandv_wait_done = 0;

	/* Save cmd pointer, for resource free if timeout or
	 * FW reload occurs
	 */
	hd->cmdPtr = mf;

	add_timer(&hd->timer);
5513
	mpt_put_msg_frame(ScsiScanDvCtx, hd->ioc->id, mf);
Pam Delaney's avatar
Pam Delaney committed
5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550
	wait_event(scandv_waitq, scandv_wait_done);

	if (hd->pLocal) {
		rc = hd->pLocal->completion;
		hd->pLocal->skip = 0;

		/* Always set fatal error codes in some cases.
		 */
		if (rc == MPT_SCANDV_SELECTION_TIMEOUT)
			rc = -ENXIO;
		else if (rc == MPT_SCANDV_SOME_ERROR)
			rc =  -rc;
	} else {
		rc = -EFAULT;
		/* This should never happen. */
		ddvprintk((MYIOC_s_INFO_FMT "_do_cmd: Null pLocal!!!\n",
				hd->ioc->name));
	}

	return rc;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_synchronize_cache - Send SYNCHRONIZE_CACHE to all disks.
 *	@hd: Pointer to MPT_SCSI_HOST structure
 *	@portnum: IOC port number
 *
 *	Uses the ISR, but with special processing.
 *	MUST be single-threaded.
 *
 *	Return: 0 on completion
 */
static int
mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, int portnum)
{
	MPT_ADAPTER		*ioc= hd->ioc;
5551
	VirtDevice		*pTarget;
Pam Delaney's avatar
Pam Delaney committed
5552 5553 5554 5555 5556 5557 5558
	SCSIDevicePage1_t	*pcfg1Data = NULL;
	INTERNAL_CMD		 iocmd;
	CONFIGPARMS		 cfg;
	dma_addr_t		 cfg1_dma_addr = -1;
	ConfigPageHeader_t	 header1;
	int			 bus = 0;
	int			 id = 0;
5559 5560
	int			 lun;
	int			 indexed_lun, lun_index;
Pam Delaney's avatar
Pam Delaney committed
5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589
	int			 hostId = ioc->pfacts[portnum].PortSCSIID;
	int			 max_id;
	int			 requested, configuration, data;
	int			 doConfig = 0;
	u8			 flags, factor;

	max_id = ioc->sh->max_id - 1;

	/* Following parameters will not change
	 * in this routine.
	 */
	iocmd.cmd = CMD_SynchronizeCache;
	iocmd.flags = 0;
	iocmd.physDiskNum = -1;
	iocmd.data = NULL;
	iocmd.data_dma = -1;
	iocmd.size = 0;
	iocmd.rsvd = iocmd.rsvd2 = 0;

	/* No SCSI hosts
	 */
	if (hd->Targets == NULL)
		return 0;

	/* Skip the host
	 */
	if (id == hostId)
		id++;

5590
	/* Write SDP1 for all SCSI devices
Pam Delaney's avatar
Pam Delaney committed
5591 5592 5593 5594 5595 5596
	 * Alloc memory and set up config buffer
	 */
	if (hd->is_spi) {
		if (ioc->spi_data.sdp1length > 0) {
			pcfg1Data = (SCSIDevicePage1_t *)pci_alloc_consistent(ioc->pcidev,
					 ioc->spi_data.sdp1length * 4, &cfg1_dma_addr);
James Bottomley's avatar
James Bottomley committed
5597

Pam Delaney's avatar
Pam Delaney committed
5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628
			if (pcfg1Data != NULL) {
				doConfig = 1;
				header1.PageVersion = ioc->spi_data.sdp1version;
				header1.PageLength = ioc->spi_data.sdp1length;
				header1.PageNumber = 1;
				header1.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
				cfg.hdr = &header1;
				cfg.physAddr = cfg1_dma_addr;
				cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
				cfg.dir = 1;
				cfg.timeout = 0;
			}
		}
	}

	/* loop through all devices on this port
	 */
	while (bus < MPT_MAX_BUS) {
		iocmd.bus = bus;
		iocmd.id = id;
		pTarget = hd->Targets[(int)id];

		if (doConfig) {

			/* Set the negotiation flags */
			if (pTarget && (pTarget = hd->Targets[id]) && !pTarget->raidVolume) {
				flags = pTarget->negoFlags;
			} else {
				flags = hd->ioc->spi_data.noQas;
				if (hd->ioc->spi_data.nvram && (hd->ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
					data = hd->ioc->spi_data.nvram[id];
James Bottomley's avatar
James Bottomley committed
5629

Pam Delaney's avatar
Pam Delaney committed
5630 5631 5632 5633 5634 5635 5636 5637
					if (data & MPT_NVRAM_WIDE_DISABLE)
						flags |= MPT_TARGET_NO_NEGO_WIDE;

					factor = (data & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
					if ((factor == 0) || (factor == MPT_ASYNC))
						flags |= MPT_TARGET_NO_NEGO_SYNC;
				}
			}
James Bottomley's avatar
James Bottomley committed
5638

Pam Delaney's avatar
Pam Delaney committed
5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650
			/* Force to async, narrow */
			mptscsih_setDevicePage1Flags(0, MPT_ASYNC, 0, &requested,
					&configuration, flags);
			pcfg1Data->RequestedParameters = le32_to_cpu(requested);
			pcfg1Data->Reserved = 0;
			pcfg1Data->Configuration = le32_to_cpu(configuration);
			cfg.pageAddr = (bus<<8) | id;
			mpt_config(hd->ioc, &cfg);
		}

		/* If target Ptr NULL or if this target is NOT a disk, skip.
		 */
5651
		if ((pTarget) && (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)){
Pam Delaney's avatar
Pam Delaney committed
5652 5653 5654
			for (lun=0; lun <= MPT_LAST_LUN; lun++) {
				/* If LUN present, issue the command
				 */
5655 5656 5657
				lun_index = (lun >> 5);  /* 32 luns per lun_index */
				indexed_lun = (lun % 32);
				if (pTarget->luns[lun_index] & (1<<indexed_lun)) {
Pam Delaney's avatar
Pam Delaney committed
5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682
					iocmd.lun = lun;
					(void) mptscsih_do_cmd(hd, &iocmd);
				}
			}
		}

		/* get next relevant device */
		id++;

		if (id == hostId)
			id++;

		if (id > max_id) {
			id = 0;
			bus++;
		}
	}

	if (pcfg1Data) {
		pci_free_consistent(ioc->pcidev, header1.PageLength * 4, pcfg1Data, cfg1_dma_addr);
	}

	return 0;
}

James Bottomley's avatar
James Bottomley committed
5683
#ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
Pam Delaney's avatar
Pam Delaney committed
5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_domainValidation - Top level handler for domain validation.
 *	@hd: Pointer to MPT_SCSI_HOST structure.
 *
 *	Uses the ISR, but with special processing.
 *	Called from schedule, should not be in interrupt mode.
 *	While thread alive, do dv for all devices needing dv
 *
 *	Return: None.
 */
static void
mptscsih_domainValidation(void *arg)
{
5698 5699
	MPT_SCSI_HOST		*hd;
	MPT_ADAPTER		*ioc;
Pam Delaney's avatar
Pam Delaney committed
5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713
	unsigned long		 flags;
	int 			 id, maxid, dvStatus, did;
	int			 ii, isPhysDisk;

	spin_lock_irqsave(&dvtaskQ_lock, flags);
	dvtaskQ_active = 1;
	if (dvtaskQ_release) {
		dvtaskQ_active = 0;
		spin_unlock_irqrestore(&dvtaskQ_lock, flags);
		return;
	}
	spin_unlock_irqrestore(&dvtaskQ_lock, flags);

	/* For this ioc, loop through all devices and do dv to each device.
5714
	 * When complete with this ioc, search through the ioc list, and
Pam Delaney's avatar
Pam Delaney committed
5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735
	 * for each scsi ioc found, do dv for all devices. Exit when no
	 * device needs dv.
	 */
	did = 1;
	while (did) {
		did = 0;
		for (ioc = mpt_adapter_find_first(); ioc != NULL; ioc = mpt_adapter_find_next(ioc)) {
			spin_lock_irqsave(&dvtaskQ_lock, flags);
			if (dvtaskQ_release) {
				dvtaskQ_active = 0;
				spin_unlock_irqrestore(&dvtaskQ_lock, flags);
				return;
			}
			spin_unlock_irqrestore(&dvtaskQ_lock, flags);

			set_current_state(TASK_INTERRUPTIBLE);
			schedule_timeout(HZ/4);

			/* DV only to SCSI adapters */
			if ((int)ioc->chip_type <= (int)FC929)
				continue;
James Bottomley's avatar
James Bottomley committed
5736

Pam Delaney's avatar
Pam Delaney committed
5737 5738 5739 5740 5741 5742 5743 5744
			/* Make sure everything looks ok */
			if (ioc->sh == NULL)
				continue;

			hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
			if (hd == NULL)
				continue;

5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761
			if ((ioc->spi_data.forceDv & MPT_SCSICFG_RELOAD_IOC_PG3) != 0) {
				mpt_read_ioc_pg_3(ioc);
				if (ioc->spi_data.pIocPg3) {
					Ioc3PhysDisk_t *pPDisk = ioc->spi_data.pIocPg3->PhysDisk;
					int		numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;

					while (numPDisk) {
						if (ioc->spi_data.dvStatus[pPDisk->PhysDiskID] & MPT_SCSICFG_DV_NOT_DONE)
							ioc->spi_data.dvStatus[pPDisk->PhysDiskID] |= MPT_SCSICFG_NEED_DV;

						pPDisk++;
						numPDisk--;
					}
				}
				ioc->spi_data.forceDv &= ~MPT_SCSICFG_RELOAD_IOC_PG3;
			}

5762
			maxid = min_t(int, ioc->sh->max_id, MPT_MAX_SCSI_DEVICES);
Pam Delaney's avatar
Pam Delaney committed
5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774

			for (id = 0; id < maxid; id++) {
				spin_lock_irqsave(&dvtaskQ_lock, flags);
				if (dvtaskQ_release) {
					dvtaskQ_active = 0;
					spin_unlock_irqrestore(&dvtaskQ_lock, flags);
					return;
				}
				spin_unlock_irqrestore(&dvtaskQ_lock, flags);
				dvStatus = hd->ioc->spi_data.dvStatus[id];

				if (dvStatus & MPT_SCSICFG_NEED_DV) {
5775
					did++;
Pam Delaney's avatar
Pam Delaney committed
5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794
					hd->ioc->spi_data.dvStatus[id] |= MPT_SCSICFG_DV_PENDING;
					hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_NEED_DV;

					set_current_state(TASK_INTERRUPTIBLE);
					schedule_timeout(HZ/4);

					/* If hidden phys disk, block IO's to all
					 *	raid volumes
					 * else, process normally
					 */
					isPhysDisk = mptscsih_is_phys_disk(ioc, id);
					if (isPhysDisk) {
						for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
							if (hd->ioc->spi_data.isRaid & (1 << ii)) {
								hd->ioc->spi_data.dvStatus[ii] |= MPT_SCSICFG_DV_PENDING;
							}
						}
					}

5795 5796 5797 5798 5799 5800 5801 5802 5803 5804
					if (mptscsih_doDv(hd, 0, id) == 1) {
						/* Untagged device was busy, try again
						 */
						hd->ioc->spi_data.dvStatus[id] |= MPT_SCSICFG_NEED_DV;
						hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_DV_PENDING;
					} else {
						/* DV is complete. Clear flags.
						 */
						hd->ioc->spi_data.dvStatus[id] &= ~(MPT_SCSICFG_DV_NOT_DONE | MPT_SCSICFG_DV_PENDING);
					}
Pam Delaney's avatar
Pam Delaney committed
5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818

					if (isPhysDisk) {
						for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
							if (hd->ioc->spi_data.isRaid & (1 << ii)) {
								hd->ioc->spi_data.dvStatus[ii] &= ~MPT_SCSICFG_DV_PENDING;
							}
						}
					}

					/* Post OS IOs that were pended while
					 * DV running.
					 */
					post_pendingQ_commands(hd);

5819
					if (hd->ioc->spi_data.noQas)
5820
						mptscsih_qas_check(hd, id);
Pam Delaney's avatar
Pam Delaney committed
5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850
				}
			}
		}
	}

	spin_lock_irqsave(&dvtaskQ_lock, flags);
	dvtaskQ_active = 0;
	spin_unlock_irqrestore(&dvtaskQ_lock, flags);

	return;
}

/* Search IOC page 3 to determine if this is hidden physical disk
 */
static int mptscsih_is_phys_disk(MPT_ADAPTER *ioc, int id)
{
	if (ioc->spi_data.pIocPg3) {
		Ioc3PhysDisk_t *pPDisk =  ioc->spi_data.pIocPg3->PhysDisk;
		int		numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;

		while (numPDisk) {
			if (pPDisk->PhysDiskID == id) {
				return 1;
			}
			pPDisk++;
			numPDisk--;
		}
	}
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
5851

Pam Delaney's avatar
Pam Delaney committed
5852 5853
/* Write SDP1 if no QAS has been enabled
 */
5854
static void mptscsih_qas_check(MPT_SCSI_HOST *hd, int id)
Pam Delaney's avatar
Pam Delaney committed
5855
{
5856
	VirtDevice *pTarget;
Pam Delaney's avatar
Pam Delaney committed
5857
	int ii;
Linus Torvalds's avatar
Linus Torvalds committed
5858

Pam Delaney's avatar
Pam Delaney committed
5859 5860 5861 5862
	if (hd->Targets == NULL)
		return;

	for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
5863 5864 5865
		if (ii == id)
			continue;

5866 5867 5868
		if ((hd->ioc->spi_data.dvStatus[ii] & MPT_SCSICFG_DV_NOT_DONE) != 0)
			continue;

Pam Delaney's avatar
Pam Delaney committed
5869 5870 5871 5872 5873
		pTarget = hd->Targets[ii];

		if ((pTarget != NULL) && (!pTarget->raidVolume)) {
			if ((pTarget->negoFlags & hd->ioc->spi_data.noQas) == 0) {
				pTarget->negoFlags |= hd->ioc->spi_data.noQas;
5874
				mptscsih_writeSDP1(hd, 0, ii, 0);
Pam Delaney's avatar
Pam Delaney committed
5875 5876 5877
			}
		} else {
			if (mptscsih_is_phys_disk(hd->ioc, ii) == 1)
5878
				mptscsih_writeSDP1(hd, 0, ii, MPT_SCSICFG_USE_NVRAM);
Pam Delaney's avatar
Pam Delaney committed
5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891
		}
	}
	return;
}



#define MPT_GET_NVRAM_VALS	0x01
#define MPT_UPDATE_MAX		0x02
#define MPT_SET_MAX		0x04
#define MPT_SET_MIN		0x08
#define MPT_FALLBACK		0x10
#define MPT_SAVE		0x20
Linus Torvalds's avatar
Linus Torvalds committed
5892 5893

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
5894 5895 5896 5897 5898 5899 5900 5901 5902
/**
 *	mptscsih_doDv - Perform domain validation to a target.
 *	@hd: Pointer to MPT_SCSI_HOST structure.
 *	@portnum: IOC port number.
 *	@target: Physical ID of this target
 *
 *	Uses the ISR, but with special processing.
 *	MUST be single-threaded.
 *	Test will exit if target is at async & narrow.
Linus Torvalds's avatar
Linus Torvalds committed
5903
 *
Pam Delaney's avatar
Pam Delaney committed
5904
 *	Return: None.
Linus Torvalds's avatar
Linus Torvalds committed
5905
 */
5906
static int
5907
mptscsih_doDv(MPT_SCSI_HOST *hd, int bus_number, int id)
Linus Torvalds's avatar
Linus Torvalds committed
5908
{
Pam Delaney's avatar
Pam Delaney committed
5909
	MPT_ADAPTER		*ioc = hd->ioc;
5910 5911 5912 5913 5914 5915
	VirtDevice		*pTarget;
	SCSIDevicePage1_t	*pcfg1Data;
	SCSIDevicePage0_t	*pcfg0Data;
	u8			*pbuf1;
	u8			*pbuf2;
	u8			*pDvBuf;
Pam Delaney's avatar
Pam Delaney committed
5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933
	dma_addr_t		 dvbuf_dma = -1;
	dma_addr_t		 buf1_dma = -1;
	dma_addr_t		 buf2_dma = -1;
	dma_addr_t		 cfg1_dma_addr = -1;
	dma_addr_t		 cfg0_dma_addr = -1;
	ConfigPageHeader_t	 header1;
	ConfigPageHeader_t	 header0;
	DVPARAMETERS		 dv;
	INTERNAL_CMD		 iocmd;
	CONFIGPARMS		 cfg;
	int			 dv_alloc = 0;
	int			 rc, sz = 0;
	int			 bufsize = 0;
	int			 dataBufSize = 0;
	int			 echoBufSize = 0;
	int			 notDone;
	int			 patt;
	int			 repeat;
5934
	int			 retcode = 0;
5935
	int			 nfactor =  MPT_ULTRA320;
Pam Delaney's avatar
Pam Delaney committed
5936 5937 5938 5939 5940 5941 5942
	char			 firstPass = 1;
	char			 doFallback = 0;
	char			 readPage0;
	char			 bus, lun;
	char			 inq0 = 0;

	if (ioc->spi_data.sdp1length == 0)
5943
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
5944

Pam Delaney's avatar
Pam Delaney committed
5945
	if (ioc->spi_data.sdp0length == 0)
5946
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
5947

5948 5949 5950 5951
	/* If multiple buses are used, require that the initiator
	 * id be the same on all buses.
	 */
	if (id == ioc->pfacts[0].PortSCSIID)
5952
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
5953

Pam Delaney's avatar
Pam Delaney committed
5954
	lun = 0;
5955
	bus = (u8) bus_number;
5956
	ddvtprintk((MYIOC_s_NOTE_FMT
5957 5958
			"DV started: bus=%d, id %d dv @ %p\n",
			ioc->name, bus, id, &dv));
Pam Delaney's avatar
Pam Delaney committed
5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971

	/* Prep DV structure
	 */
	memset (&dv, 0, sizeof(DVPARAMETERS));
	dv.id = id;

	/* Populate tmax with the current maximum
	 * transfer parameters for this target.
	 * Exit if narrow and async.
	 */
	dv.cmd = MPT_GET_NVRAM_VALS;
	mptscsih_dv_parms(hd, &dv, NULL);
	if ((!dv.max.width) && (!dv.max.offset))
5972
		return 0;
Pam Delaney's avatar
Pam Delaney committed
5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983

	/* Prep SCSI IO structure
	 */
	iocmd.id = id;
	iocmd.bus = bus;
	iocmd.lun = lun;
	iocmd.flags = 0;
	iocmd.physDiskNum = -1;
	iocmd.rsvd = iocmd.rsvd2 = 0;

	pTarget = hd->Targets[id];
5984
	if (pTarget && (pTarget->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY)) {
Pam Delaney's avatar
Pam Delaney committed
5985 5986 5987
		/* Another GEM workaround. Check peripheral device type,
		 * if PROCESSOR, quit DV.
		 */
5988
		if (((pTarget->inq_data[0] & 0x1F) == 0x03) || ((pTarget->inq_data[0] & 0x1F) > 0x08)) {
Pam Delaney's avatar
Pam Delaney committed
5989
			pTarget->negoFlags |= (MPT_TARGET_NO_NEGO_WIDE | MPT_TARGET_NO_NEGO_SYNC);
5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005
			return 0;
		}
	}

	/* Use tagged commands if possible.
	 */
	if (pTarget) {
		if (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)
			iocmd.flags |= MPT_ICFLAG_TAGGED_CMD;
		else {
			if (hd->ioc->facts.FWVersion.Word < 0x01000600)
				return 0;

			if ((hd->ioc->facts.FWVersion.Word >= 0x01010000) &&
				(hd->ioc->facts.FWVersion.Word < 0x01010B00))
				return 0;
Pam Delaney's avatar
Pam Delaney committed
6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034
		}
	}

	/* Prep cfg structure
	 */
	cfg.pageAddr = (bus<<8) | id;
	cfg.hdr = NULL;

	/* Prep SDP0 header
	 */
	header0.PageVersion = ioc->spi_data.sdp0version;
	header0.PageLength = ioc->spi_data.sdp0length;
	header0.PageNumber = 0;
	header0.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;

	/* Prep SDP1 header
	 */
	header1.PageVersion = ioc->spi_data.sdp1version;
	header1.PageLength = ioc->spi_data.sdp1length;
	header1.PageNumber = 1;
	header1.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;

	if (header0.PageLength & 1)
		dv_alloc = (header0.PageLength * 4) + 4;

	dv_alloc +=  (2048 + (header1.PageLength * 4));

	pDvBuf = pci_alloc_consistent(ioc->pcidev, dv_alloc, &dvbuf_dma);
	if (pDvBuf == NULL)
6035
		return 0;
Pam Delaney's avatar
Pam Delaney committed
6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059

	sz = 0;
	pbuf1 = (u8 *)pDvBuf;
	buf1_dma = dvbuf_dma;
	sz +=1024;

	pbuf2 = (u8 *) (pDvBuf + sz);
	buf2_dma = dvbuf_dma + sz;
	sz +=1024;

	pcfg0Data = (SCSIDevicePage0_t *) (pDvBuf + sz);
	cfg0_dma_addr = dvbuf_dma + sz;
	sz += header0.PageLength * 4;

	/* 8-byte alignment
	 */
	if (header0.PageLength & 1)
		sz += 4;

	pcfg1Data = (SCSIDevicePage1_t *) (pDvBuf + sz);
	cfg1_dma_addr = dvbuf_dma + sz;

	/* Skip this ID? Set cfg.hdr to force config page write
	 */
6060 6061 6062 6063 6064 6065 6066
	{
		ScsiCfgData *pspi_data = &hd->ioc->spi_data;
		if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
			/* Set the factor from nvram */
			nfactor = (pspi_data->nvram[id] & MPT_NVRAM_SYNC_MASK) >> 8;
			if (nfactor < pspi_data->minSyncFactor )
				nfactor = pspi_data->minSyncFactor;
Pam Delaney's avatar
Pam Delaney committed
6067

6068 6069
			if (!(pspi_data->nvram[id] & MPT_NVRAM_ID_SCAN_ENABLE) ||
				(pspi_data->PortFlags == MPI_SCSIPORTPAGE2_PORT_FLAGS_OFF_DV) ) {
Pam Delaney's avatar
Pam Delaney committed
6070

6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087
				ddvprintk((MYIOC_s_NOTE_FMT "DV Skipped: bus, id, lun (%d, %d, %d)\n",
					ioc->name, bus, id, lun));

				dv.cmd = MPT_SET_MAX;
				mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
				cfg.hdr = &header1;

				/* Save the final negotiated settings to
				 * SCSI device page 1.
				 */
				cfg.physAddr = cfg1_dma_addr;
				cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
				cfg.dir = 1;
				mpt_config(hd->ioc, &cfg);
				goto target_done;
			}
		}
Pam Delaney's avatar
Pam Delaney committed
6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134
	}

	/* Finish iocmd inititialization - hidden or visible disk? */
	if (ioc->spi_data.pIocPg3) {
		/* Searc IOC page 3 for matching id
		 */
		Ioc3PhysDisk_t *pPDisk =  ioc->spi_data.pIocPg3->PhysDisk;
		int		numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;

		while (numPDisk) {
			if (pPDisk->PhysDiskID == id) {
				/* match */
				iocmd.flags |= MPT_ICFLAG_PHYS_DISK;
				iocmd.physDiskNum = pPDisk->PhysDiskNum;

				/* Quiesce the IM
				 */
				if (mptscsih_do_raid(hd, MPI_RAID_ACTION_QUIESCE_PHYS_IO, &iocmd) < 0) {
					ddvprintk((MYIOC_s_ERR_FMT "RAID Queisce FAILED!\n", ioc->name));
					goto target_done;
				}
				break;
			}
			pPDisk++;
			numPDisk--;
		}
	}

	/* RAID Volume ID's may double for a physical device. If RAID but
	 * not a physical ID as well, skip DV.
	 */
	if ((hd->ioc->spi_data.isRaid & (1 << id)) && !(iocmd.flags & MPT_ICFLAG_PHYS_DISK))
		goto target_done;


	/* Basic Test.
	 * Async & Narrow - Inquiry
	 * Async & Narrow - Inquiry
	 * Maximum transfer rate - Inquiry
	 * Compare buffers:
	 *	If compare, test complete.
	 *	If miscompare and first pass, repeat
	 *	If miscompare and not first pass, fall back and repeat
	 */
	hd->pLocal = NULL;
	readPage0 = 0;
	sz = SCSI_STD_INQUIRY_BYTES;
6135
	rc = MPT_SCANDV_GOOD;
Pam Delaney's avatar
Pam Delaney committed
6136
	while (1) {
6137
		ddvprintk((MYIOC_s_NOTE_FMT "DV: Start Basic test on id=%d\n", ioc->name, id));
6138
		retcode = 0;
Pam Delaney's avatar
Pam Delaney committed
6139 6140 6141 6142 6143 6144 6145 6146 6147 6148
		dv.cmd = MPT_SET_MIN;
		mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);

		cfg.hdr = &header1;
		cfg.physAddr = cfg1_dma_addr;
		cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
		cfg.dir = 1;
		if (mpt_config(hd->ioc, &cfg) != 0)
			goto target_done;

6149 6150
		/* Wide - narrow - wide workaround case
		 */
6151
		if ((rc == MPT_SCANDV_ISSUE_SENSE) && dv.max.width) {
6152 6153 6154
			/* Send an untagged command to reset disk Qs corrupted
			 * when a parity error occurs on a Request Sense.
			 */
6155
			if ((hd->ioc->facts.FWVersion.Word >= 0x01000600) ||
6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170
				((hd->ioc->facts.FWVersion.Word >= 0x01010000) &&
				(hd->ioc->facts.FWVersion.Word < 0x01010B00)) ) {

				iocmd.cmd = CMD_RequestSense;
				iocmd.data_dma = buf1_dma;
				iocmd.data = pbuf1;
				iocmd.size = 0x12;
				if (mptscsih_do_cmd(hd, &iocmd) < 0)
					goto target_done;
				else {
					if (hd->pLocal == NULL)
						goto target_done;
					rc = hd->pLocal->completion;
					if ((rc == MPT_SCANDV_GOOD) || (rc == MPT_SCANDV_SENSE)) {
						dv.max.width = 0;
6171
						doFallback = 0;
6172 6173 6174 6175 6176 6177 6178
					} else
						goto target_done;
				}
			} else
				goto target_done;
		}

Pam Delaney's avatar
Pam Delaney committed
6179 6180 6181 6182 6183 6184
		iocmd.cmd = CMD_Inquiry;
		iocmd.data_dma = buf1_dma;
		iocmd.data = pbuf1;
		iocmd.size = sz;
		if (mptscsih_do_cmd(hd, &iocmd) < 0)
			goto target_done;
6185 6186 6187 6188 6189 6190
		else {
			if (hd->pLocal == NULL)
				goto target_done;
			rc = hd->pLocal->completion;
			if (rc == MPT_SCANDV_GOOD) {
				if (hd->pLocal->scsiStatus == STS_BUSY) {
6191 6192 6193 6194 6195
					if ((iocmd.flags & MPT_ICFLAG_TAGGED_CMD) == 0)
						retcode = 1;
					else
						retcode = 0;

6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207
					goto target_done;
				}
			} else if  (rc == MPT_SCANDV_SENSE) {
				;
			} else {
				/* If first command doesn't complete
				 * with a good status or with a check condition,
				 * exit.
				 */
				goto target_done;
			}
		}
Pam Delaney's avatar
Pam Delaney committed
6208

6209 6210 6211 6212 6213 6214 6215 6216
		/* Reset the size for disks
		 */
		inq0 = (*pbuf1) & 0x1F;
		if ((inq0 == 0) && pTarget && !pTarget->raidVolume) {
			sz = 0x40;
			iocmd.size = sz;
		}

Pam Delaney's avatar
Pam Delaney committed
6217 6218 6219 6220 6221 6222 6223 6224 6225
		/* Another GEM workaround. Check peripheral device type,
		 * if PROCESSOR, quit DV.
		 */
		if (((pbuf1[0] & 0x1F) == 0x03) || ((pbuf1[0] & 0x1F) > 0x08))
			goto target_done;

		if (mptscsih_do_cmd(hd, &iocmd) < 0)
			goto target_done;

6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247
		if (sz == 0x40) {
			if ((pTarget->maxWidth == 1) && (pTarget->maxOffset) && (nfactor < 0x0A)
				&& (pTarget->minSyncFactor > 0x09)) {
				if ((pbuf1[56] & 0x04) == 0)
					;
				else if ((pbuf1[56] & 0x01) == 1) {
					pTarget->minSyncFactor =
					    nfactor > MPT_ULTRA320 ? nfactor : MPT_ULTRA320;
				} else {
					pTarget->minSyncFactor =
					    nfactor > MPT_ULTRA160 ? nfactor : MPT_ULTRA160;
				}

				dv.max.factor = pTarget->minSyncFactor;

				if ((pbuf1[56] & 0x02) == 0) {
					pTarget->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
					hd->ioc->spi_data.noQas = MPT_TARGET_NO_NEGO_QAS;
				}
			}
		}

Pam Delaney's avatar
Pam Delaney committed
6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284
		if (doFallback)
			dv.cmd = MPT_FALLBACK;
		else
			dv.cmd = MPT_SET_MAX;

		mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
		if (mpt_config(hd->ioc, &cfg) != 0)
			goto target_done;

		if ((!dv.now.width) && (!dv.now.offset))
			goto target_done;

		iocmd.cmd = CMD_Inquiry;
		iocmd.data_dma = buf2_dma;
		iocmd.data = pbuf2;
		iocmd.size = sz;
		if (mptscsih_do_cmd(hd, &iocmd) < 0)
			goto target_done;
		else if (hd->pLocal == NULL)
			goto target_done;
		else {
			/* Save the return code.
			 * If this is the first pass,
			 * read SCSI Device Page 0
			 * and update the target max parameters.
			 */
			rc = hd->pLocal->completion;
			doFallback = 0;
			if (rc == MPT_SCANDV_GOOD) {
				if (!readPage0) {
					u32 sdp0_info;
					u32 sdp0_nego;

					cfg.hdr = &header0;
					cfg.physAddr = cfg0_dma_addr;
					cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
					cfg.dir = 0;
6285

Pam Delaney's avatar
Pam Delaney committed
6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296
					if (mpt_config(hd->ioc, &cfg) != 0)
						goto target_done;

					sdp0_info = le32_to_cpu(pcfg0Data->Information) & 0x0E;
					sdp0_nego = (le32_to_cpu(pcfg0Data->NegotiatedParameters) & 0xFF00 ) >> 8;

					/* Quantum and Fujitsu workarounds.
					 * Quantum: PPR U320 -> PPR reply with Ultra2 and wide
					 * Fujitsu: PPR U320 -> Msg Reject and Ultra2 and wide
					 * Resetart with a request for U160.
					 */
6297
					if ((dv.now.factor == MPT_ULTRA320) && (sdp0_nego == MPT_ULTRA2)) {
Pam Delaney's avatar
Pam Delaney committed
6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320
							doFallback = 1;
					} else {
						dv.cmd = MPT_UPDATE_MAX;
						mptscsih_dv_parms(hd, &dv, (void *)pcfg0Data);
						/* Update the SCSI device page 1 area
						 */
						pcfg1Data->RequestedParameters = pcfg0Data->NegotiatedParameters;
						readPage0 = 1;
					}
				}

				/* Quantum workaround. Restart this test will the fallback
				 * flag set.
				 */
				if (doFallback == 0) {
					if (memcmp(pbuf1, pbuf2, sz) != 0) {
						if (!firstPass)
							doFallback = 1;
					} else
						break;	/* test complete */
				}


6321
			} else if (rc == MPT_SCANDV_ISSUE_SENSE)
6322 6323
				doFallback = 1;	/* set fallback flag */
			else if ((rc == MPT_SCANDV_DID_RESET) || (rc == MPT_SCANDV_SENSE))
Pam Delaney's avatar
Pam Delaney committed
6324 6325 6326 6327 6328 6329 6330
				doFallback = 1;	/* set fallback flag */
			else
				goto target_done;

			firstPass = 0;
		}
	}
6331
	ddvprintk((MYIOC_s_NOTE_FMT "DV: Basic test on id=%d completed OK.\n", ioc->name, id));
Pam Delaney's avatar
Pam Delaney committed
6332 6333 6334 6335 6336 6337 6338
	inq0 = (*pbuf1) & 0x1F;

	/* Continue only for disks
	 */
	if (inq0 != 0)
		goto target_done;

6339 6340 6341
	if ( ioc->spi_data.PortFlags == MPI_SCSIPORTPAGE2_PORT_FLAGS_BASIC_DV_ONLY )
		goto target_done;

Pam Delaney's avatar
Pam Delaney committed
6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484
	/* Start the Enhanced Test.
	 * 0) issue TUR to clear out check conditions
	 * 1) read capacity of echo (regular) buffer
	 * 2) reserve device
	 * 3) do write-read-compare data pattern test
	 * 4) release
	 * 5) update nego parms to target struct
	 */
	cfg.hdr = &header1;
	cfg.physAddr = cfg1_dma_addr;
	cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
	cfg.dir = 1;

	iocmd.cmd = CMD_TestUnitReady;
	iocmd.data_dma = -1;
	iocmd.data = NULL;
	iocmd.size = 0;
	notDone = 1;
	while (notDone) {
		if (mptscsih_do_cmd(hd, &iocmd) < 0)
			goto target_done;

		if (hd->pLocal == NULL)
			goto target_done;

		rc = hd->pLocal->completion;
		if (rc == MPT_SCANDV_GOOD)
			notDone = 0;
		else if (rc == MPT_SCANDV_SENSE) {
			u8 skey = hd->pLocal->sense[2] & 0x0F;
			u8 asc = hd->pLocal->sense[12];
			u8 ascq = hd->pLocal->sense[13];
			ddvprintk((MYIOC_s_INFO_FMT
				"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
				ioc->name, skey, asc, ascq));

			if (skey == SK_UNIT_ATTENTION)
				notDone++; /* repeat */
			else if ((skey == SK_NOT_READY) &&
					(asc == 0x04)&&(ascq == 0x01)) {
				/* wait then repeat */
				mdelay (2000);
				notDone++;
			} else if ((skey == SK_NOT_READY) && (asc == 0x3A)) {
				/* no medium, try read test anyway */
				notDone = 0;
			} else {
				/* All other errors are fatal.
				 */
				ddvprintk((MYIOC_s_INFO_FMT "DV: fatal error.",
						ioc->name));
				goto target_done;
			}
		} else
			goto target_done;
	}

	iocmd.cmd = CMD_ReadBuffer;
	iocmd.data_dma = buf1_dma;
	iocmd.data = pbuf1;
	iocmd.size = 4;
	iocmd.flags |= MPT_ICFLAG_BUF_CAP;

	dataBufSize = 0;
	echoBufSize = 0;
	for (patt = 0; patt < 2; patt++) {
		if (patt == 0)
			iocmd.flags |= MPT_ICFLAG_ECHO;
		else
			iocmd.flags &= ~MPT_ICFLAG_ECHO;

		notDone = 1;
		while (notDone) {
			bufsize = 0;

			/* If not ready after 8 trials,
			 * give up on this device.
			 */
			if (notDone > 8)
				goto target_done;

			if (mptscsih_do_cmd(hd, &iocmd) < 0)
				goto target_done;
			else if (hd->pLocal == NULL)
				goto target_done;
			else {
				rc = hd->pLocal->completion;
				ddvprintk(("ReadBuffer Comp Code %d", rc));
				ddvprintk(("  buff: %0x %0x %0x %0x\n",
					pbuf1[0], pbuf1[1], pbuf1[2], pbuf1[3]));

				if (rc == MPT_SCANDV_GOOD) {
					notDone = 0;
					if (iocmd.flags & MPT_ICFLAG_ECHO) {
						bufsize =  ((pbuf1[2] & 0x1F) <<8) | pbuf1[3];
					} else {
						bufsize =  pbuf1[1]<<16 | pbuf1[2]<<8 | pbuf1[3];
					}
				} else if (rc == MPT_SCANDV_SENSE) {
					u8 skey = hd->pLocal->sense[2] & 0x0F;
					u8 asc = hd->pLocal->sense[12];
					u8 ascq = hd->pLocal->sense[13];
					ddvprintk((MYIOC_s_INFO_FMT
						"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
						ioc->name, skey, asc, ascq));
					if (skey == SK_ILLEGAL_REQUEST) {
						notDone = 0;
					} else if (skey == SK_UNIT_ATTENTION) {
						notDone++; /* repeat */
					} else if ((skey == SK_NOT_READY) &&
						(asc == 0x04)&&(ascq == 0x01)) {
						/* wait then repeat */
						mdelay (2000);
						notDone++;
					} else {
						/* All other errors are fatal.
						 */
						ddvprintk((MYIOC_s_INFO_FMT "DV: fatal error.",
							ioc->name));
						goto target_done;
					}
				} else {
					/* All other errors are fatal
					 */
					goto target_done;
				}
			}
		}

		if (iocmd.flags & MPT_ICFLAG_ECHO)
			echoBufSize = bufsize;
		else
			dataBufSize = bufsize;
	}
	sz = 0;
	iocmd.flags &= ~MPT_ICFLAG_BUF_CAP;

	/* Use echo buffers if possible,
	 * Exit if both buffers are 0.
	 */
	if (echoBufSize > 0) {
		iocmd.flags |= MPT_ICFLAG_ECHO;
		if (dataBufSize > 0)
6485
			bufsize = min(echoBufSize, dataBufSize);
Pam Delaney's avatar
Pam Delaney committed
6486 6487 6488 6489 6490 6491 6492 6493 6494 6495
		else
			bufsize = echoBufSize;
	} else if (dataBufSize == 0)
		goto target_done;

	ddvprintk((MYIOC_s_INFO_FMT "%s Buffer Capacity %d\n", ioc->name,
		(iocmd.flags & MPT_ICFLAG_ECHO) ? "Echo" : " ", bufsize));

	/* Data buffers for write-read-compare test max 1K.
	 */
6496
	sz = min(bufsize, 1024);
Pam Delaney's avatar
Pam Delaney committed
6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563

	/* --- loop ----
	 * On first pass, always issue a reserve.
	 * On additional loops, only if a reset has occurred.
	 * iocmd.flags indicates if echo or regular buffer
	 */
	for (patt = 0; patt < 4; patt++) {
		ddvprintk(("Pattern %d\n", patt));
		if ((iocmd.flags & MPT_ICFLAG_RESERVED) && (iocmd.flags & MPT_ICFLAG_DID_RESET)) {
			iocmd.cmd = CMD_TestUnitReady;
			iocmd.data_dma = -1;
			iocmd.data = NULL;
			iocmd.size = 0;
			if (mptscsih_do_cmd(hd, &iocmd) < 0)
				goto target_done;

			iocmd.cmd = CMD_Release6;
			iocmd.data_dma = -1;
			iocmd.data = NULL;
			iocmd.size = 0;
			if (mptscsih_do_cmd(hd, &iocmd) < 0)
				goto target_done;
			else if (hd->pLocal == NULL)
				goto target_done;
			else {
				rc = hd->pLocal->completion;
				ddvprintk(("Release rc %d\n", rc));
				if (rc == MPT_SCANDV_GOOD)
					iocmd.flags &= ~MPT_ICFLAG_RESERVED;
				else
					goto target_done;
			}
			iocmd.flags &= ~MPT_ICFLAG_RESERVED;
		}
		iocmd.flags &= ~MPT_ICFLAG_DID_RESET;

		repeat = 5;
		while (repeat && (!(iocmd.flags & MPT_ICFLAG_RESERVED))) {
			iocmd.cmd = CMD_Reserve6;
			iocmd.data_dma = -1;
			iocmd.data = NULL;
			iocmd.size = 0;
			if (mptscsih_do_cmd(hd, &iocmd) < 0)
				goto target_done;
			else if (hd->pLocal == NULL)
				goto target_done;
			else {
				rc = hd->pLocal->completion;
				if (rc == MPT_SCANDV_GOOD) {
					iocmd.flags |= MPT_ICFLAG_RESERVED;
				} else if (rc == MPT_SCANDV_SENSE) {
					/* Wait if coming ready
					 */
					u8 skey = hd->pLocal->sense[2] & 0x0F;
					u8 asc = hd->pLocal->sense[12];
					u8 ascq = hd->pLocal->sense[13];
					ddvprintk((MYIOC_s_INFO_FMT
						"DV: Reserve Failed: ", ioc->name));
					ddvprintk(("SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
							skey, asc, ascq));

					if ((skey == SK_NOT_READY) && (asc == 0x04)&&
									(ascq == 0x01)) {
						/* wait then repeat */
						mdelay (2000);
						notDone++;
					} else {
6564
						ddvprintk((MYIOC_s_INFO_FMT
Pam Delaney's avatar
Pam Delaney committed
6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627
							"DV: Reserved Failed.", ioc->name));
						goto target_done;
					}
				} else {
					ddvprintk((MYIOC_s_INFO_FMT "DV: Reserved Failed.",
							 ioc->name));
					goto target_done;
				}
			}
		}

		mptscsih_fillbuf(pbuf1, sz, patt, 1);
		iocmd.cmd = CMD_WriteBuffer;
		iocmd.data_dma = buf1_dma;
		iocmd.data = pbuf1;
		iocmd.size = sz;
		if (mptscsih_do_cmd(hd, &iocmd) < 0)
			goto target_done;
		else if (hd->pLocal == NULL)
			goto target_done;
		else {
			rc = hd->pLocal->completion;
			if (rc == MPT_SCANDV_GOOD)
				;		/* Issue read buffer */
			else if (rc == MPT_SCANDV_DID_RESET) {
				/* If using echo buffers, reset to data buffers.
				 * Else do Fallback and restart
				 * this test (re-issue reserve
				 * because of bus reset).
				 */
				if ((iocmd.flags & MPT_ICFLAG_ECHO) && (dataBufSize >= bufsize)) {
					iocmd.flags &= ~MPT_ICFLAG_ECHO;
				} else {
					dv.cmd = MPT_FALLBACK;
					mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);

					if (mpt_config(hd->ioc, &cfg) != 0)
						goto target_done;

					if ((!dv.now.width) && (!dv.now.offset))
						goto target_done;
				}

				iocmd.flags |= MPT_ICFLAG_DID_RESET;
				patt = -1;
				continue;
			} else if (rc == MPT_SCANDV_SENSE) {
				/* Restart data test if UA, else quit.
				 */
				u8 skey = hd->pLocal->sense[2] & 0x0F;
				ddvprintk((MYIOC_s_INFO_FMT
					"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n", ioc->name, skey,
					hd->pLocal->sense[12], hd->pLocal->sense[13]));
				if (skey == SK_UNIT_ATTENTION) {
					patt = -1;
					continue;
				} else if (skey == SK_ILLEGAL_REQUEST) {
					if (iocmd.flags & MPT_ICFLAG_ECHO) {
						if (dataBufSize >= bufsize) {
							iocmd.flags &= ~MPT_ICFLAG_ECHO;
							patt = -1;
							continue;
						}
6628
					}
Pam Delaney's avatar
Pam Delaney committed
6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739
					goto target_done;
				}
				else
					goto target_done;
			} else {
				/* fatal error */
				goto target_done;
			}
		}

		iocmd.cmd = CMD_ReadBuffer;
		iocmd.data_dma = buf2_dma;
		iocmd.data = pbuf2;
		iocmd.size = sz;
		if (mptscsih_do_cmd(hd, &iocmd) < 0)
			goto target_done;
		else if (hd->pLocal == NULL)
			goto target_done;
		else {
			rc = hd->pLocal->completion;
			if (rc == MPT_SCANDV_GOOD) {
				 /* If buffers compare,
				  * go to next pattern,
				  * else, do a fallback and restart
				  * data transfer test.
				  */
				if (memcmp (pbuf1, pbuf2, sz) == 0) {
					; /* goto next pattern */
				} else {
					/* Miscompare with Echo buffer, go to data buffer,
					 * if that buffer exists.
					 * Miscompare with Data buffer, check first 4 bytes,
					 * some devices return capacity. Exit in this case.
					 */
					if (iocmd.flags & MPT_ICFLAG_ECHO) {
						if (dataBufSize >= bufsize)
							iocmd.flags &= ~MPT_ICFLAG_ECHO;
						else
							goto target_done;
					} else {
						if (dataBufSize == (pbuf2[1]<<16 | pbuf2[2]<<8 | pbuf2[3])) {
							/* Argh. Device returning wrong data.
							 * Quit DV for this device.
							 */
							goto target_done;
						}

						/* Had an actual miscompare. Slow down.*/
						dv.cmd = MPT_FALLBACK;
						mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);

						if (mpt_config(hd->ioc, &cfg) != 0)
							goto target_done;

						if ((!dv.now.width) && (!dv.now.offset))
							goto target_done;
					}

					patt = -1;
					continue;
				}
			} else if (rc == MPT_SCANDV_DID_RESET) {
				/* Do Fallback and restart
				 * this test (re-issue reserve
				 * because of bus reset).
				 */
				dv.cmd = MPT_FALLBACK;
				mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);

				if (mpt_config(hd->ioc, &cfg) != 0)
					 goto target_done;

				if ((!dv.now.width) && (!dv.now.offset))
					goto target_done;

				iocmd.flags |= MPT_ICFLAG_DID_RESET;
				patt = -1;
				continue;
			} else if (rc == MPT_SCANDV_SENSE) {
				/* Restart data test if UA, else quit.
				 */
				u8 skey = hd->pLocal->sense[2] & 0x0F;
				ddvprintk((MYIOC_s_INFO_FMT
					"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n", ioc->name, skey,
					hd->pLocal->sense[12], hd->pLocal->sense[13]));
				if (skey == SK_UNIT_ATTENTION) {
					patt = -1;
					continue;
				}
				else
					goto target_done;
			} else {
				/* fatal error */
				goto target_done;
			}
		}

	} /* --- end of patt loop ---- */

target_done:
	if (iocmd.flags & MPT_ICFLAG_RESERVED) {
		iocmd.cmd = CMD_Release6;
		iocmd.data_dma = -1;
		iocmd.data = NULL;
		iocmd.size = 0;
		if (mptscsih_do_cmd(hd, &iocmd) < 0)
			printk(MYIOC_s_INFO_FMT "DV: Release failed. id %d",
					ioc->name, id);
		else if (hd->pLocal) {
			if (hd->pLocal->completion == MPT_SCANDV_GOOD)
				iocmd.flags &= ~MPT_ICFLAG_RESERVED;
Linus Torvalds's avatar
Linus Torvalds committed
6740
		} else {
6741
			printk(MYIOC_s_INFO_FMT "DV: Release failed. id %d",
Pam Delaney's avatar
Pam Delaney committed
6742 6743 6744 6745 6746 6747 6748
						ioc->name, id);
		}
	}


	/* Set if cfg1_dma_addr contents is valid
	 */
6749
	if ((cfg.hdr != NULL) && (retcode == 0)){
Pam Delaney's avatar
Pam Delaney committed
6750 6751 6752 6753 6754 6755 6756 6757
		/* If disk, not U320, disable QAS
		 */
		if ((inq0 == 0) && (dv.now.factor > MPT_ULTRA320))
			hd->ioc->spi_data.noQas = MPT_TARGET_NO_NEGO_QAS;

		dv.cmd = MPT_SAVE;
		mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);

6758 6759
		/* Double writes to SDP1 can cause problems,
		 * skip save of the final negotiated settings to
Pam Delaney's avatar
Pam Delaney committed
6760
		 * SCSI device page 1.
6761
		 *
Pam Delaney's avatar
Pam Delaney committed
6762 6763 6764 6765 6766
		cfg.hdr = &header1;
		cfg.physAddr = cfg1_dma_addr;
		cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
		cfg.dir = 1;
		mpt_config(hd->ioc, &cfg);
6767
		 */
Pam Delaney's avatar
Pam Delaney committed
6768 6769 6770 6771 6772 6773
	}

	/* If this is a RAID Passthrough, enable internal IOs
	 */
	if (iocmd.flags & MPT_ICFLAG_PHYS_DISK) {
		if (mptscsih_do_raid(hd, MPI_RAID_ACTION_ENABLE_PHYS_IO, &iocmd) < 0)
6774
			ddvprintk((MYIOC_s_ERR_FMT "RAID Enable FAILED!\n", ioc->name));
Pam Delaney's avatar
Pam Delaney committed
6775 6776 6777 6778 6779 6780 6781
	}

	/* Done with the DV scan of the current target
	 */
	if (pDvBuf)
		pci_free_consistent(ioc->pcidev, dv_alloc, pDvBuf, dvbuf_dma);

6782 6783
	ddvtprintk((MYIOC_s_INFO_FMT "DV Done.\n",
			ioc->name));
Pam Delaney's avatar
Pam Delaney committed
6784

6785
	return retcode;
Pam Delaney's avatar
Pam Delaney committed
6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*	mptscsih_dv_parms - perform a variety of operations on the
 *	parameters used for negotiation.
 *	@hd: Pointer to a SCSI host.
 *	@dv: Pointer to a structure that contains the maximum and current
 *		negotiated parameters.
 */
static void
mptscsih_dv_parms(MPT_SCSI_HOST *hd, DVPARAMETERS *dv,void *pPage)
{
6798 6799 6800
	VirtDevice		*pTarget;
	SCSIDevicePage0_t	*pPage0;
	SCSIDevicePage1_t	*pPage1;
Pam Delaney's avatar
Pam Delaney committed
6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850
	int			val = 0, data, configuration;
	u8			width = 0;
	u8			offset = 0;
	u8			factor = 0;
	u8			negoFlags = 0;
	u8			cmd = dv->cmd;
	u8			id = dv->id;

	switch (cmd) {
	case MPT_GET_NVRAM_VALS:
		ddvprintk((MYIOC_s_NOTE_FMT "Getting NVRAM: ",
							 hd->ioc->name));
		/* Get the NVRAM values and save in tmax
		 * If not an LVD bus, the adapter minSyncFactor has been
		 * already throttled back.
		 */
		if ((hd->Targets)&&((pTarget = hd->Targets[(int)id]) != NULL) && !pTarget->raidVolume) {
			width = pTarget->maxWidth;
			offset = pTarget->maxOffset;
			factor = pTarget->minSyncFactor;
			negoFlags = pTarget->negoFlags;
		} else {
			if (hd->ioc->spi_data.nvram && (hd->ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
				data = hd->ioc->spi_data.nvram[id];
				width = data & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
				if ((offset = hd->ioc->spi_data.maxSyncOffset) == 0)
					factor = MPT_ASYNC;
				else {
					factor = (data & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
					if ((factor == 0) || (factor == MPT_ASYNC)){
						factor = MPT_ASYNC;
						offset = 0;
					}
				}
			} else {
				width = MPT_NARROW;
				offset = 0;
				factor = MPT_ASYNC;
			}

			/* Set the negotiation flags */
			negoFlags = hd->ioc->spi_data.noQas;
			if (!width)
				negoFlags |= MPT_TARGET_NO_NEGO_WIDE;

			if (!offset)
				negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
		}

		/* limit by adapter capabilities */
6851 6852 6853
		width = min(width, hd->ioc->spi_data.maxBusWidth);
		offset = min(offset, hd->ioc->spi_data.maxSyncOffset);
		factor = max(factor, hd->ioc->spi_data.minSyncFactor);
Pam Delaney's avatar
Pam Delaney committed
6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911

		/* Check Consistency */
		if (offset && (factor < MPT_ULTRA2) && !width)
			factor = MPT_ULTRA2;

		dv->max.width = width;
		dv->max.offset = offset;
		dv->max.factor = factor;
		dv->max.flags = negoFlags;
		ddvprintk((" width %d, factor %x, offset %x flags %x\n",
				width, factor, offset, negoFlags));
		break;

	case MPT_UPDATE_MAX:
		ddvprintk((MYIOC_s_NOTE_FMT
			"Updating with SDP0 Data: ", hd->ioc->name));
		/* Update tmax values with those from Device Page 0.*/
		pPage0 = (SCSIDevicePage0_t *) pPage;
		if (pPage0) {
			val = cpu_to_le32(pPage0->NegotiatedParameters);
			dv->max.width = val & MPI_SCSIDEVPAGE0_NP_WIDE ? 1 : 0;
			dv->max.offset = (val&MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) >> 16;
			dv->max.factor = (val&MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> 8;
		}

		dv->now.width = dv->max.width;
		dv->now.offset = dv->max.offset;
		dv->now.factor = dv->max.factor;
		ddvprintk(("width %d, factor %x, offset %x, flags %x\n",
				dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags));
		break;

	case MPT_SET_MAX:
		ddvprintk((MYIOC_s_NOTE_FMT "Setting Max: ",
								hd->ioc->name));
		/* Set current to the max values. Update the config page.*/
		dv->now.width = dv->max.width;
		dv->now.offset = dv->max.offset;
		dv->now.factor = dv->max.factor;
		dv->now.flags = dv->max.flags;

		pPage1 = (SCSIDevicePage1_t *)pPage;
		if (pPage1) {
			mptscsih_setDevicePage1Flags (dv->now.width, dv->now.factor,
				dv->now.offset, &val, &configuration, dv->now.flags);
			pPage1->RequestedParameters = le32_to_cpu(val);
			pPage1->Reserved = 0;
			pPage1->Configuration = le32_to_cpu(configuration);

		}

		ddvprintk(("width %d, factor %x, offset %x request %x, config %x\n",
				dv->now.width, dv->now.factor, dv->now.offset, val, configuration));
		break;

	case MPT_SET_MIN:
		ddvprintk((MYIOC_s_NOTE_FMT "Setting Min: ",
								hd->ioc->name));
6912
		/* Set page to asynchronous and narrow
Pam Delaney's avatar
Pam Delaney committed
6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927
		 * Do not update now, breaks fallback routine. */
		width = MPT_NARROW;
		offset = 0;
		factor = MPT_ASYNC;
		negoFlags = dv->max.flags;

		pPage1 = (SCSIDevicePage1_t *)pPage;
		if (pPage1) {
			mptscsih_setDevicePage1Flags (width, factor,
				offset, &val, &configuration, negoFlags);
			pPage1->RequestedParameters = le32_to_cpu(val);
			pPage1->Reserved = 0;
			pPage1->Configuration = le32_to_cpu(configuration);
		}
		ddvprintk(("width %d, factor %x, offset %x request %x config %x\n",
6928
				width, factor, offset, val, configuration));
Pam Delaney's avatar
Pam Delaney committed
6929 6930 6931 6932 6933
		break;

	case MPT_FALLBACK:
		ddvprintk((MYIOC_s_NOTE_FMT
			"Fallback: Start: offset %d, factor %x, width %d \n",
6934
				hd->ioc->name, dv->now.offset,
Pam Delaney's avatar
Pam Delaney committed
6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985
				dv->now.factor, dv->now.width));
		width = dv->now.width;
		offset = dv->now.offset;
		factor = dv->now.factor;
		if ((offset) && (dv->max.width)) {
			if (factor < MPT_ULTRA160)
				factor = MPT_ULTRA160;
			else if (factor < MPT_ULTRA2) {
				factor = MPT_ULTRA2;
				width = MPT_WIDE;
			} else if ((factor == MPT_ULTRA2) && width) {
				factor = MPT_ULTRA2;
				width = MPT_NARROW;
			} else if (factor < MPT_ULTRA) {
				factor = MPT_ULTRA;
				width = MPT_WIDE;
			} else if ((factor == MPT_ULTRA) && width) {
				width = MPT_NARROW;
			} else if (factor < MPT_FAST) {
				factor = MPT_FAST;
				width = MPT_WIDE;
			} else if ((factor == MPT_FAST) && width) {
				factor = MPT_FAST;
				width = MPT_NARROW;
			} else if (factor < MPT_SCSI) {
				factor = MPT_SCSI;
				width = MPT_WIDE;
			} else if ((factor == MPT_SCSI) && width) {
				factor = MPT_SCSI;
				width = MPT_NARROW;
			} else {
				factor = MPT_ASYNC;
				offset = 0;
			}

		} else if (offset) {
			width = MPT_NARROW;
			if (factor < MPT_ULTRA)
				factor = MPT_ULTRA;
			else if (factor < MPT_FAST)
				factor = MPT_FAST;
			else if (factor < MPT_SCSI)
				factor = MPT_SCSI;
			else {
				factor = MPT_ASYNC;
				offset = 0;
			}

		} else {
			width = MPT_NARROW;
			factor = MPT_ASYNC;
Linus Torvalds's avatar
Linus Torvalds committed
6986
		}
Pam Delaney's avatar
Pam Delaney committed
6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001
		dv->max.flags |= MPT_TARGET_NO_NEGO_QAS;

		dv->now.width = width;
		dv->now.offset = offset;
		dv->now.factor = factor;
		dv->now.flags = dv->max.flags;

		pPage1 = (SCSIDevicePage1_t *)pPage;
		if (pPage1) {
			mptscsih_setDevicePage1Flags (width, factor, offset, &val,
						&configuration, dv->now.flags);

			pPage1->RequestedParameters = le32_to_cpu(val);
			pPage1->Reserved = 0;
			pPage1->Configuration = le32_to_cpu(configuration);
Linus Torvalds's avatar
Linus Torvalds committed
7002 7003
		}

Pam Delaney's avatar
Pam Delaney committed
7004 7005 7006
		ddvprintk(("Finish: offset %d, factor %x, width %d, request %x config %x\n",
			     dv->now.offset, dv->now.factor, dv->now.width, val, configuration));
		break;
Linus Torvalds's avatar
Linus Torvalds committed
7007

Pam Delaney's avatar
Pam Delaney committed
7008 7009 7010 7011 7012
	case MPT_SAVE:
		ddvprintk((MYIOC_s_NOTE_FMT
			"Saving to Target structure: ", hd->ioc->name));
		ddvprintk(("offset %d, factor %x, width %d \n",
			     dv->now.offset, dv->now.factor, dv->now.width));
Linus Torvalds's avatar
Linus Torvalds committed
7013

Pam Delaney's avatar
Pam Delaney committed
7014 7015 7016
		/* Save these values to target structures
		 * or overwrite nvram (phys disks only).
		 */
Linus Torvalds's avatar
Linus Torvalds committed
7017

Pam Delaney's avatar
Pam Delaney committed
7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028
		if ((hd->Targets)&&((pTarget = hd->Targets[(int)id]) != NULL) && !pTarget->raidVolume ) {
			pTarget->maxWidth = dv->now.width;
			pTarget->maxOffset = dv->now.offset;
			pTarget->minSyncFactor = dv->now.factor;
			pTarget->negoFlags = dv->now.flags;
		} else {
			/* Preserv all flags, use
			 * read-modify-write algorithm
			 */
			if (hd->ioc->spi_data.nvram) {
				data = hd->ioc->spi_data.nvram[id];
Linus Torvalds's avatar
Linus Torvalds committed
7029

Pam Delaney's avatar
Pam Delaney committed
7030 7031 7032 7033
				if (dv->now.width)
					data &= ~MPT_NVRAM_WIDE_DISABLE;
				else
					data |= MPT_NVRAM_WIDE_DISABLE;
Linus Torvalds's avatar
Linus Torvalds committed
7034

Pam Delaney's avatar
Pam Delaney committed
7035 7036
				if (!dv->now.offset)
					factor = MPT_ASYNC;
Linus Torvalds's avatar
Linus Torvalds committed
7037

Pam Delaney's avatar
Pam Delaney committed
7038 7039 7040 7041 7042 7043 7044 7045
				data &= ~MPT_NVRAM_SYNC_MASK;
				data |= (dv->now.factor << MPT_NVRAM_SYNC_SHIFT) & MPT_NVRAM_SYNC_MASK;

				hd->ioc->spi_data.nvram[id] = data;
			}
		}
		break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
7046 7047 7048
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
7049 7050 7051 7052 7053 7054 7055 7056 7057 7058
/*	mptscsih_fillbuf - fill a buffer with a special data pattern
 *		cleanup. For bus scan only.
 *
 *	@buffer: Pointer to data buffer to be filled.
 *	@size: Number of bytes to fill
 *	@index: Pattern index
 *	@width: bus width, 0 (8 bits) or 1 (16 bits)
 */
static void
mptscsih_fillbuf(char *buffer, int size, int index, int width)
Linus Torvalds's avatar
Linus Torvalds committed
7059
{
Pam Delaney's avatar
Pam Delaney committed
7060 7061 7062 7063
	char *ptr = buffer;
	int ii;
	char byte;
	short val;
Linus Torvalds's avatar
Linus Torvalds committed
7064

Pam Delaney's avatar
Pam Delaney committed
7065 7066
	switch (index) {
	case 0:
Linus Torvalds's avatar
Linus Torvalds committed
7067

Pam Delaney's avatar
Pam Delaney committed
7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087
		if (width) {
			/* Pattern:  0000 FFFF 0000 FFFF
			 */
			for (ii=0; ii < size; ii++, ptr++) {
				if (ii & 0x02)
					*ptr = 0xFF;
				else
					*ptr = 0x00;
			}
		} else {
			/* Pattern:  00 FF 00 FF
			 */
			for (ii=0; ii < size; ii++, ptr++) {
				if (ii & 0x01)
					*ptr = 0xFF;
				else
					*ptr = 0x00;
			}
		}
		break;
Linus Torvalds's avatar
Linus Torvalds committed
7088

Pam Delaney's avatar
Pam Delaney committed
7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109
	case 1:
		if (width) {
			/* Pattern:  5555 AAAA 5555 AAAA 5555
			 */
			for (ii=0; ii < size; ii++, ptr++) {
				if (ii & 0x02)
					*ptr = 0xAA;
				else
					*ptr = 0x55;
			}
		} else {
			/* Pattern:  55 AA 55 AA 55
			 */
			for (ii=0; ii < size; ii++, ptr++) {
				if (ii & 0x01)
					*ptr = 0xAA;
				else
					*ptr = 0x55;
			}
		}
		break;
Linus Torvalds's avatar
Linus Torvalds committed
7110

Pam Delaney's avatar
Pam Delaney committed
7111 7112 7113
	case 2:
		/* Pattern:  00 01 02 03 04 05
		 * ... FE FF 00 01..
Linus Torvalds's avatar
Linus Torvalds committed
7114
		 */
Pam Delaney's avatar
Pam Delaney committed
7115 7116 7117
		for (ii=0; ii < size; ii++, ptr++)
			*ptr = (char) ii;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
7118

Pam Delaney's avatar
Pam Delaney committed
7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147
	case 3:
		if (width) {
			/* Wide Pattern:  FFFE 0001 FFFD 0002
			 * ...  4000 DFFF 8000 EFFF
			 */
			byte = 0;
			for (ii=0; ii < size/2; ii++) {
				/* Create the base pattern
				 */
				val = (1 << byte);
				/* every 64 (0x40) bytes flip the pattern
				 * since we fill 2 bytes / iteration,
				 * test for ii = 0x20
				 */
				if (ii & 0x20)
					val = ~(val);

				if (ii & 0x01) {
					*ptr = (char)( (val & 0xFF00) >> 8);
					ptr++;
					*ptr = (char)(val & 0xFF);
					byte++;
					byte &= 0x0F;
				} else {
					val = ~val;
					*ptr = (char)( (val & 0xFF00) >> 8);
					ptr++;
					*ptr = (char)(val & 0xFF);
				}
Linus Torvalds's avatar
Linus Torvalds committed
7148

Pam Delaney's avatar
Pam Delaney committed
7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165
				ptr++;
			}
		} else {
			/* Narrow Pattern:  FE 01 FD 02 FB 04
			 * .. 7F 80 01 FE 02 FD ...  80 7F
			 */
			byte = 0;
			for (ii=0; ii < size; ii++, ptr++) {
				/* Base pattern - first 32 bytes
				 */
				if (ii & 0x01) {
					*ptr = (1 << byte);
					byte++;
					byte &= 0x07;
				} else {
					*ptr = (char) (~(1 << byte));
				}
Linus Torvalds's avatar
Linus Torvalds committed
7166

Pam Delaney's avatar
Pam Delaney committed
7167 7168 7169 7170 7171 7172 7173 7174
				/* Flip the pattern every 32 bytes
				 */
				if (ii & 0x20)
					*ptr = ~(*ptr);
			}
		}
		break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
7175
}
James Bottomley's avatar
James Bottomley committed
7176
#endif /* ~MPTSCSIH_ENABLE_DOMAIN_VALIDATION */
Linus Torvalds's avatar
Linus Torvalds committed
7177 7178

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Pam Delaney's avatar
Pam Delaney committed
7179 7180 7181
/* Commandline Parsing routines and defines.
 *
 * insmod format:
7182
 *	insmod mptscsih mptscsih="width:1 dv:n factor:0x09 saf-te:1"
Pam Delaney's avatar
Pam Delaney committed
7183
 *  boot format:
7184
 *	mptscsih=width:1,dv:n,factor:0x8,saf-te:1
Pam Delaney's avatar
Pam Delaney committed
7185
 *
Linus Torvalds's avatar
Linus Torvalds committed
7186
 */
Pam Delaney's avatar
Pam Delaney committed
7187 7188 7189 7190 7191
#ifdef MODULE
#define	ARG_SEP	' '
#else
#define	ARG_SEP	','
#endif
Linus Torvalds's avatar
Linus Torvalds committed
7192

7193
#ifdef MODULE
Pam Delaney's avatar
Pam Delaney committed
7194 7195 7196 7197
static char setup_token[] __initdata =
	"dv:"
	"width:"
	"factor:"
7198 7199
	"saf-te:"
       ;	/* DO NOT REMOVE THIS ';' */
7200 7201
#endif
       
Pam Delaney's avatar
Pam Delaney committed
7202 7203 7204
#define OPT_DV			1
#define OPT_MAX_WIDTH		2
#define OPT_MIN_SYNC_FACTOR	3
7205
#define OPT_SAF_TE		4
Linus Torvalds's avatar
Linus Torvalds committed
7206

Pam Delaney's avatar
Pam Delaney committed
7207
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
7208
#ifdef MODULE
Pam Delaney's avatar
Pam Delaney committed
7209
static int
7210
get_setup_token(char *p)
Pam Delaney's avatar
Pam Delaney committed
7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221
{
	char *cur = setup_token;
	char *pc;
	int i = 0;

	while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
		++pc;
		++i;
		if (!strncmp(p, cur, pc - cur))
			return i;
		cur = pc;
Linus Torvalds's avatar
Linus Torvalds committed
7222
	}
Pam Delaney's avatar
Pam Delaney committed
7223 7224
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
7225

Pam Delaney's avatar
Pam Delaney committed
7226 7227
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
7228
mptscsih_setup(char *str)
Pam Delaney's avatar
Pam Delaney committed
7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247
{
	char *cur = str;
	char *pc, *pv;
	unsigned long val;
	int  c;

	while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
		char *pe;

		val = 0;
		pv = pc;
		c = *++pv;

		if	(c == 'n')
			val = 0;
		else if	(c == 'y')
			val = 1;
		else
			val = (int) simple_strtoul(pv, &pe, 0);
Linus Torvalds's avatar
Linus Torvalds committed
7248

Pam Delaney's avatar
Pam Delaney committed
7249 7250 7251 7252 7253
		printk("Found Token: %s, value %x\n", cur, (int)val);
		switch (get_setup_token(cur)) {
		case OPT_DV:
			driver_setup.dv = val;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
7254

Pam Delaney's avatar
Pam Delaney committed
7255 7256 7257
		case OPT_MAX_WIDTH:
			driver_setup.max_width = val;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
7258

Pam Delaney's avatar
Pam Delaney committed
7259 7260 7261
		case OPT_MIN_SYNC_FACTOR:
			driver_setup.min_sync_fac = val;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
7262

7263 7264 7265 7266
		case OPT_SAF_TE:
			driver_setup.saf_te = val;
			break;

Pam Delaney's avatar
Pam Delaney committed
7267 7268 7269 7270
		default:
			printk("mptscsih_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
7271

Pam Delaney's avatar
Pam Delaney committed
7272 7273 7274 7275
		if ((cur = strchr(cur, ARG_SEP)) != NULL)
			++cur;
	}
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
7276
}
7277
#endif
Linus Torvalds's avatar
Linus Torvalds committed
7278 7279
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

James Bottomley's avatar
James Bottomley committed
7280 7281 7282

module_init(mptscsih_init);
module_exit(mptscsih_exit);