scsi_transport_fc.c revision 5415907af1f5ef80c95147bacbd321b0d4236dd5
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>
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_device.h>
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_host.h>
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport.h>
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport_fc.h>
34c829c394165f981d49f05a9be228404d7a9398d4James Smart#include <scsi/scsi_cmnd.h>
3584314fd4740ad73550c76dee4a9578979d84af48James Smart#include <linux/netlink.h>
3684314fd4740ad73550c76dee4a9578979d84af48James Smart#include <net/netlink.h>
3784314fd4740ad73550c76dee4a9578979d84af48James Smart#include <scsi/scsi_netlink_fc.h>
389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart#include <scsi/scsi_bsg_fc.h>
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "scsi_priv.h"
407525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori#include "scsi_transport_fc_internal.h"
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
42aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int fc_queue_work(struct Scsi_Host *, struct work_struct *);
439ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartstatic void fc_vport_sched_delete(struct work_struct *work);
44a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezstatic int fc_vport_setup(struct Scsi_Host *shost, int channel,
45a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *pdev, struct fc_vport_identifiers  *ids,
46a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport **vport);
479e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
489e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
499e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void fc_bsg_remove(struct request_queue *);
509e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void fc_bsg_goose_queue(struct fc_rport *);
51a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
52a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Redefine so that we can have same named attributes in the
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sdev/starget/host objects.
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
56ee959b00c335d7780136c5abda37809191fe52c3Tony Jones#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)		\
57ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstruct device_attribute device_attr_##_prefix##_##_name = 	\
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__ATTR(_name,_mode,_show,_store)
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_search(title, table_type, table)			\
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char *get_fc_##title##_name(enum table_type table_key)	\
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *name = NULL;						\
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
666391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value == table_key) {			\
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			name = table[i].name;				\
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;						\
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return name;							\
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_match(title, table_type, table)			\
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int get_fc_##title##_match(const char *table_key,		\
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		enum table_type *value)					\
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
816391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (strncmp(table_key, table[i].name,			\
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				table[i].matchlen) == 0) {		\
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*value = table[i].value;			\
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0; /* success */				\
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 1; /* failure */						\
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_type values to ascii string name */
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_type	value;
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_type_names[] = {
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_UNKNOWN,		"Unknown" },
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_OTHER,		"Other" },
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NOTPRESENT,	"Not Present" },
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NPORT,	"NPort (fabric via point-to-point)" },
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NLPORT,	"NLPort (fabric via loop)" },
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_LPORT,	"LPort (private loop)" },
103951948a397e3ddc96658efd648b9d66622965b19Christof Schmitt	{ FC_PORTTYPE_PTP,	"Point-To-Point (direct nport connection)" },
104a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORTTYPE_NPIV,		"NPIV VPORT" },
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTTYPE_MAX_NAMELEN		50
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
109a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Reuse fc_port_type enum function for vport_type */
110a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define get_fc_vport_type_name get_fc_port_type_name
111a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11384314fd4740ad73550c76dee4a9578979d84af48James Smart/* Convert fc_host_event_code values to ascii string name */
11484314fd4740ad73550c76dee4a9578979d84af48James Smartstatic const struct {
11584314fd4740ad73550c76dee4a9578979d84af48James Smart	enum fc_host_event_code		value;
11684314fd4740ad73550c76dee4a9578979d84af48James Smart	char				*name;
11784314fd4740ad73550c76dee4a9578979d84af48James Smart} fc_host_event_code_names[] = {
11884314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LIP,			"lip" },
11984314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINKUP,		"link_up" },
12084314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINKDOWN,		"link_down" },
12184314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LIPRESET,		"lip_reset" },
12284314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_RSCN,			"rscn" },
12384314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_ADAPTER_CHANGE,	"adapter_chg" },
12484314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_UNKNOWN,		"port_unknown" },
12584314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_ONLINE,		"port_online" },
12684314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_OFFLINE,		"port_offline" },
12784314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_FABRIC,		"port_fabric" },
12884314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINK_UNKNOWN,		"link_unknown" },
12984314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_VENDOR_UNIQUE,	"vendor_unique" },
13084314fd4740ad73550c76dee4a9578979d84af48James Smart};
13184314fd4740ad73550c76dee4a9578979d84af48James Smartfc_enum_name_search(host_event_code, fc_host_event_code,
13284314fd4740ad73550c76dee4a9578979d84af48James Smart		fc_host_event_code_names)
13384314fd4740ad73550c76dee4a9578979d84af48James Smart#define FC_HOST_EVENT_CODE_MAX_NAMELEN	30
13484314fd4740ad73550c76dee4a9578979d84af48James Smart
13584314fd4740ad73550c76dee4a9578979d84af48James Smart
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_state values to ascii string name */
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_state	value;
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_state_names[] = {
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_UNKNOWN,		"Unknown" },
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_NOTPRESENT,	"Not Present" },
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ONLINE,		"Online" },
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_OFFLINE,		"Offline" },
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BLOCKED,		"Blocked" },
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BYPASSED,	"Bypassed" },
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_DIAGNOSTICS,	"Diagnostics" },
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LINKDOWN,	"Linkdown" },
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ERROR,		"Error" },
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LOOPBACK,	"Loopback" },
15142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	{ FC_PORTSTATE_DELETED,		"Deleted" },
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTSTATE_MAX_NAMELEN	20
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
157a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Convert fc_vport_state values to ascii string name */
158a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic struct {
159a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	enum fc_vport_state	value;
160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char			*name;
161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart} fc_vport_state_names[] = {
162a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_UNKNOWN,		"Unknown" },
163a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_ACTIVE,		"Active" },
164a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_DISABLED,		"Disabled" },
165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_LINKDOWN,		"Linkdown" },
166a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_INITIALIZING,	"Initializing" },
167a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_NO_FABRIC_SUPP,	"No Fabric Support" },
168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_NO_FABRIC_RSCS,	"No Fabric Resources" },
169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FABRIC_LOGOUT,	"Fabric Logout" },
170a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FABRIC_REJ_WWN,	"Fabric Rejected WWN" },
171a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FAILED,		"VPort Failed" },
172a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart};
173a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define FC_VPORTSTATE_MAX_NAMELEN	24
175a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
176a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Reuse fc_vport_state enum function for vport_last_state */
177a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define get_fc_vport_last_state_name get_fc_vport_state_name
178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_tgtid_binding_type values to ascii string name */
1810ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_tgtid_binding_type	value;
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char				*name;
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int				matchlen;
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_tgtid_binding_type_names[] = {
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_NONE, "none", 4 },
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_BINDTYPE_MAX_NAMELEN	30
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_bitfield_name_search(title, table)			\
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t							\
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsget_fc_##title##_names(u32 table_key, char *buf)		\
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{								\
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *prefix = "";					\
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t len = 0;					\
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;							\
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds								\
2066391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value & table_key) {		\
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			len += sprintf(buf + len, "%s%s",	\
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				prefix, table[i].name);		\
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prefix = ", ";				\
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}						\
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}							\
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");			\
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;						\
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_COS bit values to ascii string name */
2190ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_cos_names[] = {
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS1,	"Class 1" },
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS2,	"Class 2" },
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS3,	"Class 3" },
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS4,	"Class 4" },
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS6,	"Class 6" },
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(cos, fc_cos_names)
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_PORTSPEED bit values to ascii string name */
2330ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_speed_names[] = {
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_1GBIT,		"1 Gbit" },
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_2GBIT,		"2 Gbit" },
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_4GBIT,		"4 Gbit" },
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_10GBIT,		"10 Gbit" },
241c3d2350a8420dbf9d48f5f8a0fb72117bfcbc1b0James Smart	{ FC_PORTSPEED_8GBIT,		"8 Gbit" },
242c3d2350a8420dbf9d48f5f8a0fb72117bfcbc1b0James Smart	{ FC_PORTSPEED_16GBIT,		"16 Gbit" },
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_NOT_NEGOTIATED,	"Not Negotiated" },
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(port_speed, fc_port_speed_names)
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_fc4s (char *buf, u8 *fc4_list)
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i, len=0;
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		len += sprintf(buf + len , "0x%02x ", *fc4_list);
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
260a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Convert FC_PORT_ROLE bit values to ascii string name */
2610ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
264a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart} fc_port_role_names[] = {
265a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_FCP_TARGET,	"FCP Target" },
266a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_FCP_INITIATOR,	"FCP Initiator" },
267a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_IP_PORT,		"IP Port" },
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
269a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_bitfield_name_search(port_roles, fc_port_role_names)
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_PORTID_MASK	0xfffff0
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_ROLE_MASK  	0x00000f
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FPORT_PORTID			0x00000e
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FABCTLR_PORTID		0x00000d
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_DIRSRVR_PORTID		0x00000c
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_TIMESRVR_PORTID		0x00000b
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_MGMTSRVR_PORTID		0x00000a
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
283c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_timeout_deleted_rport(struct work_struct *work);
284c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_timeout_fail_rport_io(struct work_struct *work);
285c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_scsi_scan_rport(struct work_struct *work);
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Attribute counts pre object type...
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Increase these values if you add attributes
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_STARGET_NUM_ATTRS 	3
2920f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart#define FC_RPORT_NUM_ATTRS	10
293a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define FC_VPORT_NUM_ATTRS	9
2944643682ba50634db5cd61292711d031630baf233James Smart#define FC_HOST_NUM_ATTRS	22
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_internal {
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_transport_template t;
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_function_template *f;
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * For attributes : each object has :
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of the actual attributes structures
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of null-terminated pointers to the attribute
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *     structures - used for mid-layer interaction.
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * The attribute containers for the starget and host are are
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * part of the midlayer. As the remote port is specific to the
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * fc transport, we must provide the attribute container.
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
310ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_starget_attrs[
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds							FC_STARGET_NUM_ATTRS];
312ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
314ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
315ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct transport_container rport_attr_cont;
318ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
319ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
320a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
321a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct transport_container vport_attr_cont;
322ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
323ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define to_fc_internal(tmpl)	container_of(tmpl, struct fc_internal, t)
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
328d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_target_setup(struct transport_container *tc, struct device *dev,
329ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   struct device *cdev)
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_target *starget = to_scsi_target(dev);
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * if parent is remote port, use values from remote port.
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Otherwise, this host uses the fc_transport, but not the
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * remote port interface. As such, initialize to known non-values.
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport) {
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = rport->node_name;
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = rport->port_name;
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = rport->port_id;
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = -1;
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = -1;
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = -1;
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_transport_class,
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_transport",
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_target_setup,
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
358d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_host_setup(struct transport_container *tc, struct device *dev,
359ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			 struct device *cdev)
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(dev);
362aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3649ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	/*
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set default values easily detected by the midlayer as
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * failure cases.  The scsi lldd is responsible for initializing
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * all transport attributes to valid values per host.
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
369aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->node_name = -1;
370aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_name = -1;
371aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->permanent_port_name = -1;
372aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->supported_classes = FC_COS_UNSPECIFIED;
373aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->supported_fc4s, 0,
374aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->supported_fc4s));
375aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
376aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->maxframe_size = -1;
377a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->max_npiv_vports = 0;
378aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->serial_number, 0,
379aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->serial_number));
380aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
381aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_id = -1;
382aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_type = FC_PORTTYPE_UNKNOWN;
383aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_state = FC_PORTSTATE_UNKNOWN;
384aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->active_fc4s, 0,
385aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->active_fc4s));
386aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->speed = FC_PORTSPEED_UNKNOWN;
387aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->fabric_name = -1;
388b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
389b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
390aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
391aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
392aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
393aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	INIT_LIST_HEAD(&fc_host->rports);
394aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	INIT_LIST_HEAD(&fc_host->rport_bindings);
395a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	INIT_LIST_HEAD(&fc_host->vports);
396aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->next_rport_number = 0;
397aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->next_target_id = 0;
398a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->next_vport_number = 0;
399a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse = 0;
400aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
401aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers	snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
402aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 "fc_wq_%d", shost->host_no);
403aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->work_q = create_singlethread_workqueue(
404aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_host->work_q_name);
405aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host->work_q)
406aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -ENOMEM;
407aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
408aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers	snprintf(fc_host->devloss_work_q_name,
409aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 sizeof(fc_host->devloss_work_q_name),
410aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 "fc_dl_%d", shost->host_no);
411aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->devloss_work_q = create_singlethread_workqueue(
412aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_host->devloss_work_q_name);
413aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host->devloss_work_q) {
414aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(fc_host->work_q);
415aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->work_q = NULL;
416aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -ENOMEM;
417aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
418aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
4199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_hostadd(shost, fc_host);
4209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* ignore any bsg add error - we just can't do sgio */
4219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
4239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
4249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4259e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_host_remove(struct transport_container *tc, struct device *dev,
4269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct device *cdev)
4279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
4289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = dev_to_shost(dev);
4299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
4309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_remove(fc_host->rqst_q);
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_host_class,
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_host",
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_host_setup,
4389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			       fc_host_remove,
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Setup and Remove actions for remote ports are handled
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in the service functions below.
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_rport_class,
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_remote_ports",
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
452a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Setup and Remove actions for virtual ports are handled
453a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * in the service functions below.
454a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
455a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic DECLARE_TRANSPORT_CLASS(fc_vport_class,
456a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       "fc_vports",
457a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL,
458a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL,
459a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL);
460a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
461a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Module Parameters
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * dev_loss_tmo: the default number of seconds that the FC transport
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   should insulate the loss of a remote port.
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4701c9e16e47a22c61d99aabb1c154e5106ddbf3575James Smartstatic unsigned int fc_dev_loss_tmo = 60;		/* seconds */
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
47210f4b89a0f0586a67f57efb2f5d12fffbb371490Masatake YAMATOmodule_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_PARM_DESC(dev_loss_tmo,
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 "Maximum number of seconds that the FC transport should"
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " insulate the loss of a remote port. Once this value is"
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " exceeded, the scsi target is removed. Value should be"
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
479eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley/*
48084314fd4740ad73550c76dee4a9578979d84af48James Smart * Netlink Infrastructure
481eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
48284314fd4740ad73550c76dee4a9578979d84af48James Smart
48384314fd4740ad73550c76dee4a9578979d84af48James Smartstatic atomic_t fc_event_seq;
48484314fd4740ad73550c76dee4a9578979d84af48James Smart
48584314fd4740ad73550c76dee4a9578979d84af48James Smart/**
48684314fd4740ad73550c76dee4a9578979d84af48James Smart * fc_get_event_number - Obtain the next sequential FC event number
48784314fd4740ad73550c76dee4a9578979d84af48James Smart *
48884314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
489eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *   We could have inlined this, but it would have required fc_event_seq to
49084314fd4740ad73550c76dee4a9578979d84af48James Smart *   be exposed. For now, live with the subroutine call.
49184314fd4740ad73550c76dee4a9578979d84af48James Smart *   Atomic used to avoid lock/unlock...
492eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
49384314fd4740ad73550c76dee4a9578979d84af48James Smartu32
49484314fd4740ad73550c76dee4a9578979d84af48James Smartfc_get_event_number(void)
49584314fd4740ad73550c76dee4a9578979d84af48James Smart{
49684314fd4740ad73550c76dee4a9578979d84af48James Smart	return atomic_add_return(1, &fc_event_seq);
49784314fd4740ad73550c76dee4a9578979d84af48James Smart}
49884314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_get_event_number);
49984314fd4740ad73550c76dee4a9578979d84af48James Smart
50084314fd4740ad73550c76dee4a9578979d84af48James Smart
50184314fd4740ad73550c76dee4a9578979d84af48James Smart/**
50284314fd4740ad73550c76dee4a9578979d84af48James Smart * fc_host_post_event - called to post an even on an fc_host.
50384314fd4740ad73550c76dee4a9578979d84af48James Smart * @shost:		host the event occurred on
50484314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_number:	fc event number obtained from get_fc_event_number()
50584314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_code:		fc_host event being posted
50684314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_data:		32bits of data for the event being posted
50784314fd4740ad73550c76dee4a9578979d84af48James Smart *
50884314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
50984314fd4740ad73550c76dee4a9578979d84af48James Smart *	This routine assumes no locks are held on entry.
510eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
51184314fd4740ad73550c76dee4a9578979d84af48James Smartvoid
51284314fd4740ad73550c76dee4a9578979d84af48James Smartfc_host_post_event(struct Scsi_Host *shost, u32 event_number,
51384314fd4740ad73550c76dee4a9578979d84af48James Smart		enum fc_host_event_code event_code, u32 event_data)
51484314fd4740ad73550c76dee4a9578979d84af48James Smart{
51584314fd4740ad73550c76dee4a9578979d84af48James Smart	struct sk_buff *skb;
51684314fd4740ad73550c76dee4a9578979d84af48James Smart	struct nlmsghdr	*nlh;
51784314fd4740ad73550c76dee4a9578979d84af48James Smart	struct fc_nl_event *event;
51884314fd4740ad73550c76dee4a9578979d84af48James Smart	const char *name;
51984314fd4740ad73550c76dee4a9578979d84af48James Smart	u32 len, skblen;
52084314fd4740ad73550c76dee4a9578979d84af48James Smart	int err;
52184314fd4740ad73550c76dee4a9578979d84af48James Smart
52284314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!scsi_nl_sock) {
52384314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOENT;
52484314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail;
52584314fd4740ad73550c76dee4a9578979d84af48James Smart	}
52684314fd4740ad73550c76dee4a9578979d84af48James Smart
52784314fd4740ad73550c76dee4a9578979d84af48James Smart	len = FC_NL_MSGALIGN(sizeof(*event));
52884314fd4740ad73550c76dee4a9578979d84af48James Smart	skblen = NLMSG_SPACE(len);
52984314fd4740ad73550c76dee4a9578979d84af48James Smart
53084314fd4740ad73550c76dee4a9578979d84af48James Smart	skb = alloc_skb(skblen, GFP_KERNEL);
53184314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!skb) {
53284314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
53384314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail;
53484314fd4740ad73550c76dee4a9578979d84af48James Smart	}
53584314fd4740ad73550c76dee4a9578979d84af48James Smart
53684314fd4740ad73550c76dee4a9578979d84af48James Smart	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
53784314fd4740ad73550c76dee4a9578979d84af48James Smart				skblen - sizeof(*nlh), 0);
53884314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!nlh) {
53984314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
54084314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail_skb;
54184314fd4740ad73550c76dee4a9578979d84af48James Smart	}
54284314fd4740ad73550c76dee4a9578979d84af48James Smart	event = NLMSG_DATA(nlh);
54384314fd4740ad73550c76dee4a9578979d84af48James Smart
54484314fd4740ad73550c76dee4a9578979d84af48James Smart	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
54584314fd4740ad73550c76dee4a9578979d84af48James Smart				FC_NL_ASYNC_EVENT, len);
54684314fd4740ad73550c76dee4a9578979d84af48James Smart	event->seconds = get_seconds();
54784314fd4740ad73550c76dee4a9578979d84af48James Smart	event->vendor_id = 0;
54884314fd4740ad73550c76dee4a9578979d84af48James Smart	event->host_no = shost->host_no;
54984314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_datalen = sizeof(u32);	/* bytes */
55084314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_num = event_number;
55184314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_code = event_code;
55284314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_data = event_data;
55384314fd4740ad73550c76dee4a9578979d84af48James Smart
554ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
555ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso			GFP_KERNEL);
55684314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
55784314fd4740ad73550c76dee4a9578979d84af48James Smart
55884314fd4740ad73550c76dee4a9578979d84af48James Smartsend_fail_skb:
55984314fd4740ad73550c76dee4a9578979d84af48James Smart	kfree_skb(skb);
56084314fd4740ad73550c76dee4a9578979d84af48James Smartsend_fail:
56184314fd4740ad73550c76dee4a9578979d84af48James Smart	name = get_fc_host_event_code_name(event_code);
56284314fd4740ad73550c76dee4a9578979d84af48James Smart	printk(KERN_WARNING
56384314fd4740ad73550c76dee4a9578979d84af48James Smart		"%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
564cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		__func__, shost->host_no,
56584314fd4740ad73550c76dee4a9578979d84af48James Smart		(name) ? name : "<unknown>", event_data, err);
56684314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
56784314fd4740ad73550c76dee4a9578979d84af48James Smart}
56884314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_host_post_event);
56984314fd4740ad73550c76dee4a9578979d84af48James Smart
57084314fd4740ad73550c76dee4a9578979d84af48James Smart
57184314fd4740ad73550c76dee4a9578979d84af48James Smart/**
572eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
57384314fd4740ad73550c76dee4a9578979d84af48James Smart * @shost:		host the event occurred on
57484314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_number:	fc event number obtained from get_fc_event_number()
57584314fd4740ad73550c76dee4a9578979d84af48James Smart * @data_len:		amount, in bytes, of vendor unique data
57684314fd4740ad73550c76dee4a9578979d84af48James Smart * @data_buf:		pointer to vendor unique data
577eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @vendor_id:          Vendor id
57884314fd4740ad73550c76dee4a9578979d84af48James Smart *
57984314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
58084314fd4740ad73550c76dee4a9578979d84af48James Smart *	This routine assumes no locks are held on entry.
581eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
58284314fd4740ad73550c76dee4a9578979d84af48James Smartvoid
58384314fd4740ad73550c76dee4a9578979d84af48James Smartfc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
584f14e2e29cdd07f80de6dec168dc2bb39de37eec3James Smart		u32 data_len, char * data_buf, u64 vendor_id)
58584314fd4740ad73550c76dee4a9578979d84af48James Smart{
58684314fd4740ad73550c76dee4a9578979d84af48James Smart	struct sk_buff *skb;
58784314fd4740ad73550c76dee4a9578979d84af48James Smart	struct nlmsghdr	*nlh;
58884314fd4740ad73550c76dee4a9578979d84af48James Smart	struct fc_nl_event *event;
58984314fd4740ad73550c76dee4a9578979d84af48James Smart	u32 len, skblen;
59084314fd4740ad73550c76dee4a9578979d84af48James Smart	int err;
59184314fd4740ad73550c76dee4a9578979d84af48James Smart
59284314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!scsi_nl_sock) {
59384314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOENT;
59484314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail;
59584314fd4740ad73550c76dee4a9578979d84af48James Smart	}
59684314fd4740ad73550c76dee4a9578979d84af48James Smart
59784314fd4740ad73550c76dee4a9578979d84af48James Smart	len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
59884314fd4740ad73550c76dee4a9578979d84af48James Smart	skblen = NLMSG_SPACE(len);
59984314fd4740ad73550c76dee4a9578979d84af48James Smart
60084314fd4740ad73550c76dee4a9578979d84af48James Smart	skb = alloc_skb(skblen, GFP_KERNEL);
60184314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!skb) {
60284314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
60384314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail;
60484314fd4740ad73550c76dee4a9578979d84af48James Smart	}
60584314fd4740ad73550c76dee4a9578979d84af48James Smart
60684314fd4740ad73550c76dee4a9578979d84af48James Smart	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
60784314fd4740ad73550c76dee4a9578979d84af48James Smart				skblen - sizeof(*nlh), 0);
60884314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!nlh) {
60984314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
61084314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail_skb;
61184314fd4740ad73550c76dee4a9578979d84af48James Smart	}
61284314fd4740ad73550c76dee4a9578979d84af48James Smart	event = NLMSG_DATA(nlh);
61384314fd4740ad73550c76dee4a9578979d84af48James Smart
61484314fd4740ad73550c76dee4a9578979d84af48James Smart	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
61584314fd4740ad73550c76dee4a9578979d84af48James Smart				FC_NL_ASYNC_EVENT, len);
61684314fd4740ad73550c76dee4a9578979d84af48James Smart	event->seconds = get_seconds();
61784314fd4740ad73550c76dee4a9578979d84af48James Smart	event->vendor_id = vendor_id;
61884314fd4740ad73550c76dee4a9578979d84af48James Smart	event->host_no = shost->host_no;
61984314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_datalen = data_len;	/* bytes */
62084314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_num = event_number;
62184314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_code = FCH_EVT_VENDOR_UNIQUE;
62284314fd4740ad73550c76dee4a9578979d84af48James Smart	memcpy(&event->event_data, data_buf, data_len);
62384314fd4740ad73550c76dee4a9578979d84af48James Smart
624ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
625ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso			GFP_KERNEL);
62684314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
62784314fd4740ad73550c76dee4a9578979d84af48James Smart
62884314fd4740ad73550c76dee4a9578979d84af48James Smartsend_vendor_fail_skb:
62984314fd4740ad73550c76dee4a9578979d84af48James Smart	kfree_skb(skb);
63084314fd4740ad73550c76dee4a9578979d84af48James Smartsend_vendor_fail:
63184314fd4740ad73550c76dee4a9578979d84af48James Smart	printk(KERN_WARNING
63284314fd4740ad73550c76dee4a9578979d84af48James Smart		"%s: Dropped Event : host %d vendor_unique - err %d\n",
633cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		__func__, shost->host_no, err);
63484314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
63584314fd4740ad73550c76dee4a9578979d84af48James Smart}
63684314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_host_post_vendor_event);
63784314fd4740ad73550c76dee4a9578979d84af48James Smart
63884314fd4740ad73550c76dee4a9578979d84af48James Smart
6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic __init int fc_transport_init(void)
6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
64284314fd4740ad73550c76dee4a9578979d84af48James Smart	int error;
64384314fd4740ad73550c76dee4a9578979d84af48James Smart
64484314fd4740ad73550c76dee4a9578979d84af48James Smart	atomic_set(&fc_event_seq, 0);
64584314fd4740ad73550c76dee4a9578979d84af48James Smart
64684314fd4740ad73550c76dee4a9578979d84af48James Smart	error = transport_class_register(&fc_host_class);
6471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
6481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return error;
649a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = transport_class_register(&fc_vport_class);
650a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error)
651a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return error;
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = transport_class_register(&fc_rport_class);
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return error;
6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return transport_class_register(&fc_transport_class);
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit fc_transport_exit(void)
6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_transport_class);
6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_rport_class);
6621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_host_class);
663a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_class_unregister(&fc_vport_class);
6641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
6671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC Remote Port Attribute Management
6681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_show_function(field, format_string, sz, cast)		\
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
672ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##field (struct device *dev, 				\
673ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
675ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
67819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((i->f->get_rport_##field) &&				\
67919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    !((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
68042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_DELETED) ||		\
68119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_NOTPRESENT)))		\
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_rport_##field(rport);				\
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_store_function(field)					\
6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
688ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_##field(struct device *dev,				\
689ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
690ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
693ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
6951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
6960f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;							\
69719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
69842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||		\
69919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))		\
70019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;						\
7010f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);				\
7020f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (*cp && (*cp != '\n'))					\
7030f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EINVAL;						\
7041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_rport_##field(rport, val);				\
7051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr(field, format_string, sz)			\
7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
710ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
7121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr_cast(field, format_string, sz, cast)		\
7141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, (cast))	\
715ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
7171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rw_attr(field, format_string, sz)			\
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
7201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_store_function(field)					\
721ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,		\
7221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##field,				\
7231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_rport_##field)
7241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_show_function(field, format_string, sz, cast)	\
7271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
728ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##field (struct device *dev, 				\
729ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
7301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
731ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr(field, format_string, sz)		\
7361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, )	\
737ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
7391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)	\
7411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, (cast)) \
742ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
7441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_enum_attr(title, maxlen)			\
7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
748ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##title (struct device *dev,				\
749ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
7501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
751ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
7531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(rport->title);			\
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
7551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
7561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
7571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
758ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, title, S_IRUGO,			\
7591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##title, NULL)
7601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RD(field)					\
763ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
7661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
7681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
7691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)				\
771ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
7731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
7741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
7761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RW(field)					\
778ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_rport_##field) {					\
7801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].attr.mode = S_IRUGO;	\
7811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].store = NULL;		\
7821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
7831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
7851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
7861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7870f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field)				\
7880f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{									\
789ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7900f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7910f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	count++;							\
7920f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
7930f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
7941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport Remote Port Attributes: */
7961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Remote Port Attributes */
7981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
8001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
802ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_supported_classes (struct device *dev,
803ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				 struct device_attribute *attr, char *buf)
8041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
805ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
8061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport->supported_classes == FC_COS_UNSPECIFIED)
8071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
8081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(rport->supported_classes, buf);
8091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
810ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
8111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_supported_classes, NULL);
8121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Remote Port Attributes */
8141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
81519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com/*
81619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * dev_loss_tmo attribute
81719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com */
81819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comfc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
81919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic ssize_t
820ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
821ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			    const char *buf, size_t count)
82219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
82319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	int val;
824ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
82519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
82619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *i = to_fc_internal(shost->transportt);
8270f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;
82819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
82942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||
83019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
83119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;
8320f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);
8330f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((*cp && (*cp != '\n')) ||
8340f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
83519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EINVAL;
83619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	i->f->set_rport_dev_loss_tmo(rport, val);
83719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	return count;
83819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
839ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
84019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
8411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Remote Port Attributes */
8441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
8461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
8471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
8481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
850ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_roles (struct device *dev, struct device_attribute *attr,
851ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		     char *buf)
8521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
853ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
8541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* identify any roles that are port_id specific */
8561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((rport->port_id != -1) &&
8571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
8581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					FC_WELLKNOWN_PORTID_MASK) {
8591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
8601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FPORT_PORTID:
8611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Port\n");
8621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FABCTLR_PORTID:
8631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Controller\n");
8641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_DIRSRVR_PORTID:
8651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Directory Server\n");
8661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_TIMESRVR_PORTID:
8671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Time Server\n");
8681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_MGMTSRVR_PORTID:
8691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Management Server\n");
8701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
8711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Unknown Fabric Entity\n");
8721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
8731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
874a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (rport->roles == FC_PORT_ROLE_UNKNOWN)
8751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 20, "unknown\n");
876a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return get_fc_port_roles_names(rport->roles, buf);
8771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
879ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, roles, S_IRUGO,
8801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_roles, NULL);
8811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
8831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
8841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8850f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart/*
8860f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * fast_io_fail_tmo attribute
8870f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart */
8880f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic ssize_t
889ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_fast_io_fail_tmo (struct device *dev,
890ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				struct device_attribute *attr, char *buf)
8910f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
892ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
8930f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
8940f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->fast_io_fail_tmo == -1)
8950f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return snprintf(buf, 5, "off\n");
8960f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
8970f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
8980f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
8990f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic ssize_t
900ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_fast_io_fail_tmo(struct device *dev,
901ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				struct device_attribute *attr, const char *buf,
902ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				size_t count)
9030f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
9040f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	int val;
9050f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;
906ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
9070f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
9080f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
9090f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (rport->port_state == FC_PORTSTATE_DELETED) ||
9100f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
9110f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EBUSY;
9120f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (strncmp(buf, "off", 3) == 0)
9130f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		rport->fast_io_fail_tmo = -1;
9140f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	else {
9150f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		val = simple_strtoul(buf, &cp, 0);
9160f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		if ((*cp && (*cp != '\n')) ||
9170f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		    (val < 0) || (val >= rport->dev_loss_tmo))
9180f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart			return -EINVAL;
9190f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		rport->fast_io_fail_tmo = val;
9200f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	}
9210f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	return count;
9220f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
923ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
9240f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
9251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
9281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC SCSI Target Attribute Management
9291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
9301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
9321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Note: in the target show function we recognize when the remote
933a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *  port is in the heirarchy and do not allow the driver to get
9341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  involved in sysfs functions. The driver only gets involved if
9351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  it's the "old" style that doesn't use rports.
9361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
9371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_show_function(field, format_string, sz, cast)	\
9381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
939ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_starget_##field (struct device *dev, 				\
940ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			 struct device_attribute *attr, char *buf)	\
9411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
942ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct scsi_target *starget = transport_class_to_starget(dev);	\
9431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);	\
9441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
9451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);		\
9461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport)							\
9471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_##field(starget) = rport->field;		\
9481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (i->f->get_starget_##field)				\
9491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_starget_##field(starget);			\
9501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, 			\
9511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cast fc_starget_##field(starget)); 			\
9521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr(field, format_string, sz)			\
9551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, )		\
956ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
9571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_starget_##field, NULL)
9581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr_cast(field, format_string, sz, cast)		\
9601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, (cast))	\
961ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
9621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_starget_##field, NULL)
9631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RD(field)				\
965ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_starget_attrs[count] = device_attr_starget_##field; \
9661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].attr.mode = S_IRUGO;		\
9671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].store = NULL;			\
9681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
9691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
9701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
9711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RW(field)				\
973ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_starget_attrs[count] = device_attr_starget_##field; \
9741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_starget_##field) {				\
9751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].attr.mode = S_IRUGO;	\
9761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].store = NULL;		\
9771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
9781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
9791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
9801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
9811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport SCSI Target Attributes: */
9831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
9841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
9851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr(port_id, "0x%06x\n", 20);
9861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
989a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * FC Virtual Port Attribute Management
990a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
991a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
992a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_show_function(field, format_string, sz, cast)		\
993a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
994ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##field (struct device *dev, 				\
995ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
996a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
997ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
998a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
999a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1000a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((i->f->get_vport_##field) &&				\
1001a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)))	\
1002a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->f->get_vport_##field(vport);				\
1003a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, sz, format_string, cast vport->field); 	\
1004a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1005a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1006a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_store_function(field)					\
1007a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1008ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1009ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1010ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1011a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1012a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int val;							\
1013ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1014a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1015a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1016a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char *cp;							\
1017a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))	\
1018a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;						\
1019a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	val = simple_strtoul(buf, &cp, 0);				\
1020a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*cp && (*cp != '\n'))					\
1021a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1022a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->f->set_vport_##field(vport, val);				\
1023a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1024a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1025a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1026a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_store_str_function(field, slen)			\
1027a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1028ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1029ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, 			\
1030ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1031a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1032ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1033a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1034a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1035a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;						\
1036a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart									\
1037a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */			\
1038a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')						\
1039a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;							\
1040a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (cnt > ((slen) - 1))						\
1041a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1042a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memcpy(vport->field, buf, cnt);					\
1043a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->f->set_vport_##field(vport);					\
1044a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1045a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1046a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1047a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rd_attr(field, format_string, sz)			\
1048a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, )		\
1049ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1050a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			 show_fc_vport_##field, NULL)
1051a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1052a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rd_attr_cast(field, format_string, sz, cast)		\
1053a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, (cast))	\
1054ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1055a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			  show_fc_vport_##field, NULL)
1056a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1057a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rw_attr(field, format_string, sz)			\
1058a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, )		\
1059a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_store_function(field)					\
1060ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1061a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##field,				\
1062a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_vport_##field)
1063a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1064a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_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	return snprintf(buf, sz, format_string, cast vport->field); 	\
1071a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1072a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1073a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_store_u32_function(field)			\
1074a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1075ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1076ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1077ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1078a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1079a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u32 val;							\
1080ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1081a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char *cp;							\
1082a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))		\
1083a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;						\
1084a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	val = simple_strtoul(buf, &cp, 0);				\
1085a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*cp && (*cp != '\n'))					\
1086a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1087a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->field = val;						\
1088a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1089a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1090a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1091a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1092a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_attr(field, format_string, sz)		\
1093a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, )	\
1094ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1095a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			 show_fc_vport_##field, NULL)
1096a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1097a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast)	\
1098a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, (cast)) \
1099ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1100a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			  show_fc_vport_##field, NULL)
1101a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1102a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rw_u32_attr(field, format_string, sz)		\
1103a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, )	\
1104a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_store_u32_function(field)			\
1105ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1106a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##field,				\
1107a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_vport_##field)
1108a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1109a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1110a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_enum_attr(title, maxlen)			\
1111a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1112ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##title (struct device *dev,				\
1113ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1114ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       char *buf)					\
1115a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1116ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1117a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	const char *name;						\
1118a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	name = get_fc_##title##_name(vport->title);			\
1119a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!name)							\
1120a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1121a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, maxlen, "%s\n", name);			\
1122a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}									\
1123ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, title, S_IRUGO,			\
1124a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##title, NULL)
1125a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1126a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1127a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_RD(field)					\
1128ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1129a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1130a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].store = NULL;			\
1131a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1132a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->get_##field)						\
1133a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		count++
1134a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: checks function not show bit */
1135a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1136a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field)				\
1137ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1138a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1139a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].store = NULL;			\
1140a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1141a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1142a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1143a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_WR(field)					\
1144ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1145a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1146a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->field)						\
1147a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		count++
1148a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: checks function */
1149a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1150a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_RW(field)					\
1151ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1152a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!i->f->set_vport_##field) {					\
1153a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->private_vport_attrs[count].attr.mode = S_IRUGO;	\
1154a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->private_vport_attrs[count].store = NULL;		\
1155a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}								\
1156a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1157a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1158a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: does not check show bit */
1159a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field)				\
1161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1162ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1163a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1164a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++;							\
1165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1166a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1167a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* The FC Transport Virtual Port Attributes: */
1169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1170a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Fixed Virtual Port Attributes */
1171a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1172a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Dynamic Virtual Port Attributes */
1173a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Private Virtual Port Attributes */
1175a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1176a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1177a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1180a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1181a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1182ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1183ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		     char *buf)
1184a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1185ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
1186a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1187a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1188a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return snprintf(buf, 20, "unknown\n");
1189a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return get_fc_port_roles_names(vport->roles, buf);
1190a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1191ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1192a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1193a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1194a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1195a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_show_function(symbolic_name, "%s\n",
1196a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1197a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1198ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1199a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1200a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1201a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1202ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1203ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf, size_t count)
1204a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1205ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
12069ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	struct Scsi_Host *shost = vport_to_shost(vport);
1207a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
12089ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	fc_queue_work(shost, &vport->vport_delete_work);
1209a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;
1210a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1211ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1212a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			NULL, store_fc_vport_delete);
1213a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1214a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1215a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1216a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Enable/Disable vport
1217a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *  Write "1" to disable, write "0" to enable
1218a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1219a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1220ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1221ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,
1222a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			   size_t count)
1223a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1224ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
1225a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);
1226a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
1227a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
1228a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1229a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1230a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;
1231a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1232a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*buf == '0') {
1233a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (vport->vport_state != FC_VPORT_DISABLED)
1234a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EALREADY;
1235a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	} else if (*buf == '1') {
1236a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (vport->vport_state == FC_VPORT_DISABLED)
1237a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EALREADY;
1238a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	} else
1239a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1240a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1241a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1242a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1243a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1244ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1245a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			NULL, store_fc_vport_disable);
1246a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1247a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1248a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
12491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Attribute Management
12501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
12511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_show_function(field, format_string, sz, cast)		\
12531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1254ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##field (struct device *dev,				\
1255ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
12561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1257ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
12581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
12591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##field)					\
12601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##field(shost);				\
12611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
12621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_store_function(field)					\
12651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1266ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_##field(struct device *dev, 				\
1267ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr,			\
1268ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf,	size_t count)			\
12691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
12701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
1271ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
12721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
12730f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;							\
12741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
12750f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);				\
12760f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (*cp && (*cp != '\n'))					\
12770f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EINVAL;						\
12781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_host_##field(shost, val);				\
12791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
12801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1282b8d08210126a7b769b857720a59721a453a57a1eJames Smart#define fc_host_store_str_function(field, slen)				\
1283b8d08210126a7b769b857720a59721a453a57a1eJames Smartstatic ssize_t								\
1284ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_##field(struct device *dev,				\
1285ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr,			\
1286ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf, size_t count)			\
1287b8d08210126a7b769b857720a59721a453a57a1eJames Smart{									\
1288ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1289b8d08210126a7b769b857720a59721a453a57a1eJames Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1290b8d08210126a7b769b857720a59721a453a57a1eJames Smart	unsigned int cnt=count;						\
1291b8d08210126a7b769b857720a59721a453a57a1eJames Smart									\
1292b8d08210126a7b769b857720a59721a453a57a1eJames Smart	/* count may include a LF at end of string */			\
1293b8d08210126a7b769b857720a59721a453a57a1eJames Smart	if (buf[cnt-1] == '\n')						\
1294b8d08210126a7b769b857720a59721a453a57a1eJames Smart		cnt--;							\
1295b8d08210126a7b769b857720a59721a453a57a1eJames Smart	if (cnt > ((slen) - 1))						\
1296b8d08210126a7b769b857720a59721a453a57a1eJames Smart		return -EINVAL;						\
1297b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memcpy(fc_host_##field(shost), buf, cnt);			\
1298b8d08210126a7b769b857720a59721a453a57a1eJames Smart	i->f->set_host_##field(shost);					\
1299b8d08210126a7b769b857720a59721a453a57a1eJames Smart	return count;							\
1300b8d08210126a7b769b857720a59721a453a57a1eJames Smart}
1301b8d08210126a7b769b857720a59721a453a57a1eJames Smart
13021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr(field, format_string, sz)			\
13031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
1304ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
13061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr_cast(field, format_string, sz, cast)		\
13081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, (cast))		\
1309ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
13111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rw_attr(field, format_string, sz)			\
13131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
13141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_store_function(field)					\
1315ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
13161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_host_##field,				\
13171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_host_##field)
13181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_enum_attr(title, maxlen)				\
13201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1321ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##title (struct device *dev,				\
1322ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
13231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1324ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
13251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
13261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
13271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##title)					\
13281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##title(shost);				\
13291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(fc_host_##title(shost));		\
13301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
13311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
13321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
13331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
1334ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
13351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RD(field)					\
1337ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
13391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
13401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
13411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
13421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
13431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1344a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_HOST_ATTRIBUTE_RD_NS(field)				\
1345ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
1346a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1347a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_host_attrs[count].store = NULL;			\
1348a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->host_attrs[count] = &i->private_host_attrs[count];		\
1349a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1350a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
13511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RW(field)					\
1352ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_host_##field) {					\
13541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].attr.mode = S_IRUGO;	\
13551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].store = NULL;		\
13561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
13571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
13581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
13601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_show_function(field, format_string, sz, cast)	\
13631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1364ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##field (struct device *dev,				\
1365ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
13661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1367ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
13681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
13691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr(field, format_string, sz)		\
13721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, )	\
1373ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
13751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr_cast(field, format_string, sz, cast)	\
13771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, (cast)) \
1378ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
13801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
1382ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
13841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
13851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
13861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
13871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)			\
138991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{									\
1390ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
139291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	count++;							\
139391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
13941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Host Attributes */
13971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1399ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_classes (struct device *dev,
1400ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			        struct device_attribute *attr, char *buf)
14011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1402ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
14051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
14061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
14081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1409ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
14101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_classes, NULL);
14111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1413ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_fc4s (struct device *dev,
1414ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			     struct device_attribute *attr, char *buf)
14151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1416ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
14181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1419ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
14201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_fc4s, NULL);
14211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1423ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_speeds (struct device *dev,
1424ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			       struct device_attribute *attr, char *buf)
14251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1426ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
14291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
14301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
14321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1433ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
14341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_speeds, NULL);
14351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
14381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
14396b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmannfc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
14406b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann			     unsigned long long);
14411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1442a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
14431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
14441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Host Attributes */
14471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1449ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_active_fc4s (struct device *dev,
1450ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			  struct device_attribute *attr, char *buf)
14511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1452ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
14541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_active_fc4s)
14561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_active_fc4s(shost);
14571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
14591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1460ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
14611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_active_fc4s, NULL);
14621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1464ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_speed (struct device *dev,
1465ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		    struct device_attribute *attr, char *buf)
14661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1467ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
14691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_speed)
14711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_speed(shost);
14721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
14741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
14751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_speed(shost), buf);
14771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1478ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, speed, S_IRUGO,
14791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_speed, NULL);
14801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr(port_id, "0x%06x\n", 20);
14831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
14841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
14851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1486016131b8fffa1085b4ad165ab228116fdc278ebeJames Smartfc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
14871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1488b8d08210126a7b769b857720a59721a453a57a1eJames Smartfc_private_host_show_function(system_hostname, "%s\n",
1489b8d08210126a7b769b857720a59721a453a57a1eJames Smart		FC_SYMBOLIC_NAME_SIZE + 1, )
1490b8d08210126a7b769b857720a59721a453a57a1eJames Smartfc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1491ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1492b8d08210126a7b769b857720a59721a453a57a1eJames Smart		show_fc_host_system_hostname, store_fc_host_system_hostname);
1493b8d08210126a7b769b857720a59721a453a57a1eJames Smart
14941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Host Attributes */
14961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1498ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_private_host_tgtid_bind_type(struct device *dev,
1499ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				     struct device_attribute *attr, char *buf)
15001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1501ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;
15031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
15051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)
15061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
15071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
15081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1510d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com#define get_list_head_entry(pos, head, member) 		\
1511d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	pos = list_entry((head)->next, typeof(*pos), member)
1512d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com
15131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1514ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_private_host_tgtid_bind_type(struct device *dev,
1515ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *attr, const char *buf, size_t count)
15161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1517ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1518d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	struct fc_rport *rport;
15191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 	enum fc_tgtid_binding_type val;
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
15211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (get_fc_tgtid_bind_type_match(buf, &val))
15231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
15241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if changing bind type, purge all unused consistent bindings */
15261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (val != fc_host_tgtid_bind_type(shost)) {
15271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_lock_irqsave(shost->host_lock, flags);
1528d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		while (!list_empty(&fc_host_rport_bindings(shost))) {
1529d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com			get_list_head_entry(rport,
1530d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com				&fc_host_rport_bindings(shost), peers);
1531aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			list_del(&rport->peers);
1532aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			rport->port_state = FC_PORTSTATE_DELETED;
1533aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			fc_queue_work(shost, &rport->rport_delete_work);
1534d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		}
15351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_unlock_irqrestore(shost->host_lock, flags);
15361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_tgtid_bind_type(shost) = val;
15391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;
15401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1542ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
15431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_private_host_tgtid_bind_type,
15441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_private_host_tgtid_bind_type);
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
154691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquezstatic ssize_t
1547ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_private_host_issue_lip(struct device *dev,
1548ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *attr, const char *buf, size_t count)
154991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{
1550ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
155191ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	struct fc_internal *i = to_fc_internal(shost->transportt);
155291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	int ret;
155391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
155491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	/* ignore any data value written to the attribute */
155591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (i->f->issue_fc_host_lip) {
155691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		ret = i->f->issue_fc_host_lip(shost);
155791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		return ret ? ret: count;
155891ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	}
155991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
156091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	return -ENOENT;
156191ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
156291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
1563ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
156491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez			store_fc_private_host_issue_lip);
156591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
1566a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1567a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1568a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
15691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
15701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Statistics Management
15711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
15721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Show a given an attribute in the statistics group */
15741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1575ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesfc_stat_show(const struct device *dev, char *buf, unsigned long offset)
15761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1577ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
15791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_host_statistics *stats;
15801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t ret = -ENOENT;
15811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (offset > sizeof(struct fc_host_statistics) ||
15831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    offset % sizeof(u64) != 0)
15841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		WARN_ON(1);
15851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_fc_host_stats) {
15871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		stats = (i->f->get_fc_host_stats)(shost);
15881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (stats)
15891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = snprintf(buf, 20, "0x%llx\n",
15901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
15911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
15931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* generate a read-only statistics attribute */
15971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_statistic(name)						\
1598ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic ssize_t show_fcstat_##name(struct device *cd,			\
1599ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				  struct device_attribute *attr,	\
1600ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				  char *buf)				\
16011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
16021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return fc_stat_show(cd, buf, 					\
16031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    offsetof(struct fc_host_statistics, name));	\
16041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
1605ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
16061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(seconds_since_last_reset);
16081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_frames);
16091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_words);
16101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_frames);
16111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_words);
16121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(lip_count);
16131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(nos_count);
16141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(error_frames);
16151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(dumped_frames);
16161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(link_failure_count);
16171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_sync_count);
16181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_signal_count);
16191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(prim_seq_protocol_err_count);
16201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_tx_word_count);
16211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_crc_count);
16221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_requests);
16231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_requests);
16241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_control_requests);
16251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_megabytes);
16261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_megabytes);
16271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1629ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesfc_reset_statistics(struct device *dev, struct device_attribute *attr,
1630ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		    const char *buf, size_t count)
16311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1632ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
16331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
16341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* ignore any data value written to the attribute */
16361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->reset_fc_host_stats) {
16371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->reset_fc_host_stats(shost);
16381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return count;
16391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -ENOENT;
16421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1643ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
16441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				fc_reset_statistics);
16451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute *fc_statistics_attrs[] = {
1647ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_seconds_since_last_reset.attr,
1648ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_tx_frames.attr,
1649ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_tx_words.attr,
1650ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_rx_frames.attr,
1651ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_rx_words.attr,
1652ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_lip_count.attr,
1653ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_nos_count.attr,
1654ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_error_frames.attr,
1655ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_dumped_frames.attr,
1656ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_link_failure_count.attr,
1657ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_loss_of_sync_count.attr,
1658ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_loss_of_signal_count.attr,
1659ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_prim_seq_protocol_err_count.attr,
1660ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_invalid_tx_word_count.attr,
1661ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_invalid_crc_count.attr,
1662ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_input_requests.attr,
1663ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_output_requests.attr,
1664ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_control_requests.attr,
1665ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_input_megabytes.attr,
1666ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_output_megabytes.attr,
1667ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_reset_statistics.attr,
16681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	NULL
16691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
16701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute_group fc_statistics_group = {
16721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name = "statistics",
16731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.attrs = fc_statistics_attrs,
16741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
16751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1676a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1677a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Host Vport Attributes */
1678a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1679a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int
1680a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_parse_wwn(const char *ns, u64 *nm)
1681a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1682a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int i, j;
1683a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u8 wwn[8];
1684a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1685a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memset(wwn, 0, sizeof(wwn));
1686a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1687a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* Validate and store the new name */
1688a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	for (i=0, j=0; i < 16; i++) {
1689a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if ((*ns >= 'a') && (*ns <= 'f'))
1690a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = ((j << 4) | ((*ns++ -'a') + 10));
1691a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		else if ((*ns >= 'A') && (*ns <= 'F'))
1692a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = ((j << 4) | ((*ns++ -'A') + 10));
1693a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		else if ((*ns >= '0') && (*ns <= '9'))
1694a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = ((j << 4) | (*ns++ -'0'));
1695a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		else
1696a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EINVAL;
1697a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (i % 2) {
1698a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			wwn[i/2] = j & 0xff;
1699a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = 0;
1700a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		}
1701a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
1702a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1703a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*nm = wwn_to_u64(wwn);
1704a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1705a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0;
1706a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1707a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1708a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1709a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1710a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * "Short-cut" sysfs variable to create a new vport on a FC Host.
1711a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1712a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1713a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1714a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1715a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1716ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1717ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   const char *buf, size_t count)
1718a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1719ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1720a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport_identifiers vid;
1721a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1722a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;
1723a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
1724a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1725a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memset(&vid, 0, sizeof(vid));
1726a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1727a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */
1728a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')
1729a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;
1730a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1731a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* validate we have enough characters for WWPN */
1732a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((cnt != (16+1+16)) || (buf[16] != ':'))
1733a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1734a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1735a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[0], &vid.port_name);
1736a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1737a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1738a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1739a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[17], &vid.node_name);
1740a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1741a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1742a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1743a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1744a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.vport_type = FC_PORTTYPE_NPIV;
1745a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* vid.symbolic_name is already zero/NULL's */
1746a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.disable = false;		/* always enabled */
1747a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1748a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* we only allow support on Channel 0 !!! */
1749a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1750a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1751a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1752ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1753a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_host_vport_create);
1754a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1755a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1756a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1757a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * "Short-cut" sysfs variable to delete a vport on a FC Host.
1758a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Vport is identified by a string containing "<WWPN>:<WWNN>".
1759a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * The WWNs are specified as hex characters, and may *not* contain
1760a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * any prefixes (e.g. 0x, x, etc)
1761a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1762a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1763ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1764ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   const char *buf, size_t count)
1765a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1766ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1767a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1768a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1769a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u64 wwpn, wwnn;
1770a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
1771a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;
1772a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat, match;
1773a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1774a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */
1775a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')
1776a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;
1777a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1778a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* validate we have enough characters for WWPN */
1779a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((cnt != (16+1+16)) || (buf[16] != ':'))
1780a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1781a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1782a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[0], &wwpn);
1783a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1784a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1785a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1786a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[17], &wwnn);
1787a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1788a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1789a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1790a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
1791a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	match = 0;
1792a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* we only allow support on Channel 0 !!! */
1793a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_for_each_entry(vport, &fc_host->vports, peers) {
1794a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if ((vport->channel == 0) &&
1795a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		    (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1796a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			match = 1;
1797a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			break;
1798a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		}
1799a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
1800a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
1801a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1802a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!match)
1803a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENODEV;
1804a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1805a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_vport_terminate(vport);
1806a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1807a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1808ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1809a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_host_vport_delete);
1810a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1811a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
18121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_host_match(struct attribute_container *cont,
18131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  struct device *dev)
18141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
18161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
18171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_host_device(dev))
18191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev);
18221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
18231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
18241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
18271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.host_attrs.ac == cont;
18291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_target_match(struct attribute_container *cont,
18321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
18331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
18351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
18361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_target_device(dev))
18381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
18411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
18421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
18431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
18461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.target_attrs.ac == cont;
18481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void fc_rport_dev_release(struct device *dev)
18511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = dev_to_rport(dev);
18531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
18541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
18551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint scsi_is_fc_rport(const struct device *dev)
18581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return dev->release == fc_rport_dev_release;
18601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(scsi_is_fc_rport);
18621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_rport_match(struct attribute_container *cont,
18641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
18651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
18671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
18681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_fc_rport(dev))
18701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
18731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
18741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
18751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
18781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->rport_attr_cont.ac == cont;
18801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18825c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
1883a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic void fc_vport_dev_release(struct device *dev)
1884a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1885a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport = dev_to_vport(dev);
1886a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev->parent);		/* release kobj parent */
1887a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	kfree(vport);
1888a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1889a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1890a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartint scsi_is_fc_vport(const struct device *dev)
1891a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1892a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return dev->release == fc_vport_dev_release;
1893a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1894a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James SmartEXPORT_SYMBOL(scsi_is_fc_vport);
1895a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1896a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int fc_vport_match(struct attribute_container *cont,
1897a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			    struct device *dev)
1898a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1899a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1900a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost;
1901a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i;
1902a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1903a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!scsi_is_fc_vport(dev))
1904a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return 0;
1905a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport = dev_to_vport(dev);
1906a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1907a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	shost = vport_to_shost(vport);
1908a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1909a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    != &fc_host_class.class)
1910a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return 0;
1911a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1912a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i = to_fc_internal(shost->transportt);
1913a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return &i->vport_attr_cont.ac == cont;
1914a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1915a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1916a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1917c829c394165f981d49f05a9be228404d7a9398d4James Smart/**
1918c829c394165f981d49f05a9be228404d7a9398d4James Smart * fc_timed_out - FC Transport I/O timeout intercept handler
1919c829c394165f981d49f05a9be228404d7a9398d4James Smart * @scmd:	The SCSI command which timed out
1920c829c394165f981d49f05a9be228404d7a9398d4James Smart *
1921c829c394165f981d49f05a9be228404d7a9398d4James Smart * This routine protects against error handlers getting invoked while a
1922c829c394165f981d49f05a9be228404d7a9398d4James Smart * rport is in a blocked state, typically due to a temporarily loss of
1923c829c394165f981d49f05a9be228404d7a9398d4James Smart * connectivity. If the error handlers are allowed to proceed, requests
1924c829c394165f981d49f05a9be228404d7a9398d4James Smart * to abort i/o, reset the target, etc will likely fail as there is no way
1925c829c394165f981d49f05a9be228404d7a9398d4James Smart * to communicate with the device to perform the requested function. These
1926c829c394165f981d49f05a9be228404d7a9398d4James Smart * failures may result in the midlayer taking the device offline, requiring
1927c829c394165f981d49f05a9be228404d7a9398d4James Smart * manual intervention to restore operation.
1928c829c394165f981d49f05a9be228404d7a9398d4James Smart *
1929c829c394165f981d49f05a9be228404d7a9398d4James Smart * This routine, called whenever an i/o times out, validates the state of
1930c829c394165f981d49f05a9be228404d7a9398d4James Smart * the underlying rport. If the rport is blocked, it returns
1931c829c394165f981d49f05a9be228404d7a9398d4James Smart * EH_RESET_TIMER, which will continue to reschedule the timeout.
1932c829c394165f981d49f05a9be228404d7a9398d4James Smart * Eventually, either the device will return, or devloss_tmo will fire,
1933c829c394165f981d49f05a9be228404d7a9398d4James Smart * and when the timeout then fires, it will be handled normally.
1934c829c394165f981d49f05a9be228404d7a9398d4James Smart * If the rport is not blocked, normal error handling continues.
1935c829c394165f981d49f05a9be228404d7a9398d4James Smart *
1936c829c394165f981d49f05a9be228404d7a9398d4James Smart * Notes:
1937c829c394165f981d49f05a9be228404d7a9398d4James Smart *	This routine assumes no locks are held on entry.
1938eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
1939242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboestatic enum blk_eh_timer_return
1940c829c394165f981d49f05a9be228404d7a9398d4James Smartfc_timed_out(struct scsi_cmnd *scmd)
1941c829c394165f981d49f05a9be228404d7a9398d4James Smart{
1942c829c394165f981d49f05a9be228404d7a9398d4James Smart	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1943c829c394165f981d49f05a9be228404d7a9398d4James Smart
1944c829c394165f981d49f05a9be228404d7a9398d4James Smart	if (rport->port_state == FC_PORTSTATE_BLOCKED)
1945242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe		return BLK_EH_RESET_TIMER;
1946c829c394165f981d49f05a9be228404d7a9398d4James Smart
1947242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe	return BLK_EH_NOT_HANDLED;
1948c829c394165f981d49f05a9be228404d7a9398d4James Smart}
1949c829c394165f981d49f05a9be228404d7a9398d4James Smart
19505c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com/*
1951bda232531f0c117921690ee3c060953c8f12e5a1James Smart * Called by fc_user_scan to locate an rport on the shost that
1952bda232531f0c117921690ee3c060953c8f12e5a1James Smart * matches the channel and target id, and invoke scsi_scan_target()
1953bda232531f0c117921690ee3c060953c8f12e5a1James Smart * on the rport.
19545c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com */
1955bda232531f0c117921690ee3c060953c8f12e5a1James Smartstatic void
1956bda232531f0c117921690ee3c060953c8f12e5a1James Smartfc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
19575c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com{
19585c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com	struct fc_rport *rport;
1959bda232531f0c117921690ee3c060953c8f12e5a1James Smart	unsigned long flags;
1960bda232531f0c117921690ee3c060953c8f12e5a1James Smart
1961bda232531f0c117921690ee3c060953c8f12e5a1James Smart	spin_lock_irqsave(shost->host_lock, flags);
19625c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
1963e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1964e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig		if (rport->scsi_target_id == -1)
1965e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig			continue;
19665c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
19670d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke		if (rport->port_state != FC_PORTSTATE_ONLINE)
19680d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke			continue;
19690d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke
1970bda232531f0c117921690ee3c060953c8f12e5a1James Smart		if ((channel == rport->channel) &&
1971bda232531f0c117921690ee3c060953c8f12e5a1James Smart		    (id == rport->scsi_target_id)) {
1972bda232531f0c117921690ee3c060953c8f12e5a1James Smart			spin_unlock_irqrestore(shost->host_lock, flags);
1973bda232531f0c117921690ee3c060953c8f12e5a1James Smart			scsi_scan_target(&rport->dev, channel, id, lun, 1);
1974bda232531f0c117921690ee3c060953c8f12e5a1James Smart			return;
1975e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig		}
1976e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	}
1977e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig
1978bda232531f0c117921690ee3c060953c8f12e5a1James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
1979bda232531f0c117921690ee3c060953c8f12e5a1James Smart}
1980bda232531f0c117921690ee3c060953c8f12e5a1James Smart
1981bda232531f0c117921690ee3c060953c8f12e5a1James Smart/*
1982bda232531f0c117921690ee3c060953c8f12e5a1James Smart * Called via sysfs scan routines. Necessary, as the FC transport
1983bda232531f0c117921690ee3c060953c8f12e5a1James Smart * wants to place all target objects below the rport object. So this
1984bda232531f0c117921690ee3c060953c8f12e5a1James Smart * routine must invoke the scsi_scan_target() routine with the rport
1985bda232531f0c117921690ee3c060953c8f12e5a1James Smart * object as the parent.
1986bda232531f0c117921690ee3c060953c8f12e5a1James Smart */
1987bda232531f0c117921690ee3c060953c8f12e5a1James Smartstatic int
1988bda232531f0c117921690ee3c060953c8f12e5a1James Smartfc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
1989bda232531f0c117921690ee3c060953c8f12e5a1James Smart{
1990bda232531f0c117921690ee3c060953c8f12e5a1James Smart	uint chlo, chhi;
1991bda232531f0c117921690ee3c060953c8f12e5a1James Smart	uint tgtlo, tgthi;
1992bda232531f0c117921690ee3c060953c8f12e5a1James Smart
1993bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1994bda232531f0c117921690ee3c060953c8f12e5a1James Smart	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1995bda232531f0c117921690ee3c060953c8f12e5a1James Smart	    ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1996bda232531f0c117921690ee3c060953c8f12e5a1James Smart		return -EINVAL;
1997bda232531f0c117921690ee3c060953c8f12e5a1James Smart
1998bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (channel == SCAN_WILD_CARD) {
1999bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chlo = 0;
2000bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chhi = shost->max_channel + 1;
2001bda232531f0c117921690ee3c060953c8f12e5a1James Smart	} else {
2002bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chlo = channel;
2003bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chhi = channel + 1;
2004bda232531f0c117921690ee3c060953c8f12e5a1James Smart	}
2005bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2006bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (id == SCAN_WILD_CARD) {
2007bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgtlo = 0;
2008bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgthi = shost->max_id;
2009bda232531f0c117921690ee3c060953c8f12e5a1James Smart	} else {
2010bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgtlo = id;
2011bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgthi = id + 1;
2012bda232531f0c117921690ee3c060953c8f12e5a1James Smart	}
2013bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2014bda232531f0c117921690ee3c060953c8f12e5a1James Smart	for ( ; chlo < chhi; chlo++)
2015bda232531f0c117921690ee3c060953c8f12e5a1James Smart		for ( ; tgtlo < tgthi; tgtlo++)
2016bda232531f0c117921690ee3c060953c8f12e5a1James Smart			fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2017bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2018e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	return 0;
20195c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com}
20205c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
20217525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonoristatic int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
20227525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori				int result)
20237525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori{
20247525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	struct fc_internal *i = to_fc_internal(shost->transportt);
20257525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
20267525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori}
20277525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
20287525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonoristatic int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
20297525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori{
20307525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	struct fc_internal *i = to_fc_internal(shost->transportt);
20317525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	return i->f->it_nexus_response(shost, nexus, result);
20327525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori}
20337525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
20341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_transport_template *
20351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_attach_transport(struct fc_function_template *ft)
20361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int count;
203824669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen	struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
203924669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen					GFP_KERNEL);
20401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!i))
20421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
20431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
20451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.class = &fc_transport_class.class;
20461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.match = fc_target_match;
20471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_size = sizeof(struct fc_starget_attrs);
20481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.target_attrs);
20491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
20511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.class = &fc_host_class.class;
20521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.match = fc_host_match;
20531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_size = sizeof(struct fc_host_attrs);
20541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ft->get_fc_host_stats)
20551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->t.host_attrs.statistics = &fc_statistics_group;
20561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.host_attrs);
20571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
20591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.class = &fc_rport_class.class;
20601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.match = fc_rport_match;
20611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->rport_attr_cont);
20621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2063a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2064a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.class = &fc_vport_class.class;
2065a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.match = fc_vport_match;
2066a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_container_register(&i->vport_attr_cont);
2067a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
20681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f = ft;
20691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport uses the shost workq for scsi scanning */
20711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.create_work_queue = 1;
20725c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
2073c829c394165f981d49f05a9be228404d7a9398d4James Smart	i->t.eh_timed_out = fc_timed_out;
2074c829c394165f981d49f05a9be228404d7a9398d4James Smart
2075e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	i->t.user_scan = fc_user_scan;
20769ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
20777525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	/* target-mode drivers' functions */
20787525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
20797525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	i->t.it_nexus_response = fc_it_nexus_response;
20807525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
20811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
20821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Target Attributes.
20831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
20841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count = 0;
20851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(node_name);
20861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_name);
20871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_id);
20881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_STARGET_NUM_ATTRS);
20901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = NULL;
20921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
20951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Host Attributes.
20961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
20971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
20981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(node_name);
20991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_name);
21006b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
21011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
21021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
21031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
21041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2105a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_create) {
2106a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2107a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2108a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
21091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(serial_number);
21101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_id);
21121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_type);
21131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_state);
21141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
21151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(speed);
21161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2117016131b8fffa1085b4ad165ab228116fdc278ebeJames Smart	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2118b8d08210126a7b769b857720a59721a453a57a1eJames Smart	SETUP_HOST_ATTRIBUTE_RW(system_hostname);
21191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport-managed attributes */
21211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
212291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (ft->issue_fc_host_lip)
212391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2124a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_create)
2125a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2126a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_delete)
2127a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
21281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_HOST_NUM_ATTRS);
21301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = NULL;
21321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
21341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup Remote Port Attributes.
21351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
21361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
21371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
21381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
21391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
21401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
21411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
21421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
21431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
21441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
21451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2146fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
21471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_RPORT_NUM_ATTRS);
21491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = NULL;
21511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2152a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
2153a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * Setup Virtual Port Attributes.
2154a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
2155a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count=0;
2156a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2157a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2158a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2159a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2162a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2163a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2164a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2166a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	BUG_ON(count > FC_VPORT_NUM_ATTRS);
2167a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = NULL;
2169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
21701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t;
21711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
21721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_attach_transport);
21731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid fc_release_transport(struct scsi_transport_template *t)
21751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
21761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(t);
21771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.target_attrs);
21791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.host_attrs);
21801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->rport_attr_cont);
2181a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_container_unregister(&i->vport_attr_cont);
21821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(i);
21841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
21851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_release_transport);
21861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2187aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2188aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_queue_work - Queue work to the fc_host workqueue.
2189aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2190aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @work:	Work to queue for execution.
2191aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart *
2192aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * Return value:
2193a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart * 	1 - work queued for execution
2194a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart *	0 - work is already queued
2195a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart *	-EINVAL - work queue doesn't exist
2196eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2197aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int
2198aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2199aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2200aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (unlikely(!fc_host_work_q(shost))) {
2201aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2202aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to queue work, "
2203aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2204aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2205aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2206aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -EINVAL;
2207aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2208aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2209aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	return queue_work(fc_host_work_q(shost), work);
2210aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2211aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2212aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2213aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_flush_work - Flush a fc_host's workqueue.
2214aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2215eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2216aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2217aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_flush_work(struct Scsi_Host *shost)
2218aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2219aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host_work_q(shost)) {
2220aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2221aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to flush work, "
2222aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2223aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2224aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return;
2225aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2226aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2227aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	flush_workqueue(fc_host_work_q(shost));
2228aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2229aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2230aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2231aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2232aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2233aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @work:	Work to queue for execution.
2234aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @delay:	jiffies to delay the work queuing
2235aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart *
2236aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * Return value:
22370f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * 	1 on success / 0 already queued / < 0 for error
2238eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2239aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int
2240c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2241aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				unsigned long delay)
2242aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2243aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (unlikely(!fc_host_devloss_work_q(shost))) {
2244aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2245aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to queue work, "
2246aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2247aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2248aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2249aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -EINVAL;
2250aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2251aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2252aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2253aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2254aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2255aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2256aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2257aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2258eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2259aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2260aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_flush_devloss(struct Scsi_Host *shost)
2261aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2262aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host_devloss_work_q(shost)) {
2263aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2264aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to flush work, "
2265aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2266aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2267aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return;
2268aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2269aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2270aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	flush_workqueue(fc_host_devloss_work_q(shost));
2271aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2272aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
22731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2275eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2276eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @shost:	Which &Scsi_Host
22771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is expected to be called immediately preceeding the
22791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a driver's call to scsi_remove_host().
22801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * WARNING: A driver utilizing the fc_transport, which fails to call
2282eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *   this routine prior to scsi_remove_host(), will leave dangling
22831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects in /sys/class/fc_remote_ports. Access to any of these
22841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects can result in a system crash !!!
22851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
22871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2288eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
22891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
22901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remove_host(struct Scsi_Host *shost)
22911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2292a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport = NULL, *next_vport = NULL;
2293a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_rport *rport = NULL, *next_rport = NULL;
2294aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct workqueue_struct *work_q;
2295aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2296a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
2297a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2298a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
2299a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2300a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* Remove any vports */
23019ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
23029ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		fc_queue_work(shost, &vport->vport_delete_work);
23031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Remove any remote ports */
23051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
2306aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			&fc_host->rports, peers) {
2307aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
2308aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
2309aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
2310aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2311aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
23121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
2313aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			&fc_host->rport_bindings, peers) {
2314aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
2315aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
2316aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
2317aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2318aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2319a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2320a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2321aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all scan work items */
2322aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	scsi_flush_work(shost);
2323aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2324aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all stgt delete, and rport delete work items, then kill it  */
2325aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->work_q) {
2326aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		work_q = fc_host->work_q;
2327aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->work_q = NULL;
2328aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(work_q);
2329aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2330aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2331aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all devloss work items, then kill it  */
2332aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->devloss_work_q) {
2333aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		work_q = fc_host->devloss_work_q;
2334aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->devloss_work_q = NULL;
2335aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(work_q);
2336aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
23371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
23381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remove_host);
23391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2340fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christiestatic void fc_terminate_rport_io(struct fc_rport *rport)
2341fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie{
2342fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	struct Scsi_Host *shost = rport_to_shost(rport);
2343fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	struct fc_internal *i = to_fc_internal(shost->transportt);
2344fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie
2345fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	/* Involve the LLDD if possible to terminate all io on the rport. */
2346fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	if (i->f->terminate_rport_io)
2347fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie		i->f->terminate_rport_io(rport);
2348fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie
2349fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	/*
2350fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 * must unblock to flush queued IO. The caller will have set
2351fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 * the port_state or flags, so that fc_remote_port_chkready will
2352fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 * fail IO.
2353fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 */
2354fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	scsi_target_unblock(&rport->dev);
2355fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie}
2356aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2357aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2358aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_starget_delete - called to delete the scsi decendents of an rport
2359c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be operated on.
2360eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
2361eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Deletes target and all sdevs.
2362eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
236319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic void
2364c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_starget_delete(struct work_struct *work)
236519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
2366c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2367c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, stgt_delete_work);
236819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2369fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
237019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_remove_target(&rport->dev);
237119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
237219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2373aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2374aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2375aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_rport_final_delete - finish rport termination and delete it.
2376c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be deleted.
2377eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2378aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2379c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_rport_final_delete(struct work_struct *work)
2380aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2381c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2382c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, rport_delete_work);
2383aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct device *dev = &rport->dev;
2384aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
23850f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
238692740b24ce6ddac6534fae985aab602548692186James Smart	unsigned long flags;
2387aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2388aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/*
23899ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	 * if a scan is pending, flush the SCSI Host work_q so that
2390aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * that we can reclaim the rport scan work element.
2391aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 */
2392aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (rport->flags & FC_RPORT_SCAN_PENDING)
2393aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		scsi_flush_work(shost);
2394aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2395fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
23969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
239792740b24ce6ddac6534fae985aab602548692186James Smart	/*
239892740b24ce6ddac6534fae985aab602548692186James Smart	 * Cancel any outstanding timers. These should really exist
239992740b24ce6ddac6534fae985aab602548692186James Smart	 * only when rmmod'ing the LLDD and we're asking for
240092740b24ce6ddac6534fae985aab602548692186James Smart	 * immediate termination of the rports
240192740b24ce6ddac6534fae985aab602548692186James Smart	 */
240292740b24ce6ddac6534fae985aab602548692186James Smart	spin_lock_irqsave(shost->host_lock, flags);
240392740b24ce6ddac6534fae985aab602548692186James Smart	if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
240492740b24ce6ddac6534fae985aab602548692186James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
240592740b24ce6ddac6534fae985aab602548692186James Smart		if (!cancel_delayed_work(&rport->fail_io_work))
240692740b24ce6ddac6534fae985aab602548692186James Smart			fc_flush_devloss(shost);
240792740b24ce6ddac6534fae985aab602548692186James Smart		if (!cancel_delayed_work(&rport->dev_loss_work))
240892740b24ce6ddac6534fae985aab602548692186James Smart			fc_flush_devloss(shost);
240992740b24ce6ddac6534fae985aab602548692186James Smart		spin_lock_irqsave(shost->host_lock, flags);
241092740b24ce6ddac6534fae985aab602548692186James Smart		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
241192740b24ce6ddac6534fae985aab602548692186James Smart	}
241292740b24ce6ddac6534fae985aab602548692186James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
241392740b24ce6ddac6534fae985aab602548692186James Smart
24140f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	/* Delete SCSI target and sdevs */
24150f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->scsi_target_id != -1)
2416c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		fc_starget_delete(&rport->stgt_delete_work);
241792740b24ce6ddac6534fae985aab602548692186James Smart
241892740b24ce6ddac6534fae985aab602548692186James Smart	/*
241992740b24ce6ddac6534fae985aab602548692186James Smart	 * Notify the driver that the rport is now dead. The LLDD will
242092740b24ce6ddac6534fae985aab602548692186James Smart	 * also guarantee that any communication to the rport is terminated
24214be98c0ca304c8a47998b29a7993664f71791250James Smart	 *
24224be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Avoid this call if we already called it when we preserved the
24234be98c0ca304c8a47998b29a7993664f71791250James Smart	 * rport for the binding.
242492740b24ce6ddac6534fae985aab602548692186James Smart	 */
24254be98c0ca304c8a47998b29a7993664f71791250James Smart	if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
24264be98c0ca304c8a47998b29a7993664f71791250James Smart	    (i->f->dev_loss_tmo_callbk))
24270f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		i->f->dev_loss_tmo_callbk(rport);
24280f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
24299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_remove(rport->rqst_q);
24309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
2431aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	transport_remove_device(dev);
2432aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	device_del(dev);
2433aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	transport_destroy_device(dev);
24343bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
24353bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(dev);			/* for self-reference */
2436aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2437aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2438aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
24391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
24401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_rport_create - allocates and creates a remote FC port.
24411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
24421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
24431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
24441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
24451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
24461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Allocates and creates the remoter port structure, including the
24471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * class and sysfs creation.
24481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
24491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
24501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2451eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
245244818efbad185eea75adad0e8cab97650a7370abAdrian Bunkstatic struct fc_rport *
24531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_rport_create(struct Scsi_Host *shost, int channel,
24541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
24551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2456aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
24571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *fci = to_fc_internal(shost->transportt);
24581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
24591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct device *dev;
24601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
24611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int error;
24621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t size;
24631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
246524669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen	rport = kzalloc(size, GFP_KERNEL);
24661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!rport)) {
2467cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		printk(KERN_ERR "%s: allocation failure\n", __func__);
24681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
24691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
24701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->maxframe_size = -1;
24721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->supported_classes = FC_COS_UNSPECIFIED;
24731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->dev_loss_tmo = fc_dev_loss_tmo;
24741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
24751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
24761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_id = ids->port_id;
24771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->roles = ids->roles;
24781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_state = FC_PORTSTATE_ONLINE;
24791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fci->f->dd_fcrport_size)
24801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->dd_data = &rport[1];
24811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->channel = channel;
24820f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	rport->fast_io_fail_tmo = -1;
24831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2484c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2485c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2486c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2487c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2488c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
24891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
24911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->number = fc_host->next_rport_number++;
2493a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
24941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = fc_host->next_target_id++;
24951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
24961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = -1;
2497aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_add_tail(&rport->peers, &fc_host->rports);
24983bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	get_device(&shost->shost_gendev);	/* for fc_host->rport list */
24991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
25011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev = &rport->dev;
25033bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	device_initialize(dev);			/* takes self reference */
25043bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	dev->parent = get_device(&shost->shost_gendev); /* parent reference */
25051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev->release = fc_rport_dev_release;
250671610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers	dev_set_name(dev, "rport-%d:%d-%d",
250771610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		     shost->host_no, channel, rport->number);
25081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_setup_device(dev);
25091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = device_add(dev);
25111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error) {
25121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "FC Remote Port device_add failed\n");
25131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto delete_rport;
25141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
25151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_add_device(dev);
25161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_configure_device(dev);
25171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_rportadd(shost, rport);
25199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* ignore any bsg add error - we just can't do sgio */
25209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
2521a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
25221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
2523aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->flags |= FC_RPORT_SCAN_PENDING;
25241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
2525aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
25261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
25281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsdelete_rport:
25301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_destroy_device(dev);
25311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
25321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_del(&rport->peers);
25333bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
25341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
25351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
25361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
25371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
25381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
25391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2541eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
25421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
25431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
25441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
25451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
25461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport of the existence
25481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
25491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of the port, it's FC address (port_id), and the FC4 roles that are
25501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * active for the port.
25511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * For ports that are FCP targets (aka scsi targets), the FC transport
25531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * maintains consistent target id bindings on behalf of the LLDD.
25541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * A consistent target id binding is an assignment of a target id to
25551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a remote port identifier, which persists while the scsi host is
25561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * attached. The remote port can disappear, then later reappear, and
25571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it's target id assignment remains the same. This allows for shifts
25581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in FC addressing (if binding by wwpn or wwnn) with no apparent
25591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * changes to the scsi subsystem which is based on scsi host number and
25601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * target id values.  Bindings are only valid during the attachment of
25611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the scsi host. If the host detaches, then later re-attaches, target
25621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * id bindings may change.
25631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is responsible for returning a remote port structure.
25651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The routine will search the list of remote ports it maintains
25661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * internally on behalf of consistent target id mappings. If found, the
25671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * remote port structure will be reused. Otherwise, a new remote port
25681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * structure will be allocated.
25691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Whenever a remote port is allocated, a new fc_remote_port class
25711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device is created.
25721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
25741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
25761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2577eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
25781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_rport *
25791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_add(struct Scsi_Host *shost, int channel,
25801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
25811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
258219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *fci = to_fc_internal(shost->transportt);
2583aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
25841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
25851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
25861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int match = 0;
25871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2588aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* ensure any stgt delete functions are done */
2589aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_flush_work(shost);
2590aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
259119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
259219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Search the list of "active" rports, for an rport that has been
259319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * deleted, but we've held off the real delete while the target
259419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * is in a "blocked" state.
259519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
259619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
259719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2598aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_for_each_entry(rport, &fc_host->rports, peers) {
259919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
260019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
260119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			(rport->channel == channel)) {
260219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2603aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			switch (fc_host->tgtid_bind_type) {
260419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWPN:
260519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_NONE:
260619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_name == ids->port_name)
260719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
260819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
260919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWNN:
261019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->node_name == ids->node_name)
261119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
261219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
261319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_ID:
261419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_id == ids->port_id)
261519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
261619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
261719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
261819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
261919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (match) {
262019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
262119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->node_name, &ids->node_name,
262219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->node_name));
262319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->port_name, &ids->port_name,
262419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->port_name));
262519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_id = ids->port_id;
262619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
262719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_state = FC_PORTSTATE_ONLINE;
262819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->roles = ids->roles;
262919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
263019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				spin_unlock_irqrestore(shost->host_lock, flags);
263119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
263219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (fci->f->dd_fcrport_size)
263319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					memset(rport->dd_data, 0,
263419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
263519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
263619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
263792740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were not a target, cancel the
263892740b24ce6ddac6534fae985aab602548692186James Smart				 * io terminate and rport timers, and
263992740b24ce6ddac6534fae985aab602548692186James Smart				 * we're done.
264092740b24ce6ddac6534fae985aab602548692186James Smart				 *
264192740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were a target, but our new role
264292740b24ce6ddac6534fae985aab602548692186James Smart				 * doesn't indicate a target, leave the
264392740b24ce6ddac6534fae985aab602548692186James Smart				 * timers running expecting the role to
264492740b24ce6ddac6534fae985aab602548692186James Smart				 * change as the target fully logs in. If
264592740b24ce6ddac6534fae985aab602548692186James Smart				 * it doesn't, the target will be torn down.
264692740b24ce6ddac6534fae985aab602548692186James Smart				 *
264792740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were a target, and our role shows
264892740b24ce6ddac6534fae985aab602548692186James Smart				 * we're still a target, cancel the timers
264992740b24ce6ddac6534fae985aab602548692186James Smart				 * and kick off a scan.
265019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
265119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
265292740b24ce6ddac6534fae985aab602548692186James Smart				/* was a target, not in roles */
265392740b24ce6ddac6534fae985aab602548692186James Smart				if ((rport->scsi_target_id != -1) &&
2654a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				    (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
265592740b24ce6ddac6534fae985aab602548692186James Smart					return rport;
265619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
265719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
265892740b24ce6ddac6534fae985aab602548692186James Smart				 * Stop the fail io and dev_loss timers.
265992740b24ce6ddac6534fae985aab602548692186James Smart				 * If they flush, the port_state will
266092740b24ce6ddac6534fae985aab602548692186James Smart				 * be checked and will NOOP the function.
266119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
26620f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart				if (!cancel_delayed_work(&rport->fail_io_work))
26630f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart					fc_flush_devloss(shost);
266492740b24ce6ddac6534fae985aab602548692186James Smart				if (!cancel_delayed_work(&rport->dev_loss_work))
2665aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_flush_devloss(shost);
2666aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2667aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				spin_lock_irqsave(shost->host_lock, flags);
2668aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2669fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie				rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
26704be98c0ca304c8a47998b29a7993664f71791250James Smart						  FC_RPORT_DEVLOSS_PENDING |
26714be98c0ca304c8a47998b29a7993664f71791250James Smart						  FC_RPORT_DEVLOSS_CALLBK_DONE);
267219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
267392740b24ce6ddac6534fae985aab602548692186James Smart				/* if target, initiate a scan */
267492740b24ce6ddac6534fae985aab602548692186James Smart				if (rport->scsi_target_id != -1) {
267592740b24ce6ddac6534fae985aab602548692186James Smart					rport->flags |= FC_RPORT_SCAN_PENDING;
267692740b24ce6ddac6534fae985aab602548692186James Smart					scsi_queue_work(shost,
267792740b24ce6ddac6534fae985aab602548692186James Smart							&rport->scan_work);
267892740b24ce6ddac6534fae985aab602548692186James Smart					spin_unlock_irqrestore(shost->host_lock,
267992740b24ce6ddac6534fae985aab602548692186James Smart							flags);
268092740b24ce6ddac6534fae985aab602548692186James Smart					scsi_target_unblock(&rport->dev);
268192740b24ce6ddac6534fae985aab602548692186James Smart				} else
268292740b24ce6ddac6534fae985aab602548692186James Smart					spin_unlock_irqrestore(shost->host_lock,
268392740b24ce6ddac6534fae985aab602548692186James Smart							flags);
2684a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart
26859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				fc_bsg_goose_queue(rport);
26869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
268719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				return rport;
268819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
268919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		}
269019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
269119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
269292740b24ce6ddac6534fae985aab602548692186James Smart	/*
269392740b24ce6ddac6534fae985aab602548692186James Smart	 * Search the bindings array
269492740b24ce6ddac6534fae985aab602548692186James Smart	 * Note: if never a FCP target, you won't be on this list
269592740b24ce6ddac6534fae985aab602548692186James Smart	 */
2696aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
26971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* search for a matching consistent binding */
26991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2700aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_for_each_entry(rport, &fc_host->rport_bindings,
27011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					peers) {
27021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (rport->channel != channel)
27031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				continue;
27041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2705aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			switch (fc_host->tgtid_bind_type) {
27061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWPN:
27071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_name == ids->port_name)
27081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
27091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWNN:
27111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->node_name == ids->node_name)
27121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
27131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_ID:
27151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_id == ids->port_id)
27161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
27171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_NONE: /* to keep compiler happy */
27191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
27211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (match) {
2723aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				list_move_tail(&rport->peers, &fc_host->rports);
27241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
27261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
27271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (match) {
27291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->node_name, &ids->node_name,
27301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->node_name));
27311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->port_name, &ids->port_name,
27321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->port_name));
27331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_id = ids->port_id;
27341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->roles = ids->roles;
27351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_state = FC_PORTSTATE_ONLINE;
2736fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie			rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
27371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
273819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (fci->f->dd_fcrport_size)
273919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memset(rport->dd_data, 0,
274019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
274119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2742a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
27431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				/* initiate a scan of the target */
2744aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				rport->flags |= FC_RPORT_SCAN_PENDING;
27451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				scsi_queue_work(shost, &rport->scan_work);
2746a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				spin_unlock_irqrestore(shost->host_lock, flags);
2747a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				scsi_target_unblock(&rport->dev);
2748a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart			} else
2749a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				spin_unlock_irqrestore(shost->host_lock, flags);
27501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return rport;
27521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
27531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
27541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
275519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
275619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
27571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* No consistent binding found - create new remote port entry */
27581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport = fc_rport_create(shost, channel, ids);
27591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
27611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
27621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_add);
27631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2766eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
27671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that no longer exists
27681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
27691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport that a remote
27701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * port is no longer part of the topology. Note: Although a port
27711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * may no longer be part of the topology, it may persist in the remote
277219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * ports displayed by the fc_host. We do this under 2 conditions:
2773eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
277419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   This allows the port to temporarily disappear, then reappear without
277519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   disrupting the SCSI device tree attached to it. During the "blocked"
277619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   period the port will still exist.
2777eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * 2) If the port was a scsi target and disappears for longer than we
277819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   expect, we'll delete the port and the tear down the SCSI device tree
277919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   attached to it. However, we want to semi-persist the target id assigned
278019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to that port if it eventually does exist. The port structure will
278119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   remain (although with minimal information) so that the target id
278219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   bindings remails.
27831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
27841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If the remote port is not an FCP Target, it will be fully torn down
27851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and deallocated, including the fc_remote_port class device.
27861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
278719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * If the remote port is an FCP Target, the port will be placed in a
278819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * temporary blocked state. From the LLDD's perspective, the rport no
278919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * longer exists. From the SCSI midlayer's perspective, the SCSI target
279019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * exists, but all sdevs on it are blocked from further I/O. The following
2791eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * is then expected.
2792eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
279319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port does not return (signaled by a LLDD call to
279419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
279519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi target is removed - killing all outstanding i/o and removing the
279619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi devices attached ot it. The port structure will be marked Not
279719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   Present and be partially cleared, leaving only enough information to
279819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   recognize the remote port relative to the scsi target id binding if
279919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   it later appears.  The port will remain as long as there is a valid
280019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   binding (e.g. until the user changes the binding type or unloads the
280119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi host with the binding).
28021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
280319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port returns within the dev_loss_tmo value (and matches
280419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   according to the target id binding type), the port structure will be
280519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   reused. If it is no longer a SCSI target, the target will be torn
280619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   down. If it continues to be a SCSI target, then the target will be
280719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   unblocked (allowing i/o to be resumed), and a scan will be activated
280819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to ensure that all luns are detected.
280919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *
281019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * Called from normal process context only - cannot be called from interrupt.
28111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
28121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
28131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2814eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
28151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
28161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_delete(struct fc_rport  *rport)
28171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2818aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
281919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	int timeout = rport->dev_loss_tmo;
2820aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	unsigned long flags;
2821aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2822aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/*
2823aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * No need to flush the fc_host work_q's, as all adds are synchronous.
2824aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 *
2825aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * We do need to reclaim the rport scan work element, so eventually
2826aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2827aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * there's still a scan pending.
2828aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 */
2829aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2830aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_lock_irqsave(shost->host_lock, flags);
28311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
283292740b24ce6ddac6534fae985aab602548692186James Smart	if (rport->port_state != FC_PORTSTATE_ONLINE) {
2833aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
28341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
28351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
28361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
283792740b24ce6ddac6534fae985aab602548692186James Smart	/*
283892740b24ce6ddac6534fae985aab602548692186James Smart	 * In the past, we if this was not an FCP-Target, we would
283992740b24ce6ddac6534fae985aab602548692186James Smart	 * unconditionally just jump to deleting the rport.
284092740b24ce6ddac6534fae985aab602548692186James Smart	 * However, rports can be used as node containers by the LLDD,
284192740b24ce6ddac6534fae985aab602548692186James Smart	 * and its not appropriate to just terminate the rport at the
284292740b24ce6ddac6534fae985aab602548692186James Smart	 * first sign of a loss in connectivity. The LLDD may want to
284392740b24ce6ddac6534fae985aab602548692186James Smart	 * send ELS traffic to re-validate the login. If the rport is
284492740b24ce6ddac6534fae985aab602548692186James Smart	 * immediately deleted, it makes it inappropriate for a node
284592740b24ce6ddac6534fae985aab602548692186James Smart	 * container.
284692740b24ce6ddac6534fae985aab602548692186James Smart	 * So... we now unconditionally wait dev_loss_tmo before
284792740b24ce6ddac6534fae985aab602548692186James Smart	 * destroying an rport.
284892740b24ce6ddac6534fae985aab602548692186James Smart	 */
284992740b24ce6ddac6534fae985aab602548692186James Smart
2850aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->port_state = FC_PORTSTATE_BLOCKED;
2851aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2852aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2853aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2854aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2855aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
28567525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
28577525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	    shost->active_mode & MODE_TARGET)
28587525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
28597525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
286019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_target_block(&rport->dev);
28611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28620f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	/* see if we need to kill io faster than waiting for device loss */
28630f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((rport->fast_io_fail_tmo != -1) &&
2864fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	    (rport->fast_io_fail_tmo < timeout))
28650f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		fc_queue_devloss_work(shost, &rport->fail_io_work,
28660f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart					rport->fast_io_fail_tmo * HZ);
28670f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
286819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* cap the length the devices can be blocked until they are deleted */
2869aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
28701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
28711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_delete);
28721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2874eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
28751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that changed.
2876eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @roles:      New roles for this port.
28771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2878eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Description: The LLDD calls this routine to notify the transport that the
2879eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * roles on a remote port may have changed. The largest effect of this is
28801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * if a port now becomes a FCP Target, it must be allocated a
28811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id.  If the port is no longer a FCP target, any
28821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id value assigned to it will persist in case the
28831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * role changes back to include FCP Target. No changes in the scsi
28841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * midlayer will be invoked if the role changes (in the expectation
28851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that the role will be resumed. If it doesn't normal error processing
28861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * will take place).
28871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
28881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
28891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
28901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
28911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2892eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
28931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
28941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
28951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
28961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);
2897aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
28981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
28991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int create = 0;
29007525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	int ret;
29011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
2903a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (roles & FC_PORT_ROLE_FCP_TARGET) {
290419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (rport->scsi_target_id == -1) {
290519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			rport->scsi_target_id = fc_host->next_target_id++;
290619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
2907a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		} else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
290819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
29097525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	} else if (shost->active_mode & MODE_TARGET) {
29107525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
29117525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori					     (char *)&rport->node_name);
29127525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		if (ret)
29137525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori			printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
29147525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori			       ret);
29151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
29161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
291719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->roles = roles;
291819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2919aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2920aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
292119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (create) {
292219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		/*
292319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * There may have been a delete timer running on the
292419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * port. Ensure that it is cancelled as we now know
292519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * the port is an FCP Target.
292619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Note: we know the rport is exists and in an online
292719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  state as the LLDD would not have had an rport
292819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  reference to pass us.
292919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *
293019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Take no action on the del_timer failure as the state
293119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * machine state change will validate the
293219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * transaction.
293319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 */
29340f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		if (!cancel_delayed_work(&rport->fail_io_work))
29350f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart			fc_flush_devloss(shost);
293619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (!cancel_delayed_work(&rport->dev_loss_work))
2937aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			fc_flush_devloss(shost);
2938aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2939aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_lock_irqsave(shost->host_lock, flags);
2940fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie		rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2941fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie				  FC_RPORT_DEVLOSS_PENDING);
2942aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
2943aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2944aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		/* ensure any stgt delete functions are done */
2945aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_flush_work(shost);
294619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
29471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
2948aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_lock_irqsave(shost->host_lock, flags);
2949aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->flags |= FC_RPORT_SCAN_PENDING;
29501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
2951aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
2952a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		scsi_target_unblock(&rport->dev);
295319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
29541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
29551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_rolechg);
29561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2958eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
295992740b24ce6ddac6534fae985aab602548692186James Smart * @work:	rport target that failed to reappear in the allotted time.
2960eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
2961eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Description: An attempt to delete a remote port blocks, and if it fails
2962eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *              to return in the allotted time this gets called.
2963eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
29641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
2965c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_timeout_deleted_rport(struct work_struct *work)
29661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2967c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2968c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, dev_loss_work.work);
296919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
29704be98c0ca304c8a47998b29a7993664f71791250James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
2971aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
297219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	unsigned long flags;
29731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
297419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
29751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2976aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2977aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
29781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
297992740b24ce6ddac6534fae985aab602548692186James Smart	 * If the port is ONLINE, then it came back. If it was a SCSI
298092740b24ce6ddac6534fae985aab602548692186James Smart	 * target, validate it still is. If not, tear down the
298192740b24ce6ddac6534fae985aab602548692186James Smart	 * scsi_target on it.
29821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2983aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
298492740b24ce6ddac6534fae985aab602548692186James Smart	    (rport->scsi_target_id != -1) &&
2985a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
298619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
2987aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"blocked FC remote port time out: no longer"
2988aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			" a FCP target, removing starget\n");
2989aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
2990a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		scsi_target_unblock(&rport->dev);
2991a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		fc_queue_work(shost, &rport->stgt_delete_work);
299219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
299319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
29941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
299592740b24ce6ddac6534fae985aab602548692186James Smart	/* NOOP state - we're flushing workq's */
299619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
299719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		spin_unlock_irqrestore(shost->host_lock, flags);
299819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
299992740b24ce6ddac6534fae985aab602548692186James Smart			"blocked FC remote port time out: leaving"
300092740b24ce6ddac6534fae985aab602548692186James Smart			" rport%s alone\n",
300192740b24ce6ddac6534fae985aab602548692186James Smart			(rport->scsi_target_id != -1) ?  " and starget" : "");
300219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
300319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
30041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
300592740b24ce6ddac6534fae985aab602548692186James Smart	if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
300692740b24ce6ddac6534fae985aab602548692186James Smart	    (rport->scsi_target_id == -1)) {
3007aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
3008aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
300919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
301092740b24ce6ddac6534fae985aab602548692186James Smart			"blocked FC remote port time out: removing"
301192740b24ce6ddac6534fae985aab602548692186James Smart			" rport%s\n",
301292740b24ce6ddac6534fae985aab602548692186James Smart			(rport->scsi_target_id != -1) ?  " and starget" : "");
3013aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
3014aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
301519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
301619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
30171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
301819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	dev_printk(KERN_ERR, &rport->dev,
301919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"blocked FC remote port time out: removing target and "
302019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"saving binding\n");
302119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
3022aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_move_tail(&rport->peers, &fc_host->rport_bindings);
30231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
302519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Note: We do not remove or clear the hostdata area. This allows
302619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   host-specific target data to persist along with the
302719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   scsi_target_id. It's up to the host to manage it's hostdata area.
30281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
30291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
303019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
303119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Reinitialize port attributes that may change if the port comes back.
303219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
303319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->maxframe_size = -1;
303419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->supported_classes = FC_COS_UNSPECIFIED;
3035a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	rport->roles = FC_PORT_ROLE_UNKNOWN;
3036aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->port_state = FC_PORTSTATE_NOTPRESENT;
3037fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
30384be98c0ca304c8a47998b29a7993664f71791250James Smart	rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
30391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3040f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	/*
3041f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * Pre-emptively kill I/O rather than waiting for the work queue
3042f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * item to teardown the starget. (FCOE libFC folks prefer this
3043f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * and to have the rport_port_id still set when it's done).
3044f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 */
3045f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3046f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	fc_terminate_rport_io(rport);
3047f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart
3048f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	BUG_ON(rport->port_state != FC_PORTSTATE_NOTPRESENT);
3049f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart
305019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* remove the identifiers that aren't used in the consisting binding */
3051aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	switch (fc_host->tgtid_bind_type) {
305219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	case FC_TGTID_BIND_BY_WWPN:
305319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->node_name = -1;
305419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->port_id = -1;
305519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		break;
305619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	case FC_TGTID_BIND_BY_WWNN:
305719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->port_name = -1;
305819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->port_id = -1;
305919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		break;
306019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	case FC_TGTID_BIND_BY_ID:
306119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->node_name = -1;
306219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		rport->port_name = -1;
306319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		break;
306419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	case FC_TGTID_BIND_NONE:	/* to keep compiler happy */
306519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		break;
306619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
306719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
306819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
306919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * As this only occurs if the remote port (scsi target)
307019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * went away and didn't come back - we'll remove
307119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * all attached scsi devices.
307219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
3073a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart	fc_queue_work(shost, &rport->stgt_delete_work);
30744be98c0ca304c8a47998b29a7993664f71791250James Smart
30754be98c0ca304c8a47998b29a7993664f71791250James Smart	/*
30764be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Notify the driver that the rport is now dead. The LLDD will
30774be98c0ca304c8a47998b29a7993664f71791250James Smart	 * also guarantee that any communication to the rport is terminated
30784be98c0ca304c8a47998b29a7993664f71791250James Smart	 *
30794be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Note: we set the CALLBK_DONE flag above to correspond
30804be98c0ca304c8a47998b29a7993664f71791250James Smart	 */
30814be98c0ca304c8a47998b29a7993664f71791250James Smart	if (i->f->dev_loss_tmo_callbk)
30824be98c0ca304c8a47998b29a7993664f71791250James Smart		i->f->dev_loss_tmo_callbk(rport);
30831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
30841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30854be98c0ca304c8a47998b29a7993664f71791250James Smart
30861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
3087eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3088c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	rport to terminate io on.
30890f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart *
30900f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * Notes: Only requests the failure of the io, not that all are flushed
30910f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart *    prior to returning.
3092eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
30930f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic void
3094c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_timeout_fail_rport_io(struct work_struct *work)
30950f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
3096c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3097c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, fail_io_work.work);
30980f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
30990f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->port_state != FC_PORTSTATE_BLOCKED)
31000f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return;
31010f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
3102fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3103fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
31040f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
31050f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
31060f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart/**
31071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3108c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be scanned.
3109eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
31101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
3111c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_scsi_scan_rport(struct work_struct *work)
31121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3113c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3114c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, scan_work);
3115aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
311603f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	struct fc_internal *i = to_fc_internal(shost->transportt);
311742e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	unsigned long flags;
311842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
3119aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
312003f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	    (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
312103f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	    !(i->f->disable_target_scan)) {
3122aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		scsi_scan_target(&rport->dev, rport->channel,
3123aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			rport->scsi_target_id, SCAN_WILD_CARD, 1);
312442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	}
3125aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
3126aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_lock_irqsave(shost->host_lock, flags);
3127aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags &= ~FC_RPORT_SCAN_PENDING;
312842e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
312942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com}
313042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
313142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
3132a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/**
3133a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * fc_vport_setup - allocates and creates a FC virtual port.
3134a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @shost:	scsi host the virtual port is connected to.
3135a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @channel:	Channel on shost port connected to.
3136a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @pdev:	parent device for vport
3137a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @ids:	The world wide names, FC4 port roles, etc for
3138a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *              the virtual port.
3139a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @ret_vport:	The pointer to the created vport.
3140a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3141a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Allocates and creates the vport structure, calls the parent host
3142a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * to instantiate the vport, the completes w/ class and sysfs creation.
3143a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3144a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Notes:
3145a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *	This routine assumes no locks are held on entry.
3146eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
3147a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int
3148a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezfc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3149a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport_identifiers  *ids, struct fc_vport **ret_vport)
3150a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
3151a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3152a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *fci = to_fc_internal(shost->transportt);
3153a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
3154a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *dev;
3155a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
3156a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	size_t size;
3157a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int error;
3158a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3159a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*ret_vport = NULL;
3160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ( ! fci->f->vport_create)
3162a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOENT;
3163a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3164a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport = kzalloc(size, GFP_KERNEL);
3166a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (unlikely(!vport)) {
3167cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		printk(KERN_ERR "%s: allocation failure\n", __func__);
3168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOMEM;
3169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3170a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3171a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_state = FC_VPORT_UNKNOWN;
3172a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_last_state = FC_VPORT_UNKNOWN;
3173a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->node_name = ids->node_name;
3174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->port_name = ids->port_name;
3175a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->roles = ids->roles;
3176a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_type = ids->vport_type;
3177a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (fci->f->dd_fcvport_size)
3178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		vport->dd_data = &vport[1];
3179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->shost = shost;
3180a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->channel = channel;
3181a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags = FC_VPORT_CREATING;
31829ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3183a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3184a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3185a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3186a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3187a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3188a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		kfree(vport);
3189a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOSPC;
3190a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3191a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse++;
3192a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->number = fc_host->next_vport_number++;
3193a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_add_tail(&vport->peers, &fc_host->vports);
3194a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	get_device(&shost->shost_gendev);	/* for fc_host->vport list */
3195a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3196a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3197a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3198a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev = &vport->dev;
3199a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_initialize(dev);			/* takes self reference */
3200a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev->parent = get_device(pdev);		/* takes parent reference */
3201a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev->release = fc_vport_dev_release;
320271610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers	dev_set_name(dev, "vport-%d:%d-%d",
320371610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		     shost->host_no, channel, vport->number);
3204a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_setup_device(dev);
3205a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3206a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = device_add(dev);
3207a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error) {
3208a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		printk(KERN_ERR "FC Virtual Port device_add failed\n");
3209a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		goto delete_vport;
3210a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3211a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_add_device(dev);
3212a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_configure_device(dev);
3213a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3214a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = fci->f->vport_create(vport, ids->disable);
3215a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error) {
3216a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3217a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		goto delete_vport_all;
3218a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3219a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3220a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
3221a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * if the parent isn't the physical adapter's Scsi_Host, ensure
3222a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * the Scsi_Host at least contains ia symlink to the vport.
3223a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
3224a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (pdev != &shost->shost_gendev) {
3225a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		error = sysfs_create_link(&shost->shost_gendev.kobj,
322671610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers				 &dev->kobj, dev_name(dev));
3227a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (error)
3228a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			printk(KERN_ERR
3229a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				"%s: Cannot create vport symlinks for "
3230a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				"%s, err=%d\n",
323171610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers				__func__, dev_name(dev), error);
3232a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3233a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3234a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags &= ~FC_VPORT_CREATING;
3235a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3236a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3237a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev_printk(KERN_NOTICE, pdev,
323871610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers			"%s created via shost%d channel %d\n", dev_name(dev),
3239a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			shost->host_no, channel);
3240a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3241a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*ret_vport = vport;
3242a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3243a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0;
3244a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3245a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartdelete_vport_all:
3246a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_remove_device(dev);
3247a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_del(dev);
3248a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartdelete_vport:
3249a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_destroy_device(dev);
3250a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3251a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_del(&vport->peers);
3252a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(&shost->shost_gendev);	/* for fc_host->vport list */
3253a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse--;
3254a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3255a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev->parent);
3256a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	kfree(vport);
3257a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3258a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return error;
3259a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
3260a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3261a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez/**
3262a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * fc_vport_create - Admin App or LLDD requests creation of a vport
3263a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @shost:	scsi host the virtual port is connected to.
3264a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @channel:	channel on shost port connected to.
3265a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @ids:	The world wide names, FC4 port roles, etc for
3266a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *              the virtual port.
3267a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *
3268a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * Notes:
3269a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *	This routine assumes no locks are held on entry.
3270a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez */
3271a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezstruct fc_vport *
3272a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezfc_vport_create(struct Scsi_Host *shost, int channel,
3273a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	struct fc_vport_identifiers *ids)
3274a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez{
3275a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	int stat;
3276a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	struct fc_vport *vport;
3277a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez
3278a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3279a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez		 ids, &vport);
3280a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	return stat ? NULL : vport;
3281a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez}
3282a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew VasquezEXPORT_SYMBOL(fc_vport_create);
3283a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3284a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/**
3285a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3286a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @vport:	fc_vport to be terminated
3287a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3288a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Calls the LLDD vport_delete() function, then deallocates and removes
3289a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * the vport from the shost and object tree.
3290a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3291a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Notes:
3292a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *	This routine assumes no locks are held on entry.
3293eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
3294a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartint
3295a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_vport_terminate(struct fc_vport *vport)
3296a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
3297a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);
3298a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3299a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
3300a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *dev = &vport->dev;
3301a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
3302a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
3303a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3304a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3305a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & FC_VPORT_CREATING) {
3306a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3307a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;
3308a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3309a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL)) {
3310a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3311a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EALREADY;
3312a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3313a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags |= FC_VPORT_DELETING;
3314a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3315a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3316a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->vport_delete)
3317a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		stat = i->f->vport_delete(vport);
3318a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	else
3319a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		stat = -ENOENT;
3320a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3321a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3322a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags &= ~FC_VPORT_DELETING;
3323a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!stat) {
3324a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		vport->flags |= FC_VPORT_DELETED;
3325a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		list_del(&vport->peers);
3326a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		fc_host->npiv_vports_inuse--;
3327a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		put_device(&shost->shost_gendev);  /* for fc_host->vport list */
3328a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3329a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3330a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3331a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
3332a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
3333a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3334a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (dev->parent != &shost->shost_gendev)
333571610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3336a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_remove_device(dev);
3337a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_del(dev);
3338a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_destroy_device(dev);
3339a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3340a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
3341a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * Removing our self-reference should mean our
3342a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * release function gets called, which will drop the remaining
3343a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * parent reference and free the data structure.
3344a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
3345a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev);			/* for self-reference */
3346a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3347a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0; /* SUCCESS */
3348a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
3349a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James SmartEXPORT_SYMBOL(fc_vport_terminate);
3350a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
33519ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/**
33529ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart * fc_vport_sched_delete - workq-based delete request for a vport
33539ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart * @work:	vport to be deleted.
3354eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
33559ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartstatic void
33569ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartfc_vport_sched_delete(struct work_struct *work)
33579ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart{
33589ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	struct fc_vport *vport =
33599ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		container_of(work, struct fc_vport, vport_delete_work);
33609ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	int stat;
33619ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
33629ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	stat = fc_vport_terminate(vport);
33639ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	if (stat)
33649ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		dev_printk(KERN_ERR, vport->dev.parent,
33659ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart			"%s: %s could not be deleted created via "
3366cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison			"shost%d channel %d - error %d\n", __func__,
336771610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers			dev_name(&vport->dev), vport->shost->host_no,
33689ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart			vport->channel, stat);
33699ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart}
33709ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
3371a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
33729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/*
33739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * BSG support
33749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
33759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
33769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
33779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
33789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
33799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	fc_bsg_job that is to be torn down
33809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
33819e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
33829e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_destroy_bsgjob(struct fc_bsg_job *job)
33839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
33849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	unsigned long flags;
33859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
33869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irqsave(&job->job_lock, flags);
33879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->ref_cnt) {
33889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		spin_unlock_irqrestore(&job->job_lock, flags);
33899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
33909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
33919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irqrestore(&job->job_lock, flags);
33929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
33939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(job->dev);	/* release reference for the request */
33949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
33959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->request_payload.sg_list);
33969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->reply_payload.sg_list);
33979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job);
33989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
33999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
34019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
34029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart *                  completed
34039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	fc_bsg_job that is complete
34049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
34059e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
34069e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_jobdone(struct fc_bsg_job *job)
34079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
34089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req = job->req;
34099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *rsp = req->next_rq;
34109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
34119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = job->req->errors = job->reply->result;
3413b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali
34149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err < 0)
34159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* we're only returning the result field in the reply */
34169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->req->sense_len = sizeof(uint32_t);
34179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
34189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->req->sense_len = job->reply_len;
34199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* we assume all request payload was transferred, residual == 0 */
34219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	req->resid_len = 0;
34229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rsp) {
34249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
34259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* set reply (bidi) residual */
34279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
34289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				      rsp->resid_len);
34299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
3430b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_complete_request(req);
3431b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali}
3432b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali
3433b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali/**
3434b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
3435fe5d20c818a8c5fe83d9f2223a051fb5bc50d180Randy Dunlap * @rq:        BSG request that holds the job to be destroyed
3436b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali */
3437b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavalistatic void fc_bsg_softirq_done(struct request *rq)
3438b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali{
3439b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	struct fc_bsg_job *job = rq->special;
3440b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	unsigned long flags;
34419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
3442b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	spin_lock_irqsave(&job->job_lock, flags);
3443b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	job->state_flags |= FC_RQST_STATE_DONE;
3444b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	job->ref_cnt--;
3445b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	spin_unlock_irqrestore(&job->job_lock, flags);
34469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
3447b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_end_request_all(rq, rq->errors);
34489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_destroy_bsgjob(job);
34499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
34509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
34529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_job_timeout - handler for when a bsg request timesout
34539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @req:	request that timed out
34549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
34559e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum blk_eh_timer_return
34569e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_job_timeout(struct request *req)
34579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
34589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job = (void *) req->special;
34599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = job->shost;
34609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
34619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	unsigned long flags;
34629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err = 0, done = 0;
34639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
34659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return BLK_EH_RESET_TIMER;
34669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irqsave(&job->job_lock, flags);
34689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->state_flags & FC_RQST_STATE_DONE)
34699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		done = 1;
34709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
34719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->ref_cnt++;
34729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irqrestore(&job->job_lock, flags);
34739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!done && i->f->bsg_timeout) {
34759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* call LLDD to abort the i/o as it has timed out */
34769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		err = i->f->bsg_timeout(job);
34779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (err)
34789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
34799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"abort failed with status %d\n", err);
34809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
34819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* the blk_end_sync_io() doesn't check the error */
348347e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali	if (done)
348447e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali		return BLK_EH_NOT_HANDLED;
348547e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali	else
348647e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali		return BLK_EH_HANDLED;
34879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
34889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34899e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
34909e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
34919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
34929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
34939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(!req->nr_phys_segments);
34959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->sg_list = kzalloc(sz, GFP_KERNEL);
34979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!buf->sg_list)
34989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
34999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	sg_init_table(buf->sg_list, req->nr_phys_segments);
35009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
35019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->payload_len = blk_rq_bytes(req);
35029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
35039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
35049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
35079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
35089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart *                   bsg request
35099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	SCSI Host corresponding to the bsg object
35109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	(optional) FC Remote Port corresponding to the bsg object
35119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @req:	BSG request that needs a job structure
35129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
35139e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
35149e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
35159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req)
35169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
35179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
35189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *rsp = req->next_rq;
35199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job;
35209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
35219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(req->special);
35239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
35259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			GFP_KERNEL);
35269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!job)
35279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
35289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/*
35309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * Note: this is a bit silly.
35319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * The request gets formatted as a SGIO v4 ioctl request, which
35329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * then gets reformatted as a blk request, which then gets
35339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * reformatted as a fc bsg request. And on completion, we have
35349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * to wrap return results such that SGIO v4 thinks it was a scsi
35359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * status.  I hope this was all worth it.
35369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 */
35379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	req->special = job;
35399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->shost = shost;
35409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->rport = rport;
35419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->req = req;
35429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (i->f->dd_bsg_size)
35439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dd_data = (void *)&job[1];
35449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_init(&job->job_lock);
35459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->request = (struct fc_bsg_request *)req->cmd;
35469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->request_len = req->cmd_len;
35479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply = req->sense;
35489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = SCSI_SENSE_BUFFERSIZE;	/* Size of sense buffer
35499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart						 * allocated */
35509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (req->bio) {
35519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_bsg_map_buffer(&job->request_payload, req);
35529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret)
35539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto failjob_rls_job;
35549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
35559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rsp && rsp->bio) {
35569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
35579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret)
35589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto failjob_rls_rqst_payload;
35599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
35609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->job_done = fc_bsg_jobdone;
35619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rport)
35629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dev = &rport->dev;
35639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
35649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dev = &shost->shost_gendev;
35659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	get_device(job->dev);		/* take a reference for the request */
35669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->ref_cnt = 1;
35689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
35709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35729e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfailjob_rls_rqst_payload:
35739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->request_payload.sg_list);
35749e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfailjob_rls_job:
35759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job);
35769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return -ENOMEM;
35779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
35789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35809e4f5e29610162fd426366f3b29e3cc6e575b858James Smartenum fc_dispatch_result {
35819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_BREAK,	/* on return, q is locked, break from q loop */
35829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_LOCKED,	/* on return, q is locked, continue on */
35839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_UNLOCKED,	/* on return, q is unlocked, continue on */
35849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart};
35859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
35889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
35895415907af1f5ef80c95147bacbd321b0d4236dd5Randy Dunlap * @q:		fc host request queue
35909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	scsi host rport attached to
35919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	bsg job to be processed
35929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
35939e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum fc_dispatch_result
35949e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
35959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct fc_bsg_job *job)
35969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
35979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
35989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
35999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
36009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* Validate the host command */
36029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	switch (job->request->msgcode) {
36039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_ADD_RPORT:
36049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_add_rport);
36059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_DEL_RPORT:
36089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_del_rport);
36099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_ELS_NOLOGIN:
36129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_els);
36139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be a xmt and rcv payloads */
36149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
36159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
36169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
36179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
36189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
36199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_CT:
36229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_ct);
36239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be xmt and rcv payloads */
36249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
36259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
36269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
36279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
36289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
36299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_VENDOR:
36329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_vendor);
36339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((shost->hostt->vendor_id == 0L) ||
36349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (job->request->rqst_data.h_vendor.vendor_id !=
36359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			shost->hostt->vendor_id)) {
36369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -ESRCH;
36379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
36389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
36399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	default:
36429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -EBADR;
36439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_host_msg;
36449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
36459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* check if we really have all the request data needed */
36479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->request_len < cmdlen) {
36489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -ENOMSG;
36499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_host_msg;
36509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
36519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	ret = i->f->bsg_request(job);
36539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!ret)
36549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return FC_DISPATCH_UNLOCKED;
36559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36569e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfail_host_msg:
36579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* return the errno failure code as the only status */
36589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(job->reply_len < sizeof(uint32_t));
36599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply->result = ret;
36609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = sizeof(uint32_t);
36619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_jobdone(job);
36629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return FC_DISPATCH_UNLOCKED;
36639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
36649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/*
36679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_goose_queue - restart rport queue in case it was stopped
36689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport to be restarted
36699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
36709e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
36719e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_goose_queue(struct fc_rport *rport)
36729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
36739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int flagset;
367439562e783928e3ea9ee2cbce99a756ab48d3c06aChristof Schmitt	unsigned long flags;
36759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!rport->rqst_q)
36779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
36789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	get_device(&rport->dev);
36809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
368139562e783928e3ea9ee2cbce99a756ab48d3c06aChristof Schmitt	spin_lock_irqsave(rport->rqst_q->queue_lock, flags);
36829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	flagset = test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags) &&
36839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		  !test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags);
36849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (flagset)
36859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		queue_flag_set(QUEUE_FLAG_REENTER, rport->rqst_q);
36869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	__blk_run_queue(rport->rqst_q);
36879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (flagset)
36889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		queue_flag_clear(QUEUE_FLAG_REENTER, rport->rqst_q);
368939562e783928e3ea9ee2cbce99a756ab48d3c06aChristof Schmitt	spin_unlock_irqrestore(rport->rqst_q->queue_lock, flags);
36909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(&rport->dev);
36929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
36939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
36969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
36975415907af1f5ef80c95147bacbd321b0d4236dd5Randy Dunlap * @q:		rport request queue
36989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	scsi host rport attached to
36999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport request destined to
37009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	bsg job to be processed
37019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
37029e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum fc_dispatch_result
37039e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
37049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct fc_rport *rport, struct fc_bsg_job *job)
37059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
37069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
37079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
37089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
37099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* Validate the rport command */
37119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	switch (job->request->msgcode) {
37129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_RPT_ELS:
37139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_rport_els);
37149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto check_bidi;
37159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_RPT_CT:
37179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_rport_ct);
37189e4f5e29610162fd426366f3b29e3cc6e575b858James Smartcheck_bidi:
37199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be xmt and rcv payloads */
37209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
37219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
37229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
37239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_rport_msg;
37249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
37259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
37269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	default:
37279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -EBADR;
37289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_rport_msg;
37299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
37309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* check if we really have all the request data needed */
37329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->request_len < cmdlen) {
37339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -ENOMSG;
37349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_rport_msg;
37359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
37369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	ret = i->f->bsg_request(job);
37389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!ret)
37399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return FC_DISPATCH_UNLOCKED;
37409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37419e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfail_rport_msg:
37429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* return the errno failure code as the only status */
37439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(job->reply_len < sizeof(uint32_t));
37449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply->result = ret;
37459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = sizeof(uint32_t);
37469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_jobdone(job);
37479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return FC_DISPATCH_UNLOCKED;
37489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
37499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
37529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_request_handler - generic handler for bsg requests
37539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		request queue to manage
37549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	Scsi_Host related to the bsg object
37559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	FC remote port related to the bsg object (optional)
37569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @dev:	device structure for bsg object
37579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
37589e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
37599e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
37609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		       struct fc_rport *rport, struct device *dev)
37619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
37629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req;
37639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job;
37649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	enum fc_dispatch_result ret;
37659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!get_device(dev))
37679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
37689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	while (!blk_queue_plugged(q)) {
37709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED))
37719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				break;
37729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		req = blk_fetch_request(q);
37749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (!req)
37759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			break;
37769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
37789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			req->errors = -ENXIO;
37799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_unlock_irq(q->queue_lock);
37809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			blk_end_request(req, -ENXIO, blk_rq_bytes(req));
37819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
37829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
37839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
37849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		spin_unlock_irq(q->queue_lock);
37869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_req_to_bsgjob(shost, rport, req);
37889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret) {
37899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			req->errors = ret;
37909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			blk_end_request(req, ret, blk_rq_bytes(req));
37919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
37929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
37939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
37949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job = req->special;
37969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* check if we have the msgcode value at least */
37989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (job->request_len < sizeof(uint32_t)) {
37999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			BUG_ON(job->reply_len < sizeof(uint32_t));
38009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			job->reply->result = -ENOMSG;
38019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			job->reply_len = sizeof(uint32_t);
38029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			fc_bsg_jobdone(job);
38039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
38049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
38059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* the dispatch routines will unlock the queue_lock */
38089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (rport)
38099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = fc_bsg_rport_dispatch(q, shost, rport, job);
38109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		else
38119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = fc_bsg_host_dispatch(q, shost, job);
38129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* did dispatcher hit state that can't process any more */
38149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret == FC_DISPATCH_BREAK)
38159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			break;
38169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* did dispatcher had released the lock */
38189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret == FC_DISPATCH_UNLOCKED)
38199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
38209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
38219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irq(q->queue_lock);
38239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(dev);
38249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irq(q->queue_lock);
38259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
38269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
38299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_host_handler - handler for bsg requests for a fc host
38309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		fc host request queue
38319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
38329e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
38339e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_host_handler(struct request_queue *q)
38349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
38359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = q->queuedata;
38369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
38389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
38399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
38429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rport_handler - handler for bsg requests for a fc rport
38439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		rport request queue
38449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
38459e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
38469e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rport_handler(struct request_queue *q)
38479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
38489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_rport *rport = q->queuedata;
38499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = rport_to_shost(rport);
38509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_request_handler(q, shost, rport, &rport->dev);
38529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
38539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
38569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
38579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	shost for fc_host
38589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @fc_host:	fc_host adding the structures to
38599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
38609e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
38619e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
38629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
38639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct device *dev = &shost->shost_gendev;
38649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
38659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request_queue *q;
38669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
38673c559ea8fd003962d9a28c64b2dd5c6d83ca6edbJames Bottomley	char bsg_name[20];
38689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_host->rqst_q = NULL;
38709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!i->f->bsg_request)
38729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOTSUPP;
38739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	snprintf(bsg_name, sizeof(bsg_name),
38759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		 "fc_host%d", shost->host_no);
38769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
38789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!q) {
38799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "fc_host%d: bsg interface failed to "
38809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - no request queue\n",
38819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 shost->host_no);
38829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
38839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
38849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q->queuedata = shost;
38869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
3887b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_queue_softirq_done(q, fc_bsg_softirq_done);
38889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
38899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
38909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = bsg_register_queue(q, dev, bsg_name, NULL);
38929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err) {
38939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "fc_host%d: bsg interface failed to "
38949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - register queue\n",
38959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				shost->host_no);
38969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
38979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return err;
38989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
38999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_host->rqst_q = q;
39019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
39029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
39069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
39079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	shost that rport is attached to
39089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport that the bsg hooks are being attached to
39099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
39109e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
39119e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
39129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
39139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct device *dev = &rport->dev;
39149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
39159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request_queue *q;
39169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
39179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	rport->rqst_q = NULL;
39199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!i->f->bsg_request)
39219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOTSUPP;
39229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
39249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!q) {
39259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "%s: bsg interface failed to "
39269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - no request queue\n",
39279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 dev->kobj.name);
39289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
39299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q->queuedata = rport;
39329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
3933b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_queue_softirq_done(q, fc_bsg_softirq_done);
39349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
39359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
39369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = bsg_register_queue(q, dev, NULL, NULL);
39389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err) {
39399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "%s: bsg interface failed to "
39409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - register queue\n",
39419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 dev->kobj.name);
39429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
39439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return err;
39449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	rport->rqst_q = q;
39479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
39489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
39529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
39539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:	the request_queue that is to be torn down.
39549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
39559e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
39569e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_remove(struct request_queue *q)
39579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
39589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (q) {
39599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		bsg_unregister_queue(q);
39609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
39619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39659ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/* Original Author:  Martin Hicks */
39669ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames SmartMODULE_AUTHOR("James Smart");
39671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("FC Transport Attributes");
39681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
39691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
39701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(fc_transport_init);
39711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(fc_transport_exit);
3972