scsi_transport_fc.c revision 2bc1c59dbdefdb6f9767e06efb86bbdb2923a8be
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>
3065d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt#include <linux/delay.h>
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_device.h>
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_host.h>
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport.h>
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport_fc.h>
35c829c394165f981d49f05a9be228404d7a9398d4James Smart#include <scsi/scsi_cmnd.h>
3684314fd4740ad73550c76dee4a9578979d84af48James Smart#include <linux/netlink.h>
3784314fd4740ad73550c76dee4a9578979d84af48James Smart#include <net/netlink.h>
3884314fd4740ad73550c76dee4a9578979d84af48James Smart#include <scsi/scsi_netlink_fc.h>
399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart#include <scsi/scsi_bsg_fc.h>
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "scsi_priv.h"
417525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori#include "scsi_transport_fc_internal.h"
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
43aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int fc_queue_work(struct Scsi_Host *, struct work_struct *);
449ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartstatic void fc_vport_sched_delete(struct work_struct *work);
45a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezstatic int fc_vport_setup(struct Scsi_Host *shost, int channel,
46a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *pdev, struct fc_vport_identifiers  *ids,
47a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport **vport);
489e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
499e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
509e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void fc_bsg_remove(struct request_queue *);
519e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void fc_bsg_goose_queue(struct fc_rport *);
52a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
53a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Redefine so that we can have same named attributes in the
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sdev/starget/host objects.
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
57ee959b00c335d7780136c5abda37809191fe52c3Tony Jones#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)		\
58ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstruct device_attribute device_attr_##_prefix##_##_name = 	\
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__ATTR(_name,_mode,_show,_store)
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_search(title, table_type, table)			\
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char *get_fc_##title##_name(enum table_type table_key)	\
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *name = NULL;						\
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
676391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value == table_key) {			\
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			name = table[i].name;				\
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;						\
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return name;							\
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_match(title, table_type, table)			\
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int get_fc_##title##_match(const char *table_key,		\
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		enum table_type *value)					\
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
826391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (strncmp(table_key, table[i].name,			\
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				table[i].matchlen) == 0) {		\
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*value = table[i].value;			\
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0; /* success */				\
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 1; /* failure */						\
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_type values to ascii string name */
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_type	value;
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_type_names[] = {
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_UNKNOWN,		"Unknown" },
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_OTHER,		"Other" },
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NOTPRESENT,	"Not Present" },
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NPORT,	"NPort (fabric via point-to-point)" },
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NLPORT,	"NLPort (fabric via loop)" },
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_LPORT,	"LPort (private loop)" },
104951948a397e3ddc96658efd648b9d66622965b19Christof Schmitt	{ FC_PORTTYPE_PTP,	"Point-To-Point (direct nport connection)" },
105a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORTTYPE_NPIV,		"NPIV VPORT" },
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTTYPE_MAX_NAMELEN		50
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
110a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Reuse fc_port_type enum function for vport_type */
111a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define get_fc_vport_type_name get_fc_port_type_name
112a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11484314fd4740ad73550c76dee4a9578979d84af48James Smart/* Convert fc_host_event_code values to ascii string name */
11584314fd4740ad73550c76dee4a9578979d84af48James Smartstatic const struct {
11684314fd4740ad73550c76dee4a9578979d84af48James Smart	enum fc_host_event_code		value;
11784314fd4740ad73550c76dee4a9578979d84af48James Smart	char				*name;
11884314fd4740ad73550c76dee4a9578979d84af48James Smart} fc_host_event_code_names[] = {
11984314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LIP,			"lip" },
12084314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINKUP,		"link_up" },
12184314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINKDOWN,		"link_down" },
12284314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LIPRESET,		"lip_reset" },
12384314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_RSCN,			"rscn" },
12484314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_ADAPTER_CHANGE,	"adapter_chg" },
12584314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_UNKNOWN,		"port_unknown" },
12684314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_ONLINE,		"port_online" },
12784314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_OFFLINE,		"port_offline" },
12884314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_FABRIC,		"port_fabric" },
12984314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINK_UNKNOWN,		"link_unknown" },
13084314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_VENDOR_UNIQUE,	"vendor_unique" },
13184314fd4740ad73550c76dee4a9578979d84af48James Smart};
13284314fd4740ad73550c76dee4a9578979d84af48James Smartfc_enum_name_search(host_event_code, fc_host_event_code,
13384314fd4740ad73550c76dee4a9578979d84af48James Smart		fc_host_event_code_names)
13484314fd4740ad73550c76dee4a9578979d84af48James Smart#define FC_HOST_EVENT_CODE_MAX_NAMELEN	30
13584314fd4740ad73550c76dee4a9578979d84af48James Smart
13684314fd4740ad73550c76dee4a9578979d84af48James Smart
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_state values to ascii string name */
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_state	value;
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_state_names[] = {
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_UNKNOWN,		"Unknown" },
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_NOTPRESENT,	"Not Present" },
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ONLINE,		"Online" },
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_OFFLINE,		"Offline" },
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BLOCKED,		"Blocked" },
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BYPASSED,	"Bypassed" },
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_DIAGNOSTICS,	"Diagnostics" },
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LINKDOWN,	"Linkdown" },
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ERROR,		"Error" },
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LOOPBACK,	"Loopback" },
15242e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	{ FC_PORTSTATE_DELETED,		"Deleted" },
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTSTATE_MAX_NAMELEN	20
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
158a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Convert fc_vport_state values to ascii string name */
159a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic struct {
160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	enum fc_vport_state	value;
161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char			*name;
162a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart} fc_vport_state_names[] = {
163a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_UNKNOWN,		"Unknown" },
164a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_ACTIVE,		"Active" },
165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_DISABLED,		"Disabled" },
166a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_LINKDOWN,		"Linkdown" },
167a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_INITIALIZING,	"Initializing" },
168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_NO_FABRIC_SUPP,	"No Fabric Support" },
169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_NO_FABRIC_RSCS,	"No Fabric Resources" },
170a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FABRIC_LOGOUT,	"Fabric Logout" },
171a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FABRIC_REJ_WWN,	"Fabric Rejected WWN" },
172a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FAILED,		"VPort Failed" },
173a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart};
174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
175a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define FC_VPORTSTATE_MAX_NAMELEN	24
176a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
177a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Reuse fc_vport_state enum function for vport_last_state */
178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define get_fc_vport_last_state_name get_fc_vport_state_name
179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
180a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_tgtid_binding_type values to ascii string name */
1820ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_tgtid_binding_type	value;
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char				*name;
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int				matchlen;
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_tgtid_binding_type_names[] = {
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_NONE, "none", 4 },
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_BINDTYPE_MAX_NAMELEN	30
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_bitfield_name_search(title, table)			\
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t							\
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsget_fc_##title##_names(u32 table_key, char *buf)		\
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{								\
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *prefix = "";					\
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t len = 0;					\
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;							\
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds								\
2076391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value & table_key) {		\
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			len += sprintf(buf + len, "%s%s",	\
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				prefix, table[i].name);		\
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prefix = ", ";				\
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}						\
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}							\
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");			\
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;						\
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_COS bit values to ascii string name */
2200ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_cos_names[] = {
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS1,	"Class 1" },
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS2,	"Class 2" },
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS3,	"Class 3" },
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS4,	"Class 4" },
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS6,	"Class 6" },
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(cos, fc_cos_names)
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_PORTSPEED bit values to ascii string name */
2340ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_speed_names[] = {
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_1GBIT,		"1 Gbit" },
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_2GBIT,		"2 Gbit" },
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_4GBIT,		"4 Gbit" },
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_10GBIT,		"10 Gbit" },
242c3d2350a8420dbf9d48f5f8a0fb72117bfcbc1b0James Smart	{ FC_PORTSPEED_8GBIT,		"8 Gbit" },
243c3d2350a8420dbf9d48f5f8a0fb72117bfcbc1b0James Smart	{ FC_PORTSPEED_16GBIT,		"16 Gbit" },
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_NOT_NEGOTIATED,	"Not Negotiated" },
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(port_speed, fc_port_speed_names)
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_fc4s (char *buf, u8 *fc4_list)
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i, len=0;
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		len += sprintf(buf + len , "0x%02x ", *fc4_list);
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
261a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Convert FC_PORT_ROLE bit values to ascii string name */
2620ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
265a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart} fc_port_role_names[] = {
266a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_FCP_TARGET,	"FCP Target" },
267a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_FCP_INITIATOR,	"FCP Initiator" },
268a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_IP_PORT,		"IP Port" },
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
270a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_bitfield_name_search(port_roles, fc_port_role_names)
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_PORTID_MASK	0xfffff0
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_ROLE_MASK  	0x00000f
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FPORT_PORTID			0x00000e
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FABCTLR_PORTID		0x00000d
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_DIRSRVR_PORTID		0x00000c
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_TIMESRVR_PORTID		0x00000b
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_MGMTSRVR_PORTID		0x00000a
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
284c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_timeout_deleted_rport(struct work_struct *work);
285c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_timeout_fail_rport_io(struct work_struct *work);
286c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_scsi_scan_rport(struct work_struct *work);
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Attribute counts pre object type...
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Increase these values if you add attributes
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_STARGET_NUM_ATTRS 	3
2930f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart#define FC_RPORT_NUM_ATTRS	10
294a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define FC_VPORT_NUM_ATTRS	9
2954643682ba50634db5cd61292711d031630baf233James Smart#define FC_HOST_NUM_ATTRS	22
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_internal {
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_transport_template t;
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_function_template *f;
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * For attributes : each object has :
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of the actual attributes structures
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of null-terminated pointers to the attribute
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *     structures - used for mid-layer interaction.
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * The attribute containers for the starget and host are are
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * part of the midlayer. As the remote port is specific to the
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * fc transport, we must provide the attribute container.
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
311ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_starget_attrs[
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds							FC_STARGET_NUM_ATTRS];
313ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
315ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
316ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct transport_container rport_attr_cont;
319ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
320ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
321a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
322a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct transport_container vport_attr_cont;
323ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
324ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define to_fc_internal(tmpl)	container_of(tmpl, struct fc_internal, t)
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
329d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_target_setup(struct transport_container *tc, struct device *dev,
330ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   struct device *cdev)
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_target *starget = to_scsi_target(dev);
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * if parent is remote port, use values from remote port.
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Otherwise, this host uses the fc_transport, but not the
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * remote port interface. As such, initialize to known non-values.
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport) {
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = rport->node_name;
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = rport->port_name;
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = rport->port_id;
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = -1;
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = -1;
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = -1;
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_transport_class,
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_transport",
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_target_setup,
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
359d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_host_setup(struct transport_container *tc, struct device *dev,
360ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			 struct device *cdev)
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(dev);
363aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3659ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	/*
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set default values easily detected by the midlayer as
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * failure cases.  The scsi lldd is responsible for initializing
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * all transport attributes to valid values per host.
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
370aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->node_name = -1;
371aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_name = -1;
372aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->permanent_port_name = -1;
373aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->supported_classes = FC_COS_UNSPECIFIED;
374aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->supported_fc4s, 0,
375aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->supported_fc4s));
376aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
377aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->maxframe_size = -1;
378a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->max_npiv_vports = 0;
379aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->serial_number, 0,
380aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->serial_number));
381aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
382aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_id = -1;
383aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_type = FC_PORTTYPE_UNKNOWN;
384aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_state = FC_PORTSTATE_UNKNOWN;
385aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->active_fc4s, 0,
386aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->active_fc4s));
387aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->speed = FC_PORTSPEED_UNKNOWN;
388aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->fabric_name = -1;
389b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
390b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
391aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
392aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
393aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
394aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	INIT_LIST_HEAD(&fc_host->rports);
395aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	INIT_LIST_HEAD(&fc_host->rport_bindings);
396a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	INIT_LIST_HEAD(&fc_host->vports);
397aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->next_rport_number = 0;
398aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->next_target_id = 0;
399a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->next_vport_number = 0;
400a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse = 0;
401aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
402aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers	snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
403aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 "fc_wq_%d", shost->host_no);
404aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->work_q = create_singlethread_workqueue(
405aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_host->work_q_name);
406aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host->work_q)
407aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -ENOMEM;
408aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
409aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers	snprintf(fc_host->devloss_work_q_name,
410aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 sizeof(fc_host->devloss_work_q_name),
411aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 "fc_dl_%d", shost->host_no);
412aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->devloss_work_q = create_singlethread_workqueue(
413aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_host->devloss_work_q_name);
414aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host->devloss_work_q) {
415aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(fc_host->work_q);
416aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->work_q = NULL;
417aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -ENOMEM;
418aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
419aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
4209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_hostadd(shost, fc_host);
4219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* ignore any bsg add error - we just can't do sgio */
4229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
4249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
4259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4269e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_host_remove(struct transport_container *tc, struct device *dev,
4279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct device *cdev)
4289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
4299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = dev_to_shost(dev);
4309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
4319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_remove(fc_host->rqst_q);
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_host_class,
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_host",
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_host_setup,
4399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			       fc_host_remove,
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Setup and Remove actions for remote ports are handled
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in the service functions below.
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_rport_class,
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_remote_ports",
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
453a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Setup and Remove actions for virtual ports are handled
454a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * in the service functions below.
455a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
456a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic DECLARE_TRANSPORT_CLASS(fc_vport_class,
457a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       "fc_vports",
458a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL,
459a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL,
460a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL);
461a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
462a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Module Parameters
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * dev_loss_tmo: the default number of seconds that the FC transport
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   should insulate the loss of a remote port.
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4711c9e16e47a22c61d99aabb1c154e5106ddbf3575James Smartstatic unsigned int fc_dev_loss_tmo = 60;		/* seconds */
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
47310f4b89a0f0586a67f57efb2f5d12fffbb371490Masatake YAMATOmodule_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_PARM_DESC(dev_loss_tmo,
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 "Maximum number of seconds that the FC transport should"
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " insulate the loss of a remote port. Once this value is"
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " exceeded, the scsi target is removed. Value should be"
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
480eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley/*
48184314fd4740ad73550c76dee4a9578979d84af48James Smart * Netlink Infrastructure
482eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
48384314fd4740ad73550c76dee4a9578979d84af48James Smart
48484314fd4740ad73550c76dee4a9578979d84af48James Smartstatic atomic_t fc_event_seq;
48584314fd4740ad73550c76dee4a9578979d84af48James Smart
48684314fd4740ad73550c76dee4a9578979d84af48James Smart/**
48784314fd4740ad73550c76dee4a9578979d84af48James Smart * fc_get_event_number - Obtain the next sequential FC event number
48884314fd4740ad73550c76dee4a9578979d84af48James Smart *
48984314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
490eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *   We could have inlined this, but it would have required fc_event_seq to
49184314fd4740ad73550c76dee4a9578979d84af48James Smart *   be exposed. For now, live with the subroutine call.
49284314fd4740ad73550c76dee4a9578979d84af48James Smart *   Atomic used to avoid lock/unlock...
493eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
49484314fd4740ad73550c76dee4a9578979d84af48James Smartu32
49584314fd4740ad73550c76dee4a9578979d84af48James Smartfc_get_event_number(void)
49684314fd4740ad73550c76dee4a9578979d84af48James Smart{
49784314fd4740ad73550c76dee4a9578979d84af48James Smart	return atomic_add_return(1, &fc_event_seq);
49884314fd4740ad73550c76dee4a9578979d84af48James Smart}
49984314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_get_event_number);
50084314fd4740ad73550c76dee4a9578979d84af48James Smart
50184314fd4740ad73550c76dee4a9578979d84af48James Smart
50284314fd4740ad73550c76dee4a9578979d84af48James Smart/**
50384314fd4740ad73550c76dee4a9578979d84af48James Smart * fc_host_post_event - called to post an even on an fc_host.
50484314fd4740ad73550c76dee4a9578979d84af48James Smart * @shost:		host the event occurred on
50584314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_number:	fc event number obtained from get_fc_event_number()
50684314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_code:		fc_host event being posted
50784314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_data:		32bits of data for the event being posted
50884314fd4740ad73550c76dee4a9578979d84af48James Smart *
50984314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
51084314fd4740ad73550c76dee4a9578979d84af48James Smart *	This routine assumes no locks are held on entry.
511eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
51284314fd4740ad73550c76dee4a9578979d84af48James Smartvoid
51384314fd4740ad73550c76dee4a9578979d84af48James Smartfc_host_post_event(struct Scsi_Host *shost, u32 event_number,
51484314fd4740ad73550c76dee4a9578979d84af48James Smart		enum fc_host_event_code event_code, u32 event_data)
51584314fd4740ad73550c76dee4a9578979d84af48James Smart{
51684314fd4740ad73550c76dee4a9578979d84af48James Smart	struct sk_buff *skb;
51784314fd4740ad73550c76dee4a9578979d84af48James Smart	struct nlmsghdr	*nlh;
51884314fd4740ad73550c76dee4a9578979d84af48James Smart	struct fc_nl_event *event;
51984314fd4740ad73550c76dee4a9578979d84af48James Smart	const char *name;
52084314fd4740ad73550c76dee4a9578979d84af48James Smart	u32 len, skblen;
52184314fd4740ad73550c76dee4a9578979d84af48James Smart	int err;
52284314fd4740ad73550c76dee4a9578979d84af48James Smart
52384314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!scsi_nl_sock) {
52484314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOENT;
52584314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail;
52684314fd4740ad73550c76dee4a9578979d84af48James Smart	}
52784314fd4740ad73550c76dee4a9578979d84af48James Smart
52884314fd4740ad73550c76dee4a9578979d84af48James Smart	len = FC_NL_MSGALIGN(sizeof(*event));
52984314fd4740ad73550c76dee4a9578979d84af48James Smart	skblen = NLMSG_SPACE(len);
53084314fd4740ad73550c76dee4a9578979d84af48James Smart
53184314fd4740ad73550c76dee4a9578979d84af48James Smart	skb = alloc_skb(skblen, GFP_KERNEL);
53284314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!skb) {
53384314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
53484314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail;
53584314fd4740ad73550c76dee4a9578979d84af48James Smart	}
53684314fd4740ad73550c76dee4a9578979d84af48James Smart
53784314fd4740ad73550c76dee4a9578979d84af48James Smart	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
53884314fd4740ad73550c76dee4a9578979d84af48James Smart				skblen - sizeof(*nlh), 0);
53984314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!nlh) {
54084314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
54184314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail_skb;
54284314fd4740ad73550c76dee4a9578979d84af48James Smart	}
54384314fd4740ad73550c76dee4a9578979d84af48James Smart	event = NLMSG_DATA(nlh);
54484314fd4740ad73550c76dee4a9578979d84af48James Smart
54584314fd4740ad73550c76dee4a9578979d84af48James Smart	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
54684314fd4740ad73550c76dee4a9578979d84af48James Smart				FC_NL_ASYNC_EVENT, len);
54784314fd4740ad73550c76dee4a9578979d84af48James Smart	event->seconds = get_seconds();
54884314fd4740ad73550c76dee4a9578979d84af48James Smart	event->vendor_id = 0;
54984314fd4740ad73550c76dee4a9578979d84af48James Smart	event->host_no = shost->host_no;
55084314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_datalen = sizeof(u32);	/* bytes */
55184314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_num = event_number;
55284314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_code = event_code;
55384314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_data = event_data;
55484314fd4740ad73550c76dee4a9578979d84af48James Smart
555ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
556ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso			GFP_KERNEL);
55784314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
55884314fd4740ad73550c76dee4a9578979d84af48James Smart
55984314fd4740ad73550c76dee4a9578979d84af48James Smartsend_fail_skb:
56084314fd4740ad73550c76dee4a9578979d84af48James Smart	kfree_skb(skb);
56184314fd4740ad73550c76dee4a9578979d84af48James Smartsend_fail:
56284314fd4740ad73550c76dee4a9578979d84af48James Smart	name = get_fc_host_event_code_name(event_code);
56384314fd4740ad73550c76dee4a9578979d84af48James Smart	printk(KERN_WARNING
56484314fd4740ad73550c76dee4a9578979d84af48James Smart		"%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
565cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		__func__, shost->host_no,
56684314fd4740ad73550c76dee4a9578979d84af48James Smart		(name) ? name : "<unknown>", event_data, err);
56784314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
56884314fd4740ad73550c76dee4a9578979d84af48James Smart}
56984314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_host_post_event);
57084314fd4740ad73550c76dee4a9578979d84af48James Smart
57184314fd4740ad73550c76dee4a9578979d84af48James Smart
57284314fd4740ad73550c76dee4a9578979d84af48James Smart/**
573eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
57484314fd4740ad73550c76dee4a9578979d84af48James Smart * @shost:		host the event occurred on
57584314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_number:	fc event number obtained from get_fc_event_number()
57684314fd4740ad73550c76dee4a9578979d84af48James Smart * @data_len:		amount, in bytes, of vendor unique data
57784314fd4740ad73550c76dee4a9578979d84af48James Smart * @data_buf:		pointer to vendor unique data
578eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @vendor_id:          Vendor id
57984314fd4740ad73550c76dee4a9578979d84af48James Smart *
58084314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
58184314fd4740ad73550c76dee4a9578979d84af48James Smart *	This routine assumes no locks are held on entry.
582eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
58384314fd4740ad73550c76dee4a9578979d84af48James Smartvoid
58484314fd4740ad73550c76dee4a9578979d84af48James Smartfc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
585f14e2e29cdd07f80de6dec168dc2bb39de37eec3James Smart		u32 data_len, char * data_buf, u64 vendor_id)
58684314fd4740ad73550c76dee4a9578979d84af48James Smart{
58784314fd4740ad73550c76dee4a9578979d84af48James Smart	struct sk_buff *skb;
58884314fd4740ad73550c76dee4a9578979d84af48James Smart	struct nlmsghdr	*nlh;
58984314fd4740ad73550c76dee4a9578979d84af48James Smart	struct fc_nl_event *event;
59084314fd4740ad73550c76dee4a9578979d84af48James Smart	u32 len, skblen;
59184314fd4740ad73550c76dee4a9578979d84af48James Smart	int err;
59284314fd4740ad73550c76dee4a9578979d84af48James Smart
59384314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!scsi_nl_sock) {
59484314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOENT;
59584314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail;
59684314fd4740ad73550c76dee4a9578979d84af48James Smart	}
59784314fd4740ad73550c76dee4a9578979d84af48James Smart
59884314fd4740ad73550c76dee4a9578979d84af48James Smart	len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
59984314fd4740ad73550c76dee4a9578979d84af48James Smart	skblen = NLMSG_SPACE(len);
60084314fd4740ad73550c76dee4a9578979d84af48James Smart
60184314fd4740ad73550c76dee4a9578979d84af48James Smart	skb = alloc_skb(skblen, GFP_KERNEL);
60284314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!skb) {
60384314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
60484314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail;
60584314fd4740ad73550c76dee4a9578979d84af48James Smart	}
60684314fd4740ad73550c76dee4a9578979d84af48James Smart
60784314fd4740ad73550c76dee4a9578979d84af48James Smart	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
60884314fd4740ad73550c76dee4a9578979d84af48James Smart				skblen - sizeof(*nlh), 0);
60984314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!nlh) {
61084314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
61184314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail_skb;
61284314fd4740ad73550c76dee4a9578979d84af48James Smart	}
61384314fd4740ad73550c76dee4a9578979d84af48James Smart	event = NLMSG_DATA(nlh);
61484314fd4740ad73550c76dee4a9578979d84af48James Smart
61584314fd4740ad73550c76dee4a9578979d84af48James Smart	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
61684314fd4740ad73550c76dee4a9578979d84af48James Smart				FC_NL_ASYNC_EVENT, len);
61784314fd4740ad73550c76dee4a9578979d84af48James Smart	event->seconds = get_seconds();
61884314fd4740ad73550c76dee4a9578979d84af48James Smart	event->vendor_id = vendor_id;
61984314fd4740ad73550c76dee4a9578979d84af48James Smart	event->host_no = shost->host_no;
62084314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_datalen = data_len;	/* bytes */
62184314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_num = event_number;
62284314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_code = FCH_EVT_VENDOR_UNIQUE;
62384314fd4740ad73550c76dee4a9578979d84af48James Smart	memcpy(&event->event_data, data_buf, data_len);
62484314fd4740ad73550c76dee4a9578979d84af48James Smart
625ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
626ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso			GFP_KERNEL);
62784314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
62884314fd4740ad73550c76dee4a9578979d84af48James Smart
62984314fd4740ad73550c76dee4a9578979d84af48James Smartsend_vendor_fail_skb:
63084314fd4740ad73550c76dee4a9578979d84af48James Smart	kfree_skb(skb);
63184314fd4740ad73550c76dee4a9578979d84af48James Smartsend_vendor_fail:
63284314fd4740ad73550c76dee4a9578979d84af48James Smart	printk(KERN_WARNING
63384314fd4740ad73550c76dee4a9578979d84af48James Smart		"%s: Dropped Event : host %d vendor_unique - err %d\n",
634cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		__func__, shost->host_no, err);
63584314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
63684314fd4740ad73550c76dee4a9578979d84af48James Smart}
63784314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_host_post_vendor_event);
63884314fd4740ad73550c76dee4a9578979d84af48James Smart
63984314fd4740ad73550c76dee4a9578979d84af48James Smart
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic __init int fc_transport_init(void)
6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
64384314fd4740ad73550c76dee4a9578979d84af48James Smart	int error;
64484314fd4740ad73550c76dee4a9578979d84af48James Smart
64584314fd4740ad73550c76dee4a9578979d84af48James Smart	atomic_set(&fc_event_seq, 0);
64684314fd4740ad73550c76dee4a9578979d84af48James Smart
64784314fd4740ad73550c76dee4a9578979d84af48James Smart	error = transport_class_register(&fc_host_class);
6481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
6491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return error;
650a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = transport_class_register(&fc_vport_class);
651a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error)
652a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return error;
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = transport_class_register(&fc_rport_class);
6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return error;
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return transport_class_register(&fc_transport_class);
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit fc_transport_exit(void)
6601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_transport_class);
6621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_rport_class);
6631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_host_class);
664a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_class_unregister(&fc_vport_class);
6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
6681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC Remote Port Attribute Management
6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_show_function(field, format_string, sz, cast)		\
6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
673ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##field (struct device *dev, 				\
674ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
676ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
6781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
67919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((i->f->get_rport_##field) &&				\
68019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    !((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
68142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_DELETED) ||		\
68219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_NOTPRESENT)))		\
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_rport_##field(rport);				\
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_store_function(field)					\
6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
689ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_##field(struct device *dev,				\
690ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
691ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
694ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
6951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
6961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
6970f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;							\
69819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
69942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||		\
70019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))		\
70119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;						\
7020f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);				\
7030f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (*cp && (*cp != '\n'))					\
7040f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EINVAL;						\
7051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_rport_##field(rport, val);				\
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr(field, format_string, sz)			\
7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
711ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
7131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr_cast(field, format_string, sz, cast)		\
7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, (cast))	\
716ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rw_attr(field, format_string, sz)			\
7201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
7211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_store_function(field)					\
722ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,		\
7231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##field,				\
7241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_rport_##field)
7251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_show_function(field, format_string, sz, cast)	\
7281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
729ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##field (struct device *dev, 				\
730ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
7311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
732ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr(field, format_string, sz)		\
7371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, )	\
738ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
7401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)	\
7421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, (cast)) \
743ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
7451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_enum_attr(title, maxlen)			\
7481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
749ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##title (struct device *dev,				\
750ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
7511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
752ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(rport->title);			\
7551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
7561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
7571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
7581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
759ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, title, S_IRUGO,			\
7601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##title, NULL)
7611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RD(field)					\
764ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
7661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
7671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
7691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
7701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)				\
772ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
7741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
7751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
7771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RW(field)					\
779ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_rport_##field) {					\
7811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].attr.mode = S_IRUGO;	\
7821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].store = NULL;		\
7831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
7841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
7861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
7871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7880f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field)				\
7890f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{									\
790ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7910f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7920f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	count++;							\
7930f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
7940f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
7951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport Remote Port Attributes: */
7971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Remote Port Attributes */
7991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
8011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
803ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_supported_classes (struct device *dev,
804ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				 struct device_attribute *attr, char *buf)
8051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
806ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
8071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport->supported_classes == FC_COS_UNSPECIFIED)
8081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
8091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(rport->supported_classes, buf);
8101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
811ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
8121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_supported_classes, NULL);
8131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Remote Port Attributes */
8151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
81619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com/*
81719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * dev_loss_tmo attribute
81819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com */
81919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comfc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
82019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic ssize_t
821ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
822ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			    const char *buf, size_t count)
82319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
82419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	int val;
825ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
82619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
82719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *i = to_fc_internal(shost->transportt);
8280f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;
82919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
83042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||
83119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
83219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;
8330f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);
8340f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((*cp && (*cp != '\n')) ||
8350f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
83619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EINVAL;
83719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	i->f->set_rport_dev_loss_tmo(rport, val);
83819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	return count;
83919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
840ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
84119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
8421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Remote Port Attributes */
8451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
8471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
8481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
8491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
851ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_roles (struct device *dev, struct device_attribute *attr,
852ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		     char *buf)
8531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
854ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
8551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* identify any roles that are port_id specific */
8571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((rport->port_id != -1) &&
8581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
8591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					FC_WELLKNOWN_PORTID_MASK) {
8601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
8611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FPORT_PORTID:
8621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Port\n");
8631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FABCTLR_PORTID:
8641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Controller\n");
8651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_DIRSRVR_PORTID:
8661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Directory Server\n");
8671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_TIMESRVR_PORTID:
8681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Time Server\n");
8691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_MGMTSRVR_PORTID:
8701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Management Server\n");
8711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
8721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Unknown Fabric Entity\n");
8731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
8741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
875a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (rport->roles == FC_PORT_ROLE_UNKNOWN)
8761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 20, "unknown\n");
877a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return get_fc_port_roles_names(rport->roles, buf);
8781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
880ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, roles, S_IRUGO,
8811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_roles, NULL);
8821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
8841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
8851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8860f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart/*
8870f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * fast_io_fail_tmo attribute
8880f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart */
8890f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic ssize_t
890ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_fast_io_fail_tmo (struct device *dev,
891ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				struct device_attribute *attr, char *buf)
8920f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
893ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
8940f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
8950f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->fast_io_fail_tmo == -1)
8960f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return snprintf(buf, 5, "off\n");
8970f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
8980f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
8990f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
9000f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic ssize_t
901ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_fast_io_fail_tmo(struct device *dev,
902ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				struct device_attribute *attr, const char *buf,
903ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				size_t count)
9040f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
9050f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	int val;
9060f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;
907ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
9080f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
9090f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
9100f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (rport->port_state == FC_PORTSTATE_DELETED) ||
9110f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
9120f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EBUSY;
9130f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (strncmp(buf, "off", 3) == 0)
9140f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		rport->fast_io_fail_tmo = -1;
9150f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	else {
9160f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		val = simple_strtoul(buf, &cp, 0);
9170f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		if ((*cp && (*cp != '\n')) ||
9180f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		    (val < 0) || (val >= rport->dev_loss_tmo))
9190f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart			return -EINVAL;
9200f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		rport->fast_io_fail_tmo = val;
9210f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	}
9220f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	return count;
9230f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
924ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
9250f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
9291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC SCSI Target Attribute Management
9301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
9311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
9331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Note: in the target show function we recognize when the remote
934a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *  port is in the heirarchy and do not allow the driver to get
9351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  involved in sysfs functions. The driver only gets involved if
9361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  it's the "old" style that doesn't use rports.
9371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
9381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_show_function(field, format_string, sz, cast)	\
9391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
940ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_starget_##field (struct device *dev, 				\
941ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			 struct device_attribute *attr, char *buf)	\
9421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
943ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct scsi_target *starget = transport_class_to_starget(dev);	\
9441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);	\
9451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
9461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);		\
9471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport)							\
9481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_##field(starget) = rport->field;		\
9491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (i->f->get_starget_##field)				\
9501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_starget_##field(starget);			\
9511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, 			\
9521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cast fc_starget_##field(starget)); 			\
9531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr(field, format_string, sz)			\
9561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, )		\
957ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
9581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_starget_##field, NULL)
9591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr_cast(field, format_string, sz, cast)		\
9611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, (cast))	\
962ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
9631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_starget_##field, NULL)
9641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RD(field)				\
966ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_starget_attrs[count] = device_attr_starget_##field; \
9671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].attr.mode = S_IRUGO;		\
9681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].store = NULL;			\
9691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
9701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
9711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
9721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RW(field)				\
974ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_starget_attrs[count] = device_attr_starget_##field; \
9751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_starget_##field) {				\
9761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].attr.mode = S_IRUGO;	\
9771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].store = NULL;		\
9781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
9791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
9801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
9811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
9821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport SCSI Target Attributes: */
9841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
9851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
9861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr(port_id, "0x%06x\n", 20);
9871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
990a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * FC Virtual Port Attribute Management
991a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
992a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
993a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_show_function(field, format_string, sz, cast)		\
994a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
995ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##field (struct device *dev, 				\
996ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
997a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
998ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
999a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1000a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1001a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((i->f->get_vport_##field) &&				\
1002a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)))	\
1003a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->f->get_vport_##field(vport);				\
1004a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, sz, format_string, cast vport->field); 	\
1005a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1006a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1007a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_store_function(field)					\
1008a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1009ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1010ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1011ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1012a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1013a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int val;							\
1014ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1015a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1016a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1017a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char *cp;							\
1018a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))	\
1019a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;						\
1020a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	val = simple_strtoul(buf, &cp, 0);				\
1021a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*cp && (*cp != '\n'))					\
1022a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1023a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->f->set_vport_##field(vport, val);				\
1024a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1025a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1026a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1027a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_store_str_function(field, slen)			\
1028a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1029ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1030ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, 			\
1031ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1032a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1033ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1034a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1035a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1036a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;						\
1037a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart									\
1038a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */			\
1039a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')						\
1040a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;							\
1041a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (cnt > ((slen) - 1))						\
1042a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1043a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memcpy(vport->field, buf, cnt);					\
1044a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->f->set_vport_##field(vport);					\
1045a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1046a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1047a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1048a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rd_attr(field, format_string, sz)			\
1049a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, )		\
1050ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1051a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			 show_fc_vport_##field, NULL)
1052a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1053a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rd_attr_cast(field, format_string, sz, cast)		\
1054a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, (cast))	\
1055ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1056a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			  show_fc_vport_##field, NULL)
1057a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1058a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rw_attr(field, format_string, sz)			\
1059a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, )		\
1060a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_store_function(field)					\
1061ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1062a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##field,				\
1063a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_vport_##field)
1064a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1065a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_show_function(field, format_string, sz, cast)	\
1066a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1067ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##field (struct device *dev,				\
1068ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
1069a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1070ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1071a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, sz, format_string, cast vport->field); 	\
1072a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1073a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1074a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_store_u32_function(field)			\
1075a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1076ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1077ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1078ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1079a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1080a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u32 val;							\
1081ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1082a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char *cp;							\
1083a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))		\
1084a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;						\
1085a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	val = simple_strtoul(buf, &cp, 0);				\
1086a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*cp && (*cp != '\n'))					\
1087a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1088a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->field = val;						\
1089a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1090a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1091a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1092a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1093a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_attr(field, format_string, sz)		\
1094a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, )	\
1095ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1096a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			 show_fc_vport_##field, NULL)
1097a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1098a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast)	\
1099a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, (cast)) \
1100ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1101a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			  show_fc_vport_##field, NULL)
1102a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1103a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rw_u32_attr(field, format_string, sz)		\
1104a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, )	\
1105a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_store_u32_function(field)			\
1106ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1107a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##field,				\
1108a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_vport_##field)
1109a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1110a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1111a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_enum_attr(title, maxlen)			\
1112a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1113ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##title (struct device *dev,				\
1114ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1115ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       char *buf)					\
1116a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1117ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1118a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	const char *name;						\
1119a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	name = get_fc_##title##_name(vport->title);			\
1120a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!name)							\
1121a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1122a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, maxlen, "%s\n", name);			\
1123a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}									\
1124ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, title, S_IRUGO,			\
1125a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##title, NULL)
1126a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1127a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1128a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_RD(field)					\
1129ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1130a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1131a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].store = NULL;			\
1132a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1133a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->get_##field)						\
1134a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		count++
1135a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: checks function not show bit */
1136a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1137a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field)				\
1138ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1139a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1140a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].store = NULL;			\
1141a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1142a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1143a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1144a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_WR(field)					\
1145ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1146a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1147a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->field)						\
1148a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		count++
1149a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: checks function */
1150a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1151a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_RW(field)					\
1152ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1153a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!i->f->set_vport_##field) {					\
1154a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->private_vport_attrs[count].attr.mode = S_IRUGO;	\
1155a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->private_vport_attrs[count].store = NULL;		\
1156a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}								\
1157a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1158a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1159a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: does not check show bit */
1160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field)				\
1162a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1163ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1164a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++;							\
1166a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1167a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* The FC Transport Virtual Port Attributes: */
1170a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1171a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Fixed Virtual Port Attributes */
1172a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1173a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Dynamic Virtual Port Attributes */
1174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1175a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Private Virtual Port Attributes */
1176a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1177a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1180a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1181a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1182a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1183ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1184ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		     char *buf)
1185a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1186ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
1187a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1188a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1189a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return snprintf(buf, 20, "unknown\n");
1190a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return get_fc_port_roles_names(vport->roles, buf);
1191a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1192ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1193a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1194a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1195a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1196a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_show_function(symbolic_name, "%s\n",
1197a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1198a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1199ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1200a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1201a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1202a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1203ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1204ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf, size_t count)
1205a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1206ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
12079ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	struct Scsi_Host *shost = vport_to_shost(vport);
1208a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
12099ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	fc_queue_work(shost, &vport->vport_delete_work);
1210a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;
1211a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1212ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1213a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			NULL, store_fc_vport_delete);
1214a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1215a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1216a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1217a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Enable/Disable vport
1218a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *  Write "1" to disable, write "0" to enable
1219a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1220a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1221ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1222ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,
1223a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			   size_t count)
1224a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1225ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
1226a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);
1227a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
1228a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
1229a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1230a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1231a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;
1232a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1233a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*buf == '0') {
1234a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (vport->vport_state != FC_VPORT_DISABLED)
1235a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EALREADY;
1236a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	} else if (*buf == '1') {
1237a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (vport->vport_state == FC_VPORT_DISABLED)
1238a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EALREADY;
1239a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	} else
1240a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1241a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1242a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1243a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1244a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1245ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1246a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			NULL, store_fc_vport_disable);
1247a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1248a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1249a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
12501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Attribute Management
12511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
12521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_show_function(field, format_string, sz, cast)		\
12541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1255ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##field (struct device *dev,				\
1256ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
12571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1258ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
12591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
12601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##field)					\
12611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##field(shost);				\
12621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
12631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_store_function(field)					\
12661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1267ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_##field(struct device *dev, 				\
1268ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr,			\
1269ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf,	size_t count)			\
12701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
12711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
1272ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
12731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
12740f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;							\
12751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
12760f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);				\
12770f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (*cp && (*cp != '\n'))					\
12780f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EINVAL;						\
12791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_host_##field(shost, val);				\
12801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
12811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1283b8d08210126a7b769b857720a59721a453a57a1eJames Smart#define fc_host_store_str_function(field, slen)				\
1284b8d08210126a7b769b857720a59721a453a57a1eJames Smartstatic ssize_t								\
1285ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_##field(struct device *dev,				\
1286ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr,			\
1287ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf, size_t count)			\
1288b8d08210126a7b769b857720a59721a453a57a1eJames Smart{									\
1289ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1290b8d08210126a7b769b857720a59721a453a57a1eJames Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1291b8d08210126a7b769b857720a59721a453a57a1eJames Smart	unsigned int cnt=count;						\
1292b8d08210126a7b769b857720a59721a453a57a1eJames Smart									\
1293b8d08210126a7b769b857720a59721a453a57a1eJames Smart	/* count may include a LF at end of string */			\
1294b8d08210126a7b769b857720a59721a453a57a1eJames Smart	if (buf[cnt-1] == '\n')						\
1295b8d08210126a7b769b857720a59721a453a57a1eJames Smart		cnt--;							\
1296b8d08210126a7b769b857720a59721a453a57a1eJames Smart	if (cnt > ((slen) - 1))						\
1297b8d08210126a7b769b857720a59721a453a57a1eJames Smart		return -EINVAL;						\
1298b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memcpy(fc_host_##field(shost), buf, cnt);			\
1299b8d08210126a7b769b857720a59721a453a57a1eJames Smart	i->f->set_host_##field(shost);					\
1300b8d08210126a7b769b857720a59721a453a57a1eJames Smart	return count;							\
1301b8d08210126a7b769b857720a59721a453a57a1eJames Smart}
1302b8d08210126a7b769b857720a59721a453a57a1eJames Smart
13031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr(field, format_string, sz)			\
13041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
1305ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
13071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr_cast(field, format_string, sz, cast)		\
13091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, (cast))		\
1310ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
13121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rw_attr(field, format_string, sz)			\
13141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
13151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_store_function(field)					\
1316ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
13171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_host_##field,				\
13181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_host_##field)
13191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_enum_attr(title, maxlen)				\
13211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1322ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##title (struct device *dev,				\
1323ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
13241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1325ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
13261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
13271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
13281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##title)					\
13291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##title(shost);				\
13301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(fc_host_##title(shost));		\
13311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
13321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
13331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
13341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
1335ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
13361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RD(field)					\
1338ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
13401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
13411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
13421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
13431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
13441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1345a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_HOST_ATTRIBUTE_RD_NS(field)				\
1346ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
1347a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1348a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_host_attrs[count].store = NULL;			\
1349a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->host_attrs[count] = &i->private_host_attrs[count];		\
1350a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1351a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
13521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RW(field)					\
1353ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_host_##field) {					\
13551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].attr.mode = S_IRUGO;	\
13561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].store = NULL;		\
13571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
13581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
13601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
13611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_show_function(field, format_string, sz, cast)	\
13641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1365ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##field (struct device *dev,				\
1366ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
13671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1368ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
13691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
13701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr(field, format_string, sz)		\
13731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, )	\
1374ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
13761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr_cast(field, format_string, sz, cast)	\
13781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, (cast)) \
1379ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
13811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
1383ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
13851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
13861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
13871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
13881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)			\
139091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{									\
1391ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
139391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	count++;							\
139491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
13951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Host Attributes */
13981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1400ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_classes (struct device *dev,
1401ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			        struct device_attribute *attr, char *buf)
14021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1403ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
14061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
14071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
14091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1410ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
14111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_classes, NULL);
14121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1414ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_fc4s (struct device *dev,
1415ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			     struct device_attribute *attr, char *buf)
14161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1417ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
14191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1420ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
14211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_fc4s, NULL);
14221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1424ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_speeds (struct device *dev,
1425ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			       struct device_attribute *attr, char *buf)
14261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1427ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
14301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
14311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
14331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1434ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
14351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_speeds, NULL);
14361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
14391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
14406b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmannfc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
14416b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann			     unsigned long long);
14421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1443a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
14441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
14451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Host Attributes */
14481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1450ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_active_fc4s (struct device *dev,
1451ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			  struct device_attribute *attr, char *buf)
14521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1453ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
14551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_active_fc4s)
14571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_active_fc4s(shost);
14581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
14601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1461ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
14621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_active_fc4s, NULL);
14631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1465ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_speed (struct device *dev,
1466ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		    struct device_attribute *attr, char *buf)
14671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1468ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
14701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_speed)
14721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_speed(shost);
14731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
14751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
14761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_speed(shost), buf);
14781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1479ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, speed, S_IRUGO,
14801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_speed, NULL);
14811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr(port_id, "0x%06x\n", 20);
14841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
14851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
14861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1487016131b8fffa1085b4ad165ab228116fdc278ebeJames Smartfc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
14881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1489b8d08210126a7b769b857720a59721a453a57a1eJames Smartfc_private_host_show_function(system_hostname, "%s\n",
1490b8d08210126a7b769b857720a59721a453a57a1eJames Smart		FC_SYMBOLIC_NAME_SIZE + 1, )
1491b8d08210126a7b769b857720a59721a453a57a1eJames Smartfc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1492ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1493b8d08210126a7b769b857720a59721a453a57a1eJames Smart		show_fc_host_system_hostname, store_fc_host_system_hostname);
1494b8d08210126a7b769b857720a59721a453a57a1eJames Smart
14951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Host Attributes */
14971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1499ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_private_host_tgtid_bind_type(struct device *dev,
1500ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				     struct device_attribute *attr, char *buf)
15011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1502ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;
15041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
15061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)
15071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
15081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
15091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1511d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com#define get_list_head_entry(pos, head, member) 		\
1512d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	pos = list_entry((head)->next, typeof(*pos), member)
1513d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com
15141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1515ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_private_host_tgtid_bind_type(struct device *dev,
1516ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *attr, const char *buf, size_t count)
15171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1518ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1519d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	struct fc_rport *rport;
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 	enum fc_tgtid_binding_type val;
15211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
15221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (get_fc_tgtid_bind_type_match(buf, &val))
15241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
15251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if changing bind type, purge all unused consistent bindings */
15271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (val != fc_host_tgtid_bind_type(shost)) {
15281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_lock_irqsave(shost->host_lock, flags);
1529d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		while (!list_empty(&fc_host_rport_bindings(shost))) {
1530d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com			get_list_head_entry(rport,
1531d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com				&fc_host_rport_bindings(shost), peers);
1532aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			list_del(&rport->peers);
1533aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			rport->port_state = FC_PORTSTATE_DELETED;
1534aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			fc_queue_work(shost, &rport->rport_delete_work);
1535d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		}
15361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_unlock_irqrestore(shost->host_lock, flags);
15371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_tgtid_bind_type(shost) = val;
15401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;
15411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1543ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
15441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_private_host_tgtid_bind_type,
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_private_host_tgtid_bind_type);
15461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
154791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquezstatic ssize_t
1548ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_private_host_issue_lip(struct device *dev,
1549ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *attr, const char *buf, size_t count)
155091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{
1551ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
155291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	struct fc_internal *i = to_fc_internal(shost->transportt);
155391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	int ret;
155491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
155591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	/* ignore any data value written to the attribute */
155691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (i->f->issue_fc_host_lip) {
155791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		ret = i->f->issue_fc_host_lip(shost);
155891ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		return ret ? ret: count;
155991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	}
156091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
156191ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	return -ENOENT;
156291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
156391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
1564ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
156591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez			store_fc_private_host_issue_lip);
156691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
1567a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1568a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1569a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
15701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
15711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Statistics Management
15721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
15731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Show a given an attribute in the statistics group */
15751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1576ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesfc_stat_show(const struct device *dev, char *buf, unsigned long offset)
15771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1578ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
15801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_host_statistics *stats;
15811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t ret = -ENOENT;
15821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (offset > sizeof(struct fc_host_statistics) ||
15841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    offset % sizeof(u64) != 0)
15851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		WARN_ON(1);
15861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_fc_host_stats) {
15881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		stats = (i->f->get_fc_host_stats)(shost);
15891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (stats)
15901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = snprintf(buf, 20, "0x%llx\n",
15911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
15921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
15941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* generate a read-only statistics attribute */
15981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_statistic(name)						\
1599ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic ssize_t show_fcstat_##name(struct device *cd,			\
1600ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				  struct device_attribute *attr,	\
1601ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				  char *buf)				\
16021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
16031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return fc_stat_show(cd, buf, 					\
16041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    offsetof(struct fc_host_statistics, name));	\
16051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
1606ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
16071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(seconds_since_last_reset);
16091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_frames);
16101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_words);
16111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_frames);
16121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_words);
16131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(lip_count);
16141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(nos_count);
16151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(error_frames);
16161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(dumped_frames);
16171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(link_failure_count);
16181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_sync_count);
16191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_signal_count);
16201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(prim_seq_protocol_err_count);
16211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_tx_word_count);
16221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_crc_count);
16231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_requests);
16241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_requests);
16251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_control_requests);
16261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_megabytes);
16271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_megabytes);
16281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1630ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesfc_reset_statistics(struct device *dev, struct device_attribute *attr,
1631ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		    const char *buf, size_t count)
16321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1633ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
16341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
16351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* ignore any data value written to the attribute */
16371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->reset_fc_host_stats) {
16381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->reset_fc_host_stats(shost);
16391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return count;
16401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -ENOENT;
16431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1644ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
16451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				fc_reset_statistics);
16461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute *fc_statistics_attrs[] = {
1648ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_seconds_since_last_reset.attr,
1649ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_tx_frames.attr,
1650ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_tx_words.attr,
1651ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_rx_frames.attr,
1652ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_rx_words.attr,
1653ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_lip_count.attr,
1654ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_nos_count.attr,
1655ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_error_frames.attr,
1656ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_dumped_frames.attr,
1657ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_link_failure_count.attr,
1658ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_loss_of_sync_count.attr,
1659ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_loss_of_signal_count.attr,
1660ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_prim_seq_protocol_err_count.attr,
1661ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_invalid_tx_word_count.attr,
1662ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_invalid_crc_count.attr,
1663ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_input_requests.attr,
1664ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_output_requests.attr,
1665ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_control_requests.attr,
1666ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_input_megabytes.attr,
1667ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_output_megabytes.attr,
1668ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_reset_statistics.attr,
16691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	NULL
16701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
16711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute_group fc_statistics_group = {
16731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name = "statistics",
16741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.attrs = fc_statistics_attrs,
16751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
16761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1677a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1678a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Host Vport Attributes */
1679a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1680a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int
1681a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_parse_wwn(const char *ns, u64 *nm)
1682a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1683a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int i, j;
1684a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u8 wwn[8];
1685a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1686a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memset(wwn, 0, sizeof(wwn));
1687a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1688a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* Validate and store the new name */
1689a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	for (i=0, j=0; i < 16; i++) {
1690a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if ((*ns >= 'a') && (*ns <= 'f'))
1691a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = ((j << 4) | ((*ns++ -'a') + 10));
1692a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		else if ((*ns >= 'A') && (*ns <= 'F'))
1693a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = ((j << 4) | ((*ns++ -'A') + 10));
1694a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		else if ((*ns >= '0') && (*ns <= '9'))
1695a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = ((j << 4) | (*ns++ -'0'));
1696a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		else
1697a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EINVAL;
1698a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (i % 2) {
1699a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			wwn[i/2] = j & 0xff;
1700a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = 0;
1701a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		}
1702a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
1703a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1704a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*nm = wwn_to_u64(wwn);
1705a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1706a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0;
1707a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1708a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1709a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1710a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1711a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * "Short-cut" sysfs variable to create a new vport on a FC Host.
1712a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1713a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1714a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1715a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1716a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1717ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1718ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   const char *buf, size_t count)
1719a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1720ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1721a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport_identifiers vid;
1722a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1723a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;
1724a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
1725a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1726a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memset(&vid, 0, sizeof(vid));
1727a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1728a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */
1729a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')
1730a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;
1731a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1732a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* validate we have enough characters for WWPN */
1733a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((cnt != (16+1+16)) || (buf[16] != ':'))
1734a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1735a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1736a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[0], &vid.port_name);
1737a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1738a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1739a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1740a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[17], &vid.node_name);
1741a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1742a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1743a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1744a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1745a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.vport_type = FC_PORTTYPE_NPIV;
1746a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* vid.symbolic_name is already zero/NULL's */
1747a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.disable = false;		/* always enabled */
1748a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1749a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* we only allow support on Channel 0 !!! */
1750a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1751a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1752a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1753ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1754a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_host_vport_create);
1755a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1756a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1757a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1758a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * "Short-cut" sysfs variable to delete a vport on a FC Host.
1759a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Vport is identified by a string containing "<WWPN>:<WWNN>".
1760a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * The WWNs are specified as hex characters, and may *not* contain
1761a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * any prefixes (e.g. 0x, x, etc)
1762a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1763a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1764ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1765ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   const char *buf, size_t count)
1766a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1767ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1768a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1769a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1770a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u64 wwpn, wwnn;
1771a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
1772a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;
1773a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat, match;
1774a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1775a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */
1776a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')
1777a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;
1778a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1779a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* validate we have enough characters for WWPN */
1780a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((cnt != (16+1+16)) || (buf[16] != ':'))
1781a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1782a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1783a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[0], &wwpn);
1784a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1785a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1786a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1787a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[17], &wwnn);
1788a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1789a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1790a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1791a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
1792a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	match = 0;
1793a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* we only allow support on Channel 0 !!! */
1794a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_for_each_entry(vport, &fc_host->vports, peers) {
1795a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if ((vport->channel == 0) &&
1796a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		    (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1797a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			match = 1;
1798a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			break;
1799a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		}
1800a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
1801a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
1802a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1803a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!match)
1804a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENODEV;
1805a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1806a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_vport_terminate(vport);
1807a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1808a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1809ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1810a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_host_vport_delete);
1811a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1812a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
18131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_host_match(struct attribute_container *cont,
18141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  struct device *dev)
18151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
18171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
18181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_host_device(dev))
18201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev);
18231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
18241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
18251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
18281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.host_attrs.ac == cont;
18301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_target_match(struct attribute_container *cont,
18331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
18341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
18361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
18371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_target_device(dev))
18391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
18421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
18431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
18441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
18471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.target_attrs.ac == cont;
18491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void fc_rport_dev_release(struct device *dev)
18521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = dev_to_rport(dev);
18541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
18551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
18561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint scsi_is_fc_rport(const struct device *dev)
18591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return dev->release == fc_rport_dev_release;
18611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(scsi_is_fc_rport);
18631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_rport_match(struct attribute_container *cont,
18651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
18661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
18681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
18691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_fc_rport(dev))
18711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
18741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
18751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
18761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
18791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->rport_attr_cont.ac == cont;
18811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18835c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
1884a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic void fc_vport_dev_release(struct device *dev)
1885a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1886a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport = dev_to_vport(dev);
1887a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev->parent);		/* release kobj parent */
1888a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	kfree(vport);
1889a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1890a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1891a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartint scsi_is_fc_vport(const struct device *dev)
1892a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1893a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return dev->release == fc_vport_dev_release;
1894a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1895a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James SmartEXPORT_SYMBOL(scsi_is_fc_vport);
1896a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1897a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int fc_vport_match(struct attribute_container *cont,
1898a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			    struct device *dev)
1899a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1900a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1901a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost;
1902a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i;
1903a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1904a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!scsi_is_fc_vport(dev))
1905a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return 0;
1906a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport = dev_to_vport(dev);
1907a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1908a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	shost = vport_to_shost(vport);
1909a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1910a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    != &fc_host_class.class)
1911a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return 0;
1912a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1913a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i = to_fc_internal(shost->transportt);
1914a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return &i->vport_attr_cont.ac == cont;
1915a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1916a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1917a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1918c829c394165f981d49f05a9be228404d7a9398d4James Smart/**
1919c829c394165f981d49f05a9be228404d7a9398d4James Smart * fc_timed_out - FC Transport I/O timeout intercept handler
1920c829c394165f981d49f05a9be228404d7a9398d4James Smart * @scmd:	The SCSI command which timed out
1921c829c394165f981d49f05a9be228404d7a9398d4James Smart *
1922c829c394165f981d49f05a9be228404d7a9398d4James Smart * This routine protects against error handlers getting invoked while a
1923c829c394165f981d49f05a9be228404d7a9398d4James Smart * rport is in a blocked state, typically due to a temporarily loss of
1924c829c394165f981d49f05a9be228404d7a9398d4James Smart * connectivity. If the error handlers are allowed to proceed, requests
1925c829c394165f981d49f05a9be228404d7a9398d4James Smart * to abort i/o, reset the target, etc will likely fail as there is no way
1926c829c394165f981d49f05a9be228404d7a9398d4James Smart * to communicate with the device to perform the requested function. These
1927c829c394165f981d49f05a9be228404d7a9398d4James Smart * failures may result in the midlayer taking the device offline, requiring
1928c829c394165f981d49f05a9be228404d7a9398d4James Smart * manual intervention to restore operation.
1929c829c394165f981d49f05a9be228404d7a9398d4James Smart *
1930c829c394165f981d49f05a9be228404d7a9398d4James Smart * This routine, called whenever an i/o times out, validates the state of
1931c829c394165f981d49f05a9be228404d7a9398d4James Smart * the underlying rport. If the rport is blocked, it returns
1932c829c394165f981d49f05a9be228404d7a9398d4James Smart * EH_RESET_TIMER, which will continue to reschedule the timeout.
1933c829c394165f981d49f05a9be228404d7a9398d4James Smart * Eventually, either the device will return, or devloss_tmo will fire,
1934c829c394165f981d49f05a9be228404d7a9398d4James Smart * and when the timeout then fires, it will be handled normally.
1935c829c394165f981d49f05a9be228404d7a9398d4James Smart * If the rport is not blocked, normal error handling continues.
1936c829c394165f981d49f05a9be228404d7a9398d4James Smart *
1937c829c394165f981d49f05a9be228404d7a9398d4James Smart * Notes:
1938c829c394165f981d49f05a9be228404d7a9398d4James Smart *	This routine assumes no locks are held on entry.
1939eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
1940242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboestatic enum blk_eh_timer_return
1941c829c394165f981d49f05a9be228404d7a9398d4James Smartfc_timed_out(struct scsi_cmnd *scmd)
1942c829c394165f981d49f05a9be228404d7a9398d4James Smart{
1943c829c394165f981d49f05a9be228404d7a9398d4James Smart	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1944c829c394165f981d49f05a9be228404d7a9398d4James Smart
1945c829c394165f981d49f05a9be228404d7a9398d4James Smart	if (rport->port_state == FC_PORTSTATE_BLOCKED)
1946242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe		return BLK_EH_RESET_TIMER;
1947c829c394165f981d49f05a9be228404d7a9398d4James Smart
1948242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe	return BLK_EH_NOT_HANDLED;
1949c829c394165f981d49f05a9be228404d7a9398d4James Smart}
1950c829c394165f981d49f05a9be228404d7a9398d4James Smart
19515c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com/*
1952bda232531f0c117921690ee3c060953c8f12e5a1James Smart * Called by fc_user_scan to locate an rport on the shost that
1953bda232531f0c117921690ee3c060953c8f12e5a1James Smart * matches the channel and target id, and invoke scsi_scan_target()
1954bda232531f0c117921690ee3c060953c8f12e5a1James Smart * on the rport.
19555c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com */
1956bda232531f0c117921690ee3c060953c8f12e5a1James Smartstatic void
1957bda232531f0c117921690ee3c060953c8f12e5a1James Smartfc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
19585c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com{
19595c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com	struct fc_rport *rport;
1960bda232531f0c117921690ee3c060953c8f12e5a1James Smart	unsigned long flags;
1961bda232531f0c117921690ee3c060953c8f12e5a1James Smart
1962bda232531f0c117921690ee3c060953c8f12e5a1James Smart	spin_lock_irqsave(shost->host_lock, flags);
19635c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
1964e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1965e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig		if (rport->scsi_target_id == -1)
1966e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig			continue;
19675c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
19680d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke		if (rport->port_state != FC_PORTSTATE_ONLINE)
19690d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke			continue;
19700d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke
1971bda232531f0c117921690ee3c060953c8f12e5a1James Smart		if ((channel == rport->channel) &&
1972bda232531f0c117921690ee3c060953c8f12e5a1James Smart		    (id == rport->scsi_target_id)) {
1973bda232531f0c117921690ee3c060953c8f12e5a1James Smart			spin_unlock_irqrestore(shost->host_lock, flags);
1974bda232531f0c117921690ee3c060953c8f12e5a1James Smart			scsi_scan_target(&rport->dev, channel, id, lun, 1);
1975bda232531f0c117921690ee3c060953c8f12e5a1James Smart			return;
1976e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig		}
1977e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	}
1978e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig
1979bda232531f0c117921690ee3c060953c8f12e5a1James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
1980bda232531f0c117921690ee3c060953c8f12e5a1James Smart}
1981bda232531f0c117921690ee3c060953c8f12e5a1James Smart
1982bda232531f0c117921690ee3c060953c8f12e5a1James Smart/*
1983bda232531f0c117921690ee3c060953c8f12e5a1James Smart * Called via sysfs scan routines. Necessary, as the FC transport
1984bda232531f0c117921690ee3c060953c8f12e5a1James Smart * wants to place all target objects below the rport object. So this
1985bda232531f0c117921690ee3c060953c8f12e5a1James Smart * routine must invoke the scsi_scan_target() routine with the rport
1986bda232531f0c117921690ee3c060953c8f12e5a1James Smart * object as the parent.
1987bda232531f0c117921690ee3c060953c8f12e5a1James Smart */
1988bda232531f0c117921690ee3c060953c8f12e5a1James Smartstatic int
1989bda232531f0c117921690ee3c060953c8f12e5a1James Smartfc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
1990bda232531f0c117921690ee3c060953c8f12e5a1James Smart{
1991bda232531f0c117921690ee3c060953c8f12e5a1James Smart	uint chlo, chhi;
1992bda232531f0c117921690ee3c060953c8f12e5a1James Smart	uint tgtlo, tgthi;
1993bda232531f0c117921690ee3c060953c8f12e5a1James Smart
1994bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1995bda232531f0c117921690ee3c060953c8f12e5a1James Smart	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1996bda232531f0c117921690ee3c060953c8f12e5a1James Smart	    ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1997bda232531f0c117921690ee3c060953c8f12e5a1James Smart		return -EINVAL;
1998bda232531f0c117921690ee3c060953c8f12e5a1James Smart
1999bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (channel == SCAN_WILD_CARD) {
2000bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chlo = 0;
2001bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chhi = shost->max_channel + 1;
2002bda232531f0c117921690ee3c060953c8f12e5a1James Smart	} else {
2003bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chlo = channel;
2004bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chhi = channel + 1;
2005bda232531f0c117921690ee3c060953c8f12e5a1James Smart	}
2006bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2007bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (id == SCAN_WILD_CARD) {
2008bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgtlo = 0;
2009bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgthi = shost->max_id;
2010bda232531f0c117921690ee3c060953c8f12e5a1James Smart	} else {
2011bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgtlo = id;
2012bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgthi = id + 1;
2013bda232531f0c117921690ee3c060953c8f12e5a1James Smart	}
2014bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2015bda232531f0c117921690ee3c060953c8f12e5a1James Smart	for ( ; chlo < chhi; chlo++)
2016bda232531f0c117921690ee3c060953c8f12e5a1James Smart		for ( ; tgtlo < tgthi; tgtlo++)
2017bda232531f0c117921690ee3c060953c8f12e5a1James Smart			fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2018bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2019e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	return 0;
20205c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com}
20215c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
20227525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonoristatic int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
20237525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori				int result)
20247525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori{
20257525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	struct fc_internal *i = to_fc_internal(shost->transportt);
20267525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
20277525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori}
20287525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
20297525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonoristatic int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
20307525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori{
20317525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	struct fc_internal *i = to_fc_internal(shost->transportt);
20327525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	return i->f->it_nexus_response(shost, nexus, result);
20337525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori}
20347525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
20351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_transport_template *
20361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_attach_transport(struct fc_function_template *ft)
20371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int count;
203924669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen	struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
204024669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen					GFP_KERNEL);
20411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!i))
20431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
20441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
20461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.class = &fc_transport_class.class;
20471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.match = fc_target_match;
20481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_size = sizeof(struct fc_starget_attrs);
20491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.target_attrs);
20501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
20521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.class = &fc_host_class.class;
20531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.match = fc_host_match;
20541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_size = sizeof(struct fc_host_attrs);
20551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ft->get_fc_host_stats)
20561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->t.host_attrs.statistics = &fc_statistics_group;
20571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.host_attrs);
20581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
20601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.class = &fc_rport_class.class;
20611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.match = fc_rport_match;
20621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->rport_attr_cont);
20631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2064a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2065a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.class = &fc_vport_class.class;
2066a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.match = fc_vport_match;
2067a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_container_register(&i->vport_attr_cont);
2068a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
20691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f = ft;
20701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport uses the shost workq for scsi scanning */
20721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.create_work_queue = 1;
20735c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
2074c829c394165f981d49f05a9be228404d7a9398d4James Smart	i->t.eh_timed_out = fc_timed_out;
2075c829c394165f981d49f05a9be228404d7a9398d4James Smart
2076e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	i->t.user_scan = fc_user_scan;
20779ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
20787525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	/* target-mode drivers' functions */
20797525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
20807525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	i->t.it_nexus_response = fc_it_nexus_response;
20817525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
20821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
20831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Target Attributes.
20841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
20851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count = 0;
20861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(node_name);
20871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_name);
20881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_id);
20891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_STARGET_NUM_ATTRS);
20911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = NULL;
20931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
20961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Host Attributes.
20971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
20981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
20991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(node_name);
21001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_name);
21016b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
21021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
21031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
21041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
21051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2106a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_create) {
2107a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2108a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2109a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
21101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(serial_number);
21111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_id);
21131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_type);
21141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_state);
21151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
21161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(speed);
21171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2118016131b8fffa1085b4ad165ab228116fdc278ebeJames Smart	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2119b8d08210126a7b769b857720a59721a453a57a1eJames Smart	SETUP_HOST_ATTRIBUTE_RW(system_hostname);
21201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport-managed attributes */
21221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
212391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (ft->issue_fc_host_lip)
212491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2125a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_create)
2126a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2127a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_delete)
2128a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
21291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_HOST_NUM_ATTRS);
21311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = NULL;
21331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
21351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup Remote Port Attributes.
21361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
21371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
21381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
21391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
21401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
21411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
21421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
21431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
21441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
21451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
21461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2147fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
21481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_RPORT_NUM_ATTRS);
21501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = NULL;
21521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2153a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
2154a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * Setup Virtual Port Attributes.
2155a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
2156a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count=0;
2157a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2158a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2159a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2162a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2163a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2164a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2166a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2167a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	BUG_ON(count > FC_VPORT_NUM_ATTRS);
2168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = NULL;
2170a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
21711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t;
21721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
21731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_attach_transport);
21741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid fc_release_transport(struct scsi_transport_template *t)
21761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
21771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(t);
21781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.target_attrs);
21801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.host_attrs);
21811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->rport_attr_cont);
2182a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_container_unregister(&i->vport_attr_cont);
21831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(i);
21851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
21861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_release_transport);
21871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2188aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2189aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_queue_work - Queue work to the fc_host workqueue.
2190aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2191aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @work:	Work to queue for execution.
2192aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart *
2193aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * Return value:
2194a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart * 	1 - work queued for execution
2195a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart *	0 - work is already queued
2196a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart *	-EINVAL - work queue doesn't exist
2197eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2198aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int
2199aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2200aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2201aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (unlikely(!fc_host_work_q(shost))) {
2202aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2203aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to queue work, "
2204aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2205aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2206aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2207aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -EINVAL;
2208aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2209aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2210aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	return queue_work(fc_host_work_q(shost), work);
2211aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2212aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2213aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2214aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_flush_work - Flush a fc_host's workqueue.
2215aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2216eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2217aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2218aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_flush_work(struct Scsi_Host *shost)
2219aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2220aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host_work_q(shost)) {
2221aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2222aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to flush work, "
2223aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2224aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2225aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return;
2226aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2227aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2228aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	flush_workqueue(fc_host_work_q(shost));
2229aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2230aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2231aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2232aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2233aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2234aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @work:	Work to queue for execution.
2235aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @delay:	jiffies to delay the work queuing
2236aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart *
2237aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * Return value:
22380f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * 	1 on success / 0 already queued / < 0 for error
2239eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2240aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int
2241c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2242aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				unsigned long delay)
2243aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2244aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (unlikely(!fc_host_devloss_work_q(shost))) {
2245aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2246aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to queue work, "
2247aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2248aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2249aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2250aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -EINVAL;
2251aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2252aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2253aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2254aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2255aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2256aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2257aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2258aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2259eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2260aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2261aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_flush_devloss(struct Scsi_Host *shost)
2262aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2263aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host_devloss_work_q(shost)) {
2264aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2265aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to flush work, "
2266aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2267aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2268aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return;
2269aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2270aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2271aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	flush_workqueue(fc_host_devloss_work_q(shost));
2272aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2273aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
22741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2276eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2277eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @shost:	Which &Scsi_Host
22781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is expected to be called immediately preceeding the
22801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a driver's call to scsi_remove_host().
22811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * WARNING: A driver utilizing the fc_transport, which fails to call
2283eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *   this routine prior to scsi_remove_host(), will leave dangling
22841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects in /sys/class/fc_remote_ports. Access to any of these
22851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects can result in a system crash !!!
22861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
22881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2289eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
22901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
22911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remove_host(struct Scsi_Host *shost)
22921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2293a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport = NULL, *next_vport = NULL;
2294a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_rport *rport = NULL, *next_rport = NULL;
2295aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct workqueue_struct *work_q;
2296aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2297a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
2298a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2299a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
2300a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2301a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* Remove any vports */
23029ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
23039ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		fc_queue_work(shost, &vport->vport_delete_work);
23041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Remove any remote ports */
23061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
2307aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			&fc_host->rports, peers) {
2308aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
2309aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
2310aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
2311aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2312aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
23131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
2314aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			&fc_host->rport_bindings, peers) {
2315aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
2316aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
2317aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
2318aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2319aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2320a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2321a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2322aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all scan work items */
2323aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	scsi_flush_work(shost);
2324aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2325aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all stgt delete, and rport delete work items, then kill it  */
2326aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->work_q) {
2327aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		work_q = fc_host->work_q;
2328aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->work_q = NULL;
2329aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(work_q);
2330aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2331aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2332aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all devloss work items, then kill it  */
2333aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->devloss_work_q) {
2334aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		work_q = fc_host->devloss_work_q;
2335aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->devloss_work_q = NULL;
2336aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(work_q);
2337aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
23381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
23391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remove_host);
23401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2341fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christiestatic void fc_terminate_rport_io(struct fc_rport *rport)
2342fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie{
2343fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	struct Scsi_Host *shost = rport_to_shost(rport);
2344fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	struct fc_internal *i = to_fc_internal(shost->transportt);
2345fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie
2346fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	/* Involve the LLDD if possible to terminate all io on the rport. */
2347fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	if (i->f->terminate_rport_io)
2348fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie		i->f->terminate_rport_io(rport);
2349fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie
2350fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	/*
2351fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 * must unblock to flush queued IO. The caller will have set
2352fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 * the port_state or flags, so that fc_remote_port_chkready will
2353fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 * fail IO.
2354fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 */
2355fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	scsi_target_unblock(&rport->dev);
2356fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie}
2357aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2358aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2359aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_starget_delete - called to delete the scsi decendents of an rport
2360c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be operated on.
2361eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
2362eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Deletes target and all sdevs.
2363eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
236419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic void
2365c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_starget_delete(struct work_struct *work)
236619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
2367c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2368c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, stgt_delete_work);
236919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2370fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
237119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_remove_target(&rport->dev);
237219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
237319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2374aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2375aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2376aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_rport_final_delete - finish rport termination and delete it.
2377c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be deleted.
2378eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2379aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2380c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_rport_final_delete(struct work_struct *work)
2381aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2382c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2383c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, rport_delete_work);
2384aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct device *dev = &rport->dev;
2385aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
23860f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
238792740b24ce6ddac6534fae985aab602548692186James Smart	unsigned long flags;
23888798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	int do_callback = 0;
2389aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2390aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/*
23919ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	 * if a scan is pending, flush the SCSI Host work_q so that
2392aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * that we can reclaim the rport scan work element.
2393aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 */
2394aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (rport->flags & FC_RPORT_SCAN_PENDING)
2395aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		scsi_flush_work(shost);
2396aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2397fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
23989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
239992740b24ce6ddac6534fae985aab602548692186James Smart	/*
240092740b24ce6ddac6534fae985aab602548692186James Smart	 * Cancel any outstanding timers. These should really exist
240192740b24ce6ddac6534fae985aab602548692186James Smart	 * only when rmmod'ing the LLDD and we're asking for
240292740b24ce6ddac6534fae985aab602548692186James Smart	 * immediate termination of the rports
240392740b24ce6ddac6534fae985aab602548692186James Smart	 */
240492740b24ce6ddac6534fae985aab602548692186James Smart	spin_lock_irqsave(shost->host_lock, flags);
240592740b24ce6ddac6534fae985aab602548692186James Smart	if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
240692740b24ce6ddac6534fae985aab602548692186James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
240792740b24ce6ddac6534fae985aab602548692186James Smart		if (!cancel_delayed_work(&rport->fail_io_work))
240892740b24ce6ddac6534fae985aab602548692186James Smart			fc_flush_devloss(shost);
240992740b24ce6ddac6534fae985aab602548692186James Smart		if (!cancel_delayed_work(&rport->dev_loss_work))
241092740b24ce6ddac6534fae985aab602548692186James Smart			fc_flush_devloss(shost);
241192740b24ce6ddac6534fae985aab602548692186James Smart		spin_lock_irqsave(shost->host_lock, flags);
241292740b24ce6ddac6534fae985aab602548692186James Smart		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
241392740b24ce6ddac6534fae985aab602548692186James Smart	}
241492740b24ce6ddac6534fae985aab602548692186James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
241592740b24ce6ddac6534fae985aab602548692186James Smart
24160f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	/* Delete SCSI target and sdevs */
24170f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->scsi_target_id != -1)
2418c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		fc_starget_delete(&rport->stgt_delete_work);
241992740b24ce6ddac6534fae985aab602548692186James Smart
242092740b24ce6ddac6534fae985aab602548692186James Smart	/*
242192740b24ce6ddac6534fae985aab602548692186James Smart	 * Notify the driver that the rport is now dead. The LLDD will
242292740b24ce6ddac6534fae985aab602548692186James Smart	 * also guarantee that any communication to the rport is terminated
24234be98c0ca304c8a47998b29a7993664f71791250James Smart	 *
24244be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Avoid this call if we already called it when we preserved the
24254be98c0ca304c8a47998b29a7993664f71791250James Smart	 * rport for the binding.
242692740b24ce6ddac6534fae985aab602548692186James Smart	 */
24278798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_lock_irqsave(shost->host_lock, flags);
24284be98c0ca304c8a47998b29a7993664f71791250James Smart	if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
24298798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	    (i->f->dev_loss_tmo_callbk)) {
24308798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
24318798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		do_callback = 1;
24328798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	}
24338798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_unlock_irqrestore(shost->host_lock, flags);
24348798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
24358798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	if (do_callback)
24360f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		i->f->dev_loss_tmo_callbk(rport);
24370f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
24389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_remove(rport->rqst_q);
24399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
2440aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	transport_remove_device(dev);
2441aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	device_del(dev);
2442aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	transport_destroy_device(dev);
24433bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
24443bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(dev);			/* for self-reference */
2445aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2446aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2447aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
24481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
24491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_rport_create - allocates and creates a remote FC port.
24501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
24511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
24521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
24531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
24541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
24551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Allocates and creates the remoter port structure, including the
24561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * class and sysfs creation.
24571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
24581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
24591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2460eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
246144818efbad185eea75adad0e8cab97650a7370abAdrian Bunkstatic struct fc_rport *
24621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_rport_create(struct Scsi_Host *shost, int channel,
24631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
24641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2465aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
24661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *fci = to_fc_internal(shost->transportt);
24671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
24681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct device *dev;
24691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
24701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int error;
24711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t size;
24721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
247424669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen	rport = kzalloc(size, GFP_KERNEL);
24751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!rport)) {
2476cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		printk(KERN_ERR "%s: allocation failure\n", __func__);
24771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
24781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
24791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->maxframe_size = -1;
24811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->supported_classes = FC_COS_UNSPECIFIED;
24821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->dev_loss_tmo = fc_dev_loss_tmo;
24831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
24841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
24851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_id = ids->port_id;
24861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->roles = ids->roles;
24871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_state = FC_PORTSTATE_ONLINE;
24881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fci->f->dd_fcrport_size)
24891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->dd_data = &rport[1];
24901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->channel = channel;
24910f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	rport->fast_io_fail_tmo = -1;
24921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2493c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2494c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2495c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2496c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2497c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
24981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
24991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
25001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->number = fc_host->next_rport_number++;
2502a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
25031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = fc_host->next_target_id++;
25041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
25051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = -1;
2506aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_add_tail(&rport->peers, &fc_host->rports);
25073bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	get_device(&shost->shost_gendev);	/* for fc_host->rport list */
25081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
25101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev = &rport->dev;
25123bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	device_initialize(dev);			/* takes self reference */
25133bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	dev->parent = get_device(&shost->shost_gendev); /* parent reference */
25141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev->release = fc_rport_dev_release;
251571610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers	dev_set_name(dev, "rport-%d:%d-%d",
251671610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		     shost->host_no, channel, rport->number);
25171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_setup_device(dev);
25181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = device_add(dev);
25201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error) {
25211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "FC Remote Port device_add failed\n");
25221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto delete_rport;
25231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
25241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_add_device(dev);
25251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_configure_device(dev);
25261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_rportadd(shost, rport);
25289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* ignore any bsg add error - we just can't do sgio */
25299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
2530a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
25311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
2532aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->flags |= FC_RPORT_SCAN_PENDING;
25331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
2534aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
25351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
25371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsdelete_rport:
25391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_destroy_device(dev);
25401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
25411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_del(&rport->peers);
25423bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
25431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
25441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
25451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
25461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
25471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
25481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2550eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
25511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
25521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
25531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
25541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
25551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport of the existence
25571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
25581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of the port, it's FC address (port_id), and the FC4 roles that are
25591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * active for the port.
25601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * For ports that are FCP targets (aka scsi targets), the FC transport
25621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * maintains consistent target id bindings on behalf of the LLDD.
25631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * A consistent target id binding is an assignment of a target id to
25641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a remote port identifier, which persists while the scsi host is
25651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * attached. The remote port can disappear, then later reappear, and
25661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it's target id assignment remains the same. This allows for shifts
25671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in FC addressing (if binding by wwpn or wwnn) with no apparent
25681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * changes to the scsi subsystem which is based on scsi host number and
25691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * target id values.  Bindings are only valid during the attachment of
25701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the scsi host. If the host detaches, then later re-attaches, target
25711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * id bindings may change.
25721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is responsible for returning a remote port structure.
25741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The routine will search the list of remote ports it maintains
25751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * internally on behalf of consistent target id mappings. If found, the
25761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * remote port structure will be reused. Otherwise, a new remote port
25771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * structure will be allocated.
25781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Whenever a remote port is allocated, a new fc_remote_port class
25801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device is created.
25811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
25831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
25851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2586eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
25871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_rport *
25881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_add(struct Scsi_Host *shost, int channel,
25891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
25901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
259119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *fci = to_fc_internal(shost->transportt);
2592aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
25931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
25941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
25951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int match = 0;
25961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2597aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* ensure any stgt delete functions are done */
2598aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_flush_work(shost);
2599aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
260019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
260119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Search the list of "active" rports, for an rport that has been
260219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * deleted, but we've held off the real delete while the target
260319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * is in a "blocked" state.
260419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
260519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
260619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2607aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_for_each_entry(rport, &fc_host->rports, peers) {
260819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
260919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
261019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			(rport->channel == channel)) {
261119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2612aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			switch (fc_host->tgtid_bind_type) {
261319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWPN:
261419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_NONE:
261519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_name == ids->port_name)
261619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
261719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
261819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWNN:
261919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->node_name == ids->node_name)
262019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
262119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
262219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_ID:
262319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_id == ids->port_id)
262419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
262519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
262619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
262719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
262819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (match) {
262919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
263019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->node_name, &ids->node_name,
263119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->node_name));
263219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->port_name, &ids->port_name,
263319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->port_name));
263419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_id = ids->port_id;
263519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
263619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_state = FC_PORTSTATE_ONLINE;
263719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->roles = ids->roles;
263819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
263919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				spin_unlock_irqrestore(shost->host_lock, flags);
264019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
264119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (fci->f->dd_fcrport_size)
264219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					memset(rport->dd_data, 0,
264319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
264419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
264519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
264692740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were not a target, cancel the
264792740b24ce6ddac6534fae985aab602548692186James Smart				 * io terminate and rport timers, and
264892740b24ce6ddac6534fae985aab602548692186James Smart				 * we're done.
264992740b24ce6ddac6534fae985aab602548692186James Smart				 *
265092740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were a target, but our new role
265192740b24ce6ddac6534fae985aab602548692186James Smart				 * doesn't indicate a target, leave the
265292740b24ce6ddac6534fae985aab602548692186James Smart				 * timers running expecting the role to
265392740b24ce6ddac6534fae985aab602548692186James Smart				 * change as the target fully logs in. If
265492740b24ce6ddac6534fae985aab602548692186James Smart				 * it doesn't, the target will be torn down.
265592740b24ce6ddac6534fae985aab602548692186James Smart				 *
265692740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were a target, and our role shows
265792740b24ce6ddac6534fae985aab602548692186James Smart				 * we're still a target, cancel the timers
265892740b24ce6ddac6534fae985aab602548692186James Smart				 * and kick off a scan.
265919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
266019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
266192740b24ce6ddac6534fae985aab602548692186James Smart				/* was a target, not in roles */
266292740b24ce6ddac6534fae985aab602548692186James Smart				if ((rport->scsi_target_id != -1) &&
2663a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				    (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
266492740b24ce6ddac6534fae985aab602548692186James Smart					return rport;
266519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
266619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
266792740b24ce6ddac6534fae985aab602548692186James Smart				 * Stop the fail io and dev_loss timers.
266892740b24ce6ddac6534fae985aab602548692186James Smart				 * If they flush, the port_state will
266992740b24ce6ddac6534fae985aab602548692186James Smart				 * be checked and will NOOP the function.
267019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
26710f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart				if (!cancel_delayed_work(&rport->fail_io_work))
26720f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart					fc_flush_devloss(shost);
267392740b24ce6ddac6534fae985aab602548692186James Smart				if (!cancel_delayed_work(&rport->dev_loss_work))
2674aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_flush_devloss(shost);
2675aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2676aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				spin_lock_irqsave(shost->host_lock, flags);
2677aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2678fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie				rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
26794be98c0ca304c8a47998b29a7993664f71791250James Smart						  FC_RPORT_DEVLOSS_PENDING |
26804be98c0ca304c8a47998b29a7993664f71791250James Smart						  FC_RPORT_DEVLOSS_CALLBK_DONE);
268119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
268292740b24ce6ddac6534fae985aab602548692186James Smart				/* if target, initiate a scan */
268392740b24ce6ddac6534fae985aab602548692186James Smart				if (rport->scsi_target_id != -1) {
268492740b24ce6ddac6534fae985aab602548692186James Smart					rport->flags |= FC_RPORT_SCAN_PENDING;
268592740b24ce6ddac6534fae985aab602548692186James Smart					scsi_queue_work(shost,
268692740b24ce6ddac6534fae985aab602548692186James Smart							&rport->scan_work);
268792740b24ce6ddac6534fae985aab602548692186James Smart					spin_unlock_irqrestore(shost->host_lock,
268892740b24ce6ddac6534fae985aab602548692186James Smart							flags);
268992740b24ce6ddac6534fae985aab602548692186James Smart					scsi_target_unblock(&rport->dev);
269092740b24ce6ddac6534fae985aab602548692186James Smart				} else
269192740b24ce6ddac6534fae985aab602548692186James Smart					spin_unlock_irqrestore(shost->host_lock,
269292740b24ce6ddac6534fae985aab602548692186James Smart							flags);
2693a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart
26949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				fc_bsg_goose_queue(rport);
26959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
269619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				return rport;
269719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
269819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		}
269919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
270019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
270192740b24ce6ddac6534fae985aab602548692186James Smart	/*
270292740b24ce6ddac6534fae985aab602548692186James Smart	 * Search the bindings array
270392740b24ce6ddac6534fae985aab602548692186James Smart	 * Note: if never a FCP target, you won't be on this list
270492740b24ce6ddac6534fae985aab602548692186James Smart	 */
2705aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
27061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* search for a matching consistent binding */
27081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2709aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_for_each_entry(rport, &fc_host->rport_bindings,
27101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					peers) {
27111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (rport->channel != channel)
27121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				continue;
27131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2714aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			switch (fc_host->tgtid_bind_type) {
27151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWPN:
27161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_name == ids->port_name)
27171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
27181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWNN:
27201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->node_name == ids->node_name)
27211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
27221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_ID:
27241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_id == ids->port_id)
27251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
27261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_NONE: /* to keep compiler happy */
27281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
27301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (match) {
2732aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				list_move_tail(&rport->peers, &fc_host->rports);
27331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
27351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
27361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (match) {
27381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->node_name, &ids->node_name,
27391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->node_name));
27401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->port_name, &ids->port_name,
27411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->port_name));
27421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_id = ids->port_id;
27431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->roles = ids->roles;
27441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_state = FC_PORTSTATE_ONLINE;
2745fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie			rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
27461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
274719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (fci->f->dd_fcrport_size)
274819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memset(rport->dd_data, 0,
274919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
275019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2751a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
27521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				/* initiate a scan of the target */
2753aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				rport->flags |= FC_RPORT_SCAN_PENDING;
27541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				scsi_queue_work(shost, &rport->scan_work);
2755a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				spin_unlock_irqrestore(shost->host_lock, flags);
2756a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				scsi_target_unblock(&rport->dev);
2757a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart			} else
2758a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				spin_unlock_irqrestore(shost->host_lock, flags);
27591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return rport;
27611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
27621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
27631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
276419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
276519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
27661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* No consistent binding found - create new remote port entry */
27671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport = fc_rport_create(shost, channel, ids);
27681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
27701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
27711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_add);
27721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2775eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
27761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that no longer exists
27771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
27781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport that a remote
27791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * port is no longer part of the topology. Note: Although a port
27801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * may no longer be part of the topology, it may persist in the remote
278119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * ports displayed by the fc_host. We do this under 2 conditions:
2782eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
278319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   This allows the port to temporarily disappear, then reappear without
278419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   disrupting the SCSI device tree attached to it. During the "blocked"
278519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   period the port will still exist.
2786eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * 2) If the port was a scsi target and disappears for longer than we
278719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   expect, we'll delete the port and the tear down the SCSI device tree
278819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   attached to it. However, we want to semi-persist the target id assigned
278919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to that port if it eventually does exist. The port structure will
279019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   remain (although with minimal information) so that the target id
279119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   bindings remails.
27921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
27931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If the remote port is not an FCP Target, it will be fully torn down
27941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and deallocated, including the fc_remote_port class device.
27951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
279619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * If the remote port is an FCP Target, the port will be placed in a
279719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * temporary blocked state. From the LLDD's perspective, the rport no
279819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * longer exists. From the SCSI midlayer's perspective, the SCSI target
279919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * exists, but all sdevs on it are blocked from further I/O. The following
2800eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * is then expected.
2801eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
280219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port does not return (signaled by a LLDD call to
280319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
280419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi target is removed - killing all outstanding i/o and removing the
280519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi devices attached ot it. The port structure will be marked Not
280619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   Present and be partially cleared, leaving only enough information to
280719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   recognize the remote port relative to the scsi target id binding if
280819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   it later appears.  The port will remain as long as there is a valid
280919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   binding (e.g. until the user changes the binding type or unloads the
281019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi host with the binding).
28111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
281219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port returns within the dev_loss_tmo value (and matches
281319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   according to the target id binding type), the port structure will be
281419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   reused. If it is no longer a SCSI target, the target will be torn
281519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   down. If it continues to be a SCSI target, then the target will be
281619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   unblocked (allowing i/o to be resumed), and a scan will be activated
281719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to ensure that all luns are detected.
281819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *
281919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * Called from normal process context only - cannot be called from interrupt.
28201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
28211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
28221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2823eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
28241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
28251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_delete(struct fc_rport  *rport)
28261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2827aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
282819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	int timeout = rport->dev_loss_tmo;
2829aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	unsigned long flags;
2830aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2831aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/*
2832aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * No need to flush the fc_host work_q's, as all adds are synchronous.
2833aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 *
2834aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * We do need to reclaim the rport scan work element, so eventually
2835aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2836aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * there's still a scan pending.
2837aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 */
2838aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2839aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_lock_irqsave(shost->host_lock, flags);
28401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
284192740b24ce6ddac6534fae985aab602548692186James Smart	if (rport->port_state != FC_PORTSTATE_ONLINE) {
2842aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
28431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
28441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
28451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
284692740b24ce6ddac6534fae985aab602548692186James Smart	/*
284792740b24ce6ddac6534fae985aab602548692186James Smart	 * In the past, we if this was not an FCP-Target, we would
284892740b24ce6ddac6534fae985aab602548692186James Smart	 * unconditionally just jump to deleting the rport.
284992740b24ce6ddac6534fae985aab602548692186James Smart	 * However, rports can be used as node containers by the LLDD,
285092740b24ce6ddac6534fae985aab602548692186James Smart	 * and its not appropriate to just terminate the rport at the
285192740b24ce6ddac6534fae985aab602548692186James Smart	 * first sign of a loss in connectivity. The LLDD may want to
285292740b24ce6ddac6534fae985aab602548692186James Smart	 * send ELS traffic to re-validate the login. If the rport is
285392740b24ce6ddac6534fae985aab602548692186James Smart	 * immediately deleted, it makes it inappropriate for a node
285492740b24ce6ddac6534fae985aab602548692186James Smart	 * container.
285592740b24ce6ddac6534fae985aab602548692186James Smart	 * So... we now unconditionally wait dev_loss_tmo before
285692740b24ce6ddac6534fae985aab602548692186James Smart	 * destroying an rport.
285792740b24ce6ddac6534fae985aab602548692186James Smart	 */
285892740b24ce6ddac6534fae985aab602548692186James Smart
2859aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->port_state = FC_PORTSTATE_BLOCKED;
2860aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2861aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2862aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2863aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2864aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
28657525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
28667525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	    shost->active_mode & MODE_TARGET)
28677525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
28687525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
286919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_target_block(&rport->dev);
28701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28710f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	/* see if we need to kill io faster than waiting for device loss */
28720f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((rport->fast_io_fail_tmo != -1) &&
2873fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	    (rport->fast_io_fail_tmo < timeout))
28740f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		fc_queue_devloss_work(shost, &rport->fail_io_work,
28750f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart					rport->fast_io_fail_tmo * HZ);
28760f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
287719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* cap the length the devices can be blocked until they are deleted */
2878aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
28791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
28801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_delete);
28811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2883eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
28841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that changed.
2885eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @roles:      New roles for this port.
28861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2887eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Description: The LLDD calls this routine to notify the transport that the
2888eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * roles on a remote port may have changed. The largest effect of this is
28891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * if a port now becomes a FCP Target, it must be allocated a
28901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id.  If the port is no longer a FCP target, any
28911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id value assigned to it will persist in case the
28921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * role changes back to include FCP Target. No changes in the scsi
28931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * midlayer will be invoked if the role changes (in the expectation
28941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that the role will be resumed. If it doesn't normal error processing
28951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * will take place).
28961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
28971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
28981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
28991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
29001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2901eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
29021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
29031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
29041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
29051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);
2906aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
29071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
29081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int create = 0;
29097525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	int ret;
29101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
2912a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (roles & FC_PORT_ROLE_FCP_TARGET) {
291319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (rport->scsi_target_id == -1) {
291419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			rport->scsi_target_id = fc_host->next_target_id++;
291519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
2916a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		} else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
291719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
29187525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	} else if (shost->active_mode & MODE_TARGET) {
29197525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
29207525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori					     (char *)&rport->node_name);
29217525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		if (ret)
29227525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori			printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
29237525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori			       ret);
29241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
29251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
292619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->roles = roles;
292719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2928aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2929aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
293019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (create) {
293119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		/*
293219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * There may have been a delete timer running on the
293319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * port. Ensure that it is cancelled as we now know
293419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * the port is an FCP Target.
293519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Note: we know the rport is exists and in an online
293619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  state as the LLDD would not have had an rport
293719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  reference to pass us.
293819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *
293919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Take no action on the del_timer failure as the state
294019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * machine state change will validate the
294119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * transaction.
294219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 */
29430f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		if (!cancel_delayed_work(&rport->fail_io_work))
29440f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart			fc_flush_devloss(shost);
294519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (!cancel_delayed_work(&rport->dev_loss_work))
2946aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			fc_flush_devloss(shost);
2947aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2948aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_lock_irqsave(shost->host_lock, flags);
2949fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie		rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2950fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie				  FC_RPORT_DEVLOSS_PENDING);
2951aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
2952aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2953aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		/* ensure any stgt delete functions are done */
2954aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_flush_work(shost);
295519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
29561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
2957aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_lock_irqsave(shost->host_lock, flags);
2958aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->flags |= FC_RPORT_SCAN_PENDING;
29591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
2960aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
2961a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		scsi_target_unblock(&rport->dev);
296219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
29631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
29641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_rolechg);
29651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2967eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
296892740b24ce6ddac6534fae985aab602548692186James Smart * @work:	rport target that failed to reappear in the allotted time.
2969eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
2970eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Description: An attempt to delete a remote port blocks, and if it fails
2971eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *              to return in the allotted time this gets called.
2972eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
29731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
2974c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_timeout_deleted_rport(struct work_struct *work)
29751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2976c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2977c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, dev_loss_work.work);
297819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
29794be98c0ca304c8a47998b29a7993664f71791250James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
2980aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
298119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	unsigned long flags;
29828798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	int do_callback = 0;
29831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
298419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
29851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2986aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2987aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
29881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
298992740b24ce6ddac6534fae985aab602548692186James Smart	 * If the port is ONLINE, then it came back. If it was a SCSI
299092740b24ce6ddac6534fae985aab602548692186James Smart	 * target, validate it still is. If not, tear down the
299192740b24ce6ddac6534fae985aab602548692186James Smart	 * scsi_target on it.
29921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
2993aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
299492740b24ce6ddac6534fae985aab602548692186James Smart	    (rport->scsi_target_id != -1) &&
2995a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
299619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
2997aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"blocked FC remote port time out: no longer"
2998aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			" a FCP target, removing starget\n");
2999aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3000a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		scsi_target_unblock(&rport->dev);
3001a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		fc_queue_work(shost, &rport->stgt_delete_work);
300219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
300319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
30041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
300592740b24ce6ddac6534fae985aab602548692186James Smart	/* NOOP state - we're flushing workq's */
300619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
300719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		spin_unlock_irqrestore(shost->host_lock, flags);
300819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
300992740b24ce6ddac6534fae985aab602548692186James Smart			"blocked FC remote port time out: leaving"
301092740b24ce6ddac6534fae985aab602548692186James Smart			" rport%s alone\n",
301192740b24ce6ddac6534fae985aab602548692186James Smart			(rport->scsi_target_id != -1) ?  " and starget" : "");
301219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
301319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
30141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
301592740b24ce6ddac6534fae985aab602548692186James Smart	if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
301692740b24ce6ddac6534fae985aab602548692186James Smart	    (rport->scsi_target_id == -1)) {
3017aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
3018aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
301919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
302092740b24ce6ddac6534fae985aab602548692186James Smart			"blocked FC remote port time out: removing"
302192740b24ce6ddac6534fae985aab602548692186James Smart			" rport%s\n",
302292740b24ce6ddac6534fae985aab602548692186James Smart			(rport->scsi_target_id != -1) ?  " and starget" : "");
3023aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
3024aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
302519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
302619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
30271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
302819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	dev_printk(KERN_ERR, &rport->dev,
302919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"blocked FC remote port time out: removing target and "
303019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"saving binding\n");
303119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
3032aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_move_tail(&rport->peers, &fc_host->rport_bindings);
30331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
303519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Note: We do not remove or clear the hostdata area. This allows
303619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   host-specific target data to persist along with the
303719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   scsi_target_id. It's up to the host to manage it's hostdata area.
30381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
30391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
304019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
304119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Reinitialize port attributes that may change if the port comes back.
304219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
304319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->maxframe_size = -1;
304419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->supported_classes = FC_COS_UNSPECIFIED;
3045a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	rport->roles = FC_PORT_ROLE_UNKNOWN;
3046aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->port_state = FC_PORTSTATE_NOTPRESENT;
3047fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
30481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3049f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	/*
3050f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * Pre-emptively kill I/O rather than waiting for the work queue
3051f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * item to teardown the starget. (FCOE libFC folks prefer this
3052f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * and to have the rport_port_id still set when it's done).
3053f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 */
3054f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3055f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	fc_terminate_rport_io(rport);
3056f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart
30578798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_lock_irqsave(shost->host_lock, flags);
30588798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
30598798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	if (rport->port_state == FC_PORTSTATE_NOTPRESENT) {	/* still missing */
30608798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
30618798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		/* remove the identifiers that aren't used in the consisting binding */
30628798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		switch (fc_host->tgtid_bind_type) {
30638798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_BY_WWPN:
30648798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->node_name = -1;
30658798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_id = -1;
30668798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
30678798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_BY_WWNN:
30688798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_name = -1;
30698798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_id = -1;
30708798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
30718798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_BY_ID:
30728798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->node_name = -1;
30738798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_name = -1;
30748798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
30758798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_NONE:	/* to keep compiler happy */
30768798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
30778798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		}
30788798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
30798798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		/*
30808798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 * As this only occurs if the remote port (scsi target)
30818798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 * went away and didn't come back - we'll remove
30828798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 * all attached scsi devices.
30838798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 */
30848798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
30858798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		fc_queue_work(shost, &rport->stgt_delete_work);
30868798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
30878798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		do_callback = 1;
308819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
308919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
30908798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_unlock_irqrestore(shost->host_lock, flags);
30914be98c0ca304c8a47998b29a7993664f71791250James Smart
30924be98c0ca304c8a47998b29a7993664f71791250James Smart	/*
30934be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Notify the driver that the rport is now dead. The LLDD will
30944be98c0ca304c8a47998b29a7993664f71791250James Smart	 * also guarantee that any communication to the rport is terminated
30954be98c0ca304c8a47998b29a7993664f71791250James Smart	 *
30964be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Note: we set the CALLBK_DONE flag above to correspond
30974be98c0ca304c8a47998b29a7993664f71791250James Smart	 */
30988798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	if (do_callback && i->f->dev_loss_tmo_callbk)
30994be98c0ca304c8a47998b29a7993664f71791250James Smart		i->f->dev_loss_tmo_callbk(rport);
31001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
31011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
31024be98c0ca304c8a47998b29a7993664f71791250James Smart
31031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
3104eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3105c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	rport to terminate io on.
31060f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart *
31070f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * Notes: Only requests the failure of the io, not that all are flushed
31080f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart *    prior to returning.
3109eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
31100f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic void
3111c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_timeout_fail_rport_io(struct work_struct *work)
31120f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
3113c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3114c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, fail_io_work.work);
31150f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
31160f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->port_state != FC_PORTSTATE_BLOCKED)
31170f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return;
31180f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
3119fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3120fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
31210f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
31220f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
31230f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart/**
31241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3125c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be scanned.
3126eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
31271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
3128c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_scsi_scan_rport(struct work_struct *work)
31291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3130c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3131c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, scan_work);
3132aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
313303f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	struct fc_internal *i = to_fc_internal(shost->transportt);
313442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	unsigned long flags;
313542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
3136aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
313703f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	    (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
313803f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	    !(i->f->disable_target_scan)) {
3139aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		scsi_scan_target(&rport->dev, rport->channel,
3140aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			rport->scsi_target_id, SCAN_WILD_CARD, 1);
314142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	}
3142aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
3143aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_lock_irqsave(shost->host_lock, flags);
3144aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags &= ~FC_RPORT_SCAN_PENDING;
314542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
314642e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com}
314742e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
314865d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt/**
314965d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
315065d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * @cmnd: SCSI command that scsi_eh is trying to recover
315165d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt *
315265d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * This routine can be called from a FC LLD scsi_eh callback. It
315365d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * blocks the scsi_eh thread until the fc_rport leaves the
315465d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * FC_PORTSTATE_BLOCKED. This is necessary to avoid the scsi_eh
315565d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * failing recovery actions for blocked rports which would lead to
315665d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * offlined SCSI devices.
315765d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt */
315865d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmittvoid fc_block_scsi_eh(struct scsi_cmnd *cmnd)
315965d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt{
316065d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	struct Scsi_Host *shost = cmnd->device->host;
316165d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
316265d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	unsigned long flags;
316365d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt
316465d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	spin_lock_irqsave(shost->host_lock, flags);
316565d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	while (rport->port_state == FC_PORTSTATE_BLOCKED) {
316665d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt		spin_unlock_irqrestore(shost->host_lock, flags);
316765d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt		msleep(1000);
316865d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt		spin_lock_irqsave(shost->host_lock, flags);
316965d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	}
317065d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	spin_unlock_irqrestore(shost->host_lock, flags);
317165d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt}
317265d430fa99cbd0e88d09a3343f697c51fc8a7009Christof SchmittEXPORT_SYMBOL(fc_block_scsi_eh);
317342e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
3174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/**
3175a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * fc_vport_setup - allocates and creates a FC virtual port.
3176a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @shost:	scsi host the virtual port is connected to.
3177a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @channel:	Channel on shost port connected to.
3178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @pdev:	parent device for vport
3179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @ids:	The world wide names, FC4 port roles, etc for
3180a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *              the virtual port.
3181a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @ret_vport:	The pointer to the created vport.
3182a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3183a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Allocates and creates the vport structure, calls the parent host
3184a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * to instantiate the vport, the completes w/ class and sysfs creation.
3185a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3186a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Notes:
3187a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *	This routine assumes no locks are held on entry.
3188eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
3189a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int
3190a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezfc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3191a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport_identifiers  *ids, struct fc_vport **ret_vport)
3192a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
3193a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3194a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *fci = to_fc_internal(shost->transportt);
3195a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
3196a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *dev;
3197a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
3198a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	size_t size;
3199a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int error;
3200a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3201a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*ret_vport = NULL;
3202a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3203a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ( ! fci->f->vport_create)
3204a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOENT;
3205a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3206a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3207a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport = kzalloc(size, GFP_KERNEL);
3208a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (unlikely(!vport)) {
3209cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		printk(KERN_ERR "%s: allocation failure\n", __func__);
3210a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOMEM;
3211a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3212a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3213a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_state = FC_VPORT_UNKNOWN;
3214a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_last_state = FC_VPORT_UNKNOWN;
3215a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->node_name = ids->node_name;
3216a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->port_name = ids->port_name;
3217a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->roles = ids->roles;
3218a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_type = ids->vport_type;
3219a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (fci->f->dd_fcvport_size)
3220a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		vport->dd_data = &vport[1];
3221a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->shost = shost;
3222a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->channel = channel;
3223a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags = FC_VPORT_CREATING;
32249ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3225a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3226a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3227a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3228a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3229a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3230a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		kfree(vport);
3231a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOSPC;
3232a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3233a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse++;
3234a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->number = fc_host->next_vport_number++;
3235a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_add_tail(&vport->peers, &fc_host->vports);
3236a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	get_device(&shost->shost_gendev);	/* for fc_host->vport list */
3237a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3238a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3239a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3240a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev = &vport->dev;
3241a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_initialize(dev);			/* takes self reference */
3242a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev->parent = get_device(pdev);		/* takes parent reference */
3243a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev->release = fc_vport_dev_release;
324471610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers	dev_set_name(dev, "vport-%d:%d-%d",
324571610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		     shost->host_no, channel, vport->number);
3246a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_setup_device(dev);
3247a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3248a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = device_add(dev);
3249a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error) {
3250a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		printk(KERN_ERR "FC Virtual Port device_add failed\n");
3251a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		goto delete_vport;
3252a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3253a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_add_device(dev);
3254a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_configure_device(dev);
3255a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3256a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = fci->f->vport_create(vport, ids->disable);
3257a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error) {
3258a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3259a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		goto delete_vport_all;
3260a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3261a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3262a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
3263a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * if the parent isn't the physical adapter's Scsi_Host, ensure
3264a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * the Scsi_Host at least contains ia symlink to the vport.
3265a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
3266a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (pdev != &shost->shost_gendev) {
3267a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		error = sysfs_create_link(&shost->shost_gendev.kobj,
326871610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers				 &dev->kobj, dev_name(dev));
3269a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (error)
3270a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			printk(KERN_ERR
3271a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				"%s: Cannot create vport symlinks for "
3272a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				"%s, err=%d\n",
327371610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers				__func__, dev_name(dev), error);
3274a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3275a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3276a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags &= ~FC_VPORT_CREATING;
3277a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3278a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3279a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev_printk(KERN_NOTICE, pdev,
328071610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers			"%s created via shost%d channel %d\n", dev_name(dev),
3281a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			shost->host_no, channel);
3282a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3283a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*ret_vport = vport;
3284a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3285a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0;
3286a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3287a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartdelete_vport_all:
3288a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_remove_device(dev);
3289a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_del(dev);
3290a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartdelete_vport:
3291a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_destroy_device(dev);
3292a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3293a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_del(&vport->peers);
3294a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(&shost->shost_gendev);	/* for fc_host->vport list */
3295a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse--;
3296a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3297a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev->parent);
3298a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	kfree(vport);
3299a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3300a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return error;
3301a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
3302a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3303a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez/**
3304a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * fc_vport_create - Admin App or LLDD requests creation of a vport
3305a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @shost:	scsi host the virtual port is connected to.
3306a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @channel:	channel on shost port connected to.
3307a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @ids:	The world wide names, FC4 port roles, etc for
3308a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *              the virtual port.
3309a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *
3310a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * Notes:
3311a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *	This routine assumes no locks are held on entry.
3312a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez */
3313a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezstruct fc_vport *
3314a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezfc_vport_create(struct Scsi_Host *shost, int channel,
3315a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	struct fc_vport_identifiers *ids)
3316a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez{
3317a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	int stat;
3318a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	struct fc_vport *vport;
3319a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez
3320a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3321a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez		 ids, &vport);
3322a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	return stat ? NULL : vport;
3323a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez}
3324a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew VasquezEXPORT_SYMBOL(fc_vport_create);
3325a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3326a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/**
3327a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3328a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @vport:	fc_vport to be terminated
3329a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3330a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Calls the LLDD vport_delete() function, then deallocates and removes
3331a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * the vport from the shost and object tree.
3332a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3333a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Notes:
3334a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *	This routine assumes no locks are held on entry.
3335eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
3336a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartint
3337a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_vport_terminate(struct fc_vport *vport)
3338a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
3339a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);
3340a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3341a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
3342a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *dev = &vport->dev;
3343a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
3344a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
3345a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3346a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3347a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & FC_VPORT_CREATING) {
3348a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3349a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;
3350a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3351a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL)) {
3352a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3353a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EALREADY;
3354a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3355a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags |= FC_VPORT_DELETING;
3356a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3357a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3358a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->vport_delete)
3359a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		stat = i->f->vport_delete(vport);
3360a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	else
3361a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		stat = -ENOENT;
3362a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3363a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3364a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags &= ~FC_VPORT_DELETING;
3365a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!stat) {
3366a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		vport->flags |= FC_VPORT_DELETED;
3367a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		list_del(&vport->peers);
3368a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		fc_host->npiv_vports_inuse--;
3369a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		put_device(&shost->shost_gendev);  /* for fc_host->vport list */
3370a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3371a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3372a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3373a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
3374a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
3375a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3376a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (dev->parent != &shost->shost_gendev)
337771610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3378a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_remove_device(dev);
3379a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_del(dev);
3380a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_destroy_device(dev);
3381a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3382a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
3383a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * Removing our self-reference should mean our
3384a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * release function gets called, which will drop the remaining
3385a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * parent reference and free the data structure.
3386a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
3387a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev);			/* for self-reference */
3388a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3389a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0; /* SUCCESS */
3390a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
3391a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James SmartEXPORT_SYMBOL(fc_vport_terminate);
3392a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
33939ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/**
33949ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart * fc_vport_sched_delete - workq-based delete request for a vport
33959ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart * @work:	vport to be deleted.
3396eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
33979ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartstatic void
33989ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartfc_vport_sched_delete(struct work_struct *work)
33999ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart{
34009ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	struct fc_vport *vport =
34019ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		container_of(work, struct fc_vport, vport_delete_work);
34029ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	int stat;
34039ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
34049ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	stat = fc_vport_terminate(vport);
34059ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	if (stat)
34069ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		dev_printk(KERN_ERR, vport->dev.parent,
34079ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart			"%s: %s could not be deleted created via "
3408cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison			"shost%d channel %d - error %d\n", __func__,
340971610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers			dev_name(&vport->dev), vport->shost->host_no,
34109ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart			vport->channel, stat);
34119ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart}
34129ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
3413a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
34149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/*
34159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * BSG support
34169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
34179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
34209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
34219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	fc_bsg_job that is to be torn down
34229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
34239e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
34249e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_destroy_bsgjob(struct fc_bsg_job *job)
34259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
34269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	unsigned long flags;
34279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irqsave(&job->job_lock, flags);
34299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->ref_cnt) {
34309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		spin_unlock_irqrestore(&job->job_lock, flags);
34319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
34329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
34339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irqrestore(&job->job_lock, flags);
34349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(job->dev);	/* release reference for the request */
34369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->request_payload.sg_list);
34389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->reply_payload.sg_list);
34399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job);
34409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
34419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
34439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
34449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart *                  completed
34459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	fc_bsg_job that is complete
34469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
34479e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
34489e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_jobdone(struct fc_bsg_job *job)
34499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
34509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req = job->req;
34519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *rsp = req->next_rq;
34529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
34539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = job->req->errors = job->reply->result;
3455b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali
34569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err < 0)
34579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* we're only returning the result field in the reply */
34589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->req->sense_len = sizeof(uint32_t);
34599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
34609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->req->sense_len = job->reply_len;
34619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* we assume all request payload was transferred, residual == 0 */
34639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	req->resid_len = 0;
34649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rsp) {
34669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
34679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* set reply (bidi) residual */
34699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
34709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				      rsp->resid_len);
34719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
3472b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_complete_request(req);
3473b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali}
3474b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali
3475b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali/**
3476b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
3477fe5d20c818a8c5fe83d9f2223a051fb5bc50d180Randy Dunlap * @rq:        BSG request that holds the job to be destroyed
3478b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali */
3479b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavalistatic void fc_bsg_softirq_done(struct request *rq)
3480b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali{
3481b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	struct fc_bsg_job *job = rq->special;
3482b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	unsigned long flags;
34839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
3484b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	spin_lock_irqsave(&job->job_lock, flags);
3485b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	job->state_flags |= FC_RQST_STATE_DONE;
3486b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	job->ref_cnt--;
3487b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	spin_unlock_irqrestore(&job->job_lock, flags);
34889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
3489b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_end_request_all(rq, rq->errors);
34909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_destroy_bsgjob(job);
34919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
34929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
34949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_job_timeout - handler for when a bsg request timesout
34959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @req:	request that timed out
34969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
34979e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum blk_eh_timer_return
34989e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_job_timeout(struct request *req)
34999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
35009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job = (void *) req->special;
35019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = job->shost;
35029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
35039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	unsigned long flags;
35049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err = 0, done = 0;
35059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
35079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return BLK_EH_RESET_TIMER;
35089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irqsave(&job->job_lock, flags);
35109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->state_flags & FC_RQST_STATE_DONE)
35119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		done = 1;
35129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
35139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->ref_cnt++;
35149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irqrestore(&job->job_lock, flags);
35159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!done && i->f->bsg_timeout) {
35179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* call LLDD to abort the i/o as it has timed out */
35189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		err = i->f->bsg_timeout(job);
35199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (err)
35209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
35219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"abort failed with status %d\n", err);
35229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
35239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* the blk_end_sync_io() doesn't check the error */
352547e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali	if (done)
352647e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali		return BLK_EH_NOT_HANDLED;
352747e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali	else
352847e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali		return BLK_EH_HANDLED;
35299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
35309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35319e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
35329e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
35339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
35349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
35359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(!req->nr_phys_segments);
35379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->sg_list = kzalloc(sz, GFP_KERNEL);
35399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!buf->sg_list)
35409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
35419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	sg_init_table(buf->sg_list, req->nr_phys_segments);
35429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
35439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->payload_len = blk_rq_bytes(req);
35449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
35459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
35469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
35499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
35509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart *                   bsg request
35519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	SCSI Host corresponding to the bsg object
35529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	(optional) FC Remote Port corresponding to the bsg object
35539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @req:	BSG request that needs a job structure
35549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
35559e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
35569e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
35579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req)
35589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
35599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
35609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *rsp = req->next_rq;
35619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job;
35629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
35639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(req->special);
35659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
35679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			GFP_KERNEL);
35689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!job)
35699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
35709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/*
35729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * Note: this is a bit silly.
35739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * The request gets formatted as a SGIO v4 ioctl request, which
35749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * then gets reformatted as a blk request, which then gets
35759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * reformatted as a fc bsg request. And on completion, we have
35769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * to wrap return results such that SGIO v4 thinks it was a scsi
35779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * status.  I hope this was all worth it.
35789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 */
35799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	req->special = job;
35819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->shost = shost;
35829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->rport = rport;
35839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->req = req;
35849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (i->f->dd_bsg_size)
35859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dd_data = (void *)&job[1];
35869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_init(&job->job_lock);
35879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->request = (struct fc_bsg_request *)req->cmd;
35889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->request_len = req->cmd_len;
35899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply = req->sense;
35909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = SCSI_SENSE_BUFFERSIZE;	/* Size of sense buffer
35919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart						 * allocated */
35929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (req->bio) {
35939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_bsg_map_buffer(&job->request_payload, req);
35949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret)
35959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto failjob_rls_job;
35969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
35979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rsp && rsp->bio) {
35989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
35999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret)
36009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto failjob_rls_rqst_payload;
36019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
36029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->job_done = fc_bsg_jobdone;
36039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rport)
36049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dev = &rport->dev;
36059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
36069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dev = &shost->shost_gendev;
36079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	get_device(job->dev);		/* take a reference for the request */
36089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->ref_cnt = 1;
36109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
36129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36149e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfailjob_rls_rqst_payload:
36159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->request_payload.sg_list);
36169e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfailjob_rls_job:
36179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job);
36189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return -ENOMEM;
36199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
36209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36229e4f5e29610162fd426366f3b29e3cc6e575b858James Smartenum fc_dispatch_result {
36239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_BREAK,	/* on return, q is locked, break from q loop */
36249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_LOCKED,	/* on return, q is locked, continue on */
36259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_UNLOCKED,	/* on return, q is unlocked, continue on */
36269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart};
36279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
36309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
36315415907af1f5ef80c95147bacbd321b0d4236dd5Randy Dunlap * @q:		fc host request queue
36329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	scsi host rport attached to
36339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	bsg job to be processed
36349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
36359e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum fc_dispatch_result
36369e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
36379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct fc_bsg_job *job)
36389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
36399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
36409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
36419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
36429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* Validate the host command */
36449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	switch (job->request->msgcode) {
36459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_ADD_RPORT:
36469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_add_rport);
36479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_DEL_RPORT:
36509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_del_rport);
36519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_ELS_NOLOGIN:
36549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_els);
36559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be a xmt and rcv payloads */
36569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
36579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
36589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
36599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
36609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
36619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_CT:
36649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_ct);
36659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be xmt and rcv payloads */
36669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
36679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
36689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
36699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
36709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
36719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_VENDOR:
36749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_vendor);
36759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((shost->hostt->vendor_id == 0L) ||
36769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (job->request->rqst_data.h_vendor.vendor_id !=
36779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			shost->hostt->vendor_id)) {
36789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -ESRCH;
36799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
36809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
36819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	default:
36849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -EBADR;
36859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_host_msg;
36869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
36879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* check if we really have all the request data needed */
36899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->request_len < cmdlen) {
36909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -ENOMSG;
36919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_host_msg;
36929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
36939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	ret = i->f->bsg_request(job);
36959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!ret)
36969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return FC_DISPATCH_UNLOCKED;
36979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36989e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfail_host_msg:
36999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* return the errno failure code as the only status */
37009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(job->reply_len < sizeof(uint32_t));
37019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply->result = ret;
37029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = sizeof(uint32_t);
37039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_jobdone(job);
37049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return FC_DISPATCH_UNLOCKED;
37059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
37069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/*
37099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_goose_queue - restart rport queue in case it was stopped
37109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport to be restarted
37119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
37129e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
37139e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_goose_queue(struct fc_rport *rport)
37149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
37159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int flagset;
371639562e783928e3ea9ee2cbce99a756ab48d3c06aChristof Schmitt	unsigned long flags;
37179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!rport->rqst_q)
37199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
37209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	get_device(&rport->dev);
37229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
372339562e783928e3ea9ee2cbce99a756ab48d3c06aChristof Schmitt	spin_lock_irqsave(rport->rqst_q->queue_lock, flags);
37249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	flagset = test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags) &&
37259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		  !test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags);
37269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (flagset)
37279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		queue_flag_set(QUEUE_FLAG_REENTER, rport->rqst_q);
37289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	__blk_run_queue(rport->rqst_q);
37299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (flagset)
37309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		queue_flag_clear(QUEUE_FLAG_REENTER, rport->rqst_q);
373139562e783928e3ea9ee2cbce99a756ab48d3c06aChristof Schmitt	spin_unlock_irqrestore(rport->rqst_q->queue_lock, flags);
37329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(&rport->dev);
37349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
37359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
37389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
37395415907af1f5ef80c95147bacbd321b0d4236dd5Randy Dunlap * @q:		rport request queue
37409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	scsi host rport attached to
37419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport request destined to
37429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	bsg job to be processed
37439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
37449e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum fc_dispatch_result
37459e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
37469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct fc_rport *rport, struct fc_bsg_job *job)
37479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
37489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
37499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
37509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
37519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* Validate the rport command */
37539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	switch (job->request->msgcode) {
37549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_RPT_ELS:
37559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_rport_els);
37569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto check_bidi;
37579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_RPT_CT:
37599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_rport_ct);
37609e4f5e29610162fd426366f3b29e3cc6e575b858James Smartcheck_bidi:
37619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be xmt and rcv payloads */
37629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
37639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
37649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
37659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_rport_msg;
37669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
37679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
37689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	default:
37699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -EBADR;
37709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_rport_msg;
37719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
37729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* check if we really have all the request data needed */
37749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->request_len < cmdlen) {
37759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -ENOMSG;
37769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_rport_msg;
37779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
37789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	ret = i->f->bsg_request(job);
37809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!ret)
37819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return FC_DISPATCH_UNLOCKED;
37829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37839e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfail_rport_msg:
37849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* return the errno failure code as the only status */
37859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(job->reply_len < sizeof(uint32_t));
37869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply->result = ret;
37879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = sizeof(uint32_t);
37889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_jobdone(job);
37899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return FC_DISPATCH_UNLOCKED;
37909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
37919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
37949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_request_handler - generic handler for bsg requests
37959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		request queue to manage
37969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	Scsi_Host related to the bsg object
37979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	FC remote port related to the bsg object (optional)
37989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @dev:	device structure for bsg object
37999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
38009e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
38019e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
38029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		       struct fc_rport *rport, struct device *dev)
38039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
38049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req;
38059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job;
38069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	enum fc_dispatch_result ret;
38079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!get_device(dev))
38099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
38109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	while (!blk_queue_plugged(q)) {
38122bc1c59dbdefdb6f9767e06efb86bbdb2923a8beMike Christie		if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
38132bc1c59dbdefdb6f9767e06efb86bbdb2923a8beMike Christie		    !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
38142bc1c59dbdefdb6f9767e06efb86bbdb2923a8beMike Christie			break;
38159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		req = blk_fetch_request(q);
38179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (!req)
38189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			break;
38199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
38219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			req->errors = -ENXIO;
38229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_unlock_irq(q->queue_lock);
38239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			blk_end_request(req, -ENXIO, blk_rq_bytes(req));
38249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
38259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
38269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		spin_unlock_irq(q->queue_lock);
38299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_req_to_bsgjob(shost, rport, req);
38319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret) {
38329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			req->errors = ret;
38339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			blk_end_request(req, ret, blk_rq_bytes(req));
38349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
38359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
38369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job = req->special;
38399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* check if we have the msgcode value at least */
38419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (job->request_len < sizeof(uint32_t)) {
38429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			BUG_ON(job->reply_len < sizeof(uint32_t));
38439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			job->reply->result = -ENOMSG;
38449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			job->reply_len = sizeof(uint32_t);
38459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			fc_bsg_jobdone(job);
38469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
38479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
38489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* the dispatch routines will unlock the queue_lock */
38519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (rport)
38529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = fc_bsg_rport_dispatch(q, shost, rport, job);
38539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		else
38549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = fc_bsg_host_dispatch(q, shost, job);
38559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* did dispatcher hit state that can't process any more */
38579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret == FC_DISPATCH_BREAK)
38589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			break;
38599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* did dispatcher had released the lock */
38619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret == FC_DISPATCH_UNLOCKED)
38629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
38639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
38649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irq(q->queue_lock);
38669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(dev);
38679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irq(q->queue_lock);
38689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
38699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
38729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_host_handler - handler for bsg requests for a fc host
38739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		fc host request queue
38749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
38759e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
38769e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_host_handler(struct request_queue *q)
38779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
38789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = q->queuedata;
38799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
38819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
38829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
38859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rport_handler - handler for bsg requests for a fc rport
38869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		rport request queue
38879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
38889e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
38899e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rport_handler(struct request_queue *q)
38909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
38919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_rport *rport = q->queuedata;
38929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = rport_to_shost(rport);
38939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_request_handler(q, shost, rport, &rport->dev);
38959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
38969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
38999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
39009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	shost for fc_host
39019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @fc_host:	fc_host adding the structures to
39029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
39039e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
39049e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
39059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
39069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct device *dev = &shost->shost_gendev;
39079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
39089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request_queue *q;
39099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
39103c559ea8fd003962d9a28c64b2dd5c6d83ca6edbJames Bottomley	char bsg_name[20];
39119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_host->rqst_q = NULL;
39139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!i->f->bsg_request)
39159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOTSUPP;
39169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	snprintf(bsg_name, sizeof(bsg_name),
39189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		 "fc_host%d", shost->host_no);
39199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
39219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!q) {
39229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "fc_host%d: bsg interface failed to "
39239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - no request queue\n",
39249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 shost->host_no);
39259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
39269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q->queuedata = shost;
39299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
3930b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_queue_softirq_done(q, fc_bsg_softirq_done);
39319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
39329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
39339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = bsg_register_queue(q, dev, bsg_name, NULL);
39359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err) {
39369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "fc_host%d: bsg interface failed to "
39379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - register queue\n",
39389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				shost->host_no);
39399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
39409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return err;
39419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_host->rqst_q = q;
39449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
39459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
39499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
39509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	shost that rport is attached to
39519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport that the bsg hooks are being attached to
39529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
39539e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
39549e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
39559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
39569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct device *dev = &rport->dev;
39579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
39589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request_queue *q;
39599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
39609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	rport->rqst_q = NULL;
39629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!i->f->bsg_request)
39649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOTSUPP;
39659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
39679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!q) {
39689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "%s: bsg interface failed to "
39699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - no request queue\n",
39709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 dev->kobj.name);
39719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
39729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q->queuedata = rport;
39759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
3976b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_queue_softirq_done(q, fc_bsg_softirq_done);
39779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
39789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
39799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = bsg_register_queue(q, dev, NULL, NULL);
39819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err) {
39829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "%s: bsg interface failed to "
39839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - register queue\n",
39849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 dev->kobj.name);
39859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
39869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return err;
39879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	rport->rqst_q = q;
39909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
39919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
39959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
39969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:	the request_queue that is to be torn down.
39979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
39989e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
39999e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_remove(struct request_queue *q)
40009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
40019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (q) {
40029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		bsg_unregister_queue(q);
40039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
40049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
40059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
40069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40089ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/* Original Author:  Martin Hicks */
40099ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames SmartMODULE_AUTHOR("James Smart");
40101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("FC Transport Attributes");
40111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
40121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
40131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(fc_transport_init);
40141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(fc_transport_exit);
4015