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 *);
173d18c3db5SGreg Kroah-Hartman void 		mptscsih_shutdown(struct pci_dev *);
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 
991d18c3db5SGreg Kroah-Hartman 	mptscsih_shutdown(pdev);
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
1029d18c3db5SGreg Kroah-Hartman mptscsih_shutdown(struct pci_dev *pdev)
10301da177e4SLinus Torvalds {
1031d18c3db5SGreg Kroah-Hartman 	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
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 {
1057d18c3db5SGreg Kroah-Hartman 	mptscsih_shutdown(pdev);
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 
19031da177e4SLinus Torvalds 	/*  If we can't locate the host to reset, then we failed. */
19041da177e4SLinus Torvalds 	if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
19051da177e4SLinus Torvalds 		dtmprintk( ( KERN_WARNING MYNAM ": mptscsih_host_reset: "
19061da177e4SLinus Torvalds 			     "Can't locate host! (sc=%p)\n",
19071da177e4SLinus Torvalds 			     SCpnt ) );
19081da177e4SLinus Torvalds 		return FAILED;
19091da177e4SLinus Torvalds 	}
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	printk(KERN_WARNING MYNAM ": %s: >> Attempting host reset! (sc=%p)\n",
19121da177e4SLinus Torvalds 	       hd->ioc->name, SCpnt);
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	/*  If our attempts to reset the host failed, then return a failed
19151da177e4SLinus Torvalds 	 *  status.  The host will be taken off line by the SCSI mid-layer.
19161da177e4SLinus Torvalds 	 */
19171da177e4SLinus Torvalds 	if (mpt_HardResetHandler(hd->ioc, CAN_SLEEP) < 0){
19181da177e4SLinus Torvalds 		status = FAILED;
19191da177e4SLinus Torvalds 	} else {
19201da177e4SLinus Torvalds 		/*  Make sure TM pending is cleared and TM state is set to
19211da177e4SLinus Torvalds 		 *  NONE.
19221da177e4SLinus Torvalds 		 */
19231da177e4SLinus Torvalds 		hd->tmPending = 0;
19241da177e4SLinus Torvalds 		hd->tmState = TM_STATE_NONE;
19251da177e4SLinus Torvalds 	}
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 	dtmprintk( ( KERN_WARNING MYNAM ": mptscsih_host_reset: "
19281da177e4SLinus Torvalds 		     "Status = %s\n",
19291da177e4SLinus Torvalds 		     (status == SUCCESS) ? "SUCCESS" : "FAILED" ) );
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	return status;
19321da177e4SLinus Torvalds }
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
19351da177e4SLinus Torvalds /**
19361da177e4SLinus Torvalds  *	mptscsih_tm_pending_wait - wait for pending task management request to
19371da177e4SLinus Torvalds  *		complete.
19381da177e4SLinus Torvalds  *	@hd: Pointer to MPT host structure.
19391da177e4SLinus Torvalds  *
19401da177e4SLinus Torvalds  *	Returns {SUCCESS,FAILED}.
19411da177e4SLinus Torvalds  */
19421da177e4SLinus Torvalds static int
19431da177e4SLinus Torvalds mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd)
19441da177e4SLinus Torvalds {
19451da177e4SLinus Torvalds 	unsigned long  flags;
19461da177e4SLinus Torvalds 	int            loop_count = 4 * 10;  /* Wait 10 seconds */
19471da177e4SLinus Torvalds 	int            status = FAILED;
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	do {
19501da177e4SLinus Torvalds 		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
19511da177e4SLinus Torvalds 		if (hd->tmState == TM_STATE_NONE) {
19521da177e4SLinus Torvalds 			hd->tmState = TM_STATE_IN_PROGRESS;
19531da177e4SLinus Torvalds 			hd->tmPending = 1;
19541da177e4SLinus Torvalds 			status = SUCCESS;
19551da177e4SLinus Torvalds 			spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
19561da177e4SLinus Torvalds 			break;
19571da177e4SLinus Torvalds 		}
19581da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
19591da177e4SLinus Torvalds 		msleep(250);
19601da177e4SLinus Torvalds 	} while (--loop_count);
19611da177e4SLinus Torvalds 
19621da177e4SLinus Torvalds 	return status;
19631da177e4SLinus Torvalds }
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
19661da177e4SLinus Torvalds /**
19671da177e4SLinus Torvalds  *	mptscsih_tm_wait_for_completion - wait for completion of TM task
19681da177e4SLinus Torvalds  *	@hd: Pointer to MPT host structure.
19691da177e4SLinus Torvalds  *
19701da177e4SLinus Torvalds  *	Returns {SUCCESS,FAILED}.
19711da177e4SLinus Torvalds  */
19721da177e4SLinus Torvalds static int
19731da177e4SLinus Torvalds mptscsih_tm_wait_for_completion(MPT_SCSI_HOST * hd, ulong timeout )
19741da177e4SLinus Torvalds {
19751da177e4SLinus Torvalds 	unsigned long  flags;
19761da177e4SLinus Torvalds 	int            loop_count = 4 * timeout;
19771da177e4SLinus Torvalds 	int            status = FAILED;
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	do {
19801da177e4SLinus Torvalds 		spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
19811da177e4SLinus Torvalds 		if(hd->tmPending == 0) {
19821da177e4SLinus Torvalds 			status = SUCCESS;
19831da177e4SLinus Torvalds 			spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
19841da177e4SLinus Torvalds 			break;
19851da177e4SLinus Torvalds 		}
19861da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
19871da177e4SLinus Torvalds 		msleep_interruptible(250);
19881da177e4SLinus Torvalds 	} while (--loop_count);
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	return status;
19911da177e4SLinus Torvalds }
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
19941da177e4SLinus Torvalds /**
19951da177e4SLinus Torvalds  *	mptscsih_taskmgmt_complete - Registered with Fusion MPT base driver
19961da177e4SLinus Torvalds  *	@ioc: Pointer to MPT_ADAPTER structure
19971da177e4SLinus Torvalds  *	@mf: Pointer to SCSI task mgmt request frame
19981da177e4SLinus Torvalds  *	@mr: Pointer to SCSI task mgmt reply frame
19991da177e4SLinus Torvalds  *
20001da177e4SLinus Torvalds  *	This routine is called from mptbase.c::mpt_interrupt() at the completion
20011da177e4SLinus Torvalds  *	of any SCSI task management request.
20021da177e4SLinus Torvalds  *	This routine is registered with the MPT (base) driver at driver
20031da177e4SLinus Torvalds  *	load/init time via the mpt_register() API call.
20041da177e4SLinus Torvalds  *
20051da177e4SLinus Torvalds  *	Returns 1 indicating alloc'd request frame ptr should be freed.
20061da177e4SLinus Torvalds  */
20070d0c7974SMoore, Eric Dean  int
20081da177e4SLinus Torvalds mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
20091da177e4SLinus Torvalds {
20101da177e4SLinus Torvalds 	SCSITaskMgmtReply_t	*pScsiTmReply;
20111da177e4SLinus Torvalds 	SCSITaskMgmt_t		*pScsiTmReq;
20121da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd;
20131da177e4SLinus Torvalds 	unsigned long		 flags;
20141da177e4SLinus Torvalds 	u16			 iocstatus;
20151da177e4SLinus Torvalds 	u8			 tmType;
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	dtmprintk((MYIOC_s_WARN_FMT "TaskMgmt completed (mf=%p,mr=%p)\n",
20181da177e4SLinus Torvalds 			ioc->name, mf, mr));
20191da177e4SLinus Torvalds 	if (ioc->sh) {
20201da177e4SLinus Torvalds 		/* Depending on the thread, a timer is activated for
20211da177e4SLinus Torvalds 		 * the TM request.  Delete this timer on completion of TM.
20221da177e4SLinus Torvalds 		 * Decrement count of outstanding TM requests.
20231da177e4SLinus Torvalds 		 */
20241da177e4SLinus Torvalds 		hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
20251da177e4SLinus Torvalds 	} else {
20261da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "TaskMgmt Complete: NULL Scsi Host Ptr\n",
20271da177e4SLinus Torvalds 			ioc->name));
20281da177e4SLinus Torvalds 		return 1;
20291da177e4SLinus Torvalds 	}
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 	if (mr == NULL) {
20321da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "ERROR! TaskMgmt Reply: NULL Request %p\n",
20331da177e4SLinus Torvalds 			ioc->name, mf));
20341da177e4SLinus Torvalds 		return 1;
20351da177e4SLinus Torvalds 	} else {
20361da177e4SLinus Torvalds 		pScsiTmReply = (SCSITaskMgmtReply_t*)mr;
20371da177e4SLinus Torvalds 		pScsiTmReq = (SCSITaskMgmt_t*)mf;
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds 		/* Figure out if this was ABORT_TASK, TARGET_RESET, or BUS_RESET! */
20401da177e4SLinus Torvalds 		tmType = pScsiTmReq->TaskType;
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "  TaskType = %d, TerminationCount=%d\n",
20431da177e4SLinus Torvalds 				ioc->name, tmType, le32_to_cpu(pScsiTmReply->TerminationCount)));
20441da177e4SLinus Torvalds 		DBG_DUMP_TM_REPLY_FRAME((u32 *)pScsiTmReply);
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 		iocstatus = le16_to_cpu(pScsiTmReply->IOCStatus) & MPI_IOCSTATUS_MASK;
20471da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "  SCSI TaskMgmt (%d) IOCStatus=%04x IOCLogInfo=%08x\n",
20481da177e4SLinus Torvalds 			ioc->name, tmType, iocstatus, le32_to_cpu(pScsiTmReply->IOCLogInfo)));
20491da177e4SLinus Torvalds 		/* Error?  (anything non-zero?) */
20501da177e4SLinus Torvalds 		if (iocstatus) {
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 			/* clear flags and continue.
20531da177e4SLinus Torvalds 			 */
20541da177e4SLinus Torvalds 			if (tmType == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
20551da177e4SLinus Torvalds 				hd->abortSCpnt = NULL;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 			/* If an internal command is present
20581da177e4SLinus Torvalds 			 * or the TM failed - reload the FW.
20591da177e4SLinus Torvalds 			 * FC FW may respond FAILED to an ABORT
20601da177e4SLinus Torvalds 			 */
20611da177e4SLinus Torvalds 			if (tmType == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) {
20621da177e4SLinus Torvalds 				if ((hd->cmdPtr) ||
20631da177e4SLinus Torvalds 				    (iocstatus == MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED)) {
20641da177e4SLinus Torvalds 					if (mpt_HardResetHandler(ioc, NO_SLEEP) < 0) {
20651da177e4SLinus Torvalds 						printk((KERN_WARNING
20661da177e4SLinus Torvalds 							" Firmware Reload FAILED!!\n"));
20671da177e4SLinus Torvalds 					}
20681da177e4SLinus Torvalds 				}
20691da177e4SLinus Torvalds 			}
20701da177e4SLinus Torvalds 		} else {
20711da177e4SLinus Torvalds 			dtmprintk((MYIOC_s_WARN_FMT " TaskMgmt SUCCESS\n", ioc->name));
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 			hd->abortSCpnt = NULL;
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds 		}
20761da177e4SLinus Torvalds 	}
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	spin_lock_irqsave(&ioc->FreeQlock, flags);
20791da177e4SLinus Torvalds 	hd->tmPending = 0;
20801da177e4SLinus Torvalds 	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
20811da177e4SLinus Torvalds 	hd->tmState = TM_STATE_NONE;
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	return 1;
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
20871da177e4SLinus Torvalds /*
20881da177e4SLinus Torvalds  *	This is anyones guess quite frankly.
20891da177e4SLinus Torvalds  */
20900d0c7974SMoore, Eric Dean  int
20911da177e4SLinus Torvalds mptscsih_bios_param(struct scsi_device * sdev, struct block_device *bdev,
20921da177e4SLinus Torvalds 		sector_t capacity, int geom[])
20931da177e4SLinus Torvalds {
20941da177e4SLinus Torvalds 	int		heads;
20951da177e4SLinus Torvalds 	int		sectors;
20961da177e4SLinus Torvalds 	sector_t	cylinders;
20971da177e4SLinus Torvalds 	ulong 		dummy;
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	heads = 64;
21001da177e4SLinus Torvalds 	sectors = 32;
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	dummy = heads * sectors;
21031da177e4SLinus Torvalds 	cylinders = capacity;
21041da177e4SLinus Torvalds 	sector_div(cylinders,dummy);
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	/*
21071da177e4SLinus Torvalds 	 * Handle extended translation size for logical drives
21081da177e4SLinus Torvalds 	 * > 1Gb
21091da177e4SLinus Torvalds 	 */
21101da177e4SLinus Torvalds 	if ((ulong)capacity >= 0x200000) {
21111da177e4SLinus Torvalds 		heads = 255;
21121da177e4SLinus Torvalds 		sectors = 63;
21131da177e4SLinus Torvalds 		dummy = heads * sectors;
21141da177e4SLinus Torvalds 		cylinders = capacity;
21151da177e4SLinus Torvalds 		sector_div(cylinders,dummy);
21161da177e4SLinus Torvalds 	}
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	/* return result */
21191da177e4SLinus Torvalds 	geom[0] = heads;
21201da177e4SLinus Torvalds 	geom[1] = sectors;
21211da177e4SLinus Torvalds 	geom[2] = cylinders;
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	dprintk((KERN_NOTICE
21241da177e4SLinus Torvalds 		": bios_param: Id=%i Lun=%i Channel=%i CHS=%i/%i/%i\n",
21251da177e4SLinus Torvalds 		sdev->id, sdev->lun,sdev->channel,(int)cylinders,heads,sectors));
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 	return 0;
21281da177e4SLinus Torvalds }
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
21311da177e4SLinus Torvalds /*
21321da177e4SLinus Torvalds  *	OS entry point to allow host driver to alloc memory
21331da177e4SLinus Torvalds  *	for each scsi device. Called once per device the bus scan.
21341da177e4SLinus Torvalds  *	Return non-zero if allocation fails.
21351da177e4SLinus Torvalds  *	Init memory once per id (not LUN).
21361da177e4SLinus Torvalds  */
21370d0c7974SMoore, Eric Dean  int
21381da177e4SLinus Torvalds mptscsih_slave_alloc(struct scsi_device *device)
21391da177e4SLinus Torvalds {
21401da177e4SLinus Torvalds 	struct Scsi_Host	*host = device->host;
21411da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd = (MPT_SCSI_HOST *)host->hostdata;
21421da177e4SLinus Torvalds 	VirtDevice		*vdev;
21431da177e4SLinus Torvalds 	uint			target = device->id;
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 	if (hd == NULL)
21461da177e4SLinus Torvalds 		return -ENODEV;
21471da177e4SLinus Torvalds 
21481da177e4SLinus Torvalds 	if ((vdev = hd->Targets[target]) != NULL)
21491da177e4SLinus Torvalds 		goto out;
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds 	vdev = kmalloc(sizeof(VirtDevice), GFP_KERNEL);
21521da177e4SLinus Torvalds 	if (!vdev) {
21531da177e4SLinus Torvalds 		printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
21541da177e4SLinus Torvalds 				hd->ioc->name, sizeof(VirtDevice));
21551da177e4SLinus Torvalds 		return -ENOMEM;
21561da177e4SLinus Torvalds 	}
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	memset(vdev, 0, sizeof(VirtDevice));
21591da177e4SLinus Torvalds 	vdev->tflags = MPT_TARGET_FLAGS_Q_YES;
21601da177e4SLinus Torvalds 	vdev->ioc_id = hd->ioc->id;
21611da177e4SLinus Torvalds 	vdev->target_id = device->id;
21621da177e4SLinus Torvalds 	vdev->bus_id = device->channel;
21631da177e4SLinus Torvalds 	vdev->raidVolume = 0;
21641da177e4SLinus Torvalds 	hd->Targets[device->id] = vdev;
21651da177e4SLinus Torvalds 	if (hd->ioc->bus_type == SCSI) {
21661da177e4SLinus Torvalds 		if (hd->ioc->spi_data.isRaid & (1 << device->id)) {
21671da177e4SLinus Torvalds 			vdev->raidVolume = 1;
21681da177e4SLinus Torvalds 			ddvtprintk((KERN_INFO
21691da177e4SLinus Torvalds 			    "RAID Volume @ id %d\n", device->id));
21701da177e4SLinus Torvalds 		}
21711da177e4SLinus Torvalds 	} else {
21721da177e4SLinus Torvalds 		vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
21731da177e4SLinus Torvalds 	}
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds  out:
21761da177e4SLinus Torvalds 	vdev->num_luns++;
21771da177e4SLinus Torvalds 	return 0;
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
21800d0c7974SMoore, Eric Dean  static int
21810d0c7974SMoore, Eric Dean  mptscsih_is_raid_volume(MPT_SCSI_HOST *hd, uint id)
21821da177e4SLinus Torvalds {
21831da177e4SLinus Torvalds 	int i;
21841da177e4SLinus Torvalds 
21851da177e4SLinus Torvalds 	if (!hd->ioc->spi_data.isRaid || !hd->ioc->spi_data.pIocPg3)
21861da177e4SLinus Torvalds 		return 0;
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	for (i = 0; i < hd->ioc->spi_data.pIocPg3->NumPhysDisks; i++) {
21891da177e4SLinus Torvalds 		if (id == hd->ioc->spi_data.pIocPg3->PhysDisk[i].PhysDiskID)
21901da177e4SLinus Torvalds 			return 1;
21911da177e4SLinus Torvalds 	}
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	return 0;
21941da177e4SLinus Torvalds }
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds /*
21971da177e4SLinus Torvalds  *	OS entry point to allow for host driver to free allocated memory
21981da177e4SLinus Torvalds  *	Called if no device present or device being unloaded
21991da177e4SLinus Torvalds  */
22000d0c7974SMoore, Eric Dean  void
22011da177e4SLinus Torvalds mptscsih_slave_destroy(struct scsi_device *device)
22021da177e4SLinus Torvalds {
22031da177e4SLinus Torvalds 	struct Scsi_Host	*host = device->host;
22041da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd = (MPT_SCSI_HOST *)host->hostdata;
22051da177e4SLinus Torvalds 	VirtDevice		*vdev;
22061da177e4SLinus Torvalds 	uint			target = device->id;
22071da177e4SLinus Torvalds 	uint			lun = device->lun;
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	if (hd == NULL)
22101da177e4SLinus Torvalds 		return;
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	mptscsih_search_running_cmds(hd, target, lun);
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds 	vdev = hd->Targets[target];
22151da177e4SLinus Torvalds 	vdev->luns[0] &= ~(1 << lun);
22161da177e4SLinus Torvalds 	if (--vdev->num_luns)
22171da177e4SLinus Torvalds 		return;
22181da177e4SLinus Torvalds 
22191da177e4SLinus Torvalds 	kfree(hd->Targets[target]);
22201da177e4SLinus Torvalds 	hd->Targets[target] = NULL;
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	if (hd->ioc->bus_type == SCSI) {
22231da177e4SLinus Torvalds 		if (mptscsih_is_raid_volume(hd, target)) {
22241da177e4SLinus Torvalds 			hd->ioc->spi_data.forceDv |= MPT_SCSICFG_RELOAD_IOC_PG3;
22251da177e4SLinus Torvalds 		} else {
22261da177e4SLinus Torvalds 			hd->ioc->spi_data.dvStatus[target] =
22271da177e4SLinus Torvalds 				MPT_SCSICFG_NEGOTIATE;
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 			if (!hd->negoNvram) {
22301da177e4SLinus Torvalds 				hd->ioc->spi_data.dvStatus[target] |=
22311da177e4SLinus Torvalds 					MPT_SCSICFG_DV_NOT_DONE;
22321da177e4SLinus Torvalds 			}
22331da177e4SLinus Torvalds 		}
22341da177e4SLinus Torvalds 	}
22351da177e4SLinus Torvalds }
22361da177e4SLinus Torvalds 
22376e3815baSMoore, Eric Dean /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
22386e3815baSMoore, Eric Dean /*
22396e3815baSMoore, Eric Dean  *	mptscsih_change_queue_depth - This function will set a devices queue depth
22406e3815baSMoore, Eric Dean  *	@sdev: per scsi_device pointer
22416e3815baSMoore, Eric Dean  *	@qdepth: requested queue depth
22426e3815baSMoore, Eric Dean  *
22436e3815baSMoore, Eric Dean  *	Adding support for new 'change_queue_depth' api.
22446e3815baSMoore, Eric Dean */
22456e3815baSMoore, Eric Dean int
22466e3815baSMoore, Eric Dean mptscsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
22471da177e4SLinus Torvalds {
22486e3815baSMoore, Eric Dean 	MPT_SCSI_HOST	*hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
22496e3815baSMoore, Eric Dean 	VirtDevice *pTarget;
22501da177e4SLinus Torvalds 	int	max_depth;
22511da177e4SLinus Torvalds 	int	tagged;
22521da177e4SLinus Torvalds 
22536e3815baSMoore, Eric Dean 	if (hd == NULL)
22546e3815baSMoore, Eric Dean 		return 0;
22556e3815baSMoore, Eric Dean 	if (!(pTarget = hd->Targets[sdev->id]))
22566e3815baSMoore, Eric Dean 		return 0;
22576e3815baSMoore, Eric Dean 
22581da177e4SLinus Torvalds 	if (hd->ioc->bus_type == SCSI) {
22591da177e4SLinus Torvalds 		if (pTarget->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY) {
22601da177e4SLinus Torvalds 			if (!(pTarget->tflags & MPT_TARGET_FLAGS_Q_YES))
22611da177e4SLinus Torvalds 				max_depth = 1;
22621da177e4SLinus Torvalds 			else if (((pTarget->inq_data[0] & 0x1f) == 0x00) &&
22631da177e4SLinus Torvalds 			         (pTarget->minSyncFactor <= MPT_ULTRA160 ))
22641da177e4SLinus Torvalds 				max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
22651da177e4SLinus Torvalds 			else
22661da177e4SLinus Torvalds 				max_depth = MPT_SCSI_CMD_PER_DEV_LOW;
22671da177e4SLinus Torvalds 		} else {
22681da177e4SLinus Torvalds 			/* error case - No Inq. Data */
22691da177e4SLinus Torvalds 			max_depth = 1;
22701da177e4SLinus Torvalds 		}
22711da177e4SLinus Torvalds 	} else
22721da177e4SLinus Torvalds 		max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds 	if (qdepth > max_depth)
22751da177e4SLinus Torvalds 		qdepth = max_depth;
22761da177e4SLinus Torvalds 	if (qdepth == 1)
22771da177e4SLinus Torvalds 		tagged = 0;
22781da177e4SLinus Torvalds 	else
22791da177e4SLinus Torvalds 		tagged = MSG_SIMPLE_TAG;
22801da177e4SLinus Torvalds 
22816e3815baSMoore, Eric Dean 	scsi_adjust_queue_depth(sdev, tagged, qdepth);
22826e3815baSMoore, Eric Dean 	return sdev->queue_depth;
22831da177e4SLinus Torvalds }
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds /*
22861da177e4SLinus Torvalds  *	OS entry point to adjust the queue_depths on a per-device basis.
22871da177e4SLinus Torvalds  *	Called once per device the bus scan. Use it to force the queue_depth
22881da177e4SLinus Torvalds  *	member to 1 if a device does not support Q tags.
22891da177e4SLinus Torvalds  *	Return non-zero if fails.
22901da177e4SLinus Torvalds  */
22910d0c7974SMoore, Eric Dean  int
22921da177e4SLinus Torvalds mptscsih_slave_configure(struct scsi_device *device)
22931da177e4SLinus Torvalds {
22941da177e4SLinus Torvalds 	struct Scsi_Host	*sh = device->host;
22951da177e4SLinus Torvalds 	VirtDevice		*pTarget;
22961da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd = (MPT_SCSI_HOST *)sh->hostdata;
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	if ((hd == NULL) || (hd->Targets == NULL)) {
22991da177e4SLinus Torvalds 		return 0;
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
23031da177e4SLinus Torvalds 		"device @ %p, id=%d, LUN=%d, channel=%d\n",
23041da177e4SLinus Torvalds 		hd->ioc->name, device, device->id, device->lun, device->channel));
23051da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
23061da177e4SLinus Torvalds 		"sdtr %d wdtr %d ppr %d inq length=%d\n",
23071da177e4SLinus Torvalds 		hd->ioc->name, device->sdtr, device->wdtr,
23081da177e4SLinus Torvalds 		device->ppr, device->inquiry_len));
23091da177e4SLinus Torvalds 
23101da177e4SLinus Torvalds 	if (device->id > sh->max_id) {
23111da177e4SLinus Torvalds 		/* error case, should never happen */
23121da177e4SLinus Torvalds 		scsi_adjust_queue_depth(device, 0, 1);
23131da177e4SLinus Torvalds 		goto slave_configure_exit;
23141da177e4SLinus Torvalds 	}
23151da177e4SLinus Torvalds 
23161da177e4SLinus Torvalds 	pTarget = hd->Targets[device->id];
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds 	if (pTarget == NULL) {
23191da177e4SLinus Torvalds 		/* Driver doesn't know about this device.
23201da177e4SLinus Torvalds 		 * Kernel may generate a "Dummy Lun 0" which
23211da177e4SLinus Torvalds 		 * may become a real Lun if a
23221da177e4SLinus Torvalds 		 * "scsi add-single-device" command is executed
23231da177e4SLinus Torvalds 		 * while the driver is active (hot-plug a
23241da177e4SLinus Torvalds 		 * device).  LSI Raid controllers need
23251da177e4SLinus Torvalds 		 * queue_depth set to DEV_HIGH for this reason.
23261da177e4SLinus Torvalds 		 */
23271da177e4SLinus Torvalds 		scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
23281da177e4SLinus Torvalds 			MPT_SCSI_CMD_PER_DEV_HIGH);
23291da177e4SLinus Torvalds 		goto slave_configure_exit;
23301da177e4SLinus Torvalds 	}
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds 	mptscsih_initTarget(hd, device->channel, device->id, device->lun,
23331da177e4SLinus Torvalds 		device->inquiry, device->inquiry_len );
23346e3815baSMoore, Eric Dean 	mptscsih_change_queue_depth(device, MPT_SCSI_CMD_PER_DEV_HIGH);
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
23371da177e4SLinus Torvalds 		"Queue depth=%d, tflags=%x\n",
23381da177e4SLinus Torvalds 		hd->ioc->name, device->queue_depth, pTarget->tflags));
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
23411da177e4SLinus Torvalds 		"negoFlags=%x, maxOffset=%x, SyncFactor=%x\n",
23421da177e4SLinus Torvalds 		hd->ioc->name, pTarget->negoFlags, pTarget->maxOffset, pTarget->minSyncFactor));
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds slave_configure_exit:
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	dsprintk((MYIOC_s_INFO_FMT
23471da177e4SLinus Torvalds 		"tagged %d, simple %d, ordered %d\n",
23481da177e4SLinus Torvalds 		hd->ioc->name,device->tagged_supported, device->simple_tags,
23491da177e4SLinus Torvalds 		device->ordered_tags));
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 	return 0;
23521da177e4SLinus Torvalds }
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
23551da177e4SLinus Torvalds /*
23561da177e4SLinus Torvalds  *  Private routines...
23571da177e4SLinus Torvalds  */
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
23601da177e4SLinus Torvalds /* Utility function to copy sense data from the scsi_cmnd buffer
23611da177e4SLinus Torvalds  * to the FC and SCSI target structures.
23621da177e4SLinus Torvalds  *
23631da177e4SLinus Torvalds  */
23641da177e4SLinus Torvalds static void
23650d0c7974SMoore, Eric Dean  mptscsih_copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply)
23661da177e4SLinus Torvalds {
23671da177e4SLinus Torvalds 	VirtDevice	*target;
23681da177e4SLinus Torvalds 	SCSIIORequest_t	*pReq;
23691da177e4SLinus Torvalds 	u32		 sense_count = le32_to_cpu(pScsiReply->SenseCount);
23701da177e4SLinus Torvalds 	int		 index;
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	/* Get target structure
23731da177e4SLinus Torvalds 	 */
23741da177e4SLinus Torvalds 	pReq = (SCSIIORequest_t *) mf;
23751da177e4SLinus Torvalds 	index = (int) pReq->TargetID;
23761da177e4SLinus Torvalds 	target = hd->Targets[index];
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 	if (sense_count) {
23791da177e4SLinus Torvalds 		u8 *sense_data;
23801da177e4SLinus Torvalds 		int req_index;
23811da177e4SLinus Torvalds 
23821da177e4SLinus Torvalds 		/* Copy the sense received into the scsi command block. */
23831da177e4SLinus Torvalds 		req_index = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
23841da177e4SLinus Torvalds 		sense_data = ((u8 *)hd->ioc->sense_buf_pool + (req_index * MPT_SENSE_BUFFER_ALLOC));
23851da177e4SLinus Torvalds 		memcpy(sc->sense_buffer, sense_data, SNS_LEN(sc));
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds 		/* Log SMART data (asc = 0x5D, non-IM case only) if required.
23881da177e4SLinus Torvalds 		 */
23891da177e4SLinus Torvalds 		if ((hd->ioc->events) && (hd->ioc->eventTypes & (1 << MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE))) {
23901da177e4SLinus Torvalds 			if ((sense_data[12] == 0x5D) && (target->raidVolume == 0)) {
23911da177e4SLinus Torvalds 				int idx;
23921da177e4SLinus Torvalds 				MPT_ADAPTER *ioc = hd->ioc;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 				idx = ioc->eventContext % ioc->eventLogSize;
23951da177e4SLinus Torvalds 				ioc->events[idx].event = MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE;
23961da177e4SLinus Torvalds 				ioc->events[idx].eventContext = ioc->eventContext;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 				ioc->events[idx].data[0] = (pReq->LUN[1] << 24) ||
23991da177e4SLinus Torvalds 					(MPI_EVENT_SCSI_DEV_STAT_RC_SMART_DATA << 16) ||
24001da177e4SLinus Torvalds 					(pReq->Bus << 8) || pReq->TargetID;
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds 				ioc->events[idx].data[1] = (sense_data[13] << 8) || sense_data[12];
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds 				ioc->eventContext++;
24051da177e4SLinus Torvalds 			}
24061da177e4SLinus Torvalds 		}
24071da177e4SLinus Torvalds 	} else {
24081da177e4SLinus Torvalds 		dprintk((MYIOC_s_INFO_FMT "Hmmm... SenseData len=0! (?)\n",
24091da177e4SLinus Torvalds 				hd->ioc->name));
24101da177e4SLinus Torvalds 	}
24111da177e4SLinus Torvalds }
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds static u32
24141da177e4SLinus Torvalds SCPNT_TO_LOOKUP_IDX(struct scsi_cmnd *sc)
24151da177e4SLinus Torvalds {
24161da177e4SLinus Torvalds 	MPT_SCSI_HOST *hd;
24171da177e4SLinus Torvalds 	int i;
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds 	hd = (MPT_SCSI_HOST *) sc->device->host->hostdata;
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	for (i = 0; i < hd->ioc->req_depth; i++) {
24221da177e4SLinus Torvalds 		if (hd->ScsiLookup[i] == sc) {
24231da177e4SLinus Torvalds 			return i;
24241da177e4SLinus Torvalds 		}
24251da177e4SLinus Torvalds 	}
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds 	return -1;
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
24310d0c7974SMoore, Eric Dean  int
24321da177e4SLinus Torvalds mptscsih_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
24331da177e4SLinus Torvalds {
24341da177e4SLinus Torvalds 	MPT_SCSI_HOST	*hd;
24351da177e4SLinus Torvalds 	unsigned long	 flags;
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 	dtmprintk((KERN_WARNING MYNAM
24381da177e4SLinus Torvalds 			": IOC %s_reset routed to SCSI host driver!\n",
24391da177e4SLinus Torvalds 			reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
24401da177e4SLinus Torvalds 			reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 	/* If a FW reload request arrives after base installed but
24431da177e4SLinus Torvalds 	 * before all scsi hosts have been attached, then an alt_ioc
24441da177e4SLinus Torvalds 	 * may have a NULL sh pointer.
24451da177e4SLinus Torvalds 	 */
24461da177e4SLinus Torvalds 	if ((ioc->sh == NULL) || (ioc->sh->hostdata == NULL))
24471da177e4SLinus Torvalds 		return 0;
24481da177e4SLinus Torvalds 	else
24491da177e4SLinus Torvalds 		hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	if (reset_phase == MPT_IOC_SETUP_RESET) {
24521da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Setup-Diag Reset\n", ioc->name));
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 		/* Clean Up:
24551da177e4SLinus Torvalds 		 * 1. Set Hard Reset Pending Flag
24561da177e4SLinus Torvalds 		 * All new commands go to doneQ
24571da177e4SLinus Torvalds 		 */
24581da177e4SLinus Torvalds 		hd->resetPending = 1;
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	} else if (reset_phase == MPT_IOC_PRE_RESET) {
24611da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Pre-Diag Reset\n", ioc->name));
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 		/* 2. Flush running commands
24641da177e4SLinus Torvalds 		 *	Clean ScsiLookup (and associated memory)
24651da177e4SLinus Torvalds 		 *	AND clean mytaskQ
24661da177e4SLinus Torvalds 		 */
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds 		/* 2b. Reply to OS all known outstanding I/O commands.
24691da177e4SLinus Torvalds 		 */
24701da177e4SLinus Torvalds 		mptscsih_flush_running_cmds(hd);
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 		/* 2c. If there was an internal command that
24731da177e4SLinus Torvalds 		 * has not completed, configuration or io request,
24741da177e4SLinus Torvalds 		 * free these resources.
24751da177e4SLinus Torvalds 		 */
24761da177e4SLinus Torvalds 		if (hd->cmdPtr) {
24771da177e4SLinus Torvalds 			del_timer(&hd->timer);
24781da177e4SLinus Torvalds 			mpt_free_msg_frame(ioc, hd->cmdPtr);
24791da177e4SLinus Torvalds 		}
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Pre-Reset complete.\n", ioc->name));
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 	} else {
24841da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Post-Diag Reset\n", ioc->name));
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds 		/* Once a FW reload begins, all new OS commands are
24871da177e4SLinus Torvalds 		 * redirected to the doneQ w/ a reset status.
24881da177e4SLinus Torvalds 		 * Init all control structures.
24891da177e4SLinus Torvalds 		 */
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds 		/* ScsiLookup initialization
24921da177e4SLinus Torvalds 		 */
24931da177e4SLinus Torvalds 		{
24941da177e4SLinus Torvalds 			int ii;
24951da177e4SLinus Torvalds 			for (ii=0; ii < hd->ioc->req_depth; ii++)
24961da177e4SLinus Torvalds 				hd->ScsiLookup[ii] = NULL;
24971da177e4SLinus Torvalds 		}
24981da177e4SLinus Torvalds 
24991da177e4SLinus Torvalds 		/* 2. Chain Buffer initialization
25001da177e4SLinus Torvalds 		 */
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 		/* 4. Renegotiate to all devices, if SCSI
25031da177e4SLinus Torvalds 		 */
25041da177e4SLinus Torvalds 		if (ioc->bus_type == SCSI) {
25051da177e4SLinus Torvalds 			dnegoprintk(("writeSDP1: ALL_IDS USE_NVRAM\n"));
25061da177e4SLinus Torvalds 			mptscsih_writeSDP1(hd, 0, 0, MPT_SCSICFG_ALL_IDS | MPT_SCSICFG_USE_NVRAM);
25071da177e4SLinus Torvalds 		}
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds 		/* 5. Enable new commands to be posted
25101da177e4SLinus Torvalds 		 */
25111da177e4SLinus Torvalds 		spin_lock_irqsave(&ioc->FreeQlock, flags);
25121da177e4SLinus Torvalds 		hd->tmPending = 0;
25131da177e4SLinus Torvalds 		spin_unlock_irqrestore(&ioc->FreeQlock, flags);
25141da177e4SLinus Torvalds 		hd->resetPending = 0;
25151da177e4SLinus Torvalds 		hd->tmState = TM_STATE_NONE;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 		/* 6. If there was an internal command,
25181da177e4SLinus Torvalds 		 * wake this process up.
25191da177e4SLinus Torvalds 		 */
25201da177e4SLinus Torvalds 		if (hd->cmdPtr) {
25211da177e4SLinus Torvalds 			/*
25221da177e4SLinus Torvalds 			 * Wake up the original calling thread
25231da177e4SLinus Torvalds 			 */
25241da177e4SLinus Torvalds 			hd->pLocal = &hd->localReply;
25251da177e4SLinus Torvalds 			hd->pLocal->completion = MPT_SCANDV_DID_RESET;
25260d0c7974SMoore, Eric Dean  			hd->scandv_wait_done = 1;
25270d0c7974SMoore, Eric Dean  			wake_up(&hd->scandv_waitq);
25281da177e4SLinus Torvalds 			hd->cmdPtr = NULL;
25291da177e4SLinus Torvalds 		}
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds 		/* 7. Set flag to force DV and re-read IOC Page 3
25321da177e4SLinus Torvalds 		 */
25331da177e4SLinus Torvalds 		if (ioc->bus_type == SCSI) {
25341da177e4SLinus Torvalds 			ioc->spi_data.forceDv = MPT_SCSICFG_NEED_DV | MPT_SCSICFG_RELOAD_IOC_PG3;
25351da177e4SLinus Torvalds 			ddvtprintk(("Set reload IOC Pg3 Flag\n"));
25361da177e4SLinus Torvalds 		}
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 		dtmprintk((MYIOC_s_WARN_FMT "Post-Reset complete.\n", ioc->name));
25391da177e4SLinus Torvalds 
25401da177e4SLinus Torvalds 	}
25411da177e4SLinus Torvalds 
25421da177e4SLinus Torvalds 	return 1;		/* currently means nothing really */
25431da177e4SLinus Torvalds }
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
25460d0c7974SMoore, Eric Dean  int
25471da177e4SLinus Torvalds mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
25481da177e4SLinus Torvalds {
25491da177e4SLinus Torvalds 	MPT_SCSI_HOST *hd;
25501da177e4SLinus Torvalds 	u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	devtprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
25531da177e4SLinus Torvalds 			ioc->name, event));
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 	switch (event) {
25561da177e4SLinus Torvalds 	case MPI_EVENT_UNIT_ATTENTION:			/* 03 */
25571da177e4SLinus Torvalds 		/* FIXME! */
25581da177e4SLinus Torvalds 		break;
25591da177e4SLinus Torvalds 	case MPI_EVENT_IOC_BUS_RESET:			/* 04 */
25601da177e4SLinus Torvalds 	case MPI_EVENT_EXT_BUS_RESET:			/* 05 */
25611da177e4SLinus Torvalds 		hd = NULL;
25621da177e4SLinus Torvalds 		if (ioc->sh) {
25631da177e4SLinus Torvalds 			hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
25641da177e4SLinus Torvalds 			if (hd && (ioc->bus_type == SCSI) && (hd->soft_resets < -1))
25651da177e4SLinus Torvalds 				hd->soft_resets++;
25661da177e4SLinus Torvalds 		}
25671da177e4SLinus Torvalds 		break;
25681da177e4SLinus Torvalds 	case MPI_EVENT_LOGOUT:				/* 09 */
25691da177e4SLinus Torvalds 		/* FIXME! */
25701da177e4SLinus Torvalds 		break;
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 		/*
25731da177e4SLinus Torvalds 		 *  CHECKME! Don't think we need to do
25741da177e4SLinus Torvalds 		 *  anything for these, but...
25751da177e4SLinus Torvalds 		 */
25761da177e4SLinus Torvalds 	case MPI_EVENT_RESCAN:				/* 06 */
25771da177e4SLinus Torvalds 	case MPI_EVENT_LINK_STATUS_CHANGE:		/* 07 */
25781da177e4SLinus Torvalds 	case MPI_EVENT_LOOP_STATE_CHANGE:		/* 08 */
25791da177e4SLinus Torvalds 		/*
25801da177e4SLinus Torvalds 		 *  CHECKME!  Falling thru...
25811da177e4SLinus Torvalds 		 */
25821da177e4SLinus Torvalds 		break;
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 	case MPI_EVENT_INTEGRATED_RAID:			/* 0B */
25851da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
25861da177e4SLinus Torvalds 		/* negoNvram set to 0 if DV enabled and to USE_NVRAM if
25871da177e4SLinus Torvalds 		 * if DV disabled. Need to check for target mode.
25881da177e4SLinus Torvalds 		 */
25891da177e4SLinus Torvalds 		hd = NULL;
25901da177e4SLinus Torvalds 		if (ioc->sh)
25911da177e4SLinus Torvalds 			hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds 		if (hd && (ioc->bus_type == SCSI) && (hd->negoNvram == 0)) {
25941da177e4SLinus Torvalds 			ScsiCfgData	*pSpi;
25951da177e4SLinus Torvalds 			Ioc3PhysDisk_t	*pPDisk;
25961da177e4SLinus Torvalds 			int		 numPDisk;
25971da177e4SLinus Torvalds 			u8		 reason;
25981da177e4SLinus Torvalds 			u8		 physDiskNum;
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds 			reason = (le32_to_cpu(pEvReply->Data[0]) & 0x00FF0000) >> 16;
26011da177e4SLinus Torvalds 			if (reason == MPI_EVENT_RAID_RC_DOMAIN_VAL_NEEDED) {
26021da177e4SLinus Torvalds 				/* New or replaced disk.
26031da177e4SLinus Torvalds 				 * Set DV flag and schedule DV.
26041da177e4SLinus Torvalds 				 */
26051da177e4SLinus Torvalds 				pSpi = &ioc->spi_data;
26061da177e4SLinus Torvalds 				physDiskNum = (le32_to_cpu(pEvReply->Data[0]) & 0xFF000000) >> 24;
26071da177e4SLinus Torvalds 				ddvtprintk(("DV requested for phys disk id %d\n", physDiskNum));
26081da177e4SLinus Torvalds 				if (pSpi->pIocPg3) {
26091da177e4SLinus Torvalds 					pPDisk =  pSpi->pIocPg3->PhysDisk;
26101da177e4SLinus Torvalds 					numPDisk =pSpi->pIocPg3->NumPhysDisks;
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds 					while (numPDisk) {
26131da177e4SLinus Torvalds 						if (physDiskNum == pPDisk->PhysDiskNum) {
26141da177e4SLinus Torvalds 							pSpi->dvStatus[pPDisk->PhysDiskID] = (MPT_SCSICFG_NEED_DV | MPT_SCSICFG_DV_NOT_DONE);
26151da177e4SLinus Torvalds 							pSpi->forceDv = MPT_SCSICFG_NEED_DV;
26161da177e4SLinus Torvalds 							ddvtprintk(("NEED_DV set for phys disk id %d\n", pPDisk->PhysDiskID));
26171da177e4SLinus Torvalds 							break;
26181da177e4SLinus Torvalds 						}
26191da177e4SLinus Torvalds 						pPDisk++;
26201da177e4SLinus Torvalds 						numPDisk--;
26211da177e4SLinus Torvalds 					}
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds 					if (numPDisk == 0) {
26241da177e4SLinus Torvalds 						/* The physical disk that needs DV was not found
26251da177e4SLinus Torvalds 						 * in the stored IOC Page 3. The driver must reload
26261da177e4SLinus Torvalds 						 * this page. DV routine will set the NEED_DV flag for
26271da177e4SLinus Torvalds 						 * all phys disks that have DV_NOT_DONE set.
26281da177e4SLinus Torvalds 						 */
26291da177e4SLinus Torvalds 						pSpi->forceDv = MPT_SCSICFG_NEED_DV | MPT_SCSICFG_RELOAD_IOC_PG3;
26301da177e4SLinus Torvalds 						ddvtprintk(("phys disk %d not found. Setting reload IOC Pg3 Flag\n", physDiskNum));
26311da177e4SLinus Torvalds 					}
26321da177e4SLinus Torvalds 				}
26331da177e4SLinus Torvalds 			}
26341da177e4SLinus Torvalds 		}
26351da177e4SLinus Torvalds #endif
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds #if defined(MPT_DEBUG_DV) || defined(MPT_DEBUG_DV_TINY)
26381da177e4SLinus Torvalds 		printk("Raid Event RF: ");
26391da177e4SLinus Torvalds 		{
26401da177e4SLinus Torvalds 			u32 *m = (u32 *)pEvReply;
26411da177e4SLinus Torvalds 			int ii;
26421da177e4SLinus Torvalds 			int n = (int)pEvReply->MsgLength;
26431da177e4SLinus Torvalds 			for (ii=6; ii < n; ii++)
26441da177e4SLinus Torvalds 				printk(" %08x", le32_to_cpu(m[ii]));
26451da177e4SLinus Torvalds 			printk("\n");
26461da177e4SLinus Torvalds 		}
26471da177e4SLinus Torvalds #endif
26481da177e4SLinus Torvalds 		break;
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	case MPI_EVENT_NONE:				/* 00 */
26511da177e4SLinus Torvalds 	case MPI_EVENT_LOG_DATA:			/* 01 */
26521da177e4SLinus Torvalds 	case MPI_EVENT_STATE_CHANGE:			/* 02 */
26531da177e4SLinus Torvalds 	case MPI_EVENT_EVENT_CHANGE:			/* 0A */
26541da177e4SLinus Torvalds 	default:
26551da177e4SLinus Torvalds 		dprintk((KERN_INFO "  Ignoring event (=%02Xh)\n", event));
26561da177e4SLinus Torvalds 		break;
26571da177e4SLinus Torvalds 	}
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 	return 1;		/* currently means nothing really */
26601da177e4SLinus Torvalds }
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
26631da177e4SLinus Torvalds /*
26641da177e4SLinus Torvalds  *	mptscsih_initTarget - Target, LUN alloc/free functionality.
26651da177e4SLinus Torvalds  *	@hd: Pointer to MPT_SCSI_HOST structure
26661da177e4SLinus Torvalds  *	@bus_id: Bus number (?)
26671da177e4SLinus Torvalds  *	@target_id: SCSI target id
26681da177e4SLinus Torvalds  *	@lun: SCSI LUN id
26691da177e4SLinus Torvalds  *	@data: Pointer to data
26701da177e4SLinus Torvalds  *	@dlen: Number of INQUIRY bytes
26711da177e4SLinus Torvalds  *
26721da177e4SLinus Torvalds  *	NOTE: It's only SAFE to call this routine if data points to
26731da177e4SLinus Torvalds  *	sane & valid STANDARD INQUIRY data!
26741da177e4SLinus Torvalds  *
26751da177e4SLinus Torvalds  *	Allocate and initialize memory for this target.
26761da177e4SLinus Torvalds  *	Save inquiry data.
26771da177e4SLinus Torvalds  *
26781da177e4SLinus Torvalds  */
26791da177e4SLinus Torvalds static void
26801da177e4SLinus Torvalds mptscsih_initTarget(MPT_SCSI_HOST *hd, int bus_id, int target_id, u8 lun, char *data, int dlen)
26811da177e4SLinus Torvalds {
26821da177e4SLinus Torvalds 	int		indexed_lun, lun_index;
26831da177e4SLinus Torvalds 	VirtDevice	*vdev;
26841da177e4SLinus Torvalds 	ScsiCfgData	*pSpi;
26851da177e4SLinus Torvalds 	char		data_56;
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds 	dinitprintk((MYIOC_s_INFO_FMT "initTarget bus=%d id=%d lun=%d hd=%p\n",
26881da177e4SLinus Torvalds 			hd->ioc->name, bus_id, target_id, lun, hd));
26891da177e4SLinus Torvalds 
26901da177e4SLinus Torvalds 	/*
26911da177e4SLinus Torvalds 	 * If the peripheral qualifier filter is enabled then if the target reports a 0x1
26921da177e4SLinus Torvalds 	 * (i.e. The targer is capable of supporting the specified peripheral device type
26931da177e4SLinus Torvalds 	 * on this logical unit; however, the physical device is not currently connected
26941da177e4SLinus Torvalds 	 * to this logical unit) it will be converted to a 0x3 (i.e. The target is not
26951da177e4SLinus Torvalds 	 * capable of supporting a physical device on this logical unit). This is to work
26961da177e4SLinus Torvalds 	 * around a bug in th emid-layer in some distributions in which the mid-layer will
26971da177e4SLinus Torvalds 	 * continue to try to communicate to the LUN and evntually create a dummy LUN.
26981da177e4SLinus Torvalds 	*/
26990d0c7974SMoore, Eric Dean  	if (hd->mpt_pq_filter && dlen && (data[0] & 0xE0))
27001da177e4SLinus Torvalds 		data[0] |= 0x40;
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	/* Is LUN supported? If so, upper 2 bits will be 0
27031da177e4SLinus Torvalds 	* in first byte of inquiry data.
27041da177e4SLinus Torvalds 	*/
27051da177e4SLinus Torvalds 	if (data[0] & 0xe0)
27061da177e4SLinus Torvalds 		return;
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds 	if ((vdev = hd->Targets[target_id]) == NULL) {
27091da177e4SLinus Torvalds 		return;
27101da177e4SLinus Torvalds 	}
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds 	lun_index = (lun >> 5);  /* 32 luns per lun_index */
27131da177e4SLinus Torvalds 	indexed_lun = (lun % 32);
27141da177e4SLinus Torvalds 	vdev->luns[lun_index] |= (1 << indexed_lun);
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds 	if (hd->ioc->bus_type == SCSI) {
27171da177e4SLinus Torvalds 		if ((data[0] == TYPE_PROCESSOR) && (hd->ioc->spi_data.Saf_Te)) {
27181da177e4SLinus Torvalds 			/* Treat all Processors as SAF-TE if
27191da177e4SLinus Torvalds 			 * command line option is set */
27201da177e4SLinus Torvalds 			vdev->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
27211da177e4SLinus Torvalds 			mptscsih_writeIOCPage4(hd, target_id, bus_id);
27221da177e4SLinus Torvalds 		}else if ((data[0] == TYPE_PROCESSOR) &&
27231da177e4SLinus Torvalds 			!(vdev->tflags & MPT_TARGET_FLAGS_SAF_TE_ISSUED )) {
27241da177e4SLinus Torvalds 			if ( dlen > 49 ) {
27251da177e4SLinus Torvalds 				vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
27261da177e4SLinus Torvalds 				if ( data[44] == 'S' &&
27271da177e4SLinus Torvalds 				     data[45] == 'A' &&
27281da177e4SLinus Torvalds 				     data[46] == 'F' &&
27291da177e4SLinus Torvalds 				     data[47] == '-' &&
27301da177e4SLinus Torvalds 				     data[48] == 'T' &&
27311da177e4SLinus Torvalds 				     data[49] == 'E' ) {
27321da177e4SLinus Torvalds 					vdev->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
27331da177e4SLinus Torvalds 					mptscsih_writeIOCPage4(hd, target_id, bus_id);
27341da177e4SLinus Torvalds 				}
27351da177e4SLinus Torvalds 			}
27361da177e4SLinus Torvalds 		}
27371da177e4SLinus Torvalds 		if (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY)) {
27381da177e4SLinus Torvalds 			if ( dlen > 8 ) {
27391da177e4SLinus Torvalds 				memcpy (vdev->inq_data, data, 8);
27401da177e4SLinus Torvalds 			} else {
27411da177e4SLinus Torvalds 				memcpy (vdev->inq_data, data, dlen);
27421da177e4SLinus Torvalds 			}
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds 			/* If have not done DV, set the DV flag.
27451da177e4SLinus Torvalds 			 */
27461da177e4SLinus Torvalds 			pSpi = &hd->ioc->spi_data;
27471da177e4SLinus Torvalds 			if ((data[0] == TYPE_TAPE) || (data[0] == TYPE_PROCESSOR)) {
27481da177e4SLinus Torvalds 				if (pSpi->dvStatus[target_id] & MPT_SCSICFG_DV_NOT_DONE)
27491da177e4SLinus Torvalds 					pSpi->dvStatus[target_id] |= MPT_SCSICFG_NEED_DV;
27501da177e4SLinus Torvalds 			}
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds 			vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds 			data_56 = 0x0F;  /* Default to full capabilities if Inq data length is < 57 */
27561da177e4SLinus Torvalds 			if (dlen > 56) {
27571da177e4SLinus Torvalds 				if ( (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_56))) {
27581da177e4SLinus Torvalds 				/* Update the target capabilities
27591da177e4SLinus Torvalds 				 */
27601da177e4SLinus Torvalds 					data_56 = data[56];
27611da177e4SLinus Torvalds 					vdev->tflags |= MPT_TARGET_FLAGS_VALID_56;
27621da177e4SLinus Torvalds 				}
27631da177e4SLinus Torvalds 			}
27641da177e4SLinus Torvalds 			mptscsih_setTargetNegoParms(hd, vdev, data_56);
27651da177e4SLinus Torvalds 		} else {
27661da177e4SLinus Torvalds 			/* Initial Inquiry may not request enough data bytes to
27671da177e4SLinus Torvalds 			 * obtain byte 57.  DV will; if target doesn't return
27681da177e4SLinus Torvalds 			 * at least 57 bytes, data[56] will be zero. */
27691da177e4SLinus Torvalds 			if (dlen > 56) {
27701da177e4SLinus Torvalds 				if ( (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_56))) {
27711da177e4SLinus Torvalds 				/* Update the target capabilities
27721da177e4SLinus Torvalds 				 */
27731da177e4SLinus Torvalds 					data_56 = data[56];
27741da177e4SLinus Torvalds 					vdev->tflags |= MPT_TARGET_FLAGS_VALID_56;
27751da177e4SLinus Torvalds 					mptscsih_setTargetNegoParms(hd, vdev, data_56);
27761da177e4SLinus Torvalds 				}
27771da177e4SLinus Torvalds 			}
27781da177e4SLinus Torvalds 		}
27791da177e4SLinus Torvalds 	}
27801da177e4SLinus Torvalds }
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
27831da177e4SLinus Torvalds /*
27841da177e4SLinus Torvalds  *  Update the target negotiation parameters based on the
27851da177e4SLinus Torvalds  *  the Inquiry data, adapter capabilities, and NVRAM settings.
27861da177e4SLinus Torvalds  *
27871da177e4SLinus Torvalds  */
27881da177e4SLinus Torvalds static void
27891da177e4SLinus Torvalds mptscsih_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtDevice *target, char byte56)
27901da177e4SLinus Torvalds {
27911da177e4SLinus Torvalds 	ScsiCfgData *pspi_data = &hd->ioc->spi_data;
27921da177e4SLinus Torvalds 	int  id = (int) target->target_id;
27931da177e4SLinus Torvalds 	int  nvram;
27941da177e4SLinus Torvalds 	VirtDevice	*vdev;
27951da177e4SLinus Torvalds 	int ii;
27961da177e4SLinus Torvalds 	u8 width = MPT_NARROW;
27971da177e4SLinus Torvalds 	u8 factor = MPT_ASYNC;
27981da177e4SLinus Torvalds 	u8 offset = 0;
27991da177e4SLinus Torvalds 	u8 version, nfactor;
28001da177e4SLinus Torvalds 	u8 noQas = 1;
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	target->negoFlags = pspi_data->noQas;
28031da177e4SLinus Torvalds 
28041da177e4SLinus Torvalds 	/* noQas == 0 => device supports QAS. Need byte 56 of Inq to determine
28051da177e4SLinus Torvalds 	 * support. If available, default QAS to off and allow enabling.
28061da177e4SLinus Torvalds 	 * If not available, default QAS to on, turn off for non-disks.
28071da177e4SLinus Torvalds 	 */
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds 	/* Set flags based on Inquiry data
28101da177e4SLinus Torvalds 	 */
28111da177e4SLinus Torvalds 	version = target->inq_data[2] & 0x07;
28121da177e4SLinus Torvalds 	if (version < 2) {
28131da177e4SLinus Torvalds 		width = 0;
28141da177e4SLinus Torvalds 		factor = MPT_ULTRA2;
28151da177e4SLinus Torvalds 		offset = pspi_data->maxSyncOffset;
28161da177e4SLinus Torvalds 		target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
28171da177e4SLinus Torvalds 	} else {
28181da177e4SLinus Torvalds 		if (target->inq_data[7] & 0x20) {
28191da177e4SLinus Torvalds 			width = 1;
28201da177e4SLinus Torvalds 		}
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds 		if (target->inq_data[7] & 0x10) {
28231da177e4SLinus Torvalds 			factor = pspi_data->minSyncFactor;
28241da177e4SLinus Torvalds 			if (target->tflags & MPT_TARGET_FLAGS_VALID_56) {
28251da177e4SLinus Torvalds 				/* bits 2 & 3 show Clocking support */
28261da177e4SLinus Torvalds 				if ((byte56 & 0x0C) == 0)
28271da177e4SLinus Torvalds 					factor = MPT_ULTRA2;
28281da177e4SLinus Torvalds 				else {
28291da177e4SLinus Torvalds 					if ((byte56 & 0x03) == 0)
28301da177e4SLinus Torvalds 						factor = MPT_ULTRA160;
28311da177e4SLinus Torvalds 					else {
28321da177e4SLinus Torvalds 						factor = MPT_ULTRA320;
28331da177e4SLinus Torvalds 						if (byte56 & 0x02)
28341da177e4SLinus Torvalds 						{
28351da177e4SLinus Torvalds 							ddvtprintk((KERN_INFO "Enabling QAS due to byte56=%02x on id=%d!\n", byte56, id));
28361da177e4SLinus Torvalds 							noQas = 0;
28371da177e4SLinus Torvalds 						}
28381da177e4SLinus Torvalds 						if (target->inq_data[0] == TYPE_TAPE) {
28391da177e4SLinus Torvalds 							if (byte56 & 0x01)
28401da177e4SLinus Torvalds 								target->negoFlags |= MPT_TAPE_NEGO_IDP;
28411da177e4SLinus Torvalds 						}
28421da177e4SLinus Torvalds 					}
28431da177e4SLinus Torvalds 				}
28441da177e4SLinus Torvalds 			} else {
28451da177e4SLinus Torvalds 				ddvtprintk((KERN_INFO "Enabling QAS on id=%d due to ~TARGET_FLAGS_VALID_56!\n", id));
28461da177e4SLinus Torvalds 				noQas = 0;
28471da177e4SLinus Torvalds 			}
28481da177e4SLinus Torvalds 
28491da177e4SLinus Torvalds 			offset = pspi_data->maxSyncOffset;
28501da177e4SLinus Torvalds 
28511da177e4SLinus Torvalds 			/* If RAID, never disable QAS
28521da177e4SLinus Torvalds 			 * else if non RAID, do not disable
28531da177e4SLinus Torvalds 			 *   QAS if bit 1 is set
28541da177e4SLinus Torvalds 			 * bit 1 QAS support, non-raid only
28551da177e4SLinus Torvalds 			 * bit 0 IU support
28561da177e4SLinus Torvalds 			 */
28571da177e4SLinus Torvalds 			if (target->raidVolume == 1) {
28581da177e4SLinus Torvalds 				noQas = 0;
28591da177e4SLinus Torvalds 			}
28601da177e4SLinus Torvalds 		} else {
28611da177e4SLinus Torvalds 			factor = MPT_ASYNC;
28621da177e4SLinus Torvalds 			offset = 0;
28631da177e4SLinus Torvalds 		}
28641da177e4SLinus Torvalds 	}
28651da177e4SLinus Torvalds 
28661da177e4SLinus Torvalds 	if ( (target->inq_data[7] & 0x02) == 0) {
28671da177e4SLinus Torvalds 		target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
28681da177e4SLinus Torvalds 	}
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 	/* Update tflags based on NVRAM settings. (SCSI only)
28711da177e4SLinus Torvalds 	 */
28721da177e4SLinus Torvalds 	if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
28731da177e4SLinus Torvalds 		nvram = pspi_data->nvram[id];
28741da177e4SLinus Torvalds 		nfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds 		if (width)
28771da177e4SLinus Torvalds 			width = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
28781da177e4SLinus Torvalds 
28791da177e4SLinus Torvalds 		if (offset > 0) {
28801da177e4SLinus Torvalds 			/* Ensure factor is set to the
28811da177e4SLinus Torvalds 			 * maximum of: adapter, nvram, inquiry
28821da177e4SLinus Torvalds 			 */
28831da177e4SLinus Torvalds 			if (nfactor) {
28841da177e4SLinus Torvalds 				if (nfactor < pspi_data->minSyncFactor )
28851da177e4SLinus Torvalds 					nfactor = pspi_data->minSyncFactor;
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds 				factor = max(factor, nfactor);
28881da177e4SLinus Torvalds 				if (factor == MPT_ASYNC)
28891da177e4SLinus Torvalds 					offset = 0;
28901da177e4SLinus Torvalds 			} else {
28911da177e4SLinus Torvalds 				offset = 0;
28921da177e4SLinus Torvalds 				factor = MPT_ASYNC;
28931da177e4SLinus Torvalds 		}
28941da177e4SLinus Torvalds 		} else {
28951da177e4SLinus Torvalds 			factor = MPT_ASYNC;
28961da177e4SLinus Torvalds 		}
28971da177e4SLinus Torvalds 	}
28981da177e4SLinus Torvalds 
28991da177e4SLinus Torvalds 	/* Make sure data is consistent
29001da177e4SLinus Torvalds 	 */
29011da177e4SLinus Torvalds 	if ((!width) && (factor < MPT_ULTRA2)) {
29021da177e4SLinus Torvalds 		factor = MPT_ULTRA2;
29031da177e4SLinus Torvalds 	}
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds 	/* Save the data to the target structure.
29061da177e4SLinus Torvalds 	 */
29071da177e4SLinus Torvalds 	target->minSyncFactor = factor;
29081da177e4SLinus Torvalds 	target->maxOffset = offset;
29091da177e4SLinus Torvalds 	target->maxWidth = width;
29101da177e4SLinus Torvalds 
29111da177e4SLinus Torvalds 	target->tflags |= MPT_TARGET_FLAGS_VALID_NEGO;
29121da177e4SLinus Torvalds 
29131da177e4SLinus Torvalds 	/* Disable unused features.
29141da177e4SLinus Torvalds 	 */
29151da177e4SLinus Torvalds 	if (!width)
29161da177e4SLinus Torvalds 		target->negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	if (!offset)
29191da177e4SLinus Torvalds 		target->negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds 	if ( factor > MPT_ULTRA320 )
29221da177e4SLinus Torvalds 		noQas = 0;
29231da177e4SLinus Torvalds 
29241da177e4SLinus Torvalds 	/* GEM, processor WORKAROUND
29251da177e4SLinus Torvalds 	 */
29261da177e4SLinus Torvalds 	if ((target->inq_data[0] == TYPE_PROCESSOR) || (target->inq_data[0] > 0x08)) {
29271da177e4SLinus Torvalds 		target->negoFlags |= (MPT_TARGET_NO_NEGO_WIDE | MPT_TARGET_NO_NEGO_SYNC);
29281da177e4SLinus Torvalds 		pspi_data->dvStatus[id] |= MPT_SCSICFG_BLK_NEGO;
29291da177e4SLinus Torvalds 	} else {
29301da177e4SLinus Torvalds 		if (noQas && (pspi_data->noQas == 0)) {
29311da177e4SLinus Torvalds 			pspi_data->noQas |= MPT_TARGET_NO_NEGO_QAS;
29321da177e4SLinus Torvalds 			target->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds 			/* Disable QAS in a mixed configuration case
29351da177e4SLinus Torvalds 	 		*/
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds 			ddvtprintk((KERN_INFO "Disabling QAS due to noQas=%02x on id=%d!\n", noQas, id));
29381da177e4SLinus Torvalds 			for (ii = 0; ii < id; ii++) {
29391da177e4SLinus Torvalds 				if ( (vdev = hd->Targets[ii]) ) {
29401da177e4SLinus Torvalds 					vdev->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
29411da177e4SLinus Torvalds 					mptscsih_writeSDP1(hd, 0, ii, vdev->negoFlags);
29421da177e4SLinus Torvalds 				}
29431da177e4SLinus Torvalds 			}
29441da177e4SLinus Torvalds 		}
29451da177e4SLinus Torvalds 	}
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds 	/* Write SDP1 on this I/O to this target */
29481da177e4SLinus Torvalds 	if (pspi_data->dvStatus[id] & MPT_SCSICFG_NEGOTIATE) {
29491da177e4SLinus Torvalds 		ddvtprintk((KERN_INFO "MPT_SCSICFG_NEGOTIATE on id=%d!\n", id));
29501da177e4SLinus Torvalds 		mptscsih_writeSDP1(hd, 0, id, hd->negoNvram);
29511da177e4SLinus Torvalds 		pspi_data->dvStatus[id] &= ~MPT_SCSICFG_NEGOTIATE;
29521da177e4SLinus Torvalds 	} else if (pspi_data->dvStatus[id] & MPT_SCSICFG_BLK_NEGO) {
29531da177e4SLinus Torvalds 		ddvtprintk((KERN_INFO "MPT_SCSICFG_BLK_NEGO on id=%d!\n", id));
29541da177e4SLinus Torvalds 		mptscsih_writeSDP1(hd, 0, id, MPT_SCSICFG_BLK_NEGO);
29551da177e4SLinus Torvalds 		pspi_data->dvStatus[id] &= ~MPT_SCSICFG_BLK_NEGO;
29561da177e4SLinus Torvalds 	}
29571da177e4SLinus Torvalds }
29581da177e4SLinus Torvalds 
29591da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
29601da177e4SLinus Torvalds /* If DV disabled (negoNvram set to USE_NVARM) or if not LUN 0, return.
29611da177e4SLinus Torvalds  * Else set the NEED_DV flag after Read Capacity Issued (disks)
29621da177e4SLinus Torvalds  * or Mode Sense (cdroms).
29631da177e4SLinus Torvalds  *
29641da177e4SLinus Torvalds  * Tapes, initTarget will set this flag on completion of Inquiry command.
29651da177e4SLinus Torvalds  * Called only if DV_NOT_DONE flag is set
29661da177e4SLinus Torvalds  */
29670d0c7974SMoore, Eric Dean  static void
29680d0c7974SMoore, Eric Dean  mptscsih_set_dvflags(MPT_SCSI_HOST *hd, SCSIIORequest_t *pReq)
29691da177e4SLinus Torvalds {
29701da177e4SLinus Torvalds 	u8 cmd;
29711da177e4SLinus Torvalds 	ScsiCfgData *pSpi;
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds 	ddvtprintk((" set_dvflags: id=%d lun=%d negoNvram=%x cmd=%x\n",
29741da177e4SLinus Torvalds 		pReq->TargetID, pReq->LUN[1], hd->negoNvram, pReq->CDB[0]));
29751da177e4SLinus Torvalds 
29761da177e4SLinus Torvalds 	if ((pReq->LUN[1] != 0) || (hd->negoNvram != 0))
29771da177e4SLinus Torvalds 		return;
29781da177e4SLinus Torvalds 
29791da177e4SLinus Torvalds 	cmd = pReq->CDB[0];
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds 	if ((cmd == READ_CAPACITY) || (cmd == MODE_SENSE)) {
29821da177e4SLinus Torvalds 		pSpi = &hd->ioc->spi_data;
29831da177e4SLinus Torvalds 		if ((pSpi->isRaid & (1 << pReq->TargetID)) && pSpi->pIocPg3) {
29841da177e4SLinus Torvalds 			/* Set NEED_DV for all hidden disks
29851da177e4SLinus Torvalds 			 */
29861da177e4SLinus Torvalds 			Ioc3PhysDisk_t *pPDisk =  pSpi->pIocPg3->PhysDisk;
29871da177e4SLinus Torvalds 			int		numPDisk = pSpi->pIocPg3->NumPhysDisks;
29881da177e4SLinus Torvalds 
29891da177e4SLinus Torvalds 			while (numPDisk) {
29901da177e4SLinus Torvalds 				pSpi->dvStatus[pPDisk->PhysDiskID] |= MPT_SCSICFG_NEED_DV;
29911da177e4SLinus Torvalds 				ddvtprintk(("NEED_DV set for phys disk id %d\n", pPDisk->PhysDiskID));
29921da177e4SLinus Torvalds 				pPDisk++;
29931da177e4SLinus Torvalds 				numPDisk--;
29941da177e4SLinus Torvalds 			}
29951da177e4SLinus Torvalds 		}
29961da177e4SLinus Torvalds 		pSpi->dvStatus[pReq->TargetID] |= MPT_SCSICFG_NEED_DV;
29971da177e4SLinus Torvalds 		ddvtprintk(("NEED_DV set for visible disk id %d\n", pReq->TargetID));
29981da177e4SLinus Torvalds 	}
29991da177e4SLinus Torvalds }
30001da177e4SLinus Torvalds 
30011da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
30021da177e4SLinus Torvalds /*
30031da177e4SLinus Torvalds  * If no Target, bus reset on 1st I/O. Set the flag to
30041da177e4SLinus Torvalds  * prevent any future negotiations to this device.
30051da177e4SLinus Torvalds  */
30060d0c7974SMoore, Eric Dean  static void
30070d0c7974SMoore, Eric Dean  mptscsih_no_negotiate(MPT_SCSI_HOST *hd, int target_id)
30081da177e4SLinus Torvalds {
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 	if ((hd->Targets) && (hd->Targets[target_id] == NULL))
30111da177e4SLinus Torvalds 		hd->ioc->spi_data.dvStatus[target_id] |= MPT_SCSICFG_BLK_NEGO;
30121da177e4SLinus Torvalds 
30131da177e4SLinus Torvalds 	return;
30141da177e4SLinus Torvalds }
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
30171da177e4SLinus Torvalds /*
30181da177e4SLinus Torvalds  *  SCSI Config Page functionality ...
30191da177e4SLinus Torvalds  */
30201da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
30211da177e4SLinus Torvalds /*	mptscsih_setDevicePage1Flags  - add Requested and Configuration fields flags
30221da177e4SLinus Torvalds  *	based on width, factor and offset parameters.
30231da177e4SLinus Torvalds  *	@width: bus width
30241da177e4SLinus Torvalds  *	@factor: sync factor
30251da177e4SLinus Torvalds  *	@offset: sync offset
30261da177e4SLinus Torvalds  *	@requestedPtr: pointer to requested values (updated)
30271da177e4SLinus Torvalds  *	@configurationPtr: pointer to configuration values (updated)
30281da177e4SLinus Torvalds  *	@flags: flags to block WDTR or SDTR negotiation
30291da177e4SLinus Torvalds  *
30301da177e4SLinus Torvalds  *	Return: None.
30311da177e4SLinus Torvalds  *
30321da177e4SLinus Torvalds  *	Remark: Called by writeSDP1 and _dv_params
30331da177e4SLinus Torvalds  */
30341da177e4SLinus Torvalds static void
30351da177e4SLinus Torvalds mptscsih_setDevicePage1Flags (u8 width, u8 factor, u8 offset, int *requestedPtr, int *configurationPtr, u8 flags)
30361da177e4SLinus Torvalds {
30371da177e4SLinus Torvalds 	u8 nowide = flags & MPT_TARGET_NO_NEGO_WIDE;
30381da177e4SLinus Torvalds 	u8 nosync = flags & MPT_TARGET_NO_NEGO_SYNC;
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds 	*configurationPtr = 0;
30411da177e4SLinus Torvalds 	*requestedPtr = width ? MPI_SCSIDEVPAGE1_RP_WIDE : 0;
30421da177e4SLinus Torvalds 	*requestedPtr |= (offset << 16) | (factor << 8);
30431da177e4SLinus Torvalds 
30441da177e4SLinus Torvalds 	if (width && offset && !nowide && !nosync) {
30451da177e4SLinus Torvalds 		if (factor < MPT_ULTRA160) {
30461da177e4SLinus Torvalds 			*requestedPtr |= (MPI_SCSIDEVPAGE1_RP_IU + MPI_SCSIDEVPAGE1_RP_DT);
30471da177e4SLinus Torvalds 			if ((flags & MPT_TARGET_NO_NEGO_QAS) == 0)
30481da177e4SLinus Torvalds 				*requestedPtr |= MPI_SCSIDEVPAGE1_RP_QAS;
30491da177e4SLinus Torvalds 			if (flags & MPT_TAPE_NEGO_IDP)
30501da177e4SLinus Torvalds 				*requestedPtr |= 0x08000000;
30511da177e4SLinus Torvalds 		} else if (factor < MPT_ULTRA2) {
30521da177e4SLinus Torvalds 			*requestedPtr |= MPI_SCSIDEVPAGE1_RP_DT;
30531da177e4SLinus Torvalds 		}
30541da177e4SLinus Torvalds 	}
30551da177e4SLinus Torvalds 
30561da177e4SLinus Torvalds 	if (nowide)
30571da177e4SLinus Torvalds 		*configurationPtr |= MPI_SCSIDEVPAGE1_CONF_WDTR_DISALLOWED;
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds 	if (nosync)
30601da177e4SLinus Torvalds 		*configurationPtr |= MPI_SCSIDEVPAGE1_CONF_SDTR_DISALLOWED;
30611da177e4SLinus Torvalds 
30621da177e4SLinus Torvalds 	return;
30631da177e4SLinus Torvalds }
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
30661da177e4SLinus Torvalds /*	mptscsih_writeSDP1  - write SCSI Device Page 1
30671da177e4SLinus Torvalds  *	@hd: Pointer to a SCSI Host Strucutre
30681da177e4SLinus Torvalds  *	@portnum: IOC port number
30691da177e4SLinus Torvalds  *	@target_id: writeSDP1 for single ID
30701da177e4SLinus Torvalds  *	@flags: MPT_SCSICFG_ALL_IDS, MPT_SCSICFG_USE_NVRAM, MPT_SCSICFG_BLK_NEGO
30711da177e4SLinus Torvalds  *
30721da177e4SLinus Torvalds  *	Return: -EFAULT if read of config page header fails
30731da177e4SLinus Torvalds  *		or 0 if success.
30741da177e4SLinus Torvalds  *
30751da177e4SLinus Torvalds  *	Remark: If a target has been found, the settings from the
30761da177e4SLinus Torvalds  *		target structure are used, else the device is set
30771da177e4SLinus Torvalds  *		to async/narrow.
30781da177e4SLinus Torvalds  *
30791da177e4SLinus Torvalds  *	Remark: Called during init and after a FW reload.
30801da177e4SLinus Torvalds  *	Remark: We do not wait for a return, write pages sequentially.
30811da177e4SLinus Torvalds  */
30821da177e4SLinus Torvalds static int
30831da177e4SLinus Torvalds mptscsih_writeSDP1(MPT_SCSI_HOST *hd, int portnum, int target_id, int flags)
30841da177e4SLinus Torvalds {
30851da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc = hd->ioc;
30861da177e4SLinus Torvalds 	Config_t		*pReq;
30871da177e4SLinus Torvalds 	SCSIDevicePage1_t	*pData;
30881da177e4SLinus Torvalds 	VirtDevice		*pTarget;
30891da177e4SLinus Torvalds 	MPT_FRAME_HDR		*mf;
30901da177e4SLinus Torvalds 	dma_addr_t		 dataDma;
30911da177e4SLinus Torvalds 	u16			 req_idx;
30921da177e4SLinus Torvalds 	u32			 frameOffset;
30931da177e4SLinus Torvalds 	u32			 requested, configuration, flagsLength;
30941da177e4SLinus Torvalds 	int			 ii, nvram;
30951da177e4SLinus Torvalds 	int			 id = 0, maxid = 0;
30961da177e4SLinus Torvalds 	u8			 width;
30971da177e4SLinus Torvalds 	u8			 factor;
30981da177e4SLinus Torvalds 	u8			 offset;
30991da177e4SLinus Torvalds 	u8			 bus = 0;
31001da177e4SLinus Torvalds 	u8			 negoFlags;
31011da177e4SLinus Torvalds 	u8			 maxwidth, maxoffset, maxfactor;
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds 	if (ioc->spi_data.sdp1length == 0)
31041da177e4SLinus Torvalds 		return 0;
31051da177e4SLinus Torvalds 
31061da177e4SLinus Torvalds 	if (flags & MPT_SCSICFG_ALL_IDS) {
31071da177e4SLinus Torvalds 		id = 0;
31081da177e4SLinus Torvalds 		maxid = ioc->sh->max_id - 1;
31091da177e4SLinus Torvalds 	} else if (ioc->sh) {
31101da177e4SLinus Torvalds 		id = target_id;
31111da177e4SLinus Torvalds 		maxid = min_t(int, id, ioc->sh->max_id - 1);
31121da177e4SLinus Torvalds 	}
31131da177e4SLinus Torvalds 
31141da177e4SLinus Torvalds 	for (; id <= maxid; id++) {
31151da177e4SLinus Torvalds 
31161da177e4SLinus Torvalds 		if (id == ioc->pfacts[portnum].PortSCSIID)
31171da177e4SLinus Torvalds 			continue;
31181da177e4SLinus Torvalds 
31191da177e4SLinus Torvalds 		/* Use NVRAM to get adapter and target maximums
31201da177e4SLinus Torvalds 		 * Data over-riden by target structure information, if present
31211da177e4SLinus Torvalds 		 */
31221da177e4SLinus Torvalds 		maxwidth = ioc->spi_data.maxBusWidth;
31231da177e4SLinus Torvalds 		maxoffset = ioc->spi_data.maxSyncOffset;
31241da177e4SLinus Torvalds 		maxfactor = ioc->spi_data.minSyncFactor;
31251da177e4SLinus Torvalds 		if (ioc->spi_data.nvram && (ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
31261da177e4SLinus Torvalds 			nvram = ioc->spi_data.nvram[id];
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 			if (maxwidth)
31291da177e4SLinus Torvalds 				maxwidth = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
31301da177e4SLinus Torvalds 
31311da177e4SLinus Torvalds 			if (maxoffset > 0) {
31321da177e4SLinus Torvalds 				maxfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
31331da177e4SLinus Torvalds 				if (maxfactor == 0) {
31341da177e4SLinus Torvalds 					/* Key for async */
31351da177e4SLinus Torvalds 					maxfactor = MPT_ASYNC;
31361da177e4SLinus Torvalds 					maxoffset = 0;
31371da177e4SLinus Torvalds 				} else if (maxfactor < ioc->spi_data.minSyncFactor) {
31381da177e4SLinus Torvalds 					maxfactor = ioc->spi_data.minSyncFactor;
31391da177e4SLinus Torvalds 				}
31401da177e4SLinus Torvalds 			} else
31411da177e4SLinus Torvalds 				maxfactor = MPT_ASYNC;
31421da177e4SLinus Torvalds 		}
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds 		/* Set the negotiation flags.
31451da177e4SLinus Torvalds 		 */
31461da177e4SLinus Torvalds 		negoFlags = ioc->spi_data.noQas;
31471da177e4SLinus Torvalds 		if (!maxwidth)
31481da177e4SLinus Torvalds 			negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds 		if (!maxoffset)
31511da177e4SLinus Torvalds 			negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
31521da177e4SLinus Torvalds 
31531da177e4SLinus Torvalds 		if (flags & MPT_SCSICFG_USE_NVRAM) {
31541da177e4SLinus Torvalds 			width = maxwidth;
31551da177e4SLinus Torvalds 			factor = maxfactor;
31561da177e4SLinus Torvalds 			offset = maxoffset;
31571da177e4SLinus Torvalds 		} else {
31581da177e4SLinus Torvalds 			width = 0;
31591da177e4SLinus Torvalds 			factor = MPT_ASYNC;
31601da177e4SLinus Torvalds 			offset = 0;
31611da177e4SLinus Torvalds 			//negoFlags = 0;
31621da177e4SLinus Torvalds 			//negoFlags = MPT_TARGET_NO_NEGO_SYNC;
31631da177e4SLinus Torvalds 		}
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 		/* If id is not a raid volume, get the updated
31661da177e4SLinus Torvalds 		 * transmission settings from the target structure.
31671da177e4SLinus Torvalds 		 */
31681da177e4SLinus Torvalds 		if (hd->Targets && (pTarget = hd->Targets[id]) && !pTarget->raidVolume) {
31691da177e4SLinus Torvalds 			width = pTarget->maxWidth;
31701da177e4SLinus Torvalds 			factor = pTarget->minSyncFactor;
31711da177e4SLinus Torvalds 			offset = pTarget->maxOffset;
31721da177e4SLinus Torvalds 			negoFlags = pTarget->negoFlags;
31731da177e4SLinus Torvalds 		}
31741da177e4SLinus Torvalds 
31751da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
31761da177e4SLinus Torvalds 		/* Force to async and narrow if DV has not been executed
31771da177e4SLinus Torvalds 		 * for this ID
31781da177e4SLinus Torvalds 		 */
31791da177e4SLinus Torvalds 		if ((hd->ioc->spi_data.dvStatus[id] & MPT_SCSICFG_DV_NOT_DONE) != 0) {
31801da177e4SLinus Torvalds 			width = 0;
31811da177e4SLinus Torvalds 			factor = MPT_ASYNC;
31821da177e4SLinus Torvalds 			offset = 0;
31831da177e4SLinus Torvalds 		}
31841da177e4SLinus Torvalds #endif
31851da177e4SLinus Torvalds 
31861da177e4SLinus Torvalds 		if (flags & MPT_SCSICFG_BLK_NEGO)
31871da177e4SLinus Torvalds 			negoFlags = MPT_TARGET_NO_NEGO_WIDE | MPT_TARGET_NO_NEGO_SYNC;
31881da177e4SLinus Torvalds 
31891da177e4SLinus Torvalds 		mptscsih_setDevicePage1Flags(width, factor, offset,
31901da177e4SLinus Torvalds 					&requested, &configuration, negoFlags);
31911da177e4SLinus Torvalds 		dnegoprintk(("writeSDP1: id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x config=%x\n",
31921da177e4SLinus Torvalds 			target_id, width, factor, offset, negoFlags, requested, configuration));
31931da177e4SLinus Torvalds 
31941da177e4SLinus Torvalds 		/* Get a MF for this command.
31951da177e4SLinus Torvalds 		 */
31960d0c7974SMoore, Eric Dean  		if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
31971da177e4SLinus Torvalds 			dprintk((MYIOC_s_WARN_FMT "write SDP1: no msg frames!\n",
31981da177e4SLinus Torvalds 						ioc->name));
31991da177e4SLinus Torvalds 			return -EAGAIN;
32001da177e4SLinus Torvalds 		}
32011da177e4SLinus Torvalds 
32021da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_INFO_FMT "WriteSDP1 (mf=%p, id=%d, req=0x%x, cfg=0x%x)\n",
32031da177e4SLinus Torvalds 			hd->ioc->name, mf, id, requested, configuration));
32041da177e4SLinus Torvalds 
32051da177e4SLinus Torvalds 
32061da177e4SLinus Torvalds 		/* Set the request and the data pointers.
32071da177e4SLinus Torvalds 		 * Request takes: 36 bytes (32 bit SGE)
32081da177e4SLinus Torvalds 		 * SCSI Device Page 1 requires 16 bytes
32091da177e4SLinus Torvalds 		 * 40 + 16 <= size of SCSI IO Request = 56 bytes
32101da177e4SLinus Torvalds 		 * and MF size >= 64 bytes.
32111da177e4SLinus Torvalds 		 * Place data at end of MF.
32121da177e4SLinus Torvalds 		 */
32131da177e4SLinus Torvalds 		pReq = (Config_t *)mf;
32141da177e4SLinus Torvalds 
32151da177e4SLinus Torvalds 		req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
32161da177e4SLinus Torvalds 		frameOffset = ioc->req_sz - sizeof(SCSIDevicePage1_t);
32171da177e4SLinus Torvalds 
32181da177e4SLinus Torvalds 		pData = (SCSIDevicePage1_t *)((u8 *) mf + frameOffset);
32191da177e4SLinus Torvalds 		dataDma = ioc->req_frames_dma + (req_idx * ioc->req_sz) + frameOffset;
32201da177e4SLinus Torvalds 
32211da177e4SLinus Torvalds 		/* Complete the request frame (same for all requests).
32221da177e4SLinus Torvalds 		 */
32231da177e4SLinus Torvalds 		pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
32241da177e4SLinus Torvalds 		pReq->Reserved = 0;
32251da177e4SLinus Torvalds 		pReq->ChainOffset = 0;
32261da177e4SLinus Torvalds 		pReq->Function = MPI_FUNCTION_CONFIG;
32271da177e4SLinus Torvalds 		pReq->ExtPageLength = 0;
32281da177e4SLinus Torvalds 		pReq->ExtPageType = 0;
32291da177e4SLinus Torvalds 		pReq->MsgFlags = 0;
32301da177e4SLinus Torvalds 		for (ii=0; ii < 8; ii++) {
32311da177e4SLinus Torvalds 			pReq->Reserved2[ii] = 0;
32321da177e4SLinus Torvalds 		}
32331da177e4SLinus Torvalds 		pReq->Header.PageVersion = ioc->spi_data.sdp1version;
32341da177e4SLinus Torvalds 		pReq->Header.PageLength = ioc->spi_data.sdp1length;
32351da177e4SLinus Torvalds 		pReq->Header.PageNumber = 1;
32361da177e4SLinus Torvalds 		pReq->Header.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
32371da177e4SLinus Torvalds 		pReq->PageAddress = cpu_to_le32(id | (bus << 8 ));
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds 		/* Add a SGE to the config request.
32401da177e4SLinus Torvalds 		 */
32411da177e4SLinus Torvalds 		flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE | ioc->spi_data.sdp1length * 4;
32421da177e4SLinus Torvalds 
32431da177e4SLinus Torvalds 		mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds 		/* Set up the common data portion
32461da177e4SLinus Torvalds 		 */
32471da177e4SLinus Torvalds 		pData->Header.PageVersion = pReq->Header.PageVersion;
32481da177e4SLinus Torvalds 		pData->Header.PageLength = pReq->Header.PageLength;
32491da177e4SLinus Torvalds 		pData->Header.PageNumber = pReq->Header.PageNumber;
32501da177e4SLinus Torvalds 		pData->Header.PageType = pReq->Header.PageType;
32511da177e4SLinus Torvalds 		pData->RequestedParameters = cpu_to_le32(requested);
32521da177e4SLinus Torvalds 		pData->Reserved = 0;
32531da177e4SLinus Torvalds 		pData->Configuration = cpu_to_le32(configuration);
32541da177e4SLinus Torvalds 
32551da177e4SLinus Torvalds 		dprintk((MYIOC_s_INFO_FMT
32561da177e4SLinus Torvalds 			"write SDP1: id %d pgaddr 0x%x req 0x%x config 0x%x\n",
32571da177e4SLinus Torvalds 				ioc->name, id, (id | (bus<<8)),
32581da177e4SLinus Torvalds 				requested, configuration));
32591da177e4SLinus Torvalds 
32600d0c7974SMoore, Eric Dean  		mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
32611da177e4SLinus Torvalds 	}
32621da177e4SLinus Torvalds 
32631da177e4SLinus Torvalds 	return 0;
32641da177e4SLinus Torvalds }
32651da177e4SLinus Torvalds 
32661da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
32671da177e4SLinus Torvalds /*	mptscsih_writeIOCPage4  - write IOC Page 4
32681da177e4SLinus Torvalds  *	@hd: Pointer to a SCSI Host Structure
32691da177e4SLinus Torvalds  *	@target_id: write IOC Page4 for this ID & Bus
32701da177e4SLinus Torvalds  *
32711da177e4SLinus Torvalds  *	Return: -EAGAIN if unable to obtain a Message Frame
32721da177e4SLinus Torvalds  *		or 0 if success.
32731da177e4SLinus Torvalds  *
32741da177e4SLinus Torvalds  *	Remark: We do not wait for a return, write pages sequentially.
32751da177e4SLinus Torvalds  */
32761da177e4SLinus Torvalds static int
32771da177e4SLinus Torvalds mptscsih_writeIOCPage4(MPT_SCSI_HOST *hd, int target_id, int bus)
32781da177e4SLinus Torvalds {
32791da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc = hd->ioc;
32801da177e4SLinus Torvalds 	Config_t		*pReq;
32811da177e4SLinus Torvalds 	IOCPage4_t		*IOCPage4Ptr;
32821da177e4SLinus Torvalds 	MPT_FRAME_HDR		*mf;
32831da177e4SLinus Torvalds 	dma_addr_t		 dataDma;
32841da177e4SLinus Torvalds 	u16			 req_idx;
32851da177e4SLinus Torvalds 	u32			 frameOffset;
32861da177e4SLinus Torvalds 	u32			 flagsLength;
32871da177e4SLinus Torvalds 	int			 ii;
32881da177e4SLinus Torvalds 
32891da177e4SLinus Torvalds 	/* Get a MF for this command.
32901da177e4SLinus Torvalds 	 */
32910d0c7974SMoore, Eric Dean  	if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
32921da177e4SLinus Torvalds 		dprintk((MYIOC_s_WARN_FMT "writeIOCPage4 : no msg frames!\n",
32931da177e4SLinus Torvalds 					ioc->name));
32941da177e4SLinus Torvalds 		return -EAGAIN;
32951da177e4SLinus Torvalds 	}
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds 	/* Set the request and the data pointers.
32981da177e4SLinus Torvalds 	 * Place data at end of MF.
32991da177e4SLinus Torvalds 	 */
33001da177e4SLinus Torvalds 	pReq = (Config_t *)mf;
33011da177e4SLinus Torvalds 
33021da177e4SLinus Torvalds 	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
33031da177e4SLinus Torvalds 	frameOffset = ioc->req_sz - sizeof(IOCPage4_t);
33041da177e4SLinus Torvalds 
33051da177e4SLinus Torvalds 	/* Complete the request frame (same for all requests).
33061da177e4SLinus Torvalds 	 */
33071da177e4SLinus Torvalds 	pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
33081da177e4SLinus Torvalds 	pReq->Reserved = 0;
33091da177e4SLinus Torvalds 	pReq->ChainOffset = 0;
33101da177e4SLinus Torvalds 	pReq->Function = MPI_FUNCTION_CONFIG;
33111da177e4SLinus Torvalds 	pReq->ExtPageLength = 0;
33121da177e4SLinus Torvalds 	pReq->ExtPageType = 0;
33131da177e4SLinus Torvalds 	pReq->MsgFlags = 0;
33141da177e4SLinus Torvalds 	for (ii=0; ii < 8; ii++) {
33151da177e4SLinus Torvalds 		pReq->Reserved2[ii] = 0;
33161da177e4SLinus Torvalds 	}
33171da177e4SLinus Torvalds 
33181da177e4SLinus Torvalds        	IOCPage4Ptr = ioc->spi_data.pIocPg4;
33191da177e4SLinus Torvalds        	dataDma = ioc->spi_data.IocPg4_dma;
33201da177e4SLinus Torvalds        	ii = IOCPage4Ptr->ActiveSEP++;
33211da177e4SLinus Torvalds        	IOCPage4Ptr->SEP[ii].SEPTargetID = target_id;
33221da177e4SLinus Torvalds        	IOCPage4Ptr->SEP[ii].SEPBus = bus;
33231da177e4SLinus Torvalds        	pReq->Header = IOCPage4Ptr->Header;
33241da177e4SLinus Torvalds 	pReq->PageAddress = cpu_to_le32(target_id | (bus << 8 ));
33251da177e4SLinus Torvalds 
33261da177e4SLinus Torvalds 	/* Add a SGE to the config request.
33271da177e4SLinus Torvalds 	 */
33281da177e4SLinus Torvalds 	flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE |
33291da177e4SLinus Torvalds 		(IOCPage4Ptr->Header.PageLength + ii) * 4;
33301da177e4SLinus Torvalds 
33311da177e4SLinus Torvalds 	mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);
33321da177e4SLinus Torvalds 
33331da177e4SLinus Torvalds 	dinitprintk((MYIOC_s_INFO_FMT
33341da177e4SLinus Torvalds 		"writeIOCPage4: MaxSEP=%d ActiveSEP=%d id=%d bus=%d\n",
33351da177e4SLinus Torvalds 			ioc->name, IOCPage4Ptr->MaxSEP, IOCPage4Ptr->ActiveSEP, target_id, bus));
33361da177e4SLinus Torvalds 
33370d0c7974SMoore, Eric Dean  	mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
33381da177e4SLinus Torvalds 
33391da177e4SLinus Torvalds 	return 0;
33401da177e4SLinus Torvalds }
33411da177e4SLinus Torvalds 
33421da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
33431da177e4SLinus Torvalds /*
33441da177e4SLinus Torvalds  *  Bus Scan and Domain Validation functionality ...
33451da177e4SLinus Torvalds  */
33461da177e4SLinus Torvalds 
33471da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
33481da177e4SLinus Torvalds /*
33491da177e4SLinus Torvalds  *	mptscsih_scandv_complete - Scan and DV callback routine registered
33501da177e4SLinus Torvalds  *	to Fustion MPT (base) driver.
33511da177e4SLinus Torvalds  *
33521da177e4SLinus Torvalds  *	@ioc: Pointer to MPT_ADAPTER structure
33531da177e4SLinus Torvalds  *	@mf: Pointer to original MPT request frame
33541da177e4SLinus Torvalds  *	@mr: Pointer to MPT reply frame (NULL if TurboReply)
33551da177e4SLinus Torvalds  *
33561da177e4SLinus Torvalds  *	This routine is called from mpt.c::mpt_interrupt() at the completion
33571da177e4SLinus Torvalds  *	of any SCSI IO request.
33581da177e4SLinus Torvalds  *	This routine is registered with the Fusion MPT (base) driver at driver
33591da177e4SLinus Torvalds  *	load/init time via the mpt_register() API call.
33601da177e4SLinus Torvalds  *
33611da177e4SLinus Torvalds  *	Returns 1 indicating alloc'd request frame ptr should be freed.
33621da177e4SLinus Torvalds  *
33631da177e4SLinus Torvalds  *	Remark: Sets a completion code and (possibly) saves sense data
33641da177e4SLinus Torvalds  *	in the IOC member localReply structure.
33651da177e4SLinus Torvalds  *	Used ONLY for DV and other internal commands.
33661da177e4SLinus Torvalds  */
33670d0c7974SMoore, Eric Dean  int
33681da177e4SLinus Torvalds mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
33691da177e4SLinus Torvalds {
33701da177e4SLinus Torvalds 	MPT_SCSI_HOST	*hd;
33711da177e4SLinus Torvalds 	SCSIIORequest_t *pReq;
33721da177e4SLinus Torvalds 	int		 completionCode;
33731da177e4SLinus Torvalds 	u16		 req_idx;
33741da177e4SLinus Torvalds 
33750d0c7974SMoore, Eric Dean  	hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
33760d0c7974SMoore, Eric Dean  
33771da177e4SLinus Torvalds 	if ((mf == NULL) ||
33781da177e4SLinus Torvalds 	    (mf >= MPT_INDEX_2_MFPTR(ioc, ioc->req_depth))) {
33791da177e4SLinus Torvalds 		printk(MYIOC_s_ERR_FMT
33801da177e4SLinus Torvalds 			"ScanDvComplete, %s req frame ptr! (=%p)\n",
33811da177e4SLinus Torvalds 				ioc->name, mf?"BAD":"NULL", (void *) mf);
33821da177e4SLinus Torvalds 		goto wakeup;
33831da177e4SLinus Torvalds 	}
33841da177e4SLinus Torvalds 
33851da177e4SLinus Torvalds 	del_timer(&hd->timer);
33861da177e4SLinus Torvalds 	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
33871da177e4SLinus Torvalds 	hd->ScsiLookup[req_idx] = NULL;
33881da177e4SLinus Torvalds 	pReq = (SCSIIORequest_t *) mf;
33891da177e4SLinus Torvalds 
33901da177e4SLinus Torvalds 	if (mf != hd->cmdPtr) {
33911da177e4SLinus Torvalds 		printk(MYIOC_s_WARN_FMT "ScanDvComplete (mf=%p, cmdPtr=%p, idx=%d)\n",
33921da177e4SLinus Torvalds 				hd->ioc->name, (void *)mf, (void *) hd->cmdPtr, req_idx);
33931da177e4SLinus Torvalds 	}
33941da177e4SLinus Torvalds 	hd->cmdPtr = NULL;
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_INFO_FMT "ScanDvComplete (mf=%p,mr=%p,idx=%d)\n",
33971da177e4SLinus Torvalds 			hd->ioc->name, mf, mr, req_idx));
33981da177e4SLinus Torvalds 
33991da177e4SLinus Torvalds 	hd->pLocal = &hd->localReply;
34001da177e4SLinus Torvalds 	hd->pLocal->scsiStatus = 0;
34011da177e4SLinus Torvalds 
34021da177e4SLinus Torvalds 	/* If target struct exists, clear sense valid flag.
34031da177e4SLinus Torvalds 	 */
34041da177e4SLinus Torvalds 	if (mr == NULL) {
34051da177e4SLinus Torvalds 		completionCode = MPT_SCANDV_GOOD;
34061da177e4SLinus Torvalds 	} else {
34071da177e4SLinus Torvalds 		SCSIIOReply_t	*pReply;
34081da177e4SLinus Torvalds 		u16		 status;
34091da177e4SLinus Torvalds 		u8		 scsi_status;
34101da177e4SLinus Torvalds 
34111da177e4SLinus Torvalds 		pReply = (SCSIIOReply_t *) mr;
34121da177e4SLinus Torvalds 
34131da177e4SLinus Torvalds 		status = le16_to_cpu(pReply->IOCStatus) & MPI_IOCSTATUS_MASK;
34141da177e4SLinus Torvalds 		scsi_status = pReply->SCSIStatus;
34151da177e4SLinus Torvalds 
34161da177e4SLinus Torvalds 		ddvtprintk((KERN_NOTICE "  IOCStatus=%04xh, SCSIState=%02xh, SCSIStatus=%02xh, IOCLogInfo=%08xh\n",
34171da177e4SLinus Torvalds 			     status, pReply->SCSIState, scsi_status,
34181da177e4SLinus Torvalds 			     le32_to_cpu(pReply->IOCLogInfo)));
34191da177e4SLinus Torvalds 
34201da177e4SLinus Torvalds 		switch(status) {
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:	/* 0x0043 */
34231da177e4SLinus Torvalds 			completionCode = MPT_SCANDV_SELECTION_TIMEOUT;
34241da177e4SLinus Torvalds 			break;
34251da177e4SLinus Torvalds 
34261da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:		/* 0x0046 */
34271da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:	/* 0x0048 */
34281da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:		/* 0x004B */
34291da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:		/* 0x004C */
34301da177e4SLinus Torvalds 			completionCode = MPT_SCANDV_DID_RESET;
34311da177e4SLinus Torvalds 			break;
34321da177e4SLinus Torvalds 
34331da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:		/* 0x0045 */
34341da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:	/* 0x0040 */
34351da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SUCCESS:			/* 0x0000 */
34361da177e4SLinus Torvalds 			if (pReply->Function == MPI_FUNCTION_CONFIG) {
34371da177e4SLinus Torvalds 				ConfigReply_t *pr = (ConfigReply_t *)mr;
34381da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_GOOD;
34391da177e4SLinus Torvalds 				hd->pLocal->header.PageVersion = pr->Header.PageVersion;
34401da177e4SLinus Torvalds 				hd->pLocal->header.PageLength = pr->Header.PageLength;
34411da177e4SLinus Torvalds 				hd->pLocal->header.PageNumber = pr->Header.PageNumber;
34421da177e4SLinus Torvalds 				hd->pLocal->header.PageType = pr->Header.PageType;
34431da177e4SLinus Torvalds 
34441da177e4SLinus Torvalds 			} else if (pReply->Function == MPI_FUNCTION_RAID_ACTION) {
34451da177e4SLinus Torvalds 				/* If the RAID Volume request is successful,
34461da177e4SLinus Torvalds 				 * return GOOD, else indicate that
34471da177e4SLinus Torvalds 				 * some type of error occurred.
34481da177e4SLinus Torvalds 				 */
34491da177e4SLinus Torvalds 				MpiRaidActionReply_t	*pr = (MpiRaidActionReply_t *)mr;
3450637fa99bSChristoph Hellwig 				if (le16_to_cpu(pr->ActionStatus) == MPI_RAID_ACTION_ASTATUS_SUCCESS)
34511da177e4SLinus Torvalds 					completionCode = MPT_SCANDV_GOOD;
34521da177e4SLinus Torvalds 				else
34531da177e4SLinus Torvalds 					completionCode = MPT_SCANDV_SOME_ERROR;
34541da177e4SLinus Torvalds 
34551da177e4SLinus Torvalds 			} else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
34561da177e4SLinus Torvalds 				u8		*sense_data;
34571da177e4SLinus Torvalds 				int		 sz;
34581da177e4SLinus Torvalds 
34591da177e4SLinus Torvalds 				/* save sense data in global structure
34601da177e4SLinus Torvalds 				 */
34611da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_SENSE;
34621da177e4SLinus Torvalds 				hd->pLocal->scsiStatus = scsi_status;
34631da177e4SLinus Torvalds 				sense_data = ((u8 *)hd->ioc->sense_buf_pool +
34641da177e4SLinus Torvalds 					(req_idx * MPT_SENSE_BUFFER_ALLOC));
34651da177e4SLinus Torvalds 
34661da177e4SLinus Torvalds 				sz = min_t(int, pReq->SenseBufferLength,
34671da177e4SLinus Torvalds 							SCSI_STD_SENSE_BYTES);
34681da177e4SLinus Torvalds 				memcpy(hd->pLocal->sense, sense_data, sz);
34691da177e4SLinus Torvalds 
34701da177e4SLinus Torvalds 				ddvprintk((KERN_NOTICE "  Check Condition, sense ptr %p\n",
34711da177e4SLinus Torvalds 						sense_data));
34721da177e4SLinus Torvalds 			} else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_FAILED) {
34731da177e4SLinus Torvalds 				if (pReq->CDB[0] == INQUIRY)
34741da177e4SLinus Torvalds 					completionCode = MPT_SCANDV_ISSUE_SENSE;
34751da177e4SLinus Torvalds 				else
34761da177e4SLinus Torvalds 					completionCode = MPT_SCANDV_DID_RESET;
34771da177e4SLinus Torvalds 			}
34781da177e4SLinus Torvalds 			else if (pReply->SCSIState & MPI_SCSI_STATE_NO_SCSI_STATUS)
34791da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_DID_RESET;
34801da177e4SLinus Torvalds 			else if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
34811da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_DID_RESET;
34821da177e4SLinus Torvalds 			else {
34831da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_GOOD;
34841da177e4SLinus Torvalds 				hd->pLocal->scsiStatus = scsi_status;
34851da177e4SLinus Torvalds 			}
34861da177e4SLinus Torvalds 			break;
34871da177e4SLinus Torvalds 
34881da177e4SLinus Torvalds 		case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:		/* 0x0047 */
34891da177e4SLinus Torvalds 			if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
34901da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_DID_RESET;
34911da177e4SLinus Torvalds 			else
34921da177e4SLinus Torvalds 				completionCode = MPT_SCANDV_SOME_ERROR;
34931da177e4SLinus Torvalds 			break;
34941da177e4SLinus Torvalds 
34951da177e4SLinus Torvalds 		default:
34961da177e4SLinus Torvalds 			completionCode = MPT_SCANDV_SOME_ERROR;
34971da177e4SLinus Torvalds 			break;
34981da177e4SLinus Torvalds 
34991da177e4SLinus Torvalds 		}	/* switch(status) */
35001da177e4SLinus Torvalds 
35011da177e4SLinus Torvalds 		ddvtprintk((KERN_NOTICE "  completionCode set to %08xh\n",
35021da177e4SLinus Torvalds 				completionCode));
35031da177e4SLinus Torvalds 	} /* end of address reply case */
35041da177e4SLinus Torvalds 
35051da177e4SLinus Torvalds 	hd->pLocal->completion = completionCode;
35061da177e4SLinus Torvalds 
35071da177e4SLinus Torvalds 	/* MF and RF are freed in mpt_interrupt
35081da177e4SLinus Torvalds 	 */
35091da177e4SLinus Torvalds wakeup:
35101da177e4SLinus Torvalds 	/* Free Chain buffers (will never chain) in scan or dv */
35111da177e4SLinus Torvalds 	//mptscsih_freeChainBuffers(ioc, req_idx);
35121da177e4SLinus Torvalds 
35131da177e4SLinus Torvalds 	/*
35141da177e4SLinus Torvalds 	 * Wake up the original calling thread
35151da177e4SLinus Torvalds 	 */
35160d0c7974SMoore, Eric Dean  	hd->scandv_wait_done = 1;
35170d0c7974SMoore, Eric Dean  	wake_up(&hd->scandv_waitq);
35181da177e4SLinus Torvalds 
35191da177e4SLinus Torvalds 	return 1;
35201da177e4SLinus Torvalds }
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
35231da177e4SLinus Torvalds /*	mptscsih_timer_expired - Call back for timer process.
35241da177e4SLinus Torvalds  *	Used only for dv functionality.
35251da177e4SLinus Torvalds  *	@data: Pointer to MPT_SCSI_HOST recast as an unsigned long
35261da177e4SLinus Torvalds  *
35271da177e4SLinus Torvalds  */
35280d0c7974SMoore, Eric Dean  void
35290d0c7974SMoore, Eric Dean  mptscsih_timer_expired(unsigned long data)
35301da177e4SLinus Torvalds {
35311da177e4SLinus Torvalds 	MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *) data;
35321da177e4SLinus Torvalds 
35331da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_WARN_FMT "Timer Expired! Cmd %p\n", hd->ioc->name, hd->cmdPtr));
35341da177e4SLinus Torvalds 
35351da177e4SLinus Torvalds 	if (hd->cmdPtr) {
35361da177e4SLinus Torvalds 		MPIHeader_t *cmd = (MPIHeader_t *)hd->cmdPtr;
35371da177e4SLinus Torvalds 
35381da177e4SLinus Torvalds 		if (cmd->Function == MPI_FUNCTION_SCSI_IO_REQUEST) {
35391da177e4SLinus Torvalds 			/* Desire to issue a task management request here.
35401da177e4SLinus Torvalds 			 * TM requests MUST be single threaded.
35411da177e4SLinus Torvalds 			 * If old eh code and no TM current, issue request.
35421da177e4SLinus Torvalds 			 * If new eh code, do nothing. Wait for OS cmd timeout
35431da177e4SLinus Torvalds 			 *	for bus reset.
35441da177e4SLinus Torvalds 			 */
35451da177e4SLinus Torvalds 			ddvtprintk((MYIOC_s_NOTE_FMT "DV Cmd Timeout: NoOp\n", hd->ioc->name));
35461da177e4SLinus Torvalds 		} else {
35471da177e4SLinus Torvalds 			/* Perform a FW reload */
35481da177e4SLinus Torvalds 			if (mpt_HardResetHandler(hd->ioc, NO_SLEEP) < 0) {
35491da177e4SLinus Torvalds 				printk(MYIOC_s_WARN_FMT "Firmware Reload FAILED!\n", hd->ioc->name);
35501da177e4SLinus Torvalds 			}
35511da177e4SLinus Torvalds 		}
35521da177e4SLinus Torvalds 	} else {
35531da177e4SLinus Torvalds 		/* This should NEVER happen */
35541da177e4SLinus Torvalds 		printk(MYIOC_s_WARN_FMT "Null cmdPtr!!!!\n", hd->ioc->name);
35551da177e4SLinus Torvalds 	}
35561da177e4SLinus Torvalds 
35571da177e4SLinus Torvalds 	/* No more processing.
35581da177e4SLinus Torvalds 	 * TM call will generate an interrupt for SCSI TM Management.
35591da177e4SLinus Torvalds 	 * The FW will reply to all outstanding commands, callback will finish cleanup.
35601da177e4SLinus Torvalds 	 * Hard reset clean-up will free all resources.
35611da177e4SLinus Torvalds 	 */
35621da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_WARN_FMT "Timer Expired Complete!\n", hd->ioc->name));
35631da177e4SLinus Torvalds 
35641da177e4SLinus Torvalds 	return;
35651da177e4SLinus Torvalds }
35661da177e4SLinus Torvalds 
35671da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
35681da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
35691da177e4SLinus Torvalds /*	mptscsih_do_raid - Format and Issue a RAID volume request message.
35701da177e4SLinus Torvalds  *	@hd: Pointer to scsi host structure
35711da177e4SLinus Torvalds  *	@action: What do be done.
35721da177e4SLinus Torvalds  *	@id: Logical target id.
35731da177e4SLinus Torvalds  *	@bus: Target locations bus.
35741da177e4SLinus Torvalds  *
35751da177e4SLinus Torvalds  *	Returns: < 0 on a fatal error
35761da177e4SLinus Torvalds  *		0 on success
35771da177e4SLinus Torvalds  *
35781da177e4SLinus Torvalds  *	Remark: Wait to return until reply processed by the ISR.
35791da177e4SLinus Torvalds  */
35801da177e4SLinus Torvalds static int
35811da177e4SLinus Torvalds mptscsih_do_raid(MPT_SCSI_HOST *hd, u8 action, INTERNAL_CMD *io)
35821da177e4SLinus Torvalds {
35831da177e4SLinus Torvalds 	MpiRaidActionRequest_t	*pReq;
35841da177e4SLinus Torvalds 	MPT_FRAME_HDR		*mf;
35851da177e4SLinus Torvalds 	int			in_isr;
35861da177e4SLinus Torvalds 
35871da177e4SLinus Torvalds 	in_isr = in_interrupt();
35881da177e4SLinus Torvalds 	if (in_isr) {
35891da177e4SLinus Torvalds 		dprintk((MYIOC_s_WARN_FMT "Internal raid request not allowed in ISR context!\n",
35901da177e4SLinus Torvalds        				hd->ioc->name));
35911da177e4SLinus Torvalds 		return -EPERM;
35921da177e4SLinus Torvalds 	}
35931da177e4SLinus Torvalds 
35941da177e4SLinus Torvalds 	/* Get and Populate a free Frame
35951da177e4SLinus Torvalds 	 */
35960d0c7974SMoore, Eric Dean  	if ((mf = mpt_get_msg_frame(hd->ioc->InternalCtx, hd->ioc)) == NULL) {
35971da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_WARN_FMT "_do_raid: no msg frames!\n",
35981da177e4SLinus Torvalds 					hd->ioc->name));
35991da177e4SLinus Torvalds 		return -EAGAIN;
36001da177e4SLinus Torvalds 	}
36011da177e4SLinus Torvalds 	pReq = (MpiRaidActionRequest_t *)mf;
36021da177e4SLinus Torvalds 	pReq->Action = action;
36031da177e4SLinus Torvalds 	pReq->Reserved1 = 0;
36041da177e4SLinus Torvalds 	pReq->ChainOffset = 0;
36051da177e4SLinus Torvalds 	pReq->Function = MPI_FUNCTION_RAID_ACTION;
36061da177e4SLinus Torvalds 	pReq->VolumeID = io->id;
36071da177e4SLinus Torvalds 	pReq->VolumeBus = io->bus;
36081da177e4SLinus Torvalds 	pReq->PhysDiskNum = io->physDiskNum;
36091da177e4SLinus Torvalds 	pReq->MsgFlags = 0;
36101da177e4SLinus Torvalds 	pReq->Reserved2 = 0;
36111da177e4SLinus Torvalds 	pReq->ActionDataWord = 0; /* Reserved for this action */
36121da177e4SLinus Torvalds 	//pReq->ActionDataSGE = 0;
36131da177e4SLinus Torvalds 
36141da177e4SLinus Torvalds 	mpt_add_sge((char *)&pReq->ActionDataSGE,
36151da177e4SLinus Torvalds 		MPT_SGE_FLAGS_SSIMPLE_READ | 0, (dma_addr_t) -1);
36161da177e4SLinus Torvalds 
36171da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_INFO_FMT "RAID Volume action %x id %d\n",
36181da177e4SLinus Torvalds 			hd->ioc->name, action, io->id));
36191da177e4SLinus Torvalds 
36201da177e4SLinus Torvalds 	hd->pLocal = NULL;
36211da177e4SLinus Torvalds 	hd->timer.expires = jiffies + HZ*10; /* 10 second timeout */
36220d0c7974SMoore, Eric Dean  	hd->scandv_wait_done = 0;
36231da177e4SLinus Torvalds 
36241da177e4SLinus Torvalds 	/* Save cmd pointer, for resource free if timeout or
36251da177e4SLinus Torvalds 	 * FW reload occurs
36261da177e4SLinus Torvalds 	 */
36271da177e4SLinus Torvalds 	hd->cmdPtr = mf;
36281da177e4SLinus Torvalds 
36291da177e4SLinus Torvalds 	add_timer(&hd->timer);
36300d0c7974SMoore, Eric Dean  	mpt_put_msg_frame(hd->ioc->InternalCtx, hd->ioc, mf);
36310d0c7974SMoore, Eric Dean  	wait_event(hd->scandv_waitq, hd->scandv_wait_done);
36321da177e4SLinus Torvalds 
36331da177e4SLinus Torvalds 	if ((hd->pLocal == NULL) || (hd->pLocal->completion != MPT_SCANDV_GOOD))
36341da177e4SLinus Torvalds 		return -1;
36351da177e4SLinus Torvalds 
36361da177e4SLinus Torvalds 	return 0;
36371da177e4SLinus Torvalds }
36381da177e4SLinus Torvalds #endif /* ~MPTSCSIH_ENABLE_DOMAIN_VALIDATION */
36391da177e4SLinus Torvalds 
36401da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
36411da177e4SLinus Torvalds /**
36421da177e4SLinus Torvalds  *	mptscsih_do_cmd - Do internal command.
36431da177e4SLinus Torvalds  *	@hd: MPT_SCSI_HOST pointer
36441da177e4SLinus Torvalds  *	@io: INTERNAL_CMD pointer.
36451da177e4SLinus Torvalds  *
36461da177e4SLinus Torvalds  *	Issue the specified internally generated command and do command
36471da177e4SLinus Torvalds  *	specific cleanup. For bus scan / DV only.
36481da177e4SLinus Torvalds  *	NOTES: If command is Inquiry and status is good,
36491da177e4SLinus Torvalds  *	initialize a target structure, save the data
36501da177e4SLinus Torvalds  *
36511da177e4SLinus Torvalds  *	Remark: Single threaded access only.
36521da177e4SLinus Torvalds  *
36531da177e4SLinus Torvalds  *	Return:
36541da177e4SLinus Torvalds  *		< 0 if an illegal command or no resources
36551da177e4SLinus Torvalds  *
36561da177e4SLinus Torvalds  *		   0 if good
36571da177e4SLinus Torvalds  *
36581da177e4SLinus Torvalds  *		 > 0 if command complete but some type of completion error.
36591da177e4SLinus Torvalds  */
36601da177e4SLinus Torvalds static int
36611da177e4SLinus Torvalds mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *io)
36621da177e4SLinus Torvalds {
36631da177e4SLinus Torvalds 	MPT_FRAME_HDR	*mf;
36641da177e4SLinus Torvalds 	SCSIIORequest_t	*pScsiReq;
36651da177e4SLinus Torvalds 	SCSIIORequest_t	 ReqCopy;
36661da177e4SLinus Torvalds 	int		 my_idx, ii, dir;
36671da177e4SLinus Torvalds 	int		 rc, cmdTimeout;
36681da177e4SLinus Torvalds 	int		in_isr;
36691da177e4SLinus Torvalds 	char		 cmdLen;
36701da177e4SLinus Torvalds 	char		 CDB[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
36711da177e4SLinus Torvalds 	char		 cmd = io->cmd;
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 	in_isr = in_interrupt();
36741da177e4SLinus Torvalds 	if (in_isr) {
36751da177e4SLinus Torvalds 		dprintk((MYIOC_s_WARN_FMT "Internal SCSI IO request not allowed in ISR context!\n",
36761da177e4SLinus Torvalds        				hd->ioc->name));
36771da177e4SLinus Torvalds 		return -EPERM;
36781da177e4SLinus Torvalds 	}
36791da177e4SLinus Torvalds 
36801da177e4SLinus Torvalds 
36811da177e4SLinus Torvalds 	/* Set command specific information
36821da177e4SLinus Torvalds 	 */
36831da177e4SLinus Torvalds 	switch (cmd) {
36841da177e4SLinus Torvalds 	case INQUIRY:
36851da177e4SLinus Torvalds 		cmdLen = 6;
36861da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
36871da177e4SLinus Torvalds 		CDB[0] = cmd;
36881da177e4SLinus Torvalds 		CDB[4] = io->size;
36891da177e4SLinus Torvalds 		cmdTimeout = 10;
36901da177e4SLinus Torvalds 		break;
36911da177e4SLinus Torvalds 
36921da177e4SLinus Torvalds 	case TEST_UNIT_READY:
36931da177e4SLinus Torvalds 		cmdLen = 6;
36941da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
36951da177e4SLinus Torvalds 		cmdTimeout = 10;
36961da177e4SLinus Torvalds 		break;
36971da177e4SLinus Torvalds 
36981da177e4SLinus Torvalds 	case START_STOP:
36991da177e4SLinus Torvalds 		cmdLen = 6;
37001da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37011da177e4SLinus Torvalds 		CDB[0] = cmd;
37021da177e4SLinus Torvalds 		CDB[4] = 1;	/*Spin up the disk */
37031da177e4SLinus Torvalds 		cmdTimeout = 15;
37041da177e4SLinus Torvalds 		break;
37051da177e4SLinus Torvalds 
37061da177e4SLinus Torvalds 	case REQUEST_SENSE:
37071da177e4SLinus Torvalds 		cmdLen = 6;
37081da177e4SLinus Torvalds 		CDB[0] = cmd;
37091da177e4SLinus Torvalds 		CDB[4] = io->size;
37101da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37111da177e4SLinus Torvalds 		cmdTimeout = 10;
37121da177e4SLinus Torvalds 		break;
37131da177e4SLinus Torvalds 
37141da177e4SLinus Torvalds 	case READ_BUFFER:
37151da177e4SLinus Torvalds 		cmdLen = 10;
37161da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37171da177e4SLinus Torvalds 		CDB[0] = cmd;
37181da177e4SLinus Torvalds 		if (io->flags & MPT_ICFLAG_ECHO) {
37191da177e4SLinus Torvalds 			CDB[1] = 0x0A;
37201da177e4SLinus Torvalds 		} else {
37211da177e4SLinus Torvalds 			CDB[1] = 0x02;
37221da177e4SLinus Torvalds 		}
37231da177e4SLinus Torvalds 
37241da177e4SLinus Torvalds 		if (io->flags & MPT_ICFLAG_BUF_CAP) {
37251da177e4SLinus Torvalds 			CDB[1] |= 0x01;
37261da177e4SLinus Torvalds 		}
37271da177e4SLinus Torvalds 		CDB[6] = (io->size >> 16) & 0xFF;
37281da177e4SLinus Torvalds 		CDB[7] = (io->size >>  8) & 0xFF;
37291da177e4SLinus Torvalds 		CDB[8] = io->size & 0xFF;
37301da177e4SLinus Torvalds 		cmdTimeout = 10;
37311da177e4SLinus Torvalds 		break;
37321da177e4SLinus Torvalds 
37331da177e4SLinus Torvalds 	case WRITE_BUFFER:
37341da177e4SLinus Torvalds 		cmdLen = 10;
37351da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_WRITE;
37361da177e4SLinus Torvalds 		CDB[0] = cmd;
37371da177e4SLinus Torvalds 		if (io->flags & MPT_ICFLAG_ECHO) {
37381da177e4SLinus Torvalds 			CDB[1] = 0x0A;
37391da177e4SLinus Torvalds 		} else {
37401da177e4SLinus Torvalds 			CDB[1] = 0x02;
37411da177e4SLinus Torvalds 		}
37421da177e4SLinus Torvalds 		CDB[6] = (io->size >> 16) & 0xFF;
37431da177e4SLinus Torvalds 		CDB[7] = (io->size >>  8) & 0xFF;
37441da177e4SLinus Torvalds 		CDB[8] = io->size & 0xFF;
37451da177e4SLinus Torvalds 		cmdTimeout = 10;
37461da177e4SLinus Torvalds 		break;
37471da177e4SLinus Torvalds 
37481da177e4SLinus Torvalds 	case RESERVE:
37491da177e4SLinus Torvalds 		cmdLen = 6;
37501da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37511da177e4SLinus Torvalds 		CDB[0] = cmd;
37521da177e4SLinus Torvalds 		cmdTimeout = 10;
37531da177e4SLinus Torvalds 		break;
37541da177e4SLinus Torvalds 
37551da177e4SLinus Torvalds 	case RELEASE:
37561da177e4SLinus Torvalds 		cmdLen = 6;
37571da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37581da177e4SLinus Torvalds 		CDB[0] = cmd;
37591da177e4SLinus Torvalds 		cmdTimeout = 10;
37601da177e4SLinus Torvalds 		break;
37611da177e4SLinus Torvalds 
37621da177e4SLinus Torvalds 	case SYNCHRONIZE_CACHE:
37631da177e4SLinus Torvalds 		cmdLen = 10;
37641da177e4SLinus Torvalds 		dir = MPI_SCSIIO_CONTROL_READ;
37651da177e4SLinus Torvalds 		CDB[0] = cmd;
37661da177e4SLinus Torvalds //		CDB[1] = 0x02;	/* set immediate bit */
37671da177e4SLinus Torvalds 		cmdTimeout = 10;
37681da177e4SLinus Torvalds 		break;
37691da177e4SLinus Torvalds 
37701da177e4SLinus Torvalds 	default:
37711da177e4SLinus Torvalds 		/* Error Case */
37721da177e4SLinus Torvalds 		return -EFAULT;
37731da177e4SLinus Torvalds 	}
37741da177e4SLinus Torvalds 
37751da177e4SLinus Torvalds 	/* Get and Populate a free Frame
37761da177e4SLinus Torvalds 	 */
37770d0c7974SMoore, Eric Dean  	if ((mf = mpt_get_msg_frame(hd->ioc->InternalCtx, hd->ioc)) == NULL) {
37781da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_WARN_FMT "No msg frames!\n",
37791da177e4SLinus Torvalds 					hd->ioc->name));
37801da177e4SLinus Torvalds 		return -EBUSY;
37811da177e4SLinus Torvalds 	}
37821da177e4SLinus Torvalds 
37831da177e4SLinus Torvalds 	pScsiReq = (SCSIIORequest_t *) mf;
37841da177e4SLinus Torvalds 
37851da177e4SLinus Torvalds 	/* Get the request index */
37861da177e4SLinus Torvalds 	my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
37871da177e4SLinus Torvalds 	ADD_INDEX_LOG(my_idx); /* for debug */
37881da177e4SLinus Torvalds 
37891da177e4SLinus Torvalds 	if (io->flags & MPT_ICFLAG_PHYS_DISK) {
37901da177e4SLinus Torvalds 		pScsiReq->TargetID = io->physDiskNum;
37911da177e4SLinus Torvalds 		pScsiReq->Bus = 0;
37921da177e4SLinus Torvalds 		pScsiReq->ChainOffset = 0;
37931da177e4SLinus Torvalds 		pScsiReq->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
37941da177e4SLinus Torvalds 	} else {
37951da177e4SLinus Torvalds 		pScsiReq->TargetID = io->id;
37961da177e4SLinus Torvalds 		pScsiReq->Bus = io->bus;
37971da177e4SLinus Torvalds 		pScsiReq->ChainOffset = 0;
37981da177e4SLinus Torvalds 		pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
37991da177e4SLinus Torvalds 	}
38001da177e4SLinus Torvalds 
38011da177e4SLinus Torvalds 	pScsiReq->CDBLength = cmdLen;
38021da177e4SLinus Torvalds 	pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
38031da177e4SLinus Torvalds 
38041da177e4SLinus Torvalds 	pScsiReq->Reserved = 0;
38051da177e4SLinus Torvalds 
38061da177e4SLinus Torvalds 	pScsiReq->MsgFlags = mpt_msg_flags();
38071da177e4SLinus Torvalds 	/* MsgContext set in mpt_get_msg_fram call  */
38081da177e4SLinus Torvalds 
38091da177e4SLinus Torvalds 	for (ii=0; ii < 8; ii++)
38101da177e4SLinus Torvalds 		pScsiReq->LUN[ii] = 0;
38111da177e4SLinus Torvalds 	pScsiReq->LUN[1] = io->lun;
38121da177e4SLinus Torvalds 
38131da177e4SLinus Torvalds 	if (io->flags & MPT_ICFLAG_TAGGED_CMD)
38141da177e4SLinus Torvalds 		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_SIMPLEQ);
38151da177e4SLinus Torvalds 	else
38161da177e4SLinus Torvalds 		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
38171da177e4SLinus Torvalds 
38181da177e4SLinus Torvalds 	if (cmd == REQUEST_SENSE) {
38191da177e4SLinus Torvalds 		pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
38201da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_INFO_FMT "Untagged! 0x%2x\n",
38211da177e4SLinus Torvalds 			hd->ioc->name, cmd));
38221da177e4SLinus Torvalds 	}
38231da177e4SLinus Torvalds 
38241da177e4SLinus Torvalds 	for (ii=0; ii < 16; ii++)
38251da177e4SLinus Torvalds 		pScsiReq->CDB[ii] = CDB[ii];
38261da177e4SLinus Torvalds 
38271da177e4SLinus Torvalds 	pScsiReq->DataLength = cpu_to_le32(io->size);
38281da177e4SLinus Torvalds 	pScsiReq->SenseBufferLowAddr = cpu_to_le32(hd->ioc->sense_buf_low_dma
38291da177e4SLinus Torvalds 					   + (my_idx * MPT_SENSE_BUFFER_ALLOC));
38301da177e4SLinus Torvalds 
38311da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_INFO_FMT "Sending Command 0x%x for (%d:%d:%d)\n",
38321da177e4SLinus Torvalds 			hd->ioc->name, cmd, io->bus, io->id, io->lun));
38331da177e4SLinus Torvalds 
38341da177e4SLinus Torvalds 	if (dir == MPI_SCSIIO_CONTROL_READ) {
38351da177e4SLinus Torvalds 		mpt_add_sge((char *) &pScsiReq->SGL,
38361da177e4SLinus Torvalds 			MPT_SGE_FLAGS_SSIMPLE_READ | io->size,
38371da177e4SLinus Torvalds 			io->data_dma);
38381da177e4SLinus Torvalds 	} else {
38391da177e4SLinus Torvalds 		mpt_add_sge((char *) &pScsiReq->SGL,
38401da177e4SLinus Torvalds 			MPT_SGE_FLAGS_SSIMPLE_WRITE | io->size,
38411da177e4SLinus Torvalds 			io->data_dma);
38421da177e4SLinus Torvalds 	}
38431da177e4SLinus Torvalds 
38441da177e4SLinus Torvalds 	/* The ISR will free the request frame, but we need
38451da177e4SLinus Torvalds 	 * the information to initialize the target. Duplicate.
38461da177e4SLinus Torvalds 	 */
38471da177e4SLinus Torvalds 	memcpy(&ReqCopy, pScsiReq, sizeof(SCSIIORequest_t));
38481da177e4SLinus Torvalds 
38491da177e4SLinus Torvalds 	/* Issue this command after:
38501da177e4SLinus Torvalds 	 *	finish init
38511da177e4SLinus Torvalds 	 *	add timer
38521da177e4SLinus Torvalds 	 * Wait until the reply has been received
38531da177e4SLinus Torvalds 	 *  ScsiScanDvCtx callback function will
38541da177e4SLinus Torvalds 	 *	set hd->pLocal;
38551da177e4SLinus Torvalds 	 *	set scandv_wait_done and call wake_up
38561da177e4SLinus Torvalds 	 */
38571da177e4SLinus Torvalds 	hd->pLocal = NULL;
38581da177e4SLinus Torvalds 	hd->timer.expires = jiffies + HZ*cmdTimeout;
38590d0c7974SMoore, Eric Dean  	hd->scandv_wait_done = 0;
38601da177e4SLinus Torvalds 
38611da177e4SLinus Torvalds 	/* Save cmd pointer, for resource free if timeout or
38621da177e4SLinus Torvalds 	 * FW reload occurs
38631da177e4SLinus Torvalds 	 */
38641da177e4SLinus Torvalds 	hd->cmdPtr = mf;
38651da177e4SLinus Torvalds 
38661da177e4SLinus Torvalds 	add_timer(&hd->timer);
38670d0c7974SMoore, Eric Dean  	mpt_put_msg_frame(hd->ioc->InternalCtx, hd->ioc, mf);
38680d0c7974SMoore, Eric Dean  	wait_event(hd->scandv_waitq, hd->scandv_wait_done);
38691da177e4SLinus Torvalds 
38701da177e4SLinus Torvalds 	if (hd->pLocal) {
38711da177e4SLinus Torvalds 		rc = hd->pLocal->completion;
38721da177e4SLinus Torvalds 		hd->pLocal->skip = 0;
38731da177e4SLinus Torvalds 
38741da177e4SLinus Torvalds 		/* Always set fatal error codes in some cases.
38751da177e4SLinus Torvalds 		 */
38761da177e4SLinus Torvalds 		if (rc == MPT_SCANDV_SELECTION_TIMEOUT)
38771da177e4SLinus Torvalds 			rc = -ENXIO;
38781da177e4SLinus Torvalds 		else if (rc == MPT_SCANDV_SOME_ERROR)
38791da177e4SLinus Torvalds 			rc =  -rc;
38801da177e4SLinus Torvalds 	} else {
38811da177e4SLinus Torvalds 		rc = -EFAULT;
38821da177e4SLinus Torvalds 		/* This should never happen. */
38831da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_INFO_FMT "_do_cmd: Null pLocal!!!\n",
38841da177e4SLinus Torvalds 				hd->ioc->name));
38851da177e4SLinus Torvalds 	}
38861da177e4SLinus Torvalds 
38871da177e4SLinus Torvalds 	return rc;
38881da177e4SLinus Torvalds }
38891da177e4SLinus Torvalds 
38901da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
38911da177e4SLinus Torvalds /**
38921da177e4SLinus Torvalds  *	mptscsih_synchronize_cache - Send SYNCHRONIZE_CACHE to all disks.
38931da177e4SLinus Torvalds  *	@hd: Pointer to MPT_SCSI_HOST structure
38941da177e4SLinus Torvalds  *	@portnum: IOC port number
38951da177e4SLinus Torvalds  *
38961da177e4SLinus Torvalds  *	Uses the ISR, but with special processing.
38971da177e4SLinus Torvalds  *	MUST be single-threaded.
38981da177e4SLinus Torvalds  *
38991da177e4SLinus Torvalds  *	Return: 0 on completion
39001da177e4SLinus Torvalds  */
39011da177e4SLinus Torvalds static int
39021da177e4SLinus Torvalds mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, int portnum)
39031da177e4SLinus Torvalds {
39041da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc= hd->ioc;
39051da177e4SLinus Torvalds 	VirtDevice		*pTarget;
39061da177e4SLinus Torvalds 	SCSIDevicePage1_t	*pcfg1Data = NULL;
39071da177e4SLinus Torvalds 	INTERNAL_CMD		 iocmd;
39081da177e4SLinus Torvalds 	CONFIGPARMS		 cfg;
39091da177e4SLinus Torvalds 	dma_addr_t		 cfg1_dma_addr = -1;
39101da177e4SLinus Torvalds 	ConfigPageHeader_t	 header1;
39111da177e4SLinus Torvalds 	int			 bus = 0;
39121da177e4SLinus Torvalds 	int			 id = 0;
39131da177e4SLinus Torvalds 	int			 lun;
39141da177e4SLinus Torvalds 	int			 indexed_lun, lun_index;
39151da177e4SLinus Torvalds 	int			 hostId = ioc->pfacts[portnum].PortSCSIID;
39161da177e4SLinus Torvalds 	int			 max_id;
39171da177e4SLinus Torvalds 	int			 requested, configuration, data;
39181da177e4SLinus Torvalds 	int			 doConfig = 0;
39191da177e4SLinus Torvalds 	u8			 flags, factor;
39201da177e4SLinus Torvalds 
39211da177e4SLinus Torvalds 	max_id = ioc->sh->max_id - 1;
39221da177e4SLinus Torvalds 
39231da177e4SLinus Torvalds 	/* Following parameters will not change
39241da177e4SLinus Torvalds 	 * in this routine.
39251da177e4SLinus Torvalds 	 */
39261da177e4SLinus Torvalds 	iocmd.cmd = SYNCHRONIZE_CACHE;
39271da177e4SLinus Torvalds 	iocmd.flags = 0;
39281da177e4SLinus Torvalds 	iocmd.physDiskNum = -1;
39291da177e4SLinus Torvalds 	iocmd.data = NULL;
39301da177e4SLinus Torvalds 	iocmd.data_dma = -1;
39311da177e4SLinus Torvalds 	iocmd.size = 0;
39321da177e4SLinus Torvalds 	iocmd.rsvd = iocmd.rsvd2 = 0;
39331da177e4SLinus Torvalds 
39341da177e4SLinus Torvalds 	/* No SCSI hosts
39351da177e4SLinus Torvalds 	 */
39361da177e4SLinus Torvalds 	if (hd->Targets == NULL)
39371da177e4SLinus Torvalds 		return 0;
39381da177e4SLinus Torvalds 
39391da177e4SLinus Torvalds 	/* Skip the host
39401da177e4SLinus Torvalds 	 */
39411da177e4SLinus Torvalds 	if (id == hostId)
39421da177e4SLinus Torvalds 		id++;
39431da177e4SLinus Torvalds 
39441da177e4SLinus Torvalds 	/* Write SDP1 for all SCSI devices
39451da177e4SLinus Torvalds 	 * Alloc memory and set up config buffer
39461da177e4SLinus Torvalds 	 */
39471da177e4SLinus Torvalds 	if (ioc->bus_type == SCSI) {
39481da177e4SLinus Torvalds 		if (ioc->spi_data.sdp1length > 0) {
39491da177e4SLinus Torvalds 			pcfg1Data = (SCSIDevicePage1_t *)pci_alloc_consistent(ioc->pcidev,
39501da177e4SLinus Torvalds 					 ioc->spi_data.sdp1length * 4, &cfg1_dma_addr);
39511da177e4SLinus Torvalds 
39521da177e4SLinus Torvalds 			if (pcfg1Data != NULL) {
39531da177e4SLinus Torvalds 				doConfig = 1;
39541da177e4SLinus Torvalds 				header1.PageVersion = ioc->spi_data.sdp1version;
39551da177e4SLinus Torvalds 				header1.PageLength = ioc->spi_data.sdp1length;
39561da177e4SLinus Torvalds 				header1.PageNumber = 1;
39571da177e4SLinus Torvalds 				header1.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
395869218ee5SChristoph Hellwig 				cfg.cfghdr.hdr = &header1;
39591da177e4SLinus Torvalds 				cfg.physAddr = cfg1_dma_addr;
39601da177e4SLinus Torvalds 				cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
39611da177e4SLinus Torvalds 				cfg.dir = 1;
39621da177e4SLinus Torvalds 				cfg.timeout = 0;
39631da177e4SLinus Torvalds 			}
39641da177e4SLinus Torvalds 		}
39651da177e4SLinus Torvalds 	}
39661da177e4SLinus Torvalds 
39671da177e4SLinus Torvalds 	/* loop through all devices on this port
39681da177e4SLinus Torvalds 	 */
39691da177e4SLinus Torvalds 	while (bus < MPT_MAX_BUS) {
39701da177e4SLinus Torvalds 		iocmd.bus = bus;
39711da177e4SLinus Torvalds 		iocmd.id = id;
39721da177e4SLinus Torvalds 		pTarget = hd->Targets[(int)id];
39731da177e4SLinus Torvalds 
39741da177e4SLinus Torvalds 		if (doConfig) {
39751da177e4SLinus Torvalds 
39761da177e4SLinus Torvalds 			/* Set the negotiation flags */
39771da177e4SLinus Torvalds 			if (pTarget && (pTarget = hd->Targets[id]) && !pTarget->raidVolume) {
39781da177e4SLinus Torvalds 				flags = pTarget->negoFlags;
39791da177e4SLinus Torvalds 			} else {
39801da177e4SLinus Torvalds 				flags = hd->ioc->spi_data.noQas;
39811da177e4SLinus Torvalds 				if (hd->ioc->spi_data.nvram && (hd->ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
39821da177e4SLinus Torvalds 					data = hd->ioc->spi_data.nvram[id];
39831da177e4SLinus Torvalds 
39841da177e4SLinus Torvalds 					if (data & MPT_NVRAM_WIDE_DISABLE)
39851da177e4SLinus Torvalds 						flags |= MPT_TARGET_NO_NEGO_WIDE;
39861da177e4SLinus Torvalds 
39871da177e4SLinus Torvalds 					factor = (data & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
39881da177e4SLinus Torvalds 					if ((factor == 0) || (factor == MPT_ASYNC))
39891da177e4SLinus Torvalds 						flags |= MPT_TARGET_NO_NEGO_SYNC;
39901da177e4SLinus Torvalds 				}
39911da177e4SLinus Torvalds 			}
39921da177e4SLinus Torvalds 
39931da177e4SLinus Torvalds 			/* Force to async, narrow */
39941da177e4SLinus Torvalds 			mptscsih_setDevicePage1Flags(0, MPT_ASYNC, 0, &requested,
39951da177e4SLinus Torvalds 					&configuration, flags);
39961da177e4SLinus Torvalds 			dnegoprintk(("syncronize cache: id=%d width=0 factor=MPT_ASYNC "
39971da177e4SLinus Torvalds 				"offset=0 negoFlags=%x request=%x config=%x\n",
39981da177e4SLinus Torvalds 				id, flags, requested, configuration));
3999637fa99bSChristoph Hellwig 			pcfg1Data->RequestedParameters = cpu_to_le32(requested);
40001da177e4SLinus Torvalds 			pcfg1Data->Reserved = 0;
4001637fa99bSChristoph Hellwig 			pcfg1Data->Configuration = cpu_to_le32(configuration);
40021da177e4SLinus Torvalds 			cfg.pageAddr = (bus<<8) | id;
40031da177e4SLinus Torvalds 			mpt_config(hd->ioc, &cfg);
40041da177e4SLinus Torvalds 		}
40051da177e4SLinus Torvalds 
40061da177e4SLinus Torvalds 		/* If target Ptr NULL or if this target is NOT a disk, skip.
40071da177e4SLinus Torvalds 		 */
40081da177e4SLinus Torvalds 		if ((pTarget) && (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)){
40091da177e4SLinus Torvalds 			for (lun=0; lun <= MPT_LAST_LUN; lun++) {
40101da177e4SLinus Torvalds 				/* If LUN present, issue the command
40111da177e4SLinus Torvalds 				 */
40121da177e4SLinus Torvalds 				lun_index = (lun >> 5);  /* 32 luns per lun_index */
40131da177e4SLinus Torvalds 				indexed_lun = (lun % 32);
40141da177e4SLinus Torvalds 				if (pTarget->luns[lun_index] & (1<<indexed_lun)) {
40151da177e4SLinus Torvalds 					iocmd.lun = lun;
40161da177e4SLinus Torvalds 					(void) mptscsih_do_cmd(hd, &iocmd);
40171da177e4SLinus Torvalds 				}
40181da177e4SLinus Torvalds 			}
40191da177e4SLinus Torvalds 		}
40201da177e4SLinus Torvalds 
40211da177e4SLinus Torvalds 		/* get next relevant device */
40221da177e4SLinus Torvalds 		id++;
40231da177e4SLinus Torvalds 
40241da177e4SLinus Torvalds 		if (id == hostId)
40251da177e4SLinus Torvalds 			id++;
40261da177e4SLinus Torvalds 
40271da177e4SLinus Torvalds 		if (id > max_id) {
40281da177e4SLinus Torvalds 			id = 0;
40291da177e4SLinus Torvalds 			bus++;
40301da177e4SLinus Torvalds 		}
40311da177e4SLinus Torvalds 	}
40321da177e4SLinus Torvalds 
40331da177e4SLinus Torvalds 	if (pcfg1Data) {
40341da177e4SLinus Torvalds 		pci_free_consistent(ioc->pcidev, header1.PageLength * 4, pcfg1Data, cfg1_dma_addr);
40351da177e4SLinus Torvalds 	}
40361da177e4SLinus Torvalds 
40371da177e4SLinus Torvalds 	return 0;
40381da177e4SLinus Torvalds }
40391da177e4SLinus Torvalds 
40401da177e4SLinus Torvalds #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
40411da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
40421da177e4SLinus Torvalds /**
40431da177e4SLinus Torvalds  *	mptscsih_domainValidation - Top level handler for domain validation.
40441da177e4SLinus Torvalds  *	@hd: Pointer to MPT_SCSI_HOST structure.
40451da177e4SLinus Torvalds  *
40461da177e4SLinus Torvalds  *	Uses the ISR, but with special processing.
40471da177e4SLinus Torvalds  *	Called from schedule, should not be in interrupt mode.
40481da177e4SLinus Torvalds  *	While thread alive, do dv for all devices needing dv
40491da177e4SLinus Torvalds  *
40501da177e4SLinus Torvalds  *	Return: None.
40511da177e4SLinus Torvalds  */
40521da177e4SLinus Torvalds static void
40531da177e4SLinus Torvalds mptscsih_domainValidation(void *arg)
40541da177e4SLinus Torvalds {
40551da177e4SLinus Torvalds 	MPT_SCSI_HOST		*hd;
40561da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc;
40571da177e4SLinus Torvalds 	unsigned long		 flags;
40581da177e4SLinus Torvalds 	int 			 id, maxid, dvStatus, did;
40591da177e4SLinus Torvalds 	int			 ii, isPhysDisk;
40601da177e4SLinus Torvalds 
40611da177e4SLinus Torvalds 	spin_lock_irqsave(&dvtaskQ_lock, flags);
40621da177e4SLinus Torvalds 	dvtaskQ_active = 1;
40631da177e4SLinus Torvalds 	if (dvtaskQ_release) {
40641da177e4SLinus Torvalds 		dvtaskQ_active = 0;
40651da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dvtaskQ_lock, flags);
40661da177e4SLinus Torvalds 		return;
40671da177e4SLinus Torvalds 	}
40681da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dvtaskQ_lock, flags);
40691da177e4SLinus Torvalds 
40701da177e4SLinus Torvalds 	/* For this ioc, loop through all devices and do dv to each device.
40711da177e4SLinus Torvalds 	 * When complete with this ioc, search through the ioc list, and
40721da177e4SLinus Torvalds 	 * for each scsi ioc found, do dv for all devices. Exit when no
40731da177e4SLinus Torvalds 	 * device needs dv.
40741da177e4SLinus Torvalds 	 */
40751da177e4SLinus Torvalds 	did = 1;
40761da177e4SLinus Torvalds 	while (did) {
40771da177e4SLinus Torvalds 		did = 0;
40781da177e4SLinus Torvalds 		list_for_each_entry(ioc, &ioc_list, list) {
40791da177e4SLinus Torvalds 			spin_lock_irqsave(&dvtaskQ_lock, flags);
40801da177e4SLinus Torvalds 			if (dvtaskQ_release) {
40811da177e4SLinus Torvalds 				dvtaskQ_active = 0;
40821da177e4SLinus Torvalds 				spin_unlock_irqrestore(&dvtaskQ_lock, flags);
40831da177e4SLinus Torvalds 				return;
40841da177e4SLinus Torvalds 			}
40851da177e4SLinus Torvalds 			spin_unlock_irqrestore(&dvtaskQ_lock, flags);
40861da177e4SLinus Torvalds 
40871da177e4SLinus Torvalds 			msleep(250);
40881da177e4SLinus Torvalds 
40891da177e4SLinus Torvalds 			/* DV only to SCSI adapters */
40901da177e4SLinus Torvalds 			if (ioc->bus_type != SCSI)
40911da177e4SLinus Torvalds 				continue;
40921da177e4SLinus Torvalds 
40931da177e4SLinus Torvalds 			/* Make sure everything looks ok */
40941da177e4SLinus Torvalds 			if (ioc->sh == NULL)
40951da177e4SLinus Torvalds 				continue;
40961da177e4SLinus Torvalds 
40971da177e4SLinus Torvalds 			hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
40981da177e4SLinus Torvalds 			if (hd == NULL)
40991da177e4SLinus Torvalds 				continue;
41001da177e4SLinus Torvalds 
41011da177e4SLinus Torvalds 			if ((ioc->spi_data.forceDv & MPT_SCSICFG_RELOAD_IOC_PG3) != 0) {
41021da177e4SLinus Torvalds 				mpt_read_ioc_pg_3(ioc);
41031da177e4SLinus Torvalds 				if (ioc->spi_data.pIocPg3) {
41041da177e4SLinus Torvalds 					Ioc3PhysDisk_t *pPDisk = ioc->spi_data.pIocPg3->PhysDisk;
41051da177e4SLinus Torvalds 					int		numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;
41061da177e4SLinus Torvalds 
41071da177e4SLinus Torvalds 					while (numPDisk) {
41081da177e4SLinus Torvalds 						if (ioc->spi_data.dvStatus[pPDisk->PhysDiskID] & MPT_SCSICFG_DV_NOT_DONE)
41091da177e4SLinus Torvalds 							ioc->spi_data.dvStatus[pPDisk->PhysDiskID] |= MPT_SCSICFG_NEED_DV;
41101da177e4SLinus Torvalds 
41111da177e4SLinus Torvalds 						pPDisk++;
41121da177e4SLinus Torvalds 						numPDisk--;
41131da177e4SLinus Torvalds 					}
41141da177e4SLinus Torvalds 				}
41151da177e4SLinus Torvalds 				ioc->spi_data.forceDv &= ~MPT_SCSICFG_RELOAD_IOC_PG3;
41161da177e4SLinus Torvalds 			}
41171da177e4SLinus Torvalds 
41181da177e4SLinus Torvalds 			maxid = min_t(int, ioc->sh->max_id, MPT_MAX_SCSI_DEVICES);
41191da177e4SLinus Torvalds 
41201da177e4SLinus Torvalds 			for (id = 0; id < maxid; id++) {
41211da177e4SLinus Torvalds 				spin_lock_irqsave(&dvtaskQ_lock, flags);
41221da177e4SLinus Torvalds 				if (dvtaskQ_release) {
41231da177e4SLinus Torvalds 					dvtaskQ_active = 0;
41241da177e4SLinus Torvalds 					spin_unlock_irqrestore(&dvtaskQ_lock, flags);
41251da177e4SLinus Torvalds 					return;
41261da177e4SLinus Torvalds 				}
41271da177e4SLinus Torvalds 				spin_unlock_irqrestore(&dvtaskQ_lock, flags);
41281da177e4SLinus Torvalds 				dvStatus = hd->ioc->spi_data.dvStatus[id];
41291da177e4SLinus Torvalds 
41301da177e4SLinus Torvalds 				if (dvStatus & MPT_SCSICFG_NEED_DV) {
41311da177e4SLinus Torvalds 					did++;
41321da177e4SLinus Torvalds 					hd->ioc->spi_data.dvStatus[id] |= MPT_SCSICFG_DV_PENDING;
41331da177e4SLinus Torvalds 					hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_NEED_DV;
41341da177e4SLinus Torvalds 
41351da177e4SLinus Torvalds 					msleep(250);
41361da177e4SLinus Torvalds 
41371da177e4SLinus Torvalds 					/* If hidden phys disk, block IO's to all
41381da177e4SLinus Torvalds 					 *	raid volumes
41391da177e4SLinus Torvalds 					 * else, process normally
41401da177e4SLinus Torvalds 					 */
41411da177e4SLinus Torvalds 					isPhysDisk = mptscsih_is_phys_disk(ioc, id);
41421da177e4SLinus Torvalds 					if (isPhysDisk) {
41431da177e4SLinus Torvalds 						for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
41441da177e4SLinus Torvalds 							if (hd->ioc->spi_data.isRaid & (1 << ii)) {
41451da177e4SLinus Torvalds 								hd->ioc->spi_data.dvStatus[ii] |= MPT_SCSICFG_DV_PENDING;
41461da177e4SLinus Torvalds 							}
41471da177e4SLinus Torvalds 						}
41481da177e4SLinus Torvalds 					}
41491da177e4SLinus Torvalds 
41501da177e4SLinus Torvalds 					if (mptscsih_doDv(hd, 0, id) == 1) {
41511da177e4SLinus Torvalds 						/* Untagged device was busy, try again
41521da177e4SLinus Torvalds 						 */
41531da177e4SLinus Torvalds 						hd->ioc->spi_data.dvStatus[id] |= MPT_SCSICFG_NEED_DV;
41541da177e4SLinus Torvalds 						hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_DV_PENDING;
41551da177e4SLinus Torvalds 					} else {
41561da177e4SLinus Torvalds 						/* DV is complete. Clear flags.
41571da177e4SLinus Torvalds 						 */
41581da177e4SLinus Torvalds 						hd->ioc->spi_data.dvStatus[id] &= ~(MPT_SCSICFG_DV_NOT_DONE | MPT_SCSICFG_DV_PENDING);
41591da177e4SLinus Torvalds 					}
41601da177e4SLinus Torvalds 
41611da177e4SLinus Torvalds 					if (isPhysDisk) {
41621da177e4SLinus Torvalds 						for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
41631da177e4SLinus Torvalds 							if (hd->ioc->spi_data.isRaid & (1 << ii)) {
41641da177e4SLinus Torvalds 								hd->ioc->spi_data.dvStatus[ii] &= ~MPT_SCSICFG_DV_PENDING;
41651da177e4SLinus Torvalds 							}
41661da177e4SLinus Torvalds 						}
41671da177e4SLinus Torvalds 					}
41681da177e4SLinus Torvalds 
41691da177e4SLinus Torvalds 					if (hd->ioc->spi_data.noQas)
41701da177e4SLinus Torvalds 						mptscsih_qas_check(hd, id);
41711da177e4SLinus Torvalds 				}
41721da177e4SLinus Torvalds 			}
41731da177e4SLinus Torvalds 		}
41741da177e4SLinus Torvalds 	}
41751da177e4SLinus Torvalds 
41761da177e4SLinus Torvalds 	spin_lock_irqsave(&dvtaskQ_lock, flags);
41771da177e4SLinus Torvalds 	dvtaskQ_active = 0;
41781da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dvtaskQ_lock, flags);
41791da177e4SLinus Torvalds 
41801da177e4SLinus Torvalds 	return;
41811da177e4SLinus Torvalds }
41821da177e4SLinus Torvalds 
41831da177e4SLinus Torvalds /* Search IOC page 3 to determine if this is hidden physical disk
41841da177e4SLinus Torvalds  */
41850d0c7974SMoore, Eric Dean  static int
41860d0c7974SMoore, Eric Dean  mptscsih_is_phys_disk(MPT_ADAPTER *ioc, int id)
41871da177e4SLinus Torvalds {
41881da177e4SLinus Torvalds 	if (ioc->spi_data.pIocPg3) {
41891da177e4SLinus Torvalds 		Ioc3PhysDisk_t *pPDisk =  ioc->spi_data.pIocPg3->PhysDisk;
41901da177e4SLinus Torvalds 		int		numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;
41911da177e4SLinus Torvalds 
41921da177e4SLinus Torvalds 		while (numPDisk) {
41931da177e4SLinus Torvalds 			if (pPDisk->PhysDiskID == id) {
41941da177e4SLinus Torvalds 				return 1;
41951da177e4SLinus Torvalds 			}
41961da177e4SLinus Torvalds 			pPDisk++;
41971da177e4SLinus Torvalds 			numPDisk--;
41981da177e4SLinus Torvalds 		}
41991da177e4SLinus Torvalds 	}
42001da177e4SLinus Torvalds 	return 0;
42011da177e4SLinus Torvalds }
42021da177e4SLinus Torvalds 
42031da177e4SLinus Torvalds /* Write SDP1 if no QAS has been enabled
42041da177e4SLinus Torvalds  */
42050d0c7974SMoore, Eric Dean  static void
42060d0c7974SMoore, Eric Dean  mptscsih_qas_check(MPT_SCSI_HOST *hd, int id)
42071da177e4SLinus Torvalds {
42081da177e4SLinus Torvalds 	VirtDevice *pTarget;
42091da177e4SLinus Torvalds 	int ii;
42101da177e4SLinus Torvalds 
42111da177e4SLinus Torvalds 	if (hd->Targets == NULL)
42121da177e4SLinus Torvalds 		return;
42131da177e4SLinus Torvalds 
42141da177e4SLinus Torvalds 	for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
42151da177e4SLinus Torvalds 		if (ii == id)
42161da177e4SLinus Torvalds 			continue;
42171da177e4SLinus Torvalds 
42181da177e4SLinus Torvalds 		if ((hd->ioc->spi_data.dvStatus[ii] & MPT_SCSICFG_DV_NOT_DONE) != 0)
42191da177e4SLinus Torvalds 			continue;
42201da177e4SLinus Torvalds 
42211da177e4SLinus Torvalds 		pTarget = hd->Targets[ii];
42221da177e4SLinus Torvalds 
42231da177e4SLinus Torvalds 		if ((pTarget != NULL) && (!pTarget->raidVolume)) {
42241da177e4SLinus Torvalds 			if ((pTarget->negoFlags & hd->ioc->spi_data.noQas) == 0) {
42251da177e4SLinus Torvalds 				pTarget->negoFlags |= hd->ioc->spi_data.noQas;
42261da177e4SLinus Torvalds 				dnegoprintk(("writeSDP1: id=%d flags=0\n", id));
42271da177e4SLinus Torvalds 				mptscsih_writeSDP1(hd, 0, ii, 0);
42281da177e4SLinus Torvalds 			}
42291da177e4SLinus Torvalds 		} else {
42301da177e4SLinus Torvalds 			if (mptscsih_is_phys_disk(hd->ioc, ii) == 1) {
42311da177e4SLinus Torvalds 				dnegoprintk(("writeSDP1: id=%d SCSICFG_USE_NVRAM\n", id));
42321da177e4SLinus Torvalds 				mptscsih_writeSDP1(hd, 0, ii, MPT_SCSICFG_USE_NVRAM);
42331da177e4SLinus Torvalds 			}
42341da177e4SLinus Torvalds 		}
42351da177e4SLinus Torvalds 	}
42361da177e4SLinus Torvalds 	return;
42371da177e4SLinus Torvalds }
42381da177e4SLinus Torvalds 
42391da177e4SLinus Torvalds 
42401da177e4SLinus Torvalds 
42411da177e4SLinus Torvalds #define MPT_GET_NVRAM_VALS	0x01
42421da177e4SLinus Torvalds #define MPT_UPDATE_MAX		0x02
42431da177e4SLinus Torvalds #define MPT_SET_MAX		0x04
42441da177e4SLinus Torvalds #define MPT_SET_MIN		0x08
42451da177e4SLinus Torvalds #define MPT_FALLBACK		0x10
42461da177e4SLinus Torvalds #define MPT_SAVE		0x20
42471da177e4SLinus Torvalds 
42481da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
42491da177e4SLinus Torvalds /**
42501da177e4SLinus Torvalds  *	mptscsih_doDv - Perform domain validation to a target.
42511da177e4SLinus Torvalds  *	@hd: Pointer to MPT_SCSI_HOST structure.
42521da177e4SLinus Torvalds  *	@portnum: IOC port number.
42531da177e4SLinus Torvalds  *	@target: Physical ID of this target
42541da177e4SLinus Torvalds  *
42551da177e4SLinus Torvalds  *	Uses the ISR, but with special processing.
42561da177e4SLinus Torvalds  *	MUST be single-threaded.
42571da177e4SLinus Torvalds  *	Test will exit if target is at async & narrow.
42581da177e4SLinus Torvalds  *
42591da177e4SLinus Torvalds  *	Return: None.
42601da177e4SLinus Torvalds  */
42611da177e4SLinus Torvalds static int
42621da177e4SLinus Torvalds mptscsih_doDv(MPT_SCSI_HOST *hd, int bus_number, int id)
42631da177e4SLinus Torvalds {
42641da177e4SLinus Torvalds 	MPT_ADAPTER		*ioc = hd->ioc;
42651da177e4SLinus Torvalds 	VirtDevice		*pTarget;
42661da177e4SLinus Torvalds 	SCSIDevicePage1_t	*pcfg1Data;
42671da177e4SLinus Torvalds 	SCSIDevicePage0_t	*pcfg0Data;
42681da177e4SLinus Torvalds 	u8			*pbuf1;
42691da177e4SLinus Torvalds 	u8			*pbuf2;
42701da177e4SLinus Torvalds 	u8			*pDvBuf;
42711da177e4SLinus Torvalds 	dma_addr_t		 dvbuf_dma = -1;
42721da177e4SLinus Torvalds 	dma_addr_t		 buf1_dma = -1;
42731da177e4SLinus Torvalds 	dma_addr_t		 buf2_dma = -1;
42741da177e4SLinus Torvalds 	dma_addr_t		 cfg1_dma_addr = -1;
42751da177e4SLinus Torvalds 	dma_addr_t		 cfg0_dma_addr = -1;
42761da177e4SLinus Torvalds 	ConfigPageHeader_t	 header1;
42771da177e4SLinus Torvalds 	ConfigPageHeader_t	 header0;
42781da177e4SLinus Torvalds 	DVPARAMETERS		 dv;
42791da177e4SLinus Torvalds 	INTERNAL_CMD		 iocmd;
42801da177e4SLinus Torvalds 	CONFIGPARMS		 cfg;
42811da177e4SLinus Torvalds 	int			 dv_alloc = 0;
42821da177e4SLinus Torvalds 	int			 rc, sz = 0;
42831da177e4SLinus Torvalds 	int			 bufsize = 0;
42841da177e4SLinus Torvalds 	int			 dataBufSize = 0;
42851da177e4SLinus Torvalds 	int			 echoBufSize = 0;
42861da177e4SLinus Torvalds 	int			 notDone;
42871da177e4SLinus Torvalds 	int			 patt;
42881da177e4SLinus Torvalds 	int			 repeat;
42891da177e4SLinus Torvalds 	int			 retcode = 0;
42901da177e4SLinus Torvalds 	int			 nfactor =  MPT_ULTRA320;
42911da177e4SLinus Torvalds 	char			 firstPass = 1;
42921da177e4SLinus Torvalds 	char			 doFallback = 0;
42931da177e4SLinus Torvalds 	char			 readPage0;
42941da177e4SLinus Torvalds 	char			 bus, lun;
42951da177e4SLinus Torvalds 	char			 inq0 = 0;
42961da177e4SLinus Torvalds 
42971da177e4SLinus Torvalds 	if (ioc->spi_data.sdp1length == 0)
42981da177e4SLinus Torvalds 		return 0;
42991da177e4SLinus Torvalds 
43001da177e4SLinus Torvalds 	if (ioc->spi_data.sdp0length == 0)
43011da177e4SLinus Torvalds 		return 0;
43021da177e4SLinus Torvalds 
43031da177e4SLinus Torvalds 	/* If multiple buses are used, require that the initiator
43041da177e4SLinus Torvalds 	 * id be the same on all buses.
43051da177e4SLinus Torvalds 	 */
43061da177e4SLinus Torvalds 	if (id == ioc->pfacts[0].PortSCSIID)
43071da177e4SLinus Torvalds 		return 0;
43081da177e4SLinus Torvalds 
43091da177e4SLinus Torvalds 	lun = 0;
43101da177e4SLinus Torvalds 	bus = (u8) bus_number;
43111da177e4SLinus Torvalds 	ddvtprintk((MYIOC_s_NOTE_FMT
43121da177e4SLinus Torvalds 			"DV started: bus=%d, id=%d dv @ %p\n",
43131da177e4SLinus Torvalds 			ioc->name, bus, id, &dv));
43141da177e4SLinus Torvalds 
43151da177e4SLinus Torvalds 	/* Prep DV structure
43161da177e4SLinus Torvalds 	 */
43171da177e4SLinus Torvalds 	memset (&dv, 0, sizeof(DVPARAMETERS));
43181da177e4SLinus Torvalds 	dv.id = id;
43191da177e4SLinus Torvalds 
43201da177e4SLinus Torvalds 	/* Populate tmax with the current maximum
43211da177e4SLinus Torvalds 	 * transfer parameters for this target.
43221da177e4SLinus Torvalds 	 * Exit if narrow and async.
43231da177e4SLinus Torvalds 	 */
43241da177e4SLinus Torvalds 	dv.cmd = MPT_GET_NVRAM_VALS;
43251da177e4SLinus Torvalds 	mptscsih_dv_parms(hd, &dv, NULL);
43261da177e4SLinus Torvalds 
43271da177e4SLinus Torvalds 	/* Prep SCSI IO structure
43281da177e4SLinus Torvalds 	 */
43291da177e4SLinus Torvalds 	iocmd.id = id;
43301da177e4SLinus Torvalds 	iocmd.bus = bus;
43311da177e4SLinus Torvalds 	iocmd.lun = lun;
43321da177e4SLinus Torvalds 	iocmd.flags = 0;
43331da177e4SLinus Torvalds 	iocmd.physDiskNum = -1;
43341da177e4SLinus Torvalds 	iocmd.rsvd = iocmd.rsvd2 = 0;
43351da177e4SLinus Torvalds 
43361da177e4SLinus Torvalds 	pTarget = hd->Targets[id];
43371da177e4SLinus Torvalds 
43381da177e4SLinus Torvalds 	/* Use tagged commands if possible.
43391da177e4SLinus Torvalds 	 */
43401da177e4SLinus Torvalds 	if (pTarget) {
43411da177e4SLinus Torvalds 		if (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)
43421da177e4SLinus Torvalds 			iocmd.flags |= MPT_ICFLAG_TAGGED_CMD;
43431da177e4SLinus Torvalds 		else {
43441da177e4SLinus Torvalds 			if (hd->ioc->facts.FWVersion.Word < 0x01000600)
43451da177e4SLinus Torvalds 				return 0;
43461da177e4SLinus Torvalds 
43471da177e4SLinus Torvalds 			if ((hd->ioc->facts.FWVersion.Word >= 0x01010000) &&
43481da177e4SLinus Torvalds 				(hd->ioc->facts.FWVersion.Word < 0x01010B00))
43491da177e4SLinus Torvalds 				return 0;
43501da177e4SLinus Torvalds 		}
43511da177e4SLinus Torvalds 	}
43521da177e4SLinus Torvalds 
43531da177e4SLinus Torvalds 	/* Prep cfg structure
43541da177e4SLinus Torvalds 	 */
43551da177e4SLinus Torvalds 	cfg.pageAddr = (bus<<8) | id;
435669218ee5SChristoph Hellwig 	cfg.cfghdr.hdr = NULL;
43571da177e4SLinus Torvalds 
43581da177e4SLinus Torvalds 	/* Prep SDP0 header
43591da177e4SLinus Torvalds 	 */
43601da177e4SLinus Torvalds 	header0.PageVersion = ioc->spi_data.sdp0version;
43611da177e4SLinus Torvalds 	header0.PageLength = ioc->spi_data.sdp0length;
43621da177e4SLinus Torvalds 	header0.PageNumber = 0;
43631da177e4SLinus Torvalds 	header0.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
43641da177e4SLinus Torvalds 
43651da177e4SLinus Torvalds 	/* Prep SDP1 header
43661da177e4SLinus Torvalds 	 */
43671da177e4SLinus Torvalds 	header1.PageVersion = ioc->spi_data.sdp1version;
43681da177e4SLinus Torvalds 	header1.PageLength = ioc->spi_data.sdp1length;
43691da177e4SLinus Torvalds 	header1.PageNumber = 1;
43701da177e4SLinus Torvalds 	header1.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
43711da177e4SLinus Torvalds 
43721da177e4SLinus Torvalds 	if (header0.PageLength & 1)
43731da177e4SLinus Torvalds 		dv_alloc = (header0.PageLength * 4) + 4;
43741da177e4SLinus Torvalds 
43751da177e4SLinus Torvalds 	dv_alloc +=  (2048 + (header1.PageLength * 4));
43761da177e4SLinus Torvalds 
43771da177e4SLinus Torvalds 	pDvBuf = pci_alloc_consistent(ioc->pcidev, dv_alloc, &dvbuf_dma);
43781da177e4SLinus Torvalds 	if (pDvBuf == NULL)
43791da177e4SLinus Torvalds 		return 0;
43801da177e4SLinus Torvalds 
43811da177e4SLinus Torvalds 	sz = 0;
43821da177e4SLinus Torvalds 	pbuf1 = (u8 *)pDvBuf;
43831da177e4SLinus Torvalds 	buf1_dma = dvbuf_dma;
43841da177e4SLinus Torvalds 	sz +=1024;
43851da177e4SLinus Torvalds 
43861da177e4SLinus Torvalds 	pbuf2 = (u8 *) (pDvBuf + sz);
43871da177e4SLinus Torvalds 	buf2_dma = dvbuf_dma + sz;
43881da177e4SLinus Torvalds 	sz +=1024;
43891da177e4SLinus Torvalds 
43901da177e4SLinus Torvalds 	pcfg0Data = (SCSIDevicePage0_t *) (pDvBuf + sz);
43911da177e4SLinus Torvalds 	cfg0_dma_addr = dvbuf_dma + sz;
43921da177e4SLinus Torvalds 	sz += header0.PageLength * 4;
43931da177e4SLinus Torvalds 
43941da177e4SLinus Torvalds 	/* 8-byte alignment
43951da177e4SLinus Torvalds 	 */
43961da177e4SLinus Torvalds 	if (header0.PageLength & 1)
43971da177e4SLinus Torvalds 		sz += 4;
43981da177e4SLinus Torvalds 
43991da177e4SLinus Torvalds 	pcfg1Data = (SCSIDevicePage1_t *) (pDvBuf + sz);
44001da177e4SLinus Torvalds 	cfg1_dma_addr = dvbuf_dma + sz;
44011da177e4SLinus Torvalds 
440269218ee5SChristoph Hellwig 	/* Skip this ID? Set cfg.cfghdr.hdr to force config page write
44031da177e4SLinus Torvalds 	 */
44041da177e4SLinus Torvalds 	{
44051da177e4SLinus Torvalds 		ScsiCfgData *pspi_data = &hd->ioc->spi_data;
44061da177e4SLinus Torvalds 		if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
44071da177e4SLinus Torvalds 			/* Set the factor from nvram */
44081da177e4SLinus Torvalds 			nfactor = (pspi_data->nvram[id] & MPT_NVRAM_SYNC_MASK) >> 8;
44091da177e4SLinus Torvalds 			if (nfactor < pspi_data->minSyncFactor )
44101da177e4SLinus Torvalds 				nfactor = pspi_data->minSyncFactor;
44111da177e4SLinus Torvalds 
44121da177e4SLinus Torvalds 			if (!(pspi_data->nvram[id] & MPT_NVRAM_ID_SCAN_ENABLE) ||
44131da177e4SLinus Torvalds 				(pspi_data->PortFlags == MPI_SCSIPORTPAGE2_PORT_FLAGS_OFF_DV) ) {
44141da177e4SLinus Torvalds 
44151da177e4SLinus Torvalds 				ddvprintk((MYIOC_s_NOTE_FMT "DV Skipped: bus, id, lun (%d, %d, %d)\n",
44161da177e4SLinus Torvalds 					ioc->name, bus, id, lun));
44171da177e4SLinus Torvalds 
44181da177e4SLinus Torvalds 				dv.cmd = MPT_SET_MAX;
44191da177e4SLinus Torvalds 				mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
442069218ee5SChristoph Hellwig 				cfg.cfghdr.hdr = &header1;
44211da177e4SLinus Torvalds 
44221da177e4SLinus Torvalds 				/* Save the final negotiated settings to
44231da177e4SLinus Torvalds 				 * SCSI device page 1.
44241da177e4SLinus Torvalds 				 */
44251da177e4SLinus Torvalds 				cfg.physAddr = cfg1_dma_addr;
44261da177e4SLinus Torvalds 				cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
44271da177e4SLinus Torvalds 				cfg.dir = 1;
44281da177e4SLinus Torvalds 				mpt_config(hd->ioc, &cfg);
44291da177e4SLinus Torvalds 				goto target_done;
44301da177e4SLinus Torvalds 			}
44311da177e4SLinus Torvalds 		}
44321da177e4SLinus Torvalds 	}
44331da177e4SLinus Torvalds 
44341da177e4SLinus Torvalds 	/* Finish iocmd inititialization - hidden or visible disk? */
44351da177e4SLinus Torvalds 	if (ioc->spi_data.pIocPg3) {
44361da177e4SLinus Torvalds 		/* Search IOC page 3 for matching id
44371da177e4SLinus Torvalds 		 */
44381da177e4SLinus Torvalds 		Ioc3PhysDisk_t *pPDisk =  ioc->spi_data.pIocPg3->PhysDisk;
44391da177e4SLinus Torvalds 		int		numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;
44401da177e4SLinus Torvalds 
44411da177e4SLinus Torvalds 		while (numPDisk) {
44421da177e4SLinus Torvalds 			if (pPDisk->PhysDiskID == id) {
44431da177e4SLinus Torvalds 				/* match */
44441da177e4SLinus Torvalds 				iocmd.flags |= MPT_ICFLAG_PHYS_DISK;
44451da177e4SLinus Torvalds 				iocmd.physDiskNum = pPDisk->PhysDiskNum;
44461da177e4SLinus Torvalds 
44471da177e4SLinus Torvalds 				/* Quiesce the IM
44481da177e4SLinus Torvalds 				 */
44491da177e4SLinus Torvalds 				if (mptscsih_do_raid(hd, MPI_RAID_ACTION_QUIESCE_PHYS_IO, &iocmd) < 0) {
44501da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_ERR_FMT "RAID Queisce FAILED!\n", ioc->name));
44511da177e4SLinus Torvalds 					goto target_done;
44521da177e4SLinus Torvalds 				}
44531da177e4SLinus Torvalds 				break;
44541da177e4SLinus Torvalds 			}
44551da177e4SLinus Torvalds 			pPDisk++;
44561da177e4SLinus Torvalds 			numPDisk--;
44571da177e4SLinus Torvalds 		}
44581da177e4SLinus Torvalds 	}
44591da177e4SLinus Torvalds 
44601da177e4SLinus Torvalds 	/* RAID Volume ID's may double for a physical device. If RAID but
44611da177e4SLinus Torvalds 	 * not a physical ID as well, skip DV.
44621da177e4SLinus Torvalds 	 */
44631da177e4SLinus Torvalds 	if ((hd->ioc->spi_data.isRaid & (1 << id)) && !(iocmd.flags & MPT_ICFLAG_PHYS_DISK))
44641da177e4SLinus Torvalds 		goto target_done;
44651da177e4SLinus Torvalds 
44661da177e4SLinus Torvalds 
44671da177e4SLinus Torvalds 	/* Basic Test.
44681da177e4SLinus Torvalds 	 * Async & Narrow - Inquiry
44691da177e4SLinus Torvalds 	 * Async & Narrow - Inquiry
44701da177e4SLinus Torvalds 	 * Maximum transfer rate - Inquiry
44711da177e4SLinus Torvalds 	 * Compare buffers:
44721da177e4SLinus Torvalds 	 *	If compare, test complete.
44731da177e4SLinus Torvalds 	 *	If miscompare and first pass, repeat
44741da177e4SLinus Torvalds 	 *	If miscompare and not first pass, fall back and repeat
44751da177e4SLinus Torvalds 	 */
44761da177e4SLinus Torvalds 	hd->pLocal = NULL;
44771da177e4SLinus Torvalds 	readPage0 = 0;
44781da177e4SLinus Torvalds 	sz = SCSI_MAX_INQUIRY_BYTES;
44791da177e4SLinus Torvalds 	rc = MPT_SCANDV_GOOD;
44801da177e4SLinus Torvalds 	while (1) {
44811da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT "DV: Start Basic test on id=%d\n", ioc->name, id));
44821da177e4SLinus Torvalds 		retcode = 0;
44831da177e4SLinus Torvalds 		dv.cmd = MPT_SET_MIN;
44841da177e4SLinus Torvalds 		mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
44851da177e4SLinus Torvalds 
448669218ee5SChristoph Hellwig 		cfg.cfghdr.hdr = &header1;
44871da177e4SLinus Torvalds 		cfg.physAddr = cfg1_dma_addr;
44881da177e4SLinus Torvalds 		cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
44891da177e4SLinus Torvalds 		cfg.dir = 1;
44901da177e4SLinus Torvalds 		if (mpt_config(hd->ioc, &cfg) != 0)
44911da177e4SLinus Torvalds 			goto target_done;
44921da177e4SLinus Torvalds 
44931da177e4SLinus Torvalds 		/* Wide - narrow - wide workaround case
44941da177e4SLinus Torvalds 		 */
44951da177e4SLinus Torvalds 		if ((rc == MPT_SCANDV_ISSUE_SENSE) && dv.max.width) {
44961da177e4SLinus Torvalds 			/* Send an untagged command to reset disk Qs corrupted
44971da177e4SLinus Torvalds 			 * when a parity error occurs on a Request Sense.
44981da177e4SLinus Torvalds 			 */
44991da177e4SLinus Torvalds 			if ((hd->ioc->facts.FWVersion.Word >= 0x01000600) ||
45001da177e4SLinus Torvalds 				((hd->ioc->facts.FWVersion.Word >= 0x01010000) &&
45011da177e4SLinus Torvalds 				(hd->ioc->facts.FWVersion.Word < 0x01010B00)) ) {
45021da177e4SLinus Torvalds 
45031da177e4SLinus Torvalds 				iocmd.cmd = REQUEST_SENSE;
45041da177e4SLinus Torvalds 				iocmd.data_dma = buf1_dma;
45051da177e4SLinus Torvalds 				iocmd.data = pbuf1;
45061da177e4SLinus Torvalds 				iocmd.size = 0x12;
45071da177e4SLinus Torvalds 				if (mptscsih_do_cmd(hd, &iocmd) < 0)
45081da177e4SLinus Torvalds 					goto target_done;
45091da177e4SLinus Torvalds 				else {
45101da177e4SLinus Torvalds 					if (hd->pLocal == NULL)
45111da177e4SLinus Torvalds 						goto target_done;
45121da177e4SLinus Torvalds 					rc = hd->pLocal->completion;
45131da177e4SLinus Torvalds 					if ((rc == MPT_SCANDV_GOOD) || (rc == MPT_SCANDV_SENSE)) {
45141da177e4SLinus Torvalds 						dv.max.width = 0;
45151da177e4SLinus Torvalds 						doFallback = 0;
45161da177e4SLinus Torvalds 					} else
45171da177e4SLinus Torvalds 						goto target_done;
45181da177e4SLinus Torvalds 				}
45191da177e4SLinus Torvalds 			} else
45201da177e4SLinus Torvalds 				goto target_done;
45211da177e4SLinus Torvalds 		}
45221da177e4SLinus Torvalds 
45231da177e4SLinus Torvalds 		iocmd.cmd = INQUIRY;
45241da177e4SLinus Torvalds 		iocmd.data_dma = buf1_dma;
45251da177e4SLinus Torvalds 		iocmd.data = pbuf1;
45261da177e4SLinus Torvalds 		iocmd.size = sz;
45271da177e4SLinus Torvalds 		memset(pbuf1, 0x00, sz);
45281da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
45291da177e4SLinus Torvalds 			goto target_done;
45301da177e4SLinus Torvalds 		else {
45311da177e4SLinus Torvalds 			if (hd->pLocal == NULL)
45321da177e4SLinus Torvalds 				goto target_done;
45331da177e4SLinus Torvalds 			rc = hd->pLocal->completion;
45341da177e4SLinus Torvalds 			if (rc == MPT_SCANDV_GOOD) {
45351da177e4SLinus Torvalds 				if (hd->pLocal->scsiStatus == SAM_STAT_BUSY) {
45361da177e4SLinus Torvalds 					if ((iocmd.flags & MPT_ICFLAG_TAGGED_CMD) == 0)
45371da177e4SLinus Torvalds 						retcode = 1;
45381da177e4SLinus Torvalds 					else
45391da177e4SLinus Torvalds 						retcode = 0;
45401da177e4SLinus Torvalds 
45411da177e4SLinus Torvalds 					goto target_done;
45421da177e4SLinus Torvalds 				}
45431da177e4SLinus Torvalds 			} else if  (rc == MPT_SCANDV_SENSE) {
45441da177e4SLinus Torvalds 				;
45451da177e4SLinus Torvalds 			} else {
45461da177e4SLinus Torvalds 				/* If first command doesn't complete
45471da177e4SLinus Torvalds 				 * with a good status or with a check condition,
45481da177e4SLinus Torvalds 				 * exit.
45491da177e4SLinus Torvalds 				 */
45501da177e4SLinus Torvalds 				goto target_done;
45511da177e4SLinus Torvalds 			}
45521da177e4SLinus Torvalds 		}
45531da177e4SLinus Torvalds 
45541da177e4SLinus Torvalds 		/* Reset the size for disks
45551da177e4SLinus Torvalds 		 */
45561da177e4SLinus Torvalds 		inq0 = (*pbuf1) & 0x1F;
45571da177e4SLinus Torvalds 		if ((inq0 == 0) && pTarget && !pTarget->raidVolume) {
45581da177e4SLinus Torvalds 			sz = 0x40;
45591da177e4SLinus Torvalds 			iocmd.size = sz;
45601da177e4SLinus Torvalds 		}
45611da177e4SLinus Torvalds 
45621da177e4SLinus Torvalds 		/* Another GEM workaround. Check peripheral device type,
45631da177e4SLinus Torvalds 		 * if PROCESSOR, quit DV.
45641da177e4SLinus Torvalds 		 */
45651da177e4SLinus Torvalds 		if (inq0 == TYPE_PROCESSOR) {
45661da177e4SLinus Torvalds 			mptscsih_initTarget(hd,
45671da177e4SLinus Torvalds 				bus,
45681da177e4SLinus Torvalds 				id,
45691da177e4SLinus Torvalds 				lun,
45701da177e4SLinus Torvalds 				pbuf1,
45711da177e4SLinus Torvalds 				sz);
45721da177e4SLinus Torvalds 			goto target_done;
45731da177e4SLinus Torvalds 		}
45741da177e4SLinus Torvalds 
45751da177e4SLinus Torvalds 		if (inq0 > 0x08)
45761da177e4SLinus Torvalds 			goto target_done;
45771da177e4SLinus Torvalds 
45781da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
45791da177e4SLinus Torvalds 			goto target_done;
45801da177e4SLinus Torvalds 
45811da177e4SLinus Torvalds 		if (sz == 0x40) {
45821da177e4SLinus Torvalds 			if ((pTarget->maxWidth == 1) && (pTarget->maxOffset) && (nfactor < 0x0A)
45831da177e4SLinus Torvalds 				&& (pTarget->minSyncFactor > 0x09)) {
45841da177e4SLinus Torvalds 				if ((pbuf1[56] & 0x04) == 0)
45851da177e4SLinus Torvalds 					;
45861da177e4SLinus Torvalds 				else if ((pbuf1[56] & 0x01) == 1) {
45871da177e4SLinus Torvalds 					pTarget->minSyncFactor =
45881da177e4SLinus Torvalds 					    nfactor > MPT_ULTRA320 ? nfactor : MPT_ULTRA320;
45891da177e4SLinus Torvalds 				} else {
45901da177e4SLinus Torvalds 					pTarget->minSyncFactor =
45911da177e4SLinus Torvalds 					    nfactor > MPT_ULTRA160 ? nfactor : MPT_ULTRA160;
45921da177e4SLinus Torvalds 				}
45931da177e4SLinus Torvalds 
45941da177e4SLinus Torvalds 				dv.max.factor = pTarget->minSyncFactor;
45951da177e4SLinus Torvalds 
45961da177e4SLinus Torvalds 				if ((pbuf1[56] & 0x02) == 0) {
45971da177e4SLinus Torvalds 					pTarget->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
45981da177e4SLinus Torvalds 					hd->ioc->spi_data.noQas = MPT_TARGET_NO_NEGO_QAS;
45991da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_NOTE_FMT
46001da177e4SLinus Torvalds 					    "DV: Start Basic noQas on id=%d due to pbuf1[56]=%x\n",
46011da177e4SLinus Torvalds 					    ioc->name, id, pbuf1[56]));
46021da177e4SLinus Torvalds 				}
46031da177e4SLinus Torvalds 			}
46041da177e4SLinus Torvalds 		}
46051da177e4SLinus Torvalds 
46061da177e4SLinus Torvalds 		if (doFallback)
46071da177e4SLinus Torvalds 			dv.cmd = MPT_FALLBACK;
46081da177e4SLinus Torvalds 		else
46091da177e4SLinus Torvalds 			dv.cmd = MPT_SET_MAX;
46101da177e4SLinus Torvalds 
46111da177e4SLinus Torvalds 		mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
46121da177e4SLinus Torvalds 		if (mpt_config(hd->ioc, &cfg) != 0)
46131da177e4SLinus Torvalds 			goto target_done;
46141da177e4SLinus Torvalds 
46151da177e4SLinus Torvalds 		if ((!dv.now.width) && (!dv.now.offset))
46161da177e4SLinus Torvalds 			goto target_done;
46171da177e4SLinus Torvalds 
46181da177e4SLinus Torvalds 		iocmd.cmd = INQUIRY;
46191da177e4SLinus Torvalds 		iocmd.data_dma = buf2_dma;
46201da177e4SLinus Torvalds 		iocmd.data = pbuf2;
46211da177e4SLinus Torvalds 		iocmd.size = sz;
46221da177e4SLinus Torvalds 		memset(pbuf2, 0x00, sz);
46231da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
46241da177e4SLinus Torvalds 			goto target_done;
46251da177e4SLinus Torvalds 		else if (hd->pLocal == NULL)
46261da177e4SLinus Torvalds 			goto target_done;
46271da177e4SLinus Torvalds 		else {
46281da177e4SLinus Torvalds 			/* Save the return code.
46291da177e4SLinus Torvalds 			 * If this is the first pass,
46301da177e4SLinus Torvalds 			 * read SCSI Device Page 0
46311da177e4SLinus Torvalds 			 * and update the target max parameters.
46321da177e4SLinus Torvalds 			 */
46331da177e4SLinus Torvalds 			rc = hd->pLocal->completion;
46341da177e4SLinus Torvalds 			doFallback = 0;
46351da177e4SLinus Torvalds 			if (rc == MPT_SCANDV_GOOD) {
46361da177e4SLinus Torvalds 				if (!readPage0) {
46371da177e4SLinus Torvalds 					u32 sdp0_info;
46381da177e4SLinus Torvalds 					u32 sdp0_nego;
46391da177e4SLinus Torvalds 
464069218ee5SChristoph Hellwig 					cfg.cfghdr.hdr = &header0;
46411da177e4SLinus Torvalds 					cfg.physAddr = cfg0_dma_addr;
46421da177e4SLinus Torvalds 					cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
46431da177e4SLinus Torvalds 					cfg.dir = 0;
46441da177e4SLinus Torvalds 
46451da177e4SLinus Torvalds 					if (mpt_config(hd->ioc, &cfg) != 0)
46461da177e4SLinus Torvalds 						goto target_done;
46471da177e4SLinus Torvalds 
46481da177e4SLinus Torvalds 					sdp0_info = le32_to_cpu(pcfg0Data->Information) & 0x0E;
46491da177e4SLinus Torvalds 					sdp0_nego = (le32_to_cpu(pcfg0Data->NegotiatedParameters) & 0xFF00 ) >> 8;
46501da177e4SLinus Torvalds 
46511da177e4SLinus Torvalds 					/* Quantum and Fujitsu workarounds.
46521da177e4SLinus Torvalds 					 * Quantum: PPR U320 -> PPR reply with Ultra2 and wide
46531da177e4SLinus Torvalds 					 * Fujitsu: PPR U320 -> Msg Reject and Ultra2 and wide
46541da177e4SLinus Torvalds 					 * Resetart with a request for U160.
46551da177e4SLinus Torvalds 					 */
46561da177e4SLinus Torvalds 					if ((dv.now.factor == MPT_ULTRA320) && (sdp0_nego == MPT_ULTRA2)) {
46571da177e4SLinus Torvalds 							doFallback = 1;
46581da177e4SLinus Torvalds 					} else {
46591da177e4SLinus Torvalds 						dv.cmd = MPT_UPDATE_MAX;
46601da177e4SLinus Torvalds 						mptscsih_dv_parms(hd, &dv, (void *)pcfg0Data);
46611da177e4SLinus Torvalds 						/* Update the SCSI device page 1 area
46621da177e4SLinus Torvalds 						 */
46631da177e4SLinus Torvalds 						pcfg1Data->RequestedParameters = pcfg0Data->NegotiatedParameters;
46641da177e4SLinus Torvalds 						readPage0 = 1;
46651da177e4SLinus Torvalds 					}
46661da177e4SLinus Torvalds 				}
46671da177e4SLinus Torvalds 
46681da177e4SLinus Torvalds 				/* Quantum workaround. Restart this test will the fallback
46691da177e4SLinus Torvalds 				 * flag set.
46701da177e4SLinus Torvalds 				 */
46711da177e4SLinus Torvalds 				if (doFallback == 0) {
46721da177e4SLinus Torvalds 					if (memcmp(pbuf1, pbuf2, sz) != 0) {
46731da177e4SLinus Torvalds 						if (!firstPass)
46741da177e4SLinus Torvalds 							doFallback = 1;
46751da177e4SLinus Torvalds 					} else {
46761da177e4SLinus Torvalds 						ddvprintk((MYIOC_s_NOTE_FMT
46771da177e4SLinus Torvalds 						    "DV:Inquiry compared id=%d, calling initTarget\n", ioc->name, id));
46781da177e4SLinus Torvalds 						hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_DV_NOT_DONE;
46791da177e4SLinus Torvalds 						mptscsih_initTarget(hd,
46801da177e4SLinus Torvalds 							bus,
46811da177e4SLinus Torvalds 							id,
46821da177e4SLinus Torvalds 							lun,
46831da177e4SLinus Torvalds 							pbuf1,
46841da177e4SLinus Torvalds 							sz);
46851da177e4SLinus Torvalds 						break;	/* test complete */
46861da177e4SLinus Torvalds 					}
46871da177e4SLinus Torvalds 				}
46881da177e4SLinus Torvalds 
46891da177e4SLinus Torvalds 
46901da177e4SLinus Torvalds 			} else if (rc == MPT_SCANDV_ISSUE_SENSE)
46911da177e4SLinus Torvalds 				doFallback = 1;	/* set fallback flag */
46921da177e4SLinus Torvalds 			else if ((rc == MPT_SCANDV_DID_RESET) ||
46931da177e4SLinus Torvalds 				 (rc == MPT_SCANDV_SENSE) ||
46941da177e4SLinus Torvalds 				 (rc == MPT_SCANDV_FALLBACK))
46951da177e4SLinus Torvalds 				doFallback = 1;	/* set fallback flag */
46961da177e4SLinus Torvalds 			else
46971da177e4SLinus Torvalds 				goto target_done;
46981da177e4SLinus Torvalds 
46991da177e4SLinus Torvalds 			firstPass = 0;
47001da177e4SLinus Torvalds 		}
47011da177e4SLinus Torvalds 	}
47021da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_NOTE_FMT "DV: Basic test on id=%d completed OK.\n", ioc->name, id));
47031da177e4SLinus Torvalds 
47040d0c7974SMoore, Eric Dean  	if (ioc->spi_data.mpt_dv == 0)
47051da177e4SLinus Torvalds 		goto target_done;
47061da177e4SLinus Torvalds 
47071da177e4SLinus Torvalds 	inq0 = (*pbuf1) & 0x1F;
47081da177e4SLinus Torvalds 
47091da177e4SLinus Torvalds 	/* Continue only for disks
47101da177e4SLinus Torvalds 	 */
47111da177e4SLinus Torvalds 	if (inq0 != 0)
47121da177e4SLinus Torvalds 		goto target_done;
47131da177e4SLinus Torvalds 
47141da177e4SLinus Torvalds 	if ( ioc->spi_data.PortFlags == MPI_SCSIPORTPAGE2_PORT_FLAGS_BASIC_DV_ONLY )
47151da177e4SLinus Torvalds 		goto target_done;
47161da177e4SLinus Torvalds 
47171da177e4SLinus Torvalds 	/* Start the Enhanced Test.
47181da177e4SLinus Torvalds 	 * 0) issue TUR to clear out check conditions
47191da177e4SLinus Torvalds 	 * 1) read capacity of echo (regular) buffer
47201da177e4SLinus Torvalds 	 * 2) reserve device
47211da177e4SLinus Torvalds 	 * 3) do write-read-compare data pattern test
47221da177e4SLinus Torvalds 	 * 4) release
47231da177e4SLinus Torvalds 	 * 5) update nego parms to target struct
47241da177e4SLinus Torvalds 	 */
472569218ee5SChristoph Hellwig 	cfg.cfghdr.hdr = &header1;
47261da177e4SLinus Torvalds 	cfg.physAddr = cfg1_dma_addr;
47271da177e4SLinus Torvalds 	cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
47281da177e4SLinus Torvalds 	cfg.dir = 1;
47291da177e4SLinus Torvalds 
47301da177e4SLinus Torvalds 	iocmd.cmd = TEST_UNIT_READY;
47311da177e4SLinus Torvalds 	iocmd.data_dma = -1;
47321da177e4SLinus Torvalds 	iocmd.data = NULL;
47331da177e4SLinus Torvalds 	iocmd.size = 0;
47341da177e4SLinus Torvalds 	notDone = 1;
47351da177e4SLinus Torvalds 	while (notDone) {
47361da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
47371da177e4SLinus Torvalds 			goto target_done;
47381da177e4SLinus Torvalds 
47391da177e4SLinus Torvalds 		if (hd->pLocal == NULL)
47401da177e4SLinus Torvalds 			goto target_done;
47411da177e4SLinus Torvalds 
47421da177e4SLinus Torvalds 		rc = hd->pLocal->completion;
47431da177e4SLinus Torvalds 		if (rc == MPT_SCANDV_GOOD)
47441da177e4SLinus Torvalds 			notDone = 0;
47451da177e4SLinus Torvalds 		else if (rc == MPT_SCANDV_SENSE) {
47461da177e4SLinus Torvalds 			u8 skey = hd->pLocal->sense[2] & 0x0F;
47471da177e4SLinus Torvalds 			u8 asc = hd->pLocal->sense[12];
47481da177e4SLinus Torvalds 			u8 ascq = hd->pLocal->sense[13];
47491da177e4SLinus Torvalds 			ddvprintk((MYIOC_s_INFO_FMT
47501da177e4SLinus Torvalds 				"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
47511da177e4SLinus Torvalds 				ioc->name, skey, asc, ascq));
47521da177e4SLinus Torvalds 
47531da177e4SLinus Torvalds 			if (skey == UNIT_ATTENTION)
47541da177e4SLinus Torvalds 				notDone++; /* repeat */
47551da177e4SLinus Torvalds 			else if ((skey == NOT_READY) &&
47561da177e4SLinus Torvalds 					(asc == 0x04)&&(ascq == 0x01)) {
47571da177e4SLinus Torvalds 				/* wait then repeat */
47581da177e4SLinus Torvalds 				mdelay (2000);
47591da177e4SLinus Torvalds 				notDone++;
47601da177e4SLinus Torvalds 			} else if ((skey == NOT_READY) && (asc == 0x3A)) {
47611da177e4SLinus Torvalds 				/* no medium, try read test anyway */
47621da177e4SLinus Torvalds 				notDone = 0;
47631da177e4SLinus Torvalds 			} else {
47641da177e4SLinus Torvalds 				/* All other errors are fatal.
47651da177e4SLinus Torvalds 				 */
47661da177e4SLinus Torvalds 				ddvprintk((MYIOC_s_INFO_FMT "DV: fatal error.",
47671da177e4SLinus Torvalds 						ioc->name));
47681da177e4SLinus Torvalds 				goto target_done;
47691da177e4SLinus Torvalds 			}
47701da177e4SLinus Torvalds 		} else
47711da177e4SLinus Torvalds 			goto target_done;
47721da177e4SLinus Torvalds 	}
47731da177e4SLinus Torvalds 
47741da177e4SLinus Torvalds 	iocmd.cmd = READ_BUFFER;
47751da177e4SLinus Torvalds 	iocmd.data_dma = buf1_dma;
47761da177e4SLinus Torvalds 	iocmd.data = pbuf1;
47771da177e4SLinus Torvalds 	iocmd.size = 4;
47781da177e4SLinus Torvalds 	iocmd.flags |= MPT_ICFLAG_BUF_CAP;
47791da177e4SLinus Torvalds 
47801da177e4SLinus Torvalds 	dataBufSize = 0;
47811da177e4SLinus Torvalds 	echoBufSize = 0;
47821da177e4SLinus Torvalds 	for (patt = 0; patt < 2; patt++) {
47831da177e4SLinus Torvalds 		if (patt == 0)
47841da177e4SLinus Torvalds 			iocmd.flags |= MPT_ICFLAG_ECHO;
47851da177e4SLinus Torvalds 		else
47861da177e4SLinus Torvalds 			iocmd.flags &= ~MPT_ICFLAG_ECHO;
47871da177e4SLinus Torvalds 
47881da177e4SLinus Torvalds 		notDone = 1;
47891da177e4SLinus Torvalds 		while (notDone) {
47901da177e4SLinus Torvalds 			bufsize = 0;
47911da177e4SLinus Torvalds 
47921da177e4SLinus Torvalds 			/* If not ready after 8 trials,
47931da177e4SLinus Torvalds 			 * give up on this device.
47941da177e4SLinus Torvalds 			 */
47951da177e4SLinus Torvalds 			if (notDone > 8)
47961da177e4SLinus Torvalds 				goto target_done;
47971da177e4SLinus Torvalds 
47981da177e4SLinus Torvalds 			if (mptscsih_do_cmd(hd, &iocmd) < 0)
47991da177e4SLinus Torvalds 				goto target_done;
48001da177e4SLinus Torvalds 			else if (hd->pLocal == NULL)
48011da177e4SLinus Torvalds 				goto target_done;
48021da177e4SLinus Torvalds 			else {
48031da177e4SLinus Torvalds 				rc = hd->pLocal->completion;
48041da177e4SLinus Torvalds 				ddvprintk(("ReadBuffer Comp Code %d", rc));
48051da177e4SLinus Torvalds 				ddvprintk(("  buff: %0x %0x %0x %0x\n",
48061da177e4SLinus Torvalds 					pbuf1[0], pbuf1[1], pbuf1[2], pbuf1[3]));
48071da177e4SLinus Torvalds 
48081da177e4SLinus Torvalds 				if (rc == MPT_SCANDV_GOOD) {
48091da177e4SLinus Torvalds 					notDone = 0;
48101da177e4SLinus Torvalds 					if (iocmd.flags & MPT_ICFLAG_ECHO) {
48111da177e4SLinus Torvalds 						bufsize =  ((pbuf1[2] & 0x1F) <<8) | pbuf1[3];
48121da177e4SLinus Torvalds 					} else {
48131da177e4SLinus Torvalds 						bufsize =  pbuf1[1]<<16 | pbuf1[2]<<8 | pbuf1[3];
48141da177e4SLinus Torvalds 					}
48151da177e4SLinus Torvalds 				} else if (rc == MPT_SCANDV_SENSE) {
48161da177e4SLinus Torvalds 					u8 skey = hd->pLocal->sense[2] & 0x0F;
48171da177e4SLinus Torvalds 					u8 asc = hd->pLocal->sense[12];
48181da177e4SLinus Torvalds 					u8 ascq = hd->pLocal->sense[13];
48191da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_INFO_FMT
48201da177e4SLinus Torvalds 						"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
48211da177e4SLinus Torvalds 						ioc->name, skey, asc, ascq));
48221da177e4SLinus Torvalds 					if (skey == ILLEGAL_REQUEST) {
48231da177e4SLinus Torvalds 						notDone = 0;
48241da177e4SLinus Torvalds 					} else if (skey == UNIT_ATTENTION) {
48251da177e4SLinus Torvalds 						notDone++; /* repeat */
48261da177e4SLinus Torvalds 					} else if ((skey == NOT_READY) &&
48271da177e4SLinus Torvalds 						(asc == 0x04)&&(ascq == 0x01)) {
48281da177e4SLinus Torvalds 						/* wait then repeat */
48291da177e4SLinus Torvalds 						mdelay (2000);
48301da177e4SLinus Torvalds 						notDone++;
48311da177e4SLinus Torvalds 					} else {
48321da177e4SLinus Torvalds 						/* All other errors are fatal.
48331da177e4SLinus Torvalds 						 */
48341da177e4SLinus Torvalds 						ddvprintk((MYIOC_s_INFO_FMT "DV: fatal error.",
48351da177e4SLinus Torvalds 							ioc->name));
48361da177e4SLinus Torvalds 						goto target_done;
48371da177e4SLinus Torvalds 					}
48381da177e4SLinus Torvalds 				} else {
48391da177e4SLinus Torvalds 					/* All other errors are fatal
48401da177e4SLinus Torvalds 					 */
48411da177e4SLinus Torvalds 					goto target_done;
48421da177e4SLinus Torvalds 				}
48431da177e4SLinus Torvalds 			}
48441da177e4SLinus Torvalds 		}
48451da177e4SLinus Torvalds 
48461da177e4SLinus Torvalds 		if (iocmd.flags & MPT_ICFLAG_ECHO)
48471da177e4SLinus Torvalds 			echoBufSize = bufsize;
48481da177e4SLinus Torvalds 		else
48491da177e4SLinus Torvalds 			dataBufSize = bufsize;
48501da177e4SLinus Torvalds 	}
48511da177e4SLinus Torvalds 	sz = 0;
48521da177e4SLinus Torvalds 	iocmd.flags &= ~MPT_ICFLAG_BUF_CAP;
48531da177e4SLinus Torvalds 
48541da177e4SLinus Torvalds 	/* Use echo buffers if possible,
48551da177e4SLinus Torvalds 	 * Exit if both buffers are 0.
48561da177e4SLinus Torvalds 	 */
48571da177e4SLinus Torvalds 	if (echoBufSize > 0) {
48581da177e4SLinus Torvalds 		iocmd.flags |= MPT_ICFLAG_ECHO;
48591da177e4SLinus Torvalds 		if (dataBufSize > 0)
48601da177e4SLinus Torvalds 			bufsize = min(echoBufSize, dataBufSize);
48611da177e4SLinus Torvalds 		else
48621da177e4SLinus Torvalds 			bufsize = echoBufSize;
48631da177e4SLinus Torvalds 	} else if (dataBufSize == 0)
48641da177e4SLinus Torvalds 		goto target_done;
48651da177e4SLinus Torvalds 
48661da177e4SLinus Torvalds 	ddvprintk((MYIOC_s_INFO_FMT "%s Buffer Capacity %d\n", ioc->name,
48671da177e4SLinus Torvalds 		(iocmd.flags & MPT_ICFLAG_ECHO) ? "Echo" : " ", bufsize));
48681da177e4SLinus Torvalds 
48691da177e4SLinus Torvalds 	/* Data buffers for write-read-compare test max 1K.
48701da177e4SLinus Torvalds 	 */
48711da177e4SLinus Torvalds 	sz = min(bufsize, 1024);
48721da177e4SLinus Torvalds 
48731da177e4SLinus Torvalds 	/* --- loop ----
48741da177e4SLinus Torvalds 	 * On first pass, always issue a reserve.
48751da177e4SLinus Torvalds 	 * On additional loops, only if a reset has occurred.
48761da177e4SLinus Torvalds 	 * iocmd.flags indicates if echo or regular buffer
48771da177e4SLinus Torvalds 	 */
48781da177e4SLinus Torvalds 	for (patt = 0; patt < 4; patt++) {
48791da177e4SLinus Torvalds 		ddvprintk(("Pattern %d\n", patt));
48801da177e4SLinus Torvalds 		if ((iocmd.flags & MPT_ICFLAG_RESERVED) && (iocmd.flags & MPT_ICFLAG_DID_RESET)) {
48811da177e4SLinus Torvalds 			iocmd.cmd = TEST_UNIT_READY;
48821da177e4SLinus Torvalds 			iocmd.data_dma = -1;
48831da177e4SLinus Torvalds 			iocmd.data = NULL;
48841da177e4SLinus Torvalds 			iocmd.size = 0;
48851da177e4SLinus Torvalds 			if (mptscsih_do_cmd(hd, &iocmd) < 0)
48861da177e4SLinus Torvalds 				goto target_done;
48871da177e4SLinus Torvalds 
48881da177e4SLinus Torvalds 			iocmd.cmd = RELEASE;
48891da177e4SLinus Torvalds 			iocmd.data_dma = -1;
48901da177e4SLinus Torvalds 			iocmd.data = NULL;
48911da177e4SLinus Torvalds 			iocmd.size = 0;
48921da177e4SLinus Torvalds 			if (mptscsih_do_cmd(hd, &iocmd) < 0)
48931da177e4SLinus Torvalds 				goto target_done;
48941da177e4SLinus Torvalds 			else if (hd->pLocal == NULL)
48951da177e4SLinus Torvalds 				goto target_done;
48961da177e4SLinus Torvalds 			else {
48971da177e4SLinus Torvalds 				rc = hd->pLocal->completion;
48981da177e4SLinus Torvalds 				ddvprintk(("Release rc %d\n", rc));
48991da177e4SLinus Torvalds 				if (rc == MPT_SCANDV_GOOD)
49001da177e4SLinus Torvalds 					iocmd.flags &= ~MPT_ICFLAG_RESERVED;
49011da177e4SLinus Torvalds 				else
49021da177e4SLinus Torvalds 					goto target_done;
49031da177e4SLinus Torvalds 			}
49041da177e4SLinus Torvalds 			iocmd.flags &= ~MPT_ICFLAG_RESERVED;
49051da177e4SLinus Torvalds 		}
49061da177e4SLinus Torvalds 		iocmd.flags &= ~MPT_ICFLAG_DID_RESET;
49071da177e4SLinus Torvalds 
49081da177e4SLinus Torvalds 		repeat = 5;
49091da177e4SLinus Torvalds 		while (repeat && (!(iocmd.flags & MPT_ICFLAG_RESERVED))) {
49101da177e4SLinus Torvalds 			iocmd.cmd = RESERVE;
49111da177e4SLinus Torvalds 			iocmd.data_dma = -1;
49121da177e4SLinus Torvalds 			iocmd.data = NULL;
49131da177e4SLinus Torvalds 			iocmd.size = 0;
49141da177e4SLinus Torvalds 			if (mptscsih_do_cmd(hd, &iocmd) < 0)
49151da177e4SLinus Torvalds 				goto target_done;
49161da177e4SLinus Torvalds 			else if (hd->pLocal == NULL)
49171da177e4SLinus Torvalds 				goto target_done;
49181da177e4SLinus Torvalds 			else {
49191da177e4SLinus Torvalds 				rc = hd->pLocal->completion;
49201da177e4SLinus Torvalds 				if (rc == MPT_SCANDV_GOOD) {
49211da177e4SLinus Torvalds 					iocmd.flags |= MPT_ICFLAG_RESERVED;
49221da177e4SLinus Torvalds 				} else if (rc == MPT_SCANDV_SENSE) {
49231da177e4SLinus Torvalds 					/* Wait if coming ready
49241da177e4SLinus Torvalds 					 */
49251da177e4SLinus Torvalds 					u8 skey = hd->pLocal->sense[2] & 0x0F;
49261da177e4SLinus Torvalds 					u8 asc = hd->pLocal->sense[12];
49271da177e4SLinus Torvalds 					u8 ascq = hd->pLocal->sense[13];
49281da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_INFO_FMT
49291da177e4SLinus Torvalds 						"DV: Reserve Failed: ", ioc->name));
49301da177e4SLinus Torvalds 					ddvprintk(("SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
49311da177e4SLinus Torvalds 							skey, asc, ascq));
49321da177e4SLinus Torvalds 
49331da177e4SLinus Torvalds 					if ((skey == NOT_READY) && (asc == 0x04)&&
49341da177e4SLinus Torvalds 									(ascq == 0x01)) {
49351da177e4SLinus Torvalds 						/* wait then repeat */
49361da177e4SLinus Torvalds 						mdelay (2000);
49371da177e4SLinus Torvalds 						notDone++;
49381da177e4SLinus Torvalds 					} else {
49391da177e4SLinus Torvalds 						ddvprintk((MYIOC_s_INFO_FMT
49401da177e4SLinus Torvalds 							"DV: Reserved Failed.", ioc->name));
49411da177e4SLinus Torvalds 						goto target_done;
49421da177e4SLinus Torvalds 					}
49431da177e4SLinus Torvalds 				} else {
49441da177e4SLinus Torvalds 					ddvprintk((MYIOC_s_INFO_FMT "DV: Reserved Failed.",
49451da177e4SLinus Torvalds 							 ioc->name));
49461da177e4SLinus Torvalds 					goto target_done;
49471da177e4SLinus Torvalds 				}
49481da177e4SLinus Torvalds 			}
49491da177e4SLinus Torvalds 		}
49501da177e4SLinus Torvalds 
49511da177e4SLinus Torvalds 		mptscsih_fillbuf(pbuf1, sz, patt, 1);
49521da177e4SLinus Torvalds 		iocmd.cmd = WRITE_BUFFER;
49531da177e4SLinus Torvalds 		iocmd.data_dma = buf1_dma;
49541da177e4SLinus Torvalds 		iocmd.data = pbuf1;
49551da177e4SLinus Torvalds 		iocmd.size = sz;
49561da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
49571da177e4SLinus Torvalds 			goto target_done;
49581da177e4SLinus Torvalds 		else if (hd->pLocal == NULL)
49591da177e4SLinus Torvalds 			goto target_done;
49601da177e4SLinus Torvalds 		else {
49611da177e4SLinus Torvalds 			rc = hd->pLocal->completion;
49621da177e4SLinus Torvalds 			if (rc == MPT_SCANDV_GOOD)
49631da177e4SLinus Torvalds 				;		/* Issue read buffer */
49641da177e4SLinus Torvalds 			else if (rc == MPT_SCANDV_DID_RESET) {
49651da177e4SLinus Torvalds 				/* If using echo buffers, reset to data buffers.
49661da177e4SLinus Torvalds 				 * Else do Fallback and restart
49671da177e4SLinus Torvalds 				 * this test (re-issue reserve
49681da177e4SLinus Torvalds 				 * because of bus reset).
49691da177e4SLinus Torvalds 				 */
49701da177e4SLinus Torvalds 				if ((iocmd.flags & MPT_ICFLAG_ECHO) && (dataBufSize >= bufsize)) {
49711da177e4SLinus Torvalds 					iocmd.flags &= ~MPT_ICFLAG_ECHO;
49721da177e4SLinus Torvalds 				} else {
49731da177e4SLinus Torvalds 					dv.cmd = MPT_FALLBACK;
49741da177e4SLinus Torvalds 					mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
49751da177e4SLinus Torvalds 
49761da177e4SLinus Torvalds 					if (mpt_config(hd->ioc, &cfg) != 0)
49771da177e4SLinus Torvalds 						goto target_done;
49781da177e4SLinus Torvalds 
49791da177e4SLinus Torvalds 					if ((!dv.now.width) && (!dv.now.offset))
49801da177e4SLinus Torvalds 						goto target_done;
49811da177e4SLinus Torvalds 				}
49821da177e4SLinus Torvalds 
49831da177e4SLinus Torvalds 				iocmd.flags |= MPT_ICFLAG_DID_RESET;
49841da177e4SLinus Torvalds 				patt = -1;
49851da177e4SLinus Torvalds 				continue;
49861da177e4SLinus Torvalds 			} else if (rc == MPT_SCANDV_SENSE) {
49871da177e4SLinus Torvalds 				/* Restart data test if UA, else quit.
49881da177e4SLinus Torvalds 				 */
49891da177e4SLinus Torvalds 				u8 skey = hd->pLocal->sense[2] & 0x0F;
49901da177e4SLinus Torvalds 				ddvprintk((MYIOC_s_INFO_FMT
49911da177e4SLinus Torvalds 					"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n", ioc->name, skey,
49921da177e4SLinus Torvalds 					hd->pLocal->sense[12], hd->pLocal->sense[13]));
49931da177e4SLinus Torvalds 				if (skey == UNIT_ATTENTION) {
49941da177e4SLinus Torvalds 					patt = -1;
49951da177e4SLinus Torvalds 					continue;
49961da177e4SLinus Torvalds 				} else if (skey == ILLEGAL_REQUEST) {
49971da177e4SLinus Torvalds 					if (iocmd.flags & MPT_ICFLAG_ECHO) {
49981da177e4SLinus Torvalds 						if (dataBufSize >= bufsize) {
49991da177e4SLinus Torvalds 							iocmd.flags &= ~MPT_ICFLAG_ECHO;
50001da177e4SLinus Torvalds 							patt = -1;
50011da177e4SLinus Torvalds 							continue;
50021da177e4SLinus Torvalds 						}
50031da177e4SLinus Torvalds 					}
50041da177e4SLinus Torvalds 					goto target_done;
50051da177e4SLinus Torvalds 				}
50061da177e4SLinus Torvalds 				else
50071da177e4SLinus Torvalds 					goto target_done;
50081da177e4SLinus Torvalds 			} else {
50091da177e4SLinus Torvalds 				/* fatal error */
50101da177e4SLinus Torvalds 				goto target_done;
50111da177e4SLinus Torvalds 			}
50121da177e4SLinus Torvalds 		}
50131da177e4SLinus Torvalds 
50141da177e4SLinus Torvalds 		iocmd.cmd = READ_BUFFER;
50151da177e4SLinus Torvalds 		iocmd.data_dma = buf2_dma;
50161da177e4SLinus Torvalds 		iocmd.data = pbuf2;
50171da177e4SLinus Torvalds 		iocmd.size = sz;
50181da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
50191da177e4SLinus Torvalds 			goto target_done;
50201da177e4SLinus Torvalds 		else if (hd->pLocal == NULL)
50211da177e4SLinus Torvalds 			goto target_done;
50221da177e4SLinus Torvalds 		else {
50231da177e4SLinus Torvalds 			rc = hd->pLocal->completion;
50241da177e4SLinus Torvalds 			if (rc == MPT_SCANDV_GOOD) {
50251da177e4SLinus Torvalds 				 /* If buffers compare,
50261da177e4SLinus Torvalds 				  * go to next pattern,
50271da177e4SLinus Torvalds 				  * else, do a fallback and restart
50281da177e4SLinus Torvalds 				  * data transfer test.
50291da177e4SLinus Torvalds 				  */
50301da177e4SLinus Torvalds 				if (memcmp (pbuf1, pbuf2, sz) == 0) {
50311da177e4SLinus Torvalds 					; /* goto next pattern */
50321da177e4SLinus Torvalds 				} else {
50331da177e4SLinus Torvalds 					/* Miscompare with Echo buffer, go to data buffer,
50341da177e4SLinus Torvalds 					 * if that buffer exists.
50351da177e4SLinus Torvalds 					 * Miscompare with Data buffer, check first 4 bytes,
50361da177e4SLinus Torvalds 					 * some devices return capacity. Exit in this case.
50371da177e4SLinus Torvalds 					 */
50381da177e4SLinus Torvalds 					if (iocmd.flags & MPT_ICFLAG_ECHO) {
50391da177e4SLinus Torvalds 						if (dataBufSize >= bufsize)
50401da177e4SLinus Torvalds 							iocmd.flags &= ~MPT_ICFLAG_ECHO;
50411da177e4SLinus Torvalds 						else
50421da177e4SLinus Torvalds 							goto target_done;
50431da177e4SLinus Torvalds 					} else {
50441da177e4SLinus Torvalds 						if (dataBufSize == (pbuf2[1]<<16 | pbuf2[2]<<8 | pbuf2[3])) {
50451da177e4SLinus Torvalds 							/* Argh. Device returning wrong data.
50461da177e4SLinus Torvalds 							 * Quit DV for this device.
50471da177e4SLinus Torvalds 							 */
50481da177e4SLinus Torvalds 							goto target_done;
50491da177e4SLinus Torvalds 						}
50501da177e4SLinus Torvalds 
50511da177e4SLinus Torvalds 						/* Had an actual miscompare. Slow down.*/
50521da177e4SLinus Torvalds 						dv.cmd = MPT_FALLBACK;
50531da177e4SLinus Torvalds 						mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
50541da177e4SLinus Torvalds 
50551da177e4SLinus Torvalds 						if (mpt_config(hd->ioc, &cfg) != 0)
50561da177e4SLinus Torvalds 							goto target_done;
50571da177e4SLinus Torvalds 
50581da177e4SLinus Torvalds 						if ((!dv.now.width) && (!dv.now.offset))
50591da177e4SLinus Torvalds 							goto target_done;
50601da177e4SLinus Torvalds 					}
50611da177e4SLinus Torvalds 
50621da177e4SLinus Torvalds 					patt = -1;
50631da177e4SLinus Torvalds 					continue;
50641da177e4SLinus Torvalds 				}
50651da177e4SLinus Torvalds 			} else if (rc == MPT_SCANDV_DID_RESET) {
50661da177e4SLinus Torvalds 				/* Do Fallback and restart
50671da177e4SLinus Torvalds 				 * this test (re-issue reserve
50681da177e4SLinus Torvalds 				 * because of bus reset).
50691da177e4SLinus Torvalds 				 */
50701da177e4SLinus Torvalds 				dv.cmd = MPT_FALLBACK;
50711da177e4SLinus Torvalds 				mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
50721da177e4SLinus Torvalds 
50731da177e4SLinus Torvalds 				if (mpt_config(hd->ioc, &cfg) != 0)
50741da177e4SLinus Torvalds 					 goto target_done;
50751da177e4SLinus Torvalds 
50761da177e4SLinus Torvalds 				if ((!dv.now.width) && (!dv.now.offset))
50771da177e4SLinus Torvalds 					goto target_done;
50781da177e4SLinus Torvalds 
50791da177e4SLinus Torvalds 				iocmd.flags |= MPT_ICFLAG_DID_RESET;
50801da177e4SLinus Torvalds 				patt = -1;
50811da177e4SLinus Torvalds 				continue;
50821da177e4SLinus Torvalds 			} else if (rc == MPT_SCANDV_SENSE) {
50831da177e4SLinus Torvalds 				/* Restart data test if UA, else quit.
50841da177e4SLinus Torvalds 				 */
50851da177e4SLinus Torvalds 				u8 skey = hd->pLocal->sense[2] & 0x0F;
50861da177e4SLinus Torvalds 				ddvprintk((MYIOC_s_INFO_FMT
50871da177e4SLinus Torvalds 					"SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n", ioc->name, skey,
50881da177e4SLinus Torvalds 					hd->pLocal->sense[12], hd->pLocal->sense[13]));
50891da177e4SLinus Torvalds 				if (skey == UNIT_ATTENTION) {
50901da177e4SLinus Torvalds 					patt = -1;
50911da177e4SLinus Torvalds 					continue;
50921da177e4SLinus Torvalds 				}
50931da177e4SLinus Torvalds 				else
50941da177e4SLinus Torvalds 					goto target_done;
50951da177e4SLinus Torvalds 			} else {
50961da177e4SLinus Torvalds 				/* fatal error */
50971da177e4SLinus Torvalds 				goto target_done;
50981da177e4SLinus Torvalds 			}
50991da177e4SLinus Torvalds 		}
51001da177e4SLinus Torvalds 
51011da177e4SLinus Torvalds 	} /* --- end of patt loop ---- */
51021da177e4SLinus Torvalds 
51031da177e4SLinus Torvalds target_done:
51041da177e4SLinus Torvalds 	if (iocmd.flags & MPT_ICFLAG_RESERVED) {
51051da177e4SLinus Torvalds 		iocmd.cmd = RELEASE;
51061da177e4SLinus Torvalds 		iocmd.data_dma = -1;
51071da177e4SLinus Torvalds 		iocmd.data = NULL;
51081da177e4SLinus Torvalds 		iocmd.size = 0;
51091da177e4SLinus Torvalds 		if (mptscsih_do_cmd(hd, &iocmd) < 0)
51101da177e4SLinus Torvalds 			printk(MYIOC_s_INFO_FMT "DV: Release failed. id %d",
51111da177e4SLinus Torvalds 					ioc->name, id);
51121da177e4SLinus Torvalds 		else if (hd->pLocal) {
51131da177e4SLinus Torvalds 			if (hd->pLocal->completion == MPT_SCANDV_GOOD)
51141da177e4SLinus Torvalds 				iocmd.flags &= ~MPT_ICFLAG_RESERVED;
51151da177e4SLinus Torvalds 		} else {
51161da177e4SLinus Torvalds 			printk(MYIOC_s_INFO_FMT "DV: Release failed. id %d",
51171da177e4SLinus Torvalds 						ioc->name, id);
51181da177e4SLinus Torvalds 		}
51191da177e4SLinus Torvalds 	}
51201da177e4SLinus Torvalds 
51211da177e4SLinus Torvalds 
51221da177e4SLinus Torvalds 	/* Set if cfg1_dma_addr contents is valid
51231da177e4SLinus Torvalds 	 */
512469218ee5SChristoph Hellwig 	if ((cfg.cfghdr.hdr != NULL) && (retcode == 0)){
51251da177e4SLinus Torvalds 		/* If disk, not U320, disable QAS
51261da177e4SLinus Torvalds 		 */
51271da177e4SLinus Torvalds 		if ((inq0 == 0) && (dv.now.factor > MPT_ULTRA320)) {
51281da177e4SLinus Torvalds 			hd->ioc->spi_data.noQas = MPT_TARGET_NO_NEGO_QAS;
51291da177e4SLinus Torvalds 			ddvprintk((MYIOC_s_NOTE_FMT
51301da177e4SLinus Torvalds 			    "noQas set due to id=%d has factor=%x\n", ioc->name, id, dv.now.factor));
51311da177e4SLinus Torvalds 		}
51321da177e4SLinus Torvalds 
51331da177e4SLinus Torvalds 		dv.cmd = MPT_SAVE;
51341da177e4SLinus Torvalds 		mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
51351da177e4SLinus Torvalds 
51361da177e4SLinus Torvalds 		/* Double writes to SDP1 can cause problems,
51371da177e4SLinus Torvalds 		 * skip save of the final negotiated settings to
51381da177e4SLinus Torvalds 		 * SCSI device page 1.
51391da177e4SLinus Torvalds 		 *
514069218ee5SChristoph Hellwig 		cfg.cfghdr.hdr = &header1;
51411da177e4SLinus Torvalds 		cfg.physAddr = cfg1_dma_addr;
51421da177e4SLinus Torvalds 		cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
51431da177e4SLinus Torvalds 		cfg.dir = 1;
51441da177e4SLinus Torvalds 		mpt_config(hd->ioc, &cfg);
51451da177e4SLinus Torvalds 		 */
51461da177e4SLinus Torvalds 	}
51471da177e4SLinus Torvalds 
51481da177e4SLinus Torvalds 	/* If this is a RAID Passthrough, enable internal IOs
51491da177e4SLinus Torvalds 	 */
51501da177e4SLinus Torvalds 	if (iocmd.flags & MPT_ICFLAG_PHYS_DISK) {
51511da177e4SLinus Torvalds 		if (mptscsih_do_raid(hd, MPI_RAID_ACTION_ENABLE_PHYS_IO, &iocmd) < 0)
51521da177e4SLinus Torvalds 			ddvprintk((MYIOC_s_ERR_FMT "RAID Enable FAILED!\n", ioc->name));
51531da177e4SLinus Torvalds 	}
51541da177e4SLinus Torvalds 
51551da177e4SLinus Torvalds 	/* Done with the DV scan of the current target
51561da177e4SLinus Torvalds 	 */
51571da177e4SLinus Torvalds 	if (pDvBuf)
51581da177e4SLinus Torvalds 		pci_free_consistent(ioc->pcidev, dv_alloc, pDvBuf, dvbuf_dma);
51591da177e4SLinus Torvalds 
51601da177e4SLinus Torvalds 	ddvtprintk((MYIOC_s_INFO_FMT "DV Done id=%d\n",
51611da177e4SLinus Torvalds 			ioc->name, id));
51621da177e4SLinus Torvalds 
51631da177e4SLinus Torvalds 	return retcode;
51641da177e4SLinus Torvalds }
51651da177e4SLinus Torvalds 
51661da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
51671da177e4SLinus Torvalds /*	mptscsih_dv_parms - perform a variety of operations on the
51681da177e4SLinus Torvalds  *	parameters used for negotiation.
51691da177e4SLinus Torvalds  *	@hd: Pointer to a SCSI host.
51701da177e4SLinus Torvalds  *	@dv: Pointer to a structure that contains the maximum and current
51711da177e4SLinus Torvalds  *		negotiated parameters.
51721da177e4SLinus Torvalds  */
51731da177e4SLinus Torvalds static void
51741da177e4SLinus Torvalds mptscsih_dv_parms(MPT_SCSI_HOST *hd, DVPARAMETERS *dv,void *pPage)
51751da177e4SLinus Torvalds {
51761da177e4SLinus Torvalds 	VirtDevice		*pTarget;
51771da177e4SLinus Torvalds 	SCSIDevicePage0_t	*pPage0;
51781da177e4SLinus Torvalds 	SCSIDevicePage1_t	*pPage1;
51791da177e4SLinus Torvalds 	int			val = 0, data, configuration;
51801da177e4SLinus Torvalds 	u8			width = 0;
51811da177e4SLinus Torvalds 	u8			offset = 0;
51821da177e4SLinus Torvalds 	u8			factor = 0;
51831da177e4SLinus Torvalds 	u8			negoFlags = 0;
51841da177e4SLinus Torvalds 	u8			cmd = dv->cmd;
51851da177e4SLinus Torvalds 	u8			id = dv->id;
51861da177e4SLinus Torvalds 
51871da177e4SLinus Torvalds 	switch (cmd) {
51881da177e4SLinus Torvalds 	case MPT_GET_NVRAM_VALS:
51891da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT "Getting NVRAM: ",
51901da177e4SLinus Torvalds 							 hd->ioc->name));
51911da177e4SLinus Torvalds 		/* Get the NVRAM values and save in tmax
51921da177e4SLinus Torvalds 		 * If not an LVD bus, the adapter minSyncFactor has been
51931da177e4SLinus Torvalds 		 * already throttled back.
51941da177e4SLinus Torvalds 		 */
51951da177e4SLinus Torvalds 		if ((hd->Targets)&&((pTarget = hd->Targets[(int)id]) != NULL) && !pTarget->raidVolume) {
51961da177e4SLinus Torvalds 			width = pTarget->maxWidth;
51971da177e4SLinus Torvalds 			offset = pTarget->maxOffset;
51981da177e4SLinus Torvalds 			factor = pTarget->minSyncFactor;
51991da177e4SLinus Torvalds 			negoFlags = pTarget->negoFlags;
52001da177e4SLinus Torvalds 		} else {
52011da177e4SLinus Torvalds 			if (hd->ioc->spi_data.nvram && (hd->ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
52021da177e4SLinus Torvalds 				data = hd->ioc->spi_data.nvram[id];
52031da177e4SLinus Torvalds 				width = data & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
52041da177e4SLinus Torvalds 				if ((offset = hd->ioc->spi_data.maxSyncOffset) == 0)
52051da177e4SLinus Torvalds 					factor = MPT_ASYNC;
52061da177e4SLinus Torvalds 				else {
52071da177e4SLinus Torvalds 					factor = (data & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
52081da177e4SLinus Torvalds 					if ((factor == 0) || (factor == MPT_ASYNC)){
52091da177e4SLinus Torvalds 						factor = MPT_ASYNC;
52101da177e4SLinus Torvalds 						offset = 0;
52111da177e4SLinus Torvalds 					}
52121da177e4SLinus Torvalds 				}
52131da177e4SLinus Torvalds 			} else {
52141da177e4SLinus Torvalds 				width = MPT_NARROW;
52151da177e4SLinus Torvalds 				offset = 0;
52161da177e4SLinus Torvalds 				factor = MPT_ASYNC;
52171da177e4SLinus Torvalds 			}
52181da177e4SLinus Torvalds 
52191da177e4SLinus Torvalds 			/* Set the negotiation flags */
52201da177e4SLinus Torvalds 			negoFlags = hd->ioc->spi_data.noQas;
52211da177e4SLinus Torvalds 			if (!width)
52221da177e4SLinus Torvalds 				negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
52231da177e4SLinus Torvalds 
52241da177e4SLinus Torvalds 			if (!offset)
52251da177e4SLinus Torvalds 				negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
52261da177e4SLinus Torvalds 		}
52271da177e4SLinus Torvalds 
52281da177e4SLinus Torvalds 		/* limit by adapter capabilities */
52291da177e4SLinus Torvalds 		width = min(width, hd->ioc->spi_data.maxBusWidth);
52301da177e4SLinus Torvalds 		offset = min(offset, hd->ioc->spi_data.maxSyncOffset);
52311da177e4SLinus Torvalds 		factor = max(factor, hd->ioc->spi_data.minSyncFactor);
52321da177e4SLinus Torvalds 
52331da177e4SLinus Torvalds 		/* Check Consistency */
52341da177e4SLinus Torvalds 		if (offset && (factor < MPT_ULTRA2) && !width)
52351da177e4SLinus Torvalds 			factor = MPT_ULTRA2;
52361da177e4SLinus Torvalds 
52371da177e4SLinus Torvalds 		dv->max.width = width;
52381da177e4SLinus Torvalds 		dv->max.offset = offset;
52391da177e4SLinus Torvalds 		dv->max.factor = factor;
52401da177e4SLinus Torvalds 		dv->max.flags = negoFlags;
52411da177e4SLinus Torvalds 		ddvprintk((" id=%d width=%d factor=%x offset=%x flags=%x\n",
52421da177e4SLinus Torvalds 				id, width, factor, offset, negoFlags));
52431da177e4SLinus Torvalds 		break;
52441da177e4SLinus Torvalds 
52451da177e4SLinus Torvalds 	case MPT_UPDATE_MAX:
52461da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT
52471da177e4SLinus Torvalds 			"Updating with SDP0 Data: ", hd->ioc->name));
52481da177e4SLinus Torvalds 		/* Update tmax values with those from Device Page 0.*/
52491da177e4SLinus Torvalds 		pPage0 = (SCSIDevicePage0_t *) pPage;
52501da177e4SLinus Torvalds 		if (pPage0) {
5251637fa99bSChristoph Hellwig 			val = le32_to_cpu(pPage0->NegotiatedParameters);
52521da177e4SLinus Torvalds 			dv->max.width = val & MPI_SCSIDEVPAGE0_NP_WIDE ? 1 : 0;
52531da177e4SLinus Torvalds 			dv->max.offset = (val&MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) >> 16;
52541da177e4SLinus Torvalds 			dv->max.factor = (val&MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> 8;
52551da177e4SLinus Torvalds 		}
52561da177e4SLinus Torvalds 
52571da177e4SLinus Torvalds 		dv->now.width = dv->max.width;
52581da177e4SLinus Torvalds 		dv->now.offset = dv->max.offset;
52591da177e4SLinus Torvalds 		dv->now.factor = dv->max.factor;
52601da177e4SLinus Torvalds 		ddvprintk(("id=%d width=%d factor=%x offset=%x flags=%x\n",
52611da177e4SLinus Torvalds 				id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags));
52621da177e4SLinus Torvalds 		break;
52631da177e4SLinus Torvalds 
52641da177e4SLinus Torvalds 	case MPT_SET_MAX:
52651da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT "Setting Max: ",
52661da177e4SLinus Torvalds 								hd->ioc->name));
52671da177e4SLinus Torvalds 		/* Set current to the max values. Update the config page.*/
52681da177e4SLinus Torvalds 		dv->now.width = dv->max.width;
52691da177e4SLinus Torvalds 		dv->now.offset = dv->max.offset;
52701da177e4SLinus Torvalds 		dv->now.factor = dv->max.factor;
52711da177e4SLinus Torvalds 		dv->now.flags = dv->max.flags;
52721da177e4SLinus Torvalds 
52731da177e4SLinus Torvalds 		pPage1 = (SCSIDevicePage1_t *)pPage;
52741da177e4SLinus Torvalds 		if (pPage1) {
52751da177e4SLinus Torvalds 			mptscsih_setDevicePage1Flags (dv->now.width, dv->now.factor,
52761da177e4SLinus Torvalds 				dv->now.offset, &val, &configuration, dv->now.flags);
52771da177e4SLinus Torvalds 			dnegoprintk(("Setting Max: id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x config=%x\n",
52781da177e4SLinus Torvalds 				id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags, val, configuration));
5279637fa99bSChristoph Hellwig 			pPage1->RequestedParameters = cpu_to_le32(val);
52801da177e4SLinus Torvalds 			pPage1->Reserved = 0;
5281637fa99bSChristoph Hellwig 			pPage1->Configuration = cpu_to_le32(configuration);
52821da177e4SLinus Torvalds 		}
52831da177e4SLinus Torvalds 
5284637fa99bSChristoph Hellwig 		ddvprintk(("id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x configuration=%x\n",
52851da177e4SLinus Torvalds 				id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags, val, configuration));
52861da177e4SLinus Torvalds 		break;
52871da177e4SLinus Torvalds 
52881da177e4SLinus Torvalds 	case MPT_SET_MIN:
52891da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT "Setting Min: ",
52901da177e4SLinus Torvalds 								hd->ioc->name));
52911da177e4SLinus Torvalds 		/* Set page to asynchronous and narrow
52921da177e4SLinus Torvalds 		 * Do not update now, breaks fallback routine. */
52931da177e4SLinus Torvalds 		width = MPT_NARROW;
52941da177e4SLinus Torvalds 		offset = 0;
52951da177e4SLinus Torvalds 		factor = MPT_ASYNC;
52961da177e4SLinus Torvalds 		negoFlags = dv->max.flags;
52971da177e4SLinus Torvalds 
52981da177e4SLinus Torvalds 		pPage1 = (SCSIDevicePage1_t *)pPage;
52991da177e4SLinus Torvalds 		if (pPage1) {
53001da177e4SLinus Torvalds 			mptscsih_setDevicePage1Flags (width, factor,
53011da177e4SLinus Torvalds 				offset, &val, &configuration, negoFlags);
53021da177e4SLinus Torvalds 			dnegoprintk(("Setting Min: id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x config=%x\n",
53031da177e4SLinus Torvalds 				id, width, factor, offset, negoFlags, val, configuration));
5304637fa99bSChristoph Hellwig 			pPage1->RequestedParameters = cpu_to_le32(val);
53051da177e4SLinus Torvalds 			pPage1->Reserved = 0;
5306637fa99bSChristoph Hellwig 			pPage1->Configuration = cpu_to_le32(configuration);
53071da177e4SLinus Torvalds 		}
53081da177e4SLinus Torvalds 		ddvprintk(("id=%d width=%d factor=%x offset=%x request=%x config=%x negoFlags=%x\n",
53091da177e4SLinus Torvalds 				id, width, factor, offset, val, configuration, negoFlags));
53101da177e4SLinus Torvalds 		break;
53111da177e4SLinus Torvalds 
53121da177e4SLinus Torvalds 	case MPT_FALLBACK:
53131da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT
53141da177e4SLinus Torvalds 			"Fallback: Start: offset %d, factor %x, width %d \n",
53151da177e4SLinus Torvalds 				hd->ioc->name, dv->now.offset,
53161da177e4SLinus Torvalds 				dv->now.factor, dv->now.width));
53171da177e4SLinus Torvalds 		width = dv->now.width;
53181da177e4SLinus Torvalds 		offset = dv->now.offset;
53191da177e4SLinus Torvalds 		factor = dv->now.factor;
53201da177e4SLinus Torvalds 		if ((offset) && (dv->max.width)) {
53211da177e4SLinus Torvalds 			if (factor < MPT_ULTRA160)
53221da177e4SLinus Torvalds 				factor = MPT_ULTRA160;
53231da177e4SLinus Torvalds 			else if (factor < MPT_ULTRA2) {
53241da177e4SLinus Torvalds 				factor = MPT_ULTRA2;
53251da177e4SLinus Torvalds 				width = MPT_WIDE;
53261da177e4SLinus Torvalds 			} else if ((factor == MPT_ULTRA2) && width) {
53271da177e4SLinus Torvalds 				factor = MPT_ULTRA2;
53281da177e4SLinus Torvalds 				width = MPT_NARROW;
53291da177e4SLinus Torvalds 			} else if (factor < MPT_ULTRA) {
53301da177e4SLinus Torvalds 				factor = MPT_ULTRA;
53311da177e4SLinus Torvalds 				width = MPT_WIDE;
53321da177e4SLinus Torvalds 			} else if ((factor == MPT_ULTRA) && width) {
53331da177e4SLinus Torvalds 				width = MPT_NARROW;
53341da177e4SLinus Torvalds 			} else if (factor < MPT_FAST) {
53351da177e4SLinus Torvalds 				factor = MPT_FAST;
53361da177e4SLinus Torvalds 				width = MPT_WIDE;
53371da177e4SLinus Torvalds 			} else if ((factor == MPT_FAST) && width) {
53381da177e4SLinus Torvalds 				factor = MPT_FAST;
53391da177e4SLinus Torvalds 				width = MPT_NARROW;
53401da177e4SLinus Torvalds 			} else if (factor < MPT_SCSI) {
53411da177e4SLinus Torvalds 				factor = MPT_SCSI;
53421da177e4SLinus Torvalds 				width = MPT_WIDE;
53431da177e4SLinus Torvalds 			} else if ((factor == MPT_SCSI) && width) {
53441da177e4SLinus Torvalds 				factor = MPT_SCSI;
53451da177e4SLinus Torvalds 				width = MPT_NARROW;
53461da177e4SLinus Torvalds 			} else {
53471da177e4SLinus Torvalds 				factor = MPT_ASYNC;
53481da177e4SLinus Torvalds 				offset = 0;
53491da177e4SLinus Torvalds 			}
53501da177e4SLinus Torvalds 
53511da177e4SLinus Torvalds 		} else if (offset) {
53521da177e4SLinus Torvalds 			width = MPT_NARROW;
53531da177e4SLinus Torvalds 			if (factor < MPT_ULTRA)
53541da177e4SLinus Torvalds 				factor = MPT_ULTRA;
53551da177e4SLinus Torvalds 			else if (factor < MPT_FAST)
53561da177e4SLinus Torvalds 				factor = MPT_FAST;
53571da177e4SLinus Torvalds 			else if (factor < MPT_SCSI)
53581da177e4SLinus Torvalds 				factor = MPT_SCSI;
53591da177e4SLinus Torvalds 			else {
53601da177e4SLinus Torvalds 				factor = MPT_ASYNC;
53611da177e4SLinus Torvalds 				offset = 0;
53621da177e4SLinus Torvalds 			}
53631da177e4SLinus Torvalds 
53641da177e4SLinus Torvalds 		} else {
53651da177e4SLinus Torvalds 			width = MPT_NARROW;
53661da177e4SLinus Torvalds 			factor = MPT_ASYNC;
53671da177e4SLinus Torvalds 		}
53681da177e4SLinus Torvalds 		dv->max.flags |= MPT_TARGET_NO_NEGO_QAS;
53691da177e4SLinus Torvalds 		dv->max.flags &= ~MPT_TAPE_NEGO_IDP;
53701da177e4SLinus Torvalds 
53711da177e4SLinus Torvalds 		dv->now.width = width;
53721da177e4SLinus Torvalds 		dv->now.offset = offset;
53731da177e4SLinus Torvalds 		dv->now.factor = factor;
53741da177e4SLinus Torvalds 		dv->now.flags = dv->max.flags;
53751da177e4SLinus Torvalds 
53761da177e4SLinus Torvalds 		pPage1 = (SCSIDevicePage1_t *)pPage;
53771da177e4SLinus Torvalds 		if (pPage1) {
53781da177e4SLinus Torvalds 			mptscsih_setDevicePage1Flags (width, factor, offset, &val,
53791da177e4SLinus Torvalds 						&configuration, dv->now.flags);
5380637fa99bSChristoph Hellwig 			dnegoprintk(("Finish: id=%d width=%d offset=%d factor=%x negoFlags=%x request=%x config=%x\n",
53811da177e4SLinus Torvalds 			     id, width, offset, factor, dv->now.flags, val, configuration));
53821da177e4SLinus Torvalds 
5383637fa99bSChristoph Hellwig 			pPage1->RequestedParameters = cpu_to_le32(val);
53841da177e4SLinus Torvalds 			pPage1->Reserved = 0;
5385637fa99bSChristoph Hellwig 			pPage1->Configuration = cpu_to_le32(configuration);
53861da177e4SLinus Torvalds 		}
53871da177e4SLinus Torvalds 
53881da177e4SLinus Torvalds 		ddvprintk(("Finish: id=%d offset=%d factor=%x width=%d request=%x config=%x\n",
53891da177e4SLinus Torvalds 			     id, dv->now.offset, dv->now.factor, dv->now.width, val, configuration));
53901da177e4SLinus Torvalds 		break;
53911da177e4SLinus Torvalds 
53921da177e4SLinus Torvalds 	case MPT_SAVE:
53931da177e4SLinus Torvalds 		ddvprintk((MYIOC_s_NOTE_FMT
53941da177e4SLinus Torvalds 			"Saving to Target structure: ", hd->ioc->name));
53951da177e4SLinus Torvalds 		ddvprintk(("id=%d width=%x factor=%x offset=%d flags=%x\n",
53961da177e4SLinus Torvalds 			     id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags));
53971da177e4SLinus Torvalds 
53981da177e4SLinus Torvalds 		/* Save these values to target structures
53991da177e4SLinus Torvalds 		 * or overwrite nvram (phys disks only).
54001da177e4SLinus Torvalds 		 */
54011da177e4SLinus Torvalds 
54021da177e4SLinus Torvalds 		if ((hd->Targets)&&((pTarget = hd->Targets[(int)id]) != NULL) && !pTarget->raidVolume ) {
54031da177e4SLinus Torvalds 			pTarget->maxWidth = dv->now.width;
54041da177e4SLinus Torvalds 			pTarget->maxOffset = dv->now.offset;
54051da177e4SLinus Torvalds 			pTarget->minSyncFactor = dv->now.factor;
54061da177e4SLinus Torvalds 			pTarget->negoFlags = dv->now.flags;
54071da177e4SLinus Torvalds 		} else {
54081da177e4SLinus Torvalds 			/* Preserv all flags, use
54091da177e4SLinus Torvalds 			 * read-modify-write algorithm
54101da177e4SLinus Torvalds 			 */
54111da177e4SLinus Torvalds 			if (hd->ioc->spi_data.nvram) {
54121da177e4SLinus Torvalds 				data = hd->ioc->spi_data.nvram[id];
54131da177e4SLinus Torvalds 
54141da177e4SLinus Torvalds 				if (dv->now.width)
54151da177e4SLinus Torvalds 					data &= ~MPT_NVRAM_WIDE_DISABLE;
54161da177e4SLinus Torvalds 				else
54171da177e4SLinus Torvalds 					data |= MPT_NVRAM_WIDE_DISABLE;
54181da177e4SLinus Torvalds 
54191da177e4SLinus Torvalds 				if (!dv->now.offset)
54201da177e4SLinus Torvalds 					factor = MPT_ASYNC;
54211da177e4SLinus Torvalds 
54221da177e4SLinus Torvalds 				data &= ~MPT_NVRAM_SYNC_MASK;
54231da177e4SLinus Torvalds 				data |= (dv->now.factor << MPT_NVRAM_SYNC_SHIFT) & MPT_NVRAM_SYNC_MASK;
54241da177e4SLinus Torvalds 
54251da177e4SLinus Torvalds 				hd->ioc->spi_data.nvram[id] = data;
54261da177e4SLinus Torvalds 			}
54271da177e4SLinus Torvalds 		}
54281da177e4SLinus Torvalds 		break;
54291da177e4SLinus Torvalds 	}
54301da177e4SLinus Torvalds }
54311da177e4SLinus Torvalds 
54321da177e4SLinus Torvalds /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
54331da177e4SLinus Torvalds /*	mptscsih_fillbuf - fill a buffer with a special data pattern
54341da177e4SLinus Torvalds  *		cleanup. For bus scan only.
54351da177e4SLinus Torvalds  *
54361da177e4SLinus Torvalds  *	@buffer: Pointer to data buffer to be filled.
54371da177e4SLinus Torvalds  *	@size: Number of bytes to fill
54381da177e4SLinus Torvalds  *	@index: Pattern index
54391da177e4SLinus Torvalds  *	@width: bus width, 0 (8 bits) or 1 (16 bits)
54401da177e4SLinus Torvalds  */
54411da177e4SLinus Torvalds static void
54421da177e4SLinus Torvalds mptscsih_fillbuf(char *buffer, int size, int index, int width)
54431da177e4SLinus Torvalds {
54441da177e4SLinus Torvalds 	char *ptr = buffer;
54451da177e4SLinus Torvalds 	int ii;
54461da177e4SLinus Torvalds 	char byte;
54471da177e4SLinus Torvalds 	short val;
54481da177e4SLinus Torvalds 
54491da177e4SLinus Torvalds 	switch (index) {
54501da177e4SLinus Torvalds 	case 0:
54511da177e4SLinus Torvalds 
54521da177e4SLinus Torvalds 		if (width) {
54531da177e4SLinus Torvalds 			/* Pattern:  0000 FFFF 0000 FFFF
54541da177e4SLinus Torvalds 			 */
54551da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
54561da177e4SLinus Torvalds 				if (ii & 0x02)
54571da177e4SLinus Torvalds 					*ptr = 0xFF;
54581da177e4SLinus Torvalds 				else
54591da177e4SLinus Torvalds 					*ptr = 0x00;
54601da177e4SLinus Torvalds 			}
54611da177e4SLinus Torvalds 		} else {
54621da177e4SLinus Torvalds 			/* Pattern:  00 FF 00 FF
54631da177e4SLinus Torvalds 			 */
54641da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
54651da177e4SLinus Torvalds 				if (ii & 0x01)
54661da177e4SLinus Torvalds 					*ptr = 0xFF;
54671da177e4SLinus Torvalds 				else
54681da177e4SLinus Torvalds 					*ptr = 0x00;
54691da177e4SLinus Torvalds 			}
54701da177e4SLinus Torvalds 		}
54711da177e4SLinus Torvalds 		break;
54721da177e4SLinus Torvalds 
54731da177e4SLinus Torvalds 	case 1:
54741da177e4SLinus Torvalds 		if (width) {
54751da177e4SLinus Torvalds 			/* Pattern:  5555 AAAA 5555 AAAA 5555
54761da177e4SLinus Torvalds 			 */
54771da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
54781da177e4SLinus Torvalds 				if (ii & 0x02)
54791da177e4SLinus Torvalds 					*ptr = 0xAA;
54801da177e4SLinus Torvalds 				else
54811da177e4SLinus Torvalds 					*ptr = 0x55;
54821da177e4SLinus Torvalds 			}
54831da177e4SLinus Torvalds 		} else {
54841da177e4SLinus Torvalds 			/* Pattern:  55 AA 55 AA 55
54851da177e4SLinus Torvalds 			 */
54861da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
54871da177e4SLinus Torvalds 				if (ii & 0x01)
54881da177e4SLinus Torvalds 					*ptr = 0xAA;
54891da177e4SLinus Torvalds 				else
54901da177e4SLinus Torvalds 					*ptr = 0x55;
54911da177e4SLinus Torvalds 			}
54921da177e4SLinus Torvalds 		}
54931da177e4SLinus Torvalds 		break;
54941da177e4SLinus Torvalds 
54951da177e4SLinus Torvalds 	case 2:
54961da177e4SLinus Torvalds 		/* Pattern:  00 01 02 03 04 05
54971da177e4SLinus Torvalds 		 * ... FE FF 00 01..
54981da177e4SLinus Torvalds 		 */
54991da177e4SLinus Torvalds 		for (ii=0; ii < size; ii++, ptr++)
55001da177e4SLinus Torvalds 			*ptr = (char) ii;
55011da177e4SLinus Torvalds 		break;
55021da177e4SLinus Torvalds 
55031da177e4SLinus Torvalds 	case 3:
55041da177e4SLinus Torvalds 		if (width) {
55051da177e4SLinus Torvalds 			/* Wide Pattern:  FFFE 0001 FFFD 0002
55061da177e4SLinus Torvalds 			 * ...  4000 DFFF 8000 EFFF
55071da177e4SLinus Torvalds 			 */
55081da177e4SLinus Torvalds 			byte = 0;
55091da177e4SLinus Torvalds 			for (ii=0; ii < size/2; ii++) {
55101da177e4SLinus Torvalds 				/* Create the base pattern
55111da177e4SLinus Torvalds 				 */
55121da177e4SLinus Torvalds 				val = (1 << byte);
55131da177e4SLinus Torvalds 				/* every 64 (0x40) bytes flip the pattern
55141da177e4SLinus Torvalds 				 * since we fill 2 bytes / iteration,
55151da177e4SLinus Torvalds 				 * test for ii = 0x20
55161da177e4SLinus Torvalds 				 */
55171da177e4SLinus Torvalds 				if (ii & 0x20)
55181da177e4SLinus Torvalds 					val = ~(val);
55191da177e4SLinus Torvalds 
55201da177e4SLinus Torvalds 				if (ii & 0x01) {
55211da177e4SLinus Torvalds 					*ptr = (char)( (val & 0xFF00) >> 8);
55221da177e4SLinus Torvalds 					ptr++;
55231da177e4SLinus Torvalds 					*ptr = (char)(val & 0xFF);
55241da177e4SLinus Torvalds 					byte++;
55251da177e4SLinus Torvalds 					byte &= 0x0F;
55261da177e4SLinus Torvalds 				} else {
55271da177e4SLinus Torvalds 					val = ~val;
55281da177e4SLinus Torvalds 					*ptr = (char)( (val & 0xFF00) >> 8);
55291da177e4SLinus Torvalds 					ptr++;
55301da177e4SLinus Torvalds 					*ptr = (char)(val & 0xFF);
55311da177e4SLinus Torvalds 				}
55321da177e4SLinus Torvalds 
55331da177e4SLinus Torvalds 				ptr++;
55341da177e4SLinus Torvalds 			}
55351da177e4SLinus Torvalds 		} else {
55361da177e4SLinus Torvalds 			/* Narrow Pattern:  FE 01 FD 02 FB 04
55371da177e4SLinus Torvalds 			 * .. 7F 80 01 FE 02 FD ...  80 7F
55381da177e4SLinus Torvalds 			 */
55391da177e4SLinus Torvalds 			byte = 0;
55401da177e4SLinus Torvalds 			for (ii=0; ii < size; ii++, ptr++) {
55411da177e4SLinus Torvalds 				/* Base pattern - first 32 bytes
55421da177e4SLinus Torvalds 				 */
55431da177e4SLinus Torvalds 				if (ii & 0x01) {
55441da177e4SLinus Torvalds 					*ptr = (1 << byte);
55451da177e4SLinus Torvalds 					byte++;
55461da177e4SLinus Torvalds 					byte &= 0x07;
55471da177e4SLinus Torvalds 				} else {
55481da177e4SLinus Torvalds 					*ptr = (char) (~(1 << byte));
55491da177e4SLinus Torvalds 				}
55501da177e4SLinus Torvalds 
55511da177e4SLinus Torvalds 				/* Flip the pattern every 32 bytes
55521da177e4SLinus Torvalds 				 */
55531da177e4SLinus Torvalds 				if (ii & 0x20)
55541da177e4SLinus Torvalds 					*ptr = ~(*ptr);
55551da177e4SLinus Torvalds 			}
55561da177e4SLinus Torvalds 		}
55571da177e4SLinus Torvalds 		break;
55581da177e4SLinus Torvalds 	}
55591da177e4SLinus Torvalds }
55601da177e4SLinus Torvalds #endif /* ~MPTSCSIH_ENABLE_DOMAIN_VALIDATION */
55611da177e4SLinus Torvalds 
55620d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_remove);
55630d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_shutdown);
55640d0c7974SMoore, Eric Dean  #ifdef CONFIG_PM
55650d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_suspend);
55660d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_resume);
55670d0c7974SMoore, Eric Dean  #endif
55680d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_proc_info);
55690d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_info);
55700d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_qcmd);
55710d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_slave_alloc);
55720d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_slave_destroy);
55730d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_slave_configure);
55740d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_abort);
55750d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_dev_reset);
55760d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_bus_reset);
55770d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_host_reset);
55780d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_bios_param);
55790d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_io_done);
55800d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_taskmgmt_complete);
55810d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_scandv_complete);
55820d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_event_process);
55830d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_ioc_reset);
55846e3815baSMoore, Eric Dean EXPORT_SYMBOL(mptscsih_change_queue_depth);
55850d0c7974SMoore, Eric Dean  EXPORT_SYMBOL(mptscsih_timer_expired);
55861da177e4SLinus Torvalds 
55870d0c7974SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
5588