mptscsih.c 89 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2
/*
 *  linux/drivers/message/fusion/mptscsih.c
3 4
 *      For use with LSI PCI chip/adapter(s)
 *      running LSI Fusion MPT (Message Passing Technology) firmware.
Linus Torvalds's avatar
Linus Torvalds committed
5
 *
6
 *  Copyright (c) 1999-2008 LSI Corporation
7
 *  (mailto:DL-MPTFusionLinux@lsi.com)
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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
 *
 */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
    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>
#include <linux/delay.h>	/* for mdelay */
#include <linux/interrupt.h>	/* needed for in_interrupt() proto */
#include <linux/reboot.h>	/* notifier code */
#include <linux/workqueue.h>

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_tcq.h>
63
#include <scsi/scsi_dbg.h>
Linus Torvalds's avatar
Linus Torvalds committed
64 65 66

#include "mptbase.h"
#include "mptscsih.h"
67
#include "lsi/mpi_log_sas.h"
Linus Torvalds's avatar
Linus Torvalds committed
68 69 70 71 72 73 74 75 76

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
#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);
MODULE_LICENSE("GPL");
77
MODULE_VERSION(my_VERSION);
Linus Torvalds's avatar
Linus Torvalds committed
78 79 80 81 82

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  Other private/forward protos...
 */
83
struct scsi_cmnd	*mptscsih_get_scsi_lookup(MPT_ADAPTER *ioc, int i);
84 85 86
static struct scsi_cmnd * mptscsih_getclear_scsi_lookup(MPT_ADAPTER *ioc, int i);
static void	mptscsih_set_scsi_lookup(MPT_ADAPTER *ioc, int i, struct scsi_cmnd *scmd);
static int	SCPNT_TO_LOOKUP_IDX(MPT_ADAPTER *ioc, struct scsi_cmnd *scmd);
87
int		mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
Linus Torvalds's avatar
Linus Torvalds committed
88
static void	mptscsih_report_queue_full(struct scsi_cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq);
89
int		mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
Linus Torvalds's avatar
Linus Torvalds committed
90 91 92 93

static int	mptscsih_AddSGE(MPT_ADAPTER *ioc, struct scsi_cmnd *SCpnt,
				 SCSIIORequest_t *pReq, int req_idx);
static void	mptscsih_freeChainBuffers(MPT_ADAPTER *ioc, int req_idx);
94
static void	mptscsih_copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply);
Linus Torvalds's avatar
Linus Torvalds committed
95

96 97
int	mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 id,
		int lun, int ctx2abort, ulong timeout);
Linus Torvalds's avatar
Linus Torvalds committed
98

99 100
int		mptscsih_ioc_reset(MPT_ADAPTER *ioc, int post_reset);
int		mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply);
Linus Torvalds's avatar
Linus Torvalds committed
101

102
void
103
mptscsih_taskmgmt_response_code(MPT_ADAPTER *ioc, u8 response_code);
104 105
static int	mptscsih_get_completion_code(MPT_ADAPTER *ioc,
		MPT_FRAME_HDR *req, MPT_FRAME_HDR *reply);
106
int		mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
Linus Torvalds's avatar
Linus Torvalds committed
107
static int	mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *iocmd);
108
static void	mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, VirtDevice *vdevice);
Linus Torvalds's avatar
Linus Torvalds committed
109

110 111 112
static int
mptscsih_taskmgmt_reply(MPT_ADAPTER *ioc, u8 type,
				SCSITaskMgmtReply_t *pScsiTmReply);
113
void 		mptscsih_remove(struct pci_dev *);
114
void 		mptscsih_shutdown(struct pci_dev *);
Linus Torvalds's avatar
Linus Torvalds committed
115
#ifdef CONFIG_PM
116 117
int 		mptscsih_suspend(struct pci_dev *pdev, pm_message_t state);
int 		mptscsih_resume(struct pci_dev *pdev);
Linus Torvalds's avatar
Linus Torvalds committed
118 119
#endif

120
#define SNS_LEN(scp)	SCSI_SENSE_BUFFERSIZE
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139


/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_getFreeChainBuffer - Function to get a free chain
 *	from the MPT_SCSI_HOST FreeChainQ.
 *	@ioc: Pointer to MPT_ADAPTER structure
 *	@req_idx: Index of the SCSI IO request frame. (output)
 *
 *	return SUCCESS or FAILED
 */
static inline int
mptscsih_getFreeChainBuffer(MPT_ADAPTER *ioc, int *retIndex)
{
	MPT_FRAME_HDR *chainBuf;
	unsigned long flags;
	int rc;
	int chain_idx;

140
	dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "getFreeChainBuffer called\n",
141
	    ioc->name));
Linus Torvalds's avatar
Linus Torvalds committed
142 143 144 145 146 147 148 149 150 151
	spin_lock_irqsave(&ioc->FreeQlock, flags);
	if (!list_empty(&ioc->FreeChainQ)) {
		int offset;

		chainBuf = list_entry(ioc->FreeChainQ.next, MPT_FRAME_HDR,
				u.frame.linkage.list);
		list_del(&chainBuf->u.frame.linkage.list);
		offset = (u8 *)chainBuf - (u8 *)ioc->ChainBuffer;
		chain_idx = offset / ioc->req_sz;
		rc = SUCCESS;
152 153 154
		dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "getFreeChainBuffer chainBuf=%p ChainBuffer=%p offset=%d chain_idx=%d\n",
		    ioc->name, chainBuf, ioc->ChainBuffer, offset, chain_idx));
Linus Torvalds's avatar
Linus Torvalds committed
155 156 157
	} else {
		rc = FAILED;
		chain_idx = MPT_HOST_NO_CHAIN;
158 159
		dfailprintk(ioc, printk(MYIOC_s_ERR_FMT "getFreeChainBuffer failed\n",
		    ioc->name));
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	}
	spin_unlock_irqrestore(&ioc->FreeQlock, flags);

	*retIndex = chain_idx;
	return rc;
} /* mptscsih_getFreeChainBuffer() */

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_AddSGE - Add a SGE (plus chain buffers) to the
 *	SCSIIORequest_t Message Frame.
 *	@ioc: Pointer to MPT_ADAPTER structure
 *	@SCpnt: Pointer to scsi_cmnd structure
 *	@pReq: Pointer to SCSIIORequest_t structure
 *
 *	Returns ...
 */
static int
mptscsih_AddSGE(MPT_ADAPTER *ioc, struct 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;
	u32	RequestNB;

	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 = ioc->req_sz;

	/* Map the data portion, if any.
	 * sges_left  = 0 if no data transfer.
	 */
209 210 211
	sges_left = scsi_dma_map(SCpnt);
	if (sges_left < 0)
		return FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
212 213 214

	/* Handle the SG case.
	 */
215
	sg = scsi_sglist(SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
216 217 218 219 220 221 222 223 224 225 226
	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:
227
	numSgeSlots = ((frm_sz - sgeOffset) / ioc->SGE_size);
Linus Torvalds's avatar
Linus Torvalds committed
228 229
	numSgeThisFrame = (sges_left < numSgeSlots) ? sges_left : numSgeSlots;

230
	sgflags = MPT_SGE_FLAGS_SIMPLE_ELEMENT | sgdir;
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233 234 235 236 237 238

	/* 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) {
239 240
			/* Get next SG element from the OS */
			sg = sg_next(sg);
Linus Torvalds's avatar
Linus Torvalds committed
241 242 243 244 245
			sg_done++;
			continue;
		}

		v2 = sg_dma_address(sg);
246
		ioc->add_sge(psge, sgflags | thisxfer, v2);
Linus Torvalds's avatar
Linus Torvalds committed
247

248 249
		/* Get next SG element from the OS */
		sg = sg_next(sg);
250 251
		psge += ioc->SGE_size;
		sgeOffset += ioc->SGE_size;
Linus Torvalds's avatar
Linus Torvalds committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
		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);
268 269
		ioc->add_sge(psge, sgflags | thisxfer, v2);
		sgeOffset += ioc->SGE_size;
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272 273 274 275 276 277
		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.
			 */
278 279
			ioc->add_chain((char *)chainSge, 0, sgeOffset,
				ioc->ChainBufferDMA + chain_dma_off);
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282 283 284 285
		} else {
			/* The current buffer is the original MF
			 * and there is no Chain buffer.
			 */
			pReq->ChainOffset = 0;
			RequestNB = (((sgeOffset - 1) >> ioc->NBShiftFactor)  + 1) & 0x03;
286
			dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT
Linus Torvalds's avatar
Linus Torvalds committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
			    "Single Buffer RequestNB=%x, sgeOffset=%d\n", ioc->name, RequestNB, sgeOffset));
			ioc->RequestNB[req_idx] = RequestNB;
		}
	} 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.
		 */

302
		dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SG: Chain Required! sg done %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
303 304 305 306 307 308 309 310 311
				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) {
312
			u32 *ptmp = (u32 *) (psge - ioc->SGE_size);
Linus Torvalds's avatar
Linus Torvalds committed
313 314 315 316 317 318 319 320 321 322 323 324 325
			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);
326 327 328
			sgeOffset += ioc->SGE_size;
			ioc->add_chain((char *)chainSge, nextChain, sgeOffset,
					 ioc->ChainBufferDMA + chain_dma_off);
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332 333 334 335
		} 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);
			RequestNB = (((sgeOffset - 1) >> ioc->NBShiftFactor)  + 1) & 0x03;
336
			dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Chain Buffer Needed, RequestNB=%x sgeOffset=%d\n", ioc->name, RequestNB, sgeOffset));
Linus Torvalds's avatar
Linus Torvalds committed
337 338 339 340 341 342 343 344 345
			ioc->RequestNB[req_idx] = RequestNB;
		}

		sges_left -= sg_done;


		/* NOTE: psge points to the beginning of the chain element
		 * in current buffer. Get a chain buffer.
		 */
346
		if ((mptscsih_getFreeChainBuffer(ioc, &newIndex)) == FAILED) {
347
			dfailprintk(ioc, printk(MYIOC_s_DEBUG_FMT
348 349
			    "getFreeChainBuffer FAILED SCSI cmd=%02x (%p)\n",
 			    ioc->name, pReq->CDB[0], SCpnt));
Linus Torvalds's avatar
Linus Torvalds committed
350
			return FAILED;
351
		}
Linus Torvalds's avatar
Linus Torvalds committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

		/* Update the tracking arrays.
		 * If chainSge == NULL, update ReqToChain, else ChainToChain
		 */
		if (chainSge) {
			ioc->ChainToChain[chain_idx] = newIndex;
		} else {
			ioc->ReqToChain[req_idx] = newIndex;
		}
		chain_idx = newIndex;
		chain_dma_off = 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;
369 370
		dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "  Current buff @ %p (index 0x%x)",
		    ioc->name, psge, req_idx));
Linus Torvalds's avatar
Linus Torvalds committed
371 372 373 374 375 376 377

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

378 379
		dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "  Chain buff @ %p (index 0x%x)\n",
		    ioc->name, psge, chain_idx));
Linus Torvalds's avatar
Linus Torvalds committed
380 381 382 383 384 385 386 387 388 389

		/* Start the SGE for the next buffer
		 */

		goto nextSGEset;
	}

	return SUCCESS;
} /* mptscsih_AddSGE() */

390 391 392 393 394 395 396
static void
mptscsih_issue_sep_command(MPT_ADAPTER *ioc, VirtTarget *vtarget,
    U32 SlotStatus)
{
	MPT_FRAME_HDR *mf;
	SEPRequest_t 	 *SEPMsg;

397 398 399 400 401 402
	if (ioc->bus_type != SAS)
		return;

	/* Not supported for hidden raid components
	 */
	if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
403 404 405
		return;

	if ((mf = mpt_get_msg_frame(ioc->InternalCtx, ioc)) == NULL) {
406
		dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s: no msg frames!!\n",
407
		    ioc->name,__func__));
408 409 410 411 412
		return;
	}

	SEPMsg = (SEPRequest_t *)mf;
	SEPMsg->Function = MPI_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
413 414
	SEPMsg->Bus = vtarget->channel;
	SEPMsg->TargetID = vtarget->id;
415 416
	SEPMsg->Action = MPI_SEP_REQ_ACTION_WRITE_STATUS;
	SEPMsg->SlotStatus = SlotStatus;
417
	devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT
418 419
	    "Sending SEP cmd=%x channel=%d id=%d\n",
	    ioc->name, SlotStatus, SEPMsg->Bus, SEPMsg->TargetID));
420 421 422
	mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
}

423
#ifdef CONFIG_FUSION_LOGGING
424
/**
425
 *	mptscsih_info_scsiio - debug print info on reply frame
426 427
 *	@ioc: Pointer to MPT_ADAPTER structure
 *	@sc: original scsi cmnd pointer
428 429 430
 *	@pScsiReply: Pointer to MPT reply frame
 *
 *	MPT_DEBUG_REPLY needs to be enabled to obtain this info
431 432 433 434
 *
 *	Refer to lsi/mpi.h.
 **/
static void
435
mptscsih_info_scsiio(MPT_ADAPTER *ioc, struct scsi_cmnd *sc, SCSIIOReply_t * pScsiReply)
436
{
437 438 439 440 441 442
	char	*desc = NULL;
	char	*desc1 = NULL;
	u16	ioc_status;
	u8	skey, asc, ascq;

	ioc_status = le16_to_cpu(pScsiReply->IOCStatus) & MPI_IOCSTATUS_MASK;
443 444 445

	switch (ioc_status) {

446 447
	case MPI_IOCSTATUS_SUCCESS:
		desc = "success";
448
		break;
449 450
	case MPI_IOCSTATUS_SCSI_INVALID_BUS:
		desc = "invalid bus";
451
		break;
452 453
	case MPI_IOCSTATUS_SCSI_INVALID_TARGETID:
		desc = "invalid target_id";
454
		break;
455 456
	case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
		desc = "device not there";
457
		break;
458 459
	case MPI_IOCSTATUS_SCSI_DATA_OVERRUN:
		desc = "data overrun";
460
		break;
461 462
	case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:
		desc = "data underrun";
463
		break;
464 465
	case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:
		desc = "I/O data error";
466
		break;
467 468
	case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:
		desc = "protocol error";
469
		break;
470 471
	case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:
		desc = "task terminated";
472
		break;
473 474 475 476 477 478 479 480 481 482 483
	case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
		desc = "residual mismatch";
		break;
	case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
		desc = "task management failed";
		break;
	case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:
		desc = "IOC terminated";
		break;
	case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:
		desc = "ext terminated";
484
		break;
485 486 487 488 489 490 491
	default:
		desc = "";
		break;
	}

	switch (pScsiReply->SCSIStatus)
	{
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 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	case MPI_SCSI_STATUS_SUCCESS:
		desc1 = "success";
		break;
	case MPI_SCSI_STATUS_CHECK_CONDITION:
		desc1 = "check condition";
		break;
	case MPI_SCSI_STATUS_CONDITION_MET:
		desc1 = "condition met";
		break;
	case MPI_SCSI_STATUS_BUSY:
		desc1 = "busy";
		break;
	case MPI_SCSI_STATUS_INTERMEDIATE:
		desc1 = "intermediate";
		break;
	case MPI_SCSI_STATUS_INTERMEDIATE_CONDMET:
		desc1 = "intermediate condmet";
		break;
	case MPI_SCSI_STATUS_RESERVATION_CONFLICT:
		desc1 = "reservation conflict";
		break;
	case MPI_SCSI_STATUS_COMMAND_TERMINATED:
		desc1 = "command terminated";
		break;
	case MPI_SCSI_STATUS_TASK_SET_FULL:
		desc1 = "task set full";
		break;
	case MPI_SCSI_STATUS_ACA_ACTIVE:
		desc1 = "aca active";
		break;
	case MPI_SCSI_STATUS_FCPEXT_DEVICE_LOGGED_OUT:
		desc1 = "fcpext device logged out";
		break;
	case MPI_SCSI_STATUS_FCPEXT_NO_LINK:
		desc1 = "fcpext no link";
		break;
	case MPI_SCSI_STATUS_FCPEXT_UNASSIGNED:
		desc1 = "fcpext unassigned";
		break;
	default:
		desc1 = "";
534 535 536
		break;
	}

537
	scsi_print_command(sc);
538 539
	printk(MYIOC_s_DEBUG_FMT "\tfw_channel = %d, fw_id = %d, lun = %d\n",
	    ioc->name, pScsiReply->Bus, pScsiReply->TargetID, sc->device->lun);
540 541 542 543 544
	printk(MYIOC_s_DEBUG_FMT "\trequest_len = %d, underflow = %d, "
	    "resid = %d\n", ioc->name, scsi_bufflen(sc), sc->underflow,
	    scsi_get_resid(sc));
	printk(MYIOC_s_DEBUG_FMT "\ttag = %d, transfer_count = %d, "
	    "sc->result = %08X\n", ioc->name, le16_to_cpu(pScsiReply->TaskTag),
545
	    le32_to_cpu(pScsiReply->TransferCount), sc->result);
546

547
	printk(MYIOC_s_DEBUG_FMT "\tiocstatus = %s (0x%04x), "
548
	    "scsi_status = %s (0x%02x), scsi_state = (0x%02x)\n",
549
	    ioc->name, desc, ioc_status, desc1, pScsiReply->SCSIStatus,
550
	    pScsiReply->SCSIState);
551

552 553 554 555 556
	if (pScsiReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
		skey = sc->sense_buffer[2] & 0x0F;
		asc = sc->sense_buffer[12];
		ascq = sc->sense_buffer[13];

557 558
		printk(MYIOC_s_DEBUG_FMT "\t[sense_key,asc,ascq]: "
		    "[0x%02x,0x%02x,0x%02x]\n", ioc->name, skey, asc, ascq);
559 560 561 562 563 564 565
	}

	/*
	 *  Look for + dump FCP ResponseInfo[]!
	 */
	if (pScsiReply->SCSIState & MPI_SCSI_STATE_RESPONSE_INFO_VALID &&
	    pScsiReply->ResponseInfo)
566 567
		printk(MYIOC_s_DEBUG_FMT "response_info = %08xh\n",
		    ioc->name, le32_to_cpu(pScsiReply->ResponseInfo));
568 569 570
}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	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.
 */
586
int
Linus Torvalds's avatar
Linus Torvalds committed
587 588 589 590 591 592
mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
{
	struct scsi_cmnd	*sc;
	MPT_SCSI_HOST	*hd;
	SCSIIORequest_t	*pScsiReq;
	SCSIIOReply_t	*pScsiReply;
593
	u16		 req_idx, req_idx_MR;
594
	VirtDevice	 *vdevice;
595
	VirtTarget	 *vtarget;
Linus Torvalds's avatar
Linus Torvalds committed
596

597
	hd = shost_priv(ioc->sh);
Linus Torvalds's avatar
Linus Torvalds committed
598
	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
599 600
	req_idx_MR = (mr != NULL) ?
	    le16_to_cpu(mr->u.frame.hwhdr.msgctxu.fld.req_idx) : req_idx;
601 602 603 604 605

	/* Special case, where already freed message frame is received from
	 * Firmware. It happens with Resetting IOC.
	 * Return immediately. Do not care
	 */
606
	if ((req_idx != req_idx_MR) ||
607
	    (le32_to_cpu(mf->u.frame.linkage.arg1) == 0xdeadbeaf))
608 609
		return 0;

610
	sc = mptscsih_getclear_scsi_lookup(ioc, req_idx);
Linus Torvalds's avatar
Linus Torvalds committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
	if (sc == NULL) {
		MPIHeader_t *hdr = (MPIHeader_t *)mf;

		/* 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.
		 */
		if (hdr->Function == MPI_FUNCTION_SCSI_IO_REQUEST)
			printk(MYIOC_s_ERR_FMT "NULL ScsiCmd ptr!\n",
			ioc->name);

		mptscsih_freeChainBuffers(ioc, req_idx);
		return 1;
	}

626 627 628 629 630 631
	if ((unsigned char *)mf != sc->host_scribble) {
		mptscsih_freeChainBuffers(ioc, req_idx);
		return 1;
	}

	sc->host_scribble = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
632 633 634 635
	sc->result = DID_OK << 16;		/* Set default reply as OK */
	pScsiReq = (SCSIIORequest_t *) mf;
	pScsiReply = (SCSIIOReply_t *) mr;

636
	if((ioc->facts.MsgVersion >= MPI_VERSION_01_05) && pScsiReply){
637
		dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT
638 639 640
			"ScsiDone (mf=%p,mr=%p,sc=%p,idx=%d,task-tag=%d)\n",
			ioc->name, mf, mr, sc, req_idx, pScsiReply->TaskTag));
	}else{
641
		dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT
642 643 644 645
			"ScsiDone (mf=%p,mr=%p,sc=%p,idx=%d)\n",
			ioc->name, mf, mr, sc, req_idx));
	}

Linus Torvalds's avatar
Linus Torvalds committed
646 647 648 649 650 651 652
	if (pScsiReply == NULL) {
		/* special context reply handling */
		;
	} else {
		u32	 xfer_cnt;
		u16	 status;
		u8	 scsi_state, scsi_status;
653
		u32	 log_info;
Linus Torvalds's avatar
Linus Torvalds committed
654 655 656 657 658

		status = le16_to_cpu(pScsiReply->IOCStatus) & MPI_IOCSTATUS_MASK;
		scsi_state = pScsiReply->SCSIState;
		scsi_status = pScsiReply->SCSIStatus;
		xfer_cnt = le32_to_cpu(pScsiReply->TransferCount);
659
		scsi_set_resid(sc, scsi_bufflen(sc) - xfer_cnt);
660
		log_info = le32_to_cpu(pScsiReply->IOCLogInfo);
Linus Torvalds's avatar
Linus Torvalds committed
661

662 663 664 665 666 667 668 669 670 671 672 673 674
		/*
		 *  if we get a data underrun indication, yet no data was
		 *  transferred and the SCSI status indicates that the
		 *  command was never started, change the data underrun
		 *  to success
		 */
		if (status == MPI_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
		    (scsi_status == MPI_SCSI_STATUS_BUSY ||
		     scsi_status == MPI_SCSI_STATUS_RESERVATION_CONFLICT ||
		     scsi_status == MPI_SCSI_STATUS_TASK_SET_FULL)) {
			status = MPI_IOCSTATUS_SUCCESS;
		}

Linus Torvalds's avatar
Linus Torvalds committed
675
		if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID)
676 677
			mptscsih_copy_sense_data(sc, hd, mf, pScsiReply);

Linus Torvalds's avatar
Linus Torvalds committed
678 679 680
		/*
		 *  Look for + dump FCP ResponseInfo[]!
		 */
681 682
		if (scsi_state & MPI_SCSI_STATE_RESPONSE_INFO_VALID &&
		    pScsiReply->ResponseInfo) {
683 684
			printk(MYIOC_s_NOTE_FMT "[%d:%d:%d:%d] "
			"FCP_ResponseInfo=%08xh\n", ioc->name,
685 686
			sc->device->host->host_no, sc->device->channel,
			sc->device->id, sc->device->lun,
Linus Torvalds's avatar
Linus Torvalds committed
687 688 689 690 691
			le32_to_cpu(pScsiReply->ResponseInfo));
		}

		switch(status) {
		case MPI_IOCSTATUS_BUSY:			/* 0x0002 */
692
		case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES:	/* 0x0006 */
Linus Torvalds's avatar
Linus Torvalds committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
			/* CHECKME!
			 * Maybe: DRIVER_BUSY | SUGGEST_RETRY | DID_SOFT_ERROR (retry)
			 * But not: DID_BUS_BUSY lest one risk
			 * killing interrupt handler:-(
			 */
			sc->result = SAM_STAT_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! */
708 709 710 711 712
			if (ioc->bus_type != FC)
				sc->result = DID_NO_CONNECT << 16;
			/* else fibre, just stall until rescan event */
			else
				sc->result = DID_REQUEUE << 16;
Linus Torvalds's avatar
Linus Torvalds committed
713 714 715

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

717 718
			vdevice = sc->device->hostdata;
			if (!vdevice)
719
				break;
720
			vtarget = vdevice->vtarget;
721 722 723 724 725
			if (vtarget->tflags & MPT_TARGET_FLAGS_LED_ON) {
				mptscsih_issue_sep_command(ioc, vtarget,
				    MPI_SEP_REQ_SLOTSTATUS_UNCONFIGURED);
				vtarget->tflags &= ~MPT_TARGET_FLAGS_LED_ON;
			}
Linus Torvalds's avatar
Linus Torvalds committed
726 727 728
			break;

		case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:		/* 0x004B */
729 730 731
			if ( ioc->bus_type == SAS ) {
				u16 ioc_status = le16_to_cpu(pScsiReply->IOCStatus);
				if (ioc_status & MPI_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
732 733
					if ((log_info & SAS_LOGINFO_MASK)
					    == SAS_LOGINFO_NEXUS_LOSS) {
734 735 736 737
						sc->result = (DID_BUS_BUSY << 16);
						break;
					}
				}
738 739 740 741 742 743 744 745 746 747 748
			} else if (ioc->bus_type == FC) {
				/*
				 * The FC IOC may kill a request for variety of
				 * reasons, some of which may be recovered by a
				 * retry, some which are unlikely to be
				 * recovered. Return DID_ERROR instead of
				 * DID_RESET to permit retry of the command,
				 * just not an infinite number of them
				 */
				sc->result = DID_ERROR << 16;
				break;
749 750 751 752 753 754 755
			}

			/*
			 * Allow non-SAS & non-NEXUS_LOSS to drop into below code
			 */

		case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:	/* 0x0048 */
Linus Torvalds's avatar
Linus Torvalds committed
756 757 758 759 760
			/* Linux handles an unsolicited DID_RESET better
			 * than an unsolicited DID_ABORT.
			 */
			sc->result = DID_RESET << 16;

761 762 763 764 765
		case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:		/* 0x004C */
			if (ioc->bus_type == FC)
				sc->result = DID_ERROR << 16;
			else
				sc->result = DID_RESET << 16;
Linus Torvalds's avatar
Linus Torvalds committed
766 767 768
			break;

		case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:	/* 0x0049 */
769
			scsi_set_resid(sc, scsi_bufflen(sc) - xfer_cnt);
770 771 772
			if((xfer_cnt==0)||(sc->underflow > xfer_cnt))
				sc->result=DID_SOFT_ERROR << 16;
			else /* Sufficient data transfer occurred */
Linus Torvalds's avatar
Linus Torvalds committed
773
				sc->result = (DID_OK << 16) | scsi_status;
774
			dreplyprintk(ioc, printk(MYIOC_s_DEBUG_FMT
775
			    "RESIDUAL_MISMATCH: result=%x on channel=%d id=%d\n",
776
			    ioc->name, sc->result, sc->device->channel, sc->device->id));
Linus Torvalds's avatar
Linus Torvalds committed
777
			break;
778

Linus Torvalds's avatar
Linus Torvalds committed
779 780 781 782 783 784 785 786 787 788 789 790
		case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:		/* 0x0045 */
			/*
			 *  Do upfront check for valid SenseData and give it
			 *  precedence!
			 */
			sc->result = (DID_OK << 16) | scsi_status;
			if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
				/* Have already saved the status and sense data
				 */
				;
			} else {
				if (xfer_cnt < sc->underflow) {
791 792 793 794
					if (scsi_status == SAM_STAT_BUSY)
						sc->result = SAM_STAT_BUSY;
					else
						sc->result = DID_SOFT_ERROR << 16;
Linus Torvalds's avatar
Linus Torvalds committed
795 796 797 798 799 800 801 802 803 804 805 806
				}
				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;
				}
			}

807

808 809 810 811 812
			dreplyprintk(ioc, printk(MYIOC_s_DEBUG_FMT
			    "  sc->underflow={report ERR if < %02xh bytes xfer'd}\n",
			    ioc->name, sc->underflow));
			dreplyprintk(ioc, printk(MYIOC_s_DEBUG_FMT
			    "  ActBytesXferd=%02xh\n", ioc->name, xfer_cnt));
813

Linus Torvalds's avatar
Linus Torvalds committed
814 815 816 817
			/* Report Queue Full
			 */
			if (scsi_status == MPI_SCSI_STATUS_TASK_SET_FULL)
				mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
818

Linus Torvalds's avatar
Linus Torvalds committed
819 820
			break;

821
		case MPI_IOCSTATUS_SCSI_DATA_OVERRUN:		/* 0x0044 */
822
			scsi_set_resid(sc, 0);
Linus Torvalds's avatar
Linus Torvalds committed
823 824
		case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:	/* 0x0040 */
		case MPI_IOCSTATUS_SUCCESS:			/* 0x0000 */
825
			sc->result = (DID_OK << 16) | scsi_status;
Linus Torvalds's avatar
Linus Torvalds committed
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
			if (scsi_state == 0) {
				;
			} else if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
				/*
				 * If running against circa 200003dd 909 MPT f/w,
				 * may get this (AUTOSENSE_VALID) for actual TASK_SET_FULL
				 * (QUEUE_FULL) returned from device! --> get 0x0000?128
				 * and with SenseBytes set to 0.
				 */
				if (pScsiReply->SCSIStatus == MPI_SCSI_STATUS_TASK_SET_FULL)
					mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);

			}
			else 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;
			}
			else if (scsi_state & MPI_SCSI_STATE_QUEUE_TAG_REJECTED) {
				/* Device Inq. data indicates that it supports
				 * QTags, but rejects QTag messages.
				 * This command completed OK.
				 *
				 * Not real sure here either so do nothing...  */
			}

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

			/* Add handling of:
			 * Reservation Conflict, Busy,
			 * Command Terminated, CHECK
			 */
			break;

		case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:		/* 0x0047 */
			sc->result = DID_SOFT_ERROR << 16;
			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_INVALID_FIELD:		/* 0x0007 */
		case MPI_IOCSTATUS_INVALID_STATE:		/* 0x0008 */
		case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:		/* 0x0046 */
		case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED:	/* 0x004A */
		default:
			/*
			 * What to do?
			 */
			sc->result = DID_SOFT_ERROR << 16;
			break;

		}	/* switch(status) */

889 890 891
#ifdef CONFIG_FUSION_LOGGING
		if (sc->result && (ioc->debug_level & MPT_DEBUG_REPLY))
			mptscsih_info_scsiio(ioc, sc, pScsiReply);
892 893
#endif

Linus Torvalds's avatar
Linus Torvalds committed
894 895 896
	} /* end of address reply case */

	/* Unmap the DMA buffers, if any. */
897
	scsi_dma_unmap(sc);
Linus Torvalds's avatar
Linus Torvalds committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

	sc->scsi_done(sc);		/* Issue the command callback */

	/* Free Chain buffers */
	mptscsih_freeChainBuffers(ioc, req_idx);
	return 1;
}

/*
 *	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
 *
 *	Returns: None.
 *
 *	Must be called while new I/Os are being queued.
 */
static void
mptscsih_flush_running_cmds(MPT_SCSI_HOST *hd)
{
	MPT_ADAPTER *ioc = hd->ioc;
920 921
	struct scsi_cmnd *sc;
	SCSIIORequest_t	*mf = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
922
	int		 ii;
923
	int		 channel, id;
Linus Torvalds's avatar
Linus Torvalds committed
924

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	for (ii= 0; ii < ioc->req_depth; ii++) {
		sc = mptscsih_getclear_scsi_lookup(ioc, ii);
		if (!sc)
			continue;
		mf = (SCSIIORequest_t *)MPT_INDEX_2_MFPTR(ioc, ii);
		if (!mf)
			continue;
		channel = mf->Bus;
		id = mf->TargetID;
		mptscsih_freeChainBuffers(ioc, ii);
		mpt_free_msg_frame(ioc, (MPT_FRAME_HDR *)mf);
		if ((unsigned char *)mf != sc->host_scribble)
			continue;
		scsi_dma_unmap(sc);
		sc->result = DID_RESET << 16;
		sc->host_scribble = NULL;
941 942 943
		dtmprintk(ioc, sdev_printk(KERN_INFO, sc->device, MYIOC_s_FMT
		    "completing cmds: fw_channel %d, fw_id %d, sc=%p, mf = %p, "
		    "idx=%x\n", ioc->name, channel, id, sc, mf, ii));
944
		sc->scsi_done(sc);
Linus Torvalds's avatar
Linus Torvalds committed
945 946 947 948 949 950 951 952 953
	}
}

/*
 *	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.
954
 *		(BUT, BUT, BUT, the code does reference it! - mdr)
955 956
 *      @hd: Pointer to a SCSI HOST structure
 *	@vdevice: per device private data
Linus Torvalds's avatar
Linus Torvalds committed
957 958 959 960 961 962
 *
 *	Returns: None.
 *
 *	Called from slave_destroy.
 */
static void
963
mptscsih_search_running_cmds(MPT_SCSI_HOST *hd, VirtDevice *vdevice)
Linus Torvalds's avatar
Linus Torvalds committed
964 965 966
{
	SCSIIORequest_t	*mf = NULL;
	int		 ii;
967
	struct scsi_cmnd *sc;
968
	struct scsi_lun  lun;
969
	MPT_ADAPTER *ioc = hd->ioc;
970
	unsigned long	flags;
Linus Torvalds's avatar
Linus Torvalds committed
971

972 973 974
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	for (ii = 0; ii < ioc->req_depth; ii++) {
		if ((sc = ioc->ScsiLookup[ii]) != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
975

976
			mf = (SCSIIORequest_t *)MPT_INDEX_2_MFPTR(ioc, ii);
977 978
			if (mf == NULL)
				continue;
979 980 981 982 983 984 985 986
			/* If the device is a hidden raid component, then its
			 * expected that the mf->function will be RAID_SCSI_IO
			 */
			if (vdevice->vtarget->tflags &
			    MPT_TARGET_FLAGS_RAID_COMPONENT && mf->Function !=
			    MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)
				continue;

987 988 989 990
			int_to_scsilun(vdevice->lun, &lun);
			if ((mf->Bus != vdevice->vtarget->channel) ||
			    (mf->TargetID != vdevice->vtarget->id) ||
			    memcmp(lun.scsi_lun, mf->LUN, 8))
Linus Torvalds's avatar
Linus Torvalds committed
991 992
				continue;

993 994
			if ((unsigned char *)mf != sc->host_scribble)
				continue;
995 996 997 998
			ioc->ScsiLookup[ii] = NULL;
			spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
			mptscsih_freeChainBuffers(ioc, ii);
			mpt_free_msg_frame(ioc, (MPT_FRAME_HDR *)mf);
999
			scsi_dma_unmap(sc);
1000 1001
			sc->host_scribble = NULL;
			sc->result = DID_NO_CONNECT << 16;
1002 1003 1004 1005 1006
			dtmprintk(ioc, sdev_printk(KERN_INFO, sc->device,
			   MYIOC_s_FMT "completing cmds: fw_channel %d, "
			   "fw_id %d, sc=%p, mf = %p, idx=%x\n", ioc->name,
			   vdevice->vtarget->channel, vdevice->vtarget->id,
			   sc, mf, ii));
1007
			sc->scsi_done(sc);
1008
			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1009 1010
		}
	}
1011
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	return;
}

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

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	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
 *
 *	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.
 */
static void
mptscsih_report_queue_full(struct scsi_cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq)
{
	long time = jiffies;
	MPT_SCSI_HOST		*hd;
1034
	MPT_ADAPTER	*ioc;
Linus Torvalds's avatar
Linus Torvalds committed
1035

1036 1037 1038 1039
	if (sc->device == NULL)
		return;
	if (sc->device->host == NULL)
		return;
1040
	if ((hd = shost_priv(sc->device->host)) == NULL)
1041
		return;
1042
	ioc = hd->ioc;
1043
	if (time - hd->last_queue_full > 10 * HZ) {
1044 1045
		dprintk(ioc, printk(MYIOC_s_WARN_FMT "Device (%d:%d:%d) reported QUEUE_FULL!\n",
				ioc->name, 0, sc->device->id, sc->device->lun));
1046
		hd->last_queue_full = time;
Linus Torvalds's avatar
Linus Torvalds committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	}
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_remove - Removed scsi devices
 *	@pdev: Pointer to pci_dev structure
 *
 *
 */
1057
void
Linus Torvalds's avatar
Linus Torvalds committed
1058 1059 1060 1061 1062
mptscsih_remove(struct pci_dev *pdev)
{
	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
	struct Scsi_Host 	*host = ioc->sh;
	MPT_SCSI_HOST		*hd;
1063
	int sz1;
Linus Torvalds's avatar
Linus Torvalds committed
1064

1065 1066
	if(!host) {
		mpt_detach(pdev);
Linus Torvalds's avatar
Linus Torvalds committed
1067
		return;
1068
	}
Linus Torvalds's avatar
Linus Torvalds committed
1069 1070 1071

	scsi_remove_host(host);

1072
	if((hd = shost_priv(host)) == NULL)
1073 1074
		return;

1075
	mptscsih_shutdown(pdev);
Linus Torvalds's avatar
Linus Torvalds committed
1076

1077
	sz1=0;
Linus Torvalds's avatar
Linus Torvalds committed
1078

1079
	if (ioc->ScsiLookup != NULL) {
1080
		sz1 = ioc->req_depth * sizeof(void *);
1081 1082
		kfree(ioc->ScsiLookup);
		ioc->ScsiLookup = NULL;
1083
	}
Linus Torvalds's avatar
Linus Torvalds committed
1084

1085
	dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
1086
	    "Free'd ScsiLookup (%d) memory\n",
1087
	    ioc->name, sz1));
Linus Torvalds's avatar
Linus Torvalds committed
1088

1089
	kfree(hd->info_kbuf);
Linus Torvalds's avatar
Linus Torvalds committed
1090

1091 1092
	/* NULL the Scsi_Host pointer
	 */
1093
	ioc->sh = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1094 1095 1096

	scsi_host_put(host);

1097
	mpt_detach(pdev);
1098

Linus Torvalds's avatar
Linus Torvalds committed
1099 1100 1101 1102 1103 1104 1105
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_shutdown - reboot notifier
 *
 */
1106
void
1107
mptscsih_shutdown(struct pci_dev *pdev)
Linus Torvalds's avatar
Linus Torvalds committed
1108 1109 1110 1111 1112 1113
{
}

#ifdef CONFIG_PM
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
1114
 *	mptscsih_suspend - Fusion MPT scsi driver suspend routine.
Linus Torvalds's avatar
Linus Torvalds committed
1115 1116 1117
 *
 *
 */
1118
int
1119
mptscsih_suspend(struct pci_dev *pdev, pm_message_t state)
Linus Torvalds's avatar
Linus Torvalds committed
1120
{
1121 1122 1123 1124
	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);

	scsi_block_requests(ioc->sh);
	flush_scheduled_work();
1125
	mptscsih_shutdown(pdev);
1126
	return mpt_suspend(pdev,state);
Linus Torvalds's avatar
Linus Torvalds committed
1127 1128 1129 1130 1131 1132 1133 1134
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_resume - Fusion MPT scsi driver resume routine.
 *
 *
 */
1135
int
Linus Torvalds's avatar
Linus Torvalds committed
1136 1137
mptscsih_resume(struct pci_dev *pdev)
{
1138 1139 1140 1141 1142 1143
	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
	int rc;

	rc = mpt_resume(pdev);
	scsi_unblock_requests(ioc->sh);
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
}

#endif

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_info - Return information about MPT adapter
 *	@SChost: Pointer to Scsi_Host structure
 *
 *	(linux scsi_host_template.info routine)
 *
 *	Returns pointer to buffer where information was written.
 */
1157
const char *
Linus Torvalds's avatar
Linus Torvalds committed
1158 1159 1160 1161 1162
mptscsih_info(struct Scsi_Host *SChost)
{
	MPT_SCSI_HOST *h;
	int size = 0;

1163
	h = shost_priv(SChost);
1164

Linus Torvalds's avatar
Linus Torvalds committed
1165
	if (h) {
1166 1167 1168 1169 1170 1171 1172
		if (h->info_kbuf == NULL)
			if ((h->info_kbuf = kmalloc(0x1000 /* 4Kb */, GFP_KERNEL)) == NULL)
				return h->info_kbuf;
		h->info_kbuf[0] = '\0';

		mpt_print_ioc_summary(h->ioc, h->info_kbuf, &size, 0, 0);
		h->info_kbuf[size-1] = '\0';
Linus Torvalds's avatar
Linus Torvalds committed
1173 1174
	}

1175
	return h->info_kbuf;
Linus Torvalds's avatar
Linus Torvalds committed
1176 1177 1178 1179 1180 1181 1182 1183 1184
}

struct info_str {
	char *buffer;
	int   length;
	int   offset;
	int   pos;
};

1185 1186
static void
mptscsih_copy_mem_info(struct info_str *info, char *data, int len)
Linus Torvalds's avatar
Linus Torvalds committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
{
	if (info->pos + len > info->length)
		len = info->length - info->pos;

	if (info->pos + len < info->offset) {
		info->pos += len;
		return;
	}

	if (info->pos < info->offset) {
	        data += (info->offset - info->pos);
	        len  -= (info->offset - info->pos);
	}

	if (len > 0) {
                memcpy(info->buffer + info->pos, data, len);
                info->pos += len;
	}
}

1207 1208
static int
mptscsih_copy_info(struct info_str *info, char *fmt, ...)
Linus Torvalds's avatar
Linus Torvalds committed
1209 1210 1211 1212 1213 1214 1215 1216 1217
{
	va_list args;
	char buf[81];
	int len;

	va_start(args, fmt);
	len = vsprintf(buf, fmt, args);
	va_end(args);

1218
	mptscsih_copy_mem_info(info, buf, len);
Linus Torvalds's avatar
Linus Torvalds committed
1219 1220 1221
	return len;
}

1222 1223
static int
mptscsih_host_info(MPT_ADAPTER *ioc, char *pbuf, off_t offset, int len)
Linus Torvalds's avatar
Linus Torvalds committed
1224 1225 1226 1227 1228 1229 1230 1231
{
	struct info_str info;

	info.buffer	= pbuf;
	info.length	= len;
	info.offset	= offset;
	info.pos	= 0;

1232 1233 1234 1235
	mptscsih_copy_info(&info, "%s: %s, ", ioc->name, ioc->prod_name);
	mptscsih_copy_info(&info, "%s%08xh, ", MPT_FW_REV_MAGIC_ID_STRING, ioc->facts.FWVersion.Word);
	mptscsih_copy_info(&info, "Ports=%d, ", ioc->facts.NumberOfPorts);
	mptscsih_copy_info(&info, "MaxQ=%d\n", ioc->req_depth);
Linus Torvalds's avatar
Linus Torvalds committed
1236 1237 1238 1239 1240 1241 1242

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

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_proc_info - Return information about MPT adapter
1243 1244 1245 1246 1247 1248 1249
 * 	@host:   scsi host struct
 * 	@buffer: if write, user data; if read, buffer for user
 *	@start: returns the buffer address
 * 	@offset: if write, 0; if read, the current offset into the buffer from
 * 		 the previous read.
 * 	@length: if write, return length;
 *	@func:   write = 1; read = 0
Linus Torvalds's avatar
Linus Torvalds committed
1250 1251 1252
 *
 *	(linux scsi_host_template.info routine)
 */
1253
int
Linus Torvalds's avatar
Linus Torvalds committed
1254 1255 1256
mptscsih_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
			int length, int func)
{
1257
	MPT_SCSI_HOST	*hd = shost_priv(host);
Linus Torvalds's avatar
Linus Torvalds committed
1258 1259 1260 1261
	MPT_ADAPTER	*ioc = hd->ioc;
	int size = 0;

	if (func) {
1262 1263
		/*
		 * write is not supported
Linus Torvalds's avatar
Linus Torvalds committed
1264 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
		 */
	} else {
		if (start)
			*start = buffer;

		size = mptscsih_host_info(ioc, buffer, offset, length);
	}

	return size;
}

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

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_qcmd - Primary Fusion MPT SCSI initiator IO start routine.
 *	@SCpnt: Pointer to scsi_cmnd structure
 *	@done: Pointer SCSI mid-layer IO completion function
 *
 *	(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)
 */
1290
int
Linus Torvalds's avatar
Linus Torvalds committed
1291 1292 1293 1294 1295
mptscsih_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
{
	MPT_SCSI_HOST		*hd;
	MPT_FRAME_HDR		*mf;
	SCSIIORequest_t		*pScsiReq;
1296
	VirtDevice		*vdevice = SCpnt->device->hostdata;
Linus Torvalds's avatar
Linus Torvalds committed
1297 1298 1299 1300 1301 1302
	u32	 datalen;
	u32	 scsictl;
	u32	 scsidir;
	u32	 cmd_len;
	int	 my_idx;
	int	 ii;
1303
	MPT_ADAPTER *ioc;
Linus Torvalds's avatar
Linus Torvalds committed
1304

1305
	hd = shost_priv(SCpnt->device->host);
1306
	ioc = hd->ioc;
Linus Torvalds's avatar
Linus Torvalds committed
1307 1308
	SCpnt->scsi_done = done;

1309 1310
	dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "qcmd: SCpnt=%p, done()=%p\n",
		ioc->name, SCpnt, done));
Linus Torvalds's avatar
Linus Torvalds committed
1311

1312
	if (ioc->taskmgmt_quiesce_io) {
1313 1314
		dtmprintk(ioc, printk(MYIOC_s_WARN_FMT "qcmd: SCpnt=%p timeout + 60HZ\n",
			ioc->name, SCpnt));
Linus Torvalds's avatar
Linus Torvalds committed
1315 1316 1317 1318 1319 1320
		return SCSI_MLQUEUE_HOST_BUSY;
	}

	/*
	 *  Put together a MPT SCSI request...
	 */
1321
	if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
1322 1323
		dprintk(ioc, printk(MYIOC_s_WARN_FMT "QueueCmd, no msg frames!!\n",
				ioc->name));
Linus Torvalds's avatar
Linus Torvalds committed
1324 1325 1326 1327 1328 1329 1330 1331 1332
		return SCSI_MLQUEUE_HOST_BUSY;
	}

	pScsiReq = (SCSIIORequest_t *) mf;

	my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);

	ADD_INDEX_LOG(my_idx);

1333
	/*    TUR's being issued with scsictl=0x02000000 (DATA_IN)!
Linus Torvalds's avatar
Linus Torvalds committed
1334 1335 1336 1337
	 *    Seems we may receive a buffer (datalen>0) even when there
	 *    will be no data transfer!  GRRRRR...
	 */
	if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
1338
		datalen = scsi_bufflen(SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1339 1340
		scsidir = MPI_SCSIIO_CONTROL_READ;	/* DATA IN  (host<--ioc<--dev) */
	} else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
1341
		datalen = scsi_bufflen(SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1342 1343 1344 1345 1346 1347 1348 1349 1350
		scsidir = MPI_SCSIIO_CONTROL_WRITE;	/* DATA OUT (host-->ioc-->dev) */
	} else {
		datalen = 0;
		scsidir = MPI_SCSIIO_CONTROL_NODATATRANSFER;
	}

	/* Default to untagged. Once a target structure has been allocated,
	 * use the Inquiry data to determine if device supports tagged.
	 */
1351 1352
	if (vdevice
	    && (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_Q_YES)
Linus Torvalds's avatar
Linus Torvalds committed
1353 1354 1355 1356 1357 1358 1359 1360
	    && (SCpnt->device->tagged_supported)) {
		scsictl = scsidir | MPI_SCSIIO_CONTROL_SIMPLEQ;
	} else {
		scsictl = scsidir | MPI_SCSIIO_CONTROL_UNTAGGED;
	}

	/* Use the above information to set up the message frame
	 */
1361 1362
	pScsiReq->TargetID = (u8) vdevice->vtarget->id;
	pScsiReq->Bus = vdevice->vtarget->channel;
Linus Torvalds's avatar
Linus Torvalds committed
1363
	pScsiReq->ChainOffset = 0;
1364
	if (vdevice->vtarget->tflags &  MPT_TARGET_FLAGS_RAID_COMPONENT)
1365 1366 1367
		pScsiReq->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
	else
		pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
Linus Torvalds's avatar
Linus Torvalds committed
1368 1369 1370
	pScsiReq->CDBLength = SCpnt->cmd_len;
	pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
	pScsiReq->Reserved = 0;
1371
	pScsiReq->MsgFlags = mpt_msg_flags(ioc);
1372
	int_to_scsilun(SCpnt->device->lun, (struct scsi_lun *)pScsiReq->LUN);
Linus Torvalds's avatar
Linus Torvalds committed
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
	pScsiReq->Control = cpu_to_le32(scsictl);

	/*
	 *  Write SCSI CDB into the message
	 */
	cmd_len = SCpnt->cmd_len;
	for (ii=0; ii < cmd_len; ii++)
		pScsiReq->CDB[ii] = SCpnt->cmnd[ii];

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

	/* DataLength */
	pScsiReq->DataLength = cpu_to_le32(datalen);

	/* SenseBuffer low address */
1389
	pScsiReq->SenseBufferLowAddr = cpu_to_le32(ioc->sense_buf_low_dma
Linus Torvalds's avatar
Linus Torvalds committed
1390 1391 1392 1393 1394 1395 1396
					   + (my_idx * MPT_SENSE_BUFFER_ALLOC));

	/* Now add the SG list
	 * Always have a SGE even if null length.
	 */
	if (datalen == 0) {
		/* Add a NULL SGE */
1397 1398
		ioc->add_sge((char *)&pScsiReq->SGL,
			MPT_SGE_FLAGS_SSIMPLE_READ | 0,
Linus Torvalds's avatar
Linus Torvalds committed
1399 1400 1401
			(dma_addr_t) -1);
	} else {
		/* Add a 32 or 64 bit SGE */
1402
		if (mptscsih_AddSGE(ioc, SCpnt, pScsiReq, my_idx) != SUCCESS)
Linus Torvalds's avatar
Linus Torvalds committed
1403 1404 1405
			goto fail;
	}

1406
	SCpnt->host_scribble = (unsigned char *)mf;
1407
	mptscsih_set_scsi_lookup(ioc, my_idx, SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1408

1409
	mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
1410 1411
	dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Issued SCSI cmd (%p) mf=%p idx=%d\n",
			ioc->name, SCpnt, mf, my_idx));
1412
	DBG_DUMP_REQUEST_FRAME(ioc, (u32 *)mf);
Linus Torvalds's avatar
Linus Torvalds committed
1413 1414 1415
	return 0;

 fail:
1416 1417
	mptscsih_freeChainBuffers(ioc, my_idx);
	mpt_free_msg_frame(ioc, mf);
Linus Torvalds's avatar
Linus Torvalds committed
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 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
	return SCSI_MLQUEUE_HOST_BUSY;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	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.
 */
static void
mptscsih_freeChainBuffers(MPT_ADAPTER *ioc, int req_idx)
{
	MPT_FRAME_HDR *chain;
	unsigned long flags;
	int chain_idx;
	int next;

	/* Get the first chain index and reset
	 * tracker state.
	 */
	chain_idx = ioc->ReqToChain[req_idx];
	ioc->ReqToChain[req_idx] = MPT_HOST_NO_CHAIN;

	while (chain_idx != MPT_HOST_NO_CHAIN) {

		/* Save the next chain buffer index */
		next = ioc->ChainToChain[chain_idx];

		/* Free this chain buffer and reset
		 * tracker
		 */
		ioc->ChainToChain[chain_idx] = MPT_HOST_NO_CHAIN;

		chain = (MPT_FRAME_HDR *) (ioc->ChainBuffer
					+ (chain_idx * ioc->req_sz));

		spin_lock_irqsave(&ioc->FreeQlock, flags);
		list_add_tail(&chain->u.frame.linkage.list, &ioc->FreeChainQ);
		spin_unlock_irqrestore(&ioc->FreeQlock, flags);

1462
		dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "FreeChainBuffers (index %d)\n",
Linus Torvalds's avatar
Linus Torvalds committed
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
				ioc->name, chain_idx));

		/* handle next */
		chain_idx = next;
	}
	return;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	Reset Handling
 */

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1477
/**
1478 1479
 *	mptscsih_IssueTaskMgmt - Generic send Task Management function.
 *	@hd: Pointer to MPT_SCSI_HOST structure
Linus Torvalds's avatar
Linus Torvalds committed
1480
 *	@type: Task Management type
1481
 *	@channel: channel number for task management
1482
 *	@id: Logical Target ID for reset (if appropriate)
Linus Torvalds's avatar
Linus Torvalds committed
1483 1484
 *	@lun: Logical Unit for reset (if appropriate)
 *	@ctx2abort: Context for the task to be aborted (if appropriate)
1485 1486
 *	@timeout: timeout for task management control
 *
1487 1488
 *	Remark: _HardResetHandler can be invoked from an interrupt thread (timer)
 *	or a non-interrupt thread.  In the former, must not call schedule().
Linus Torvalds's avatar
Linus Torvalds committed
1489
 *
1490
 *	Not all fields are meaningfull for all task types.
Linus Torvalds's avatar
Linus Torvalds committed
1491
 *
1492
 *	Returns 0 for SUCCESS, or FAILED.
Linus Torvalds's avatar
Linus Torvalds committed
1493
 *
1494
 **/
James Bottomley's avatar
James Bottomley committed
1495
int
1496 1497
mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 id, int lun,
	int ctx2abort, ulong timeout)
Linus Torvalds's avatar
Linus Torvalds committed
1498
{
1499 1500 1501 1502 1503 1504 1505
	MPT_FRAME_HDR	*mf;
	SCSITaskMgmt_t	*pScsiTm;
	int		 ii;
	int		 retval;
	MPT_ADAPTER 	*ioc = hd->ioc;
	unsigned long	 timeleft;
	u8		 issue_hard_reset;
Linus Torvalds's avatar
Linus Torvalds committed
1506
	u32		 ioc_raw_state;
1507
	unsigned long	 time_count;
Linus Torvalds's avatar
Linus Torvalds committed
1508

1509
	issue_hard_reset = 0;
1510
	ioc_raw_state = mpt_GetIocState(ioc, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1511 1512 1513

	if ((ioc_raw_state & MPI_IOC_STATE_MASK) != MPI_IOC_STATE_OPERATIONAL) {
		printk(MYIOC_s_WARN_FMT
1514
			"TaskMgmt type=%x: IOC Not operational (0x%x)!\n",
1515
			ioc->name, type, ioc_raw_state);
1516 1517
		printk(MYIOC_s_WARN_FMT "Issuing HardReset from %s!!\n",
		    ioc->name, __func__);
1518
		if (mpt_HardResetHandler(ioc, CAN_SLEEP) < 0)
1519
			printk(MYIOC_s_WARN_FMT "TaskMgmt HardReset "
1520
			    "FAILED!!\n", ioc->name);
1521
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1522 1523
	}

1524 1525
	if (ioc_raw_state & MPI_DOORBELL_ACTIVE) {
		printk(MYIOC_s_WARN_FMT
1526
			"TaskMgmt type=%x: ioc_state: "
1527 1528 1529
			"DOORBELL_ACTIVE (0x%x)!\n",
			ioc->name, type, ioc_raw_state);
		return FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
1530 1531
	}

1532 1533 1534 1535 1536 1537
	mutex_lock(&ioc->taskmgmt_cmds.mutex);
	if (mpt_set_taskmgmt_in_progress_flag(ioc) != 0) {
		mf = NULL;
		retval = FAILED;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1538 1539 1540

	/* Return Fail to calling function if no message frames available.
	 */
1541
	if ((mf = mpt_get_msg_frame(ioc->TaskCtx, ioc)) == NULL) {
1542 1543 1544 1545 1546
		dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
			"TaskMgmt no msg frames!!\n", ioc->name));
		retval = FAILED;
		mpt_clear_taskmgmt_in_progress_flag(ioc);
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1547
	}
1548
	dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt request (mf=%p)\n",
1549
			ioc->name, mf));
Linus Torvalds's avatar
Linus Torvalds committed
1550 1551 1552 1553

	/* Format the Request
	 */
	pScsiTm = (SCSITaskMgmt_t *) mf;
1554
	pScsiTm->TargetID = id;
Linus Torvalds's avatar
Linus Torvalds committed
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
	pScsiTm->Bus = channel;
	pScsiTm->ChainOffset = 0;
	pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;

	pScsiTm->Reserved = 0;
	pScsiTm->TaskType = type;
	pScsiTm->Reserved1 = 0;
	pScsiTm->MsgFlags = (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS)
                    ? MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION : 0;

1565
	int_to_scsilun(lun, (struct scsi_lun *)pScsiTm->LUN);
Linus Torvalds's avatar
Linus Torvalds committed
1566 1567 1568 1569 1570 1571

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

	pScsiTm->TaskMsgContext = ctx2abort;

1572 1573 1574
	dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt: ctx2abort (0x%08x) "
		"task_type = 0x%02X, timeout = %ld\n", ioc->name, ctx2abort,
		type, timeout));
Linus Torvalds's avatar
Linus Torvalds committed
1575

1576
	DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)pScsiTm);
Linus Torvalds's avatar
Linus Torvalds committed
1577

1578 1579
	INITIALIZE_MGMT_STATUS(ioc->taskmgmt_cmds.status)
	time_count = jiffies;
1580 1581 1582
	if ((ioc->facts.IOCCapabilities & MPI_IOCFACTS_CAPABILITY_HIGH_PRI_Q) &&
	    (ioc->facts.MsgVersion >= MPI_VERSION_01_05))
		mpt_put_msg_frame_hi_pri(ioc->TaskCtx, ioc, mf);
1583
	else {
1584
		retval = mpt_send_handshake_request(ioc->TaskCtx, ioc,
1585 1586
			sizeof(SCSITaskMgmt_t), (u32*)pScsiTm, CAN_SLEEP);
		if (retval) {
1587 1588 1589 1590 1591 1592
			dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
				"TaskMgmt handshake FAILED!(mf=%p, rc=%d) \n",
				ioc->name, mf, retval));
			mpt_free_msg_frame(ioc, mf);
			mpt_clear_taskmgmt_in_progress_flag(ioc);
			goto out;
1593
		}
Linus Torvalds's avatar
Linus Torvalds committed
1594 1595
	}

1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
	timeleft = wait_for_completion_timeout(&ioc->taskmgmt_cmds.done,
		timeout*HZ);
	if (!(ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
		retval = FAILED;
		dtmprintk(ioc, printk(MYIOC_s_ERR_FMT
		    "TaskMgmt TIMED OUT!(mf=%p)\n", ioc->name, mf));
		mpt_clear_taskmgmt_in_progress_flag(ioc);
		if (ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_DID_IOCRESET)
			goto out;
		issue_hard_reset = 1;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1607 1608
	}

1609 1610
	retval = mptscsih_taskmgmt_reply(ioc, type,
	    (SCSITaskMgmtReply_t *) ioc->taskmgmt_cmds.reply);
1611

1612 1613 1614
	dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
	    "TaskMgmt completed (%d seconds)\n",
	    ioc->name, jiffies_to_msecs(jiffies - time_count)/1000));
1615

1616
 out:
1617

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
	CLEAR_MGMT_STATUS(ioc->taskmgmt_cmds.status)
	if (issue_hard_reset) {
		printk(MYIOC_s_WARN_FMT "Issuing Reset from %s!!\n",
			ioc->name, __func__);
		retval = mpt_HardResetHandler(ioc, CAN_SLEEP);
		mpt_free_msg_frame(ioc, mf);
	}

	retval = (retval == 0) ? 0 : FAILED;
	mutex_unlock(&ioc->taskmgmt_cmds.mutex);
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1629
}
1630
EXPORT_SYMBOL(mptscsih_IssueTaskMgmt);
Linus Torvalds's avatar
Linus Torvalds committed
1631

1632 1633 1634 1635 1636 1637 1638 1639 1640
static int
mptscsih_get_tm_timeout(MPT_ADAPTER *ioc)
{
	switch (ioc->bus_type) {
	case FC:
		return 40;
	case SAS:
	case SPI:
	default:
1641
		return 10;
1642 1643 1644
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
1645 1646 1647 1648 1649 1650 1651 1652
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	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.
1653
 **/
1654
int
Linus Torvalds's avatar
Linus Torvalds committed
1655 1656 1657 1658 1659 1660
mptscsih_abort(struct scsi_cmnd * SCpnt)
{
	MPT_SCSI_HOST	*hd;
	MPT_FRAME_HDR	*mf;
	u32		 ctx2abort;
	int		 scpnt_idx;
1661
	int		 retval;
1662
	VirtDevice	 *vdevice;
1663
	ulong	 	 sn = SCpnt->serial_number;
1664
	MPT_ADAPTER	*ioc;
Linus Torvalds's avatar
Linus Torvalds committed
1665 1666 1667

	/* If we can't locate our host adapter structure, return FAILED status.
	 */
1668
	if ((hd = shost_priv(SCpnt->device->host)) == NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
1669 1670
		SCpnt->result = DID_RESET << 16;
		SCpnt->scsi_done(SCpnt);
1671 1672
		printk(KERN_ERR MYNAM ": task abort: "
		    "can't locate host! (sc=%p)\n", SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1673 1674 1675
		return FAILED;
	}

1676 1677 1678 1679 1680 1681 1682
	ioc = hd->ioc;
	printk(MYIOC_s_INFO_FMT "attempting task abort! (sc=%p)\n",
	       ioc->name, SCpnt);
	scsi_print_command(SCpnt);

	vdevice = SCpnt->device->hostdata;
	if (!vdevice || !vdevice->vtarget) {
1683 1684 1685
		dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "task abort: device has been deleted (sc=%p)\n",
		    ioc->name, SCpnt));
1686 1687 1688 1689 1690 1691
		SCpnt->result = DID_NO_CONNECT << 16;
		SCpnt->scsi_done(SCpnt);
		retval = 0;
		goto out;
	}

1692 1693 1694
	/* Task aborts are not supported for hidden raid components.
	 */
	if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1695 1696 1697
		dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "task abort: hidden raid component (sc=%p)\n",
		    ioc->name, SCpnt));
1698 1699 1700 1701 1702
		SCpnt->result = DID_RESET << 16;
		retval = FAILED;
		goto out;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1703 1704
	/* Find this command
	 */
1705
	if ((scpnt_idx = SCPNT_TO_LOOKUP_IDX(ioc, SCpnt)) < 0) {
1706
		/* Cmd not found in ScsiLookup.
Linus Torvalds's avatar
Linus Torvalds committed
1707 1708 1709
		 * Do OS callback.
		 */
		SCpnt->result = DID_RESET << 16;
1710
		dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "task abort: "
1711 1712 1713 1714
		   "Command not in the active list! (sc=%p)\n", ioc->name,
		   SCpnt));
		retval = 0;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1715 1716
	}

1717 1718
	if (ioc->timeouts < -1)
		ioc->timeouts++;
1719

1720 1721 1722
	if (mpt_fwfault_debug)
		mpt_halt_firmware(ioc);

Linus Torvalds's avatar
Linus Torvalds committed
1723 1724 1725 1726 1727 1728 1729
	/* 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
	 */
1730
	mf = MPT_INDEX_2_MFPTR(ioc, scpnt_idx);
Linus Torvalds's avatar
Linus Torvalds committed
1731
	ctx2abort = mf->u.frame.hwhdr.msgctxu.MsgContext;
1732 1733 1734 1735 1736
	retval = mptscsih_IssueTaskMgmt(hd,
			 MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
			 vdevice->vtarget->channel,
			 vdevice->vtarget->id, vdevice->lun,
			 ctx2abort, mptscsih_get_tm_timeout(ioc));
Linus Torvalds's avatar
Linus Torvalds committed
1737

1738
	if (SCPNT_TO_LOOKUP_IDX(ioc, SCpnt) == scpnt_idx &&
1739 1740 1741 1742
	    SCpnt->serial_number == sn) {
		dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "task abort: command still in active list! (sc=%p)\n",
		    ioc->name, SCpnt));
1743
		retval = FAILED;
1744 1745 1746 1747 1748 1749
	} else {
		dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "task abort: command cleared from active list! (sc=%p)\n",
		    ioc->name, SCpnt));
		retval = SUCCESS;
	}
1750

1751 1752
 out:
	printk(MYIOC_s_INFO_FMT "task abort: %s (sc=%p)\n",
1753
	    ioc->name, ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1754

1755
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_dev_reset - Perform a SCSI TARGET_RESET!  new_eh variant
 *	@SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
 *
 *	(linux scsi_host_template.eh_dev_reset_handler routine)
 *
 *	Returns SUCCESS or FAILED.
1766
 **/
1767
int
Linus Torvalds's avatar
Linus Torvalds committed
1768 1769 1770
mptscsih_dev_reset(struct scsi_cmnd * SCpnt)
{
	MPT_SCSI_HOST	*hd;
1771
	int		 retval;
1772 1773
	VirtDevice	 *vdevice;
	MPT_ADAPTER	*ioc;
Linus Torvalds's avatar
Linus Torvalds committed
1774 1775 1776

	/* If we can't locate our host adapter structure, return FAILED status.
	 */
1777
	if ((hd = shost_priv(SCpnt->device->host)) == NULL){
1778 1779
		printk(KERN_ERR MYNAM ": target reset: "
		   "Can't locate host! (sc=%p)\n", SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1780 1781 1782
		return FAILED;
	}

1783 1784 1785
	ioc = hd->ioc;
	printk(MYIOC_s_INFO_FMT "attempting target reset! (sc=%p)\n",
	       ioc->name, SCpnt);
1786
	scsi_print_command(SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1787

1788 1789
	vdevice = SCpnt->device->hostdata;
	if (!vdevice || !vdevice->vtarget) {
1790
		retval = SUCCESS;
1791 1792 1793
		goto out;
	}

1794 1795 1796 1797 1798 1799 1800
	/* Target reset to hidden raid component is not supported
	 */
	if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
		retval = FAILED;
		goto out;
	}

1801 1802 1803 1804 1805
	retval = mptscsih_IssueTaskMgmt(hd,
				MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
				vdevice->vtarget->channel,
				vdevice->vtarget->id, 0, 0,
				mptscsih_get_tm_timeout(ioc));
1806

1807 1808 1809
 out:
	printk (MYIOC_s_INFO_FMT "target reset: %s (sc=%p)\n",
	    ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
1810 1811 1812

	if (retval == 0)
		return SUCCESS;
1813 1814
	else
		return FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
1815 1816
}

1817

Linus Torvalds's avatar
Linus Torvalds committed
1818 1819 1820 1821 1822 1823 1824 1825
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	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.
1826
 **/
1827
int
Linus Torvalds's avatar
Linus Torvalds committed
1828 1829 1830
mptscsih_bus_reset(struct scsi_cmnd * SCpnt)
{
	MPT_SCSI_HOST	*hd;
1831
	int		 retval;
1832
	VirtDevice	 *vdevice;
1833
	MPT_ADAPTER	*ioc;
Linus Torvalds's avatar
Linus Torvalds committed
1834 1835 1836

	/* If we can't locate our host adapter structure, return FAILED status.
	 */
1837
	if ((hd = shost_priv(SCpnt->device->host)) == NULL){
1838 1839
		printk(KERN_ERR MYNAM ": bus reset: "
		   "Can't locate host! (sc=%p)\n", SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1840 1841 1842
		return FAILED;
	}

1843 1844 1845
	ioc = hd->ioc;
	printk(MYIOC_s_INFO_FMT "attempting bus reset! (sc=%p)\n",
	       ioc->name, SCpnt);
1846
	scsi_print_command(SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1847

1848 1849
	if (ioc->timeouts < -1)
		ioc->timeouts++;
Linus Torvalds's avatar
Linus Torvalds committed
1850

1851
	vdevice = SCpnt->device->hostdata;
1852 1853
	if (!vdevice || !vdevice->vtarget)
		return SUCCESS;
1854 1855 1856 1857
	retval = mptscsih_IssueTaskMgmt(hd,
					MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
					vdevice->vtarget->channel, 0, 0, 0,
					mptscsih_get_tm_timeout(ioc));
Linus Torvalds's avatar
Linus Torvalds committed
1858

1859 1860
	printk(MYIOC_s_INFO_FMT "bus reset: %s (sc=%p)\n",
	    ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
1861 1862 1863

	if (retval == 0)
		return SUCCESS;
1864 1865
	else
		return FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
1866 1867 1868 1869
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
1870
 *	mptscsih_host_reset - Perform a SCSI host adapter RESET (new_eh variant)
Linus Torvalds's avatar
Linus Torvalds committed
1871 1872 1873 1874 1875 1876
 *	@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.
 */
1877
int
Linus Torvalds's avatar
Linus Torvalds committed
1878 1879 1880
mptscsih_host_reset(struct scsi_cmnd *SCpnt)
{
	MPT_SCSI_HOST *  hd;
1881
	int              status = SUCCESS;
1882
	MPT_ADAPTER	*ioc;
1883
	int		retval;
Linus Torvalds's avatar
Linus Torvalds committed
1884 1885

	/*  If we can't locate the host to reset, then we failed. */
1886
	if ((hd = shost_priv(SCpnt->device->host)) == NULL){
1887 1888
		printk(KERN_ERR MYNAM ": host reset: "
		    "Can't locate host! (sc=%p)\n", SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1889 1890 1891
		return FAILED;
	}

1892 1893 1894
	/* make sure we have no outstanding commands at this stage */
	mptscsih_flush_running_cmds(hd);

1895 1896 1897
	ioc = hd->ioc;
	printk(MYIOC_s_INFO_FMT "attempting host reset! (sc=%p)\n",
	    ioc->name, SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1898 1899 1900 1901

	/*  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.
	 */
1902 1903 1904 1905 1906
    retval = mpt_HardResetHandler(ioc, CAN_SLEEP);
	if (retval < 0)
		status = FAILED;
	else
		status = SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
1907

1908 1909
	printk(MYIOC_s_INFO_FMT "host reset: %s (sc=%p)\n",
	    ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
Linus Torvalds's avatar
Linus Torvalds committed
1910

1911
	return status;
Linus Torvalds's avatar
Linus Torvalds committed
1912 1913 1914
}

static int
1915 1916
mptscsih_taskmgmt_reply(MPT_ADAPTER *ioc, u8 type,
	SCSITaskMgmtReply_t *pScsiTmReply)
Linus Torvalds's avatar
Linus Torvalds committed
1917
{
1918 1919 1920
	u16			 iocstatus;
	u32			 termination_count;
	int			 retval;
Linus Torvalds's avatar
Linus Torvalds committed
1921

1922 1923 1924 1925
	if (!(ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_RF_VALID)) {
		retval = FAILED;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1926

1927
	DBG_DUMP_TM_REPLY_FRAME(ioc, (u32 *)pScsiTmReply);
Linus Torvalds's avatar
Linus Torvalds committed
1928

1929 1930
	iocstatus = le16_to_cpu(pScsiTmReply->IOCStatus) & MPI_IOCSTATUS_MASK;
	termination_count = le32_to_cpu(pScsiTmReply->TerminationCount);
Linus Torvalds's avatar
Linus Torvalds committed
1931

1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
	dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
	    "TaskMgmt fw_channel = %d, fw_id = %d, task_type = 0x%02X,\n"
	    "\tiocstatus = 0x%04X, loginfo = 0x%08X, response_code = 0x%02X,\n"
	    "\tterm_cmnds = %d\n", ioc->name, pScsiTmReply->Bus,
	    pScsiTmReply->TargetID, type, le16_to_cpu(pScsiTmReply->IOCStatus),
	    le32_to_cpu(pScsiTmReply->IOCLogInfo), pScsiTmReply->ResponseCode,
	    termination_count));

	if (ioc->facts.MsgVersion >= MPI_VERSION_01_05 &&
	    pScsiTmReply->ResponseCode)
		mptscsih_taskmgmt_response_code(ioc,
		    pScsiTmReply->ResponseCode);
Linus Torvalds's avatar
Linus Torvalds committed
1944

1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
	if (iocstatus == MPI_IOCSTATUS_SUCCESS) {
		retval = 0;
		goto out;
	}

	retval = FAILED;
	if (type == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
		if (termination_count == 1)
			retval = 0;
		goto out;
	}

	if (iocstatus == MPI_IOCSTATUS_SCSI_TASK_TERMINATED ||
	   iocstatus == MPI_IOCSTATUS_SCSI_IOC_TERMINATED)
		retval = 0;

 out:
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1963 1964
}

1965
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1966
void
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
mptscsih_taskmgmt_response_code(MPT_ADAPTER *ioc, u8 response_code)
{
	char *desc;

	switch (response_code) {
	case MPI_SCSITASKMGMT_RSP_TM_COMPLETE:
		desc = "The task completed.";
		break;
	case MPI_SCSITASKMGMT_RSP_INVALID_FRAME:
		desc = "The IOC received an invalid frame status.";
		break;
	case MPI_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
		desc = "The task type is not supported.";
		break;
	case MPI_SCSITASKMGMT_RSP_TM_FAILED:
		desc = "The requested task failed.";
		break;
	case MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED:
		desc = "The task completed successfully.";
		break;
	case MPI_SCSITASKMGMT_RSP_TM_INVALID_LUN:
		desc = "The LUN request is invalid.";
		break;
	case MPI_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
		desc = "The task is in the IOC queue and has not been sent to target.";
		break;
	default:
		desc = "unknown";
		break;
	}
	printk(MYIOC_s_INFO_FMT "Response Code(0x%08x): F/W: %s\n",
		ioc->name, response_code, desc);
}
2000
EXPORT_SYMBOL(mptscsih_taskmgmt_response_code);
2001

Linus Torvalds's avatar
Linus Torvalds committed
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	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
 *
 *	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.
 *
 *	Returns 1 indicating alloc'd request frame ptr should be freed.
2015
 **/
2016
int
2017 2018
mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf,
	MPT_FRAME_HDR *mr)
Linus Torvalds's avatar
Linus Torvalds committed
2019
{
2020 2021
	dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		"TaskMgmt completed (mf=%p, mr=%p)\n", ioc->name, mf, mr));
2022

2023
	ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
2024

2025
	if (!mr)
2026
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2027

2028 2029 2030
	ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_RF_VALID;
	memcpy(ioc->taskmgmt_cmds.reply, mr,
	    min(MPT_DEFAULT_FRAME_SIZE, 4 * mr->u.reply.MsgLength));
2031
 out:
2032 2033 2034 2035 2036 2037 2038
	if (ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_PENDING) {
		mpt_clear_taskmgmt_in_progress_flag(ioc);
		ioc->taskmgmt_cmds.status &= ~MPT_MGMT_STATUS_PENDING;
		complete(&ioc->taskmgmt_cmds.done);
		return 1;
	}
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2039 2040 2041 2042 2043 2044
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	This is anyones guess quite frankly.
 */
2045
int
Linus Torvalds's avatar
Linus Torvalds committed
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
mptscsih_bios_param(struct scsi_device * sdev, struct block_device *bdev,
		sector_t capacity, int geom[])
{
	int		heads;
	int		sectors;
	sector_t	cylinders;
	ulong 		dummy;

	heads = 64;
	sectors = 32;

	dummy = heads * sectors;
	cylinders = capacity;
	sector_div(cylinders,dummy);

	/*
	 * Handle extended translation size for logical drives
	 * > 1Gb
	 */
	if ((ulong)capacity >= 0x200000) {
		heads = 255;
		sectors = 63;
		dummy = heads * sectors;
		cylinders = capacity;
		sector_div(cylinders,dummy);
	}

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

	return 0;
}

2081 2082 2083 2084
/* Search IOC page 3 to determine if this is hidden physical disk
 *
 */
int
2085
mptscsih_is_phys_disk(MPT_ADAPTER *ioc, u8 channel, u8 id)
2086
{
2087
	struct inactive_raid_component_info *component_info;
2088 2089
	int i, j;
	RaidPhysDiskPage1_t *phys_disk;
2090
	int rc = 0;
2091
	int num_paths;
2092

2093 2094
	if (!ioc->raid_data.pIocPg3)
		goto out;
2095
	for (i = 0; i < ioc->raid_data.pIocPg3->NumPhysDisks; i++) {
2096 2097 2098 2099 2100
		if ((id == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskID) &&
		    (channel == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskBus)) {
			rc = 1;
			goto out;
		}
2101 2102
	}

2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
	if (ioc->bus_type != SAS)
		goto out;

	/*
	 * Check if dual path
	 */
	for (i = 0; i < ioc->raid_data.pIocPg3->NumPhysDisks; i++) {
		num_paths = mpt_raid_phys_disk_get_num_paths(ioc,
		    ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskNum);
		if (num_paths < 2)
			continue;
		phys_disk = kzalloc(offsetof(RaidPhysDiskPage1_t, Path) +
		   (num_paths * sizeof(RAID_PHYS_DISK1_PATH)), GFP_KERNEL);
		if (!phys_disk)
			continue;
		if ((mpt_raid_phys_disk_pg1(ioc,
		    ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskNum,
		    phys_disk))) {
			kfree(phys_disk);
			continue;
		}
		for (j = 0; j < num_paths; j++) {
			if ((phys_disk->Path[j].Flags &
			    MPI_RAID_PHYSDISK1_FLAG_INVALID))
				continue;
			if ((phys_disk->Path[j].Flags &
			    MPI_RAID_PHYSDISK1_FLAG_BROKEN))
				continue;
			if ((id == phys_disk->Path[j].PhysDiskID) &&
			    (channel == phys_disk->Path[j].PhysDiskBus)) {
				rc = 1;
				kfree(phys_disk);
				goto out;
			}
		}
		kfree(phys_disk);
	}


2142 2143 2144 2145 2146 2147
	/*
	 * Check inactive list for matching phys disks
	 */
	if (list_empty(&ioc->raid_data.inactive_list))
		goto out;

2148
	mutex_lock(&ioc->raid_data.inactive_list_mutex);
2149 2150 2151 2152 2153 2154
	list_for_each_entry(component_info, &ioc->raid_data.inactive_list,
	    list) {
		if ((component_info->d.PhysDiskID == id) &&
		    (component_info->d.PhysDiskBus == channel))
			rc = 1;
	}
2155
	mutex_unlock(&ioc->raid_data.inactive_list_mutex);
2156

2157 2158
 out:
	return rc;
2159
}
2160
EXPORT_SYMBOL(mptscsih_is_phys_disk);
2161

2162 2163
u8
mptscsih_raid_id_to_num(MPT_ADAPTER *ioc, u8 channel, u8 id)
Linus Torvalds's avatar
Linus Torvalds committed
2164
{
2165
	struct inactive_raid_component_info *component_info;
2166 2167
	int i, j;
	RaidPhysDiskPage1_t *phys_disk;
2168
	int rc = -ENXIO;
2169
	int num_paths;
2170

2171 2172 2173 2174 2175 2176 2177
	if (!ioc->raid_data.pIocPg3)
		goto out;
	for (i = 0; i < ioc->raid_data.pIocPg3->NumPhysDisks; i++) {
		if ((id == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskID) &&
		    (channel == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskBus)) {
			rc = ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskNum;
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2178 2179 2180
		}
	}

2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
	if (ioc->bus_type != SAS)
		goto out;

	/*
	 * Check if dual path
	 */
	for (i = 0; i < ioc->raid_data.pIocPg3->NumPhysDisks; i++) {
		num_paths = mpt_raid_phys_disk_get_num_paths(ioc,
		    ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskNum);
		if (num_paths < 2)
			continue;
		phys_disk = kzalloc(offsetof(RaidPhysDiskPage1_t, Path) +
		   (num_paths * sizeof(RAID_PHYS_DISK1_PATH)), GFP_KERNEL);
		if (!phys_disk)
			continue;
		if ((mpt_raid_phys_disk_pg1(ioc,
		    ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskNum,
		    phys_disk))) {
			kfree(phys_disk);
			continue;
		}
		for (j = 0; j < num_paths; j++) {
			if ((phys_disk->Path[j].Flags &
			    MPI_RAID_PHYSDISK1_FLAG_INVALID))
				continue;
			if ((phys_disk->Path[j].Flags &
			    MPI_RAID_PHYSDISK1_FLAG_BROKEN))
				continue;
			if ((id == phys_disk->Path[j].PhysDiskID) &&
			    (channel == phys_disk->Path[j].PhysDiskBus)) {
				rc = phys_disk->PhysDiskNum;
				kfree(phys_disk);
				goto out;
			}
		}
		kfree(phys_disk);
	}

2219 2220 2221 2222 2223 2224
	/*
	 * Check inactive list for matching phys disks
	 */
	if (list_empty(&ioc->raid_data.inactive_list))
		goto out;

2225
	mutex_lock(&ioc->raid_data.inactive_list_mutex);
2226 2227 2228 2229 2230 2231
	list_for_each_entry(component_info, &ioc->raid_data.inactive_list,
	    list) {
		if ((component_info->d.PhysDiskID == id) &&
		    (component_info->d.PhysDiskBus == channel))
			rc = component_info->d.PhysDiskNum;
	}
2232
	mutex_unlock(&ioc->raid_data.inactive_list_mutex);
2233

2234 2235
 out:
	return rc;
2236
}
2237
EXPORT_SYMBOL(mptscsih_raid_id_to_num);
Linus Torvalds's avatar
Linus Torvalds committed
2238

2239 2240 2241 2242 2243 2244 2245 2246
/*
 *	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(struct scsi_device *sdev)
{
	struct Scsi_Host	*host = sdev->host;
2247
	MPT_SCSI_HOST		*hd = shost_priv(host);
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
	VirtTarget		*vtarget;
	VirtDevice		*vdevice;
	struct scsi_target 	*starget;

	starget = scsi_target(sdev);
	vtarget = starget->hostdata;
	vdevice = sdev->hostdata;

	mptscsih_search_running_cmds(hd, vdevice);
	vtarget->num_luns--;
	mptscsih_synchronize_cache(hd, vdevice);
	kfree(vdevice);
	sdev->hostdata = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
2261 2262
}

2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *	mptscsih_change_queue_depth - This function will set a devices queue depth
 *	@sdev: per scsi_device pointer
 *	@qdepth: requested queue depth
 *
 *	Adding support for new 'change_queue_depth' api.
*/
int
mptscsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
Linus Torvalds's avatar
Linus Torvalds committed
2273
{
2274
	MPT_SCSI_HOST		*hd = shost_priv(sdev->host);
2275 2276 2277 2278
	VirtTarget 		*vtarget;
	struct scsi_target 	*starget;
	int			max_depth;
	int			tagged;
2279
	MPT_ADAPTER		*ioc = hd->ioc;
Linus Torvalds's avatar
Linus Torvalds committed
2280

2281 2282
	starget = scsi_target(sdev);
	vtarget = starget->hostdata;
2283

2284
	if (ioc->bus_type == SPI) {
2285
		if (!(vtarget->tflags & MPT_TARGET_FLAGS_Q_YES))
Linus Torvalds's avatar
Linus Torvalds committed
2286
			max_depth = 1;
2287 2288 2289 2290 2291
		else if (sdev->type == TYPE_DISK &&
			 vtarget->minSyncFactor <= MPT_ULTRA160)
			max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
		else
			max_depth = MPT_SCSI_CMD_PER_DEV_LOW;
Linus Torvalds's avatar
Linus Torvalds committed
2292
	} else
2293 2294 2295 2296
		 max_depth = ioc->sh->can_queue;

	if (!sdev->tagged_supported)
		max_depth = 1;
Linus Torvalds's avatar
Linus Torvalds committed
2297 2298 2299 2300 2301 2302 2303 2304

	if (qdepth > max_depth)
		qdepth = max_depth;
	if (qdepth == 1)
		tagged = 0;
	else
		tagged = MSG_SIMPLE_TAG;

2305 2306
	scsi_adjust_queue_depth(sdev, tagged, qdepth);
	return sdev->queue_depth;
Linus Torvalds's avatar
Linus Torvalds committed
2307 2308 2309 2310 2311 2312 2313 2314
}

/*
 *	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.
 *	Return non-zero if fails.
 */
2315
int
2316
mptscsih_slave_configure(struct scsi_device *sdev)
Linus Torvalds's avatar
Linus Torvalds committed
2317
{
2318 2319 2320 2321
	struct Scsi_Host	*sh = sdev->host;
	VirtTarget		*vtarget;
	VirtDevice		*vdevice;
	struct scsi_target 	*starget;
2322
	MPT_SCSI_HOST		*hd = shost_priv(sh);
2323
	MPT_ADAPTER		*ioc = hd->ioc;
Linus Torvalds's avatar
Linus Torvalds committed
2324

2325 2326 2327
	starget = scsi_target(sdev);
	vtarget = starget->hostdata;
	vdevice = sdev->hostdata;
Linus Torvalds's avatar
Linus Torvalds committed
2328

2329
	dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2330
		"device @ %p, channel=%d, id=%d, lun=%d\n",
2331 2332 2333
		ioc->name, sdev, sdev->channel, sdev->id, sdev->lun));
	if (ioc->bus_type == SPI)
		dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2334
		    "sdtr %d wdtr %d ppr %d inq length=%d\n",
2335
		    ioc->name, sdev->sdtr, sdev->wdtr,
2336 2337
		    sdev->ppr, sdev->inquiry_len));

2338
	vdevice->configured_lun = 1;
Linus Torvalds's avatar
Linus Torvalds committed
2339

2340
	dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
Linus Torvalds's avatar
Linus Torvalds committed
2341
		"Queue depth=%d, tflags=%x\n",
2342
		ioc->name, sdev->queue_depth, vtarget->tflags));
Linus Torvalds's avatar
Linus Torvalds committed
2343

2344 2345
	if (ioc->bus_type == SPI)
		dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2346
		    "negoFlags=%x, maxOffset=%x, SyncFactor=%x\n",
2347
		    ioc->name, vtarget->negoFlags, vtarget->maxOffset,
2348
		    vtarget->minSyncFactor));
Linus Torvalds's avatar
Linus Torvalds committed
2349

2350
	mptscsih_change_queue_depth(sdev, MPT_SCSI_CMD_PER_DEV_HIGH);
2351
	dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
Linus Torvalds's avatar
Linus Torvalds committed
2352
		"tagged %d, simple %d, ordered %d\n",
2353
		ioc->name,sdev->tagged_supported, sdev->simple_tags,
2354
		sdev->ordered_tags));
Linus Torvalds's avatar
Linus Torvalds committed
2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369

	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  Private routines...
 */

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* Utility function to copy sense data from the scsi_cmnd buffer
 * to the FC and SCSI target structures.
 *
 */
static void
2370
mptscsih_copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply)
Linus Torvalds's avatar
Linus Torvalds committed
2371
{
2372
	VirtDevice	*vdevice;
Linus Torvalds's avatar
Linus Torvalds committed
2373 2374
	SCSIIORequest_t	*pReq;
	u32		 sense_count = le32_to_cpu(pScsiReply->SenseCount);
2375
	MPT_ADAPTER 	*ioc = hd->ioc;
Linus Torvalds's avatar
Linus Torvalds committed
2376 2377 2378 2379

	/* Get target structure
	 */
	pReq = (SCSIIORequest_t *) mf;
2380
	vdevice = sc->device->hostdata;
Linus Torvalds's avatar
Linus Torvalds committed
2381 2382 2383 2384 2385 2386 2387

	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);
2388
		sense_data = ((u8 *)ioc->sense_buf_pool + (req_index * MPT_SENSE_BUFFER_ALLOC));
Linus Torvalds's avatar
Linus Torvalds committed
2389 2390 2391 2392
		memcpy(sc->sense_buffer, sense_data, SNS_LEN(sc));

		/* Log SMART data (asc = 0x5D, non-IM case only) if required.
		 */
2393
		if ((ioc->events) && (ioc->eventTypes & (1 << MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE))) {
2394
			if ((sense_data[12] == 0x5D) && (vdevice->vtarget->raidVolume == 0)) {
Linus Torvalds's avatar
Linus Torvalds committed
2395 2396
				int idx;

2397
				idx = ioc->eventContext % MPTCTL_EVENT_LOG_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
2398 2399 2400
				ioc->events[idx].event = MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE;
				ioc->events[idx].eventContext = ioc->eventContext;

2401 2402 2403
				ioc->events[idx].data[0] = (pReq->LUN[1] << 24) |
					(MPI_EVENT_SCSI_DEV_STAT_RC_SMART_DATA << 16) |
					(sc->device->channel << 8) | sc->device->id;
Linus Torvalds's avatar
Linus Torvalds committed
2404

2405
				ioc->events[idx].data[1] = (sense_data[13] << 8) | sense_data[12];
Linus Torvalds's avatar
Linus Torvalds committed
2406 2407

				ioc->eventContext++;
2408
				if (ioc->pcidev->vendor ==
2409
				    PCI_VENDOR_ID_IBM) {
2410
					mptscsih_issue_sep_command(ioc,
2411 2412
					    vdevice->vtarget, MPI_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
					vdevice->vtarget->tflags |=
2413 2414
					    MPT_TARGET_FLAGS_LED_ON;
				}
Linus Torvalds's avatar
Linus Torvalds committed
2415 2416 2417
			}
		}
	} else {
2418 2419
		dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Hmmm... SenseData len=0! (?)\n",
				ioc->name));
Linus Torvalds's avatar
Linus Torvalds committed
2420 2421 2422
	}
}

2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
/**
 * mptscsih_get_scsi_lookup - retrieves scmd entry
 * @ioc: Pointer to MPT_ADAPTER structure
 * @i: index into the array
 *
 * Returns the scsi_cmd pointer
 */
struct scsi_cmnd *
mptscsih_get_scsi_lookup(MPT_ADAPTER *ioc, int i)
{
	unsigned long	flags;
	struct scsi_cmnd *scmd;

	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	scmd = ioc->ScsiLookup[i];
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);

	return scmd;
}
EXPORT_SYMBOL(mptscsih_get_scsi_lookup);
2443 2444

/**
2445
 * mptscsih_getclear_scsi_lookup -  retrieves and clears scmd entry from ScsiLookup[] array list
2446 2447 2448
 * @ioc: Pointer to MPT_ADAPTER structure
 * @i: index into the array
 *
2449
 * Returns the scsi_cmd pointer
2450
 *
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479
 **/
static struct scsi_cmnd *
mptscsih_getclear_scsi_lookup(MPT_ADAPTER *ioc, int i)
{
	unsigned long	flags;
	struct scsi_cmnd *scmd;

	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	scmd = ioc->ScsiLookup[i];
	ioc->ScsiLookup[i] = NULL;
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);

	return scmd;
}

/**
 * mptscsih_set_scsi_lookup
 *
 * writes a scmd entry into the ScsiLookup[] array list
 *
 * @ioc: Pointer to MPT_ADAPTER structure
 * @i: index into the array
 * @scmd: scsi_cmnd pointer
 *
 **/
static void
mptscsih_set_scsi_lookup(MPT_ADAPTER *ioc, int i, struct scsi_cmnd *scmd)
{
	unsigned long	flags;
Linus Torvalds's avatar
Linus Torvalds committed
2480

2481 2482 2483 2484 2485 2486
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	ioc->ScsiLookup[i] = scmd;
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
}

/**
2487
 * SCPNT_TO_LOOKUP_IDX - searches for a given scmd in the ScsiLookup[] array list
2488
 * @ioc: Pointer to MPT_ADAPTER structure
2489 2490
 * @sc: scsi_cmnd pointer
 */
2491 2492 2493 2494 2495
static int
SCPNT_TO_LOOKUP_IDX(MPT_ADAPTER *ioc, struct scsi_cmnd *sc)
{
	unsigned long	flags;
	int i, index=-1;
Linus Torvalds's avatar
Linus Torvalds committed
2496

2497 2498 2499 2500 2501
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	for (i = 0; i < ioc->req_depth; i++) {
		if (ioc->ScsiLookup[i] == sc) {
			index = i;
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2502 2503 2504
		}
	}

2505 2506 2507
 out:
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
	return index;
Linus Torvalds's avatar
Linus Torvalds committed
2508 2509 2510
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2511
int
Linus Torvalds's avatar
Linus Torvalds committed
2512 2513 2514 2515
mptscsih_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
{
	MPT_SCSI_HOST	*hd;

2516
	if (ioc->sh == NULL || shost_priv(ioc->sh) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
2517 2518
		return 0;

2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
	hd = shost_priv(ioc->sh);
	switch (reset_phase) {
	case MPT_IOC_SETUP_RESET:
		dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "%s: MPT_IOC_SETUP_RESET\n", ioc->name, __func__));
		break;
	case MPT_IOC_PRE_RESET:
		dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "%s: MPT_IOC_PRE_RESET\n", ioc->name, __func__));
		mptscsih_flush_running_cmds(hd);
		break;
	case MPT_IOC_POST_RESET:
		dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "%s: MPT_IOC_POST_RESET\n", ioc->name, __func__));
		if (ioc->internal_cmds.status & MPT_MGMT_STATUS_PENDING) {
			ioc->internal_cmds.status |=
				MPT_MGMT_STATUS_DID_IOCRESET;
			complete(&ioc->internal_cmds.done);
Linus Torvalds's avatar
Linus Torvalds committed
2537
		}
2538 2539 2540
		break;
	default:
		break;
Linus Torvalds's avatar
Linus Torvalds committed
2541 2542 2543 2544 2545
	}
	return 1;		/* currently means nothing really */
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2546
int
Linus Torvalds's avatar
Linus Torvalds committed
2547 2548 2549 2550
mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
{
	u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;

2551 2552 2553
	devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		"MPT event (=%02Xh) routed to SCSI host driver!\n",
		ioc->name, event));
Linus Torvalds's avatar
Linus Torvalds committed
2554

2555 2556 2557 2558
	if ((event == MPI_EVENT_IOC_BUS_RESET ||
	    event == MPI_EVENT_EXT_BUS_RESET) &&
	    (ioc->bus_type == SPI) && (ioc->soft_resets < -1))
			ioc->soft_resets++;
Linus Torvalds's avatar
Linus Torvalds committed
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587

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

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  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.
 *	Used ONLY for DV and other internal commands.
 */
2588
int
2589 2590
mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
				MPT_FRAME_HDR *reply)
Linus Torvalds's avatar
Linus Torvalds committed
2591 2592
{
	SCSIIORequest_t *pReq;
2593 2594
	SCSIIOReply_t	*pReply;
	u8		 cmd;
Linus Torvalds's avatar
Linus Torvalds committed
2595
	u16		 req_idx;
2596 2597
	u8	*sense_data;
	int		 sz;
Linus Torvalds's avatar
Linus Torvalds committed
2598

2599 2600 2601 2602
	ioc->internal_cmds.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
	ioc->internal_cmds.completion_code = MPT_SCANDV_GOOD;
	if (!reply)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2603

2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
	pReply = (SCSIIOReply_t *) reply;
	pReq = (SCSIIORequest_t *) req;
	ioc->internal_cmds.completion_code =
	    mptscsih_get_completion_code(ioc, req, reply);
	ioc->internal_cmds.status |= MPT_MGMT_STATUS_RF_VALID;
	memcpy(ioc->internal_cmds.reply, reply,
	    min(MPT_DEFAULT_FRAME_SIZE, 4 * reply->u.reply.MsgLength));
	cmd = reply->u.hdr.Function;
	if (((cmd == MPI_FUNCTION_SCSI_IO_REQUEST) ||
	    (cmd == MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) &&
	    (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID)) {
		req_idx = le16_to_cpu(req->u.frame.hwhdr.msgctxu.fld.req_idx);
		sense_data = ((u8 *)ioc->sense_buf_pool +
		    (req_idx * MPT_SENSE_BUFFER_ALLOC));
		sz = min_t(int, pReq->SenseBufferLength,
		    MPT_SENSE_BUFFER_ALLOC);
		memcpy(ioc->internal_cmds.sense, sense_data, sz);
Linus Torvalds's avatar
Linus Torvalds committed
2621
	}
2622 2623 2624 2625 2626
 out:
	if (!(ioc->internal_cmds.status & MPT_MGMT_STATUS_PENDING))
		return 0;
	ioc->internal_cmds.status &= ~MPT_MGMT_STATUS_PENDING;
	complete(&ioc->internal_cmds.done);
Linus Torvalds's avatar
Linus Torvalds committed
2627 2628 2629 2630
	return 1;
}


2631 2632 2633
/**
 *	mptscsih_get_completion_code -
 *	@ioc: Pointer to MPT_ADAPTER structure
2634 2635
 *	@req: Pointer to original MPT request frame
 *	@reply: Pointer to MPT reply frame (NULL if TurboReply)
2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 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 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
 *
 **/
static int
mptscsih_get_completion_code(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
				MPT_FRAME_HDR *reply)
{
	SCSIIOReply_t	*pReply;
	MpiRaidActionReply_t *pr;
	u8		 scsi_status;
	u16		 status;
	int		 completion_code;

	pReply = (SCSIIOReply_t *)reply;
	status = le16_to_cpu(pReply->IOCStatus) & MPI_IOCSTATUS_MASK;
	scsi_status = pReply->SCSIStatus;

	devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
	    "IOCStatus=%04xh, SCSIState=%02xh, SCSIStatus=%02xh,"
	    "IOCLogInfo=%08xh\n", ioc->name, status, pReply->SCSIState,
	    scsi_status, le32_to_cpu(pReply->IOCLogInfo)));

	switch (status) {

	case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:	/* 0x0043 */
		completion_code = 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 */
		completion_code = MPT_SCANDV_DID_RESET;
		break;

	case MPI_IOCSTATUS_BUSY:
	case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES:
		completion_code = MPT_SCANDV_BUSY;
		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) {
			completion_code = MPT_SCANDV_GOOD;
		} else if (pReply->Function == MPI_FUNCTION_RAID_ACTION) {
			pr = (MpiRaidActionReply_t *)reply;
			if (le16_to_cpu(pr->ActionStatus) ==
				MPI_RAID_ACTION_ASTATUS_SUCCESS)
				completion_code = MPT_SCANDV_GOOD;
			else
				completion_code = MPT_SCANDV_SOME_ERROR;
		} else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID)
			completion_code = MPT_SCANDV_SENSE;
		else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_FAILED) {
			if (req->u.scsireq.CDB[0] == INQUIRY)
				completion_code = MPT_SCANDV_ISSUE_SENSE;
			else
				completion_code = MPT_SCANDV_DID_RESET;
		} else if (pReply->SCSIState & MPI_SCSI_STATE_NO_SCSI_STATUS)
			completion_code = MPT_SCANDV_DID_RESET;
		else if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
			completion_code = MPT_SCANDV_DID_RESET;
		else if (scsi_status == MPI_SCSI_STATUS_BUSY)
			completion_code = MPT_SCANDV_BUSY;
		else
			completion_code = MPT_SCANDV_GOOD;
		break;

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

	}	/* switch(status) */

	devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
	    "  completionCode set to %08xh\n", ioc->name, completion_code));
	return completion_code;
}
Linus Torvalds's avatar
Linus Torvalds committed
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	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;
	int		 my_idx, ii, dir;
2747
	int		 timeout;
Linus Torvalds's avatar
Linus Torvalds committed
2748 2749
	char		 cmdLen;
	char		 CDB[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2750 2751 2752 2753 2754
	u8		 cmd = io->cmd;
	MPT_ADAPTER *ioc = hd->ioc;
	int		 ret = 0;
	unsigned long	 timeleft;
	unsigned long	 flags;
Linus Torvalds's avatar
Linus Torvalds committed
2755

2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
	/* don't send internal command during diag reset */
	spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
	if (ioc->ioc_reset_in_progress) {
		spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
		dfailprintk(ioc, printk(MYIOC_s_DEBUG_FMT
			"%s: busy with host reset\n", ioc->name, __func__));
		return MPT_SCANDV_BUSY;
	}
	spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);

2766
	mutex_lock(&ioc->internal_cmds.mutex);
Linus Torvalds's avatar
Linus Torvalds committed
2767 2768 2769 2770 2771 2772 2773 2774 2775

	/* Set command specific information
	 */
	switch (cmd) {
	case INQUIRY:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
		CDB[4] = io->size;
2776
		timeout = 10;
Linus Torvalds's avatar
Linus Torvalds committed
2777 2778 2779 2780 2781
		break;

	case TEST_UNIT_READY:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
2782
		timeout = 10;
Linus Torvalds's avatar
Linus Torvalds committed
2783 2784 2785 2786 2787 2788 2789
		break;

	case START_STOP:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
		CDB[4] = 1;	/*Spin up the disk */
2790
		timeout = 15;
Linus Torvalds's avatar
Linus Torvalds committed
2791 2792 2793 2794 2795 2796 2797
		break;

	case REQUEST_SENSE:
		cmdLen = 6;
		CDB[0] = cmd;
		CDB[4] = io->size;
		dir = MPI_SCSIIO_CONTROL_READ;
2798
		timeout = 10;
Linus Torvalds's avatar
Linus Torvalds committed
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816
		break;

	case READ_BUFFER:
		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;
2817
		timeout = 10;
Linus Torvalds's avatar
Linus Torvalds committed
2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
		break;

	case WRITE_BUFFER:
		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;
2832
		timeout = 10;
Linus Torvalds's avatar
Linus Torvalds committed
2833 2834 2835 2836 2837 2838
		break;

	case RESERVE:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
2839
		timeout = 10;
Linus Torvalds's avatar
Linus Torvalds committed
2840 2841 2842 2843 2844 2845
		break;

	case RELEASE:
		cmdLen = 6;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
2846
		timeout = 10;
Linus Torvalds's avatar
Linus Torvalds committed
2847 2848 2849 2850 2851 2852 2853
		break;

	case SYNCHRONIZE_CACHE:
		cmdLen = 10;
		dir = MPI_SCSIIO_CONTROL_READ;
		CDB[0] = cmd;
//		CDB[1] = 0x02;	/* set immediate bit */
2854
		timeout = 10;
Linus Torvalds's avatar
Linus Torvalds committed
2855 2856 2857 2858
		break;

	default:
		/* Error Case */
2859 2860
		ret = -EFAULT;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2861 2862 2863
	}

	/* Get and Populate a free Frame
2864
	 * MsgContext set in mpt_get_msg_frame call
Linus Torvalds's avatar
Linus Torvalds committed
2865
	 */
2866
	if ((mf = mpt_get_msg_frame(ioc->InternalCtx, ioc)) == NULL) {
2867 2868 2869 2870
		dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s: No msg frames!\n",
		    ioc->name, __func__));
		ret = MPT_SCANDV_BUSY;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
	}

	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;
2886
		pScsiReq->Bus = io->channel;
Linus Torvalds's avatar
Linus Torvalds committed
2887 2888 2889 2890 2891 2892 2893 2894 2895
		pScsiReq->ChainOffset = 0;
		pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
	}

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

	pScsiReq->Reserved = 0;

2896
	pScsiReq->MsgFlags = mpt_msg_flags(ioc);
Linus Torvalds's avatar
Linus Torvalds committed
2897 2898
	/* MsgContext set in mpt_get_msg_fram call  */

2899
	int_to_scsilun(io->lun, (struct scsi_lun *)pScsiReq->LUN);
Linus Torvalds's avatar
Linus Torvalds committed
2900 2901 2902 2903 2904 2905 2906 2907

	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);

	if (cmd == REQUEST_SENSE) {
		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
2908 2909
		devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "%s: Untagged! 0x%02x\n", ioc->name, __func__, cmd));
Linus Torvalds's avatar
Linus Torvalds committed
2910 2911
	}

2912
	for (ii = 0; ii < 16; ii++)
Linus Torvalds's avatar
Linus Torvalds committed
2913 2914 2915
		pScsiReq->CDB[ii] = CDB[ii];

	pScsiReq->DataLength = cpu_to_le32(io->size);
2916
	pScsiReq->SenseBufferLowAddr = cpu_to_le32(ioc->sense_buf_low_dma
Linus Torvalds's avatar
Linus Torvalds committed
2917 2918
					   + (my_idx * MPT_SENSE_BUFFER_ALLOC));

2919 2920 2921
	devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
	    "%s: Sending Command 0x%02x for fw_channel=%d fw_id=%d lun=%d\n",
	    ioc->name, __func__, cmd, io->channel, io->id, io->lun));
Linus Torvalds's avatar
Linus Torvalds committed
2922

2923
	if (dir == MPI_SCSIIO_CONTROL_READ)
2924
		ioc->add_sge((char *) &pScsiReq->SGL,
2925 2926
		    MPT_SGE_FLAGS_SSIMPLE_READ | io->size, io->data_dma);
	else
2927
		ioc->add_sge((char *) &pScsiReq->SGL,
2928
		    MPT_SGE_FLAGS_SSIMPLE_WRITE | io->size, io->data_dma);
Linus Torvalds's avatar
Linus Torvalds committed
2929

2930
	INITIALIZE_MGMT_STATUS(ioc->internal_cmds.status)
2931
	mpt_put_msg_frame(ioc->InternalCtx, ioc, mf);
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
	timeleft = wait_for_completion_timeout(&ioc->internal_cmds.done,
	    timeout*HZ);
	if (!(ioc->internal_cmds.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
		ret = MPT_SCANDV_DID_RESET;
		dfailprintk(ioc, printk(MYIOC_s_DEBUG_FMT
		    "%s: TIMED OUT for cmd=0x%02x\n", ioc->name, __func__,
		    cmd));
		if (ioc->internal_cmds.status & MPT_MGMT_STATUS_DID_IOCRESET) {
			mpt_free_msg_frame(ioc, mf);
			goto out;
		}
		if (!timeleft) {
			printk(MYIOC_s_WARN_FMT "Issuing Reset from %s!!\n",
			    ioc->name, __func__);
			mpt_HardResetHandler(ioc, CAN_SLEEP);
			mpt_free_msg_frame(ioc, mf);
		}
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2950 2951
	}

2952 2953 2954 2955 2956 2957 2958 2959
	ret = ioc->internal_cmds.completion_code;
	devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: success, rc=0x%02x\n",
			ioc->name, __func__, ret));

 out:
	CLEAR_MGMT_STATUS(ioc->internal_cmds.status)
	mutex_unlock(&ioc->internal_cmds.mutex);
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
2960 2961
}

2962 2963 2964 2965
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	mptscsih_synchronize_cache - Send SYNCHRONIZE_CACHE to all disks.
 *	@hd: Pointer to a SCSI HOST structure
2966
 *	@vdevice: virtual target device
2967 2968 2969 2970 2971 2972 2973 2974 2975
 *
 *	Uses the ISR, but with special processing.
 *	MUST be single-threaded.
 *
 */
static void
mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, VirtDevice *vdevice)
{
	INTERNAL_CMD		 iocmd;
Linus Torvalds's avatar
Linus Torvalds committed
2976

2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
	/* Ignore hidden raid components, this is handled when the command
	 * is sent to the volume
	 */
	if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
		return;

	if (vdevice->vtarget->type != TYPE_DISK || vdevice->vtarget->deleted ||
	    !vdevice->configured_lun)
		return;

2987 2988 2989 2990 2991 2992 2993 2994 2995 2996
	/* Following parameters will not change
	 * in this routine.
	 */
	iocmd.cmd = SYNCHRONIZE_CACHE;
	iocmd.flags = 0;
	iocmd.physDiskNum = -1;
	iocmd.data = NULL;
	iocmd.data_dma = -1;
	iocmd.size = 0;
	iocmd.rsvd = iocmd.rsvd2 = 0;
2997 2998 2999
	iocmd.channel = vdevice->vtarget->channel;
	iocmd.id = vdevice->vtarget->id;
	iocmd.lun = vdevice->lun;
Linus Torvalds's avatar
Linus Torvalds committed
3000

3001
	mptscsih_do_cmd(hd, &iocmd);
Linus Torvalds's avatar
Linus Torvalds committed
3002 3003
}

3004
static ssize_t
3005 3006
mptscsih_version_fw_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
3007
{
3008
	struct Scsi_Host *host = class_to_shost(dev);
3009
	MPT_SCSI_HOST	*hd = shost_priv(host);
3010 3011 3012 3013 3014 3015 3016 3017
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
	    ioc->facts.FWVersion.Word & 0x000000FF);
}
3018
static DEVICE_ATTR(version_fw, S_IRUGO, mptscsih_version_fw_show, NULL);
3019 3020

static ssize_t
3021 3022
mptscsih_version_bios_show(struct device *dev, struct device_attribute *attr,
			   char *buf)
3023
{
3024
	struct Scsi_Host *host = class_to_shost(dev);
3025
	MPT_SCSI_HOST	*hd = shost_priv(host);
3026 3027 3028 3029 3030 3031 3032 3033
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
	    (ioc->biosVersion & 0xFF000000) >> 24,
	    (ioc->biosVersion & 0x00FF0000) >> 16,
	    (ioc->biosVersion & 0x0000FF00) >> 8,
	    ioc->biosVersion & 0x000000FF);
}
3034
static DEVICE_ATTR(version_bios, S_IRUGO, mptscsih_version_bios_show, NULL);
3035 3036

static ssize_t
3037 3038
mptscsih_version_mpi_show(struct device *dev, struct device_attribute *attr,
			  char *buf)
3039
{
3040
	struct Scsi_Host *host = class_to_shost(dev);
3041
	MPT_SCSI_HOST	*hd = shost_priv(host);
3042 3043 3044 3045
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%03x\n", ioc->facts.MsgVersion);
}
3046
static DEVICE_ATTR(version_mpi, S_IRUGO, mptscsih_version_mpi_show, NULL);
3047 3048

static ssize_t
3049 3050 3051
mptscsih_version_product_show(struct device *dev,
			      struct device_attribute *attr,
char *buf)
3052
{
3053
	struct Scsi_Host *host = class_to_shost(dev);
3054
	MPT_SCSI_HOST	*hd = shost_priv(host);
3055 3056 3057 3058
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%s\n", ioc->prod_name);
}
3059
static DEVICE_ATTR(version_product, S_IRUGO,
3060 3061 3062
    mptscsih_version_product_show, NULL);

static ssize_t
3063 3064 3065
mptscsih_version_nvdata_persistent_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
3066
{
3067
	struct Scsi_Host *host = class_to_shost(dev);
3068
	MPT_SCSI_HOST	*hd = shost_priv(host);
3069 3070 3071 3072 3073
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%02xh\n",
	    ioc->nvdata_version_persistent);
}
3074
static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
3075 3076 3077
    mptscsih_version_nvdata_persistent_show, NULL);

static ssize_t
3078 3079
mptscsih_version_nvdata_default_show(struct device *dev,
				     struct device_attribute *attr, char *buf)
3080
{
3081
	struct Scsi_Host *host = class_to_shost(dev);
3082
	MPT_SCSI_HOST	*hd = shost_priv(host);
3083 3084 3085 3086
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%02xh\n",ioc->nvdata_version_default);
}
3087
static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
3088 3089 3090
    mptscsih_version_nvdata_default_show, NULL);

static ssize_t
3091 3092
mptscsih_board_name_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
3093
{
3094
	struct Scsi_Host *host = class_to_shost(dev);
3095
	MPT_SCSI_HOST	*hd = shost_priv(host);
3096 3097 3098 3099
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%s\n", ioc->board_name);
}
3100
static DEVICE_ATTR(board_name, S_IRUGO, mptscsih_board_name_show, NULL);
3101 3102

static ssize_t
3103 3104
mptscsih_board_assembly_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
3105
{
3106
	struct Scsi_Host *host = class_to_shost(dev);
3107
	MPT_SCSI_HOST	*hd = shost_priv(host);
3108 3109 3110 3111
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%s\n", ioc->board_assembly);
}
3112
static DEVICE_ATTR(board_assembly, S_IRUGO,
3113 3114 3115
    mptscsih_board_assembly_show, NULL);

static ssize_t
3116 3117
mptscsih_board_tracer_show(struct device *dev, struct device_attribute *attr,
			   char *buf)
3118
{
3119
	struct Scsi_Host *host = class_to_shost(dev);
3120
	MPT_SCSI_HOST	*hd = shost_priv(host);
3121 3122 3123 3124
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%s\n", ioc->board_tracer);
}
3125
static DEVICE_ATTR(board_tracer, S_IRUGO,
3126 3127 3128
    mptscsih_board_tracer_show, NULL);

static ssize_t
3129 3130
mptscsih_io_delay_show(struct device *dev, struct device_attribute *attr,
		       char *buf)
3131
{
3132
	struct Scsi_Host *host = class_to_shost(dev);
3133
	MPT_SCSI_HOST	*hd = shost_priv(host);
3134 3135 3136 3137
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
}
3138
static DEVICE_ATTR(io_delay, S_IRUGO,
3139 3140 3141
    mptscsih_io_delay_show, NULL);

static ssize_t
3142 3143
mptscsih_device_delay_show(struct device *dev, struct device_attribute *attr,
			   char *buf)
3144
{
3145
	struct Scsi_Host *host = class_to_shost(dev);
3146
	MPT_SCSI_HOST	*hd = shost_priv(host);
3147 3148 3149 3150
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
}
3151
static DEVICE_ATTR(device_delay, S_IRUGO,
3152 3153
    mptscsih_device_delay_show, NULL);

3154
static ssize_t
3155 3156
mptscsih_debug_level_show(struct device *dev, struct device_attribute *attr,
			  char *buf)
3157
{
3158
	struct Scsi_Host *host = class_to_shost(dev);
3159
	MPT_SCSI_HOST	*hd = shost_priv(host);
3160 3161 3162 3163 3164
	MPT_ADAPTER *ioc = hd->ioc;

	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->debug_level);
}
static ssize_t
3165 3166
mptscsih_debug_level_store(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
3167
{
3168
	struct Scsi_Host *host = class_to_shost(dev);
3169
	MPT_SCSI_HOST	*hd = shost_priv(host);
3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180
	MPT_ADAPTER *ioc = hd->ioc;
	int val = 0;

	if (sscanf(buf, "%x", &val) != 1)
		return -EINVAL;

	ioc->debug_level = val;
	printk(MYIOC_s_INFO_FMT "debug_level=%08xh\n",
				ioc->name, ioc->debug_level);
	return strlen(buf);
}
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196
static DEVICE_ATTR(debug_level, S_IRUGO | S_IWUSR,
	mptscsih_debug_level_show, mptscsih_debug_level_store);

struct device_attribute *mptscsih_host_attrs[] = {
	&dev_attr_version_fw,
	&dev_attr_version_bios,
	&dev_attr_version_mpi,
	&dev_attr_version_product,
	&dev_attr_version_nvdata_persistent,
	&dev_attr_version_nvdata_default,
	&dev_attr_board_name,
	&dev_attr_board_assembly,
	&dev_attr_board_tracer,
	&dev_attr_io_delay,
	&dev_attr_device_delay,
	&dev_attr_debug_level,
3197 3198
	NULL,
};
3199

3200 3201
EXPORT_SYMBOL(mptscsih_host_attrs);

3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
EXPORT_SYMBOL(mptscsih_remove);
EXPORT_SYMBOL(mptscsih_shutdown);
#ifdef CONFIG_PM
EXPORT_SYMBOL(mptscsih_suspend);
EXPORT_SYMBOL(mptscsih_resume);
#endif
EXPORT_SYMBOL(mptscsih_proc_info);
EXPORT_SYMBOL(mptscsih_info);
EXPORT_SYMBOL(mptscsih_qcmd);
EXPORT_SYMBOL(mptscsih_slave_destroy);
EXPORT_SYMBOL(mptscsih_slave_configure);
EXPORT_SYMBOL(mptscsih_abort);
EXPORT_SYMBOL(mptscsih_dev_reset);
EXPORT_SYMBOL(mptscsih_bus_reset);
EXPORT_SYMBOL(mptscsih_host_reset);
EXPORT_SYMBOL(mptscsih_bios_param);
EXPORT_SYMBOL(mptscsih_io_done);
EXPORT_SYMBOL(mptscsih_taskmgmt_complete);
EXPORT_SYMBOL(mptscsih_scandv_complete);
EXPORT_SYMBOL(mptscsih_event_process);
EXPORT_SYMBOL(mptscsih_ioc_reset);
3223
EXPORT_SYMBOL(mptscsih_change_queue_depth);
Linus Torvalds's avatar
Linus Torvalds committed
3224

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