xref: /openbmc/linux/drivers/message/fusion/mptfc.c (revision 5d947f2b7607c4674d104accbd3768744aaa4154)
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   *
62496af39SMoore, Eric Dean   *  Copyright (c) 1999-2005 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");
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 
882496af39SMoore, Eric Dean  static int	mptfcDoneCtx = -1;
892496af39SMoore, Eric Dean  static int	mptfcTaskCtx = -1;
902496af39SMoore, Eric Dean  static int	mptfcInternalCtx = -1; /* Used only for internal commands */
912496af39SMoore, Eric Dean  
923bc7bf1dSMichael Reed static int mptfc_target_alloc(struct scsi_target *starget);
933bc7bf1dSMichael Reed static int mptfc_slave_alloc(struct scsi_device *sdev);
9405e8ec17SMichael Reed static int mptfc_qcmd(struct scsi_cmnd *SCpnt,
9505e8ec17SMichael Reed 		      void (*done)(struct scsi_cmnd *));
963bc7bf1dSMichael Reed static void mptfc_target_destroy(struct scsi_target *starget);
9705e8ec17SMichael Reed static void mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout);
9805e8ec17SMichael Reed static void __devexit mptfc_remove(struct pci_dev *pdev);
9905e8ec17SMichael Reed 
1002496af39SMoore, Eric Dean  static struct scsi_host_template mptfc_driver_template = {
101f78496daSMoore, Eric Dean 	.module				= THIS_MODULE,
1022496af39SMoore, Eric Dean  	.proc_name			= "mptfc",
1032496af39SMoore, Eric Dean  	.proc_info			= mptscsih_proc_info,
1042496af39SMoore, Eric Dean  	.name				= "MPT FC Host",
1052496af39SMoore, Eric Dean  	.info				= mptscsih_info,
10605e8ec17SMichael Reed 	.queuecommand			= mptfc_qcmd,
1073bc7bf1dSMichael Reed 	.target_alloc			= mptfc_target_alloc,
10805e8ec17SMichael Reed 	.slave_alloc			= mptfc_slave_alloc,
1092496af39SMoore, Eric Dean  	.slave_configure		= mptscsih_slave_configure,
1103bc7bf1dSMichael Reed 	.target_destroy			= mptfc_target_destroy,
1112496af39SMoore, Eric Dean  	.slave_destroy			= mptscsih_slave_destroy,
1126e3815baSMoore, Eric Dean 	.change_queue_depth 		= mptscsih_change_queue_depth,
1132496af39SMoore, Eric Dean  	.eh_abort_handler		= mptscsih_abort,
1142496af39SMoore, Eric Dean  	.eh_device_reset_handler	= mptscsih_dev_reset,
1152496af39SMoore, Eric Dean  	.eh_bus_reset_handler		= mptscsih_bus_reset,
1162496af39SMoore, Eric Dean  	.eh_host_reset_handler		= mptscsih_host_reset,
1172496af39SMoore, Eric Dean  	.bios_param			= mptscsih_bios_param,
1182496af39SMoore, Eric Dean  	.can_queue			= MPT_FC_CAN_QUEUE,
1192496af39SMoore, Eric Dean  	.this_id			= -1,
1202496af39SMoore, Eric Dean  	.sg_tablesize			= MPT_SCSI_SG_DEPTH,
1212496af39SMoore, Eric Dean  	.max_sectors			= 8192,
1222496af39SMoore, Eric Dean  	.cmd_per_lun			= 7,
1232496af39SMoore, Eric Dean  	.use_clustering			= ENABLE_CLUSTERING,
1242496af39SMoore, Eric Dean  };
1252496af39SMoore, Eric Dean  
1262496af39SMoore, Eric Dean  /****************************************************************************
1272496af39SMoore, Eric Dean   * Supported hardware
1282496af39SMoore, Eric Dean   */
1292496af39SMoore, Eric Dean  
1302496af39SMoore, Eric Dean  static struct pci_device_id mptfc_pci_table[] = {
13187cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC909,
1322496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
13387cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919,
1342496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
13587cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929,
1362496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
13787cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919X,
1382496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
13987cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929X,
1402496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14187cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC939X,
1423fadc59dSMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14387cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949X,
1443fadc59dSMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14587cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949E,
1466d5b0c31SMoore, Eric 		PCI_ANY_ID, PCI_ANY_ID },
1472496af39SMoore, Eric Dean  	{0}	/* Terminating entry */
1482496af39SMoore, Eric Dean  };
1492496af39SMoore, Eric Dean  MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
1502496af39SMoore, Eric Dean  
15105e8ec17SMichael Reed static struct scsi_transport_template *mptfc_transport_template = NULL;
15205e8ec17SMichael Reed 
15303fbcbcdSAdrian Bunk static struct fc_function_template mptfc_transport_functions = {
15405e8ec17SMichael Reed 	.dd_fcrport_size = 8,
15505e8ec17SMichael Reed 	.show_host_node_name = 1,
15605e8ec17SMichael Reed 	.show_host_port_name = 1,
15705e8ec17SMichael Reed 	.show_host_supported_classes = 1,
15805e8ec17SMichael Reed 	.show_host_port_id = 1,
15905e8ec17SMichael Reed 	.show_rport_supported_classes = 1,
16005e8ec17SMichael Reed 	.show_starget_node_name = 1,
16105e8ec17SMichael Reed 	.show_starget_port_name = 1,
16205e8ec17SMichael Reed 	.show_starget_port_id = 1,
16305e8ec17SMichael Reed 	.set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
16405e8ec17SMichael Reed 	.show_rport_dev_loss_tmo = 1,
165*5d947f2bSMichael Reed 	.show_host_supported_speeds = 1,
166*5d947f2bSMichael Reed 	.show_host_maxframe_size = 1,
167*5d947f2bSMichael Reed 	.show_host_speed = 1,
168*5d947f2bSMichael Reed 	.show_host_fabric_name = 1,
169*5d947f2bSMichael Reed 	.show_host_port_type = 1,
170*5d947f2bSMichael Reed 	.show_host_port_state = 1,
171*5d947f2bSMichael Reed 	.show_host_symbolic_name = 1,
17205e8ec17SMichael Reed };
17305e8ec17SMichael Reed 
17405e8ec17SMichael Reed static void
17505e8ec17SMichael Reed mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
17605e8ec17SMichael Reed {
17705e8ec17SMichael Reed 	if (timeout > 0)
17805e8ec17SMichael Reed 		rport->dev_loss_tmo = timeout;
17905e8ec17SMichael Reed 	else
18005e8ec17SMichael Reed 		rport->dev_loss_tmo = mptfc_dev_loss_tmo;
18105e8ec17SMichael Reed }
18205e8ec17SMichael Reed 
18305e8ec17SMichael Reed static int
18405e8ec17SMichael Reed mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
18505e8ec17SMichael Reed {
18605e8ec17SMichael Reed 	FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
18705e8ec17SMichael Reed 	FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
18805e8ec17SMichael Reed 
18905e8ec17SMichael Reed 	if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
19005e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
19105e8ec17SMichael Reed 			return 0;
19205e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
19305e8ec17SMichael Reed 			return -1;
19405e8ec17SMichael Reed 		return 1;
19505e8ec17SMichael Reed 	}
19605e8ec17SMichael Reed 	if ((*aa)->CurrentBus < (*bb)->CurrentBus)
19705e8ec17SMichael Reed 		return -1;
19805e8ec17SMichael Reed 	return 1;
19905e8ec17SMichael Reed }
20005e8ec17SMichael Reed 
20105e8ec17SMichael Reed static int
20205e8ec17SMichael Reed mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
20305e8ec17SMichael Reed 	void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
20405e8ec17SMichael Reed {
20505e8ec17SMichael Reed 	ConfigPageHeader_t	 hdr;
20605e8ec17SMichael Reed 	CONFIGPARMS		 cfg;
20705e8ec17SMichael Reed 	FCDevicePage0_t		*ppage0_alloc, *fc;
20805e8ec17SMichael Reed 	dma_addr_t		 page0_dma;
20905e8ec17SMichael Reed 	int			 data_sz;
21005e8ec17SMichael Reed 	int			 ii;
21105e8ec17SMichael Reed 
21205e8ec17SMichael Reed 	FCDevicePage0_t		*p0_array=NULL, *p_p0;
21305e8ec17SMichael Reed 	FCDevicePage0_t		**pp0_array=NULL, **p_pp0;
21405e8ec17SMichael Reed 
21505e8ec17SMichael Reed 	int			 rc = -ENOMEM;
21605e8ec17SMichael Reed 	U32			 port_id = 0xffffff;
21705e8ec17SMichael Reed 	int			 num_targ = 0;
21805e8ec17SMichael Reed 	int			 max_bus = ioc->facts.MaxBuses;
21905e8ec17SMichael Reed 	int			 max_targ = ioc->facts.MaxDevices;
22005e8ec17SMichael Reed 
22105e8ec17SMichael Reed 	if (max_bus == 0 || max_targ == 0)
22205e8ec17SMichael Reed 		goto out;
22305e8ec17SMichael Reed 
22405e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
22505e8ec17SMichael Reed 	p_p0 = p0_array =  kzalloc(data_sz, GFP_KERNEL);
22605e8ec17SMichael Reed 	if (!p0_array)
22705e8ec17SMichael Reed 		goto out;
22805e8ec17SMichael Reed 
22905e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
23005e8ec17SMichael Reed 	p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
23105e8ec17SMichael Reed 	if (!pp0_array)
23205e8ec17SMichael Reed 		goto out;
23305e8ec17SMichael Reed 
23405e8ec17SMichael Reed 	do {
23505e8ec17SMichael Reed 		/* Get FC Device Page 0 header */
23605e8ec17SMichael Reed 		hdr.PageVersion = 0;
23705e8ec17SMichael Reed 		hdr.PageLength = 0;
23805e8ec17SMichael Reed 		hdr.PageNumber = 0;
23905e8ec17SMichael Reed 		hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
24005e8ec17SMichael Reed 		cfg.cfghdr.hdr = &hdr;
24105e8ec17SMichael Reed 		cfg.physAddr = -1;
24205e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
24305e8ec17SMichael Reed 		cfg.dir = 0;
24405e8ec17SMichael Reed 		cfg.pageAddr = port_id;
24505e8ec17SMichael Reed 		cfg.timeout = 0;
24605e8ec17SMichael Reed 
24705e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) != 0)
24805e8ec17SMichael Reed 			break;
24905e8ec17SMichael Reed 
25005e8ec17SMichael Reed 		if (hdr.PageLength <= 0)
25105e8ec17SMichael Reed 			break;
25205e8ec17SMichael Reed 
25305e8ec17SMichael Reed 		data_sz = hdr.PageLength * 4;
25405e8ec17SMichael Reed 		ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
25505e8ec17SMichael Reed 		    					&page0_dma);
25605e8ec17SMichael Reed 		rc = -ENOMEM;
25705e8ec17SMichael Reed 		if (!ppage0_alloc)
25805e8ec17SMichael Reed 			break;
25905e8ec17SMichael Reed 
26005e8ec17SMichael Reed 		cfg.physAddr = page0_dma;
26105e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
26205e8ec17SMichael Reed 
26305e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
26405e8ec17SMichael Reed 			ppage0_alloc->PortIdentifier =
26505e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->PortIdentifier);
26605e8ec17SMichael Reed 
26705e8ec17SMichael Reed 			ppage0_alloc->WWNN.Low =
26805e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.Low);
26905e8ec17SMichael Reed 
27005e8ec17SMichael Reed 			ppage0_alloc->WWNN.High =
27105e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.High);
27205e8ec17SMichael Reed 
27305e8ec17SMichael Reed 			ppage0_alloc->WWPN.Low =
27405e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.Low);
27505e8ec17SMichael Reed 
27605e8ec17SMichael Reed 			ppage0_alloc->WWPN.High =
27705e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.High);
27805e8ec17SMichael Reed 
27905e8ec17SMichael Reed 			ppage0_alloc->BBCredit =
28005e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->BBCredit);
28105e8ec17SMichael Reed 
28205e8ec17SMichael Reed 			ppage0_alloc->MaxRxFrameSize =
28305e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
28405e8ec17SMichael Reed 
28505e8ec17SMichael Reed 			port_id = ppage0_alloc->PortIdentifier;
28605e8ec17SMichael Reed 			num_targ++;
28705e8ec17SMichael Reed 			*p_p0 = *ppage0_alloc;	/* save data */
28805e8ec17SMichael Reed 			*p_pp0++ = p_p0++;	/* save addr */
28905e8ec17SMichael Reed 		}
29005e8ec17SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz,
29105e8ec17SMichael Reed 		    			(u8 *) ppage0_alloc, page0_dma);
29205e8ec17SMichael Reed 		if (rc != 0)
29305e8ec17SMichael Reed 			break;
29405e8ec17SMichael Reed 
29505e8ec17SMichael Reed 	} while (port_id <= 0xff0000);
29605e8ec17SMichael Reed 
29705e8ec17SMichael Reed 	if (num_targ) {
29805e8ec17SMichael Reed 		/* sort array */
29905e8ec17SMichael Reed 		if (num_targ > 1)
30005e8ec17SMichael Reed 			sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
30105e8ec17SMichael Reed 				mptfc_FcDevPage0_cmp_func, NULL);
30205e8ec17SMichael Reed 		/* call caller's func for each targ */
30305e8ec17SMichael Reed 		for (ii = 0; ii < num_targ;  ii++) {
30405e8ec17SMichael Reed 			fc = *(pp0_array+ii);
30505e8ec17SMichael Reed 			func(ioc, ioc_port, fc);
30605e8ec17SMichael Reed 		}
30705e8ec17SMichael Reed 	}
30805e8ec17SMichael Reed 
30905e8ec17SMichael Reed  out:
31005e8ec17SMichael Reed 	kfree(pp0_array);
31105e8ec17SMichael Reed 	kfree(p0_array);
31205e8ec17SMichael Reed 	return rc;
31305e8ec17SMichael Reed }
31405e8ec17SMichael Reed 
31505e8ec17SMichael Reed static int
31605e8ec17SMichael Reed mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
31705e8ec17SMichael Reed {
31805e8ec17SMichael Reed 	/* not currently usable */
31905e8ec17SMichael Reed 	if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
32005e8ec17SMichael Reed 			  MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
32105e8ec17SMichael Reed 		return -1;
32205e8ec17SMichael Reed 
32305e8ec17SMichael Reed 	if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
32405e8ec17SMichael Reed 		return -1;
32505e8ec17SMichael Reed 
32605e8ec17SMichael Reed 	if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
32705e8ec17SMichael Reed 		return -1;
32805e8ec17SMichael Reed 
32905e8ec17SMichael Reed 	/*
33005e8ec17SMichael Reed 	 * board data structure already normalized to platform endianness
33105e8ec17SMichael Reed 	 * shifted to avoid unaligned access on 64 bit architecture
33205e8ec17SMichael Reed 	 */
33305e8ec17SMichael Reed 	rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
33405e8ec17SMichael Reed 	rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
33505e8ec17SMichael Reed 	rid->port_id =   pg0->PortIdentifier;
33605e8ec17SMichael Reed 	rid->roles = FC_RPORT_ROLE_UNKNOWN;
33705e8ec17SMichael Reed 
33805e8ec17SMichael Reed 	return 0;
33905e8ec17SMichael Reed }
34005e8ec17SMichael Reed 
34105e8ec17SMichael Reed static void
34205e8ec17SMichael Reed mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
34305e8ec17SMichael Reed {
34405e8ec17SMichael Reed 	struct fc_rport_identifiers rport_ids;
34505e8ec17SMichael Reed 	struct fc_rport		*rport;
34605e8ec17SMichael Reed 	struct mptfc_rport_info	*ri;
3473bc7bf1dSMichael Reed 	int			new_ri = 1;
34865207fedSMoore, Eric 	u64			pn, nn;
3493bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
3506dd727daSmdr@sgi.com 	u32			roles = FC_RPORT_ROLE_UNKNOWN;
35105e8ec17SMichael Reed 
35205e8ec17SMichael Reed 	if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
35305e8ec17SMichael Reed 		return;
35405e8ec17SMichael Reed 
3556dd727daSmdr@sgi.com 	roles |= FC_RPORT_ROLE_FCP_TARGET;
3566dd727daSmdr@sgi.com 	if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
3576dd727daSmdr@sgi.com 		roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3586dd727daSmdr@sgi.com 
35905e8ec17SMichael Reed 	/* scan list looking for a match */
36005e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
3613bc7bf1dSMichael Reed 		pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
3623bc7bf1dSMichael Reed 		if (pn == rport_ids.port_name) {	/* match */
36305e8ec17SMichael Reed 			list_move_tail(&ri->list, &ioc->fc_rports);
3643bc7bf1dSMichael Reed 			new_ri = 0;
36505e8ec17SMichael Reed 			break;
36605e8ec17SMichael Reed 		}
36705e8ec17SMichael Reed 	}
3683bc7bf1dSMichael Reed 	if (new_ri) {	/* allocate one */
36905e8ec17SMichael Reed 		ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
37005e8ec17SMichael Reed 		if (!ri)
37105e8ec17SMichael Reed 			return;
37205e8ec17SMichael Reed 		list_add_tail(&ri->list, &ioc->fc_rports);
37305e8ec17SMichael Reed 	}
37405e8ec17SMichael Reed 
37505e8ec17SMichael Reed 	ri->pg0 = *pg0;	/* add/update pg0 data */
37605e8ec17SMichael Reed 	ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
37705e8ec17SMichael Reed 
3783bc7bf1dSMichael Reed 	/* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
37905e8ec17SMichael Reed 	if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
38005e8ec17SMichael Reed 		ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
38105e8ec17SMichael Reed 		rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
38205e8ec17SMichael Reed 		if (rport) {
38305e8ec17SMichael Reed 			ri->rport = rport;
3843bc7bf1dSMichael Reed 			if (new_ri) /* may have been reset by user */
38505e8ec17SMichael Reed 				rport->dev_loss_tmo = mptfc_dev_loss_tmo;
38605e8ec17SMichael Reed 			/*
38705e8ec17SMichael Reed 			 * if already mapped, remap here.  If not mapped,
3883bc7bf1dSMichael Reed 			 * target_alloc will allocate vtarget and map,
3893bc7bf1dSMichael Reed 			 * slave_alloc will fill in vdev from vtarget.
39005e8ec17SMichael Reed 			 */
3913bc7bf1dSMichael Reed 			if (ri->starget) {
3923bc7bf1dSMichael Reed 				vtarget = ri->starget->hostdata;
3933bc7bf1dSMichael Reed 				if (vtarget) {
3943bc7bf1dSMichael Reed 					vtarget->target_id = pg0->CurrentTargetID;
3953bc7bf1dSMichael Reed 					vtarget->bus_id = pg0->CurrentBus;
39605e8ec17SMichael Reed 				}
3973bc7bf1dSMichael Reed 			}
39865207fedSMoore, Eric 			*((struct mptfc_rport_info **)rport->dd_data) = ri;
3996dd727daSmdr@sgi.com 			/* scan will be scheduled once rport becomes a target */
4006dd727daSmdr@sgi.com 			fc_remote_port_rolechg(rport,roles);
40165207fedSMoore, Eric 
40265207fedSMoore, Eric 			pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
40365207fedSMoore, Eric 			nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
4043bc7bf1dSMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
4053bc7bf1dSMichael Reed 				"mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
40605e8ec17SMichael Reed 				"rport tid %d, tmo %d\n",
4073bc7bf1dSMichael Reed 					ioc->name,
408914c2d8eSMoore, Eric 					ioc->sh->host_no,
40905e8ec17SMichael Reed 					pg0->PortIdentifier,
41065207fedSMoore, Eric 					(unsigned long long)nn,
41165207fedSMoore, Eric 					(unsigned long long)pn,
41205e8ec17SMichael Reed 					pg0->CurrentTargetID,
41305e8ec17SMichael Reed 					ri->rport->scsi_target_id,
4143bc7bf1dSMichael Reed 					ri->rport->dev_loss_tmo));
41505e8ec17SMichael Reed 		} else {
41605e8ec17SMichael Reed 			list_del(&ri->list);
41705e8ec17SMichael Reed 			kfree(ri);
41805e8ec17SMichael Reed 			ri = NULL;
41905e8ec17SMichael Reed 		}
42005e8ec17SMichael Reed 	}
42105e8ec17SMichael Reed }
42205e8ec17SMichael Reed 
42305e8ec17SMichael Reed /*
4243bc7bf1dSMichael Reed  *	OS entry point to allow for host driver to free allocated memory
4253bc7bf1dSMichael Reed  *	Called if no device present or device being unloaded
4263bc7bf1dSMichael Reed  */
4273bc7bf1dSMichael Reed static void
4283bc7bf1dSMichael Reed mptfc_target_destroy(struct scsi_target *starget)
4293bc7bf1dSMichael Reed {
4303bc7bf1dSMichael Reed 	struct fc_rport		*rport;
4313bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
4323bc7bf1dSMichael Reed 
4333bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
4343bc7bf1dSMichael Reed 	if (rport) {
4353bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
4363bc7bf1dSMichael Reed 		if (ri)	/* better be! */
4373bc7bf1dSMichael Reed 			ri->starget = NULL;
4383bc7bf1dSMichael Reed 	}
4393bc7bf1dSMichael Reed 	if (starget->hostdata)
4403bc7bf1dSMichael Reed 		kfree(starget->hostdata);
4413bc7bf1dSMichael Reed 	starget->hostdata = NULL;
4423bc7bf1dSMichael Reed }
4433bc7bf1dSMichael Reed 
4443bc7bf1dSMichael Reed /*
4453bc7bf1dSMichael Reed  *	OS entry point to allow host driver to alloc memory
4463bc7bf1dSMichael Reed  *	for each scsi target. Called once per device the bus scan.
4473bc7bf1dSMichael Reed  *	Return non-zero if allocation fails.
4483bc7bf1dSMichael Reed  */
4493bc7bf1dSMichael Reed static int
4503bc7bf1dSMichael Reed mptfc_target_alloc(struct scsi_target *starget)
4513bc7bf1dSMichael Reed {
4523bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
4533bc7bf1dSMichael Reed 	struct fc_rport		*rport;
4543bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
4553bc7bf1dSMichael Reed 	int			rc;
4563bc7bf1dSMichael Reed 
4573bc7bf1dSMichael Reed 	vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
4583bc7bf1dSMichael Reed 	if (!vtarget)
4593bc7bf1dSMichael Reed 		return -ENOMEM;
4603bc7bf1dSMichael Reed 	starget->hostdata = vtarget;
4613bc7bf1dSMichael Reed 
4623bc7bf1dSMichael Reed 	rc = -ENODEV;
4633bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
4643bc7bf1dSMichael Reed 	if (rport) {
4653bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
4663bc7bf1dSMichael Reed 		if (ri) {	/* better be! */
4673bc7bf1dSMichael Reed 			vtarget->target_id = ri->pg0.CurrentTargetID;
4683bc7bf1dSMichael Reed 			vtarget->bus_id = ri->pg0.CurrentBus;
4693bc7bf1dSMichael Reed 			ri->starget = starget;
4703bc7bf1dSMichael Reed 			rc = 0;
4713bc7bf1dSMichael Reed 		}
4723bc7bf1dSMichael Reed 	}
4733bc7bf1dSMichael Reed 	if (rc != 0) {
4743bc7bf1dSMichael Reed 		kfree(vtarget);
4753bc7bf1dSMichael Reed 		starget->hostdata = NULL;
4763bc7bf1dSMichael Reed 	}
4773bc7bf1dSMichael Reed 
4783bc7bf1dSMichael Reed 	return rc;
4793bc7bf1dSMichael Reed }
4803bc7bf1dSMichael Reed 
4813bc7bf1dSMichael Reed /*
48205e8ec17SMichael Reed  *	OS entry point to allow host driver to alloc memory
48305e8ec17SMichael Reed  *	for each scsi device. Called once per device the bus scan.
48405e8ec17SMichael Reed  *	Return non-zero if allocation fails.
48505e8ec17SMichael Reed  *	Init memory once per LUN.
48605e8ec17SMichael Reed  */
48703fbcbcdSAdrian Bunk static int
48805e8ec17SMichael Reed mptfc_slave_alloc(struct scsi_device *sdev)
48905e8ec17SMichael Reed {
49005e8ec17SMichael Reed 	MPT_SCSI_HOST		*hd;
49105e8ec17SMichael Reed 	VirtTarget		*vtarget;
49205e8ec17SMichael Reed 	VirtDevice		*vdev;
49305e8ec17SMichael Reed 	struct scsi_target	*starget;
49405e8ec17SMichael Reed 	struct fc_rport		*rport;
49505e8ec17SMichael Reed 
49605e8ec17SMichael Reed 
49765207fedSMoore, Eric 	starget = scsi_target(sdev);
49865207fedSMoore, Eric 	rport = starget_to_rport(starget);
49905e8ec17SMichael Reed 
50005e8ec17SMichael Reed 	if (!rport || fc_remote_port_chkready(rport))
50105e8ec17SMichael Reed 		return -ENXIO;
50205e8ec17SMichael Reed 
50305e8ec17SMichael Reed 	hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
50405e8ec17SMichael Reed 
5053bc7bf1dSMichael Reed 	vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
50605e8ec17SMichael Reed 	if (!vdev) {
50705e8ec17SMichael Reed 		printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
50805e8ec17SMichael Reed 				hd->ioc->name, sizeof(VirtDevice));
50905e8ec17SMichael Reed 		return -ENOMEM;
51005e8ec17SMichael Reed 	}
51105e8ec17SMichael Reed 
51205e8ec17SMichael Reed 
51305e8ec17SMichael Reed 	sdev->hostdata = vdev;
51405e8ec17SMichael Reed 	vtarget = starget->hostdata;
5153bc7bf1dSMichael Reed 
51605e8ec17SMichael Reed 	if (vtarget->num_luns == 0) {
5173bc7bf1dSMichael Reed 		vtarget->ioc_id = hd->ioc->id;
518ba856d32SEric Moore 		vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
51905e8ec17SMichael Reed 		hd->Targets[sdev->id] = vtarget;
52005e8ec17SMichael Reed 	}
52105e8ec17SMichael Reed 
52205e8ec17SMichael Reed 	vdev->vtarget = vtarget;
52305e8ec17SMichael Reed 	vdev->lun = sdev->lun;
52405e8ec17SMichael Reed 
52505e8ec17SMichael Reed 	vtarget->num_luns++;
52605e8ec17SMichael Reed 
52765207fedSMoore, Eric 
528914c2d8eSMoore, Eric #ifdef DMPT_DEBUG_FC
529914c2d8eSMoore, Eric 	{
53065207fedSMoore, Eric 	u64 nn, pn;
531914c2d8eSMoore, Eric 	struct mptfc_rport_info *ri;
532914c2d8eSMoore, Eric 	ri = *((struct mptfc_rport_info **)rport->dd_data);
53365207fedSMoore, Eric 	pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
53465207fedSMoore, Eric 	nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
5353bc7bf1dSMichael Reed 	dfcprintk ((MYIOC_s_INFO_FMT
5363bc7bf1dSMichael Reed 		"mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
53705e8ec17SMichael Reed 	        "CurrentTargetID %d, %x %llx %llx\n",
538914c2d8eSMoore, Eric 		hd->ioc->name,
53905e8ec17SMichael Reed 		sdev->host->host_no,
54005e8ec17SMichael Reed 		vtarget->num_luns,
54105e8ec17SMichael Reed 		sdev->id, ri->pg0.CurrentTargetID,
54265207fedSMoore, Eric 		ri->pg0.PortIdentifier,
54365207fedSMoore, Eric 		(unsigned long long)pn,
54465207fedSMoore, Eric 		(unsigned long long)nn));
545914c2d8eSMoore, Eric 	}
546914c2d8eSMoore, Eric #endif
54705e8ec17SMichael Reed 
54805e8ec17SMichael Reed 	return 0;
54905e8ec17SMichael Reed }
55005e8ec17SMichael Reed 
55105e8ec17SMichael Reed static int
55205e8ec17SMichael Reed mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
55305e8ec17SMichael Reed {
5543bc7bf1dSMichael Reed 	struct mptfc_rport_info	*ri;
55505e8ec17SMichael Reed 	struct fc_rport	*rport = starget_to_rport(scsi_target(SCpnt->device));
55605e8ec17SMichael Reed 	int		err;
55705e8ec17SMichael Reed 
55805e8ec17SMichael Reed 	err = fc_remote_port_chkready(rport);
55905e8ec17SMichael Reed 	if (unlikely(err)) {
56005e8ec17SMichael Reed 		SCpnt->result = err;
56105e8ec17SMichael Reed 		done(SCpnt);
56205e8ec17SMichael Reed 		return 0;
56305e8ec17SMichael Reed 	}
5643bc7bf1dSMichael Reed 
56565207fedSMoore, Eric 	/* dd_data is null until finished adding target */
56665207fedSMoore, Eric 	ri = *((struct mptfc_rport_info **)rport->dd_data);
56765207fedSMoore, Eric 	if (unlikely(!ri)) {
56865207fedSMoore, Eric 		dfcprintk ((MYIOC_s_INFO_FMT
56965207fedSMoore, Eric 			"mptfc_qcmd.%d: %d:%d, dd_data is null.\n",
57065207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
57165207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
57265207fedSMoore, Eric 			SCpnt->device->id,SCpnt->device->lun));
57365207fedSMoore, Eric 		SCpnt->result = DID_IMM_RETRY << 16;
57465207fedSMoore, Eric 		done(SCpnt);
57565207fedSMoore, Eric 		return 0;
57665207fedSMoore, Eric 	}
57765207fedSMoore, Eric 
57865207fedSMoore, Eric 	err = mptscsih_qcmd(SCpnt,done);
57965207fedSMoore, Eric #ifdef DMPT_DEBUG_FC
58065207fedSMoore, Eric 	if (unlikely(err)) {
58165207fedSMoore, Eric 		dfcprintk ((MYIOC_s_INFO_FMT
582419835e2SMichael Reed 			"mptfc_qcmd.%d: %d:%d, mptscsih_qcmd returns non-zero, (%x).\n",
58365207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
58465207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
585419835e2SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,err));
58665207fedSMoore, Eric 	}
58765207fedSMoore, Eric #endif
58865207fedSMoore, Eric 	return err;
58905e8ec17SMichael Reed }
59005e8ec17SMichael Reed 
59180d3ac77SMichael Reed /*
59280d3ac77SMichael Reed  *	mptfc_GetFcPortPage0 - Fetch FCPort config Page0.
59380d3ac77SMichael Reed  *	@ioc: Pointer to MPT_ADAPTER structure
59480d3ac77SMichael Reed  *	@portnum: IOC Port number
59580d3ac77SMichael Reed  *
59680d3ac77SMichael Reed  *	Return: 0 for success
59780d3ac77SMichael Reed  *	-ENOMEM if no memory available
59880d3ac77SMichael Reed  *		-EPERM if not allowed due to ISR context
59980d3ac77SMichael Reed  *		-EAGAIN if no msg frames currently available
60080d3ac77SMichael Reed  *		-EFAULT for non-successful reply or no reply (timeout)
60180d3ac77SMichael Reed  *		-EINVAL portnum arg out of range (hardwired to two elements)
60280d3ac77SMichael Reed  */
60380d3ac77SMichael Reed static int
60480d3ac77SMichael Reed mptfc_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
60580d3ac77SMichael Reed {
60680d3ac77SMichael Reed 	ConfigPageHeader_t	 hdr;
60780d3ac77SMichael Reed 	CONFIGPARMS		 cfg;
60880d3ac77SMichael Reed 	FCPortPage0_t		*ppage0_alloc;
60980d3ac77SMichael Reed 	FCPortPage0_t		*pp0dest;
61080d3ac77SMichael Reed 	dma_addr_t		 page0_dma;
61180d3ac77SMichael Reed 	int			 data_sz;
61280d3ac77SMichael Reed 	int			 copy_sz;
61380d3ac77SMichael Reed 	int			 rc;
61480d3ac77SMichael Reed 	int			 count = 400;
61580d3ac77SMichael Reed 
61680d3ac77SMichael Reed 	if (portnum > 1)
61780d3ac77SMichael Reed 		return -EINVAL;
61880d3ac77SMichael Reed 
61980d3ac77SMichael Reed 	/* Get FCPort Page 0 header */
62080d3ac77SMichael Reed 	hdr.PageVersion = 0;
62180d3ac77SMichael Reed 	hdr.PageLength = 0;
62280d3ac77SMichael Reed 	hdr.PageNumber = 0;
62380d3ac77SMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
62480d3ac77SMichael Reed 	cfg.cfghdr.hdr = &hdr;
62580d3ac77SMichael Reed 	cfg.physAddr = -1;
62680d3ac77SMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
62780d3ac77SMichael Reed 	cfg.dir = 0;
62880d3ac77SMichael Reed 	cfg.pageAddr = portnum;
62980d3ac77SMichael Reed 	cfg.timeout = 0;
63080d3ac77SMichael Reed 
63180d3ac77SMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
63280d3ac77SMichael Reed 		return rc;
63380d3ac77SMichael Reed 
63480d3ac77SMichael Reed 	if (hdr.PageLength == 0)
63580d3ac77SMichael Reed 		return 0;
63680d3ac77SMichael Reed 
63780d3ac77SMichael Reed 	data_sz = hdr.PageLength * 4;
63880d3ac77SMichael Reed 	rc = -ENOMEM;
63980d3ac77SMichael Reed 	ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
64080d3ac77SMichael Reed 	if (ppage0_alloc) {
64180d3ac77SMichael Reed 
64280d3ac77SMichael Reed  try_again:
64380d3ac77SMichael Reed 		memset((u8 *)ppage0_alloc, 0, data_sz);
64480d3ac77SMichael Reed 		cfg.physAddr = page0_dma;
64580d3ac77SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
64680d3ac77SMichael Reed 
64780d3ac77SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
64880d3ac77SMichael Reed 			/* save the data */
64980d3ac77SMichael Reed 			pp0dest = &ioc->fc_port_page0[portnum];
65080d3ac77SMichael Reed 			copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
65180d3ac77SMichael Reed 			memcpy(pp0dest, ppage0_alloc, copy_sz);
65280d3ac77SMichael Reed 
65380d3ac77SMichael Reed 			/*
65480d3ac77SMichael Reed 			 *	Normalize endianness of structure data,
65580d3ac77SMichael Reed 			 *	by byte-swapping all > 1 byte fields!
65680d3ac77SMichael Reed 			 */
65780d3ac77SMichael Reed 			pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
65880d3ac77SMichael Reed 			pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
65980d3ac77SMichael Reed 			pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
66080d3ac77SMichael Reed 			pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
66180d3ac77SMichael Reed 			pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
66280d3ac77SMichael Reed 			pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
66380d3ac77SMichael Reed 			pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
66480d3ac77SMichael Reed 			pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
66580d3ac77SMichael Reed 			pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
66680d3ac77SMichael Reed 			pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
66780d3ac77SMichael Reed 			pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
66880d3ac77SMichael Reed 			pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
66980d3ac77SMichael Reed 			pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
67080d3ac77SMichael Reed 			pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
67180d3ac77SMichael Reed 			pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
67280d3ac77SMichael Reed 			pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
67380d3ac77SMichael Reed 
67480d3ac77SMichael Reed 			/*
67580d3ac77SMichael Reed 			 * if still doing discovery,
67680d3ac77SMichael Reed 			 * hang loose a while until finished
67780d3ac77SMichael Reed 			 */
67880d3ac77SMichael Reed 			if (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) {
67980d3ac77SMichael Reed 				if (count-- > 0) {
680d6be06c8SMichael Reed 					msleep(100);
68180d3ac77SMichael Reed 					goto try_again;
68280d3ac77SMichael Reed 				}
68380d3ac77SMichael Reed 				printk(MYIOC_s_INFO_FMT "Firmware discovery not"
68480d3ac77SMichael Reed 							" complete.\n",
68580d3ac77SMichael Reed 						ioc->name);
68680d3ac77SMichael Reed 			}
68780d3ac77SMichael Reed 		}
68880d3ac77SMichael Reed 
68980d3ac77SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
69080d3ac77SMichael Reed 	}
69180d3ac77SMichael Reed 
69280d3ac77SMichael Reed 	return rc;
69380d3ac77SMichael Reed }
69480d3ac77SMichael Reed 
695ca2f938eSMichael Reed static int
696ca2f938eSMichael Reed mptfc_WriteFcPortPage1(MPT_ADAPTER *ioc, int portnum)
697ca2f938eSMichael Reed {
698ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
699ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
700ca2f938eSMichael Reed 	int			 rc;
701ca2f938eSMichael Reed 
702ca2f938eSMichael Reed 	if (portnum > 1)
703ca2f938eSMichael Reed 		return -EINVAL;
704ca2f938eSMichael Reed 
705ca2f938eSMichael Reed 	if (!(ioc->fc_data.fc_port_page1[portnum].data))
706ca2f938eSMichael Reed 		return -EINVAL;
707ca2f938eSMichael Reed 
708ca2f938eSMichael Reed 	/* get fcport page 1 header */
709ca2f938eSMichael Reed 	hdr.PageVersion = 0;
710ca2f938eSMichael Reed 	hdr.PageLength = 0;
711ca2f938eSMichael Reed 	hdr.PageNumber = 1;
712ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
713ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
714ca2f938eSMichael Reed 	cfg.physAddr = -1;
715ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
716ca2f938eSMichael Reed 	cfg.dir = 0;
717ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
718ca2f938eSMichael Reed 	cfg.timeout = 0;
719ca2f938eSMichael Reed 
720ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
721ca2f938eSMichael Reed 		return rc;
722ca2f938eSMichael Reed 
723ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
724ca2f938eSMichael Reed 		return -ENODEV;
725ca2f938eSMichael Reed 
726ca2f938eSMichael Reed 	if (hdr.PageLength*4 != ioc->fc_data.fc_port_page1[portnum].pg_sz)
727ca2f938eSMichael Reed 		return -EINVAL;
728ca2f938eSMichael Reed 
729ca2f938eSMichael Reed 	cfg.physAddr = ioc->fc_data.fc_port_page1[portnum].dma;
730ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
731ca2f938eSMichael Reed 	cfg.dir = 1;
732ca2f938eSMichael Reed 
733ca2f938eSMichael Reed 	rc = mpt_config(ioc, &cfg);
734ca2f938eSMichael Reed 
735ca2f938eSMichael Reed 	return rc;
736ca2f938eSMichael Reed }
737ca2f938eSMichael Reed 
738ca2f938eSMichael Reed static int
739ca2f938eSMichael Reed mptfc_GetFcPortPage1(MPT_ADAPTER *ioc, int portnum)
740ca2f938eSMichael Reed {
741ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
742ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
743ca2f938eSMichael Reed 	FCPortPage1_t		*page1_alloc;
744ca2f938eSMichael Reed 	dma_addr_t		 page1_dma;
745ca2f938eSMichael Reed 	int			 data_sz;
746ca2f938eSMichael Reed 	int			 rc;
747ca2f938eSMichael Reed 
748ca2f938eSMichael Reed 	if (portnum > 1)
749ca2f938eSMichael Reed 		return -EINVAL;
750ca2f938eSMichael Reed 
751ca2f938eSMichael Reed 	/* get fcport page 1 header */
752ca2f938eSMichael Reed 	hdr.PageVersion = 0;
753ca2f938eSMichael Reed 	hdr.PageLength = 0;
754ca2f938eSMichael Reed 	hdr.PageNumber = 1;
755ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
756ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
757ca2f938eSMichael Reed 	cfg.physAddr = -1;
758ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
759ca2f938eSMichael Reed 	cfg.dir = 0;
760ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
761ca2f938eSMichael Reed 	cfg.timeout = 0;
762ca2f938eSMichael Reed 
763ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
764ca2f938eSMichael Reed 		return rc;
765ca2f938eSMichael Reed 
766ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
767ca2f938eSMichael Reed 		return -ENODEV;
768ca2f938eSMichael Reed 
769ca2f938eSMichael Reed start_over:
770ca2f938eSMichael Reed 
771ca2f938eSMichael Reed 	if (ioc->fc_data.fc_port_page1[portnum].data == NULL) {
772ca2f938eSMichael Reed 		data_sz = hdr.PageLength * 4;
773ca2f938eSMichael Reed 		if (data_sz < sizeof(FCPortPage1_t))
774ca2f938eSMichael Reed 			data_sz = sizeof(FCPortPage1_t);
775ca2f938eSMichael Reed 
776ca2f938eSMichael Reed 		page1_alloc = (FCPortPage1_t *) pci_alloc_consistent(ioc->pcidev,
777ca2f938eSMichael Reed 						data_sz,
778ca2f938eSMichael Reed 						&page1_dma);
779ca2f938eSMichael Reed 		if (!page1_alloc)
780ca2f938eSMichael Reed 			return -ENOMEM;
781ca2f938eSMichael Reed 	}
782ca2f938eSMichael Reed 	else {
783ca2f938eSMichael Reed 		page1_alloc = ioc->fc_data.fc_port_page1[portnum].data;
784ca2f938eSMichael Reed 		page1_dma = ioc->fc_data.fc_port_page1[portnum].dma;
785ca2f938eSMichael Reed 		data_sz = ioc->fc_data.fc_port_page1[portnum].pg_sz;
786ca2f938eSMichael Reed 		if (hdr.PageLength * 4 > data_sz) {
787ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[portnum].data = NULL;
788ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
789ca2f938eSMichael Reed 				page1_alloc, page1_dma);
790ca2f938eSMichael Reed 			goto start_over;
791ca2f938eSMichael Reed 		}
792ca2f938eSMichael Reed 	}
793ca2f938eSMichael Reed 
794ca2f938eSMichael Reed 	memset(page1_alloc,0,data_sz);
795ca2f938eSMichael Reed 
796ca2f938eSMichael Reed 	cfg.physAddr = page1_dma;
797ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
798ca2f938eSMichael Reed 
799ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) == 0) {
800ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = page1_alloc;
801ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].pg_sz = data_sz;
802ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].dma = page1_dma;
803ca2f938eSMichael Reed 	}
804ca2f938eSMichael Reed 	else {
805ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = NULL;
806ca2f938eSMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
807ca2f938eSMichael Reed 			page1_alloc, page1_dma);
808ca2f938eSMichael Reed 	}
809ca2f938eSMichael Reed 
810ca2f938eSMichael Reed 	return rc;
811ca2f938eSMichael Reed }
812ca2f938eSMichael Reed 
813ca2f938eSMichael Reed static void
814ca2f938eSMichael Reed mptfc_SetFcPortPage1_defaults(MPT_ADAPTER *ioc)
815ca2f938eSMichael Reed {
816ca2f938eSMichael Reed 	int		ii;
817ca2f938eSMichael Reed 	FCPortPage1_t	*pp1;
818ca2f938eSMichael Reed 
819ca2f938eSMichael Reed 	#define MPTFC_FW_DEVICE_TIMEOUT	(1)
820ca2f938eSMichael Reed 	#define MPTFC_FW_IO_PEND_TIMEOUT (1)
821ca2f938eSMichael Reed 	#define ON_FLAGS  (MPI_FCPORTPAGE1_FLAGS_IMMEDIATE_ERROR_REPLY)
822ca2f938eSMichael Reed 	#define OFF_FLAGS (MPI_FCPORTPAGE1_FLAGS_VERBOSE_RESCAN_EVENTS)
823ca2f938eSMichael Reed 
824ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
825ca2f938eSMichael Reed 		if (mptfc_GetFcPortPage1(ioc, ii) != 0)
826ca2f938eSMichael Reed 			continue;
827ca2f938eSMichael Reed 		pp1 = ioc->fc_data.fc_port_page1[ii].data;
828ca2f938eSMichael Reed 		if ((pp1->InitiatorDeviceTimeout == MPTFC_FW_DEVICE_TIMEOUT)
829ca2f938eSMichael Reed 		 && (pp1->InitiatorIoPendTimeout == MPTFC_FW_IO_PEND_TIMEOUT)
830ca2f938eSMichael Reed 		 && ((pp1->Flags & ON_FLAGS) == ON_FLAGS)
831ca2f938eSMichael Reed 		 && ((pp1->Flags & OFF_FLAGS) == 0))
832ca2f938eSMichael Reed 			continue;
833ca2f938eSMichael Reed 		pp1->InitiatorDeviceTimeout = MPTFC_FW_DEVICE_TIMEOUT;
834ca2f938eSMichael Reed 		pp1->InitiatorIoPendTimeout = MPTFC_FW_IO_PEND_TIMEOUT;
835ca2f938eSMichael Reed 		pp1->Flags &= ~OFF_FLAGS;
836ca2f938eSMichael Reed 		pp1->Flags |= ON_FLAGS;
837ca2f938eSMichael Reed 		mptfc_WriteFcPortPage1(ioc, ii);
838ca2f938eSMichael Reed 	}
839ca2f938eSMichael Reed }
840ca2f938eSMichael Reed 
841ca2f938eSMichael Reed 
84205e8ec17SMichael Reed static void
84305e8ec17SMichael Reed mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
84405e8ec17SMichael Reed {
845*5d947f2bSMichael Reed 	unsigned	class = 0;
846*5d947f2bSMichael Reed 	unsigned	cos = 0;
847*5d947f2bSMichael Reed 	unsigned	speed;
848*5d947f2bSMichael Reed 	unsigned	port_type;
849*5d947f2bSMichael Reed 	unsigned	port_state;
850*5d947f2bSMichael Reed 	FCPortPage0_t	*pp0;
851*5d947f2bSMichael Reed 	struct Scsi_Host *sh;
852*5d947f2bSMichael Reed 	char		*sn;
85305e8ec17SMichael Reed 
85405e8ec17SMichael Reed 	/* don't know what to do as only one scsi (fc) host was allocated */
85505e8ec17SMichael Reed 	if (portnum != 0)
85605e8ec17SMichael Reed 		return;
85705e8ec17SMichael Reed 
858*5d947f2bSMichael Reed 	pp0 = &ioc->fc_port_page0[portnum];
859*5d947f2bSMichael Reed 	sh = ioc->sh;
860*5d947f2bSMichael Reed 
861*5d947f2bSMichael Reed 	sn = fc_host_symbolic_name(sh);
862*5d947f2bSMichael Reed 	snprintf(sn, FC_SYMBOLIC_NAME_SIZE, "%s %s%08xh",
863*5d947f2bSMichael Reed 	    ioc->prod_name,
864*5d947f2bSMichael Reed 	    MPT_FW_REV_MAGIC_ID_STRING,
865*5d947f2bSMichael Reed 	    ioc->facts.FWVersion.Word);
866*5d947f2bSMichael Reed 
867*5d947f2bSMichael Reed 	fc_host_tgtid_bind_type(sh) = FC_TGTID_BIND_BY_WWPN;
868*5d947f2bSMichael Reed 
869*5d947f2bSMichael Reed 	fc_host_maxframe_size(sh) = pp0->MaxFrameSize;
870*5d947f2bSMichael Reed 
871*5d947f2bSMichael Reed 	fc_host_node_name(sh) =
872*5d947f2bSMichael Reed 	    	(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
873*5d947f2bSMichael Reed 
874*5d947f2bSMichael Reed 	fc_host_port_name(sh) =
875*5d947f2bSMichael Reed 	    	(u64)pp0->WWPN.High << 32 | (u64)pp0->WWPN.Low;
876*5d947f2bSMichael Reed 
877*5d947f2bSMichael Reed 	fc_host_port_id(sh) = pp0->PortIdentifier;
878*5d947f2bSMichael Reed 
879*5d947f2bSMichael Reed 	class = pp0->SupportedServiceClass;
88005e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
88105e8ec17SMichael Reed 		cos |= FC_COS_CLASS1;
88205e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
88305e8ec17SMichael Reed 		cos |= FC_COS_CLASS2;
88405e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
88505e8ec17SMichael Reed 		cos |= FC_COS_CLASS3;
886*5d947f2bSMichael Reed 	fc_host_supported_classes(sh) = cos;
88705e8ec17SMichael Reed 
888*5d947f2bSMichael Reed 	if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT)
889*5d947f2bSMichael Reed 		speed = FC_PORTSPEED_1GBIT;
890*5d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT)
891*5d947f2bSMichael Reed 		speed = FC_PORTSPEED_2GBIT;
892*5d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT)
893*5d947f2bSMichael Reed 		speed = FC_PORTSPEED_4GBIT;
894*5d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_10GBIT)
895*5d947f2bSMichael Reed 		speed = FC_PORTSPEED_10GBIT;
896*5d947f2bSMichael Reed 	else
897*5d947f2bSMichael Reed 		speed = FC_PORTSPEED_UNKNOWN;
898*5d947f2bSMichael Reed 	fc_host_speed(sh) = speed;
89905e8ec17SMichael Reed 
900*5d947f2bSMichael Reed 	speed = 0;
901*5d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_1GBIT_SPEED)
902*5d947f2bSMichael Reed 		speed |= FC_PORTSPEED_1GBIT;
903*5d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_2GBIT_SPEED)
904*5d947f2bSMichael Reed 		speed |= FC_PORTSPEED_2GBIT;
905*5d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_4GBIT_SPEED)
906*5d947f2bSMichael Reed 		speed |= FC_PORTSPEED_4GBIT;
907*5d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_10GBIT_SPEED)
908*5d947f2bSMichael Reed 		speed |= FC_PORTSPEED_10GBIT;
909*5d947f2bSMichael Reed 	fc_host_supported_speeds(sh) = speed;
91005e8ec17SMichael Reed 
911*5d947f2bSMichael Reed 	port_state = FC_PORTSTATE_UNKNOWN;
912*5d947f2bSMichael Reed 	if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE)
913*5d947f2bSMichael Reed 		port_state = FC_PORTSTATE_ONLINE;
914*5d947f2bSMichael Reed 	else if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_OFFLINE)
915*5d947f2bSMichael Reed 		port_state = FC_PORTSTATE_LINKDOWN;
916*5d947f2bSMichael Reed 	fc_host_port_state(sh) = port_state;
91705e8ec17SMichael Reed 
918*5d947f2bSMichael Reed 	port_type = FC_PORTTYPE_UNKNOWN;
919*5d947f2bSMichael Reed 	if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_POINT_TO_POINT)
920*5d947f2bSMichael Reed 		port_type = FC_PORTTYPE_PTP;
921*5d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PRIVATE_LOOP)
922*5d947f2bSMichael Reed 		port_type = FC_PORTTYPE_LPORT;
923*5d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PUBLIC_LOOP)
924*5d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NLPORT;
925*5d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_FABRIC_DIRECT)
926*5d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NPORT;
927*5d947f2bSMichael Reed 	fc_host_port_type(sh) = port_type;
92805e8ec17SMichael Reed 
929*5d947f2bSMichael Reed 	fc_host_fabric_name(sh) =
930*5d947f2bSMichael Reed 	    (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_FABRIC_WWN_VALID) ?
931*5d947f2bSMichael Reed 		(u64) pp0->FabricWWNN.High << 32 | (u64) pp0->FabricWWPN.Low :
932*5d947f2bSMichael Reed 		(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
933*5d947f2bSMichael Reed 
93405e8ec17SMichael Reed }
93505e8ec17SMichael Reed 
93605e8ec17SMichael Reed static void
937419835e2SMichael Reed mptfc_setup_reset(void *arg)
938419835e2SMichael Reed {
939419835e2SMichael Reed 	MPT_ADAPTER		*ioc = (MPT_ADAPTER *)arg;
940419835e2SMichael Reed 	u64			pn;
941419835e2SMichael Reed 	struct mptfc_rport_info *ri;
942419835e2SMichael Reed 
943419835e2SMichael Reed 	/* reset about to happen, delete (block) all rports */
944419835e2SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
945419835e2SMichael Reed 		if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
946419835e2SMichael Reed 			ri->flags &= ~MPT_RPORT_INFO_FLAGS_REGISTERED;
947419835e2SMichael Reed 			fc_remote_port_delete(ri->rport);	/* won't sleep */
948419835e2SMichael Reed 			ri->rport = NULL;
949419835e2SMichael Reed 
950419835e2SMichael Reed 			pn = (u64)ri->pg0.WWPN.High << 32 |
951419835e2SMichael Reed 			     (u64)ri->pg0.WWPN.Low;
952419835e2SMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
953419835e2SMichael Reed 				"mptfc_setup_reset.%d: %llx deleted\n",
954419835e2SMichael Reed 				ioc->name,
955419835e2SMichael Reed 				ioc->sh->host_no,
956419835e2SMichael Reed 				(unsigned long long)pn));
957419835e2SMichael Reed 		}
958419835e2SMichael Reed 	}
959419835e2SMichael Reed }
960419835e2SMichael Reed 
961419835e2SMichael Reed static void
96205e8ec17SMichael Reed mptfc_rescan_devices(void *arg)
96305e8ec17SMichael Reed {
96405e8ec17SMichael Reed 	MPT_ADAPTER		*ioc = (MPT_ADAPTER *)arg;
96505e8ec17SMichael Reed 	int			ii;
96605e8ec17SMichael Reed 	int			work_to_do;
96765207fedSMoore, Eric 	u64			pn;
96805e8ec17SMichael Reed 	unsigned long		flags;
96905e8ec17SMichael Reed 	struct mptfc_rport_info *ri;
97005e8ec17SMichael Reed 
97105e8ec17SMichael Reed 	do {
97205e8ec17SMichael Reed 		/* start by tagging all ports as missing */
97305e8ec17SMichael Reed 		list_for_each_entry(ri, &ioc->fc_rports, list) {
97405e8ec17SMichael Reed 			if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
97505e8ec17SMichael Reed 				ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
97605e8ec17SMichael Reed 			}
97705e8ec17SMichael Reed 		}
97805e8ec17SMichael Reed 
97905e8ec17SMichael Reed 		/*
98005e8ec17SMichael Reed 		 * now rescan devices known to adapter,
98105e8ec17SMichael Reed 		 * will reregister existing rports
98205e8ec17SMichael Reed 		 */
98305e8ec17SMichael Reed 		for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
98480d3ac77SMichael Reed 			(void) mptfc_GetFcPortPage0(ioc, ii);
98505e8ec17SMichael Reed 			mptfc_init_host_attr(ioc,ii);	/* refresh */
98605e8ec17SMichael Reed 			mptfc_GetFcDevPage0(ioc,ii,mptfc_register_dev);
98705e8ec17SMichael Reed 		}
98805e8ec17SMichael Reed 
98905e8ec17SMichael Reed 		/* delete devices still missing */
99005e8ec17SMichael Reed 		list_for_each_entry(ri, &ioc->fc_rports, list) {
99105e8ec17SMichael Reed 			/* if newly missing, delete it */
99265207fedSMoore, Eric 			if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
99305e8ec17SMichael Reed 
99405e8ec17SMichael Reed 				ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
99505e8ec17SMichael Reed 					       MPT_RPORT_INFO_FLAGS_MISSING);
99665207fedSMoore, Eric 				fc_remote_port_delete(ri->rport);	/* won't sleep */
9973bc7bf1dSMichael Reed 				ri->rport = NULL;
99865207fedSMoore, Eric 
99965207fedSMoore, Eric 				pn = (u64)ri->pg0.WWPN.High << 32 |
100065207fedSMoore, Eric 				     (u64)ri->pg0.WWPN.Low;
10013bc7bf1dSMichael Reed 				dfcprintk ((MYIOC_s_INFO_FMT
10023bc7bf1dSMichael Reed 					"mptfc_rescan.%d: %llx deleted\n",
10033bc7bf1dSMichael Reed 					ioc->name,
10043bc7bf1dSMichael Reed 					ioc->sh->host_no,
100565207fedSMoore, Eric 					(unsigned long long)pn));
100605e8ec17SMichael Reed 			}
100705e8ec17SMichael Reed 		}
100805e8ec17SMichael Reed 
100905e8ec17SMichael Reed 		/*
101005e8ec17SMichael Reed 		 * allow multiple passes as target state
101105e8ec17SMichael Reed 		 * might have changed during scan
101205e8ec17SMichael Reed 		 */
101305e8ec17SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
101405e8ec17SMichael Reed 		if (ioc->fc_rescan_work_count > 2) 	/* only need one more */
101505e8ec17SMichael Reed 			ioc->fc_rescan_work_count = 2;
101605e8ec17SMichael Reed 		work_to_do = --ioc->fc_rescan_work_count;
101705e8ec17SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
101805e8ec17SMichael Reed 	} while (work_to_do);
101905e8ec17SMichael Reed }
102005e8ec17SMichael Reed 
10212496af39SMoore, Eric Dean  static int
10222496af39SMoore, Eric Dean  mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
10232496af39SMoore, Eric Dean  {
10242496af39SMoore, Eric Dean  	struct Scsi_Host	*sh;
10252496af39SMoore, Eric Dean  	MPT_SCSI_HOST		*hd;
10262496af39SMoore, Eric Dean  	MPT_ADAPTER 		*ioc;
10272496af39SMoore, Eric Dean  	unsigned long		 flags;
10281ca00bb7SChristoph Hellwig 	int			 ii;
10292496af39SMoore, Eric Dean  	int			 numSGE = 0;
10302496af39SMoore, Eric Dean  	int			 scale;
10312496af39SMoore, Eric Dean  	int			 ioc_cap;
10322496af39SMoore, Eric Dean  	int			error=0;
10332496af39SMoore, Eric Dean  	int			r;
10342496af39SMoore, Eric Dean  
10352496af39SMoore, Eric Dean  	if ((r = mpt_attach(pdev,id)) != 0)
10362496af39SMoore, Eric Dean  		return r;
10372496af39SMoore, Eric Dean  
10382496af39SMoore, Eric Dean  	ioc = pci_get_drvdata(pdev);
1039d335cc38SMoore, Eric Dean  	ioc->DoneCtx = mptfcDoneCtx;
1040d335cc38SMoore, Eric Dean  	ioc->TaskCtx = mptfcTaskCtx;
1041d335cc38SMoore, Eric Dean  	ioc->InternalCtx = mptfcInternalCtx;
10422496af39SMoore, Eric Dean  
10432496af39SMoore, Eric Dean  	/*  Added sanity check on readiness of the MPT adapter.
10442496af39SMoore, Eric Dean  	 */
10452496af39SMoore, Eric Dean  	if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
10462496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
10472496af39SMoore, Eric Dean  		  "Skipping because it's not operational!\n",
10482496af39SMoore, Eric Dean  		  ioc->name);
10497acec1e7SMoore, Eric Dean 		error = -ENODEV;
10507acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
10512496af39SMoore, Eric Dean  	}
10522496af39SMoore, Eric Dean  
10532496af39SMoore, Eric Dean  	if (!ioc->active) {
10542496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
10552496af39SMoore, Eric Dean  		  ioc->name);
10567acec1e7SMoore, Eric Dean 		error = -ENODEV;
10577acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
10582496af39SMoore, Eric Dean  	}
10592496af39SMoore, Eric Dean  
10602496af39SMoore, Eric Dean  	/*  Sanity check - ensure at least 1 port is INITIATOR capable
10612496af39SMoore, Eric Dean  	 */
10622496af39SMoore, Eric Dean  	ioc_cap = 0;
10632496af39SMoore, Eric Dean  	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
10642496af39SMoore, Eric Dean  		if (ioc->pfacts[ii].ProtocolFlags &
10652496af39SMoore, Eric Dean  		    MPI_PORTFACTS_PROTOCOL_INITIATOR)
10662496af39SMoore, Eric Dean  			ioc_cap ++;
10672496af39SMoore, Eric Dean  	}
10682496af39SMoore, Eric Dean  
10692496af39SMoore, Eric Dean  	if (!ioc_cap) {
10702496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
10712496af39SMoore, Eric Dean  			"Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
10722496af39SMoore, Eric Dean  			ioc->name, ioc);
107305e8ec17SMichael Reed 		return -ENODEV;
10742496af39SMoore, Eric Dean  	}
10752496af39SMoore, Eric Dean  
10762496af39SMoore, Eric Dean  	sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
10772496af39SMoore, Eric Dean  
10782496af39SMoore, Eric Dean  	if (!sh) {
10792496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
10802496af39SMoore, Eric Dean  			"Unable to register controller with SCSI subsystem\n",
10812496af39SMoore, Eric Dean  			ioc->name);
10827acec1e7SMoore, Eric Dean 		error = -1;
10837acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
10842496af39SMoore, Eric Dean          }
10852496af39SMoore, Eric Dean  
108680d3ac77SMichael Reed 	spin_lock_init(&ioc->fc_rescan_work_lock);
108705e8ec17SMichael Reed 	INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices,(void *)ioc);
1088419835e2SMichael Reed 	INIT_WORK(&ioc->fc_setup_reset_work, mptfc_setup_reset, (void *)ioc);
108905e8ec17SMichael Reed 
10902496af39SMoore, Eric Dean  	spin_lock_irqsave(&ioc->FreeQlock, flags);
10912496af39SMoore, Eric Dean  
10922496af39SMoore, Eric Dean  	/* Attach the SCSI Host to the IOC structure
10932496af39SMoore, Eric Dean  	 */
10942496af39SMoore, Eric Dean  	ioc->sh = sh;
10952496af39SMoore, Eric Dean  
10962496af39SMoore, Eric Dean  	sh->io_port = 0;
10972496af39SMoore, Eric Dean  	sh->n_io_port = 0;
10982496af39SMoore, Eric Dean  	sh->irq = 0;
10992496af39SMoore, Eric Dean  
11002496af39SMoore, Eric Dean  	/* set 16 byte cdb's */
11012496af39SMoore, Eric Dean  	sh->max_cmd_len = 16;
11022496af39SMoore, Eric Dean  
11032496af39SMoore, Eric Dean  	sh->max_id = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
11042496af39SMoore, Eric Dean  
11052496af39SMoore, Eric Dean  	sh->max_lun = MPT_LAST_LUN + 1;
11062496af39SMoore, Eric Dean  	sh->max_channel = 0;
11072496af39SMoore, Eric Dean  	sh->this_id = ioc->pfacts[0].PortSCSIID;
11082496af39SMoore, Eric Dean  
11092496af39SMoore, Eric Dean  	/* Required entry.
11102496af39SMoore, Eric Dean  	 */
11112496af39SMoore, Eric Dean  	sh->unique_id = ioc->id;
11122496af39SMoore, Eric Dean  
11132496af39SMoore, Eric Dean  	/* Verify that we won't exceed the maximum
11142496af39SMoore, Eric Dean  	 * number of chain buffers
11152496af39SMoore, Eric Dean  	 * We can optimize:  ZZ = req_sz/sizeof(SGE)
11162496af39SMoore, Eric Dean  	 * For 32bit SGE's:
11172496af39SMoore, Eric Dean  	 *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
11182496af39SMoore, Eric Dean  	 *               + (req_sz - 64)/sizeof(SGE)
11192496af39SMoore, Eric Dean  	 * A slightly different algorithm is required for
11202496af39SMoore, Eric Dean  	 * 64bit SGEs.
11212496af39SMoore, Eric Dean  	 */
11222496af39SMoore, Eric Dean  	scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
11232496af39SMoore, Eric Dean  	if (sizeof(dma_addr_t) == sizeof(u64)) {
11242496af39SMoore, Eric Dean  		numSGE = (scale - 1) *
11252496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
11262496af39SMoore, Eric Dean  		  (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
11272496af39SMoore, Eric Dean  		  sizeof(u32));
11282496af39SMoore, Eric Dean  	} else {
11292496af39SMoore, Eric Dean  		numSGE = 1 + (scale - 1) *
11302496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
11312496af39SMoore, Eric Dean  		  (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
11322496af39SMoore, Eric Dean  		  sizeof(u32));
11332496af39SMoore, Eric Dean  	}
11342496af39SMoore, Eric Dean  
11352496af39SMoore, Eric Dean  	if (numSGE < sh->sg_tablesize) {
11362496af39SMoore, Eric Dean  		/* Reset this value */
11372496af39SMoore, Eric Dean  		dprintk((MYIOC_s_INFO_FMT
11382496af39SMoore, Eric Dean  		  "Resetting sg_tablesize to %d from %d\n",
11392496af39SMoore, Eric Dean  		  ioc->name, numSGE, sh->sg_tablesize));
11402496af39SMoore, Eric Dean  		sh->sg_tablesize = numSGE;
11412496af39SMoore, Eric Dean  	}
11422496af39SMoore, Eric Dean  
11432496af39SMoore, Eric Dean  	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
11442496af39SMoore, Eric Dean  
11452496af39SMoore, Eric Dean  	hd = (MPT_SCSI_HOST *) sh->hostdata;
11462496af39SMoore, Eric Dean  	hd->ioc = ioc;
11472496af39SMoore, Eric Dean  
11482496af39SMoore, Eric Dean  	/* SCSI needs scsi_cmnd lookup table!
11492496af39SMoore, Eric Dean  	 * (with size equal to req_depth*PtrSz!)
11502496af39SMoore, Eric Dean  	 */
11511ca00bb7SChristoph Hellwig 	hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
11521ca00bb7SChristoph Hellwig 	if (!hd->ScsiLookup) {
11532496af39SMoore, Eric Dean  		error = -ENOMEM;
11547acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11552496af39SMoore, Eric Dean  	}
11562496af39SMoore, Eric Dean  
11571ca00bb7SChristoph Hellwig 	dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
11581ca00bb7SChristoph Hellwig 		 ioc->name, hd->ScsiLookup));
11592496af39SMoore, Eric Dean  
11602496af39SMoore, Eric Dean  	/* Allocate memory for the device structures.
11612496af39SMoore, Eric Dean  	 * A non-Null pointer at an offset
11622496af39SMoore, Eric Dean  	 * indicates a device exists.
11632496af39SMoore, Eric Dean  	 * max_id = 1 + maximum id (hosts.h)
11642496af39SMoore, Eric Dean  	 */
11651ca00bb7SChristoph Hellwig 	hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
11661ca00bb7SChristoph Hellwig 	if (!hd->Targets) {
11672496af39SMoore, Eric Dean  		error = -ENOMEM;
11687acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11692496af39SMoore, Eric Dean  	}
11702496af39SMoore, Eric Dean  
11711ca00bb7SChristoph Hellwig 	dprintk((KERN_INFO "  vdev @ %p\n", hd->Targets));
11722496af39SMoore, Eric Dean  
11732496af39SMoore, Eric Dean  	/* Clear the TM flags
11742496af39SMoore, Eric Dean  	 */
11752496af39SMoore, Eric Dean  	hd->tmPending = 0;
11762496af39SMoore, Eric Dean  	hd->tmState = TM_STATE_NONE;
11772496af39SMoore, Eric Dean  	hd->resetPending = 0;
11782496af39SMoore, Eric Dean  	hd->abortSCpnt = NULL;
11792496af39SMoore, Eric Dean  
11802496af39SMoore, Eric Dean  	/* Clear the pointer used to store
11812496af39SMoore, Eric Dean  	 * single-threaded commands, i.e., those
11822496af39SMoore, Eric Dean  	 * issued during a bus scan, dv and
11832496af39SMoore, Eric Dean  	 * configuration pages.
11842496af39SMoore, Eric Dean  	 */
11852496af39SMoore, Eric Dean  	hd->cmdPtr = NULL;
11862496af39SMoore, Eric Dean  
11872496af39SMoore, Eric Dean  	/* Initialize this SCSI Hosts' timers
11882496af39SMoore, Eric Dean  	 * To use, set the timer expires field
11892496af39SMoore, Eric Dean  	 * and add_timer
11902496af39SMoore, Eric Dean  	 */
11912496af39SMoore, Eric Dean  	init_timer(&hd->timer);
11922496af39SMoore, Eric Dean  	hd->timer.data = (unsigned long) hd;
11932496af39SMoore, Eric Dean  	hd->timer.function = mptscsih_timer_expired;
11942496af39SMoore, Eric Dean  
11952496af39SMoore, Eric Dean  	init_waitqueue_head(&hd->scandv_waitq);
11962496af39SMoore, Eric Dean  	hd->scandv_wait_done = 0;
11972496af39SMoore, Eric Dean  	hd->last_queue_full = 0;
11982496af39SMoore, Eric Dean  
119905e8ec17SMichael Reed 	sh->transportt = mptfc_transport_template;
12002496af39SMoore, Eric Dean  	error = scsi_add_host (sh, &ioc->pcidev->dev);
12012496af39SMoore, Eric Dean  	if(error) {
12022496af39SMoore, Eric Dean  		dprintk((KERN_ERR MYNAM
12032496af39SMoore, Eric Dean  		  "scsi_add_host failed\n"));
12047acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12052496af39SMoore, Eric Dean  	}
12062496af39SMoore, Eric Dean  
120765207fedSMoore, Eric 	/* initialize workqueue */
120865207fedSMoore, Eric 
120965207fedSMoore, Eric 	snprintf(ioc->fc_rescan_work_q_name, KOBJ_NAME_LEN, "mptfc_wq_%d",
121065207fedSMoore, Eric 		sh->host_no);
121165207fedSMoore, Eric 	ioc->fc_rescan_work_q =
121265207fedSMoore, Eric 		create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
121365207fedSMoore, Eric 	if (!ioc->fc_rescan_work_q)
121465207fedSMoore, Eric 		goto out_mptfc_probe;
121565207fedSMoore, Eric 
121665207fedSMoore, Eric 	/*
121780d3ac77SMichael Reed 	 *  Pre-fetch FC port WWN and stuff...
121880d3ac77SMichael Reed 	 *  (FCPortPage0_t stuff)
121980d3ac77SMichael Reed 	 */
122080d3ac77SMichael Reed 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
122180d3ac77SMichael Reed 		(void) mptfc_GetFcPortPage0(ioc, ii);
122280d3ac77SMichael Reed 	}
1223ca2f938eSMichael Reed 	mptfc_SetFcPortPage1_defaults(ioc);
122480d3ac77SMichael Reed 
122580d3ac77SMichael Reed 	/*
122665207fedSMoore, Eric 	 * scan for rports -
122765207fedSMoore, Eric 	 *	by doing it via the workqueue, some locking is eliminated
122865207fedSMoore, Eric 	 */
122965207fedSMoore, Eric 
123065207fedSMoore, Eric 	ioc->fc_rescan_work_count = 1;
123165207fedSMoore, Eric 	queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
123265207fedSMoore, Eric 	flush_workqueue(ioc->fc_rescan_work_q);
123305e8ec17SMichael Reed 
12342496af39SMoore, Eric Dean  	return 0;
12352496af39SMoore, Eric Dean  
12367acec1e7SMoore, Eric Dean out_mptfc_probe:
12372496af39SMoore, Eric Dean  
12382496af39SMoore, Eric Dean  	mptscsih_remove(pdev);
12392496af39SMoore, Eric Dean  	return error;
12402496af39SMoore, Eric Dean  }
12412496af39SMoore, Eric Dean  
12422496af39SMoore, Eric Dean  static struct pci_driver mptfc_driver = {
12432496af39SMoore, Eric Dean  	.name		= "mptfc",
12442496af39SMoore, Eric Dean  	.id_table	= mptfc_pci_table,
12452496af39SMoore, Eric Dean  	.probe		= mptfc_probe,
124605e8ec17SMichael Reed 	.remove		= __devexit_p(mptfc_remove),
12472496af39SMoore, Eric Dean  	.shutdown	= mptscsih_shutdown,
12482496af39SMoore, Eric Dean  #ifdef CONFIG_PM
12492496af39SMoore, Eric Dean  	.suspend	= mptscsih_suspend,
12502496af39SMoore, Eric Dean  	.resume		= mptscsih_resume,
12512496af39SMoore, Eric Dean  #endif
12522496af39SMoore, Eric Dean  };
12532496af39SMoore, Eric Dean  
125480d3ac77SMichael Reed static int
125580d3ac77SMichael Reed mptfc_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
125680d3ac77SMichael Reed {
125780d3ac77SMichael Reed 	MPT_SCSI_HOST *hd;
125880d3ac77SMichael Reed 	u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
125980d3ac77SMichael Reed 	unsigned long flags;
126080d3ac77SMichael Reed 	int rc=1;
126180d3ac77SMichael Reed 
126280d3ac77SMichael Reed 	devtverboseprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
126380d3ac77SMichael Reed 			ioc->name, event));
126480d3ac77SMichael Reed 
126580d3ac77SMichael Reed 	if (ioc->sh == NULL ||
126680d3ac77SMichael Reed 		((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL))
126780d3ac77SMichael Reed 		return 1;
126880d3ac77SMichael Reed 
126980d3ac77SMichael Reed 	switch (event) {
127080d3ac77SMichael Reed 	case MPI_EVENT_RESCAN:
127180d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
127280d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
127380d3ac77SMichael Reed 			if (ioc->fc_rescan_work_count++ == 0) {
127480d3ac77SMichael Reed 				queue_work(ioc->fc_rescan_work_q,
127580d3ac77SMichael Reed 					   &ioc->fc_rescan_work);
127680d3ac77SMichael Reed 			}
127780d3ac77SMichael Reed 		}
127880d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
127980d3ac77SMichael Reed 		break;
128080d3ac77SMichael Reed 	default:
128180d3ac77SMichael Reed 		rc = mptscsih_event_process(ioc,pEvReply);
128280d3ac77SMichael Reed 		break;
128380d3ac77SMichael Reed 	}
128480d3ac77SMichael Reed 	return rc;
128580d3ac77SMichael Reed }
128680d3ac77SMichael Reed 
128780d3ac77SMichael Reed static int
128880d3ac77SMichael Reed mptfc_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
128980d3ac77SMichael Reed {
129080d3ac77SMichael Reed 	int		rc;
129180d3ac77SMichael Reed 	unsigned long	flags;
129280d3ac77SMichael Reed 
129380d3ac77SMichael Reed 	rc = mptscsih_ioc_reset(ioc,reset_phase);
129480d3ac77SMichael Reed 	if (rc == 0)
129580d3ac77SMichael Reed 		return rc;
129680d3ac77SMichael Reed 
129780d3ac77SMichael Reed 
129880d3ac77SMichael Reed 	dtmprintk((KERN_WARNING MYNAM
129980d3ac77SMichael Reed 		": IOC %s_reset routed to FC host driver!\n",
130080d3ac77SMichael Reed 		reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
130180d3ac77SMichael Reed 		reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
130280d3ac77SMichael Reed 
130380d3ac77SMichael Reed 	if (reset_phase == MPT_IOC_SETUP_RESET) {
1304419835e2SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1305419835e2SMichael Reed 		if (ioc->fc_rescan_work_q) {
1306419835e2SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
1307419835e2SMichael Reed 				   &ioc->fc_setup_reset_work);
1308419835e2SMichael Reed 		}
1309419835e2SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
131080d3ac77SMichael Reed 	}
131180d3ac77SMichael Reed 
131280d3ac77SMichael Reed 	else if (reset_phase == MPT_IOC_PRE_RESET) {
131380d3ac77SMichael Reed 	}
131480d3ac77SMichael Reed 
131580d3ac77SMichael Reed 	else {	/* MPT_IOC_POST_RESET */
1316ca2f938eSMichael Reed 		mptfc_SetFcPortPage1_defaults(ioc);
131780d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
131880d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
131980d3ac77SMichael Reed 			if (ioc->fc_rescan_work_count++ == 0) {
132080d3ac77SMichael Reed 				queue_work(ioc->fc_rescan_work_q,
132180d3ac77SMichael Reed 					   &ioc->fc_rescan_work);
132280d3ac77SMichael Reed 			}
132380d3ac77SMichael Reed 		}
132480d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
132580d3ac77SMichael Reed 	}
132680d3ac77SMichael Reed 	return 1;
132780d3ac77SMichael Reed }
132880d3ac77SMichael Reed 
13292496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
13302496af39SMoore, Eric Dean  /**
13312496af39SMoore, Eric Dean   *	mptfc_init - Register MPT adapter(s) as SCSI host(s) with
13322496af39SMoore, Eric Dean   *	linux scsi mid-layer.
13332496af39SMoore, Eric Dean   *
13342496af39SMoore, Eric Dean   *	Returns 0 for success, non-zero for failure.
13352496af39SMoore, Eric Dean   */
13362496af39SMoore, Eric Dean  static int __init
13372496af39SMoore, Eric Dean  mptfc_init(void)
13382496af39SMoore, Eric Dean  {
133905e8ec17SMichael Reed 	int error;
13402496af39SMoore, Eric Dean  
13412496af39SMoore, Eric Dean  	show_mptmod_ver(my_NAME, my_VERSION);
13422496af39SMoore, Eric Dean  
1343ca2f938eSMichael Reed 	/* sanity check module parameters */
1344ca2f938eSMichael Reed 	if (mptfc_dev_loss_tmo <= 0)
134505e8ec17SMichael Reed 		mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
134605e8ec17SMichael Reed 
134705e8ec17SMichael Reed 	mptfc_transport_template =
134805e8ec17SMichael Reed 		fc_attach_transport(&mptfc_transport_functions);
134905e8ec17SMichael Reed 
135005e8ec17SMichael Reed 	if (!mptfc_transport_template)
135105e8ec17SMichael Reed 		return -ENODEV;
135205e8ec17SMichael Reed 
13532496af39SMoore, Eric Dean  	mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
13542496af39SMoore, Eric Dean  	mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
13552496af39SMoore, Eric Dean  	mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
13562496af39SMoore, Eric Dean  
135780d3ac77SMichael Reed 	if (mpt_event_register(mptfcDoneCtx, mptfc_event_process) == 0) {
13583a892befSMoore, Eric 		devtverboseprintk((KERN_INFO MYNAM
13592496af39SMoore, Eric Dean  		  ": Registered for IOC event notifications\n"));
13602496af39SMoore, Eric Dean  	}
13612496af39SMoore, Eric Dean  
136280d3ac77SMichael Reed 	if (mpt_reset_register(mptfcDoneCtx, mptfc_ioc_reset) == 0) {
13632496af39SMoore, Eric Dean  		dprintk((KERN_INFO MYNAM
13642496af39SMoore, Eric Dean  		  ": Registered for IOC reset notifications\n"));
13652496af39SMoore, Eric Dean  	}
13662496af39SMoore, Eric Dean  
136705e8ec17SMichael Reed 	error = pci_register_driver(&mptfc_driver);
13683bc7bf1dSMichael Reed 	if (error)
136905e8ec17SMichael Reed 		fc_release_transport(mptfc_transport_template);
137005e8ec17SMichael Reed 
137105e8ec17SMichael Reed 	return error;
137205e8ec17SMichael Reed }
137305e8ec17SMichael Reed 
137405e8ec17SMichael Reed /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
137505e8ec17SMichael Reed /**
137605e8ec17SMichael Reed  *	mptfc_remove - Removed fc infrastructure for devices
137705e8ec17SMichael Reed  *	@pdev: Pointer to pci_dev structure
137805e8ec17SMichael Reed  *
137905e8ec17SMichael Reed  */
13803bc7bf1dSMichael Reed static void __devexit
13813bc7bf1dSMichael Reed mptfc_remove(struct pci_dev *pdev)
138205e8ec17SMichael Reed {
138305e8ec17SMichael Reed 	MPT_ADAPTER		*ioc = pci_get_drvdata(pdev);
138405e8ec17SMichael Reed 	struct mptfc_rport_info	*p, *n;
138565207fedSMoore, Eric 	struct workqueue_struct *work_q;
138665207fedSMoore, Eric 	unsigned long		flags;
1387ca2f938eSMichael Reed 	int			ii;
138865207fedSMoore, Eric 
138965207fedSMoore, Eric 	/* destroy workqueue */
139065207fedSMoore, Eric 	if ((work_q=ioc->fc_rescan_work_q)) {
139165207fedSMoore, Eric 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
139265207fedSMoore, Eric 		ioc->fc_rescan_work_q = NULL;
139365207fedSMoore, Eric 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
139465207fedSMoore, Eric 		destroy_workqueue(work_q);
139565207fedSMoore, Eric 	}
139605e8ec17SMichael Reed 
139705e8ec17SMichael Reed 	fc_remove_host(ioc->sh);
139805e8ec17SMichael Reed 
139905e8ec17SMichael Reed 	list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
140005e8ec17SMichael Reed 		list_del(&p->list);
140105e8ec17SMichael Reed 		kfree(p);
140205e8ec17SMichael Reed 	}
140305e8ec17SMichael Reed 
1404ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
1405ca2f938eSMichael Reed 		if (ioc->fc_data.fc_port_page1[ii].data) {
1406ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev,
1407ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].pg_sz,
1408ca2f938eSMichael Reed 				(u8 *) ioc->fc_data.fc_port_page1[ii].data,
1409ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].dma);
1410ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[ii].data = NULL;
1411ca2f938eSMichael Reed 		}
1412ca2f938eSMichael Reed 	}
1413ca2f938eSMichael Reed 
141405e8ec17SMichael Reed 	mptscsih_remove(pdev);
14152496af39SMoore, Eric Dean  }
14162496af39SMoore, Eric Dean  
14172496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14182496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14192496af39SMoore, Eric Dean  /**
14202496af39SMoore, Eric Dean   *	mptfc_exit - Unregisters MPT adapter(s)
14212496af39SMoore, Eric Dean   *
14222496af39SMoore, Eric Dean   */
14232496af39SMoore, Eric Dean  static void __exit
14242496af39SMoore, Eric Dean  mptfc_exit(void)
14252496af39SMoore, Eric Dean  {
14262496af39SMoore, Eric Dean  	pci_unregister_driver(&mptfc_driver);
142705e8ec17SMichael Reed 	fc_release_transport(mptfc_transport_template);
14282496af39SMoore, Eric Dean  
14292496af39SMoore, Eric Dean  	mpt_reset_deregister(mptfcDoneCtx);
14302496af39SMoore, Eric Dean  	dprintk((KERN_INFO MYNAM
14312496af39SMoore, Eric Dean  	  ": Deregistered for IOC reset notifications\n"));
14322496af39SMoore, Eric Dean  
14332496af39SMoore, Eric Dean  	mpt_event_deregister(mptfcDoneCtx);
14342496af39SMoore, Eric Dean  	dprintk((KERN_INFO MYNAM
14352496af39SMoore, Eric Dean  	  ": Deregistered for IOC event notifications\n"));
14362496af39SMoore, Eric Dean  
14372496af39SMoore, Eric Dean  	mpt_deregister(mptfcInternalCtx);
14382496af39SMoore, Eric Dean  	mpt_deregister(mptfcTaskCtx);
14392496af39SMoore, Eric Dean  	mpt_deregister(mptfcDoneCtx);
14402496af39SMoore, Eric Dean  }
14412496af39SMoore, Eric Dean  
14422496af39SMoore, Eric Dean  module_init(mptfc_init);
14432496af39SMoore, Eric Dean  module_exit(mptfc_exit);
1444