scsi_transport_fc.c revision e07ebea0ccfaf627464460eb57d7f2fdbcccf8ec
19ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/*
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 *
22a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *  Copyright (C) 2004-2007   James Smart, Emulex Corporation
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    Rewrite for host, target, device, and remote port attributes,
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    statistics, and service functions...
259ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart *    Add vports, etc
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
305a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
3165d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt#include <linux/delay.h>
32ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko#include <linux/kernel.h>
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_device.h>
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_host.h>
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport.h>
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport_fc.h>
37c829c394165f981d49f05a9be228404d7a9398d4James Smart#include <scsi/scsi_cmnd.h>
3884314fd4740ad73550c76dee4a9578979d84af48James Smart#include <net/netlink.h>
3984314fd4740ad73550c76dee4a9578979d84af48James Smart#include <scsi/scsi_netlink_fc.h>
409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart#include <scsi/scsi_bsg_fc.h>
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "scsi_priv.h"
427525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori#include "scsi_transport_fc_internal.h"
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
44aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int fc_queue_work(struct Scsi_Host *, struct work_struct *);
459ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartstatic void fc_vport_sched_delete(struct work_struct *work);
46a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezstatic int fc_vport_setup(struct Scsi_Host *shost, int channel,
47a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *pdev, struct fc_vport_identifiers  *ids,
48a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport **vport);
499e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
509e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
519e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void fc_bsg_remove(struct request_queue *);
529e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void fc_bsg_goose_queue(struct fc_rport *);
53a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
54a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
5543ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie * Module Parameters
5643ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie */
5743ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
5843ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie/*
5943ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie * dev_loss_tmo: the default number of seconds that the FC transport
6043ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie *   should insulate the loss of a remote port.
6143ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
6243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie */
6343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiestatic unsigned int fc_dev_loss_tmo = 60;		/* seconds */
6443ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
6543ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiemodule_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
6643ca910a9c90566308f39f51ac03a55f94a5f83cMike ChristieMODULE_PARM_DESC(dev_loss_tmo,
6743ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		 "Maximum number of seconds that the FC transport should"
6843ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		 " insulate the loss of a remote port. Once this value is"
6943ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		 " exceeded, the scsi target is removed. Value should be"
7043ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
7143ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		 " fast_io_fail_tmo is not set.");
7243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
7343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie/*
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Redefine so that we can have same named attributes in the
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sdev/starget/host objects.
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
77ee959b00c335d7780136c5abda37809191fe52c3Tony Jones#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)		\
78ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstruct device_attribute device_attr_##_prefix##_##_name = 	\
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__ATTR(_name,_mode,_show,_store)
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_search(title, table_type, table)			\
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char *get_fc_##title##_name(enum table_type table_key)	\
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *name = NULL;						\
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
876391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value == table_key) {			\
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			name = table[i].name;				\
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;						\
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return name;							\
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_match(title, table_type, table)			\
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int get_fc_##title##_match(const char *table_key,		\
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		enum table_type *value)					\
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
1026391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (strncmp(table_key, table[i].name,			\
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				table[i].matchlen) == 0) {		\
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*value = table[i].value;			\
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0; /* success */				\
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 1; /* failure */						\
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_type values to ascii string name */
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_type	value;
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_type_names[] = {
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_UNKNOWN,		"Unknown" },
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_OTHER,		"Other" },
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NOTPRESENT,	"Not Present" },
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NPORT,	"NPort (fabric via point-to-point)" },
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NLPORT,	"NLPort (fabric via loop)" },
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_LPORT,	"LPort (private loop)" },
124951948a397e3ddc96658efd648b9d66622965b19Christof Schmitt	{ FC_PORTTYPE_PTP,	"Point-To-Point (direct nport connection)" },
125a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORTTYPE_NPIV,		"NPIV VPORT" },
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTTYPE_MAX_NAMELEN		50
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
130a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Reuse fc_port_type enum function for vport_type */
131a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define get_fc_vport_type_name get_fc_port_type_name
132a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13484314fd4740ad73550c76dee4a9578979d84af48James Smart/* Convert fc_host_event_code values to ascii string name */
13584314fd4740ad73550c76dee4a9578979d84af48James Smartstatic const struct {
13684314fd4740ad73550c76dee4a9578979d84af48James Smart	enum fc_host_event_code		value;
13784314fd4740ad73550c76dee4a9578979d84af48James Smart	char				*name;
13884314fd4740ad73550c76dee4a9578979d84af48James Smart} fc_host_event_code_names[] = {
13984314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LIP,			"lip" },
14084314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINKUP,		"link_up" },
14184314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINKDOWN,		"link_down" },
14284314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LIPRESET,		"lip_reset" },
14384314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_RSCN,			"rscn" },
14484314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_ADAPTER_CHANGE,	"adapter_chg" },
14584314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_UNKNOWN,		"port_unknown" },
14684314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_ONLINE,		"port_online" },
14784314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_OFFLINE,		"port_offline" },
14884314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_FABRIC,		"port_fabric" },
14984314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINK_UNKNOWN,		"link_unknown" },
15084314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_VENDOR_UNIQUE,	"vendor_unique" },
15184314fd4740ad73550c76dee4a9578979d84af48James Smart};
15284314fd4740ad73550c76dee4a9578979d84af48James Smartfc_enum_name_search(host_event_code, fc_host_event_code,
15384314fd4740ad73550c76dee4a9578979d84af48James Smart		fc_host_event_code_names)
15484314fd4740ad73550c76dee4a9578979d84af48James Smart#define FC_HOST_EVENT_CODE_MAX_NAMELEN	30
15584314fd4740ad73550c76dee4a9578979d84af48James Smart
15684314fd4740ad73550c76dee4a9578979d84af48James Smart
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_state values to ascii string name */
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_state	value;
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_state_names[] = {
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_UNKNOWN,		"Unknown" },
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_NOTPRESENT,	"Not Present" },
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ONLINE,		"Online" },
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_OFFLINE,		"Offline" },
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BLOCKED,		"Blocked" },
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BYPASSED,	"Bypassed" },
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_DIAGNOSTICS,	"Diagnostics" },
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LINKDOWN,	"Linkdown" },
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ERROR,		"Error" },
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LOOPBACK,	"Loopback" },
17242e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	{ FC_PORTSTATE_DELETED,		"Deleted" },
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTSTATE_MAX_NAMELEN	20
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Convert fc_vport_state values to ascii string name */
179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic struct {
180a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	enum fc_vport_state	value;
181a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char			*name;
182a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart} fc_vport_state_names[] = {
183a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_UNKNOWN,		"Unknown" },
184a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_ACTIVE,		"Active" },
185a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_DISABLED,		"Disabled" },
186a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_LINKDOWN,		"Linkdown" },
187a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_INITIALIZING,	"Initializing" },
188a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_NO_FABRIC_SUPP,	"No Fabric Support" },
189a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_NO_FABRIC_RSCS,	"No Fabric Resources" },
190a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FABRIC_LOGOUT,	"Fabric Logout" },
191a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FABRIC_REJ_WWN,	"Fabric Rejected WWN" },
192a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FAILED,		"VPort Failed" },
193a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart};
194a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
195a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define FC_VPORTSTATE_MAX_NAMELEN	24
196a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
197a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Reuse fc_vport_state enum function for vport_last_state */
198a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define get_fc_vport_last_state_name get_fc_vport_state_name
199a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
200a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_tgtid_binding_type values to ascii string name */
2020ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_tgtid_binding_type	value;
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char				*name;
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int				matchlen;
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_tgtid_binding_type_names[] = {
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_NONE, "none", 4 },
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_BINDTYPE_MAX_NAMELEN	30
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_bitfield_name_search(title, table)			\
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t							\
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsget_fc_##title##_names(u32 table_key, char *buf)		\
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{								\
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *prefix = "";					\
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t len = 0;					\
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;							\
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds								\
2276391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value & table_key) {		\
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			len += sprintf(buf + len, "%s%s",	\
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				prefix, table[i].name);		\
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prefix = ", ";				\
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}						\
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}							\
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");			\
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;						\
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_COS bit values to ascii string name */
2400ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_cos_names[] = {
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS1,	"Class 1" },
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS2,	"Class 2" },
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS3,	"Class 3" },
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS4,	"Class 4" },
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS6,	"Class 6" },
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(cos, fc_cos_names)
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_PORTSPEED bit values to ascii string name */
2540ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_speed_names[] = {
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_1GBIT,		"1 Gbit" },
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_2GBIT,		"2 Gbit" },
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_4GBIT,		"4 Gbit" },
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_10GBIT,		"10 Gbit" },
262c3d2350a8420dbf9d48f5f8a0fb72117bfcbc1b0James Smart	{ FC_PORTSPEED_8GBIT,		"8 Gbit" },
263c3d2350a8420dbf9d48f5f8a0fb72117bfcbc1b0James Smart	{ FC_PORTSPEED_16GBIT,		"16 Gbit" },
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_NOT_NEGOTIATED,	"Not Negotiated" },
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(port_speed, fc_port_speed_names)
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_fc4s (char *buf, u8 *fc4_list)
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i, len=0;
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		len += sprintf(buf + len , "0x%02x ", *fc4_list);
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
281a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Convert FC_PORT_ROLE bit values to ascii string name */
2820ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
285a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart} fc_port_role_names[] = {
286a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_FCP_TARGET,	"FCP Target" },
287a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_FCP_INITIATOR,	"FCP Initiator" },
288a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_IP_PORT,		"IP Port" },
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
290a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_bitfield_name_search(port_roles, fc_port_role_names)
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_PORTID_MASK	0xfffff0
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_ROLE_MASK  	0x00000f
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FPORT_PORTID			0x00000e
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FABCTLR_PORTID		0x00000d
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_DIRSRVR_PORTID		0x00000c
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_TIMESRVR_PORTID		0x00000b
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_MGMTSRVR_PORTID		0x00000a
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
304c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_timeout_deleted_rport(struct work_struct *work);
305c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_timeout_fail_rport_io(struct work_struct *work);
306c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_scsi_scan_rport(struct work_struct *work);
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Attribute counts pre object type...
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Increase these values if you add attributes
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_STARGET_NUM_ATTRS 	3
3130f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart#define FC_RPORT_NUM_ATTRS	10
314a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define FC_VPORT_NUM_ATTRS	9
315bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh#define FC_HOST_NUM_ATTRS	29
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_internal {
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_transport_template t;
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_function_template *f;
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * For attributes : each object has :
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of the actual attributes structures
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of null-terminated pointers to the attribute
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *     structures - used for mid-layer interaction.
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * The attribute containers for the starget and host are are
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * part of the midlayer. As the remote port is specific to the
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * fc transport, we must provide the attribute container.
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
331ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_starget_attrs[
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds							FC_STARGET_NUM_ATTRS];
333ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
335ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
336ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct transport_container rport_attr_cont;
339ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
340ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
341a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
342a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct transport_container vport_attr_cont;
343ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
344ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define to_fc_internal(tmpl)	container_of(tmpl, struct fc_internal, t)
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
349d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_target_setup(struct transport_container *tc, struct device *dev,
350ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   struct device *cdev)
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_target *starget = to_scsi_target(dev);
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * if parent is remote port, use values from remote port.
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Otherwise, this host uses the fc_transport, but not the
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * remote port interface. As such, initialize to known non-values.
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport) {
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = rport->node_name;
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = rport->port_name;
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = rport->port_id;
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = -1;
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = -1;
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = -1;
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_transport_class,
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_transport",
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_target_setup,
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
379d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_host_setup(struct transport_container *tc, struct device *dev,
380ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			 struct device *cdev)
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(dev);
383aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3859ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	/*
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set default values easily detected by the midlayer as
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * failure cases.  The scsi lldd is responsible for initializing
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * all transport attributes to valid values per host.
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
390aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->node_name = -1;
391aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_name = -1;
392aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->permanent_port_name = -1;
393aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->supported_classes = FC_COS_UNSPECIFIED;
394aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->supported_fc4s, 0,
395aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->supported_fc4s));
396aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
397aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->maxframe_size = -1;
398a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->max_npiv_vports = 0;
399aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->serial_number, 0,
400aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->serial_number));
401bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	memset(fc_host->manufacturer, 0,
402bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh		sizeof(fc_host->manufacturer));
403bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	memset(fc_host->model, 0,
404bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh		sizeof(fc_host->model));
405bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	memset(fc_host->model_description, 0,
406bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh		sizeof(fc_host->model_description));
407bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	memset(fc_host->hardware_version, 0,
408bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh		sizeof(fc_host->hardware_version));
409bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	memset(fc_host->driver_version, 0,
410bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh		sizeof(fc_host->driver_version));
411bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	memset(fc_host->firmware_version, 0,
412bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh		sizeof(fc_host->firmware_version));
413bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	memset(fc_host->optionrom_version, 0,
414bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh		sizeof(fc_host->optionrom_version));
415aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
416aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_id = -1;
417aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_type = FC_PORTTYPE_UNKNOWN;
418aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_state = FC_PORTSTATE_UNKNOWN;
419aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->active_fc4s, 0,
420aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->active_fc4s));
421aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->speed = FC_PORTSPEED_UNKNOWN;
422aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->fabric_name = -1;
423b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
424b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
425aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
426aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
427aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
428aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	INIT_LIST_HEAD(&fc_host->rports);
429aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	INIT_LIST_HEAD(&fc_host->rport_bindings);
430a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	INIT_LIST_HEAD(&fc_host->vports);
431aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->next_rport_number = 0;
432aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->next_target_id = 0;
433a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->next_vport_number = 0;
434a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse = 0;
435aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
436aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers	snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
437aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 "fc_wq_%d", shost->host_no);
438112f661d6dac9af1235d2d05299fc2c9cb876ae7Nithin Nayak Sujir	fc_host->work_q = alloc_workqueue(fc_host->work_q_name, 0, 0);
439aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host->work_q)
440aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -ENOMEM;
441aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
44243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	fc_host->dev_loss_tmo = fc_dev_loss_tmo;
443aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers	snprintf(fc_host->devloss_work_q_name,
444aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 sizeof(fc_host->devloss_work_q_name),
445aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 "fc_dl_%d", shost->host_no);
446112f661d6dac9af1235d2d05299fc2c9cb876ae7Nithin Nayak Sujir	fc_host->devloss_work_q =
447112f661d6dac9af1235d2d05299fc2c9cb876ae7Nithin Nayak Sujir			alloc_workqueue(fc_host->devloss_work_q_name, 0, 0);
448aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host->devloss_work_q) {
449aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(fc_host->work_q);
450aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->work_q = NULL;
451aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -ENOMEM;
452aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
453aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
4549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_hostadd(shost, fc_host);
4559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* ignore any bsg add error - we just can't do sgio */
4569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
4589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
4599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4609e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_host_remove(struct transport_container *tc, struct device *dev,
4619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct device *cdev)
4629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
4639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = dev_to_shost(dev);
4649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
4659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_remove(fc_host->rqst_q);
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_host_class,
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_host",
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_host_setup,
4739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			       fc_host_remove,
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Setup and Remove actions for remote ports are handled
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in the service functions below.
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_rport_class,
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_remote_ports",
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
487a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Setup and Remove actions for virtual ports are handled
488a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * in the service functions below.
489a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
490a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic DECLARE_TRANSPORT_CLASS(fc_vport_class,
491a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       "fc_vports",
492a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL,
493a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL,
494a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL);
495a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
496a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
49784314fd4740ad73550c76dee4a9578979d84af48James Smart * Netlink Infrastructure
498eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
49984314fd4740ad73550c76dee4a9578979d84af48James Smart
50084314fd4740ad73550c76dee4a9578979d84af48James Smartstatic atomic_t fc_event_seq;
50184314fd4740ad73550c76dee4a9578979d84af48James Smart
50284314fd4740ad73550c76dee4a9578979d84af48James Smart/**
50384314fd4740ad73550c76dee4a9578979d84af48James Smart * fc_get_event_number - Obtain the next sequential FC event number
50484314fd4740ad73550c76dee4a9578979d84af48James Smart *
50584314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
506eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *   We could have inlined this, but it would have required fc_event_seq to
50784314fd4740ad73550c76dee4a9578979d84af48James Smart *   be exposed. For now, live with the subroutine call.
50884314fd4740ad73550c76dee4a9578979d84af48James Smart *   Atomic used to avoid lock/unlock...
509eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
51084314fd4740ad73550c76dee4a9578979d84af48James Smartu32
51184314fd4740ad73550c76dee4a9578979d84af48James Smartfc_get_event_number(void)
51284314fd4740ad73550c76dee4a9578979d84af48James Smart{
51384314fd4740ad73550c76dee4a9578979d84af48James Smart	return atomic_add_return(1, &fc_event_seq);
51484314fd4740ad73550c76dee4a9578979d84af48James Smart}
51584314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_get_event_number);
51684314fd4740ad73550c76dee4a9578979d84af48James Smart
51784314fd4740ad73550c76dee4a9578979d84af48James Smart
51884314fd4740ad73550c76dee4a9578979d84af48James Smart/**
51984314fd4740ad73550c76dee4a9578979d84af48James Smart * fc_host_post_event - called to post an even on an fc_host.
52084314fd4740ad73550c76dee4a9578979d84af48James Smart * @shost:		host the event occurred on
52184314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_number:	fc event number obtained from get_fc_event_number()
52284314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_code:		fc_host event being posted
52384314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_data:		32bits of data for the event being posted
52484314fd4740ad73550c76dee4a9578979d84af48James Smart *
52584314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
52684314fd4740ad73550c76dee4a9578979d84af48James Smart *	This routine assumes no locks are held on entry.
527eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
52884314fd4740ad73550c76dee4a9578979d84af48James Smartvoid
52984314fd4740ad73550c76dee4a9578979d84af48James Smartfc_host_post_event(struct Scsi_Host *shost, u32 event_number,
53084314fd4740ad73550c76dee4a9578979d84af48James Smart		enum fc_host_event_code event_code, u32 event_data)
53184314fd4740ad73550c76dee4a9578979d84af48James Smart{
53284314fd4740ad73550c76dee4a9578979d84af48James Smart	struct sk_buff *skb;
53384314fd4740ad73550c76dee4a9578979d84af48James Smart	struct nlmsghdr	*nlh;
53484314fd4740ad73550c76dee4a9578979d84af48James Smart	struct fc_nl_event *event;
53584314fd4740ad73550c76dee4a9578979d84af48James Smart	const char *name;
536e07ebea0ccfaf627464460eb57d7f2fdbcccf8ecHong zhi guo	u32 len;
53784314fd4740ad73550c76dee4a9578979d84af48James Smart	int err;
53884314fd4740ad73550c76dee4a9578979d84af48James Smart
53984314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!scsi_nl_sock) {
54084314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOENT;
54184314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail;
54284314fd4740ad73550c76dee4a9578979d84af48James Smart	}
54384314fd4740ad73550c76dee4a9578979d84af48James Smart
54484314fd4740ad73550c76dee4a9578979d84af48James Smart	len = FC_NL_MSGALIGN(sizeof(*event));
54584314fd4740ad73550c76dee4a9578979d84af48James Smart
546e07ebea0ccfaf627464460eb57d7f2fdbcccf8ecHong zhi guo	skb = nlmsg_new(len, GFP_KERNEL);
54784314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!skb) {
54884314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
54984314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail;
55084314fd4740ad73550c76dee4a9578979d84af48James Smart	}
55184314fd4740ad73550c76dee4a9578979d84af48James Smart
552e07ebea0ccfaf627464460eb57d7f2fdbcccf8ecHong zhi guo	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
55384314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!nlh) {
55484314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
55584314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail_skb;
55684314fd4740ad73550c76dee4a9578979d84af48James Smart	}
557e07ebea0ccfaf627464460eb57d7f2fdbcccf8ecHong zhi guo	event = nlmsg_data(nlh);
55884314fd4740ad73550c76dee4a9578979d84af48James Smart
55984314fd4740ad73550c76dee4a9578979d84af48James Smart	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
56084314fd4740ad73550c76dee4a9578979d84af48James Smart				FC_NL_ASYNC_EVENT, len);
56184314fd4740ad73550c76dee4a9578979d84af48James Smart	event->seconds = get_seconds();
56284314fd4740ad73550c76dee4a9578979d84af48James Smart	event->vendor_id = 0;
56384314fd4740ad73550c76dee4a9578979d84af48James Smart	event->host_no = shost->host_no;
56484314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_datalen = sizeof(u32);	/* bytes */
56584314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_num = event_number;
56684314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_code = event_code;
56784314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_data = event_data;
56884314fd4740ad73550c76dee4a9578979d84af48James Smart
569ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
570ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso			GFP_KERNEL);
57184314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
57284314fd4740ad73550c76dee4a9578979d84af48James Smart
57384314fd4740ad73550c76dee4a9578979d84af48James Smartsend_fail_skb:
57484314fd4740ad73550c76dee4a9578979d84af48James Smart	kfree_skb(skb);
57584314fd4740ad73550c76dee4a9578979d84af48James Smartsend_fail:
57684314fd4740ad73550c76dee4a9578979d84af48James Smart	name = get_fc_host_event_code_name(event_code);
57784314fd4740ad73550c76dee4a9578979d84af48James Smart	printk(KERN_WARNING
57884314fd4740ad73550c76dee4a9578979d84af48James Smart		"%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
579cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		__func__, shost->host_no,
58084314fd4740ad73550c76dee4a9578979d84af48James Smart		(name) ? name : "<unknown>", event_data, err);
58184314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
58284314fd4740ad73550c76dee4a9578979d84af48James Smart}
58384314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_host_post_event);
58484314fd4740ad73550c76dee4a9578979d84af48James Smart
58584314fd4740ad73550c76dee4a9578979d84af48James Smart
58684314fd4740ad73550c76dee4a9578979d84af48James Smart/**
587eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
58884314fd4740ad73550c76dee4a9578979d84af48James Smart * @shost:		host the event occurred on
58984314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_number:	fc event number obtained from get_fc_event_number()
59084314fd4740ad73550c76dee4a9578979d84af48James Smart * @data_len:		amount, in bytes, of vendor unique data
59184314fd4740ad73550c76dee4a9578979d84af48James Smart * @data_buf:		pointer to vendor unique data
592eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @vendor_id:          Vendor id
59384314fd4740ad73550c76dee4a9578979d84af48James Smart *
59484314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
59584314fd4740ad73550c76dee4a9578979d84af48James Smart *	This routine assumes no locks are held on entry.
596eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
59784314fd4740ad73550c76dee4a9578979d84af48James Smartvoid
59884314fd4740ad73550c76dee4a9578979d84af48James Smartfc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
599f14e2e29cdd07f80de6dec168dc2bb39de37eec3James Smart		u32 data_len, char * data_buf, u64 vendor_id)
60084314fd4740ad73550c76dee4a9578979d84af48James Smart{
60184314fd4740ad73550c76dee4a9578979d84af48James Smart	struct sk_buff *skb;
60284314fd4740ad73550c76dee4a9578979d84af48James Smart	struct nlmsghdr	*nlh;
60384314fd4740ad73550c76dee4a9578979d84af48James Smart	struct fc_nl_event *event;
604e07ebea0ccfaf627464460eb57d7f2fdbcccf8ecHong zhi guo	u32 len;
60584314fd4740ad73550c76dee4a9578979d84af48James Smart	int err;
60684314fd4740ad73550c76dee4a9578979d84af48James Smart
60784314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!scsi_nl_sock) {
60884314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOENT;
60984314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail;
61084314fd4740ad73550c76dee4a9578979d84af48James Smart	}
61184314fd4740ad73550c76dee4a9578979d84af48James Smart
61284314fd4740ad73550c76dee4a9578979d84af48James Smart	len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
61384314fd4740ad73550c76dee4a9578979d84af48James Smart
614e07ebea0ccfaf627464460eb57d7f2fdbcccf8ecHong zhi guo	skb = nlmsg_new(len, GFP_KERNEL);
61584314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!skb) {
61684314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
61784314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail;
61884314fd4740ad73550c76dee4a9578979d84af48James Smart	}
61984314fd4740ad73550c76dee4a9578979d84af48James Smart
620e07ebea0ccfaf627464460eb57d7f2fdbcccf8ecHong zhi guo	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
62184314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!nlh) {
62284314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
62384314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail_skb;
62484314fd4740ad73550c76dee4a9578979d84af48James Smart	}
625e07ebea0ccfaf627464460eb57d7f2fdbcccf8ecHong zhi guo	event = nlmsg_data(nlh);
62684314fd4740ad73550c76dee4a9578979d84af48James Smart
62784314fd4740ad73550c76dee4a9578979d84af48James Smart	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
62884314fd4740ad73550c76dee4a9578979d84af48James Smart				FC_NL_ASYNC_EVENT, len);
62984314fd4740ad73550c76dee4a9578979d84af48James Smart	event->seconds = get_seconds();
63084314fd4740ad73550c76dee4a9578979d84af48James Smart	event->vendor_id = vendor_id;
63184314fd4740ad73550c76dee4a9578979d84af48James Smart	event->host_no = shost->host_no;
63284314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_datalen = data_len;	/* bytes */
63384314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_num = event_number;
63484314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_code = FCH_EVT_VENDOR_UNIQUE;
63584314fd4740ad73550c76dee4a9578979d84af48James Smart	memcpy(&event->event_data, data_buf, data_len);
63684314fd4740ad73550c76dee4a9578979d84af48James Smart
637ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
638ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso			GFP_KERNEL);
63984314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
64084314fd4740ad73550c76dee4a9578979d84af48James Smart
64184314fd4740ad73550c76dee4a9578979d84af48James Smartsend_vendor_fail_skb:
64284314fd4740ad73550c76dee4a9578979d84af48James Smart	kfree_skb(skb);
64384314fd4740ad73550c76dee4a9578979d84af48James Smartsend_vendor_fail:
64484314fd4740ad73550c76dee4a9578979d84af48James Smart	printk(KERN_WARNING
64584314fd4740ad73550c76dee4a9578979d84af48James Smart		"%s: Dropped Event : host %d vendor_unique - err %d\n",
646cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		__func__, shost->host_no, err);
64784314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
64884314fd4740ad73550c76dee4a9578979d84af48James Smart}
64984314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_host_post_vendor_event);
65084314fd4740ad73550c76dee4a9578979d84af48James Smart
65184314fd4740ad73550c76dee4a9578979d84af48James Smart
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic __init int fc_transport_init(void)
6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
65584314fd4740ad73550c76dee4a9578979d84af48James Smart	int error;
65684314fd4740ad73550c76dee4a9578979d84af48James Smart
65784314fd4740ad73550c76dee4a9578979d84af48James Smart	atomic_set(&fc_event_seq, 0);
65884314fd4740ad73550c76dee4a9578979d84af48James Smart
65984314fd4740ad73550c76dee4a9578979d84af48James Smart	error = transport_class_register(&fc_host_class);
6601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return error;
662a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = transport_class_register(&fc_vport_class);
663a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error)
66448de68a40aef032a2e198437f4781a83bfb938dbMike Christie		goto unreg_host_class;
6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = transport_class_register(&fc_rport_class);
6661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
66748de68a40aef032a2e198437f4781a83bfb938dbMike Christie		goto unreg_vport_class;
66848de68a40aef032a2e198437f4781a83bfb938dbMike Christie	error = transport_class_register(&fc_transport_class);
66948de68a40aef032a2e198437f4781a83bfb938dbMike Christie	if (error)
67048de68a40aef032a2e198437f4781a83bfb938dbMike Christie		goto unreg_rport_class;
67148de68a40aef032a2e198437f4781a83bfb938dbMike Christie	return 0;
67248de68a40aef032a2e198437f4781a83bfb938dbMike Christie
67348de68a40aef032a2e198437f4781a83bfb938dbMike Christieunreg_rport_class:
67448de68a40aef032a2e198437f4781a83bfb938dbMike Christie	transport_class_unregister(&fc_rport_class);
67548de68a40aef032a2e198437f4781a83bfb938dbMike Christieunreg_vport_class:
67648de68a40aef032a2e198437f4781a83bfb938dbMike Christie	transport_class_unregister(&fc_vport_class);
67748de68a40aef032a2e198437f4781a83bfb938dbMike Christieunreg_host_class:
67848de68a40aef032a2e198437f4781a83bfb938dbMike Christie	transport_class_unregister(&fc_host_class);
67948de68a40aef032a2e198437f4781a83bfb938dbMike Christie	return error;
6801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit fc_transport_exit(void)
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_transport_class);
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_rport_class);
6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_host_class);
687a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_class_unregister(&fc_vport_class);
6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC Remote Port Attribute Management
6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_show_function(field, format_string, sz, cast)		\
6951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
696ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##field (struct device *dev, 				\
697ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
6981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
699ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
7011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
70219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((i->f->get_rport_##field) &&				\
70319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    !((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
70442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_DELETED) ||		\
70519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_NOTPRESENT)))		\
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_rport_##field(rport);				\
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_store_function(field)					\
7111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
712ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_##field(struct device *dev,				\
713ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
714ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
717ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
7200f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;							\
72119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
72242e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||		\
72319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))		\
72419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;						\
7250f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);				\
7260f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (*cp && (*cp != '\n'))					\
7270f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EINVAL;						\
7281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_rport_##field(rport, val);				\
7291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
7301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr(field, format_string, sz)			\
7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
734ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
7361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr_cast(field, format_string, sz, cast)		\
7381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, (cast))	\
739ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
7411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rw_attr(field, format_string, sz)			\
7431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
7441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_store_function(field)					\
745ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,		\
7461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##field,				\
7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_rport_##field)
7481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_show_function(field, format_string, sz, cast)	\
7511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
752ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##field (struct device *dev, 				\
753ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
755ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
7571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr(field, format_string, sz)		\
7601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, )	\
761ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
7631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)	\
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, (cast)) \
766ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
7681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_enum_attr(title, maxlen)			\
7711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
772ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##title (struct device *dev,				\
773ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
7741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
775ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
7771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(rport->title);			\
7781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
7791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
7801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
7811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
782ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, title, S_IRUGO,			\
7831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##title, NULL)
7841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RD(field)					\
787ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
7891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
7901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
7921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
7931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)				\
795ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
7971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
7981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
8001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RW(field)					\
802ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
8031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_rport_##field) {					\
8041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].attr.mode = S_IRUGO;	\
8051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].store = NULL;		\
8061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
8071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
8081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
8091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
8101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8110f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field)				\
8120f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{									\
813ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
8140f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
8150f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	count++;							\
8160f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
8170f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
8181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport Remote Port Attributes: */
8201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Remote Port Attributes */
8221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
8241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
826ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_supported_classes (struct device *dev,
827ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				 struct device_attribute *attr, char *buf)
8281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
829ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
8301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport->supported_classes == FC_COS_UNSPECIFIED)
8311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
8321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(rport->supported_classes, buf);
8331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
834ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
8351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_supported_classes, NULL);
8361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Remote Port Attributes */
8381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
83919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com/*
84019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * dev_loss_tmo attribute
84119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com */
84243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiestatic int fc_str_to_dev_loss(const char *buf, unsigned long *val)
84343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie{
84443ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	char *cp;
84543ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
84643ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	*val = simple_strtoul(buf, &cp, 0);
84743ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	if ((*cp && (*cp != '\n')) || (*val < 0))
84843ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		return -EINVAL;
84943ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	/*
85043ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	 * Check for overflow; dev_loss_tmo is u32
85143ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	 */
85243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	if (*val > UINT_MAX)
85343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		return -EINVAL;
85443ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
85543ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	return 0;
85643ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie}
85743ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
85843ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiestatic int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
85943ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie				     unsigned long val)
86019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
86119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
86219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *i = to_fc_internal(shost->transportt);
86343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
86419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
86542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||
86619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
86719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;
868f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	/*
86936dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	 * Check for overflow; dev_loss_tmo is u32
87036dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	 */
87136dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	if (val > UINT_MAX)
87236dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke		return -EINVAL;
87336dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke
87436dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	/*
875f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	 * If fast_io_fail is off we have to cap
876f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
877f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	 */
878f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	if (rport->fast_io_fail_tmo == -1 &&
879f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	    val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
880f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		return -EINVAL;
881f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke
88219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	i->f->set_rport_dev_loss_tmo(rport, val);
88343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	return 0;
88443ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie}
88543ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
88643ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiefc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
88743ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiestatic ssize_t
88843ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiestore_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
88943ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie			    const char *buf, size_t count)
89043ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie{
89143ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	struct fc_rport *rport = transport_class_to_rport(dev);
89243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	unsigned long val;
89343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	int rc;
89443ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
89543ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	rc = fc_str_to_dev_loss(buf, &val);
89643ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	if (rc)
89743ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		return rc;
89843ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
89943ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	rc = fc_rport_set_dev_loss_tmo(rport, val);
90043ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	if (rc)
90143ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		return rc;
90219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	return count;
90319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
904ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
90519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
9061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Remote Port Attributes */
9091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
9111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
9121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
9131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
915ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_roles (struct device *dev, struct device_attribute *attr,
916ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		     char *buf)
9171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
918ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
9191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* identify any roles that are port_id specific */
9211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((rport->port_id != -1) &&
9221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
9231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					FC_WELLKNOWN_PORTID_MASK) {
9241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
9251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FPORT_PORTID:
9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Port\n");
9271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FABCTLR_PORTID:
9281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Controller\n");
9291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_DIRSRVR_PORTID:
9301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Directory Server\n");
9311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_TIMESRVR_PORTID:
9321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Time Server\n");
9331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_MGMTSRVR_PORTID:
9341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Management Server\n");
9351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
9361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Unknown Fabric Entity\n");
9371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
9381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
939a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (rport->roles == FC_PORT_ROLE_UNKNOWN)
9401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 20, "unknown\n");
941a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return get_fc_port_roles_names(rport->roles, buf);
9421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
9431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
944ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, roles, S_IRUGO,
9451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_roles, NULL);
9461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
9481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
9491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9500f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart/*
9510f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * fast_io_fail_tmo attribute
9520f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart */
9530f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic ssize_t
954ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_fast_io_fail_tmo (struct device *dev,
955ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				struct device_attribute *attr, char *buf)
9560f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
957ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
9580f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
9590f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->fast_io_fail_tmo == -1)
9600f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return snprintf(buf, 5, "off\n");
9610f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
9620f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
9630f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
9640f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic ssize_t
965ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_fast_io_fail_tmo(struct device *dev,
966ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				struct device_attribute *attr, const char *buf,
967ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				size_t count)
9680f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
9690f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	int val;
9700f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;
971ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
9720f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
9730f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
9740f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (rport->port_state == FC_PORTSTATE_DELETED) ||
9750f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
9760f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EBUSY;
9770f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (strncmp(buf, "off", 3) == 0)
9780f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		rport->fast_io_fail_tmo = -1;
9790f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	else {
9800f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		val = simple_strtoul(buf, &cp, 0);
981f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		if ((*cp && (*cp != '\n')) || (val < 0))
982f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke			return -EINVAL;
983f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		/*
984f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		 * Cap fast_io_fail by dev_loss_tmo or
985f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
986f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		 */
987f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		if ((val >= rport->dev_loss_tmo) ||
988f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		    (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
9890f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart			return -EINVAL;
990f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke
9910f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		rport->fast_io_fail_tmo = val;
9920f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	}
9930f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	return count;
9940f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
995ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
9960f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
9971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
10001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC SCSI Target Attribute Management
10011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
10021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
10041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Note: in the target show function we recognize when the remote
1005732bee7af3102cad811fb047dee8d15966efe569Uwe Kleine-König *  port is in the hierarchy and do not allow the driver to get
10061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  involved in sysfs functions. The driver only gets involved if
10071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  it's the "old" style that doesn't use rports.
10081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
10091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_show_function(field, format_string, sz, cast)	\
10101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1011ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_starget_##field (struct device *dev, 				\
1012ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			 struct device_attribute *attr, char *buf)	\
10131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1014ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct scsi_target *starget = transport_class_to_starget(dev);	\
10151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);	\
10161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
10171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);		\
10181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport)							\
10191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_##field(starget) = rport->field;		\
10201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (i->f->get_starget_##field)				\
10211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_starget_##field(starget);			\
10221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, 			\
10231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cast fc_starget_##field(starget)); 			\
10241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr(field, format_string, sz)			\
10271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, )		\
1028ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
10291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_starget_##field, NULL)
10301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr_cast(field, format_string, sz, cast)		\
10321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, (cast))	\
1033ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
10341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_starget_##field, NULL)
10351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RD(field)				\
1037ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_starget_attrs[count] = device_attr_starget_##field; \
10381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].attr.mode = S_IRUGO;		\
10391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].store = NULL;			\
10401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
10411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
10421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
10431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RW(field)				\
1045ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_starget_attrs[count] = device_attr_starget_##field; \
10461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_starget_##field) {				\
10471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].attr.mode = S_IRUGO;	\
10481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].store = NULL;		\
10491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
10501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
10511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
10521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
10531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport SCSI Target Attributes: */
10551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
10561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
10571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr(port_id, "0x%06x\n", 20);
10581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1061a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * FC Virtual Port Attribute Management
1062a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1063a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1064a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_show_function(field, format_string, sz, cast)		\
1065a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1066ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##field (struct device *dev, 				\
1067ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
1068a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1069ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1070a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1071a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1072a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((i->f->get_vport_##field) &&				\
1073a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)))	\
1074a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->f->get_vport_##field(vport);				\
1075a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, sz, format_string, cast vport->field); 	\
1076a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1077a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1078a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_store_function(field)					\
1079a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1080ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1081ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1082ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1083a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1084a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int val;							\
1085ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1086a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1087a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1088a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char *cp;							\
1089a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))	\
1090a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;						\
1091a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	val = simple_strtoul(buf, &cp, 0);				\
1092a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*cp && (*cp != '\n'))					\
1093a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1094a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->f->set_vport_##field(vport, val);				\
1095a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1096a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1097a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1098a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_store_str_function(field, slen)			\
1099a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1100ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1101ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, 			\
1102ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1103a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1104ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1105a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1106a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1107a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;						\
1108a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart									\
1109a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */			\
1110a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')						\
1111a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;							\
1112a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (cnt > ((slen) - 1))						\
1113a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1114a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memcpy(vport->field, buf, cnt);					\
1115a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->f->set_vport_##field(vport);					\
1116a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1117a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1118a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1119a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rd_attr(field, format_string, sz)			\
1120a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, )		\
1121ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1122a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			 show_fc_vport_##field, NULL)
1123a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1124a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rd_attr_cast(field, format_string, sz, cast)		\
1125a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, (cast))	\
1126ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1127a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			  show_fc_vport_##field, NULL)
1128a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1129a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rw_attr(field, format_string, sz)			\
1130a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, )		\
1131a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_store_function(field)					\
1132ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1133a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##field,				\
1134a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_vport_##field)
1135a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1136a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_show_function(field, format_string, sz, cast)	\
1137a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1138ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##field (struct device *dev,				\
1139ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
1140a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1141ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1142a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, sz, format_string, cast vport->field); 	\
1143a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1144a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1145a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_store_u32_function(field)			\
1146a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1147ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1148ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1149ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1150a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1151a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u32 val;							\
1152ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1153a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char *cp;							\
1154a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))		\
1155a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;						\
1156a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	val = simple_strtoul(buf, &cp, 0);				\
1157a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*cp && (*cp != '\n'))					\
1158a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1159a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->field = val;						\
1160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1162a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1163a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1164a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_attr(field, format_string, sz)		\
1165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, )	\
1166ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1167a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			 show_fc_vport_##field, NULL)
1168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast)	\
1170a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, (cast)) \
1171ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1172a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			  show_fc_vport_##field, NULL)
1173a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rw_u32_attr(field, format_string, sz)		\
1175a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, )	\
1176a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_store_u32_function(field)			\
1177ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##field,				\
1179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_vport_##field)
1180a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1181a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1182a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_enum_attr(title, maxlen)			\
1183a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1184ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##title (struct device *dev,				\
1185ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1186ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       char *buf)					\
1187a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1188ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1189a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	const char *name;						\
1190a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	name = get_fc_##title##_name(vport->title);			\
1191a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!name)							\
1192a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1193a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, maxlen, "%s\n", name);			\
1194a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}									\
1195ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, title, S_IRUGO,			\
1196a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##title, NULL)
1197a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1198a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1199a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_RD(field)					\
1200ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1201a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1202a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].store = NULL;			\
1203a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1204a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->get_##field)						\
1205a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		count++
1206a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: checks function not show bit */
1207a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1208a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field)				\
1209ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1210a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1211a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].store = NULL;			\
1212a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1213a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1214a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1215a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_WR(field)					\
1216ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1217a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1218a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->field)						\
1219a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		count++
1220a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: checks function */
1221a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1222a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_RW(field)					\
1223ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1224a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!i->f->set_vport_##field) {					\
1225a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->private_vport_attrs[count].attr.mode = S_IRUGO;	\
1226a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->private_vport_attrs[count].store = NULL;		\
1227a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}								\
1228a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1229a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1230a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: does not check show bit */
1231a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1232a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field)				\
1233a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1234ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1235a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1236a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++;							\
1237a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1238a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1239a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1240a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* The FC Transport Virtual Port Attributes: */
1241a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1242a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Fixed Virtual Port Attributes */
1243a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1244a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Dynamic Virtual Port Attributes */
1245a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1246a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Private Virtual Port Attributes */
1247a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1248a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1249a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1250a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1251a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1252a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1253a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1254ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1255ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		     char *buf)
1256a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1257ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
1258a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1259a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1260a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return snprintf(buf, 20, "unknown\n");
1261a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return get_fc_port_roles_names(vport->roles, buf);
1262a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1263ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1264a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1265a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1266a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1267a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_show_function(symbolic_name, "%s\n",
1268a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1269a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1270ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1271a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1272a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1273a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1274ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1275ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf, size_t count)
1276a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1277ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
12789ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	struct Scsi_Host *shost = vport_to_shost(vport);
12790d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	unsigned long flags;
12800d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen
12810d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	spin_lock_irqsave(shost->host_lock, flags);
12820d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
12830d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen		spin_unlock_irqrestore(shost->host_lock, flags);
12840d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen		return -EBUSY;
12850d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	}
12860d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	vport->flags |= FC_VPORT_DELETING;
12870d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	spin_unlock_irqrestore(shost->host_lock, flags);
1288a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
12899ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	fc_queue_work(shost, &vport->vport_delete_work);
1290a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;
1291a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1292ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1293a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			NULL, store_fc_vport_delete);
1294a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1295a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1296a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1297a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Enable/Disable vport
1298a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *  Write "1" to disable, write "0" to enable
1299a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1300a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1301ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1302ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,
1303a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			   size_t count)
1304a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1305ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
1306a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);
1307a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
1308a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
1309a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1310a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1311a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;
1312a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1313a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*buf == '0') {
1314a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (vport->vport_state != FC_VPORT_DISABLED)
1315a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EALREADY;
1316a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	} else if (*buf == '1') {
1317a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (vport->vport_state == FC_VPORT_DISABLED)
1318a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EALREADY;
1319a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	} else
1320a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1321a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1322a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1323a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1324a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1325ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1326a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			NULL, store_fc_vport_disable);
1327a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1328a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1329a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
13301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Attribute Management
13311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
13321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_show_function(field, format_string, sz, cast)		\
13341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1335ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##field (struct device *dev,				\
1336ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
13371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1338ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
13391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
13401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##field)					\
13411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##field(shost);				\
13421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
13431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_store_function(field)					\
13461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1347ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_##field(struct device *dev, 				\
1348ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr,			\
1349ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf,	size_t count)			\
13501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
13511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
1352ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
13531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
13540f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;							\
13551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
13560f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);				\
13570f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (*cp && (*cp != '\n'))					\
13580f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EINVAL;						\
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_host_##field(shost, val);				\
13601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
13611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1363b8d08210126a7b769b857720a59721a453a57a1eJames Smart#define fc_host_store_str_function(field, slen)				\
1364b8d08210126a7b769b857720a59721a453a57a1eJames Smartstatic ssize_t								\
1365ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_##field(struct device *dev,				\
1366ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr,			\
1367ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf, size_t count)			\
1368b8d08210126a7b769b857720a59721a453a57a1eJames Smart{									\
1369ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1370b8d08210126a7b769b857720a59721a453a57a1eJames Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1371b8d08210126a7b769b857720a59721a453a57a1eJames Smart	unsigned int cnt=count;						\
1372b8d08210126a7b769b857720a59721a453a57a1eJames Smart									\
1373b8d08210126a7b769b857720a59721a453a57a1eJames Smart	/* count may include a LF at end of string */			\
1374b8d08210126a7b769b857720a59721a453a57a1eJames Smart	if (buf[cnt-1] == '\n')						\
1375b8d08210126a7b769b857720a59721a453a57a1eJames Smart		cnt--;							\
1376b8d08210126a7b769b857720a59721a453a57a1eJames Smart	if (cnt > ((slen) - 1))						\
1377b8d08210126a7b769b857720a59721a453a57a1eJames Smart		return -EINVAL;						\
1378b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memcpy(fc_host_##field(shost), buf, cnt);			\
1379b8d08210126a7b769b857720a59721a453a57a1eJames Smart	i->f->set_host_##field(shost);					\
1380b8d08210126a7b769b857720a59721a453a57a1eJames Smart	return count;							\
1381b8d08210126a7b769b857720a59721a453a57a1eJames Smart}
1382b8d08210126a7b769b857720a59721a453a57a1eJames Smart
13831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr(field, format_string, sz)			\
13841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
1385ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
13871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr_cast(field, format_string, sz, cast)		\
13891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, (cast))		\
1390ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
13921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rw_attr(field, format_string, sz)			\
13941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
13951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_store_function(field)					\
1396ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
13971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_host_##field,				\
13981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_host_##field)
13991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_enum_attr(title, maxlen)				\
14011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1402ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##title (struct device *dev,				\
1403ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
14041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1405ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
14061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
14071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
14081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##title)					\
14091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##title(shost);				\
14101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(fc_host_##title(shost));		\
14111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
14121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
14131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
14141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
1415ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
14161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RD(field)					\
1418ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
14191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
14201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
14211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
14221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
14231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
14241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1425a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_HOST_ATTRIBUTE_RD_NS(field)				\
1426ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
1427a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1428a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_host_attrs[count].store = NULL;			\
1429a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->host_attrs[count] = &i->private_host_attrs[count];		\
1430a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1431a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
14321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RW(field)					\
1433ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
14341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_host_##field) {					\
14351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].attr.mode = S_IRUGO;	\
14361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].store = NULL;		\
14371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
14381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
14391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
14401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
14411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_show_function(field, format_string, sz, cast)	\
14441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1445ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##field (struct device *dev,				\
1446ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
14471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1448ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
14491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
14501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
14511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr(field, format_string, sz)		\
14531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, )	\
1454ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
14551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
14561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr_cast(field, format_string, sz, cast)	\
14581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, (cast)) \
1459ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
14601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
14611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
1463ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
14641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
14651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
14661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
14671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
14681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)			\
147091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{									\
1471ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
14721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
147391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	count++;							\
147491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
14751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Host Attributes */
14781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1480ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_classes (struct device *dev,
1481ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			        struct device_attribute *attr, char *buf)
14821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1483ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
14861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
14871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
14891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1490ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
14911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_classes, NULL);
14921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1494ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_fc4s (struct device *dev,
1495ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			     struct device_attribute *attr, char *buf)
14961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1497ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
14991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1500ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
15011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_fc4s, NULL);
15021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1504ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_speeds (struct device *dev,
1505ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			       struct device_attribute *attr, char *buf)
15061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1507ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
15101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
15111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
15131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1514ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
15151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_speeds, NULL);
15161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
15191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
15206b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmannfc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
15216b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann			     unsigned long long);
15221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1523a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
15241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1525bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikhfc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
1526bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikhfc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1527bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikhfc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1528bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikhfc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1529bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikhfc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1530bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikhfc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1531bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikhfc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
15321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Host Attributes */
15351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1537ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_active_fc4s (struct device *dev,
1538ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			  struct device_attribute *attr, char *buf)
15391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1540ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
15421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_active_fc4s)
15441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_active_fc4s(shost);
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
15471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1548ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
15491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_active_fc4s, NULL);
15501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1552ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_speed (struct device *dev,
1553ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		    struct device_attribute *attr, char *buf)
15541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1555ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
15571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_speed)
15591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_speed(shost);
15601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
15621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
15631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_speed(shost), buf);
15651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1566ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, speed, S_IRUGO,
15671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_speed, NULL);
15681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr(port_id, "0x%06x\n", 20);
15711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
15721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
15731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1574016131b8fffa1085b4ad165ab228116fdc278ebeJames Smartfc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
15751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1576b8d08210126a7b769b857720a59721a453a57a1eJames Smartfc_private_host_show_function(system_hostname, "%s\n",
1577b8d08210126a7b769b857720a59721a453a57a1eJames Smart		FC_SYMBOLIC_NAME_SIZE + 1, )
1578b8d08210126a7b769b857720a59721a453a57a1eJames Smartfc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1579ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1580b8d08210126a7b769b857720a59721a453a57a1eJames Smart		show_fc_host_system_hostname, store_fc_host_system_hostname);
1581b8d08210126a7b769b857720a59721a453a57a1eJames Smart
15821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Host Attributes */
15841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1586ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_private_host_tgtid_bind_type(struct device *dev,
1587ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				     struct device_attribute *attr, char *buf)
15881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1589ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;
15911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
15931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)
15941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
15951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
15961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1598d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com#define get_list_head_entry(pos, head, member) 		\
1599d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	pos = list_entry((head)->next, typeof(*pos), member)
1600d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com
16011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1602ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_private_host_tgtid_bind_type(struct device *dev,
1603ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *attr, const char *buf, size_t count)
16041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1605ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1606d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	struct fc_rport *rport;
16071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 	enum fc_tgtid_binding_type val;
16081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
16091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (get_fc_tgtid_bind_type_match(buf, &val))
16111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
16121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if changing bind type, purge all unused consistent bindings */
16141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (val != fc_host_tgtid_bind_type(shost)) {
16151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_lock_irqsave(shost->host_lock, flags);
1616d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		while (!list_empty(&fc_host_rport_bindings(shost))) {
1617d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com			get_list_head_entry(rport,
1618d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com				&fc_host_rport_bindings(shost), peers);
1619aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			list_del(&rport->peers);
1620aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			rport->port_state = FC_PORTSTATE_DELETED;
1621aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			fc_queue_work(shost, &rport->rport_delete_work);
1622d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		}
16231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_unlock_irqrestore(shost->host_lock, flags);
16241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_tgtid_bind_type(shost) = val;
16271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;
16281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
16291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1630ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
16311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_private_host_tgtid_bind_type,
16321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_private_host_tgtid_bind_type);
16331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
163491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquezstatic ssize_t
1635ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_private_host_issue_lip(struct device *dev,
1636ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *attr, const char *buf, size_t count)
163791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{
1638ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
163991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	struct fc_internal *i = to_fc_internal(shost->transportt);
164091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	int ret;
164191ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
164291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	/* ignore any data value written to the attribute */
164391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (i->f->issue_fc_host_lip) {
164491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		ret = i->f->issue_fc_host_lip(shost);
164591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		return ret ? ret: count;
164691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	}
164791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
164891ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	return -ENOENT;
164991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
165091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
1651ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
165291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez			store_fc_private_host_issue_lip);
165391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
165443ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiestatic ssize_t
165543ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiestore_fc_private_host_dev_loss_tmo(struct device *dev,
165643ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie				   struct device_attribute *attr,
165743ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie				   const char *buf, size_t count)
165843ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie{
165943ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	struct Scsi_Host *shost = transport_class_to_shost(dev);
166043ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
166143ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	struct fc_rport *rport;
166243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	unsigned long val, flags;
166343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	int rc;
166443ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
166543ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	rc = fc_str_to_dev_loss(buf, &val);
166643ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	if (rc)
166743ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		return rc;
166843ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
166943ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	fc_host_dev_loss_tmo(shost) = val;
167043ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	spin_lock_irqsave(shost->host_lock, flags);
167143ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	list_for_each_entry(rport, &fc_host->rports, peers)
167243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		fc_rport_set_dev_loss_tmo(rport, val);
167343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	spin_unlock_irqrestore(shost->host_lock, flags);
167443ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	return count;
167543ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie}
1676a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
167743ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiefc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
167843ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiestatic FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
167943ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		      show_fc_host_dev_loss_tmo,
168043ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie		      store_fc_private_host_dev_loss_tmo);
168143ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie
168243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christiefc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1683a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
16841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
16851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Statistics Management
16861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
16871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Show a given an attribute in the statistics group */
16891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1690ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesfc_stat_show(const struct device *dev, char *buf, unsigned long offset)
16911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1692ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
16931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
16941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_host_statistics *stats;
16951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t ret = -ENOENT;
16961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (offset > sizeof(struct fc_host_statistics) ||
16981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    offset % sizeof(u64) != 0)
16991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		WARN_ON(1);
17001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_fc_host_stats) {
17021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		stats = (i->f->get_fc_host_stats)(shost);
17031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (stats)
17041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = snprintf(buf, 20, "0x%llx\n",
17051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
17061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
17071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
17081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* generate a read-only statistics attribute */
17121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_statistic(name)						\
1713ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic ssize_t show_fcstat_##name(struct device *cd,			\
1714ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				  struct device_attribute *attr,	\
1715ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				  char *buf)				\
17161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
17171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return fc_stat_show(cd, buf, 					\
17181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    offsetof(struct fc_host_statistics, name));	\
17191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
1720ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
17211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(seconds_since_last_reset);
17231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_frames);
17241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_words);
17251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_frames);
17261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_words);
17271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(lip_count);
17281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(nos_count);
17291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(error_frames);
17301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(dumped_frames);
17311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(link_failure_count);
17321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_sync_count);
17331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_signal_count);
17341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(prim_seq_protocol_err_count);
17351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_tx_word_count);
17361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_crc_count);
17371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_requests);
17381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_requests);
17391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_control_requests);
17401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_megabytes);
17411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_megabytes);
1742e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Devfc_host_statistic(fcp_packet_alloc_failures);
1743e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Devfc_host_statistic(fcp_packet_aborts);
1744e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Devfc_host_statistic(fcp_frame_alloc_failures);
1745e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Devfc_host_statistic(fc_no_free_exch);
1746e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Devfc_host_statistic(fc_no_free_exch_xid);
1747e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Devfc_host_statistic(fc_xid_not_found);
1748e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Devfc_host_statistic(fc_xid_busy);
1749e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Devfc_host_statistic(fc_seq_not_found);
1750e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Devfc_host_statistic(fc_non_bls_resp);
17511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1753ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesfc_reset_statistics(struct device *dev, struct device_attribute *attr,
1754ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		    const char *buf, size_t count)
17551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1756ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
17571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
17581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* ignore any data value written to the attribute */
17601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->reset_fc_host_stats) {
17611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->reset_fc_host_stats(shost);
17621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return count;
17631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
17641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -ENOENT;
17661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1767ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
17681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				fc_reset_statistics);
17691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute *fc_statistics_attrs[] = {
1771ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_seconds_since_last_reset.attr,
1772ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_tx_frames.attr,
1773ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_tx_words.attr,
1774ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_rx_frames.attr,
1775ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_rx_words.attr,
1776ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_lip_count.attr,
1777ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_nos_count.attr,
1778ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_error_frames.attr,
1779ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_dumped_frames.attr,
1780ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_link_failure_count.attr,
1781ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_loss_of_sync_count.attr,
1782ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_loss_of_signal_count.attr,
1783ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_prim_seq_protocol_err_count.attr,
1784ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_invalid_tx_word_count.attr,
1785ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_invalid_crc_count.attr,
1786ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_input_requests.attr,
1787ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_output_requests.attr,
1788ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_control_requests.attr,
1789ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_input_megabytes.attr,
1790ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_output_megabytes.attr,
1791e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Dev	&device_attr_host_fcp_packet_alloc_failures.attr,
1792e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Dev	&device_attr_host_fcp_packet_aborts.attr,
1793e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Dev	&device_attr_host_fcp_frame_alloc_failures.attr,
1794e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Dev	&device_attr_host_fc_no_free_exch.attr,
1795e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Dev	&device_attr_host_fc_no_free_exch_xid.attr,
1796e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Dev	&device_attr_host_fc_xid_not_found.attr,
1797e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Dev	&device_attr_host_fc_xid_busy.attr,
1798e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Dev	&device_attr_host_fc_seq_not_found.attr,
1799e58abb0ca423fc7adcf70bee018723b87c9e38c2Vasu Dev	&device_attr_host_fc_non_bls_resp.attr,
1800ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_reset_statistics.attr,
18011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	NULL
18021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
18031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute_group fc_statistics_group = {
18051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name = "statistics",
18061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.attrs = fc_statistics_attrs,
18071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
18081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1809a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1810a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Host Vport Attributes */
1811a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1812a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int
1813a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_parse_wwn(const char *ns, u64 *nm)
1814a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1815a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int i, j;
1816a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u8 wwn[8];
1817a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1818a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memset(wwn, 0, sizeof(wwn));
1819a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1820a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* Validate and store the new name */
1821a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	for (i=0, j=0; i < 16; i++) {
1822ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko		int value;
1823ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko
1824ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko		value = hex_to_bin(*ns++);
1825ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko		if (value >= 0)
1826ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko			j = (j << 4) | value;
1827a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		else
1828a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EINVAL;
1829a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (i % 2) {
1830a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			wwn[i/2] = j & 0xff;
1831a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = 0;
1832a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		}
1833a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
1834a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1835a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*nm = wwn_to_u64(wwn);
1836a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1837a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0;
1838a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1839a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1840a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1841a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1842a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * "Short-cut" sysfs variable to create a new vport on a FC Host.
1843a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1844a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1845a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1846a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1847a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1848ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1849ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   const char *buf, size_t count)
1850a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1851ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1852a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport_identifiers vid;
1853a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1854a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;
1855a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
1856a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1857a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memset(&vid, 0, sizeof(vid));
1858a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1859a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */
1860a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')
1861a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;
1862a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1863a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* validate we have enough characters for WWPN */
1864a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((cnt != (16+1+16)) || (buf[16] != ':'))
1865a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1866a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1867a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[0], &vid.port_name);
1868a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1869a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1870a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1871a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[17], &vid.node_name);
1872a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1873a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1874a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1875a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1876a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.vport_type = FC_PORTTYPE_NPIV;
1877a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* vid.symbolic_name is already zero/NULL's */
1878a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.disable = false;		/* always enabled */
1879a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1880a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* we only allow support on Channel 0 !!! */
1881a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1882a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1883a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1884ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1885a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_host_vport_create);
1886a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1887a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1888a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1889a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * "Short-cut" sysfs variable to delete a vport on a FC Host.
1890a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Vport is identified by a string containing "<WWPN>:<WWNN>".
1891a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * The WWNs are specified as hex characters, and may *not* contain
1892a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * any prefixes (e.g. 0x, x, etc)
1893a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1894a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1895ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1896ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   const char *buf, size_t count)
1897a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1898ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1899a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1900a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1901a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u64 wwpn, wwnn;
1902a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
1903a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;
1904a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat, match;
1905a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1906a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */
1907a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')
1908a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;
1909a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1910a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* validate we have enough characters for WWPN */
1911a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((cnt != (16+1+16)) || (buf[16] != ':'))
1912a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1913a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1914a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[0], &wwpn);
1915a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1916a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1917a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1918a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[17], &wwnn);
1919a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1920a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1921a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1922a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
1923a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	match = 0;
1924a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* we only allow support on Channel 0 !!! */
1925a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_for_each_entry(vport, &fc_host->vports, peers) {
1926a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if ((vport->channel == 0) &&
1927a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		    (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
19280d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen			if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
19290d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen				break;
19300d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen			vport->flags |= FC_VPORT_DELETING;
1931a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			match = 1;
1932a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			break;
1933a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		}
1934a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
1935a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
1936a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1937a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!match)
1938a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENODEV;
1939a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1940a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_vport_terminate(vport);
1941a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1942a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1943ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1944a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_host_vport_delete);
1945a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1946a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
19471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_host_match(struct attribute_container *cont,
19481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  struct device *dev)
19491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
19511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
19521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_host_device(dev))
19541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
19551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev);
19571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
19581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
19591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
19601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
19621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.host_attrs.ac == cont;
19641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_target_match(struct attribute_container *cont,
19671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
19681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
19701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
19711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_target_device(dev))
19731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
19741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
19761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
19771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
19781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
19791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
19811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.target_attrs.ac == cont;
19831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void fc_rport_dev_release(struct device *dev)
19861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = dev_to_rport(dev);
19881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
19891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
19901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint scsi_is_fc_rport(const struct device *dev)
19931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return dev->release == fc_rport_dev_release;
19951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(scsi_is_fc_rport);
19971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_rport_match(struct attribute_container *cont,
19991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
20001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
20021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
20031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_fc_rport(dev))
20051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
20061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
20081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
20091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
20101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
20111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
20131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->rport_attr_cont.ac == cont;
20151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
20161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20175c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
2018a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic void fc_vport_dev_release(struct device *dev)
2019a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
2020a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport = dev_to_vport(dev);
2021a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev->parent);		/* release kobj parent */
2022a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	kfree(vport);
2023a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
2024a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2025a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartint scsi_is_fc_vport(const struct device *dev)
2026a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
2027a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return dev->release == fc_vport_dev_release;
2028a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
2029a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James SmartEXPORT_SYMBOL(scsi_is_fc_vport);
2030a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2031a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int fc_vport_match(struct attribute_container *cont,
2032a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			    struct device *dev)
2033a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
2034a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
2035a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost;
2036a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i;
2037a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2038a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!scsi_is_fc_vport(dev))
2039a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return 0;
2040a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport = dev_to_vport(dev);
2041a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2042a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	shost = vport_to_shost(vport);
2043a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
2044a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    != &fc_host_class.class)
2045a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return 0;
2046a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2047a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i = to_fc_internal(shost->transportt);
2048a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return &i->vport_attr_cont.ac == cont;
2049a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
2050a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2051a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2052c829c394165f981d49f05a9be228404d7a9398d4James Smart/**
2053c829c394165f981d49f05a9be228404d7a9398d4James Smart * fc_timed_out - FC Transport I/O timeout intercept handler
2054c829c394165f981d49f05a9be228404d7a9398d4James Smart * @scmd:	The SCSI command which timed out
2055c829c394165f981d49f05a9be228404d7a9398d4James Smart *
2056c829c394165f981d49f05a9be228404d7a9398d4James Smart * This routine protects against error handlers getting invoked while a
2057c829c394165f981d49f05a9be228404d7a9398d4James Smart * rport is in a blocked state, typically due to a temporarily loss of
2058c829c394165f981d49f05a9be228404d7a9398d4James Smart * connectivity. If the error handlers are allowed to proceed, requests
2059c829c394165f981d49f05a9be228404d7a9398d4James Smart * to abort i/o, reset the target, etc will likely fail as there is no way
2060c829c394165f981d49f05a9be228404d7a9398d4James Smart * to communicate with the device to perform the requested function. These
2061c829c394165f981d49f05a9be228404d7a9398d4James Smart * failures may result in the midlayer taking the device offline, requiring
2062c829c394165f981d49f05a9be228404d7a9398d4James Smart * manual intervention to restore operation.
2063c829c394165f981d49f05a9be228404d7a9398d4James Smart *
2064c829c394165f981d49f05a9be228404d7a9398d4James Smart * This routine, called whenever an i/o times out, validates the state of
2065c829c394165f981d49f05a9be228404d7a9398d4James Smart * the underlying rport. If the rport is blocked, it returns
2066c829c394165f981d49f05a9be228404d7a9398d4James Smart * EH_RESET_TIMER, which will continue to reschedule the timeout.
2067c829c394165f981d49f05a9be228404d7a9398d4James Smart * Eventually, either the device will return, or devloss_tmo will fire,
2068c829c394165f981d49f05a9be228404d7a9398d4James Smart * and when the timeout then fires, it will be handled normally.
2069c829c394165f981d49f05a9be228404d7a9398d4James Smart * If the rport is not blocked, normal error handling continues.
2070c829c394165f981d49f05a9be228404d7a9398d4James Smart *
2071c829c394165f981d49f05a9be228404d7a9398d4James Smart * Notes:
2072c829c394165f981d49f05a9be228404d7a9398d4James Smart *	This routine assumes no locks are held on entry.
2073eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2074242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboestatic enum blk_eh_timer_return
2075c829c394165f981d49f05a9be228404d7a9398d4James Smartfc_timed_out(struct scsi_cmnd *scmd)
2076c829c394165f981d49f05a9be228404d7a9398d4James Smart{
2077c829c394165f981d49f05a9be228404d7a9398d4James Smart	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
2078c829c394165f981d49f05a9be228404d7a9398d4James Smart
2079c829c394165f981d49f05a9be228404d7a9398d4James Smart	if (rport->port_state == FC_PORTSTATE_BLOCKED)
2080242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe		return BLK_EH_RESET_TIMER;
2081c829c394165f981d49f05a9be228404d7a9398d4James Smart
2082242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe	return BLK_EH_NOT_HANDLED;
2083c829c394165f981d49f05a9be228404d7a9398d4James Smart}
2084c829c394165f981d49f05a9be228404d7a9398d4James Smart
20855c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com/*
2086bda232531f0c117921690ee3c060953c8f12e5a1James Smart * Called by fc_user_scan to locate an rport on the shost that
2087bda232531f0c117921690ee3c060953c8f12e5a1James Smart * matches the channel and target id, and invoke scsi_scan_target()
2088bda232531f0c117921690ee3c060953c8f12e5a1James Smart * on the rport.
20895c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com */
2090bda232531f0c117921690ee3c060953c8f12e5a1James Smartstatic void
2091bda232531f0c117921690ee3c060953c8f12e5a1James Smartfc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
20925c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com{
20935c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com	struct fc_rport *rport;
2094bda232531f0c117921690ee3c060953c8f12e5a1James Smart	unsigned long flags;
2095bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2096bda232531f0c117921690ee3c060953c8f12e5a1James Smart	spin_lock_irqsave(shost->host_lock, flags);
20975c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
2098e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2099e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig		if (rport->scsi_target_id == -1)
2100e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig			continue;
21015c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
21020d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke		if (rport->port_state != FC_PORTSTATE_ONLINE)
21030d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke			continue;
21040d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke
2105bda232531f0c117921690ee3c060953c8f12e5a1James Smart		if ((channel == rport->channel) &&
2106bda232531f0c117921690ee3c060953c8f12e5a1James Smart		    (id == rport->scsi_target_id)) {
2107bda232531f0c117921690ee3c060953c8f12e5a1James Smart			spin_unlock_irqrestore(shost->host_lock, flags);
2108bda232531f0c117921690ee3c060953c8f12e5a1James Smart			scsi_scan_target(&rport->dev, channel, id, lun, 1);
2109bda232531f0c117921690ee3c060953c8f12e5a1James Smart			return;
2110e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig		}
2111e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	}
2112e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig
2113bda232531f0c117921690ee3c060953c8f12e5a1James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2114bda232531f0c117921690ee3c060953c8f12e5a1James Smart}
2115bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2116bda232531f0c117921690ee3c060953c8f12e5a1James Smart/*
2117bda232531f0c117921690ee3c060953c8f12e5a1James Smart * Called via sysfs scan routines. Necessary, as the FC transport
2118bda232531f0c117921690ee3c060953c8f12e5a1James Smart * wants to place all target objects below the rport object. So this
2119bda232531f0c117921690ee3c060953c8f12e5a1James Smart * routine must invoke the scsi_scan_target() routine with the rport
2120bda232531f0c117921690ee3c060953c8f12e5a1James Smart * object as the parent.
2121bda232531f0c117921690ee3c060953c8f12e5a1James Smart */
2122bda232531f0c117921690ee3c060953c8f12e5a1James Smartstatic int
2123bda232531f0c117921690ee3c060953c8f12e5a1James Smartfc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2124bda232531f0c117921690ee3c060953c8f12e5a1James Smart{
2125bda232531f0c117921690ee3c060953c8f12e5a1James Smart	uint chlo, chhi;
2126bda232531f0c117921690ee3c060953c8f12e5a1James Smart	uint tgtlo, tgthi;
2127bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2128bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2129bda232531f0c117921690ee3c060953c8f12e5a1James Smart	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2130bda232531f0c117921690ee3c060953c8f12e5a1James Smart	    ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2131bda232531f0c117921690ee3c060953c8f12e5a1James Smart		return -EINVAL;
2132bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2133bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (channel == SCAN_WILD_CARD) {
2134bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chlo = 0;
2135bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chhi = shost->max_channel + 1;
2136bda232531f0c117921690ee3c060953c8f12e5a1James Smart	} else {
2137bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chlo = channel;
2138bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chhi = channel + 1;
2139bda232531f0c117921690ee3c060953c8f12e5a1James Smart	}
2140bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2141bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (id == SCAN_WILD_CARD) {
2142bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgtlo = 0;
2143bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgthi = shost->max_id;
2144bda232531f0c117921690ee3c060953c8f12e5a1James Smart	} else {
2145bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgtlo = id;
2146bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgthi = id + 1;
2147bda232531f0c117921690ee3c060953c8f12e5a1James Smart	}
2148bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2149bda232531f0c117921690ee3c060953c8f12e5a1James Smart	for ( ; chlo < chhi; chlo++)
2150bda232531f0c117921690ee3c060953c8f12e5a1James Smart		for ( ; tgtlo < tgthi; tgtlo++)
2151bda232531f0c117921690ee3c060953c8f12e5a1James Smart			fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2152bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2153e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	return 0;
21545c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com}
21555c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
21567525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonoristatic int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
21577525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori				int result)
21587525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori{
21597525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	struct fc_internal *i = to_fc_internal(shost->transportt);
21607525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
21617525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori}
21627525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
21637525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonoristatic int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
21647525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori{
21657525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	struct fc_internal *i = to_fc_internal(shost->transportt);
21667525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	return i->f->it_nexus_response(shost, nexus, result);
21677525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori}
21687525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
21691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_transport_template *
21701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_attach_transport(struct fc_function_template *ft)
21711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
21721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int count;
217324669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen	struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
217424669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen					GFP_KERNEL);
21751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!i))
21771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
21781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
21801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.class = &fc_transport_class.class;
21811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.match = fc_target_match;
21821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_size = sizeof(struct fc_starget_attrs);
21831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.target_attrs);
21841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
21861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.class = &fc_host_class.class;
21871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.match = fc_host_match;
21881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_size = sizeof(struct fc_host_attrs);
21891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ft->get_fc_host_stats)
21901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->t.host_attrs.statistics = &fc_statistics_group;
21911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.host_attrs);
21921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
21941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.class = &fc_rport_class.class;
21951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.match = fc_rport_match;
21961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->rport_attr_cont);
21971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2198a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2199a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.class = &fc_vport_class.class;
2200a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.match = fc_vport_match;
2201a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_container_register(&i->vport_attr_cont);
2202a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
22031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f = ft;
22041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport uses the shost workq for scsi scanning */
22061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.create_work_queue = 1;
22075c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
2208c829c394165f981d49f05a9be228404d7a9398d4James Smart	i->t.eh_timed_out = fc_timed_out;
2209c829c394165f981d49f05a9be228404d7a9398d4James Smart
2210e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	i->t.user_scan = fc_user_scan;
22119ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
22127525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	/* target-mode drivers' functions */
22137525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
22147525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	i->t.it_nexus_response = fc_it_nexus_response;
22157525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
22161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Target Attributes.
22181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count = 0;
22201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(node_name);
22211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_name);
22221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_id);
22231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_STARGET_NUM_ATTRS);
22251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = NULL;
22271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Host Attributes.
22311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
22331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(node_name);
22341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_name);
22356b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
22361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
22371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
22381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
22391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2240a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_create) {
2241a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2242a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2243a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
22441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(serial_number);
2245bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	SETUP_HOST_ATTRIBUTE_RD(manufacturer);
2246bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	SETUP_HOST_ATTRIBUTE_RD(model);
2247bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	SETUP_HOST_ATTRIBUTE_RD(model_description);
2248bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	SETUP_HOST_ATTRIBUTE_RD(hardware_version);
2249bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	SETUP_HOST_ATTRIBUTE_RD(driver_version);
2250bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	SETUP_HOST_ATTRIBUTE_RD(firmware_version);
2251bb8ef587a715c6a084f80b9c311136aa765ebfadNeerav Parikh	SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
22521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_id);
22541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_type);
22551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_state);
22561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
22571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(speed);
22581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2259016131b8fffa1085b4ad165ab228116fdc278ebeJames Smart	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2260b8d08210126a7b769b857720a59721a453a57a1eJames Smart	SETUP_HOST_ATTRIBUTE_RW(system_hostname);
22611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport-managed attributes */
226343ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
22641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
226591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (ft->issue_fc_host_lip)
226691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2267a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_create)
2268a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2269a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_delete)
2270a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
22711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_HOST_NUM_ATTRS);
22731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = NULL;
22751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup Remote Port Attributes.
22781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
22801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
22811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
22821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
22831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
22841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
22851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
22861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
22871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
22881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2289fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
22901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_RPORT_NUM_ATTRS);
22921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = NULL;
22941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2295a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
2296a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * Setup Virtual Port Attributes.
2297a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
2298a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count=0;
2299a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2300a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2301a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2302a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2303a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2304a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2305a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2306a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2307a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2308a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2309a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	BUG_ON(count > FC_VPORT_NUM_ATTRS);
2310a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2311a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = NULL;
2312a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
23131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t;
23141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
23151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_attach_transport);
23161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid fc_release_transport(struct scsi_transport_template *t)
23181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
23191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(t);
23201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.target_attrs);
23221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.host_attrs);
23231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->rport_attr_cont);
2324a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_container_unregister(&i->vport_attr_cont);
23251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(i);
23271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
23281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_release_transport);
23291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2330aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2331aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_queue_work - Queue work to the fc_host workqueue.
2332aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2333aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @work:	Work to queue for execution.
2334aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart *
2335aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * Return value:
2336a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart * 	1 - work queued for execution
2337a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart *	0 - work is already queued
2338a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart *	-EINVAL - work queue doesn't exist
2339eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2340aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int
2341aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2342aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2343aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (unlikely(!fc_host_work_q(shost))) {
2344aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2345aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to queue work, "
2346aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2347aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2348aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2349aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -EINVAL;
2350aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2351aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2352aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	return queue_work(fc_host_work_q(shost), work);
2353aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2354aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2355aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2356aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_flush_work - Flush a fc_host's workqueue.
2357aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2358eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2359aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2360aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_flush_work(struct Scsi_Host *shost)
2361aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2362aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host_work_q(shost)) {
2363aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2364aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to flush work, "
2365aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2366aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2367aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return;
2368aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2369aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2370aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	flush_workqueue(fc_host_work_q(shost));
2371aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2372aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2373aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2374aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2375aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2376aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @work:	Work to queue for execution.
2377aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @delay:	jiffies to delay the work queuing
2378aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart *
2379aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * Return value:
23800f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * 	1 on success / 0 already queued / < 0 for error
2381eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2382aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int
2383c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2384aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				unsigned long delay)
2385aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2386aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (unlikely(!fc_host_devloss_work_q(shost))) {
2387aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2388aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to queue work, "
2389aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2390aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2391aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2392aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -EINVAL;
2393aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2394aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2395aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2396aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2397aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2398aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2399aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2400aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2401eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2402aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2403aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_flush_devloss(struct Scsi_Host *shost)
2404aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2405aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host_devloss_work_q(shost)) {
2406aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2407aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to flush work, "
2408aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2409aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2410aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return;
2411aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2412aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2413aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	flush_workqueue(fc_host_devloss_work_q(shost));
2414aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2415aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
24161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2418eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2419eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @shost:	Which &Scsi_Host
24201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
242125985edcedea6396277003854657b5f3cb31a628Lucas De Marchi * This routine is expected to be called immediately preceding the
24221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a driver's call to scsi_remove_host().
24231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
24241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * WARNING: A driver utilizing the fc_transport, which fails to call
2425eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *   this routine prior to scsi_remove_host(), will leave dangling
24261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects in /sys/class/fc_remote_ports. Access to any of these
24271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects can result in a system crash !!!
24281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
24291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
24301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2431eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
24321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
24331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remove_host(struct Scsi_Host *shost)
24341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2435a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport = NULL, *next_vport = NULL;
2436a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_rport *rport = NULL, *next_rport = NULL;
2437aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct workqueue_struct *work_q;
2438aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2439a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
2440a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2441a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
2442a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2443a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* Remove any vports */
24449ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
24459ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		fc_queue_work(shost, &vport->vport_delete_work);
24461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Remove any remote ports */
24481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
2449aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			&fc_host->rports, peers) {
2450aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
2451aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
2452aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
2453aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2454aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
24551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
2456aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			&fc_host->rport_bindings, peers) {
2457aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
2458aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
2459aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
2460aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2461aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2462a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2463a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2464aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all scan work items */
2465aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	scsi_flush_work(shost);
2466aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2467aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all stgt delete, and rport delete work items, then kill it  */
2468aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->work_q) {
2469aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		work_q = fc_host->work_q;
2470aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->work_q = NULL;
2471aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(work_q);
2472aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2473aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2474aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all devloss work items, then kill it  */
2475aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->devloss_work_q) {
2476aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		work_q = fc_host->devloss_work_q;
2477aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->devloss_work_q = NULL;
2478aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(work_q);
2479aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
24801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
24811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remove_host);
24821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2483fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christiestatic void fc_terminate_rport_io(struct fc_rport *rport)
2484fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie{
2485fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	struct Scsi_Host *shost = rport_to_shost(rport);
2486fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	struct fc_internal *i = to_fc_internal(shost->transportt);
2487fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie
2488fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	/* Involve the LLDD if possible to terminate all io on the rport. */
2489fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	if (i->f->terminate_rport_io)
2490fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie		i->f->terminate_rport_io(rport);
2491fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie
2492fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	/*
24935d9fb5cc1b88277bb28a2a54e51b34cacaa123c2Mike Christie	 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
2494fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 */
24955d9fb5cc1b88277bb28a2a54e51b34cacaa123c2Mike Christie	scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
2496fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie}
2497aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2498aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
249925985edcedea6396277003854657b5f3cb31a628Lucas De Marchi * fc_starget_delete - called to delete the scsi descendants of an rport
2500c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be operated on.
2501eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
2502eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Deletes target and all sdevs.
2503eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
250419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic void
2505c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_starget_delete(struct work_struct *work)
250619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
2507c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2508c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, stgt_delete_work);
250919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2510fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
251119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_remove_target(&rport->dev);
251219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
251319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2514aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2515aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2516aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_rport_final_delete - finish rport termination and delete it.
2517c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be deleted.
2518eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2519aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2520c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_rport_final_delete(struct work_struct *work)
2521aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2522c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2523c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, rport_delete_work);
2524aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct device *dev = &rport->dev;
2525aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
25260f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
252792740b24ce6ddac6534fae985aab602548692186James Smart	unsigned long flags;
25288798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	int do_callback = 0;
2529aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2530112f661d6dac9af1235d2d05299fc2c9cb876ae7Nithin Nayak Sujir	fc_terminate_rport_io(rport);
2531112f661d6dac9af1235d2d05299fc2c9cb876ae7Nithin Nayak Sujir
2532aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/*
25339ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	 * if a scan is pending, flush the SCSI Host work_q so that
2534aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * that we can reclaim the rport scan work element.
2535aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 */
2536aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (rport->flags & FC_RPORT_SCAN_PENDING)
2537aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		scsi_flush_work(shost);
2538aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
253992740b24ce6ddac6534fae985aab602548692186James Smart	/*
254092740b24ce6ddac6534fae985aab602548692186James Smart	 * Cancel any outstanding timers. These should really exist
254192740b24ce6ddac6534fae985aab602548692186James Smart	 * only when rmmod'ing the LLDD and we're asking for
254292740b24ce6ddac6534fae985aab602548692186James Smart	 * immediate termination of the rports
254392740b24ce6ddac6534fae985aab602548692186James Smart	 */
254492740b24ce6ddac6534fae985aab602548692186James Smart	spin_lock_irqsave(shost->host_lock, flags);
254592740b24ce6ddac6534fae985aab602548692186James Smart	if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
254692740b24ce6ddac6534fae985aab602548692186James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
254792740b24ce6ddac6534fae985aab602548692186James Smart		if (!cancel_delayed_work(&rport->fail_io_work))
254892740b24ce6ddac6534fae985aab602548692186James Smart			fc_flush_devloss(shost);
254992740b24ce6ddac6534fae985aab602548692186James Smart		if (!cancel_delayed_work(&rport->dev_loss_work))
255092740b24ce6ddac6534fae985aab602548692186James Smart			fc_flush_devloss(shost);
255192740b24ce6ddac6534fae985aab602548692186James Smart		spin_lock_irqsave(shost->host_lock, flags);
255292740b24ce6ddac6534fae985aab602548692186James Smart		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
255392740b24ce6ddac6534fae985aab602548692186James Smart	}
255492740b24ce6ddac6534fae985aab602548692186James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
255592740b24ce6ddac6534fae985aab602548692186James Smart
25560f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	/* Delete SCSI target and sdevs */
25570f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->scsi_target_id != -1)
2558c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		fc_starget_delete(&rport->stgt_delete_work);
255992740b24ce6ddac6534fae985aab602548692186James Smart
256092740b24ce6ddac6534fae985aab602548692186James Smart	/*
256192740b24ce6ddac6534fae985aab602548692186James Smart	 * Notify the driver that the rport is now dead. The LLDD will
256292740b24ce6ddac6534fae985aab602548692186James Smart	 * also guarantee that any communication to the rport is terminated
25634be98c0ca304c8a47998b29a7993664f71791250James Smart	 *
25644be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Avoid this call if we already called it when we preserved the
25654be98c0ca304c8a47998b29a7993664f71791250James Smart	 * rport for the binding.
256692740b24ce6ddac6534fae985aab602548692186James Smart	 */
25678798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_lock_irqsave(shost->host_lock, flags);
25684be98c0ca304c8a47998b29a7993664f71791250James Smart	if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
25698798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	    (i->f->dev_loss_tmo_callbk)) {
25708798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
25718798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		do_callback = 1;
25728798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	}
25738798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_unlock_irqrestore(shost->host_lock, flags);
25748798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
25758798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	if (do_callback)
25760f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		i->f->dev_loss_tmo_callbk(rport);
25770f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
25789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_remove(rport->rqst_q);
25799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
2580aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	transport_remove_device(dev);
2581aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	device_del(dev);
2582aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	transport_destroy_device(dev);
25833bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
25843bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(dev);			/* for self-reference */
2585aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2586aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2587aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
25881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
25891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_rport_create - allocates and creates a remote FC port.
25901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
25911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
25921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
25931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
25941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Allocates and creates the remoter port structure, including the
25961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * class and sysfs creation.
25971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
25991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2600eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
260144818efbad185eea75adad0e8cab97650a7370abAdrian Bunkstatic struct fc_rport *
26021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_rport_create(struct Scsi_Host *shost, int channel,
26031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
26041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2605aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
26061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *fci = to_fc_internal(shost->transportt);
26071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
26081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct device *dev;
26091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
26101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int error;
26111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t size;
26121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
261424669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen	rport = kzalloc(size, GFP_KERNEL);
26151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!rport)) {
2616cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		printk(KERN_ERR "%s: allocation failure\n", __func__);
26171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
26181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
26191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->maxframe_size = -1;
26211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->supported_classes = FC_COS_UNSPECIFIED;
262243ca910a9c90566308f39f51ac03a55f94a5f83cMike Christie	rport->dev_loss_tmo = fc_host->dev_loss_tmo;
26231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
26241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
26251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_id = ids->port_id;
26261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->roles = ids->roles;
26271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_state = FC_PORTSTATE_ONLINE;
26281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fci->f->dd_fcrport_size)
26291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->dd_data = &rport[1];
26301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->channel = channel;
26310f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	rport->fast_io_fail_tmo = -1;
26321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2633c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2634c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2635c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2636c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2637c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
26381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
26401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->number = fc_host->next_rport_number++;
2642a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
26431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = fc_host->next_target_id++;
26441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
26451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = -1;
2646aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_add_tail(&rport->peers, &fc_host->rports);
26473bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	get_device(&shost->shost_gendev);	/* for fc_host->rport list */
26481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
26501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev = &rport->dev;
26523bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	device_initialize(dev);			/* takes self reference */
26533bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	dev->parent = get_device(&shost->shost_gendev); /* parent reference */
26541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev->release = fc_rport_dev_release;
265571610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers	dev_set_name(dev, "rport-%d:%d-%d",
265671610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		     shost->host_no, channel, rport->number);
26571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_setup_device(dev);
26581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = device_add(dev);
26601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error) {
26611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "FC Remote Port device_add failed\n");
26621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto delete_rport;
26631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
26641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_add_device(dev);
26651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_configure_device(dev);
26661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_rportadd(shost, rport);
26689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* ignore any bsg add error - we just can't do sgio */
26699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
2670a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
26711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
2672aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->flags |= FC_RPORT_SCAN_PENDING;
26731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
2674aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
26751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
26771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsdelete_rport:
26791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_destroy_device(dev);
26801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
26811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_del(&rport->peers);
26823bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
26831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
26841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
26851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
26861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
26871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
26881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2690eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
26911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
26921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
26931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
26941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
26951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
26961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport of the existence
26971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
26981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of the port, it's FC address (port_id), and the FC4 roles that are
26991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * active for the port.
27001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
27011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * For ports that are FCP targets (aka scsi targets), the FC transport
27021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * maintains consistent target id bindings on behalf of the LLDD.
27031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * A consistent target id binding is an assignment of a target id to
27041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a remote port identifier, which persists while the scsi host is
27051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * attached. The remote port can disappear, then later reappear, and
27061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it's target id assignment remains the same. This allows for shifts
27071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in FC addressing (if binding by wwpn or wwnn) with no apparent
27081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * changes to the scsi subsystem which is based on scsi host number and
27091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * target id values.  Bindings are only valid during the attachment of
27101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the scsi host. If the host detaches, then later re-attaches, target
27111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * id bindings may change.
27121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
27131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is responsible for returning a remote port structure.
27141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The routine will search the list of remote ports it maintains
27151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * internally on behalf of consistent target id mappings. If found, the
27161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * remote port structure will be reused. Otherwise, a new remote port
27171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * structure will be allocated.
27181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
27191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Whenever a remote port is allocated, a new fc_remote_port class
27201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device is created.
27211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
27221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
27231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
27241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
27251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2726eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
27271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_rport *
27281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_add(struct Scsi_Host *shost, int channel,
27291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
27301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
273119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *fci = to_fc_internal(shost->transportt);
2732aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
27331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
27341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
27351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int match = 0;
27361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2737aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* ensure any stgt delete functions are done */
2738aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_flush_work(shost);
2739aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
274019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
274119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Search the list of "active" rports, for an rport that has been
274219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * deleted, but we've held off the real delete while the target
274319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * is in a "blocked" state.
274419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
274519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
274619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2747aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_for_each_entry(rport, &fc_host->rports, peers) {
274819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
274919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
275019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			(rport->channel == channel)) {
275119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2752aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			switch (fc_host->tgtid_bind_type) {
275319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWPN:
275419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_NONE:
275519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_name == ids->port_name)
275619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
275719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
275819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWNN:
275919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->node_name == ids->node_name)
276019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
276119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
276219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_ID:
276319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_id == ids->port_id)
276419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
276519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
276619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
276719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
276819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (match) {
276919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
277019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->node_name, &ids->node_name,
277119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->node_name));
277219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->port_name, &ids->port_name,
277319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->port_name));
277419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_id = ids->port_id;
277519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
277619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_state = FC_PORTSTATE_ONLINE;
277719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->roles = ids->roles;
277819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
277919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				spin_unlock_irqrestore(shost->host_lock, flags);
278019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
278119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (fci->f->dd_fcrport_size)
278219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					memset(rport->dd_data, 0,
278319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
278419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
278519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
278692740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were not a target, cancel the
278792740b24ce6ddac6534fae985aab602548692186James Smart				 * io terminate and rport timers, and
278892740b24ce6ddac6534fae985aab602548692186James Smart				 * we're done.
278992740b24ce6ddac6534fae985aab602548692186James Smart				 *
279092740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were a target, but our new role
279192740b24ce6ddac6534fae985aab602548692186James Smart				 * doesn't indicate a target, leave the
279292740b24ce6ddac6534fae985aab602548692186James Smart				 * timers running expecting the role to
279392740b24ce6ddac6534fae985aab602548692186James Smart				 * change as the target fully logs in. If
279492740b24ce6ddac6534fae985aab602548692186James Smart				 * it doesn't, the target will be torn down.
279592740b24ce6ddac6534fae985aab602548692186James Smart				 *
279692740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were a target, and our role shows
279792740b24ce6ddac6534fae985aab602548692186James Smart				 * we're still a target, cancel the timers
279892740b24ce6ddac6534fae985aab602548692186James Smart				 * and kick off a scan.
279919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
280019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
280192740b24ce6ddac6534fae985aab602548692186James Smart				/* was a target, not in roles */
280292740b24ce6ddac6534fae985aab602548692186James Smart				if ((rport->scsi_target_id != -1) &&
2803a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				    (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
280492740b24ce6ddac6534fae985aab602548692186James Smart					return rport;
280519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
280619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
280792740b24ce6ddac6534fae985aab602548692186James Smart				 * Stop the fail io and dev_loss timers.
280892740b24ce6ddac6534fae985aab602548692186James Smart				 * If they flush, the port_state will
280992740b24ce6ddac6534fae985aab602548692186James Smart				 * be checked and will NOOP the function.
281019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
28110f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart				if (!cancel_delayed_work(&rport->fail_io_work))
28120f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart					fc_flush_devloss(shost);
281392740b24ce6ddac6534fae985aab602548692186James Smart				if (!cancel_delayed_work(&rport->dev_loss_work))
2814aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_flush_devloss(shost);
2815aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2816aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				spin_lock_irqsave(shost->host_lock, flags);
2817aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2818fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie				rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
28194be98c0ca304c8a47998b29a7993664f71791250James Smart						  FC_RPORT_DEVLOSS_PENDING |
28204be98c0ca304c8a47998b29a7993664f71791250James Smart						  FC_RPORT_DEVLOSS_CALLBK_DONE);
282119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
28228fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie				spin_unlock_irqrestore(shost->host_lock, flags);
28238fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie
282492740b24ce6ddac6534fae985aab602548692186James Smart				/* if target, initiate a scan */
282592740b24ce6ddac6534fae985aab602548692186James Smart				if (rport->scsi_target_id != -1) {
28265d9fb5cc1b88277bb28a2a54e51b34cacaa123c2Mike Christie					scsi_target_unblock(&rport->dev,
28275d9fb5cc1b88277bb28a2a54e51b34cacaa123c2Mike Christie							    SDEV_RUNNING);
28288fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie					spin_lock_irqsave(shost->host_lock,
28298fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie							  flags);
283092740b24ce6ddac6534fae985aab602548692186James Smart					rport->flags |= FC_RPORT_SCAN_PENDING;
283192740b24ce6ddac6534fae985aab602548692186James Smart					scsi_queue_work(shost,
283292740b24ce6ddac6534fae985aab602548692186James Smart							&rport->scan_work);
283392740b24ce6ddac6534fae985aab602548692186James Smart					spin_unlock_irqrestore(shost->host_lock,
283492740b24ce6ddac6534fae985aab602548692186James Smart							flags);
28358fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie				}
2836a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart
28379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				fc_bsg_goose_queue(rport);
28389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
283919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				return rport;
284019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
284119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		}
284219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
284319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
284492740b24ce6ddac6534fae985aab602548692186James Smart	/*
284592740b24ce6ddac6534fae985aab602548692186James Smart	 * Search the bindings array
284692740b24ce6ddac6534fae985aab602548692186James Smart	 * Note: if never a FCP target, you won't be on this list
284792740b24ce6ddac6534fae985aab602548692186James Smart	 */
2848aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
28491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* search for a matching consistent binding */
28511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2852aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_for_each_entry(rport, &fc_host->rport_bindings,
28531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					peers) {
28541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (rport->channel != channel)
28551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				continue;
28561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2857aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			switch (fc_host->tgtid_bind_type) {
28581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWPN:
28591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_name == ids->port_name)
28601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
28611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
28621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWNN:
28631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->node_name == ids->node_name)
28641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
28651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
28661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_ID:
28671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_id == ids->port_id)
28681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
28691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
28701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_NONE: /* to keep compiler happy */
28711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
28721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
28731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (match) {
2875aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				list_move_tail(&rport->peers, &fc_host->rports);
28761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
28771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
28781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
28791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (match) {
28811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->node_name, &ids->node_name,
28821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->node_name));
28831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->port_name, &ids->port_name,
28841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->port_name));
28851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_id = ids->port_id;
28861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->roles = ids->roles;
28871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_state = FC_PORTSTATE_ONLINE;
2888fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie			rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
28891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
289019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (fci->f->dd_fcrport_size)
289119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memset(rport->dd_data, 0,
289219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
28938fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie			spin_unlock_irqrestore(shost->host_lock, flags);
28948fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie
28958fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie			if (ids->roles & FC_PORT_ROLE_FCP_TARGET) {
28965d9fb5cc1b88277bb28a2a54e51b34cacaa123c2Mike Christie				scsi_target_unblock(&rport->dev, SDEV_RUNNING);
289719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
28981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				/* initiate a scan of the target */
28998fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie				spin_lock_irqsave(shost->host_lock, flags);
2900aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				rport->flags |= FC_RPORT_SCAN_PENDING;
29011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				scsi_queue_work(shost, &rport->scan_work);
2902a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				spin_unlock_irqrestore(shost->host_lock, flags);
29038fb2ef89b696624ea9853868e370d2e132e76f54Mike Christie			}
29041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return rport;
29051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
29061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
29071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
290819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
290919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
29101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* No consistent binding found - create new remote port entry */
29111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport = fc_rport_create(shost, channel, ids);
29121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
29141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
29151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_add);
29161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2919eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
29201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that no longer exists
29211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
29221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport that a remote
29231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * port is no longer part of the topology. Note: Although a port
29241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * may no longer be part of the topology, it may persist in the remote
292519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * ports displayed by the fc_host. We do this under 2 conditions:
2926eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
292719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   This allows the port to temporarily disappear, then reappear without
292819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   disrupting the SCSI device tree attached to it. During the "blocked"
292919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   period the port will still exist.
2930eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * 2) If the port was a scsi target and disappears for longer than we
293119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   expect, we'll delete the port and the tear down the SCSI device tree
293219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   attached to it. However, we want to semi-persist the target id assigned
293319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to that port if it eventually does exist. The port structure will
293419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   remain (although with minimal information) so that the target id
293519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   bindings remails.
29361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
29371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If the remote port is not an FCP Target, it will be fully torn down
29381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and deallocated, including the fc_remote_port class device.
29391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
294019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * If the remote port is an FCP Target, the port will be placed in a
294119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * temporary blocked state. From the LLDD's perspective, the rport no
294219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * longer exists. From the SCSI midlayer's perspective, the SCSI target
294319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * exists, but all sdevs on it are blocked from further I/O. The following
2944eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * is then expected.
2945eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
294619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port does not return (signaled by a LLDD call to
294719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
294819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi target is removed - killing all outstanding i/o and removing the
294919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi devices attached ot it. The port structure will be marked Not
295019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   Present and be partially cleared, leaving only enough information to
295119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   recognize the remote port relative to the scsi target id binding if
295219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   it later appears.  The port will remain as long as there is a valid
295319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   binding (e.g. until the user changes the binding type or unloads the
295419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi host with the binding).
29551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
295619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port returns within the dev_loss_tmo value (and matches
295719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   according to the target id binding type), the port structure will be
295819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   reused. If it is no longer a SCSI target, the target will be torn
295919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   down. If it continues to be a SCSI target, then the target will be
296019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   unblocked (allowing i/o to be resumed), and a scan will be activated
296119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to ensure that all luns are detected.
296219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *
296319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * Called from normal process context only - cannot be called from interrupt.
29641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
29651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
29661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2967eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
29681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
29691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_delete(struct fc_rport  *rport)
29701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2971aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
297236dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	unsigned long timeout = rport->dev_loss_tmo;
2973aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	unsigned long flags;
2974aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2975aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/*
2976aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * No need to flush the fc_host work_q's, as all adds are synchronous.
2977aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 *
2978aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * We do need to reclaim the rport scan work element, so eventually
2979aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2980aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * there's still a scan pending.
2981aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 */
2982aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2983aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_lock_irqsave(shost->host_lock, flags);
29841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
298592740b24ce6ddac6534fae985aab602548692186James Smart	if (rport->port_state != FC_PORTSTATE_ONLINE) {
2986aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
29871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
29881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
29891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
299092740b24ce6ddac6534fae985aab602548692186James Smart	/*
299192740b24ce6ddac6534fae985aab602548692186James Smart	 * In the past, we if this was not an FCP-Target, we would
299292740b24ce6ddac6534fae985aab602548692186James Smart	 * unconditionally just jump to deleting the rport.
299392740b24ce6ddac6534fae985aab602548692186James Smart	 * However, rports can be used as node containers by the LLDD,
299492740b24ce6ddac6534fae985aab602548692186James Smart	 * and its not appropriate to just terminate the rport at the
299592740b24ce6ddac6534fae985aab602548692186James Smart	 * first sign of a loss in connectivity. The LLDD may want to
299692740b24ce6ddac6534fae985aab602548692186James Smart	 * send ELS traffic to re-validate the login. If the rport is
299792740b24ce6ddac6534fae985aab602548692186James Smart	 * immediately deleted, it makes it inappropriate for a node
299892740b24ce6ddac6534fae985aab602548692186James Smart	 * container.
299992740b24ce6ddac6534fae985aab602548692186James Smart	 * So... we now unconditionally wait dev_loss_tmo before
300092740b24ce6ddac6534fae985aab602548692186James Smart	 * destroying an rport.
300192740b24ce6ddac6534fae985aab602548692186James Smart	 */
300292740b24ce6ddac6534fae985aab602548692186James Smart
3003aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->port_state = FC_PORTSTATE_BLOCKED;
3004aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
3005aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags |= FC_RPORT_DEVLOSS_PENDING;
3006aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
3007aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3008aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
30097525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
30107525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	    shost->active_mode & MODE_TARGET)
30117525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
30127525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
301319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_target_block(&rport->dev);
30141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30150f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	/* see if we need to kill io faster than waiting for device loss */
30160f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((rport->fast_io_fail_tmo != -1) &&
3017fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	    (rport->fast_io_fail_tmo < timeout))
30180f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		fc_queue_devloss_work(shost, &rport->fail_io_work,
30190f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart					rport->fast_io_fail_tmo * HZ);
30200f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
302119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* cap the length the devices can be blocked until they are deleted */
3022aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
30231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
30241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_delete);
30251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
3027eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
30281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that changed.
3029eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @roles:      New roles for this port.
30301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
3031eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Description: The LLDD calls this routine to notify the transport that the
3032eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * roles on a remote port may have changed. The largest effect of this is
30331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * if a port now becomes a FCP Target, it must be allocated a
30341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id.  If the port is no longer a FCP target, any
30351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id value assigned to it will persist in case the
30361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * role changes back to include FCP Target. No changes in the scsi
30371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * midlayer will be invoked if the role changes (in the expectation
30381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that the role will be resumed. If it doesn't normal error processing
30391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * will take place).
30401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
30411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
30421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
30431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
30441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
3045eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
30461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
30471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
30481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
30491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);
3050aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
30511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
30521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int create = 0;
30537525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	int ret;
30541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
3056a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (roles & FC_PORT_ROLE_FCP_TARGET) {
305719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (rport->scsi_target_id == -1) {
305819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			rport->scsi_target_id = fc_host->next_target_id++;
305919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
3060a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		} else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
306119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
30627525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	} else if (shost->active_mode & MODE_TARGET) {
30637525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
30647525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori					     (char *)&rport->node_name);
30657525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		if (ret)
30667525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori			printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
30677525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori			       ret);
30681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
30691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
307019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->roles = roles;
307119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
3072aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3073aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
307419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (create) {
307519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		/*
307619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * There may have been a delete timer running on the
307719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * port. Ensure that it is cancelled as we now know
307819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * the port is an FCP Target.
307919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Note: we know the rport is exists and in an online
308019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  state as the LLDD would not have had an rport
308119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  reference to pass us.
308219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *
308319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Take no action on the del_timer failure as the state
308419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * machine state change will validate the
308519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * transaction.
308619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 */
30870f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		if (!cancel_delayed_work(&rport->fail_io_work))
30880f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart			fc_flush_devloss(shost);
308919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (!cancel_delayed_work(&rport->dev_loss_work))
3090aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			fc_flush_devloss(shost);
3091aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
3092aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_lock_irqsave(shost->host_lock, flags);
3093fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie		rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
30940eecee413d3157bac4477362bfc4b907b4b2988bJames Smart				  FC_RPORT_DEVLOSS_PENDING |
30950eecee413d3157bac4477362bfc4b907b4b2988bJames Smart				  FC_RPORT_DEVLOSS_CALLBK_DONE);
3096aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3097aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
3098aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		/* ensure any stgt delete functions are done */
3099aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_flush_work(shost);
310019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
31015d9fb5cc1b88277bb28a2a54e51b34cacaa123c2Mike Christie		scsi_target_unblock(&rport->dev, SDEV_RUNNING);
31021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
3103aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_lock_irqsave(shost->host_lock, flags);
3104aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->flags |= FC_RPORT_SCAN_PENDING;
31051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
3106aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
310719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
31081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
31091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_rolechg);
31101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
31111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
3112eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
311392740b24ce6ddac6534fae985aab602548692186James Smart * @work:	rport target that failed to reappear in the allotted time.
3114eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
3115eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Description: An attempt to delete a remote port blocks, and if it fails
3116eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *              to return in the allotted time this gets called.
3117eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
31181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
3119c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_timeout_deleted_rport(struct work_struct *work)
31201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3121c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3122c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, dev_loss_work.work);
312319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
31244be98c0ca304c8a47998b29a7993664f71791250James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
3125aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
312619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	unsigned long flags;
31278798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	int do_callback = 0;
31281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
312919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
31301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3131aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3132aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
31331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
313492740b24ce6ddac6534fae985aab602548692186James Smart	 * If the port is ONLINE, then it came back. If it was a SCSI
313592740b24ce6ddac6534fae985aab602548692186James Smart	 * target, validate it still is. If not, tear down the
313692740b24ce6ddac6534fae985aab602548692186James Smart	 * scsi_target on it.
31371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3138aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
313992740b24ce6ddac6534fae985aab602548692186James Smart	    (rport->scsi_target_id != -1) &&
3140a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
314119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
3142aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"blocked FC remote port time out: no longer"
3143aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			" a FCP target, removing starget\n");
3144aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
31455d9fb5cc1b88277bb28a2a54e51b34cacaa123c2Mike Christie		scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
3146a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		fc_queue_work(shost, &rport->stgt_delete_work);
314719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
314819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
31491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
315092740b24ce6ddac6534fae985aab602548692186James Smart	/* NOOP state - we're flushing workq's */
315119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
315219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		spin_unlock_irqrestore(shost->host_lock, flags);
315319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
315492740b24ce6ddac6534fae985aab602548692186James Smart			"blocked FC remote port time out: leaving"
315592740b24ce6ddac6534fae985aab602548692186James Smart			" rport%s alone\n",
315692740b24ce6ddac6534fae985aab602548692186James Smart			(rport->scsi_target_id != -1) ?  " and starget" : "");
315719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
315819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
31591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
316092740b24ce6ddac6534fae985aab602548692186James Smart	if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
316192740b24ce6ddac6534fae985aab602548692186James Smart	    (rport->scsi_target_id == -1)) {
3162aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
3163aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
316419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
316592740b24ce6ddac6534fae985aab602548692186James Smart			"blocked FC remote port time out: removing"
316692740b24ce6ddac6534fae985aab602548692186James Smart			" rport%s\n",
316792740b24ce6ddac6534fae985aab602548692186James Smart			(rport->scsi_target_id != -1) ?  " and starget" : "");
3168aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
3169aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
317019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
317119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
31721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
317319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	dev_printk(KERN_ERR, &rport->dev,
317419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"blocked FC remote port time out: removing target and "
317519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"saving binding\n");
317619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
3177aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_move_tail(&rport->peers, &fc_host->rport_bindings);
31781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
31791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
318019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Note: We do not remove or clear the hostdata area. This allows
318119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   host-specific target data to persist along with the
318219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   scsi_target_id. It's up to the host to manage it's hostdata area.
31831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
31841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
318519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
318619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Reinitialize port attributes that may change if the port comes back.
318719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
318819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->maxframe_size = -1;
318919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->supported_classes = FC_COS_UNSPECIFIED;
3190a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	rport->roles = FC_PORT_ROLE_UNKNOWN;
3191aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->port_state = FC_PORTSTATE_NOTPRESENT;
3192fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
31931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3194f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	/*
3195f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * Pre-emptively kill I/O rather than waiting for the work queue
3196f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * item to teardown the starget. (FCOE libFC folks prefer this
3197f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * and to have the rport_port_id still set when it's done).
3198f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 */
3199f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3200f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	fc_terminate_rport_io(rport);
3201f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart
32028798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_lock_irqsave(shost->host_lock, flags);
32038798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
32048798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	if (rport->port_state == FC_PORTSTATE_NOTPRESENT) {	/* still missing */
3205f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart
32068798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		/* remove the identifiers that aren't used in the consisting binding */
32078798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		switch (fc_host->tgtid_bind_type) {
32088798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_BY_WWPN:
32098798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->node_name = -1;
32108798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_id = -1;
32118798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
32128798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_BY_WWNN:
32138798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_name = -1;
32148798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_id = -1;
32158798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
32168798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_BY_ID:
32178798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->node_name = -1;
32188798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_name = -1;
32198798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
32208798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_NONE:	/* to keep compiler happy */
32218798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
32228798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		}
32238798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
32248798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		/*
32258798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 * As this only occurs if the remote port (scsi target)
32268798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 * went away and didn't come back - we'll remove
32278798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 * all attached scsi devices.
32288798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 */
32298798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
32308798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		fc_queue_work(shost, &rport->stgt_delete_work);
32318798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
32328798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		do_callback = 1;
323319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
323419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
32358798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_unlock_irqrestore(shost->host_lock, flags);
32364be98c0ca304c8a47998b29a7993664f71791250James Smart
32374be98c0ca304c8a47998b29a7993664f71791250James Smart	/*
32384be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Notify the driver that the rport is now dead. The LLDD will
32394be98c0ca304c8a47998b29a7993664f71791250James Smart	 * also guarantee that any communication to the rport is terminated
32404be98c0ca304c8a47998b29a7993664f71791250James Smart	 *
32414be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Note: we set the CALLBK_DONE flag above to correspond
32424be98c0ca304c8a47998b29a7993664f71791250James Smart	 */
32438798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	if (do_callback && i->f->dev_loss_tmo_callbk)
32444be98c0ca304c8a47998b29a7993664f71791250James Smart		i->f->dev_loss_tmo_callbk(rport);
32451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
32461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32474be98c0ca304c8a47998b29a7993664f71791250James Smart
32481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
3249eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3250c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	rport to terminate io on.
32510f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart *
32520f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * Notes: Only requests the failure of the io, not that all are flushed
32530f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart *    prior to returning.
3254eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
32550f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic void
3256c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_timeout_fail_rport_io(struct work_struct *work)
32570f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
3258c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3259c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, fail_io_work.work);
32600f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
32610f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->port_state != FC_PORTSTATE_BLOCKED)
32620f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return;
32630f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
3264fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3265fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
32660f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
32670f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
32680f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart/**
32691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3270c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be scanned.
3271eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
32721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
3273c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_scsi_scan_rport(struct work_struct *work)
32741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3275c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3276c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, scan_work);
3277aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
327803f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	struct fc_internal *i = to_fc_internal(shost->transportt);
327942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	unsigned long flags;
328042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
3281aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
328203f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	    (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
328303f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	    !(i->f->disable_target_scan)) {
3284aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		scsi_scan_target(&rport->dev, rport->channel,
3285aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			rport->scsi_target_id, SCAN_WILD_CARD, 1);
328642e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	}
3287aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
3288aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_lock_irqsave(shost->host_lock, flags);
3289aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags &= ~FC_RPORT_SCAN_PENDING;
329042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
329142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com}
329242e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
329365d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt/**
329465d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
329565d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * @cmnd: SCSI command that scsi_eh is trying to recover
329665d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt *
329765d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * This routine can be called from a FC LLD scsi_eh callback. It
329865d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * blocks the scsi_eh thread until the fc_rport leaves the
32992f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
33002f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt * necessary to avoid the scsi_eh failing recovery actions for blocked
33012f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt * rports which would lead to offlined SCSI devices.
33022f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt *
33032f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
33042f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt *	    FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
33052f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt *	    passed back to scsi_eh.
330665d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt */
33072f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmittint fc_block_scsi_eh(struct scsi_cmnd *cmnd)
330865d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt{
330965d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	struct Scsi_Host *shost = cmnd->device->host;
331065d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
331165d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	unsigned long flags;
331265d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt
331365d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	spin_lock_irqsave(shost->host_lock, flags);
33142f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt	while (rport->port_state == FC_PORTSTATE_BLOCKED &&
33152f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt	       !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
331665d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt		spin_unlock_irqrestore(shost->host_lock, flags);
331765d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt		msleep(1000);
331865d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt		spin_lock_irqsave(shost->host_lock, flags);
331965d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	}
332065d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	spin_unlock_irqrestore(shost->host_lock, flags);
33212f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt
33222f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt	if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
33232f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt		return FAST_IO_FAIL;
33242f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt
33252f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt	return 0;
332665d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt}
332765d430fa99cbd0e88d09a3343f697c51fc8a7009Christof SchmittEXPORT_SYMBOL(fc_block_scsi_eh);
332842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
3329a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/**
3330a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * fc_vport_setup - allocates and creates a FC virtual port.
3331a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @shost:	scsi host the virtual port is connected to.
3332a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @channel:	Channel on shost port connected to.
3333a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @pdev:	parent device for vport
3334a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @ids:	The world wide names, FC4 port roles, etc for
3335a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *              the virtual port.
3336a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @ret_vport:	The pointer to the created vport.
3337a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3338a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Allocates and creates the vport structure, calls the parent host
3339a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * to instantiate the vport, the completes w/ class and sysfs creation.
3340a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3341a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Notes:
3342a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *	This routine assumes no locks are held on entry.
3343eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
3344a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int
3345a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezfc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3346a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport_identifiers  *ids, struct fc_vport **ret_vport)
3347a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
3348a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3349a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *fci = to_fc_internal(shost->transportt);
3350a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
3351a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *dev;
3352a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
3353a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	size_t size;
3354a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int error;
3355a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3356a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*ret_vport = NULL;
3357a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3358a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ( ! fci->f->vport_create)
3359a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOENT;
3360a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3361a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3362a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport = kzalloc(size, GFP_KERNEL);
3363a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (unlikely(!vport)) {
3364cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		printk(KERN_ERR "%s: allocation failure\n", __func__);
3365a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOMEM;
3366a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3367a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3368a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_state = FC_VPORT_UNKNOWN;
3369a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_last_state = FC_VPORT_UNKNOWN;
3370a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->node_name = ids->node_name;
3371a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->port_name = ids->port_name;
3372a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->roles = ids->roles;
3373a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_type = ids->vport_type;
3374a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (fci->f->dd_fcvport_size)
3375a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		vport->dd_data = &vport[1];
3376a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->shost = shost;
3377a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->channel = channel;
3378a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags = FC_VPORT_CREATING;
33799ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3380a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3381a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3382a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3383a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3384a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3385a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		kfree(vport);
3386a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOSPC;
3387a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3388a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse++;
3389a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->number = fc_host->next_vport_number++;
3390a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_add_tail(&vport->peers, &fc_host->vports);
3391a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	get_device(&shost->shost_gendev);	/* for fc_host->vport list */
3392a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3393a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3394a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3395a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev = &vport->dev;
3396a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_initialize(dev);			/* takes self reference */
3397a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev->parent = get_device(pdev);		/* takes parent reference */
3398a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev->release = fc_vport_dev_release;
339971610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers	dev_set_name(dev, "vport-%d:%d-%d",
340071610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		     shost->host_no, channel, vport->number);
3401a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_setup_device(dev);
3402a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3403a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = device_add(dev);
3404a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error) {
3405a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		printk(KERN_ERR "FC Virtual Port device_add failed\n");
3406a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		goto delete_vport;
3407a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3408a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_add_device(dev);
3409a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_configure_device(dev);
3410a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3411a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = fci->f->vport_create(vport, ids->disable);
3412a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error) {
3413a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3414a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		goto delete_vport_all;
3415a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3416a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3417a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
3418a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * if the parent isn't the physical adapter's Scsi_Host, ensure
3419a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * the Scsi_Host at least contains ia symlink to the vport.
3420a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
3421a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (pdev != &shost->shost_gendev) {
3422a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		error = sysfs_create_link(&shost->shost_gendev.kobj,
342371610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers				 &dev->kobj, dev_name(dev));
3424a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (error)
3425a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			printk(KERN_ERR
3426a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				"%s: Cannot create vport symlinks for "
3427a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				"%s, err=%d\n",
342871610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers				__func__, dev_name(dev), error);
3429a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3430a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3431a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags &= ~FC_VPORT_CREATING;
3432a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3433a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3434a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev_printk(KERN_NOTICE, pdev,
343571610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers			"%s created via shost%d channel %d\n", dev_name(dev),
3436a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			shost->host_no, channel);
3437a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3438a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*ret_vport = vport;
3439a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3440a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0;
3441a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3442a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartdelete_vport_all:
3443a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_remove_device(dev);
3444a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_del(dev);
3445a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartdelete_vport:
3446a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_destroy_device(dev);
3447a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3448a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_del(&vport->peers);
3449a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(&shost->shost_gendev);	/* for fc_host->vport list */
3450a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse--;
3451a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3452a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev->parent);
3453a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	kfree(vport);
3454a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3455a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return error;
3456a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
3457a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3458a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez/**
3459a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * fc_vport_create - Admin App or LLDD requests creation of a vport
3460a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @shost:	scsi host the virtual port is connected to.
3461a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @channel:	channel on shost port connected to.
3462a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @ids:	The world wide names, FC4 port roles, etc for
3463a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *              the virtual port.
3464a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *
3465a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * Notes:
3466a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *	This routine assumes no locks are held on entry.
3467a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez */
3468a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezstruct fc_vport *
3469a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezfc_vport_create(struct Scsi_Host *shost, int channel,
3470a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	struct fc_vport_identifiers *ids)
3471a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez{
3472a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	int stat;
3473a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	struct fc_vport *vport;
3474a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez
3475a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3476a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez		 ids, &vport);
3477a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	return stat ? NULL : vport;
3478a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez}
3479a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew VasquezEXPORT_SYMBOL(fc_vport_create);
3480a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3481a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/**
3482a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3483a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @vport:	fc_vport to be terminated
3484a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3485a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Calls the LLDD vport_delete() function, then deallocates and removes
3486a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * the vport from the shost and object tree.
3487a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3488a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Notes:
3489a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *	This routine assumes no locks are held on entry.
3490eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
3491a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartint
3492a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_vport_terminate(struct fc_vport *vport)
3493a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
3494a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);
3495a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3496a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
3497a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *dev = &vport->dev;
3498a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
3499a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
3500a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3501a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->vport_delete)
3502a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		stat = i->f->vport_delete(vport);
3503a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	else
3504a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		stat = -ENOENT;
3505a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3506a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3507a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags &= ~FC_VPORT_DELETING;
3508a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!stat) {
3509a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		vport->flags |= FC_VPORT_DELETED;
3510a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		list_del(&vport->peers);
3511a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		fc_host->npiv_vports_inuse--;
3512a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		put_device(&shost->shost_gendev);  /* for fc_host->vport list */
3513a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3514a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3515a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3516a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
3517a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
3518a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3519a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (dev->parent != &shost->shost_gendev)
352071610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3521a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_remove_device(dev);
3522a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_del(dev);
3523a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_destroy_device(dev);
3524a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3525a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
3526a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * Removing our self-reference should mean our
3527a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * release function gets called, which will drop the remaining
3528a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * parent reference and free the data structure.
3529a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
3530a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev);			/* for self-reference */
3531a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3532a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0; /* SUCCESS */
3533a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
3534a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James SmartEXPORT_SYMBOL(fc_vport_terminate);
3535a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
35369ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/**
35379ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart * fc_vport_sched_delete - workq-based delete request for a vport
35389ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart * @work:	vport to be deleted.
3539eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
35409ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartstatic void
35419ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartfc_vport_sched_delete(struct work_struct *work)
35429ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart{
35439ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	struct fc_vport *vport =
35449ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		container_of(work, struct fc_vport, vport_delete_work);
35459ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	int stat;
35469ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
35479ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	stat = fc_vport_terminate(vport);
35489ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	if (stat)
35499ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		dev_printk(KERN_ERR, vport->dev.parent,
35509ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart			"%s: %s could not be deleted created via "
3551cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison			"shost%d channel %d - error %d\n", __func__,
355271610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers			dev_name(&vport->dev), vport->shost->host_no,
35539ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart			vport->channel, stat);
35549ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart}
35559ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
3556a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
35579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/*
35589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * BSG support
35599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
35609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
35639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
35649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	fc_bsg_job that is to be torn down
35659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
35669e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
35679e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_destroy_bsgjob(struct fc_bsg_job *job)
35689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
35699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	unsigned long flags;
35709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irqsave(&job->job_lock, flags);
35729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->ref_cnt) {
35739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		spin_unlock_irqrestore(&job->job_lock, flags);
35749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
35759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
35769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irqrestore(&job->job_lock, flags);
35779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(job->dev);	/* release reference for the request */
35799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->request_payload.sg_list);
35819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->reply_payload.sg_list);
35829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job);
35839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
35849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
35869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
35879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart *                  completed
35889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	fc_bsg_job that is complete
35899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
35909e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
35919e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_jobdone(struct fc_bsg_job *job)
35929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
35939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req = job->req;
35949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *rsp = req->next_rq;
35959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
35969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = job->req->errors = job->reply->result;
3598b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali
35999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err < 0)
36009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* we're only returning the result field in the reply */
36019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->req->sense_len = sizeof(uint32_t);
36029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
36039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->req->sense_len = job->reply_len;
36049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* we assume all request payload was transferred, residual == 0 */
36069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	req->resid_len = 0;
36079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rsp) {
36099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
36109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* set reply (bidi) residual */
36129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
36139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				      rsp->resid_len);
36149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
3615b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_complete_request(req);
3616b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali}
3617b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali
3618b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali/**
3619b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
3620fe5d20c818a8c5fe83d9f2223a051fb5bc50d180Randy Dunlap * @rq:        BSG request that holds the job to be destroyed
3621b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali */
3622b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavalistatic void fc_bsg_softirq_done(struct request *rq)
3623b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali{
3624b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	struct fc_bsg_job *job = rq->special;
3625b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	unsigned long flags;
36269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
3627b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	spin_lock_irqsave(&job->job_lock, flags);
3628b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	job->state_flags |= FC_RQST_STATE_DONE;
3629b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	job->ref_cnt--;
3630b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	spin_unlock_irqrestore(&job->job_lock, flags);
36319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
3632b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_end_request_all(rq, rq->errors);
36339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_destroy_bsgjob(job);
36349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
36359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
36379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_job_timeout - handler for when a bsg request timesout
36389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @req:	request that timed out
36399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
36409e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum blk_eh_timer_return
36419e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_job_timeout(struct request *req)
36429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
36439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job = (void *) req->special;
36449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = job->shost;
36459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
36469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	unsigned long flags;
36479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err = 0, done = 0;
36489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
36509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return BLK_EH_RESET_TIMER;
36519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irqsave(&job->job_lock, flags);
36539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->state_flags & FC_RQST_STATE_DONE)
36549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		done = 1;
36559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
36569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->ref_cnt++;
36579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irqrestore(&job->job_lock, flags);
36589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!done && i->f->bsg_timeout) {
36609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* call LLDD to abort the i/o as it has timed out */
36619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		err = i->f->bsg_timeout(job);
3662b8f08645f80a0c93246f3539ce53d3cae6a91fa0Swen Schillig		if (err == -EAGAIN) {
3663b8f08645f80a0c93246f3539ce53d3cae6a91fa0Swen Schillig			job->ref_cnt--;
3664b8f08645f80a0c93246f3539ce53d3cae6a91fa0Swen Schillig			return BLK_EH_RESET_TIMER;
3665b8f08645f80a0c93246f3539ce53d3cae6a91fa0Swen Schillig		} else if (err)
36669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
36679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"abort failed with status %d\n", err);
36689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
36699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* the blk_end_sync_io() doesn't check the error */
367147e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali	if (done)
367247e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali		return BLK_EH_NOT_HANDLED;
367347e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali	else
367447e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali		return BLK_EH_HANDLED;
36759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
36769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36779e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
36789e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
36799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
36809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
36819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(!req->nr_phys_segments);
36839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->sg_list = kzalloc(sz, GFP_KERNEL);
36859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!buf->sg_list)
36869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
36879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	sg_init_table(buf->sg_list, req->nr_phys_segments);
36889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
36899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->payload_len = blk_rq_bytes(req);
36909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
36919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
36929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
36959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
36969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart *                   bsg request
36979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	SCSI Host corresponding to the bsg object
36989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	(optional) FC Remote Port corresponding to the bsg object
36999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @req:	BSG request that needs a job structure
37009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
37019e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
37029e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
37039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req)
37049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
37059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
37069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *rsp = req->next_rq;
37079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job;
37089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
37099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(req->special);
37119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
37139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			GFP_KERNEL);
37149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!job)
37159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
37169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/*
37189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * Note: this is a bit silly.
37199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * The request gets formatted as a SGIO v4 ioctl request, which
37209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * then gets reformatted as a blk request, which then gets
37219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * reformatted as a fc bsg request. And on completion, we have
37229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * to wrap return results such that SGIO v4 thinks it was a scsi
37239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * status.  I hope this was all worth it.
37249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 */
37259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	req->special = job;
37279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->shost = shost;
37289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->rport = rport;
37299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->req = req;
37309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (i->f->dd_bsg_size)
37319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dd_data = (void *)&job[1];
37329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_init(&job->job_lock);
37339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->request = (struct fc_bsg_request *)req->cmd;
37349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->request_len = req->cmd_len;
37359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply = req->sense;
37369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = SCSI_SENSE_BUFFERSIZE;	/* Size of sense buffer
37379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart						 * allocated */
37389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (req->bio) {
37399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_bsg_map_buffer(&job->request_payload, req);
37409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret)
37419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto failjob_rls_job;
37429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
37439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rsp && rsp->bio) {
37449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
37459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret)
37469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto failjob_rls_rqst_payload;
37479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
37489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->job_done = fc_bsg_jobdone;
37499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rport)
37509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dev = &rport->dev;
37519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
37529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dev = &shost->shost_gendev;
37539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	get_device(job->dev);		/* take a reference for the request */
37549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->ref_cnt = 1;
37569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
37589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37609e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfailjob_rls_rqst_payload:
37619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->request_payload.sg_list);
37629e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfailjob_rls_job:
37639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job);
37649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return -ENOMEM;
37659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
37669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37689e4f5e29610162fd426366f3b29e3cc6e575b858James Smartenum fc_dispatch_result {
37699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_BREAK,	/* on return, q is locked, break from q loop */
37709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_LOCKED,	/* on return, q is locked, continue on */
37719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_UNLOCKED,	/* on return, q is unlocked, continue on */
37729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart};
37739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
37769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
37775415907af1f5ef80c95147bacbd321b0d4236dd5Randy Dunlap * @q:		fc host request queue
37789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	scsi host rport attached to
37799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	bsg job to be processed
37809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
37819e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum fc_dispatch_result
37829e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
37839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct fc_bsg_job *job)
37849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
37859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
37869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
37879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
37889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* Validate the host command */
37909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	switch (job->request->msgcode) {
37919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_ADD_RPORT:
37929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_add_rport);
37939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
37949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_DEL_RPORT:
37969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_del_rport);
37979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
37989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_ELS_NOLOGIN:
38009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_els);
38019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be a xmt and rcv payloads */
38029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
38039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
38049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
38059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
38069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
38089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_CT:
38109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_ct);
38119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be xmt and rcv payloads */
38129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
38139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
38149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
38159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
38169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
38189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_VENDOR:
38209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_vendor);
38219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((shost->hostt->vendor_id == 0L) ||
38229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (job->request->rqst_data.h_vendor.vendor_id !=
38239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			shost->hostt->vendor_id)) {
38249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -ESRCH;
38259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
38269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
38289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	default:
38309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -EBADR;
38319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_host_msg;
38329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
38339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* check if we really have all the request data needed */
38359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->request_len < cmdlen) {
38369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -ENOMSG;
38379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_host_msg;
38389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
38399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	ret = i->f->bsg_request(job);
38419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!ret)
38429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return FC_DISPATCH_UNLOCKED;
38439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38449e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfail_host_msg:
38459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* return the errno failure code as the only status */
38469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(job->reply_len < sizeof(uint32_t));
384761ec33eb7d8e1e94a93a8243238dc5f9a183001aBrian King	job->reply->reply_payload_rcv_len = 0;
38489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply->result = ret;
38499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = sizeof(uint32_t);
38509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_jobdone(job);
38519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return FC_DISPATCH_UNLOCKED;
38529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
38539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/*
38569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_goose_queue - restart rport queue in case it was stopped
38579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport to be restarted
38589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
38599e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
38609e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_goose_queue(struct fc_rport *rport)
38619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
38629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!rport->rqst_q)
38639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
38649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
3865c21e6beba8835d09bb80e34961430b13e60381c5Jens Axboe	/*
3866c21e6beba8835d09bb80e34961430b13e60381c5Jens Axboe	 * This get/put dance makes no sense
3867c21e6beba8835d09bb80e34961430b13e60381c5Jens Axboe	 */
38689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	get_device(&rport->dev);
3869c21e6beba8835d09bb80e34961430b13e60381c5Jens Axboe	blk_run_queue_async(rport->rqst_q);
38709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(&rport->dev);
38719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
38729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
38749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
38755415907af1f5ef80c95147bacbd321b0d4236dd5Randy Dunlap * @q:		rport request queue
38769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	scsi host rport attached to
38779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport request destined to
38789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	bsg job to be processed
38799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
38809e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum fc_dispatch_result
38819e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
38829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct fc_rport *rport, struct fc_bsg_job *job)
38839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
38849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
38859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
38869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
38879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* Validate the rport command */
38899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	switch (job->request->msgcode) {
38909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_RPT_ELS:
38919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_rport_els);
38929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto check_bidi;
38939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_RPT_CT:
38959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_rport_ct);
38969e4f5e29610162fd426366f3b29e3cc6e575b858James Smartcheck_bidi:
38979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be xmt and rcv payloads */
38989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
38999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
39009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
39019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_rport_msg;
39029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
39039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
39049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	default:
39059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -EBADR;
39069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_rport_msg;
39079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* check if we really have all the request data needed */
39109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->request_len < cmdlen) {
39119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -ENOMSG;
39129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_rport_msg;
39139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	ret = i->f->bsg_request(job);
39169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!ret)
39179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return FC_DISPATCH_UNLOCKED;
39189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39199e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfail_rport_msg:
39209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* return the errno failure code as the only status */
39219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(job->reply_len < sizeof(uint32_t));
392261ec33eb7d8e1e94a93a8243238dc5f9a183001aBrian King	job->reply->reply_payload_rcv_len = 0;
39239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply->result = ret;
39249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = sizeof(uint32_t);
39259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_jobdone(job);
39269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return FC_DISPATCH_UNLOCKED;
39279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
39319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_request_handler - generic handler for bsg requests
39329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		request queue to manage
39339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	Scsi_Host related to the bsg object
39349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	FC remote port related to the bsg object (optional)
39359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @dev:	device structure for bsg object
39369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
39379e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
39389e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
39399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		       struct fc_rport *rport, struct device *dev)
39409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
39419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req;
39429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job;
39439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	enum fc_dispatch_result ret;
39449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!get_device(dev))
39469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
39479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39487eaceaccab5f40bbfda044629a6298616aeaed50Jens Axboe	while (1) {
39492bc1c59dbdefdb6f9767e06efb86bbdb2923a8beMike Christie		if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
39502bc1c59dbdefdb6f9767e06efb86bbdb2923a8beMike Christie		    !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
39512bc1c59dbdefdb6f9767e06efb86bbdb2923a8beMike Christie			break;
39529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		req = blk_fetch_request(q);
39549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (!req)
39559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			break;
39569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
39589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			req->errors = -ENXIO;
39599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_unlock_irq(q->queue_lock);
3960d88a714bfefa7aed7b9cb6c3721707fcd056b472Sarang Radke			blk_end_request_all(req, -ENXIO);
39619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
39629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
39639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
39649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		spin_unlock_irq(q->queue_lock);
39669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_req_to_bsgjob(shost, rport, req);
39689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret) {
39699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			req->errors = ret;
3970d88a714bfefa7aed7b9cb6c3721707fcd056b472Sarang Radke			blk_end_request_all(req, ret);
39719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
39729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
39739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
39749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job = req->special;
39769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* check if we have the msgcode value at least */
39789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (job->request_len < sizeof(uint32_t)) {
39799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			BUG_ON(job->reply_len < sizeof(uint32_t));
398061ec33eb7d8e1e94a93a8243238dc5f9a183001aBrian King			job->reply->reply_payload_rcv_len = 0;
39819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			job->reply->result = -ENOMSG;
39829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			job->reply_len = sizeof(uint32_t);
39839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			fc_bsg_jobdone(job);
39849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
39859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
39869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
39879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* the dispatch routines will unlock the queue_lock */
39899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (rport)
39909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = fc_bsg_rport_dispatch(q, shost, rport, job);
39919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		else
39929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = fc_bsg_host_dispatch(q, shost, job);
39939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* did dispatcher hit state that can't process any more */
39959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret == FC_DISPATCH_BREAK)
39969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			break;
39979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* did dispatcher had released the lock */
39999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret == FC_DISPATCH_UNLOCKED)
40009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
40019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
40029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irq(q->queue_lock);
40049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(dev);
40059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irq(q->queue_lock);
40069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
40079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
40109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_host_handler - handler for bsg requests for a fc host
40119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		fc host request queue
40129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
40139e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
40149e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_host_handler(struct request_queue *q)
40159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
40169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = q->queuedata;
40179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
40199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
40209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
40239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rport_handler - handler for bsg requests for a fc rport
40249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		rport request queue
40259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
40269e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
40279e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rport_handler(struct request_queue *q)
40289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
40299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_rport *rport = q->queuedata;
40309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = rport_to_shost(rport);
40319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_request_handler(q, shost, rport, &rport->dev);
40339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
40349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
40379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
40389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	shost for fc_host
40399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @fc_host:	fc_host adding the structures to
40409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
40419e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
40429e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
40439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
40449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct device *dev = &shost->shost_gendev;
40459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
40469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request_queue *q;
40479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
40483c559ea8fd003962d9a28c64b2dd5c6d83ca6edbJames Bottomley	char bsg_name[20];
40499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_host->rqst_q = NULL;
40519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!i->f->bsg_request)
40539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOTSUPP;
40549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	snprintf(bsg_name, sizeof(bsg_name),
40569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		 "fc_host%d", shost->host_no);
40579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
40599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!q) {
40609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "fc_host%d: bsg interface failed to "
40619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - no request queue\n",
40629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 shost->host_no);
40639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
40649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
40659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q->queuedata = shost;
40679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
4068b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_queue_softirq_done(q, fc_bsg_softirq_done);
40699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
40709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
40719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = bsg_register_queue(q, dev, bsg_name, NULL);
40739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err) {
40749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "fc_host%d: bsg interface failed to "
40759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - register queue\n",
40769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				shost->host_no);
40779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
40789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return err;
40799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
40809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_host->rqst_q = q;
40829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
40839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
40849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
40879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
40889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	shost that rport is attached to
40899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport that the bsg hooks are being attached to
40909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
40919e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
40929e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
40939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
40949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct device *dev = &rport->dev;
40959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
40969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request_queue *q;
40979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
40989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	rport->rqst_q = NULL;
41009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
41019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!i->f->bsg_request)
41029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOTSUPP;
41039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
41049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
41059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!q) {
41069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "%s: bsg interface failed to "
41079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - no request queue\n",
41089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 dev->kobj.name);
41099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
41109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
41119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
41129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q->queuedata = rport;
41139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
4114b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_queue_softirq_done(q, fc_bsg_softirq_done);
41159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
41169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
41179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
41189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = bsg_register_queue(q, dev, NULL, NULL);
41199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err) {
41209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "%s: bsg interface failed to "
41219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - register queue\n",
41229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 dev->kobj.name);
41239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
41249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return err;
41259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
41269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
41279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	rport->rqst_q = q;
41289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
41299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
41309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
41319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
41329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
41339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
41349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:	the request_queue that is to be torn down.
413578d16341facf829a71b6f7c68ec5511b9c168060James Smart *
413678d16341facf829a71b6f7c68ec5511b9c168060James Smart * Notes:
413778d16341facf829a71b6f7c68ec5511b9c168060James Smart *   Before unregistering the queue empty any requests that are blocked
413878d16341facf829a71b6f7c68ec5511b9c168060James Smart *
413978d16341facf829a71b6f7c68ec5511b9c168060James Smart *
41409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
41419e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
41429e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_remove(struct request_queue *q)
41439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
41449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (q) {
41459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		bsg_unregister_queue(q);
41469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
41479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
41489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
41499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
41509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
41519ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/* Original Author:  Martin Hicks */
41529ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames SmartMODULE_AUTHOR("James Smart");
41531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("FC Transport Attributes");
41541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
41551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
41561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(fc_transport_init);
41571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(fc_transport_exit);
4158