scsi_transport_fc.c revision 6b7281d0a0f8f99d39808088a036459f6f7906a6
11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  FiberChannel transport specific attributes exported to sysfs.
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  This program is free software; you can redistribute it and/or modify
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  it under the terms of the GNU General Public License as published by
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  the Free Software Foundation; either version 2 of the License, or
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  (at your option) any later version.
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  This program is distributed in the hope that it will be useful,
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  but WITHOUT ANY WARRANTY; without even the implied warranty of
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  GNU General Public License for more details.
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  You should have received a copy of the GNU General Public License
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  along with this program; if not, write to the Free Software
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  ========
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Copyright (C) 2004-2005   James Smart, Emulex Corporation
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    Rewrite for host, target, device, and remote port attributes,
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    statistics, and service functions...
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
294e57b6817880946a3a78d5d8cad1ace363f7e449Tim Schmielau#include <linux/sched.h>	/* workqueue stuff, HZ */
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_device.h>
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_host.h>
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport.h>
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport_fc.h>
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "scsi_priv.h"
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Redefine so that we can have same named attributes in the
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sdev/starget/host objects.
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)		\
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct class_device_attribute class_device_attr_##_prefix##_##_name = 	\
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__ATTR(_name,_mode,_show,_store)
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_search(title, table_type, table)			\
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char *get_fc_##title##_name(enum table_type table_key)	\
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *name = NULL;						\
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {		\
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value == table_key) {			\
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			name = table[i].name;				\
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;						\
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return name;							\
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_match(title, table_type, table)			\
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int get_fc_##title##_match(const char *table_key,		\
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		enum table_type *value)					\
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {		\
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (strncmp(table_key, table[i].name,			\
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				table[i].matchlen) == 0) {		\
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*value = table[i].value;			\
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0; /* success */				\
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 1; /* failure */						\
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_type values to ascii string name */
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_type	value;
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_type_names[] = {
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_UNKNOWN,		"Unknown" },
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_OTHER,		"Other" },
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NOTPRESENT,	"Not Present" },
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NPORT,	"NPort (fabric via point-to-point)" },
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NLPORT,	"NLPort (fabric via loop)" },
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_LPORT,	"LPort (private loop)" },
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_PTP,	"Point-To-Point (direct nport connection" },
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTTYPE_MAX_NAMELEN		50
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_state values to ascii string name */
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_state	value;
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_state_names[] = {
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_UNKNOWN,		"Unknown" },
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_NOTPRESENT,	"Not Present" },
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ONLINE,		"Online" },
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_OFFLINE,		"Offline" },
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BLOCKED,		"Blocked" },
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BYPASSED,	"Bypassed" },
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_DIAGNOSTICS,	"Diagnostics" },
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LINKDOWN,	"Linkdown" },
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ERROR,		"Error" },
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LOOPBACK,	"Loopback" },
10842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	{ FC_PORTSTATE_DELETED,		"Deleted" },
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTSTATE_MAX_NAMELEN	20
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_tgtid_binding_type values to ascii string name */
1150ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_tgtid_binding_type	value;
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char				*name;
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int				matchlen;
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_tgtid_binding_type_names[] = {
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_NONE, "none", 4 },
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_BINDTYPE_MAX_NAMELEN	30
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_bitfield_name_search(title, table)			\
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t							\
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsget_fc_##title##_names(u32 table_key, char *buf)		\
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{								\
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *prefix = "";					\
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t len = 0;					\
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;							\
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds								\
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {	\
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value & table_key) {		\
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			len += sprintf(buf + len, "%s%s",	\
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				prefix, table[i].name);		\
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prefix = ", ";				\
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}						\
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}							\
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");			\
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;						\
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_COS bit values to ascii string name */
1530ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_cos_names[] = {
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS1,	"Class 1" },
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS2,	"Class 2" },
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS3,	"Class 3" },
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS4,	"Class 4" },
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS6,	"Class 6" },
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(cos, fc_cos_names)
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_PORTSPEED bit values to ascii string name */
1670ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_speed_names[] = {
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_1GBIT,		"1 Gbit" },
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_2GBIT,		"2 Gbit" },
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_4GBIT,		"4 Gbit" },
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_10GBIT,		"10 Gbit" },
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_NOT_NEGOTIATED,	"Not Negotiated" },
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(port_speed, fc_port_speed_names)
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_fc4s (char *buf, u8 *fc4_list)
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i, len=0;
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		len += sprintf(buf + len , "0x%02x ", *fc4_list);
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_RPORT_ROLE bit values to ascii string name */
1930ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_remote_port_role_names[] = {
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_RPORT_ROLE_FCP_TARGET,	"FCP Target" },
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_RPORT_ROLE_FCP_INITIATOR,	"FCP Initiator" },
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_RPORT_ROLE_IP_PORT,	"IP Port" },
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_PORTID_MASK	0xfffff0
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_ROLE_MASK  	0x00000f
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FPORT_PORTID			0x00000e
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FABCTLR_PORTID		0x00000d
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_DIRSRVR_PORTID		0x00000c
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_TIMESRVR_PORTID		0x00000b
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_MGMTSRVR_PORTID		0x00000a
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Comstatic void fc_shost_remove_rports(void  *data);
21619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic void fc_timeout_deleted_rport(void *data);
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void fc_scsi_scan_rport(void *data);
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void fc_rport_terminate(struct fc_rport  *rport);
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Attribute counts pre object type...
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Increase these values if you add attributes
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_STARGET_NUM_ATTRS 	3
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_RPORT_NUM_ATTRS	9
22691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez#define FC_HOST_NUM_ATTRS	16
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_internal {
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_transport_template t;
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_function_template *f;
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * For attributes : each object has :
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of the actual attributes structures
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of null-terminated pointers to the attribute
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *     structures - used for mid-layer interaction.
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * The attribute containers for the starget and host are are
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * part of the midlayer. As the remote port is specific to the
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * fc transport, we must provide the attribute container.
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct class_device_attribute private_starget_attrs[
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds							FC_STARGET_NUM_ATTRS];
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct transport_container rport_attr_cont;
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define to_fc_internal(tmpl)	container_of(tmpl, struct fc_internal, t)
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
256d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_target_setup(struct transport_container *tc, struct device *dev,
257d0a7e574007fd547d72ec693bfa35778623d0738James Bottomley			   struct class_device *cdev)
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_target *starget = to_scsi_target(dev);
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * if parent is remote port, use values from remote port.
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Otherwise, this host uses the fc_transport, but not the
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * remote port interface. As such, initialize to known non-values.
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport) {
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = rport->node_name;
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = rport->port_name;
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = rport->port_id;
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = -1;
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = -1;
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = -1;
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_transport_class,
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_transport",
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_target_setup,
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
286d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_host_setup(struct transport_container *tc, struct device *dev,
287d0a7e574007fd547d72ec693bfa35778623d0738James Bottomley			 struct class_device *cdev)
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(dev);
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set default values easily detected by the midlayer as
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * failure cases.  The scsi lldd is responsible for initializing
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * all transport attributes to valid values per host.
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_node_name(shost) = -1;
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_port_name(shost) = -1;
2986b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann	fc_host_permanent_port_name(shost) = -1;
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_supported_classes(shost) = FC_COS_UNSPECIFIED;
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(fc_host_supported_fc4s(shost), 0,
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sizeof(fc_host_supported_fc4s(shost)));
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(fc_host_symbolic_name(shost), 0,
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sizeof(fc_host_symbolic_name(shost)));
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_maxframe_size(shost) = -1;
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(fc_host_serial_number(shost), 0,
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sizeof(fc_host_serial_number(shost)));
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_port_id(shost) = -1;
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(fc_host_active_fc4s(shost), 0,
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sizeof(fc_host_active_fc4s(shost)));
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_fabric_name(shost) = -1;
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	INIT_LIST_HEAD(&fc_host_rports(shost));
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	INIT_LIST_HEAD(&fc_host_rport_bindings(shost));
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_next_rport_number(shost) = 0;
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_next_target_id(shost) = 0;
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	fc_host_flags(shost) = 0;
32542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	INIT_WORK(&fc_host_rport_del_work(shost), fc_shost_remove_rports, shost);
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_host_class,
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_host",
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_host_setup,
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Setup and Remove actions for remote ports are handled
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in the service functions below.
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_rport_class,
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_remote_ports",
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Module Parameters
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * dev_loss_tmo: the default number of seconds that the FC transport
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   should insulate the loss of a remote port.
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic unsigned int fc_dev_loss_tmo = SCSI_DEVICE_BLOCK_MAX_TIMEOUT;
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_PARM_DESC(dev_loss_tmo,
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 "Maximum number of seconds that the FC transport should"
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " insulate the loss of a remote port. Once this value is"
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " exceeded, the scsi target is removed. Value should be"
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic __init int fc_transport_init(void)
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int error = transport_class_register(&fc_host_class);
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return error;
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = transport_class_register(&fc_rport_class);
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return error;
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return transport_class_register(&fc_transport_class);
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit fc_transport_exit(void)
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_transport_class);
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_rport_class);
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_host_class);
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC Remote Port Attribute Management
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_show_function(field, format_string, sz, cast)		\
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_rport_##field (struct class_device *cdev, char *buf)		\
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = transport_class_to_rport(cdev);	\
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
39319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((i->f->get_rport_##field) &&				\
39419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    !((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
39542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_DELETED) ||		\
39619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_NOTPRESENT)))		\
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_rport_##field(rport);				\
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_store_function(field)					\
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstore_fc_rport_##field(struct class_device *cdev, const char *buf,	\
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			   size_t count)				\
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = transport_class_to_rport(cdev);	\
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
41019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
41142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||		\
41219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))		\
41319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;						\
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	val = simple_strtoul(buf, NULL, 0);				\
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_rport_##field(rport, val);				\
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr(field, format_string, sz)			\
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,			\
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr_cast(field, format_string, sz, cast)		\
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, (cast))	\
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,			\
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rw_attr(field, format_string, sz)			\
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_store_function(field)					\
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,		\
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##field,				\
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_rport_##field)
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_show_function(field, format_string, sz, cast)	\
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_rport_##field (struct class_device *cdev, char *buf)		\
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = transport_class_to_rport(cdev);	\
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr(field, format_string, sz)		\
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, )	\
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,			\
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)	\
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, (cast)) \
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,			\
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_enum_attr(title, maxlen)			\
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_rport_##title (struct class_device *cdev, char *buf)		\
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = transport_class_to_rport(cdev);	\
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(rport->title);			\
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO,			\
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##title, NULL)
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RD(field)					\
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count] = class_device_attr_rport_##field; \
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)				\
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count] = class_device_attr_rport_##field; \
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RW(field)					\
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count] = class_device_attr_rport_##field; \
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_rport_##field) {					\
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].attr.mode = S_IRUGO;	\
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].store = NULL;		\
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport Remote Port Attributes: */
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Remote Port Attributes */
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_rport_supported_classes (struct class_device *cdev, char *buf)
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = transport_class_to_rport(cdev);
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport->supported_classes == FC_COS_UNSPECIFIED)
5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(rport->supported_classes, buf);
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_supported_classes, NULL);
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Remote Port Attributes */
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
51619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com/*
51719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * dev_loss_tmo attribute
51819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com */
51919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comfc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
52019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic ssize_t
52119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstore_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
52219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			   size_t count)
52319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
52419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	int val;
52519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_rport *rport = transport_class_to_rport(cdev);
52619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
52719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *i = to_fc_internal(shost->transportt);
52819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
52942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||
53019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
53119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;
53219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	val = simple_strtoul(buf, NULL, 0);
53319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
53419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EINVAL;
53519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	i->f->set_rport_dev_loss_tmo(rport, val);
53619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	return count;
53719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
53819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
53919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Remote Port Attributes */
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_rport_roles (struct class_device *cdev, char *buf)
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = transport_class_to_rport(cdev);
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* identify any roles that are port_id specific */
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((rport->port_id != -1) &&
5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					FC_WELLKNOWN_PORTID_MASK) {
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FPORT_PORTID:
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Port\n");
5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FABCTLR_PORTID:
5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Controller\n");
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_DIRSRVR_PORTID:
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Directory Server\n");
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_TIMESRVR_PORTID:
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Time Server\n");
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_MGMTSRVR_PORTID:
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Management Server\n");
5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Unknown Fabric Entity\n");
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
5731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 20, "unknown\n");
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return get_fc_remote_port_roles_names(rport->roles, buf);
5751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
5781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_roles, NULL);
5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC SCSI Target Attribute Management
5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
5901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Note: in the target show function we recognize when the remote
5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  port is in the heirarchy and do not allow the driver to get
5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  involved in sysfs functions. The driver only gets involved if
5931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  it's the "old" style that doesn't use rports.
5941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
5951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_show_function(field, format_string, sz, cast)	\
5961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
5971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_starget_##field (struct class_device *cdev, char *buf)		\
5981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
5991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_target *starget = transport_class_to_starget(cdev);	\
6001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);	\
6011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
6021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);		\
6031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport)							\
6041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_##field(starget) = rport->field;		\
6051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (i->f->get_starget_##field)				\
6061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_starget_##field(starget);			\
6071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, 			\
6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cast fc_starget_##field(starget)); 			\
6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr(field, format_string, sz)			\
6121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, )		\
6131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,			\
6141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_starget_##field, NULL)
6151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr_cast(field, format_string, sz, cast)		\
6171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, (cast))	\
6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,			\
6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_starget_##field, NULL)
6201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RD(field)				\
6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count] = class_device_attr_starget_##field; \
6231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].attr.mode = S_IRUGO;		\
6241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].store = NULL;			\
6251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
6261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
6271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
6281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RW(field)				\
6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count] = class_device_attr_starget_##field; \
6311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_starget_##field) {				\
6321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].attr.mode = S_IRUGO;	\
6331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].store = NULL;		\
6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
6351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
6361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
6371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
6381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport SCSI Target Attributes: */
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr(port_id, "0x%06x\n", 20);
6431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Attribute Management
6471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
6481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_show_function(field, format_string, sz, cast)		\
6501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
6511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_host_##field (struct class_device *cdev, char *buf)		\
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);	\
6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##field)					\
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##field(shost);				\
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
6581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_store_function(field)					\
6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
6621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstore_fc_host_##field(struct class_device *cdev, const char *buf,	\
6631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			   size_t count)				\
6641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
6661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);	\
6671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
6681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	val = simple_strtoul(buf, NULL, 0);				\
6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_host_##field(shost, val);				\
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr(field, format_string, sz)			\
6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,			\
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
6781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr_cast(field, format_string, sz, cast)		\
6801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, (cast))		\
6811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,			\
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rw_attr(field, format_string, sz)			\
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_store_function(field)					\
6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_host_##field,				\
6891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_host_##field)
6901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_enum_attr(title, maxlen)				\
6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_host_##title (struct class_device *cdev, char *buf)		\
6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
6951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);	\
6961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
6971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
6981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##title)					\
6991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##title(shost);				\
7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(fc_host_##title(shost));		\
7011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
7021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
7031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
7041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
7051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RD(field)					\
7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count] = class_device_attr_host_##field;	\
7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
7111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
7121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
7131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
7141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RW(field)					\
7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count] = class_device_attr_host_##field;	\
7171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_host_##field) {					\
7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].attr.mode = S_IRUGO;	\
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].store = NULL;		\
7201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
7211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
7221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
7231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
7241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_show_function(field, format_string, sz, cast)	\
7271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
7281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_host_##field (struct class_device *cdev, char *buf)		\
7291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
7301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);	\
7311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
7321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr(field, format_string, sz)		\
7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, )	\
7361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,			\
7371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
7381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr_cast(field, format_string, sz, cast)	\
7401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, (cast)) \
7411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,			\
7421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
7431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
7451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count] = class_device_attr_host_##field;	\
7461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
7481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
7491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
7501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)			\
75291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{									\
7531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count] = class_device_attr_host_##field;	\
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
75591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	count++;							\
75691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
7571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Host Attributes */
7601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_host_supported_classes (struct class_device *cdev, char *buf)
7631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
7671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
7681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
7701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
7721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_classes, NULL);
7731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
7751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
7761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);
7781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
7791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
7811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_fc4s, NULL);
7821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
7841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_host_supported_speeds (struct class_device *cdev, char *buf)
7851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);
7871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
7891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
7901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
7921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
7941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_speeds, NULL);
7951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
7981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
7996b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmannfc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
8006b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann			     unsigned long long);
8011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
8021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
8031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
8041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Host Attributes */
8071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
8091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_host_active_fc4s (struct class_device *cdev, char *buf)
8101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);
8121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
8131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_active_fc4s)
8151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_active_fc4s(shost);
8161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
8181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
8201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_active_fc4s, NULL);
8211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
8231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_host_speed (struct class_device *cdev, char *buf)
8241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);
8261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
8271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_speed)
8291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_speed(shost);
8301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
8321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
8331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_speed(shost), buf);
8351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
8371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_speed, NULL);
8381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr(port_id, "0x%06x\n", 20);
8411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
8421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
8431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
8441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Host Attributes */
8471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
8491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
8501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);
8521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;
8531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
8551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)
8561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
8571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
8581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
860d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com#define get_list_head_entry(pos, head, member) 		\
861d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	pos = list_entry((head)->next, typeof(*pos), member)
862d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com
8631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
8641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstore_fc_private_host_tgtid_bind_type(struct class_device *cdev,
8651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *buf, size_t count)
8661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);
868d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	struct fc_rport *rport;
8691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 	enum fc_tgtid_binding_type val;
8701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
8711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (get_fc_tgtid_bind_type_match(buf, &val))
8731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
8741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if changing bind type, purge all unused consistent bindings */
8761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (val != fc_host_tgtid_bind_type(shost)) {
8771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_lock_irqsave(shost->host_lock, flags);
878d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		while (!list_empty(&fc_host_rport_bindings(shost))) {
879d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com			get_list_head_entry(rport,
880d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com				&fc_host_rport_bindings(shost), peers);
881d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com			spin_unlock_irqrestore(shost->host_lock, flags);
8821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			fc_rport_terminate(rport);
883d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com			spin_lock_irqsave(shost->host_lock, flags);
884d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		}
8851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_unlock_irqrestore(shost->host_lock, flags);
8861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_tgtid_bind_type(shost) = val;
8891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;
8901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
8931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_private_host_tgtid_bind_type,
8941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_private_host_tgtid_bind_type);
8951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
89691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquezstatic ssize_t
89791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquezstore_fc_private_host_issue_lip(struct class_device *cdev,
89891ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	const char *buf, size_t count)
89991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{
90091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	struct Scsi_Host *shost = transport_class_to_shost(cdev);
90191ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	struct fc_internal *i = to_fc_internal(shost->transportt);
90291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	int ret;
90391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
90491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	/* ignore any data value written to the attribute */
90591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (i->f->issue_fc_host_lip) {
90691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		ret = i->f->issue_fc_host_lip(shost);
90791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		return ret ? ret: count;
90891ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	}
90991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
91091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	return -ENOENT;
91191ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
91291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
91391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquezstatic FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
91491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez			store_fc_private_host_issue_lip);
91591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
9161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
9171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Statistics Management
9181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
9191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Show a given an attribute in the statistics group */
9211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
9221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
9231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);
9251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_host_statistics *stats;
9271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t ret = -ENOENT;
9281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (offset > sizeof(struct fc_host_statistics) ||
9301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    offset % sizeof(u64) != 0)
9311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		WARN_ON(1);
9321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_fc_host_stats) {
9341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		stats = (i->f->get_fc_host_stats)(shost);
9351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (stats)
9361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = snprintf(buf, 20, "0x%llx\n",
9371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
9381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
9391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
9401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* generate a read-only statistics attribute */
9441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_statistic(name)						\
9451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t show_fcstat_##name(struct class_device *cd, char *buf) 	\
9461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
9471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return fc_stat_show(cd, buf, 					\
9481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    offsetof(struct fc_host_statistics, name));	\
9491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
9501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
9511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(seconds_since_last_reset);
9531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_frames);
9541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_words);
9551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_frames);
9561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_words);
9571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(lip_count);
9581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(nos_count);
9591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(error_frames);
9601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(dumped_frames);
9611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(link_failure_count);
9621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_sync_count);
9631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_signal_count);
9641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(prim_seq_protocol_err_count);
9651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_tx_word_count);
9661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_crc_count);
9671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_requests);
9681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_requests);
9691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_control_requests);
9701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_megabytes);
9711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_megabytes);
9721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
9741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_reset_statistics(struct class_device *cdev, const char *buf,
9751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			   size_t count)
9761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = transport_class_to_shost(cdev);
9781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
9791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* ignore any data value written to the attribute */
9811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->reset_fc_host_stats) {
9821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->reset_fc_host_stats(shost);
9831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return count;
9841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
9851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -ENOENT;
9871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
9891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				fc_reset_statistics);
9901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute *fc_statistics_attrs[] = {
9931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_seconds_since_last_reset.attr,
9941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_tx_frames.attr,
9951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_tx_words.attr,
9961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_rx_frames.attr,
9971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_rx_words.attr,
9981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_lip_count.attr,
9991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_nos_count.attr,
10001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_error_frames.attr,
10011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_dumped_frames.attr,
10021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_link_failure_count.attr,
10031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_loss_of_sync_count.attr,
10041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_loss_of_signal_count.attr,
10051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_prim_seq_protocol_err_count.attr,
10061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_invalid_tx_word_count.attr,
10071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_invalid_crc_count.attr,
10081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_fcp_input_requests.attr,
10091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_fcp_output_requests.attr,
10101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_fcp_control_requests.attr,
10111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_fcp_input_megabytes.attr,
10121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_fcp_output_megabytes.attr,
10131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&class_device_attr_host_reset_statistics.attr,
10141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	NULL
10151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
10161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute_group fc_statistics_group = {
10181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name = "statistics",
10191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.attrs = fc_statistics_attrs,
10201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
10211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_host_match(struct attribute_container *cont,
10231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  struct device *dev)
10241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
10261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
10271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_host_device(dev))
10291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
10301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev);
10321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
10331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
10341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
10351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
10371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.host_attrs.ac == cont;
10391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_target_match(struct attribute_container *cont,
10421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
10431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
10451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
10461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_target_device(dev))
10481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
10491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
10511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
10521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
10531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
10541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
10561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.target_attrs.ac == cont;
10581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void fc_rport_dev_release(struct device *dev)
10611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = dev_to_rport(dev);
10631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
10641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
10651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint scsi_is_fc_rport(const struct device *dev)
10681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return dev->release == fc_rport_dev_release;
10701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(scsi_is_fc_rport);
10721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_rport_match(struct attribute_container *cont,
10741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
10751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
10771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
10781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_fc_rport(dev))
10801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
10811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
10831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
10841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
10851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
10861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
10881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->rport_attr_cont.ac == cont;
10901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10925c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
10935c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com/*
10945c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com * Must be called with shost->host_lock held
10955c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com */
10965c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Comstatic struct device *fc_target_parent(struct Scsi_Host *shost,
10975c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com					int channel, uint id)
10985c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com{
10995c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com	struct fc_rport *rport;
11005c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
11015c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com	list_for_each_entry(rport, &fc_host_rports(shost), peers)
11025c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com		if ((rport->channel == channel) &&
11035c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com		    (rport->scsi_target_id == id))
11045c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com			return &rport->dev;
11055c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
11065c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com	return NULL;
11075c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com}
11085c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
11091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_transport_template *
11101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_attach_transport(struct fc_function_template *ft)
11111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
11131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					GFP_KERNEL);
11141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int count;
11151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!i))
11171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
11181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(i, 0, sizeof(struct fc_internal));
11201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
11221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.class = &fc_transport_class.class;
11231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.match = fc_target_match;
11241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_size = sizeof(struct fc_starget_attrs);
11251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.target_attrs);
11261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
11281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.class = &fc_host_class.class;
11291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.match = fc_host_match;
11301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_size = sizeof(struct fc_host_attrs);
11311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ft->get_fc_host_stats)
11321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->t.host_attrs.statistics = &fc_statistics_group;
11331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.host_attrs);
11341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
11361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.class = &fc_rport_class.class;
11371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.match = fc_rport_match;
11381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->rport_attr_cont);
11391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f = ft;
11411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport uses the shost workq for scsi scanning */
11431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.create_work_queue = 1;
11445c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
11455c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com	i->t.target_parent = fc_target_parent;
11461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
11481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Target Attributes.
11491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
11501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count = 0;
11511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(node_name);
11521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_name);
11531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_id);
11541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_STARGET_NUM_ATTRS);
11561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = NULL;
11581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
11611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Host Attributes.
11621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
11631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
11641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(node_name);
11651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_name);
11666b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
11671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
11681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
11691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
11701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
11711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
11721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(serial_number);
11731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_id);
11751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_type);
11761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_state);
11771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
11781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(speed);
11791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(fabric_name);
11801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport-managed attributes */
11821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
118391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (ft->issue_fc_host_lip)
118491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
11851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_HOST_NUM_ATTRS);
11871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = NULL;
11891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
11911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup Remote Port Attributes.
11921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
11931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
11941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
11951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
11961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
11971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
11981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
11991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
12001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
12011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
12021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
12031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_RPORT_NUM_ATTRS);
12051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = NULL;
12071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t;
12091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_attach_transport);
12111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid fc_release_transport(struct scsi_transport_template *t)
12131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(t);
12151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.target_attrs);
12171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.host_attrs);
12181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->rport_attr_cont);
12191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(i);
12211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_release_transport);
12231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
12261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_remove_host - called to terminate any fc_transport-related elements
12271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *                  for a scsi host.
12281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	remote port to be unblocked.
12291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
12301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is expected to be called immediately preceeding the
12311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a driver's call to scsi_remove_host().
12321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
12331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * WARNING: A driver utilizing the fc_transport, which fails to call
12341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   this routine prior to scsi_remote_host(), will leave dangling
12351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects in /sys/class/fc_remote_ports. Access to any of these
12361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects can result in a system crash !!!
12371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
12381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
12391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
12401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds **/
12411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
12421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remove_host(struct Scsi_Host *shost)
12431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport, *next_rport;
12451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Remove any remote ports */
12471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
12481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			&fc_host_rports(shost), peers)
12491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_rport_terminate(rport);
12501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
12511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			&fc_host_rport_bindings(shost), peers)
12521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_rport_terminate(rport);
12531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remove_host);
12551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
125619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com/*
125719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * fc_rport_tgt_remove - Removes the scsi target on the remote port
125819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * @rport:	The remote port to be operated on
125919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com */
126019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic void
126119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comfc_rport_tgt_remove(struct fc_rport *rport)
126219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
126319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
126419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
126519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_target_unblock(&rport->dev);
126619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
126719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* Stop anything on the workq */
126819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (!cancel_delayed_work(&rport->dev_loss_work))
126919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		flush_scheduled_work();
127019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_flush_work(shost);
127119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
127219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_remove_target(&rport->dev);
127319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
127419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
12751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
12761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_rport_create - allocates and creates a remote FC port.
12771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
12781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
12791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
12801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
12811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
12821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Allocates and creates the remoter port structure, including the
12831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * class and sysfs creation.
12841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
12851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
12861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
12871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds **/
12881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_rport *
12891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_rport_create(struct Scsi_Host *shost, int channel,
12901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
12911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_host_attrs *fc_host =
12931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			(struct fc_host_attrs *)shost->shost_data;
12941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *fci = to_fc_internal(shost->transportt);
12951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
12961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct device *dev;
12971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
12981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int error;
12991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t size;
13001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
13021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport = kmalloc(size, GFP_KERNEL);
13031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!rport)) {
13041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
13051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
13061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(rport, 0, size);
13081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->maxframe_size = -1;
13101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->supported_classes = FC_COS_UNSPECIFIED;
13111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->dev_loss_tmo = fc_dev_loss_tmo;
13121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
13131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
13141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_id = ids->port_id;
13151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->roles = ids->roles;
13161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_state = FC_PORTSTATE_ONLINE;
13171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fci->f->dd_fcrport_size)
13181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->dd_data = &rport[1];
13191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->channel = channel;
13201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
132119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	INIT_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport, rport);
13221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
13231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
13251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->number = fc_host->next_rport_number++;
13271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
13281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = fc_host->next_target_id++;
13291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
13301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = -1;
13311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_add_tail(&rport->peers, &fc_host_rports(shost));
13321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	get_device(&shost->shost_gendev);
13331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
13351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev = &rport->dev;
13371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_initialize(dev);
13381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev->parent = get_device(&shost->shost_gendev);
13391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev->release = fc_rport_dev_release;
13401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sprintf(dev->bus_id, "rport-%d:%d-%d",
13411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		shost->host_no, channel, rport->number);
13421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_setup_device(dev);
13431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = device_add(dev);
13451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error) {
13461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "FC Remote Port device_add failed\n");
13471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto delete_rport;
13481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_add_device(dev);
13501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_configure_device(dev);
13511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
13531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
13541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
13551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
13571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsdelete_rport:
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_destroy_device(dev);
13601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
13611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
13621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_del(&rport->peers);
13631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(&shost->shost_gendev);
13641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
13651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
13661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
13671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
13681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
13711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_remote_port_add - notifies the fc transport of the existence
13721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		of a remote FC port.
13731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
13741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
13751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
13761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
13771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
13781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport of the existence
13791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
13801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of the port, it's FC address (port_id), and the FC4 roles that are
13811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * active for the port.
13821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
13831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * For ports that are FCP targets (aka scsi targets), the FC transport
13841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * maintains consistent target id bindings on behalf of the LLDD.
13851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * A consistent target id binding is an assignment of a target id to
13861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a remote port identifier, which persists while the scsi host is
13871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * attached. The remote port can disappear, then later reappear, and
13881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it's target id assignment remains the same. This allows for shifts
13891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in FC addressing (if binding by wwpn or wwnn) with no apparent
13901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * changes to the scsi subsystem which is based on scsi host number and
13911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * target id values.  Bindings are only valid during the attachment of
13921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the scsi host. If the host detaches, then later re-attaches, target
13931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * id bindings may change.
13941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
13951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is responsible for returning a remote port structure.
13961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The routine will search the list of remote ports it maintains
13971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * internally on behalf of consistent target id mappings. If found, the
13981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * remote port structure will be reused. Otherwise, a new remote port
13991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * structure will be allocated.
14001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
14011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Whenever a remote port is allocated, a new fc_remote_port class
14021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device is created.
14031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
14041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
14051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
14061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
14071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
14081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds **/
14091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_rport *
14101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_add(struct Scsi_Host *shost, int channel,
14111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
14121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
141319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *fci = to_fc_internal(shost->transportt);
14141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
14151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
14161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int match = 0;
14171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
141819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
141919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Search the list of "active" rports, for an rport that has been
142019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * deleted, but we've held off the real delete while the target
142119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * is in a "blocked" state.
142219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
142319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
142419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
142519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
142619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
142719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
142819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			(rport->channel == channel)) {
142919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
143019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			switch (fc_host_tgtid_bind_type(shost)) {
143119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWPN:
143219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_NONE:
143319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_name == ids->port_name)
143419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
143519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
143619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWNN:
143719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->node_name == ids->node_name)
143819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
143919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
144019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_ID:
144119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_id == ids->port_id)
144219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
144319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
144419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
144519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
144619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (match) {
144719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				struct work_struct *work =
144819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com							&rport->dev_loss_work;
144919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
145019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->node_name, &ids->node_name,
145119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->node_name));
145219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->port_name, &ids->port_name,
145319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->port_name));
145419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_id = ids->port_id;
145519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
145619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_state = FC_PORTSTATE_ONLINE;
145719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->roles = ids->roles;
145819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
145919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				spin_unlock_irqrestore(shost->host_lock, flags);
146019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
146119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (fci->f->dd_fcrport_size)
146219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					memset(rport->dd_data, 0,
146319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
146419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
146519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
146619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 * If we were blocked, we were a target.
146719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 * If no longer a target, we leave the timer
146819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 * running in case the port changes roles
146919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 * prior to the timer expiring. If the timer
147019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 * fires, the target will be torn down.
147119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
147219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
147319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					return rport;
147419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
147519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/* restart the target */
147619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
147719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
147819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 * Stop the target timer first. Take no action
147919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 * on the del_timer failure as the state
148019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 * machine state change will validate the
148119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 * transaction.
148219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
148319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (!cancel_delayed_work(work))
148419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					flush_scheduled_work();
148519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
148619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/* initiate a scan of the target */
148719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				scsi_queue_work(shost, &rport->scan_work);
148819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
148919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				return rport;
149019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
149119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		}
149219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
149319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
149419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* Search the bindings array */
14951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
14961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		(fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
14971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* search for a matching consistent binding */
14991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		list_for_each_entry(rport, &fc_host_rport_bindings(shost),
15011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					peers) {
15021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (rport->channel != channel)
15031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				continue;
15041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			switch (fc_host_tgtid_bind_type(shost)) {
15061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWPN:
15071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_name == ids->port_name)
15081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
15091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
15101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWNN:
15111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->node_name == ids->node_name)
15121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
15131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
15141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_ID:
15151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_id == ids->port_id)
15161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
15171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
15181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_NONE: /* to keep compiler happy */
15191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
15211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (match) {
15231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				list_move_tail(&rport->peers,
15241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					&fc_host_rports(shost));
15251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
15261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
15271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
15281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (match) {
15301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->node_name, &ids->node_name,
15311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->node_name));
15321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->port_name, &ids->port_name,
15331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->port_name));
15341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_id = ids->port_id;
15351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->roles = ids->roles;
15361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_state = FC_PORTSTATE_ONLINE;
15371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
153819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			spin_unlock_irqrestore(shost->host_lock, flags);
153919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
154019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (fci->f->dd_fcrport_size)
154119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memset(rport->dd_data, 0,
154219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
154319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
15441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				/* initiate a scan of the target */
15461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				scsi_queue_work(shost, &rport->scan_work);
15471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return rport;
15491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
15501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
155219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
155319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
15541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* No consistent binding found - create new remote port entry */
15551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport = fc_rport_create(shost, channel, ids);
15561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
15581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_add);
15601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
15621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_rport_terminate - this routine tears down and deallocates a remote port.
15631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port to be terminated
15641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
15651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
15661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
15671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
15681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
15691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_rport_terminate(struct fc_rport  *rport)
15701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);
15721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct device *dev = &rport->dev;
15731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
15741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_tgt_remove(rport);
15761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_remove_device(dev);
15781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_del(dev);
15791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_destroy_device(dev);
15801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
15811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_del(&rport->peers);
15821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
15831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(&shost->shost_gendev);
15841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
15871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_remote_port_delete - notifies the fc transport that a remote
15881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		port is no longer in existence.
15891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that no longer exists
15901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
15911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport that a remote
15921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * port is no longer part of the topology. Note: Although a port
15931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * may no longer be part of the topology, it may persist in the remote
159419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * ports displayed by the fc_host. We do this under 2 conditions:
159519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * - If the port was a scsi target, we delay its deletion by "blocking" it.
159619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   This allows the port to temporarily disappear, then reappear without
159719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   disrupting the SCSI device tree attached to it. During the "blocked"
159819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   period the port will still exist.
159919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * - If the port was a scsi target and disappears for longer than we
160019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   expect, we'll delete the port and the tear down the SCSI device tree
160119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   attached to it. However, we want to semi-persist the target id assigned
160219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to that port if it eventually does exist. The port structure will
160319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   remain (although with minimal information) so that the target id
160419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   bindings remails.
16051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
16061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If the remote port is not an FCP Target, it will be fully torn down
16071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and deallocated, including the fc_remote_port class device.
16081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
160919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * If the remote port is an FCP Target, the port will be placed in a
161019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * temporary blocked state. From the LLDD's perspective, the rport no
161119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * longer exists. From the SCSI midlayer's perspective, the SCSI target
161219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * exists, but all sdevs on it are blocked from further I/O. The following
161319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * is then expected:
161419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port does not return (signaled by a LLDD call to
161519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
161619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi target is removed - killing all outstanding i/o and removing the
161719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi devices attached ot it. The port structure will be marked Not
161819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   Present and be partially cleared, leaving only enough information to
161919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   recognize the remote port relative to the scsi target id binding if
162019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   it later appears.  The port will remain as long as there is a valid
162119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   binding (e.g. until the user changes the binding type or unloads the
162219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi host with the binding).
16231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
162419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port returns within the dev_loss_tmo value (and matches
162519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   according to the target id binding type), the port structure will be
162619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   reused. If it is no longer a SCSI target, the target will be torn
162719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   down. If it continues to be a SCSI target, then the target will be
162819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   unblocked (allowing i/o to be resumed), and a scan will be activated
162919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to ensure that all luns are detected.
163019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *
163119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * Called from normal process context only - cannot be called from interrupt.
16321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
16331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
16341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
16351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds **/
16361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
16371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_delete(struct fc_rport  *rport)
16381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
163919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	int timeout = rport->dev_loss_tmo;
16401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
164119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* If no scsi target id mapping, delete it */
164219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (rport->scsi_target_id == -1) {
16431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_rport_terminate(rport);
16441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
16451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
164719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_target_block(&rport->dev);
16481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
164919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* cap the length the devices can be blocked until they are deleted */
165019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	schedule_delayed_work(&rport->dev_loss_work, timeout * HZ);
16511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
165219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->port_state = FC_PORTSTATE_BLOCKED;
16531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
16541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_delete);
16551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
16571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_remote_port_rolechg - notifies the fc transport that the roles
16581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		on a remote may have changed.
16591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that changed.
16601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
16611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport that the roles
16621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * on a remote port may have changed. The largest effect of this is
16631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * if a port now becomes a FCP Target, it must be allocated a
16641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id.  If the port is no longer a FCP target, any
16651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id value assigned to it will persist in case the
16661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * role changes back to include FCP Target. No changes in the scsi
16671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * midlayer will be invoked if the role changes (in the expectation
16681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that the role will be resumed. If it doesn't normal error processing
16691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * will take place).
16701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
16711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
16721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
16731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
16741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
16751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds **/
16761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
16771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
16781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
16791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);
16801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_host_attrs *fc_host =
16811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			(struct fc_host_attrs *)shost->shost_data;
16821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
16831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int create = 0;
16841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
168619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (roles & FC_RPORT_ROLE_FCP_TARGET) {
168719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (rport->scsi_target_id == -1) {
168819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			rport->scsi_target_id = fc_host->next_target_id++;
168919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
169019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		} else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
169119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
16921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
16941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
169519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->roles = roles;
169619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
169719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (create) {
169819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		/*
169919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * There may have been a delete timer running on the
170019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * port. Ensure that it is cancelled as we now know
170119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * the port is an FCP Target.
170219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Note: we know the rport is exists and in an online
170319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  state as the LLDD would not have had an rport
170419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  reference to pass us.
170519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *
170619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Take no action on the del_timer failure as the state
170719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * machine state change will validate the
170819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * transaction.
170919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 */
171019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (!cancel_delayed_work(&rport->dev_loss_work))
171119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			flush_scheduled_work();
171219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
17131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
17141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
171519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
17161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_rolechg);
17181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
172019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
172119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *                       was a SCSI target (thus was blocked), and failed
172219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *                       to return in the alloted time.
172319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *
172419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * @data:	rport target that failed to reappear in the alloted time.
17251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds **/
17261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
172719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comfc_timeout_deleted_rport(void  *data)
17281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
17291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = (struct fc_rport *)data;
173019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
173119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	unsigned long flags;
17321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
173319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
17341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
173619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * If the port is ONLINE, then it came back, but was no longer an
173719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * FCP target. Thus we need to tear down the scsi_target on it.
17381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
173919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (rport->port_state == FC_PORTSTATE_ONLINE) {
174019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		spin_unlock_irqrestore(shost->host_lock, flags);
17411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
174219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
174319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			"blocked FC remote port time out: removing target\n");
17441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
174519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		fc_rport_tgt_remove(rport);
17461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
174719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
174819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
17491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
175019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
175119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		spin_unlock_irqrestore(shost->host_lock, flags);
175219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
175319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			"blocked FC remote port time out: leaving target alone\n");
175419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
175519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
17561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
175719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE) {
175819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		spin_unlock_irqrestore(shost->host_lock, flags);
175919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
176019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			"blocked FC remote port time out: removing target\n");
176119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		fc_rport_terminate(rport);
176219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
176319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
17641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
176519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	dev_printk(KERN_ERR, &rport->dev,
176619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"blocked FC remote port time out: removing target and "
176719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"saving binding\n");
176819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
176919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
17701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
177219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Note: We do not remove or clear the hostdata area. This allows
177319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   host-specific target data to persist along with the
177419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   scsi_target_id. It's up to the host to manage it's hostdata area.
17751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
17761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
177719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
177819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Reinitialize port attributes that may change if the port comes back.
177919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
178019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->maxframe_size = -1;
178119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->supported_classes = FC_COS_UNSPECIFIED;
178219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->roles = FC_RPORT_ROLE_UNKNOWN;
178342e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	rport->port_state = FC_PORTSTATE_DELETED;
17841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
178519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* remove the identifiers that aren't used in the consisting binding */
178619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	switch (fc_host_tgtid_bind_type(shost)) {
178719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	case FC_TGTID_BIND_BY_WWPN:
178819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->node_name = -1;
178919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->port_id = -1;
179019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		break;
179119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	case FC_TGTID_BIND_BY_WWNN:
179219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->port_name = -1;
179319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->port_id = -1;
179419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		break;
179519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	case FC_TGTID_BIND_BY_ID:
179619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->node_name = -1;
179719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->port_name = -1;
179819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		break;
179919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	case FC_TGTID_BIND_NONE:	/* to keep compiler happy */
180019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		break;
180119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
180219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
180319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
180419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * As this only occurs if the remote port (scsi target)
180519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * went away and didn't come back - we'll remove
180619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * all attached scsi devices.
180742e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	 *
180842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	 * We'll schedule the shost work item to perform the actual removal
180942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	 * to avoid recursion in the different flush calls if we perform
181042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	 * the removal in each target - and there are lots of targets
181142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	 * whose timeouts fire at the same time.
181219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
181342e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
181442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	if ( !(fc_host_flags(shost) & FC_SHOST_RPORT_DEL_SCHEDULED)) {
181542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com		fc_host_flags(shost) |= FC_SHOST_RPORT_DEL_SCHEDULED;
181642e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com		scsi_queue_work(shost, &fc_host_rport_del_work(shost));
181742e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	}
181842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
181942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
18201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
18231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
182419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *
182519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * Will unblock the target (in case it went away and has now come back),
182619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * then invoke a scan.
182719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *
18281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @data:	remote port to be scanned.
18291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds **/
18301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
18311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_scsi_scan_rport(void *data)
18321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = (struct fc_rport *)data;
18341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
183519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_target_unblock(&rport->dev);
18361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
18371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			SCAN_WILD_CARD, 1);
18381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
184142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com/**
184242e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com * fc_shost_remove_rports - called to remove all rports that are marked
184342e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com *                       as in a deleted (not connected) state.
184442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com *
184542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com * @data:	shost whose rports are to be looked at
184642e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com **/
184742e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Comstatic void
184842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Comfc_shost_remove_rports(void  *data)
184942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com{
185042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	struct Scsi_Host *shost = (struct Scsi_Host *)data;
185142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	struct fc_rport *rport, *next_rport;
185242e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	unsigned long flags;
185342e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
185442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
185542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	while (fc_host_flags(shost) & FC_SHOST_RPORT_DEL_SCHEDULED) {
185642e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
185742e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com		fc_host_flags(shost) &= ~FC_SHOST_RPORT_DEL_SCHEDULED;
185842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
185942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Comrestart_search:
186042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com		list_for_each_entry_safe(rport, next_rport,
186142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com				&fc_host_rport_bindings(shost), peers) {
186242e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com			if (rport->port_state == FC_PORTSTATE_DELETED) {
186342e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com				rport->port_state = FC_PORTSTATE_NOTPRESENT;
186442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com				spin_unlock_irqrestore(shost->host_lock, flags);
186542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com				fc_rport_tgt_remove(rport);
186642e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com				spin_lock_irqsave(shost->host_lock, flags);
186742e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com				goto restart_search;
186842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com			}
186942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com		}
187042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
187142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	}
187242e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
187342e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com}
187442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
187542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
18761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_AUTHOR("Martin Hicks");
18771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("FC Transport Attributes");
18781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
18791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(fc_transport_init);
18811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(fc_transport_exit);
1882