11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/message/fusion/mptscsih.c
30d0c7974SMoore, Eric Dean   *      For use with LSI Logic PCI chip/adapter(s)
41da177e4SLinus Torvalds  *      running LSI Logic Fusion MPT (Message Passing Technology) firmware.
51da177e4SLinus Torvalds  *
60d0c7974SMoore, Eric Dean   *  Copyright (c) 1999-2005 LSI Logic Corporation
71da177e4SLinus Torvalds  *  (mailto:mpt_linux_developer@lsil.com)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
111da177e4SLinus Torvalds /*
121da177e4SLinus Torvalds     This program is free software; you can redistribute it and/or modify
131da177e4SLinus Torvalds     it under the terms of the GNU General Public License as published by
141da177e4SLinus Torvalds     the Free Software Foundation; version 2 of the License.
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds     This program is distributed in the hope that it will be useful,
171da177e4SLinus Torvalds     but WITHOUT ANY WARRANTY; without even the implied warranty of
181da177e4SLinus Torvalds     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
191da177e4SLinus Torvalds     GNU General Public License for more details.
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds     NO WARRANTY
221da177e4SLinus Torvalds     THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
231da177e4SLinus Torvalds     CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
241da177e4SLinus Torvalds     LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
251da177e4SLinus Torvalds     MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
261da177e4SLinus Torvalds     solely responsible for determining the appropriateness of using and
271da177e4SLinus Torvalds     distributing the Program and assumes all risks associated with its
281da177e4SLinus Torvalds     exercise of rights under this Agreement, including but not limited to
291da177e4SLinus Torvalds     the risks and costs of program errors, damage to or loss of data,
301da177e4SLinus Torvalds     programs or equipment, and unavailability or interruption of operations.
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds     DISCLAIMER OF LIABILITY
331da177e4SLinus Torvalds     NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
341da177e4SLinus Torvalds     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
351da177e4SLinus Torvalds     DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
361da177e4SLinus Torvalds     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
371da177e4SLinus Torvalds     TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
381da177e4SLinus Torvalds     USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
391da177e4SLinus Torvalds     HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds     You should have received a copy of the GNU General Public License
421da177e4SLinus Torvalds     along with this program; if not, write to the Free Software
431da177e4SLinus Torvalds     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
441da177e4SLinus Torvalds */
451da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include "linux_compat.h"	/* linux-2.6 tweaks */
481da177e4SLinus Torvalds #include <linux/module.h>
491da177e4SLinus Torvalds #include <linux/kernel.h>
501da177e4SLinus Torvalds #include <linux/init.h>
511da177e4SLinus Torvalds #include <linux/errno.h>
521da177e4SLinus Torvalds #include <linux/kdev_t.h>
531da177e4SLinus Torvalds #include <linux/blkdev.h>
541da177e4SLinus Torvalds #include <linux/delay.h>	/* for mdelay */
551da177e4SLinus Torvalds #include <linux/interrupt.h>	/* needed for in_interrupt() proto */
561da177e4SLinus Torvalds #include <linux/reboot.h>	/* notifier code */
571da177e4SLinus Torvalds #include <linux/sched.h>
581da177e4SLinus Torvalds #include <linux/workqueue.h>
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #include <scsi/scsi.h>
611da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h>
621da177e4SLinus Torvalds #include <scsi/scsi_device.h>
631da177e4SLinus Torvalds #include <scsi/scsi_host.h>
641da177e4SLinus Torvalds #include <scsi/scsi_tcq.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include "mptbase.h"
671da177e4SLinus Torvalds #include "mptscsih.h"
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
701da177e4SLinus Torvalds #define my_NAME		"Fusion MPT SCSI Host driver"
711da177e4SLinus Torvalds #define my_VERSION	MPT_LINUX_VERSION_COMMON
721da177e4SLinus Torvalds #define MYNAM		"mptscsih"
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds MODULE_AUTHOR(MODULEAUTHOR);
751da177e4SLinus Torvalds MODULE_DESCRIPTION(my_NAME);
761da177e4SLinus Torvalds MODULE_LICENSE("GPL");
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds typedef struct _BIG_SENSE_BUF {
811da177e4SLinus Torvalds 	u8		data[MPT_SENSE_BUFFER_ALLOC];
821da177e4SLinus Torvalds } BIG_SENSE_BUF;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #define MPT_SCANDV_GOOD			(0x00000000) /* must be 0 */
851da177e4SLinus Torvalds #define MPT_SCANDV_DID_RESET		(0x00000001)
861da177e4SLinus Torvalds #define MPT_SCANDV_SENSE		(0x00000002)
871da177e4SLinus Torvalds #define MPT_SCANDV_SOME_ERROR		(0x00000004)
881da177e4SLinus Torvalds #define MPT_SCANDV_SELECTION_TIMEOUT	(0x00000008)
891da177e4SLinus Torvalds #define MPT_SCANDV_ISSUE_SENSE		(0x00000010)
901da177e4SLinus Torvalds #define MPT_SCANDV_FALLBACK		(0x00000020)
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #define MPT_SCANDV_MAX_RETRIES		(10)
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #define MPT_ICFLAG_BUF_CAP	0x01	/* ReadBuffer Read Capacity format */
951da177e4SLinus Torvalds #define MPT_ICFLAG_ECHO		0x02	/* ReadBuffer Echo buffer format */
961da177e4SLinus Torvalds #define MPT_ICFLAG_PHYS_DISK	0x04	/* Any SCSI IO but do Phys Disk Format */
971da177e4SLinus Torvalds #define MPT_ICFLAG_TAGGED_CMD	0x08	/* Do tagged IO */
981da177e4SLinus Torvalds #define MPT_ICFLAG_DID_RESET	0x20	/* Bus Reset occurred with this command */
991da177e4SLinus Torvalds #define MPT_ICFLAG_RESERVED	0x40	/* Reserved has been issued */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds typedef struct _internal_cmd {
1021da177e4SLinus Torvalds 	char		*data;		/* data pointer */
1031da177e4SLinus Torvalds 	dma_addr_t	data_dma;	/* data dma address */
1041da177e4SLinus Torvalds 	int		size;		/* transfer size */
1051da177e4SLinus Torvalds 	u8		cmd;		/* SCSI Op Code */
1061da177e4SLinus Torvalds 	u8		bus;		/* bus number */
1071da177e4SLinus Torvalds 	u8		id;		/* SCSI ID (virtual) */
1081da177e4SLinus Torvalds 	u8		lun;
1091da177e4SLinus Torvalds 	u8		flags;		/* Bit Field - See above */
1101da177e4SLinus Torvalds 	u8		physDiskNum;	/* Phys disk number, -1 else */
1111da177e4SLinus Torvalds 	u8		rsvd2;
1121da177e4SLinus Torvalds 	u8		rsvd;
1131da177e4SLinus Torvalds } INTERNAL_CMD;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds typedef struct _negoparms {
1161da177e4SLinus Torvalds 	u8 width;
1171da177e4SLinus Torvalds 	u8 offset;
1181da177e4SLinus Torvalds 	u8 factor;
1191da177e4SLinus Torvalds 	u8 flags;
1201da177e4SLinus Torvalds } NEGOPARMS;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds typedef struct _dv_parameters {
1231da177e4SLinus Torvalds 	NEGOPARMS	 max;
1241da177e4SLinus Torvalds 	NEGOPARMS	 now;
1251da177e4SLinus Torvalds 	u8		 cmd;
1261da177e4SLinus Torvalds 	u8		 id;
1271da177e4SLinus Torvalds 	u16		 pad1;
1281da177e4SLinus Torvalds } DVPARAMETERS;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /*
1311da177e4SLinus Torvalds  *  Other private/forward protos...
1321da177e4SLinus Torvalds  */
1330d0c7974SMoore, Eric Dean  int		mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
1341da177e4SLinus Torvalds static void	mptscsih_report_queue_full(struct scsi_cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq);
1350d0c7974SMoore, Eric Dean  int		mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds static int	mptscsih_AddSGE(MPT_ADAPTER *ioc, struct scsi_cmnd *SCpnt,
1381da177e4SLinus Torvalds 				 SCSIIORequest_t *pReq, int req_idx);
1391da177e4SLinus Torvalds static void	mptscsih_freeChainBuffers(MPT_ADAPTER *ioc, int req_idx);
1400d0c7974SMoore, Eric Dean  static void	mptscsih_copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply);
1411da177e4SLinus Torvalds static int	mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd);
1421da177e4SLinus Torvalds static int	mptscsih_tm_wait_for_completion(MPT_SCSI_HOST * hd, ulong timeout );
1431da177e4SLinus Torvalds static u32	SCPNT_TO_LOOKUP_IDX(struct scsi_cmnd *sc);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds static int	mptscsih_TMHandler(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout);
1461da177e4SLinus Torvalds static int	mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout);
1471da177e4SLinus Torvalds 
1480d0c7974SMoore, Eric Dean  int		mptscsih_ioc_reset(MPT_ADAPTER *ioc, int post_reset);
1490d0c7974SMoore, Eric Dean  int		mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static void	mptscsih_initTarget(MPT_SCSI_HOST *hd, int bus_id, int target_id, u8 lun, char *data, int dlen);
1521da177e4SLinus Torvalds static void	mptscsih_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtDevice *target, char byte56);
1531da177e4SLinus Torvalds static void	mptscsih_set_dvflags(MPT_SCSI_HOST *hd, SCSIIORequest_t *pReq);
1541da177e4SLinus Torvalds static void	mptscsih_setDevicePage1Flags (u8 width, u8 factor, u8 offset, int *requestedPtr, int *configurationPtr, u8 flags);
1551da177e4SLinus Torvalds static void	mptscsih_no_negotiate(MPT_SCSI_HOST *hd, int target_id);
1561da177e4SLinus Torvalds static int	mptscsih_writeSDP1(MPT_SCSI_HOST *hd, int portnum, int target, int flags);
1571da177e4SLinus Torvalds static int	mptscsih_writeIOCPage4(MPT_SCSI_HOST *hd, int target_id, int bus);
1580d0c7974SMoore, Eric Dean  int		mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
1591da177e4SLinus Torvalds static int	mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *iocmd);
1601da177e4SLinus Torvalds static int	mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, int portnum);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
1631da177e4SLinus Torvalds static int	mptscsih_do_raid(MPT_SCSI_HOST *hd, u8 action, INTERNAL_CMD *io);
1641da177e4SLinus Torvalds static void	mptscsih_domainValidation(void *hd);
1651da177e4SLinus Torvalds static int	mptscsih_is_phys_disk(MPT_ADAPTER *ioc, int id);
1661da177e4SLinus Torvalds static void	mptscsih_qas_check(MPT_SCSI_HOST *hd, int id);
1671da177e4SLinus Torvalds static int	mptscsih_doDv(MPT_SCSI_HOST *hd, int channel, int target);
1681da177e4SLinus Torvalds static void	mptscsih_dv_parms(MPT_SCSI_HOST *hd, DVPARAMETERS *dv,void *pPage);
1691da177e4SLinus Torvalds static void	mptscsih_fillbuf(char *buffer, int size, int index, int width);
1701da177e4SLinus Torvalds #endif
1711da177e4SLinus Torvalds 
1720d0c7974SMoore, Eric Dean  void 		mptscsih_remove(struct pci_dev *);
1730d0c7974SMoore, Eric Dean  void 		mptscsih_shutdown(struct device *);
1741da177e4SLinus Torvalds #ifdef CONFIG_PM
1750d0c7974SMoore, Eric Dean  int 		mptscsih_suspend(struct pci_dev *pdev, pm_message_t state);
1760d0c7974SMoore, Eric Dean  int 		mptscsih_resume(struct pci_dev *pdev);
1771da177e4SLinus Torvalds #endif
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds #define SNS_LEN(scp)	sizeof((scp)->sense_buffer)
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
1821da177e4SLinus Torvalds /*
1831da177e4SLinus Torvalds  * Domain Validation task structure
1841da177e4SLinus Torvalds  */
1851da177e4SLinus Torvalds static DEFINE_SPINLOCK(dvtaskQ_lock);
1861da177e4SLinus Torvalds static int dvtaskQ_active = 0;
1871da177e4SLinus Torvalds static int dvtaskQ_release = 0;
1880d0c7974SMoore, Eric Dean  static struct work_struct	dvTaskQ_task;
1891da177e4SLinus Torvalds #endif
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1921da177e4SLinus Torvalds /**
1931da177e4SLinus Torvalds  *	mptscsih_add_sge - Place a simple SGE at address pAddr.
1941da177e4SLinus Torvalds  *	@pAddr: virtual address for SGE
1951da177e4SLinus Torvalds  *	@flagslength: SGE flags and data transfer length
1961da177e4SLinus Torvalds  *	@dma_addr: Physical address
1971da177e4SLinus Torvalds  *
1981da177e4SLinus Torvalds  *	This routine places a MPT request frame back on the MPT adapter's
1991da177e4SLinus Torvalds  *	FreeQ.
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds static inline void
2021da177e4SLinus Torvalds mptscsih_add_sge(char *pAddr, u32 flagslength, dma_addr_t dma_addr)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	if (sizeof(dma_addr_t) == sizeof(u64)) {
2051da177e4SLinus Torvalds 		SGESimple64_t *pSge = (SGESimple64_t *) pAddr;
2061da177e4SLinus Torvalds 		u32 tmp = dma_addr & 0xFFFFFFFF;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 		pSge->FlagsLength = cpu_to_le32(flagslength);
2091da177e4SLinus Torvalds 		pSge->Address.Low = cpu_to_le32(tmp);
2101da177e4SLinus Torvalds 		tmp = (u32) ((u64)dma_addr >> 32);
2111da177e4SLinus Torvalds 		pSge->Address.High = cpu_to_le32(tmp);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	} else {
2141da177e4SLinus Torvalds 		SGESimple32_t *pSge = (SGESimple32_t *) pAddr;
2151da177e4SLinus Torvalds 		pSge->FlagsLength = cpu_to_le32(flagslength);
2161da177e4SLinus Torvalds 		pSge->Address = cpu_to_le32(dma_addr);
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds } /* mptscsih_add_sge() */
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2211da177e4SLinus Torvalds /**
2221da177e4SLinus Torvalds  *	mptscsih_add_chain - Place a chain SGE at address pAddr.
2231da177e4SLinus Torvalds  *	@pAddr: virtual address for SGE
2241da177e4SLinus Torvalds  *	@next: nextChainOffset value (u32's)
2251da177e4SLinus Torvalds  *	@length: length of next SGL segment
2261da177e4SLinus Torvalds  *	@dma_addr: Physical address
2271da177e4SLinus Torvalds  *
2281da177e4SLinus Torvalds  *	This routine places a MPT request frame back on the MPT adapter's
2291da177e4SLinus Torvalds  *	FreeQ.
2301da177e4SLinus Torvalds  */
2311da177e4SLinus Torvalds static inline void
2321da177e4SLinus Torvalds mptscsih_add_chain(char *pAddr, u8 next, u16 length, dma_addr_t dma_addr)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	if (sizeof(dma_addr_t) == sizeof(u64)) {
2351da177e4SLinus Torvalds 		SGEChain64_t *pChain = (SGEChain64_t *) pAddr;
2361da177e4SLinus Torvalds 		u32 tmp = dma_addr & 0xFFFFFFFF;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 		pChain->Length = cpu_to_le16(length);
2391da177e4SLinus Torvalds 		pChain->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT | mpt_addr_size();
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 		pChain->NextChainOffset = next;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 		pChain->Address.Low = cpu_to_le32(tmp);
2441da177e4SLinus Torvalds 		tmp = (u32) ((u64)dma_addr >> 32);
2451da177e4SLinus Torvalds 		pChain->Address.High = cpu_to_le32(tmp);
2461da177e4SLinus Torvalds 	} else {
2471da177e4SLinus Torvalds 		SGEChain32_t *pChain = (SGEChain32_t *) pAddr;
2481da177e4SLinus Torvalds 		pChain->Length = cpu_to_le16(length);
2491da177e4SLinus Torvalds 		pChain->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT | mpt_addr_size();
2501da177e4SLinus Torvalds 		pChain->NextChainOffset = next;
2511da177e4SLinus Torvalds 		pChain->Address = cpu_to_le32(dma_addr);
2521da177e4SLinus Torvalds 	}
2531da177e4SLinus Torvalds } /* mptscsih_add_chain() */
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2561da177e4SLinus Torvalds /*
2571da177e4SLinus Torvalds  *	mptscsih_getFreeChainBuffer - Function to get a free chain
2581da177e4SLinus Torvalds  *	from the MPT_SCSI_HOST FreeChainQ.
2591da177e4SLinus Torvalds  *	@ioc: Pointer to MPT_ADAPTER structure
2601da177e4SLinus Torvalds  *	@req_idx: Index of the SCSI IO request frame. (output)
2611da177e4SLinus Torvalds  *
2621da177e4SLinus Torvalds  *	return SUCCESS or FAILED
2631da177e4SLinus Torvalds  */
2641da177e4SLinus Torvalds static inline int
2651da177e4SLinus Torvalds mptscsih_getFreeChainBuffer(MPT_ADAPTER *ioc, int *retIndex)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	MPT_FRAME_HDR *chainBuf;
2681da177e4SLinus Torvalds 	unsigned long flags;
2691da177e4SLinus Torvalds 	int rc;
2701da177e4SLinus Torvalds 	int chain_idx;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	dsgprintk((MYIOC_s_INFO_FMT "getFreeChainBuffer called\n",
2731da177e4SLinus Torvalds 			ioc->name));
2741da177e4SLinus Torvalds 	spin_lock_irqsave(&ioc->FreeQlock, flags);
2751da177e4SLinus Torvalds 	if (!list_empty(&ioc->FreeChainQ)) {
2761da177e4SLinus Torvalds 		int offset;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 		chainBuf = list_entry(ioc->FreeChainQ.next, MPT_FRAME_HDR,
2791da177e4SLinus Torvalds 				u.frame.linkage.list);
2801da177e4SLinus Torvalds 		list_del(&chainBuf->u.frame.linkage.list);
2811da177e4SLinus Torvalds 		offset = (u8 *)chainBuf - (u8 *)ioc->ChainBuffer;
2821da177e4SLinus Torvalds 		chain_idx = offset / ioc->req_sz;
2831da177e4SLinus Torvalds 		rc = SUCCESS;
2841da177e4SLinus Torvalds 		dsgprintk((MYIOC_s_INFO_FMT "getFreeChainBuffer (index %d), got buf=%p\n",
2851da177e4SLinus Torvalds 			ioc->name, *retIndex, chainBuf));
2861da177e4SLinus Torvalds 	} else {
2871da177e4SLinus Torvalds 		rc = FAILED;
2881da177e4SLinus Torvalds 		chain_idx = MPT_HOST_NO_CHAIN;
2891da177e4SLinus Torvalds 		dfailprintk((MYIOC_s_ERR_FMT "getFreeChainBuffer failed\n",
2901da177e4SLinus Torvalds 			ioc->name));
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	*retIndex = chain_idx;
2951da177e4SLinus Torvalds 	return rc;
2961da177e4SLinus Torvalds } /* mptscsih_getFreeChainBuffer() */
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  *	mptscsih_AddSGE - Add a SGE (plus chain buffers) to the
3011da177e4SLinus Torvalds  *	SCSIIORequest_t Message Frame.
3021da177e4SLinus Torvalds  *	@ioc: Pointer to MPT_ADAPTER structure
3031da177e4SLinus Torvalds  *	@SCpnt: Pointer to scsi_cmnd structure
3041da177e4SLinus Torvalds  *	@pReq: Pointer to SCSIIORequest_t structure
3051da177e4SLinus Torvalds  *
3061da177e4SLinus Torvalds  *	Returns ...
3071da177e4SLinus Torvalds  */
3081da177e4SLinus Torvalds static int
3091da177e4SLinus Torvalds mptscsih_AddSGE(MPT_ADAPTER *ioc, struct scsi_cmnd *SCpnt,
3101da177e4SLinus Torvalds 		SCSIIORequest_t *pReq, int req_idx)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	char 	*psge;
3131da177e4SLinus Torvalds 	char	*chainSge;
3141da177e4SLinus Torvalds 	struct scatterlist *sg;
3151da177e4SLinus Torvalds 	int	 frm_sz;
3161da177e4SLinus Torvalds 	int	 sges_left, sg_done;
3171da177e4SLinus Torvalds 	int	 chain_idx = MPT_HOST_NO_CHAIN;
3181da177e4SLinus Torvalds 	int	 sgeOffset;
3191da177e4SLinus Torvalds 	int	 numSgeSlots, numSgeThisFrame;
3201da177e4SLinus Torvalds 	u32	 sgflags, sgdir, thisxfer = 0;
3211da177e4SLinus Torvalds 	int	 chain_dma_off = 0;
3221da177e4SLinus Torvalds 	int	 newIndex;
3231da177e4SLinus Torvalds 	int	 ii;
3241da177e4SLinus Torvalds 	dma_addr_t v2;
3251da177e4SLinus Torvalds 	u32	RequestNB;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	sgdir = le32_to_cpu(pReq->Control) & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK;
3281da177e4SLinus Torvalds 	if (sgdir == MPI_SCSIIO_CONTROL_WRITE)  {
3291da177e4SLinus Torvalds 		sgdir = MPT_TRANSFER_HOST_TO_IOC;
3301da177e4SLinus Torvalds 	} else {
3311da177e4SLinus Torvalds 		sgdir = MPT_TRANSFER_IOC_TO_HOST;
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	psge = (char *) &pReq->SGL;
3351da177e4SLinus Torvalds 	frm_sz = ioc->req_sz;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	/* Map the data portion, if any.
3381da177e4SLinus Torvalds 	 * sges_left  = 0 if no data transfer.
3391da177e4SLinus Torvalds 	 */
3401da177e4SLinus Torvalds 	if ( (sges_left = SCpnt->use_sg) ) {
3411da177e4SLinus Torvalds 		sges_left = pci_map_sg(ioc->pcidev,
3421da177e4SLinus Torvalds 			       (struct scatterlist *) SCpnt->request_buffer,
3431da177e4SLinus Torvalds  			       SCpnt->use_sg,
3441da177e4SLinus Torvalds 			       SCpnt->sc_data_direction);
3451da177e4SLinus Torvalds 		if (sges_left == 0)
3461da177e4SLinus Torvalds 			return FAILED;
3471da177e4SLinus Torvalds 	} else if (SCpnt->request_bufflen) {
3481da177e4SLinus Torvalds 		SCpnt->SCp.dma_handle = pci_map_single(ioc->pcidev,
3491da177e4SLinus Torvalds 				      SCpnt->request_buffer,
3501da177e4SLinus Torvalds 				      SCpnt->request_bufflen,
3511da177e4SLinus Torvalds 				      SCpnt->sc_data_direction);
3521da177e4SLinus Torvalds 		dsgprintk((MYIOC_s_INFO_FMT "SG: non-SG for %p, len=%d\n",
3531da177e4SLinus Torvalds 				ioc->name, SCpnt, SCpnt->request_bufflen));
3541da177e4SLinus Torvalds 		mptscsih_add_sge((char *) &pReq->SGL,
3551da177e4SLinus Torvalds 			0xD1000000|MPT_SGE_FLAGS_ADDRESSING|sgdir|SCpnt->request_bufflen,
3561da177e4SLinus Torvalds 			SCpnt->SCp.dma_handle);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 		return SUCCESS;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	/* Handle the SG case.
3621da177e4SLinus Torvalds 	 */
3631da177e4SLinus Torvalds 	sg = (struct scatterlist *) SCpnt->request_buffer;
3641da177e4SLinus Torvalds 	sg_done  = 0;
3651da177e4SLinus Torvalds 	sgeOffset = sizeof(SCSIIORequest_t) - sizeof(SGE_IO_UNION);
3661da177e4SLinus Torvalds 	chainSge = NULL;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/* Prior to entering this loop - the following must be set
3691da177e4SLinus Torvalds 	 * current MF:  sgeOffset (bytes)
3701da177e4SLinus Torvalds 	 *              chainSge (Null if original MF is not a chain buffer)
3711da177e4SLinus Torvalds 	 *              sg_done (num SGE done for this MF)
3721da177e4SLinus Torvalds 	 */
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds nextSGEset:
3751da177e4SLinus Torvalds 	numSgeSlots = ((frm_sz - sgeOffset) / (sizeof(u32) + sizeof(dma_addr_t)) );
3761da177e4SLinus Torvalds 	numSgeThisFrame = (sges_left < numSgeSlots) ? sges_left : numSgeSlots;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	sgflags = MPT_SGE_FLAGS_SIMPLE_ELEMENT | MPT_SGE_FLAGS_ADDRESSING | sgdir;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	/* Get first (num - 1) SG elements
3811da177e4SLinus Torvalds 	 * Skip any SG entries with a length of 0
3821da177e4SLinus Torvalds 	 * NOTE: at finish, sg and psge pointed to NEXT data/location positions
3831da177e4SLinus Torvalds 	 */
3841da177e4SLinus Torvalds 	for (ii=0; ii < (numSgeThisFrame-1); ii++) {
3851da177e4SLinus Torvalds 		thisxfer = sg_dma_len(sg);
3861da177e4SLinus Torvalds 		if (thisxfer == 0) {
3871da177e4SLinus Torvalds 			sg ++; /* Get next SG element from the OS */
3881da177e4SLinus Torvalds 			sg_done++;
3891da177e4SLinus Torvalds 			continue;
3901da177e4SLinus Torvalds 		}
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 		v2 = sg_dma_address(sg);
3931da177e4SLinus Torvalds 		mptscsih_add_sge(psge, sgflags | thisxfer, v2);
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 		sg++;		/* Get next SG element from the OS */
3961da177e4SLinus Torvalds 		psge += (sizeof(u32) + sizeof(dma_addr_t));
3971da177e4SLinus Torvalds 		sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
3981da177e4SLinus Torvalds 		sg_done++;
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	if (numSgeThisFrame == sges_left) {
4021da177e4SLinus Torvalds 		/* Add last element, end of buffer and end of list flags.
4031da177e4SLinus Torvalds 		 */
4041da177e4SLinus Torvalds 		sgflags |= MPT_SGE_FLAGS_LAST_ELEMENT |
4051da177e4SLinus Torvalds 				MPT_SGE_FLAGS_END_OF_BUFFER |
4061da177e4SLinus Torvalds 				MPT_SGE_FLAGS_END_OF_LIST;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 		/* Add last SGE and set termination flags.
4091da177e4SLinus Torvalds 		 * Note: Last SGE may have a length of 0 - which should be ok.
4101da177e4SLinus Torvalds 		 */
4111da177e4SLinus Torvalds 		thisxfer = sg_dma_len(sg);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		v2 = sg_dma_address(sg);
4141da177e4SLinus Torvalds 		mptscsih_add_sge(psge, sgflags | thisxfer, v2);
4151da177e4SLinus Torvalds 		/*
4161da177e4SLinus Torvalds 		sg++;
4171da177e4SLinus Torvalds 		psge += (sizeof(u32) + sizeof(dma_addr_t));
4181da177e4SLinus Torvalds 		*/
4191da177e4SLinus Torvalds 		sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
4201da177e4SLinus Torvalds 		sg_done++;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 		if (chainSge) {
4231da177e4SLinus Torvalds 			/* The current buffer is a chain buffer,
4241da177e4SLinus Torvalds 			 * but there is not another one.
4251da177e4SLinus Torvalds 			 * Update the chain element
4261da177e4SLinus Torvalds 			 * Offset and Length fields.
4271da177e4SLinus Torvalds 			 */
4281da177e4SLinus Torvalds 			mptscsih_add_chain((char *)chainSge, 0, sgeOffset, ioc->ChainBufferDMA + chain_dma_off);
4291da177e4SLinus Torvalds 		} else {
4301da177e4SLinus Torvalds 			/* The current buffer is the original MF
4311da177e4SLinus Torvalds 			 * and there is no Chain buffer.
4321da177e4SLinus Torvalds 			 */
4331da177e4SLinus Torvalds 			pReq->ChainOffset = 0;
4341da177e4SLinus Torvalds 			RequestNB = (((sgeOffset - 1) >> ioc->NBShiftFactor)  + 1) & 0x03;
4351da177e4SLinus Torvalds 			dsgprintk((MYIOC_s_ERR_FMT
4361da177e4SLinus Torvalds 			    "Single Buffer RequestNB=%x, sgeOffset=%d\n", ioc->name, RequestNB, sgeOffset));
4371da177e4SLinus Torvalds 			ioc->RequestNB[req_idx] = RequestNB;
4381da177e4SLinus Torvalds 		}
4391da177e4SLinus Torvalds 	} else {
4401da177e4SLinus Torvalds 		/* At least one chain buffer is needed.
4411da177e4SLinus Torvalds 		 * Complete the first MF
4421da177e4SLinus Torvalds 		 *  - last SGE element, set the LastElement bit
4431da177e4SLinus Torvalds 		 *  - set ChainOffset (words) for orig MF
4441da177e4SLinus Torvalds 		 *             (OR finish previous MF chain buffer)
4451da177e4SLinus Torvalds 		 *  - update MFStructPtr ChainIndex
4461da177e4SLinus Torvalds 		 *  - Populate chain element
4471da177e4SLinus Torvalds 		 * Also
4481da177e4SLinus Torvalds 		 * Loop until done.
4491da177e4SLinus Torvalds 		 */
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 		dsgprintk((MYIOC_s_INFO_FMT "SG: Chain Required! sg done %d\n",
4521da177e4SLinus Torvalds 				ioc->name, sg_done));
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 		/* Set LAST_ELEMENT flag for last non-chain element
4551da177e4SLinus Torvalds 		 * in the buffer. Since psge points at the NEXT
4561da177e4SLinus Torvalds 		 * SGE element, go back one SGE element, update the flags
4571da177e4SLinus Torvalds 		 * and reset the pointer. (Note: sgflags & thisxfer are already
4581da177e4SLinus Torvalds 		 * set properly).
4591da177e4SLinus Torvalds 		 */
4601da177e4SLinus Torvalds 		if (sg_done) {
4611da177e4SLinus Torvalds 			u32 *ptmp = (u32 *) (psge - (sizeof(u32) + sizeof(dma_addr_t)));
4621da177e4SLinus Torvalds 			sgflags = le32_to_cpu(*ptmp);
4631da177e4SLinus Torvalds 			sgflags |= MPT_SGE_FLAGS_LAST_ELEMENT;
4641da177e4SLinus Torvalds 			*ptmp = cpu_to_le32(sgflags);
4651da177e4SLinus Torvalds 		}
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 		if (chainSge) {
4681da177e4SLinus Torvalds 			/* The current buffer is a chain buffer.
4691da177e4SLinus Torvalds 			 * chainSge points to the previous Chain Element.
4701da177e4SLinus Torvalds 			 * Update its chain element Offset and Length (must
4711da177e4SLinus Torvalds 			 * include chain element size) fields.
4721da177e4SLinus Torvalds 			 * Old chain element is now complete.
4731da177e4SLinus Torvalds 			 */
4741da177e4SLinus Torvalds 			u8 nextChain = (u8) (sgeOffset >> 2);
4751da177e4SLinus Torvalds 			sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
4761da177e4SLinus Torvalds 			mptscsih_add_chain((char *)chainSge, nextChain, sgeOffset, ioc->ChainBufferDMA + chain_dma_off);
4771da177e4SLinus Torvalds 		} else {
4781da177e4SLinus Torvalds 			/* The original MF buffer requires a chain buffer -
4791da177e4SLinus Torvalds 			 * set the offset.
4801da177e4SLinus Torvalds 			 * Last element in this MF is a chain element.
4811da177e4SLinus Torvalds 			 */
4821da177e4SLinus Torvalds 			pReq->ChainOffset = (u8) (sgeOffset >> 2);
4831da177e4SLinus Torvalds 			RequestNB = (((sgeOffset - 1) >> ioc->NBShiftFactor)  + 1) & 0x03;
4841da177e4SLinus Torvalds 			dsgprintk((MYIOC_s_ERR_FMT "Chain Buffer Needed, RequestNB=%x sgeOffset=%d\n", ioc->name, RequestNB, sgeOffset));
4851da177e4SLinus Torvalds 			ioc->RequestNB[req_idx] = RequestNB;
4861da177e4SLinus Torvalds 		}
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 		sges_left -= sg_done;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 		/* NOTE: psge points to the beginning of the chain element
4921da177e4SLinus Torvalds 		 * in current buffer. Get a chain buffer.
4931da177e4SLinus Torvalds 		 */
4941da177e4SLinus Torvalds 		dsgprintk((MYIOC_s_INFO_FMT
4951da177e4SLinus Torvalds 		    "calling getFreeChainBuffer SCSI cmd=%02x (%p)\n",
4961da177e4SLinus Torvalds 		    ioc->name, pReq->CDB[0], SCpnt));
4971da177e4SLinus Torvalds 		if ((mptscsih_getFreeChainBuffer(ioc, &newIndex)) == FAILED)
4981da177e4SLinus Torvalds 			return FAILED;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 		/* Update the tracking arrays.
5011da177e4SLinus Torvalds 		 * If chainSge == NULL, update ReqToChain, else ChainToChain
5021da177e4SLinus Torvalds 		 */
5031da177e4SLinus Torvalds 		if (chainSge) {
5041da177e4SLinus Torvalds 			ioc->ChainToChain[chain_idx] = newIndex;
5051da177e4SLinus Torvalds 		} else {
5061da177e4SLinus Torvalds 			ioc->ReqToChain[req_idx] = newIndex;
5071da177e4SLinus Torvalds 		}
5081da177e4SLinus Torvalds 		chain_idx = newIndex;
5091da177e4SLinus Torvalds 		chain_dma_off = ioc->req_sz * chain_idx;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 		/* Populate the chainSGE for the current buffer.
5121da177e4SLinus Torvalds 		 * - Set chain buffer pointer to psge and fill
5131da177e4SLinus Torvalds 		 *   out the Address and Flags fields.
5141da177e4SLinus Torvalds 		 */
5151da177e4SLinus Torvalds 		chainSge = (char *) psge;
5161da177e4SLinus Torvalds 		dsgprintk((KERN_INFO "  Current buff @ %p (index 0x%x)",
5171da177e4SLinus Torvalds 				psge, req_idx));
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 		/* Start the SGE for the next buffer
5201da177e4SLinus Torvalds 		 */
5211da177e4SLinus Torvalds 		psge = (char *) (ioc->ChainBuffer + chain_dma_off);
5221da177e4SLinus Torvalds 		sgeOffset = 0;
5231da177e4SLinus Torvalds 		sg_done = 0;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 		dsgprintk((KERN_INFO "  Chain buff @ %p (index 0x%x)\n",
5261da177e4SLinus Torvalds 				psge, chain_idx));
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 		/* Start the SGE for the next buffer
5291da177e4SLinus Torvalds 		 */
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 		goto nextSGEset;
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	return SUCCESS;
5351da177e4SLinus Torvalds } /* mptscsih_AddSGE() */
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5381da177e4SLinus Torvalds /*
5391da177e4SLinus Torvalds  *	mptscsih_io_done - Main SCSI IO callback routine registered to
5401da177e4SLinus Torvalds  *	Fusion MPT (base) driver
5411da177e4SLinus Torvalds  *	@ioc: Pointer to MPT_ADAPTER structure
5421da177e4SLinus Torvalds  *	@mf: Pointer to original MPT request frame
5431da177e4SLinus Torvalds  *	@r: Pointer to MPT reply frame (NULL if TurboReply)
5441da177e4SLinus Torvalds  *
5451da177e4SLinus Torvalds  *	This routine is called from mpt.c::mpt_interrupt() at the completion
5461da177e4SLinus Torvalds  *	of any SCSI IO request.
5471da177e4SLinus Torvalds  *	This routine is registered with the Fusion MPT (base) driver at driver
5481da177e4SLinus Torvalds  *	load/init time via the mpt_register() API call.
5491da177e4SLinus Torvalds  *
5501da177e4SLinus Torvalds  *	Returns 1 indicating alloc'd request frame ptr should be freed.
5511da177e4SLinus Torvalds  */
5520d0c7974SMoore, Eric Dean  int
5531da177e4SLinus Torvalds mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	struct scsi_cmnd	*sc;
5561da177e4SLinus Torvalds 	MPT_SCSI_HOST	*hd;
5571da177e4SLinus Torvalds 	SCSIIORequest_t	*pScsiReq;
5581da177e4SLinus Torvalds 	SCSIIOReply_t	*pScsiReply;
5591da177e4SLinus Torvalds 	u16		 req_idx;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
5641da177e4SLinus Torvalds 	sc = hd->ScsiLookup[req_idx];
5651da177e4SLinus Torvalds 	if (sc == NULL) {
5661da177e4SLinus Torvalds 		MPIHeader_t *hdr = (MPIHeader_t *)mf;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 		/* Remark: writeSDP1 will use the ScsiDoneCtx
5691da177e4SLinus Torvalds 		 * If a SCSI I/O cmd, device disabled by OS and
5701da177e4SLinus Torvalds 		 * completion done. Cannot touch sc struct. Just free mem.
5711da177e4SLinus Torvalds 		 */
5721da177e4SLinus Torvalds 		if (hdr->Function == MPI_FUNCTION_SCSI_IO_REQUEST)
5731da177e4SLinus Torvalds 			printk(MYIOC_s_ERR_FMT "NULL ScsiCmd ptr!\n",
5741da177e4SLinus Torvalds 			ioc->name);
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 		mptscsih_freeChainBuffers(ioc, req_idx);
5771da177e4SLinus Torvalds 		return 1;
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	dmfprintk((MYIOC_s_INFO_FMT
5811da177e4SLinus Torvalds 		"ScsiDone (mf=%p,mr=%p,sc=%p,idx=%d)\n",
5821da177e4SLinus Torvalds 		ioc->name, mf, mr, sc, req_idx));
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	sc->result = DID_OK << 16;		/* Set default reply as OK */
5851da177e4SLinus Torvalds 	pScsiReq = (SCSIIORequest_t *) mf;
5861da177e4SLinus Torvalds 	pScsiReply = (SCSIIOReply_t *) mr;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	if (pScsiReply == NULL) {
5891da177e4SLinus Torvalds 		/* special context reply handling */
5901da177e4SLinus Torvalds 		;
5911da177e4SLinus Torvalds 	} else {
5921da177e4SLinus Torvalds 		u32	 xfer_cnt;
5931da177e4SLinus Torvalds 		u16	 status;
5941da177e4SLinus Torvalds 		u8	 scsi_state, scsi_status;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 		status = le16_to_cpu(pScsiReply->IOCStatus) & MPI_IOCSTATUS_MASK;
5971da177e4SLinus Torvalds 		scsi_state = pScsiReply->SCSIState;
5981da177e4SLinus Torvalds 		scsi_status = pScsiReply->SCSIStatus;
5991da177e4SLinus Torvalds 		xfer_cnt = le32_to_cpu(pScsiReply->TransferCount);
6001da177e4SLinus Torvalds 		sc->resid = sc->request_bufflen - xfer_cnt;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 		dreplyprintk((KERN_NOTICE "Reply ha=%d id=%d lun=%d:\n"
6031da177e4SLinus Torvalds 			"IOCStatus=%04xh SCSIState=%02xh SCSIStatus=%02xh\n"
6041da177e4SLinus Torvalds 			"resid=%d bufflen=%d xfer_cnt=%d\n",
6051da177e4SLinus Torvalds 			ioc->id, pScsiReq->TargetID, pScsiReq->LUN[1],
6061da177e4SLinus Torvalds 			status, scsi_state, scsi_status, sc->resid,
6071da177e4SLinus Torvalds 			sc->request_bufflen, xfer_cnt));
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 		if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID)
6100d0c7974SMoore, Eric Dean  			mptscsih_copy_sense_data(sc, hd, mf, pScsiReply);
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 		/*
6131da177e4SLinus Torvalds 		 *  Look for + dump FCP ResponseInfo[]!
6141da177e4SLinus Torvalds 		 */
6151da177e4SLinus Torvalds 		if (scsi_state & MPI_SCSI_STATE_RESPONSE_INFO_VALID) {
6161da177e4SLinus Torvalds 			printk(KERN_NOTICE "  FCP_ResponseInfo=%08xh\n",
6171da177e4SLinus Torvalds 			le32_to_cpu(pScsiReply->ResponseInfo));
6181da177e4SLinus Torvalds 		}
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 		switch(status) {
6211da177e4SLinus Torvalds 		case MPI_IOCSTATUS_BUSY:			/* 0x0002 */
6221da177e4SLinus Torvalds 			/* CHECKME!
6231da177e4SLinus Torvalds 			 * Maybe: DRIVER_BUSY | SUGGEST_RETRY | DID_SOFT_ERROR (retry)
6241da177e4SLinus Torvalds 			 * But not: DID_BUS_BUSY lest one risk
6251da177e4SLinus Torvalds 			 * killing interrupt handler:-(
6261da177e4SLinus Torvalds 			 */
6271da177e4SLinus Torvalds 			sc->result = SAM_STAT_BUSY;
6281da177e4SLinus Torvalds 			break;
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_INVALID_BUS:		/* 0x0041 */
6311da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_INVALID_TARGETID:	/* 0x0042 */
6321da177e4SLinus Torvalds 			sc->result = DID_BAD_TARGET << 16;
6331da177e4SLinus Torvalds 			break;
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:	/* 0x0043 */
6361da177e4SLinus Torvalds 			/* Spoof to SCSI Selection Timeout! */
6371da177e4SLinus Torvalds 			sc->result = DID_NO_CONNECT << 16;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 			if (hd->sel_timeout[pScsiReq->TargetID] < 0xFFFF)
6401da177e4SLinus Torvalds 				hd->sel_timeout[pScsiReq->TargetID]++;
6411da177e4SLinus Torvalds 			break;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:	/* 0x0048 */
6441da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:		/* 0x004B */
6451da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:		/* 0x004C */
6461da177e4SLinus Torvalds 			/* Linux handles an unsolicited DID_RESET better
6471da177e4SLinus Torvalds 			 * than an unsolicited DID_ABORT.
6481da177e4SLinus Torvalds 			 */
6491da177e4SLinus Torvalds 			sc->result = DID_RESET << 16;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 			/* GEM Workaround. */
6521da177e4SLinus Torvalds 			if (ioc->bus_type == SCSI)
6531da177e4SLinus Torvalds 				mptscsih_no_negotiate(hd, sc->device->id);
6541da177e4SLinus Torvalds 			break;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:	/* 0x0049 */
6571da177e4SLinus Torvalds 			if ( xfer_cnt >= sc->underflow ) {
6581da177e4SLinus Torvalds 				/* Sufficient data transfer occurred */
6591da177e4SLinus Torvalds 				sc->result = (DID_OK << 16) | scsi_status;
6601da177e4SLinus Torvalds 			} else if ( xfer_cnt == 0 ) {
6611da177e4SLinus Torvalds 				/* A CRC Error causes this condition; retry */
6621da177e4SLinus Torvalds 				sc->result = (DRIVER_SENSE << 24) | (DID_OK << 16) |
6631da177e4SLinus Torvalds 					(CHECK_CONDITION << 1);
6641da177e4SLinus Torvalds 				sc->sense_buffer[0] = 0x70;
6651da177e4SLinus Torvalds 				sc->sense_buffer[2] = NO_SENSE;
6661da177e4SLinus Torvalds 				sc->sense_buffer[12] = 0;
6671da177e4SLinus Torvalds 				sc->sense_buffer[13] = 0;
6681da177e4SLinus Torvalds 			} else {
6691da177e4SLinus Torvalds 				sc->result = DID_SOFT_ERROR << 16;
6701da177e4SLinus Torvalds 			}
6711da177e4SLinus Torvalds 			dreplyprintk((KERN_NOTICE "RESIDUAL_MISMATCH: result=%x on id=%d\n", sc->result, sc->target));
6721da177e4SLinus Torvalds 			break;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:		/* 0x0045 */
6751da177e4SLinus Torvalds 			/*
6761da177e4SLinus Torvalds 			 *  Do upfront check for valid SenseData and give it
6771da177e4SLinus Torvalds 			 *  precedence!
6781da177e4SLinus Torvalds 			 */
6791da177e4SLinus Torvalds 			sc->result = (DID_OK << 16) | scsi_status;
6801da177e4SLinus Torvalds 			if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
6811da177e4SLinus Torvalds 				/* Have already saved the status and sense data
6821da177e4SLinus Torvalds 				 */
6831da177e4SLinus Torvalds 				;
6841da177e4SLinus Torvalds 			} else {
6851da177e4SLinus Torvalds 				if (xfer_cnt < sc->underflow) {
6861da177e4SLinus Torvalds 					sc->result = DID_SOFT_ERROR << 16;
6871da177e4SLinus Torvalds 				}
6881da177e4SLinus Torvalds 				if (scsi_state & (MPI_SCSI_STATE_AUTOSENSE_FAILED | MPI_SCSI_STATE_NO_SCSI_STATUS)) {
6891da177e4SLinus Torvalds 					/* What to do?
6901da177e4SLinus Torvalds 				 	*/
6911da177e4SLinus Torvalds 					sc->result = DID_SOFT_ERROR << 16;
6921da177e4SLinus Torvalds 				}
6931da177e4SLinus Torvalds 				else if (scsi_state & MPI_SCSI_STATE_TERMINATED) {
6941da177e4SLinus Torvalds 					/*  Not real sure here either...  */
6951da177e4SLinus Torvalds 					sc->result = DID_RESET << 16;
6961da177e4SLinus Torvalds 				}
6971da177e4SLinus Torvalds 			}
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 			dreplyprintk((KERN_NOTICE "  sc->underflow={report ERR if < %02xh bytes xfer'd}\n",
7001da177e4SLinus Torvalds 					sc->underflow));
7011da177e4SLinus Torvalds 			dreplyprintk((KERN_NOTICE "  ActBytesXferd=%02xh\n", xfer_cnt));
7021da177e4SLinus Torvalds 			/* Report Queue Full
7031da177e4SLinus Torvalds 			 */
7041da177e4SLinus Torvalds 			if (scsi_status == MPI_SCSI_STATUS_TASK_SET_FULL)
7051da177e4SLinus Torvalds 				mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 			break;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:	/* 0x0040 */
7101da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SUCCESS:			/* 0x0000 */
7111da177e4SLinus Torvalds 			scsi_status = pScsiReply->SCSIStatus;
7121da177e4SLinus Torvalds 			sc->result = (DID_OK << 16) | scsi_status;
7131da177e4SLinus Torvalds 			if (scsi_state == 0) {
7141da177e4SLinus Torvalds 				;
7151da177e4SLinus Torvalds 			} else if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
7161da177e4SLinus Torvalds 				/*
7171da177e4SLinus Torvalds 				 * If running against circa 200003dd 909 MPT f/w,
7181da177e4SLinus Torvalds 				 * may get this (AUTOSENSE_VALID) for actual TASK_SET_FULL
7191da177e4SLinus Torvalds 				 * (QUEUE_FULL) returned from device! --> get 0x0000?128
7201da177e4SLinus Torvalds 				 * and with SenseBytes set to 0.
7211da177e4SLinus Torvalds 				 */
7221da177e4SLinus Torvalds 				if (pScsiReply->SCSIStatus == MPI_SCSI_STATUS_TASK_SET_FULL)
7231da177e4SLinus Torvalds 					mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 			}
7261da177e4SLinus Torvalds 			else if (scsi_state &
7271da177e4SLinus Torvalds 			         (MPI_SCSI_STATE_AUTOSENSE_FAILED | MPI_SCSI_STATE_NO_SCSI_STATUS)
7281da177e4SLinus Torvalds 			   ) {
7291da177e4SLinus Torvalds 				/*
7301da177e4SLinus Torvalds 				 * What to do?
7311da177e4SLinus Torvalds 				 */
7321da177e4SLinus Torvalds 				sc->result = DID_SOFT_ERROR << 16;
7331da177e4SLinus Torvalds 			}
7341da177e4SLinus Torvalds 			else if (scsi_state & MPI_SCSI_STATE_TERMINATED) {
7351da177e4SLinus Torvalds 				/*  Not real sure here either...  */
7361da177e4SLinus Torvalds 				sc->result = DID_RESET << 16;
7371da177e4SLinus Torvalds 			}
7381da177e4SLinus Torvalds 			else if (scsi_state & MPI_SCSI_STATE_QUEUE_TAG_REJECTED) {
7391da177e4SLinus Torvalds 				/* Device Inq. data indicates that it supports
7401da177e4SLinus Torvalds 				 * QTags, but rejects QTag messages.
7411da177e4SLinus Torvalds 				 * This command completed OK.
7421da177e4SLinus Torvalds 				 *
7431da177e4SLinus Torvalds 				 * Not real sure here either so do nothing...  */
7441da177e4SLinus Torvalds 			}
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 			if (sc->result == MPI_SCSI_STATUS_TASK_SET_FULL)
7471da177e4SLinus Torvalds 				mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 			/* Add handling of:
7501da177e4SLinus Torvalds 			 * Reservation Conflict, Busy,
7511da177e4SLinus Torvalds 			 * Command Terminated, CHECK
7521da177e4SLinus Torvalds 			 */
7531da177e4SLinus Torvalds 			break;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:		/* 0x0047 */
7561da177e4SLinus Torvalds 			sc->result = DID_SOFT_ERROR << 16;
7571da177e4SLinus Torvalds 			break;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 		case MPI_IOCSTATUS_INVALID_FUNCTION:		/* 0x0001 */
7601da177e4SLinus Torvalds 		case MPI_IOCSTATUS_INVALID_SGL:			/* 0x0003 */
7611da177e4SLinus Torvalds 		case MPI_IOCSTATUS_INTERNAL_ERROR:		/* 0x0004 */
7621da177e4SLinus Torvalds 		case MPI_IOCSTATUS_RESERVED:			/* 0x0005 */
7631da177e4SLinus Torvalds 		case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES:	/* 0x0006 */
7641da177e4SLinus Torvalds 		case MPI_IOCSTATUS_INVALID_FIELD:		/* 0x0007 */
7651da177e4SLinus Torvalds 		case MPI_IOCSTATUS_INVALID_STATE:		/* 0x0008 */
7661da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_DATA_OVERRUN:		/* 0x0044 */
7671da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:		/* 0x0046 */
7681da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED:	/* 0x004A */
7691da177e4SLinus Torvalds 		default:
7701da177e4SLinus Torvalds 			/*
7711da177e4SLinus Torvalds 			 * What to do?
7721da177e4SLinus Torvalds 			 */
7731da177e4SLinus Torvalds 			sc->result = DID_SOFT_ERROR << 16;
7741da177e4SLinus Torvalds 			break;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 		}	/* switch(status) */
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 		dreplyprintk((KERN_NOTICE "  sc->result is %08xh\n", sc->result));
7791da177e4SLinus Torvalds 	} /* end of address reply case */
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	/* Unmap the DMA buffers, if any. */
7821da177e4SLinus Torvalds 	if (sc->use_sg) {
7831da177e4SLinus Torvalds 		pci_unmap_sg(ioc->pcidev, (struct scatterlist *) sc->request_buffer,
7841da177e4SLinus Torvalds 			    sc->use_sg, sc->sc_data_direction);
7851da177e4SLinus Torvalds 	} else if (sc->request_bufflen) {
7861da177e4SLinus Torvalds 		pci_unmap_single(ioc->pcidev, sc->SCp.dma_handle,
7871da177e4SLinus Torvalds 				sc->request_bufflen, sc->sc_data_direction);
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	hd->ScsiLookup[req_idx] = NULL;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	sc->scsi_done(sc);		/* Issue the command callback */
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	/* Free Chain buffers */
7951da177e4SLinus Torvalds 	mptscsih_freeChainBuffers(ioc, req_idx);
7961da177e4SLinus Torvalds 	return 1;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds /*
8011da177e4SLinus Torvalds  *	mptscsih_flush_running_cmds - For each command found, search
8021da177e4SLinus Torvalds  *		Scsi_Host instance taskQ and reply to OS.
8031da177e4SLinus Torvalds  *		Called only if recovering from a FW reload.
8041da177e4SLinus Torvalds  *	@hd: Pointer to a SCSI HOST structure
8051da177e4SLinus Torvalds  *
8061da177e4SLinus Torvalds  *	Returns: None.
8071da177e4SLinus Torvalds  *
8081da177e4SLinus Torvalds  *	Must be called while new I/Os are being queued.
8091da177e4SLinus Torvalds  */
8101da177e4SLinus Torvalds static void
8111da177e4SLinus Torvalds mptscsih_flush_running_cmds(MPT_SCSI_HOST *hd)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	MPT_ADAPTER *ioc = hd->ioc;
8141da177e4SLinus Torvalds 	struct scsi_cmnd	*SCpnt;
8151da177e4SLinus Torvalds 	MPT_FRAME_HDR	*mf;
8161da177e4SLinus Torvalds 	int		 ii;
8171da177e4SLinus Torvalds 	int		 max = ioc->req_depth;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	dprintk((KERN_INFO MYNAM ": flush_ScsiLookup called\n"));
8201da177e4SLinus Torvalds 	for (ii= 0; ii < max; ii++) {
8211da177e4SLinus Torvalds 		if ((SCpnt = hd->ScsiLookup[ii]) != NULL) {
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 			/* Command found.
8241da177e4SLinus Torvalds 			 */
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 			/* Null ScsiLookup index
8271da177e4SLinus Torvalds 			 */
8281da177e4SLinus Torvalds 			hd->ScsiLookup[ii] = NULL;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 			mf = MPT_INDEX_2_MFPTR(ioc, ii);
8311da177e4SLinus Torvalds 			dmfprintk(( "flush: ScsiDone (mf=%p,sc=%p)\n",
8321da177e4SLinus Torvalds 					mf, SCpnt));
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 			/* Set status, free OS resources (SG DMA buffers)
8351da177e4SLinus Torvalds 			 * Do OS callback
8361da177e4SLinus Torvalds 			 * Free driver resources (chain, msg buffers)
8371da177e4SLinus Torvalds 			 */
8381da177e4SLinus Torvalds 			if (SCpnt->use_sg) {
8391da177e4SLinus Torvalds 				pci_unmap_sg(ioc->pcidev,
8401da177e4SLinus Torvalds 					(struct scatterlist *) SCpnt->request_buffer,
8411da177e4SLinus Torvalds 					SCpnt->use_sg,
8421da177e4SLinus Torvalds 					SCpnt->sc_data_direction);
8431da177e4SLinus Torvalds 			} else if (SCpnt->request_bufflen) {
8441da177e4SLinus Torvalds 				pci_unmap_single(ioc->pcidev,
8451da177e4SLinus Torvalds 					SCpnt->SCp.dma_handle,
8461da177e4SLinus Torvalds 					SCpnt->request_bufflen,
8471da177e4SLinus Torvalds 					SCpnt->sc_data_direction);
8481da177e4SLinus Torvalds 			}
8491da177e4SLinus Torvalds 			SCpnt->result = DID_RESET << 16;
8501da177e4SLinus Torvalds 			SCpnt->host_scribble = NULL;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 			/* Free Chain buffers */
8531da177e4SLinus Torvalds 			mptscsih_freeChainBuffers(ioc, ii);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 			/* Free Message frames */
8561da177e4SLinus Torvalds 			mpt_free_msg_frame(ioc, mf);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 			SCpnt->scsi_done(SCpnt);	/* Issue the command callback */
8591da177e4SLinus Torvalds 		}
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	return;
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds /*
8661da177e4SLinus Torvalds  *	mptscsih_search_running_cmds - Delete any commands associated
8671da177e4SLinus Torvalds  *		with the specified target and lun. Function called only
8681da177e4SLinus Torvalds  *		when a lun is disable by mid-layer.
8691da177e4SLinus Torvalds  *		Do NOT access the referenced scsi_cmnd structure or
8701da177e4SLinus Torvalds  *		members. Will cause either a paging or NULL ptr error.
8711da177e4SLinus Torvalds  *	@hd: Pointer to a SCSI HOST structure
8721da177e4SLinus Torvalds  *	@target: target id
8731da177e4SLinus Torvalds  *	@lun: lun
8741da177e4SLinus Torvalds  *
8751da177e4SLinus Torvalds  *	Returns: None.
8761da177e4SLinus Torvalds  *
8771da177e4SLinus Torvalds  *	Called from slave_destroy.
8781da177e4SLinus Torvalds  */
8791da177e4SLinus Torvalds static void
8801da177e4SLinus Torvalds mptscsih_search_running_cmds(MPT_SCSI_HOST *hd, uint target, uint lun)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	SCSIIORequest_t	*mf = NULL;
8831da177e4SLinus Torvalds 	int		 ii;
8841da177e4SLinus Torvalds 	int		 max = hd->ioc->req_depth;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	dsprintk((KERN_INFO MYNAM ": search_running target %d lun %d max %d\n",
8871da177e4SLinus Torvalds 			target, lun, max));
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	for (ii=0; ii < max; ii++) {
8901da177e4SLinus Torvalds 		if (hd->ScsiLookup[ii] != NULL) {
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 			mf = (SCSIIORequest_t *)MPT_INDEX_2_MFPTR(hd->ioc, ii);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 			dsprintk(( "search_running: found (sc=%p, mf = %p) target %d, lun %d \n",
8951da177e4SLinus Torvalds 					hd->ScsiLookup[ii], mf, mf->TargetID, mf->LUN[1]));
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 			if ((mf->TargetID != ((u8)target)) || (mf->LUN[1] != ((u8) lun)))
8981da177e4SLinus Torvalds 				continue;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 			/* Cleanup
9011da177e4SLinus Torvalds 			 */
9021da177e4SLinus Torvalds 			hd->ScsiLookup[ii] = NULL;
9031da177e4SLinus Torvalds 			mptscsih_freeChainBuffers(hd->ioc, ii);
9041da177e4SLinus Torvalds 			mpt_free_msg_frame(hd->ioc, (MPT_FRAME_HDR *)mf);
9051da177e4SLinus Torvalds 		}
9061da177e4SLinus Torvalds 	}
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	return;
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
9141da177e4SLinus Torvalds /*
9151da177e4SLinus Torvalds  *	mptscsih_report_queue_full - Report QUEUE_FULL status returned
9161da177e4SLinus Torvalds  *	from a SCSI target device.
9171da177e4SLinus Torvalds  *	@sc: Pointer to scsi_cmnd structure
9181da177e4SLinus Torvalds  *	@pScsiReply: Pointer to SCSIIOReply_t
9191da177e4SLinus Torvalds  *	@pScsiReq: Pointer to original SCSI request
9201da177e4SLinus Torvalds  *
9211da177e4SLinus Torvalds  *	This routine periodically reports QUEUE_FULL status returned from a
9221da177e4SLinus Torvalds  *	SCSI target device.  It reports this to the console via kernel
9231da177e4SLinus Torvalds  *	printk() API call, not more than once every 10 seconds.
9241da177e4SLinus Torvalds  */
9251da177e4SLinus Torvalds static void
9261da177e4SLinus Torvalds mptscsih_report_queue_full(struct scsi_cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq)
9271da177e4SLinus Torvalds {
9281da177e4SLinus Torvalds 	long time = jiffies;
9291da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd;
9301da177e4SLinus Torvalds 
9310d0c7974SMoore, Eric Dean  	if (sc->device == NULL)
9320d0c7974SMoore, Eric Dean  		return;
9330d0c7974SMoore, Eric Dean  	if (sc->device->host == NULL)
9340d0c7974SMoore, Eric Dean  		return;
9350d0c7974SMoore, Eric Dean  	if ((hd = (MPT_SCSI_HOST *)sc->device->host->hostdata) == NULL)
9360d0c7974SMoore, Eric Dean  		return;
9371da177e4SLinus Torvalds 
9380d0c7974SMoore, Eric Dean  	if (time - hd->last_queue_full > 10 * HZ) {
9390d0c7974SMoore, Eric Dean  		dprintk((MYIOC_s_WARN_FMT "Device (%d:%d:%d) reported QUEUE_FULL!\n",
9400d0c7974SMoore, Eric Dean  				hd->ioc->name, 0, sc->device->id, sc->device->lun));
9410d0c7974SMoore, Eric Dean  		hd->last_queue_full = time;
9421da177e4SLinus Torvalds 	}
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
9461da177e4SLinus Torvalds /*
9471da177e4SLinus Torvalds  *	mptscsih_remove - Removed scsi devices
9481da177e4SLinus Torvalds  *	@pdev: Pointer to pci_dev structure
9491da177e4SLinus Torvalds  *
9501da177e4SLinus Torvalds  *
9511da177e4SLinus Torvalds  */
9520d0c7974SMoore, Eric Dean  void
9531da177e4SLinus Torvalds mptscsih_remove(struct pci_dev *pdev)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds 	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
9561da177e4SLinus Torvalds 	struct Scsi_Host 	*host = ioc->sh;
9571da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd;
9581da177e4SLinus Torvalds 	int 		 	count;
9591da177e4SLinus Torvalds 	unsigned long	 	flags;
9600d0c7974SMoore, Eric Dean  	int sz1;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	if(!host)
9631da177e4SLinus Torvalds 		return;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	scsi_remove_host(host);
9661da177e4SLinus Torvalds 
9670d0c7974SMoore, Eric Dean  	if((hd = (MPT_SCSI_HOST *)host->hostdata) == NULL)
9680d0c7974SMoore, Eric Dean  		return;
9690d0c7974SMoore, Eric Dean  
9701da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
9711da177e4SLinus Torvalds 	/* Check DV thread active */
9721da177e4SLinus Torvalds 	count = 10 * HZ;
9731da177e4SLinus Torvalds 	spin_lock_irqsave(&dvtaskQ_lock, flags);
9741da177e4SLinus Torvalds 	if (dvtaskQ_active) {
9751da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dvtaskQ_lock, flags);
9761da177e4SLinus Torvalds 		while(dvtaskQ_active && --count) {
9771da177e4SLinus Torvalds 			set_current_state(TASK_INTERRUPTIBLE);
9781da177e4SLinus Torvalds 			schedule_timeout(1);
9791da177e4SLinus Torvalds 		}
9801da177e4SLinus Torvalds 	} else {
9811da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dvtaskQ_lock, flags);
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 	if (!count)
9841da177e4SLinus Torvalds 		printk(KERN_ERR MYNAM ": ERROR - DV thread still active!\n");
9851da177e4SLinus Torvalds #if defined(MPT_DEBUG_DV) || defined(MPT_DEBUG_DV_TINY)
9861da177e4SLinus Torvalds 	else
9871da177e4SLinus Torvalds 		printk(KERN_ERR MYNAM ": DV thread orig %d, count %d\n", 10 * HZ, count);
9881da177e4SLinus Torvalds #endif
9891da177e4SLinus Torvalds #endif
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	mptscsih_shutdown(&pdev->dev);
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	sz1=0;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	if (hd->ScsiLookup != NULL) {
9961da177e4SLinus Torvalds 		sz1 = hd->ioc->req_depth * sizeof(void *);
9971da177e4SLinus Torvalds 		kfree(hd->ScsiLookup);
9981da177e4SLinus Torvalds 		hd->ScsiLookup = NULL;
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	/*
10021da177e4SLinus Torvalds 	 * Free pointer array.
10031da177e4SLinus Torvalds 	 */
10041da177e4SLinus Torvalds 	kfree(hd->Targets);
10051da177e4SLinus Torvalds 	hd->Targets = NULL;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	dprintk((MYIOC_s_INFO_FMT
10081da177e4SLinus Torvalds 	    "Free'd ScsiLookup (%d) memory\n",
10091da177e4SLinus Torvalds 	    hd->ioc->name, sz1));
10101da177e4SLinus Torvalds 
10110d0c7974SMoore, Eric Dean  	kfree(hd->info_kbuf);
10120d0c7974SMoore, Eric Dean  
10131da177e4SLinus Torvalds 	/* NULL the Scsi_Host pointer
10141da177e4SLinus Torvalds 	 */
10151da177e4SLinus Torvalds 	hd->ioc->sh = NULL;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	scsi_host_put(host);
10180d0c7974SMoore, Eric Dean  
10190d0c7974SMoore, Eric Dean  	mpt_detach(pdev);
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
10241da177e4SLinus Torvalds /*
10251da177e4SLinus Torvalds  *	mptscsih_shutdown - reboot notifier
10261da177e4SLinus Torvalds  *
10271da177e4SLinus Torvalds  */
10280d0c7974SMoore, Eric Dean  void
10291da177e4SLinus Torvalds mptscsih_shutdown(struct device * dev)
10301da177e4SLinus Torvalds {
10311da177e4SLinus Torvalds 	MPT_ADAPTER 		*ioc = pci_get_drvdata(to_pci_dev(dev));
10321da177e4SLinus Torvalds 	struct Scsi_Host 	*host = ioc->sh;
10331da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	if(!host)
10361da177e4SLinus Torvalds 		return;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	hd = (MPT_SCSI_HOST *)host->hostdata;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	/* Flush the cache of this adapter
10411da177e4SLinus Torvalds 	 */
10421da177e4SLinus Torvalds 	if(hd != NULL)
10431da177e4SLinus Torvalds 		mptscsih_synchronize_cache(hd, 0);
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds #ifdef CONFIG_PM
10481da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
10491da177e4SLinus Torvalds /*
10500d0c7974SMoore, Eric Dean   *	mptscsih_suspend - Fusion MPT scsi driver suspend routine.
10511da177e4SLinus Torvalds  *
10521da177e4SLinus Torvalds  *
10531da177e4SLinus Torvalds  */
10540d0c7974SMoore, Eric Dean  int
10558d189f72SPavel Machek mptscsih_suspend(struct pci_dev *pdev, pm_message_t state)
10561da177e4SLinus Torvalds {
10571da177e4SLinus Torvalds 	mptscsih_shutdown(&pdev->dev);
10580d0c7974SMoore, Eric Dean  	return mpt_suspend(pdev,state);
10591da177e4SLinus Torvalds }
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
10621da177e4SLinus Torvalds /*
10631da177e4SLinus Torvalds  *	mptscsih_resume - Fusion MPT scsi driver resume routine.
10641da177e4SLinus Torvalds  *
10651da177e4SLinus Torvalds  *
10661da177e4SLinus Torvalds  */
10670d0c7974SMoore, Eric Dean  int
10681da177e4SLinus Torvalds mptscsih_resume(struct pci_dev *pdev)
10691da177e4SLinus Torvalds {
10701da177e4SLinus Torvalds 	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
10711da177e4SLinus Torvalds 	struct Scsi_Host 	*host = ioc->sh;
10721da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd;
10731da177e4SLinus Torvalds 
10740d0c7974SMoore, Eric Dean  	mpt_resume(pdev);
10750d0c7974SMoore, Eric Dean  
10761da177e4SLinus Torvalds 	if(!host)
10771da177e4SLinus Torvalds 		return 0;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	hd = (MPT_SCSI_HOST *)host->hostdata;
10801da177e4SLinus Torvalds 	if(!hd)
10811da177e4SLinus Torvalds 		return 0;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
10841da177e4SLinus Torvalds 	{
10851da177e4SLinus Torvalds 	unsigned long lflags;
10861da177e4SLinus Torvalds 	spin_lock_irqsave(&dvtaskQ_lock, lflags);
10871da177e4SLinus Torvalds 	if (!dvtaskQ_active) {
10881da177e4SLinus Torvalds 		dvtaskQ_active = 1;
10891da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
10900d0c7974SMoore, Eric Dean  		INIT_WORK(&dvTaskQ_task,
10911da177e4SLinus Torvalds 		  mptscsih_domainValidation, (void *) hd);
10920d0c7974SMoore, Eric Dean  		schedule_work(&dvTaskQ_task);
10931da177e4SLinus Torvalds 	} else {
10941da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
10951da177e4SLinus Torvalds 	}
10961da177e4SLinus Torvalds 	}
10971da177e4SLinus Torvalds #endif
10981da177e4SLinus Torvalds 	return 0;
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds #endif
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
11041da177e4SLinus Torvalds /**
11051da177e4SLinus Torvalds  *	mptscsih_info - Return information about MPT adapter
11061da177e4SLinus Torvalds  *	@SChost: Pointer to Scsi_Host structure
11071da177e4SLinus Torvalds  *
11081da177e4SLinus Torvalds  *	(linux scsi_host_template.info routine)
11091da177e4SLinus Torvalds  *
11101da177e4SLinus Torvalds  *	Returns pointer to buffer where information was written.
11111da177e4SLinus Torvalds  */
11120d0c7974SMoore, Eric Dean  const char *
11131da177e4SLinus Torvalds mptscsih_info(struct Scsi_Host *SChost)
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 	MPT_SCSI_HOST *h;
11161da177e4SLinus Torvalds 	int size = 0;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	h = (MPT_SCSI_HOST *)SChost->hostdata;
11190d0c7974SMoore, Eric Dean  
11201da177e4SLinus Torvalds 	if (h) {
11210d0c7974SMoore, Eric Dean  		if (h->info_kbuf == NULL)
11220d0c7974SMoore, Eric Dean  			if ((h->info_kbuf = kmalloc(0x1000 /* 4Kb */, GFP_KERNEL)) == NULL)
11230d0c7974SMoore, Eric Dean  				return h->info_kbuf;
11240d0c7974SMoore, Eric Dean  		h->info_kbuf[0] = '\0';
11250d0c7974SMoore, Eric Dean  
11260d0c7974SMoore, Eric Dean  		mpt_print_ioc_summary(h->ioc, h->info_kbuf, &size, 0, 0);
11270d0c7974SMoore, Eric Dean  		h->info_kbuf[size-1] = '\0';
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 
11300d0c7974SMoore, Eric Dean  	return h->info_kbuf;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds struct info_str {
11341da177e4SLinus Torvalds 	char *buffer;
11351da177e4SLinus Torvalds 	int   length;
11361da177e4SLinus Torvalds 	int   offset;
11371da177e4SLinus Torvalds 	int   pos;
11381da177e4SLinus Torvalds };
11391da177e4SLinus Torvalds 
11400d0c7974SMoore, Eric Dean  static void
11410d0c7974SMoore, Eric Dean  mptscsih_copy_mem_info(struct info_str *info, char *data, int len)
11421da177e4SLinus Torvalds {
11431da177e4SLinus Torvalds 	if (info->pos + len > info->length)
11441da177e4SLinus Torvalds 		len = info->length - info->pos;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	if (info->pos + len < info->offset) {
11471da177e4SLinus Torvalds 		info->pos += len;
11481da177e4SLinus Torvalds 		return;
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	if (info->pos < info->offset) {
11521da177e4SLinus Torvalds 	        data += (info->offset - info->pos);
11531da177e4SLinus Torvalds 	        len  -= (info->offset - info->pos);
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	if (len > 0) {
11571da177e4SLinus Torvalds                 memcpy(info->buffer + info->pos, data, len);
11581da177e4SLinus Torvalds                 info->pos += len;
11591da177e4SLinus Torvalds 	}
11601da177e4SLinus Torvalds }
11611da177e4SLinus Torvalds 
11620d0c7974SMoore, Eric Dean  static int
11630d0c7974SMoore, Eric Dean  mptscsih_copy_info(struct info_str *info, char *fmt, ...)
11641da177e4SLinus Torvalds {
11651da177e4SLinus Torvalds 	va_list args;
11661da177e4SLinus Torvalds 	char buf[81];
11671da177e4SLinus Torvalds 	int len;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	va_start(args, fmt);
11701da177e4SLinus Torvalds 	len = vsprintf(buf, fmt, args);
11711da177e4SLinus Torvalds 	va_end(args);
11721da177e4SLinus Torvalds 
11730d0c7974SMoore, Eric Dean  	mptscsih_copy_mem_info(info, buf, len);
11741da177e4SLinus Torvalds 	return len;
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
11770d0c7974SMoore, Eric Dean  static int
11780d0c7974SMoore, Eric Dean  mptscsih_host_info(MPT_ADAPTER *ioc, char *pbuf, off_t offset, int len)
11791da177e4SLinus Torvalds {
11801da177e4SLinus Torvalds 	struct info_str info;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	info.buffer	= pbuf;
11831da177e4SLinus Torvalds 	info.length	= len;
11841da177e4SLinus Torvalds 	info.offset	= offset;
11851da177e4SLinus Torvalds 	info.pos	= 0;
11861da177e4SLinus Torvalds 
11870d0c7974SMoore, Eric Dean  	mptscsih_copy_info(&info, "%s: %s, ", ioc->name, ioc->prod_name);
11880d0c7974SMoore, Eric Dean  	mptscsih_copy_info(&info, "%s%08xh, ", MPT_FW_REV_MAGIC_ID_STRING, ioc->facts.FWVersion.Word);
11890d0c7974SMoore, Eric Dean  	mptscsih_copy_info(&info, "Ports=%d, ", ioc->facts.NumberOfPorts);
11900d0c7974SMoore, Eric Dean  	mptscsih_copy_info(&info, "MaxQ=%d\n", ioc->req_depth);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	return ((info.pos > info.offset) ? info.pos - info.offset : 0);
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
11961da177e4SLinus Torvalds /**
11971da177e4SLinus Torvalds  *	mptscsih_proc_info - Return information about MPT adapter
11981da177e4SLinus Torvalds  *
11991da177e4SLinus Torvalds  *	(linux scsi_host_template.info routine)
12001da177e4SLinus Torvalds  *
12011da177e4SLinus Torvalds  * 	buffer: if write, user data; if read, buffer for user
12021da177e4SLinus Torvalds  * 	length: if write, return length;
12031da177e4SLinus Torvalds  * 	offset: if write, 0; if read, the current offset into the buffer from
12041da177e4SLinus Torvalds  * 		the previous read.
12051da177e4SLinus Torvalds  * 	hostno: scsi host number
12061da177e4SLinus Torvalds  *	func:   if write = 1; if read = 0
12071da177e4SLinus Torvalds  */
12080d0c7974SMoore, Eric Dean  int
12091da177e4SLinus Torvalds mptscsih_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
12101da177e4SLinus Torvalds 			int length, int func)
12111da177e4SLinus Torvalds {
12121da177e4SLinus Torvalds 	MPT_SCSI_HOST	*hd = (MPT_SCSI_HOST *)host->hostdata;
12131da177e4SLinus Torvalds 	MPT_ADAPTER	*ioc = hd->ioc;
12141da177e4SLinus Torvalds 	int size = 0;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	if (func) {
12171da177e4SLinus Torvalds 		/*
12181da177e4SLinus Torvalds 		 * write is not supported
12191da177e4SLinus Torvalds 		 */
12201da177e4SLinus Torvalds 	} else {
12211da177e4SLinus Torvalds 		if (start)
12221da177e4SLinus Torvalds 			*start = buffer;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 		size = mptscsih_host_info(ioc, buffer, offset, length);
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	return size;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
12311da177e4SLinus Torvalds #define ADD_INDEX_LOG(req_ent)	do { } while(0)
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
12341da177e4SLinus Torvalds /**
12351da177e4SLinus Torvalds  *	mptscsih_qcmd - Primary Fusion MPT SCSI initiator IO start routine.
12361da177e4SLinus Torvalds  *	@SCpnt: Pointer to scsi_cmnd structure
12371da177e4SLinus Torvalds  *	@done: Pointer SCSI mid-layer IO completion function
12381da177e4SLinus Torvalds  *
12391da177e4SLinus Torvalds  *	(linux scsi_host_template.queuecommand routine)
12401da177e4SLinus Torvalds  *	This is the primary SCSI IO start routine.  Create a MPI SCSIIORequest
12411da177e4SLinus Torvalds  *	from a linux scsi_cmnd request and send it to the IOC.
12421da177e4SLinus Torvalds  *
12431da177e4SLinus Torvalds  *	Returns 0. (rtn value discarded by linux scsi mid-layer)
12441da177e4SLinus Torvalds  */
12450d0c7974SMoore, Eric Dean  int
12461da177e4SLinus Torvalds mptscsih_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
12471da177e4SLinus Torvalds {
12481da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd;
12491da177e4SLinus Torvalds 	MPT_FRAME_HDR		*mf;
12501da177e4SLinus Torvalds 	SCSIIORequest_t		*pScsiReq;
12511da177e4SLinus Torvalds 	VirtDevice		*pTarget;
12521da177e4SLinus Torvalds 	int	 target;
12531da177e4SLinus Torvalds 	int	 lun;
12541da177e4SLinus Torvalds 	u32	 datalen;
12551da177e4SLinus Torvalds 	u32	 scsictl;
12561da177e4SLinus Torvalds 	u32	 scsidir;
12571da177e4SLinus Torvalds 	u32	 cmd_len;
12581da177e4SLinus Torvalds 	int	 my_idx;
12591da177e4SLinus Torvalds 	int	 ii;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata;
12621da177e4SLinus Torvalds 	target = SCpnt->device->id;
12631da177e4SLinus Torvalds 	lun = SCpnt->device->lun;
12641da177e4SLinus Torvalds 	SCpnt->scsi_done = done;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	pTarget = hd->Targets[target];
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	dmfprintk((MYIOC_s_INFO_FMT "qcmd: SCpnt=%p, done()=%p\n",
12691da177e4SLinus Torvalds 			(hd && hd->ioc) ? hd->ioc->name : "ioc?", SCpnt, done));
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	if (hd->resetPending) {
12721da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "qcmd: SCpnt=%p timeout + 60HZ\n",
12731da177e4SLinus Torvalds 			(hd && hd->ioc) ? hd->ioc->name : "ioc?", SCpnt));
12741da177e4SLinus Torvalds 		return SCSI_MLQUEUE_HOST_BUSY;
12751da177e4SLinus Torvalds 	}
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 	/*
12781da177e4SLinus Torvalds 	 *  Put together a MPT SCSI request...
12791da177e4SLinus Torvalds 	 */
12800d0c7974SMoore, Eric Dean  	if ((mf = mpt_get_msg_frame(hd->ioc->DoneCtx, hd->ioc)) == NULL) {
12811da177e4SLinus Torvalds 		dprintk((MYIOC_s_WARN_FMT "QueueCmd, no msg frames!!\n",
12821da177e4SLinus Torvalds 				hd->ioc->name));
12831da177e4SLinus Torvalds 		return SCSI_MLQUEUE_HOST_BUSY;
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	pScsiReq = (SCSIIORequest_t *) mf;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	ADD_INDEX_LOG(my_idx);
12911da177e4SLinus Torvalds 
12920d0c7974SMoore, Eric Dean  	/*    TUR's being issued with scsictl=0x02000000 (DATA_IN)!
12931da177e4SLinus Torvalds 	 *    Seems we may receive a buffer (datalen>0) even when there
12941da177e4SLinus Torvalds 	 *    will be no data transfer!  GRRRRR...
12951da177e4SLinus Torvalds 	 */
12961da177e4SLinus Torvalds 	if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
12971da177e4SLinus Torvalds 		datalen = SCpnt->request_bufflen;
12981da177e4SLinus Torvalds 		scsidir = MPI_SCSIIO_CONTROL_READ;	/* DATA IN  (host<--ioc<--dev) */
12991da177e4SLinus Torvalds 	} else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
13001da177e4SLinus Torvalds 		datalen = SCpnt->request_bufflen;
13011da177e4SLinus Torvalds 		scsidir = MPI_SCSIIO_CONTROL_WRITE;	/* DATA OUT (host-->ioc-->dev) */
13021da177e4SLinus Torvalds 	} else {
13031da177e4SLinus Torvalds 		datalen = 0;
13041da177e4SLinus Torvalds 		scsidir = MPI_SCSIIO_CONTROL_NODATATRANSFER;
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	/* Default to untagged. Once a target structure has been allocated,
13081da177e4SLinus Torvalds 	 * use the Inquiry data to determine if device supports tagged.
13091da177e4SLinus Torvalds 	 */
13101da177e4SLinus Torvalds 	if (   pTarget
13111da177e4SLinus Torvalds 	    && (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)
13121da177e4SLinus Torvalds 	    && (SCpnt->device->tagged_supported)) {
13131da177e4SLinus Torvalds 		scsictl = scsidir | MPI_SCSIIO_CONTROL_SIMPLEQ;
13141da177e4SLinus Torvalds 	} else {
13151da177e4SLinus Torvalds 		scsictl = scsidir | MPI_SCSIIO_CONTROL_UNTAGGED;
13161da177e4SLinus Torvalds 	}
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	/* Use the above information to set up the message frame
13191da177e4SLinus Torvalds 	 */
13201da177e4SLinus Torvalds 	pScsiReq->TargetID = (u8) target;
13211da177e4SLinus Torvalds 	pScsiReq->Bus = (u8) SCpnt->device->channel;
13221da177e4SLinus Torvalds 	pScsiReq->ChainOffset = 0;
13231da177e4SLinus Torvalds 	pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
13241da177e4SLinus Torvalds 	pScsiReq->CDBLength = SCpnt->cmd_len;
13251da177e4SLinus Torvalds 	pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
13261da177e4SLinus Torvalds 	pScsiReq->Reserved = 0;
13271da177e4SLinus Torvalds 	pScsiReq->MsgFlags = mpt_msg_flags();
13281da177e4SLinus Torvalds 	pScsiReq->LUN[0] = 0;
13291da177e4SLinus Torvalds 	pScsiReq->LUN[1] = lun;
13301da177e4SLinus Torvalds 	pScsiReq->LUN[2] = 0;
13311da177e4SLinus Torvalds 	pScsiReq->LUN[3] = 0;
13321da177e4SLinus Torvalds 	pScsiReq->LUN[4] = 0;
13331da177e4SLinus Torvalds 	pScsiReq->LUN[5] = 0;
13341da177e4SLinus Torvalds 	pScsiReq->LUN[6] = 0;
13351da177e4SLinus Torvalds 	pScsiReq->LUN[7] = 0;
13361da177e4SLinus Torvalds 	pScsiReq->Control = cpu_to_le32(scsictl);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	/*
13391da177e4SLinus Torvalds 	 *  Write SCSI CDB into the message
13401da177e4SLinus Torvalds 	 */
13411da177e4SLinus Torvalds 	cmd_len = SCpnt->cmd_len;
13421da177e4SLinus Torvalds 	for (ii=0; ii < cmd_len; ii++)
13431da177e4SLinus Torvalds 		pScsiReq->CDB[ii] = SCpnt->cmnd[ii];
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	for (ii=cmd_len; ii < 16; ii++)
13461da177e4SLinus Torvalds 		pScsiReq->CDB[ii] = 0;
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	/* DataLength */
13491da177e4SLinus Torvalds 	pScsiReq->DataLength = cpu_to_le32(datalen);
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	/* SenseBuffer low address */
13521da177e4SLinus Torvalds 	pScsiReq->SenseBufferLowAddr = cpu_to_le32(hd->ioc->sense_buf_low_dma
13531da177e4SLinus Torvalds 					   + (my_idx * MPT_SENSE_BUFFER_ALLOC));
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	/* Now add the SG list
13561da177e4SLinus Torvalds 	 * Always have a SGE even if null length.
13571da177e4SLinus Torvalds 	 */
13581da177e4SLinus Torvalds 	if (datalen == 0) {
13591da177e4SLinus Torvalds 		/* Add a NULL SGE */
13601da177e4SLinus Torvalds 		mptscsih_add_sge((char *)&pScsiReq->SGL, MPT_SGE_FLAGS_SSIMPLE_READ | 0,
13611da177e4SLinus Torvalds 			(dma_addr_t) -1);
13621da177e4SLinus Torvalds 	} else {
13631da177e4SLinus Torvalds 		/* Add a 32 or 64 bit SGE */
13641da177e4SLinus Torvalds 		if (mptscsih_AddSGE(hd->ioc, SCpnt, pScsiReq, my_idx) != SUCCESS)
13651da177e4SLinus Torvalds 			goto fail;
13661da177e4SLinus Torvalds 	}
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	hd->ScsiLookup[my_idx] = SCpnt;
13691da177e4SLinus Torvalds 	SCpnt->host_scribble = NULL;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
13721da177e4SLinus Torvalds 	if (hd->ioc->bus_type == SCSI) {
13731da177e4SLinus Torvalds 		int dvStatus = hd->ioc->spi_data.dvStatus[target];
13741da177e4SLinus Torvalds 		int issueCmd = 1;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 		if (dvStatus || hd->ioc->spi_data.forceDv) {
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 			if ((dvStatus & MPT_SCSICFG_NEED_DV) ||
13791da177e4SLinus Torvalds 				(hd->ioc->spi_data.forceDv & MPT_SCSICFG_NEED_DV)) {
13801da177e4SLinus Torvalds 				unsigned long lflags;
13811da177e4SLinus Torvalds 				/* Schedule DV if necessary */
13821da177e4SLinus Torvalds 				spin_lock_irqsave(&dvtaskQ_lock, lflags);
13831da177e4SLinus Torvalds 				if (!dvtaskQ_active) {
13841da177e4SLinus Torvalds 					dvtaskQ_active = 1;
13851da177e4SLinus Torvalds 					spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
13860d0c7974SMoore, Eric Dean  					INIT_WORK(&dvTaskQ_task, mptscsih_domainValidation, (void *) hd);
13871da177e4SLinus Torvalds 
13880d0c7974SMoore, Eric Dean  					schedule_work(&dvTaskQ_task);
13891da177e4SLinus Torvalds 				} else {
13901da177e4SLinus Torvalds 					spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
13911da177e4SLinus Torvalds 				}
13921da177e4SLinus Torvalds 				hd->ioc->spi_data.forceDv &= ~MPT_SCSICFG_NEED_DV;
13931da177e4SLinus Torvalds 			}
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 			/* Trying to do DV to this target, extend timeout.
13961da177e4SLinus Torvalds 			 * Wait to issue until flag is clear
13971da177e4SLinus Torvalds 			 */
13981da177e4SLinus Torvalds 			if (dvStatus & MPT_SCSICFG_DV_PENDING) {
13991da177e4SLinus Torvalds 				mod_timer(&SCpnt->eh_timeout, jiffies + 40 * HZ);
14001da177e4SLinus Torvalds 				issueCmd = 0;
14011da177e4SLinus Torvalds 			}
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 			/* Set the DV flags.
14041da177e4SLinus Torvalds 			 */
14051da177e4SLinus Torvalds 			if (dvStatus & MPT_SCSICFG_DV_NOT_DONE)
14061da177e4SLinus Torvalds 				mptscsih_set_dvflags(hd, pScsiReq);
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 			if (!issueCmd)
14091da177e4SLinus Torvalds 				goto fail;
14101da177e4SLinus Torvalds 		}
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds #endif
14131da177e4SLinus Torvalds 
14140d0c7974SMoore, Eric Dean  	mpt_put_msg_frame(hd->ioc->DoneCtx, hd->ioc, mf);
14151da177e4SLinus Torvalds 	dmfprintk((MYIOC_s_INFO_FMT "Issued SCSI cmd (%p) mf=%p idx=%d\n",
14161da177e4SLinus Torvalds 			hd->ioc->name, SCpnt, mf, my_idx));
14171da177e4SLinus Torvalds 	DBG_DUMP_REQUEST_FRAME(mf)
14181da177e4SLinus Torvalds 	return 0;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds  fail:
14211da177e4SLinus Torvalds 	mptscsih_freeChainBuffers(hd->ioc, my_idx);
14221da177e4SLinus Torvalds 	mpt_free_msg_frame(hd->ioc, mf);
14231da177e4SLinus Torvalds 	return SCSI_MLQUEUE_HOST_BUSY;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14271da177e4SLinus Torvalds /*
14281da177e4SLinus Torvalds  *	mptscsih_freeChainBuffers - Function to free chain buffers associated
14291da177e4SLinus Torvalds  *	with a SCSI IO request
14301da177e4SLinus Torvalds  *	@hd: Pointer to the MPT_SCSI_HOST instance
14311da177e4SLinus Torvalds  *	@req_idx: Index of the SCSI IO request frame.
14321da177e4SLinus Torvalds  *
14331da177e4SLinus Torvalds  *	Called if SG chain buffer allocation fails and mptscsih callbacks.
14341da177e4SLinus Torvalds  *	No return.
14351da177e4SLinus Torvalds  */
14361da177e4SLinus Torvalds static void
14371da177e4SLinus Torvalds mptscsih_freeChainBuffers(MPT_ADAPTER *ioc, int req_idx)
14381da177e4SLinus Torvalds {
14391da177e4SLinus Torvalds 	MPT_FRAME_HDR *chain;
14401da177e4SLinus Torvalds 	unsigned long flags;
14411da177e4SLinus Torvalds 	int chain_idx;
14421da177e4SLinus Torvalds 	int next;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	/* Get the first chain index and reset
14451da177e4SLinus Torvalds 	 * tracker state.
14461da177e4SLinus Torvalds 	 */
14471da177e4SLinus Torvalds 	chain_idx = ioc->ReqToChain[req_idx];
14481da177e4SLinus Torvalds 	ioc->ReqToChain[req_idx] = MPT_HOST_NO_CHAIN;
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	while (chain_idx != MPT_HOST_NO_CHAIN) {
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 		/* Save the next chain buffer index */
14531da177e4SLinus Torvalds 		next = ioc->ChainToChain[chain_idx];
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 		/* Free this chain buffer and reset
14561da177e4SLinus Torvalds 		 * tracker
14571da177e4SLinus Torvalds 		 */
14581da177e4SLinus Torvalds 		ioc->ChainToChain[chain_idx] = MPT_HOST_NO_CHAIN;
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 		chain = (MPT_FRAME_HDR *) (ioc->ChainBuffer
14611da177e4SLinus Torvalds 					+ (chain_idx * ioc->req_sz));
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 		spin_lock_irqsave(&ioc->FreeQlock, flags);
14641da177e4SLinus Torvalds 		list_add_tail(&chain->u.frame.linkage.list, &ioc->FreeChainQ);
14651da177e4SLinus Torvalds 		spin_unlock_irqrestore(&ioc->FreeQlock, flags);
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 		dmfprintk((MYIOC_s_INFO_FMT "FreeChainBuffers (index %d)\n",
14681da177e4SLinus Torvalds 				ioc->name, chain_idx));
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 		/* handle next */
14711da177e4SLinus Torvalds 		chain_idx = next;
14721da177e4SLinus Torvalds 	}
14731da177e4SLinus Torvalds 	return;
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14771da177e4SLinus Torvalds /*
14781da177e4SLinus Torvalds  *	Reset Handling
14791da177e4SLinus Torvalds  */
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14821da177e4SLinus Torvalds /*
14831da177e4SLinus Torvalds  *	mptscsih_TMHandler - Generic handler for SCSI Task Management.
14841da177e4SLinus Torvalds  *	Fall through to mpt_HardResetHandler if: not operational, too many
14851da177e4SLinus Torvalds  *	failed TM requests or handshake failure.
14861da177e4SLinus Torvalds  *
14871da177e4SLinus Torvalds  *	@ioc: Pointer to MPT_ADAPTER structure
14881da177e4SLinus Torvalds  *	@type: Task Management type
14891da177e4SLinus Torvalds  *	@target: Logical Target ID for reset (if appropriate)
14901da177e4SLinus Torvalds  *	@lun: Logical Unit for reset (if appropriate)
14911da177e4SLinus Torvalds  *	@ctx2abort: Context for the task to be aborted (if appropriate)
14921da177e4SLinus Torvalds  *
14931da177e4SLinus Torvalds  *	Remark: Currently invoked from a non-interrupt thread (_bh).
14941da177e4SLinus Torvalds  *
14951da177e4SLinus Torvalds  *	Remark: With old EH code, at most 1 SCSI TaskMgmt function per IOC
14961da177e4SLinus Torvalds  *	will be active.
14971da177e4SLinus Torvalds  *
14981da177e4SLinus Torvalds  *	Returns 0 for SUCCESS or -1 if FAILED.
14991da177e4SLinus Torvalds  */
15001da177e4SLinus Torvalds static int
15011da177e4SLinus Torvalds mptscsih_TMHandler(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout)
15021da177e4SLinus Torvalds {
15031da177e4SLinus Torvalds 	MPT_ADAPTER	*ioc;
15041da177e4SLinus Torvalds 	int		 rc = -1;
15051da177e4SLinus Torvalds 	int		 doTask = 1;
15061da177e4SLinus Torvalds 	u32		 ioc_raw_state;
15071da177e4SLinus Torvalds 	unsigned long	 flags;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	/* If FW is being reloaded currently, return success to
15101da177e4SLinus Torvalds 	 * the calling function.
15111da177e4SLinus Torvalds 	 */
15121da177e4SLinus Torvalds 	if (hd == NULL)
15131da177e4SLinus Torvalds 		return 0;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	ioc = hd->ioc;
15161da177e4SLinus Torvalds 	if (ioc == NULL) {
15171da177e4SLinus Torvalds 		printk(KERN_ERR MYNAM " TMHandler" " NULL ioc!\n");
15181da177e4SLinus Torvalds 		return FAILED;
15191da177e4SLinus Torvalds 	}
15201da177e4SLinus Torvalds 	dtmprintk((MYIOC_s_INFO_FMT "TMHandler Entered!\n", ioc->name));
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	// SJR - CHECKME - Can we avoid this here?
15231da177e4SLinus Torvalds 	// (mpt_HardResetHandler has this check...)
15241da177e4SLinus Torvalds 	spin_lock_irqsave(&ioc->diagLock, flags);
15251da177e4SLinus Torvalds 	if ((ioc->diagPending) || (ioc->alt_ioc && ioc->alt_ioc->diagPending)) {
15261da177e4SLinus Torvalds 		spin_unlock_irqrestore(&ioc->diagLock, flags);
15271da177e4SLinus Torvalds 		return FAILED;
15281da177e4SLinus Torvalds 	}
15291da177e4SLinus Torvalds 	spin_unlock_irqrestore(&ioc->diagLock, flags);
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	/*  Wait a fixed amount of time for the TM pending flag to be cleared.
15321da177e4SLinus Torvalds 	 *  If we time out and not bus reset, then we return a FAILED status to the caller.
15331da177e4SLinus Torvalds 	 *  The call to mptscsih_tm_pending_wait() will set the pending flag if we are
15341da177e4SLinus Torvalds 	 *  successful. Otherwise, reload the FW.
15351da177e4SLinus Torvalds 	 */
15361da177e4SLinus Torvalds 	if (mptscsih_tm_pending_wait(hd) == FAILED) {
15371da177e4SLinus Torvalds 		if (type == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
15381da177e4SLinus Torvalds 			dtmprintk((KERN_WARNING MYNAM ": %s: TMHandler abort: "
15391da177e4SLinus Torvalds 			   "Timed out waiting for last TM (%d) to complete! \n",
15401da177e4SLinus Torvalds 			   hd->ioc->name, hd->tmPending));
15411da177e4SLinus Torvalds 			return FAILED;
15421da177e4SLinus Torvalds 		} else if (type == MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
15431da177e4SLinus Torvalds 			dtmprintk((KERN_WARNING MYNAM ": %s: TMHandler target reset: "
15441da177e4SLinus Torvalds 			   "Timed out waiting for last TM (%d) to complete! \n",
15451da177e4SLinus Torvalds 			   hd->ioc->name, hd->tmPending));
15461da177e4SLinus Torvalds 			return FAILED;
15471da177e4SLinus Torvalds 		} else if (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) {
15481da177e4SLinus Torvalds 			dtmprintk((KERN_WARNING MYNAM ": %s: TMHandler bus reset: "
15491da177e4SLinus Torvalds 			   "Timed out waiting for last TM (%d) to complete! \n",
15501da177e4SLinus Torvalds 			   hd->ioc->name, hd->tmPending));
15511da177e4SLinus Torvalds 			if (hd->tmPending & (1 << MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS))
15521da177e4SLinus Torvalds 				return FAILED;
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 			doTask = 0;
15551da177e4SLinus Torvalds 		}
15561da177e4SLinus Torvalds 	} else {
15571da177e4SLinus Torvalds 		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
15581da177e4SLinus Torvalds 		hd->tmPending |=  (1 << type);
15591da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
15601da177e4SLinus Torvalds 	}
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	/* Is operational?
15631da177e4SLinus Torvalds 	 */
15641da177e4SLinus Torvalds 	ioc_raw_state = mpt_GetIocState(hd->ioc, 0);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds #ifdef MPT_DEBUG_RESET
15671da177e4SLinus Torvalds 	if ((ioc_raw_state & MPI_IOC_STATE_MASK) != MPI_IOC_STATE_OPERATIONAL) {
15681da177e4SLinus Torvalds 		printk(MYIOC_s_WARN_FMT
15691da177e4SLinus Torvalds 			"TM Handler: IOC Not operational(0x%x)!\n",
15701da177e4SLinus Torvalds 			hd->ioc->name, ioc_raw_state);
15711da177e4SLinus Torvalds 	}
15721da177e4SLinus Torvalds #endif
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	if (doTask && ((ioc_raw_state & MPI_IOC_STATE_MASK) == MPI_IOC_STATE_OPERATIONAL)
15751da177e4SLinus Torvalds 				&& !(ioc_raw_state & MPI_DOORBELL_ACTIVE)) {
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 		/* Isse the Task Mgmt request.
15781da177e4SLinus Torvalds 		 */
15791da177e4SLinus Torvalds 		if (hd->hard_resets < -1)
15801da177e4SLinus Torvalds 			hd->hard_resets++;
15811da177e4SLinus Torvalds 		rc = mptscsih_IssueTaskMgmt(hd, type, channel, target, lun, ctx2abort, timeout);
15821da177e4SLinus Torvalds 		if (rc) {
15831da177e4SLinus Torvalds 			printk(MYIOC_s_INFO_FMT "Issue of TaskMgmt failed!\n", hd->ioc->name);
15841da177e4SLinus Torvalds 		} else {
15851da177e4SLinus Torvalds 			dtmprintk((MYIOC_s_INFO_FMT "Issue of TaskMgmt Successful!\n", hd->ioc->name));
15861da177e4SLinus Torvalds 		}
15871da177e4SLinus Torvalds 	}
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds 	/* Only fall through to the HRH if this is a bus reset
15901da177e4SLinus Torvalds 	 */
15911da177e4SLinus Torvalds 	if ((type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) && (rc ||
15921da177e4SLinus Torvalds 		ioc->reload_fw || (ioc->alt_ioc && ioc->alt_ioc->reload_fw))) {
15931da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_INFO_FMT "Calling HardReset! \n",
15941da177e4SLinus Torvalds 			 hd->ioc->name));
15951da177e4SLinus Torvalds 		rc = mpt_HardResetHandler(hd->ioc, CAN_SLEEP);
15961da177e4SLinus Torvalds 	}
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	dtmprintk((MYIOC_s_INFO_FMT "TMHandler rc = %d!\n", hd->ioc->name, rc));
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	return rc;
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
16051da177e4SLinus Torvalds /*
16061da177e4SLinus Torvalds  *	mptscsih_IssueTaskMgmt - Generic send Task Management function.
16071da177e4SLinus Torvalds  *	@hd: Pointer to MPT_SCSI_HOST structure
16081da177e4SLinus Torvalds  *	@type: Task Management type
16091da177e4SLinus Torvalds  *	@target: Logical Target ID for reset (if appropriate)
16101da177e4SLinus Torvalds  *	@lun: Logical Unit for reset (if appropriate)
16111da177e4SLinus Torvalds  *	@ctx2abort: Context for the task to be aborted (if appropriate)
16121da177e4SLinus Torvalds  *
16131da177e4SLinus Torvalds  *	Remark: _HardResetHandler can be invoked from an interrupt thread (timer)
16141da177e4SLinus Torvalds  *	or a non-interrupt thread.  In the former, must not call schedule().
16151da177e4SLinus Torvalds  *
16161da177e4SLinus Torvalds  *	Not all fields are meaningfull for all task types.
16171da177e4SLinus Torvalds  *
16181da177e4SLinus Torvalds  *	Returns 0 for SUCCESS, -999 for "no msg frames",
16191da177e4SLinus Torvalds  *	else other non-zero value returned.
16201da177e4SLinus Torvalds  */
16211da177e4SLinus Torvalds static int
16221da177e4SLinus Torvalds mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout)
16231da177e4SLinus Torvalds {
16241da177e4SLinus Torvalds 	MPT_FRAME_HDR	*mf;
16251da177e4SLinus Torvalds 	SCSITaskMgmt_t	*pScsiTm;
16261da177e4SLinus Torvalds 	int		 ii;
16271da177e4SLinus Torvalds 	int		 retval;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	/* Return Fail to calling function if no message frames available.
16301da177e4SLinus Torvalds 	 */
16310d0c7974SMoore, Eric Dean  	if ((mf = mpt_get_msg_frame(hd->ioc->TaskCtx, hd->ioc)) == NULL) {
16321da177e4SLinus Torvalds 		dfailprintk((MYIOC_s_ERR_FMT "IssueTaskMgmt, no msg frames!!\n",
16331da177e4SLinus Torvalds 				hd->ioc->name));
16341da177e4SLinus Torvalds 		//return FAILED;
16351da177e4SLinus Torvalds 		return -999;
16361da177e4SLinus Torvalds 	}
16371da177e4SLinus Torvalds 	dtmprintk((MYIOC_s_INFO_FMT "IssueTaskMgmt request @ %p\n",
16381da177e4SLinus Torvalds 			hd->ioc->name, mf));
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds 	/* Format the Request
16411da177e4SLinus Torvalds 	 */
16421da177e4SLinus Torvalds 	pScsiTm = (SCSITaskMgmt_t *) mf;
16431da177e4SLinus Torvalds 	pScsiTm->TargetID = target;
16441da177e4SLinus Torvalds 	pScsiTm->Bus = channel;
16451da177e4SLinus Torvalds 	pScsiTm->ChainOffset = 0;
16461da177e4SLinus Torvalds 	pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	pScsiTm->Reserved = 0;
16491da177e4SLinus Torvalds 	pScsiTm->TaskType = type;
16501da177e4SLinus Torvalds 	pScsiTm->Reserved1 = 0;
16511da177e4SLinus Torvalds 	pScsiTm->MsgFlags = (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS)
16521da177e4SLinus Torvalds                     ? MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION : 0;
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	for (ii= 0; ii < 8; ii++) {
16551da177e4SLinus Torvalds 		pScsiTm->LUN[ii] = 0;
16561da177e4SLinus Torvalds 	}
16571da177e4SLinus Torvalds 	pScsiTm->LUN[1] = lun;
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	for (ii=0; ii < 7; ii++)
16601da177e4SLinus Torvalds 		pScsiTm->Reserved2[ii] = 0;
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	pScsiTm->TaskMsgContext = ctx2abort;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	dtmprintk((MYIOC_s_INFO_FMT
16651da177e4SLinus Torvalds 		"IssueTaskMgmt: ctx2abort (0x%08x) type=%d\n",
16661da177e4SLinus Torvalds 		hd->ioc->name, ctx2abort, type));
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	DBG_DUMP_TM_REQUEST_FRAME((u32 *)pScsiTm);
16691da177e4SLinus Torvalds 
16700d0c7974SMoore, Eric Dean  	if ((retval = mpt_send_handshake_request(hd->ioc->TaskCtx, hd->ioc,
16711da177e4SLinus Torvalds 		sizeof(SCSITaskMgmt_t), (u32*)pScsiTm,
16721da177e4SLinus Torvalds 		CAN_SLEEP)) != 0) {
16731da177e4SLinus Torvalds 		dfailprintk((MYIOC_s_ERR_FMT "_send_handshake FAILED!"
16741da177e4SLinus Torvalds 			" (hd %p, ioc %p, mf %p) \n", hd->ioc->name, hd,
16751da177e4SLinus Torvalds 			hd->ioc, mf));
16761da177e4SLinus Torvalds 		mpt_free_msg_frame(hd->ioc, mf);
16771da177e4SLinus Torvalds 		return retval;
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 	if(mptscsih_tm_wait_for_completion(hd, timeout) == FAILED) {
16811da177e4SLinus Torvalds 		dfailprintk((MYIOC_s_ERR_FMT "_wait_for_completion FAILED!"
16821da177e4SLinus Torvalds 			" (hd %p, ioc %p, mf %p) \n", hd->ioc->name, hd,
16831da177e4SLinus Torvalds 			hd->ioc, mf));
16841da177e4SLinus Torvalds 		mpt_free_msg_frame(hd->ioc, mf);
16851da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_INFO_FMT "Calling HardReset! \n",
16861da177e4SLinus Torvalds 			 hd->ioc->name));
16871da177e4SLinus Torvalds 		retval = mpt_HardResetHandler(hd->ioc, CAN_SLEEP);
16881da177e4SLinus Torvalds 	}
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 	return retval;
16911da177e4SLinus Torvalds }
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
16941da177e4SLinus Torvalds /**
16951da177e4SLinus Torvalds  *	mptscsih_abort - Abort linux scsi_cmnd routine, new_eh variant
16961da177e4SLinus Torvalds  *	@SCpnt: Pointer to scsi_cmnd structure, IO to be aborted
16971da177e4SLinus Torvalds  *
16981da177e4SLinus Torvalds  *	(linux scsi_host_template.eh_abort_handler routine)
16991da177e4SLinus Torvalds  *
17001da177e4SLinus Torvalds  *	Returns SUCCESS or FAILED.
17011da177e4SLinus Torvalds  */
17020d0c7974SMoore, Eric Dean  int
17031da177e4SLinus Torvalds mptscsih_abort(struct scsi_cmnd * SCpnt)
17041da177e4SLinus Torvalds {
17051da177e4SLinus Torvalds 	MPT_SCSI_HOST	*hd;
17061da177e4SLinus Torvalds 	MPT_ADAPTER	*ioc;
17071da177e4SLinus Torvalds 	MPT_FRAME_HDR	*mf;
17081da177e4SLinus Torvalds 	u32		 ctx2abort;
17091da177e4SLinus Torvalds 	int		 scpnt_idx;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	/* If we can't locate our host adapter structure, return FAILED status.
17121da177e4SLinus Torvalds 	 */
17131da177e4SLinus Torvalds 	if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL) {
17141da177e4SLinus Torvalds 		SCpnt->result = DID_RESET << 16;
17151da177e4SLinus Torvalds 		SCpnt->scsi_done(SCpnt);
17161da177e4SLinus Torvalds 		dfailprintk((KERN_WARNING MYNAM ": mptscsih_abort: "
17171da177e4SLinus Torvalds 			   "Can't locate host! (sc=%p)\n",
17181da177e4SLinus Torvalds 			   SCpnt));
17191da177e4SLinus Torvalds 		return FAILED;
17201da177e4SLinus Torvalds 	}
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	ioc = hd->ioc;
17231da177e4SLinus Torvalds 	if (hd->resetPending)
17241da177e4SLinus Torvalds 		return FAILED;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	printk(KERN_WARNING MYNAM ": %s: >> Attempting task abort! (sc=%p)\n",
17271da177e4SLinus Torvalds 	       hd->ioc->name, SCpnt);
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds 	if (hd->timeouts < -1)
17301da177e4SLinus Torvalds 		hd->timeouts++;
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 	/* Find this command
17331da177e4SLinus Torvalds 	 */
17341da177e4SLinus Torvalds 	if ((scpnt_idx = SCPNT_TO_LOOKUP_IDX(SCpnt)) < 0) {
17351da177e4SLinus Torvalds 		/* Cmd not found in ScsiLookup.
17361da177e4SLinus Torvalds 		 * Do OS callback.
17371da177e4SLinus Torvalds 		 */
17381da177e4SLinus Torvalds 		SCpnt->result = DID_RESET << 16;
17391da177e4SLinus Torvalds 		dtmprintk((KERN_WARNING MYNAM ": %s: mptscsih_abort: "
17401da177e4SLinus Torvalds 			   "Command not in the active list! (sc=%p)\n",
17411da177e4SLinus Torvalds 			   hd->ioc->name, SCpnt));
17421da177e4SLinus Torvalds 		return SUCCESS;
17431da177e4SLinus Torvalds 	}
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 	/* Most important!  Set TaskMsgContext to SCpnt's MsgContext!
17461da177e4SLinus Torvalds 	 * (the IO to be ABORT'd)
17471da177e4SLinus Torvalds 	 *
17481da177e4SLinus Torvalds 	 * NOTE: Since we do not byteswap MsgContext, we do not
17491da177e4SLinus Torvalds 	 *	 swap it here either.  It is an opaque cookie to
17501da177e4SLinus Torvalds 	 *	 the controller, so it does not matter. -DaveM
17511da177e4SLinus Torvalds 	 */
17521da177e4SLinus Torvalds 	mf = MPT_INDEX_2_MFPTR(hd->ioc, scpnt_idx);
17531da177e4SLinus Torvalds 	ctx2abort = mf->u.frame.hwhdr.msgctxu.MsgContext;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	hd->abortSCpnt = SCpnt;
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 	if (mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
17581da177e4SLinus Torvalds 		SCpnt->device->channel, SCpnt->device->id, SCpnt->device->lun,
17591da177e4SLinus Torvalds 		ctx2abort, 2 /* 2 second timeout */)
17601da177e4SLinus Torvalds 		< 0) {
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 		/* The TM request failed and the subsequent FW-reload failed!
17631da177e4SLinus Torvalds 		 * Fatal error case.
17641da177e4SLinus Torvalds 		 */
17651da177e4SLinus Torvalds 		printk(MYIOC_s_WARN_FMT "Error issuing abort task! (sc=%p)\n",
17661da177e4SLinus Torvalds 		       hd->ioc->name, SCpnt);
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 		/* We must clear our pending flag before clearing our state.
17691da177e4SLinus Torvalds 		 */
17701da177e4SLinus Torvalds 		hd->tmPending = 0;
17711da177e4SLinus Torvalds 		hd->tmState = TM_STATE_NONE;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 		/* Unmap the DMA buffers, if any. */
17741da177e4SLinus Torvalds 		if (SCpnt->use_sg) {
17751da177e4SLinus Torvalds 			pci_unmap_sg(ioc->pcidev, (struct scatterlist *) SCpnt->request_buffer,
17761da177e4SLinus Torvalds 				    SCpnt->use_sg, SCpnt->sc_data_direction);
17771da177e4SLinus Torvalds 		} else if (SCpnt->request_bufflen) {
17781da177e4SLinus Torvalds 			pci_unmap_single(ioc->pcidev, SCpnt->SCp.dma_handle,
17791da177e4SLinus Torvalds 				SCpnt->request_bufflen, SCpnt->sc_data_direction);
17801da177e4SLinus Torvalds 		}
17811da177e4SLinus Torvalds 		hd->ScsiLookup[scpnt_idx] = NULL;
17821da177e4SLinus Torvalds 		SCpnt->result = DID_RESET << 16;
17831da177e4SLinus Torvalds 		SCpnt->scsi_done(SCpnt);		/* Issue the command callback */
17841da177e4SLinus Torvalds 		mptscsih_freeChainBuffers(ioc, scpnt_idx);
17851da177e4SLinus Torvalds 		mpt_free_msg_frame(ioc, mf);
17861da177e4SLinus Torvalds 		return FAILED;
17871da177e4SLinus Torvalds 	}
17881da177e4SLinus Torvalds 	return SUCCESS;
17891da177e4SLinus Torvalds }
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
17921da177e4SLinus Torvalds /**
17931da177e4SLinus Torvalds  *	mptscsih_dev_reset - Perform a SCSI TARGET_RESET!  new_eh variant
17941da177e4SLinus Torvalds  *	@SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
17951da177e4SLinus Torvalds  *
17961da177e4SLinus Torvalds  *	(linux scsi_host_template.eh_dev_reset_handler routine)
17971da177e4SLinus Torvalds  *
17981da177e4SLinus Torvalds  *	Returns SUCCESS or FAILED.
17991da177e4SLinus Torvalds  */
18000d0c7974SMoore, Eric Dean  int
18011da177e4SLinus Torvalds mptscsih_dev_reset(struct scsi_cmnd * SCpnt)
18021da177e4SLinus Torvalds {
18031da177e4SLinus Torvalds 	MPT_SCSI_HOST	*hd;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	/* If we can't locate our host adapter structure, return FAILED status.
18061da177e4SLinus Torvalds 	 */
18071da177e4SLinus Torvalds 	if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
18081da177e4SLinus Torvalds 		dtmprintk((KERN_WARNING MYNAM ": mptscsih_dev_reset: "
18091da177e4SLinus Torvalds 			   "Can't locate host! (sc=%p)\n",
18101da177e4SLinus Torvalds 			   SCpnt));
18111da177e4SLinus Torvalds 		return FAILED;
18121da177e4SLinus Torvalds 	}
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 	if (hd->resetPending)
18151da177e4SLinus Torvalds 		return FAILED;
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	printk(KERN_WARNING MYNAM ": %s: >> Attempting target reset! (sc=%p)\n",
18181da177e4SLinus Torvalds 	       hd->ioc->name, SCpnt);
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	if (mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
18211da177e4SLinus Torvalds 		SCpnt->device->channel, SCpnt->device->id,
18221da177e4SLinus Torvalds 		0, 0, 5 /* 5 second timeout */)
18231da177e4SLinus Torvalds 		< 0){
18241da177e4SLinus Torvalds 		/* The TM request failed and the subsequent FW-reload failed!
18251da177e4SLinus Torvalds 		 * Fatal error case.
18261da177e4SLinus Torvalds 		 */
18271da177e4SLinus Torvalds 		printk(MYIOC_s_WARN_FMT "Error processing TaskMgmt request (sc=%p)\n",
18281da177e4SLinus Torvalds 		 		hd->ioc->name, SCpnt);
18291da177e4SLinus Torvalds 		hd->tmPending = 0;
18301da177e4SLinus Torvalds 		hd->tmState = TM_STATE_NONE;
18311da177e4SLinus Torvalds 		return FAILED;
18321da177e4SLinus Torvalds 	}
18331da177e4SLinus Torvalds 
183494d0e7b8SJeff Garzik  	return SUCCESS;
18351da177e4SLinus Torvalds }
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
18381da177e4SLinus Torvalds /**
18391da177e4SLinus Torvalds  *	mptscsih_bus_reset - Perform a SCSI BUS_RESET!	new_eh variant
18401da177e4SLinus Torvalds  *	@SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
18411da177e4SLinus Torvalds  *
18421da177e4SLinus Torvalds  *	(linux scsi_host_template.eh_bus_reset_handler routine)
18431da177e4SLinus Torvalds  *
18441da177e4SLinus Torvalds  *	Returns SUCCESS or FAILED.
18451da177e4SLinus Torvalds  */
18460d0c7974SMoore, Eric Dean  int
18471da177e4SLinus Torvalds mptscsih_bus_reset(struct scsi_cmnd * SCpnt)
18481da177e4SLinus Torvalds {
18491da177e4SLinus Torvalds 	MPT_SCSI_HOST	*hd;
18501da177e4SLinus Torvalds 	spinlock_t	*host_lock = SCpnt->device->host->host_lock;
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	/* If we can't locate our host adapter structure, return FAILED status.
18531da177e4SLinus Torvalds 	 */
18541da177e4SLinus Torvalds 	if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
18551da177e4SLinus Torvalds 		dtmprintk((KERN_WARNING MYNAM ": mptscsih_bus_reset: "
18561da177e4SLinus Torvalds 			   "Can't locate host! (sc=%p)\n",
18571da177e4SLinus Torvalds 			   SCpnt ) );
18581da177e4SLinus Torvalds 		return FAILED;
18591da177e4SLinus Torvalds 	}
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	printk(KERN_WARNING MYNAM ": %s: >> Attempting bus reset! (sc=%p)\n",
18621da177e4SLinus Torvalds 	       hd->ioc->name, SCpnt);
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	if (hd->timeouts < -1)
18651da177e4SLinus Torvalds 		hd->timeouts++;
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	/* We are now ready to execute the task management request. */
18681da177e4SLinus Torvalds 	if (mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
18691da177e4SLinus Torvalds 		SCpnt->device->channel, 0, 0, 0, 5 /* 5 second timeout */)
18701da177e4SLinus Torvalds 	    < 0){
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 		/* The TM request failed and the subsequent FW-reload failed!
18731da177e4SLinus Torvalds 		 * Fatal error case.
18741da177e4SLinus Torvalds 		 */
18751da177e4SLinus Torvalds 		printk(MYIOC_s_WARN_FMT
18761da177e4SLinus Torvalds 		       "Error processing TaskMgmt request (sc=%p)\n",
18771da177e4SLinus Torvalds 		       hd->ioc->name, SCpnt);
18781da177e4SLinus Torvalds 		hd->tmPending = 0;
18791da177e4SLinus Torvalds 		hd->tmState = TM_STATE_NONE;
18801da177e4SLinus Torvalds 		spin_lock_irq(host_lock);
18811da177e4SLinus Torvalds 		return FAILED;
18821da177e4SLinus Torvalds 	}
188368b3aa7cSJeff Garzik  
18841da177e4SLinus Torvalds 	return SUCCESS;
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
18881da177e4SLinus Torvalds /**
18891da177e4SLinus Torvalds  *	mptscsih_host_reset - Perform a SCSI host adapter RESET!
18901da177e4SLinus Torvalds  *	new_eh variant
18911da177e4SLinus Torvalds  *	@SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
18921da177e4SLinus Torvalds  *
18931da177e4SLinus Torvalds  *	(linux scsi_host_template.eh_host_reset_handler routine)
18941da177e4SLinus Torvalds  *
18951da177e4SLinus Torvalds  *	Returns SUCCESS or FAILED.
18961da177e4SLinus Torvalds  */
18970d0c7974SMoore, Eric Dean  int
18981da177e4SLinus Torvalds mptscsih_host_reset(struct scsi_cmnd *SCpnt)
18991da177e4SLinus Torvalds {
19001da177e4SLinus Torvalds 	MPT_SCSI_HOST *  hd;
19011da177e4SLinus Torvalds 	int              status = SUCCESS;
19021da177e4SLinus Torvalds 	spinlock_t	*host_lock = SCpnt->device->host->host_lock;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	/*  If we can't locate the host to reset, then we failed. */
19051da177e4SLinus Torvalds 	if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
19061da177e4SLinus Torvalds 		dtmprintk( ( KERN_WARNING MYNAM ": mptscsih_host_reset: "
19071da177e4SLinus Torvalds 			     "Can't locate host! (sc=%p)\n",
19081da177e4SLinus Torvalds 			     SCpnt ) );
19091da177e4SLinus Torvalds 		return FAILED;
19101da177e4SLinus Torvalds 	}
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	printk(KERN_WARNING MYNAM ": %s: >> Attempting host reset! (sc=%p)\n",
19131da177e4SLinus Torvalds 	       hd->ioc->name, SCpnt);
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds 	/*  If our attempts to reset the host failed, then return a failed
19161da177e4SLinus Torvalds 	 *  status.  The host will be taken off line by the SCSI mid-layer.
19171da177e4SLinus Torvalds 	 */
19181da177e4SLinus Torvalds 	spin_unlock_irq(host_lock);
19191da177e4SLinus Torvalds 	if (mpt_HardResetHandler(hd->ioc, CAN_SLEEP) < 0){
19201da177e4SLinus Torvalds 		status = FAILED;
19211da177e4SLinus Torvalds 	} else {
19221da177e4SLinus Torvalds 		/*  Make sure TM pending is cleared and TM state is set to
19231da177e4SLinus Torvalds 		 *  NONE.
19241da177e4SLinus Torvalds 		 */
19251da177e4SLinus Torvalds 		hd->tmPending = 0;
19261da177e4SLinus Torvalds 		hd->tmState = TM_STATE_NONE;
19271da177e4SLinus Torvalds 	}
19281da177e4SLinus Torvalds 	spin_lock_irq(host_lock);
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	dtmprintk( ( KERN_WARNING MYNAM ": mptscsih_host_reset: "
19321da177e4SLinus Torvalds 		     "Status = %s\n",
19331da177e4SLinus Torvalds 		     (status == SUCCESS) ? "SUCCESS" : "FAILED" ) );
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	return status;
19361da177e4SLinus Torvalds }
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
19391da177e4SLinus Torvalds /**
19401da177e4SLinus Torvalds  *	mptscsih_tm_pending_wait - wait for pending task management request to
19411da177e4SLinus Torvalds  *		complete.
19421da177e4SLinus Torvalds  *	@hd: Pointer to MPT host structure.
19431da177e4SLinus Torvalds  *
19441da177e4SLinus Torvalds  *	Returns {SUCCESS,FAILED}.
19451da177e4SLinus Torvalds  */
19461da177e4SLinus Torvalds static int
19471da177e4SLinus Torvalds mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd)
19481da177e4SLinus Torvalds {
19491da177e4SLinus Torvalds 	unsigned long  flags;
19501da177e4SLinus Torvalds 	int            loop_count = 4 * 10;  /* Wait 10 seconds */
19511da177e4SLinus Torvalds 	int            status = FAILED;
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 	do {
19541da177e4SLinus Torvalds 		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
19551da177e4SLinus Torvalds 		if (hd->tmState == TM_STATE_NONE) {
19561da177e4SLinus Torvalds 			hd->tmState = TM_STATE_IN_PROGRESS;
19571da177e4SLinus Torvalds 			hd->tmPending = 1;
19581da177e4SLinus Torvalds 			status = SUCCESS;
19591da177e4SLinus Torvalds 			spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
19601da177e4SLinus Torvalds 			break;
19611da177e4SLinus Torvalds 		}
19621da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
19631da177e4SLinus Torvalds 		msleep(250);
19641da177e4SLinus Torvalds 	} while (--loop_count);
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds 	return status;
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
19701da177e4SLinus Torvalds /**
19711da177e4SLinus Torvalds  *	mptscsih_tm_wait_for_completion - wait for completion of TM task
19721da177e4SLinus Torvalds  *	@hd: Pointer to MPT host structure.
19731da177e4SLinus Torvalds  *
19741da177e4SLinus Torvalds  *	Returns {SUCCESS,FAILED}.
19751da177e4SLinus Torvalds  */
19761da177e4SLinus Torvalds static int
19771da177e4SLinus Torvalds mptscsih_tm_wait_for_completion(MPT_SCSI_HOST * hd, ulong timeout )
19781da177e4SLinus Torvalds {
19791da177e4SLinus Torvalds 	unsigned long  flags;
19801da177e4SLinus Torvalds 	int            loop_count = 4 * timeout;
19811da177e4SLinus Torvalds 	int            status = FAILED;
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	do {
19841da177e4SLinus Torvalds 		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
19851da177e4SLinus Torvalds 		if(hd->tmPending == 0) {
19861da177e4SLinus Torvalds 			status = SUCCESS;
19871da177e4SLinus Torvalds 			spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
19881da177e4SLinus Torvalds 			break;
19891da177e4SLinus Torvalds 		}
19901da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
19911da177e4SLinus Torvalds 		msleep_interruptible(250);
19921da177e4SLinus Torvalds 	} while (--loop_count);
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	return status;
19951da177e4SLinus Torvalds }
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
19981da177e4SLinus Torvalds /**
19991da177e4SLinus Torvalds  *	mptscsih_taskmgmt_complete - Registered with Fusion MPT base driver
20001da177e4SLinus Torvalds  *	@ioc: Pointer to MPT_ADAPTER structure
20011da177e4SLinus Torvalds  *	@mf: Pointer to SCSI task mgmt request frame
20021da177e4SLinus Torvalds  *	@mr: Pointer to SCSI task mgmt reply frame
20031da177e4SLinus Torvalds  *
20041da177e4SLinus Torvalds  *	This routine is called from mptbase.c::mpt_interrupt() at the completion
20051da177e4SLinus Torvalds  *	of any SCSI task management request.
20061da177e4SLinus Torvalds  *	This routine is registered with the MPT (base) driver at driver
20071da177e4SLinus Torvalds  *	load/init time via the mpt_register() API call.
20081da177e4SLinus Torvalds  *
20091da177e4SLinus Torvalds  *	Returns 1 indicating alloc'd request frame ptr should be freed.
20101da177e4SLinus Torvalds  */
20110d0c7974SMoore, Eric Dean  int
20121da177e4SLinus Torvalds mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
20131da177e4SLinus Torvalds {
20141da177e4SLinus Torvalds 	SCSITaskMgmtReply_t	*pScsiTmReply;
20151da177e4SLinus Torvalds 	SCSITaskMgmt_t		*pScsiTmReq;
20161da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd;
20171da177e4SLinus Torvalds 	unsigned long		 flags;
20181da177e4SLinus Torvalds 	u16			 iocstatus;
20191da177e4SLinus Torvalds 	u8			 tmType;
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	dtmprintk((MYIOC_s_WARN_FMT "TaskMgmt completed (mf=%p,mr=%p)\n",
20221da177e4SLinus Torvalds 			ioc->name, mf, mr));
20231da177e4SLinus Torvalds 	if (ioc->sh) {
20241da177e4SLinus Torvalds 		/* Depending on the thread, a timer is activated for
20251da177e4SLinus Torvalds 		 * the TM request.  Delete this timer on completion of TM.
20261da177e4SLinus Torvalds 		 * Decrement count of outstanding TM requests.
20271da177e4SLinus Torvalds 		 */
20281da177e4SLinus Torvalds 		hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
20291da177e4SLinus Torvalds 	} else {
20301da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "TaskMgmt Complete: NULL Scsi Host Ptr\n",
20311da177e4SLinus Torvalds 			ioc->name));
20321da177e4SLinus Torvalds 		return 1;
20331da177e4SLinus Torvalds 	}
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	if (mr == NULL) {
20361da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "ERROR! TaskMgmt Reply: NULL Request %p\n",
20371da177e4SLinus Torvalds 			ioc->name, mf));
20381da177e4SLinus Torvalds 		return 1;
20391da177e4SLinus Torvalds 	} else {
20401da177e4SLinus Torvalds 		pScsiTmReply = (SCSITaskMgmtReply_t*)mr;
20411da177e4SLinus Torvalds 		pScsiTmReq = (SCSITaskMgmt_t*)mf;
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 		/* Figure out if this was ABORT_TASK, TARGET_RESET, or BUS_RESET! */
20441da177e4SLinus Torvalds 		tmType = pScsiTmReq->TaskType;
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "  TaskType = %d, TerminationCount=%d\n",
20471da177e4SLinus Torvalds 				ioc->name, tmType, le32_to_cpu(pScsiTmReply->TerminationCount)));
20481da177e4SLinus Torvalds 		DBG_DUMP_TM_REPLY_FRAME((u32 *)pScsiTmReply);
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 		iocstatus = le16_to_cpu(pScsiTmReply->IOCStatus) & MPI_IOCSTATUS_MASK;
20511da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "  SCSI TaskMgmt (%d) IOCStatus=%04x IOCLogInfo=%08x\n",
20521da177e4SLinus Torvalds 			ioc->name, tmType, iocstatus, le32_to_cpu(pScsiTmReply->IOCLogInfo)));
20531da177e4SLinus Torvalds 		/* Error?  (anything non-zero?) */
20541da177e4SLinus Torvalds 		if (iocstatus) {
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 			/* clear flags and continue.
20571da177e4SLinus Torvalds 			 */
20581da177e4SLinus Torvalds 			if (tmType == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
20591da177e4SLinus Torvalds 				hd->abortSCpnt = NULL;
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 			/* If an internal command is present
20621da177e4SLinus Torvalds 			 * or the TM failed - reload the FW.
20631da177e4SLinus Torvalds 			 * FC FW may respond FAILED to an ABORT
20641da177e4SLinus Torvalds 			 */
20651da177e4SLinus Torvalds 			if (tmType == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) {
20661da177e4SLinus Torvalds 				if ((hd->cmdPtr) ||
20671da177e4SLinus Torvalds 				    (iocstatus == MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED)) {
20681da177e4SLinus Torvalds 					if (mpt_HardResetHandler(ioc, NO_SLEEP) < 0) {
20691da177e4SLinus Torvalds 						printk((KERN_WARNING
20701da177e4SLinus Torvalds 							" Firmware Reload FAILED!!\n"));
20711da177e4SLinus Torvalds 					}
20721da177e4SLinus Torvalds 				}
20731da177e4SLinus Torvalds 			}
20741da177e4SLinus Torvalds 		} else {
20751da177e4SLinus Torvalds 			dtmprintk((MYIOC_s_WARN_FMT " TaskMgmt SUCCESS\n", ioc->name));
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 			hd->abortSCpnt = NULL;
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 		}
20801da177e4SLinus Torvalds 	}
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	spin_lock_irqsave(&ioc->FreeQlock, flags);
20831da177e4SLinus Torvalds 	hd->tmPending = 0;
20841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
20851da177e4SLinus Torvalds 	hd->tmState = TM_STATE_NONE;
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 	return 1;
20881da177e4SLinus Torvalds }
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
20911da177e4SLinus Torvalds /*
20921da177e4SLinus Torvalds  *	This is anyones guess quite frankly.
20931da177e4SLinus Torvalds  */
20940d0c7974SMoore, Eric Dean  int
20951da177e4SLinus Torvalds mptscsih_bios_param(struct scsi_device * sdev, struct block_device *bdev,
20961da177e4SLinus Torvalds 		sector_t capacity, int geom[])
20971da177e4SLinus Torvalds {
20981da177e4SLinus Torvalds 	int		heads;
20991da177e4SLinus Torvalds 	int		sectors;
21001da177e4SLinus Torvalds 	sector_t	cylinders;
21011da177e4SLinus Torvalds 	ulong 		dummy;
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	heads = 64;
21041da177e4SLinus Torvalds 	sectors = 32;
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	dummy = heads * sectors;
21071da177e4SLinus Torvalds 	cylinders = capacity;
21081da177e4SLinus Torvalds 	sector_div(cylinders,dummy);
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	/*
21111da177e4SLinus Torvalds 	 * Handle extended translation size for logical drives
21121da177e4SLinus Torvalds 	 * > 1Gb
21131da177e4SLinus Torvalds 	 */
21141da177e4SLinus Torvalds 	if ((ulong)capacity >= 0x200000) {
21151da177e4SLinus Torvalds 		heads = 255;
21161da177e4SLinus Torvalds 		sectors = 63;
21171da177e4SLinus Torvalds 		dummy = heads * sectors;
21181da177e4SLinus Torvalds 		cylinders = capacity;
21191da177e4SLinus Torvalds 		sector_div(cylinders,dummy);
21201da177e4SLinus Torvalds 	}
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 	/* return result */
21231da177e4SLinus Torvalds 	geom[0] = heads;
21241da177e4SLinus Torvalds 	geom[1] = sectors;
21251da177e4SLinus Torvalds 	geom[2] = cylinders;
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 	dprintk((KERN_NOTICE
21281da177e4SLinus Torvalds 		": bios_param: Id=%i Lun=%i Channel=%i CHS=%i/%i/%i\n",
21291da177e4SLinus Torvalds 		sdev->id, sdev->lun,sdev->channel,(int)cylinders,heads,sectors));
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	return 0;
21321da177e4SLinus Torvalds }
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
21351da177e4SLinus Torvalds /*
21361da177e4SLinus Torvalds  *	OS entry point to allow host driver to alloc memory
21371da177e4SLinus Torvalds  *	for each scsi device. Called once per device the bus scan.
21381da177e4SLinus Torvalds  *	Return non-zero if allocation fails.
21391da177e4SLinus Torvalds  *	Init memory once per id (not LUN).
21401da177e4SLinus Torvalds  */
21410d0c7974SMoore, Eric Dean  int
21421da177e4SLinus Torvalds mptscsih_slave_alloc(struct scsi_device *device)
21431da177e4SLinus Torvalds {
21441da177e4SLinus Torvalds 	struct Scsi_Host	*host = device->host;
21451da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd = (MPT_SCSI_HOST *)host->hostdata;
21461da177e4SLinus Torvalds 	VirtDevice		*vdev;
21471da177e4SLinus Torvalds 	uint			target = device->id;
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	if (hd == NULL)
21501da177e4SLinus Torvalds 		return -ENODEV;
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 	if ((vdev = hd->Targets[target]) != NULL)
21531da177e4SLinus Torvalds 		goto out;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	vdev = kmalloc(sizeof(VirtDevice), GFP_KERNEL);
21561da177e4SLinus Torvalds 	if (!vdev) {
21571da177e4SLinus Torvalds 		printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
21581da177e4SLinus Torvalds 				hd->ioc->name, sizeof(VirtDevice));
21591da177e4SLinus Torvalds 		return -ENOMEM;
21601da177e4SLinus Torvalds 	}
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	memset(vdev, 0, sizeof(VirtDevice));
21631da177e4SLinus Torvalds 	vdev->tflags = MPT_TARGET_FLAGS_Q_YES;
21641da177e4SLinus Torvalds 	vdev->ioc_id = hd->ioc->id;
21651da177e4SLinus Torvalds 	vdev->target_id = device->id;
21661da177e4SLinus Torvalds 	vdev->bus_id = device->channel;
21671da177e4SLinus Torvalds 	vdev->raidVolume = 0;
21681da177e4SLinus Torvalds 	hd->Targets[device->id] = vdev;
21691da177e4SLinus Torvalds 	if (hd->ioc->bus_type == SCSI) {
21701da177e4SLinus Torvalds 		if (hd->ioc->spi_data.isRaid & (1 << device->id)) {
21711da177e4SLinus Torvalds 			vdev->raidVolume = 1;
21721da177e4SLinus Torvalds 			ddvtprintk((KERN_INFO
21731da177e4SLinus Torvalds 			    "RAID Volume @ id %d\n", device->id));
21741da177e4SLinus Torvalds 		}
21751da177e4SLinus Torvalds 	} else {
21761da177e4SLinus Torvalds 		vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
21771da177e4SLinus Torvalds 	}
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds  out:
21801da177e4SLinus Torvalds 	vdev->num_luns++;
21811da177e4SLinus Torvalds 	return 0;
21821da177e4SLinus Torvalds }
21831da177e4SLinus Torvalds 
21840d0c7974SMoore, Eric Dean  static int
21850d0c7974SMoore, Eric Dean  mptscsih_is_raid_volume(MPT_SCSI_HOST *hd, uint id)
21861da177e4SLinus Torvalds {
21871da177e4SLinus Torvalds 	int i;
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 	if (!hd->ioc->spi_data.isRaid || !hd->ioc->spi_data.pIocPg3)
21901da177e4SLinus Torvalds 		return 0;
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds 	for (i = 0; i < hd->ioc->spi_data.pIocPg3->NumPhysDisks; i++) {
21931da177e4SLinus Torvalds 		if (id == hd->ioc->spi_data.pIocPg3->PhysDisk[i].PhysDiskID)
21941da177e4SLinus Torvalds 			return 1;
21951da177e4SLinus Torvalds 	}
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	return 0;
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds /*
22011da177e4SLinus Torvalds  *	OS entry point to allow for host driver to free allocated memory
22021da177e4SLinus Torvalds  *	Called if no device present or device being unloaded
22031da177e4SLinus Torvalds  */
22040d0c7974SMoore, Eric Dean  void
22051da177e4SLinus Torvalds mptscsih_slave_destroy(struct scsi_device *device)
22061da177e4SLinus Torvalds {
22071da177e4SLinus Torvalds 	struct Scsi_Host	*host = device->host;
22081da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd = (MPT_SCSI_HOST *)host->hostdata;
22091da177e4SLinus Torvalds 	VirtDevice		*vdev;
22101da177e4SLinus Torvalds 	uint			target = device->id;
22111da177e4SLinus Torvalds 	uint			lun = device->lun;
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	if (hd == NULL)
22141da177e4SLinus Torvalds 		return;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	mptscsih_search_running_cmds(hd, target, lun);
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	vdev = hd->Targets[target];
22191da177e4SLinus Torvalds 	vdev->luns[0] &= ~(1 << lun);
22201da177e4SLinus Torvalds 	if (--vdev->num_luns)
22211da177e4SLinus Torvalds 		return;
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	kfree(hd->Targets[target]);
22241da177e4SLinus Torvalds 	hd->Targets[target] = NULL;
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	if (hd->ioc->bus_type == SCSI) {
22271da177e4SLinus Torvalds 		if (mptscsih_is_raid_volume(hd, target)) {
22281da177e4SLinus Torvalds 			hd->ioc->spi_data.forceDv |= MPT_SCSICFG_RELOAD_IOC_PG3;
22291da177e4SLinus Torvalds 		} else {
22301da177e4SLinus Torvalds 			hd->ioc->spi_data.dvStatus[target] =
22311da177e4SLinus Torvalds 				MPT_SCSICFG_NEGOTIATE;
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 			if (!hd->negoNvram) {
22341da177e4SLinus Torvalds 				hd->ioc->spi_data.dvStatus[target] |=
22351da177e4SLinus Torvalds 					MPT_SCSICFG_DV_NOT_DONE;
22361da177e4SLinus Torvalds 			}
22371da177e4SLinus Torvalds 		}
22381da177e4SLinus Torvalds 	}
22391da177e4SLinus Torvalds }
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds static void
22421da177e4SLinus Torvalds mptscsih_set_queue_depth(struct scsi_device *device, MPT_SCSI_HOST *hd,
22431da177e4SLinus Torvalds 	VirtDevice *pTarget, int qdepth)
22441da177e4SLinus Torvalds {
22451da177e4SLinus Torvalds 	int	max_depth;
22461da177e4SLinus Torvalds 	int	tagged;
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds 	if (hd->ioc->bus_type == SCSI) {
22491da177e4SLinus Torvalds 		if (pTarget->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY) {
22501da177e4SLinus Torvalds 			if (!(pTarget->tflags & MPT_TARGET_FLAGS_Q_YES))
22511da177e4SLinus Torvalds 				max_depth = 1;
22521da177e4SLinus Torvalds 			else if (((pTarget->inq_data[0] & 0x1f) == 0x00) &&
22531da177e4SLinus Torvalds 			         (pTarget->minSyncFactor <= MPT_ULTRA160 ))
22541da177e4SLinus Torvalds 				max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
22551da177e4SLinus Torvalds 			else
22561da177e4SLinus Torvalds 				max_depth = MPT_SCSI_CMD_PER_DEV_LOW;
22571da177e4SLinus Torvalds 		} else {
22581da177e4SLinus Torvalds 			/* error case - No Inq. Data */
22591da177e4SLinus Torvalds 			max_depth = 1;
22601da177e4SLinus Torvalds 		}
22611da177e4SLinus Torvalds 	} else
22621da177e4SLinus Torvalds 		max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds 	if (qdepth > max_depth)
22651da177e4SLinus Torvalds 		qdepth = max_depth;
22661da177e4SLinus Torvalds 	if (qdepth == 1)
22671da177e4SLinus Torvalds 		tagged = 0;
22681da177e4SLinus Torvalds 	else
22691da177e4SLinus Torvalds 		tagged = MSG_SIMPLE_TAG;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	scsi_adjust_queue_depth(device, tagged, qdepth);
22721da177e4SLinus Torvalds }
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds /*
22761da177e4SLinus Torvalds  *	OS entry point to adjust the queue_depths on a per-device basis.
22771da177e4SLinus Torvalds  *	Called once per device the bus scan. Use it to force the queue_depth
22781da177e4SLinus Torvalds  *	member to 1 if a device does not support Q tags.
22791da177e4SLinus Torvalds  *	Return non-zero if fails.
22801da177e4SLinus Torvalds  */
22810d0c7974SMoore, Eric Dean  int
22821da177e4SLinus Torvalds mptscsih_slave_configure(struct scsi_device *device)
22831da177e4SLinus Torvalds {
22841da177e4SLinus Torvalds 	struct Scsi_Host	*sh = device->host;
22851da177e4SLinus Torvalds 	VirtDevice		*pTarget;
22861da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd = (MPT_SCSI_HOST *)sh->hostdata;
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	if ((hd == NULL) || (hd->Targets == NULL)) {
22891da177e4SLinus Torvalds 		return 0;
22901da177e4SLinus Torvalds 	}
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
22931da177e4SLinus Torvalds 		"device @ %p, id=%d, LUN=%d, channel=%d\n",
22941da177e4SLinus Torvalds 		hd->ioc->name, device, device->id, device->lun, device->channel));
22951da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
22961da177e4SLinus Torvalds 		"sdtr %d wdtr %d ppr %d inq length=%d\n",
22971da177e4SLinus Torvalds 		hd->ioc->name, device->sdtr, device->wdtr,
22981da177e4SLinus Torvalds 		device->ppr, device->inquiry_len));
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	if (device->id > sh->max_id) {
23011da177e4SLinus Torvalds 		/* error case, should never happen */
23021da177e4SLinus Torvalds 		scsi_adjust_queue_depth(device, 0, 1);
23031da177e4SLinus Torvalds 		goto slave_configure_exit;
23041da177e4SLinus Torvalds 	}
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds 	pTarget = hd->Targets[device->id];
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds 	if (pTarget == NULL) {
23091da177e4SLinus Torvalds 		/* Driver doesn't know about this device.
23101da177e4SLinus Torvalds 		 * Kernel may generate a "Dummy Lun 0" which
23111da177e4SLinus Torvalds 		 * may become a real Lun if a
23121da177e4SLinus Torvalds 		 * "scsi add-single-device" command is executed
23131da177e4SLinus Torvalds 		 * while the driver is active (hot-plug a
23141da177e4SLinus Torvalds 		 * device).  LSI Raid controllers need
23151da177e4SLinus Torvalds 		 * queue_depth set to DEV_HIGH for this reason.
23161da177e4SLinus Torvalds 		 */
23171da177e4SLinus Torvalds 		scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
23181da177e4SLinus Torvalds 			MPT_SCSI_CMD_PER_DEV_HIGH);
23191da177e4SLinus Torvalds 		goto slave_configure_exit;
23201da177e4SLinus Torvalds 	}
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 	mptscsih_initTarget(hd, device->channel, device->id, device->lun,
23231da177e4SLinus Torvalds 		device->inquiry, device->inquiry_len );
23241da177e4SLinus Torvalds 	mptscsih_set_queue_depth(device, hd, pTarget, MPT_SCSI_CMD_PER_DEV_HIGH);
23251da177e4SLinus Torvalds 
23261da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
23271da177e4SLinus Torvalds 		"Queue depth=%d, tflags=%x\n",
23281da177e4SLinus Torvalds 		hd->ioc->name, device->queue_depth, pTarget->tflags));
23291da177e4SLinus Torvalds 
23301da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
23311da177e4SLinus Torvalds 		"negoFlags=%x, maxOffset=%x, SyncFactor=%x\n",
23321da177e4SLinus Torvalds 		hd->ioc->name, pTarget->negoFlags, pTarget->maxOffset, pTarget->minSyncFactor));
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds slave_configure_exit:
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
23371da177e4SLinus Torvalds 		"tagged %d, simple %d, ordered %d\n",
23381da177e4SLinus Torvalds 		hd->ioc->name,device->tagged_supported, device->simple_tags,
23391da177e4SLinus Torvalds 		device->ordered_tags));
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	return 0;
23421da177e4SLinus Torvalds }
23431da177e4SLinus Torvalds 
23440d0c7974SMoore, Eric Dean  ssize_t
23451da177e4SLinus Torvalds mptscsih_store_queue_depth(struct device *dev, const char *buf, size_t count)
23461da177e4SLinus Torvalds {
23471da177e4SLinus Torvalds 	int			 depth;
23481da177e4SLinus Torvalds 	struct scsi_device	*sdev = to_scsi_device(dev);
23491da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd = (MPT_SCSI_HOST *) sdev->host->hostdata;
23501da177e4SLinus Torvalds 	VirtDevice		*pTarget;
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds 	depth = simple_strtoul(buf, NULL, 0);
23531da177e4SLinus Torvalds 	if (depth == 0)
23541da177e4SLinus Torvalds 		return -EINVAL;
23551da177e4SLinus Torvalds 	pTarget = hd->Targets[sdev->id];
23561da177e4SLinus Torvalds 	if (pTarget == NULL)
23571da177e4SLinus Torvalds 		return -EINVAL;
23581da177e4SLinus Torvalds 	mptscsih_set_queue_depth(sdev, (MPT_SCSI_HOST *) sdev->host->hostdata,
23591da177e4SLinus Torvalds 		pTarget, depth);
23601da177e4SLinus Torvalds 	return count;
23611da177e4SLinus Torvalds }
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
23641da177e4SLinus Torvalds /*
23651da177e4SLinus Torvalds  *  Private routines...
23661da177e4SLinus Torvalds  */
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
23691da177e4SLinus Torvalds /* Utility function to copy sense data from the scsi_cmnd buffer
23701da177e4SLinus Torvalds  * to the FC and SCSI target structures.
23711da177e4SLinus Torvalds  *
23721da177e4SLinus Torvalds  */
23731da177e4SLinus Torvalds static void
23740d0c7974SMoore, Eric Dean  mptscsih_copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply)
23751da177e4SLinus Torvalds {
23761da177e4SLinus Torvalds 	VirtDevice	*target;
23771da177e4SLinus Torvalds 	SCSIIORequest_t	*pReq;
23781da177e4SLinus Torvalds 	u32		 sense_count = le32_to_cpu(pScsiReply->SenseCount);
23791da177e4SLinus Torvalds 	int		 index;
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	/* Get target structure
23821da177e4SLinus Torvalds 	 */
23831da177e4SLinus Torvalds 	pReq = (SCSIIORequest_t *) mf;
23841da177e4SLinus Torvalds 	index = (int) pReq->TargetID;
23851da177e4SLinus Torvalds 	target = hd->Targets[index];
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds 	if (sense_count) {
23881da177e4SLinus Torvalds 		u8 *sense_data;
23891da177e4SLinus Torvalds 		int req_index;
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 		/* Copy the sense received into the scsi command block. */
23921da177e4SLinus Torvalds 		req_index = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
23931da177e4SLinus Torvalds 		sense_data = ((u8 *)hd->ioc->sense_buf_pool + (req_index * MPT_SENSE_BUFFER_ALLOC));
23941da177e4SLinus Torvalds 		memcpy(sc->sense_buffer, sense_data, SNS_LEN(sc));
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 		/* Log SMART data (asc = 0x5D, non-IM case only) if required.
23971da177e4SLinus Torvalds 		 */
23981da177e4SLinus Torvalds 		if ((hd->ioc->events) && (hd->ioc->eventTypes & (1 << MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE))) {
23991da177e4SLinus Torvalds 			if ((sense_data[12] == 0x5D) && (target->raidVolume == 0)) {
24001da177e4SLinus Torvalds 				int idx;
24011da177e4SLinus Torvalds 				MPT_ADAPTER *ioc = hd->ioc;
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds 				idx = ioc->eventContext % ioc->eventLogSize;
24041da177e4SLinus Torvalds 				ioc->events[idx].event = MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE;
24051da177e4SLinus Torvalds 				ioc->events[idx].eventContext = ioc->eventContext;
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds 				ioc->events[idx].data[0] = (pReq->LUN[1] << 24) ||
24081da177e4SLinus Torvalds 					(MPI_EVENT_SCSI_DEV_STAT_RC_SMART_DATA << 16) ||
24091da177e4SLinus Torvalds 					(pReq->Bus << 8) || pReq->TargetID;
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 				ioc->events[idx].data[1] = (sense_data[13] << 8) || sense_data[12];
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 				ioc->eventContext++;
24141da177e4SLinus Torvalds 			}
24151da177e4SLinus Torvalds 		}
24161da177e4SLinus Torvalds 	} else {
24171da177e4SLinus Torvalds 		dprintk((MYIOC_s_INFO_FMT "Hmmm... SenseData len=0! (?)\n",
24181da177e4SLinus Torvalds 				hd->ioc->name));
24191da177e4SLinus Torvalds 	}
24201da177e4SLinus Torvalds }
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds static u32
24231da177e4SLinus Torvalds SCPNT_TO_LOOKUP_IDX(struct scsi_cmnd *sc)
24241da177e4SLinus Torvalds {
24251da177e4SLinus Torvalds 	MPT_SCSI_HOST *hd;
24261da177e4SLinus Torvalds 	int i;
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds 	hd = (MPT_SCSI_HOST *) sc->device->host->hostdata;
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	for (i = 0; i < hd->ioc->req_depth; i++) {
24311da177e4SLinus Torvalds 		if (hd->ScsiLookup[i] == sc) {
24321da177e4SLinus Torvalds 			return i;
24331da177e4SLinus Torvalds 		}
24341da177e4SLinus Torvalds 	}
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	return -1;
24371da177e4SLinus Torvalds }
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
24400d0c7974SMoore, Eric Dean  int
24411da177e4SLinus Torvalds mptscsih_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
24421da177e4SLinus Torvalds {
24431da177e4SLinus Torvalds 	MPT_SCSI_HOST	*hd;
24441da177e4SLinus Torvalds 	unsigned long	 flags;
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds 	dtmprintk((KERN_WARNING MYNAM
24471da177e4SLinus Torvalds 			": IOC %s_reset routed to SCSI host driver!\n",
24481da177e4SLinus Torvalds 			reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
24491da177e4SLinus Torvalds 			reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	/* If a FW reload request arrives after base installed but
24521da177e4SLinus Torvalds 	 * before all scsi hosts have been attached, then an alt_ioc
24531da177e4SLinus Torvalds 	 * may have a NULL sh pointer.
24541da177e4SLinus Torvalds 	 */
24551da177e4SLinus Torvalds 	if ((ioc->sh == NULL) || (ioc->sh->hostdata == NULL))
24561da177e4SLinus Torvalds 		return 0;
24571da177e4SLinus Torvalds 	else
24581da177e4SLinus Torvalds 		hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	if (reset_phase == MPT_IOC_SETUP_RESET) {
24611da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Setup-Diag Reset\n", ioc->name));
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 		/* Clean Up:
24641da177e4SLinus Torvalds 		 * 1. Set Hard Reset Pending Flag
24651da177e4SLinus Torvalds 		 * All new commands go to doneQ
24661da177e4SLinus Torvalds 		 */
24671da177e4SLinus Torvalds 		hd->resetPending = 1;
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds 	} else if (reset_phase == MPT_IOC_PRE_RESET) {
24701da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Pre-Diag Reset\n", ioc->name));
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 		/* 2. Flush running commands
24731da177e4SLinus Torvalds 		 *	Clean ScsiLookup (and associated memory)
24741da177e4SLinus Torvalds 		 *	AND clean mytaskQ
24751da177e4SLinus Torvalds 		 */
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 		/* 2b. Reply to OS all known outstanding I/O commands.
24781da177e4SLinus Torvalds 		 */
24791da177e4SLinus Torvalds 		mptscsih_flush_running_cmds(hd);
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 		/* 2c. If there was an internal command that
24821da177e4SLinus Torvalds 		 * has not completed, configuration or io request,
24831da177e4SLinus Torvalds 		 * free these resources.
24841da177e4SLinus Torvalds 		 */
24851da177e4SLinus Torvalds 		if (hd->cmdPtr) {
24861da177e4SLinus Torvalds 			del_timer(&hd->timer);
24871da177e4SLinus Torvalds 			mpt_free_msg_frame(ioc, hd->cmdPtr);
24881da177e4SLinus Torvalds 		}
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Pre-Reset complete.\n", ioc->name));
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	} else {
24931da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Post-Diag Reset\n", ioc->name));
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 		/* Once a FW reload begins, all new OS commands are
24961da177e4SLinus Torvalds 		 * redirected to the doneQ w/ a reset status.
24971da177e4SLinus Torvalds 		 * Init all control structures.
24981da177e4SLinus Torvalds 		 */
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds 		/* ScsiLookup initialization
25011da177e4SLinus Torvalds 		 */
25021da177e4SLinus Torvalds 		{
25031da177e4SLinus Torvalds 			int ii;
25041da177e4SLinus Torvalds 			for (ii=0; ii < hd->ioc->req_depth; ii++)
25051da177e4SLinus Torvalds 				hd->ScsiLookup[ii] = NULL;
25061da177e4SLinus Torvalds 		}
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds 		/* 2. Chain Buffer initialization
25091da177e4SLinus Torvalds 		 */
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 		/* 4. Renegotiate to all devices, if SCSI
25121da177e4SLinus Torvalds 		 */
25131da177e4SLinus Torvalds 		if (ioc->bus_type == SCSI) {
25141da177e4SLinus Torvalds 			dnegoprintk(("writeSDP1: ALL_IDS USE_NVRAM\n"));
25151da177e4SLinus Torvalds 			mptscsih_writeSDP1(hd, 0, 0, MPT_SCSICFG_ALL_IDS | MPT_SCSICFG_USE_NVRAM);
25161da177e4SLinus Torvalds 		}
25171da177e4SLinus Torvalds 
25181da177e4SLinus Torvalds 		/* 5. Enable new commands to be posted
25191da177e4SLinus Torvalds 		 */
25201da177e4SLinus Torvalds 		spin_lock_irqsave(&ioc->FreeQlock, flags);
25211da177e4SLinus Torvalds 		hd->tmPending = 0;
25221da177e4SLinus Torvalds 		spin_unlock_irqrestore(&ioc->FreeQlock, flags);
25231da177e4SLinus Torvalds 		hd->resetPending = 0;
25241da177e4SLinus Torvalds 		hd->tmState = TM_STATE_NONE;
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 		/* 6. If there was an internal command,
25271da177e4SLinus Torvalds 		 * wake this process up.
25281da177e4SLinus Torvalds 		 */
25291da177e4SLinus Torvalds 		if (hd->cmdPtr) {
25301da177e4SLinus Torvalds 			/*
25311da177e4SLinus Torvalds 			 * Wake up the original calling thread
25321da177e4SLinus Torvalds 			 */
25331da177e4SLinus Torvalds 			hd->pLocal = &hd->localReply;
25341da177e4SLinus Torvalds 			hd->pLocal->completion = MPT_SCANDV_DID_RESET;
25350d0c7974SMoore, Eric Dean  			hd->scandv_wait_done = 1;
25360d0c7974SMoore, Eric Dean  			wake_up(&hd->scandv_waitq);
25371da177e4SLinus Torvalds 			hd->cmdPtr = NULL;
25381da177e4SLinus Torvalds 		}
25391da177e4SLinus Torvalds 
25401da177e4SLinus Torvalds 		/* 7. Set flag to force DV and re-read IOC Page 3
25411da177e4SLinus Torvalds 		 */
25421da177e4SLinus Torvalds 		if (ioc->bus_type == SCSI) {
25431da177e4SLinus Torvalds 			ioc->spi_data.forceDv = MPT_SCSICFG_NEED_DV | MPT_SCSICFG_RELOAD_IOC_PG3;
25441da177e4SLinus Torvalds 			ddvtprintk(("Set reload IOC Pg3 Flag\n"));
25451da177e4SLinus Torvalds 		}
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Post-Reset complete.\n", ioc->name));
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	}
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 	return 1;		/* currently means nothing really */
25521da177e4SLinus Torvalds }
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
25550d0c7974SMoore, Eric Dean  int
25561da177e4SLinus Torvalds mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
25571da177e4SLinus Torvalds {
25581da177e4SLinus Torvalds 	MPT_SCSI_HOST *hd;
25591da177e4SLinus Torvalds 	u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds 	devtprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
25621da177e4SLinus Torvalds 			ioc->name, event));
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 	switch (event) {
25651da177e4SLinus Torvalds 	case MPI_EVENT_UNIT_ATTENTION:			/* 03 */
25661da177e4SLinus Torvalds 		/* FIXME! */
25671da177e4SLinus Torvalds 		break;
25681da177e4SLinus Torvalds 	case MPI_EVENT_IOC_BUS_RESET:			/* 04 */
25691da177e4SLinus Torvalds 	case MPI_EVENT_EXT_BUS_RESET:			/* 05 */
25701da177e4SLinus Torvalds 		hd = NULL;
25711da177e4SLinus Torvalds 		if (ioc->sh) {
25721da177e4SLinus Torvalds 			hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
25731da177e4SLinus Torvalds 			if (hd && (ioc->bus_type == SCSI) && (hd->soft_resets < -1))
25741da177e4SLinus Torvalds 				hd->soft_resets++;
25751da177e4SLinus Torvalds 		}
25761da177e4SLinus Torvalds 		break;
25771da177e4SLinus Torvalds 	case MPI_EVENT_LOGOUT:				/* 09 */
25781da177e4SLinus Torvalds 		/* FIXME! */
25791da177e4SLinus Torvalds 		break;
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds 		/*
25821da177e4SLinus Torvalds 		 *  CHECKME! Don't think we need to do
25831da177e4SLinus Torvalds 		 *  anything for these, but...
25841da177e4SLinus Torvalds 		 */
25851da177e4SLinus Torvalds 	case MPI_EVENT_RESCAN:				/* 06 */
25861da177e4SLinus Torvalds 	case MPI_EVENT_LINK_STATUS_CHANGE:		/* 07 */
25871da177e4SLinus Torvalds 	case MPI_EVENT_LOOP_STATE_CHANGE:		/* 08 */
25881da177e4SLinus Torvalds 		/*
25891da177e4SLinus Torvalds 		 *  CHECKME!  Falling thru...
25901da177e4SLinus Torvalds 		 */
25911da177e4SLinus Torvalds 		break;
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds 	case MPI_EVENT_INTEGRATED_RAID:			/* 0B */
25941da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
25951da177e4SLinus Torvalds 		/* negoNvram set to 0 if DV enabled and to USE_NVRAM if
25961da177e4SLinus Torvalds 		 * if DV disabled. Need to check for target mode.
25971da177e4SLinus Torvalds 		 */
25981da177e4SLinus Torvalds 		hd = NULL;
25991da177e4SLinus Torvalds 		if (ioc->sh)
26001da177e4SLinus Torvalds 			hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 		if (hd && (ioc->bus_type == SCSI) && (hd->negoNvram == 0)) {
26031da177e4SLinus Torvalds 			ScsiCfgData	*pSpi;
26041da177e4SLinus Torvalds 			Ioc3PhysDisk_t	*pPDisk;
26051da177e4SLinus Torvalds 			int		 numPDisk;
26061da177e4SLinus Torvalds 			u8		 reason;
26071da177e4SLinus Torvalds 			u8		 physDiskNum;
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds 			reason = (le32_to_cpu(pEvReply->Data[0]) & 0x00FF0000) >> 16;
26101da177e4SLinus Torvalds 			if (reason == MPI_EVENT_RAID_RC_DOMAIN_VAL_NEEDED) {
26111da177e4SLinus Torvalds 				/* New or replaced disk.
26121da177e4SLinus Torvalds 				 * Set DV flag and schedule DV.
26131da177e4SLinus Torvalds 				 */
26141da177e4SLinus Torvalds 				pSpi = &ioc->spi_data;
26151da177e4SLinus Torvalds 				physDiskNum = (le32_to_cpu(pEvReply->Data[0]) & 0xFF000000) >> 24;
26161da177e4SLinus Torvalds 				ddvtprintk(("DV requested for phys disk id %d\n", physDiskNum));
26171da177e4SLinus Torvalds 				if (pSpi->pIocPg3) {
26181da177e4SLinus Torvalds 					pPDisk =  pSpi->pIocPg3->PhysDisk;
26191da177e4SLinus Torvalds 					numPDisk =pSpi->pIocPg3->NumPhysDisks;
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds 					while (numPDisk) {
26221da177e4SLinus Torvalds 						if (physDiskNum == pPDisk->PhysDiskNum) {
26231da177e4SLinus Torvalds 							pSpi->dvStatus[pPDisk->PhysDiskID] = (MPT_SCSICFG_NEED_DV | MPT_SCSICFG_DV_NOT_DONE);
26241da177e4SLinus Torvalds 							pSpi->forceDv = MPT_SCSICFG_NEED_DV;
26251da177e4SLinus Torvalds 							ddvtprintk(("NEED_DV set for phys disk id %d\n", pPDisk->PhysDiskID));
26261da177e4SLinus Torvalds 							break;
26271da177e4SLinus Torvalds 						}
26281da177e4SLinus Torvalds 						pPDisk++;
26291da177e4SLinus Torvalds 						numPDisk--;
26301da177e4SLinus Torvalds 					}
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds 					if (numPDisk == 0) {
26331da177e4SLinus Torvalds 						/* The physical disk that needs DV was not found
26341da177e4SLinus Torvalds 						 * in the stored IOC Page 3. The driver must reload
26351da177e4SLinus Torvalds 						 * this page. DV routine will set the NEED_DV flag for
26361da177e4SLinus Torvalds 						 * all phys disks that have DV_NOT_DONE set.
26371da177e4SLinus Torvalds 						 */
26381da177e4SLinus Torvalds 						pSpi->forceDv = MPT_SCSICFG_NEED_DV | MPT_SCSICFG_RELOAD_IOC_PG3;
26391da177e4SLinus Torvalds 						ddvtprintk(("phys disk %d not found. Setting reload IOC Pg3 Flag\n", physDiskNum));
26401da177e4SLinus Torvalds 					}
26411da177e4SLinus Torvalds 				}
26421da177e4SLinus Torvalds 			}
26431da177e4SLinus Torvalds 		}
26441da177e4SLinus Torvalds #endif
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds #if defined(MPT_DEBUG_DV) || defined(MPT_DEBUG_DV_TINY)
26471da177e4SLinus Torvalds 		printk("Raid Event RF: ");
26481da177e4SLinus Torvalds 		{
26491da177e4SLinus Torvalds 			u32 *m = (u32 *)pEvReply;
26501da177e4SLinus Torvalds 			int ii;
26511da177e4SLinus Torvalds 			int n = (int)pEvReply->MsgLength;
26521da177e4SLinus Torvalds 			for (ii=6; ii < n; ii++)
26531da177e4SLinus Torvalds 				printk(" %08x", le32_to_cpu(m[ii]));
26541da177e4SLinus Torvalds 			printk("\n");
26551da177e4SLinus Torvalds 		}
26561da177e4SLinus Torvalds #endif
26571da177e4SLinus Torvalds 		break;
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 	case MPI_EVENT_NONE:				/* 00 */
26601da177e4SLinus Torvalds 	case MPI_EVENT_LOG_DATA:			/* 01 */
26611da177e4SLinus Torvalds 	case MPI_EVENT_STATE_CHANGE:			/* 02 */
26621da177e4SLinus Torvalds 	case MPI_EVENT_EVENT_CHANGE:			/* 0A */
26631da177e4SLinus Torvalds 	default:
26641da177e4SLinus Torvalds 		dprintk((KERN_INFO "  Ignoring event (=%02Xh)\n", event));
26651da177e4SLinus Torvalds 		break;
26661da177e4SLinus Torvalds 	}
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds 	return 1;		/* currently means nothing really */
26691da177e4SLinus Torvalds }
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
26721da177e4SLinus Torvalds /*
26731da177e4SLinus Torvalds  *	mptscsih_initTarget - Target, LUN alloc/free functionality.
26741da177e4SLinus Torvalds  *	@hd: Pointer to MPT_SCSI_HOST structure
26751da177e4SLinus Torvalds  *	@bus_id: Bus number (?)
26761da177e4SLinus Torvalds  *	@target_id: SCSI target id
26771da177e4SLinus Torvalds  *	@lun: SCSI LUN id
26781da177e4SLinus Torvalds  *	@data: Pointer to data
26791da177e4SLinus Torvalds  *	@dlen: Number of INQUIRY bytes
26801da177e4SLinus Torvalds  *
26811da177e4SLinus Torvalds  *	NOTE: It's only SAFE to call this routine if data points to
26821da177e4SLinus Torvalds  *	sane & valid STANDARD INQUIRY data!
26831da177e4SLinus Torvalds  *
26841da177e4SLinus Torvalds  *	Allocate and initialize memory for this target.
26851da177e4SLinus Torvalds  *	Save inquiry data.
26861da177e4SLinus Torvalds  *
26871da177e4SLinus Torvalds  */
26881da177e4SLinus Torvalds static void
26891da177e4SLinus Torvalds mptscsih_initTarget(MPT_SCSI_HOST *hd, int bus_id, int target_id, u8 lun, char *data, int dlen)
26901da177e4SLinus Torvalds {
26911da177e4SLinus Torvalds 	int		indexed_lun, lun_index;
26921da177e4SLinus Torvalds 	VirtDevice	*vdev;
26931da177e4SLinus Torvalds 	ScsiCfgData	*pSpi;
26941da177e4SLinus Torvalds 	char		data_56;
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	dinitprintk((MYIOC_s_INFO_FMT "initTarget bus=%d id=%d lun=%d hd=%p\n",
26971da177e4SLinus Torvalds 			hd->ioc->name, bus_id, target_id, lun, hd));
26981da177e4SLinus Torvalds 
26991da177e4SLinus Torvalds 	/*
27001da177e4SLinus Torvalds 	 * If the peripheral qualifier filter is enabled then if the target reports a 0x1
27011da177e4SLinus Torvalds 	 * (i.e. The targer is capable of supporting the specified peripheral device type
27021da177e4SLinus Torvalds 	 * on this logical unit; however, the physical device is not currently connected
27031da177e4SLinus Torvalds 	 * to this logical unit) it will be converted to a 0x3 (i.e. The target is not
27041da177e4SLinus Torvalds 	 * capable of supporting a physical device on this logical unit). This is to work
27051da177e4SLinus Torvalds 	 * around a bug in th emid-layer in some distributions in which the mid-layer will
27061da177e4SLinus Torvalds 	 * continue to try to communicate to the LUN and evntually create a dummy LUN.
27071da177e4SLinus Torvalds 	*/
27080d0c7974SMoore, Eric Dean  	if (hd->mpt_pq_filter && dlen && (data[0] & 0xE0))
27091da177e4SLinus Torvalds 		data[0] |= 0x40;
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds 	/* Is LUN supported? If so, upper 2 bits will be 0
27121da177e4SLinus Torvalds 	* in first byte of inquiry data.
27131da177e4SLinus Torvalds 	*/
27141da177e4SLinus Torvalds 	if (data[0] & 0xe0)
27151da177e4SLinus Torvalds 		return;
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds 	if ((vdev = hd->Targets[target_id]) == NULL) {
27181da177e4SLinus Torvalds 		return;
27191da177e4SLinus Torvalds 	}
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds 	lun_index = (lun >> 5);  /* 32 luns per lun_index */
27221da177e4SLinus Torvalds 	indexed_lun = (lun % 32);
27231da177e4SLinus Torvalds 	vdev->luns[lun_index] |= (1 << indexed_lun);
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds 	if (hd->ioc->bus_type == SCSI) {
27261da177e4SLinus Torvalds 		if ((data[0] == TYPE_PROCESSOR) && (hd->ioc->spi_data.Saf_Te)) {
27271da177e4SLinus Torvalds 			/* Treat all Processors as SAF-TE if
27281da177e4SLinus Torvalds 			 * command line option is set */
27291da177e4SLinus Torvalds 			vdev->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
27301da177e4SLinus Torvalds 			mptscsih_writeIOCPage4(hd, target_id, bus_id);
27311da177e4SLinus Torvalds 		}else if ((data[0] == TYPE_PROCESSOR) &&
27321da177e4SLinus Torvalds 			!(vdev->tflags & MPT_TARGET_FLAGS_SAF_TE_ISSUED )) {
27331da177e4SLinus Torvalds 			if ( dlen > 49 ) {
27341da177e4SLinus Torvalds 				vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
27351da177e4SLinus Torvalds 				if ( data[44] == 'S' &&
27361da177e4SLinus Torvalds 				     data[45] == 'A' &&
27371da177e4SLinus Torvalds 				     data[46] == 'F' &&
27381da177e4SLinus Torvalds 				     data[47] == '-' &&
27391da177e4SLinus Torvalds 				     data[48] == 'T' &&
27401da177e4SLinus Torvalds 				     data[49] == 'E' ) {
27411da177e4SLinus Torvalds 					vdev->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
27421da177e4SLinus Torvalds 					mptscsih_writeIOCPage4(hd, target_id, bus_id);
27431da177e4SLinus Torvalds 				}
27441da177e4SLinus Torvalds 			}
27451da177e4SLinus Torvalds 		}
27461da177e4SLinus Torvalds 		if (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY)) {
27471da177e4SLinus Torvalds 			if ( dlen > 8 ) {
27481da177e4SLinus Torvalds 				memcpy (vdev->inq_data, data, 8);
27491da177e4SLinus Torvalds 			} else {
27501da177e4SLinus Torvalds 				memcpy (vdev->inq_data, data, dlen);
27511da177e4SLinus Torvalds 			}
27521da177e4SLinus Torvalds 
27531da177e4SLinus Torvalds 			/* If have not done DV, set the DV flag.
27541da177e4SLinus Torvalds 			 */
27551da177e4SLinus Torvalds 			pSpi = &hd->ioc->spi_data;
27561da177e4SLinus Torvalds 			if ((data[0] == TYPE_TAPE) || (data[0] == TYPE_PROCESSOR)) {
27571da177e4SLinus Torvalds 				if (pSpi->dvStatus[target_id] & MPT_SCSICFG_DV_NOT_DONE)
27581da177e4SLinus Torvalds 					pSpi->dvStatus[target_id] |= MPT_SCSICFG_NEED_DV;
27591da177e4SLinus Torvalds 			}
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 			vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
27621da177e4SLinus Torvalds 
27631da177e4SLinus Torvalds 
27641da177e4SLinus Torvalds 			data_56 = 0x0F;  /* Default to full capabilities if Inq data length is < 57 */
27651da177e4SLinus Torvalds 			if (dlen > 56) {
27661da177e4SLinus Torvalds 				if ( (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_56))) {
27671da177e4SLinus Torvalds 				/* Update the target capabilities
27681da177e4SLinus Torvalds 				 */
27691da177e4SLinus Torvalds 					data_56 = data[56];
27701da177e4SLinus Torvalds 					vdev->tflags |= MPT_TARGET_FLAGS_VALID_56;
27711da177e4SLinus Torvalds 				}
27721da177e4SLinus Torvalds 			}
27731da177e4SLinus Torvalds 			mptscsih_setTargetNegoParms(hd, vdev, data_56);
27741da177e4SLinus Torvalds 		} else {
27751da177e4SLinus Torvalds 			/* Initial Inquiry may not request enough data bytes to
27761da177e4SLinus Torvalds 			 * obtain byte 57.  DV will; if target doesn't return
27771da177e4SLinus Torvalds 			 * at least 57 bytes, data[56] will be zero. */
27781da177e4SLinus Torvalds 			if (dlen > 56) {
27791da177e4SLinus Torvalds 				if ( (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_56))) {
27801da177e4SLinus Torvalds 				/* Update the target capabilities
27811da177e4SLinus Torvalds 				 */
27821da177e4SLinus Torvalds 					data_56 = data[56];
27831da177e4SLinus Torvalds 					vdev->tflags |= MPT_TARGET_FLAGS_VALID_56;
27841da177e4SLinus Torvalds 					mptscsih_setTargetNegoParms(hd, vdev, data_56);
27851da177e4SLinus Torvalds 				}
27861da177e4SLinus Torvalds 			}
27871da177e4SLinus Torvalds 		}
27881da177e4SLinus Torvalds 	}
27891da177e4SLinus Torvalds }
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
27921da177e4SLinus Torvalds /*
27931da177e4SLinus Torvalds  *  Update the target negotiation parameters based on the
27941da177e4SLinus Torvalds  *  the Inquiry data, adapter capabilities, and NVRAM settings.
27951da177e4SLinus Torvalds  *
27961da177e4SLinus Torvalds  */
27971da177e4SLinus Torvalds static void
27981da177e4SLinus Torvalds mptscsih_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtDevice *target, char byte56)
27991da177e4SLinus Torvalds {
28001da177e4SLinus Torvalds 	ScsiCfgData *pspi_data = &hd->ioc->spi_data;
28011da177e4SLinus Torvalds 	int  id = (int) target->target_id;
28021da177e4SLinus Torvalds 	int  nvram;
28031da177e4SLinus Torvalds 	VirtDevice	*vdev;
28041da177e4SLinus Torvalds 	int ii;
28051da177e4SLinus Torvalds 	u8 width = MPT_NARROW;
28061da177e4SLinus Torvalds 	u8 factor = MPT_ASYNC;
28071da177e4SLinus Torvalds 	u8 offset = 0;
28081da177e4SLinus Torvalds 	u8 version, nfactor;
28091da177e4SLinus Torvalds 	u8 noQas = 1;
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds 	target->negoFlags = pspi_data->noQas;
28121da177e4SLinus Torvalds 
28131da177e4SLinus Torvalds 	/* noQas == 0 => device supports QAS. Need byte 56 of Inq to determine
28141da177e4SLinus Torvalds 	 * support. If available, default QAS to off and allow enabling.
28151da177e4SLinus Torvalds 	 * If not available, default QAS to on, turn off for non-disks.
28161da177e4SLinus Torvalds 	 */
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds 	/* Set flags based on Inquiry data
28191da177e4SLinus Torvalds 	 */
28201da177e4SLinus Torvalds 	version = target->inq_data[2] & 0x07;
28211da177e4SLinus Torvalds 	if (version < 2) {
28221da177e4SLinus Torvalds 		width = 0;
28231da177e4SLinus Torvalds 		factor = MPT_ULTRA2;
28241da177e4SLinus Torvalds 		offset = pspi_data->maxSyncOffset;
28251da177e4SLinus Torvalds 		target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
28261da177e4SLinus Torvalds 	} else {
28271da177e4SLinus Torvalds 		if (target->inq_data[7] & 0x20) {
28281da177e4SLinus Torvalds 			width = 1;
28291da177e4SLinus Torvalds 		}
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds 		if (target->inq_data[7] & 0x10) {
28321da177e4SLinus Torvalds 			factor = pspi_data->minSyncFactor;
28331da177e4SLinus Torvalds 			if (target->tflags & MPT_TARGET_FLAGS_VALID_56) {
28341da177e4SLinus Torvalds 				/* bits 2 & 3 show Clocking support */
28351da177e4SLinus Torvalds 				if ((byte56 & 0x0C) == 0)
28361da177e4SLinus Torvalds 					factor = MPT_ULTRA2;
28371da177e4SLinus Torvalds 				else {
28381da177e4SLinus Torvalds 					if ((byte56 & 0x03) == 0)
28391da177e4SLinus Torvalds 						factor = MPT_ULTRA160;
28401da177e4SLinus Torvalds 					else {
28411da177e4SLinus Torvalds 						factor = MPT_ULTRA320;
28421da177e4SLinus Torvalds 						if (byte56 & 0x02)
28431da177e4SLinus Torvalds 						{
28441da177e4SLinus Torvalds 							ddvtprintk((KERN_INFO "Enabling QAS due to byte56=%02x on id=%d!\n", byte56, id));
28451da177e4SLinus Torvalds 							noQas = 0;
28461da177e4SLinus Torvalds 						}
28471da177e4SLinus Torvalds 						if (target->inq_data[0] == TYPE_TAPE) {
28481da177e4SLinus Torvalds 							if (byte56 & 0x01)
28491da177e4SLinus Torvalds 								target->negoFlags |= MPT_TAPE_NEGO_IDP;
28501da177e4SLinus Torvalds 						}
28511da177e4SLinus Torvalds 					}
28521da177e4SLinus Torvalds 				}
28531da177e4SLinus Torvalds 			} else {
28541da177e4SLinus Torvalds 				ddvtprintk((KERN_INFO "Enabling QAS on id=%d due to ~TARGET_FLAGS_VALID_56!\n", id));
28551da177e4SLinus Torvalds 				noQas = 0;
28561da177e4SLinus Torvalds 			}
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds 			offset = pspi_data->maxSyncOffset;
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 			/* If RAID, never disable QAS
28611da177e4SLinus Torvalds 			 * else if non RAID, do not disable
28621da177e4SLinus Torvalds 			 *   QAS if bit 1 is set
28631da177e4SLinus Torvalds 			 * bit 1 QAS support, non-raid only
28641da177e4SLinus Torvalds 			 * bit 0 IU support
28651da177e4SLinus Torvalds 			 */
28661da177e4SLinus Torvalds 			if (target->raidVolume == 1) {
28671da177e4SLinus Torvalds 				noQas = 0;
28681da177e4SLinus Torvalds 			}
28691da177e4SLinus Torvalds 		} else {
28701da177e4SLinus Torvalds 			factor = MPT_ASYNC;
28711da177e4SLinus Torvalds 			offset = 0;
28721da177e4SLinus Torvalds 		}
28731da177e4SLinus Torvalds 	}
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 	if ( (target->inq_data[7] & 0x02) == 0) {
28761da177e4SLinus Torvalds 		target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
28771da177e4SLinus Torvalds 	}
28781da177e4SLinus Torvalds 
28791da177e4SLinus Torvalds 	/* Update tflags based on NVRAM settings. (SCSI only)
28801da177e4SLinus Torvalds 	 */
28811da177e4SLinus Torvalds 	if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
28821da177e4SLinus Torvalds 		nvram = pspi_data->nvram[id];
28831da177e4SLinus Torvalds 		nfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
28841da177e4SLinus Torvalds 
28851da177e4SLinus Torvalds 		if (width)
28861da177e4SLinus Torvalds 			width = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
28871da177e4SLinus Torvalds 
28881da177e4SLinus Torvalds 		if (offset > 0) {
28891da177e4SLinus Torvalds 			/* Ensure factor is set to the
28901da177e4SLinus Torvalds 			 * maximum of: adapter, nvram, inquiry
28911da177e4SLinus Torvalds 			 */
28921da177e4SLinus Torvalds 			if (nfactor) {
28931da177e4SLinus Torvalds 				if (nfactor < pspi_data->minSyncFactor )
28941da177e4SLinus Torvalds 					nfactor = pspi_data->minSyncFactor;
28951da177e4SLinus Torvalds 
28961da177e4SLinus Torvalds 				factor = max(factor, nfactor);
28971da177e4SLinus Torvalds 				if (factor == MPT_ASYNC)
28981da177e4SLinus Torvalds 					offset = 0;
28991da177e4SLinus Torvalds 			} else {
29001da177e4SLinus Torvalds 				offset = 0;
29011da177e4SLinus Torvalds 				factor = MPT_ASYNC;
29021da177e4SLinus Torvalds 		}
29031da177e4SLinus Torvalds 		} else {
29041da177e4SLinus Torvalds 			factor = MPT_ASYNC;
29051da177e4SLinus Torvalds 		}
29061da177e4SLinus Torvalds 	}
29071da177e4SLinus Torvalds 
29081da177e4SLinus Torvalds 	/* Make sure data is consistent
29091da177e4SLinus Torvalds 	 */
29101da177e4SLinus Torvalds 	if ((!width) && (factor < MPT_ULTRA2)) {
29111da177e4SLinus Torvalds 		factor = MPT_ULTRA2;
29121da177e4SLinus Torvalds 	}
29131da177e4SLinus Torvalds 
29141da177e4SLinus Torvalds 	/* Save the data to the target structure.
29151da177e4SLinus Torvalds 	 */
29161da177e4SLinus Torvalds 	target->minSyncFactor = factor;
29171da177e4SLinus Torvalds 	target->maxOffset = offset;
29181da177e4SLinus Torvalds 	target->maxWidth = width;
29191da177e4SLinus Torvalds 
29201da177e4SLinus Torvalds 	target->tflags |= MPT_TARGET_FLAGS_VALID_NEGO;
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds 	/* Disable unused features.
29231da177e4SLinus Torvalds 	 */
29241da177e4SLinus Torvalds 	if (!width)
29251da177e4SLinus Torvalds 		target->negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds 	if (!offset)
29281da177e4SLinus Torvalds 		target->negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds 	if ( factor > MPT_ULTRA320 )
29311da177e4SLinus Torvalds 		noQas = 0;
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds 	/* GEM, processor WORKAROUND
29341da177e4SLinus Torvalds 	 */
29351da177e4SLinus Torvalds 	if ((target->inq_data[0] == TYPE_PROCESSOR) || (target->inq_data[0] > 0x08)) {
29361da177e4SLinus Torvalds 		target->negoFlags |= (MPT_TARGET_NO_NEGO_WIDE | MPT_TARGET_NO_NEGO_SYNC);
29371da177e4SLinus Torvalds 		pspi_data->dvStatus[id] |= MPT_SCSICFG_BLK_NEGO;
29381da177e4SLinus Torvalds 	} else {
29391da177e4SLinus Torvalds 		if (noQas && (pspi_data->noQas == 0)) {
29401da177e4SLinus Torvalds 			pspi_data->noQas |= MPT_TARGET_NO_NEGO_QAS;
29411da177e4SLinus Torvalds 			target->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds 			/* Disable QAS in a mixed configuration case
29441da177e4SLinus Torvalds 	 		*/
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds 			ddvtprintk((KERN_INFO "Disabling QAS due to noQas=%02x on id=%d!\n", noQas, id));
29471da177e4SLinus Torvalds 			for (ii = 0; ii < id; ii++) {
29481da177e4SLinus Torvalds 				if ( (vdev = hd->Targets[ii]) ) {
29491da177e4SLinus Torvalds 					vdev->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
29501da177e4SLinus Torvalds 					mptscsih_writeSDP1(hd, 0, ii, vdev->negoFlags);
29511da177e4SLinus Torvalds 				}
29521da177e4SLinus Torvalds 			}
29531da177e4SLinus Torvalds 		}
29541da177e4SLinus Torvalds 	}
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	/* Write SDP1 on this I/O to this target */
29571da177e4SLinus Torvalds 	if (pspi_data->dvStatus[id] & MPT_SCSICFG_NEGOTIATE) {
29581da177e4SLinus Torvalds 		ddvtprintk((KERN_INFO "MPT_SCSICFG_NEGOTIATE on id=%d!\n", id));
29591da177e4SLinus Torvalds 		mptscsih_writeSDP1(hd, 0, id, hd->negoNvram);
29601da177e4SLinus Torvalds 		pspi_data->dvStatus[id] &= ~MPT_SCSICFG_NEGOTIATE;
29611da177e4SLinus Torvalds 	} else if (pspi_data->dvStatus[id] & MPT_SCSICFG_BLK_NEGO) {
29621da177e4SLinus Torvalds 		ddvtprintk((KERN_INFO "MPT_SCSICFG_BLK_NEGO on id=%d!\n", id));
29631da177e4SLinus Torvalds 		mptscsih_writeSDP1(hd, 0, id, MPT_SCSICFG_BLK_NEGO);
29641da177e4SLinus Torvalds 		pspi_data->dvStatus[id] &= ~MPT_SCSICFG_BLK_NEGO;
29651da177e4SLinus Torvalds 	}
29661da177e4SLinus Torvalds }
29671da177e4SLinus Torvalds 
29681da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
29691da177e4SLinus Torvalds /* If DV disabled (negoNvram set to USE_NVARM) or if not LUN 0, return.
29701da177e4SLinus Torvalds  * Else set the NEED_DV flag after Read Capacity Issued (disks)
29711da177e4SLinus Torvalds  * or Mode Sense (cdroms).
29721da177e4SLinus Torvalds  *
29731da177e4SLinus Torvalds  * Tapes, initTarget will set this flag on completion of Inquiry command.
29741da177e4SLinus Torvalds  * Called only if DV_NOT_DONE flag is set
29751da177e4SLinus Torvalds  */
29760d0c7974SMoore, Eric Dean  static void
29770d0c7974SMoore, Eric Dean  mptscsih_set_dvflags(MPT_SCSI_HOST *hd, SCSIIORequest_t *pReq)
29781da177e4SLinus Torvalds {
29791da177e4SLinus Torvalds 	u8 cmd;
29801da177e4SLinus Torvalds 	ScsiCfgData *pSpi;
29811da177e4SLinus Torvalds 
29821da177e4SLinus Torvalds 	ddvtprintk((" set_dvflags: id=%d lun=%d negoNvram=%x cmd=%x\n",
29831da177e4SLinus Torvalds 		pReq->TargetID, pReq->LUN[1], hd->negoNvram, pReq->CDB[0]));
29841da177e4SLinus Torvalds 
29851da177e4SLinus Torvalds 	if ((pReq->LUN[1] != 0) || (hd->negoNvram != 0))
29861da177e4SLinus Torvalds 		return;
29871da177e4SLinus Torvalds 
29881da177e4SLinus Torvalds 	cmd = pReq->CDB[0];
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds 	if ((cmd == READ_CAPACITY) || (cmd == MODE_SENSE)) {
29911da177e4SLinus Torvalds 		pSpi = &hd->ioc->spi_data;
29921da177e4SLinus Torvalds 		if ((pSpi->isRaid & (1 << pReq->TargetID)) && pSpi->pIocPg3) {
29931da177e4SLinus Torvalds 			/* Set NEED_DV for all hidden disks
29941da177e4SLinus Torvalds 			 */
29951da177e4SLinus Torvalds 			Ioc3PhysDisk_t *pPDisk =  pSpi->pIocPg3->PhysDisk;
29961da177e4SLinus Torvalds 			int		numPDisk = pSpi->pIocPg3->NumPhysDisks;
29971da177e4SLinus Torvalds 
29981da177e4SLinus Torvalds 			while (numPDisk) {
29991da177e4SLinus Torvalds 				pSpi->dvStatus[pPDisk->PhysDiskID] |= MPT_SCSICFG_NEED_DV;
30001da177e4SLinus Torvalds 				ddvtprintk(("NEED_DV set for phys disk id %d\n", pPDisk->PhysDiskID));
30011da177e4SLinus Torvalds 				pPDisk++;
30021da177e4SLinus Torvalds 				numPDisk--;
30031da177e4SLinus Torvalds 			}
30041da177e4SLinus Torvalds 		}
30051da177e4SLinus Torvalds 		pSpi->dvStatus[pReq->TargetID] |= MPT_SCSICFG_NEED_DV;
30061da177e4SLinus Torvalds 		ddvtprintk(("NEED_DV set for visible disk id %d\n", pReq->TargetID));
30071da177e4SLinus Torvalds 	}
30081da177e4SLinus Torvalds }
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
30111da177e4SLinus Torvalds /*
30121da177e4SLinus Torvalds  * If no Target, bus reset on 1st I/O. Set the flag to
30131da177e4SLinus Torvalds  * prevent any future negotiations to this device.
30141da177e4SLinus Torvalds  */
30150d0c7974SMoore, Eric Dean  static void
30160d0c7974SMoore, Eric Dean  mptscsih_no_negotiate(MPT_SCSI_HOST *hd, int target_id)
30171da177e4SLinus Torvalds {
30181da177e4SLinus Torvalds 
30191da177e4SLinus Torvalds 	if ((hd->Targets) && (hd->Targets[target_id] == NULL))
30201da177e4SLinus Torvalds 		hd->ioc->spi_data.dvStatus[target_id] |= MPT_SCSICFG_BLK_NEGO;
30211da177e4SLinus Torvalds 
30221da177e4SLinus Torvalds 	return;
30231da177e4SLinus Torvalds }
30241da177e4SLinus Torvalds 
30251da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
30261da177e4SLinus Torvalds /*
30271da177e4SLinus Torvalds  *  SCSI Config Page functionality ...
30281da177e4SLinus Torvalds  */
30291da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
30301da177e4SLinus Torvalds /*	mptscsih_setDevicePage1Flags  - add Requested and Configuration fields flags
30311da177e4SLinus Torvalds  *	based on width, factor and offset parameters.
30321da177e4SLinus Torvalds  *	@width: bus width
30331da177e4SLinus Torvalds  *	@factor: sync factor
30341da177e4SLinus Torvalds  *	@offset: sync offset
30351da177e4SLinus Torvalds  *	@requestedPtr: pointer to requested values (updated)
30361da177e4SLinus Torvalds  *	@configurationPtr: pointer to configuration values (updated)
30371da177e4SLinus Torvalds  *	@flags: flags to block WDTR or SDTR negotiation
30381da177e4SLinus Torvalds  *
30391da177e4SLinus Torvalds  *	Return: None.
30401da177e4SLinus Torvalds  *
30411da177e4SLinus Torvalds  *	Remark: Called by writeSDP1 and _dv_params
30421da177e4SLinus Torvalds  */
30431da177e4SLinus Torvalds static void
30441da177e4SLinus Torvalds mptscsih_setDevicePage1Flags (u8 width, u8 factor, u8 offset, int *requestedPtr, int *configurationPtr, u8 flags)
30451da177e4SLinus Torvalds {
30461da177e4SLinus Torvalds 	u8 nowide = flags & MPT_TARGET_NO_NEGO_WIDE;
30471da177e4SLinus Torvalds 	u8 nosync = flags & MPT_TARGET_NO_NEGO_SYNC;
30481da177e4SLinus Torvalds 
30491da177e4SLinus Torvalds 	*configurationPtr = 0;
30501da177e4SLinus Torvalds 	*requestedPtr = width ? MPI_SCSIDEVPAGE1_RP_WIDE : 0;
30511da177e4SLinus Torvalds 	*requestedPtr |= (offset << 16) | (factor << 8);
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds 	if (width && offset && !nowide && !nosync) {
30541da177e4SLinus Torvalds 		if (factor < MPT_ULTRA160) {
30551da177e4SLinus Torvalds 			*requestedPtr |= (MPI_SCSIDEVPAGE1_RP_IU + MPI_SCSIDEVPAGE1_RP_DT);
30561da177e4SLinus Torvalds 			if ((flags & MPT_TARGET_NO_NEGO_QAS) == 0)
30571da177e4SLinus Torvalds 				*requestedPtr |= MPI_SCSIDEVPAGE1_RP_QAS;
30581da177e4SLinus Torvalds 			if (flags & MPT_TAPE_NEGO_IDP)
30591da177e4SLinus Torvalds 				*requestedPtr |= 0x08000000;
30601da177e4SLinus Torvalds 		} else if (factor < MPT_ULTRA2) {
30611da177e4SLinus Torvalds 			*requestedPtr |= MPI_SCSIDEVPAGE1_RP_DT;
30621da177e4SLinus Torvalds 		}
30631da177e4SLinus Torvalds 	}
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 	if (nowide)
30661da177e4SLinus Torvalds 		*configurationPtr |= MPI_SCSIDEVPAGE1_CONF_WDTR_DISALLOWED;
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds 	if (nosync)
30691da177e4SLinus Torvalds 		*configurationPtr |= MPI_SCSIDEVPAGE1_CONF_SDTR_DISALLOWED;
30701da177e4SLinus Torvalds 
30711da177e4SLinus Torvalds 	return;
30721da177e4SLinus Torvalds }
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
30751da177e4SLinus Torvalds /*	mptscsih_writeSDP1  - write SCSI Device Page 1
30761da177e4SLinus Torvalds  *	@hd: Pointer to a SCSI Host Strucutre
30771da177e4SLinus Torvalds  *	@portnum: IOC port number
30781da177e4SLinus Torvalds  *	@target_id: writeSDP1 for single ID
30791da177e4SLinus Torvalds  *	@flags: MPT_SCSICFG_ALL_IDS, MPT_SCSICFG_USE_NVRAM, MPT_SCSICFG_BLK_NEGO
30801da177e4SLinus Torvalds  *
30811da177e4SLinus Torvalds  *	Return: -EFAULT if read of config page header fails
30821da177e4SLinus Torvalds  *		or 0 if success.
30831da177e4SLinus Torvalds  *
30841da177e4SLinus Torvalds  *	Remark: If a target has been found, the settings from the
30851da177e4SLinus Torvalds  *		target structure are used, else the device is set
30861da177e4SLinus Torvalds  *		to async/narrow.
30871da177e4SLinus Torvalds  *
30881da177e4SLinus Torvalds  *	Remark: Called during init and after a FW reload.
30891da177e4SLinus Torvalds  *	Remark: We do not wait for a return, write pages sequentially.
30901da177e4SLinus Torvalds  */
30911da177e4SLinus Torvalds static int
30921da177e4SLinus Torvalds mptscsih_writeSDP1(MPT_SCSI_HOST *hd, int portnum, int target_id, int flags)
30931da177e4SLinus Torvalds {
30941da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc = hd->ioc;
30951da177e4SLinus Torvalds 	Config_t		*pReq;
30961da177e4SLinus Torvalds 	SCSIDevicePage1_t	*pData;
30971da177e4SLinus Torvalds 	VirtDevice		*pTarget;
30981da177e4SLinus Torvalds 	MPT_FRAME_HDR		*mf;
30991da177e4SLinus Torvalds 	dma_addr_t		 dataDma;
31001da177e4SLinus Torvalds 	u16			 req_idx;
31011da177e4SLinus Torvalds 	u32			 frameOffset;
31021da177e4SLinus Torvalds 	u32			 requested, configuration, flagsLength;
31031da177e4SLinus Torvalds 	int			 ii, nvram;
31041da177e4SLinus Torvalds 	int			 id = 0, maxid = 0;
31051da177e4SLinus Torvalds 	u8			 width;
31061da177e4SLinus Torvalds 	u8			 factor;
31071da177e4SLinus Torvalds 	u8			 offset;
31081da177e4SLinus Torvalds 	u8			 bus = 0;
31091da177e4SLinus Torvalds 	u8			 negoFlags;
31101da177e4SLinus Torvalds 	u8			 maxwidth, maxoffset, maxfactor;
31111da177e4SLinus Torvalds 
31121da177e4SLinus Torvalds 	if (ioc->spi_data.sdp1length == 0)
31131da177e4SLinus Torvalds 		return 0;
31141da177e4SLinus Torvalds 
31151da177e4SLinus Torvalds 	if (flags & MPT_SCSICFG_ALL_IDS) {
31161da177e4SLinus Torvalds 		id = 0;
31171da177e4SLinus Torvalds 		maxid = ioc->sh->max_id - 1;
31181da177e4SLinus Torvalds 	} else if (ioc->sh) {
31191da177e4SLinus Torvalds 		id = target_id;
31201da177e4SLinus Torvalds 		maxid = min_t(int, id, ioc->sh->max_id - 1);
31211da177e4SLinus Torvalds 	}
31221da177e4SLinus Torvalds 
31231da177e4SLinus Torvalds 	for (; id <= maxid; id++) {
31241da177e4SLinus Torvalds 
31251da177e4SLinus Torvalds 		if (id == ioc->pfacts[portnum].PortSCSIID)
31261da177e4SLinus Torvalds 			continue;
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 		/* Use NVRAM to get adapter and target maximums
31291da177e4SLinus Torvalds 		 * Data over-riden by target structure information, if present
31301da177e4SLinus Torvalds 		 */
31311da177e4SLinus Torvalds 		maxwidth = ioc->spi_data.maxBusWidth;
31321da177e4SLinus Torvalds 		maxoffset = ioc->spi_data.maxSyncOffset;
31331da177e4SLinus Torvalds 		maxfactor = ioc->spi_data.minSyncFactor;
31341da177e4SLinus Torvalds 		if (ioc->spi_data.nvram && (ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
31351da177e4SLinus Torvalds 			nvram = ioc->spi_data.nvram[id];
31361da177e4SLinus Torvalds 
31371da177e4SLinus Torvalds 			if (maxwidth)
31381da177e4SLinus Torvalds 				maxwidth = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
31391da177e4SLinus Torvalds 
31401da177e4SLinus Torvalds 			if (maxoffset > 0) {
31411da177e4SLinus Torvalds 				maxfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
31421da177e4SLinus Torvalds 				if (maxfactor == 0) {
31431da177e4SLinus Torvalds 					/* Key for async */
31441da177e4SLinus Torvalds 					maxfactor = MPT_ASYNC;
31451da177e4SLinus Torvalds 					maxoffset = 0;
31461da177e4SLinus Torvalds 				} else if (maxfactor < ioc->spi_data.minSyncFactor) {
31471da177e4SLinus Torvalds 					maxfactor = ioc->spi_data.minSyncFactor;
31481da177e4SLinus Torvalds 				}
31491da177e4SLinus Torvalds 			} else
31501da177e4SLinus Torvalds 				maxfactor = MPT_ASYNC;
31511da177e4SLinus Torvalds 		}
31521da177e4SLinus Torvalds 
31531da177e4SLinus Torvalds 		/* Set the negotiation flags.
31541da177e4SLinus Torvalds 		 */
31551da177e4SLinus Torvalds 		negoFlags = ioc->spi_data.noQas;
31561da177e4SLinus Torvalds 		if (!maxwidth)
31571da177e4SLinus Torvalds 			negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
31581da177e4SLinus Torvalds 
31591da177e4SLinus Torvalds 		if (!maxoffset)
31601da177e4SLinus Torvalds 			negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds 		if (flags & MPT_SCSICFG_USE_NVRAM) {
31631da177e4SLinus Torvalds 			width = maxwidth;
31641da177e4SLinus Torvalds 			factor = maxfactor;
31651da177e4SLinus Torvalds 			offset = maxoffset;
31661da177e4SLinus Torvalds 		} else {
31671da177e4SLinus Torvalds 			width = 0;
31681da177e4SLinus Torvalds 			factor = MPT_ASYNC;
31691da177e4SLinus Torvalds 			offset = 0;
31701da177e4SLinus Torvalds 			//negoFlags = 0;
31711da177e4SLinus Torvalds 			//negoFlags = MPT_TARGET_NO_NEGO_SYNC;
31721da177e4SLinus Torvalds 		}
31731da177e4SLinus Torvalds 
31741da177e4SLinus Torvalds 		/* If id is not a raid volume, get the updated
31751da177e4SLinus Torvalds 		 * transmission settings from the target structure.
31761da177e4SLinus Torvalds 		 */
31771da177e4SLinus Torvalds 		if (hd->Targets && (pTarget = hd->Targets[id]) && !pTarget->raidVolume) {
31781da177e4SLinus Torvalds 			width = pTarget->maxWidth;
31791da177e4SLinus Torvalds 			factor = pTarget->minSyncFactor;
31801da177e4SLinus Torvalds 			offset = pTarget->maxOffset;
31811da177e4SLinus Torvalds 			negoFlags = pTarget->negoFlags;
31821da177e4SLinus Torvalds 		}
31831da177e4SLinus Torvalds 
31841da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
31851da177e4SLinus Torvalds 		/* Force to async and narrow if DV has not been executed
31861da177e4SLinus Torvalds 		 * for this ID
31871da177e4SLinus Torvalds 		 */
31881da177e4SLinus Torvalds 		if ((hd->ioc->spi_data.dvStatus[id] & MPT_SCSICFG_DV_NOT_DONE) != 0) {
31891da177e4SLinus Torvalds 			width = 0;
31901da177e4SLinus Torvalds 			factor = MPT_ASYNC;
31911da177e4SLinus Torvalds 			offset = 0;
31921da177e4SLinus Torvalds 		}
31931da177e4SLinus Torvalds #endif
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds 		if (flags & MPT_SCSICFG_BLK_NEGO)
31961da177e4SLinus Torvalds 			negoFlags = MPT_TARGET_NO_NEGO_WIDE | MPT_TARGET_NO_NEGO_SYNC;
31971da177e4SLinus Torvalds 
31981da177e4SLinus Torvalds 		mptscsih_setDevicePage1Flags(width, factor, offset,
31991da177e4SLinus Torvalds 					&requested, &configuration, negoFlags);
32001da177e4SLinus Torvalds 		dnegoprintk(("writeSDP1: id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x config=%x\n",
32011da177e4SLinus Torvalds 			target_id, width, factor, offset, negoFlags, requested, configuration));
32021da177e4SLinus Torvalds 
32031da177e4SLinus Torvalds 		/* Get a MF for this command.
32041da177e4SLinus Torvalds 		 */
32050d0c7974SMoore, Eric Dean  		if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
32061da177e4SLinus Torvalds 			dprintk((MYIOC_s_WARN_FMT "write SDP1: no msg frames!\n",
32071da177e4SLinus Torvalds 						ioc->name));
32081da177e4SLinus Torvalds 			return -EAGAIN;
32091da177e4SLinus Torvalds 		}
32101da177e4SLinus Torvalds 
32111da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_INFO_FMT "WriteSDP1 (mf=%p, id=%d, req=0x%x, cfg=0x%x)\n",
32121da177e4SLinus Torvalds 			hd->ioc->name, mf, id, requested, configuration));
32131da177e4SLinus Torvalds 
32141da177e4SLinus Torvalds 
32151da177e4SLinus Torvalds 		/* Set the request and the data pointers.
32161da177e4SLinus Torvalds 		 * Request takes: 36 bytes (32 bit SGE)
32171da177e4SLinus Torvalds 		 * SCSI Device Page 1 requires 16 bytes
32181da177e4SLinus Torvalds 		 * 40 + 16 <= size of SCSI IO Request = 56 bytes
32191da177e4SLinus Torvalds 		 * and MF size >= 64 bytes.
32201da177e4SLinus Torvalds 		 * Place data at end of MF.
32211da177e4SLinus Torvalds 		 */
32221da177e4SLinus Torvalds 		pReq = (Config_t *)mf;
32231da177e4SLinus Torvalds 
32241da177e4SLinus Torvalds 		req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
32251da177e4SLinus Torvalds 		frameOffset = ioc->req_sz - sizeof(SCSIDevicePage1_t);
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds 		pData = (SCSIDevicePage1_t *)((u8 *) mf + frameOffset);
32281da177e4SLinus Torvalds 		dataDma = ioc->req_frames_dma + (req_idx * ioc->req_sz) + frameOffset;
32291da177e4SLinus Torvalds 
32301da177e4SLinus Torvalds 		/* Complete the request frame (same for all requests).
32311da177e4SLinus Torvalds 		 */
32321da177e4SLinus Torvalds 		pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
32331da177e4SLinus Torvalds 		pReq->Reserved = 0;
32341da177e4SLinus Torvalds 		pReq->ChainOffset = 0;
32351da177e4SLinus Torvalds 		pReq->Function = MPI_FUNCTION_CONFIG;
32361da177e4SLinus Torvalds 		pReq->ExtPageLength = 0;
32371da177e4SLinus Torvalds 		pReq->ExtPageType = 0;
32381da177e4SLinus Torvalds 		pReq->MsgFlags = 0;
32391da177e4SLinus Torvalds 		for (ii=0; ii < 8; ii++) {
32401da177e4SLinus Torvalds 			pReq->Reserved2[ii] = 0;
32411da177e4SLinus Torvalds 		}
32421da177e4SLinus Torvalds 		pReq->Header.PageVersion = ioc->spi_data.sdp1version;
32431da177e4SLinus Torvalds 		pReq->Header.PageLength = ioc->spi_data.sdp1length;
32441da177e4SLinus Torvalds 		pReq->Header.PageNumber = 1;
32451da177e4SLinus Torvalds 		pReq->Header.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
32461da177e4SLinus Torvalds 		pReq->PageAddress = cpu_to_le32(id | (bus << 8 ));
32471da177e4SLinus Torvalds 
32481da177e4SLinus Torvalds 		/* Add a SGE to the config request.
32491da177e4SLinus Torvalds 		 */
32501da177e4SLinus Torvalds 		flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE | ioc->spi_data.sdp1length * 4;
32511da177e4SLinus Torvalds 
32521da177e4SLinus Torvalds 		mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);
32531da177e4SLinus Torvalds 
32541da177e4SLinus Torvalds 		/* Set up the common data portion
32551da177e4SLinus Torvalds 		 */
32561da177e4SLinus Torvalds 		pData->Header.PageVersion = pReq->Header.PageVersion;
32571da177e4SLinus Torvalds 		pData->Header.PageLength = pReq->Header.PageLength;
32581da177e4SLinus Torvalds 		pData->Header.PageNumber = pReq->Header.PageNumber;
32591da177e4SLinus Torvalds 		pData->Header.PageType = pReq->Header.PageType;
32601da177e4SLinus Torvalds 		pData->RequestedParameters = cpu_to_le32(requested);
32611da177e4SLinus Torvalds 		pData->Reserved = 0;
32621da177e4SLinus Torvalds 		pData->Configuration = cpu_to_le32(configuration);
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds 		dprintk((MYIOC_s_INFO_FMT
32651da177e4SLinus Torvalds 			"write SDP1: id %d pgaddr 0x%x req 0x%x config 0x%x\n",
32661da177e4SLinus Torvalds 				ioc->name, id, (id | (bus<<8)),
32671da177e4SLinus Torvalds 				requested, configuration));
32681da177e4SLinus Torvalds 
32690d0c7974SMoore, Eric Dean  		mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
32701da177e4SLinus Torvalds 	}
32711da177e4SLinus Torvalds 
32721da177e4SLinus Torvalds 	return 0;
32731da177e4SLinus Torvalds }
32741da177e4SLinus Torvalds 
32751da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
32761da177e4SLinus Torvalds /*	mptscsih_writeIOCPage4  - write IOC Page 4
32771da177e4SLinus Torvalds  *	@hd: Pointer to a SCSI Host Structure
32781da177e4SLinus Torvalds  *	@target_id: write IOC Page4 for this ID & Bus
32791da177e4SLinus Torvalds  *
32801da177e4SLinus Torvalds  *	Return: -EAGAIN if unable to obtain a Message Frame
32811da177e4SLinus Torvalds  *		or 0 if success.
32821da177e4SLinus Torvalds  *
32831da177e4SLinus Torvalds  *	Remark: We do not wait for a return, write pages sequentially.
32841da177e4SLinus Torvalds  */
32851da177e4SLinus Torvalds static int
32861da177e4SLinus Torvalds mptscsih_writeIOCPage4(MPT_SCSI_HOST *hd, int target_id, int bus)
32871da177e4SLinus Torvalds {
32881da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc = hd->ioc;
32891da177e4SLinus Torvalds 	Config_t		*pReq;
32901da177e4SLinus Torvalds 	IOCPage4_t		*IOCPage4Ptr;
32911da177e4SLinus Torvalds 	MPT_FRAME_HDR		*mf;
32921da177e4SLinus Torvalds 	dma_addr_t		 dataDma;
32931da177e4SLinus Torvalds 	u16			 req_idx;
32941da177e4SLinus Torvalds 	u32			 frameOffset;
32951da177e4SLinus Torvalds 	u32			 flagsLength;
32961da177e4SLinus Torvalds 	int			 ii;
32971da177e4SLinus Torvalds 
32981da177e4SLinus Torvalds 	/* Get a MF for this command.
32991da177e4SLinus Torvalds 	 */
33000d0c7974SMoore, Eric Dean  	if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
33011da177e4SLinus Torvalds 		dprintk((MYIOC_s_WARN_FMT "writeIOCPage4 : no msg frames!\n",
33021da177e4SLinus Torvalds 					ioc->name));
33031da177e4SLinus Torvalds 		return -EAGAIN;
33041da177e4SLinus Torvalds 	}
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds 	/* Set the request and the data pointers.
33071da177e4SLinus Torvalds 	 * Place data at end of MF.
33081da177e4SLinus Torvalds 	 */
33091da177e4SLinus Torvalds 	pReq = (Config_t *)mf;
33101da177e4SLinus Torvalds 
33111da177e4SLinus Torvalds 	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
33121da177e4SLinus Torvalds 	frameOffset = ioc->req_sz - sizeof(IOCPage4_t);
33131da177e4SLinus Torvalds 
33141da177e4SLinus Torvalds 	/* Complete the request frame (same for all requests).
33151da177e4SLinus Torvalds 	 */
33161da177e4SLinus Torvalds 	pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
33171da177e4SLinus Torvalds 	pReq->Reserved = 0;
33181da177e4SLinus Torvalds 	pReq->ChainOffset = 0;
33191da177e4SLinus Torvalds 	pReq->Function = MPI_FUNCTION_CONFIG;
33201da177e4SLinus Torvalds 	pReq->ExtPageLength = 0;
33211da177e4SLinus Torvalds 	pReq->ExtPageType = 0;
33221da177e4SLinus Torvalds 	pReq->MsgFlags = 0;
33231da177e4SLinus Torvalds 	for (ii=0; ii < 8; ii++) {
33241da177e4SLinus Torvalds 		pReq->Reserved2[ii] = 0;
33251da177e4SLinus Torvalds 	}
33261da177e4SLinus Torvalds 
33271da177e4SLinus Torvalds        	IOCPage4Ptr = ioc->spi_data.pIocPg4;
33281da177e4SLinus Torvalds        	dataDma = ioc->spi_data.IocPg4_dma;
33291da177e4SLinus Torvalds        	ii = IOCPage4Ptr->ActiveSEP++;
33301da177e4SLinus Torvalds        	IOCPage4Ptr->SEP[ii].SEPTargetID = target_id;
33311da177e4SLinus Torvalds        	IOCPage4Ptr->SEP[ii].SEPBus = bus;
33321da177e4SLinus Torvalds        	pReq->Header = IOCPage4Ptr->Header;
33331da177e4SLinus Torvalds 	pReq->PageAddress = cpu_to_le32(target_id | (bus << 8 ));
33341da177e4SLinus Torvalds 
33351da177e4SLinus Torvalds 	/* Add a SGE to the config request.
33361da177e4SLinus Torvalds 	 */
33371da177e4SLinus Torvalds 	flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE |
33381da177e4SLinus Torvalds 		(IOCPage4Ptr->Header.PageLength + ii) * 4;
33391da177e4SLinus Torvalds 
33401da177e4SLinus Torvalds 	mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);
33411da177e4SLinus Torvalds 
33421da177e4SLinus Torvalds 	dinitprintk((MYIOC_s_INFO_FMT
33431da177e4SLinus Torvalds 		"writeIOCPage4: MaxSEP=%d ActiveSEP=%d id=%d bus=%d\n",
33441da177e4SLinus Torvalds 			ioc->name, IOCPage4Ptr->MaxSEP, IOCPage4Ptr->ActiveSEP, target_id, bus));
33451da177e4SLinus Torvalds 
33460d0c7974SMoore, Eric Dean  	mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
33471da177e4SLinus Torvalds 
33481da177e4SLinus Torvalds 	return 0;
33491da177e4SLinus Torvalds }
33501da177e4SLinus Torvalds 
33511da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
33521da177e4SLinus Torvalds /*
33531da177e4SLinus Torvalds  *  Bus Scan and Domain Validation functionality ...
33541da177e4SLinus Torvalds  */
33551da177e4SLinus Torvalds 
33561da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
33571da177e4SLinus Torvalds /*
33581da177e4SLinus Torvalds  *	mptscsih_scandv_complete - Scan and DV callback routine registered
33591da177e4SLinus Torvalds  *	to Fustion MPT (base) driver.
33601da177e4SLinus Torvalds  *
33611da177e4SLinus Torvalds  *	@ioc: Pointer to MPT_ADAPTER structure
33621da177e4SLinus Torvalds  *	@mf: Pointer to original MPT request frame
33631da177e4SLinus Torvalds  *	@mr: Pointer to MPT reply frame (NULL if TurboReply)
33641da177e4SLinus Torvalds  *
33651da177e4SLinus Torvalds  *	This routine is called from mpt.c::mpt_interrupt() at the completion
33661da177e4SLinus Torvalds  *	of any SCSI IO request.
33671da177e4SLinus Torvalds  *	This routine is registered with the Fusion MPT (base) driver at driver
33681da177e4SLinus Torvalds  *	load/init time via the mpt_register() API call.
33691da177e4SLinus Torvalds  *
33701da177e4SLinus Torvalds  *	Returns 1 indicating alloc'd request frame ptr should be freed.
33711da177e4SLinus Torvalds  *
33721da177e4SLinus Torvalds  *	Remark: Sets a completion code and (possibly) saves sense data
33731da177e4SLinus Torvalds  *	in the IOC member localReply structure.
33741da177e4SLinus Torvalds  *	Used ONLY for DV and other internal commands.
33751da177e4SLinus Torvalds  */
33760d0c7974SMoore, Eric Dean  int
33771da177e4SLinus Torvalds mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
33781da177e4SLinus Torvalds {
33791da177e4SLinus Torvalds 	MPT_SCSI_HOST	*hd;
33801da177e4SLinus Torvalds 	SCSIIORequest_t *pReq;
33811da177e4SLinus Torvalds 	int		 completionCode;
33821da177e4SLinus Torvalds 	u16		 req_idx;
33831da177e4SLinus Torvalds 
33840d0c7974SMoore, Eric Dean  	hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
33850d0c7974SMoore, Eric Dean  
33861da177e4SLinus Torvalds 	if ((mf == NULL) ||
33871da177e4SLinus Torvalds 	    (mf >= MPT_INDEX_2_MFPTR(ioc, ioc->req_depth))) {
33881da177e4SLinus Torvalds 		printk(MYIOC_s_ERR_FMT
33891da177e4SLinus Torvalds 			"ScanDvComplete, %s req frame ptr! (=%p)\n",
33901da177e4SLinus Torvalds 				ioc->name, mf?"BAD":"NULL", (void *) mf);
33911da177e4SLinus Torvalds 		goto wakeup;
33921da177e4SLinus Torvalds 	}
33931da177e4SLinus Torvalds 
33941da177e4SLinus Torvalds 	del_timer(&hd->timer);
33951da177e4SLinus Torvalds 	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
33961da177e4SLinus Torvalds 	hd->ScsiLookup[req_idx] = NULL;
33971da177e4SLinus Torvalds 	pReq = (SCSIIORequest_t *) mf;
33981da177e4SLinus Torvalds 
33991da177e4SLinus Torvalds 	if (mf != hd->cmdPtr) {
34001da177e4SLinus Torvalds 		printk(MYIOC_s_WARN_FMT "ScanDvComplete (mf=%p, cmdPtr=%p, idx=%d)\n",
34011da177e4SLinus Torvalds 				hd->ioc->name, (void *)mf, (void *) hd->cmdPtr, req_idx);
34021da177e4SLinus Torvalds 	}
34031da177e4SLinus Torvalds 	hd->cmdPtr = NULL;
34041da177e4SLinus Torvalds 
34051da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_INFO_FMT "ScanDvComplete (mf=%p,mr=%p,idx=%d)\n",
34061da177e4SLinus Torvalds 			hd->ioc->name, mf, mr, req_idx));
34071da177e4SLinus Torvalds 
34081da177e4SLinus Torvalds 	hd->pLocal = &hd->localReply;
34091da177e4SLinus Torvalds 	hd->pLocal->scsiStatus = 0;
34101da177e4SLinus Torvalds 
34111da177e4SLinus Torvalds 	/* If target struct exists, clear sense valid flag.
34121da177e4SLinus Torvalds 	 */
34131da177e4SLinus Torvalds 	if (mr == NULL) {
34141da177e4SLinus Torvalds 		completionCode = MPT_SCANDV_GOOD;
34151da177e4SLinus Torvalds 	} else {
34161da177e4SLinus Torvalds 		SCSIIOReply_t	*pReply;
34171da177e4SLinus Torvalds 		u16		 status;
34181da177e4SLinus Torvalds 		u8		 scsi_status;
34191da177e4SLinus Torvalds 
34201da177e4SLinus Torvalds 		pReply = (SCSIIOReply_t *) mr;
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds 		status = le16_to_cpu(pReply->IOCStatus) & MPI_IOCSTATUS_MASK;
34231da177e4SLinus Torvalds 		scsi_status = pReply->SCSIStatus;
34241da177e4SLinus Torvalds 
34251da177e4SLinus Torvalds 		ddvtprintk((KERN_NOTICE "  IOCStatus=%04xh, SCSIState=%02xh, SCSIStatus=%02xh, IOCLogInfo=%08xh\n",
34261da177e4SLinus Torvalds 			     status, pReply->SCSIState, scsi_status,
34271da177e4SLinus Torvalds 			     le32_to_cpu(pReply->IOCLogInfo)));
34281da177e4SLinus Torvalds 
34291da177e4SLinus Torvalds 		switch(status) {
34301da177e4SLinus Torvalds 
34311da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:	/* 0x0043 */
34321da177e4SLinus Torvalds 			completionCode = MPT_SCANDV_SELECTION_TIMEOUT;
34331da177e4SLinus Torvalds 			break;
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:		/* 0x0046 */
34361da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:	/* 0x0048 */
34371da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:		/* 0x004B */
34381da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:		/* 0x004C */
34391da177e4SLinus Torvalds 			completionCode = MPT_SCANDV_DID_RESET;
34401da177e4SLinus Torvalds 			break;
34411da177e4SLinus Torvalds 
34421da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:		/* 0x0045 */
34431da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:	/* 0x0040 */
34441da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SUCCESS:			/* 0x0000 */
34451da177e4SLinus Torvalds 			if (pReply->Function == MPI_FUNCTION_CONFIG) {
34461da177e4SLinus Torvalds 				ConfigReply_t *pr = (ConfigReply_t *)mr;
34471da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_GOOD;
34481da177e4SLinus Torvalds 				hd->pLocal->header.PageVersion = pr->Header.PageVersion;
34491da177e4SLinus Torvalds 				hd->pLocal->header.PageLength = pr->Header.PageLength;
34501da177e4SLinus Torvalds 				hd->pLocal->header.PageNumber = pr->Header.PageNumber;
34511da177e4SLinus Torvalds 				hd->pLocal->header.PageType = pr->Header.PageType;
34521da177e4SLinus Torvalds 
34531da177e4SLinus Torvalds 			} else if (pReply->Function == MPI_FUNCTION_RAID_ACTION) {
34541da177e4SLinus Torvalds 				/* If the RAID Volume request is successful,
34551da177e4SLinus Torvalds 				 * return GOOD, else indicate that
34561da177e4SLinus Torvalds 				 * some type of error occurred.
34571da177e4SLinus Torvalds 				 */
34581da177e4SLinus Torvalds 				MpiRaidActionReply_t	*pr = (MpiRaidActionReply_t *)mr;
34591da177e4SLinus Torvalds 				if (pr->ActionStatus == MPI_RAID_ACTION_ASTATUS_SUCCESS)
34601da177e4SLinus Torvalds 					completionCode = MPT_SCANDV_GOOD;
34611da177e4SLinus Torvalds 				else
34621da177e4SLinus Torvalds 					completionCode = MPT_SCANDV_SOME_ERROR;
34631da177e4SLinus Torvalds 
34641da177e4SLinus Torvalds 			} else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
34651da177e4SLinus Torvalds 				u8		*sense_data;
34661da177e4SLinus Torvalds 				int		 sz;
34671da177e4SLinus Torvalds 
34681da177e4SLinus Torvalds 				/* save sense data in global structure
34691da177e4SLinus Torvalds 				 */
34701da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_SENSE;
34711da177e4SLinus Torvalds 				hd->pLocal->scsiStatus = scsi_status;
34721da177e4SLinus Torvalds 				sense_data = ((u8 *)hd->ioc->sense_buf_pool +
34731da177e4SLinus Torvalds 					(req_idx * MPT_SENSE_BUFFER_ALLOC));
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds 				sz = min_t(int, pReq->SenseBufferLength,
34761da177e4SLinus Torvalds 							SCSI_STD_SENSE_BYTES);
34771da177e4SLinus Torvalds 				memcpy(hd->pLocal->sense, sense_data, sz);
34781da177e4SLinus Torvalds 
34791da177e4SLinus Torvalds 				ddvprintk((KERN_NOTICE "  Check Condition, sense ptr %p\n",
34801da177e4SLinus Torvalds 						sense_data));
34811da177e4SLinus Torvalds 			} else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_FAILED) {
34821da177e4SLinus Torvalds 				if (pReq->CDB[0] == INQUIRY)
34831da177e4SLinus Torvalds 					completionCode = MPT_SCANDV_ISSUE_SENSE;
34841da177e4SLinus Torvalds 				else
34851da177e4SLinus Torvalds 					completionCode = MPT_SCANDV_DID_RESET;
34861da177e4SLinus Torvalds 			}
34871da177e4SLinus Torvalds 			else if (pReply->SCSIState & MPI_SCSI_STATE_NO_SCSI_STATUS)
34881da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_DID_RESET;
34891da177e4SLinus Torvalds 			else if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
34901da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_DID_RESET;
34911da177e4SLinus Torvalds 			else {
34921da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_GOOD;
34931da177e4SLinus Torvalds 				hd->pLocal->scsiStatus = scsi_status;
34941da177e4SLinus Torvalds 			}
34951da177e4SLinus Torvalds 			break;
34961da177e4SLinus Torvalds 
34971da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:		/* 0x0047 */
34981da177e4SLinus Torvalds 			if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
34991da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_DID_RESET;
35001da177e4SLinus Torvalds 			else
35011da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_SOME_ERROR;
35021da177e4SLinus Torvalds 			break;
35031da177e4SLinus Torvalds 
35041da177e4SLinus Torvalds 		default:
35051da177e4SLinus Torvalds 			completionCode = MPT_SCANDV_SOME_ERROR;
35061da177e4SLinus Torvalds 			break;
35071da177e4SLinus Torvalds 
35081da177e4SLinus Torvalds 		}	/* switch(status) */
35091da177e4SLinus Torvalds 
35101da177e4SLinus Torvalds 		ddvtprintk((KERN_NOTICE "  completionCode set to %08xh\n",
35111da177e4SLinus Torvalds 				completionCode));
35121da177e4SLinus Torvalds 	} /* end of address reply case */
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds 	hd->pLocal->completion = completionCode;
35151da177e4SLinus Torvalds 
35161da177e4SLinus Torvalds 	/* MF and RF are freed in mpt_interrupt
35171da177e4SLinus Torvalds 	 */
35181da177e4SLinus Torvalds wakeup:
35191da177e4SLinus Torvalds 	/* Free Chain buffers (will never chain) in scan or dv */
35201da177e4SLinus Torvalds 	//mptscsih_freeChainBuffers(ioc, req_idx);
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds 	/*
35231da177e4SLinus Torvalds 	 * Wake up the original calling thread
35241da177e4SLinus Torvalds 	 */
35250d0c7974SMoore, Eric Dean  	hd->scandv_wait_done = 1;
35260d0c7974SMoore, Eric Dean  	wake_up(&hd->scandv_waitq);
35271da177e4SLinus Torvalds 
35281da177e4SLinus Torvalds 	return 1;
35291da177e4SLinus Torvalds }
35301da177e4SLinus Torvalds 
35311da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
35321da177e4SLinus Torvalds /*	mptscsih_timer_expired - Call back for timer process.
35331da177e4SLinus Torvalds  *	Used only for dv functionality.
35341da177e4SLinus Torvalds  *	@data: Pointer to MPT_SCSI_HOST recast as an unsigned long
35351da177e4SLinus Torvalds  *
35361da177e4SLinus Torvalds  */
35370d0c7974SMoore, Eric Dean  void
35380d0c7974SMoore, Eric Dean  mptscsih_timer_expired(unsigned long data)
35391da177e4SLinus Torvalds {
35401da177e4SLinus Torvalds 	MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *) data;
35411da177e4SLinus Torvalds 
35421da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_WARN_FMT "Timer Expired! Cmd %p\n", hd->ioc->name, hd->cmdPtr));
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds 	if (hd->cmdPtr) {
35451da177e4SLinus Torvalds 		MPIHeader_t *cmd = (MPIHeader_t *)hd->cmdPtr;
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 		if (cmd->Function == MPI_FUNCTION_SCSI_IO_REQUEST) {
35481da177e4SLinus Torvalds 			/* Desire to issue a task management request here.
35491da177e4SLinus Torvalds 			 * TM requests MUST be single threaded.
35501da177e4SLinus Torvalds 			 * If old eh code and no TM current, issue request.
35511da177e4SLinus Torvalds 			 * If new eh code, do nothing. Wait for OS cmd timeout
35521da177e4SLinus Torvalds 			 *	for bus reset.
35531da177e4SLinus Torvalds 			 */
35541da177e4SLinus Torvalds 			ddvtprintk((MYIOC_s_NOTE_FMT "DV Cmd Timeout: NoOp\n", hd->ioc->name));
35551da177e4SLinus Torvalds 		} else {
35561da177e4SLinus Torvalds 			/* Perform a FW reload */
35571da177e4SLinus Torvalds 			if (mpt_HardResetHandler(hd->ioc, NO_SLEEP) < 0) {
35581da177e4SLinus Torvalds 				printk(MYIOC_s_WARN_FMT "Firmware Reload FAILED!\n", hd->ioc->name);
35591da177e4SLinus Torvalds 			}
35601da177e4SLinus Torvalds 		}
35611da177e4SLinus Torvalds 	} else {
35621da177e4SLinus Torvalds 		/* This should NEVER happen */
35631da177e4SLinus Torvalds 		printk(MYIOC_s_WARN_FMT "Null cmdPtr!!!!\n", hd->ioc->name);
35641da177e4SLinus Torvalds 	}
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds 	/* No more processing.
35671da177e4SLinus Torvalds 	 * TM call will generate an interrupt for SCSI TM Management.
35681da177e4SLinus Torvalds 	 * The FW will reply to all outstanding commands, callback will finish cleanup.
35691da177e4SLinus Torvalds 	 * Hard reset clean-up will free all resources.
35701da177e4SLinus Torvalds 	 */
35711da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_WARN_FMT "Timer Expired Complete!\n", hd->ioc->name));
35721da177e4SLinus Torvalds 
35731da177e4SLinus Torvalds 	return;
35741da177e4SLinus Torvalds }
35751da177e4SLinus Torvalds 
35761da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
35771da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
35781da177e4SLinus Torvalds /*	mptscsih_do_raid - Format and Issue a RAID volume request message.
35791da177e4SLinus Torvalds  *	@hd: Pointer to scsi host structure
35801da177e4SLinus Torvalds  *	@action: What do be done.
35811da177e4SLinus Torvalds  *	@id: Logical target id.
35821da177e4SLinus Torvalds  *	@bus: Target locations bus.
35831da177e4SLinus Torvalds  *
35841da177e4SLinus Torvalds  *	Returns: < 0 on a fatal error
35851da177e4SLinus Torvalds  *		0 on success
35861da177e4SLinus Torvalds  *
35871da177e4SLinus Torvalds  *	Remark: Wait to return until reply processed by the ISR.
35881da177e4SLinus Torvalds  */
35891da177e4SLinus Torvalds static int
35901da177e4SLinus Torvalds mptscsih_do_raid(MPT_SCSI_HOST *hd, u8 action, INTERNAL_CMD *io)
35911da177e4SLinus Torvalds {
35921da177e4SLinus Torvalds 	MpiRaidActionRequest_t	*pReq;
35931da177e4SLinus Torvalds 	MPT_FRAME_HDR		*mf;
35941da177e4SLinus Torvalds 	int			in_isr;
35951da177e4SLinus Torvalds 
35961da177e4SLinus Torvalds 	in_isr = in_interrupt();
35971da177e4SLinus Torvalds 	if (in_isr) {
35981da177e4SLinus Torvalds 		dprintk((MYIOC_s_WARN_FMT "Internal raid request not allowed in ISR context!\n",
35991da177e4SLinus Torvalds        				hd->ioc->name));
36001da177e4SLinus Torvalds 		return -EPERM;
36011da177e4SLinus Torvalds 	}
36021da177e4SLinus Torvalds 
36031da177e4SLinus Torvalds 	/* Get and Populate a free Frame
36041da177e4SLinus Torvalds 	 */
36050d0c7974SMoore, Eric Dean  	if ((mf = mpt_get_msg_frame(hd->ioc->InternalCtx, hd->ioc)) == NULL) {
36061da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_WARN_FMT "_do_raid: no msg frames!\n",
36071da177e4SLinus Torvalds 					hd->ioc->name));
36081da177e4SLinus Torvalds 		return -EAGAIN;
36091da177e4SLinus Torvalds 	}
36101da177e4SLinus Torvalds 	pReq = (MpiRaidActionRequest_t *)mf;
36111da177e4SLinus Torvalds 	pReq->Action = action;
36121da177e4SLinus Torvalds 	pReq->Reserved1 = 0;
36131da177e4SLinus Torvalds 	pReq->ChainOffset = 0;
36141da177e4SLinus Torvalds 	pReq->Function = MPI_FUNCTION_RAID_ACTION;
36151da177e4SLinus Torvalds 	pReq->VolumeID = io->id;
36161da177e4SLinus Torvalds 	pReq->VolumeBus = io->bus;
36171da177e4SLinus Torvalds 	pReq->PhysDiskNum = io->physDiskNum;
36181da177e4SLinus Torvalds 	pReq->MsgFlags = 0;
36191da177e4SLinus Torvalds 	pReq->Reserved2 = 0;
36201da177e4SLinus Torvalds 	pReq->ActionDataWord = 0; /* Reserved for this action */
36211da177e4SLinus Torvalds 	//pReq->ActionDataSGE = 0;
36221da177e4SLinus Torvalds 
36231da177e4SLinus Torvalds 	mpt_add_sge((char *)&pReq->ActionDataSGE,
36241da177e4SLinus Torvalds 		MPT_SGE_FLAGS_SSIMPLE_READ | 0, (dma_addr_t) -1);
36251da177e4SLinus Torvalds 
36261da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_INFO_FMT "RAID Volume action %x id %d\n",
36271da177e4SLinus Torvalds 			hd->ioc->name, action, io->id));
36281da177e4SLinus Torvalds 
36291da177e4SLinus Torvalds 	hd->pLocal = NULL;
36301da177e4SLinus Torvalds 	hd->timer.expires = jiffies + HZ*10; /* 10 second timeout */
36310d0c7974SMoore, Eric Dean  	hd->scandv_wait_done = 0;
36321da177e4SLinus Torvalds 
36331da177e4SLinus Torvalds 	/* Save cmd pointer, for resource free if timeout or
36341da177e4SLinus Torvalds 	 * FW reload occurs
36351da177e4SLinus Torvalds 	 */
36361da177e4SLinus Torvalds 	hd->cmdPtr = mf;
36371da177e4SLinus Torvalds 
36381da177e4SLinus Torvalds 	add_timer(&hd->timer);
36390d0c7974SMoore, Eric Dean  	mpt_put_msg_frame(hd->ioc->InternalCtx, hd->ioc, mf);
36400d0c7974SMoore, Eric Dean  	wait_event(hd->scandv_waitq, hd->scandv_wait_done);
36411da177e4SLinus Torvalds 
36421da177e4SLinus Torvalds 	if ((hd->pLocal == NULL) || (hd->pLocal->completion != MPT_SCANDV_GOOD))
36431da177e4SLinus Torvalds 		return -1;
36441da177e4SLinus Torvalds 
36451da177e4SLinus Torvalds 	return 0;
36461da177e4SLinus Torvalds }
36471da177e4SLinus Torvalds #endif /* ~MPTSCSIH_ENABLE_DOMAIN_VALIDATION */
36481da177e4SLinus Torvalds 
36491da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
36501da177e4SLinus Torvalds /**
36511da177e4SLinus Torvalds  *	mptscsih_do_cmd - Do internal command.
36521da177e4SLinus Torvalds  *	@hd: MPT_SCSI_HOST pointer
36531da177e4SLinus Torvalds  *	@io: INTERNAL_CMD pointer.
36541da177e4SLinus Torvalds  *
36551da177e4SLinus Torvalds  *	Issue the specified internally generated command and do command
36561da177e4SLinus Torvalds  *	specific cleanup. For bus scan / DV only.
36571da177e4SLinus Torvalds  *	NOTES: If command is Inquiry and status is good,
36581da177e4SLinus Torvalds  *	initialize a target structure, save the data
36591da177e4SLinus Torvalds  *
36601da177e4SLinus Torvalds  *	Remark: Single threaded access only.
36611da177e4SLinus Torvalds  *
36621da177e4SLinus Torvalds  *	Return:
36631da177e4SLinus Torvalds  *		< 0 if an illegal command or no resources
36641da177e4SLinus Torvalds  *
36651da177e4SLinus Torvalds  *		   0 if good
36661da177e4SLinus Torvalds  *
36671da177e4SLinus Torvalds  *		 > 0 if command complete but some type of completion error.
36681da177e4SLinus Torvalds  */
36691da177e4SLinus Torvalds static int
36701da177e4SLinus Torvalds mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *io)
36711da177e4SLinus Torvalds {
36721da177e4SLinus Torvalds 	MPT_FRAME_HDR	*mf;
36731da177e4SLinus Torvalds 	SCSIIORequest_t	*pScsiReq;
36741da177e4SLinus Torvalds 	SCSIIORequest_t	 ReqCopy;
36751da177e4SLinus Torvalds 	int		 my_idx, ii, dir;
36761da177e4SLinus Torvalds 	int		 rc, cmdTimeout;
36771da177e4SLinus Torvalds 	int		in_isr;
36781da177e4SLinus Torvalds 	char		 cmdLen;
36791da177e4SLinus Torvalds 	char		 CDB[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
36801da177e4SLinus Torvalds 	char		 cmd = io->cmd;
36811da177e4SLinus Torvalds 
36821da177e4SLinus Torvalds 	in_isr = in_interrupt();
36831da177e4SLinus Torvalds 	if (in_isr) {
36841da177e4SLinus Torvalds 		dprintk((MYIOC_s_WARN_FMT "Internal SCSI IO request not allowed in ISR context!\n",
36851da177e4SLinus Torvalds        				hd->ioc->name));
36861da177e4SLinus Torvalds 		return -EPERM;
36871da177e4SLinus Torvalds 	}
36881da177e4SLinus Torvalds 
36891da177e4SLinus Torvalds 
36901da177e4SLinus Torvalds 	/* Set command specific information
36911da177e4SLinus Torvalds 	 */
36921da177e4SLinus Torvalds 	switch (cmd) {
36931da177e4SLinus Torvalds 	case INQUIRY:
36941da177e4SLinus Torvalds 		cmdLen = 6;
36951da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
36961da177e4SLinus Torvalds 		CDB[0] = cmd;
36971da177e4SLinus Torvalds 		CDB[4] = io->size;
36981da177e4SLinus Torvalds 		cmdTimeout = 10;
36991da177e4SLinus Torvalds 		break;
37001da177e4SLinus Torvalds 
37011da177e4SLinus Torvalds 	case TEST_UNIT_READY:
37021da177e4SLinus Torvalds 		cmdLen = 6;
37031da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37041da177e4SLinus Torvalds 		cmdTimeout = 10;
37051da177e4SLinus Torvalds 		break;
37061da177e4SLinus Torvalds 
37071da177e4SLinus Torvalds 	case START_STOP:
37081da177e4SLinus Torvalds 		cmdLen = 6;
37091da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37101da177e4SLinus Torvalds 		CDB[0] = cmd;
37111da177e4SLinus Torvalds 		CDB[4] = 1;	/*Spin up the disk */
37121da177e4SLinus Torvalds 		cmdTimeout = 15;
37131da177e4SLinus Torvalds 		break;
37141da177e4SLinus Torvalds 
37151da177e4SLinus Torvalds 	case REQUEST_SENSE:
37161da177e4SLinus Torvalds 		cmdLen = 6;
37171da177e4SLinus Torvalds 		CDB[0] = cmd;
37181da177e4SLinus Torvalds 		CDB[4] = io->size;
37191da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37201da177e4SLinus Torvalds 		cmdTimeout = 10;
37211da177e4SLinus Torvalds 		break;
37221da177e4SLinus Torvalds 
37231da177e4SLinus Torvalds 	case READ_BUFFER:
37241da177e4SLinus Torvalds 		cmdLen = 10;
37251da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37261da177e4SLinus Torvalds 		CDB[0] = cmd;
37271da177e4SLinus Torvalds 		if (io->flags & MPT_ICFLAG_ECHO) {
37281da177e4SLinus Torvalds 			CDB[1] = 0x0A;
37291da177e4SLinus Torvalds 		} else {
37301da177e4SLinus Torvalds 			CDB[1] = 0x02;
37311da177e4SLinus Torvalds 		}
37321da177e4SLinus Torvalds 
37331da177e4SLinus Torvalds 		if (io->flags & MPT_ICFLAG_BUF_CAP) {
37341da177e4SLinus Torvalds 			CDB[1] |= 0x01;
37351da177e4SLinus Torvalds 		}
37361da177e4SLinus Torvalds 		CDB[6] = (io->size >> 16) & 0xFF;
37371da177e4SLinus Torvalds 		CDB[7] = (io->size >>  8) & 0xFF;
37381da177e4SLinus Torvalds 		CDB[8] = io->size & 0xFF;
37391da177e4SLinus Torvalds 		cmdTimeout = 10;
37401da177e4SLinus Torvalds 		break;
37411da177e4SLinus Torvalds 
37421da177e4SLinus Torvalds 	case WRITE_BUFFER:
37431da177e4SLinus Torvalds 		cmdLen = 10;
37441da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_WRITE;
37451da177e4SLinus Torvalds 		CDB[0] = cmd;
37461da177e4SLinus Torvalds 		if (io->flags & MPT_ICFLAG_ECHO) {
37471da177e4SLinus Torvalds 			CDB[1] = 0x0A;
37481da177e4SLinus Torvalds 		} else {
37491da177e4SLinus Torvalds 			CDB[1] = 0x02;
37501da177e4SLinus Torvalds 		}
37511da177e4SLinus Torvalds 		CDB[6] = (io->size >> 16) & 0xFF;
37521da177e4SLinus Torvalds 		CDB[7] = (io->size >>  8) & 0xFF;
37531da177e4SLinus Torvalds 		CDB[8] = io->size & 0xFF;
37541da177e4SLinus Torvalds 		cmdTimeout = 10;
37551da177e4SLinus Torvalds 		break;
37561da177e4SLinus Torvalds 
37571da177e4SLinus Torvalds 	case RESERVE:
37581da177e4SLinus Torvalds 		cmdLen = 6;
37591da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37601da177e4SLinus Torvalds 		CDB[0] = cmd;
37611da177e4SLinus Torvalds 		cmdTimeout = 10;
37621da177e4SLinus Torvalds 		break;
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds 	case RELEASE:
37651da177e4SLinus Torvalds 		cmdLen = 6;
37661da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37671da177e4SLinus Torvalds 		CDB[0] = cmd;
37681da177e4SLinus Torvalds 		cmdTimeout = 10;
37691da177e4SLinus Torvalds 		break;
37701da177e4SLinus Torvalds 
37711da177e4SLinus Torvalds 	case SYNCHRONIZE_CACHE:
37721da177e4SLinus Torvalds 		cmdLen = 10;
37731da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37741da177e4SLinus Torvalds 		CDB[0] = cmd;
37751da177e4SLinus Torvalds //		CDB[1] = 0x02;	/* set immediate bit */
37761da177e4SLinus Torvalds 		cmdTimeout = 10;
37771da177e4SLinus Torvalds 		break;
37781da177e4SLinus Torvalds 
37791da177e4SLinus Torvalds 	default:
37801da177e4SLinus Torvalds 		/* Error Case */
37811da177e4SLinus Torvalds 		return -EFAULT;
37821da177e4SLinus Torvalds 	}
37831da177e4SLinus Torvalds 
37841da177e4SLinus Torvalds 	/* Get and Populate a free Frame
37851da177e4SLinus Torvalds 	 */
37860d0c7974SMoore, Eric Dean  	if ((mf = mpt_get_msg_frame(hd->ioc->InternalCtx, hd->ioc)) == NULL) {
37871da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_WARN_FMT "No msg frames!\n",
37881da177e4SLinus Torvalds 					hd->ioc->name));
37891da177e4SLinus Torvalds 		return -EBUSY;
37901da177e4SLinus Torvalds 	}
37911da177e4SLinus Torvalds 
37921da177e4SLinus Torvalds 	pScsiReq = (SCSIIORequest_t *) mf;
37931da177e4SLinus Torvalds 
37941da177e4SLinus Torvalds 	/* Get the request index */
37951da177e4SLinus Torvalds 	my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
37961da177e4SLinus Torvalds 	ADD_INDEX_LOG(my_idx); /* for debug */
37971da177e4SLinus Torvalds 
37981da177e4SLinus Torvalds 	if (io->flags & MPT_ICFLAG_PHYS_DISK) {
37991da177e4SLinus Torvalds 		pScsiReq->TargetID = io->physDiskNum;
38001da177e4SLinus Torvalds 		pScsiReq->Bus = 0;
38011da177e4SLinus Torvalds 		pScsiReq->ChainOffset = 0;
38021da177e4SLinus Torvalds 		pScsiReq->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
38031da177e4SLinus Torvalds 	} else {
38041da177e4SLinus Torvalds 		pScsiReq->TargetID = io->id;
38051da177e4SLinus Torvalds 		pScsiReq->Bus = io->bus;
38061da177e4SLinus Torvalds 		pScsiReq->ChainOffset = 0;
38071da177e4SLinus Torvalds 		pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
38081da177e4SLinus Torvalds 	}
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds 	pScsiReq->CDBLength = cmdLen;
38111da177e4SLinus Torvalds 	pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
38121da177e4SLinus Torvalds 
38131da177e4SLinus Torvalds 	pScsiReq->Reserved = 0;
38141da177e4SLinus Torvalds 
38151da177e4SLinus Torvalds 	pScsiReq->MsgFlags = mpt_msg_flags();
38161da177e4SLinus Torvalds 	/* MsgContext set in mpt_get_msg_fram call  */
38171da177e4SLinus Torvalds 
38181da177e4SLinus Torvalds 	for (ii=0; ii < 8; ii++)
38191da177e4SLinus Torvalds 		pScsiReq->LUN[ii] = 0;
38201da177e4SLinus Torvalds 	pScsiReq->LUN[1] = io->lun;
38211da177e4SLinus Torvalds 
38221da177e4SLinus Torvalds 	if (io->flags & MPT_ICFLAG_TAGGED_CMD)
38231da177e4SLinus Torvalds 		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_SIMPLEQ);
38241da177e4SLinus Torvalds 	else
38251da177e4SLinus Torvalds 		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
38261da177e4SLinus Torvalds 
38271da177e4SLinus Torvalds 	if (cmd == REQUEST_SENSE) {
38281da177e4SLinus Torvalds 		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
38291da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_INFO_FMT "Untagged! 0x%2x\n",
38301da177e4SLinus Torvalds 			hd->ioc->name, cmd));
38311da177e4SLinus Torvalds 	}
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds 	for (ii=0; ii < 16; ii++)
38341da177e4SLinus Torvalds 		pScsiReq->CDB[ii] = CDB[ii];
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds 	pScsiReq->DataLength = cpu_to_le32(io->size);
38371da177e4SLinus Torvalds 	pScsiReq->SenseBufferLowAddr = cpu_to_le32(hd->ioc->sense_buf_low_dma
38381da177e4SLinus Torvalds 					   + (my_idx * MPT_SENSE_BUFFER_ALLOC));
38391da177e4SLinus Torvalds 
38401da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_INFO_FMT "Sending Command 0x%x for (%d:%d:%d)\n",
38411da177e4SLinus Torvalds 			hd->ioc->name, cmd, io->bus, io->id, io->lun));
38421da177e4SLinus Torvalds 
38431da177e4SLinus Torvalds 	if (dir == MPI_SCSIIO_CONTROL_READ) {
38441da177e4SLinus Torvalds 		mpt_add_sge((char *) &pScsiReq->SGL,
38451da177e4SLinus Torvalds 			MPT_SGE_FLAGS_SSIMPLE_READ | io->size,
38461da177e4SLinus Torvalds 			io->data_dma);
38471da177e4SLinus Torvalds 	} else {
38481da177e4SLinus Torvalds 		mpt_add_sge((char *) &pScsiReq->SGL,
38491da177e4SLinus Torvalds 			MPT_SGE_FLAGS_SSIMPLE_WRITE | io->size,
38501da177e4SLinus Torvalds 			io->data_dma);
38511da177e4SLinus Torvalds 	}
38521da177e4SLinus Torvalds 
38531da177e4SLinus Torvalds 	/* The ISR will free the request frame, but we need
38541da177e4SLinus Torvalds 	 * the information to initialize the target. Duplicate.
38551da177e4SLinus Torvalds 	 */
38561da177e4SLinus Torvalds 	memcpy(&ReqCopy, pScsiReq, sizeof(SCSIIORequest_t));
38571da177e4SLinus Torvalds 
38581da177e4SLinus Torvalds 	/* Issue this command after:
38591da177e4SLinus Torvalds 	 *	finish init
38601da177e4SLinus Torvalds 	 *	add timer
38611da177e4SLinus Torvalds 	 * Wait until the reply has been received
38621da177e4SLinus Torvalds 	 *  ScsiScanDvCtx callback function will
38631da177e4SLinus Torvalds 	 *	set hd->pLocal;
38641da177e4SLinus Torvalds 	 *	set scandv_wait_done and call wake_up
38651da177e4SLinus Torvalds 	 */
38661da177e4SLinus Torvalds 	hd->pLocal = NULL;
38671da177e4SLinus Torvalds 	hd->timer.expires = jiffies + HZ*cmdTimeout;
38680d0c7974SMoore, Eric Dean  	hd->scandv_wait_done = 0;
38691da177e4SLinus Torvalds 
38701da177e4SLinus Torvalds 	/* Save cmd pointer, for resource free if timeout or
38711da177e4SLinus Torvalds 	 * FW reload occurs
38721da177e4SLinus Torvalds 	 */
38731da177e4SLinus Torvalds 	hd->cmdPtr = mf;
38741da177e4SLinus Torvalds 
38751da177e4SLinus Torvalds 	add_timer(&hd->timer);
38760d0c7974SMoore, Eric Dean  	mpt_put_msg_frame(hd->ioc->InternalCtx, hd->ioc, mf);
38770d0c7974SMoore, Eric Dean  	wait_event(hd->scandv_waitq, hd->scandv_wait_done);
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds 	if (hd->pLocal) {
38801da177e4SLinus Torvalds 		rc = hd->pLocal->completion;
38811da177e4SLinus Torvalds 		hd->pLocal->skip = 0;
38821da177e4SLinus Torvalds 
38831da177e4SLinus Torvalds 		/* Always set fatal error codes in some cases.
38841da177e4SLinus Torvalds 		 */
38851da177e4SLinus Torvalds 		if (rc == MPT_SCANDV_SELECTION_TIMEOUT)
38861da177e4SLinus Torvalds 			rc = -ENXIO;
38871da177e4SLinus Torvalds 		else if (rc == MPT_SCANDV_SOME_ERROR)
38881da177e4SLinus Torvalds 			rc =  -rc;
38891da177e4SLinus Torvalds 	} else {
38901da177e4SLinus Torvalds 		rc = -EFAULT;
38911da177e4SLinus Torvalds 		/* This should never happen. */
38921da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_INFO_FMT "_do_cmd: Null pLocal!!!\n",
38931da177e4SLinus Torvalds 				hd->ioc->name));
38941da177e4SLinus Torvalds 	}
38951da177e4SLinus Torvalds 
38961da177e4SLinus Torvalds 	return rc;
38971da177e4SLinus Torvalds }
38981da177e4SLinus Torvalds 
38991da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
39001da177e4SLinus Torvalds /**
39011da177e4SLinus Torvalds  *	mptscsih_synchronize_cache - Send SYNCHRONIZE_CACHE to all disks.
39021da177e4SLinus Torvalds  *	@hd: Pointer to MPT_SCSI_HOST structure
39031da177e4SLinus Torvalds  *	@portnum: IOC port number
39041da177e4SLinus Torvalds  *
39051da177e4SLinus Torvalds  *	Uses the ISR, but with special processing.
39061da177e4SLinus Torvalds  *	MUST be single-threaded.
39071da177e4SLinus Torvalds  *
39081da177e4SLinus Torvalds  *	Return: 0 on completion
39091da177e4SLinus Torvalds  */
39101da177e4SLinus Torvalds static int
39111da177e4SLinus Torvalds mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, int portnum)
39121da177e4SLinus Torvalds {
39131da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc= hd->ioc;
39141da177e4SLinus Torvalds 	VirtDevice		*pTarget;
39151da177e4SLinus Torvalds 	SCSIDevicePage1_t	*pcfg1Data = NULL;
39161da177e4SLinus Torvalds 	INTERNAL_CMD		 iocmd;
39171da177e4SLinus Torvalds 	CONFIGPARMS		 cfg;
39181da177e4SLinus Torvalds 	dma_addr_t		 cfg1_dma_addr = -1;
39191da177e4SLinus Torvalds 	ConfigPageHeader_t	 header1;
39201da177e4SLinus Torvalds 	int			 bus = 0;
39211da177e4SLinus Torvalds 	int			 id = 0;
39221da177e4SLinus Torvalds 	int			 lun;
39231da177e4SLinus Torvalds 	int			 indexed_lun, lun_index;
39241da177e4SLinus Torvalds 	int			 hostId = ioc->pfacts[portnum].PortSCSIID;
39251da177e4SLinus Torvalds 	int			 max_id;
39261da177e4SLinus Torvalds 	int			 requested, configuration, data;
39271da177e4SLinus Torvalds 	int			 doConfig = 0;
39281da177e4SLinus Torvalds 	u8			 flags, factor;
39291da177e4SLinus Torvalds 
39301da177e4SLinus Torvalds 	max_id = ioc->sh->max_id - 1;
39311da177e4SLinus Torvalds 
39321da177e4SLinus Torvalds 	/* Following parameters will not change
39331da177e4SLinus Torvalds 	 * in this routine.
39341da177e4SLinus Torvalds 	 */
39351da177e4SLinus Torvalds 	iocmd.cmd = SYNCHRONIZE_CACHE;
39361da177e4SLinus Torvalds 	iocmd.flags = 0;
39371da177e4SLinus Torvalds 	iocmd.physDiskNum = -1;
39381da177e4SLinus Torvalds 	iocmd.data = NULL;
39391da177e4SLinus Torvalds 	iocmd.data_dma = -1;
39401da177e4SLinus Torvalds 	iocmd.size = 0;
39411da177e4SLinus Torvalds 	iocmd.rsvd = iocmd.rsvd2 = 0;
39421da177e4SLinus Torvalds 
39431da177e4SLinus Torvalds 	/* No SCSI hosts
39441da177e4SLinus Torvalds 	 */
39451da177e4SLinus Torvalds 	if (hd->Targets == NULL)
39461da177e4SLinus Torvalds 		return 0;
39471da177e4SLinus Torvalds 
39481da177e4SLinus Torvalds 	/* Skip the host
39491da177e4SLinus Torvalds 	 */
39501da177e4SLinus Torvalds 	if (id == hostId)
39511da177e4SLinus Torvalds 		id++;
39521da177e4SLinus Torvalds 
39531da177e4SLinus Torvalds 	/* Write SDP1 for all SCSI devices
39541da177e4SLinus Torvalds 	 * Alloc memory and set up config buffer
39551da177e4SLinus Torvalds 	 */
39561da177e4SLinus Torvalds 	if (ioc->bus_type == SCSI) {
39571da177e4SLinus Torvalds 		if (ioc->spi_data.sdp1length > 0) {
39581da177e4SLinus Torvalds 			pcfg1Data = (SCSIDevicePage1_t *)pci_alloc_consistent(ioc->pcidev,
39591da177e4SLinus Torvalds 					 ioc->spi_data.sdp1length * 4, &cfg1_dma_addr);
39601da177e4SLinus Torvalds 
39611da177e4SLinus Torvalds 			if (pcfg1Data != NULL) {
39621da177e4SLinus Torvalds 				doConfig = 1;
39631da177e4SLinus Torvalds 				header1.PageVersion = ioc->spi_data.sdp1version;
39641da177e4SLinus Torvalds 				header1.PageLength = ioc->spi_data.sdp1length;
39651da177e4SLinus Torvalds 				header1.PageNumber = 1;
39661da177e4SLinus Torvalds 				header1.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
39671da177e4SLinus Torvalds 				cfg.hdr = &header1;
39681da177e4SLinus Torvalds 				cfg.physAddr = cfg1_dma_addr;
39691da177e4SLinus Torvalds 				cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
39701da177e4SLinus Torvalds 				cfg.dir = 1;
39711da177e4SLinus Torvalds 				cfg.timeout = 0;
39721da177e4SLinus Torvalds 			}
39731da177e4SLinus Torvalds 		}
39741da177e4SLinus Torvalds 	}
39751da177e4SLinus Torvalds 
39761da177e4SLinus Torvalds 	/* loop through all devices on this port
39771da177e4SLinus Torvalds 	 */
39781da177e4SLinus Torvalds 	while (bus < MPT_MAX_BUS) {
39791da177e4SLinus Torvalds 		iocmd.bus = bus;
39801da177e4SLinus Torvalds 		iocmd.id = id;
39811da177e4SLinus Torvalds 		pTarget = hd->Targets[(int)id];
39821da177e4SLinus Torvalds 
39831da177e4SLinus Torvalds 		if (doConfig) {
39841da177e4SLinus Torvalds 
39851da177e4SLinus Torvalds 			/* Set the negotiation flags */
39861da177e4SLinus Torvalds 			if (pTarget && (pTarget = hd->Targets[id]) && !pTarget->raidVolume) {
39871da177e4SLinus Torvalds 				flags = pTarget->negoFlags;
39881da177e4SLinus Torvalds 			} else {
39891da177e4SLinus Torvalds 				flags = hd->ioc->spi_data.noQas;
39901da177e4SLinus Torvalds 				if (hd->ioc->spi_data.nvram && (hd->ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
39911da177e4SLinus Torvalds 					data = hd->ioc->spi_data.nvram[id];
39921da177e4SLinus Torvalds 
39931da177e4SLinus Torvalds 					if (data & MPT_NVRAM_WIDE_DISABLE)
39941da177e4SLinus Torvalds 						flags |= MPT_TARGET_NO_NEGO_WIDE;
39951da177e4SLinus Torvalds 
39961da177e4SLinus Torvalds 					factor = (data & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
39971da177e4SLinus Torvalds 					if ((factor == 0) || (factor == MPT_ASYNC))
39981da177e4SLinus Torvalds 						flags |= MPT_TARGET_NO_NEGO_SYNC;
39991da177e4SLinus Torvalds 				}
40001da177e4SLinus Torvalds 			}
40011da177e4SLinus Torvalds 
40021da177e4SLinus Torvalds 			/* Force to async, narrow */
40031da177e4SLinus Torvalds 			mptscsih_setDevicePage1Flags(0, MPT_ASYNC, 0, &requested,
40041da177e4SLinus Torvalds 					&configuration, flags);
40051da177e4SLinus Torvalds 			dnegoprintk(("syncronize cache: id=%d width=0 factor=MPT_ASYNC "
40061da177e4SLinus Torvalds 				"offset=0 negoFlags=%x request=%x config=%x\n",
40071da177e4SLinus Torvalds 				id, flags, requested, configuration));
40081da177e4SLinus Torvalds 			pcfg1Data->RequestedParameters = le32_to_cpu(requested);
40091da177e4SLinus Torvalds 			pcfg1Data->Reserved = 0;
40101da177e4SLinus Torvalds 			pcfg1Data->Configuration = le32_to_cpu(configuration);
40111da177e4SLinus Torvalds 			cfg.pageAddr = (bus<<8) | id;
40121da177e4SLinus Torvalds 			mpt_config(hd->ioc, &cfg);
40131da177e4SLinus Torvalds 		}
40141da177e4SLinus Torvalds 
40151da177e4SLinus Torvalds 		/* If target Ptr NULL or if this target is NOT a disk, skip.
40161da177e4SLinus Torvalds 		 */
40171da177e4SLinus Torvalds 		if ((pTarget) && (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)){
40181da177e4SLinus Torvalds 			for (lun=0; lun <= MPT_LAST_LUN; lun++) {
40191da177e4SLinus Torvalds 				/* If LUN present, issue the command
40201da177e4SLinus Torvalds 				 */
40211da177e4SLinus Torvalds 				lun_index = (lun >> 5);  /* 32 luns per lun_index */
40221da177e4SLinus Torvalds 				indexed_lun = (lun % 32);
40231da177e4SLinus Torvalds 				if (pTarget->luns[lun_index] & (1<<indexed_lun)) {
40241da177e4SLinus Torvalds 					iocmd.lun = lun;
40251da177e4SLinus Torvalds 					(void) mptscsih_do_cmd(hd, &iocmd);
40261da177e4SLinus Torvalds 				}
40271da177e4SLinus Torvalds 			}
40281da177e4SLinus Torvalds 		}
40291da177e4SLinus Torvalds 
40301da177e4SLinus Torvalds 		/* get next relevant device */
40311da177e4SLinus Torvalds 		id++;
40321da177e4SLinus Torvalds 
40331da177e4SLinus Torvalds 		if (id == hostId)
40341da177e4SLinus Torvalds 			id++;
40351da177e4SLinus Torvalds 
40361da177e4SLinus Torvalds 		if (id > max_id) {
40371da177e4SLinus Torvalds 			id = 0;
40381da177e4SLinus Torvalds 			bus++;
40391da177e4SLinus Torvalds 		}
40401da177e4SLinus Torvalds 	}
40411da177e4SLinus Torvalds 
40421da177e4SLinus Torvalds 	if (pcfg1Data) {
40431da177e4SLinus Torvalds 		pci_free_consistent(ioc->pcidev, header1.PageLength * 4, pcfg1Data, cfg1_dma_addr);
40441da177e4SLinus Torvalds 	}
40451da177e4SLinus Torvalds 
40461da177e4SLinus Torvalds 	return 0;
40471da177e4SLinus Torvalds }
40481da177e4SLinus Torvalds 
40491da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
40501da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
40511da177e4SLinus Torvalds /**
40521da177e4SLinus Torvalds  *	mptscsih_domainValidation - Top level handler for domain validation.
40531da177e4SLinus Torvalds  *	@hd: Pointer to MPT_SCSI_HOST structure.
40541da177e4SLinus Torvalds  *
40551da177e4SLinus Torvalds  *	Uses the ISR, but with special processing.
40561da177e4SLinus Torvalds  *	Called from schedule, should not be in interrupt mode.
40571da177e4SLinus Torvalds  *	While thread alive, do dv for all devices needing dv
40581da177e4SLinus Torvalds  *
40591da177e4SLinus Torvalds  *	Return: None.
40601da177e4SLinus Torvalds  */
40611da177e4SLinus Torvalds static void
40621da177e4SLinus Torvalds mptscsih_domainValidation(void *arg)
40631da177e4SLinus Torvalds {
40641da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd;
40651da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc;
40661da177e4SLinus Torvalds 	unsigned long		 flags;
40671da177e4SLinus Torvalds 	int 			 id, maxid, dvStatus, did;
40681da177e4SLinus Torvalds 	int			 ii, isPhysDisk;
40691da177e4SLinus Torvalds 
40701da177e4SLinus Torvalds 	spin_lock_irqsave(&dvtaskQ_lock, flags);
40711da177e4SLinus Torvalds 	dvtaskQ_active = 1;
40721da177e4SLinus Torvalds 	if (dvtaskQ_release) {
40731da177e4SLinus Torvalds 		dvtaskQ_active = 0;
40741da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dvtaskQ_lock, flags);
40751da177e4SLinus Torvalds 		return;
40761da177e4SLinus Torvalds 	}
40771da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dvtaskQ_lock, flags);
40781da177e4SLinus Torvalds 
40791da177e4SLinus Torvalds 	/* For this ioc, loop through all devices and do dv to each device.
40801da177e4SLinus Torvalds 	 * When complete with this ioc, search through the ioc list, and
40811da177e4SLinus Torvalds 	 * for each scsi ioc found, do dv for all devices. Exit when no
40821da177e4SLinus Torvalds 	 * device needs dv.
40831da177e4SLinus Torvalds 	 */
40841da177e4SLinus Torvalds 	did = 1;
40851da177e4SLinus Torvalds 	while (did) {
40861da177e4SLinus Torvalds 		did = 0;
40871da177e4SLinus Torvalds 		list_for_each_entry(ioc, &ioc_list, list) {
40881da177e4SLinus Torvalds 			spin_lock_irqsave(&dvtaskQ_lock, flags);
40891da177e4SLinus Torvalds 			if (dvtaskQ_release) {
40901da177e4SLinus Torvalds 				dvtaskQ_active = 0;
40911da177e4SLinus Torvalds 				spin_unlock_irqrestore(&dvtaskQ_lock, flags);
40921da177e4SLinus Torvalds 				return;
40931da177e4SLinus Torvalds 			}
40941da177e4SLinus Torvalds 			spin_unlock_irqrestore(&dvtaskQ_lock, flags);
40951da177e4SLinus Torvalds 
40961da177e4SLinus Torvalds 			msleep(250);
40971da177e4SLinus Torvalds 
40981da177e4SLinus Torvalds 			/* DV only to SCSI adapters */
40991da177e4SLinus Torvalds 			if (ioc->bus_type != SCSI)
41001da177e4SLinus Torvalds 				continue;
41011da177e4SLinus Torvalds 
41021da177e4SLinus Torvalds 			/* Make sure everything looks ok */
41031da177e4SLinus Torvalds 			if (ioc->sh == NULL)
41041da177e4SLinus Torvalds 				continue;
41051da177e4SLinus Torvalds 
41061da177e4SLinus Torvalds 			hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
41071da177e4SLinus Torvalds 			if (hd == NULL)
41081da177e4SLinus Torvalds 				continue;
41091da177e4SLinus Torvalds 
41101da177e4SLinus Torvalds 			if ((ioc->spi_data.forceDv & MPT_SCSICFG_RELOAD_IOC_PG3) != 0) {
41111da177e4SLinus Torvalds 				mpt_read_ioc_pg_3(ioc);
41121da177e4SLinus Torvalds 				if (ioc->spi_data.pIocPg3) {
41131da177e4SLinus Torvalds 					Ioc3PhysDisk_t *pPDisk = ioc->spi_data.pIocPg3->PhysDisk;
41141da177e4SLinus Torvalds 					int		numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;
41151da177e4SLinus Torvalds 
41161da177e4SLinus Torvalds 					while (numPDisk) {
41171da177e4SLinus Torvalds 						if (ioc->spi_data.dvStatus[pPDisk->PhysDiskID] & MPT_SCSICFG_DV_NOT_DONE)
41181da177e4SLinus Torvalds 							ioc->spi_data.dvStatus[pPDisk->PhysDiskID] |= MPT_SCSICFG_NEED_DV;
41191da177e4SLinus Torvalds 
41201da177e4SLinus Torvalds 						pPDisk++;
41211da177e4SLinus Torvalds 						numPDisk--;
41221da177e4SLinus Torvalds 					}
41231da177e4SLinus Torvalds 				}
41241da177e4SLinus Torvalds 				ioc->spi_data.forceDv &= ~MPT_SCSICFG_RELOAD_IOC_PG3;
41251da177e4SLinus Torvalds 			}
41261da177e4SLinus Torvalds 
41271da177e4SLinus Torvalds 			maxid = min_t(int, ioc->sh->max_id, MPT_MAX_SCSI_DEVICES);
41281da177e4SLinus Torvalds 
41291da177e4SLinus Torvalds 			for (id = 0; id < maxid; id++) {
41301da177e4SLinus Torvalds 				spin_lock_irqsave(&dvtaskQ_lock, flags);
41311da177e4SLinus Torvalds 				if (dvtaskQ_release) {
41321da177e4SLinus Torvalds 					dvtaskQ_active = 0;
41331da177e4SLinus Torvalds 					spin_unlock_irqrestore(&dvtaskQ_lock, flags);
41341da177e4SLinus Torvalds 					return;
41351da177e4SLinus Torvalds 				}
41361da177e4SLinus Torvalds 				spin_unlock_irqrestore(&dvtaskQ_lock, flags);
41371da177e4SLinus Torvalds 				dvStatus = hd->ioc->spi_data.dvStatus[id];
41381da177e4SLinus Torvalds 
41391da177e4SLinus Torvalds 				if (dvStatus & MPT_SCSICFG_NEED_DV) {
41401da177e4SLinus Torvalds 					did++;
41411da177e4SLinus Torvalds 					hd->ioc->spi_data.dvStatus[id] |= MPT_SCSICFG_DV_PENDING;
41421da177e4SLinus Torvalds 					hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_NEED_DV;
41431da177e4SLinus Torvalds 
41441da177e4SLinus Torvalds 					msleep(250);
41451da177e4SLinus Torvalds 
41461da177e4SLinus Torvalds 					/* If hidden phys disk, block IO's to all
41471da177e4SLinus Torvalds 					 *	raid volumes
41481da177e4SLinus Torvalds 					 * else, process normally
41491da177e4SLinus Torvalds 					 */
41501da177e4SLinus Torvalds 					isPhysDisk = mptscsih_is_phys_disk(ioc, id);
41511da177e4SLinus Torvalds 					if (isPhysDisk) {
41521da177e4SLinus Torvalds 						for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
41531da177e4SLinus Torvalds 							if (hd->ioc->spi_data.isRaid & (1 << ii)) {
41541da177e4SLinus Torvalds 								hd->ioc->spi_data.dvStatus[ii] |= MPT_SCSICFG_DV_PENDING;
41551da177e4SLinus Torvalds 							}
41561da177e4SLinus Torvalds 						}
41571da177e4SLinus Torvalds 					}
41581da177e4SLinus Torvalds 
41591da177e4SLinus Torvalds 					if (mptscsih_doDv(hd, 0, id) == 1) {
41601da177e4SLinus Torvalds 						/* Untagged device was busy, try again
41611da177e4SLinus Torvalds 						 */
41621da177e4SLinus Torvalds 						hd->ioc->spi_data.dvStatus[id] |= MPT_SCSICFG_NEED_DV;
41631da177e4SLinus Torvalds 						hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_DV_PENDING;
41641da177e4SLinus Torvalds 					} else {
41651da177e4SLinus Torvalds 						/* DV is complete. Clear flags.
41661da177e4SLinus Torvalds 						 */
41671da177e4SLinus Torvalds 						hd->ioc->spi_data.dvStatus[id] &= ~(MPT_SCSICFG_DV_NOT_DONE | MPT_SCSICFG_DV_PENDING);
41681da177e4SLinus Torvalds 					}
41691da177e4SLinus Torvalds 
41701da177e4SLinus Torvalds 					if (isPhysDisk) {
41711da177e4SLinus Torvalds 						for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
41721da177e4SLinus Torvalds 							if (hd->ioc->spi_data.isRaid & (1 << ii)) {
41731da177e4SLinus Torvalds 								hd->ioc->spi_data.dvStatus[ii] &= ~MPT_SCSICFG_DV_PENDING;
41741da177e4SLinus Torvalds 							}
41751da177e4SLinus Torvalds 						}
41761da177e4SLinus Torvalds 					}
41771da177e4SLinus Torvalds 
41781da177e4SLinus Torvalds 					if (hd->ioc->spi_data.noQas)
41791da177e4SLinus Torvalds 						mptscsih_qas_check(hd, id);
41801da177e4SLinus Torvalds 				}
41811da177e4SLinus Torvalds 			}
41821da177e4SLinus Torvalds 		}
41831da177e4SLinus Torvalds 	}
41841da177e4SLinus Torvalds 
41851da177e4SLinus Torvalds 	spin_lock_irqsave(&dvtaskQ_lock, flags);
41861da177e4SLinus Torvalds 	dvtaskQ_active = 0;
41871da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dvtaskQ_lock, flags);
41881da177e4SLinus Torvalds 
41891da177e4SLinus Torvalds 	return;
41901da177e4SLinus Torvalds }
41911da177e4SLinus Torvalds 
41921da177e4SLinus Torvalds /* Search IOC page 3 to determine if this is hidden physical disk
41931da177e4SLinus Torvalds  */
41940d0c7974SMoore, Eric Dean  static int
41950d0c7974SMoore, Eric Dean  mptscsih_is_phys_disk(MPT_ADAPTER *ioc, int id)
41961da177e4SLinus Torvalds {
41971da177e4SLinus Torvalds 	if (ioc->spi_data.pIocPg3) {
41981da177e4SLinus Torvalds 		Ioc3PhysDisk_t *pPDisk =  ioc->spi_data.pIocPg3->PhysDisk;
41991da177e4SLinus Torvalds 		int		numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;
42001da177e4SLinus Torvalds 
42011da177e4SLinus Torvalds 		while (numPDisk) {
42021da177e4SLinus Torvalds 			if (pPDisk->PhysDiskID == id) {
42031da177e4SLinus Torvalds 				return 1;
42041da177e4SLinus Torvalds 			}
42051da177e4SLinus Torvalds 			pPDisk++;
42061da177e4SLinus Torvalds 			numPDisk--;
42071da177e4SLinus Torvalds 		}
42081da177e4SLinus Torvalds 	}
42091da177e4SLinus Torvalds 	return 0;
42101da177e4SLinus Torvalds }
42111da177e4SLinus Torvalds 
42121da177e4SLinus Torvalds /* Write SDP1 if no QAS has been enabled
42131da177e4SLinus Torvalds  */
42140d0c7974SMoore, Eric Dean  static void
42150d0c7974SMoore, Eric Dean  mptscsih_qas_check(MPT_SCSI_HOST *hd, int id)
42161da177e4SLinus Torvalds {
42171da177e4SLinus Torvalds 	VirtDevice *pTarget;
42181da177e4SLinus Torvalds 	int ii;
42191da177e4SLinus Torvalds 
42201da177e4SLinus Torvalds 	if (hd->Targets == NULL)
42211da177e4SLinus Torvalds 		return;
42221da177e4SLinus Torvalds 
42231da177e4SLinus Torvalds 	for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
42241da177e4SLinus Torvalds 		if (ii == id)
42251da177e4SLinus Torvalds 			continue;
42261da177e4SLinus Torvalds 
42271da177e4SLinus Torvalds 		if ((hd->ioc->spi_data.dvStatus[ii] & MPT_SCSICFG_DV_NOT_DONE) != 0)
42281da177e4SLinus Torvalds 			continue;
42291da177e4SLinus Torvalds 
42301da177e4SLinus Torvalds 		pTarget = hd->Targets[ii];
42311da177e4SLinus Torvalds 
42321da177e4SLinus Torvalds 		if ((pTarget != NULL) && (!pTarget->raidVolume)) {
42331da177e4SLinus Torvalds 			if ((pTarget->negoFlags & hd->ioc->spi_data.noQas) == 0) {
42341da177e4SLinus Torvalds 				pTarget->negoFlags |= hd->ioc->spi_data.noQas;
42351da177e4SLinus Torvalds 				dnegoprintk(("writeSDP1: id=%d flags=0\n", id));
42361da177e4SLinus Torvalds 				mptscsih_writeSDP1(hd, 0, ii, 0);
42371da177e4SLinus Torvalds 			}
42381da177e4SLinus Torvalds 		} else {
42391da177e4SLinus Torvalds 			if (mptscsih_is_phys_disk(hd->ioc, ii) == 1) {
42401da177e4SLinus Torvalds 				dnegoprintk(("writeSDP1: id=%d SCSICFG_USE_NVRAM\n", id));
42411da177e4SLinus Torvalds 				mptscsih_writeSDP1(hd, 0, ii, MPT_SCSICFG_USE_NVRAM);
42421da177e4SLinus Torvalds 			}
42431da177e4SLinus Torvalds 		}
42441da177e4SLinus Torvalds 	}
42451da177e4SLinus Torvalds 	return;
42461da177e4SLinus Torvalds }
42471da177e4SLinus Torvalds 
42481da177e4SLinus Torvalds 
42491da177e4SLinus Torvalds 
42501da177e4SLinus Torvalds #define MPT_GET_NVRAM_VALS	0x01
42511da177e4SLinus Torvalds #define MPT_UPDATE_MAX		0x02
42521da177e4SLinus Torvalds #define MPT_SET_MAX		0x04
42531da177e4SLinus Torvalds #define MPT_SET_MIN		0x08
42541da177e4SLinus Torvalds #define MPT_FALLBACK		0x10
42551da177e4SLinus Torvalds #define MPT_SAVE		0x20
42561da177e4SLinus Torvalds 
42571da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
42581da177e4SLinus Torvalds /**
42591da177e4SLinus Torvalds  *	mptscsih_doDv - Perform domain validation to a target.
42601da177e4SLinus Torvalds  *	@hd: Pointer to MPT_SCSI_HOST structure.
42611da177e4SLinus Torvalds  *	@portnum: IOC port number.
42621da177e4SLinus Torvalds  *	@target: Physical ID of this target
42631da177e4SLinus Torvalds  *
42641da177e4SLinus Torvalds  *	Uses the ISR, but with special processing.
42651da177e4SLinus Torvalds  *	MUST be single-threaded.
42661da177e4SLinus Torvalds  *	Test will exit if target is at async & narrow.
42671da177e4SLinus Torvalds  *
42681da177e4SLinus Torvalds  *	Return: None.
42691da177e4SLinus Torvalds  */
42701da177e4SLinus Torvalds static int
42711da177e4SLinus Torvalds mptscsih_doDv(MPT_SCSI_HOST *hd, int bus_number, int id)
42721da177e4SLinus Torvalds {
42731da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc = hd->ioc;
42741da177e4SLinus Torvalds 	VirtDevice		*pTarget;
42751da177e4SLinus Torvalds 	SCSIDevicePage1_t	*pcfg1Data;
42761da177e4SLinus Torvalds 	SCSIDevicePage0_t	*pcfg0Data;
42771da177e4SLinus Torvalds 	u8			*pbuf1;
42781da177e4SLinus Torvalds 	u8			*pbuf2;
42791da177e4SLinus Torvalds 	u8			*pDvBuf;
42801da177e4SLinus Torvalds 	dma_addr_t		 dvbuf_dma = -1;
42811da177e4SLinus Torvalds 	dma_addr_t		 buf1_dma = -1;
42821da177e4SLinus Torvalds 	dma_addr_t		 buf2_dma = -1;
42831da177e4SLinus Torvalds 	dma_addr_t		 cfg1_dma_addr = -1;
42841da177e4SLinus Torvalds 	dma_addr_t		 cfg0_dma_addr = -1;
42851da177e4SLinus Torvalds 	ConfigPageHeader_t	 header1;
42861da177e4SLinus Torvalds 	ConfigPageHeader_t	 header0;
42871da177e4SLinus Torvalds 	DVPARAMETERS		 dv;
42881da177e4SLinus Torvalds 	INTERNAL_CMD		 iocmd;
42891da177e4SLinus Torvalds 	CONFIGPARMS		 cfg;
42901da177e4SLinus Torvalds 	int			 dv_alloc = 0;
42911da177e4SLinus Torvalds 	int			 rc, sz = 0;
42921da177e4SLinus Torvalds 	int			 bufsize = 0;
42931da177e4SLinus Torvalds 	int			 dataBufSize = 0;
42941da177e4SLinus Torvalds 	int			 echoBufSize = 0;
42951da177e4SLinus Torvalds 	int			 notDone;
42961da177e4SLinus Torvalds 	int			 patt;
42971da177e4SLinus Torvalds 	int			 repeat;
42981da177e4SLinus Torvalds 	int			 retcode = 0;
42991da177e4SLinus Torvalds 	int			 nfactor =  MPT_ULTRA320;
43001da177e4SLinus Torvalds 	char			 firstPass = 1;
43011da177e4SLinus Torvalds 	char			 doFallback = 0;
43021da177e4SLinus Torvalds 	char			 readPage0;
43031da177e4SLinus Torvalds 	char			 bus, lun;
43041da177e4SLinus Torvalds 	char			 inq0 = 0;
43051da177e4SLinus Torvalds 
43061da177e4SLinus Torvalds 	if (ioc->spi_data.sdp1length == 0)
43071da177e4SLinus Torvalds 		return 0;
43081da177e4SLinus Torvalds 
43091da177e4SLinus Torvalds 	if (ioc->spi_data.sdp0length == 0)
43101da177e4SLinus Torvalds 		return 0;
43111da177e4SLinus Torvalds 
43121da177e4SLinus Torvalds 	/* If multiple buses are used, require that the initiator
43131da177e4SLinus Torvalds 	 * id be the same on all buses.
43141da177e4SLinus Torvalds 	 */
43151da177e4SLinus Torvalds 	if (id == ioc->pfacts[0].PortSCSIID)
43161da177e4SLinus Torvalds 		return 0;
43171da177e4SLinus Torvalds 
43181da177e4SLinus Torvalds 	lun = 0;
43191da177e4SLinus Torvalds 	bus = (u8) bus_number;
43201da177e4SLinus Torvalds 	ddvtprintk((MYIOC_s_NOTE_FMT
43211da177e4SLinus Torvalds 			"DV started: bus=%d, id=%d dv @ %p\n",
43221da177e4SLinus Torvalds 			ioc->name, bus, id, &dv));
43231da177e4SLinus Torvalds 
43241da177e4SLinus Torvalds 	/* Prep DV structure
43251da177e4SLinus Torvalds 	 */
43261da177e4SLinus Torvalds 	memset (&dv, 0, sizeof(DVPARAMETERS));
43271da177e4SLinus Torvalds 	dv.id = id;
43281da177e4SLinus Torvalds 
43291da177e4SLinus Torvalds 	/* Populate tmax with the current maximum
43301da177e4SLinus Torvalds 	 * transfer parameters for this target.
43311da177e4SLinus Torvalds 	 * Exit if narrow and async.
43321da177e4SLinus Torvalds 	 */
43331da177e4SLinus Torvalds 	dv.cmd = MPT_GET_NVRAM_VALS;
43341da177e4SLinus Torvalds 	mptscsih_dv_parms(hd, &dv, NULL);
43351da177e4SLinus Torvalds 
43361da177e4SLinus Torvalds 	/* Prep SCSI IO structure
43371da177e4SLinus Torvalds 	 */
43381da177e4SLinus Torvalds 	iocmd.id = id;
43391da177e4SLinus Torvalds 	iocmd.bus = bus;
43401da177e4SLinus Torvalds 	iocmd.lun = lun;
43411da177e4SLinus Torvalds 	iocmd.flags = 0;
43421da177e4SLinus Torvalds 	iocmd.physDiskNum = -1;
43431da177e4SLinus Torvalds 	iocmd.rsvd = iocmd.rsvd2 = 0;
43441da177e4SLinus Torvalds 
43451da177e4SLinus Torvalds 	pTarget = hd->Targets[id];
43461da177e4SLinus Torvalds 
43471da177e4SLinus Torvalds 	/* Use tagged commands if possible.
43481da177e4SLinus Torvalds 	 */
43491da177e4SLinus Torvalds 	if (pTarget) {
43501da177e4SLinus Torvalds 		if (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)
43511da177e4SLinus Torvalds 			iocmd.flags |= MPT_ICFLAG_TAGGED_CMD;
43521da177e4SLinus Torvalds 		else {
43531da177e4SLinus Torvalds 			if (hd->ioc->facts.FWVersion.Word < 0x01000600)
43541da177e4SLinus Torvalds 				return 0;
43551da177e4SLinus Torvalds 
43561da177e4SLinus Torvalds 			if ((hd->ioc->facts.FWVersion.Word >= 0x01010000) &&
43571da177e4SLinus Torvalds 				(hd->ioc->facts.FWVersion.Word < 0x01010B00))
43581da177e4SLinus Torvalds 				return 0;
43591da177e4SLinus Torvalds 		}
43601da177e4SLinus Torvalds 	}
43611da177e4SLinus Torvalds 
43621da177e4SLinus Torvalds 	/* Prep cfg structure
43631da177e4SLinus Torvalds 	 */
43641da177e4SLinus Torvalds 	cfg.pageAddr = (bus<<8) | id;
43651da177e4SLinus Torvalds 	cfg.hdr = NULL;
43661da177e4SLinus Torvalds 
43671da177e4SLinus Torvalds 	/* Prep SDP0 header
43681da177e4SLinus Torvalds 	 */
43691da177e4SLinus Torvalds 	header0.PageVersion = ioc->spi_data.sdp0version;
43701da177e4SLinus Torvalds 	header0.PageLength = ioc->spi_data.sdp0length;
43711da177e4SLinus Torvalds 	header0.PageNumber = 0;
43721da177e4SLinus Torvalds 	header0.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
43731da177e4SLinus Torvalds 
43741da177e4SLinus Torvalds 	/* Prep SDP1 header
43751da177e4SLinus Torvalds 	 */
43761da177e4SLinus Torvalds 	header1.PageVersion = ioc->spi_data.sdp1version;
43771da177e4SLinus Torvalds 	header1.PageLength = ioc->spi_data.sdp1length;
43781da177e4SLinus Torvalds 	header1.PageNumber = 1;
43791da177e4SLinus Torvalds 	header1.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
43801da177e4SLinus Torvalds 
43811da177e4SLinus Torvalds 	if (header0.PageLength & 1)
43821da177e4SLinus Torvalds 		dv_alloc = (header0.PageLength * 4) + 4;
43831da177e4SLinus Torvalds 
43841da177e4SLinus Torvalds 	dv_alloc +=  (2048 + (header1.PageLength * 4));
43851da177e4SLinus Torvalds 
43861da177e4SLinus Torvalds 	pDvBuf = pci_alloc_consistent(ioc->pcidev, dv_alloc, &dvbuf_dma);
43871da177e4SLinus Torvalds 	if (pDvBuf == NULL)
43881da177e4SLinus Torvalds 		return 0;
43891da177e4SLinus Torvalds 
43901da177e4SLinus Torvalds 	sz = 0;
43911da177e4SLinus Torvalds 	pbuf1 = (u8 *)pDvBuf;
43921da177e4SLinus Torvalds 	buf1_dma = dvbuf_dma;
43931da177e4SLinus Torvalds 	sz +=1024;
43941da177e4SLinus Torvalds 
43951da177e4SLinus Torvalds 	pbuf2 = (u8 *) (pDvBuf + sz);
43961da177e4SLinus Torvalds 	buf2_dma = dvbuf_dma + sz;
43971da177e4SLinus Torvalds 	sz +=1024;
43981da177e4SLinus Torvalds 
43991da177e4SLinus Torvalds 	pcfg0Data = (SCSIDevicePage0_t *) (pDvBuf + sz);
44001da177e4SLinus Torvalds 	cfg0_dma_addr = dvbuf_dma + sz;
44011da177e4SLinus Torvalds 	sz += header0.PageLength * 4;
44021da177e4SLinus Torvalds 
44031da177e4SLinus Torvalds 	/* 8-byte alignment
44041da177e4SLinus Torvalds 	 */
44051da177e4SLinus Torvalds 	if (header0.PageLength & 1)
44061da177e4SLinus Torvalds 		sz += 4;
44071da177e4SLinus Torvalds 
44081da177e4SLinus Torvalds 	pcfg1Data = (SCSIDevicePage1_t *) (pDvBuf + sz);
44091da177e4SLinus Torvalds 	cfg1_dma_addr = dvbuf_dma + sz;
44101da177e4SLinus Torvalds 
44111da177e4SLinus Torvalds 	/* Skip this ID? Set cfg.hdr to force config page write
44121da177e4SLinus Torvalds 	 */
44131da177e4SLinus Torvalds 	{
44141da177e4SLinus Torvalds 		ScsiCfgData *pspi_data = &hd->ioc->spi_data;
44151da177e4SLinus Torvalds 		if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
44161da177e4SLinus Torvalds 			/* Set the factor from nvram */
44171da177e4SLinus Torvalds 			nfactor = (pspi_data->nvram[id] & MPT_NVRAM_SYNC_MASK) >> 8;
44181da177e4SLinus Torvalds 			if (nfactor < pspi_data->minSyncFactor )
44191da177e4SLinus Torvalds 				nfactor = pspi_data->minSyncFactor;
44201da177e4SLinus Torvalds 
44211da177e4SLinus Torvalds 			if (!(pspi_data->nvram[id] & MPT_NVRAM_ID_SCAN_ENABLE) ||
44221da177e4SLinus Torvalds 				(pspi_data->PortFlags == MPI_SCSIPORTPAGE2_PORT_FLAGS_OFF_DV) ) {
44231da177e4SLinus Torvalds 
44241da177e4SLinus Torvalds 				ddvprintk((MYIOC_s_NOTE_FMT "DV Skipped: bus, id, lun (%d, %d, %d)\n",
44251da177e4SLinus Torvalds 					ioc->name, bus, id, lun));
44261da177e4SLinus Torvalds 
44271da177e4SLinus Torvalds 				dv.cmd = MPT_SET_MAX;
44281da177e4SLinus Torvalds 				mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
44291da177e4SLinus Torvalds 				cfg.hdr = &header1;
44301da177e4SLinus Torvalds 
44311da177e4SLinus Torvalds 				/* Save the final negotiated settings to
44321da177e4SLinus Torvalds 				 * SCSI device page 1.
44331da177e4SLinus Torvalds 				 */
44341da177e4SLinus Torvalds 				cfg.physAddr = cfg1_dma_addr;
44351da177e4SLinus Torvalds 				cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
44361da177e4SLinus Torvalds 				cfg.dir = 1;
44371da177e4SLinus Torvalds 				mpt_config(hd->ioc, &cfg);
44381da177e4SLinus Torvalds 				goto target_done;
44391da177e4SLinus Torvalds 			}
44401da177e4SLinus Torvalds 		}
44411da177e4SLinus Torvalds 	}
44421da177e4SLinus Torvalds 
44431da177e4SLinus Torvalds 	/* Finish iocmd inititialization - hidden or visible disk? */
44441da177e4SLinus Torvalds 	if (ioc->spi_data.pIocPg3) {
44451da177e4SLinus Torvalds 		/* Search IOC page 3 for matching id
44461da177e4SLinus Torvalds 		 */
44471da177e4SLinus Torvalds 		Ioc3PhysDisk_t *pPDisk =  ioc->spi_data.pIocPg3->PhysDisk;
44481da177e4SLinus Torvalds 		int		numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;
44491da177e4SLinus Torvalds 
44501da177e4SLinus Torvalds 		while (numPDisk) {
44511da177e4SLinus Torvalds 			if (pPDisk->PhysDiskID == id) {
44521da177e4SLinus Torvalds 				/* match */
44531da177e4SLinus Torvalds 				iocmd.flags |= MPT_ICFLAG_PHYS_DISK;
44541da177e4SLinus Torvalds 				iocmd.physDiskNum = pPDisk->PhysDiskNum;
44551da177e4SLinus Torvalds 
44561da177e4SLinus Torvalds 				/* Quiesce the IM
44571da177e4SLinus Torvalds 				 */
44581da177e4SLinus Torvalds 				if (mptscsih_do_raid(hd, MPI_RAID_ACTION_QUIESCE_PHYS_IO, &iocmd) < 0) {
44591da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_ERR_FMT "RAID Queisce FAILED!\n", ioc->name));
44601da177e4SLinus Torvalds 					goto target_done;
44611da177e4SLinus Torvalds 				}
44621da177e4SLinus Torvalds 				break;
44631da177e4SLinus Torvalds 			}
44641da177e4SLinus Torvalds 			pPDisk++;
44651da177e4SLinus Torvalds 			numPDisk--;
44661da177e4SLinus Torvalds 		}
44671da177e4SLinus Torvalds 	}
44681da177e4SLinus Torvalds 
44691da177e4SLinus Torvalds 	/* RAID Volume ID's may double for a physical device. If RAID but
44701da177e4SLinus Torvalds 	 * not a physical ID as well, skip DV.
44711da177e4SLinus Torvalds 	 */
44721da177e4SLinus Torvalds 	if ((hd->ioc->spi_data.isRaid & (1 << id)) && !(iocmd.flags & MPT_ICFLAG_PHYS_DISK))
44731da177e4SLinus Torvalds 		goto target_done;
44741da177e4SLinus Torvalds 
44751da177e4SLinus Torvalds 
44761da177e4SLinus Torvalds 	/* Basic Test.
44771da177e4SLinus Torvalds 	 * Async & Narrow - Inquiry
44781da177e4SLinus Torvalds 	 * Async & Narrow - Inquiry
44791da177e4SLinus Torvalds 	 * Maximum transfer rate - Inquiry
44801da177e4SLinus Torvalds 	 * Compare buffers:
44811da177e4SLinus Torvalds 	 *	If compare, test complete.
44821da177e4SLinus Torvalds 	 *	If miscompare and first pass, repeat
44831da177e4SLinus Torvalds 	 *	If miscompare and not first pass, fall back and repeat
44841da177e4SLinus Torvalds 	 */
44851da177e4SLinus Torvalds 	hd->pLocal = NULL;
44861da177e4SLinus Torvalds 	readPage0 = 0;
44871da177e4SLinus Torvalds 	sz = SCSI_MAX_INQUIRY_BYTES;
44881da177e4SLinus Torvalds 	rc = MPT_SCANDV_GOOD;
44891da177e4SLinus Torvalds 	while (1) {
44901da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT "DV: Start Basic test on id=%d\n", ioc->name, id));
44911da177e4SLinus Torvalds 		retcode = 0;
44921da177e4SLinus Torvalds 		dv.cmd = MPT_SET_MIN;
44931da177e4SLinus Torvalds 		mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
44941da177e4SLinus Torvalds 
44951da177e4SLinus Torvalds 		cfg.hdr = &header1;
44961da177e4SLinus Torvalds 		cfg.physAddr = cfg1_dma_addr;
44971da177e4SLinus Torvalds 		cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
44981da177e4SLinus Torvalds 		cfg.dir = 1;
44991da177e4SLinus Torvalds 		if (mpt_config(hd->ioc, &cfg) != 0)
45001da177e4SLinus Torvalds 			goto target_done;
45011da177e4SLinus Torvalds 
45021da177e4SLinus Torvalds 		/* Wide - narrow - wide workaround case
45031da177e4SLinus Torvalds 		 */
45041da177e4SLinus Torvalds 		if ((rc == MPT_SCANDV_ISSUE_SENSE) && dv.max.width) {
45051da177e4SLinus Torvalds 			/* Send an untagged command to reset disk Qs corrupted
45061da177e4SLinus Torvalds 			 * when a parity error occurs on a Request Sense.
45071da177e4SLinus Torvalds 			 */
45081da177e4SLinus Torvalds 			if ((hd->ioc->facts.FWVersion.Word >= 0x01000600) ||
45091da177e4SLinus Torvalds 				((hd->ioc->facts.FWVersion.Word >= 0x01010000) &&
45101da177e4SLinus Torvalds 				(hd->ioc->facts.FWVersion.Word < 0x01010B00)) ) {
45111da177e4SLinus Torvalds 
45121da177e4SLinus Torvalds 				iocmd.cmd = REQUEST_SENSE;
45131da177e4SLinus Torvalds 				iocmd.data_dma = buf1_dma;
45141da177e4SLinus Torvalds 				iocmd.data = pbuf1;
45151da177e4SLinus Torvalds 				iocmd.size = 0x12;
45161da177e4SLinus Torvalds 				if (mptscsih_do_cmd(hd, &iocmd) < 0)
45171da177e4SLinus Torvalds 					goto target_done;
45181da177e4SLinus Torvalds 				else {
45191da177e4SLinus Torvalds 					if (hd->pLocal == NULL)
45201da177e4SLinus Torvalds 						goto target_done;
45211da177e4SLinus Torvalds 					rc = hd->pLocal->completion;
45221da177e4SLinus Torvalds 					if ((rc == MPT_SCANDV_GOOD) || (rc == MPT_SCANDV_SENSE)) {
45231da177e4SLinus Torvalds 						dv.max.width = 0;
45241da177e4SLinus Torvalds 						doFallback = 0;
45251da177e4SLinus Torvalds 					} else
45261da177e4SLinus Torvalds 						goto target_done;
45271da177e4SLinus Torvalds 				}
45281da177e4SLinus Torvalds 			} else
45291da177e4SLinus Torvalds 				goto target_done;
45301da177e4SLinus Torvalds 		}
45311da177e4SLinus Torvalds 
45321da177e4SLinus Torvalds 		iocmd.cmd = INQUIRY;
45331da177e4SLinus Torvalds 		iocmd.data_dma = buf1_dma;
45341da177e4SLinus Torvalds 		iocmd.data = pbuf1;
45351da177e4SLinus Torvalds 		iocmd.size = sz;
45361da177e4SLinus Torvalds 		memset(pbuf1, 0x00, sz);
45371da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
45381da177e4SLinus Torvalds 			goto target_done;
45391da177e4SLinus Torvalds 		else {
45401da177e4SLinus Torvalds 			if (hd->pLocal == NULL)
45411da177e4SLinus Torvalds 				goto target_done;
45421da177e4SLinus Torvalds 			rc = hd->pLocal->completion;
45431da177e4SLinus Torvalds 			if (rc == MPT_SCANDV_GOOD) {
45441da177e4SLinus Torvalds 				if (hd->pLocal->scsiStatus == SAM_STAT_BUSY) {
45451da177e4SLinus Torvalds 					if ((iocmd.flags & MPT_ICFLAG_TAGGED_CMD) == 0)
45461da177e4SLinus Torvalds 						retcode = 1;
45471da177e4SLinus Torvalds 					else
45481da177e4SLinus Torvalds 						retcode = 0;
45491da177e4SLinus Torvalds 
45501da177e4SLinus Torvalds 					goto target_done;
45511da177e4SLinus Torvalds 				}
45521da177e4SLinus Torvalds 			} else if  (rc == MPT_SCANDV_SENSE) {
45531da177e4SLinus Torvalds 				;
45541da177e4SLinus Torvalds 			} else {
45551da177e4SLinus Torvalds 				/* If first command doesn't complete
45561da177e4SLinus Torvalds 				 * with a good status or with a check condition,
45571da177e4SLinus Torvalds 				 * exit.
45581da177e4SLinus Torvalds 				 */
45591da177e4SLinus Torvalds 				goto target_done;
45601da177e4SLinus Torvalds 			}
45611da177e4SLinus Torvalds 		}
45621da177e4SLinus Torvalds 
45631da177e4SLinus Torvalds 		/* Reset the size for disks
45641da177e4SLinus Torvalds 		 */
45651da177e4SLinus Torvalds 		inq0 = (*pbuf1) & 0x1F;
45661da177e4SLinus Torvalds 		if ((inq0 == 0) && pTarget && !pTarget->raidVolume) {
45671da177e4SLinus Torvalds 			sz = 0x40;
45681da177e4SLinus Torvalds 			iocmd.size = sz;
45691da177e4SLinus Torvalds 		}
45701da177e4SLinus Torvalds 
45711da177e4SLinus Torvalds 		/* Another GEM workaround. Check peripheral device type,
45721da177e4SLinus Torvalds 		 * if PROCESSOR, quit DV.
45731da177e4SLinus Torvalds 		 */
45741da177e4SLinus Torvalds 		if (inq0 == TYPE_PROCESSOR) {
45751da177e4SLinus Torvalds 			mptscsih_initTarget(hd,
45761da177e4SLinus Torvalds 				bus,
45771da177e4SLinus Torvalds 				id,
45781da177e4SLinus Torvalds 				lun,
45791da177e4SLinus Torvalds 				pbuf1,
45801da177e4SLinus Torvalds 				sz);
45811da177e4SLinus Torvalds 			goto target_done;
45821da177e4SLinus Torvalds 		}
45831da177e4SLinus Torvalds 
45841da177e4SLinus Torvalds 		if (inq0 > 0x08)
45851da177e4SLinus Torvalds 			goto target_done;
45861da177e4SLinus Torvalds 
45871da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
45881da177e4SLinus Torvalds 			goto target_done;
45891da177e4SLinus Torvalds 
45901da177e4SLinus Torvalds 		if (sz == 0x40) {
45911da177e4SLinus Torvalds 			if ((pTarget->maxWidth == 1) && (pTarget->maxOffset) && (nfactor < 0x0A)
45921da177e4SLinus Torvalds 				&& (pTarget->minSyncFactor > 0x09)) {
45931da177e4SLinus Torvalds 				if ((pbuf1[56] & 0x04) == 0)
45941da177e4SLinus Torvalds 					;
45951da177e4SLinus Torvalds 				else if ((pbuf1[56] & 0x01) == 1) {
45961da177e4SLinus Torvalds 					pTarget->minSyncFactor =
45971da177e4SLinus Torvalds 					    nfactor > MPT_ULTRA320 ? nfactor : MPT_ULTRA320;
45981da177e4SLinus Torvalds 				} else {
45991da177e4SLinus Torvalds 					pTarget->minSyncFactor =
46001da177e4SLinus Torvalds 					    nfactor > MPT_ULTRA160 ? nfactor : MPT_ULTRA160;
46011da177e4SLinus Torvalds 				}
46021da177e4SLinus Torvalds 
46031da177e4SLinus Torvalds 				dv.max.factor = pTarget->minSyncFactor;
46041da177e4SLinus Torvalds 
46051da177e4SLinus Torvalds 				if ((pbuf1[56] & 0x02) == 0) {
46061da177e4SLinus Torvalds 					pTarget->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
46071da177e4SLinus Torvalds 					hd->ioc->spi_data.noQas = MPT_TARGET_NO_NEGO_QAS;
46081da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_NOTE_FMT
46091da177e4SLinus Torvalds 					    "DV: Start Basic noQas on id=%d due to pbuf1[56]=%x\n",
46101da177e4SLinus Torvalds 					    ioc->name, id, pbuf1[56]));
46111da177e4SLinus Torvalds 				}
46121da177e4SLinus Torvalds 			}
46131da177e4SLinus Torvalds 		}
46141da177e4SLinus Torvalds 
46151da177e4SLinus Torvalds 		if (doFallback)
46161da177e4SLinus Torvalds 			dv.cmd = MPT_FALLBACK;
46171da177e4SLinus Torvalds 		else
46181da177e4SLinus Torvalds 			dv.cmd = MPT_SET_MAX;
46191da177e4SLinus Torvalds 
46201da177e4SLinus Torvalds 		mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
46211da177e4SLinus Torvalds 		if (mpt_config(hd->ioc, &cfg) != 0)
46221da177e4SLinus Torvalds 			goto target_done;
46231da177e4SLinus Torvalds 
46241da177e4SLinus Torvalds 		if ((!dv.now.width) && (!dv.now.offset))
46251da177e4SLinus Torvalds 			goto target_done;
46261da177e4SLinus Torvalds 
46271da177e4SLinus Torvalds 		iocmd.cmd = INQUIRY;
46281da177e4SLinus Torvalds 		iocmd.data_dma = buf2_dma;
46291da177e4SLinus Torvalds 		iocmd.data = pbuf2;
46301da177e4SLinus Torvalds 		iocmd.size = sz;
46311da177e4SLinus Torvalds 		memset(pbuf2, 0x00, sz);
46321da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
46331da177e4SLinus Torvalds 			goto target_done;
46341da177e4SLinus Torvalds 		else if (hd->pLocal == NULL)
46351da177e4SLinus Torvalds 			goto target_done;
46361da177e4SLinus Torvalds 		else {
46371da177e4SLinus Torvalds 			/* Save the return code.
46381da177e4SLinus Torvalds 			 * If this is the first pass,
46391da177e4SLinus Torvalds 			 * read SCSI Device Page 0
46401da177e4SLinus Torvalds 			 * and update the target max parameters.
46411da177e4SLinus Torvalds 			 */
46421da177e4SLinus Torvalds 			rc = hd->pLocal->completion;
46431da177e4SLinus Torvalds 			doFallback = 0;
46441da177e4SLinus Torvalds 			if (rc == MPT_SCANDV_GOOD) {
46451da177e4SLinus Torvalds 				if (!readPage0) {
46461da177e4SLinus Torvalds 					u32 sdp0_info;
46471da177e4SLinus Torvalds 					u32 sdp0_nego;
46481da177e4SLinus Torvalds 
46491da177e4SLinus Torvalds 					cfg.hdr = &header0;
46501da177e4SLinus Torvalds 					cfg.physAddr = cfg0_dma_addr;
46511da177e4SLinus Torvalds 					cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
46521da177e4SLinus Torvalds 					cfg.dir = 0;
46531da177e4SLinus Torvalds 
46541da177e4SLinus Torvalds 					if (mpt_config(hd->ioc, &cfg) != 0)
46551da177e4SLinus Torvalds 						goto target_done;
46561da177e4SLinus Torvalds 
46571da177e4SLinus Torvalds 					sdp0_info = le32_to_cpu(pcfg0Data->Information) & 0x0E;
46581da177e4SLinus Torvalds 					sdp0_nego = (le32_to_cpu(pcfg0Data->NegotiatedParameters) & 0xFF00 ) >> 8;
46591da177e4SLinus Torvalds 
46601da177e4SLinus Torvalds 					/* Quantum and Fujitsu workarounds.
46611da177e4SLinus Torvalds 					 * Quantum: PPR U320 -> PPR reply with Ultra2 and wide
46621da177e4SLinus Torvalds 					 * Fujitsu: PPR U320 -> Msg Reject and Ultra2 and wide
46631da177e4SLinus Torvalds 					 * Resetart with a request for U160.
46641da177e4SLinus Torvalds 					 */
46651da177e4SLinus Torvalds 					if ((dv.now.factor == MPT_ULTRA320) && (sdp0_nego == MPT_ULTRA2)) {
46661da177e4SLinus Torvalds 							doFallback = 1;
46671da177e4SLinus Torvalds 					} else {
46681da177e4SLinus Torvalds 						dv.cmd = MPT_UPDATE_MAX;
46691da177e4SLinus Torvalds 						mptscsih_dv_parms(hd, &dv, (void *)pcfg0Data);
46701da177e4SLinus Torvalds 						/* Update the SCSI device page 1 area
46711da177e4SLinus Torvalds 						 */
46721da177e4SLinus Torvalds 						pcfg1Data->RequestedParameters = pcfg0Data->NegotiatedParameters;
46731da177e4SLinus Torvalds 						readPage0 = 1;
46741da177e4SLinus Torvalds 					}
46751da177e4SLinus Torvalds 				}
46761da177e4SLinus Torvalds 
46771da177e4SLinus Torvalds 				/* Quantum workaround. Restart this test will the fallback
46781da177e4SLinus Torvalds 				 * flag set.
46791da177e4SLinus Torvalds 				 */
46801da177e4SLinus Torvalds 				if (doFallback == 0) {
46811da177e4SLinus Torvalds 					if (memcmp(pbuf1, pbuf2, sz) != 0) {
46821da177e4SLinus Torvalds 						if (!firstPass)
46831da177e4SLinus Torvalds 							doFallback = 1;
46841da177e4SLinus Torvalds 					} else {
46851da177e4SLinus Torvalds 						ddvprintk((MYIOC_s_NOTE_FMT
46861da177e4SLinus Torvalds 						    "DV:Inquiry compared id=%d, calling initTarget\n", ioc->name, id));
46871da177e4SLinus Torvalds 						hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_DV_NOT_DONE;
46881da177e4SLinus Torvalds 						mptscsih_initTarget(hd,
46891da177e4SLinus Torvalds 							bus,
46901da177e4SLinus Torvalds 							id,
46911da177e4SLinus Torvalds 							lun,
46921da177e4SLinus Torvalds 							pbuf1,
46931da177e4SLinus Torvalds 							sz);
46941da177e4SLinus Torvalds 						break;	/* test complete */
46951da177e4SLinus Torvalds 					}
46961da177e4SLinus Torvalds 				}
46971da177e4SLinus Torvalds 
46981da177e4SLinus Torvalds 
46991da177e4SLinus Torvalds 			} else if (rc == MPT_SCANDV_ISSUE_SENSE)
47001da177e4SLinus Torvalds 				doFallback = 1;	/* set fallback flag */
47011da177e4SLinus Torvalds 			else if ((rc == MPT_SCANDV_DID_RESET) ||
47021da177e4SLinus Torvalds 				 (rc == MPT_SCANDV_SENSE) ||
47031da177e4SLinus Torvalds 				 (rc == MPT_SCANDV_FALLBACK))
47041da177e4SLinus Torvalds 				doFallback = 1;	/* set fallback flag */
47051da177e4SLinus Torvalds 			else
47061da177e4SLinus Torvalds 				goto target_done;
47071da177e4SLinus Torvalds 
47081da177e4SLinus Torvalds 			firstPass = 0;
47091da177e4SLinus Torvalds 		}
47101da177e4SLinus Torvalds 	}
47111da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_NOTE_FMT "DV: Basic test on id=%d completed OK.\n", ioc->name, id));
47121da177e4SLinus Torvalds 
47130d0c7974SMoore, Eric Dean  	if (ioc->spi_data.mpt_dv == 0)
47141da177e4SLinus Torvalds 		goto target_done;
47151da177e4SLinus Torvalds 
47161da177e4SLinus Torvalds 	inq0 = (*pbuf1) & 0x1F;
47171da177e4SLinus Torvalds 
47181da177e4SLinus Torvalds 	/* Continue only for disks
47191da177e4SLinus Torvalds 	 */
47201da177e4SLinus Torvalds 	if (inq0 != 0)
47211da177e4SLinus Torvalds 		goto target_done;
47221da177e4SLinus Torvalds 
47231da177e4SLinus Torvalds 	if ( ioc->spi_data.PortFlags == MPI_SCSIPORTPAGE2_PORT_FLAGS_BASIC_DV_ONLY )
47241da177e4SLinus Torvalds 		goto target_done;
47251da177e4SLinus Torvalds 
47261da177e4SLinus Torvalds 	/* Start the Enhanced Test.
47271da177e4SLinus Torvalds 	 * 0) issue TUR to clear out check conditions
47281da177e4SLinus Torvalds 	 * 1) read capacity of echo (regular) buffer
47291da177e4SLinus Torvalds 	 * 2) reserve device
47301da177e4SLinus Torvalds 	 * 3) do write-read-compare data pattern test
47311da177e4SLinus Torvalds 	 * 4) release
47321da177e4SLinus Torvalds 	 * 5) update nego parms to target struct
47331da177e4SLinus Torvalds 	 */
47341da177e4SLinus Torvalds 	cfg.hdr = &header1;
47351da177e4SLinus Torvalds 	cfg.physAddr = cfg1_dma_addr;
47361da177e4SLinus Torvalds 	cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
47371da177e4SLinus Torvalds 	cfg.dir = 1;
47381da177e4SLinus Torvalds 
47391da177e4SLinus Torvalds 	iocmd.cmd = TEST_UNIT_READY;
47401da177e4SLinus Torvalds 	iocmd.data_dma = -1;
47411da177e4SLinus Torvalds 	iocmd.data = NULL;
47421da177e4SLinus Torvalds 	iocmd.size = 0;
47431da177e4SLinus Torvalds 	notDone = 1;
47441da177e4SLinus Torvalds 	while (notDone) {
47451da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
47461da177e4SLinus Torvalds 			goto target_done;
47471da177e4SLinus Torvalds 
47481da177e4SLinus Torvalds 		if (hd->pLocal == NULL)
47491da177e4SLinus Torvalds 			goto target_done;
47501da177e4SLinus Torvalds 
47511da177e4SLinus Torvalds 		rc = hd->pLocal->completion;
47521da177e4SLinus Torvalds 		if (rc == MPT_SCANDV_GOOD)
47531da177e4SLinus Torvalds 			notDone = 0;
47541da177e4SLinus Torvalds 		else if (rc == MPT_SCANDV_SENSE) {
47551da177e4SLinus Torvalds 			u8 skey = hd->pLocal->sense[2] & 0x0F;
47561da177e4SLinus Torvalds 			u8 asc = hd->pLocal->sense[12];
47571da177e4SLinus Torvalds 			u8 ascq = hd->pLocal->sense[13];
47581da177e4SLinus Torvalds 			ddvprintk((MYIOC_s_INFO_FMT
47591da177e4SLinus Torvalds 				"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
47601da177e4SLinus Torvalds 				ioc->name, skey, asc, ascq));
47611da177e4SLinus Torvalds 
47621da177e4SLinus Torvalds 			if (skey == UNIT_ATTENTION)
47631da177e4SLinus Torvalds 				notDone++; /* repeat */
47641da177e4SLinus Torvalds 			else if ((skey == NOT_READY) &&
47651da177e4SLinus Torvalds 					(asc == 0x04)&&(ascq == 0x01)) {
47661da177e4SLinus Torvalds 				/* wait then repeat */
47671da177e4SLinus Torvalds 				mdelay (2000);
47681da177e4SLinus Torvalds 				notDone++;
47691da177e4SLinus Torvalds 			} else if ((skey == NOT_READY) && (asc == 0x3A)) {
47701da177e4SLinus Torvalds 				/* no medium, try read test anyway */
47711da177e4SLinus Torvalds 				notDone = 0;
47721da177e4SLinus Torvalds 			} else {
47731da177e4SLinus Torvalds 				/* All other errors are fatal.
47741da177e4SLinus Torvalds 				 */
47751da177e4SLinus Torvalds 				ddvprintk((MYIOC_s_INFO_FMT "DV: fatal error.",
47761da177e4SLinus Torvalds 						ioc->name));
47771da177e4SLinus Torvalds 				goto target_done;
47781da177e4SLinus Torvalds 			}
47791da177e4SLinus Torvalds 		} else
47801da177e4SLinus Torvalds 			goto target_done;
47811da177e4SLinus Torvalds 	}
47821da177e4SLinus Torvalds 
47831da177e4SLinus Torvalds 	iocmd.cmd = READ_BUFFER;
47841da177e4SLinus Torvalds 	iocmd.data_dma = buf1_dma;
47851da177e4SLinus Torvalds 	iocmd.data = pbuf1;
47861da177e4SLinus Torvalds 	iocmd.size = 4;
47871da177e4SLinus Torvalds 	iocmd.flags |= MPT_ICFLAG_BUF_CAP;
47881da177e4SLinus Torvalds 
47891da177e4SLinus Torvalds 	dataBufSize = 0;
47901da177e4SLinus Torvalds 	echoBufSize = 0;
47911da177e4SLinus Torvalds 	for (patt = 0; patt < 2; patt++) {
47921da177e4SLinus Torvalds 		if (patt == 0)
47931da177e4SLinus Torvalds 			iocmd.flags |= MPT_ICFLAG_ECHO;
47941da177e4SLinus Torvalds 		else
47951da177e4SLinus Torvalds 			iocmd.flags &= ~MPT_ICFLAG_ECHO;
47961da177e4SLinus Torvalds 
47971da177e4SLinus Torvalds 		notDone = 1;
47981da177e4SLinus Torvalds 		while (notDone) {
47991da177e4SLinus Torvalds 			bufsize = 0;
48001da177e4SLinus Torvalds 
48011da177e4SLinus Torvalds 			/* If not ready after 8 trials,
48021da177e4SLinus Torvalds 			 * give up on this device.
48031da177e4SLinus Torvalds 			 */
48041da177e4SLinus Torvalds 			if (notDone > 8)
48051da177e4SLinus Torvalds 				goto target_done;
48061da177e4SLinus Torvalds 
48071da177e4SLinus Torvalds 			if (mptscsih_do_cmd(hd, &iocmd) < 0)
48081da177e4SLinus Torvalds 				goto target_done;
48091da177e4SLinus Torvalds 			else if (hd->pLocal == NULL)
48101da177e4SLinus Torvalds 				goto target_done;
48111da177e4SLinus Torvalds 			else {
48121da177e4SLinus Torvalds 				rc = hd->pLocal->completion;
48131da177e4SLinus Torvalds 				ddvprintk(("ReadBuffer Comp Code %d", rc));
48141da177e4SLinus Torvalds 				ddvprintk(("  buff: %0x %0x %0x %0x\n",
48151da177e4SLinus Torvalds 					pbuf1[0], pbuf1[1], pbuf1[2], pbuf1[3]));
48161da177e4SLinus Torvalds 
48171da177e4SLinus Torvalds 				if (rc == MPT_SCANDV_GOOD) {
48181da177e4SLinus Torvalds 					notDone = 0;
48191da177e4SLinus Torvalds 					if (iocmd.flags & MPT_ICFLAG_ECHO) {
48201da177e4SLinus Torvalds 						bufsize =  ((pbuf1[2] & 0x1F) <<8) | pbuf1[3];
48211da177e4SLinus Torvalds 					} else {
48221da177e4SLinus Torvalds 						bufsize =  pbuf1[1]<<16 | pbuf1[2]<<8 | pbuf1[3];
48231da177e4SLinus Torvalds 					}
48241da177e4SLinus Torvalds 				} else if (rc == MPT_SCANDV_SENSE) {
48251da177e4SLinus Torvalds 					u8 skey = hd->pLocal->sense[2] & 0x0F;
48261da177e4SLinus Torvalds 					u8 asc = hd->pLocal->sense[12];
48271da177e4SLinus Torvalds 					u8 ascq = hd->pLocal->sense[13];
48281da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_INFO_FMT
48291da177e4SLinus Torvalds 						"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
48301da177e4SLinus Torvalds 						ioc->name, skey, asc, ascq));
48311da177e4SLinus Torvalds 					if (skey == ILLEGAL_REQUEST) {
48321da177e4SLinus Torvalds 						notDone = 0;
48331da177e4SLinus Torvalds 					} else if (skey == UNIT_ATTENTION) {
48341da177e4SLinus Torvalds 						notDone++; /* repeat */
48351da177e4SLinus Torvalds 					} else if ((skey == NOT_READY) &&
48361da177e4SLinus Torvalds 						(asc == 0x04)&&(ascq == 0x01)) {
48371da177e4SLinus Torvalds 						/* wait then repeat */
48381da177e4SLinus Torvalds 						mdelay (2000);
48391da177e4SLinus Torvalds 						notDone++;
48401da177e4SLinus Torvalds 					} else {
48411da177e4SLinus Torvalds 						/* All other errors are fatal.
48421da177e4SLinus Torvalds 						 */
48431da177e4SLinus Torvalds 						ddvprintk((MYIOC_s_INFO_FMT "DV: fatal error.",
48441da177e4SLinus Torvalds 							ioc->name));
48451da177e4SLinus Torvalds 						goto target_done;
48461da177e4SLinus Torvalds 					}
48471da177e4SLinus Torvalds 				} else {
48481da177e4SLinus Torvalds 					/* All other errors are fatal
48491da177e4SLinus Torvalds 					 */
48501da177e4SLinus Torvalds 					goto target_done;
48511da177e4SLinus Torvalds 				}
48521da177e4SLinus Torvalds 			}
48531da177e4SLinus Torvalds 		}
48541da177e4SLinus Torvalds 
48551da177e4SLinus Torvalds 		if (iocmd.flags & MPT_ICFLAG_ECHO)
48561da177e4SLinus Torvalds 			echoBufSize = bufsize;
48571da177e4SLinus Torvalds 		else
48581da177e4SLinus Torvalds 			dataBufSize = bufsize;
48591da177e4SLinus Torvalds 	}
48601da177e4SLinus Torvalds 	sz = 0;
48611da177e4SLinus Torvalds 	iocmd.flags &= ~MPT_ICFLAG_BUF_CAP;
48621da177e4SLinus Torvalds 
48631da177e4SLinus Torvalds 	/* Use echo buffers if possible,
48641da177e4SLinus Torvalds 	 * Exit if both buffers are 0.
48651da177e4SLinus Torvalds 	 */
48661da177e4SLinus Torvalds 	if (echoBufSize > 0) {
48671da177e4SLinus Torvalds 		iocmd.flags |= MPT_ICFLAG_ECHO;
48681da177e4SLinus Torvalds 		if (dataBufSize > 0)
48691da177e4SLinus Torvalds 			bufsize = min(echoBufSize, dataBufSize);
48701da177e4SLinus Torvalds 		else
48711da177e4SLinus Torvalds 			bufsize = echoBufSize;
48721da177e4SLinus Torvalds 	} else if (dataBufSize == 0)
48731da177e4SLinus Torvalds 		goto target_done;
48741da177e4SLinus Torvalds 
48751da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_INFO_FMT "%s Buffer Capacity %d\n", ioc->name,
48761da177e4SLinus Torvalds 		(iocmd.flags & MPT_ICFLAG_ECHO) ? "Echo" : " ", bufsize));
48771da177e4SLinus Torvalds 
48781da177e4SLinus Torvalds 	/* Data buffers for write-read-compare test max 1K.
48791da177e4SLinus Torvalds 	 */
48801da177e4SLinus Torvalds 	sz = min(bufsize, 1024);
48811da177e4SLinus Torvalds 
48821da177e4SLinus Torvalds 	/* --- loop ----
48831da177e4SLinus Torvalds 	 * On first pass, always issue a reserve.
48841da177e4SLinus Torvalds 	 * On additional loops, only if a reset has occurred.
48851da177e4SLinus Torvalds 	 * iocmd.flags indicates if echo or regular buffer
48861da177e4SLinus Torvalds 	 */
48871da177e4SLinus Torvalds 	for (patt = 0; patt < 4; patt++) {
48881da177e4SLinus Torvalds 		ddvprintk(("Pattern %d\n", patt));
48891da177e4SLinus Torvalds 		if ((iocmd.flags & MPT_ICFLAG_RESERVED) && (iocmd.flags & MPT_ICFLAG_DID_RESET)) {
48901da177e4SLinus Torvalds 			iocmd.cmd = TEST_UNIT_READY;
48911da177e4SLinus Torvalds 			iocmd.data_dma = -1;
48921da177e4SLinus Torvalds 			iocmd.data = NULL;
48931da177e4SLinus Torvalds 			iocmd.size = 0;
48941da177e4SLinus Torvalds 			if (mptscsih_do_cmd(hd, &iocmd) < 0)
48951da177e4SLinus Torvalds 				goto target_done;
48961da177e4SLinus Torvalds 
48971da177e4SLinus Torvalds 			iocmd.cmd = RELEASE;
48981da177e4SLinus Torvalds 			iocmd.data_dma = -1;
48991da177e4SLinus Torvalds 			iocmd.data = NULL;
49001da177e4SLinus Torvalds 			iocmd.size = 0;
49011da177e4SLinus Torvalds 			if (mptscsih_do_cmd(hd, &iocmd) < 0)
49021da177e4SLinus Torvalds 				goto target_done;
49031da177e4SLinus Torvalds 			else if (hd->pLocal == NULL)
49041da177e4SLinus Torvalds 				goto target_done;
49051da177e4SLinus Torvalds 			else {
49061da177e4SLinus Torvalds 				rc = hd->pLocal->completion;
49071da177e4SLinus Torvalds 				ddvprintk(("Release rc %d\n", rc));
49081da177e4SLinus Torvalds 				if (rc == MPT_SCANDV_GOOD)
49091da177e4SLinus Torvalds 					iocmd.flags &= ~MPT_ICFLAG_RESERVED;
49101da177e4SLinus Torvalds 				else
49111da177e4SLinus Torvalds 					goto target_done;
49121da177e4SLinus Torvalds 			}
49131da177e4SLinus Torvalds 			iocmd.flags &= ~MPT_ICFLAG_RESERVED;
49141da177e4SLinus Torvalds 		}
49151da177e4SLinus Torvalds 		iocmd.flags &= ~MPT_ICFLAG_DID_RESET;
49161da177e4SLinus Torvalds 
49171da177e4SLinus Torvalds 		repeat = 5;
49181da177e4SLinus Torvalds 		while (repeat && (!(iocmd.flags & MPT_ICFLAG_RESERVED))) {
49191da177e4SLinus Torvalds 			iocmd.cmd = RESERVE;
49201da177e4SLinus Torvalds 			iocmd.data_dma = -1;
49211da177e4SLinus Torvalds 			iocmd.data = NULL;
49221da177e4SLinus Torvalds 			iocmd.size = 0;
49231da177e4SLinus Torvalds 			if (mptscsih_do_cmd(hd, &iocmd) < 0)
49241da177e4SLinus Torvalds 				goto target_done;
49251da177e4SLinus Torvalds 			else if (hd->pLocal == NULL)
49261da177e4SLinus Torvalds 				goto target_done;
49271da177e4SLinus Torvalds 			else {
49281da177e4SLinus Torvalds 				rc = hd->pLocal->completion;
49291da177e4SLinus Torvalds 				if (rc == MPT_SCANDV_GOOD) {
49301da177e4SLinus Torvalds 					iocmd.flags |= MPT_ICFLAG_RESERVED;
49311da177e4SLinus Torvalds 				} else if (rc == MPT_SCANDV_SENSE) {
49321da177e4SLinus Torvalds 					/* Wait if coming ready
49331da177e4SLinus Torvalds 					 */
49341da177e4SLinus Torvalds 					u8 skey = hd->pLocal->sense[2] & 0x0F;
49351da177e4SLinus Torvalds 					u8 asc = hd->pLocal->sense[12];
49361da177e4SLinus Torvalds 					u8 ascq = hd->pLocal->sense[13];
49371da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_INFO_FMT
49381da177e4SLinus Torvalds 						"DV: Reserve Failed: ", ioc->name));
49391da177e4SLinus Torvalds 					ddvprintk(("SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
49401da177e4SLinus Torvalds 							skey, asc, ascq));
49411da177e4SLinus Torvalds 
49421da177e4SLinus Torvalds 					if ((skey == NOT_READY) && (asc == 0x04)&&
49431da177e4SLinus Torvalds 									(ascq == 0x01)) {
49441da177e4SLinus Torvalds 						/* wait then repeat */
49451da177e4SLinus Torvalds 						mdelay (2000);
49461da177e4SLinus Torvalds 						notDone++;
49471da177e4SLinus Torvalds 					} else {
49481da177e4SLinus Torvalds 						ddvprintk((MYIOC_s_INFO_FMT
49491da177e4SLinus Torvalds 							"DV: Reserved Failed.", ioc->name));
49501da177e4SLinus Torvalds 						goto target_done;
49511da177e4SLinus Torvalds 					}
49521da177e4SLinus Torvalds 				} else {
49531da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_INFO_FMT "DV: Reserved Failed.",
49541da177e4SLinus Torvalds 							 ioc->name));
49551da177e4SLinus Torvalds 					goto target_done;
49561da177e4SLinus Torvalds 				}
49571da177e4SLinus Torvalds 			}
49581da177e4SLinus Torvalds 		}
49591da177e4SLinus Torvalds 
49601da177e4SLinus Torvalds 		mptscsih_fillbuf(pbuf1, sz, patt, 1);
49611da177e4SLinus Torvalds 		iocmd.cmd = WRITE_BUFFER;
49621da177e4SLinus Torvalds 		iocmd.data_dma = buf1_dma;
49631da177e4SLinus Torvalds 		iocmd.data = pbuf1;
49641da177e4SLinus Torvalds 		iocmd.size = sz;
49651da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
49661da177e4SLinus Torvalds 			goto target_done;
49671da177e4SLinus Torvalds 		else if (hd->pLocal == NULL)
49681da177e4SLinus Torvalds 			goto target_done;
49691da177e4SLinus Torvalds 		else {
49701da177e4SLinus Torvalds 			rc = hd->pLocal->completion;
49711da177e4SLinus Torvalds 			if (rc == MPT_SCANDV_GOOD)
49721da177e4SLinus Torvalds 				;		/* Issue read buffer */
49731da177e4SLinus Torvalds 			else if (rc == MPT_SCANDV_DID_RESET) {
49741da177e4SLinus Torvalds 				/* If using echo buffers, reset to data buffers.
49751da177e4SLinus Torvalds 				 * Else do Fallback and restart
49761da177e4SLinus Torvalds 				 * this test (re-issue reserve
49771da177e4SLinus Torvalds 				 * because of bus reset).
49781da177e4SLinus Torvalds 				 */
49791da177e4SLinus Torvalds 				if ((iocmd.flags & MPT_ICFLAG_ECHO) && (dataBufSize >= bufsize)) {
49801da177e4SLinus Torvalds 					iocmd.flags &= ~MPT_ICFLAG_ECHO;
49811da177e4SLinus Torvalds 				} else {
49821da177e4SLinus Torvalds 					dv.cmd = MPT_FALLBACK;
49831da177e4SLinus Torvalds 					mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
49841da177e4SLinus Torvalds 
49851da177e4SLinus Torvalds 					if (mpt_config(hd->ioc, &cfg) != 0)
49861da177e4SLinus Torvalds 						goto target_done;
49871da177e4SLinus Torvalds 
49881da177e4SLinus Torvalds 					if ((!dv.now.width) && (!dv.now.offset))
49891da177e4SLinus Torvalds 						goto target_done;
49901da177e4SLinus Torvalds 				}
49911da177e4SLinus Torvalds 
49921da177e4SLinus Torvalds 				iocmd.flags |= MPT_ICFLAG_DID_RESET;
49931da177e4SLinus Torvalds 				patt = -1;
49941da177e4SLinus Torvalds 				continue;
49951da177e4SLinus Torvalds 			} else if (rc == MPT_SCANDV_SENSE) {
49961da177e4SLinus Torvalds 				/* Restart data test if UA, else quit.
49971da177e4SLinus Torvalds 				 */
49981da177e4SLinus Torvalds 				u8 skey = hd->pLocal->sense[2] & 0x0F;
49991da177e4SLinus Torvalds 				ddvprintk((MYIOC_s_INFO_FMT
50001da177e4SLinus Torvalds 					"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n", ioc->name, skey,
50011da177e4SLinus Torvalds 					hd->pLocal->sense[12], hd->pLocal->sense[13]));
50021da177e4SLinus Torvalds 				if (skey == UNIT_ATTENTION) {
50031da177e4SLinus Torvalds 					patt = -1;
50041da177e4SLinus Torvalds 					continue;
50051da177e4SLinus Torvalds 				} else if (skey == ILLEGAL_REQUEST) {
50061da177e4SLinus Torvalds 					if (iocmd.flags & MPT_ICFLAG_ECHO) {
50071da177e4SLinus Torvalds 						if (dataBufSize >= bufsize) {
50081da177e4SLinus Torvalds 							iocmd.flags &= ~MPT_ICFLAG_ECHO;
50091da177e4SLinus Torvalds 							patt = -1;
50101da177e4SLinus Torvalds 							continue;
50111da177e4SLinus Torvalds 						}
50121da177e4SLinus Torvalds 					}
50131da177e4SLinus Torvalds 					goto target_done;
50141da177e4SLinus Torvalds 				}
50151da177e4SLinus Torvalds 				else
50161da177e4SLinus Torvalds 					goto target_done;
50171da177e4SLinus Torvalds 			} else {
50181da177e4SLinus Torvalds 				/* fatal error */
50191da177e4SLinus Torvalds 				goto target_done;
50201da177e4SLinus Torvalds 			}
50211da177e4SLinus Torvalds 		}
50221da177e4SLinus Torvalds 
50231da177e4SLinus Torvalds 		iocmd.cmd = READ_BUFFER;
50241da177e4SLinus Torvalds 		iocmd.data_dma = buf2_dma;
50251da177e4SLinus Torvalds 		iocmd.data = pbuf2;
50261da177e4SLinus Torvalds 		iocmd.size = sz;
50271da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
50281da177e4SLinus Torvalds 			goto target_done;
50291da177e4SLinus Torvalds 		else if (hd->pLocal == NULL)
50301da177e4SLinus Torvalds 			goto target_done;
50311da177e4SLinus Torvalds 		else {
50321da177e4SLinus Torvalds 			rc = hd->pLocal->completion;
50331da177e4SLinus Torvalds 			if (rc == MPT_SCANDV_GOOD) {
50341da177e4SLinus Torvalds 				 /* If buffers compare,
50351da177e4SLinus Torvalds 				  * go to next pattern,
50361da177e4SLinus Torvalds 				  * else, do a fallback and restart
50371da177e4SLinus Torvalds 				  * data transfer test.
50381da177e4SLinus Torvalds 				  */
50391da177e4SLinus Torvalds 				if (memcmp (pbuf1, pbuf2, sz) == 0) {
50401da177e4SLinus Torvalds 					; /* goto next pattern */
50411da177e4SLinus Torvalds 				} else {
50421da177e4SLinus Torvalds 					/* Miscompare with Echo buffer, go to data buffer,
50431da177e4SLinus Torvalds 					 * if that buffer exists.
50441da177e4SLinus Torvalds 					 * Miscompare with Data buffer, check first 4 bytes,
50451da177e4SLinus Torvalds 					 * some devices return capacity. Exit in this case.
50461da177e4SLinus Torvalds 					 */
50471da177e4SLinus Torvalds 					if (iocmd.flags & MPT_ICFLAG_ECHO) {
50481da177e4SLinus Torvalds 						if (dataBufSize >= bufsize)
50491da177e4SLinus Torvalds 							iocmd.flags &= ~MPT_ICFLAG_ECHO;
50501da177e4SLinus Torvalds 						else
50511da177e4SLinus Torvalds 							goto target_done;
50521da177e4SLinus Torvalds 					} else {
50531da177e4SLinus Torvalds 						if (dataBufSize == (pbuf2[1]<<16 | pbuf2[2]<<8 | pbuf2[3])) {
50541da177e4SLinus Torvalds 							/* Argh. Device returning wrong data.
50551da177e4SLinus Torvalds 							 * Quit DV for this device.
50561da177e4SLinus Torvalds 							 */
50571da177e4SLinus Torvalds 							goto target_done;
50581da177e4SLinus Torvalds 						}
50591da177e4SLinus Torvalds 
50601da177e4SLinus Torvalds 						/* Had an actual miscompare. Slow down.*/
50611da177e4SLinus Torvalds 						dv.cmd = MPT_FALLBACK;
50621da177e4SLinus Torvalds 						mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
50631da177e4SLinus Torvalds 
50641da177e4SLinus Torvalds 						if (mpt_config(hd->ioc, &cfg) != 0)
50651da177e4SLinus Torvalds 							goto target_done;
50661da177e4SLinus Torvalds 
50671da177e4SLinus Torvalds 						if ((!dv.now.width) && (!dv.now.offset))
50681da177e4SLinus Torvalds 							goto target_done;
50691da177e4SLinus Torvalds 					}
50701da177e4SLinus Torvalds 
50711da177e4SLinus Torvalds 					patt = -1;
50721da177e4SLinus Torvalds 					continue;
50731da177e4SLinus Torvalds 				}
50741da177e4SLinus Torvalds 			} else if (rc == MPT_SCANDV_DID_RESET) {
50751da177e4SLinus Torvalds 				/* Do Fallback and restart
50761da177e4SLinus Torvalds 				 * this test (re-issue reserve
50771da177e4SLinus Torvalds 				 * because of bus reset).
50781da177e4SLinus Torvalds 				 */
50791da177e4SLinus Torvalds 				dv.cmd = MPT_FALLBACK;
50801da177e4SLinus Torvalds 				mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
50811da177e4SLinus Torvalds 
50821da177e4SLinus Torvalds 				if (mpt_config(hd->ioc, &cfg) != 0)
50831da177e4SLinus Torvalds 					 goto target_done;
50841da177e4SLinus Torvalds 
50851da177e4SLinus Torvalds 				if ((!dv.now.width) && (!dv.now.offset))
50861da177e4SLinus Torvalds 					goto target_done;
50871da177e4SLinus Torvalds 
50881da177e4SLinus Torvalds 				iocmd.flags |= MPT_ICFLAG_DID_RESET;
50891da177e4SLinus Torvalds 				patt = -1;
50901da177e4SLinus Torvalds 				continue;
50911da177e4SLinus Torvalds 			} else if (rc == MPT_SCANDV_SENSE) {
50921da177e4SLinus Torvalds 				/* Restart data test if UA, else quit.
50931da177e4SLinus Torvalds 				 */
50941da177e4SLinus Torvalds 				u8 skey = hd->pLocal->sense[2] & 0x0F;
50951da177e4SLinus Torvalds 				ddvprintk((MYIOC_s_INFO_FMT
50961da177e4SLinus Torvalds 					"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n", ioc->name, skey,
50971da177e4SLinus Torvalds 					hd->pLocal->sense[12], hd->pLocal->sense[13]));
50981da177e4SLinus Torvalds 				if (skey == UNIT_ATTENTION) {
50991da177e4SLinus Torvalds 					patt = -1;
51001da177e4SLinus Torvalds 					continue;
51011da177e4SLinus Torvalds 				}
51021da177e4SLinus Torvalds 				else
51031da177e4SLinus Torvalds 					goto target_done;
51041da177e4SLinus Torvalds 			} else {
51051da177e4SLinus Torvalds 				/* fatal error */
51061da177e4SLinus Torvalds 				goto target_done;
51071da177e4SLinus Torvalds 			}
51081da177e4SLinus Torvalds 		}
51091da177e4SLinus Torvalds 
51101da177e4SLinus Torvalds 	} /* --- end of patt loop ---- */
51111da177e4SLinus Torvalds 
51121da177e4SLinus Torvalds target_done:
51131da177e4SLinus Torvalds 	if (iocmd.flags & MPT_ICFLAG_RESERVED) {
51141da177e4SLinus Torvalds 		iocmd.cmd = RELEASE;
51151da177e4SLinus Torvalds 		iocmd.data_dma = -1;
51161da177e4SLinus Torvalds 		iocmd.data = NULL;
51171da177e4SLinus Torvalds 		iocmd.size = 0;
51181da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
51191da177e4SLinus Torvalds 			printk(MYIOC_s_INFO_FMT "DV: Release failed. id %d",
51201da177e4SLinus Torvalds 					ioc->name, id);
51211da177e4SLinus Torvalds 		else if (hd->pLocal) {
51221da177e4SLinus Torvalds 			if (hd->pLocal->completion == MPT_SCANDV_GOOD)
51231da177e4SLinus Torvalds 				iocmd.flags &= ~MPT_ICFLAG_RESERVED;
51241da177e4SLinus Torvalds 		} else {
51251da177e4SLinus Torvalds 			printk(MYIOC_s_INFO_FMT "DV: Release failed. id %d",
51261da177e4SLinus Torvalds 						ioc->name, id);
51271da177e4SLinus Torvalds 		}
51281da177e4SLinus Torvalds 	}
51291da177e4SLinus Torvalds 
51301da177e4SLinus Torvalds 
51311da177e4SLinus Torvalds 	/* Set if cfg1_dma_addr contents is valid
51321da177e4SLinus Torvalds 	 */
51331da177e4SLinus Torvalds 	if ((cfg.hdr != NULL) && (retcode == 0)){
51341da177e4SLinus Torvalds 		/* If disk, not U320, disable QAS
51351da177e4SLinus Torvalds 		 */
51361da177e4SLinus Torvalds 		if ((inq0 == 0) && (dv.now.factor > MPT_ULTRA320)) {
51371da177e4SLinus Torvalds 			hd->ioc->spi_data.noQas = MPT_TARGET_NO_NEGO_QAS;
51381da177e4SLinus Torvalds 			ddvprintk((MYIOC_s_NOTE_FMT
51391da177e4SLinus Torvalds 			    "noQas set due to id=%d has factor=%x\n", ioc->name, id, dv.now.factor));
51401da177e4SLinus Torvalds 		}
51411da177e4SLinus Torvalds 
51421da177e4SLinus Torvalds 		dv.cmd = MPT_SAVE;
51431da177e4SLinus Torvalds 		mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
51441da177e4SLinus Torvalds 
51451da177e4SLinus Torvalds 		/* Double writes to SDP1 can cause problems,
51461da177e4SLinus Torvalds 		 * skip save of the final negotiated settings to
51471da177e4SLinus Torvalds 		 * SCSI device page 1.
51481da177e4SLinus Torvalds 		 *
51491da177e4SLinus Torvalds 		cfg.hdr = &header1;
51501da177e4SLinus Torvalds 		cfg.physAddr = cfg1_dma_addr;
51511da177e4SLinus Torvalds 		cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
51521da177e4SLinus Torvalds 		cfg.dir = 1;
51531da177e4SLinus Torvalds 		mpt_config(hd->ioc, &cfg);
51541da177e4SLinus Torvalds 		 */
51551da177e4SLinus Torvalds 	}
51561da177e4SLinus Torvalds 
51571da177e4SLinus Torvalds 	/* If this is a RAID Passthrough, enable internal IOs
51581da177e4SLinus Torvalds 	 */
51591da177e4SLinus Torvalds 	if (iocmd.flags & MPT_ICFLAG_PHYS_DISK) {
51601da177e4SLinus Torvalds 		if (mptscsih_do_raid(hd, MPI_RAID_ACTION_ENABLE_PHYS_IO, &iocmd) < 0)
51611da177e4SLinus Torvalds 			ddvprintk((MYIOC_s_ERR_FMT "RAID Enable FAILED!\n", ioc->name));
51621da177e4SLinus Torvalds 	}
51631da177e4SLinus Torvalds 
51641da177e4SLinus Torvalds 	/* Done with the DV scan of the current target
51651da177e4SLinus Torvalds 	 */
51661da177e4SLinus Torvalds 	if (pDvBuf)
51671da177e4SLinus Torvalds 		pci_free_consistent(ioc->pcidev, dv_alloc, pDvBuf, dvbuf_dma);
51681da177e4SLinus Torvalds 
51691da177e4SLinus Torvalds 	ddvtprintk((MYIOC_s_INFO_FMT "DV Done id=%d\n",
51701da177e4SLinus Torvalds 			ioc->name, id));
51711da177e4SLinus Torvalds 
51721da177e4SLinus Torvalds 	return retcode;
51731da177e4SLinus Torvalds }
51741da177e4SLinus Torvalds 
51751da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
51761da177e4SLinus Torvalds /*	mptscsih_dv_parms - perform a variety of operations on the
51771da177e4SLinus Torvalds  *	parameters used for negotiation.
51781da177e4SLinus Torvalds  *	@hd: Pointer to a SCSI host.
51791da177e4SLinus Torvalds  *	@dv: Pointer to a structure that contains the maximum and current
51801da177e4SLinus Torvalds  *		negotiated parameters.
51811da177e4SLinus Torvalds  */
51821da177e4SLinus Torvalds static void
51831da177e4SLinus Torvalds mptscsih_dv_parms(MPT_SCSI_HOST *hd, DVPARAMETERS *dv,void *pPage)
51841da177e4SLinus Torvalds {
51851da177e4SLinus Torvalds 	VirtDevice		*pTarget;
51861da177e4SLinus Torvalds 	SCSIDevicePage0_t	*pPage0;
51871da177e4SLinus Torvalds 	SCSIDevicePage1_t	*pPage1;
51881da177e4SLinus Torvalds 	int			val = 0, data, configuration;
51891da177e4SLinus Torvalds 	u8			width = 0;
51901da177e4SLinus Torvalds 	u8			offset = 0;
51911da177e4SLinus Torvalds 	u8			factor = 0;
51921da177e4SLinus Torvalds 	u8			negoFlags = 0;
51931da177e4SLinus Torvalds 	u8			cmd = dv->cmd;
51941da177e4SLinus Torvalds 	u8			id = dv->id;
51951da177e4SLinus Torvalds 
51961da177e4SLinus Torvalds 	switch (cmd) {
51971da177e4SLinus Torvalds 	case MPT_GET_NVRAM_VALS:
51981da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT "Getting NVRAM: ",
51991da177e4SLinus Torvalds 							 hd->ioc->name));
52001da177e4SLinus Torvalds 		/* Get the NVRAM values and save in tmax
52011da177e4SLinus Torvalds 		 * If not an LVD bus, the adapter minSyncFactor has been
52021da177e4SLinus Torvalds 		 * already throttled back.
52031da177e4SLinus Torvalds 		 */
52041da177e4SLinus Torvalds 		if ((hd->Targets)&&((pTarget = hd->Targets[(int)id]) != NULL) && !pTarget->raidVolume) {
52051da177e4SLinus Torvalds 			width = pTarget->maxWidth;
52061da177e4SLinus Torvalds 			offset = pTarget->maxOffset;
52071da177e4SLinus Torvalds 			factor = pTarget->minSyncFactor;
52081da177e4SLinus Torvalds 			negoFlags = pTarget->negoFlags;
52091da177e4SLinus Torvalds 		} else {
52101da177e4SLinus Torvalds 			if (hd->ioc->spi_data.nvram && (hd->ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
52111da177e4SLinus Torvalds 				data = hd->ioc->spi_data.nvram[id];
52121da177e4SLinus Torvalds 				width = data & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
52131da177e4SLinus Torvalds 				if ((offset = hd->ioc->spi_data.maxSyncOffset) == 0)
52141da177e4SLinus Torvalds 					factor = MPT_ASYNC;
52151da177e4SLinus Torvalds 				else {
52161da177e4SLinus Torvalds 					factor = (data & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
52171da177e4SLinus Torvalds 					if ((factor == 0) || (factor == MPT_ASYNC)){
52181da177e4SLinus Torvalds 						factor = MPT_ASYNC;
52191da177e4SLinus Torvalds 						offset = 0;
52201da177e4SLinus Torvalds 					}
52211da177e4SLinus Torvalds 				}
52221da177e4SLinus Torvalds 			} else {
52231da177e4SLinus Torvalds 				width = MPT_NARROW;
52241da177e4SLinus Torvalds 				offset = 0;
52251da177e4SLinus Torvalds 				factor = MPT_ASYNC;
52261da177e4SLinus Torvalds 			}
52271da177e4SLinus Torvalds 
52281da177e4SLinus Torvalds 			/* Set the negotiation flags */
52291da177e4SLinus Torvalds 			negoFlags = hd->ioc->spi_data.noQas;
52301da177e4SLinus Torvalds 			if (!width)
52311da177e4SLinus Torvalds 				negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
52321da177e4SLinus Torvalds 
52331da177e4SLinus Torvalds 			if (!offset)
52341da177e4SLinus Torvalds 				negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
52351da177e4SLinus Torvalds 		}
52361da177e4SLinus Torvalds 
52371da177e4SLinus Torvalds 		/* limit by adapter capabilities */
52381da177e4SLinus Torvalds 		width = min(width, hd->ioc->spi_data.maxBusWidth);
52391da177e4SLinus Torvalds 		offset = min(offset, hd->ioc->spi_data.maxSyncOffset);
52401da177e4SLinus Torvalds 		factor = max(factor, hd->ioc->spi_data.minSyncFactor);
52411da177e4SLinus Torvalds 
52421da177e4SLinus Torvalds 		/* Check Consistency */
52431da177e4SLinus Torvalds 		if (offset && (factor < MPT_ULTRA2) && !width)
52441da177e4SLinus Torvalds 			factor = MPT_ULTRA2;
52451da177e4SLinus Torvalds 
52461da177e4SLinus Torvalds 		dv->max.width = width;
52471da177e4SLinus Torvalds 		dv->max.offset = offset;
52481da177e4SLinus Torvalds 		dv->max.factor = factor;
52491da177e4SLinus Torvalds 		dv->max.flags = negoFlags;
52501da177e4SLinus Torvalds 		ddvprintk((" id=%d width=%d factor=%x offset=%x flags=%x\n",
52511da177e4SLinus Torvalds 				id, width, factor, offset, negoFlags));
52521da177e4SLinus Torvalds 		break;
52531da177e4SLinus Torvalds 
52541da177e4SLinus Torvalds 	case MPT_UPDATE_MAX:
52551da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT
52561da177e4SLinus Torvalds 			"Updating with SDP0 Data: ", hd->ioc->name));
52571da177e4SLinus Torvalds 		/* Update tmax values with those from Device Page 0.*/
52581da177e4SLinus Torvalds 		pPage0 = (SCSIDevicePage0_t *) pPage;
52591da177e4SLinus Torvalds 		if (pPage0) {
52601da177e4SLinus Torvalds 			val = cpu_to_le32(pPage0->NegotiatedParameters);
52611da177e4SLinus Torvalds 			dv->max.width = val & MPI_SCSIDEVPAGE0_NP_WIDE ? 1 : 0;
52621da177e4SLinus Torvalds 			dv->max.offset = (val&MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) >> 16;
52631da177e4SLinus Torvalds 			dv->max.factor = (val&MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> 8;
52641da177e4SLinus Torvalds 		}
52651da177e4SLinus Torvalds 
52661da177e4SLinus Torvalds 		dv->now.width = dv->max.width;
52671da177e4SLinus Torvalds 		dv->now.offset = dv->max.offset;
52681da177e4SLinus Torvalds 		dv->now.factor = dv->max.factor;
52691da177e4SLinus Torvalds 		ddvprintk(("id=%d width=%d factor=%x offset=%x flags=%x\n",
52701da177e4SLinus Torvalds 				id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags));
52711da177e4SLinus Torvalds 		break;
52721da177e4SLinus Torvalds 
52731da177e4SLinus Torvalds 	case MPT_SET_MAX:
52741da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT "Setting Max: ",
52751da177e4SLinus Torvalds 								hd->ioc->name));
52761da177e4SLinus Torvalds 		/* Set current to the max values. Update the config page.*/
52771da177e4SLinus Torvalds 		dv->now.width = dv->max.width;
52781da177e4SLinus Torvalds 		dv->now.offset = dv->max.offset;
52791da177e4SLinus Torvalds 		dv->now.factor = dv->max.factor;
52801da177e4SLinus Torvalds 		dv->now.flags = dv->max.flags;
52811da177e4SLinus Torvalds 
52821da177e4SLinus Torvalds 		pPage1 = (SCSIDevicePage1_t *)pPage;
52831da177e4SLinus Torvalds 		if (pPage1) {
52841da177e4SLinus Torvalds 			mptscsih_setDevicePage1Flags (dv->now.width, dv->now.factor,
52851da177e4SLinus Torvalds 				dv->now.offset, &val, &configuration, dv->now.flags);
52861da177e4SLinus Torvalds 			dnegoprintk(("Setting Max: id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x config=%x\n",
52871da177e4SLinus Torvalds 				id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags, val, configuration));
52881da177e4SLinus Torvalds 			pPage1->RequestedParameters = le32_to_cpu(val);
52891da177e4SLinus Torvalds 			pPage1->Reserved = 0;
52901da177e4SLinus Torvalds 			pPage1->Configuration = le32_to_cpu(configuration);
52911da177e4SLinus Torvalds 		}
52921da177e4SLinus Torvalds 
52931da177e4SLinus Torvalds 		ddvprintk(("id=%d width=%d factor=%x offset=%x flags=%x request=%x configuration=%x\n",
52941da177e4SLinus Torvalds 				id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags, val, configuration));
52951da177e4SLinus Torvalds 		break;
52961da177e4SLinus Torvalds 
52971da177e4SLinus Torvalds 	case MPT_SET_MIN:
52981da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT "Setting Min: ",
52991da177e4SLinus Torvalds 								hd->ioc->name));
53001da177e4SLinus Torvalds 		/* Set page to asynchronous and narrow
53011da177e4SLinus Torvalds 		 * Do not update now, breaks fallback routine. */
53021da177e4SLinus Torvalds 		width = MPT_NARROW;
53031da177e4SLinus Torvalds 		offset = 0;
53041da177e4SLinus Torvalds 		factor = MPT_ASYNC;
53051da177e4SLinus Torvalds 		negoFlags = dv->max.flags;
53061da177e4SLinus Torvalds 
53071da177e4SLinus Torvalds 		pPage1 = (SCSIDevicePage1_t *)pPage;
53081da177e4SLinus Torvalds 		if (pPage1) {
53091da177e4SLinus Torvalds 			mptscsih_setDevicePage1Flags (width, factor,
53101da177e4SLinus Torvalds 				offset, &val, &configuration, negoFlags);
53111da177e4SLinus Torvalds 			dnegoprintk(("Setting Min: id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x config=%x\n",
53121da177e4SLinus Torvalds 				id, width, factor, offset, negoFlags, val, configuration));
53131da177e4SLinus Torvalds 			pPage1->RequestedParameters = le32_to_cpu(val);
53141da177e4SLinus Torvalds 			pPage1->Reserved = 0;
53151da177e4SLinus Torvalds 			pPage1->Configuration = le32_to_cpu(configuration);
53161da177e4SLinus Torvalds 		}
53171da177e4SLinus Torvalds 		ddvprintk(("id=%d width=%d factor=%x offset=%x request=%x config=%x negoFlags=%x\n",
53181da177e4SLinus Torvalds 				id, width, factor, offset, val, configuration, negoFlags));
53191da177e4SLinus Torvalds 		break;
53201da177e4SLinus Torvalds 
53211da177e4SLinus Torvalds 	case MPT_FALLBACK:
53221da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT
53231da177e4SLinus Torvalds 			"Fallback: Start: offset %d, factor %x, width %d \n",
53241da177e4SLinus Torvalds 				hd->ioc->name, dv->now.offset,
53251da177e4SLinus Torvalds 				dv->now.factor, dv->now.width));
53261da177e4SLinus Torvalds 		width = dv->now.width;
53271da177e4SLinus Torvalds 		offset = dv->now.offset;
53281da177e4SLinus Torvalds 		factor = dv->now.factor;
53291da177e4SLinus Torvalds 		if ((offset) && (dv->max.width)) {
53301da177e4SLinus Torvalds 			if (factor < MPT_ULTRA160)
53311da177e4SLinus Torvalds 				factor = MPT_ULTRA160;
53321da177e4SLinus Torvalds 			else if (factor < MPT_ULTRA2) {
53331da177e4SLinus Torvalds 				factor = MPT_ULTRA2;
53341da177e4SLinus Torvalds 				width = MPT_WIDE;
53351da177e4SLinus Torvalds 			} else if ((factor == MPT_ULTRA2) && width) {
53361da177e4SLinus Torvalds 				factor = MPT_ULTRA2;
53371da177e4SLinus Torvalds 				width = MPT_NARROW;
53381da177e4SLinus Torvalds 			} else if (factor < MPT_ULTRA) {
53391da177e4SLinus Torvalds 				factor = MPT_ULTRA;
53401da177e4SLinus Torvalds 				width = MPT_WIDE;
53411da177e4SLinus Torvalds 			} else if ((factor == MPT_ULTRA) && width) {
53421da177e4SLinus Torvalds 				width = MPT_NARROW;
53431da177e4SLinus Torvalds 			} else if (factor < MPT_FAST) {
53441da177e4SLinus Torvalds 				factor = MPT_FAST;
53451da177e4SLinus Torvalds 				width = MPT_WIDE;
53461da177e4SLinus Torvalds 			} else if ((factor == MPT_FAST) && width) {
53471da177e4SLinus Torvalds 				factor = MPT_FAST;
53481da177e4SLinus Torvalds 				width = MPT_NARROW;
53491da177e4SLinus Torvalds 			} else if (factor < MPT_SCSI) {
53501da177e4SLinus Torvalds 				factor = MPT_SCSI;
53511da177e4SLinus Torvalds 				width = MPT_WIDE;
53521da177e4SLinus Torvalds 			} else if ((factor == MPT_SCSI) && width) {
53531da177e4SLinus Torvalds 				factor = MPT_SCSI;
53541da177e4SLinus Torvalds 				width = MPT_NARROW;
53551da177e4SLinus Torvalds 			} else {
53561da177e4SLinus Torvalds 				factor = MPT_ASYNC;
53571da177e4SLinus Torvalds 				offset = 0;
53581da177e4SLinus Torvalds 			}
53591da177e4SLinus Torvalds 
53601da177e4SLinus Torvalds 		} else if (offset) {
53611da177e4SLinus Torvalds 			width = MPT_NARROW;
53621da177e4SLinus Torvalds 			if (factor < MPT_ULTRA)
53631da177e4SLinus Torvalds 				factor = MPT_ULTRA;
53641da177e4SLinus Torvalds 			else if (factor < MPT_FAST)
53651da177e4SLinus Torvalds 				factor = MPT_FAST;
53661da177e4SLinus Torvalds 			else if (factor < MPT_SCSI)
53671da177e4SLinus Torvalds 				factor = MPT_SCSI;
53681da177e4SLinus Torvalds 			else {
53691da177e4SLinus Torvalds 				factor = MPT_ASYNC;
53701da177e4SLinus Torvalds 				offset = 0;
53711da177e4SLinus Torvalds 			}
53721da177e4SLinus Torvalds 
53731da177e4SLinus Torvalds 		} else {
53741da177e4SLinus Torvalds 			width = MPT_NARROW;
53751da177e4SLinus Torvalds 			factor = MPT_ASYNC;
53761da177e4SLinus Torvalds 		}
53771da177e4SLinus Torvalds 		dv->max.flags |= MPT_TARGET_NO_NEGO_QAS;
53781da177e4SLinus Torvalds 		dv->max.flags &= ~MPT_TAPE_NEGO_IDP;
53791da177e4SLinus Torvalds 
53801da177e4SLinus Torvalds 		dv->now.width = width;
53811da177e4SLinus Torvalds 		dv->now.offset = offset;
53821da177e4SLinus Torvalds 		dv->now.factor = factor;
53831da177e4SLinus Torvalds 		dv->now.flags = dv->max.flags;
53841da177e4SLinus Torvalds 
53851da177e4SLinus Torvalds 		pPage1 = (SCSIDevicePage1_t *)pPage;
53861da177e4SLinus Torvalds 		if (pPage1) {
53871da177e4SLinus Torvalds 			mptscsih_setDevicePage1Flags (width, factor, offset, &val,
53881da177e4SLinus Torvalds 						&configuration, dv->now.flags);
53891da177e4SLinus Torvalds 			dnegoprintk(("Finish: id=%d width=%d offset=%d factor=%x flags=%x request=%x config=%x\n",
53901da177e4SLinus Torvalds 			     id, width, offset, factor, dv->now.flags, val, configuration));
53911da177e4SLinus Torvalds 
53921da177e4SLinus Torvalds 			pPage1->RequestedParameters = le32_to_cpu(val);
53931da177e4SLinus Torvalds 			pPage1->Reserved = 0;
53941da177e4SLinus Torvalds 			pPage1->Configuration = le32_to_cpu(configuration);
53951da177e4SLinus Torvalds 		}
53961da177e4SLinus Torvalds 
53971da177e4SLinus Torvalds 		ddvprintk(("Finish: id=%d offset=%d factor=%x width=%d request=%x config=%x\n",
53981da177e4SLinus Torvalds 			     id, dv->now.offset, dv->now.factor, dv->now.width, val, configuration));
53991da177e4SLinus Torvalds 		break;
54001da177e4SLinus Torvalds 
54011da177e4SLinus Torvalds 	case MPT_SAVE:
54021da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT
54031da177e4SLinus Torvalds 			"Saving to Target structure: ", hd->ioc->name));
54041da177e4SLinus Torvalds 		ddvprintk(("id=%d width=%x factor=%x offset=%d flags=%x\n",
54051da177e4SLinus Torvalds 			     id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags));
54061da177e4SLinus Torvalds 
54071da177e4SLinus Torvalds 		/* Save these values to target structures
54081da177e4SLinus Torvalds 		 * or overwrite nvram (phys disks only).
54091da177e4SLinus Torvalds 		 */
54101da177e4SLinus Torvalds 
54111da177e4SLinus Torvalds 		if ((hd->Targets)&&((pTarget = hd->Targets[(int)id]) != NULL) && !pTarget->raidVolume ) {
54121da177e4SLinus Torvalds 			pTarget->maxWidth = dv->now.width;
54131da177e4SLinus Torvalds 			pTarget->maxOffset = dv->now.offset;
54141da177e4SLinus Torvalds 			pTarget->minSyncFactor = dv->now.factor;
54151da177e4SLinus Torvalds 			pTarget->negoFlags = dv->now.flags;
54161da177e4SLinus Torvalds 		} else {
54171da177e4SLinus Torvalds 			/* Preserv all flags, use
54181da177e4SLinus Torvalds 			 * read-modify-write algorithm
54191da177e4SLinus Torvalds 			 */
54201da177e4SLinus Torvalds 			if (hd->ioc->spi_data.nvram) {
54211da177e4SLinus Torvalds 				data = hd->ioc->spi_data.nvram[id];
54221da177e4SLinus Torvalds 
54231da177e4SLinus Torvalds 				if (dv->now.width)
54241da177e4SLinus Torvalds 					data &= ~MPT_NVRAM_WIDE_DISABLE;
54251da177e4SLinus Torvalds 				else
54261da177e4SLinus Torvalds 					data |= MPT_NVRAM_WIDE_DISABLE;
54271da177e4SLinus Torvalds 
54281da177e4SLinus Torvalds 				if (!dv->now.offset)
54291da177e4SLinus Torvalds 					factor = MPT_ASYNC;
54301da177e4SLinus Torvalds 
54311da177e4SLinus Torvalds 				data &= ~MPT_NVRAM_SYNC_MASK;
54321da177e4SLinus Torvalds 				data |= (dv->now.factor << MPT_NVRAM_SYNC_SHIFT) & MPT_NVRAM_SYNC_MASK;
54331da177e4SLinus Torvalds 
54341da177e4SLinus Torvalds 				hd->ioc->spi_data.nvram[id] = data;
54351da177e4SLinus Torvalds 			}
54361da177e4SLinus Torvalds 		}
54371da177e4SLinus Torvalds 		break;
54381da177e4SLinus Torvalds 	}
54391da177e4SLinus Torvalds }
54401da177e4SLinus Torvalds 
54411da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
54421da177e4SLinus Torvalds /*	mptscsih_fillbuf - fill a buffer with a special data pattern
54431da177e4SLinus Torvalds  *		cleanup. For bus scan only.
54441da177e4SLinus Torvalds  *
54451da177e4SLinus Torvalds  *	@buffer: Pointer to data buffer to be filled.
54461da177e4SLinus Torvalds  *	@size: Number of bytes to fill
54471da177e4SLinus Torvalds  *	@index: Pattern index
54481da177e4SLinus Torvalds  *	@width: bus width, 0 (8 bits) or 1 (16 bits)
54491da177e4SLinus Torvalds  */
54501da177e4SLinus Torvalds static void
54511da177e4SLinus Torvalds mptscsih_fillbuf(char *buffer, int size, int index, int width)
54521da177e4SLinus Torvalds {
54531da177e4SLinus Torvalds 	char *ptr = buffer;
54541da177e4SLinus Torvalds 	int ii;
54551da177e4SLinus Torvalds 	char byte;
54561da177e4SLinus Torvalds 	short val;
54571da177e4SLinus Torvalds 
54581da177e4SLinus Torvalds 	switch (index) {
54591da177e4SLinus Torvalds 	case 0:
54601da177e4SLinus Torvalds 
54611da177e4SLinus Torvalds 		if (width) {
54621da177e4SLinus Torvalds 			/* Pattern:  0000 FFFF 0000 FFFF
54631da177e4SLinus Torvalds 			 */
54641da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
54651da177e4SLinus Torvalds 				if (ii & 0x02)
54661da177e4SLinus Torvalds 					*ptr = 0xFF;
54671da177e4SLinus Torvalds 				else
54681da177e4SLinus Torvalds 					*ptr = 0x00;
54691da177e4SLinus Torvalds 			}
54701da177e4SLinus Torvalds 		} else {
54711da177e4SLinus Torvalds 			/* Pattern:  00 FF 00 FF
54721da177e4SLinus Torvalds 			 */
54731da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
54741da177e4SLinus Torvalds 				if (ii & 0x01)
54751da177e4SLinus Torvalds 					*ptr = 0xFF;
54761da177e4SLinus Torvalds 				else
54771da177e4SLinus Torvalds 					*ptr = 0x00;
54781da177e4SLinus Torvalds 			}
54791da177e4SLinus Torvalds 		}
54801da177e4SLinus Torvalds 		break;
54811da177e4SLinus Torvalds 
54821da177e4SLinus Torvalds 	case 1:
54831da177e4SLinus Torvalds 		if (width) {
54841da177e4SLinus Torvalds 			/* Pattern:  5555 AAAA 5555 AAAA 5555
54851da177e4SLinus Torvalds 			 */
54861da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
54871da177e4SLinus Torvalds 				if (ii & 0x02)
54881da177e4SLinus Torvalds 					*ptr = 0xAA;
54891da177e4SLinus Torvalds 				else
54901da177e4SLinus Torvalds 					*ptr = 0x55;
54911da177e4SLinus Torvalds 			}
54921da177e4SLinus Torvalds 		} else {
54931da177e4SLinus Torvalds 			/* Pattern:  55 AA 55 AA 55
54941da177e4SLinus Torvalds 			 */
54951da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
54961da177e4SLinus Torvalds 				if (ii & 0x01)
54971da177e4SLinus Torvalds 					*ptr = 0xAA;
54981da177e4SLinus Torvalds 				else
54991da177e4SLinus Torvalds 					*ptr = 0x55;
55001da177e4SLinus Torvalds 			}
55011da177e4SLinus Torvalds 		}
55021da177e4SLinus Torvalds 		break;
55031da177e4SLinus Torvalds 
55041da177e4SLinus Torvalds 	case 2:
55051da177e4SLinus Torvalds 		/* Pattern:  00 01 02 03 04 05
55061da177e4SLinus Torvalds 		 * ... FE FF 00 01..
55071da177e4SLinus Torvalds 		 */
55081da177e4SLinus Torvalds 		for (ii=0; ii < size; ii++, ptr++)
55091da177e4SLinus Torvalds 			*ptr = (char) ii;
55101da177e4SLinus Torvalds 		break;
55111da177e4SLinus Torvalds 
55121da177e4SLinus Torvalds 	case 3:
55131da177e4SLinus Torvalds 		if (width) {
55141da177e4SLinus Torvalds 			/* Wide Pattern:  FFFE 0001 FFFD 0002
55151da177e4SLinus Torvalds 			 * ...  4000 DFFF 8000 EFFF
55161da177e4SLinus Torvalds 			 */
55171da177e4SLinus Torvalds 			byte = 0;
55181da177e4SLinus Torvalds 			for (ii=0; ii < size/2; ii++) {
55191da177e4SLinus Torvalds 				/* Create the base pattern
55201da177e4SLinus Torvalds 				 */
55211da177e4SLinus Torvalds 				val = (1 << byte);
55221da177e4SLinus Torvalds 				/* every 64 (0x40) bytes flip the pattern
55231da177e4SLinus Torvalds 				 * since we fill 2 bytes / iteration,
55241da177e4SLinus Torvalds 				 * test for ii = 0x20
55251da177e4SLinus Torvalds 				 */
55261da177e4SLinus Torvalds 				if (ii & 0x20)
55271da177e4SLinus Torvalds 					val = ~(val);
55281da177e4SLinus Torvalds 
55291da177e4SLinus Torvalds 				if (ii & 0x01) {
55301da177e4SLinus Torvalds 					*ptr = (char)( (val & 0xFF00) >> 8);
55311da177e4SLinus Torvalds 					ptr++;
55321da177e4SLinus Torvalds 					*ptr = (char)(val & 0xFF);
55331da177e4SLinus Torvalds 					byte++;
55341da177e4SLinus Torvalds 					byte &= 0x0F;
55351da177e4SLinus Torvalds 				} else {
55361da177e4SLinus Torvalds 					val = ~val;
55371da177e4SLinus Torvalds 					*ptr = (char)( (val & 0xFF00) >> 8);
55381da177e4SLinus Torvalds 					ptr++;
55391da177e4SLinus Torvalds 					*ptr = (char)(val & 0xFF);
55401da177e4SLinus Torvalds 				}
55411da177e4SLinus Torvalds 
55421da177e4SLinus Torvalds 				ptr++;
55431da177e4SLinus Torvalds 			}
55441da177e4SLinus Torvalds 		} else {
55451da177e4SLinus Torvalds 			/* Narrow Pattern:  FE 01 FD 02 FB 04
55461da177e4SLinus Torvalds 			 * .. 7F 80 01 FE 02 FD ...  80 7F
55471da177e4SLinus Torvalds 			 */
55481da177e4SLinus Torvalds 			byte = 0;
55491da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
55501da177e4SLinus Torvalds 				/* Base pattern - first 32 bytes
55511da177e4SLinus Torvalds 				 */
55521da177e4SLinus Torvalds 				if (ii & 0x01) {
55531da177e4SLinus Torvalds 					*ptr = (1 << byte);
55541da177e4SLinus Torvalds 					byte++;
55551da177e4SLinus Torvalds 					byte &= 0x07;
55561da177e4SLinus Torvalds 				} else {
55571da177e4SLinus Torvalds 					*ptr = (char) (~(1 << byte));
55581da177e4SLinus Torvalds 				}
55591da177e4SLinus Torvalds 
55601da177e4SLinus Torvalds 				/* Flip the pattern every 32 bytes
55611da177e4SLinus Torvalds 				 */
55621da177e4SLinus Torvalds 				if (ii & 0x20)
55631da177e4SLinus Torvalds 					*ptr = ~(*ptr);
55641da177e4SLinus Torvalds 			}
55651da177e4SLinus Torvalds 		}
55661da177e4SLinus Torvalds 		break;
55671da177e4SLinus Torvalds 	}
55681da177e4SLinus Torvalds }
55691da177e4SLinus Torvalds #endif /* ~MPTSCSIH_ENABLE_DOMAIN_VALIDATION */
55701da177e4SLinus Torvalds 
55710d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_remove);
55720d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_shutdown);
55730d0c7974SMoore, Eric Dean  #ifdef CONFIG_PM
55740d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_suspend);
55750d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_resume);
55760d0c7974SMoore, Eric Dean  #endif
55770d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_proc_info);
55780d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_info);
55790d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_qcmd);
55800d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_slave_alloc);
55810d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_slave_destroy);
55820d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_slave_configure);
55830d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_abort);
55840d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_dev_reset);
55850d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_bus_reset);
55860d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_host_reset);
55870d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_bios_param);
55880d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_io_done);
55890d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_taskmgmt_complete);
55900d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_scandv_complete);
55910d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_event_process);
55920d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_ioc_reset);
55930d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_store_queue_depth);
55940d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_timer_expired);
55951da177e4SLinus Torvalds 
55960d0c7974SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5597