xref: /openbmc/linux/drivers/message/fusion/mptfc.c (revision 793955f549c710a1b0c18f823d5d710840747b15)
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   *
69f4203b3SEric 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");
789f4203b3SEric 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 
89*793955f5SEric Moore /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
90*793955f5SEric Moore #define MPTFC_MAX_LUN (16895)
91*793955f5SEric Moore static int max_lun = MPTFC_MAX_LUN;
92*793955f5SEric Moore module_param(max_lun, int, 0);
93*793955f5SEric Moore MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
94*793955f5SEric Moore 
952496af39SMoore, Eric Dean  static int	mptfcDoneCtx = -1;
962496af39SMoore, Eric Dean  static int	mptfcTaskCtx = -1;
972496af39SMoore, Eric Dean  static int	mptfcInternalCtx = -1; /* Used only for internal commands */
982496af39SMoore, Eric Dean  
993bc7bf1dSMichael Reed static int mptfc_target_alloc(struct scsi_target *starget);
1003bc7bf1dSMichael Reed static int mptfc_slave_alloc(struct scsi_device *sdev);
10105e8ec17SMichael Reed static int mptfc_qcmd(struct scsi_cmnd *SCpnt,
10205e8ec17SMichael Reed 		      void (*done)(struct scsi_cmnd *));
1033bc7bf1dSMichael Reed static void mptfc_target_destroy(struct scsi_target *starget);
10405e8ec17SMichael Reed static void mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout);
10505e8ec17SMichael Reed static void __devexit mptfc_remove(struct pci_dev *pdev);
10635508e46SMichael Reed static int mptfc_abort(struct scsi_cmnd *SCpnt);
10735508e46SMichael Reed static int mptfc_dev_reset(struct scsi_cmnd *SCpnt);
10835508e46SMichael Reed static int mptfc_bus_reset(struct scsi_cmnd *SCpnt);
10935508e46SMichael Reed static int mptfc_host_reset(struct scsi_cmnd *SCpnt);
11005e8ec17SMichael Reed 
1112496af39SMoore, Eric Dean  static struct scsi_host_template mptfc_driver_template = {
112f78496daSMoore, Eric Dean 	.module				= THIS_MODULE,
1132496af39SMoore, Eric Dean  	.proc_name			= "mptfc",
1142496af39SMoore, Eric Dean  	.proc_info			= mptscsih_proc_info,
1152496af39SMoore, Eric Dean  	.name				= "MPT FC Host",
1162496af39SMoore, Eric Dean  	.info				= mptscsih_info,
11705e8ec17SMichael Reed 	.queuecommand			= mptfc_qcmd,
1183bc7bf1dSMichael Reed 	.target_alloc			= mptfc_target_alloc,
11905e8ec17SMichael Reed 	.slave_alloc			= mptfc_slave_alloc,
1202496af39SMoore, Eric Dean  	.slave_configure		= mptscsih_slave_configure,
1213bc7bf1dSMichael Reed 	.target_destroy			= mptfc_target_destroy,
1222496af39SMoore, Eric Dean  	.slave_destroy			= mptscsih_slave_destroy,
1236e3815baSMoore, Eric Dean 	.change_queue_depth 		= mptscsih_change_queue_depth,
12435508e46SMichael Reed 	.eh_abort_handler		= mptfc_abort,
12535508e46SMichael Reed 	.eh_device_reset_handler	= mptfc_dev_reset,
12635508e46SMichael Reed 	.eh_bus_reset_handler		= mptfc_bus_reset,
12735508e46SMichael Reed 	.eh_host_reset_handler		= mptfc_host_reset,
1282496af39SMoore, Eric Dean  	.bios_param			= mptscsih_bios_param,
1292496af39SMoore, Eric Dean  	.can_queue			= MPT_FC_CAN_QUEUE,
1302496af39SMoore, Eric Dean  	.this_id			= -1,
1312496af39SMoore, Eric Dean  	.sg_tablesize			= MPT_SCSI_SG_DEPTH,
1322496af39SMoore, Eric Dean  	.max_sectors			= 8192,
1332496af39SMoore, Eric Dean  	.cmd_per_lun			= 7,
1342496af39SMoore, Eric Dean  	.use_clustering			= ENABLE_CLUSTERING,
1352496af39SMoore, Eric Dean  };
1362496af39SMoore, Eric Dean  
1372496af39SMoore, Eric Dean  /****************************************************************************
1382496af39SMoore, Eric Dean   * Supported hardware
1392496af39SMoore, Eric Dean   */
1402496af39SMoore, Eric Dean  
1412496af39SMoore, Eric Dean  static struct pci_device_id mptfc_pci_table[] = {
14287cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC909,
1432496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14487cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919,
1452496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14687cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929,
1472496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14887cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919X,
1492496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
15087cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929X,
1512496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
15287cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC939X,
1533fadc59dSMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
15487cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949X,
1553fadc59dSMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
15687cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949E,
1576d5b0c31SMoore, Eric 		PCI_ANY_ID, PCI_ANY_ID },
1582496af39SMoore, Eric Dean  	{0}	/* Terminating entry */
1592496af39SMoore, Eric Dean  };
1602496af39SMoore, Eric Dean  MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
1612496af39SMoore, Eric Dean  
16205e8ec17SMichael Reed static struct scsi_transport_template *mptfc_transport_template = NULL;
16305e8ec17SMichael Reed 
16403fbcbcdSAdrian Bunk static struct fc_function_template mptfc_transport_functions = {
16505e8ec17SMichael Reed 	.dd_fcrport_size = 8,
16605e8ec17SMichael Reed 	.show_host_node_name = 1,
16705e8ec17SMichael Reed 	.show_host_port_name = 1,
16805e8ec17SMichael Reed 	.show_host_supported_classes = 1,
16905e8ec17SMichael Reed 	.show_host_port_id = 1,
17005e8ec17SMichael Reed 	.show_rport_supported_classes = 1,
17105e8ec17SMichael Reed 	.show_starget_node_name = 1,
17205e8ec17SMichael Reed 	.show_starget_port_name = 1,
17305e8ec17SMichael Reed 	.show_starget_port_id = 1,
17405e8ec17SMichael Reed 	.set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
17505e8ec17SMichael Reed 	.show_rport_dev_loss_tmo = 1,
1765d947f2bSMichael Reed 	.show_host_supported_speeds = 1,
1775d947f2bSMichael Reed 	.show_host_maxframe_size = 1,
1785d947f2bSMichael Reed 	.show_host_speed = 1,
1795d947f2bSMichael Reed 	.show_host_fabric_name = 1,
1805d947f2bSMichael Reed 	.show_host_port_type = 1,
1815d947f2bSMichael Reed 	.show_host_port_state = 1,
1825d947f2bSMichael Reed 	.show_host_symbolic_name = 1,
18305e8ec17SMichael Reed };
18405e8ec17SMichael Reed 
18535508e46SMichael Reed static int
18635508e46SMichael Reed mptfc_block_error_handler(struct scsi_cmnd *SCpnt,
18735508e46SMichael Reed 			  int (*func)(struct scsi_cmnd *SCpnt),
18835508e46SMichael Reed 			  const char *caller)
18935508e46SMichael Reed {
19035508e46SMichael Reed 	struct scsi_device	*sdev = SCpnt->device;
19135508e46SMichael Reed 	struct Scsi_Host	*shost = sdev->host;
19235508e46SMichael Reed 	struct fc_rport		*rport = starget_to_rport(scsi_target(sdev));
19335508e46SMichael Reed 	unsigned long		flags;
19435508e46SMichael Reed 	int			ready;
19535508e46SMichael Reed 
19635508e46SMichael Reed 	spin_lock_irqsave(shost->host_lock, flags);
19735508e46SMichael Reed 	while ((ready = fc_remote_port_chkready(rport) >> 16) == DID_IMM_RETRY) {
19835508e46SMichael Reed 		spin_unlock_irqrestore(shost->host_lock, flags);
19935508e46SMichael Reed 		dfcprintk ((MYIOC_s_INFO_FMT
20035508e46SMichael Reed 			"mptfc_block_error_handler.%d: %d:%d, port status is "
20135508e46SMichael Reed 			"DID_IMM_RETRY, deferring %s recovery.\n",
20235508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->name,
20335508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->sh->host_no,
20435508e46SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,caller));
20535508e46SMichael Reed 		msleep(1000);
20635508e46SMichael Reed 		spin_lock_irqsave(shost->host_lock, flags);
20735508e46SMichael Reed 	}
20835508e46SMichael Reed 	spin_unlock_irqrestore(shost->host_lock, flags);
20935508e46SMichael Reed 
21035508e46SMichael Reed 	if (ready == DID_NO_CONNECT || !SCpnt->device->hostdata) {
21135508e46SMichael Reed 		dfcprintk ((MYIOC_s_INFO_FMT
21235508e46SMichael Reed 			"%s.%d: %d:%d, failing recovery, "
21335508e46SMichael Reed 			"port state %d, vdev %p.\n", caller,
21435508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->name,
21535508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->sh->host_no,
21635508e46SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,ready,
21735508e46SMichael Reed 			SCpnt->device->hostdata));
21835508e46SMichael Reed 		return FAILED;
21935508e46SMichael Reed 	}
22035508e46SMichael Reed 	dfcprintk ((MYIOC_s_INFO_FMT
22135508e46SMichael Reed 		"%s.%d: %d:%d, executing recovery.\n", caller,
22235508e46SMichael Reed 		((MPT_SCSI_HOST *) shost->hostdata)->ioc->name,
22335508e46SMichael Reed 		((MPT_SCSI_HOST *) shost->hostdata)->ioc->sh->host_no,
22435508e46SMichael Reed 		SCpnt->device->id,SCpnt->device->lun));
22535508e46SMichael Reed 	return (*func)(SCpnt);
22635508e46SMichael Reed }
22735508e46SMichael Reed 
22835508e46SMichael Reed static int
22935508e46SMichael Reed mptfc_abort(struct scsi_cmnd *SCpnt)
23035508e46SMichael Reed {
23135508e46SMichael Reed 	return
23235508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_abort, __FUNCTION__);
23335508e46SMichael Reed }
23435508e46SMichael Reed 
23535508e46SMichael Reed static int
23635508e46SMichael Reed mptfc_dev_reset(struct scsi_cmnd *SCpnt)
23735508e46SMichael Reed {
23835508e46SMichael Reed 	return
23935508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_dev_reset, __FUNCTION__);
24035508e46SMichael Reed }
24135508e46SMichael Reed 
24235508e46SMichael Reed static int
24335508e46SMichael Reed mptfc_bus_reset(struct scsi_cmnd *SCpnt)
24435508e46SMichael Reed {
24535508e46SMichael Reed 	return
24635508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_bus_reset, __FUNCTION__);
24735508e46SMichael Reed }
24835508e46SMichael Reed 
24935508e46SMichael Reed static int
25035508e46SMichael Reed mptfc_host_reset(struct scsi_cmnd *SCpnt)
25135508e46SMichael Reed {
25235508e46SMichael Reed 	return
25335508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_host_reset, __FUNCTION__);
25435508e46SMichael Reed }
25535508e46SMichael Reed 
25605e8ec17SMichael Reed static void
25705e8ec17SMichael Reed mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
25805e8ec17SMichael Reed {
25905e8ec17SMichael Reed 	if (timeout > 0)
26005e8ec17SMichael Reed 		rport->dev_loss_tmo = timeout;
26105e8ec17SMichael Reed 	else
26205e8ec17SMichael Reed 		rport->dev_loss_tmo = mptfc_dev_loss_tmo;
26305e8ec17SMichael Reed }
26405e8ec17SMichael Reed 
26505e8ec17SMichael Reed static int
26605e8ec17SMichael Reed mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
26705e8ec17SMichael Reed {
26805e8ec17SMichael Reed 	FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
26905e8ec17SMichael Reed 	FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
27005e8ec17SMichael Reed 
27105e8ec17SMichael Reed 	if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
27205e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
27305e8ec17SMichael Reed 			return 0;
27405e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
27505e8ec17SMichael Reed 			return -1;
27605e8ec17SMichael Reed 		return 1;
27705e8ec17SMichael Reed 	}
27805e8ec17SMichael Reed 	if ((*aa)->CurrentBus < (*bb)->CurrentBus)
27905e8ec17SMichael Reed 		return -1;
28005e8ec17SMichael Reed 	return 1;
28105e8ec17SMichael Reed }
28205e8ec17SMichael Reed 
28305e8ec17SMichael Reed static int
28405e8ec17SMichael Reed mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
28505e8ec17SMichael Reed 	void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
28605e8ec17SMichael Reed {
28705e8ec17SMichael Reed 	ConfigPageHeader_t	 hdr;
28805e8ec17SMichael Reed 	CONFIGPARMS		 cfg;
28905e8ec17SMichael Reed 	FCDevicePage0_t		*ppage0_alloc, *fc;
29005e8ec17SMichael Reed 	dma_addr_t		 page0_dma;
29105e8ec17SMichael Reed 	int			 data_sz;
29205e8ec17SMichael Reed 	int			 ii;
29305e8ec17SMichael Reed 
29405e8ec17SMichael Reed 	FCDevicePage0_t		*p0_array=NULL, *p_p0;
29505e8ec17SMichael Reed 	FCDevicePage0_t		**pp0_array=NULL, **p_pp0;
29605e8ec17SMichael Reed 
29705e8ec17SMichael Reed 	int			 rc = -ENOMEM;
29805e8ec17SMichael Reed 	U32			 port_id = 0xffffff;
29905e8ec17SMichael Reed 	int			 num_targ = 0;
30005e8ec17SMichael Reed 	int			 max_bus = ioc->facts.MaxBuses;
301*793955f5SEric Moore 	int			 max_targ;
30205e8ec17SMichael Reed 
303*793955f5SEric Moore 	max_targ = (ioc->facts.MaxDevices == 0) ? 256 : ioc->facts.MaxDevices;
30405e8ec17SMichael Reed 
30505e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
30605e8ec17SMichael Reed 	p_p0 = p0_array =  kzalloc(data_sz, GFP_KERNEL);
30705e8ec17SMichael Reed 	if (!p0_array)
30805e8ec17SMichael Reed 		goto out;
30905e8ec17SMichael Reed 
31005e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
31105e8ec17SMichael Reed 	p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
31205e8ec17SMichael Reed 	if (!pp0_array)
31305e8ec17SMichael Reed 		goto out;
31405e8ec17SMichael Reed 
31505e8ec17SMichael Reed 	do {
31605e8ec17SMichael Reed 		/* Get FC Device Page 0 header */
31705e8ec17SMichael Reed 		hdr.PageVersion = 0;
31805e8ec17SMichael Reed 		hdr.PageLength = 0;
31905e8ec17SMichael Reed 		hdr.PageNumber = 0;
32005e8ec17SMichael Reed 		hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
32105e8ec17SMichael Reed 		cfg.cfghdr.hdr = &hdr;
32205e8ec17SMichael Reed 		cfg.physAddr = -1;
32305e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
32405e8ec17SMichael Reed 		cfg.dir = 0;
32505e8ec17SMichael Reed 		cfg.pageAddr = port_id;
32605e8ec17SMichael Reed 		cfg.timeout = 0;
32705e8ec17SMichael Reed 
32805e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) != 0)
32905e8ec17SMichael Reed 			break;
33005e8ec17SMichael Reed 
33105e8ec17SMichael Reed 		if (hdr.PageLength <= 0)
33205e8ec17SMichael Reed 			break;
33305e8ec17SMichael Reed 
33405e8ec17SMichael Reed 		data_sz = hdr.PageLength * 4;
33505e8ec17SMichael Reed 		ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
33605e8ec17SMichael Reed 		    					&page0_dma);
33705e8ec17SMichael Reed 		rc = -ENOMEM;
33805e8ec17SMichael Reed 		if (!ppage0_alloc)
33905e8ec17SMichael Reed 			break;
34005e8ec17SMichael Reed 
34105e8ec17SMichael Reed 		cfg.physAddr = page0_dma;
34205e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
34305e8ec17SMichael Reed 
34405e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
34505e8ec17SMichael Reed 			ppage0_alloc->PortIdentifier =
34605e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->PortIdentifier);
34705e8ec17SMichael Reed 
34805e8ec17SMichael Reed 			ppage0_alloc->WWNN.Low =
34905e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.Low);
35005e8ec17SMichael Reed 
35105e8ec17SMichael Reed 			ppage0_alloc->WWNN.High =
35205e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.High);
35305e8ec17SMichael Reed 
35405e8ec17SMichael Reed 			ppage0_alloc->WWPN.Low =
35505e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.Low);
35605e8ec17SMichael Reed 
35705e8ec17SMichael Reed 			ppage0_alloc->WWPN.High =
35805e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.High);
35905e8ec17SMichael Reed 
36005e8ec17SMichael Reed 			ppage0_alloc->BBCredit =
36105e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->BBCredit);
36205e8ec17SMichael Reed 
36305e8ec17SMichael Reed 			ppage0_alloc->MaxRxFrameSize =
36405e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
36505e8ec17SMichael Reed 
36605e8ec17SMichael Reed 			port_id = ppage0_alloc->PortIdentifier;
36705e8ec17SMichael Reed 			num_targ++;
36805e8ec17SMichael Reed 			*p_p0 = *ppage0_alloc;	/* save data */
36905e8ec17SMichael Reed 			*p_pp0++ = p_p0++;	/* save addr */
37005e8ec17SMichael Reed 		}
37105e8ec17SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz,
37205e8ec17SMichael Reed 		    			(u8 *) ppage0_alloc, page0_dma);
37305e8ec17SMichael Reed 		if (rc != 0)
37405e8ec17SMichael Reed 			break;
37505e8ec17SMichael Reed 
37605e8ec17SMichael Reed 	} while (port_id <= 0xff0000);
37705e8ec17SMichael Reed 
37805e8ec17SMichael Reed 	if (num_targ) {
37905e8ec17SMichael Reed 		/* sort array */
38005e8ec17SMichael Reed 		if (num_targ > 1)
38105e8ec17SMichael Reed 			sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
38205e8ec17SMichael Reed 				mptfc_FcDevPage0_cmp_func, NULL);
38305e8ec17SMichael Reed 		/* call caller's func for each targ */
38405e8ec17SMichael Reed 		for (ii = 0; ii < num_targ;  ii++) {
38505e8ec17SMichael Reed 			fc = *(pp0_array+ii);
38605e8ec17SMichael Reed 			func(ioc, ioc_port, fc);
38705e8ec17SMichael Reed 		}
38805e8ec17SMichael Reed 	}
38905e8ec17SMichael Reed 
39005e8ec17SMichael Reed  out:
39105e8ec17SMichael Reed 	kfree(pp0_array);
39205e8ec17SMichael Reed 	kfree(p0_array);
39305e8ec17SMichael Reed 	return rc;
39405e8ec17SMichael Reed }
39505e8ec17SMichael Reed 
39605e8ec17SMichael Reed static int
39705e8ec17SMichael Reed mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
39805e8ec17SMichael Reed {
39905e8ec17SMichael Reed 	/* not currently usable */
40005e8ec17SMichael Reed 	if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
40105e8ec17SMichael Reed 			  MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
40205e8ec17SMichael Reed 		return -1;
40305e8ec17SMichael Reed 
40405e8ec17SMichael Reed 	if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
40505e8ec17SMichael Reed 		return -1;
40605e8ec17SMichael Reed 
40705e8ec17SMichael Reed 	if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
40805e8ec17SMichael Reed 		return -1;
40905e8ec17SMichael Reed 
41005e8ec17SMichael Reed 	/*
41105e8ec17SMichael Reed 	 * board data structure already normalized to platform endianness
41205e8ec17SMichael Reed 	 * shifted to avoid unaligned access on 64 bit architecture
41305e8ec17SMichael Reed 	 */
41405e8ec17SMichael Reed 	rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
41505e8ec17SMichael Reed 	rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
41605e8ec17SMichael Reed 	rid->port_id =   pg0->PortIdentifier;
41705e8ec17SMichael Reed 	rid->roles = FC_RPORT_ROLE_UNKNOWN;
41805e8ec17SMichael Reed 
41905e8ec17SMichael Reed 	return 0;
42005e8ec17SMichael Reed }
42105e8ec17SMichael Reed 
42205e8ec17SMichael Reed static void
42305e8ec17SMichael Reed mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
42405e8ec17SMichael Reed {
42505e8ec17SMichael Reed 	struct fc_rport_identifiers rport_ids;
42605e8ec17SMichael Reed 	struct fc_rport		*rport;
42705e8ec17SMichael Reed 	struct mptfc_rport_info	*ri;
4283bc7bf1dSMichael Reed 	int			new_ri = 1;
42965207fedSMoore, Eric 	u64			pn, nn;
4303bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
4316dd727daSmdr@sgi.com 	u32			roles = FC_RPORT_ROLE_UNKNOWN;
43205e8ec17SMichael Reed 
43305e8ec17SMichael Reed 	if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
43405e8ec17SMichael Reed 		return;
43505e8ec17SMichael Reed 
4366dd727daSmdr@sgi.com 	roles |= FC_RPORT_ROLE_FCP_TARGET;
4376dd727daSmdr@sgi.com 	if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
4386dd727daSmdr@sgi.com 		roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4396dd727daSmdr@sgi.com 
44005e8ec17SMichael Reed 	/* scan list looking for a match */
44105e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
4423bc7bf1dSMichael Reed 		pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
4433bc7bf1dSMichael Reed 		if (pn == rport_ids.port_name) {	/* match */
44405e8ec17SMichael Reed 			list_move_tail(&ri->list, &ioc->fc_rports);
4453bc7bf1dSMichael Reed 			new_ri = 0;
44605e8ec17SMichael Reed 			break;
44705e8ec17SMichael Reed 		}
44805e8ec17SMichael Reed 	}
4493bc7bf1dSMichael Reed 	if (new_ri) {	/* allocate one */
45005e8ec17SMichael Reed 		ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
45105e8ec17SMichael Reed 		if (!ri)
45205e8ec17SMichael Reed 			return;
45305e8ec17SMichael Reed 		list_add_tail(&ri->list, &ioc->fc_rports);
45405e8ec17SMichael Reed 	}
45505e8ec17SMichael Reed 
45605e8ec17SMichael Reed 	ri->pg0 = *pg0;	/* add/update pg0 data */
45705e8ec17SMichael Reed 	ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
45805e8ec17SMichael Reed 
4593bc7bf1dSMichael Reed 	/* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
46005e8ec17SMichael Reed 	if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
46105e8ec17SMichael Reed 		ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
46205e8ec17SMichael Reed 		rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
46305e8ec17SMichael Reed 		if (rport) {
46405e8ec17SMichael Reed 			ri->rport = rport;
4653bc7bf1dSMichael Reed 			if (new_ri) /* may have been reset by user */
46605e8ec17SMichael Reed 				rport->dev_loss_tmo = mptfc_dev_loss_tmo;
46705e8ec17SMichael Reed 			/*
46805e8ec17SMichael Reed 			 * if already mapped, remap here.  If not mapped,
4693bc7bf1dSMichael Reed 			 * target_alloc will allocate vtarget and map,
4703bc7bf1dSMichael Reed 			 * slave_alloc will fill in vdev from vtarget.
47105e8ec17SMichael Reed 			 */
4723bc7bf1dSMichael Reed 			if (ri->starget) {
4733bc7bf1dSMichael Reed 				vtarget = ri->starget->hostdata;
4743bc7bf1dSMichael Reed 				if (vtarget) {
475*793955f5SEric Moore 					vtarget->id = pg0->CurrentTargetID;
476*793955f5SEric Moore 					vtarget->channel = pg0->CurrentBus;
47705e8ec17SMichael Reed 				}
4783bc7bf1dSMichael Reed 			}
47965207fedSMoore, Eric 			*((struct mptfc_rport_info **)rport->dd_data) = ri;
4806dd727daSmdr@sgi.com 			/* scan will be scheduled once rport becomes a target */
4816dd727daSmdr@sgi.com 			fc_remote_port_rolechg(rport,roles);
48265207fedSMoore, Eric 
48365207fedSMoore, Eric 			pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
48465207fedSMoore, Eric 			nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
4853bc7bf1dSMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
4863bc7bf1dSMichael Reed 				"mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
48705e8ec17SMichael Reed 				"rport tid %d, tmo %d\n",
4883bc7bf1dSMichael Reed 					ioc->name,
489914c2d8eSMoore, Eric 					ioc->sh->host_no,
49005e8ec17SMichael Reed 					pg0->PortIdentifier,
49165207fedSMoore, Eric 					(unsigned long long)nn,
49265207fedSMoore, Eric 					(unsigned long long)pn,
49305e8ec17SMichael Reed 					pg0->CurrentTargetID,
49405e8ec17SMichael Reed 					ri->rport->scsi_target_id,
4953bc7bf1dSMichael Reed 					ri->rport->dev_loss_tmo));
49605e8ec17SMichael Reed 		} else {
49705e8ec17SMichael Reed 			list_del(&ri->list);
49805e8ec17SMichael Reed 			kfree(ri);
49905e8ec17SMichael Reed 			ri = NULL;
50005e8ec17SMichael Reed 		}
50105e8ec17SMichael Reed 	}
50205e8ec17SMichael Reed }
50305e8ec17SMichael Reed 
50405e8ec17SMichael Reed /*
5053bc7bf1dSMichael Reed  *	OS entry point to allow for host driver to free allocated memory
5063bc7bf1dSMichael Reed  *	Called if no device present or device being unloaded
5073bc7bf1dSMichael Reed  */
5083bc7bf1dSMichael Reed static void
5093bc7bf1dSMichael Reed mptfc_target_destroy(struct scsi_target *starget)
5103bc7bf1dSMichael Reed {
5113bc7bf1dSMichael Reed 	struct fc_rport		*rport;
5123bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
5133bc7bf1dSMichael Reed 
5143bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
5153bc7bf1dSMichael Reed 	if (rport) {
5163bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
5173bc7bf1dSMichael Reed 		if (ri)	/* better be! */
5183bc7bf1dSMichael Reed 			ri->starget = NULL;
5193bc7bf1dSMichael Reed 	}
5203bc7bf1dSMichael Reed 	if (starget->hostdata)
5213bc7bf1dSMichael Reed 		kfree(starget->hostdata);
5223bc7bf1dSMichael Reed 	starget->hostdata = NULL;
5233bc7bf1dSMichael Reed }
5243bc7bf1dSMichael Reed 
5253bc7bf1dSMichael Reed /*
5263bc7bf1dSMichael Reed  *	OS entry point to allow host driver to alloc memory
5273bc7bf1dSMichael Reed  *	for each scsi target. Called once per device the bus scan.
5283bc7bf1dSMichael Reed  *	Return non-zero if allocation fails.
5293bc7bf1dSMichael Reed  */
5303bc7bf1dSMichael Reed static int
5313bc7bf1dSMichael Reed mptfc_target_alloc(struct scsi_target *starget)
5323bc7bf1dSMichael Reed {
5333bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
5343bc7bf1dSMichael Reed 	struct fc_rport		*rport;
5353bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
5363bc7bf1dSMichael Reed 	int			rc;
5373bc7bf1dSMichael Reed 
5383bc7bf1dSMichael Reed 	vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
5393bc7bf1dSMichael Reed 	if (!vtarget)
5403bc7bf1dSMichael Reed 		return -ENOMEM;
5413bc7bf1dSMichael Reed 	starget->hostdata = vtarget;
5423bc7bf1dSMichael Reed 
5433bc7bf1dSMichael Reed 	rc = -ENODEV;
5443bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
5453bc7bf1dSMichael Reed 	if (rport) {
5463bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
5473bc7bf1dSMichael Reed 		if (ri) {	/* better be! */
548*793955f5SEric Moore 			vtarget->id = ri->pg0.CurrentTargetID;
549*793955f5SEric Moore 			vtarget->channel = ri->pg0.CurrentBus;
5503bc7bf1dSMichael Reed 			ri->starget = starget;
5513bc7bf1dSMichael Reed 			rc = 0;
5523bc7bf1dSMichael Reed 		}
5533bc7bf1dSMichael Reed 	}
5543bc7bf1dSMichael Reed 	if (rc != 0) {
5553bc7bf1dSMichael Reed 		kfree(vtarget);
5563bc7bf1dSMichael Reed 		starget->hostdata = NULL;
5573bc7bf1dSMichael Reed 	}
5583bc7bf1dSMichael Reed 
5593bc7bf1dSMichael Reed 	return rc;
5603bc7bf1dSMichael Reed }
5613bc7bf1dSMichael Reed 
5623bc7bf1dSMichael Reed /*
56305e8ec17SMichael Reed  *	OS entry point to allow host driver to alloc memory
56405e8ec17SMichael Reed  *	for each scsi device. Called once per device the bus scan.
56505e8ec17SMichael Reed  *	Return non-zero if allocation fails.
56605e8ec17SMichael Reed  *	Init memory once per LUN.
56705e8ec17SMichael Reed  */
56803fbcbcdSAdrian Bunk static int
56905e8ec17SMichael Reed mptfc_slave_alloc(struct scsi_device *sdev)
57005e8ec17SMichael Reed {
57105e8ec17SMichael Reed 	MPT_SCSI_HOST		*hd;
57205e8ec17SMichael Reed 	VirtTarget		*vtarget;
57305e8ec17SMichael Reed 	VirtDevice		*vdev;
57405e8ec17SMichael Reed 	struct scsi_target	*starget;
57505e8ec17SMichael Reed 	struct fc_rport		*rport;
57605e8ec17SMichael Reed 
57705e8ec17SMichael Reed 
57865207fedSMoore, Eric 	starget = scsi_target(sdev);
57965207fedSMoore, Eric 	rport = starget_to_rport(starget);
58005e8ec17SMichael Reed 
58105e8ec17SMichael Reed 	if (!rport || fc_remote_port_chkready(rport))
58205e8ec17SMichael Reed 		return -ENXIO;
58305e8ec17SMichael Reed 
58405e8ec17SMichael Reed 	hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
58505e8ec17SMichael Reed 
5863bc7bf1dSMichael Reed 	vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
58705e8ec17SMichael Reed 	if (!vdev) {
58805e8ec17SMichael Reed 		printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
58905e8ec17SMichael Reed 				hd->ioc->name, sizeof(VirtDevice));
59005e8ec17SMichael Reed 		return -ENOMEM;
59105e8ec17SMichael Reed 	}
59205e8ec17SMichael Reed 
59305e8ec17SMichael Reed 
59405e8ec17SMichael Reed 	sdev->hostdata = vdev;
59505e8ec17SMichael Reed 	vtarget = starget->hostdata;
5963bc7bf1dSMichael Reed 
59705e8ec17SMichael Reed 	if (vtarget->num_luns == 0) {
5983bc7bf1dSMichael Reed 		vtarget->ioc_id = hd->ioc->id;
599ba856d32SEric Moore 		vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
60005e8ec17SMichael Reed 	}
60105e8ec17SMichael Reed 
60205e8ec17SMichael Reed 	vdev->vtarget = vtarget;
60305e8ec17SMichael Reed 	vdev->lun = sdev->lun;
60405e8ec17SMichael Reed 
60505e8ec17SMichael Reed 	vtarget->num_luns++;
60605e8ec17SMichael Reed 
60765207fedSMoore, Eric 
608914c2d8eSMoore, Eric #ifdef DMPT_DEBUG_FC
609914c2d8eSMoore, Eric 	{
61065207fedSMoore, Eric 	u64 nn, pn;
611914c2d8eSMoore, Eric 	struct mptfc_rport_info *ri;
612914c2d8eSMoore, Eric 	ri = *((struct mptfc_rport_info **)rport->dd_data);
61365207fedSMoore, Eric 	pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
61465207fedSMoore, Eric 	nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
6153bc7bf1dSMichael Reed 	dfcprintk ((MYIOC_s_INFO_FMT
6163bc7bf1dSMichael Reed 		"mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
61705e8ec17SMichael Reed 	        "CurrentTargetID %d, %x %llx %llx\n",
618914c2d8eSMoore, Eric 		hd->ioc->name,
61905e8ec17SMichael Reed 		sdev->host->host_no,
62005e8ec17SMichael Reed 		vtarget->num_luns,
62105e8ec17SMichael Reed 		sdev->id, ri->pg0.CurrentTargetID,
62265207fedSMoore, Eric 		ri->pg0.PortIdentifier,
62365207fedSMoore, Eric 		(unsigned long long)pn,
62465207fedSMoore, Eric 		(unsigned long long)nn));
625914c2d8eSMoore, Eric 	}
626914c2d8eSMoore, Eric #endif
62705e8ec17SMichael Reed 
62805e8ec17SMichael Reed 	return 0;
62905e8ec17SMichael Reed }
63005e8ec17SMichael Reed 
63105e8ec17SMichael Reed static int
63205e8ec17SMichael Reed mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
63305e8ec17SMichael Reed {
6343bc7bf1dSMichael Reed 	struct mptfc_rport_info	*ri;
63505e8ec17SMichael Reed 	struct fc_rport	*rport = starget_to_rport(scsi_target(SCpnt->device));
63605e8ec17SMichael Reed 	int		err;
637*793955f5SEric Moore 	VirtDevice	*vdev = SCpnt->device->hostdata;
63805e8ec17SMichael Reed 
639*793955f5SEric Moore 	if (!vdev || !vdev->vtarget) {
640*793955f5SEric Moore 		SCpnt->result = DID_NO_CONNECT << 16;
64105e8ec17SMichael Reed 		done(SCpnt);
64205e8ec17SMichael Reed 		return 0;
64305e8ec17SMichael Reed 	}
6443bc7bf1dSMichael Reed 
645*793955f5SEric Moore 	err = fc_remote_port_chkready(rport);
646*793955f5SEric Moore 	if (unlikely(err)) {
647*793955f5SEric Moore 		SCpnt->result = err;
64835508e46SMichael Reed 		done(SCpnt);
64935508e46SMichael Reed 		return 0;
65035508e46SMichael Reed 	}
65135508e46SMichael Reed 
65265207fedSMoore, Eric 	/* dd_data is null until finished adding target */
65365207fedSMoore, Eric 	ri = *((struct mptfc_rport_info **)rport->dd_data);
65465207fedSMoore, Eric 	if (unlikely(!ri)) {
65565207fedSMoore, Eric 		dfcprintk ((MYIOC_s_INFO_FMT
65665207fedSMoore, Eric 			"mptfc_qcmd.%d: %d:%d, dd_data is null.\n",
65765207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
65865207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
65965207fedSMoore, Eric 			SCpnt->device->id,SCpnt->device->lun));
66065207fedSMoore, Eric 		SCpnt->result = DID_IMM_RETRY << 16;
66165207fedSMoore, Eric 		done(SCpnt);
66265207fedSMoore, Eric 		return 0;
66365207fedSMoore, Eric 	}
66465207fedSMoore, Eric 
66565207fedSMoore, Eric 	err = mptscsih_qcmd(SCpnt,done);
66665207fedSMoore, Eric #ifdef DMPT_DEBUG_FC
66765207fedSMoore, Eric 	if (unlikely(err)) {
66865207fedSMoore, Eric 		dfcprintk ((MYIOC_s_INFO_FMT
669419835e2SMichael Reed 			"mptfc_qcmd.%d: %d:%d, mptscsih_qcmd returns non-zero, (%x).\n",
67065207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
67165207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
672419835e2SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,err));
67365207fedSMoore, Eric 	}
67465207fedSMoore, Eric #endif
67565207fedSMoore, Eric 	return err;
67605e8ec17SMichael Reed }
67705e8ec17SMichael Reed 
67880d3ac77SMichael Reed /*
67980d3ac77SMichael Reed  *	mptfc_GetFcPortPage0 - Fetch FCPort config Page0.
68080d3ac77SMichael Reed  *	@ioc: Pointer to MPT_ADAPTER structure
68180d3ac77SMichael Reed  *	@portnum: IOC Port number
68280d3ac77SMichael Reed  *
68380d3ac77SMichael Reed  *	Return: 0 for success
68480d3ac77SMichael Reed  *	-ENOMEM if no memory available
68580d3ac77SMichael Reed  *		-EPERM if not allowed due to ISR context
68680d3ac77SMichael Reed  *		-EAGAIN if no msg frames currently available
68780d3ac77SMichael Reed  *		-EFAULT for non-successful reply or no reply (timeout)
68880d3ac77SMichael Reed  *		-EINVAL portnum arg out of range (hardwired to two elements)
68980d3ac77SMichael Reed  */
69080d3ac77SMichael Reed static int
69180d3ac77SMichael Reed mptfc_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
69280d3ac77SMichael Reed {
69380d3ac77SMichael Reed 	ConfigPageHeader_t	 hdr;
69480d3ac77SMichael Reed 	CONFIGPARMS		 cfg;
69580d3ac77SMichael Reed 	FCPortPage0_t		*ppage0_alloc;
69680d3ac77SMichael Reed 	FCPortPage0_t		*pp0dest;
69780d3ac77SMichael Reed 	dma_addr_t		 page0_dma;
69880d3ac77SMichael Reed 	int			 data_sz;
69980d3ac77SMichael Reed 	int			 copy_sz;
70080d3ac77SMichael Reed 	int			 rc;
70180d3ac77SMichael Reed 	int			 count = 400;
70280d3ac77SMichael Reed 
70380d3ac77SMichael Reed 	if (portnum > 1)
70480d3ac77SMichael Reed 		return -EINVAL;
70580d3ac77SMichael Reed 
70680d3ac77SMichael Reed 	/* Get FCPort Page 0 header */
70780d3ac77SMichael Reed 	hdr.PageVersion = 0;
70880d3ac77SMichael Reed 	hdr.PageLength = 0;
70980d3ac77SMichael Reed 	hdr.PageNumber = 0;
71080d3ac77SMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
71180d3ac77SMichael Reed 	cfg.cfghdr.hdr = &hdr;
71280d3ac77SMichael Reed 	cfg.physAddr = -1;
71380d3ac77SMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
71480d3ac77SMichael Reed 	cfg.dir = 0;
71580d3ac77SMichael Reed 	cfg.pageAddr = portnum;
71680d3ac77SMichael Reed 	cfg.timeout = 0;
71780d3ac77SMichael Reed 
71880d3ac77SMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
71980d3ac77SMichael Reed 		return rc;
72080d3ac77SMichael Reed 
72180d3ac77SMichael Reed 	if (hdr.PageLength == 0)
72280d3ac77SMichael Reed 		return 0;
72380d3ac77SMichael Reed 
72480d3ac77SMichael Reed 	data_sz = hdr.PageLength * 4;
72580d3ac77SMichael Reed 	rc = -ENOMEM;
72680d3ac77SMichael Reed 	ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
72780d3ac77SMichael Reed 	if (ppage0_alloc) {
72880d3ac77SMichael Reed 
72980d3ac77SMichael Reed  try_again:
73080d3ac77SMichael Reed 		memset((u8 *)ppage0_alloc, 0, data_sz);
73180d3ac77SMichael Reed 		cfg.physAddr = page0_dma;
73280d3ac77SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
73380d3ac77SMichael Reed 
73480d3ac77SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
73580d3ac77SMichael Reed 			/* save the data */
73680d3ac77SMichael Reed 			pp0dest = &ioc->fc_port_page0[portnum];
73780d3ac77SMichael Reed 			copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
73880d3ac77SMichael Reed 			memcpy(pp0dest, ppage0_alloc, copy_sz);
73980d3ac77SMichael Reed 
74080d3ac77SMichael Reed 			/*
74180d3ac77SMichael Reed 			 *	Normalize endianness of structure data,
74280d3ac77SMichael Reed 			 *	by byte-swapping all > 1 byte fields!
74380d3ac77SMichael Reed 			 */
74480d3ac77SMichael Reed 			pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
74580d3ac77SMichael Reed 			pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
74680d3ac77SMichael Reed 			pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
74780d3ac77SMichael Reed 			pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
74880d3ac77SMichael Reed 			pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
74980d3ac77SMichael Reed 			pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
75080d3ac77SMichael Reed 			pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
75180d3ac77SMichael Reed 			pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
75280d3ac77SMichael Reed 			pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
75380d3ac77SMichael Reed 			pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
75480d3ac77SMichael Reed 			pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
75580d3ac77SMichael Reed 			pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
75680d3ac77SMichael Reed 			pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
75780d3ac77SMichael Reed 			pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
75880d3ac77SMichael Reed 			pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
75980d3ac77SMichael Reed 			pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
76080d3ac77SMichael Reed 
76180d3ac77SMichael Reed 			/*
76280d3ac77SMichael Reed 			 * if still doing discovery,
76380d3ac77SMichael Reed 			 * hang loose a while until finished
76480d3ac77SMichael Reed 			 */
76577d88ee2SMichael Reed 			if ((pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) ||
76677d88ee2SMichael Reed 			    (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE &&
76777d88ee2SMichael Reed 			     (pp0dest->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_TYPE_MASK)
76877d88ee2SMichael Reed 			      == MPI_FCPORTPAGE0_FLAGS_ATTACH_NO_INIT)) {
76980d3ac77SMichael Reed 				if (count-- > 0) {
770d6be06c8SMichael Reed 					msleep(100);
77180d3ac77SMichael Reed 					goto try_again;
77280d3ac77SMichael Reed 				}
77380d3ac77SMichael Reed 				printk(MYIOC_s_INFO_FMT "Firmware discovery not"
77480d3ac77SMichael Reed 							" complete.\n",
77580d3ac77SMichael Reed 						ioc->name);
77680d3ac77SMichael Reed 			}
77780d3ac77SMichael Reed 		}
77880d3ac77SMichael Reed 
77980d3ac77SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
78080d3ac77SMichael Reed 	}
78180d3ac77SMichael Reed 
78280d3ac77SMichael Reed 	return rc;
78380d3ac77SMichael Reed }
78480d3ac77SMichael Reed 
785ca2f938eSMichael Reed static int
786ca2f938eSMichael Reed mptfc_WriteFcPortPage1(MPT_ADAPTER *ioc, int portnum)
787ca2f938eSMichael Reed {
788ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
789ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
790ca2f938eSMichael Reed 	int			 rc;
791ca2f938eSMichael Reed 
792ca2f938eSMichael Reed 	if (portnum > 1)
793ca2f938eSMichael Reed 		return -EINVAL;
794ca2f938eSMichael Reed 
795ca2f938eSMichael Reed 	if (!(ioc->fc_data.fc_port_page1[portnum].data))
796ca2f938eSMichael Reed 		return -EINVAL;
797ca2f938eSMichael Reed 
798ca2f938eSMichael Reed 	/* get fcport page 1 header */
799ca2f938eSMichael Reed 	hdr.PageVersion = 0;
800ca2f938eSMichael Reed 	hdr.PageLength = 0;
801ca2f938eSMichael Reed 	hdr.PageNumber = 1;
802ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
803ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
804ca2f938eSMichael Reed 	cfg.physAddr = -1;
805ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
806ca2f938eSMichael Reed 	cfg.dir = 0;
807ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
808ca2f938eSMichael Reed 	cfg.timeout = 0;
809ca2f938eSMichael Reed 
810ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
811ca2f938eSMichael Reed 		return rc;
812ca2f938eSMichael Reed 
813ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
814ca2f938eSMichael Reed 		return -ENODEV;
815ca2f938eSMichael Reed 
816ca2f938eSMichael Reed 	if (hdr.PageLength*4 != ioc->fc_data.fc_port_page1[portnum].pg_sz)
817ca2f938eSMichael Reed 		return -EINVAL;
818ca2f938eSMichael Reed 
819ca2f938eSMichael Reed 	cfg.physAddr = ioc->fc_data.fc_port_page1[portnum].dma;
820ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
821ca2f938eSMichael Reed 	cfg.dir = 1;
822ca2f938eSMichael Reed 
823ca2f938eSMichael Reed 	rc = mpt_config(ioc, &cfg);
824ca2f938eSMichael Reed 
825ca2f938eSMichael Reed 	return rc;
826ca2f938eSMichael Reed }
827ca2f938eSMichael Reed 
828ca2f938eSMichael Reed static int
829ca2f938eSMichael Reed mptfc_GetFcPortPage1(MPT_ADAPTER *ioc, int portnum)
830ca2f938eSMichael Reed {
831ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
832ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
833ca2f938eSMichael Reed 	FCPortPage1_t		*page1_alloc;
834ca2f938eSMichael Reed 	dma_addr_t		 page1_dma;
835ca2f938eSMichael Reed 	int			 data_sz;
836ca2f938eSMichael Reed 	int			 rc;
837ca2f938eSMichael Reed 
838ca2f938eSMichael Reed 	if (portnum > 1)
839ca2f938eSMichael Reed 		return -EINVAL;
840ca2f938eSMichael Reed 
841ca2f938eSMichael Reed 	/* get fcport page 1 header */
842ca2f938eSMichael Reed 	hdr.PageVersion = 0;
843ca2f938eSMichael Reed 	hdr.PageLength = 0;
844ca2f938eSMichael Reed 	hdr.PageNumber = 1;
845ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
846ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
847ca2f938eSMichael Reed 	cfg.physAddr = -1;
848ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
849ca2f938eSMichael Reed 	cfg.dir = 0;
850ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
851ca2f938eSMichael Reed 	cfg.timeout = 0;
852ca2f938eSMichael Reed 
853ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
854ca2f938eSMichael Reed 		return rc;
855ca2f938eSMichael Reed 
856ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
857ca2f938eSMichael Reed 		return -ENODEV;
858ca2f938eSMichael Reed 
859ca2f938eSMichael Reed start_over:
860ca2f938eSMichael Reed 
861ca2f938eSMichael Reed 	if (ioc->fc_data.fc_port_page1[portnum].data == NULL) {
862ca2f938eSMichael Reed 		data_sz = hdr.PageLength * 4;
863ca2f938eSMichael Reed 		if (data_sz < sizeof(FCPortPage1_t))
864ca2f938eSMichael Reed 			data_sz = sizeof(FCPortPage1_t);
865ca2f938eSMichael Reed 
866ca2f938eSMichael Reed 		page1_alloc = (FCPortPage1_t *) pci_alloc_consistent(ioc->pcidev,
867ca2f938eSMichael Reed 						data_sz,
868ca2f938eSMichael Reed 						&page1_dma);
869ca2f938eSMichael Reed 		if (!page1_alloc)
870ca2f938eSMichael Reed 			return -ENOMEM;
871ca2f938eSMichael Reed 	}
872ca2f938eSMichael Reed 	else {
873ca2f938eSMichael Reed 		page1_alloc = ioc->fc_data.fc_port_page1[portnum].data;
874ca2f938eSMichael Reed 		page1_dma = ioc->fc_data.fc_port_page1[portnum].dma;
875ca2f938eSMichael Reed 		data_sz = ioc->fc_data.fc_port_page1[portnum].pg_sz;
876ca2f938eSMichael Reed 		if (hdr.PageLength * 4 > data_sz) {
877ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[portnum].data = NULL;
878ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
879ca2f938eSMichael Reed 				page1_alloc, page1_dma);
880ca2f938eSMichael Reed 			goto start_over;
881ca2f938eSMichael Reed 		}
882ca2f938eSMichael Reed 	}
883ca2f938eSMichael Reed 
884ca2f938eSMichael Reed 	memset(page1_alloc,0,data_sz);
885ca2f938eSMichael Reed 
886ca2f938eSMichael Reed 	cfg.physAddr = page1_dma;
887ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
888ca2f938eSMichael Reed 
889ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) == 0) {
890ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = page1_alloc;
891ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].pg_sz = data_sz;
892ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].dma = page1_dma;
893ca2f938eSMichael Reed 	}
894ca2f938eSMichael Reed 	else {
895ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = NULL;
896ca2f938eSMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
897ca2f938eSMichael Reed 			page1_alloc, page1_dma);
898ca2f938eSMichael Reed 	}
899ca2f938eSMichael Reed 
900ca2f938eSMichael Reed 	return rc;
901ca2f938eSMichael Reed }
902ca2f938eSMichael Reed 
903ca2f938eSMichael Reed static void
904ca2f938eSMichael Reed mptfc_SetFcPortPage1_defaults(MPT_ADAPTER *ioc)
905ca2f938eSMichael Reed {
906ca2f938eSMichael Reed 	int		ii;
907ca2f938eSMichael Reed 	FCPortPage1_t	*pp1;
908ca2f938eSMichael Reed 
909ca2f938eSMichael Reed 	#define MPTFC_FW_DEVICE_TIMEOUT	(1)
910ca2f938eSMichael Reed 	#define MPTFC_FW_IO_PEND_TIMEOUT (1)
911ca2f938eSMichael Reed 	#define ON_FLAGS  (MPI_FCPORTPAGE1_FLAGS_IMMEDIATE_ERROR_REPLY)
912ca2f938eSMichael Reed 	#define OFF_FLAGS (MPI_FCPORTPAGE1_FLAGS_VERBOSE_RESCAN_EVENTS)
913ca2f938eSMichael Reed 
914ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
915ca2f938eSMichael Reed 		if (mptfc_GetFcPortPage1(ioc, ii) != 0)
916ca2f938eSMichael Reed 			continue;
917ca2f938eSMichael Reed 		pp1 = ioc->fc_data.fc_port_page1[ii].data;
918ca2f938eSMichael Reed 		if ((pp1->InitiatorDeviceTimeout == MPTFC_FW_DEVICE_TIMEOUT)
919ca2f938eSMichael Reed 		 && (pp1->InitiatorIoPendTimeout == MPTFC_FW_IO_PEND_TIMEOUT)
920ca2f938eSMichael Reed 		 && ((pp1->Flags & ON_FLAGS) == ON_FLAGS)
921ca2f938eSMichael Reed 		 && ((pp1->Flags & OFF_FLAGS) == 0))
922ca2f938eSMichael Reed 			continue;
923ca2f938eSMichael Reed 		pp1->InitiatorDeviceTimeout = MPTFC_FW_DEVICE_TIMEOUT;
924ca2f938eSMichael Reed 		pp1->InitiatorIoPendTimeout = MPTFC_FW_IO_PEND_TIMEOUT;
925ca2f938eSMichael Reed 		pp1->Flags &= ~OFF_FLAGS;
926ca2f938eSMichael Reed 		pp1->Flags |= ON_FLAGS;
927ca2f938eSMichael Reed 		mptfc_WriteFcPortPage1(ioc, ii);
928ca2f938eSMichael Reed 	}
929ca2f938eSMichael Reed }
930ca2f938eSMichael Reed 
931ca2f938eSMichael Reed 
93205e8ec17SMichael Reed static void
93305e8ec17SMichael Reed mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
93405e8ec17SMichael Reed {
9355d947f2bSMichael Reed 	unsigned	class = 0;
9365d947f2bSMichael Reed 	unsigned	cos = 0;
9375d947f2bSMichael Reed 	unsigned	speed;
9385d947f2bSMichael Reed 	unsigned	port_type;
9395d947f2bSMichael Reed 	unsigned	port_state;
9405d947f2bSMichael Reed 	FCPortPage0_t	*pp0;
9415d947f2bSMichael Reed 	struct Scsi_Host *sh;
9425d947f2bSMichael Reed 	char		*sn;
94305e8ec17SMichael Reed 
94405e8ec17SMichael Reed 	/* don't know what to do as only one scsi (fc) host was allocated */
94505e8ec17SMichael Reed 	if (portnum != 0)
94605e8ec17SMichael Reed 		return;
94705e8ec17SMichael Reed 
9485d947f2bSMichael Reed 	pp0 = &ioc->fc_port_page0[portnum];
9495d947f2bSMichael Reed 	sh = ioc->sh;
9505d947f2bSMichael Reed 
9515d947f2bSMichael Reed 	sn = fc_host_symbolic_name(sh);
9525d947f2bSMichael Reed 	snprintf(sn, FC_SYMBOLIC_NAME_SIZE, "%s %s%08xh",
9535d947f2bSMichael Reed 	    ioc->prod_name,
9545d947f2bSMichael Reed 	    MPT_FW_REV_MAGIC_ID_STRING,
9555d947f2bSMichael Reed 	    ioc->facts.FWVersion.Word);
9565d947f2bSMichael Reed 
9575d947f2bSMichael Reed 	fc_host_tgtid_bind_type(sh) = FC_TGTID_BIND_BY_WWPN;
9585d947f2bSMichael Reed 
9595d947f2bSMichael Reed 	fc_host_maxframe_size(sh) = pp0->MaxFrameSize;
9605d947f2bSMichael Reed 
9615d947f2bSMichael Reed 	fc_host_node_name(sh) =
9625d947f2bSMichael Reed 	    	(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
9635d947f2bSMichael Reed 
9645d947f2bSMichael Reed 	fc_host_port_name(sh) =
9655d947f2bSMichael Reed 	    	(u64)pp0->WWPN.High << 32 | (u64)pp0->WWPN.Low;
9665d947f2bSMichael Reed 
9675d947f2bSMichael Reed 	fc_host_port_id(sh) = pp0->PortIdentifier;
9685d947f2bSMichael Reed 
9695d947f2bSMichael Reed 	class = pp0->SupportedServiceClass;
97005e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
97105e8ec17SMichael Reed 		cos |= FC_COS_CLASS1;
97205e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
97305e8ec17SMichael Reed 		cos |= FC_COS_CLASS2;
97405e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
97505e8ec17SMichael Reed 		cos |= FC_COS_CLASS3;
9765d947f2bSMichael Reed 	fc_host_supported_classes(sh) = cos;
97705e8ec17SMichael Reed 
9785d947f2bSMichael Reed 	if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT)
9795d947f2bSMichael Reed 		speed = FC_PORTSPEED_1GBIT;
9805d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT)
9815d947f2bSMichael Reed 		speed = FC_PORTSPEED_2GBIT;
9825d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT)
9835d947f2bSMichael Reed 		speed = FC_PORTSPEED_4GBIT;
9845d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_10GBIT)
9855d947f2bSMichael Reed 		speed = FC_PORTSPEED_10GBIT;
9865d947f2bSMichael Reed 	else
9875d947f2bSMichael Reed 		speed = FC_PORTSPEED_UNKNOWN;
9885d947f2bSMichael Reed 	fc_host_speed(sh) = speed;
98905e8ec17SMichael Reed 
9905d947f2bSMichael Reed 	speed = 0;
9915d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_1GBIT_SPEED)
9925d947f2bSMichael Reed 		speed |= FC_PORTSPEED_1GBIT;
9935d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_2GBIT_SPEED)
9945d947f2bSMichael Reed 		speed |= FC_PORTSPEED_2GBIT;
9955d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_4GBIT_SPEED)
9965d947f2bSMichael Reed 		speed |= FC_PORTSPEED_4GBIT;
9975d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_10GBIT_SPEED)
9985d947f2bSMichael Reed 		speed |= FC_PORTSPEED_10GBIT;
9995d947f2bSMichael Reed 	fc_host_supported_speeds(sh) = speed;
100005e8ec17SMichael Reed 
10015d947f2bSMichael Reed 	port_state = FC_PORTSTATE_UNKNOWN;
10025d947f2bSMichael Reed 	if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE)
10035d947f2bSMichael Reed 		port_state = FC_PORTSTATE_ONLINE;
10045d947f2bSMichael Reed 	else if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_OFFLINE)
10055d947f2bSMichael Reed 		port_state = FC_PORTSTATE_LINKDOWN;
10065d947f2bSMichael Reed 	fc_host_port_state(sh) = port_state;
100705e8ec17SMichael Reed 
10085d947f2bSMichael Reed 	port_type = FC_PORTTYPE_UNKNOWN;
10095d947f2bSMichael Reed 	if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_POINT_TO_POINT)
10105d947f2bSMichael Reed 		port_type = FC_PORTTYPE_PTP;
10115d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PRIVATE_LOOP)
10125d947f2bSMichael Reed 		port_type = FC_PORTTYPE_LPORT;
10135d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PUBLIC_LOOP)
10145d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NLPORT;
10155d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_FABRIC_DIRECT)
10165d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NPORT;
10175d947f2bSMichael Reed 	fc_host_port_type(sh) = port_type;
101805e8ec17SMichael Reed 
10195d947f2bSMichael Reed 	fc_host_fabric_name(sh) =
10205d947f2bSMichael Reed 	    (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_FABRIC_WWN_VALID) ?
10215d947f2bSMichael Reed 		(u64) pp0->FabricWWNN.High << 32 | (u64) pp0->FabricWWPN.Low :
10225d947f2bSMichael Reed 		(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
10235d947f2bSMichael Reed 
102405e8ec17SMichael Reed }
102505e8ec17SMichael Reed 
102605e8ec17SMichael Reed static void
1027c4028958SDavid Howells mptfc_setup_reset(struct work_struct *work)
1028419835e2SMichael Reed {
1029c4028958SDavid Howells 	MPT_ADAPTER		*ioc =
1030c4028958SDavid Howells 		container_of(work, MPT_ADAPTER, fc_setup_reset_work);
1031419835e2SMichael Reed 	u64			pn;
1032419835e2SMichael Reed 	struct mptfc_rport_info *ri;
1033419835e2SMichael Reed 
1034419835e2SMichael Reed 	/* reset about to happen, delete (block) all rports */
1035419835e2SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
1036419835e2SMichael Reed 		if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
1037419835e2SMichael Reed 			ri->flags &= ~MPT_RPORT_INFO_FLAGS_REGISTERED;
1038419835e2SMichael Reed 			fc_remote_port_delete(ri->rport);	/* won't sleep */
1039419835e2SMichael Reed 			ri->rport = NULL;
1040419835e2SMichael Reed 
1041419835e2SMichael Reed 			pn = (u64)ri->pg0.WWPN.High << 32 |
1042419835e2SMichael Reed 			     (u64)ri->pg0.WWPN.Low;
1043419835e2SMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
1044419835e2SMichael Reed 				"mptfc_setup_reset.%d: %llx deleted\n",
1045419835e2SMichael Reed 				ioc->name,
1046419835e2SMichael Reed 				ioc->sh->host_no,
1047419835e2SMichael Reed 				(unsigned long long)pn));
1048419835e2SMichael Reed 		}
1049419835e2SMichael Reed 	}
1050419835e2SMichael Reed }
1051419835e2SMichael Reed 
1052419835e2SMichael Reed static void
1053c4028958SDavid Howells mptfc_rescan_devices(struct work_struct *work)
105405e8ec17SMichael Reed {
1055c4028958SDavid Howells 	MPT_ADAPTER		*ioc =
1056c4028958SDavid Howells 		container_of(work, MPT_ADAPTER, fc_rescan_work);
105705e8ec17SMichael Reed 	int			ii;
105865207fedSMoore, Eric 	u64			pn;
105905e8ec17SMichael Reed 	struct mptfc_rport_info *ri;
106005e8ec17SMichael Reed 
106105e8ec17SMichael Reed 	/* start by tagging all ports as missing */
106205e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
106305e8ec17SMichael Reed 		if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
106405e8ec17SMichael Reed 			ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
106505e8ec17SMichael Reed 		}
106605e8ec17SMichael Reed 	}
106705e8ec17SMichael Reed 
106805e8ec17SMichael Reed 	/*
106905e8ec17SMichael Reed 	 * now rescan devices known to adapter,
107005e8ec17SMichael Reed 	 * will reregister existing rports
107105e8ec17SMichael Reed 	 */
107205e8ec17SMichael Reed 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
107380d3ac77SMichael Reed 		(void) mptfc_GetFcPortPage0(ioc, ii);
107405e8ec17SMichael Reed 		mptfc_init_host_attr(ioc, ii);	/* refresh */
107505e8ec17SMichael Reed 		mptfc_GetFcDevPage0(ioc, ii, mptfc_register_dev);
107605e8ec17SMichael Reed 	}
107705e8ec17SMichael Reed 
107805e8ec17SMichael Reed 	/* delete devices still missing */
107905e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
108005e8ec17SMichael Reed 		/* if newly missing, delete it */
108165207fedSMoore, Eric 		if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
108205e8ec17SMichael Reed 
108305e8ec17SMichael Reed 			ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
108405e8ec17SMichael Reed 				       MPT_RPORT_INFO_FLAGS_MISSING);
108565207fedSMoore, Eric 			fc_remote_port_delete(ri->rport);	/* won't sleep */
10863bc7bf1dSMichael Reed 			ri->rport = NULL;
108765207fedSMoore, Eric 
108865207fedSMoore, Eric 			pn = (u64)ri->pg0.WWPN.High << 32 |
108965207fedSMoore, Eric 			     (u64)ri->pg0.WWPN.Low;
10903bc7bf1dSMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
10913bc7bf1dSMichael Reed 				"mptfc_rescan.%d: %llx deleted\n",
10923bc7bf1dSMichael Reed 				ioc->name,
10933bc7bf1dSMichael Reed 				ioc->sh->host_no,
109465207fedSMoore, Eric 				(unsigned long long)pn));
109505e8ec17SMichael Reed 		}
109605e8ec17SMichael Reed 	}
109705e8ec17SMichael Reed }
109805e8ec17SMichael Reed 
10992496af39SMoore, Eric Dean  static int
11002496af39SMoore, Eric Dean  mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
11012496af39SMoore, Eric Dean  {
11022496af39SMoore, Eric Dean  	struct Scsi_Host	*sh;
11032496af39SMoore, Eric Dean  	MPT_SCSI_HOST		*hd;
11042496af39SMoore, Eric Dean  	MPT_ADAPTER 		*ioc;
11052496af39SMoore, Eric Dean  	unsigned long		 flags;
11061ca00bb7SChristoph Hellwig 	int			 ii;
11072496af39SMoore, Eric Dean  	int			 numSGE = 0;
11082496af39SMoore, Eric Dean  	int			 scale;
11092496af39SMoore, Eric Dean  	int			 ioc_cap;
11102496af39SMoore, Eric Dean  	int			error=0;
11112496af39SMoore, Eric Dean  	int			r;
11122496af39SMoore, Eric Dean  
11132496af39SMoore, Eric Dean  	if ((r = mpt_attach(pdev,id)) != 0)
11142496af39SMoore, Eric Dean  		return r;
11152496af39SMoore, Eric Dean  
11162496af39SMoore, Eric Dean  	ioc = pci_get_drvdata(pdev);
1117d335cc38SMoore, Eric Dean  	ioc->DoneCtx = mptfcDoneCtx;
1118d335cc38SMoore, Eric Dean  	ioc->TaskCtx = mptfcTaskCtx;
1119d335cc38SMoore, Eric Dean  	ioc->InternalCtx = mptfcInternalCtx;
11202496af39SMoore, Eric Dean  
11212496af39SMoore, Eric Dean  	/*  Added sanity check on readiness of the MPT adapter.
11222496af39SMoore, Eric Dean  	 */
11232496af39SMoore, Eric Dean  	if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
11242496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
11252496af39SMoore, Eric Dean  		  "Skipping because it's not operational!\n",
11262496af39SMoore, Eric Dean  		  ioc->name);
11277acec1e7SMoore, Eric Dean 		error = -ENODEV;
11287acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11292496af39SMoore, Eric Dean  	}
11302496af39SMoore, Eric Dean  
11312496af39SMoore, Eric Dean  	if (!ioc->active) {
11322496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
11332496af39SMoore, Eric Dean  		  ioc->name);
11347acec1e7SMoore, Eric Dean 		error = -ENODEV;
11357acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11362496af39SMoore, Eric Dean  	}
11372496af39SMoore, Eric Dean  
11382496af39SMoore, Eric Dean  	/*  Sanity check - ensure at least 1 port is INITIATOR capable
11392496af39SMoore, Eric Dean  	 */
11402496af39SMoore, Eric Dean  	ioc_cap = 0;
11412496af39SMoore, Eric Dean  	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
11422496af39SMoore, Eric Dean  		if (ioc->pfacts[ii].ProtocolFlags &
11432496af39SMoore, Eric Dean  		    MPI_PORTFACTS_PROTOCOL_INITIATOR)
11442496af39SMoore, Eric Dean  			ioc_cap ++;
11452496af39SMoore, Eric Dean  	}
11462496af39SMoore, Eric Dean  
11472496af39SMoore, Eric Dean  	if (!ioc_cap) {
11482496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
11492496af39SMoore, Eric Dean  			"Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
11502496af39SMoore, Eric Dean  			ioc->name, ioc);
1151*793955f5SEric Moore 		return 0;
11522496af39SMoore, Eric Dean  	}
11532496af39SMoore, Eric Dean  
11542496af39SMoore, Eric Dean  	sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
11552496af39SMoore, Eric Dean  
11562496af39SMoore, Eric Dean  	if (!sh) {
11572496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
11582496af39SMoore, Eric Dean  			"Unable to register controller with SCSI subsystem\n",
11592496af39SMoore, Eric Dean  			ioc->name);
11607acec1e7SMoore, Eric Dean 		error = -1;
11617acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11622496af39SMoore, Eric Dean          }
11632496af39SMoore, Eric Dean  
116480d3ac77SMichael Reed 	spin_lock_init(&ioc->fc_rescan_work_lock);
1165c4028958SDavid Howells 	INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices);
1166c4028958SDavid Howells 	INIT_WORK(&ioc->fc_setup_reset_work, mptfc_setup_reset);
116705e8ec17SMichael Reed 
11682496af39SMoore, Eric Dean  	spin_lock_irqsave(&ioc->FreeQlock, flags);
11692496af39SMoore, Eric Dean  
11702496af39SMoore, Eric Dean  	/* Attach the SCSI Host to the IOC structure
11712496af39SMoore, Eric Dean  	 */
11722496af39SMoore, Eric Dean  	ioc->sh = sh;
11732496af39SMoore, Eric Dean  
11742496af39SMoore, Eric Dean  	sh->io_port = 0;
11752496af39SMoore, Eric Dean  	sh->n_io_port = 0;
11762496af39SMoore, Eric Dean  	sh->irq = 0;
11772496af39SMoore, Eric Dean  
11782496af39SMoore, Eric Dean  	/* set 16 byte cdb's */
11792496af39SMoore, Eric Dean  	sh->max_cmd_len = 16;
11802496af39SMoore, Eric Dean  
1181*793955f5SEric Moore 	sh->max_id = ioc->pfacts->MaxDevices;
1182*793955f5SEric Moore 	sh->max_lun = max_lun;
11832496af39SMoore, Eric Dean  
11842496af39SMoore, Eric Dean  	sh->this_id = ioc->pfacts[0].PortSCSIID;
11852496af39SMoore, Eric Dean  
11862496af39SMoore, Eric Dean  	/* Required entry.
11872496af39SMoore, Eric Dean  	 */
11882496af39SMoore, Eric Dean  	sh->unique_id = ioc->id;
11892496af39SMoore, Eric Dean  
11902496af39SMoore, Eric Dean  	/* Verify that we won't exceed the maximum
11912496af39SMoore, Eric Dean  	 * number of chain buffers
11922496af39SMoore, Eric Dean  	 * We can optimize:  ZZ = req_sz/sizeof(SGE)
11932496af39SMoore, Eric Dean  	 * For 32bit SGE's:
11942496af39SMoore, Eric Dean  	 *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
11952496af39SMoore, Eric Dean  	 *               + (req_sz - 64)/sizeof(SGE)
11962496af39SMoore, Eric Dean  	 * A slightly different algorithm is required for
11972496af39SMoore, Eric Dean  	 * 64bit SGEs.
11982496af39SMoore, Eric Dean  	 */
11992496af39SMoore, Eric Dean  	scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
12002496af39SMoore, Eric Dean  	if (sizeof(dma_addr_t) == sizeof(u64)) {
12012496af39SMoore, Eric Dean  		numSGE = (scale - 1) *
12022496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
12032496af39SMoore, Eric Dean  		  (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
12042496af39SMoore, Eric Dean  		  sizeof(u32));
12052496af39SMoore, Eric Dean  	} else {
12062496af39SMoore, Eric Dean  		numSGE = 1 + (scale - 1) *
12072496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
12082496af39SMoore, Eric Dean  		  (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
12092496af39SMoore, Eric Dean  		  sizeof(u32));
12102496af39SMoore, Eric Dean  	}
12112496af39SMoore, Eric Dean  
12122496af39SMoore, Eric Dean  	if (numSGE < sh->sg_tablesize) {
12132496af39SMoore, Eric Dean  		/* Reset this value */
12142496af39SMoore, Eric Dean  		dprintk((MYIOC_s_INFO_FMT
12152496af39SMoore, Eric Dean  		  "Resetting sg_tablesize to %d from %d\n",
12162496af39SMoore, Eric Dean  		  ioc->name, numSGE, sh->sg_tablesize));
12172496af39SMoore, Eric Dean  		sh->sg_tablesize = numSGE;
12182496af39SMoore, Eric Dean  	}
12192496af39SMoore, Eric Dean  
12202496af39SMoore, Eric Dean  	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
12212496af39SMoore, Eric Dean  
12222496af39SMoore, Eric Dean  	hd = (MPT_SCSI_HOST *) sh->hostdata;
12232496af39SMoore, Eric Dean  	hd->ioc = ioc;
12242496af39SMoore, Eric Dean  
12252496af39SMoore, Eric Dean  	/* SCSI needs scsi_cmnd lookup table!
12262496af39SMoore, Eric Dean  	 * (with size equal to req_depth*PtrSz!)
12272496af39SMoore, Eric Dean  	 */
12281ca00bb7SChristoph Hellwig 	hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
12291ca00bb7SChristoph Hellwig 	if (!hd->ScsiLookup) {
12302496af39SMoore, Eric Dean  		error = -ENOMEM;
12317acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12322496af39SMoore, Eric Dean  	}
12332496af39SMoore, Eric Dean  
12341ca00bb7SChristoph Hellwig 	dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
12351ca00bb7SChristoph Hellwig 		 ioc->name, hd->ScsiLookup));
12362496af39SMoore, Eric Dean  
12372496af39SMoore, Eric Dean  	/* Clear the TM flags
12382496af39SMoore, Eric Dean  	 */
12392496af39SMoore, Eric Dean  	hd->tmPending = 0;
12402496af39SMoore, Eric Dean  	hd->tmState = TM_STATE_NONE;
12412496af39SMoore, Eric Dean  	hd->resetPending = 0;
12422496af39SMoore, Eric Dean  	hd->abortSCpnt = NULL;
12432496af39SMoore, Eric Dean  
12442496af39SMoore, Eric Dean  	/* Clear the pointer used to store
12452496af39SMoore, Eric Dean  	 * single-threaded commands, i.e., those
12462496af39SMoore, Eric Dean  	 * issued during a bus scan, dv and
12472496af39SMoore, Eric Dean  	 * configuration pages.
12482496af39SMoore, Eric Dean  	 */
12492496af39SMoore, Eric Dean  	hd->cmdPtr = NULL;
12502496af39SMoore, Eric Dean  
12512496af39SMoore, Eric Dean  	/* Initialize this SCSI Hosts' timers
12522496af39SMoore, Eric Dean  	 * To use, set the timer expires field
12532496af39SMoore, Eric Dean  	 * and add_timer
12542496af39SMoore, Eric Dean  	 */
12552496af39SMoore, Eric Dean  	init_timer(&hd->timer);
12562496af39SMoore, Eric Dean  	hd->timer.data = (unsigned long) hd;
12572496af39SMoore, Eric Dean  	hd->timer.function = mptscsih_timer_expired;
12582496af39SMoore, Eric Dean  
12592496af39SMoore, Eric Dean  	init_waitqueue_head(&hd->scandv_waitq);
12602496af39SMoore, Eric Dean  	hd->scandv_wait_done = 0;
12612496af39SMoore, Eric Dean  	hd->last_queue_full = 0;
12622496af39SMoore, Eric Dean  
126305e8ec17SMichael Reed 	sh->transportt = mptfc_transport_template;
12642496af39SMoore, Eric Dean  	error = scsi_add_host (sh, &ioc->pcidev->dev);
12652496af39SMoore, Eric Dean  	if(error) {
12662496af39SMoore, Eric Dean  		dprintk((KERN_ERR MYNAM
12672496af39SMoore, Eric Dean  		  "scsi_add_host failed\n"));
12687acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12692496af39SMoore, Eric Dean  	}
12702496af39SMoore, Eric Dean  
127165207fedSMoore, Eric 	/* initialize workqueue */
127265207fedSMoore, Eric 
127365207fedSMoore, Eric 	snprintf(ioc->fc_rescan_work_q_name, KOBJ_NAME_LEN, "mptfc_wq_%d",
127465207fedSMoore, Eric 		sh->host_no);
127565207fedSMoore, Eric 	ioc->fc_rescan_work_q =
127665207fedSMoore, Eric 		create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
127765207fedSMoore, Eric 	if (!ioc->fc_rescan_work_q)
127865207fedSMoore, Eric 		goto out_mptfc_probe;
127965207fedSMoore, Eric 
128065207fedSMoore, Eric 	/*
128180d3ac77SMichael Reed 	 *  Pre-fetch FC port WWN and stuff...
128280d3ac77SMichael Reed 	 *  (FCPortPage0_t stuff)
128380d3ac77SMichael Reed 	 */
128480d3ac77SMichael Reed 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
128580d3ac77SMichael Reed 		(void) mptfc_GetFcPortPage0(ioc, ii);
128680d3ac77SMichael Reed 	}
1287ca2f938eSMichael Reed 	mptfc_SetFcPortPage1_defaults(ioc);
128880d3ac77SMichael Reed 
128980d3ac77SMichael Reed 	/*
129065207fedSMoore, Eric 	 * scan for rports -
129165207fedSMoore, Eric 	 *	by doing it via the workqueue, some locking is eliminated
129265207fedSMoore, Eric 	 */
129365207fedSMoore, Eric 
129465207fedSMoore, Eric 	queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
129565207fedSMoore, Eric 	flush_workqueue(ioc->fc_rescan_work_q);
129605e8ec17SMichael Reed 
12972496af39SMoore, Eric Dean  	return 0;
12982496af39SMoore, Eric Dean  
12997acec1e7SMoore, Eric Dean out_mptfc_probe:
13002496af39SMoore, Eric Dean  
13012496af39SMoore, Eric Dean  	mptscsih_remove(pdev);
13022496af39SMoore, Eric Dean  	return error;
13032496af39SMoore, Eric Dean  }
13042496af39SMoore, Eric Dean  
13052496af39SMoore, Eric Dean  static struct pci_driver mptfc_driver = {
13062496af39SMoore, Eric Dean  	.name		= "mptfc",
13072496af39SMoore, Eric Dean  	.id_table	= mptfc_pci_table,
13082496af39SMoore, Eric Dean  	.probe		= mptfc_probe,
130905e8ec17SMichael Reed 	.remove		= __devexit_p(mptfc_remove),
13102496af39SMoore, Eric Dean  	.shutdown	= mptscsih_shutdown,
13112496af39SMoore, Eric Dean  #ifdef CONFIG_PM
13122496af39SMoore, Eric Dean  	.suspend	= mptscsih_suspend,
13132496af39SMoore, Eric Dean  	.resume		= mptscsih_resume,
13142496af39SMoore, Eric Dean  #endif
13152496af39SMoore, Eric Dean  };
13162496af39SMoore, Eric Dean  
131780d3ac77SMichael Reed static int
131880d3ac77SMichael Reed mptfc_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
131980d3ac77SMichael Reed {
132080d3ac77SMichael Reed 	MPT_SCSI_HOST *hd;
132180d3ac77SMichael Reed 	u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
132280d3ac77SMichael Reed 	unsigned long flags;
132380d3ac77SMichael Reed 	int rc=1;
132480d3ac77SMichael Reed 
132580d3ac77SMichael Reed 	devtverboseprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
132680d3ac77SMichael Reed 			ioc->name, event));
132780d3ac77SMichael Reed 
132880d3ac77SMichael Reed 	if (ioc->sh == NULL ||
132980d3ac77SMichael Reed 		((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL))
133080d3ac77SMichael Reed 		return 1;
133180d3ac77SMichael Reed 
133280d3ac77SMichael Reed 	switch (event) {
133380d3ac77SMichael Reed 	case MPI_EVENT_RESCAN:
133480d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
133580d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
133680d3ac77SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
133780d3ac77SMichael Reed 				   &ioc->fc_rescan_work);
133880d3ac77SMichael Reed 		}
133980d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
134080d3ac77SMichael Reed 		break;
134180d3ac77SMichael Reed 	default:
134280d3ac77SMichael Reed 		rc = mptscsih_event_process(ioc,pEvReply);
134380d3ac77SMichael Reed 		break;
134480d3ac77SMichael Reed 	}
134580d3ac77SMichael Reed 	return rc;
134680d3ac77SMichael Reed }
134780d3ac77SMichael Reed 
134880d3ac77SMichael Reed static int
134980d3ac77SMichael Reed mptfc_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
135080d3ac77SMichael Reed {
135180d3ac77SMichael Reed 	int		rc;
135280d3ac77SMichael Reed 	unsigned long	flags;
135380d3ac77SMichael Reed 
135480d3ac77SMichael Reed 	rc = mptscsih_ioc_reset(ioc,reset_phase);
135580d3ac77SMichael Reed 	if (rc == 0)
135680d3ac77SMichael Reed 		return rc;
135780d3ac77SMichael Reed 
135880d3ac77SMichael Reed 
135980d3ac77SMichael Reed 	dtmprintk((KERN_WARNING MYNAM
136080d3ac77SMichael Reed 		": IOC %s_reset routed to FC host driver!\n",
136180d3ac77SMichael Reed 		reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
136280d3ac77SMichael Reed 		reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
136380d3ac77SMichael Reed 
136480d3ac77SMichael Reed 	if (reset_phase == MPT_IOC_SETUP_RESET) {
1365419835e2SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1366419835e2SMichael Reed 		if (ioc->fc_rescan_work_q) {
1367419835e2SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
1368419835e2SMichael Reed 				   &ioc->fc_setup_reset_work);
1369419835e2SMichael Reed 		}
1370419835e2SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
137180d3ac77SMichael Reed 	}
137280d3ac77SMichael Reed 
137380d3ac77SMichael Reed 	else if (reset_phase == MPT_IOC_PRE_RESET) {
137480d3ac77SMichael Reed 	}
137580d3ac77SMichael Reed 
137680d3ac77SMichael Reed 	else {	/* MPT_IOC_POST_RESET */
1377ca2f938eSMichael Reed 		mptfc_SetFcPortPage1_defaults(ioc);
137880d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
137980d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
138080d3ac77SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
138180d3ac77SMichael Reed 				   &ioc->fc_rescan_work);
138280d3ac77SMichael Reed 		}
138380d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
138480d3ac77SMichael Reed 	}
138580d3ac77SMichael Reed 	return 1;
138680d3ac77SMichael Reed }
138780d3ac77SMichael Reed 
13882496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
13892496af39SMoore, Eric Dean  /**
1390d9489fb6SRandy Dunlap  *	mptfc_init - Register MPT adapter(s) as SCSI host(s) with SCSI mid-layer.
13912496af39SMoore, Eric Dean   *
13922496af39SMoore, Eric Dean   *	Returns 0 for success, non-zero for failure.
13932496af39SMoore, Eric Dean   */
13942496af39SMoore, Eric Dean  static int __init
13952496af39SMoore, Eric Dean  mptfc_init(void)
13962496af39SMoore, Eric Dean  {
139705e8ec17SMichael Reed 	int error;
13982496af39SMoore, Eric Dean  
13992496af39SMoore, Eric Dean  	show_mptmod_ver(my_NAME, my_VERSION);
14002496af39SMoore, Eric Dean  
1401ca2f938eSMichael Reed 	/* sanity check module parameters */
1402ca2f938eSMichael Reed 	if (mptfc_dev_loss_tmo <= 0)
140305e8ec17SMichael Reed 		mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
140405e8ec17SMichael Reed 
140505e8ec17SMichael Reed 	mptfc_transport_template =
140605e8ec17SMichael Reed 		fc_attach_transport(&mptfc_transport_functions);
140705e8ec17SMichael Reed 
140805e8ec17SMichael Reed 	if (!mptfc_transport_template)
140905e8ec17SMichael Reed 		return -ENODEV;
141005e8ec17SMichael Reed 
14112496af39SMoore, Eric Dean  	mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
14122496af39SMoore, Eric Dean  	mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
14132496af39SMoore, Eric Dean  	mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
14142496af39SMoore, Eric Dean  
141580d3ac77SMichael Reed 	if (mpt_event_register(mptfcDoneCtx, mptfc_event_process) == 0) {
14163a892befSMoore, Eric 		devtverboseprintk((KERN_INFO MYNAM
14172496af39SMoore, Eric Dean  		  ": Registered for IOC event notifications\n"));
14182496af39SMoore, Eric Dean  	}
14192496af39SMoore, Eric Dean  
142080d3ac77SMichael Reed 	if (mpt_reset_register(mptfcDoneCtx, mptfc_ioc_reset) == 0) {
14212496af39SMoore, Eric Dean  		dprintk((KERN_INFO MYNAM
14222496af39SMoore, Eric Dean  		  ": Registered for IOC reset notifications\n"));
14232496af39SMoore, Eric Dean  	}
14242496af39SMoore, Eric Dean  
142505e8ec17SMichael Reed 	error = pci_register_driver(&mptfc_driver);
14263bc7bf1dSMichael Reed 	if (error)
142705e8ec17SMichael Reed 		fc_release_transport(mptfc_transport_template);
142805e8ec17SMichael Reed 
142905e8ec17SMichael Reed 	return error;
143005e8ec17SMichael Reed }
143105e8ec17SMichael Reed 
143205e8ec17SMichael Reed /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
143305e8ec17SMichael Reed /**
1434d9489fb6SRandy Dunlap  *	mptfc_remove - Remove fc infrastructure for devices
143505e8ec17SMichael Reed  *	@pdev: Pointer to pci_dev structure
143605e8ec17SMichael Reed  *
143705e8ec17SMichael Reed  */
14383bc7bf1dSMichael Reed static void __devexit
14393bc7bf1dSMichael Reed mptfc_remove(struct pci_dev *pdev)
144005e8ec17SMichael Reed {
144105e8ec17SMichael Reed 	MPT_ADAPTER		*ioc = pci_get_drvdata(pdev);
144205e8ec17SMichael Reed 	struct mptfc_rport_info	*p, *n;
144365207fedSMoore, Eric 	struct workqueue_struct *work_q;
144465207fedSMoore, Eric 	unsigned long		flags;
1445ca2f938eSMichael Reed 	int			ii;
144665207fedSMoore, Eric 
144765207fedSMoore, Eric 	/* destroy workqueue */
144865207fedSMoore, Eric 	if ((work_q=ioc->fc_rescan_work_q)) {
144965207fedSMoore, Eric 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
145065207fedSMoore, Eric 		ioc->fc_rescan_work_q = NULL;
145165207fedSMoore, Eric 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
145265207fedSMoore, Eric 		destroy_workqueue(work_q);
145365207fedSMoore, Eric 	}
145405e8ec17SMichael Reed 
145505e8ec17SMichael Reed 	fc_remove_host(ioc->sh);
145605e8ec17SMichael Reed 
145705e8ec17SMichael Reed 	list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
145805e8ec17SMichael Reed 		list_del(&p->list);
145905e8ec17SMichael Reed 		kfree(p);
146005e8ec17SMichael Reed 	}
146105e8ec17SMichael Reed 
1462ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
1463ca2f938eSMichael Reed 		if (ioc->fc_data.fc_port_page1[ii].data) {
1464ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev,
1465ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].pg_sz,
1466ca2f938eSMichael Reed 				(u8 *) ioc->fc_data.fc_port_page1[ii].data,
1467ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].dma);
1468ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[ii].data = NULL;
1469ca2f938eSMichael Reed 		}
1470ca2f938eSMichael Reed 	}
1471ca2f938eSMichael Reed 
147205e8ec17SMichael Reed 	mptscsih_remove(pdev);
14732496af39SMoore, Eric Dean  }
14742496af39SMoore, Eric Dean  
14752496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14762496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14772496af39SMoore, Eric Dean  /**
14782496af39SMoore, Eric Dean   *	mptfc_exit - Unregisters MPT adapter(s)
14792496af39SMoore, Eric Dean   *
14802496af39SMoore, Eric Dean   */
14812496af39SMoore, Eric Dean  static void __exit
14822496af39SMoore, Eric Dean  mptfc_exit(void)
14832496af39SMoore, Eric Dean  {
14842496af39SMoore, Eric Dean  	pci_unregister_driver(&mptfc_driver);
148505e8ec17SMichael Reed 	fc_release_transport(mptfc_transport_template);
14862496af39SMoore, Eric Dean  
14872496af39SMoore, Eric Dean  	mpt_reset_deregister(mptfcDoneCtx);
14882496af39SMoore, Eric Dean  	dprintk((KERN_INFO MYNAM
14892496af39SMoore, Eric Dean  	  ": Deregistered for IOC reset notifications\n"));
14902496af39SMoore, Eric Dean  
14912496af39SMoore, Eric Dean  	mpt_event_deregister(mptfcDoneCtx);
14922496af39SMoore, Eric Dean  	dprintk((KERN_INFO MYNAM
14932496af39SMoore, Eric Dean  	  ": Deregistered for IOC event notifications\n"));
14942496af39SMoore, Eric Dean  
14952496af39SMoore, Eric Dean  	mpt_deregister(mptfcInternalCtx);
14962496af39SMoore, Eric Dean  	mpt_deregister(mptfcTaskCtx);
14972496af39SMoore, Eric Dean  	mpt_deregister(mptfcDoneCtx);
14982496af39SMoore, Eric Dean  }
14992496af39SMoore, Eric Dean  
15002496af39SMoore, Eric Dean  module_init(mptfc_init);
15012496af39SMoore, Eric Dean  module_exit(mptfc_exit);
1502