xref: /openbmc/linux/drivers/message/fusion/mptfc.c (revision 35508e46aae4b57bd07d095eb11533e296b254dc)
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);
99*35508e46SMichael Reed static int mptfc_abort(struct scsi_cmnd *SCpnt);
100*35508e46SMichael Reed static int mptfc_dev_reset(struct scsi_cmnd *SCpnt);
101*35508e46SMichael Reed static int mptfc_bus_reset(struct scsi_cmnd *SCpnt);
102*35508e46SMichael Reed static int mptfc_host_reset(struct scsi_cmnd *SCpnt);
10305e8ec17SMichael Reed 
1042496af39SMoore, Eric Dean  static struct scsi_host_template mptfc_driver_template = {
105f78496daSMoore, Eric Dean 	.module				= THIS_MODULE,
1062496af39SMoore, Eric Dean  	.proc_name			= "mptfc",
1072496af39SMoore, Eric Dean  	.proc_info			= mptscsih_proc_info,
1082496af39SMoore, Eric Dean  	.name				= "MPT FC Host",
1092496af39SMoore, Eric Dean  	.info				= mptscsih_info,
11005e8ec17SMichael Reed 	.queuecommand			= mptfc_qcmd,
1113bc7bf1dSMichael Reed 	.target_alloc			= mptfc_target_alloc,
11205e8ec17SMichael Reed 	.slave_alloc			= mptfc_slave_alloc,
1132496af39SMoore, Eric Dean  	.slave_configure		= mptscsih_slave_configure,
1143bc7bf1dSMichael Reed 	.target_destroy			= mptfc_target_destroy,
1152496af39SMoore, Eric Dean  	.slave_destroy			= mptscsih_slave_destroy,
1166e3815baSMoore, Eric Dean 	.change_queue_depth 		= mptscsih_change_queue_depth,
117*35508e46SMichael Reed 	.eh_abort_handler		= mptfc_abort,
118*35508e46SMichael Reed 	.eh_device_reset_handler	= mptfc_dev_reset,
119*35508e46SMichael Reed 	.eh_bus_reset_handler		= mptfc_bus_reset,
120*35508e46SMichael Reed 	.eh_host_reset_handler		= mptfc_host_reset,
1212496af39SMoore, Eric Dean  	.bios_param			= mptscsih_bios_param,
1222496af39SMoore, Eric Dean  	.can_queue			= MPT_FC_CAN_QUEUE,
1232496af39SMoore, Eric Dean  	.this_id			= -1,
1242496af39SMoore, Eric Dean  	.sg_tablesize			= MPT_SCSI_SG_DEPTH,
1252496af39SMoore, Eric Dean  	.max_sectors			= 8192,
1262496af39SMoore, Eric Dean  	.cmd_per_lun			= 7,
1272496af39SMoore, Eric Dean  	.use_clustering			= ENABLE_CLUSTERING,
1282496af39SMoore, Eric Dean  };
1292496af39SMoore, Eric Dean  
1302496af39SMoore, Eric Dean  /****************************************************************************
1312496af39SMoore, Eric Dean   * Supported hardware
1322496af39SMoore, Eric Dean   */
1332496af39SMoore, Eric Dean  
1342496af39SMoore, Eric Dean  static struct pci_device_id mptfc_pci_table[] = {
13587cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC909,
1362496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
13787cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919,
1382496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
13987cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929,
1402496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14187cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919X,
1422496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14387cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929X,
1442496af39SMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14587cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC939X,
1463fadc59dSMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14787cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949X,
1483fadc59dSMoore, Eric Dean  		PCI_ANY_ID, PCI_ANY_ID },
14987cf8986SEric Moore 	{ PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949E,
1506d5b0c31SMoore, Eric 		PCI_ANY_ID, PCI_ANY_ID },
1512496af39SMoore, Eric Dean  	{0}	/* Terminating entry */
1522496af39SMoore, Eric Dean  };
1532496af39SMoore, Eric Dean  MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
1542496af39SMoore, Eric Dean  
15505e8ec17SMichael Reed static struct scsi_transport_template *mptfc_transport_template = NULL;
15605e8ec17SMichael Reed 
15703fbcbcdSAdrian Bunk static struct fc_function_template mptfc_transport_functions = {
15805e8ec17SMichael Reed 	.dd_fcrport_size = 8,
15905e8ec17SMichael Reed 	.show_host_node_name = 1,
16005e8ec17SMichael Reed 	.show_host_port_name = 1,
16105e8ec17SMichael Reed 	.show_host_supported_classes = 1,
16205e8ec17SMichael Reed 	.show_host_port_id = 1,
16305e8ec17SMichael Reed 	.show_rport_supported_classes = 1,
16405e8ec17SMichael Reed 	.show_starget_node_name = 1,
16505e8ec17SMichael Reed 	.show_starget_port_name = 1,
16605e8ec17SMichael Reed 	.show_starget_port_id = 1,
16705e8ec17SMichael Reed 	.set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
16805e8ec17SMichael Reed 	.show_rport_dev_loss_tmo = 1,
1695d947f2bSMichael Reed 	.show_host_supported_speeds = 1,
1705d947f2bSMichael Reed 	.show_host_maxframe_size = 1,
1715d947f2bSMichael Reed 	.show_host_speed = 1,
1725d947f2bSMichael Reed 	.show_host_fabric_name = 1,
1735d947f2bSMichael Reed 	.show_host_port_type = 1,
1745d947f2bSMichael Reed 	.show_host_port_state = 1,
1755d947f2bSMichael Reed 	.show_host_symbolic_name = 1,
17605e8ec17SMichael Reed };
17705e8ec17SMichael Reed 
178*35508e46SMichael Reed static int
179*35508e46SMichael Reed mptfc_block_error_handler(struct scsi_cmnd *SCpnt,
180*35508e46SMichael Reed 			  int (*func)(struct scsi_cmnd *SCpnt),
181*35508e46SMichael Reed 			  const char *caller)
182*35508e46SMichael Reed {
183*35508e46SMichael Reed 	struct scsi_device	*sdev = SCpnt->device;
184*35508e46SMichael Reed 	struct Scsi_Host	*shost = sdev->host;
185*35508e46SMichael Reed 	struct fc_rport		*rport = starget_to_rport(scsi_target(sdev));
186*35508e46SMichael Reed 	unsigned long		flags;
187*35508e46SMichael Reed 	int			ready;
188*35508e46SMichael Reed 
189*35508e46SMichael Reed 	spin_lock_irqsave(shost->host_lock, flags);
190*35508e46SMichael Reed 	while ((ready = fc_remote_port_chkready(rport) >> 16) == DID_IMM_RETRY) {
191*35508e46SMichael Reed 		spin_unlock_irqrestore(shost->host_lock, flags);
192*35508e46SMichael Reed 		dfcprintk ((MYIOC_s_INFO_FMT
193*35508e46SMichael Reed 			"mptfc_block_error_handler.%d: %d:%d, port status is "
194*35508e46SMichael Reed 			"DID_IMM_RETRY, deferring %s recovery.\n",
195*35508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->name,
196*35508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->sh->host_no,
197*35508e46SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,caller));
198*35508e46SMichael Reed 		msleep(1000);
199*35508e46SMichael Reed 		spin_lock_irqsave(shost->host_lock, flags);
200*35508e46SMichael Reed 	}
201*35508e46SMichael Reed 	spin_unlock_irqrestore(shost->host_lock, flags);
202*35508e46SMichael Reed 
203*35508e46SMichael Reed 	if (ready == DID_NO_CONNECT || !SCpnt->device->hostdata) {
204*35508e46SMichael Reed 		dfcprintk ((MYIOC_s_INFO_FMT
205*35508e46SMichael Reed 			"%s.%d: %d:%d, failing recovery, "
206*35508e46SMichael Reed 			"port state %d, vdev %p.\n", caller,
207*35508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->name,
208*35508e46SMichael Reed 			((MPT_SCSI_HOST *) shost->hostdata)->ioc->sh->host_no,
209*35508e46SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,ready,
210*35508e46SMichael Reed 			SCpnt->device->hostdata));
211*35508e46SMichael Reed 		return FAILED;
212*35508e46SMichael Reed 	}
213*35508e46SMichael Reed 	dfcprintk ((MYIOC_s_INFO_FMT
214*35508e46SMichael Reed 		"%s.%d: %d:%d, executing recovery.\n", caller,
215*35508e46SMichael Reed 		((MPT_SCSI_HOST *) shost->hostdata)->ioc->name,
216*35508e46SMichael Reed 		((MPT_SCSI_HOST *) shost->hostdata)->ioc->sh->host_no,
217*35508e46SMichael Reed 		SCpnt->device->id,SCpnt->device->lun));
218*35508e46SMichael Reed 	return (*func)(SCpnt);
219*35508e46SMichael Reed }
220*35508e46SMichael Reed 
221*35508e46SMichael Reed static int
222*35508e46SMichael Reed mptfc_abort(struct scsi_cmnd *SCpnt)
223*35508e46SMichael Reed {
224*35508e46SMichael Reed 	return
225*35508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_abort, __FUNCTION__);
226*35508e46SMichael Reed }
227*35508e46SMichael Reed 
228*35508e46SMichael Reed static int
229*35508e46SMichael Reed mptfc_dev_reset(struct scsi_cmnd *SCpnt)
230*35508e46SMichael Reed {
231*35508e46SMichael Reed 	return
232*35508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_dev_reset, __FUNCTION__);
233*35508e46SMichael Reed }
234*35508e46SMichael Reed 
235*35508e46SMichael Reed static int
236*35508e46SMichael Reed mptfc_bus_reset(struct scsi_cmnd *SCpnt)
237*35508e46SMichael Reed {
238*35508e46SMichael Reed 	return
239*35508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_bus_reset, __FUNCTION__);
240*35508e46SMichael Reed }
241*35508e46SMichael Reed 
242*35508e46SMichael Reed static int
243*35508e46SMichael Reed mptfc_host_reset(struct scsi_cmnd *SCpnt)
244*35508e46SMichael Reed {
245*35508e46SMichael Reed 	return
246*35508e46SMichael Reed 	    mptfc_block_error_handler(SCpnt, mptscsih_host_reset, __FUNCTION__);
247*35508e46SMichael Reed }
248*35508e46SMichael Reed 
24905e8ec17SMichael Reed static void
25005e8ec17SMichael Reed mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
25105e8ec17SMichael Reed {
25205e8ec17SMichael Reed 	if (timeout > 0)
25305e8ec17SMichael Reed 		rport->dev_loss_tmo = timeout;
25405e8ec17SMichael Reed 	else
25505e8ec17SMichael Reed 		rport->dev_loss_tmo = mptfc_dev_loss_tmo;
25605e8ec17SMichael Reed }
25705e8ec17SMichael Reed 
25805e8ec17SMichael Reed static int
25905e8ec17SMichael Reed mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
26005e8ec17SMichael Reed {
26105e8ec17SMichael Reed 	FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
26205e8ec17SMichael Reed 	FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
26305e8ec17SMichael Reed 
26405e8ec17SMichael Reed 	if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
26505e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
26605e8ec17SMichael Reed 			return 0;
26705e8ec17SMichael Reed 		if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
26805e8ec17SMichael Reed 			return -1;
26905e8ec17SMichael Reed 		return 1;
27005e8ec17SMichael Reed 	}
27105e8ec17SMichael Reed 	if ((*aa)->CurrentBus < (*bb)->CurrentBus)
27205e8ec17SMichael Reed 		return -1;
27305e8ec17SMichael Reed 	return 1;
27405e8ec17SMichael Reed }
27505e8ec17SMichael Reed 
27605e8ec17SMichael Reed static int
27705e8ec17SMichael Reed mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
27805e8ec17SMichael Reed 	void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
27905e8ec17SMichael Reed {
28005e8ec17SMichael Reed 	ConfigPageHeader_t	 hdr;
28105e8ec17SMichael Reed 	CONFIGPARMS		 cfg;
28205e8ec17SMichael Reed 	FCDevicePage0_t		*ppage0_alloc, *fc;
28305e8ec17SMichael Reed 	dma_addr_t		 page0_dma;
28405e8ec17SMichael Reed 	int			 data_sz;
28505e8ec17SMichael Reed 	int			 ii;
28605e8ec17SMichael Reed 
28705e8ec17SMichael Reed 	FCDevicePage0_t		*p0_array=NULL, *p_p0;
28805e8ec17SMichael Reed 	FCDevicePage0_t		**pp0_array=NULL, **p_pp0;
28905e8ec17SMichael Reed 
29005e8ec17SMichael Reed 	int			 rc = -ENOMEM;
29105e8ec17SMichael Reed 	U32			 port_id = 0xffffff;
29205e8ec17SMichael Reed 	int			 num_targ = 0;
29305e8ec17SMichael Reed 	int			 max_bus = ioc->facts.MaxBuses;
29405e8ec17SMichael Reed 	int			 max_targ = ioc->facts.MaxDevices;
29505e8ec17SMichael Reed 
29605e8ec17SMichael Reed 	if (max_bus == 0 || max_targ == 0)
29705e8ec17SMichael Reed 		goto out;
29805e8ec17SMichael Reed 
29905e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
30005e8ec17SMichael Reed 	p_p0 = p0_array =  kzalloc(data_sz, GFP_KERNEL);
30105e8ec17SMichael Reed 	if (!p0_array)
30205e8ec17SMichael Reed 		goto out;
30305e8ec17SMichael Reed 
30405e8ec17SMichael Reed 	data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
30505e8ec17SMichael Reed 	p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
30605e8ec17SMichael Reed 	if (!pp0_array)
30705e8ec17SMichael Reed 		goto out;
30805e8ec17SMichael Reed 
30905e8ec17SMichael Reed 	do {
31005e8ec17SMichael Reed 		/* Get FC Device Page 0 header */
31105e8ec17SMichael Reed 		hdr.PageVersion = 0;
31205e8ec17SMichael Reed 		hdr.PageLength = 0;
31305e8ec17SMichael Reed 		hdr.PageNumber = 0;
31405e8ec17SMichael Reed 		hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
31505e8ec17SMichael Reed 		cfg.cfghdr.hdr = &hdr;
31605e8ec17SMichael Reed 		cfg.physAddr = -1;
31705e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
31805e8ec17SMichael Reed 		cfg.dir = 0;
31905e8ec17SMichael Reed 		cfg.pageAddr = port_id;
32005e8ec17SMichael Reed 		cfg.timeout = 0;
32105e8ec17SMichael Reed 
32205e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) != 0)
32305e8ec17SMichael Reed 			break;
32405e8ec17SMichael Reed 
32505e8ec17SMichael Reed 		if (hdr.PageLength <= 0)
32605e8ec17SMichael Reed 			break;
32705e8ec17SMichael Reed 
32805e8ec17SMichael Reed 		data_sz = hdr.PageLength * 4;
32905e8ec17SMichael Reed 		ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
33005e8ec17SMichael Reed 		    					&page0_dma);
33105e8ec17SMichael Reed 		rc = -ENOMEM;
33205e8ec17SMichael Reed 		if (!ppage0_alloc)
33305e8ec17SMichael Reed 			break;
33405e8ec17SMichael Reed 
33505e8ec17SMichael Reed 		cfg.physAddr = page0_dma;
33605e8ec17SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
33705e8ec17SMichael Reed 
33805e8ec17SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
33905e8ec17SMichael Reed 			ppage0_alloc->PortIdentifier =
34005e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->PortIdentifier);
34105e8ec17SMichael Reed 
34205e8ec17SMichael Reed 			ppage0_alloc->WWNN.Low =
34305e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.Low);
34405e8ec17SMichael Reed 
34505e8ec17SMichael Reed 			ppage0_alloc->WWNN.High =
34605e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWNN.High);
34705e8ec17SMichael Reed 
34805e8ec17SMichael Reed 			ppage0_alloc->WWPN.Low =
34905e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.Low);
35005e8ec17SMichael Reed 
35105e8ec17SMichael Reed 			ppage0_alloc->WWPN.High =
35205e8ec17SMichael Reed 				le32_to_cpu(ppage0_alloc->WWPN.High);
35305e8ec17SMichael Reed 
35405e8ec17SMichael Reed 			ppage0_alloc->BBCredit =
35505e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->BBCredit);
35605e8ec17SMichael Reed 
35705e8ec17SMichael Reed 			ppage0_alloc->MaxRxFrameSize =
35805e8ec17SMichael Reed 				le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
35905e8ec17SMichael Reed 
36005e8ec17SMichael Reed 			port_id = ppage0_alloc->PortIdentifier;
36105e8ec17SMichael Reed 			num_targ++;
36205e8ec17SMichael Reed 			*p_p0 = *ppage0_alloc;	/* save data */
36305e8ec17SMichael Reed 			*p_pp0++ = p_p0++;	/* save addr */
36405e8ec17SMichael Reed 		}
36505e8ec17SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz,
36605e8ec17SMichael Reed 		    			(u8 *) ppage0_alloc, page0_dma);
36705e8ec17SMichael Reed 		if (rc != 0)
36805e8ec17SMichael Reed 			break;
36905e8ec17SMichael Reed 
37005e8ec17SMichael Reed 	} while (port_id <= 0xff0000);
37105e8ec17SMichael Reed 
37205e8ec17SMichael Reed 	if (num_targ) {
37305e8ec17SMichael Reed 		/* sort array */
37405e8ec17SMichael Reed 		if (num_targ > 1)
37505e8ec17SMichael Reed 			sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
37605e8ec17SMichael Reed 				mptfc_FcDevPage0_cmp_func, NULL);
37705e8ec17SMichael Reed 		/* call caller's func for each targ */
37805e8ec17SMichael Reed 		for (ii = 0; ii < num_targ;  ii++) {
37905e8ec17SMichael Reed 			fc = *(pp0_array+ii);
38005e8ec17SMichael Reed 			func(ioc, ioc_port, fc);
38105e8ec17SMichael Reed 		}
38205e8ec17SMichael Reed 	}
38305e8ec17SMichael Reed 
38405e8ec17SMichael Reed  out:
38505e8ec17SMichael Reed 	kfree(pp0_array);
38605e8ec17SMichael Reed 	kfree(p0_array);
38705e8ec17SMichael Reed 	return rc;
38805e8ec17SMichael Reed }
38905e8ec17SMichael Reed 
39005e8ec17SMichael Reed static int
39105e8ec17SMichael Reed mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
39205e8ec17SMichael Reed {
39305e8ec17SMichael Reed 	/* not currently usable */
39405e8ec17SMichael Reed 	if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
39505e8ec17SMichael Reed 			  MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
39605e8ec17SMichael Reed 		return -1;
39705e8ec17SMichael Reed 
39805e8ec17SMichael Reed 	if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
39905e8ec17SMichael Reed 		return -1;
40005e8ec17SMichael Reed 
40105e8ec17SMichael Reed 	if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
40205e8ec17SMichael Reed 		return -1;
40305e8ec17SMichael Reed 
40405e8ec17SMichael Reed 	/*
40505e8ec17SMichael Reed 	 * board data structure already normalized to platform endianness
40605e8ec17SMichael Reed 	 * shifted to avoid unaligned access on 64 bit architecture
40705e8ec17SMichael Reed 	 */
40805e8ec17SMichael Reed 	rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
40905e8ec17SMichael Reed 	rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
41005e8ec17SMichael Reed 	rid->port_id =   pg0->PortIdentifier;
41105e8ec17SMichael Reed 	rid->roles = FC_RPORT_ROLE_UNKNOWN;
41205e8ec17SMichael Reed 
41305e8ec17SMichael Reed 	return 0;
41405e8ec17SMichael Reed }
41505e8ec17SMichael Reed 
41605e8ec17SMichael Reed static void
41705e8ec17SMichael Reed mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
41805e8ec17SMichael Reed {
41905e8ec17SMichael Reed 	struct fc_rport_identifiers rport_ids;
42005e8ec17SMichael Reed 	struct fc_rport		*rport;
42105e8ec17SMichael Reed 	struct mptfc_rport_info	*ri;
4223bc7bf1dSMichael Reed 	int			new_ri = 1;
42365207fedSMoore, Eric 	u64			pn, nn;
4243bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
4256dd727daSmdr@sgi.com 	u32			roles = FC_RPORT_ROLE_UNKNOWN;
42605e8ec17SMichael Reed 
42705e8ec17SMichael Reed 	if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
42805e8ec17SMichael Reed 		return;
42905e8ec17SMichael Reed 
4306dd727daSmdr@sgi.com 	roles |= FC_RPORT_ROLE_FCP_TARGET;
4316dd727daSmdr@sgi.com 	if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
4326dd727daSmdr@sgi.com 		roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4336dd727daSmdr@sgi.com 
43405e8ec17SMichael Reed 	/* scan list looking for a match */
43505e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
4363bc7bf1dSMichael Reed 		pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
4373bc7bf1dSMichael Reed 		if (pn == rport_ids.port_name) {	/* match */
43805e8ec17SMichael Reed 			list_move_tail(&ri->list, &ioc->fc_rports);
4393bc7bf1dSMichael Reed 			new_ri = 0;
44005e8ec17SMichael Reed 			break;
44105e8ec17SMichael Reed 		}
44205e8ec17SMichael Reed 	}
4433bc7bf1dSMichael Reed 	if (new_ri) {	/* allocate one */
44405e8ec17SMichael Reed 		ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
44505e8ec17SMichael Reed 		if (!ri)
44605e8ec17SMichael Reed 			return;
44705e8ec17SMichael Reed 		list_add_tail(&ri->list, &ioc->fc_rports);
44805e8ec17SMichael Reed 	}
44905e8ec17SMichael Reed 
45005e8ec17SMichael Reed 	ri->pg0 = *pg0;	/* add/update pg0 data */
45105e8ec17SMichael Reed 	ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
45205e8ec17SMichael Reed 
4533bc7bf1dSMichael Reed 	/* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
45405e8ec17SMichael Reed 	if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
45505e8ec17SMichael Reed 		ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
45605e8ec17SMichael Reed 		rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
45705e8ec17SMichael Reed 		if (rport) {
45805e8ec17SMichael Reed 			ri->rport = rport;
4593bc7bf1dSMichael Reed 			if (new_ri) /* may have been reset by user */
46005e8ec17SMichael Reed 				rport->dev_loss_tmo = mptfc_dev_loss_tmo;
46105e8ec17SMichael Reed 			/*
46205e8ec17SMichael Reed 			 * if already mapped, remap here.  If not mapped,
4633bc7bf1dSMichael Reed 			 * target_alloc will allocate vtarget and map,
4643bc7bf1dSMichael Reed 			 * slave_alloc will fill in vdev from vtarget.
46505e8ec17SMichael Reed 			 */
4663bc7bf1dSMichael Reed 			if (ri->starget) {
4673bc7bf1dSMichael Reed 				vtarget = ri->starget->hostdata;
4683bc7bf1dSMichael Reed 				if (vtarget) {
4693bc7bf1dSMichael Reed 					vtarget->target_id = pg0->CurrentTargetID;
4703bc7bf1dSMichael Reed 					vtarget->bus_id = pg0->CurrentBus;
47105e8ec17SMichael Reed 				}
4723bc7bf1dSMichael Reed 			}
47365207fedSMoore, Eric 			*((struct mptfc_rport_info **)rport->dd_data) = ri;
4746dd727daSmdr@sgi.com 			/* scan will be scheduled once rport becomes a target */
4756dd727daSmdr@sgi.com 			fc_remote_port_rolechg(rport,roles);
47665207fedSMoore, Eric 
47765207fedSMoore, Eric 			pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
47865207fedSMoore, Eric 			nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
4793bc7bf1dSMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
4803bc7bf1dSMichael Reed 				"mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
48105e8ec17SMichael Reed 				"rport tid %d, tmo %d\n",
4823bc7bf1dSMichael Reed 					ioc->name,
483914c2d8eSMoore, Eric 					ioc->sh->host_no,
48405e8ec17SMichael Reed 					pg0->PortIdentifier,
48565207fedSMoore, Eric 					(unsigned long long)nn,
48665207fedSMoore, Eric 					(unsigned long long)pn,
48705e8ec17SMichael Reed 					pg0->CurrentTargetID,
48805e8ec17SMichael Reed 					ri->rport->scsi_target_id,
4893bc7bf1dSMichael Reed 					ri->rport->dev_loss_tmo));
49005e8ec17SMichael Reed 		} else {
49105e8ec17SMichael Reed 			list_del(&ri->list);
49205e8ec17SMichael Reed 			kfree(ri);
49305e8ec17SMichael Reed 			ri = NULL;
49405e8ec17SMichael Reed 		}
49505e8ec17SMichael Reed 	}
49605e8ec17SMichael Reed }
49705e8ec17SMichael Reed 
49805e8ec17SMichael Reed /*
4993bc7bf1dSMichael Reed  *	OS entry point to allow for host driver to free allocated memory
5003bc7bf1dSMichael Reed  *	Called if no device present or device being unloaded
5013bc7bf1dSMichael Reed  */
5023bc7bf1dSMichael Reed static void
5033bc7bf1dSMichael Reed mptfc_target_destroy(struct scsi_target *starget)
5043bc7bf1dSMichael Reed {
5053bc7bf1dSMichael Reed 	struct fc_rport		*rport;
5063bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
5073bc7bf1dSMichael Reed 
5083bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
5093bc7bf1dSMichael Reed 	if (rport) {
5103bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
5113bc7bf1dSMichael Reed 		if (ri)	/* better be! */
5123bc7bf1dSMichael Reed 			ri->starget = NULL;
5133bc7bf1dSMichael Reed 	}
5143bc7bf1dSMichael Reed 	if (starget->hostdata)
5153bc7bf1dSMichael Reed 		kfree(starget->hostdata);
5163bc7bf1dSMichael Reed 	starget->hostdata = NULL;
5173bc7bf1dSMichael Reed }
5183bc7bf1dSMichael Reed 
5193bc7bf1dSMichael Reed /*
5203bc7bf1dSMichael Reed  *	OS entry point to allow host driver to alloc memory
5213bc7bf1dSMichael Reed  *	for each scsi target. Called once per device the bus scan.
5223bc7bf1dSMichael Reed  *	Return non-zero if allocation fails.
5233bc7bf1dSMichael Reed  */
5243bc7bf1dSMichael Reed static int
5253bc7bf1dSMichael Reed mptfc_target_alloc(struct scsi_target *starget)
5263bc7bf1dSMichael Reed {
5273bc7bf1dSMichael Reed 	VirtTarget		*vtarget;
5283bc7bf1dSMichael Reed 	struct fc_rport		*rport;
5293bc7bf1dSMichael Reed 	struct mptfc_rport_info *ri;
5303bc7bf1dSMichael Reed 	int			rc;
5313bc7bf1dSMichael Reed 
5323bc7bf1dSMichael Reed 	vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
5333bc7bf1dSMichael Reed 	if (!vtarget)
5343bc7bf1dSMichael Reed 		return -ENOMEM;
5353bc7bf1dSMichael Reed 	starget->hostdata = vtarget;
5363bc7bf1dSMichael Reed 
5373bc7bf1dSMichael Reed 	rc = -ENODEV;
5383bc7bf1dSMichael Reed 	rport = starget_to_rport(starget);
5393bc7bf1dSMichael Reed 	if (rport) {
5403bc7bf1dSMichael Reed 		ri = *((struct mptfc_rport_info **)rport->dd_data);
5413bc7bf1dSMichael Reed 		if (ri) {	/* better be! */
5423bc7bf1dSMichael Reed 			vtarget->target_id = ri->pg0.CurrentTargetID;
5433bc7bf1dSMichael Reed 			vtarget->bus_id = ri->pg0.CurrentBus;
5443bc7bf1dSMichael Reed 			ri->starget = starget;
5453bc7bf1dSMichael Reed 			rc = 0;
5463bc7bf1dSMichael Reed 		}
5473bc7bf1dSMichael Reed 	}
5483bc7bf1dSMichael Reed 	if (rc != 0) {
5493bc7bf1dSMichael Reed 		kfree(vtarget);
5503bc7bf1dSMichael Reed 		starget->hostdata = NULL;
5513bc7bf1dSMichael Reed 	}
5523bc7bf1dSMichael Reed 
5533bc7bf1dSMichael Reed 	return rc;
5543bc7bf1dSMichael Reed }
5553bc7bf1dSMichael Reed 
5563bc7bf1dSMichael Reed /*
55705e8ec17SMichael Reed  *	OS entry point to allow host driver to alloc memory
55805e8ec17SMichael Reed  *	for each scsi device. Called once per device the bus scan.
55905e8ec17SMichael Reed  *	Return non-zero if allocation fails.
56005e8ec17SMichael Reed  *	Init memory once per LUN.
56105e8ec17SMichael Reed  */
56203fbcbcdSAdrian Bunk static int
56305e8ec17SMichael Reed mptfc_slave_alloc(struct scsi_device *sdev)
56405e8ec17SMichael Reed {
56505e8ec17SMichael Reed 	MPT_SCSI_HOST		*hd;
56605e8ec17SMichael Reed 	VirtTarget		*vtarget;
56705e8ec17SMichael Reed 	VirtDevice		*vdev;
56805e8ec17SMichael Reed 	struct scsi_target	*starget;
56905e8ec17SMichael Reed 	struct fc_rport		*rport;
57005e8ec17SMichael Reed 
57105e8ec17SMichael Reed 
57265207fedSMoore, Eric 	starget = scsi_target(sdev);
57365207fedSMoore, Eric 	rport = starget_to_rport(starget);
57405e8ec17SMichael Reed 
57505e8ec17SMichael Reed 	if (!rport || fc_remote_port_chkready(rport))
57605e8ec17SMichael Reed 		return -ENXIO;
57705e8ec17SMichael Reed 
57805e8ec17SMichael Reed 	hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
57905e8ec17SMichael Reed 
5803bc7bf1dSMichael Reed 	vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
58105e8ec17SMichael Reed 	if (!vdev) {
58205e8ec17SMichael Reed 		printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
58305e8ec17SMichael Reed 				hd->ioc->name, sizeof(VirtDevice));
58405e8ec17SMichael Reed 		return -ENOMEM;
58505e8ec17SMichael Reed 	}
58605e8ec17SMichael Reed 
58705e8ec17SMichael Reed 
58805e8ec17SMichael Reed 	sdev->hostdata = vdev;
58905e8ec17SMichael Reed 	vtarget = starget->hostdata;
5903bc7bf1dSMichael Reed 
59105e8ec17SMichael Reed 	if (vtarget->num_luns == 0) {
5923bc7bf1dSMichael Reed 		vtarget->ioc_id = hd->ioc->id;
593ba856d32SEric Moore 		vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
59405e8ec17SMichael Reed 		hd->Targets[sdev->id] = vtarget;
59505e8ec17SMichael Reed 	}
59605e8ec17SMichael Reed 
59705e8ec17SMichael Reed 	vdev->vtarget = vtarget;
59805e8ec17SMichael Reed 	vdev->lun = sdev->lun;
59905e8ec17SMichael Reed 
60005e8ec17SMichael Reed 	vtarget->num_luns++;
60105e8ec17SMichael Reed 
60265207fedSMoore, Eric 
603914c2d8eSMoore, Eric #ifdef DMPT_DEBUG_FC
604914c2d8eSMoore, Eric 	{
60565207fedSMoore, Eric 	u64 nn, pn;
606914c2d8eSMoore, Eric 	struct mptfc_rport_info *ri;
607914c2d8eSMoore, Eric 	ri = *((struct mptfc_rport_info **)rport->dd_data);
60865207fedSMoore, Eric 	pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
60965207fedSMoore, Eric 	nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
6103bc7bf1dSMichael Reed 	dfcprintk ((MYIOC_s_INFO_FMT
6113bc7bf1dSMichael Reed 		"mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
61205e8ec17SMichael Reed 	        "CurrentTargetID %d, %x %llx %llx\n",
613914c2d8eSMoore, Eric 		hd->ioc->name,
61405e8ec17SMichael Reed 		sdev->host->host_no,
61505e8ec17SMichael Reed 		vtarget->num_luns,
61605e8ec17SMichael Reed 		sdev->id, ri->pg0.CurrentTargetID,
61765207fedSMoore, Eric 		ri->pg0.PortIdentifier,
61865207fedSMoore, Eric 		(unsigned long long)pn,
61965207fedSMoore, Eric 		(unsigned long long)nn));
620914c2d8eSMoore, Eric 	}
621914c2d8eSMoore, Eric #endif
62205e8ec17SMichael Reed 
62305e8ec17SMichael Reed 	return 0;
62405e8ec17SMichael Reed }
62505e8ec17SMichael Reed 
62605e8ec17SMichael Reed static int
62705e8ec17SMichael Reed mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
62805e8ec17SMichael Reed {
6293bc7bf1dSMichael Reed 	struct mptfc_rport_info	*ri;
63005e8ec17SMichael Reed 	struct fc_rport	*rport = starget_to_rport(scsi_target(SCpnt->device));
63105e8ec17SMichael Reed 	int		err;
63205e8ec17SMichael Reed 
63305e8ec17SMichael Reed 	err = fc_remote_port_chkready(rport);
63405e8ec17SMichael Reed 	if (unlikely(err)) {
63505e8ec17SMichael Reed 		SCpnt->result = err;
63605e8ec17SMichael Reed 		done(SCpnt);
63705e8ec17SMichael Reed 		return 0;
63805e8ec17SMichael Reed 	}
6393bc7bf1dSMichael Reed 
640*35508e46SMichael Reed 	if (!SCpnt->device->hostdata) {	/* vdev */
641*35508e46SMichael Reed 		SCpnt->result = DID_NO_CONNECT << 16;
642*35508e46SMichael Reed 		done(SCpnt);
643*35508e46SMichael Reed 		return 0;
644*35508e46SMichael Reed 	}
645*35508e46SMichael Reed 
64665207fedSMoore, Eric 	/* dd_data is null until finished adding target */
64765207fedSMoore, Eric 	ri = *((struct mptfc_rport_info **)rport->dd_data);
64865207fedSMoore, Eric 	if (unlikely(!ri)) {
64965207fedSMoore, Eric 		dfcprintk ((MYIOC_s_INFO_FMT
65065207fedSMoore, Eric 			"mptfc_qcmd.%d: %d:%d, dd_data is null.\n",
65165207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
65265207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
65365207fedSMoore, Eric 			SCpnt->device->id,SCpnt->device->lun));
65465207fedSMoore, Eric 		SCpnt->result = DID_IMM_RETRY << 16;
65565207fedSMoore, Eric 		done(SCpnt);
65665207fedSMoore, Eric 		return 0;
65765207fedSMoore, Eric 	}
65865207fedSMoore, Eric 
65965207fedSMoore, Eric 	err = mptscsih_qcmd(SCpnt,done);
66065207fedSMoore, Eric #ifdef DMPT_DEBUG_FC
66165207fedSMoore, Eric 	if (unlikely(err)) {
66265207fedSMoore, Eric 		dfcprintk ((MYIOC_s_INFO_FMT
663419835e2SMichael Reed 			"mptfc_qcmd.%d: %d:%d, mptscsih_qcmd returns non-zero, (%x).\n",
66465207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
66565207fedSMoore, Eric 			((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
666419835e2SMichael Reed 			SCpnt->device->id,SCpnt->device->lun,err));
66765207fedSMoore, Eric 	}
66865207fedSMoore, Eric #endif
66965207fedSMoore, Eric 	return err;
67005e8ec17SMichael Reed }
67105e8ec17SMichael Reed 
67280d3ac77SMichael Reed /*
67380d3ac77SMichael Reed  *	mptfc_GetFcPortPage0 - Fetch FCPort config Page0.
67480d3ac77SMichael Reed  *	@ioc: Pointer to MPT_ADAPTER structure
67580d3ac77SMichael Reed  *	@portnum: IOC Port number
67680d3ac77SMichael Reed  *
67780d3ac77SMichael Reed  *	Return: 0 for success
67880d3ac77SMichael Reed  *	-ENOMEM if no memory available
67980d3ac77SMichael Reed  *		-EPERM if not allowed due to ISR context
68080d3ac77SMichael Reed  *		-EAGAIN if no msg frames currently available
68180d3ac77SMichael Reed  *		-EFAULT for non-successful reply or no reply (timeout)
68280d3ac77SMichael Reed  *		-EINVAL portnum arg out of range (hardwired to two elements)
68380d3ac77SMichael Reed  */
68480d3ac77SMichael Reed static int
68580d3ac77SMichael Reed mptfc_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
68680d3ac77SMichael Reed {
68780d3ac77SMichael Reed 	ConfigPageHeader_t	 hdr;
68880d3ac77SMichael Reed 	CONFIGPARMS		 cfg;
68980d3ac77SMichael Reed 	FCPortPage0_t		*ppage0_alloc;
69080d3ac77SMichael Reed 	FCPortPage0_t		*pp0dest;
69180d3ac77SMichael Reed 	dma_addr_t		 page0_dma;
69280d3ac77SMichael Reed 	int			 data_sz;
69380d3ac77SMichael Reed 	int			 copy_sz;
69480d3ac77SMichael Reed 	int			 rc;
69580d3ac77SMichael Reed 	int			 count = 400;
69680d3ac77SMichael Reed 
69780d3ac77SMichael Reed 	if (portnum > 1)
69880d3ac77SMichael Reed 		return -EINVAL;
69980d3ac77SMichael Reed 
70080d3ac77SMichael Reed 	/* Get FCPort Page 0 header */
70180d3ac77SMichael Reed 	hdr.PageVersion = 0;
70280d3ac77SMichael Reed 	hdr.PageLength = 0;
70380d3ac77SMichael Reed 	hdr.PageNumber = 0;
70480d3ac77SMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
70580d3ac77SMichael Reed 	cfg.cfghdr.hdr = &hdr;
70680d3ac77SMichael Reed 	cfg.physAddr = -1;
70780d3ac77SMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
70880d3ac77SMichael Reed 	cfg.dir = 0;
70980d3ac77SMichael Reed 	cfg.pageAddr = portnum;
71080d3ac77SMichael Reed 	cfg.timeout = 0;
71180d3ac77SMichael Reed 
71280d3ac77SMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
71380d3ac77SMichael Reed 		return rc;
71480d3ac77SMichael Reed 
71580d3ac77SMichael Reed 	if (hdr.PageLength == 0)
71680d3ac77SMichael Reed 		return 0;
71780d3ac77SMichael Reed 
71880d3ac77SMichael Reed 	data_sz = hdr.PageLength * 4;
71980d3ac77SMichael Reed 	rc = -ENOMEM;
72080d3ac77SMichael Reed 	ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
72180d3ac77SMichael Reed 	if (ppage0_alloc) {
72280d3ac77SMichael Reed 
72380d3ac77SMichael Reed  try_again:
72480d3ac77SMichael Reed 		memset((u8 *)ppage0_alloc, 0, data_sz);
72580d3ac77SMichael Reed 		cfg.physAddr = page0_dma;
72680d3ac77SMichael Reed 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
72780d3ac77SMichael Reed 
72880d3ac77SMichael Reed 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
72980d3ac77SMichael Reed 			/* save the data */
73080d3ac77SMichael Reed 			pp0dest = &ioc->fc_port_page0[portnum];
73180d3ac77SMichael Reed 			copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
73280d3ac77SMichael Reed 			memcpy(pp0dest, ppage0_alloc, copy_sz);
73380d3ac77SMichael Reed 
73480d3ac77SMichael Reed 			/*
73580d3ac77SMichael Reed 			 *	Normalize endianness of structure data,
73680d3ac77SMichael Reed 			 *	by byte-swapping all > 1 byte fields!
73780d3ac77SMichael Reed 			 */
73880d3ac77SMichael Reed 			pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
73980d3ac77SMichael Reed 			pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
74080d3ac77SMichael Reed 			pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
74180d3ac77SMichael Reed 			pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
74280d3ac77SMichael Reed 			pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
74380d3ac77SMichael Reed 			pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
74480d3ac77SMichael Reed 			pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
74580d3ac77SMichael Reed 			pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
74680d3ac77SMichael Reed 			pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
74780d3ac77SMichael Reed 			pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
74880d3ac77SMichael Reed 			pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
74980d3ac77SMichael Reed 			pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
75080d3ac77SMichael Reed 			pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
75180d3ac77SMichael Reed 			pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
75280d3ac77SMichael Reed 			pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
75380d3ac77SMichael Reed 			pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
75480d3ac77SMichael Reed 
75580d3ac77SMichael Reed 			/*
75680d3ac77SMichael Reed 			 * if still doing discovery,
75780d3ac77SMichael Reed 			 * hang loose a while until finished
75880d3ac77SMichael Reed 			 */
75977d88ee2SMichael Reed 			if ((pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) ||
76077d88ee2SMichael Reed 			    (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE &&
76177d88ee2SMichael Reed 			     (pp0dest->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_TYPE_MASK)
76277d88ee2SMichael Reed 			      == MPI_FCPORTPAGE0_FLAGS_ATTACH_NO_INIT)) {
76380d3ac77SMichael Reed 				if (count-- > 0) {
764d6be06c8SMichael Reed 					msleep(100);
76580d3ac77SMichael Reed 					goto try_again;
76680d3ac77SMichael Reed 				}
76780d3ac77SMichael Reed 				printk(MYIOC_s_INFO_FMT "Firmware discovery not"
76880d3ac77SMichael Reed 							" complete.\n",
76980d3ac77SMichael Reed 						ioc->name);
77080d3ac77SMichael Reed 			}
77180d3ac77SMichael Reed 		}
77280d3ac77SMichael Reed 
77380d3ac77SMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
77480d3ac77SMichael Reed 	}
77580d3ac77SMichael Reed 
77680d3ac77SMichael Reed 	return rc;
77780d3ac77SMichael Reed }
77880d3ac77SMichael Reed 
779ca2f938eSMichael Reed static int
780ca2f938eSMichael Reed mptfc_WriteFcPortPage1(MPT_ADAPTER *ioc, int portnum)
781ca2f938eSMichael Reed {
782ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
783ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
784ca2f938eSMichael Reed 	int			 rc;
785ca2f938eSMichael Reed 
786ca2f938eSMichael Reed 	if (portnum > 1)
787ca2f938eSMichael Reed 		return -EINVAL;
788ca2f938eSMichael Reed 
789ca2f938eSMichael Reed 	if (!(ioc->fc_data.fc_port_page1[portnum].data))
790ca2f938eSMichael Reed 		return -EINVAL;
791ca2f938eSMichael Reed 
792ca2f938eSMichael Reed 	/* get fcport page 1 header */
793ca2f938eSMichael Reed 	hdr.PageVersion = 0;
794ca2f938eSMichael Reed 	hdr.PageLength = 0;
795ca2f938eSMichael Reed 	hdr.PageNumber = 1;
796ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
797ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
798ca2f938eSMichael Reed 	cfg.physAddr = -1;
799ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
800ca2f938eSMichael Reed 	cfg.dir = 0;
801ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
802ca2f938eSMichael Reed 	cfg.timeout = 0;
803ca2f938eSMichael Reed 
804ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
805ca2f938eSMichael Reed 		return rc;
806ca2f938eSMichael Reed 
807ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
808ca2f938eSMichael Reed 		return -ENODEV;
809ca2f938eSMichael Reed 
810ca2f938eSMichael Reed 	if (hdr.PageLength*4 != ioc->fc_data.fc_port_page1[portnum].pg_sz)
811ca2f938eSMichael Reed 		return -EINVAL;
812ca2f938eSMichael Reed 
813ca2f938eSMichael Reed 	cfg.physAddr = ioc->fc_data.fc_port_page1[portnum].dma;
814ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
815ca2f938eSMichael Reed 	cfg.dir = 1;
816ca2f938eSMichael Reed 
817ca2f938eSMichael Reed 	rc = mpt_config(ioc, &cfg);
818ca2f938eSMichael Reed 
819ca2f938eSMichael Reed 	return rc;
820ca2f938eSMichael Reed }
821ca2f938eSMichael Reed 
822ca2f938eSMichael Reed static int
823ca2f938eSMichael Reed mptfc_GetFcPortPage1(MPT_ADAPTER *ioc, int portnum)
824ca2f938eSMichael Reed {
825ca2f938eSMichael Reed 	ConfigPageHeader_t	 hdr;
826ca2f938eSMichael Reed 	CONFIGPARMS		 cfg;
827ca2f938eSMichael Reed 	FCPortPage1_t		*page1_alloc;
828ca2f938eSMichael Reed 	dma_addr_t		 page1_dma;
829ca2f938eSMichael Reed 	int			 data_sz;
830ca2f938eSMichael Reed 	int			 rc;
831ca2f938eSMichael Reed 
832ca2f938eSMichael Reed 	if (portnum > 1)
833ca2f938eSMichael Reed 		return -EINVAL;
834ca2f938eSMichael Reed 
835ca2f938eSMichael Reed 	/* get fcport page 1 header */
836ca2f938eSMichael Reed 	hdr.PageVersion = 0;
837ca2f938eSMichael Reed 	hdr.PageLength = 0;
838ca2f938eSMichael Reed 	hdr.PageNumber = 1;
839ca2f938eSMichael Reed 	hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
840ca2f938eSMichael Reed 	cfg.cfghdr.hdr = &hdr;
841ca2f938eSMichael Reed 	cfg.physAddr = -1;
842ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
843ca2f938eSMichael Reed 	cfg.dir = 0;
844ca2f938eSMichael Reed 	cfg.pageAddr = portnum;
845ca2f938eSMichael Reed 	cfg.timeout = 0;
846ca2f938eSMichael Reed 
847ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) != 0)
848ca2f938eSMichael Reed 		return rc;
849ca2f938eSMichael Reed 
850ca2f938eSMichael Reed 	if (hdr.PageLength == 0)
851ca2f938eSMichael Reed 		return -ENODEV;
852ca2f938eSMichael Reed 
853ca2f938eSMichael Reed start_over:
854ca2f938eSMichael Reed 
855ca2f938eSMichael Reed 	if (ioc->fc_data.fc_port_page1[portnum].data == NULL) {
856ca2f938eSMichael Reed 		data_sz = hdr.PageLength * 4;
857ca2f938eSMichael Reed 		if (data_sz < sizeof(FCPortPage1_t))
858ca2f938eSMichael Reed 			data_sz = sizeof(FCPortPage1_t);
859ca2f938eSMichael Reed 
860ca2f938eSMichael Reed 		page1_alloc = (FCPortPage1_t *) pci_alloc_consistent(ioc->pcidev,
861ca2f938eSMichael Reed 						data_sz,
862ca2f938eSMichael Reed 						&page1_dma);
863ca2f938eSMichael Reed 		if (!page1_alloc)
864ca2f938eSMichael Reed 			return -ENOMEM;
865ca2f938eSMichael Reed 	}
866ca2f938eSMichael Reed 	else {
867ca2f938eSMichael Reed 		page1_alloc = ioc->fc_data.fc_port_page1[portnum].data;
868ca2f938eSMichael Reed 		page1_dma = ioc->fc_data.fc_port_page1[portnum].dma;
869ca2f938eSMichael Reed 		data_sz = ioc->fc_data.fc_port_page1[portnum].pg_sz;
870ca2f938eSMichael Reed 		if (hdr.PageLength * 4 > data_sz) {
871ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[portnum].data = NULL;
872ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
873ca2f938eSMichael Reed 				page1_alloc, page1_dma);
874ca2f938eSMichael Reed 			goto start_over;
875ca2f938eSMichael Reed 		}
876ca2f938eSMichael Reed 	}
877ca2f938eSMichael Reed 
878ca2f938eSMichael Reed 	memset(page1_alloc,0,data_sz);
879ca2f938eSMichael Reed 
880ca2f938eSMichael Reed 	cfg.physAddr = page1_dma;
881ca2f938eSMichael Reed 	cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
882ca2f938eSMichael Reed 
883ca2f938eSMichael Reed 	if ((rc = mpt_config(ioc, &cfg)) == 0) {
884ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = page1_alloc;
885ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].pg_sz = data_sz;
886ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].dma = page1_dma;
887ca2f938eSMichael Reed 	}
888ca2f938eSMichael Reed 	else {
889ca2f938eSMichael Reed 		ioc->fc_data.fc_port_page1[portnum].data = NULL;
890ca2f938eSMichael Reed 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
891ca2f938eSMichael Reed 			page1_alloc, page1_dma);
892ca2f938eSMichael Reed 	}
893ca2f938eSMichael Reed 
894ca2f938eSMichael Reed 	return rc;
895ca2f938eSMichael Reed }
896ca2f938eSMichael Reed 
897ca2f938eSMichael Reed static void
898ca2f938eSMichael Reed mptfc_SetFcPortPage1_defaults(MPT_ADAPTER *ioc)
899ca2f938eSMichael Reed {
900ca2f938eSMichael Reed 	int		ii;
901ca2f938eSMichael Reed 	FCPortPage1_t	*pp1;
902ca2f938eSMichael Reed 
903ca2f938eSMichael Reed 	#define MPTFC_FW_DEVICE_TIMEOUT	(1)
904ca2f938eSMichael Reed 	#define MPTFC_FW_IO_PEND_TIMEOUT (1)
905ca2f938eSMichael Reed 	#define ON_FLAGS  (MPI_FCPORTPAGE1_FLAGS_IMMEDIATE_ERROR_REPLY)
906ca2f938eSMichael Reed 	#define OFF_FLAGS (MPI_FCPORTPAGE1_FLAGS_VERBOSE_RESCAN_EVENTS)
907ca2f938eSMichael Reed 
908ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
909ca2f938eSMichael Reed 		if (mptfc_GetFcPortPage1(ioc, ii) != 0)
910ca2f938eSMichael Reed 			continue;
911ca2f938eSMichael Reed 		pp1 = ioc->fc_data.fc_port_page1[ii].data;
912ca2f938eSMichael Reed 		if ((pp1->InitiatorDeviceTimeout == MPTFC_FW_DEVICE_TIMEOUT)
913ca2f938eSMichael Reed 		 && (pp1->InitiatorIoPendTimeout == MPTFC_FW_IO_PEND_TIMEOUT)
914ca2f938eSMichael Reed 		 && ((pp1->Flags & ON_FLAGS) == ON_FLAGS)
915ca2f938eSMichael Reed 		 && ((pp1->Flags & OFF_FLAGS) == 0))
916ca2f938eSMichael Reed 			continue;
917ca2f938eSMichael Reed 		pp1->InitiatorDeviceTimeout = MPTFC_FW_DEVICE_TIMEOUT;
918ca2f938eSMichael Reed 		pp1->InitiatorIoPendTimeout = MPTFC_FW_IO_PEND_TIMEOUT;
919ca2f938eSMichael Reed 		pp1->Flags &= ~OFF_FLAGS;
920ca2f938eSMichael Reed 		pp1->Flags |= ON_FLAGS;
921ca2f938eSMichael Reed 		mptfc_WriteFcPortPage1(ioc, ii);
922ca2f938eSMichael Reed 	}
923ca2f938eSMichael Reed }
924ca2f938eSMichael Reed 
925ca2f938eSMichael Reed 
92605e8ec17SMichael Reed static void
92705e8ec17SMichael Reed mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
92805e8ec17SMichael Reed {
9295d947f2bSMichael Reed 	unsigned	class = 0;
9305d947f2bSMichael Reed 	unsigned	cos = 0;
9315d947f2bSMichael Reed 	unsigned	speed;
9325d947f2bSMichael Reed 	unsigned	port_type;
9335d947f2bSMichael Reed 	unsigned	port_state;
9345d947f2bSMichael Reed 	FCPortPage0_t	*pp0;
9355d947f2bSMichael Reed 	struct Scsi_Host *sh;
9365d947f2bSMichael Reed 	char		*sn;
93705e8ec17SMichael Reed 
93805e8ec17SMichael Reed 	/* don't know what to do as only one scsi (fc) host was allocated */
93905e8ec17SMichael Reed 	if (portnum != 0)
94005e8ec17SMichael Reed 		return;
94105e8ec17SMichael Reed 
9425d947f2bSMichael Reed 	pp0 = &ioc->fc_port_page0[portnum];
9435d947f2bSMichael Reed 	sh = ioc->sh;
9445d947f2bSMichael Reed 
9455d947f2bSMichael Reed 	sn = fc_host_symbolic_name(sh);
9465d947f2bSMichael Reed 	snprintf(sn, FC_SYMBOLIC_NAME_SIZE, "%s %s%08xh",
9475d947f2bSMichael Reed 	    ioc->prod_name,
9485d947f2bSMichael Reed 	    MPT_FW_REV_MAGIC_ID_STRING,
9495d947f2bSMichael Reed 	    ioc->facts.FWVersion.Word);
9505d947f2bSMichael Reed 
9515d947f2bSMichael Reed 	fc_host_tgtid_bind_type(sh) = FC_TGTID_BIND_BY_WWPN;
9525d947f2bSMichael Reed 
9535d947f2bSMichael Reed 	fc_host_maxframe_size(sh) = pp0->MaxFrameSize;
9545d947f2bSMichael Reed 
9555d947f2bSMichael Reed 	fc_host_node_name(sh) =
9565d947f2bSMichael Reed 	    	(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
9575d947f2bSMichael Reed 
9585d947f2bSMichael Reed 	fc_host_port_name(sh) =
9595d947f2bSMichael Reed 	    	(u64)pp0->WWPN.High << 32 | (u64)pp0->WWPN.Low;
9605d947f2bSMichael Reed 
9615d947f2bSMichael Reed 	fc_host_port_id(sh) = pp0->PortIdentifier;
9625d947f2bSMichael Reed 
9635d947f2bSMichael Reed 	class = pp0->SupportedServiceClass;
96405e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
96505e8ec17SMichael Reed 		cos |= FC_COS_CLASS1;
96605e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
96705e8ec17SMichael Reed 		cos |= FC_COS_CLASS2;
96805e8ec17SMichael Reed 	if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
96905e8ec17SMichael Reed 		cos |= FC_COS_CLASS3;
9705d947f2bSMichael Reed 	fc_host_supported_classes(sh) = cos;
97105e8ec17SMichael Reed 
9725d947f2bSMichael Reed 	if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT)
9735d947f2bSMichael Reed 		speed = FC_PORTSPEED_1GBIT;
9745d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT)
9755d947f2bSMichael Reed 		speed = FC_PORTSPEED_2GBIT;
9765d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT)
9775d947f2bSMichael Reed 		speed = FC_PORTSPEED_4GBIT;
9785d947f2bSMichael Reed 	else if (pp0->CurrentSpeed == MPI_FCPORTPAGE0_CURRENT_SPEED_10GBIT)
9795d947f2bSMichael Reed 		speed = FC_PORTSPEED_10GBIT;
9805d947f2bSMichael Reed 	else
9815d947f2bSMichael Reed 		speed = FC_PORTSPEED_UNKNOWN;
9825d947f2bSMichael Reed 	fc_host_speed(sh) = speed;
98305e8ec17SMichael Reed 
9845d947f2bSMichael Reed 	speed = 0;
9855d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_1GBIT_SPEED)
9865d947f2bSMichael Reed 		speed |= FC_PORTSPEED_1GBIT;
9875d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_2GBIT_SPEED)
9885d947f2bSMichael Reed 		speed |= FC_PORTSPEED_2GBIT;
9895d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_4GBIT_SPEED)
9905d947f2bSMichael Reed 		speed |= FC_PORTSPEED_4GBIT;
9915d947f2bSMichael Reed 	if (pp0->SupportedSpeeds & MPI_FCPORTPAGE0_SUPPORT_10GBIT_SPEED)
9925d947f2bSMichael Reed 		speed |= FC_PORTSPEED_10GBIT;
9935d947f2bSMichael Reed 	fc_host_supported_speeds(sh) = speed;
99405e8ec17SMichael Reed 
9955d947f2bSMichael Reed 	port_state = FC_PORTSTATE_UNKNOWN;
9965d947f2bSMichael Reed 	if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE)
9975d947f2bSMichael Reed 		port_state = FC_PORTSTATE_ONLINE;
9985d947f2bSMichael Reed 	else if (pp0->PortState == MPI_FCPORTPAGE0_PORTSTATE_OFFLINE)
9995d947f2bSMichael Reed 		port_state = FC_PORTSTATE_LINKDOWN;
10005d947f2bSMichael Reed 	fc_host_port_state(sh) = port_state;
100105e8ec17SMichael Reed 
10025d947f2bSMichael Reed 	port_type = FC_PORTTYPE_UNKNOWN;
10035d947f2bSMichael Reed 	if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_POINT_TO_POINT)
10045d947f2bSMichael Reed 		port_type = FC_PORTTYPE_PTP;
10055d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PRIVATE_LOOP)
10065d947f2bSMichael Reed 		port_type = FC_PORTTYPE_LPORT;
10075d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_PUBLIC_LOOP)
10085d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NLPORT;
10095d947f2bSMichael Reed 	else if (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_FABRIC_DIRECT)
10105d947f2bSMichael Reed 		port_type = FC_PORTTYPE_NPORT;
10115d947f2bSMichael Reed 	fc_host_port_type(sh) = port_type;
101205e8ec17SMichael Reed 
10135d947f2bSMichael Reed 	fc_host_fabric_name(sh) =
10145d947f2bSMichael Reed 	    (pp0->Flags & MPI_FCPORTPAGE0_FLAGS_FABRIC_WWN_VALID) ?
10155d947f2bSMichael Reed 		(u64) pp0->FabricWWNN.High << 32 | (u64) pp0->FabricWWPN.Low :
10165d947f2bSMichael Reed 		(u64)pp0->WWNN.High << 32 | (u64)pp0->WWNN.Low;
10175d947f2bSMichael Reed 
101805e8ec17SMichael Reed }
101905e8ec17SMichael Reed 
102005e8ec17SMichael Reed static void
1021419835e2SMichael Reed mptfc_setup_reset(void *arg)
1022419835e2SMichael Reed {
1023419835e2SMichael Reed 	MPT_ADAPTER		*ioc = (MPT_ADAPTER *)arg;
1024419835e2SMichael Reed 	u64			pn;
1025419835e2SMichael Reed 	struct mptfc_rport_info *ri;
1026419835e2SMichael Reed 
1027419835e2SMichael Reed 	/* reset about to happen, delete (block) all rports */
1028419835e2SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
1029419835e2SMichael Reed 		if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
1030419835e2SMichael Reed 			ri->flags &= ~MPT_RPORT_INFO_FLAGS_REGISTERED;
1031419835e2SMichael Reed 			fc_remote_port_delete(ri->rport);	/* won't sleep */
1032419835e2SMichael Reed 			ri->rport = NULL;
1033419835e2SMichael Reed 
1034419835e2SMichael Reed 			pn = (u64)ri->pg0.WWPN.High << 32 |
1035419835e2SMichael Reed 			     (u64)ri->pg0.WWPN.Low;
1036419835e2SMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
1037419835e2SMichael Reed 				"mptfc_setup_reset.%d: %llx deleted\n",
1038419835e2SMichael Reed 				ioc->name,
1039419835e2SMichael Reed 				ioc->sh->host_no,
1040419835e2SMichael Reed 				(unsigned long long)pn));
1041419835e2SMichael Reed 		}
1042419835e2SMichael Reed 	}
1043419835e2SMichael Reed }
1044419835e2SMichael Reed 
1045419835e2SMichael Reed static void
104605e8ec17SMichael Reed mptfc_rescan_devices(void *arg)
104705e8ec17SMichael Reed {
104805e8ec17SMichael Reed 	MPT_ADAPTER		*ioc = (MPT_ADAPTER *)arg;
104905e8ec17SMichael Reed 	int			ii;
105065207fedSMoore, Eric 	u64			pn;
105105e8ec17SMichael Reed 	struct mptfc_rport_info *ri;
105205e8ec17SMichael Reed 
105305e8ec17SMichael Reed 	/* start by tagging all ports as missing */
105405e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
105505e8ec17SMichael Reed 		if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
105605e8ec17SMichael Reed 			ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
105705e8ec17SMichael Reed 		}
105805e8ec17SMichael Reed 	}
105905e8ec17SMichael Reed 
106005e8ec17SMichael Reed 	/*
106105e8ec17SMichael Reed 	 * now rescan devices known to adapter,
106205e8ec17SMichael Reed 	 * will reregister existing rports
106305e8ec17SMichael Reed 	 */
106405e8ec17SMichael Reed 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
106580d3ac77SMichael Reed 		(void) mptfc_GetFcPortPage0(ioc, ii);
106605e8ec17SMichael Reed 		mptfc_init_host_attr(ioc, ii);	/* refresh */
106705e8ec17SMichael Reed 		mptfc_GetFcDevPage0(ioc, ii, mptfc_register_dev);
106805e8ec17SMichael Reed 	}
106905e8ec17SMichael Reed 
107005e8ec17SMichael Reed 	/* delete devices still missing */
107105e8ec17SMichael Reed 	list_for_each_entry(ri, &ioc->fc_rports, list) {
107205e8ec17SMichael Reed 		/* if newly missing, delete it */
107365207fedSMoore, Eric 		if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
107405e8ec17SMichael Reed 
107505e8ec17SMichael Reed 			ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
107605e8ec17SMichael Reed 				       MPT_RPORT_INFO_FLAGS_MISSING);
107765207fedSMoore, Eric 			fc_remote_port_delete(ri->rport);	/* won't sleep */
10783bc7bf1dSMichael Reed 			ri->rport = NULL;
107965207fedSMoore, Eric 
108065207fedSMoore, Eric 			pn = (u64)ri->pg0.WWPN.High << 32 |
108165207fedSMoore, Eric 			     (u64)ri->pg0.WWPN.Low;
10823bc7bf1dSMichael Reed 			dfcprintk ((MYIOC_s_INFO_FMT
10833bc7bf1dSMichael Reed 				"mptfc_rescan.%d: %llx deleted\n",
10843bc7bf1dSMichael Reed 				ioc->name,
10853bc7bf1dSMichael Reed 				ioc->sh->host_no,
108665207fedSMoore, Eric 				(unsigned long long)pn));
108705e8ec17SMichael Reed 		}
108805e8ec17SMichael Reed 	}
108905e8ec17SMichael Reed }
109005e8ec17SMichael Reed 
10912496af39SMoore, Eric Dean  static int
10922496af39SMoore, Eric Dean  mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
10932496af39SMoore, Eric Dean  {
10942496af39SMoore, Eric Dean  	struct Scsi_Host	*sh;
10952496af39SMoore, Eric Dean  	MPT_SCSI_HOST		*hd;
10962496af39SMoore, Eric Dean  	MPT_ADAPTER 		*ioc;
10972496af39SMoore, Eric Dean  	unsigned long		 flags;
10981ca00bb7SChristoph Hellwig 	int			 ii;
10992496af39SMoore, Eric Dean  	int			 numSGE = 0;
11002496af39SMoore, Eric Dean  	int			 scale;
11012496af39SMoore, Eric Dean  	int			 ioc_cap;
11022496af39SMoore, Eric Dean  	int			error=0;
11032496af39SMoore, Eric Dean  	int			r;
11042496af39SMoore, Eric Dean  
11052496af39SMoore, Eric Dean  	if ((r = mpt_attach(pdev,id)) != 0)
11062496af39SMoore, Eric Dean  		return r;
11072496af39SMoore, Eric Dean  
11082496af39SMoore, Eric Dean  	ioc = pci_get_drvdata(pdev);
1109d335cc38SMoore, Eric Dean  	ioc->DoneCtx = mptfcDoneCtx;
1110d335cc38SMoore, Eric Dean  	ioc->TaskCtx = mptfcTaskCtx;
1111d335cc38SMoore, Eric Dean  	ioc->InternalCtx = mptfcInternalCtx;
11122496af39SMoore, Eric Dean  
11132496af39SMoore, Eric Dean  	/*  Added sanity check on readiness of the MPT adapter.
11142496af39SMoore, Eric Dean  	 */
11152496af39SMoore, Eric Dean  	if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
11162496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
11172496af39SMoore, Eric Dean  		  "Skipping because it's not operational!\n",
11182496af39SMoore, Eric Dean  		  ioc->name);
11197acec1e7SMoore, Eric Dean 		error = -ENODEV;
11207acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11212496af39SMoore, Eric Dean  	}
11222496af39SMoore, Eric Dean  
11232496af39SMoore, Eric Dean  	if (!ioc->active) {
11242496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
11252496af39SMoore, Eric Dean  		  ioc->name);
11267acec1e7SMoore, Eric Dean 		error = -ENODEV;
11277acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11282496af39SMoore, Eric Dean  	}
11292496af39SMoore, Eric Dean  
11302496af39SMoore, Eric Dean  	/*  Sanity check - ensure at least 1 port is INITIATOR capable
11312496af39SMoore, Eric Dean  	 */
11322496af39SMoore, Eric Dean  	ioc_cap = 0;
11332496af39SMoore, Eric Dean  	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
11342496af39SMoore, Eric Dean  		if (ioc->pfacts[ii].ProtocolFlags &
11352496af39SMoore, Eric Dean  		    MPI_PORTFACTS_PROTOCOL_INITIATOR)
11362496af39SMoore, Eric Dean  			ioc_cap ++;
11372496af39SMoore, Eric Dean  	}
11382496af39SMoore, Eric Dean  
11392496af39SMoore, Eric Dean  	if (!ioc_cap) {
11402496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
11412496af39SMoore, Eric Dean  			"Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
11422496af39SMoore, Eric Dean  			ioc->name, ioc);
114305e8ec17SMichael Reed 		return -ENODEV;
11442496af39SMoore, Eric Dean  	}
11452496af39SMoore, Eric Dean  
11462496af39SMoore, Eric Dean  	sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
11472496af39SMoore, Eric Dean  
11482496af39SMoore, Eric Dean  	if (!sh) {
11492496af39SMoore, Eric Dean  		printk(MYIOC_s_WARN_FMT
11502496af39SMoore, Eric Dean  			"Unable to register controller with SCSI subsystem\n",
11512496af39SMoore, Eric Dean  			ioc->name);
11527acec1e7SMoore, Eric Dean 		error = -1;
11537acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
11542496af39SMoore, Eric Dean          }
11552496af39SMoore, Eric Dean  
115680d3ac77SMichael Reed 	spin_lock_init(&ioc->fc_rescan_work_lock);
115705e8ec17SMichael Reed 	INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices,(void *)ioc);
1158419835e2SMichael Reed 	INIT_WORK(&ioc->fc_setup_reset_work, mptfc_setup_reset, (void *)ioc);
115905e8ec17SMichael Reed 
11602496af39SMoore, Eric Dean  	spin_lock_irqsave(&ioc->FreeQlock, flags);
11612496af39SMoore, Eric Dean  
11622496af39SMoore, Eric Dean  	/* Attach the SCSI Host to the IOC structure
11632496af39SMoore, Eric Dean  	 */
11642496af39SMoore, Eric Dean  	ioc->sh = sh;
11652496af39SMoore, Eric Dean  
11662496af39SMoore, Eric Dean  	sh->io_port = 0;
11672496af39SMoore, Eric Dean  	sh->n_io_port = 0;
11682496af39SMoore, Eric Dean  	sh->irq = 0;
11692496af39SMoore, Eric Dean  
11702496af39SMoore, Eric Dean  	/* set 16 byte cdb's */
11712496af39SMoore, Eric Dean  	sh->max_cmd_len = 16;
11722496af39SMoore, Eric Dean  
11732496af39SMoore, Eric Dean  	sh->max_id = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
11742496af39SMoore, Eric Dean  
11752496af39SMoore, Eric Dean  	sh->max_lun = MPT_LAST_LUN + 1;
11762496af39SMoore, Eric Dean  	sh->max_channel = 0;
11772496af39SMoore, Eric Dean  	sh->this_id = ioc->pfacts[0].PortSCSIID;
11782496af39SMoore, Eric Dean  
11792496af39SMoore, Eric Dean  	/* Required entry.
11802496af39SMoore, Eric Dean  	 */
11812496af39SMoore, Eric Dean  	sh->unique_id = ioc->id;
11822496af39SMoore, Eric Dean  
11832496af39SMoore, Eric Dean  	/* Verify that we won't exceed the maximum
11842496af39SMoore, Eric Dean  	 * number of chain buffers
11852496af39SMoore, Eric Dean  	 * We can optimize:  ZZ = req_sz/sizeof(SGE)
11862496af39SMoore, Eric Dean  	 * For 32bit SGE's:
11872496af39SMoore, Eric Dean  	 *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
11882496af39SMoore, Eric Dean  	 *               + (req_sz - 64)/sizeof(SGE)
11892496af39SMoore, Eric Dean  	 * A slightly different algorithm is required for
11902496af39SMoore, Eric Dean  	 * 64bit SGEs.
11912496af39SMoore, Eric Dean  	 */
11922496af39SMoore, Eric Dean  	scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
11932496af39SMoore, Eric Dean  	if (sizeof(dma_addr_t) == sizeof(u64)) {
11942496af39SMoore, Eric Dean  		numSGE = (scale - 1) *
11952496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
11962496af39SMoore, Eric Dean  		  (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
11972496af39SMoore, Eric Dean  		  sizeof(u32));
11982496af39SMoore, Eric Dean  	} else {
11992496af39SMoore, Eric Dean  		numSGE = 1 + (scale - 1) *
12002496af39SMoore, Eric Dean  		  (ioc->facts.MaxChainDepth-1) + scale +
12012496af39SMoore, Eric Dean  		  (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
12022496af39SMoore, Eric Dean  		  sizeof(u32));
12032496af39SMoore, Eric Dean  	}
12042496af39SMoore, Eric Dean  
12052496af39SMoore, Eric Dean  	if (numSGE < sh->sg_tablesize) {
12062496af39SMoore, Eric Dean  		/* Reset this value */
12072496af39SMoore, Eric Dean  		dprintk((MYIOC_s_INFO_FMT
12082496af39SMoore, Eric Dean  		  "Resetting sg_tablesize to %d from %d\n",
12092496af39SMoore, Eric Dean  		  ioc->name, numSGE, sh->sg_tablesize));
12102496af39SMoore, Eric Dean  		sh->sg_tablesize = numSGE;
12112496af39SMoore, Eric Dean  	}
12122496af39SMoore, Eric Dean  
12132496af39SMoore, Eric Dean  	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
12142496af39SMoore, Eric Dean  
12152496af39SMoore, Eric Dean  	hd = (MPT_SCSI_HOST *) sh->hostdata;
12162496af39SMoore, Eric Dean  	hd->ioc = ioc;
12172496af39SMoore, Eric Dean  
12182496af39SMoore, Eric Dean  	/* SCSI needs scsi_cmnd lookup table!
12192496af39SMoore, Eric Dean  	 * (with size equal to req_depth*PtrSz!)
12202496af39SMoore, Eric Dean  	 */
12211ca00bb7SChristoph Hellwig 	hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
12221ca00bb7SChristoph Hellwig 	if (!hd->ScsiLookup) {
12232496af39SMoore, Eric Dean  		error = -ENOMEM;
12247acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12252496af39SMoore, Eric Dean  	}
12262496af39SMoore, Eric Dean  
12271ca00bb7SChristoph Hellwig 	dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
12281ca00bb7SChristoph Hellwig 		 ioc->name, hd->ScsiLookup));
12292496af39SMoore, Eric Dean  
12302496af39SMoore, Eric Dean  	/* Allocate memory for the device structures.
12312496af39SMoore, Eric Dean  	 * A non-Null pointer at an offset
12322496af39SMoore, Eric Dean  	 * indicates a device exists.
12332496af39SMoore, Eric Dean  	 * max_id = 1 + maximum id (hosts.h)
12342496af39SMoore, Eric Dean  	 */
12351ca00bb7SChristoph Hellwig 	hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
12361ca00bb7SChristoph Hellwig 	if (!hd->Targets) {
12372496af39SMoore, Eric Dean  		error = -ENOMEM;
12387acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12392496af39SMoore, Eric Dean  	}
12402496af39SMoore, Eric Dean  
12411ca00bb7SChristoph Hellwig 	dprintk((KERN_INFO "  vdev @ %p\n", hd->Targets));
12422496af39SMoore, Eric Dean  
12432496af39SMoore, Eric Dean  	/* Clear the TM flags
12442496af39SMoore, Eric Dean  	 */
12452496af39SMoore, Eric Dean  	hd->tmPending = 0;
12462496af39SMoore, Eric Dean  	hd->tmState = TM_STATE_NONE;
12472496af39SMoore, Eric Dean  	hd->resetPending = 0;
12482496af39SMoore, Eric Dean  	hd->abortSCpnt = NULL;
12492496af39SMoore, Eric Dean  
12502496af39SMoore, Eric Dean  	/* Clear the pointer used to store
12512496af39SMoore, Eric Dean  	 * single-threaded commands, i.e., those
12522496af39SMoore, Eric Dean  	 * issued during a bus scan, dv and
12532496af39SMoore, Eric Dean  	 * configuration pages.
12542496af39SMoore, Eric Dean  	 */
12552496af39SMoore, Eric Dean  	hd->cmdPtr = NULL;
12562496af39SMoore, Eric Dean  
12572496af39SMoore, Eric Dean  	/* Initialize this SCSI Hosts' timers
12582496af39SMoore, Eric Dean  	 * To use, set the timer expires field
12592496af39SMoore, Eric Dean  	 * and add_timer
12602496af39SMoore, Eric Dean  	 */
12612496af39SMoore, Eric Dean  	init_timer(&hd->timer);
12622496af39SMoore, Eric Dean  	hd->timer.data = (unsigned long) hd;
12632496af39SMoore, Eric Dean  	hd->timer.function = mptscsih_timer_expired;
12642496af39SMoore, Eric Dean  
12652496af39SMoore, Eric Dean  	init_waitqueue_head(&hd->scandv_waitq);
12662496af39SMoore, Eric Dean  	hd->scandv_wait_done = 0;
12672496af39SMoore, Eric Dean  	hd->last_queue_full = 0;
12682496af39SMoore, Eric Dean  
126905e8ec17SMichael Reed 	sh->transportt = mptfc_transport_template;
12702496af39SMoore, Eric Dean  	error = scsi_add_host (sh, &ioc->pcidev->dev);
12712496af39SMoore, Eric Dean  	if(error) {
12722496af39SMoore, Eric Dean  		dprintk((KERN_ERR MYNAM
12732496af39SMoore, Eric Dean  		  "scsi_add_host failed\n"));
12747acec1e7SMoore, Eric Dean 		goto out_mptfc_probe;
12752496af39SMoore, Eric Dean  	}
12762496af39SMoore, Eric Dean  
127765207fedSMoore, Eric 	/* initialize workqueue */
127865207fedSMoore, Eric 
127965207fedSMoore, Eric 	snprintf(ioc->fc_rescan_work_q_name, KOBJ_NAME_LEN, "mptfc_wq_%d",
128065207fedSMoore, Eric 		sh->host_no);
128165207fedSMoore, Eric 	ioc->fc_rescan_work_q =
128265207fedSMoore, Eric 		create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
128365207fedSMoore, Eric 	if (!ioc->fc_rescan_work_q)
128465207fedSMoore, Eric 		goto out_mptfc_probe;
128565207fedSMoore, Eric 
128665207fedSMoore, Eric 	/*
128780d3ac77SMichael Reed 	 *  Pre-fetch FC port WWN and stuff...
128880d3ac77SMichael Reed 	 *  (FCPortPage0_t stuff)
128980d3ac77SMichael Reed 	 */
129080d3ac77SMichael Reed 	for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
129180d3ac77SMichael Reed 		(void) mptfc_GetFcPortPage0(ioc, ii);
129280d3ac77SMichael Reed 	}
1293ca2f938eSMichael Reed 	mptfc_SetFcPortPage1_defaults(ioc);
129480d3ac77SMichael Reed 
129580d3ac77SMichael Reed 	/*
129665207fedSMoore, Eric 	 * scan for rports -
129765207fedSMoore, Eric 	 *	by doing it via the workqueue, some locking is eliminated
129865207fedSMoore, Eric 	 */
129965207fedSMoore, Eric 
130065207fedSMoore, Eric 	queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
130165207fedSMoore, Eric 	flush_workqueue(ioc->fc_rescan_work_q);
130205e8ec17SMichael Reed 
13032496af39SMoore, Eric Dean  	return 0;
13042496af39SMoore, Eric Dean  
13057acec1e7SMoore, Eric Dean out_mptfc_probe:
13062496af39SMoore, Eric Dean  
13072496af39SMoore, Eric Dean  	mptscsih_remove(pdev);
13082496af39SMoore, Eric Dean  	return error;
13092496af39SMoore, Eric Dean  }
13102496af39SMoore, Eric Dean  
13112496af39SMoore, Eric Dean  static struct pci_driver mptfc_driver = {
13122496af39SMoore, Eric Dean  	.name		= "mptfc",
13132496af39SMoore, Eric Dean  	.id_table	= mptfc_pci_table,
13142496af39SMoore, Eric Dean  	.probe		= mptfc_probe,
131505e8ec17SMichael Reed 	.remove		= __devexit_p(mptfc_remove),
13162496af39SMoore, Eric Dean  	.shutdown	= mptscsih_shutdown,
13172496af39SMoore, Eric Dean  #ifdef CONFIG_PM
13182496af39SMoore, Eric Dean  	.suspend	= mptscsih_suspend,
13192496af39SMoore, Eric Dean  	.resume		= mptscsih_resume,
13202496af39SMoore, Eric Dean  #endif
13212496af39SMoore, Eric Dean  };
13222496af39SMoore, Eric Dean  
132380d3ac77SMichael Reed static int
132480d3ac77SMichael Reed mptfc_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
132580d3ac77SMichael Reed {
132680d3ac77SMichael Reed 	MPT_SCSI_HOST *hd;
132780d3ac77SMichael Reed 	u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
132880d3ac77SMichael Reed 	unsigned long flags;
132980d3ac77SMichael Reed 	int rc=1;
133080d3ac77SMichael Reed 
133180d3ac77SMichael Reed 	devtverboseprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
133280d3ac77SMichael Reed 			ioc->name, event));
133380d3ac77SMichael Reed 
133480d3ac77SMichael Reed 	if (ioc->sh == NULL ||
133580d3ac77SMichael Reed 		((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL))
133680d3ac77SMichael Reed 		return 1;
133780d3ac77SMichael Reed 
133880d3ac77SMichael Reed 	switch (event) {
133980d3ac77SMichael Reed 	case MPI_EVENT_RESCAN:
134080d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
134180d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
134280d3ac77SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
134380d3ac77SMichael Reed 				   &ioc->fc_rescan_work);
134480d3ac77SMichael Reed 		}
134580d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
134680d3ac77SMichael Reed 		break;
134780d3ac77SMichael Reed 	default:
134880d3ac77SMichael Reed 		rc = mptscsih_event_process(ioc,pEvReply);
134980d3ac77SMichael Reed 		break;
135080d3ac77SMichael Reed 	}
135180d3ac77SMichael Reed 	return rc;
135280d3ac77SMichael Reed }
135380d3ac77SMichael Reed 
135480d3ac77SMichael Reed static int
135580d3ac77SMichael Reed mptfc_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
135680d3ac77SMichael Reed {
135780d3ac77SMichael Reed 	int		rc;
135880d3ac77SMichael Reed 	unsigned long	flags;
135980d3ac77SMichael Reed 
136080d3ac77SMichael Reed 	rc = mptscsih_ioc_reset(ioc,reset_phase);
136180d3ac77SMichael Reed 	if (rc == 0)
136280d3ac77SMichael Reed 		return rc;
136380d3ac77SMichael Reed 
136480d3ac77SMichael Reed 
136580d3ac77SMichael Reed 	dtmprintk((KERN_WARNING MYNAM
136680d3ac77SMichael Reed 		": IOC %s_reset routed to FC host driver!\n",
136780d3ac77SMichael Reed 		reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
136880d3ac77SMichael Reed 		reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
136980d3ac77SMichael Reed 
137080d3ac77SMichael Reed 	if (reset_phase == MPT_IOC_SETUP_RESET) {
1371419835e2SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1372419835e2SMichael Reed 		if (ioc->fc_rescan_work_q) {
1373419835e2SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
1374419835e2SMichael Reed 				   &ioc->fc_setup_reset_work);
1375419835e2SMichael Reed 		}
1376419835e2SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
137780d3ac77SMichael Reed 	}
137880d3ac77SMichael Reed 
137980d3ac77SMichael Reed 	else if (reset_phase == MPT_IOC_PRE_RESET) {
138080d3ac77SMichael Reed 	}
138180d3ac77SMichael Reed 
138280d3ac77SMichael Reed 	else {	/* MPT_IOC_POST_RESET */
1383ca2f938eSMichael Reed 		mptfc_SetFcPortPage1_defaults(ioc);
138480d3ac77SMichael Reed 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
138580d3ac77SMichael Reed 		if (ioc->fc_rescan_work_q) {
138680d3ac77SMichael Reed 			queue_work(ioc->fc_rescan_work_q,
138780d3ac77SMichael Reed 				   &ioc->fc_rescan_work);
138880d3ac77SMichael Reed 		}
138980d3ac77SMichael Reed 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
139080d3ac77SMichael Reed 	}
139180d3ac77SMichael Reed 	return 1;
139280d3ac77SMichael Reed }
139380d3ac77SMichael Reed 
13942496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
13952496af39SMoore, Eric Dean  /**
13962496af39SMoore, Eric Dean   *	mptfc_init - Register MPT adapter(s) as SCSI host(s) with
13972496af39SMoore, Eric Dean   *	linux scsi mid-layer.
13982496af39SMoore, Eric Dean   *
13992496af39SMoore, Eric Dean   *	Returns 0 for success, non-zero for failure.
14002496af39SMoore, Eric Dean   */
14012496af39SMoore, Eric Dean  static int __init
14022496af39SMoore, Eric Dean  mptfc_init(void)
14032496af39SMoore, Eric Dean  {
140405e8ec17SMichael Reed 	int error;
14052496af39SMoore, Eric Dean  
14062496af39SMoore, Eric Dean  	show_mptmod_ver(my_NAME, my_VERSION);
14072496af39SMoore, Eric Dean  
1408ca2f938eSMichael Reed 	/* sanity check module parameters */
1409ca2f938eSMichael Reed 	if (mptfc_dev_loss_tmo <= 0)
141005e8ec17SMichael Reed 		mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
141105e8ec17SMichael Reed 
141205e8ec17SMichael Reed 	mptfc_transport_template =
141305e8ec17SMichael Reed 		fc_attach_transport(&mptfc_transport_functions);
141405e8ec17SMichael Reed 
141505e8ec17SMichael Reed 	if (!mptfc_transport_template)
141605e8ec17SMichael Reed 		return -ENODEV;
141705e8ec17SMichael Reed 
14182496af39SMoore, Eric Dean  	mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
14192496af39SMoore, Eric Dean  	mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
14202496af39SMoore, Eric Dean  	mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
14212496af39SMoore, Eric Dean  
142280d3ac77SMichael Reed 	if (mpt_event_register(mptfcDoneCtx, mptfc_event_process) == 0) {
14233a892befSMoore, Eric 		devtverboseprintk((KERN_INFO MYNAM
14242496af39SMoore, Eric Dean  		  ": Registered for IOC event notifications\n"));
14252496af39SMoore, Eric Dean  	}
14262496af39SMoore, Eric Dean  
142780d3ac77SMichael Reed 	if (mpt_reset_register(mptfcDoneCtx, mptfc_ioc_reset) == 0) {
14282496af39SMoore, Eric Dean  		dprintk((KERN_INFO MYNAM
14292496af39SMoore, Eric Dean  		  ": Registered for IOC reset notifications\n"));
14302496af39SMoore, Eric Dean  	}
14312496af39SMoore, Eric Dean  
143205e8ec17SMichael Reed 	error = pci_register_driver(&mptfc_driver);
14333bc7bf1dSMichael Reed 	if (error)
143405e8ec17SMichael Reed 		fc_release_transport(mptfc_transport_template);
143505e8ec17SMichael Reed 
143605e8ec17SMichael Reed 	return error;
143705e8ec17SMichael Reed }
143805e8ec17SMichael Reed 
143905e8ec17SMichael Reed /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
144005e8ec17SMichael Reed /**
144105e8ec17SMichael Reed  *	mptfc_remove - Removed fc infrastructure for devices
144205e8ec17SMichael Reed  *	@pdev: Pointer to pci_dev structure
144305e8ec17SMichael Reed  *
144405e8ec17SMichael Reed  */
14453bc7bf1dSMichael Reed static void __devexit
14463bc7bf1dSMichael Reed mptfc_remove(struct pci_dev *pdev)
144705e8ec17SMichael Reed {
144805e8ec17SMichael Reed 	MPT_ADAPTER		*ioc = pci_get_drvdata(pdev);
144905e8ec17SMichael Reed 	struct mptfc_rport_info	*p, *n;
145065207fedSMoore, Eric 	struct workqueue_struct *work_q;
145165207fedSMoore, Eric 	unsigned long		flags;
1452ca2f938eSMichael Reed 	int			ii;
145365207fedSMoore, Eric 
145465207fedSMoore, Eric 	/* destroy workqueue */
145565207fedSMoore, Eric 	if ((work_q=ioc->fc_rescan_work_q)) {
145665207fedSMoore, Eric 		spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
145765207fedSMoore, Eric 		ioc->fc_rescan_work_q = NULL;
145865207fedSMoore, Eric 		spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
145965207fedSMoore, Eric 		destroy_workqueue(work_q);
146065207fedSMoore, Eric 	}
146105e8ec17SMichael Reed 
146205e8ec17SMichael Reed 	fc_remove_host(ioc->sh);
146305e8ec17SMichael Reed 
146405e8ec17SMichael Reed 	list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
146505e8ec17SMichael Reed 		list_del(&p->list);
146605e8ec17SMichael Reed 		kfree(p);
146705e8ec17SMichael Reed 	}
146805e8ec17SMichael Reed 
1469ca2f938eSMichael Reed 	for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
1470ca2f938eSMichael Reed 		if (ioc->fc_data.fc_port_page1[ii].data) {
1471ca2f938eSMichael Reed 			pci_free_consistent(ioc->pcidev,
1472ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].pg_sz,
1473ca2f938eSMichael Reed 				(u8 *) ioc->fc_data.fc_port_page1[ii].data,
1474ca2f938eSMichael Reed 				ioc->fc_data.fc_port_page1[ii].dma);
1475ca2f938eSMichael Reed 			ioc->fc_data.fc_port_page1[ii].data = NULL;
1476ca2f938eSMichael Reed 		}
1477ca2f938eSMichael Reed 	}
1478ca2f938eSMichael Reed 
147905e8ec17SMichael Reed 	mptscsih_remove(pdev);
14802496af39SMoore, Eric Dean  }
14812496af39SMoore, Eric Dean  
14822496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14832496af39SMoore, Eric Dean  /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
14842496af39SMoore, Eric Dean  /**
14852496af39SMoore, Eric Dean   *	mptfc_exit - Unregisters MPT adapter(s)
14862496af39SMoore, Eric Dean   *
14872496af39SMoore, Eric Dean   */
14882496af39SMoore, Eric Dean  static void __exit
14892496af39SMoore, Eric Dean  mptfc_exit(void)
14902496af39SMoore, Eric Dean  {
14912496af39SMoore, Eric Dean  	pci_unregister_driver(&mptfc_driver);
149205e8ec17SMichael Reed 	fc_release_transport(mptfc_transport_template);
14932496af39SMoore, Eric Dean  
14942496af39SMoore, Eric Dean  	mpt_reset_deregister(mptfcDoneCtx);
14952496af39SMoore, Eric Dean  	dprintk((KERN_INFO MYNAM
14962496af39SMoore, Eric Dean  	  ": Deregistered for IOC reset notifications\n"));
14972496af39SMoore, Eric Dean  
14982496af39SMoore, Eric Dean  	mpt_event_deregister(mptfcDoneCtx);
14992496af39SMoore, Eric Dean  	dprintk((KERN_INFO MYNAM
15002496af39SMoore, Eric Dean  	  ": Deregistered for IOC event notifications\n"));
15012496af39SMoore, Eric Dean  
15022496af39SMoore, Eric Dean  	mpt_deregister(mptfcInternalCtx);
15032496af39SMoore, Eric Dean  	mpt_deregister(mptfcTaskCtx);
15042496af39SMoore, Eric Dean  	mpt_deregister(mptfcDoneCtx);
15052496af39SMoore, Eric Dean  }
15062496af39SMoore, Eric Dean  
15072496af39SMoore, Eric Dean  module_init(mptfc_init);
15082496af39SMoore, Eric Dean  module_exit(mptfc_exit);
1509