xref: /openbmc/linux/drivers/message/fusion/mptfc.c (revision cfd2aff711aa9de301258d322a0b5a3c64010220)
12496af39SMoore, Eric Dean  /*
22496af39SMoore, Eric Dean   *  linux/drivers/message/fusion/mptfc.c
3f36789e2SPrakash, Sathya  *      For use with LSI PCI chip/adapter(s)
4f36789e2SPrakash, Sathya  *      running LSI Fusion MPT (Message Passing Technology) firmware.
52496af39SMoore, Eric Dean   *
6cddc0ab7SPrakash, Sathya  *  Copyright (c) 1999-2008 LSI Corporation
716d20101SEric Moore  *  (mailto:DL-MPTFusionLinux@lsi.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/module.h>
472496af39SMoore, Eric Dean  #include <linux/kernel.h>
482496af39SMoore, Eric Dean  #include <linux/init.h>
492496af39SMoore, Eric Dean  #include <linux/errno.h>
502496af39SMoore, Eric Dean  #include <linux/kdev_t.h>
512496af39SMoore, Eric Dean  #include <linux/blkdev.h>
522496af39SMoore, Eric Dean  #include <linux/delay.h>	/* for mdelay */
532496af39SMoore, Eric Dean  #include <linux/interrupt.h>	/* needed for in_interrupt() proto */
542496af39SMoore, Eric Dean  #include <linux/reboot.h>	/* notifier code */
552496af39SMoore, Eric Dean  #include <linux/workqueue.h>
5605e8ec17SMichael Reed #include <linux/sort.h>
575a0e3ad6STejun Heo #include <linux/slab.h>
582496af39SMoore, Eric Dean  
592496af39SMoore, Eric Dean  #include <scsi/scsi.h>
602496af39SMoore, Eric Dean  #include <scsi/scsi_cmnd.h>
612496af39SMoore, Eric Dean  #include <scsi/scsi_device.h>
622496af39SMoore, Eric Dean  #include <scsi/scsi_host.h>
632496af39SMoore, Eric Dean  #include <scsi/scsi_tcq.h>
6405e8ec17SMichael Reed #include <scsi/scsi_transport_fc.h>
652496af39SMoore, Eric Dean  
662496af39SMoore, Eric Dean  #include "mptbase.h"
672496af39SMoore, Eric Dean  #include "mptscsih.h"
682496af39SMoore, Eric Dean  
692496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
702496af39SMoore, Eric Dean  #define my_NAME		"Fusion MPT FC Host driver"
712496af39SMoore, Eric Dean  #define my_VERSION	MPT_LINUX_VERSION_COMMON
722496af39SMoore, Eric Dean  #define MYNAM		"mptfc"
732496af39SMoore, Eric Dean  
742496af39SMoore, Eric Dean  MODULE_AUTHOR(MODULEAUTHOR);
752496af39SMoore, Eric Dean  MODULE_DESCRIPTION(my_NAME);
762496af39SMoore, Eric Dean  MODULE_LICENSE("GPL");
779f4203b3SEric Moore MODULE_VERSION(my_VERSION);
782496af39SMoore, Eric Dean  
792496af39SMoore, Eric Dean  /* Command line args */
8005e8ec17SMichael Reed #define MPTFC_DEV_LOSS_TMO (60)
8105e8ec17SMichael Reed static int mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;	/* reasonable default */
8205e8ec17SMichael Reed module_param(mptfc_dev_loss_tmo, int, 0);
8305e8ec17SMichael Reed MODULE_PARM_DESC(mptfc_dev_loss_tmo, " Initial time the driver programs the "
8405e8ec17SMichael Reed     				     " transport to wait for an rport to "
8505e8ec17SMichael Reed 				     " return following a device loss event."
8605e8ec17SMichael Reed 				     "  Default=60.");
8705e8ec17SMichael Reed 
88793955f5SEric Moore /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
89793955f5SEric Moore #define MPTFC_MAX_LUN (16895)
90793955f5SEric Moore static int max_lun = MPTFC_MAX_LUN;
91793955f5SEric Moore module_param(max_lun, int, 0);
92793955f5SEric Moore MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
93793955f5SEric Moore 
94f606f571SPrakash, Sathya static u8	mptfcDoneCtx = MPT_MAX_PROTOCOL_DRIVERS;
95f606f571SPrakash, Sathya static u8	mptfcTaskCtx = MPT_MAX_PROTOCOL_DRIVERS;
96f606f571SPrakash, Sathya static u8	mptfcInternalCtx = MPT_MAX_PROTOCOL_DRIVERS;
972496af39SMoore, Eric Dean  
983bc7bf1dSMichael Reed static int mptfc_target_alloc(struct scsi_target *starget);
993bc7bf1dSMichael Reed static int mptfc_slave_alloc(struct scsi_device *sdev);
100f281233dSJeff Garzik static int mptfc_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *SCpnt);
1013bc7bf1dSMichael Reed static void mptfc_target_destroy(struct scsi_target *starget);
10205e8ec17SMichael Reed static void mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout);
10347b1ea75SGreg Kroah-Hartman static void mptfc_remove(struct pci_dev *pdev);
10435508e46SMichael Reed static int mptfc_abort(struct scsi_cmnd *SCpnt);
10535508e46SMichael Reed static int mptfc_dev_reset(struct scsi_cmnd *SCpnt);
10635508e46SMichael Reed static int mptfc_bus_reset(struct scsi_cmnd *SCpnt);
10735508e46SMichael Reed static int mptfc_host_reset(struct scsi_cmnd *SCpnt);
10805e8ec17SMichael Reed 
1092496af39SMoore, Eric Dean  static struct scsi_host_template mptfc_driver_template = {
110f78496daSMoore, Eric Dean 	.module				= THIS_MODULE,
1112496af39SMoore, Eric Dean  	.proc_name			= "mptfc",
112cac19703SAl Viro 	.show_info			= mptscsih_show_info,
1132496af39SMoore, Eric Dean  	.name				= "MPT FC Host",
1142496af39SMoore, Eric Dean  	.info				= mptscsih_info,
11505e8ec17SMichael Reed 	.queuecommand			= mptfc_qcmd,
1163bc7bf1dSMichael Reed 	.target_alloc			= mptfc_target_alloc,
11705e8ec17SMichael Reed 	.slave_alloc			= mptfc_slave_alloc,
1182496af39SMoore, Eric Dean  	.slave_configure		= mptscsih_slave_configure,
1193bc7bf1dSMichael Reed 	.target_destroy			= mptfc_target_destroy,
1202496af39SMoore, Eric Dean  	.slave_destroy			= mptscsih_slave_destroy,
1216e3815baSMoore, Eric Dean 	.change_queue_depth 		= mptscsih_change_queue_depth,
122b6a05c82SChristoph Hellwig 	.eh_timed_out			= fc_eh_timed_out,
12335508e46SMichael Reed 	.eh_abort_handler		= mptfc_abort,
12435508e46SMichael Reed 	.eh_device_reset_handler	= mptfc_dev_reset,
12535508e46SMichael Reed 	.eh_bus_reset_handler		= mptfc_bus_reset,
12635508e46SMichael Reed 	.eh_host_reset_handler		= mptfc_host_reset,
1272496af39SMoore, Eric Dean  	.bios_param			= mptscsih_bios_param,
1282496af39SMoore, Eric Dean  	.can_queue			= MPT_FC_CAN_QUEUE,
1292496af39SMoore, Eric Dean  	.this_id			= -1,
1302496af39SMoore, Eric Dean  	.sg_tablesize			= MPT_SCSI_SG_DEPTH,
1312496af39SMoore, Eric Dean  	.max_sectors			= 8192,
1322496af39SMoore, Eric Dean  	.cmd_per_lun			= 7,
1332496af39SMoore, Eric Dean  	.use_clustering			= ENABLE_CLUSTERING,
134edb9068dSPrakash, Sathya 	.shost_attrs			= mptscsih_host_attrs,
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 },
158ef1d8df7SPrakash, Sathya 	{ PCI_VENDOR_ID_BROCADE, MPI_MANUFACTPAGE_DEVICEID_FC949E,
159ef1d8df7SPrakash, Sathya 		PCI_ANY_ID, PCI_ANY_ID },
1602496af39SMoore, Eric Dean  	{0}	/* Terminating entry */
1612496af39SMoore, Eric Dean  };
1622496af39SMoore, Eric Dean  MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
1632496af39SMoore, Eric Dean  
16405e8ec17SMichael Reed static struct scsi_transport_template *mptfc_transport_template = NULL;
16505e8ec17SMichael Reed 
16603fbcbcdSAdrian Bunk static struct fc_function_template mptfc_transport_functions = {
16705e8ec17SMichael Reed 	.dd_fcrport_size = 8,
16805e8ec17SMichael Reed 	.show_host_node_name = 1,
16905e8ec17SMichael Reed 	.show_host_port_name = 1,
17005e8ec17SMichael Reed 	.show_host_supported_classes = 1,
17105e8ec17SMichael Reed 	.show_host_port_id = 1,
17205e8ec17SMichael Reed 	.show_rport_supported_classes = 1,
17305e8ec17SMichael Reed 	.show_starget_node_name = 1,
17405e8ec17SMichael Reed 	.show_starget_port_name = 1,
17505e8ec17SMichael Reed 	.show_starget_port_id = 1,
17605e8ec17SMichael Reed 	.set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
17705e8ec17SMichael Reed 	.show_rport_dev_loss_tmo = 1,
1785d947f2bSMichael Reed 	.show_host_supported_speeds = 1,
1795d947f2bSMichael Reed 	.show_host_maxframe_size = 1,
1805d947f2bSMichael Reed 	.show_host_speed = 1,
1815d947f2bSMichael Reed 	.show_host_fabric_name = 1,
1825d947f2bSMichael Reed 	.show_host_port_type = 1,
1835d947f2bSMichael Reed 	.show_host_port_state = 1,
1845d947f2bSMichael Reed 	.show_host_symbolic_name = 1,
18505e8ec17SMichael Reed };
18605e8ec17SMichael Reed 
18735508e46SMichael Reed static int
18835508e46SMichael Reed mptfc_block_error_handler(struct scsi_cmnd *SCpnt,
18935508e46SMichael Reed 			  int (*func)(struct scsi_cmnd *SCpnt),
19035508e46SMichael Reed 			  const char *caller)
19135508e46SMichael Reed {
192d6ecdd63SPrakash, Sathya 	MPT_SCSI_HOST		*hd;
19335508e46SMichael Reed 	struct scsi_device	*sdev = SCpnt->device;
19435508e46SMichael Reed 	struct Scsi_Host	*shost = sdev->host;
19535508e46SMichael Reed 	struct fc_rport		*rport = starget_to_rport(scsi_target(sdev));
19635508e46SMichael Reed 	unsigned long		flags;
19735508e46SMichael Reed 	int			ready;
198e80b002bSEric Moore 	MPT_ADAPTER 		*ioc;
19903cb3829SMichael Reed 	int			loops = 40;	/* seconds */
20035508e46SMichael Reed 
201e7eae9f6SEric Moore 	hd = shost_priv(SCpnt->device->host);
202e80b002bSEric Moore 	ioc = hd->ioc;
20335508e46SMichael Reed 	spin_lock_irqsave(shost->host_lock, flags);
20403cb3829SMichael Reed 	while ((ready = fc_remote_port_chkready(rport) >> 16) == DID_IMM_RETRY
20503cb3829SMichael Reed 	 || (loops > 0 && ioc->active == 0)) {
20635508e46SMichael Reed 		spin_unlock_irqrestore(shost->host_lock, flags);
207e80b002bSEric Moore 		dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
2089cb78c16SHannes Reinecke 			"mptfc_block_error_handler.%d: %d:%llu, port status is "
20903cb3829SMichael Reed 			"%x, active flag %d, deferring %s recovery.\n",
210e7eae9f6SEric Moore 			ioc->name, ioc->sh->host_no,
21103cb3829SMichael Reed 			SCpnt->device->id, SCpnt->device->lun,
21203cb3829SMichael Reed 			ready, ioc->active, caller));
21335508e46SMichael Reed 		msleep(1000);
21435508e46SMichael Reed 		spin_lock_irqsave(shost->host_lock, flags);
21503cb3829SMichael Reed 		loops --;
21635508e46SMichael Reed 	}
21735508e46SMichael Reed 	spin_unlock_irqrestore(shost->host_lock, flags);
21835508e46SMichael Reed 
21903cb3829SMichael Reed 	if (ready == DID_NO_CONNECT || !SCpnt->device->hostdata
22003cb3829SMichael Reed 	 || ioc->active == 0) {
221e80b002bSEric Moore 		dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
2229cb78c16SHannes Reinecke 			"%s.%d: %d:%llu, failing recovery, "
22303cb3829SMichael Reed 			"port state %x, active %d, vdevice %p.\n", caller,
224e7eae9f6SEric Moore 			ioc->name, ioc->sh->host_no,
22535508e46SMichael Reed 			SCpnt->device->id, SCpnt->device->lun, ready,
22603cb3829SMichael Reed 			ioc->active, SCpnt->device->hostdata));
22735508e46SMichael Reed 		return FAILED;
22835508e46SMichael Reed 	}
229e80b002bSEric Moore 	dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
2309cb78c16SHannes Reinecke 		"%s.%d: %d:%llu, executing recovery.\n", caller,
231e7eae9f6SEric Moore 		ioc->name, ioc->sh->host_no,
23235508e46SMichael Reed 		SCpnt->device->id, SCpnt->device->lun));
23335508e46SMichael Reed 	return (*func)(SCpnt);
23435508e46SMichael Reed }
23535508e46SMichael Reed 
23635508e46SMichael Reed static int
23735508e46SMichael Reed mptfc_abort(struct scsi_cmnd *SCpnt)
23835508e46SMichael Reed {
23935508e46SMichael Reed 	return
240cadbd4a5SHarvey Harrison 	    mptfc_block_error_handler(SCpnt, mptscsih_abort, __func__);
24135508e46SMichael Reed }
24235508e46SMichael Reed 
24335508e46SMichael Reed static int
24435508e46SMichael Reed mptfc_dev_reset(struct scsi_cmnd *SCpnt)
24535508e46SMichael Reed {
24635508e46SMichael Reed 	return
247cadbd4a5SHarvey Harrison 	    mptfc_block_error_handler(SCpnt, mptscsih_dev_reset, __func__);
24835508e46SMichael Reed }
24935508e46SMichael Reed 
25035508e46SMichael Reed static int
25135508e46SMichael Reed mptfc_bus_reset(struct scsi_cmnd *SCpnt)
25235508e46SMichael Reed {
25335508e46SMichael Reed 	return
254cadbd4a5SHarvey Harrison 	    mptfc_block_error_handler(SCpnt, mptscsih_bus_reset, __func__);
25535508e46SMichael Reed }
25635508e46SMichael Reed 
25735508e46SMichael Reed static int
25835508e46SMichael Reed mptfc_host_reset(struct scsi_cmnd *SCpnt)
25935508e46SMichael Reed {
26035508e46SMichael Reed 	return
261cadbd4a5SHarvey Harrison 	    mptfc_block_error_handler(SCpnt, mptscsih_host_reset, __func__);
26235508e46SMichael Reed }
26335508e46SMichael Reed 
26405e8ec17SMichael Reed static void
26505e8ec17SMichael Reed mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
26605e8ec17SMichael Reed {
26705e8ec17SMichael Reed 	if (timeout > 0)
26805e8ec17SMichael Reed 		rport->dev_loss_tmo = timeout;
26905e8ec17SMichael Reed 	else
27005e8ec17SMichael Reed 		rport->dev_loss_tmo = mptfc_dev_loss_tmo;
27105e8ec17SMichael Reed }
27205e8ec17SMichael Reed 
27305e8ec17SMichael Reed static int
27405e8ec17SMichael Reed mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
27505e8ec17SMichael Reed {
27605e8ec17SMichael Reed 	FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
27705e8ec17SMichael Reed 	FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
27805e8ec17SMichael Reed 
27905e8ec17SMichael Reed 	if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
28005e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
28105e8ec17SMichael Reed 			return 0;
28205e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
28305e8ec17SMichael Reed 			return -1;
28405e8ec17SMichael Reed 		return 1;
28505e8ec17SMichael Reed 	}
28605e8ec17SMichael Reed 	if ((*aa)->CurrentBus < (*bb)->CurrentBus)
28705e8ec17SMichael Reed 		return -1;
28805e8ec17SMichael Reed 	return 1;
28905e8ec17SMichael Reed }
29005e8ec17SMichael Reed 
29105e8ec17SMichael Reed static int
29205e8ec17SMichael Reed mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
29305e8ec17SMichael Reed 	void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
29405e8ec17SMichael Reed {
29505e8ec17SMichael Reed 	ConfigPageHeader_t	 hdr;
29605e8ec17SMichael Reed 	CONFIGPARMS		 cfg;
29705e8ec17SMichael Reed 	FCDevicePage0_t		*ppage0_alloc, *fc;
29805e8ec17SMichael Reed 	dma_addr_t		 page0_dma;
29905e8ec17SMichael Reed 	int			 data_sz;
30005e8ec17SMichael Reed 	int			 ii;
30105e8ec17SMichael Reed 
30205e8ec17SMichael Reed 	FCDevicePage0_t		*p0_array=NULL, *p_p0;
30305e8ec17SMichael Reed 	FCDevicePage0_t		**pp0_array=NULL, **p_pp0;
30405e8ec17SMichael Reed 
30505e8ec17SMichael Reed 	int			 rc = -ENOMEM;
30605e8ec17SMichael Reed 	U32			 port_id = 0xffffff;
30705e8ec17SMichael Reed 	int			 num_targ = 0;
30805e8ec17SMichael Reed 	int			 max_bus = ioc->facts.MaxBuses;
309793955f5SEric Moore 	int			 max_targ;
31005e8ec17SMichael Reed 
311793955f5SEric Moore 	max_targ = (ioc->facts.MaxDevices == 0) ? 256 : ioc->facts.MaxDevices;
31205e8ec17SMichael Reed 
31305e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
31405e8ec17SMichael Reed 	p_p0 = p0_array =  kzalloc(data_sz, GFP_KERNEL);
31505e8ec17SMichael Reed 	if (!p0_array)
31605e8ec17SMichael Reed 		goto out;
31705e8ec17SMichael Reed 
31805e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
31905e8ec17SMichael Reed 	p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
32005e8ec17SMichael Reed 	if (!pp0_array)
32105e8ec17SMichael Reed 		goto out;
32205e8ec17SMichael Reed 
32305e8ec17SMichael Reed 	do {
32405e8ec17SMichael Reed 		/* Get FC Device Page 0 header */
32505e8ec17SMichael Reed 		hdr.PageVersion = 0;
32605e8ec17SMichael Reed 		hdr.PageLength = 0;
32705e8ec17SMichael Reed 		hdr.PageNumber = 0;
32805e8ec17SMichael Reed 		hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
32905e8ec17SMichael Reed 		cfg.cfghdr.hdr = &hdr;
33005e8ec17SMichael Reed 		cfg.physAddr = -1;
33105e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
33205e8ec17SMichael Reed 		cfg.dir = 0;
33305e8ec17SMichael Reed 		cfg.pageAddr = port_id;
33405e8ec17SMichael Reed 		cfg.timeout = 0;
33505e8ec17SMichael Reed 
33605e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) != 0)
33705e8ec17SMichael Reed 			break;
33805e8ec17SMichael Reed 
33905e8ec17SMichael Reed 		if (hdr.PageLength <= 0)
34005e8ec17SMichael Reed 			break;
34105e8ec17SMichael Reed 
34205e8ec17SMichael Reed 		data_sz = hdr.PageLength * 4;
34305e8ec17SMichael Reed 		ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
34405e8ec17SMichael Reed 		    					&page0_dma);
34505e8ec17SMichael Reed 		rc = -ENOMEM;
34605e8ec17SMichael Reed 		if (!ppage0_alloc)
34705e8ec17SMichael Reed 			break;
34805e8ec17SMichael Reed 
34905e8ec17SMichael Reed 		cfg.physAddr = page0_dma;
35005e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
35105e8ec17SMichael Reed 
35205e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
35305e8ec17SMichael Reed 			ppage0_alloc->PortIdentifier =
35405e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->PortIdentifier);
35505e8ec17SMichael Reed 
35605e8ec17SMichael Reed 			ppage0_alloc->WWNN.Low =
35705e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.Low);
35805e8ec17SMichael Reed 
35905e8ec17SMichael Reed 			ppage0_alloc->WWNN.High =
36005e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.High);
36105e8ec17SMichael Reed 
36205e8ec17SMichael Reed 			ppage0_alloc->WWPN.Low =
36305e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.Low);
36405e8ec17SMichael Reed 
36505e8ec17SMichael Reed 			ppage0_alloc->WWPN.High =
36605e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.High);
36705e8ec17SMichael Reed 
36805e8ec17SMichael Reed 			ppage0_alloc->BBCredit =
36905e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->BBCredit);
37005e8ec17SMichael Reed 
37105e8ec17SMichael Reed 			ppage0_alloc->MaxRxFrameSize =
37205e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
37305e8ec17SMichael Reed 
37405e8ec17SMichael Reed 			port_id = ppage0_alloc->PortIdentifier;
37505e8ec17SMichael Reed 			num_targ++;
37605e8ec17SMichael Reed 			*p_p0 = *ppage0_alloc;	/* save data */
37705e8ec17SMichael Reed 			*p_pp0++ = p_p0++;	/* save addr */
37805e8ec17SMichael Reed 		}
37905e8ec17SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz,
38005e8ec17SMichael Reed 		    			(u8 *) ppage0_alloc, page0_dma);
38105e8ec17SMichael Reed 		if (rc != 0)
38205e8ec17SMichael Reed 			break;
38305e8ec17SMichael Reed 
38405e8ec17SMichael Reed 	} while (port_id <= 0xff0000);
38505e8ec17SMichael Reed 
38605e8ec17SMichael Reed 	if (num_targ) {
38705e8ec17SMichael Reed 		/* sort array */
38805e8ec17SMichael Reed 		if (num_targ > 1)
38905e8ec17SMichael Reed 			sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
39005e8ec17SMichael Reed 				mptfc_FcDevPage0_cmp_func, NULL);
39105e8ec17SMichael Reed 		/* call caller's func for each targ */
39205e8ec17SMichael Reed 		for (ii = 0; ii < num_targ;  ii++) {
39305e8ec17SMichael Reed 			fc = *(pp0_array+ii);
39405e8ec17SMichael Reed 			func(ioc, ioc_port, fc);
39505e8ec17SMichael Reed 		}
39605e8ec17SMichael Reed 	}
39705e8ec17SMichael Reed 
39805e8ec17SMichael Reed  out:
39905e8ec17SMichael Reed 	kfree(pp0_array);
40005e8ec17SMichael Reed 	kfree(p0_array);
40105e8ec17SMichael Reed 	return rc;
40205e8ec17SMichael Reed }
40305e8ec17SMichael Reed 
40405e8ec17SMichael Reed static int
40505e8ec17SMichael Reed mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
40605e8ec17SMichael Reed {
40705e8ec17SMichael Reed 	/* not currently usable */
40805e8ec17SMichael Reed 	if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
40905e8ec17SMichael Reed 			  MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
41005e8ec17SMichael Reed 		return -1;
41105e8ec17SMichael Reed 
41205e8ec17SMichael Reed 	if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
41305e8ec17SMichael Reed 		return -1;
41405e8ec17SMichael Reed 
41505e8ec17SMichael Reed 	if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
41605e8ec17SMichael Reed 		return -1;
41705e8ec17SMichael Reed 
41805e8ec17SMichael Reed 	/*
41905e8ec17SMichael Reed 	 * board data structure already normalized to platform endianness
42005e8ec17SMichael Reed 	 * shifted to avoid unaligned access on 64 bit architecture
42105e8ec17SMichael Reed 	 */
42205e8ec17SMichael Reed 	rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
42305e8ec17SMichael Reed 	rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
42405e8ec17SMichael Reed 	rid->port_id =   pg0->PortIdentifier;
42505e8ec17SMichael Reed 	rid->roles = FC_RPORT_ROLE_UNKNOWN;
42605e8ec17SMichael Reed 
42705e8ec17SMichael Reed 	return 0;
42805e8ec17SMichael Reed }
42905e8ec17SMichael Reed 
43005e8ec17SMichael Reed static void
43105e8ec17SMichael Reed mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
43205e8ec17SMichael Reed {
43305e8ec17SMichael Reed 	struct fc_rport_identifiers rport_ids;
43405e8ec17SMichael Reed 	struct fc_rport		*rport;
43505e8ec17SMichael Reed 	struct mptfc_rport_info	*ri;
4363bc7bf1dSMichael Reed 	int			new_ri = 1;
43765207fedSMoore, Eric 	u64			pn, nn;
4383bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
4396dd727daSmdr@sgi.com 	u32			roles = FC_RPORT_ROLE_UNKNOWN;
44005e8ec17SMichael Reed 
44105e8ec17SMichael Reed 	if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
44205e8ec17SMichael Reed 		return;
44305e8ec17SMichael Reed 
4446dd727daSmdr@sgi.com 	roles |= FC_RPORT_ROLE_FCP_TARGET;
4456dd727daSmdr@sgi.com 	if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
4466dd727daSmdr@sgi.com 		roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4476dd727daSmdr@sgi.com 
44805e8ec17SMichael Reed 	/* scan list looking for a match */
44905e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
4503bc7bf1dSMichael Reed 		pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
4513bc7bf1dSMichael Reed 		if (pn == rport_ids.port_name) {	/* match */
45205e8ec17SMichael Reed 			list_move_tail(&ri->list, &ioc->fc_rports);
4533bc7bf1dSMichael Reed 			new_ri = 0;
45405e8ec17SMichael Reed 			break;
45505e8ec17SMichael Reed 		}
45605e8ec17SMichael Reed 	}
4573bc7bf1dSMichael Reed 	if (new_ri) {	/* allocate one */
45805e8ec17SMichael Reed 		ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
45905e8ec17SMichael Reed 		if (!ri)
46005e8ec17SMichael Reed 			return;
46105e8ec17SMichael Reed 		list_add_tail(&ri->list, &ioc->fc_rports);
46205e8ec17SMichael Reed 	}
46305e8ec17SMichael Reed 
46405e8ec17SMichael Reed 	ri->pg0 = *pg0;	/* add/update pg0 data */
46505e8ec17SMichael Reed 	ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
46605e8ec17SMichael Reed 
4673bc7bf1dSMichael Reed 	/* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
46805e8ec17SMichael Reed 	if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
46905e8ec17SMichael Reed 		ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
47005e8ec17SMichael Reed 		rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
47105e8ec17SMichael Reed 		if (rport) {
47205e8ec17SMichael Reed 			ri->rport = rport;
4733bc7bf1dSMichael Reed 			if (new_ri) /* may have been reset by user */
47405e8ec17SMichael Reed 				rport->dev_loss_tmo = mptfc_dev_loss_tmo;
47505e8ec17SMichael Reed 			/*
47605e8ec17SMichael Reed 			 * if already mapped, remap here.  If not mapped,
4773bc7bf1dSMichael Reed 			 * target_alloc will allocate vtarget and map,
478a69de507SEric Moore 			 * slave_alloc will fill in vdevice from vtarget.
47905e8ec17SMichael Reed 			 */
4803bc7bf1dSMichael Reed 			if (ri->starget) {
4813bc7bf1dSMichael Reed 				vtarget = ri->starget->hostdata;
4823bc7bf1dSMichael Reed 				if (vtarget) {
483793955f5SEric Moore 					vtarget->id = pg0->CurrentTargetID;
484793955f5SEric Moore 					vtarget->channel = pg0->CurrentBus;
48508f5c5c2SKashyap, Desai 					vtarget->deleted = 0;
48605e8ec17SMichael Reed 				}
4873bc7bf1dSMichael Reed 			}
48865207fedSMoore, Eric 			*((struct mptfc_rport_info **)rport->dd_data) = ri;
4896dd727daSmdr@sgi.com 			/* scan will be scheduled once rport becomes a target */
4906dd727daSmdr@sgi.com 			fc_remote_port_rolechg(rport,roles);
49165207fedSMoore, Eric 
49265207fedSMoore, Eric 			pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
49365207fedSMoore, Eric 			nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
494d6ecdd63SPrakash, Sathya 			dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
4953bc7bf1dSMichael Reed 				"mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
49605e8ec17SMichael Reed 				"rport tid %d, tmo %d\n",
4973bc7bf1dSMichael Reed 					ioc->name,
498914c2d8eSMoore, Eric 					ioc->sh->host_no,
49905e8ec17SMichael Reed 					pg0->PortIdentifier,
50065207fedSMoore, Eric 					(unsigned long long)nn,
50165207fedSMoore, Eric 					(unsigned long long)pn,
50205e8ec17SMichael Reed 					pg0->CurrentTargetID,
50305e8ec17SMichael Reed 					ri->rport->scsi_target_id,
5043bc7bf1dSMichael Reed 					ri->rport->dev_loss_tmo));
50505e8ec17SMichael Reed 		} else {
50605e8ec17SMichael Reed 			list_del(&ri->list);
50705e8ec17SMichael Reed 			kfree(ri);
50805e8ec17SMichael Reed 			ri = NULL;
50905e8ec17SMichael Reed 		}
51005e8ec17SMichael Reed 	}
51105e8ec17SMichael Reed }
51205e8ec17SMichael Reed 
51305e8ec17SMichael Reed /*
5143bc7bf1dSMichael Reed  *	OS entry point to allow for host driver to free allocated memory
5153bc7bf1dSMichael Reed  *	Called if no device present or device being unloaded
5163bc7bf1dSMichael Reed  */
5173bc7bf1dSMichael Reed static void
5183bc7bf1dSMichael Reed mptfc_target_destroy(struct scsi_target *starget)
5193bc7bf1dSMichael Reed {
5203bc7bf1dSMichael Reed 	struct fc_rport		*rport;
5213bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
5223bc7bf1dSMichael Reed 
5233bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
5243bc7bf1dSMichael Reed 	if (rport) {
5253bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
5263bc7bf1dSMichael Reed 		if (ri)	/* better be! */
5273bc7bf1dSMichael Reed 			ri->starget = NULL;
5283bc7bf1dSMichael Reed 	}
5293bc7bf1dSMichael Reed 	kfree(starget->hostdata);
5303bc7bf1dSMichael Reed 	starget->hostdata = NULL;
5313bc7bf1dSMichael Reed }
5323bc7bf1dSMichael Reed 
5333bc7bf1dSMichael Reed /*
5343bc7bf1dSMichael Reed  *	OS entry point to allow host driver to alloc memory
5353bc7bf1dSMichael Reed  *	for each scsi target. Called once per device the bus scan.
5363bc7bf1dSMichael Reed  *	Return non-zero if allocation fails.
5373bc7bf1dSMichael Reed  */
5383bc7bf1dSMichael Reed static int
5393bc7bf1dSMichael Reed mptfc_target_alloc(struct scsi_target *starget)
5403bc7bf1dSMichael Reed {
5413bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
5423bc7bf1dSMichael Reed 	struct fc_rport		*rport;
5433bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
5443bc7bf1dSMichael Reed 	int			rc;
5453bc7bf1dSMichael Reed 
5463bc7bf1dSMichael Reed 	vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
5473bc7bf1dSMichael Reed 	if (!vtarget)
5483bc7bf1dSMichael Reed 		return -ENOMEM;
5493bc7bf1dSMichael Reed 	starget->hostdata = vtarget;
5503bc7bf1dSMichael Reed 
5513bc7bf1dSMichael Reed 	rc = -ENODEV;
5523bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
5533bc7bf1dSMichael Reed 	if (rport) {
5543bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
5553bc7bf1dSMichael Reed 		if (ri) {	/* better be! */
556793955f5SEric Moore 			vtarget->id = ri->pg0.CurrentTargetID;
557793955f5SEric Moore 			vtarget->channel = ri->pg0.CurrentBus;
5583bc7bf1dSMichael Reed 			ri->starget = starget;
5593bc7bf1dSMichael Reed 			rc = 0;
5603bc7bf1dSMichael Reed 		}
5613bc7bf1dSMichael Reed 	}
5623bc7bf1dSMichael Reed 	if (rc != 0) {
5633bc7bf1dSMichael Reed 		kfree(vtarget);
5643bc7bf1dSMichael Reed 		starget->hostdata = NULL;
5653bc7bf1dSMichael Reed 	}
5663bc7bf1dSMichael Reed 
5673bc7bf1dSMichael Reed 	return rc;
5683bc7bf1dSMichael Reed }
569d6ecdd63SPrakash, Sathya /*
570d6ecdd63SPrakash, Sathya  *	mptfc_dump_lun_info
571d6ecdd63SPrakash, Sathya  *	@ioc
572d6ecdd63SPrakash, Sathya  *	@rport
573d6ecdd63SPrakash, Sathya  *	@sdev
574d6ecdd63SPrakash, Sathya  *
575d6ecdd63SPrakash, Sathya  */
576d6ecdd63SPrakash, Sathya static void
577d6ecdd63SPrakash, Sathya mptfc_dump_lun_info(MPT_ADAPTER *ioc, struct fc_rport *rport, struct scsi_device *sdev,
578d6ecdd63SPrakash, Sathya 		VirtTarget *vtarget)
579d6ecdd63SPrakash, Sathya {
580d6ecdd63SPrakash, Sathya 	u64 nn, pn;
581d6ecdd63SPrakash, Sathya 	struct mptfc_rport_info *ri;
582d6ecdd63SPrakash, Sathya 
583d6ecdd63SPrakash, Sathya 	ri = *((struct mptfc_rport_info **)rport->dd_data);
584d6ecdd63SPrakash, Sathya 	pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
585d6ecdd63SPrakash, Sathya 	nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
586d6ecdd63SPrakash, Sathya 	dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
587d6ecdd63SPrakash, Sathya 		"mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
588d6ecdd63SPrakash, Sathya 		"CurrentTargetID %d, %x %llx %llx\n",
589d6ecdd63SPrakash, Sathya 		ioc->name,
590d6ecdd63SPrakash, Sathya 		sdev->host->host_no,
591d6ecdd63SPrakash, Sathya 		vtarget->num_luns,
592d6ecdd63SPrakash, Sathya 		sdev->id, ri->pg0.CurrentTargetID,
593d6ecdd63SPrakash, Sathya 		ri->pg0.PortIdentifier,
594d6ecdd63SPrakash, Sathya 		(unsigned long long)pn,
595d6ecdd63SPrakash, Sathya 		(unsigned long long)nn));
596d6ecdd63SPrakash, Sathya }
597d6ecdd63SPrakash, Sathya 
5983bc7bf1dSMichael Reed 
5993bc7bf1dSMichael Reed /*
60005e8ec17SMichael Reed  *	OS entry point to allow host driver to alloc memory
60105e8ec17SMichael Reed  *	for each scsi device. Called once per device the bus scan.
60205e8ec17SMichael Reed  *	Return non-zero if allocation fails.
60305e8ec17SMichael Reed  *	Init memory once per LUN.
60405e8ec17SMichael Reed  */
60503fbcbcdSAdrian Bunk static int
60605e8ec17SMichael Reed mptfc_slave_alloc(struct scsi_device *sdev)
60705e8ec17SMichael Reed {
60805e8ec17SMichael Reed 	MPT_SCSI_HOST		*hd;
60905e8ec17SMichael Reed 	VirtTarget		*vtarget;
610a69de507SEric Moore 	VirtDevice		*vdevice;
61105e8ec17SMichael Reed 	struct scsi_target	*starget;
61205e8ec17SMichael Reed 	struct fc_rport		*rport;
613e80b002bSEric Moore 	MPT_ADAPTER 		*ioc;
61405e8ec17SMichael Reed 
61565207fedSMoore, Eric 	starget = scsi_target(sdev);
61665207fedSMoore, Eric 	rport = starget_to_rport(starget);
61705e8ec17SMichael Reed 
61805e8ec17SMichael Reed 	if (!rport || fc_remote_port_chkready(rport))
61905e8ec17SMichael Reed 		return -ENXIO;
62005e8ec17SMichael Reed 
621e7eae9f6SEric Moore 	hd = shost_priv(sdev->host);
622e80b002bSEric Moore 	ioc = hd->ioc;
62305e8ec17SMichael Reed 
624a69de507SEric Moore 	vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
625a69de507SEric Moore 	if (!vdevice) {
62605e8ec17SMichael Reed 		printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
627e80b002bSEric Moore 				ioc->name, sizeof(VirtDevice));
62805e8ec17SMichael Reed 		return -ENOMEM;
62905e8ec17SMichael Reed 	}
63005e8ec17SMichael Reed 
63105e8ec17SMichael Reed 
632a69de507SEric Moore 	sdev->hostdata = vdevice;
63305e8ec17SMichael Reed 	vtarget = starget->hostdata;
6343bc7bf1dSMichael Reed 
63505e8ec17SMichael Reed 	if (vtarget->num_luns == 0) {
636e80b002bSEric Moore 		vtarget->ioc_id = ioc->id;
637ba856d32SEric Moore 		vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
63805e8ec17SMichael Reed 	}
63905e8ec17SMichael Reed 
640a69de507SEric Moore 	vdevice->vtarget = vtarget;
641a69de507SEric Moore 	vdevice->lun = sdev->lun;
64205e8ec17SMichael Reed 
64305e8ec17SMichael Reed 	vtarget->num_luns++;
64405e8ec17SMichael Reed 
64565207fedSMoore, Eric 
646e80b002bSEric Moore 	mptfc_dump_lun_info(ioc, rport, sdev, vtarget);
64705e8ec17SMichael Reed 
64805e8ec17SMichael Reed 	return 0;
64905e8ec17SMichael Reed }
65005e8ec17SMichael Reed 
65105e8ec17SMichael Reed static int
652a48ac9e5SMatthew Wilcox mptfc_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *SCpnt)
65305e8ec17SMichael Reed {
6543bc7bf1dSMichael Reed 	struct mptfc_rport_info	*ri;
65505e8ec17SMichael Reed 	struct fc_rport	*rport = starget_to_rport(scsi_target(SCpnt->device));
65605e8ec17SMichael Reed 	int		err;
657a69de507SEric Moore 	VirtDevice	*vdevice = SCpnt->device->hostdata;
65805e8ec17SMichael Reed 
659a69de507SEric Moore 	if (!vdevice || !vdevice->vtarget) {
660793955f5SEric Moore 		SCpnt->result = DID_NO_CONNECT << 16;
661a48ac9e5SMatthew Wilcox 		SCpnt->scsi_done(SCpnt);
66205e8ec17SMichael Reed 		return 0;
66305e8ec17SMichael Reed 	}
6643bc7bf1dSMichael Reed 
665793955f5SEric Moore 	err = fc_remote_port_chkready(rport);
666793955f5SEric Moore 	if (unlikely(err)) {
667793955f5SEric Moore 		SCpnt->result = err;
668a48ac9e5SMatthew Wilcox 		SCpnt->scsi_done(SCpnt);
66935508e46SMichael Reed 		return 0;
67035508e46SMichael Reed 	}
67135508e46SMichael Reed 
67265207fedSMoore, Eric 	/* dd_data is null until finished adding target */
67365207fedSMoore, Eric 	ri = *((struct mptfc_rport_info **)rport->dd_data);
67465207fedSMoore, Eric 	if (unlikely(!ri)) {
67565207fedSMoore, Eric 		SCpnt->result = DID_IMM_RETRY << 16;
676a48ac9e5SMatthew Wilcox 		SCpnt->scsi_done(SCpnt);
67765207fedSMoore, Eric 		return 0;
67865207fedSMoore, Eric 	}
67965207fedSMoore, Eric 
680a48ac9e5SMatthew Wilcox 	return mptscsih_qcmd(SCpnt);
68105e8ec17SMichael Reed }
68205e8ec17SMichael Reed 
68380d3ac77SMichael Reed /*
684eb5329f4SPrakash, Sathya  *	mptfc_display_port_link_speed - displaying link speed
685eb5329f4SPrakash, Sathya  *	@ioc: Pointer to MPT_ADAPTER structure
686eb5329f4SPrakash, Sathya  *	@portnum: IOC Port number
687eb5329f4SPrakash, Sathya  *	@pp0dest: port page0 data payload
688eb5329f4SPrakash, Sathya  *
689eb5329f4SPrakash, Sathya  */
690eb5329f4SPrakash, Sathya static void
691eb5329f4SPrakash, Sathya mptfc_display_port_link_speed(MPT_ADAPTER *ioc, int portnum, FCPortPage0_t *pp0dest)
692eb5329f4SPrakash, Sathya {
693eb5329f4SPrakash, Sathya 	u8	old_speed, new_speed, state;
694eb5329f4SPrakash, Sathya 	char	*old, *new;
695eb5329f4SPrakash, Sathya 
696eb5329f4SPrakash, Sathya 	if (portnum >= 2)
697eb5329f4SPrakash, Sathya 		return;
698eb5329f4SPrakash, Sathya 
699eb5329f4SPrakash, Sathya 	old_speed = ioc->fc_link_speed[portnum];
700eb5329f4SPrakash, Sathya 	new_speed = pp0dest->CurrentSpeed;
701eb5329f4SPrakash, Sathya 	state = pp0dest->PortState;
702eb5329f4SPrakash, Sathya 
703eb5329f4SPrakash, Sathya 	if (state != MPI_FCPORTPAGE0_PORTSTATE_OFFLINE &&
704eb5329f4SPrakash, Sathya 	    new_speed != MPI_FCPORTPAGE0_CURRENT_SPEED_UKNOWN) {
705eb5329f4SPrakash, Sathya 
706eb5329f4SPrakash, Sathya 		old = old_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT ? "1 Gbps" :
707eb5329f4SPrakash, Sathya 		       old_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT ? "2 Gbps" :
708eb5329f4SPrakash, Sathya 			old_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT ? "4 Gbps" :
709eb5329f4SPrakash, Sathya 			 "Unknown";
710eb5329f4SPrakash, Sathya 		new = new_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT ? "1 Gbps" :
711eb5329f4SPrakash, Sathya 		       new_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT ? "2 Gbps" :
712eb5329f4SPrakash, Sathya 			new_speed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT ? "4 Gbps" :
713eb5329f4SPrakash, Sathya 			 "Unknown";
714eb5329f4SPrakash, Sathya 		if (old_speed == 0)
715eb5329f4SPrakash, Sathya 			printk(MYIOC_s_NOTE_FMT
716eb5329f4SPrakash, Sathya 				"FC Link Established, Speed = %s\n",
717eb5329f4SPrakash, Sathya 				ioc->name, new);
718eb5329f4SPrakash, Sathya 		else if (old_speed != new_speed)
719eb5329f4SPrakash, Sathya 			printk(MYIOC_s_WARN_FMT
720eb5329f4SPrakash, Sathya 				"FC Link Speed Change, Old Speed = %s, New Speed = %s\n",
721eb5329f4SPrakash, Sathya 				ioc->name, old, new);
722eb5329f4SPrakash, Sathya 
723eb5329f4SPrakash, Sathya 		ioc->fc_link_speed[portnum] = new_speed;
724eb5329f4SPrakash, Sathya 	}
725eb5329f4SPrakash, Sathya }
726eb5329f4SPrakash, Sathya 
727eb5329f4SPrakash, Sathya /*
72880d3ac77SMichael Reed  *	mptfc_GetFcPortPage0 - Fetch FCPort config Page0.
72980d3ac77SMichael Reed  *	@ioc: Pointer to MPT_ADAPTER structure
73080d3ac77SMichael Reed  *	@portnum: IOC Port number
73180d3ac77SMichael Reed  *
73280d3ac77SMichael Reed  *	Return: 0 for success
73380d3ac77SMichael Reed  *	-ENOMEM if no memory available
73480d3ac77SMichael Reed  *		-EPERM if not allowed due to ISR context
73580d3ac77SMichael Reed  *		-EAGAIN if no msg frames currently available
73680d3ac77SMichael Reed  *		-EFAULT for non-successful reply or no reply (timeout)
73780d3ac77SMichael Reed  *		-EINVAL portnum arg out of range (hardwired to two elements)
73880d3ac77SMichael Reed  */
73980d3ac77SMichael Reed static int
74080d3ac77SMichael Reed mptfc_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
74180d3ac77SMichael Reed {
74280d3ac77SMichael Reed 	ConfigPageHeader_t	 hdr;
74380d3ac77SMichael Reed 	CONFIGPARMS		 cfg;
74480d3ac77SMichael Reed 	FCPortPage0_t		*ppage0_alloc;
74580d3ac77SMichael Reed 	FCPortPage0_t		*pp0dest;
74680d3ac77SMichael Reed 	dma_addr_t		 page0_dma;
74780d3ac77SMichael Reed 	int			 data_sz;
74880d3ac77SMichael Reed 	int			 copy_sz;
74980d3ac77SMichael Reed 	int			 rc;
75080d3ac77SMichael Reed 	int			 count = 400;
75180d3ac77SMichael Reed 
75280d3ac77SMichael Reed 	if (portnum > 1)
75380d3ac77SMichael Reed 		return -EINVAL;
75480d3ac77SMichael Reed 
75580d3ac77SMichael Reed 	/* Get FCPort Page 0 header */
75680d3ac77SMichael Reed 	hdr.PageVersion = 0;
75780d3ac77SMichael Reed 	hdr.PageLength = 0;
75880d3ac77SMichael Reed 	hdr.PageNumber = 0;
75980d3ac77SMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
76080d3ac77SMichael Reed 	cfg.cfghdr.hdr = &hdr;
76180d3ac77SMichael Reed 	cfg.physAddr = -1;
76280d3ac77SMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
76380d3ac77SMichael Reed 	cfg.dir = 0;
76480d3ac77SMichael Reed 	cfg.pageAddr = portnum;
76580d3ac77SMichael Reed 	cfg.timeout = 0;
76680d3ac77SMichael Reed 
76780d3ac77SMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
76880d3ac77SMichael Reed 		return rc;
76980d3ac77SMichael Reed 
77080d3ac77SMichael Reed 	if (hdr.PageLength == 0)
77180d3ac77SMichael Reed 		return 0;
77280d3ac77SMichael Reed 
77380d3ac77SMichael Reed 	data_sz = hdr.PageLength * 4;
77480d3ac77SMichael Reed 	rc = -ENOMEM;
77580d3ac77SMichael Reed 	ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
77680d3ac77SMichael Reed 	if (ppage0_alloc) {
77780d3ac77SMichael Reed 
77880d3ac77SMichael Reed  try_again:
77980d3ac77SMichael Reed 		memset((u8 *)ppage0_alloc, 0, data_sz);
78080d3ac77SMichael Reed 		cfg.physAddr = page0_dma;
78180d3ac77SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
78280d3ac77SMichael Reed 
78380d3ac77SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
78480d3ac77SMichael Reed 			/* save the data */
78580d3ac77SMichael Reed 			pp0dest = &ioc->fc_port_page0[portnum];
78680d3ac77SMichael Reed 			copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
78780d3ac77SMichael Reed 			memcpy(pp0dest, ppage0_alloc, copy_sz);
78880d3ac77SMichael Reed 
78980d3ac77SMichael Reed 			/*
79080d3ac77SMichael Reed 			 *	Normalize endianness of structure data,
79180d3ac77SMichael Reed 			 *	by byte-swapping all > 1 byte fields!
79280d3ac77SMichael Reed 			 */
79380d3ac77SMichael Reed 			pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
79480d3ac77SMichael Reed 			pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
79580d3ac77SMichael Reed 			pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
79680d3ac77SMichael Reed 			pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
79780d3ac77SMichael Reed 			pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
79880d3ac77SMichael Reed 			pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
79980d3ac77SMichael Reed 			pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
80080d3ac77SMichael Reed 			pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
80180d3ac77SMichael Reed 			pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
80280d3ac77SMichael Reed 			pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
80380d3ac77SMichael Reed 			pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
80480d3ac77SMichael Reed 			pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
80580d3ac77SMichael Reed 			pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
80680d3ac77SMichael Reed 			pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
80780d3ac77SMichael Reed 			pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
80880d3ac77SMichael Reed 			pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
80980d3ac77SMichael Reed 
81080d3ac77SMichael Reed 			/*
81180d3ac77SMichael Reed 			 * if still doing discovery,
81280d3ac77SMichael Reed 			 * hang loose a while until finished
81380d3ac77SMichael Reed 			 */
81477d88ee2SMichael Reed 			if ((pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) ||
81577d88ee2SMichael Reed 			    (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE &&
81677d88ee2SMichael Reed 			     (pp0dest->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_TYPE_MASK)
81777d88ee2SMichael Reed 			      == MPI_FCPORTPAGE0_FLAGS_ATTACH_NO_INIT)) {
81880d3ac77SMichael Reed 				if (count-- > 0) {
819d6be06c8SMichael Reed 					msleep(100);
82080d3ac77SMichael Reed 					goto try_again;
82180d3ac77SMichael Reed 				}
82280d3ac77SMichael Reed 				printk(MYIOC_s_INFO_FMT "Firmware discovery not"
82380d3ac77SMichael Reed 							" complete.\n",
82480d3ac77SMichael Reed 						ioc->name);
82580d3ac77SMichael Reed 			}
826eb5329f4SPrakash, Sathya 			mptfc_display_port_link_speed(ioc, portnum, pp0dest);
82780d3ac77SMichael Reed 		}
82880d3ac77SMichael Reed 
82980d3ac77SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
83080d3ac77SMichael Reed 	}
83180d3ac77SMichael Reed 
83280d3ac77SMichael Reed 	return rc;
83380d3ac77SMichael Reed }
83480d3ac77SMichael Reed 
835ca2f938eSMichael Reed static int
836ca2f938eSMichael Reed mptfc_WriteFcPortPage1(MPT_ADAPTER *ioc, int portnum)
837ca2f938eSMichael Reed {
838ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
839ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
840ca2f938eSMichael Reed 	int			 rc;
841ca2f938eSMichael Reed 
842ca2f938eSMichael Reed 	if (portnum > 1)
843ca2f938eSMichael Reed 		return -EINVAL;
844ca2f938eSMichael Reed 
845ca2f938eSMichael Reed 	if (!(ioc->fc_data.fc_port_page1[portnum].data))
846ca2f938eSMichael Reed 		return -EINVAL;
847ca2f938eSMichael Reed 
848ca2f938eSMichael Reed 	/* get fcport page 1 header */
849ca2f938eSMichael Reed 	hdr.PageVersion = 0;
850ca2f938eSMichael Reed 	hdr.PageLength = 0;
851ca2f938eSMichael Reed 	hdr.PageNumber = 1;
852ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
853ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
854ca2f938eSMichael Reed 	cfg.physAddr = -1;
855ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
856ca2f938eSMichael Reed 	cfg.dir = 0;
857ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
858ca2f938eSMichael Reed 	cfg.timeout = 0;
859ca2f938eSMichael Reed 
860ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
861ca2f938eSMichael Reed 		return rc;
862ca2f938eSMichael Reed 
863ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
864ca2f938eSMichael Reed 		return -ENODEV;
865ca2f938eSMichael Reed 
866ca2f938eSMichael Reed 	if (hdr.PageLength*4 != ioc->fc_data.fc_port_page1[portnum].pg_sz)
867ca2f938eSMichael Reed 		return -EINVAL;
868ca2f938eSMichael Reed 
869ca2f938eSMichael Reed 	cfg.physAddr = ioc->fc_data.fc_port_page1[portnum].dma;
870ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
871ca2f938eSMichael Reed 	cfg.dir = 1;
872ca2f938eSMichael Reed 
873ca2f938eSMichael Reed 	rc = mpt_config(ioc, &cfg);
874ca2f938eSMichael Reed 
875ca2f938eSMichael Reed 	return rc;
876ca2f938eSMichael Reed }
877ca2f938eSMichael Reed 
878ca2f938eSMichael Reed static int
879ca2f938eSMichael Reed mptfc_GetFcPortPage1(MPT_ADAPTER *ioc, int portnum)
880ca2f938eSMichael Reed {
881ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
882ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
883ca2f938eSMichael Reed 	FCPortPage1_t		*page1_alloc;
884ca2f938eSMichael Reed 	dma_addr_t		 page1_dma;
885ca2f938eSMichael Reed 	int			 data_sz;
886ca2f938eSMichael Reed 	int			 rc;
887ca2f938eSMichael Reed 
888ca2f938eSMichael Reed 	if (portnum > 1)
889ca2f938eSMichael Reed 		return -EINVAL;
890ca2f938eSMichael Reed 
891ca2f938eSMichael Reed 	/* get fcport page 1 header */
892ca2f938eSMichael Reed 	hdr.PageVersion = 0;
893ca2f938eSMichael Reed 	hdr.PageLength = 0;
894ca2f938eSMichael Reed 	hdr.PageNumber = 1;
895ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
896ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
897ca2f938eSMichael Reed 	cfg.physAddr = -1;
898ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
899ca2f938eSMichael Reed 	cfg.dir = 0;
900ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
901ca2f938eSMichael Reed 	cfg.timeout = 0;
902ca2f938eSMichael Reed 
903ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
904ca2f938eSMichael Reed 		return rc;
905ca2f938eSMichael Reed 
906ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
907ca2f938eSMichael Reed 		return -ENODEV;
908ca2f938eSMichael Reed 
909ca2f938eSMichael Reed start_over:
910ca2f938eSMichael Reed 
911ca2f938eSMichael Reed 	if (ioc->fc_data.fc_port_page1[portnum].data == NULL) {
912ca2f938eSMichael Reed 		data_sz = hdr.PageLength * 4;
913ca2f938eSMichael Reed 		if (data_sz < sizeof(FCPortPage1_t))
914ca2f938eSMichael Reed 			data_sz = sizeof(FCPortPage1_t);
915ca2f938eSMichael Reed 
916ca2f938eSMichael Reed 		page1_alloc = (FCPortPage1_t *) pci_alloc_consistent(ioc->pcidev,
917ca2f938eSMichael Reed 						data_sz,
918ca2f938eSMichael Reed 						&page1_dma);
919ca2f938eSMichael Reed 		if (!page1_alloc)
920ca2f938eSMichael Reed 			return -ENOMEM;
921ca2f938eSMichael Reed 	}
922ca2f938eSMichael Reed 	else {
923ca2f938eSMichael Reed 		page1_alloc = ioc->fc_data.fc_port_page1[portnum].data;
924ca2f938eSMichael Reed 		page1_dma = ioc->fc_data.fc_port_page1[portnum].dma;
925ca2f938eSMichael Reed 		data_sz = ioc->fc_data.fc_port_page1[portnum].pg_sz;
926ca2f938eSMichael Reed 		if (hdr.PageLength * 4 > data_sz) {
927ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[portnum].data = NULL;
928ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
929ca2f938eSMichael Reed 				page1_alloc, page1_dma);
930ca2f938eSMichael Reed 			goto start_over;
931ca2f938eSMichael Reed 		}
932ca2f938eSMichael Reed 	}
933ca2f938eSMichael Reed 
934ca2f938eSMichael Reed 	memset(page1_alloc,0,data_sz);
935ca2f938eSMichael Reed 
936ca2f938eSMichael Reed 	cfg.physAddr = page1_dma;
937ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
938ca2f938eSMichael Reed 
939ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) == 0) {
940ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = page1_alloc;
941ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].pg_sz = data_sz;
942ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].dma = page1_dma;
943ca2f938eSMichael Reed 	}
944ca2f938eSMichael Reed 	else {
945ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = NULL;
946ca2f938eSMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
947ca2f938eSMichael Reed 			page1_alloc, page1_dma);
948ca2f938eSMichael Reed 	}
949ca2f938eSMichael Reed 
950ca2f938eSMichael Reed 	return rc;
951ca2f938eSMichael Reed }
952ca2f938eSMichael Reed 
953ca2f938eSMichael Reed static void
954ca2f938eSMichael Reed mptfc_SetFcPortPage1_defaults(MPT_ADAPTER *ioc)
955ca2f938eSMichael Reed {
956ca2f938eSMichael Reed 	int		ii;
957ca2f938eSMichael Reed 	FCPortPage1_t	*pp1;
958ca2f938eSMichael Reed 
959ca2f938eSMichael Reed 	#define MPTFC_FW_DEVICE_TIMEOUT	(1)
960ca2f938eSMichael Reed 	#define MPTFC_FW_IO_PEND_TIMEOUT (1)
961ca2f938eSMichael Reed 	#define ON_FLAGS  (MPI_FCPORTPAGE1_FLAGS_IMMEDIATE_ERROR_REPLY)
962ca2f938eSMichael Reed 	#define OFF_FLAGS (MPI_FCPORTPAGE1_FLAGS_VERBOSE_RESCAN_EVENTS)
963ca2f938eSMichael Reed 
964ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
965ca2f938eSMichael Reed 		if (mptfc_GetFcPortPage1(ioc, ii) != 0)
966ca2f938eSMichael Reed 			continue;
967ca2f938eSMichael Reed 		pp1 = ioc->fc_data.fc_port_page1[ii].data;
968ca2f938eSMichael Reed 		if ((pp1->InitiatorDeviceTimeout == MPTFC_FW_DEVICE_TIMEOUT)
969ca2f938eSMichael Reed 		 && (pp1->InitiatorIoPendTimeout == MPTFC_FW_IO_PEND_TIMEOUT)
970ca2f938eSMichael Reed 		 && ((pp1->Flags & ON_FLAGS) == ON_FLAGS)
971ca2f938eSMichael Reed 		 && ((pp1->Flags & OFF_FLAGS) == 0))
972ca2f938eSMichael Reed 			continue;
973ca2f938eSMichael Reed 		pp1->InitiatorDeviceTimeout = MPTFC_FW_DEVICE_TIMEOUT;
974ca2f938eSMichael Reed 		pp1->InitiatorIoPendTimeout = MPTFC_FW_IO_PEND_TIMEOUT;
975ca2f938eSMichael Reed 		pp1->Flags &= ~OFF_FLAGS;
976ca2f938eSMichael Reed 		pp1->Flags |= ON_FLAGS;
977ca2f938eSMichael Reed 		mptfc_WriteFcPortPage1(ioc, ii);
978ca2f938eSMichael Reed 	}
979ca2f938eSMichael Reed }
980ca2f938eSMichael Reed 
981ca2f938eSMichael Reed 
98205e8ec17SMichael Reed static void
98305e8ec17SMichael Reed mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
98405e8ec17SMichael Reed {
9855d947f2bSMichael Reed 	unsigned	class = 0;
9865d947f2bSMichael Reed 	unsigned	cos = 0;
9875d947f2bSMichael Reed 	unsigned	speed;
9885d947f2bSMichael Reed 	unsigned	port_type;
9895d947f2bSMichael Reed 	unsigned	port_state;
9905d947f2bSMichael Reed 	FCPortPage0_t	*pp0;
9915d947f2bSMichael Reed 	struct Scsi_Host *sh;
9925d947f2bSMichael Reed 	char		*sn;
99305e8ec17SMichael Reed 
99405e8ec17SMichael Reed 	/* don't know what to do as only one scsi (fc) host was allocated */
99505e8ec17SMichael Reed 	if (portnum != 0)
99605e8ec17SMichael Reed 		return;
99705e8ec17SMichael Reed 
9985d947f2bSMichael Reed 	pp0 = &ioc->fc_port_page0[portnum];
9995d947f2bSMichael Reed 	sh = ioc->sh;
10005d947f2bSMichael Reed 
10015d947f2bSMichael Reed 	sn = fc_host_symbolic_name(sh);
10025d947f2bSMichael Reed 	snprintf(sn, FC_SYMBOLIC_NAME_SIZE, "%s %s%08xh",
10035d947f2bSMichael Reed 	    ioc->prod_name,
10045d947f2bSMichael Reed 	    MPT_FW_REV_MAGIC_ID_STRING,
10055d947f2bSMichael Reed 	    ioc->facts.FWVersion.Word);
10065d947f2bSMichael Reed 
10075d947f2bSMichael Reed 	fc_host_tgtid_bind_type(sh) = FC_TGTID_BIND_BY_WWPN;
10085d947f2bSMichael Reed 
10095d947f2bSMichael Reed 	fc_host_maxframe_size(sh) = pp0->MaxFrameSize;
10105d947f2bSMichael Reed 
10115d947f2bSMichael Reed 	fc_host_node_name(sh) =
10125d947f2bSMichael Reed 	    	(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
10135d947f2bSMichael Reed 
10145d947f2bSMichael Reed 	fc_host_port_name(sh) =
10155d947f2bSMichael Reed 	    	(u64)pp0->WWPN.High << 32 | (u64)pp0->WWPN.Low;
10165d947f2bSMichael Reed 
10175d947f2bSMichael Reed 	fc_host_port_id(sh) = pp0->PortIdentifier;
10185d947f2bSMichael Reed 
10195d947f2bSMichael Reed 	class = pp0->SupportedServiceClass;
102005e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
102105e8ec17SMichael Reed 		cos |= FC_COS_CLASS1;
102205e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
102305e8ec17SMichael Reed 		cos |= FC_COS_CLASS2;
102405e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
102505e8ec17SMichael Reed 		cos |= FC_COS_CLASS3;
10265d947f2bSMichael Reed 	fc_host_supported_classes(sh) = cos;
102705e8ec17SMichael Reed 
10285d947f2bSMichael Reed 	if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT)
10295d947f2bSMichael Reed 		speed = FC_PORTSPEED_1GBIT;
10305d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT)
10315d947f2bSMichael Reed 		speed = FC_PORTSPEED_2GBIT;
10325d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT)
10335d947f2bSMichael Reed 		speed = FC_PORTSPEED_4GBIT;
10345d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_10GBIT)
10355d947f2bSMichael Reed 		speed = FC_PORTSPEED_10GBIT;
10365d947f2bSMichael Reed 	else
10375d947f2bSMichael Reed 		speed = FC_PORTSPEED_UNKNOWN;
10385d947f2bSMichael Reed 	fc_host_speed(sh) = speed;
103905e8ec17SMichael Reed 
10405d947f2bSMichael Reed 	speed = 0;
10415d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_1GBIT_SPEED)
10425d947f2bSMichael Reed 		speed |= FC_PORTSPEED_1GBIT;
10435d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_2GBIT_SPEED)
10445d947f2bSMichael Reed 		speed |= FC_PORTSPEED_2GBIT;
10455d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_4GBIT_SPEED)
10465d947f2bSMichael Reed 		speed |= FC_PORTSPEED_4GBIT;
10475d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_10GBIT_SPEED)
10485d947f2bSMichael Reed 		speed |= FC_PORTSPEED_10GBIT;
10495d947f2bSMichael Reed 	fc_host_supported_speeds(sh) = speed;
105005e8ec17SMichael Reed 
10515d947f2bSMichael Reed 	port_state = FC_PORTSTATE_UNKNOWN;
10525d947f2bSMichael Reed 	if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE)
10535d947f2bSMichael Reed 		port_state = FC_PORTSTATE_ONLINE;
10545d947f2bSMichael Reed 	else if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_OFFLINE)
10555d947f2bSMichael Reed 		port_state = FC_PORTSTATE_LINKDOWN;
10565d947f2bSMichael Reed 	fc_host_port_state(sh) = port_state;
105705e8ec17SMichael Reed 
10585d947f2bSMichael Reed 	port_type = FC_PORTTYPE_UNKNOWN;
10595d947f2bSMichael Reed 	if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_POINT_TO_POINT)
10605d947f2bSMichael Reed 		port_type = FC_PORTTYPE_PTP;
10615d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PRIVATE_LOOP)
10625d947f2bSMichael Reed 		port_type = FC_PORTTYPE_LPORT;
10635d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PUBLIC_LOOP)
10645d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NLPORT;
10655d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_FABRIC_DIRECT)
10665d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NPORT;
10675d947f2bSMichael Reed 	fc_host_port_type(sh) = port_type;
106805e8ec17SMichael Reed 
10695d947f2bSMichael Reed 	fc_host_fabric_name(sh) =
10705d947f2bSMichael Reed 	    (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_FABRIC_WWN_VALID) ?
10715d947f2bSMichael Reed 		(u64) pp0->FabricWWNN.High << 32 | (u64) pp0->FabricWWPN.Low :
10725d947f2bSMichael Reed 		(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
10735d947f2bSMichael Reed 
107405e8ec17SMichael Reed }
107505e8ec17SMichael Reed 
107605e8ec17SMichael Reed static void
1077eb5329f4SPrakash, Sathya mptfc_link_status_change(struct work_struct *work)
1078eb5329f4SPrakash, Sathya {
1079eb5329f4SPrakash, Sathya 	MPT_ADAPTER             *ioc =
1080eb5329f4SPrakash, Sathya 		container_of(work, MPT_ADAPTER, fc_rescan_work);
1081eb5329f4SPrakash, Sathya 	int ii;
1082eb5329f4SPrakash, Sathya 
1083eb5329f4SPrakash, Sathya 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++)
1084eb5329f4SPrakash, Sathya 		(void) mptfc_GetFcPortPage0(ioc, ii);
1085eb5329f4SPrakash, Sathya 
1086eb5329f4SPrakash, Sathya }
1087eb5329f4SPrakash, Sathya 
1088eb5329f4SPrakash, Sathya static void
1089c4028958SDavid Howells mptfc_setup_reset(struct work_struct *work)
1090419835e2SMichael Reed {
1091c4028958SDavid Howells 	MPT_ADAPTER		*ioc =
1092c4028958SDavid Howells 		container_of(work, MPT_ADAPTER, fc_setup_reset_work);
1093419835e2SMichael Reed 	u64			pn;
1094419835e2SMichael Reed 	struct mptfc_rport_info *ri;
109508f5c5c2SKashyap, Desai 	struct scsi_target      *starget;
109608f5c5c2SKashyap, Desai 	VirtTarget              *vtarget;
1097419835e2SMichael Reed 
1098419835e2SMichael Reed 	/* reset about to happen, delete (block) all rports */
1099419835e2SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
1100419835e2SMichael Reed 		if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
1101419835e2SMichael Reed 			ri->flags &= ~MPT_RPORT_INFO_FLAGS_REGISTERED;
1102419835e2SMichael Reed 			fc_remote_port_delete(ri->rport);	/* won't sleep */
1103419835e2SMichael Reed 			ri->rport = NULL;
110408f5c5c2SKashyap, Desai 			starget = ri->starget;
110508f5c5c2SKashyap, Desai 			if (starget) {
110608f5c5c2SKashyap, Desai 				vtarget = starget->hostdata;
110708f5c5c2SKashyap, Desai 				if (vtarget)
110808f5c5c2SKashyap, Desai 					vtarget->deleted = 1;
110908f5c5c2SKashyap, Desai 			}
1110419835e2SMichael Reed 
1111419835e2SMichael Reed 			pn = (u64)ri->pg0.WWPN.High << 32 |
1112419835e2SMichael Reed 			     (u64)ri->pg0.WWPN.Low;
1113d6ecdd63SPrakash, Sathya 			dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
1114419835e2SMichael Reed 				"mptfc_setup_reset.%d: %llx deleted\n",
1115419835e2SMichael Reed 				ioc->name,
1116419835e2SMichael Reed 				ioc->sh->host_no,
1117419835e2SMichael Reed 				(unsigned long long)pn));
1118419835e2SMichael Reed 		}
1119419835e2SMichael Reed 	}
1120419835e2SMichael Reed }
1121419835e2SMichael Reed 
1122419835e2SMichael Reed static void
1123c4028958SDavid Howells mptfc_rescan_devices(struct work_struct *work)
112405e8ec17SMichael Reed {
1125c4028958SDavid Howells 	MPT_ADAPTER		*ioc =
1126c4028958SDavid Howells 		container_of(work, MPT_ADAPTER, fc_rescan_work);
112705e8ec17SMichael Reed 	int			ii;
112865207fedSMoore, Eric 	u64			pn;
112905e8ec17SMichael Reed 	struct mptfc_rport_info *ri;
113008f5c5c2SKashyap, Desai 	struct scsi_target      *starget;
113108f5c5c2SKashyap, Desai 	VirtTarget              *vtarget;
113205e8ec17SMichael Reed 
113305e8ec17SMichael Reed 	/* start by tagging all ports as missing */
113405e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
113505e8ec17SMichael Reed 		if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
113605e8ec17SMichael Reed 			ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
113705e8ec17SMichael Reed 		}
113805e8ec17SMichael Reed 	}
113905e8ec17SMichael Reed 
114005e8ec17SMichael Reed 	/*
114105e8ec17SMichael Reed 	 * now rescan devices known to adapter,
114205e8ec17SMichael Reed 	 * will reregister existing rports
114305e8ec17SMichael Reed 	 */
114405e8ec17SMichael Reed 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
114580d3ac77SMichael Reed 		(void) mptfc_GetFcPortPage0(ioc, ii);
114605e8ec17SMichael Reed 		mptfc_init_host_attr(ioc, ii);	/* refresh */
114705e8ec17SMichael Reed 		mptfc_GetFcDevPage0(ioc, ii, mptfc_register_dev);
114805e8ec17SMichael Reed 	}
114905e8ec17SMichael Reed 
115005e8ec17SMichael Reed 	/* delete devices still missing */
115105e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
115205e8ec17SMichael Reed 		/* if newly missing, delete it */
115365207fedSMoore, Eric 		if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
115405e8ec17SMichael Reed 
115505e8ec17SMichael Reed 			ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
115605e8ec17SMichael Reed 				       MPT_RPORT_INFO_FLAGS_MISSING);
115765207fedSMoore, Eric 			fc_remote_port_delete(ri->rport);	/* won't sleep */
11583bc7bf1dSMichael Reed 			ri->rport = NULL;
115908f5c5c2SKashyap, Desai 			starget = ri->starget;
116008f5c5c2SKashyap, Desai 			if (starget) {
116108f5c5c2SKashyap, Desai 				vtarget = starget->hostdata;
116208f5c5c2SKashyap, Desai 				if (vtarget)
116308f5c5c2SKashyap, Desai 					vtarget->deleted = 1;
116408f5c5c2SKashyap, Desai 			}
116565207fedSMoore, Eric 
116665207fedSMoore, Eric 			pn = (u64)ri->pg0.WWPN.High << 32 |
116765207fedSMoore, Eric 			     (u64)ri->pg0.WWPN.Low;
1168d6ecdd63SPrakash, Sathya 			dfcprintk (ioc, printk(MYIOC_s_DEBUG_FMT
11693bc7bf1dSMichael Reed 				"mptfc_rescan.%d: %llx deleted\n",
11703bc7bf1dSMichael Reed 				ioc->name,
11713bc7bf1dSMichael Reed 				ioc->sh->host_no,
117265207fedSMoore, Eric 				(unsigned long long)pn));
117305e8ec17SMichael Reed 		}
117405e8ec17SMichael Reed 	}
117505e8ec17SMichael Reed }
117605e8ec17SMichael Reed 
11772496af39SMoore, Eric Dean  static int
11782496af39SMoore, Eric Dean  mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
11792496af39SMoore, Eric Dean  {
11802496af39SMoore, Eric Dean  	struct Scsi_Host	*sh;
11812496af39SMoore, Eric Dean  	MPT_SCSI_HOST		*hd;
11822496af39SMoore, Eric Dean  	MPT_ADAPTER 		*ioc;
11832496af39SMoore, Eric Dean  	unsigned long		 flags;
11841ca00bb7SChristoph Hellwig 	int			 ii;
11852496af39SMoore, Eric Dean  	int			 numSGE = 0;
11862496af39SMoore, Eric Dean  	int			 scale;
11872496af39SMoore, Eric Dean  	int			 ioc_cap;
11882496af39SMoore, Eric Dean  	int			error=0;
11892496af39SMoore, Eric Dean  	int			r;
11902496af39SMoore, Eric Dean  
11912496af39SMoore, Eric Dean  	if ((r = mpt_attach(pdev,id)) != 0)
11922496af39SMoore, Eric Dean  		return r;
11932496af39SMoore, Eric Dean  
11942496af39SMoore, Eric Dean  	ioc = pci_get_drvdata(pdev);
1195d335cc38SMoore, Eric Dean  	ioc->DoneCtx = mptfcDoneCtx;
1196d335cc38SMoore, Eric Dean  	ioc->TaskCtx = mptfcTaskCtx;
1197d335cc38SMoore, Eric Dean  	ioc->InternalCtx = mptfcInternalCtx;
11982496af39SMoore, Eric Dean  
11992496af39SMoore, Eric Dean  	/*  Added sanity check on readiness of the MPT adapter.
12002496af39SMoore, Eric Dean  	 */
12012496af39SMoore, Eric Dean  	if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
12022496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
12032496af39SMoore, Eric Dean  		  "Skipping because it's not operational!\n",
12042496af39SMoore, Eric Dean  		  ioc->name);
12057acec1e7SMoore, Eric Dean 		error = -ENODEV;
12067acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12072496af39SMoore, Eric Dean  	}
12082496af39SMoore, Eric Dean  
12092496af39SMoore, Eric Dean  	if (!ioc->active) {
12102496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
12112496af39SMoore, Eric Dean  		  ioc->name);
12127acec1e7SMoore, Eric Dean 		error = -ENODEV;
12137acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12142496af39SMoore, Eric Dean  	}
12152496af39SMoore, Eric Dean  
12162496af39SMoore, Eric Dean  	/*  Sanity check - ensure at least 1 port is INITIATOR capable
12172496af39SMoore, Eric Dean  	 */
12182496af39SMoore, Eric Dean  	ioc_cap = 0;
12192496af39SMoore, Eric Dean  	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
12202496af39SMoore, Eric Dean  		if (ioc->pfacts[ii].ProtocolFlags &
12212496af39SMoore, Eric Dean  		    MPI_PORTFACTS_PROTOCOL_INITIATOR)
12222496af39SMoore, Eric Dean  			ioc_cap ++;
12232496af39SMoore, Eric Dean  	}
12242496af39SMoore, Eric Dean  
12252496af39SMoore, Eric Dean  	if (!ioc_cap) {
12262496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
12272496af39SMoore, Eric Dean  			"Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
12282496af39SMoore, Eric Dean  			ioc->name, ioc);
1229793955f5SEric Moore 		return 0;
12302496af39SMoore, Eric Dean  	}
12312496af39SMoore, Eric Dean  
12322496af39SMoore, Eric Dean  	sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
12332496af39SMoore, Eric Dean  
12342496af39SMoore, Eric Dean  	if (!sh) {
12352496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
12362496af39SMoore, Eric Dean  			"Unable to register controller with SCSI subsystem\n",
12372496af39SMoore, Eric Dean  			ioc->name);
12387acec1e7SMoore, Eric Dean 		error = -1;
12397acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12402496af39SMoore, Eric Dean          }
12412496af39SMoore, Eric Dean  
124280d3ac77SMichael Reed 	spin_lock_init(&ioc->fc_rescan_work_lock);
1243c4028958SDavid Howells 	INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices);
1244c4028958SDavid Howells 	INIT_WORK(&ioc->fc_setup_reset_work, mptfc_setup_reset);
1245eb5329f4SPrakash, Sathya 	INIT_WORK(&ioc->fc_lsc_work, mptfc_link_status_change);
124605e8ec17SMichael Reed 
12472496af39SMoore, Eric Dean  	spin_lock_irqsave(&ioc->FreeQlock, flags);
12482496af39SMoore, Eric Dean  
12492496af39SMoore, Eric Dean  	/* Attach the SCSI Host to the IOC structure
12502496af39SMoore, Eric Dean  	 */
12512496af39SMoore, Eric Dean  	ioc->sh = sh;
12522496af39SMoore, Eric Dean  
12532496af39SMoore, Eric Dean  	sh->io_port = 0;
12542496af39SMoore, Eric Dean  	sh->n_io_port = 0;
12552496af39SMoore, Eric Dean  	sh->irq = 0;
12562496af39SMoore, Eric Dean  
12572496af39SMoore, Eric Dean  	/* set 16 byte cdb's */
12582496af39SMoore, Eric Dean  	sh->max_cmd_len = 16;
12592496af39SMoore, Eric Dean  
1260793955f5SEric Moore 	sh->max_id = ioc->pfacts->MaxDevices;
1261793955f5SEric Moore 	sh->max_lun = max_lun;
12622496af39SMoore, Eric Dean  
12632496af39SMoore, Eric Dean  	/* Required entry.
12642496af39SMoore, Eric Dean  	 */
12652496af39SMoore, Eric Dean  	sh->unique_id = ioc->id;
12662496af39SMoore, Eric Dean  
12672496af39SMoore, Eric Dean  	/* Verify that we won't exceed the maximum
12682496af39SMoore, Eric Dean  	 * number of chain buffers
12692496af39SMoore, Eric Dean  	 * We can optimize:  ZZ = req_sz/sizeof(SGE)
12702496af39SMoore, Eric Dean  	 * For 32bit SGE's:
12712496af39SMoore, Eric Dean  	 *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
12722496af39SMoore, Eric Dean  	 *               + (req_sz - 64)/sizeof(SGE)
12732496af39SMoore, Eric Dean  	 * A slightly different algorithm is required for
12742496af39SMoore, Eric Dean  	 * 64bit SGEs.
12752496af39SMoore, Eric Dean  	 */
127614d0f0b0SKashyap, Desai 	scale = ioc->req_sz/ioc->SGE_size;
127714d0f0b0SKashyap, Desai 	if (ioc->sg_addr_size == sizeof(u64)) {
12782496af39SMoore, Eric Dean  		numSGE = (scale - 1) *
12792496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
128014d0f0b0SKashyap, Desai 		  (ioc->req_sz - 60) / ioc->SGE_size;
12812496af39SMoore, Eric Dean  	} else {
12822496af39SMoore, Eric Dean  		numSGE = 1 + (scale - 1) *
12832496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
128414d0f0b0SKashyap, Desai 		  (ioc->req_sz - 64) / ioc->SGE_size;
12852496af39SMoore, Eric Dean  	}
12862496af39SMoore, Eric Dean  
12872496af39SMoore, Eric Dean  	if (numSGE < sh->sg_tablesize) {
12882496af39SMoore, Eric Dean  		/* Reset this value */
1289d6ecdd63SPrakash, Sathya 		dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
12902496af39SMoore, Eric Dean  		  "Resetting sg_tablesize to %d from %d\n",
12912496af39SMoore, Eric Dean  		  ioc->name, numSGE, sh->sg_tablesize));
12922496af39SMoore, Eric Dean  		sh->sg_tablesize = numSGE;
12932496af39SMoore, Eric Dean  	}
12942496af39SMoore, Eric Dean  
12952496af39SMoore, Eric Dean  	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
12962496af39SMoore, Eric Dean  
1297e7eae9f6SEric Moore 	hd = shost_priv(sh);
12982496af39SMoore, Eric Dean  	hd->ioc = ioc;
12992496af39SMoore, Eric Dean  
13002496af39SMoore, Eric Dean  	/* SCSI needs scsi_cmnd lookup table!
13012496af39SMoore, Eric Dean  	 * (with size equal to req_depth*PtrSz!)
13022496af39SMoore, Eric Dean  	 */
1303e8206381SEric Moore 	ioc->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1304e8206381SEric Moore 	if (!ioc->ScsiLookup) {
13052496af39SMoore, Eric Dean  		error = -ENOMEM;
13067acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
13072496af39SMoore, Eric Dean  	}
1308e8206381SEric Moore 	spin_lock_init(&ioc->scsi_lookup_lock);
13092496af39SMoore, Eric Dean  
1310d6ecdd63SPrakash, Sathya 	dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
1311e8206381SEric Moore 		 ioc->name, ioc->ScsiLookup));
13122496af39SMoore, Eric Dean  
13132496af39SMoore, Eric Dean  	hd->last_queue_full = 0;
13142496af39SMoore, Eric Dean  
131505e8ec17SMichael Reed 	sh->transportt = mptfc_transport_template;
13162496af39SMoore, Eric Dean  	error = scsi_add_host (sh, &ioc->pcidev->dev);
13172496af39SMoore, Eric Dean  	if(error) {
131829dd3609SEric Moore 		dprintk(ioc, printk(MYIOC_s_ERR_FMT
131929dd3609SEric Moore 		  "scsi_add_host failed\n", ioc->name));
13207acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
13212496af39SMoore, Eric Dean  	}
13222496af39SMoore, Eric Dean  
132365207fedSMoore, Eric 	/* initialize workqueue */
132465207fedSMoore, Eric 
1325aab0de24SKay Sievers 	snprintf(ioc->fc_rescan_work_q_name, sizeof(ioc->fc_rescan_work_q_name),
1326aab0de24SKay Sievers 		 "mptfc_wq_%d", sh->host_no);
132765207fedSMoore, Eric 	ioc->fc_rescan_work_q =
132877d4f080SBhaktipriya Shridhar 		alloc_ordered_workqueue(ioc->fc_rescan_work_q_name,
132977d4f080SBhaktipriya Shridhar 					WQ_MEM_RECLAIM);
1330c08b3f9aSWei Yongjun 	if (!ioc->fc_rescan_work_q) {
1331c08b3f9aSWei Yongjun 		error = -ENOMEM;
1332*cfd2aff7SHannes Reinecke 		goto out_mptfc_host;
1333c08b3f9aSWei Yongjun 	}
133465207fedSMoore, Eric 
133565207fedSMoore, Eric 	/*
133680d3ac77SMichael Reed 	 *  Pre-fetch FC port WWN and stuff...
133780d3ac77SMichael Reed 	 *  (FCPortPage0_t stuff)
133880d3ac77SMichael Reed 	 */
133980d3ac77SMichael Reed 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
134080d3ac77SMichael Reed 		(void) mptfc_GetFcPortPage0(ioc, ii);
134180d3ac77SMichael Reed 	}
1342ca2f938eSMichael Reed 	mptfc_SetFcPortPage1_defaults(ioc);
134380d3ac77SMichael Reed 
134480d3ac77SMichael Reed 	/*
134565207fedSMoore, Eric 	 * scan for rports -
134665207fedSMoore, Eric 	 *	by doing it via the workqueue, some locking is eliminated
134765207fedSMoore, Eric 	 */
134865207fedSMoore, Eric 
134965207fedSMoore, Eric 	queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
135065207fedSMoore, Eric 	flush_workqueue(ioc->fc_rescan_work_q);
135105e8ec17SMichael Reed 
13522496af39SMoore, Eric Dean  	return 0;
13532496af39SMoore, Eric Dean  
1354*cfd2aff7SHannes Reinecke out_mptfc_host:
1355*cfd2aff7SHannes Reinecke 	scsi_remove_host(sh);
1356*cfd2aff7SHannes Reinecke 
13577acec1e7SMoore, Eric Dean out_mptfc_probe:
13582496af39SMoore, Eric Dean  
13592496af39SMoore, Eric Dean  	mptscsih_remove(pdev);
13602496af39SMoore, Eric Dean  	return error;
13612496af39SMoore, Eric Dean  }
13622496af39SMoore, Eric Dean  
13632496af39SMoore, Eric Dean  static struct pci_driver mptfc_driver = {
13642496af39SMoore, Eric Dean  	.name		= "mptfc",
13652496af39SMoore, Eric Dean  	.id_table	= mptfc_pci_table,
13662496af39SMoore, Eric Dean  	.probe		= mptfc_probe,
136747b1ea75SGreg Kroah-Hartman 	.remove		= mptfc_remove,
13682496af39SMoore, Eric Dean  	.shutdown	= mptscsih_shutdown,
13692496af39SMoore, Eric Dean  #ifdef CONFIG_PM
13702496af39SMoore, Eric Dean  	.suspend	= mptscsih_suspend,
13712496af39SMoore, Eric Dean  	.resume		= mptscsih_resume,
13722496af39SMoore, Eric Dean  #endif
13732496af39SMoore, Eric Dean  };
13742496af39SMoore, Eric Dean  
137580d3ac77SMichael Reed static int
137680d3ac77SMichael Reed mptfc_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
137780d3ac77SMichael Reed {
137880d3ac77SMichael Reed 	MPT_SCSI_HOST *hd;
137980d3ac77SMichael Reed 	u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
138080d3ac77SMichael Reed 	unsigned long flags;
138180d3ac77SMichael Reed 	int rc=1;
138280d3ac77SMichael Reed 
1383ffb7fef3SKashyap, Desai 	if (ioc->bus_type != FC)
1384ffb7fef3SKashyap, Desai 		return 0;
1385ffb7fef3SKashyap, Desai 
1386d6ecdd63SPrakash, Sathya 	devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
138780d3ac77SMichael Reed 			ioc->name, event));
138880d3ac77SMichael Reed 
138980d3ac77SMichael Reed 	if (ioc->sh == NULL ||
1390e7eae9f6SEric Moore 		((hd = shost_priv(ioc->sh)) == NULL))
139180d3ac77SMichael Reed 		return 1;
139280d3ac77SMichael Reed 
139380d3ac77SMichael Reed 	switch (event) {
139480d3ac77SMichael Reed 	case MPI_EVENT_RESCAN:
139580d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
139680d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
139780d3ac77SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
139880d3ac77SMichael Reed 				   &ioc->fc_rescan_work);
139980d3ac77SMichael Reed 		}
140080d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
140180d3ac77SMichael Reed 		break;
1402eb5329f4SPrakash, Sathya 	case MPI_EVENT_LINK_STATUS_CHANGE:
1403eb5329f4SPrakash, Sathya 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1404eb5329f4SPrakash, Sathya 		if (ioc->fc_rescan_work_q) {
1405eb5329f4SPrakash, Sathya 			queue_work(ioc->fc_rescan_work_q,
1406eb5329f4SPrakash, Sathya 				   &ioc->fc_lsc_work);
1407eb5329f4SPrakash, Sathya 		}
1408eb5329f4SPrakash, Sathya 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
1409eb5329f4SPrakash, Sathya 		break;
141080d3ac77SMichael Reed 	default:
141180d3ac77SMichael Reed 		rc = mptscsih_event_process(ioc,pEvReply);
141280d3ac77SMichael Reed 		break;
141380d3ac77SMichael Reed 	}
141480d3ac77SMichael Reed 	return rc;
141580d3ac77SMichael Reed }
141680d3ac77SMichael Reed 
141780d3ac77SMichael Reed static int
141880d3ac77SMichael Reed mptfc_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
141980d3ac77SMichael Reed {
142080d3ac77SMichael Reed 	int		rc;
142180d3ac77SMichael Reed 	unsigned long	flags;
142280d3ac77SMichael Reed 
142380d3ac77SMichael Reed 	rc = mptscsih_ioc_reset(ioc,reset_phase);
1424ffb7fef3SKashyap, Desai 	if ((ioc->bus_type != FC) || (!rc))
142580d3ac77SMichael Reed 		return rc;
142680d3ac77SMichael Reed 
142780d3ac77SMichael Reed 
1428d6ecdd63SPrakash, Sathya 	dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
1429d6ecdd63SPrakash, Sathya 		": IOC %s_reset routed to FC host driver!\n",ioc->name,
143080d3ac77SMichael Reed 		reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
143180d3ac77SMichael Reed 		reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
143280d3ac77SMichael Reed 
143380d3ac77SMichael Reed 	if (reset_phase == MPT_IOC_SETUP_RESET) {
1434419835e2SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1435419835e2SMichael Reed 		if (ioc->fc_rescan_work_q) {
1436419835e2SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
1437419835e2SMichael Reed 				   &ioc->fc_setup_reset_work);
1438419835e2SMichael Reed 		}
1439419835e2SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
144080d3ac77SMichael Reed 	}
144180d3ac77SMichael Reed 
144280d3ac77SMichael Reed 	else if (reset_phase == MPT_IOC_PRE_RESET) {
144380d3ac77SMichael Reed 	}
144480d3ac77SMichael Reed 
144580d3ac77SMichael Reed 	else {	/* MPT_IOC_POST_RESET */
1446ca2f938eSMichael Reed 		mptfc_SetFcPortPage1_defaults(ioc);
144780d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
144880d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
144980d3ac77SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
145080d3ac77SMichael Reed 				   &ioc->fc_rescan_work);
145180d3ac77SMichael Reed 		}
145280d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
145380d3ac77SMichael Reed 	}
145480d3ac77SMichael Reed 	return 1;
145580d3ac77SMichael Reed }
145680d3ac77SMichael Reed 
14572496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14582496af39SMoore, Eric Dean  /**
1459d9489fb6SRandy Dunlap  *	mptfc_init - Register MPT adapter(s) as SCSI host(s) with SCSI mid-layer.
14602496af39SMoore, Eric Dean   *
14612496af39SMoore, Eric Dean   *	Returns 0 for success, non-zero for failure.
14622496af39SMoore, Eric Dean   */
14632496af39SMoore, Eric Dean  static int __init
14642496af39SMoore, Eric Dean  mptfc_init(void)
14652496af39SMoore, Eric Dean  {
146605e8ec17SMichael Reed 	int error;
14672496af39SMoore, Eric Dean  
14682496af39SMoore, Eric Dean  	show_mptmod_ver(my_NAME, my_VERSION);
14692496af39SMoore, Eric Dean  
1470ca2f938eSMichael Reed 	/* sanity check module parameters */
1471ca2f938eSMichael Reed 	if (mptfc_dev_loss_tmo <= 0)
147205e8ec17SMichael Reed 		mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
147305e8ec17SMichael Reed 
147405e8ec17SMichael Reed 	mptfc_transport_template =
147505e8ec17SMichael Reed 		fc_attach_transport(&mptfc_transport_functions);
147605e8ec17SMichael Reed 
147705e8ec17SMichael Reed 	if (!mptfc_transport_template)
147805e8ec17SMichael Reed 		return -ENODEV;
147905e8ec17SMichael Reed 
1480213aaca3SKashyap, Desai 	mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER,
1481213aaca3SKashyap, Desai 	    "mptscsih_scandv_complete");
1482213aaca3SKashyap, Desai 	mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER,
1483213aaca3SKashyap, Desai 	    "mptscsih_scandv_complete");
1484213aaca3SKashyap, Desai 	mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER,
1485213aaca3SKashyap, Desai 	    "mptscsih_scandv_complete");
14862496af39SMoore, Eric Dean  
1487d6ecdd63SPrakash, Sathya 	mpt_event_register(mptfcDoneCtx, mptfc_event_process);
1488d6ecdd63SPrakash, Sathya 	mpt_reset_register(mptfcDoneCtx, mptfc_ioc_reset);
14892496af39SMoore, Eric Dean  
149005e8ec17SMichael Reed 	error = pci_register_driver(&mptfc_driver);
14913bc7bf1dSMichael Reed 	if (error)
149205e8ec17SMichael Reed 		fc_release_transport(mptfc_transport_template);
149305e8ec17SMichael Reed 
149405e8ec17SMichael Reed 	return error;
149505e8ec17SMichael Reed }
149605e8ec17SMichael Reed 
149705e8ec17SMichael Reed /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
149805e8ec17SMichael Reed /**
1499d9489fb6SRandy Dunlap  *	mptfc_remove - Remove fc infrastructure for devices
150005e8ec17SMichael Reed  *	@pdev: Pointer to pci_dev structure
150105e8ec17SMichael Reed  *
150205e8ec17SMichael Reed  */
150347b1ea75SGreg Kroah-Hartman static void mptfc_remove(struct pci_dev *pdev)
150405e8ec17SMichael Reed {
150505e8ec17SMichael Reed 	MPT_ADAPTER		*ioc = pci_get_drvdata(pdev);
150605e8ec17SMichael Reed 	struct mptfc_rport_info	*p, *n;
150765207fedSMoore, Eric 	struct workqueue_struct *work_q;
150865207fedSMoore, Eric 	unsigned long		flags;
1509ca2f938eSMichael Reed 	int			ii;
151065207fedSMoore, Eric 
151165207fedSMoore, Eric 	/* destroy workqueue */
151265207fedSMoore, Eric 	if ((work_q=ioc->fc_rescan_work_q)) {
151365207fedSMoore, Eric 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
151465207fedSMoore, Eric 		ioc->fc_rescan_work_q = NULL;
151565207fedSMoore, Eric 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
151665207fedSMoore, Eric 		destroy_workqueue(work_q);
151765207fedSMoore, Eric 	}
151805e8ec17SMichael Reed 
151905e8ec17SMichael Reed 	fc_remove_host(ioc->sh);
152005e8ec17SMichael Reed 
152105e8ec17SMichael Reed 	list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
152205e8ec17SMichael Reed 		list_del(&p->list);
152305e8ec17SMichael Reed 		kfree(p);
152405e8ec17SMichael Reed 	}
152505e8ec17SMichael Reed 
1526ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
1527ca2f938eSMichael Reed 		if (ioc->fc_data.fc_port_page1[ii].data) {
1528ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev,
1529ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].pg_sz,
1530ca2f938eSMichael Reed 				(u8 *) ioc->fc_data.fc_port_page1[ii].data,
1531ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].dma);
1532ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[ii].data = NULL;
1533ca2f938eSMichael Reed 		}
1534ca2f938eSMichael Reed 	}
1535ca2f938eSMichael Reed 
1536*cfd2aff7SHannes Reinecke 	scsi_remove_host(ioc->sh);
1537*cfd2aff7SHannes Reinecke 
153805e8ec17SMichael Reed 	mptscsih_remove(pdev);
15392496af39SMoore, Eric Dean  }
15402496af39SMoore, Eric Dean  
15412496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
15422496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
15432496af39SMoore, Eric Dean  /**
15442496af39SMoore, Eric Dean   *	mptfc_exit - Unregisters MPT adapter(s)
15452496af39SMoore, Eric Dean   *
15462496af39SMoore, Eric Dean   */
15472496af39SMoore, Eric Dean  static void __exit
15482496af39SMoore, Eric Dean  mptfc_exit(void)
15492496af39SMoore, Eric Dean  {
15502496af39SMoore, Eric Dean  	pci_unregister_driver(&mptfc_driver);
155105e8ec17SMichael Reed 	fc_release_transport(mptfc_transport_template);
15522496af39SMoore, Eric Dean  
15532496af39SMoore, Eric Dean  	mpt_reset_deregister(mptfcDoneCtx);
15542496af39SMoore, Eric Dean  	mpt_event_deregister(mptfcDoneCtx);
15552496af39SMoore, Eric Dean  
15562496af39SMoore, Eric Dean  	mpt_deregister(mptfcInternalCtx);
15572496af39SMoore, Eric Dean  	mpt_deregister(mptfcTaskCtx);
15582496af39SMoore, Eric Dean  	mpt_deregister(mptfcDoneCtx);
15592496af39SMoore, Eric Dean  }
15602496af39SMoore, Eric Dean  
15612496af39SMoore, Eric Dean  module_init(mptfc_init);
15622496af39SMoore, Eric Dean  module_exit(mptfc_exit);
1563