xref: /openbmc/linux/drivers/message/fusion/mptfc.c (revision 9f4203b357d46a66c042df3dedf277ccaa5e2b1d)
12496af39SMoore, Eric Dean  /*
22496af39SMoore, Eric Dean   *  linux/drivers/message/fusion/mptfc.c
32496af39SMoore, Eric Dean   *      For use with LSI Logic PCI chip/adapter(s)
42496af39SMoore, Eric Dean   *      running LSI Logic Fusion MPT (Message Passing Technology) firmware.
52496af39SMoore, Eric Dean   *
6*9f4203b3SEric Moore  *  Copyright (c) 1999-2007 LSI Logic Corporation
72496af39SMoore, Eric Dean   *  (mailto:mpt_linux_developer@lsil.com)
82496af39SMoore, Eric Dean   *
92496af39SMoore, Eric Dean   */
102496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
112496af39SMoore, Eric Dean  /*
122496af39SMoore, Eric Dean      This program is free software; you can redistribute it and/or modify
132496af39SMoore, Eric Dean      it under the terms of the GNU General Public License as published by
142496af39SMoore, Eric Dean      the Free Software Foundation; version 2 of the License.
152496af39SMoore, Eric Dean  
162496af39SMoore, Eric Dean      This program is distributed in the hope that it will be useful,
172496af39SMoore, Eric Dean      but WITHOUT ANY WARRANTY; without even the implied warranty of
182496af39SMoore, Eric Dean      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
192496af39SMoore, Eric Dean      GNU General Public License for more details.
202496af39SMoore, Eric Dean  
212496af39SMoore, Eric Dean      NO WARRANTY
222496af39SMoore, Eric Dean      THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
232496af39SMoore, Eric Dean      CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
242496af39SMoore, Eric Dean      LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
252496af39SMoore, Eric Dean      MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
262496af39SMoore, Eric Dean      solely responsible for determining the appropriateness of using and
272496af39SMoore, Eric Dean      distributing the Program and assumes all risks associated with its
282496af39SMoore, Eric Dean      exercise of rights under this Agreement, including but not limited to
292496af39SMoore, Eric Dean      the risks and costs of program errors, damage to or loss of data,
302496af39SMoore, Eric Dean      programs or equipment, and unavailability or interruption of operations.
312496af39SMoore, Eric Dean  
322496af39SMoore, Eric Dean      DISCLAIMER OF LIABILITY
332496af39SMoore, Eric Dean      NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
342496af39SMoore, Eric Dean      DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
352496af39SMoore, Eric Dean      DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
362496af39SMoore, Eric Dean      ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
372496af39SMoore, Eric Dean      TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
382496af39SMoore, Eric Dean      USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
392496af39SMoore, Eric Dean      HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
402496af39SMoore, Eric Dean  
412496af39SMoore, Eric Dean      You should have received a copy of the GNU General Public License
422496af39SMoore, Eric Dean      along with this program; if not, write to the Free Software
432496af39SMoore, Eric Dean      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
442496af39SMoore, Eric Dean  */
452496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
462496af39SMoore, Eric Dean  #include "linux_compat.h"	/* linux-2.6 tweaks */
472496af39SMoore, Eric Dean  #include <linux/module.h>
482496af39SMoore, Eric Dean  #include <linux/kernel.h>
492496af39SMoore, Eric Dean  #include <linux/init.h>
502496af39SMoore, Eric Dean  #include <linux/errno.h>
512496af39SMoore, Eric Dean  #include <linux/kdev_t.h>
522496af39SMoore, Eric Dean  #include <linux/blkdev.h>
532496af39SMoore, Eric Dean  #include <linux/delay.h>	/* for mdelay */
542496af39SMoore, Eric Dean  #include <linux/interrupt.h>	/* needed for in_interrupt() proto */
552496af39SMoore, Eric Dean  #include <linux/reboot.h>	/* notifier code */
562496af39SMoore, Eric Dean  #include <linux/sched.h>
572496af39SMoore, Eric Dean  #include <linux/workqueue.h>
5805e8ec17SMichael Reed #include <linux/sort.h>
592496af39SMoore, Eric Dean  
602496af39SMoore, Eric Dean  #include <scsi/scsi.h>
612496af39SMoore, Eric Dean  #include <scsi/scsi_cmnd.h>
622496af39SMoore, Eric Dean  #include <scsi/scsi_device.h>
632496af39SMoore, Eric Dean  #include <scsi/scsi_host.h>
642496af39SMoore, Eric Dean  #include <scsi/scsi_tcq.h>
6505e8ec17SMichael Reed #include <scsi/scsi_transport_fc.h>
662496af39SMoore, Eric Dean  
672496af39SMoore, Eric Dean  #include "mptbase.h"
682496af39SMoore, Eric Dean  #include "mptscsih.h"
692496af39SMoore, Eric Dean  
702496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
712496af39SMoore, Eric Dean  #define my_NAME		"Fusion MPT FC Host driver"
722496af39SMoore, Eric Dean  #define my_VERSION	MPT_LINUX_VERSION_COMMON
732496af39SMoore, Eric Dean  #define MYNAM		"mptfc"
742496af39SMoore, Eric Dean  
752496af39SMoore, Eric Dean  MODULE_AUTHOR(MODULEAUTHOR);
762496af39SMoore, Eric Dean  MODULE_DESCRIPTION(my_NAME);
772496af39SMoore, Eric Dean  MODULE_LICENSE("GPL");
78*9f4203b3SEric Moore MODULE_VERSION(my_VERSION);
792496af39SMoore, Eric Dean  
802496af39SMoore, Eric Dean  /* Command line args */
8105e8ec17SMichael Reed #define MPTFC_DEV_LOSS_TMO (60)
8205e8ec17SMichael Reed static int mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;	/* reasonable default */
8305e8ec17SMichael Reed module_param(mptfc_dev_loss_tmo, int, 0);
8405e8ec17SMichael Reed MODULE_PARM_DESC(mptfc_dev_loss_tmo, " Initial time the driver programs the "
8505e8ec17SMichael Reed     				     " transport to wait for an rport to "
8605e8ec17SMichael Reed 				     " return following a device loss event."
8705e8ec17SMichael Reed 				     "  Default=60.");
8805e8ec17SMichael Reed 
892496af39SMoore, Eric Dean  static int	mptfcDoneCtx = -1;
902496af39SMoore, Eric Dean  static int	mptfcTaskCtx = -1;
912496af39SMoore, Eric Dean  static int	mptfcInternalCtx = -1; /* Used only for internal commands */
922496af39SMoore, Eric Dean  
933bc7bf1dSMichael Reed static int mptfc_target_alloc(struct scsi_target *starget);
943bc7bf1dSMichael Reed static int mptfc_slave_alloc(struct scsi_device *sdev);
9505e8ec17SMichael Reed static int mptfc_qcmd(struct scsi_cmnd *SCpnt,
9605e8ec17SMichael Reed 		      void (*done)(struct scsi_cmnd *));
973bc7bf1dSMichael Reed static void mptfc_target_destroy(struct scsi_target *starget);
9805e8ec17SMichael Reed static void mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout);
9905e8ec17SMichael Reed static void __devexit mptfc_remove(struct pci_dev *pdev);
10035508e46SMichael Reed static int mptfc_abort(struct scsi_cmnd *SCpnt);
10135508e46SMichael Reed static int mptfc_dev_reset(struct scsi_cmnd *SCpnt);
10235508e46SMichael Reed static int mptfc_bus_reset(struct scsi_cmnd *SCpnt);
10335508e46SMichael Reed static int mptfc_host_reset(struct scsi_cmnd *SCpnt);
10405e8ec17SMichael Reed 
1052496af39SMoore, Eric Dean  static struct scsi_host_template mptfc_driver_template = {
106f78496daSMoore, Eric Dean 	.module				= THIS_MODULE,
1072496af39SMoore, Eric Dean  	.proc_name			= "mptfc",
1082496af39SMoore, Eric Dean  	.proc_info			= mptscsih_proc_info,
1092496af39SMoore, Eric Dean  	.name				= "MPT FC Host",
1102496af39SMoore, Eric Dean  	.info				= mptscsih_info,
11105e8ec17SMichael Reed 	.queuecommand			= mptfc_qcmd,
1123bc7bf1dSMichael Reed 	.target_alloc			= mptfc_target_alloc,
11305e8ec17SMichael Reed 	.slave_alloc			= mptfc_slave_alloc,
1142496af39SMoore, Eric Dean  	.slave_configure		= mptscsih_slave_configure,
1153bc7bf1dSMichael Reed 	.target_destroy			= mptfc_target_destroy,
1162496af39SMoore, Eric Dean  	.slave_destroy			= mptscsih_slave_destroy,
1176e3815baSMoore, Eric Dean 	.change_queue_depth 		= mptscsih_change_queue_depth,
11835508e46SMichael Reed 	.eh_abort_handler		= mptfc_abort,
11935508e46SMichael Reed 	.eh_device_reset_handler	= mptfc_dev_reset,
12035508e46SMichael Reed 	.eh_bus_reset_handler		= mptfc_bus_reset,
12135508e46SMichael Reed 	.eh_host_reset_handler		= mptfc_host_reset,
1222496af39SMoore, Eric Dean  	.bios_param			= mptscsih_bios_param,
1232496af39SMoore, Eric Dean  	.can_queue			= MPT_FC_CAN_QUEUE,
1242496af39SMoore, Eric Dean  	.this_id			= -1,
1252496af39SMoore, Eric Dean  	.sg_tablesize			= MPT_SCSI_SG_DEPTH,
1262496af39SMoore, Eric Dean  	.max_sectors			= 8192,
1272496af39SMoore, Eric Dean  	.cmd_per_lun			= 7,
1282496af39SMoore, Eric Dean  	.use_clustering			= ENABLE_CLUSTERING,
1292496af39SMoore, Eric Dean  };
1302496af39SMoore, Eric Dean  
1312496af39SMoore, Eric Dean  /****************************************************************************
1322496af39SMoore, Eric Dean   * Supported hardware
1332496af39SMoore, Eric Dean   */
1342496af39SMoore, Eric Dean  
1352496af39SMoore, Eric Dean  static struct pci_device_id mptfc_pci_table[] = {
13687cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC909,
1372496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
13887cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919,
1392496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14087cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929,
1412496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14287cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919X,
1432496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14487cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929X,
1452496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14687cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC939X,
1473fadc59dSMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14887cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949X,
1493fadc59dSMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
15087cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949E,
1516d5b0c31SMoore, Eric 		PCI_ANY_ID, PCI_ANY_ID },
1522496af39SMoore, Eric Dean  	{0}	/* Terminating entry */
1532496af39SMoore, Eric Dean  };
1542496af39SMoore, Eric Dean  MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
1552496af39SMoore, Eric Dean  
15605e8ec17SMichael Reed static struct scsi_transport_template *mptfc_transport_template = NULL;
15705e8ec17SMichael Reed 
15803fbcbcdSAdrian Bunk static struct fc_function_template mptfc_transport_functions = {
15905e8ec17SMichael Reed 	.dd_fcrport_size = 8,
16005e8ec17SMichael Reed 	.show_host_node_name = 1,
16105e8ec17SMichael Reed 	.show_host_port_name = 1,
16205e8ec17SMichael Reed 	.show_host_supported_classes = 1,
16305e8ec17SMichael Reed 	.show_host_port_id = 1,
16405e8ec17SMichael Reed 	.show_rport_supported_classes = 1,
16505e8ec17SMichael Reed 	.show_starget_node_name = 1,
16605e8ec17SMichael Reed 	.show_starget_port_name = 1,
16705e8ec17SMichael Reed 	.show_starget_port_id = 1,
16805e8ec17SMichael Reed 	.set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
16905e8ec17SMichael Reed 	.show_rport_dev_loss_tmo = 1,
1705d947f2bSMichael Reed 	.show_host_supported_speeds = 1,
1715d947f2bSMichael Reed 	.show_host_maxframe_size = 1,
1725d947f2bSMichael Reed 	.show_host_speed = 1,
1735d947f2bSMichael Reed 	.show_host_fabric_name = 1,
1745d947f2bSMichael Reed 	.show_host_port_type = 1,
1755d947f2bSMichael Reed 	.show_host_port_state = 1,
1765d947f2bSMichael Reed 	.show_host_symbolic_name = 1,
17705e8ec17SMichael Reed };
17805e8ec17SMichael Reed 
17935508e46SMichael Reed static int
18035508e46SMichael Reed mptfc_block_error_handler(struct scsi_cmnd *SCpnt,
18135508e46SMichael Reed 			  int (*func)(struct scsi_cmnd *SCpnt),
18235508e46SMichael Reed 			  const char *caller)
18335508e46SMichael Reed {
18435508e46SMichael Reed 	struct scsi_device	*sdev = SCpnt->device;
18535508e46SMichael Reed 	struct Scsi_Host	*shost = sdev->host;
18635508e46SMichael Reed 	struct fc_rport		*rport = starget_to_rport(scsi_target(sdev));
18735508e46SMichael Reed 	unsigned long		flags;
18835508e46SMichael Reed 	int			ready;
18935508e46SMichael Reed 
19035508e46SMichael Reed 	spin_lock_irqsave(shost->host_lock, flags);
19135508e46SMichael Reed 	while ((ready = fc_remote_port_chkready(rport) >> 16) == DID_IMM_RETRY) {
19235508e46SMichael Reed 		spin_unlock_irqrestore(shost->host_lock, flags);
19335508e46SMichael Reed 		dfcprintk ((MYIOC_s_INFO_FMT
19435508e46SMichael Reed 			"mptfc_block_error_handler.%d: %d:%d, port status is "
19535508e46SMichael Reed 			"DID_IMM_RETRY, deferring %s recovery.\n",
19635508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->name,
19735508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->sh->host_no,
19835508e46SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,caller));
19935508e46SMichael Reed 		msleep(1000);
20035508e46SMichael Reed 		spin_lock_irqsave(shost->host_lock, flags);
20135508e46SMichael Reed 	}
20235508e46SMichael Reed 	spin_unlock_irqrestore(shost->host_lock, flags);
20335508e46SMichael Reed 
20435508e46SMichael Reed 	if (ready == DID_NO_CONNECT || !SCpnt->device->hostdata) {
20535508e46SMichael Reed 		dfcprintk ((MYIOC_s_INFO_FMT
20635508e46SMichael Reed 			"%s.%d: %d:%d, failing recovery, "
20735508e46SMichael Reed 			"port state %d, vdev %p.\n", caller,
20835508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->name,
20935508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->sh->host_no,
21035508e46SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,ready,
21135508e46SMichael Reed 			SCpnt->device->hostdata));
21235508e46SMichael Reed 		return FAILED;
21335508e46SMichael Reed 	}
21435508e46SMichael Reed 	dfcprintk ((MYIOC_s_INFO_FMT
21535508e46SMichael Reed 		"%s.%d: %d:%d, executing recovery.\n", caller,
21635508e46SMichael Reed 		((MPT_SCSI_HOST *) shost->hostdata)->ioc->name,
21735508e46SMichael Reed 		((MPT_SCSI_HOST *) shost->hostdata)->ioc->sh->host_no,
21835508e46SMichael Reed 		SCpnt->device->id,SCpnt->device->lun));
21935508e46SMichael Reed 	return (*func)(SCpnt);
22035508e46SMichael Reed }
22135508e46SMichael Reed 
22235508e46SMichael Reed static int
22335508e46SMichael Reed mptfc_abort(struct scsi_cmnd *SCpnt)
22435508e46SMichael Reed {
22535508e46SMichael Reed 	return
22635508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_abort, __FUNCTION__);
22735508e46SMichael Reed }
22835508e46SMichael Reed 
22935508e46SMichael Reed static int
23035508e46SMichael Reed mptfc_dev_reset(struct scsi_cmnd *SCpnt)
23135508e46SMichael Reed {
23235508e46SMichael Reed 	return
23335508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_dev_reset, __FUNCTION__);
23435508e46SMichael Reed }
23535508e46SMichael Reed 
23635508e46SMichael Reed static int
23735508e46SMichael Reed mptfc_bus_reset(struct scsi_cmnd *SCpnt)
23835508e46SMichael Reed {
23935508e46SMichael Reed 	return
24035508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_bus_reset, __FUNCTION__);
24135508e46SMichael Reed }
24235508e46SMichael Reed 
24335508e46SMichael Reed static int
24435508e46SMichael Reed mptfc_host_reset(struct scsi_cmnd *SCpnt)
24535508e46SMichael Reed {
24635508e46SMichael Reed 	return
24735508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_host_reset, __FUNCTION__);
24835508e46SMichael Reed }
24935508e46SMichael Reed 
25005e8ec17SMichael Reed static void
25105e8ec17SMichael Reed mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
25205e8ec17SMichael Reed {
25305e8ec17SMichael Reed 	if (timeout > 0)
25405e8ec17SMichael Reed 		rport->dev_loss_tmo = timeout;
25505e8ec17SMichael Reed 	else
25605e8ec17SMichael Reed 		rport->dev_loss_tmo = mptfc_dev_loss_tmo;
25705e8ec17SMichael Reed }
25805e8ec17SMichael Reed 
25905e8ec17SMichael Reed static int
26005e8ec17SMichael Reed mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
26105e8ec17SMichael Reed {
26205e8ec17SMichael Reed 	FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
26305e8ec17SMichael Reed 	FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
26405e8ec17SMichael Reed 
26505e8ec17SMichael Reed 	if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
26605e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
26705e8ec17SMichael Reed 			return 0;
26805e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
26905e8ec17SMichael Reed 			return -1;
27005e8ec17SMichael Reed 		return 1;
27105e8ec17SMichael Reed 	}
27205e8ec17SMichael Reed 	if ((*aa)->CurrentBus < (*bb)->CurrentBus)
27305e8ec17SMichael Reed 		return -1;
27405e8ec17SMichael Reed 	return 1;
27505e8ec17SMichael Reed }
27605e8ec17SMichael Reed 
27705e8ec17SMichael Reed static int
27805e8ec17SMichael Reed mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
27905e8ec17SMichael Reed 	void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
28005e8ec17SMichael Reed {
28105e8ec17SMichael Reed 	ConfigPageHeader_t	 hdr;
28205e8ec17SMichael Reed 	CONFIGPARMS		 cfg;
28305e8ec17SMichael Reed 	FCDevicePage0_t		*ppage0_alloc, *fc;
28405e8ec17SMichael Reed 	dma_addr_t		 page0_dma;
28505e8ec17SMichael Reed 	int			 data_sz;
28605e8ec17SMichael Reed 	int			 ii;
28705e8ec17SMichael Reed 
28805e8ec17SMichael Reed 	FCDevicePage0_t		*p0_array=NULL, *p_p0;
28905e8ec17SMichael Reed 	FCDevicePage0_t		**pp0_array=NULL, **p_pp0;
29005e8ec17SMichael Reed 
29105e8ec17SMichael Reed 	int			 rc = -ENOMEM;
29205e8ec17SMichael Reed 	U32			 port_id = 0xffffff;
29305e8ec17SMichael Reed 	int			 num_targ = 0;
29405e8ec17SMichael Reed 	int			 max_bus = ioc->facts.MaxBuses;
29505e8ec17SMichael Reed 	int			 max_targ = ioc->facts.MaxDevices;
29605e8ec17SMichael Reed 
29705e8ec17SMichael Reed 	if (max_bus == 0 || max_targ == 0)
29805e8ec17SMichael Reed 		goto out;
29905e8ec17SMichael Reed 
30005e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
30105e8ec17SMichael Reed 	p_p0 = p0_array =  kzalloc(data_sz, GFP_KERNEL);
30205e8ec17SMichael Reed 	if (!p0_array)
30305e8ec17SMichael Reed 		goto out;
30405e8ec17SMichael Reed 
30505e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
30605e8ec17SMichael Reed 	p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
30705e8ec17SMichael Reed 	if (!pp0_array)
30805e8ec17SMichael Reed 		goto out;
30905e8ec17SMichael Reed 
31005e8ec17SMichael Reed 	do {
31105e8ec17SMichael Reed 		/* Get FC Device Page 0 header */
31205e8ec17SMichael Reed 		hdr.PageVersion = 0;
31305e8ec17SMichael Reed 		hdr.PageLength = 0;
31405e8ec17SMichael Reed 		hdr.PageNumber = 0;
31505e8ec17SMichael Reed 		hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
31605e8ec17SMichael Reed 		cfg.cfghdr.hdr = &hdr;
31705e8ec17SMichael Reed 		cfg.physAddr = -1;
31805e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
31905e8ec17SMichael Reed 		cfg.dir = 0;
32005e8ec17SMichael Reed 		cfg.pageAddr = port_id;
32105e8ec17SMichael Reed 		cfg.timeout = 0;
32205e8ec17SMichael Reed 
32305e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) != 0)
32405e8ec17SMichael Reed 			break;
32505e8ec17SMichael Reed 
32605e8ec17SMichael Reed 		if (hdr.PageLength <= 0)
32705e8ec17SMichael Reed 			break;
32805e8ec17SMichael Reed 
32905e8ec17SMichael Reed 		data_sz = hdr.PageLength * 4;
33005e8ec17SMichael Reed 		ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
33105e8ec17SMichael Reed 		    					&page0_dma);
33205e8ec17SMichael Reed 		rc = -ENOMEM;
33305e8ec17SMichael Reed 		if (!ppage0_alloc)
33405e8ec17SMichael Reed 			break;
33505e8ec17SMichael Reed 
33605e8ec17SMichael Reed 		cfg.physAddr = page0_dma;
33705e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
33805e8ec17SMichael Reed 
33905e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
34005e8ec17SMichael Reed 			ppage0_alloc->PortIdentifier =
34105e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->PortIdentifier);
34205e8ec17SMichael Reed 
34305e8ec17SMichael Reed 			ppage0_alloc->WWNN.Low =
34405e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.Low);
34505e8ec17SMichael Reed 
34605e8ec17SMichael Reed 			ppage0_alloc->WWNN.High =
34705e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.High);
34805e8ec17SMichael Reed 
34905e8ec17SMichael Reed 			ppage0_alloc->WWPN.Low =
35005e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.Low);
35105e8ec17SMichael Reed 
35205e8ec17SMichael Reed 			ppage0_alloc->WWPN.High =
35305e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.High);
35405e8ec17SMichael Reed 
35505e8ec17SMichael Reed 			ppage0_alloc->BBCredit =
35605e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->BBCredit);
35705e8ec17SMichael Reed 
35805e8ec17SMichael Reed 			ppage0_alloc->MaxRxFrameSize =
35905e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
36005e8ec17SMichael Reed 
36105e8ec17SMichael Reed 			port_id = ppage0_alloc->PortIdentifier;
36205e8ec17SMichael Reed 			num_targ++;
36305e8ec17SMichael Reed 			*p_p0 = *ppage0_alloc;	/* save data */
36405e8ec17SMichael Reed 			*p_pp0++ = p_p0++;	/* save addr */
36505e8ec17SMichael Reed 		}
36605e8ec17SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz,
36705e8ec17SMichael Reed 		    			(u8 *) ppage0_alloc, page0_dma);
36805e8ec17SMichael Reed 		if (rc != 0)
36905e8ec17SMichael Reed 			break;
37005e8ec17SMichael Reed 
37105e8ec17SMichael Reed 	} while (port_id <= 0xff0000);
37205e8ec17SMichael Reed 
37305e8ec17SMichael Reed 	if (num_targ) {
37405e8ec17SMichael Reed 		/* sort array */
37505e8ec17SMichael Reed 		if (num_targ > 1)
37605e8ec17SMichael Reed 			sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
37705e8ec17SMichael Reed 				mptfc_FcDevPage0_cmp_func, NULL);
37805e8ec17SMichael Reed 		/* call caller's func for each targ */
37905e8ec17SMichael Reed 		for (ii = 0; ii < num_targ;  ii++) {
38005e8ec17SMichael Reed 			fc = *(pp0_array+ii);
38105e8ec17SMichael Reed 			func(ioc, ioc_port, fc);
38205e8ec17SMichael Reed 		}
38305e8ec17SMichael Reed 	}
38405e8ec17SMichael Reed 
38505e8ec17SMichael Reed  out:
38605e8ec17SMichael Reed 	kfree(pp0_array);
38705e8ec17SMichael Reed 	kfree(p0_array);
38805e8ec17SMichael Reed 	return rc;
38905e8ec17SMichael Reed }
39005e8ec17SMichael Reed 
39105e8ec17SMichael Reed static int
39205e8ec17SMichael Reed mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
39305e8ec17SMichael Reed {
39405e8ec17SMichael Reed 	/* not currently usable */
39505e8ec17SMichael Reed 	if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
39605e8ec17SMichael Reed 			  MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
39705e8ec17SMichael Reed 		return -1;
39805e8ec17SMichael Reed 
39905e8ec17SMichael Reed 	if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
40005e8ec17SMichael Reed 		return -1;
40105e8ec17SMichael Reed 
40205e8ec17SMichael Reed 	if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
40305e8ec17SMichael Reed 		return -1;
40405e8ec17SMichael Reed 
40505e8ec17SMichael Reed 	/*
40605e8ec17SMichael Reed 	 * board data structure already normalized to platform endianness
40705e8ec17SMichael Reed 	 * shifted to avoid unaligned access on 64 bit architecture
40805e8ec17SMichael Reed 	 */
40905e8ec17SMichael Reed 	rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
41005e8ec17SMichael Reed 	rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
41105e8ec17SMichael Reed 	rid->port_id =   pg0->PortIdentifier;
41205e8ec17SMichael Reed 	rid->roles = FC_RPORT_ROLE_UNKNOWN;
41305e8ec17SMichael Reed 
41405e8ec17SMichael Reed 	return 0;
41505e8ec17SMichael Reed }
41605e8ec17SMichael Reed 
41705e8ec17SMichael Reed static void
41805e8ec17SMichael Reed mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
41905e8ec17SMichael Reed {
42005e8ec17SMichael Reed 	struct fc_rport_identifiers rport_ids;
42105e8ec17SMichael Reed 	struct fc_rport		*rport;
42205e8ec17SMichael Reed 	struct mptfc_rport_info	*ri;
4233bc7bf1dSMichael Reed 	int			new_ri = 1;
42465207fedSMoore, Eric 	u64			pn, nn;
4253bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
4266dd727daSmdr@sgi.com 	u32			roles = FC_RPORT_ROLE_UNKNOWN;
42705e8ec17SMichael Reed 
42805e8ec17SMichael Reed 	if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
42905e8ec17SMichael Reed 		return;
43005e8ec17SMichael Reed 
4316dd727daSmdr@sgi.com 	roles |= FC_RPORT_ROLE_FCP_TARGET;
4326dd727daSmdr@sgi.com 	if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
4336dd727daSmdr@sgi.com 		roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4346dd727daSmdr@sgi.com 
43505e8ec17SMichael Reed 	/* scan list looking for a match */
43605e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
4373bc7bf1dSMichael Reed 		pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
4383bc7bf1dSMichael Reed 		if (pn == rport_ids.port_name) {	/* match */
43905e8ec17SMichael Reed 			list_move_tail(&ri->list, &ioc->fc_rports);
4403bc7bf1dSMichael Reed 			new_ri = 0;
44105e8ec17SMichael Reed 			break;
44205e8ec17SMichael Reed 		}
44305e8ec17SMichael Reed 	}
4443bc7bf1dSMichael Reed 	if (new_ri) {	/* allocate one */
44505e8ec17SMichael Reed 		ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
44605e8ec17SMichael Reed 		if (!ri)
44705e8ec17SMichael Reed 			return;
44805e8ec17SMichael Reed 		list_add_tail(&ri->list, &ioc->fc_rports);
44905e8ec17SMichael Reed 	}
45005e8ec17SMichael Reed 
45105e8ec17SMichael Reed 	ri->pg0 = *pg0;	/* add/update pg0 data */
45205e8ec17SMichael Reed 	ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
45305e8ec17SMichael Reed 
4543bc7bf1dSMichael Reed 	/* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
45505e8ec17SMichael Reed 	if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
45605e8ec17SMichael Reed 		ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
45705e8ec17SMichael Reed 		rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
45805e8ec17SMichael Reed 		if (rport) {
45905e8ec17SMichael Reed 			ri->rport = rport;
4603bc7bf1dSMichael Reed 			if (new_ri) /* may have been reset by user */
46105e8ec17SMichael Reed 				rport->dev_loss_tmo = mptfc_dev_loss_tmo;
46205e8ec17SMichael Reed 			/*
46305e8ec17SMichael Reed 			 * if already mapped, remap here.  If not mapped,
4643bc7bf1dSMichael Reed 			 * target_alloc will allocate vtarget and map,
4653bc7bf1dSMichael Reed 			 * slave_alloc will fill in vdev from vtarget.
46605e8ec17SMichael Reed 			 */
4673bc7bf1dSMichael Reed 			if (ri->starget) {
4683bc7bf1dSMichael Reed 				vtarget = ri->starget->hostdata;
4693bc7bf1dSMichael Reed 				if (vtarget) {
4703bc7bf1dSMichael Reed 					vtarget->target_id = pg0->CurrentTargetID;
4713bc7bf1dSMichael Reed 					vtarget->bus_id = pg0->CurrentBus;
47205e8ec17SMichael Reed 				}
4733bc7bf1dSMichael Reed 			}
47465207fedSMoore, Eric 			*((struct mptfc_rport_info **)rport->dd_data) = ri;
4756dd727daSmdr@sgi.com 			/* scan will be scheduled once rport becomes a target */
4766dd727daSmdr@sgi.com 			fc_remote_port_rolechg(rport,roles);
47765207fedSMoore, Eric 
47865207fedSMoore, Eric 			pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
47965207fedSMoore, Eric 			nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
4803bc7bf1dSMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
4813bc7bf1dSMichael Reed 				"mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
48205e8ec17SMichael Reed 				"rport tid %d, tmo %d\n",
4833bc7bf1dSMichael Reed 					ioc->name,
484914c2d8eSMoore, Eric 					ioc->sh->host_no,
48505e8ec17SMichael Reed 					pg0->PortIdentifier,
48665207fedSMoore, Eric 					(unsigned long long)nn,
48765207fedSMoore, Eric 					(unsigned long long)pn,
48805e8ec17SMichael Reed 					pg0->CurrentTargetID,
48905e8ec17SMichael Reed 					ri->rport->scsi_target_id,
4903bc7bf1dSMichael Reed 					ri->rport->dev_loss_tmo));
49105e8ec17SMichael Reed 		} else {
49205e8ec17SMichael Reed 			list_del(&ri->list);
49305e8ec17SMichael Reed 			kfree(ri);
49405e8ec17SMichael Reed 			ri = NULL;
49505e8ec17SMichael Reed 		}
49605e8ec17SMichael Reed 	}
49705e8ec17SMichael Reed }
49805e8ec17SMichael Reed 
49905e8ec17SMichael Reed /*
5003bc7bf1dSMichael Reed  *	OS entry point to allow for host driver to free allocated memory
5013bc7bf1dSMichael Reed  *	Called if no device present or device being unloaded
5023bc7bf1dSMichael Reed  */
5033bc7bf1dSMichael Reed static void
5043bc7bf1dSMichael Reed mptfc_target_destroy(struct scsi_target *starget)
5053bc7bf1dSMichael Reed {
5063bc7bf1dSMichael Reed 	struct fc_rport		*rport;
5073bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
5083bc7bf1dSMichael Reed 
5093bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
5103bc7bf1dSMichael Reed 	if (rport) {
5113bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
5123bc7bf1dSMichael Reed 		if (ri)	/* better be! */
5133bc7bf1dSMichael Reed 			ri->starget = NULL;
5143bc7bf1dSMichael Reed 	}
5153bc7bf1dSMichael Reed 	if (starget->hostdata)
5163bc7bf1dSMichael Reed 		kfree(starget->hostdata);
5173bc7bf1dSMichael Reed 	starget->hostdata = NULL;
5183bc7bf1dSMichael Reed }
5193bc7bf1dSMichael Reed 
5203bc7bf1dSMichael Reed /*
5213bc7bf1dSMichael Reed  *	OS entry point to allow host driver to alloc memory
5223bc7bf1dSMichael Reed  *	for each scsi target. Called once per device the bus scan.
5233bc7bf1dSMichael Reed  *	Return non-zero if allocation fails.
5243bc7bf1dSMichael Reed  */
5253bc7bf1dSMichael Reed static int
5263bc7bf1dSMichael Reed mptfc_target_alloc(struct scsi_target *starget)
5273bc7bf1dSMichael Reed {
5283bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
5293bc7bf1dSMichael Reed 	struct fc_rport		*rport;
5303bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
5313bc7bf1dSMichael Reed 	int			rc;
5323bc7bf1dSMichael Reed 
5333bc7bf1dSMichael Reed 	vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
5343bc7bf1dSMichael Reed 	if (!vtarget)
5353bc7bf1dSMichael Reed 		return -ENOMEM;
5363bc7bf1dSMichael Reed 	starget->hostdata = vtarget;
5373bc7bf1dSMichael Reed 
5383bc7bf1dSMichael Reed 	rc = -ENODEV;
5393bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
5403bc7bf1dSMichael Reed 	if (rport) {
5413bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
5423bc7bf1dSMichael Reed 		if (ri) {	/* better be! */
5433bc7bf1dSMichael Reed 			vtarget->target_id = ri->pg0.CurrentTargetID;
5443bc7bf1dSMichael Reed 			vtarget->bus_id = ri->pg0.CurrentBus;
5453bc7bf1dSMichael Reed 			ri->starget = starget;
5463bc7bf1dSMichael Reed 			rc = 0;
5473bc7bf1dSMichael Reed 		}
5483bc7bf1dSMichael Reed 	}
5493bc7bf1dSMichael Reed 	if (rc != 0) {
5503bc7bf1dSMichael Reed 		kfree(vtarget);
5513bc7bf1dSMichael Reed 		starget->hostdata = NULL;
5523bc7bf1dSMichael Reed 	}
5533bc7bf1dSMichael Reed 
5543bc7bf1dSMichael Reed 	return rc;
5553bc7bf1dSMichael Reed }
5563bc7bf1dSMichael Reed 
5573bc7bf1dSMichael Reed /*
55805e8ec17SMichael Reed  *	OS entry point to allow host driver to alloc memory
55905e8ec17SMichael Reed  *	for each scsi device. Called once per device the bus scan.
56005e8ec17SMichael Reed  *	Return non-zero if allocation fails.
56105e8ec17SMichael Reed  *	Init memory once per LUN.
56205e8ec17SMichael Reed  */
56303fbcbcdSAdrian Bunk static int
56405e8ec17SMichael Reed mptfc_slave_alloc(struct scsi_device *sdev)
56505e8ec17SMichael Reed {
56605e8ec17SMichael Reed 	MPT_SCSI_HOST		*hd;
56705e8ec17SMichael Reed 	VirtTarget		*vtarget;
56805e8ec17SMichael Reed 	VirtDevice		*vdev;
56905e8ec17SMichael Reed 	struct scsi_target	*starget;
57005e8ec17SMichael Reed 	struct fc_rport		*rport;
57105e8ec17SMichael Reed 
57205e8ec17SMichael Reed 
57365207fedSMoore, Eric 	starget = scsi_target(sdev);
57465207fedSMoore, Eric 	rport = starget_to_rport(starget);
57505e8ec17SMichael Reed 
57605e8ec17SMichael Reed 	if (!rport || fc_remote_port_chkready(rport))
57705e8ec17SMichael Reed 		return -ENXIO;
57805e8ec17SMichael Reed 
57905e8ec17SMichael Reed 	hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
58005e8ec17SMichael Reed 
5813bc7bf1dSMichael Reed 	vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
58205e8ec17SMichael Reed 	if (!vdev) {
58305e8ec17SMichael Reed 		printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
58405e8ec17SMichael Reed 				hd->ioc->name, sizeof(VirtDevice));
58505e8ec17SMichael Reed 		return -ENOMEM;
58605e8ec17SMichael Reed 	}
58705e8ec17SMichael Reed 
58805e8ec17SMichael Reed 
58905e8ec17SMichael Reed 	sdev->hostdata = vdev;
59005e8ec17SMichael Reed 	vtarget = starget->hostdata;
5913bc7bf1dSMichael Reed 
59205e8ec17SMichael Reed 	if (vtarget->num_luns == 0) {
5933bc7bf1dSMichael Reed 		vtarget->ioc_id = hd->ioc->id;
594ba856d32SEric Moore 		vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
59505e8ec17SMichael Reed 		hd->Targets[sdev->id] = vtarget;
59605e8ec17SMichael Reed 	}
59705e8ec17SMichael Reed 
59805e8ec17SMichael Reed 	vdev->vtarget = vtarget;
59905e8ec17SMichael Reed 	vdev->lun = sdev->lun;
60005e8ec17SMichael Reed 
60105e8ec17SMichael Reed 	vtarget->num_luns++;
60205e8ec17SMichael Reed 
60365207fedSMoore, Eric 
604914c2d8eSMoore, Eric #ifdef DMPT_DEBUG_FC
605914c2d8eSMoore, Eric 	{
60665207fedSMoore, Eric 	u64 nn, pn;
607914c2d8eSMoore, Eric 	struct mptfc_rport_info *ri;
608914c2d8eSMoore, Eric 	ri = *((struct mptfc_rport_info **)rport->dd_data);
60965207fedSMoore, Eric 	pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
61065207fedSMoore, Eric 	nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
6113bc7bf1dSMichael Reed 	dfcprintk ((MYIOC_s_INFO_FMT
6123bc7bf1dSMichael Reed 		"mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
61305e8ec17SMichael Reed 	        "CurrentTargetID %d, %x %llx %llx\n",
614914c2d8eSMoore, Eric 		hd->ioc->name,
61505e8ec17SMichael Reed 		sdev->host->host_no,
61605e8ec17SMichael Reed 		vtarget->num_luns,
61705e8ec17SMichael Reed 		sdev->id, ri->pg0.CurrentTargetID,
61865207fedSMoore, Eric 		ri->pg0.PortIdentifier,
61965207fedSMoore, Eric 		(unsigned long long)pn,
62065207fedSMoore, Eric 		(unsigned long long)nn));
621914c2d8eSMoore, Eric 	}
622914c2d8eSMoore, Eric #endif
62305e8ec17SMichael Reed 
62405e8ec17SMichael Reed 	return 0;
62505e8ec17SMichael Reed }
62605e8ec17SMichael Reed 
62705e8ec17SMichael Reed static int
62805e8ec17SMichael Reed mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
62905e8ec17SMichael Reed {
6303bc7bf1dSMichael Reed 	struct mptfc_rport_info	*ri;
63105e8ec17SMichael Reed 	struct fc_rport	*rport = starget_to_rport(scsi_target(SCpnt->device));
63205e8ec17SMichael Reed 	int		err;
63305e8ec17SMichael Reed 
63405e8ec17SMichael Reed 	err = fc_remote_port_chkready(rport);
63505e8ec17SMichael Reed 	if (unlikely(err)) {
63605e8ec17SMichael Reed 		SCpnt->result = err;
63705e8ec17SMichael Reed 		done(SCpnt);
63805e8ec17SMichael Reed 		return 0;
63905e8ec17SMichael Reed 	}
6403bc7bf1dSMichael Reed 
64135508e46SMichael Reed 	if (!SCpnt->device->hostdata) {	/* vdev */
64235508e46SMichael Reed 		SCpnt->result = DID_NO_CONNECT << 16;
64335508e46SMichael Reed 		done(SCpnt);
64435508e46SMichael Reed 		return 0;
64535508e46SMichael Reed 	}
64635508e46SMichael Reed 
64765207fedSMoore, Eric 	/* dd_data is null until finished adding target */
64865207fedSMoore, Eric 	ri = *((struct mptfc_rport_info **)rport->dd_data);
64965207fedSMoore, Eric 	if (unlikely(!ri)) {
65065207fedSMoore, Eric 		dfcprintk ((MYIOC_s_INFO_FMT
65165207fedSMoore, Eric 			"mptfc_qcmd.%d: %d:%d, dd_data is null.\n",
65265207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
65365207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
65465207fedSMoore, Eric 			SCpnt->device->id,SCpnt->device->lun));
65565207fedSMoore, Eric 		SCpnt->result = DID_IMM_RETRY << 16;
65665207fedSMoore, Eric 		done(SCpnt);
65765207fedSMoore, Eric 		return 0;
65865207fedSMoore, Eric 	}
65965207fedSMoore, Eric 
66065207fedSMoore, Eric 	err = mptscsih_qcmd(SCpnt,done);
66165207fedSMoore, Eric #ifdef DMPT_DEBUG_FC
66265207fedSMoore, Eric 	if (unlikely(err)) {
66365207fedSMoore, Eric 		dfcprintk ((MYIOC_s_INFO_FMT
664419835e2SMichael Reed 			"mptfc_qcmd.%d: %d:%d, mptscsih_qcmd returns non-zero, (%x).\n",
66565207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
66665207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
667419835e2SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,err));
66865207fedSMoore, Eric 	}
66965207fedSMoore, Eric #endif
67065207fedSMoore, Eric 	return err;
67105e8ec17SMichael Reed }
67205e8ec17SMichael Reed 
67380d3ac77SMichael Reed /*
67480d3ac77SMichael Reed  *	mptfc_GetFcPortPage0 - Fetch FCPort config Page0.
67580d3ac77SMichael Reed  *	@ioc: Pointer to MPT_ADAPTER structure
67680d3ac77SMichael Reed  *	@portnum: IOC Port number
67780d3ac77SMichael Reed  *
67880d3ac77SMichael Reed  *	Return: 0 for success
67980d3ac77SMichael Reed  *	-ENOMEM if no memory available
68080d3ac77SMichael Reed  *		-EPERM if not allowed due to ISR context
68180d3ac77SMichael Reed  *		-EAGAIN if no msg frames currently available
68280d3ac77SMichael Reed  *		-EFAULT for non-successful reply or no reply (timeout)
68380d3ac77SMichael Reed  *		-EINVAL portnum arg out of range (hardwired to two elements)
68480d3ac77SMichael Reed  */
68580d3ac77SMichael Reed static int
68680d3ac77SMichael Reed mptfc_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
68780d3ac77SMichael Reed {
68880d3ac77SMichael Reed 	ConfigPageHeader_t	 hdr;
68980d3ac77SMichael Reed 	CONFIGPARMS		 cfg;
69080d3ac77SMichael Reed 	FCPortPage0_t		*ppage0_alloc;
69180d3ac77SMichael Reed 	FCPortPage0_t		*pp0dest;
69280d3ac77SMichael Reed 	dma_addr_t		 page0_dma;
69380d3ac77SMichael Reed 	int			 data_sz;
69480d3ac77SMichael Reed 	int			 copy_sz;
69580d3ac77SMichael Reed 	int			 rc;
69680d3ac77SMichael Reed 	int			 count = 400;
69780d3ac77SMichael Reed 
69880d3ac77SMichael Reed 	if (portnum > 1)
69980d3ac77SMichael Reed 		return -EINVAL;
70080d3ac77SMichael Reed 
70180d3ac77SMichael Reed 	/* Get FCPort Page 0 header */
70280d3ac77SMichael Reed 	hdr.PageVersion = 0;
70380d3ac77SMichael Reed 	hdr.PageLength = 0;
70480d3ac77SMichael Reed 	hdr.PageNumber = 0;
70580d3ac77SMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
70680d3ac77SMichael Reed 	cfg.cfghdr.hdr = &hdr;
70780d3ac77SMichael Reed 	cfg.physAddr = -1;
70880d3ac77SMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
70980d3ac77SMichael Reed 	cfg.dir = 0;
71080d3ac77SMichael Reed 	cfg.pageAddr = portnum;
71180d3ac77SMichael Reed 	cfg.timeout = 0;
71280d3ac77SMichael Reed 
71380d3ac77SMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
71480d3ac77SMichael Reed 		return rc;
71580d3ac77SMichael Reed 
71680d3ac77SMichael Reed 	if (hdr.PageLength == 0)
71780d3ac77SMichael Reed 		return 0;
71880d3ac77SMichael Reed 
71980d3ac77SMichael Reed 	data_sz = hdr.PageLength * 4;
72080d3ac77SMichael Reed 	rc = -ENOMEM;
72180d3ac77SMichael Reed 	ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
72280d3ac77SMichael Reed 	if (ppage0_alloc) {
72380d3ac77SMichael Reed 
72480d3ac77SMichael Reed  try_again:
72580d3ac77SMichael Reed 		memset((u8 *)ppage0_alloc, 0, data_sz);
72680d3ac77SMichael Reed 		cfg.physAddr = page0_dma;
72780d3ac77SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
72880d3ac77SMichael Reed 
72980d3ac77SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
73080d3ac77SMichael Reed 			/* save the data */
73180d3ac77SMichael Reed 			pp0dest = &ioc->fc_port_page0[portnum];
73280d3ac77SMichael Reed 			copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
73380d3ac77SMichael Reed 			memcpy(pp0dest, ppage0_alloc, copy_sz);
73480d3ac77SMichael Reed 
73580d3ac77SMichael Reed 			/*
73680d3ac77SMichael Reed 			 *	Normalize endianness of structure data,
73780d3ac77SMichael Reed 			 *	by byte-swapping all > 1 byte fields!
73880d3ac77SMichael Reed 			 */
73980d3ac77SMichael Reed 			pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
74080d3ac77SMichael Reed 			pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
74180d3ac77SMichael Reed 			pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
74280d3ac77SMichael Reed 			pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
74380d3ac77SMichael Reed 			pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
74480d3ac77SMichael Reed 			pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
74580d3ac77SMichael Reed 			pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
74680d3ac77SMichael Reed 			pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
74780d3ac77SMichael Reed 			pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
74880d3ac77SMichael Reed 			pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
74980d3ac77SMichael Reed 			pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
75080d3ac77SMichael Reed 			pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
75180d3ac77SMichael Reed 			pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
75280d3ac77SMichael Reed 			pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
75380d3ac77SMichael Reed 			pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
75480d3ac77SMichael Reed 			pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
75580d3ac77SMichael Reed 
75680d3ac77SMichael Reed 			/*
75780d3ac77SMichael Reed 			 * if still doing discovery,
75880d3ac77SMichael Reed 			 * hang loose a while until finished
75980d3ac77SMichael Reed 			 */
76077d88ee2SMichael Reed 			if ((pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) ||
76177d88ee2SMichael Reed 			    (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE &&
76277d88ee2SMichael Reed 			     (pp0dest->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_TYPE_MASK)
76377d88ee2SMichael Reed 			      == MPI_FCPORTPAGE0_FLAGS_ATTACH_NO_INIT)) {
76480d3ac77SMichael Reed 				if (count-- > 0) {
765d6be06c8SMichael Reed 					msleep(100);
76680d3ac77SMichael Reed 					goto try_again;
76780d3ac77SMichael Reed 				}
76880d3ac77SMichael Reed 				printk(MYIOC_s_INFO_FMT "Firmware discovery not"
76980d3ac77SMichael Reed 							" complete.\n",
77080d3ac77SMichael Reed 						ioc->name);
77180d3ac77SMichael Reed 			}
77280d3ac77SMichael Reed 		}
77380d3ac77SMichael Reed 
77480d3ac77SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
77580d3ac77SMichael Reed 	}
77680d3ac77SMichael Reed 
77780d3ac77SMichael Reed 	return rc;
77880d3ac77SMichael Reed }
77980d3ac77SMichael Reed 
780ca2f938eSMichael Reed static int
781ca2f938eSMichael Reed mptfc_WriteFcPortPage1(MPT_ADAPTER *ioc, int portnum)
782ca2f938eSMichael Reed {
783ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
784ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
785ca2f938eSMichael Reed 	int			 rc;
786ca2f938eSMichael Reed 
787ca2f938eSMichael Reed 	if (portnum > 1)
788ca2f938eSMichael Reed 		return -EINVAL;
789ca2f938eSMichael Reed 
790ca2f938eSMichael Reed 	if (!(ioc->fc_data.fc_port_page1[portnum].data))
791ca2f938eSMichael Reed 		return -EINVAL;
792ca2f938eSMichael Reed 
793ca2f938eSMichael Reed 	/* get fcport page 1 header */
794ca2f938eSMichael Reed 	hdr.PageVersion = 0;
795ca2f938eSMichael Reed 	hdr.PageLength = 0;
796ca2f938eSMichael Reed 	hdr.PageNumber = 1;
797ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
798ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
799ca2f938eSMichael Reed 	cfg.physAddr = -1;
800ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
801ca2f938eSMichael Reed 	cfg.dir = 0;
802ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
803ca2f938eSMichael Reed 	cfg.timeout = 0;
804ca2f938eSMichael Reed 
805ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
806ca2f938eSMichael Reed 		return rc;
807ca2f938eSMichael Reed 
808ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
809ca2f938eSMichael Reed 		return -ENODEV;
810ca2f938eSMichael Reed 
811ca2f938eSMichael Reed 	if (hdr.PageLength*4 != ioc->fc_data.fc_port_page1[portnum].pg_sz)
812ca2f938eSMichael Reed 		return -EINVAL;
813ca2f938eSMichael Reed 
814ca2f938eSMichael Reed 	cfg.physAddr = ioc->fc_data.fc_port_page1[portnum].dma;
815ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
816ca2f938eSMichael Reed 	cfg.dir = 1;
817ca2f938eSMichael Reed 
818ca2f938eSMichael Reed 	rc = mpt_config(ioc, &cfg);
819ca2f938eSMichael Reed 
820ca2f938eSMichael Reed 	return rc;
821ca2f938eSMichael Reed }
822ca2f938eSMichael Reed 
823ca2f938eSMichael Reed static int
824ca2f938eSMichael Reed mptfc_GetFcPortPage1(MPT_ADAPTER *ioc, int portnum)
825ca2f938eSMichael Reed {
826ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
827ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
828ca2f938eSMichael Reed 	FCPortPage1_t		*page1_alloc;
829ca2f938eSMichael Reed 	dma_addr_t		 page1_dma;
830ca2f938eSMichael Reed 	int			 data_sz;
831ca2f938eSMichael Reed 	int			 rc;
832ca2f938eSMichael Reed 
833ca2f938eSMichael Reed 	if (portnum > 1)
834ca2f938eSMichael Reed 		return -EINVAL;
835ca2f938eSMichael Reed 
836ca2f938eSMichael Reed 	/* get fcport page 1 header */
837ca2f938eSMichael Reed 	hdr.PageVersion = 0;
838ca2f938eSMichael Reed 	hdr.PageLength = 0;
839ca2f938eSMichael Reed 	hdr.PageNumber = 1;
840ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
841ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
842ca2f938eSMichael Reed 	cfg.physAddr = -1;
843ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
844ca2f938eSMichael Reed 	cfg.dir = 0;
845ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
846ca2f938eSMichael Reed 	cfg.timeout = 0;
847ca2f938eSMichael Reed 
848ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
849ca2f938eSMichael Reed 		return rc;
850ca2f938eSMichael Reed 
851ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
852ca2f938eSMichael Reed 		return -ENODEV;
853ca2f938eSMichael Reed 
854ca2f938eSMichael Reed start_over:
855ca2f938eSMichael Reed 
856ca2f938eSMichael Reed 	if (ioc->fc_data.fc_port_page1[portnum].data == NULL) {
857ca2f938eSMichael Reed 		data_sz = hdr.PageLength * 4;
858ca2f938eSMichael Reed 		if (data_sz < sizeof(FCPortPage1_t))
859ca2f938eSMichael Reed 			data_sz = sizeof(FCPortPage1_t);
860ca2f938eSMichael Reed 
861ca2f938eSMichael Reed 		page1_alloc = (FCPortPage1_t *) pci_alloc_consistent(ioc->pcidev,
862ca2f938eSMichael Reed 						data_sz,
863ca2f938eSMichael Reed 						&page1_dma);
864ca2f938eSMichael Reed 		if (!page1_alloc)
865ca2f938eSMichael Reed 			return -ENOMEM;
866ca2f938eSMichael Reed 	}
867ca2f938eSMichael Reed 	else {
868ca2f938eSMichael Reed 		page1_alloc = ioc->fc_data.fc_port_page1[portnum].data;
869ca2f938eSMichael Reed 		page1_dma = ioc->fc_data.fc_port_page1[portnum].dma;
870ca2f938eSMichael Reed 		data_sz = ioc->fc_data.fc_port_page1[portnum].pg_sz;
871ca2f938eSMichael Reed 		if (hdr.PageLength * 4 > data_sz) {
872ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[portnum].data = NULL;
873ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
874ca2f938eSMichael Reed 				page1_alloc, page1_dma);
875ca2f938eSMichael Reed 			goto start_over;
876ca2f938eSMichael Reed 		}
877ca2f938eSMichael Reed 	}
878ca2f938eSMichael Reed 
879ca2f938eSMichael Reed 	memset(page1_alloc,0,data_sz);
880ca2f938eSMichael Reed 
881ca2f938eSMichael Reed 	cfg.physAddr = page1_dma;
882ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
883ca2f938eSMichael Reed 
884ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) == 0) {
885ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = page1_alloc;
886ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].pg_sz = data_sz;
887ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].dma = page1_dma;
888ca2f938eSMichael Reed 	}
889ca2f938eSMichael Reed 	else {
890ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = NULL;
891ca2f938eSMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
892ca2f938eSMichael Reed 			page1_alloc, page1_dma);
893ca2f938eSMichael Reed 	}
894ca2f938eSMichael Reed 
895ca2f938eSMichael Reed 	return rc;
896ca2f938eSMichael Reed }
897ca2f938eSMichael Reed 
898ca2f938eSMichael Reed static void
899ca2f938eSMichael Reed mptfc_SetFcPortPage1_defaults(MPT_ADAPTER *ioc)
900ca2f938eSMichael Reed {
901ca2f938eSMichael Reed 	int		ii;
902ca2f938eSMichael Reed 	FCPortPage1_t	*pp1;
903ca2f938eSMichael Reed 
904ca2f938eSMichael Reed 	#define MPTFC_FW_DEVICE_TIMEOUT	(1)
905ca2f938eSMichael Reed 	#define MPTFC_FW_IO_PEND_TIMEOUT (1)
906ca2f938eSMichael Reed 	#define ON_FLAGS  (MPI_FCPORTPAGE1_FLAGS_IMMEDIATE_ERROR_REPLY)
907ca2f938eSMichael Reed 	#define OFF_FLAGS (MPI_FCPORTPAGE1_FLAGS_VERBOSE_RESCAN_EVENTS)
908ca2f938eSMichael Reed 
909ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
910ca2f938eSMichael Reed 		if (mptfc_GetFcPortPage1(ioc, ii) != 0)
911ca2f938eSMichael Reed 			continue;
912ca2f938eSMichael Reed 		pp1 = ioc->fc_data.fc_port_page1[ii].data;
913ca2f938eSMichael Reed 		if ((pp1->InitiatorDeviceTimeout == MPTFC_FW_DEVICE_TIMEOUT)
914ca2f938eSMichael Reed 		 && (pp1->InitiatorIoPendTimeout == MPTFC_FW_IO_PEND_TIMEOUT)
915ca2f938eSMichael Reed 		 && ((pp1->Flags & ON_FLAGS) == ON_FLAGS)
916ca2f938eSMichael Reed 		 && ((pp1->Flags & OFF_FLAGS) == 0))
917ca2f938eSMichael Reed 			continue;
918ca2f938eSMichael Reed 		pp1->InitiatorDeviceTimeout = MPTFC_FW_DEVICE_TIMEOUT;
919ca2f938eSMichael Reed 		pp1->InitiatorIoPendTimeout = MPTFC_FW_IO_PEND_TIMEOUT;
920ca2f938eSMichael Reed 		pp1->Flags &= ~OFF_FLAGS;
921ca2f938eSMichael Reed 		pp1->Flags |= ON_FLAGS;
922ca2f938eSMichael Reed 		mptfc_WriteFcPortPage1(ioc, ii);
923ca2f938eSMichael Reed 	}
924ca2f938eSMichael Reed }
925ca2f938eSMichael Reed 
926ca2f938eSMichael Reed 
92705e8ec17SMichael Reed static void
92805e8ec17SMichael Reed mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
92905e8ec17SMichael Reed {
9305d947f2bSMichael Reed 	unsigned	class = 0;
9315d947f2bSMichael Reed 	unsigned	cos = 0;
9325d947f2bSMichael Reed 	unsigned	speed;
9335d947f2bSMichael Reed 	unsigned	port_type;
9345d947f2bSMichael Reed 	unsigned	port_state;
9355d947f2bSMichael Reed 	FCPortPage0_t	*pp0;
9365d947f2bSMichael Reed 	struct Scsi_Host *sh;
9375d947f2bSMichael Reed 	char		*sn;
93805e8ec17SMichael Reed 
93905e8ec17SMichael Reed 	/* don't know what to do as only one scsi (fc) host was allocated */
94005e8ec17SMichael Reed 	if (portnum != 0)
94105e8ec17SMichael Reed 		return;
94205e8ec17SMichael Reed 
9435d947f2bSMichael Reed 	pp0 = &ioc->fc_port_page0[portnum];
9445d947f2bSMichael Reed 	sh = ioc->sh;
9455d947f2bSMichael Reed 
9465d947f2bSMichael Reed 	sn = fc_host_symbolic_name(sh);
9475d947f2bSMichael Reed 	snprintf(sn, FC_SYMBOLIC_NAME_SIZE, "%s %s%08xh",
9485d947f2bSMichael Reed 	    ioc->prod_name,
9495d947f2bSMichael Reed 	    MPT_FW_REV_MAGIC_ID_STRING,
9505d947f2bSMichael Reed 	    ioc->facts.FWVersion.Word);
9515d947f2bSMichael Reed 
9525d947f2bSMichael Reed 	fc_host_tgtid_bind_type(sh) = FC_TGTID_BIND_BY_WWPN;
9535d947f2bSMichael Reed 
9545d947f2bSMichael Reed 	fc_host_maxframe_size(sh) = pp0->MaxFrameSize;
9555d947f2bSMichael Reed 
9565d947f2bSMichael Reed 	fc_host_node_name(sh) =
9575d947f2bSMichael Reed 	    	(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
9585d947f2bSMichael Reed 
9595d947f2bSMichael Reed 	fc_host_port_name(sh) =
9605d947f2bSMichael Reed 	    	(u64)pp0->WWPN.High << 32 | (u64)pp0->WWPN.Low;
9615d947f2bSMichael Reed 
9625d947f2bSMichael Reed 	fc_host_port_id(sh) = pp0->PortIdentifier;
9635d947f2bSMichael Reed 
9645d947f2bSMichael Reed 	class = pp0->SupportedServiceClass;
96505e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
96605e8ec17SMichael Reed 		cos |= FC_COS_CLASS1;
96705e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
96805e8ec17SMichael Reed 		cos |= FC_COS_CLASS2;
96905e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
97005e8ec17SMichael Reed 		cos |= FC_COS_CLASS3;
9715d947f2bSMichael Reed 	fc_host_supported_classes(sh) = cos;
97205e8ec17SMichael Reed 
9735d947f2bSMichael Reed 	if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT)
9745d947f2bSMichael Reed 		speed = FC_PORTSPEED_1GBIT;
9755d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT)
9765d947f2bSMichael Reed 		speed = FC_PORTSPEED_2GBIT;
9775d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT)
9785d947f2bSMichael Reed 		speed = FC_PORTSPEED_4GBIT;
9795d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_10GBIT)
9805d947f2bSMichael Reed 		speed = FC_PORTSPEED_10GBIT;
9815d947f2bSMichael Reed 	else
9825d947f2bSMichael Reed 		speed = FC_PORTSPEED_UNKNOWN;
9835d947f2bSMichael Reed 	fc_host_speed(sh) = speed;
98405e8ec17SMichael Reed 
9855d947f2bSMichael Reed 	speed = 0;
9865d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_1GBIT_SPEED)
9875d947f2bSMichael Reed 		speed |= FC_PORTSPEED_1GBIT;
9885d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_2GBIT_SPEED)
9895d947f2bSMichael Reed 		speed |= FC_PORTSPEED_2GBIT;
9905d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_4GBIT_SPEED)
9915d947f2bSMichael Reed 		speed |= FC_PORTSPEED_4GBIT;
9925d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_10GBIT_SPEED)
9935d947f2bSMichael Reed 		speed |= FC_PORTSPEED_10GBIT;
9945d947f2bSMichael Reed 	fc_host_supported_speeds(sh) = speed;
99505e8ec17SMichael Reed 
9965d947f2bSMichael Reed 	port_state = FC_PORTSTATE_UNKNOWN;
9975d947f2bSMichael Reed 	if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE)
9985d947f2bSMichael Reed 		port_state = FC_PORTSTATE_ONLINE;
9995d947f2bSMichael Reed 	else if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_OFFLINE)
10005d947f2bSMichael Reed 		port_state = FC_PORTSTATE_LINKDOWN;
10015d947f2bSMichael Reed 	fc_host_port_state(sh) = port_state;
100205e8ec17SMichael Reed 
10035d947f2bSMichael Reed 	port_type = FC_PORTTYPE_UNKNOWN;
10045d947f2bSMichael Reed 	if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_POINT_TO_POINT)
10055d947f2bSMichael Reed 		port_type = FC_PORTTYPE_PTP;
10065d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PRIVATE_LOOP)
10075d947f2bSMichael Reed 		port_type = FC_PORTTYPE_LPORT;
10085d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PUBLIC_LOOP)
10095d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NLPORT;
10105d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_FABRIC_DIRECT)
10115d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NPORT;
10125d947f2bSMichael Reed 	fc_host_port_type(sh) = port_type;
101305e8ec17SMichael Reed 
10145d947f2bSMichael Reed 	fc_host_fabric_name(sh) =
10155d947f2bSMichael Reed 	    (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_FABRIC_WWN_VALID) ?
10165d947f2bSMichael Reed 		(u64) pp0->FabricWWNN.High << 32 | (u64) pp0->FabricWWPN.Low :
10175d947f2bSMichael Reed 		(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
10185d947f2bSMichael Reed 
101905e8ec17SMichael Reed }
102005e8ec17SMichael Reed 
102105e8ec17SMichael Reed static void
1022c4028958SDavid Howells mptfc_setup_reset(struct work_struct *work)
1023419835e2SMichael Reed {
1024c4028958SDavid Howells 	MPT_ADAPTER		*ioc =
1025c4028958SDavid Howells 		container_of(work, MPT_ADAPTER, fc_setup_reset_work);
1026419835e2SMichael Reed 	u64			pn;
1027419835e2SMichael Reed 	struct mptfc_rport_info *ri;
1028419835e2SMichael Reed 
1029419835e2SMichael Reed 	/* reset about to happen, delete (block) all rports */
1030419835e2SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
1031419835e2SMichael Reed 		if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
1032419835e2SMichael Reed 			ri->flags &= ~MPT_RPORT_INFO_FLAGS_REGISTERED;
1033419835e2SMichael Reed 			fc_remote_port_delete(ri->rport);	/* won't sleep */
1034419835e2SMichael Reed 			ri->rport = NULL;
1035419835e2SMichael Reed 
1036419835e2SMichael Reed 			pn = (u64)ri->pg0.WWPN.High << 32 |
1037419835e2SMichael Reed 			     (u64)ri->pg0.WWPN.Low;
1038419835e2SMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
1039419835e2SMichael Reed 				"mptfc_setup_reset.%d: %llx deleted\n",
1040419835e2SMichael Reed 				ioc->name,
1041419835e2SMichael Reed 				ioc->sh->host_no,
1042419835e2SMichael Reed 				(unsigned long long)pn));
1043419835e2SMichael Reed 		}
1044419835e2SMichael Reed 	}
1045419835e2SMichael Reed }
1046419835e2SMichael Reed 
1047419835e2SMichael Reed static void
1048c4028958SDavid Howells mptfc_rescan_devices(struct work_struct *work)
104905e8ec17SMichael Reed {
1050c4028958SDavid Howells 	MPT_ADAPTER		*ioc =
1051c4028958SDavid Howells 		container_of(work, MPT_ADAPTER, fc_rescan_work);
105205e8ec17SMichael Reed 	int			ii;
105365207fedSMoore, Eric 	u64			pn;
105405e8ec17SMichael Reed 	struct mptfc_rport_info *ri;
105505e8ec17SMichael Reed 
105605e8ec17SMichael Reed 	/* start by tagging all ports as missing */
105705e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
105805e8ec17SMichael Reed 		if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
105905e8ec17SMichael Reed 			ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
106005e8ec17SMichael Reed 		}
106105e8ec17SMichael Reed 	}
106205e8ec17SMichael Reed 
106305e8ec17SMichael Reed 	/*
106405e8ec17SMichael Reed 	 * now rescan devices known to adapter,
106505e8ec17SMichael Reed 	 * will reregister existing rports
106605e8ec17SMichael Reed 	 */
106705e8ec17SMichael Reed 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
106880d3ac77SMichael Reed 		(void) mptfc_GetFcPortPage0(ioc, ii);
106905e8ec17SMichael Reed 		mptfc_init_host_attr(ioc, ii);	/* refresh */
107005e8ec17SMichael Reed 		mptfc_GetFcDevPage0(ioc, ii, mptfc_register_dev);
107105e8ec17SMichael Reed 	}
107205e8ec17SMichael Reed 
107305e8ec17SMichael Reed 	/* delete devices still missing */
107405e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
107505e8ec17SMichael Reed 		/* if newly missing, delete it */
107665207fedSMoore, Eric 		if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
107705e8ec17SMichael Reed 
107805e8ec17SMichael Reed 			ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
107905e8ec17SMichael Reed 				       MPT_RPORT_INFO_FLAGS_MISSING);
108065207fedSMoore, Eric 			fc_remote_port_delete(ri->rport);	/* won't sleep */
10813bc7bf1dSMichael Reed 			ri->rport = NULL;
108265207fedSMoore, Eric 
108365207fedSMoore, Eric 			pn = (u64)ri->pg0.WWPN.High << 32 |
108465207fedSMoore, Eric 			     (u64)ri->pg0.WWPN.Low;
10853bc7bf1dSMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
10863bc7bf1dSMichael Reed 				"mptfc_rescan.%d: %llx deleted\n",
10873bc7bf1dSMichael Reed 				ioc->name,
10883bc7bf1dSMichael Reed 				ioc->sh->host_no,
108965207fedSMoore, Eric 				(unsigned long long)pn));
109005e8ec17SMichael Reed 		}
109105e8ec17SMichael Reed 	}
109205e8ec17SMichael Reed }
109305e8ec17SMichael Reed 
10942496af39SMoore, Eric Dean  static int
10952496af39SMoore, Eric Dean  mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
10962496af39SMoore, Eric Dean  {
10972496af39SMoore, Eric Dean  	struct Scsi_Host	*sh;
10982496af39SMoore, Eric Dean  	MPT_SCSI_HOST		*hd;
10992496af39SMoore, Eric Dean  	MPT_ADAPTER 		*ioc;
11002496af39SMoore, Eric Dean  	unsigned long		 flags;
11011ca00bb7SChristoph Hellwig 	int			 ii;
11022496af39SMoore, Eric Dean  	int			 numSGE = 0;
11032496af39SMoore, Eric Dean  	int			 scale;
11042496af39SMoore, Eric Dean  	int			 ioc_cap;
11052496af39SMoore, Eric Dean  	int			error=0;
11062496af39SMoore, Eric Dean  	int			r;
11072496af39SMoore, Eric Dean  
11082496af39SMoore, Eric Dean  	if ((r = mpt_attach(pdev,id)) != 0)
11092496af39SMoore, Eric Dean  		return r;
11102496af39SMoore, Eric Dean  
11112496af39SMoore, Eric Dean  	ioc = pci_get_drvdata(pdev);
1112d335cc38SMoore, Eric Dean  	ioc->DoneCtx = mptfcDoneCtx;
1113d335cc38SMoore, Eric Dean  	ioc->TaskCtx = mptfcTaskCtx;
1114d335cc38SMoore, Eric Dean  	ioc->InternalCtx = mptfcInternalCtx;
11152496af39SMoore, Eric Dean  
11162496af39SMoore, Eric Dean  	/*  Added sanity check on readiness of the MPT adapter.
11172496af39SMoore, Eric Dean  	 */
11182496af39SMoore, Eric Dean  	if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
11192496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
11202496af39SMoore, Eric Dean  		  "Skipping because it's not operational!\n",
11212496af39SMoore, Eric Dean  		  ioc->name);
11227acec1e7SMoore, Eric Dean 		error = -ENODEV;
11237acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11242496af39SMoore, Eric Dean  	}
11252496af39SMoore, Eric Dean  
11262496af39SMoore, Eric Dean  	if (!ioc->active) {
11272496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
11282496af39SMoore, Eric Dean  		  ioc->name);
11297acec1e7SMoore, Eric Dean 		error = -ENODEV;
11307acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11312496af39SMoore, Eric Dean  	}
11322496af39SMoore, Eric Dean  
11332496af39SMoore, Eric Dean  	/*  Sanity check - ensure at least 1 port is INITIATOR capable
11342496af39SMoore, Eric Dean  	 */
11352496af39SMoore, Eric Dean  	ioc_cap = 0;
11362496af39SMoore, Eric Dean  	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
11372496af39SMoore, Eric Dean  		if (ioc->pfacts[ii].ProtocolFlags &
11382496af39SMoore, Eric Dean  		    MPI_PORTFACTS_PROTOCOL_INITIATOR)
11392496af39SMoore, Eric Dean  			ioc_cap ++;
11402496af39SMoore, Eric Dean  	}
11412496af39SMoore, Eric Dean  
11422496af39SMoore, Eric Dean  	if (!ioc_cap) {
11432496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
11442496af39SMoore, Eric Dean  			"Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
11452496af39SMoore, Eric Dean  			ioc->name, ioc);
114605e8ec17SMichael Reed 		return -ENODEV;
11472496af39SMoore, Eric Dean  	}
11482496af39SMoore, Eric Dean  
11492496af39SMoore, Eric Dean  	sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
11502496af39SMoore, Eric Dean  
11512496af39SMoore, Eric Dean  	if (!sh) {
11522496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
11532496af39SMoore, Eric Dean  			"Unable to register controller with SCSI subsystem\n",
11542496af39SMoore, Eric Dean  			ioc->name);
11557acec1e7SMoore, Eric Dean 		error = -1;
11567acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11572496af39SMoore, Eric Dean          }
11582496af39SMoore, Eric Dean  
115980d3ac77SMichael Reed 	spin_lock_init(&ioc->fc_rescan_work_lock);
1160c4028958SDavid Howells 	INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices);
1161c4028958SDavid Howells 	INIT_WORK(&ioc->fc_setup_reset_work, mptfc_setup_reset);
116205e8ec17SMichael Reed 
11632496af39SMoore, Eric Dean  	spin_lock_irqsave(&ioc->FreeQlock, flags);
11642496af39SMoore, Eric Dean  
11652496af39SMoore, Eric Dean  	/* Attach the SCSI Host to the IOC structure
11662496af39SMoore, Eric Dean  	 */
11672496af39SMoore, Eric Dean  	ioc->sh = sh;
11682496af39SMoore, Eric Dean  
11692496af39SMoore, Eric Dean  	sh->io_port = 0;
11702496af39SMoore, Eric Dean  	sh->n_io_port = 0;
11712496af39SMoore, Eric Dean  	sh->irq = 0;
11722496af39SMoore, Eric Dean  
11732496af39SMoore, Eric Dean  	/* set 16 byte cdb's */
11742496af39SMoore, Eric Dean  	sh->max_cmd_len = 16;
11752496af39SMoore, Eric Dean  
11762496af39SMoore, Eric Dean  	sh->max_id = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
11772496af39SMoore, Eric Dean  
11782496af39SMoore, Eric Dean  	sh->max_lun = MPT_LAST_LUN + 1;
11792496af39SMoore, Eric Dean  	sh->max_channel = 0;
11802496af39SMoore, Eric Dean  	sh->this_id = ioc->pfacts[0].PortSCSIID;
11812496af39SMoore, Eric Dean  
11822496af39SMoore, Eric Dean  	/* Required entry.
11832496af39SMoore, Eric Dean  	 */
11842496af39SMoore, Eric Dean  	sh->unique_id = ioc->id;
11852496af39SMoore, Eric Dean  
11862496af39SMoore, Eric Dean  	/* Verify that we won't exceed the maximum
11872496af39SMoore, Eric Dean  	 * number of chain buffers
11882496af39SMoore, Eric Dean  	 * We can optimize:  ZZ = req_sz/sizeof(SGE)
11892496af39SMoore, Eric Dean  	 * For 32bit SGE's:
11902496af39SMoore, Eric Dean  	 *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
11912496af39SMoore, Eric Dean  	 *               + (req_sz - 64)/sizeof(SGE)
11922496af39SMoore, Eric Dean  	 * A slightly different algorithm is required for
11932496af39SMoore, Eric Dean  	 * 64bit SGEs.
11942496af39SMoore, Eric Dean  	 */
11952496af39SMoore, Eric Dean  	scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
11962496af39SMoore, Eric Dean  	if (sizeof(dma_addr_t) == sizeof(u64)) {
11972496af39SMoore, Eric Dean  		numSGE = (scale - 1) *
11982496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
11992496af39SMoore, Eric Dean  		  (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
12002496af39SMoore, Eric Dean  		  sizeof(u32));
12012496af39SMoore, Eric Dean  	} else {
12022496af39SMoore, Eric Dean  		numSGE = 1 + (scale - 1) *
12032496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
12042496af39SMoore, Eric Dean  		  (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
12052496af39SMoore, Eric Dean  		  sizeof(u32));
12062496af39SMoore, Eric Dean  	}
12072496af39SMoore, Eric Dean  
12082496af39SMoore, Eric Dean  	if (numSGE < sh->sg_tablesize) {
12092496af39SMoore, Eric Dean  		/* Reset this value */
12102496af39SMoore, Eric Dean  		dprintk((MYIOC_s_INFO_FMT
12112496af39SMoore, Eric Dean  		  "Resetting sg_tablesize to %d from %d\n",
12122496af39SMoore, Eric Dean  		  ioc->name, numSGE, sh->sg_tablesize));
12132496af39SMoore, Eric Dean  		sh->sg_tablesize = numSGE;
12142496af39SMoore, Eric Dean  	}
12152496af39SMoore, Eric Dean  
12162496af39SMoore, Eric Dean  	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
12172496af39SMoore, Eric Dean  
12182496af39SMoore, Eric Dean  	hd = (MPT_SCSI_HOST *) sh->hostdata;
12192496af39SMoore, Eric Dean  	hd->ioc = ioc;
12202496af39SMoore, Eric Dean  
12212496af39SMoore, Eric Dean  	/* SCSI needs scsi_cmnd lookup table!
12222496af39SMoore, Eric Dean  	 * (with size equal to req_depth*PtrSz!)
12232496af39SMoore, Eric Dean  	 */
12241ca00bb7SChristoph Hellwig 	hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
12251ca00bb7SChristoph Hellwig 	if (!hd->ScsiLookup) {
12262496af39SMoore, Eric Dean  		error = -ENOMEM;
12277acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12282496af39SMoore, Eric Dean  	}
12292496af39SMoore, Eric Dean  
12301ca00bb7SChristoph Hellwig 	dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
12311ca00bb7SChristoph Hellwig 		 ioc->name, hd->ScsiLookup));
12322496af39SMoore, Eric Dean  
12332496af39SMoore, Eric Dean  	/* Allocate memory for the device structures.
12342496af39SMoore, Eric Dean  	 * A non-Null pointer at an offset
12352496af39SMoore, Eric Dean  	 * indicates a device exists.
12362496af39SMoore, Eric Dean  	 * max_id = 1 + maximum id (hosts.h)
12372496af39SMoore, Eric Dean  	 */
12381ca00bb7SChristoph Hellwig 	hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
12391ca00bb7SChristoph Hellwig 	if (!hd->Targets) {
12402496af39SMoore, Eric Dean  		error = -ENOMEM;
12417acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12422496af39SMoore, Eric Dean  	}
12432496af39SMoore, Eric Dean  
12441ca00bb7SChristoph Hellwig 	dprintk((KERN_INFO "  vdev @ %p\n", hd->Targets));
12452496af39SMoore, Eric Dean  
12462496af39SMoore, Eric Dean  	/* Clear the TM flags
12472496af39SMoore, Eric Dean  	 */
12482496af39SMoore, Eric Dean  	hd->tmPending = 0;
12492496af39SMoore, Eric Dean  	hd->tmState = TM_STATE_NONE;
12502496af39SMoore, Eric Dean  	hd->resetPending = 0;
12512496af39SMoore, Eric Dean  	hd->abortSCpnt = NULL;
12522496af39SMoore, Eric Dean  
12532496af39SMoore, Eric Dean  	/* Clear the pointer used to store
12542496af39SMoore, Eric Dean  	 * single-threaded commands, i.e., those
12552496af39SMoore, Eric Dean  	 * issued during a bus scan, dv and
12562496af39SMoore, Eric Dean  	 * configuration pages.
12572496af39SMoore, Eric Dean  	 */
12582496af39SMoore, Eric Dean  	hd->cmdPtr = NULL;
12592496af39SMoore, Eric Dean  
12602496af39SMoore, Eric Dean  	/* Initialize this SCSI Hosts' timers
12612496af39SMoore, Eric Dean  	 * To use, set the timer expires field
12622496af39SMoore, Eric Dean  	 * and add_timer
12632496af39SMoore, Eric Dean  	 */
12642496af39SMoore, Eric Dean  	init_timer(&hd->timer);
12652496af39SMoore, Eric Dean  	hd->timer.data = (unsigned long) hd;
12662496af39SMoore, Eric Dean  	hd->timer.function = mptscsih_timer_expired;
12672496af39SMoore, Eric Dean  
12682496af39SMoore, Eric Dean  	init_waitqueue_head(&hd->scandv_waitq);
12692496af39SMoore, Eric Dean  	hd->scandv_wait_done = 0;
12702496af39SMoore, Eric Dean  	hd->last_queue_full = 0;
12712496af39SMoore, Eric Dean  
127205e8ec17SMichael Reed 	sh->transportt = mptfc_transport_template;
12732496af39SMoore, Eric Dean  	error = scsi_add_host (sh, &ioc->pcidev->dev);
12742496af39SMoore, Eric Dean  	if(error) {
12752496af39SMoore, Eric Dean  		dprintk((KERN_ERR MYNAM
12762496af39SMoore, Eric Dean  		  "scsi_add_host failed\n"));
12777acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12782496af39SMoore, Eric Dean  	}
12792496af39SMoore, Eric Dean  
128065207fedSMoore, Eric 	/* initialize workqueue */
128165207fedSMoore, Eric 
128265207fedSMoore, Eric 	snprintf(ioc->fc_rescan_work_q_name, KOBJ_NAME_LEN, "mptfc_wq_%d",
128365207fedSMoore, Eric 		sh->host_no);
128465207fedSMoore, Eric 	ioc->fc_rescan_work_q =
128565207fedSMoore, Eric 		create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
128665207fedSMoore, Eric 	if (!ioc->fc_rescan_work_q)
128765207fedSMoore, Eric 		goto out_mptfc_probe;
128865207fedSMoore, Eric 
128965207fedSMoore, Eric 	/*
129080d3ac77SMichael Reed 	 *  Pre-fetch FC port WWN and stuff...
129180d3ac77SMichael Reed 	 *  (FCPortPage0_t stuff)
129280d3ac77SMichael Reed 	 */
129380d3ac77SMichael Reed 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
129480d3ac77SMichael Reed 		(void) mptfc_GetFcPortPage0(ioc, ii);
129580d3ac77SMichael Reed 	}
1296ca2f938eSMichael Reed 	mptfc_SetFcPortPage1_defaults(ioc);
129780d3ac77SMichael Reed 
129880d3ac77SMichael Reed 	/*
129965207fedSMoore, Eric 	 * scan for rports -
130065207fedSMoore, Eric 	 *	by doing it via the workqueue, some locking is eliminated
130165207fedSMoore, Eric 	 */
130265207fedSMoore, Eric 
130365207fedSMoore, Eric 	queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
130465207fedSMoore, Eric 	flush_workqueue(ioc->fc_rescan_work_q);
130505e8ec17SMichael Reed 
13062496af39SMoore, Eric Dean  	return 0;
13072496af39SMoore, Eric Dean  
13087acec1e7SMoore, Eric Dean out_mptfc_probe:
13092496af39SMoore, Eric Dean  
13102496af39SMoore, Eric Dean  	mptscsih_remove(pdev);
13112496af39SMoore, Eric Dean  	return error;
13122496af39SMoore, Eric Dean  }
13132496af39SMoore, Eric Dean  
13142496af39SMoore, Eric Dean  static struct pci_driver mptfc_driver = {
13152496af39SMoore, Eric Dean  	.name		= "mptfc",
13162496af39SMoore, Eric Dean  	.id_table	= mptfc_pci_table,
13172496af39SMoore, Eric Dean  	.probe		= mptfc_probe,
131805e8ec17SMichael Reed 	.remove		= __devexit_p(mptfc_remove),
13192496af39SMoore, Eric Dean  	.shutdown	= mptscsih_shutdown,
13202496af39SMoore, Eric Dean  #ifdef CONFIG_PM
13212496af39SMoore, Eric Dean  	.suspend	= mptscsih_suspend,
13222496af39SMoore, Eric Dean  	.resume		= mptscsih_resume,
13232496af39SMoore, Eric Dean  #endif
13242496af39SMoore, Eric Dean  };
13252496af39SMoore, Eric Dean  
132680d3ac77SMichael Reed static int
132780d3ac77SMichael Reed mptfc_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
132880d3ac77SMichael Reed {
132980d3ac77SMichael Reed 	MPT_SCSI_HOST *hd;
133080d3ac77SMichael Reed 	u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
133180d3ac77SMichael Reed 	unsigned long flags;
133280d3ac77SMichael Reed 	int rc=1;
133380d3ac77SMichael Reed 
133480d3ac77SMichael Reed 	devtverboseprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
133580d3ac77SMichael Reed 			ioc->name, event));
133680d3ac77SMichael Reed 
133780d3ac77SMichael Reed 	if (ioc->sh == NULL ||
133880d3ac77SMichael Reed 		((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL))
133980d3ac77SMichael Reed 		return 1;
134080d3ac77SMichael Reed 
134180d3ac77SMichael Reed 	switch (event) {
134280d3ac77SMichael Reed 	case MPI_EVENT_RESCAN:
134380d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
134480d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
134580d3ac77SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
134680d3ac77SMichael Reed 				   &ioc->fc_rescan_work);
134780d3ac77SMichael Reed 		}
134880d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
134980d3ac77SMichael Reed 		break;
135080d3ac77SMichael Reed 	default:
135180d3ac77SMichael Reed 		rc = mptscsih_event_process(ioc,pEvReply);
135280d3ac77SMichael Reed 		break;
135380d3ac77SMichael Reed 	}
135480d3ac77SMichael Reed 	return rc;
135580d3ac77SMichael Reed }
135680d3ac77SMichael Reed 
135780d3ac77SMichael Reed static int
135880d3ac77SMichael Reed mptfc_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
135980d3ac77SMichael Reed {
136080d3ac77SMichael Reed 	int		rc;
136180d3ac77SMichael Reed 	unsigned long	flags;
136280d3ac77SMichael Reed 
136380d3ac77SMichael Reed 	rc = mptscsih_ioc_reset(ioc,reset_phase);
136480d3ac77SMichael Reed 	if (rc == 0)
136580d3ac77SMichael Reed 		return rc;
136680d3ac77SMichael Reed 
136780d3ac77SMichael Reed 
136880d3ac77SMichael Reed 	dtmprintk((KERN_WARNING MYNAM
136980d3ac77SMichael Reed 		": IOC %s_reset routed to FC host driver!\n",
137080d3ac77SMichael Reed 		reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
137180d3ac77SMichael Reed 		reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
137280d3ac77SMichael Reed 
137380d3ac77SMichael Reed 	if (reset_phase == MPT_IOC_SETUP_RESET) {
1374419835e2SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1375419835e2SMichael Reed 		if (ioc->fc_rescan_work_q) {
1376419835e2SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
1377419835e2SMichael Reed 				   &ioc->fc_setup_reset_work);
1378419835e2SMichael Reed 		}
1379419835e2SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
138080d3ac77SMichael Reed 	}
138180d3ac77SMichael Reed 
138280d3ac77SMichael Reed 	else if (reset_phase == MPT_IOC_PRE_RESET) {
138380d3ac77SMichael Reed 	}
138480d3ac77SMichael Reed 
138580d3ac77SMichael Reed 	else {	/* MPT_IOC_POST_RESET */
1386ca2f938eSMichael Reed 		mptfc_SetFcPortPage1_defaults(ioc);
138780d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
138880d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
138980d3ac77SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
139080d3ac77SMichael Reed 				   &ioc->fc_rescan_work);
139180d3ac77SMichael Reed 		}
139280d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
139380d3ac77SMichael Reed 	}
139480d3ac77SMichael Reed 	return 1;
139580d3ac77SMichael Reed }
139680d3ac77SMichael Reed 
13972496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
13982496af39SMoore, Eric Dean  /**
1399d9489fb6SRandy Dunlap  *	mptfc_init - Register MPT adapter(s) as SCSI host(s) with SCSI mid-layer.
14002496af39SMoore, Eric Dean   *
14012496af39SMoore, Eric Dean   *	Returns 0 for success, non-zero for failure.
14022496af39SMoore, Eric Dean   */
14032496af39SMoore, Eric Dean  static int __init
14042496af39SMoore, Eric Dean  mptfc_init(void)
14052496af39SMoore, Eric Dean  {
140605e8ec17SMichael Reed 	int error;
14072496af39SMoore, Eric Dean  
14082496af39SMoore, Eric Dean  	show_mptmod_ver(my_NAME, my_VERSION);
14092496af39SMoore, Eric Dean  
1410ca2f938eSMichael Reed 	/* sanity check module parameters */
1411ca2f938eSMichael Reed 	if (mptfc_dev_loss_tmo <= 0)
141205e8ec17SMichael Reed 		mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
141305e8ec17SMichael Reed 
141405e8ec17SMichael Reed 	mptfc_transport_template =
141505e8ec17SMichael Reed 		fc_attach_transport(&mptfc_transport_functions);
141605e8ec17SMichael Reed 
141705e8ec17SMichael Reed 	if (!mptfc_transport_template)
141805e8ec17SMichael Reed 		return -ENODEV;
141905e8ec17SMichael Reed 
14202496af39SMoore, Eric Dean  	mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
14212496af39SMoore, Eric Dean  	mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
14222496af39SMoore, Eric Dean  	mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
14232496af39SMoore, Eric Dean  
142480d3ac77SMichael Reed 	if (mpt_event_register(mptfcDoneCtx, mptfc_event_process) == 0) {
14253a892befSMoore, Eric 		devtverboseprintk((KERN_INFO MYNAM
14262496af39SMoore, Eric Dean  		  ": Registered for IOC event notifications\n"));
14272496af39SMoore, Eric Dean  	}
14282496af39SMoore, Eric Dean  
142980d3ac77SMichael Reed 	if (mpt_reset_register(mptfcDoneCtx, mptfc_ioc_reset) == 0) {
14302496af39SMoore, Eric Dean  		dprintk((KERN_INFO MYNAM
14312496af39SMoore, Eric Dean  		  ": Registered for IOC reset notifications\n"));
14322496af39SMoore, Eric Dean  	}
14332496af39SMoore, Eric Dean  
143405e8ec17SMichael Reed 	error = pci_register_driver(&mptfc_driver);
14353bc7bf1dSMichael Reed 	if (error)
143605e8ec17SMichael Reed 		fc_release_transport(mptfc_transport_template);
143705e8ec17SMichael Reed 
143805e8ec17SMichael Reed 	return error;
143905e8ec17SMichael Reed }
144005e8ec17SMichael Reed 
144105e8ec17SMichael Reed /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
144205e8ec17SMichael Reed /**
1443d9489fb6SRandy Dunlap  *	mptfc_remove - Remove fc infrastructure for devices
144405e8ec17SMichael Reed  *	@pdev: Pointer to pci_dev structure
144505e8ec17SMichael Reed  *
144605e8ec17SMichael Reed  */
14473bc7bf1dSMichael Reed static void __devexit
14483bc7bf1dSMichael Reed mptfc_remove(struct pci_dev *pdev)
144905e8ec17SMichael Reed {
145005e8ec17SMichael Reed 	MPT_ADAPTER		*ioc = pci_get_drvdata(pdev);
145105e8ec17SMichael Reed 	struct mptfc_rport_info	*p, *n;
145265207fedSMoore, Eric 	struct workqueue_struct *work_q;
145365207fedSMoore, Eric 	unsigned long		flags;
1454ca2f938eSMichael Reed 	int			ii;
145565207fedSMoore, Eric 
145665207fedSMoore, Eric 	/* destroy workqueue */
145765207fedSMoore, Eric 	if ((work_q=ioc->fc_rescan_work_q)) {
145865207fedSMoore, Eric 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
145965207fedSMoore, Eric 		ioc->fc_rescan_work_q = NULL;
146065207fedSMoore, Eric 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
146165207fedSMoore, Eric 		destroy_workqueue(work_q);
146265207fedSMoore, Eric 	}
146305e8ec17SMichael Reed 
146405e8ec17SMichael Reed 	fc_remove_host(ioc->sh);
146505e8ec17SMichael Reed 
146605e8ec17SMichael Reed 	list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
146705e8ec17SMichael Reed 		list_del(&p->list);
146805e8ec17SMichael Reed 		kfree(p);
146905e8ec17SMichael Reed 	}
147005e8ec17SMichael Reed 
1471ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
1472ca2f938eSMichael Reed 		if (ioc->fc_data.fc_port_page1[ii].data) {
1473ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev,
1474ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].pg_sz,
1475ca2f938eSMichael Reed 				(u8 *) ioc->fc_data.fc_port_page1[ii].data,
1476ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].dma);
1477ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[ii].data = NULL;
1478ca2f938eSMichael Reed 		}
1479ca2f938eSMichael Reed 	}
1480ca2f938eSMichael Reed 
148105e8ec17SMichael Reed 	mptscsih_remove(pdev);
14822496af39SMoore, Eric Dean  }
14832496af39SMoore, Eric Dean  
14842496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14852496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14862496af39SMoore, Eric Dean  /**
14872496af39SMoore, Eric Dean   *	mptfc_exit - Unregisters MPT adapter(s)
14882496af39SMoore, Eric Dean   *
14892496af39SMoore, Eric Dean   */
14902496af39SMoore, Eric Dean  static void __exit
14912496af39SMoore, Eric Dean  mptfc_exit(void)
14922496af39SMoore, Eric Dean  {
14932496af39SMoore, Eric Dean  	pci_unregister_driver(&mptfc_driver);
149405e8ec17SMichael Reed 	fc_release_transport(mptfc_transport_template);
14952496af39SMoore, Eric Dean  
14962496af39SMoore, Eric Dean  	mpt_reset_deregister(mptfcDoneCtx);
14972496af39SMoore, Eric Dean  	dprintk((KERN_INFO MYNAM
14982496af39SMoore, Eric Dean  	  ": Deregistered for IOC reset notifications\n"));
14992496af39SMoore, Eric Dean  
15002496af39SMoore, Eric Dean  	mpt_event_deregister(mptfcDoneCtx);
15012496af39SMoore, Eric Dean  	dprintk((KERN_INFO MYNAM
15022496af39SMoore, Eric Dean  	  ": Deregistered for IOC event notifications\n"));
15032496af39SMoore, Eric Dean  
15042496af39SMoore, Eric Dean  	mpt_deregister(mptfcInternalCtx);
15052496af39SMoore, Eric Dean  	mpt_deregister(mptfcTaskCtx);
15062496af39SMoore, Eric Dean  	mpt_deregister(mptfcDoneCtx);
15072496af39SMoore, Eric Dean  }
15082496af39SMoore, Eric Dean  
15092496af39SMoore, Eric Dean  module_init(mptfc_init);
15102496af39SMoore, Eric Dean  module_exit(mptfc_exit);
1511