scsi_transport_fc.c revision ecc3099002c1cc87e9e4b3dc5fdf7821828f6733
19ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  FiberChannel transport specific attributes exported to sysfs.
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  This program is free software; you can redistribute it and/or modify
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  it under the terms of the GNU General Public License as published by
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  the Free Software Foundation; either version 2 of the License, or
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  (at your option) any later version.
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  This program is distributed in the hope that it will be useful,
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  but WITHOUT ANY WARRANTY; without even the implied warranty of
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  GNU General Public License for more details.
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  You should have received a copy of the GNU General Public License
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  along with this program; if not, write to the Free Software
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  ========
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
22a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *  Copyright (C) 2004-2007   James Smart, Emulex Corporation
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    Rewrite for host, target, device, and remote port attributes,
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    statistics, and service functions...
259ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart *    Add vports, etc
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
305a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
3165d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt#include <linux/delay.h>
32ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko#include <linux/kernel.h>
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_device.h>
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_host.h>
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport.h>
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_transport_fc.h>
37c829c394165f981d49f05a9be228404d7a9398d4James Smart#include <scsi/scsi_cmnd.h>
3884314fd4740ad73550c76dee4a9578979d84af48James Smart#include <linux/netlink.h>
3984314fd4740ad73550c76dee4a9578979d84af48James Smart#include <net/netlink.h>
4084314fd4740ad73550c76dee4a9578979d84af48James Smart#include <scsi/scsi_netlink_fc.h>
419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart#include <scsi/scsi_bsg_fc.h>
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "scsi_priv.h"
437525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori#include "scsi_transport_fc_internal.h"
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
45aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int fc_queue_work(struct Scsi_Host *, struct work_struct *);
469ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartstatic void fc_vport_sched_delete(struct work_struct *work);
47a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezstatic int fc_vport_setup(struct Scsi_Host *shost, int channel,
48a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *pdev, struct fc_vport_identifiers  *ids,
49a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport **vport);
509e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
519e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
529e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void fc_bsg_remove(struct request_queue *);
539e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void fc_bsg_goose_queue(struct fc_rport *);
54a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
55a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Redefine so that we can have same named attributes in the
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sdev/starget/host objects.
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
59ee959b00c335d7780136c5abda37809191fe52c3Tony Jones#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)		\
60ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstruct device_attribute device_attr_##_prefix##_##_name = 	\
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__ATTR(_name,_mode,_show,_store)
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_search(title, table_type, table)			\
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char *get_fc_##title##_name(enum table_type table_key)	\
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *name = NULL;						\
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
696391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value == table_key) {			\
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			name = table[i].name;				\
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;						\
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return name;							\
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_enum_name_match(title, table_type, table)			\
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int get_fc_##title##_match(const char *table_key,		\
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		enum table_type *value)					\
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;								\
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
846391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (strncmp(table_key, table[i].name,			\
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				table[i].matchlen) == 0) {		\
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*value = table[i].value;			\
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0; /* success */				\
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}							\
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 1; /* failure */						\
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_type values to ascii string name */
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_type	value;
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_type_names[] = {
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_UNKNOWN,		"Unknown" },
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_OTHER,		"Other" },
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NOTPRESENT,	"Not Present" },
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NPORT,	"NPort (fabric via point-to-point)" },
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_NLPORT,	"NLPort (fabric via loop)" },
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTTYPE_LPORT,	"LPort (private loop)" },
106951948a397e3ddc96658efd648b9d66622965b19Christof Schmitt	{ FC_PORTTYPE_PTP,	"Point-To-Point (direct nport connection)" },
107a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORTTYPE_NPIV,		"NPIV VPORT" },
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTTYPE_MAX_NAMELEN		50
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
112a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Reuse fc_port_type enum function for vport_type */
113a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define get_fc_vport_type_name get_fc_port_type_name
114a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11684314fd4740ad73550c76dee4a9578979d84af48James Smart/* Convert fc_host_event_code values to ascii string name */
11784314fd4740ad73550c76dee4a9578979d84af48James Smartstatic const struct {
11884314fd4740ad73550c76dee4a9578979d84af48James Smart	enum fc_host_event_code		value;
11984314fd4740ad73550c76dee4a9578979d84af48James Smart	char				*name;
12084314fd4740ad73550c76dee4a9578979d84af48James Smart} fc_host_event_code_names[] = {
12184314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LIP,			"lip" },
12284314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINKUP,		"link_up" },
12384314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINKDOWN,		"link_down" },
12484314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LIPRESET,		"lip_reset" },
12584314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_RSCN,			"rscn" },
12684314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_ADAPTER_CHANGE,	"adapter_chg" },
12784314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_UNKNOWN,		"port_unknown" },
12884314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_ONLINE,		"port_online" },
12984314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_OFFLINE,		"port_offline" },
13084314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_PORT_FABRIC,		"port_fabric" },
13184314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_LINK_UNKNOWN,		"link_unknown" },
13284314fd4740ad73550c76dee4a9578979d84af48James Smart	{ FCH_EVT_VENDOR_UNIQUE,	"vendor_unique" },
13384314fd4740ad73550c76dee4a9578979d84af48James Smart};
13484314fd4740ad73550c76dee4a9578979d84af48James Smartfc_enum_name_search(host_event_code, fc_host_event_code,
13584314fd4740ad73550c76dee4a9578979d84af48James Smart		fc_host_event_code_names)
13684314fd4740ad73550c76dee4a9578979d84af48James Smart#define FC_HOST_EVENT_CODE_MAX_NAMELEN	30
13784314fd4740ad73550c76dee4a9578979d84af48James Smart
13884314fd4740ad73550c76dee4a9578979d84af48James Smart
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_port_state values to ascii string name */
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct {
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_port_state	value;
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_state_names[] = {
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_UNKNOWN,		"Unknown" },
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_NOTPRESENT,	"Not Present" },
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ONLINE,		"Online" },
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_OFFLINE,		"Offline" },
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BLOCKED,		"Blocked" },
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_BYPASSED,	"Bypassed" },
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_DIAGNOSTICS,	"Diagnostics" },
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LINKDOWN,	"Linkdown" },
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_ERROR,		"Error" },
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSTATE_LOOPBACK,	"Loopback" },
15442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	{ FC_PORTSTATE_DELETED,		"Deleted" },
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_PORTSTATE_MAX_NAMELEN	20
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Convert fc_vport_state values to ascii string name */
161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic struct {
162a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	enum fc_vport_state	value;
163a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char			*name;
164a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart} fc_vport_state_names[] = {
165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_UNKNOWN,		"Unknown" },
166a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_ACTIVE,		"Active" },
167a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_DISABLED,		"Disabled" },
168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_LINKDOWN,		"Linkdown" },
169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_INITIALIZING,	"Initializing" },
170a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_NO_FABRIC_SUPP,	"No Fabric Support" },
171a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_NO_FABRIC_RSCS,	"No Fabric Resources" },
172a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FABRIC_LOGOUT,	"Fabric Logout" },
173a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FABRIC_REJ_WWN,	"Fabric Rejected WWN" },
174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_VPORT_FAILED,		"VPort Failed" },
175a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart};
176a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
177a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define FC_VPORTSTATE_MAX_NAMELEN	24
178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Reuse fc_vport_state enum function for vport_last_state */
180a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define get_fc_vport_last_state_name get_fc_vport_state_name
181a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
182a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert fc_tgtid_binding_type values to ascii string name */
1840ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum fc_tgtid_binding_type	value;
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char				*name;
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int				matchlen;
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_tgtid_binding_type_names[] = {
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_NONE, "none", 4 },
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_tgtid_binding_type_names)
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_BINDTYPE_MAX_NAMELEN	30
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_bitfield_name_search(title, table)			\
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t							\
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsget_fc_##title##_names(u32 table_key, char *buf)		\
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{								\
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *prefix = "";					\
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t len = 0;					\
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;							\
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds								\
2096391a11375de5e2bb1eb8481e54619761dc65d9fTobias Klauser	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (table[i].value & table_key) {		\
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			len += sprintf(buf + len, "%s%s",	\
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				prefix, table[i].name);		\
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prefix = ", ";				\
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}						\
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}							\
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");			\
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;						\
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_COS bit values to ascii string name */
2220ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_cos_names[] = {
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS1,	"Class 1" },
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS2,	"Class 2" },
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS3,	"Class 3" },
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS4,	"Class 4" },
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_COS_CLASS6,	"Class 6" },
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(cos, fc_cos_names)
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Convert FC_PORTSPEED bit values to ascii string name */
2360ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} fc_port_speed_names[] = {
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_1GBIT,		"1 Gbit" },
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_2GBIT,		"2 Gbit" },
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_4GBIT,		"4 Gbit" },
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_10GBIT,		"10 Gbit" },
244c3d2350a8420dbf9d48f5f8a0fb72117bfcbc1b0James Smart	{ FC_PORTSPEED_8GBIT,		"8 Gbit" },
245c3d2350a8420dbf9d48f5f8a0fb72117bfcbc1b0James Smart	{ FC_PORTSPEED_16GBIT,		"16 Gbit" },
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ FC_PORTSPEED_NOT_NEGOTIATED,	"Not Negotiated" },
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_bitfield_name_search(port_speed, fc_port_speed_names)
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_fc_fc4s (char *buf, u8 *fc4_list)
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i, len=0;
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		len += sprintf(buf + len , "0x%02x ", *fc4_list);
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	len += sprintf(buf + len, "\n");
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return len;
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
263a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Convert FC_PORT_ROLE bit values to ascii string name */
2640ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Venstatic const struct {
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 			value;
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char			*name;
267a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart} fc_port_role_names[] = {
268a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_FCP_TARGET,	"FCP Target" },
269a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_FCP_INITIATOR,	"FCP Initiator" },
270a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	{ FC_PORT_ROLE_IP_PORT,		"IP Port" },
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
272a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_bitfield_name_search(port_roles, fc_port_role_names)
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_PORTID_MASK	0xfffff0
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_WELLKNOWN_ROLE_MASK  	0x00000f
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FPORT_PORTID			0x00000e
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_FABCTLR_PORTID		0x00000d
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_DIRSRVR_PORTID		0x00000c
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_TIMESRVR_PORTID		0x00000b
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_MGMTSRVR_PORTID		0x00000a
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
286c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_timeout_deleted_rport(struct work_struct *work);
287c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_timeout_fail_rport_io(struct work_struct *work);
288c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void fc_scsi_scan_rport(struct work_struct *work);
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Attribute counts pre object type...
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Increase these values if you add attributes
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FC_STARGET_NUM_ATTRS 	3
2950f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart#define FC_RPORT_NUM_ATTRS	10
296a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define FC_VPORT_NUM_ATTRS	9
2974643682ba50634db5cd61292711d031630baf233James Smart#define FC_HOST_NUM_ATTRS	22
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_internal {
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_transport_template t;
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_function_template *f;
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * For attributes : each object has :
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of the actual attributes structures
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *   An array of null-terminated pointers to the attribute
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *     structures - used for mid-layer interaction.
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * The attribute containers for the starget and host are are
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * part of the midlayer. As the remote port is specific to the
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * fc transport, we must provide the attribute container.
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
313ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_starget_attrs[
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds							FC_STARGET_NUM_ATTRS];
315ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
317ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
318ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct transport_container rport_attr_cont;
321ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
322ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
323a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
324a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct transport_container vport_attr_cont;
325ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
326ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define to_fc_internal(tmpl)	container_of(tmpl, struct fc_internal, t)
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
331d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_target_setup(struct transport_container *tc, struct device *dev,
332ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   struct device *cdev)
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_target *starget = to_scsi_target(dev);
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * if parent is remote port, use values from remote port.
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Otherwise, this host uses the fc_transport, but not the
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * remote port interface. As such, initialize to known non-values.
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport) {
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = rport->node_name;
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = rport->port_name;
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = rport->port_id;
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_node_name(starget) = -1;
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_name(starget) = -1;
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_port_id(starget) = -1;
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_transport_class,
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_transport",
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_target_setup,
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
361d0a7e574007fd547d72ec693bfa35778623d0738James Bottomleystatic int fc_host_setup(struct transport_container *tc, struct device *dev,
362ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			 struct device *cdev)
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(dev);
365aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3679ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	/*
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set default values easily detected by the midlayer as
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * failure cases.  The scsi lldd is responsible for initializing
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * all transport attributes to valid values per host.
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
372aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->node_name = -1;
373aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_name = -1;
374aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->permanent_port_name = -1;
375aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->supported_classes = FC_COS_UNSPECIFIED;
376aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->supported_fc4s, 0,
377aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->supported_fc4s));
378aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
379aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->maxframe_size = -1;
380a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->max_npiv_vports = 0;
381aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->serial_number, 0,
382aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->serial_number));
383aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
384aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_id = -1;
385aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_type = FC_PORTTYPE_UNKNOWN;
386aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->port_state = FC_PORTSTATE_UNKNOWN;
387aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	memset(fc_host->active_fc4s, 0,
388aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		sizeof(fc_host->active_fc4s));
389aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->speed = FC_PORTSPEED_UNKNOWN;
390aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->fabric_name = -1;
391b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
392b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
393aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
394aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
395aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
396aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	INIT_LIST_HEAD(&fc_host->rports);
397aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	INIT_LIST_HEAD(&fc_host->rport_bindings);
398a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	INIT_LIST_HEAD(&fc_host->vports);
399aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->next_rport_number = 0;
400aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->next_target_id = 0;
401a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->next_vport_number = 0;
402a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse = 0;
403aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
404aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers	snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
405aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 "fc_wq_%d", shost->host_no);
406aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->work_q = create_singlethread_workqueue(
407aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_host->work_q_name);
408aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host->work_q)
409aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -ENOMEM;
410aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
411aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers	snprintf(fc_host->devloss_work_q_name,
412aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 sizeof(fc_host->devloss_work_q_name),
413aab0de245150c09e61c30962feb16aacde508dc3Kay Sievers		 "fc_dl_%d", shost->host_no);
414aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_host->devloss_work_q = create_singlethread_workqueue(
415aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_host->devloss_work_q_name);
416aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host->devloss_work_q) {
417aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(fc_host->work_q);
418aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->work_q = NULL;
419aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -ENOMEM;
420aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
421aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
4229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_hostadd(shost, fc_host);
4239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* ignore any bsg add error - we just can't do sgio */
4249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
4269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
4279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4289e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int fc_host_remove(struct transport_container *tc, struct device *dev,
4299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct device *cdev)
4309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
4319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = dev_to_shost(dev);
4329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
4339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
4349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_remove(fc_host->rqst_q);
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_host_class,
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_host",
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       fc_host_setup,
4419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			       fc_host_remove,
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Setup and Remove actions for remote ports are handled
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in the service functions below.
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DECLARE_TRANSPORT_CLASS(fc_rport_class,
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "fc_remote_ports",
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL,
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       NULL);
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
455a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Setup and Remove actions for virtual ports are handled
456a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * in the service functions below.
457a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
458a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic DECLARE_TRANSPORT_CLASS(fc_vport_class,
459a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       "fc_vports",
460a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL,
461a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL,
462a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			       NULL);
463a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
464a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Module Parameters
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * dev_loss_tmo: the default number of seconds that the FC transport
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   should insulate the loss of a remote port.
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4731c9e16e47a22c61d99aabb1c154e5106ddbf3575James Smartstatic unsigned int fc_dev_loss_tmo = 60;		/* seconds */
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
47510f4b89a0f0586a67f57efb2f5d12fffbb371490Masatake YAMATOmodule_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_PARM_DESC(dev_loss_tmo,
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 "Maximum number of seconds that the FC transport should"
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " insulate the loss of a remote port. Once this value is"
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 " exceeded, the scsi target is removed. Value should be"
480f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
481f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		 " fast_io_fail_tmo is not set.");
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
483eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley/*
48484314fd4740ad73550c76dee4a9578979d84af48James Smart * Netlink Infrastructure
485eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
48684314fd4740ad73550c76dee4a9578979d84af48James Smart
48784314fd4740ad73550c76dee4a9578979d84af48James Smartstatic atomic_t fc_event_seq;
48884314fd4740ad73550c76dee4a9578979d84af48James Smart
48984314fd4740ad73550c76dee4a9578979d84af48James Smart/**
49084314fd4740ad73550c76dee4a9578979d84af48James Smart * fc_get_event_number - Obtain the next sequential FC event number
49184314fd4740ad73550c76dee4a9578979d84af48James Smart *
49284314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
493eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *   We could have inlined this, but it would have required fc_event_seq to
49484314fd4740ad73550c76dee4a9578979d84af48James Smart *   be exposed. For now, live with the subroutine call.
49584314fd4740ad73550c76dee4a9578979d84af48James Smart *   Atomic used to avoid lock/unlock...
496eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
49784314fd4740ad73550c76dee4a9578979d84af48James Smartu32
49884314fd4740ad73550c76dee4a9578979d84af48James Smartfc_get_event_number(void)
49984314fd4740ad73550c76dee4a9578979d84af48James Smart{
50084314fd4740ad73550c76dee4a9578979d84af48James Smart	return atomic_add_return(1, &fc_event_seq);
50184314fd4740ad73550c76dee4a9578979d84af48James Smart}
50284314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_get_event_number);
50384314fd4740ad73550c76dee4a9578979d84af48James Smart
50484314fd4740ad73550c76dee4a9578979d84af48James Smart
50584314fd4740ad73550c76dee4a9578979d84af48James Smart/**
50684314fd4740ad73550c76dee4a9578979d84af48James Smart * fc_host_post_event - called to post an even on an fc_host.
50784314fd4740ad73550c76dee4a9578979d84af48James Smart * @shost:		host the event occurred on
50884314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_number:	fc event number obtained from get_fc_event_number()
50984314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_code:		fc_host event being posted
51084314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_data:		32bits of data for the event being posted
51184314fd4740ad73550c76dee4a9578979d84af48James Smart *
51284314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
51384314fd4740ad73550c76dee4a9578979d84af48James Smart *	This routine assumes no locks are held on entry.
514eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
51584314fd4740ad73550c76dee4a9578979d84af48James Smartvoid
51684314fd4740ad73550c76dee4a9578979d84af48James Smartfc_host_post_event(struct Scsi_Host *shost, u32 event_number,
51784314fd4740ad73550c76dee4a9578979d84af48James Smart		enum fc_host_event_code event_code, u32 event_data)
51884314fd4740ad73550c76dee4a9578979d84af48James Smart{
51984314fd4740ad73550c76dee4a9578979d84af48James Smart	struct sk_buff *skb;
52084314fd4740ad73550c76dee4a9578979d84af48James Smart	struct nlmsghdr	*nlh;
52184314fd4740ad73550c76dee4a9578979d84af48James Smart	struct fc_nl_event *event;
52284314fd4740ad73550c76dee4a9578979d84af48James Smart	const char *name;
52384314fd4740ad73550c76dee4a9578979d84af48James Smart	u32 len, skblen;
52484314fd4740ad73550c76dee4a9578979d84af48James Smart	int err;
52584314fd4740ad73550c76dee4a9578979d84af48James Smart
52684314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!scsi_nl_sock) {
52784314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOENT;
52884314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail;
52984314fd4740ad73550c76dee4a9578979d84af48James Smart	}
53084314fd4740ad73550c76dee4a9578979d84af48James Smart
53184314fd4740ad73550c76dee4a9578979d84af48James Smart	len = FC_NL_MSGALIGN(sizeof(*event));
53284314fd4740ad73550c76dee4a9578979d84af48James Smart	skblen = NLMSG_SPACE(len);
53384314fd4740ad73550c76dee4a9578979d84af48James Smart
53484314fd4740ad73550c76dee4a9578979d84af48James Smart	skb = alloc_skb(skblen, GFP_KERNEL);
53584314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!skb) {
53684314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
53784314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail;
53884314fd4740ad73550c76dee4a9578979d84af48James Smart	}
53984314fd4740ad73550c76dee4a9578979d84af48James Smart
54084314fd4740ad73550c76dee4a9578979d84af48James Smart	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
54184314fd4740ad73550c76dee4a9578979d84af48James Smart				skblen - sizeof(*nlh), 0);
54284314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!nlh) {
54384314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
54484314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_fail_skb;
54584314fd4740ad73550c76dee4a9578979d84af48James Smart	}
54684314fd4740ad73550c76dee4a9578979d84af48James Smart	event = NLMSG_DATA(nlh);
54784314fd4740ad73550c76dee4a9578979d84af48James Smart
54884314fd4740ad73550c76dee4a9578979d84af48James Smart	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
54984314fd4740ad73550c76dee4a9578979d84af48James Smart				FC_NL_ASYNC_EVENT, len);
55084314fd4740ad73550c76dee4a9578979d84af48James Smart	event->seconds = get_seconds();
55184314fd4740ad73550c76dee4a9578979d84af48James Smart	event->vendor_id = 0;
55284314fd4740ad73550c76dee4a9578979d84af48James Smart	event->host_no = shost->host_no;
55384314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_datalen = sizeof(u32);	/* bytes */
55484314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_num = event_number;
55584314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_code = event_code;
55684314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_data = event_data;
55784314fd4740ad73550c76dee4a9578979d84af48James Smart
558ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
559ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso			GFP_KERNEL);
56084314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
56184314fd4740ad73550c76dee4a9578979d84af48James Smart
56284314fd4740ad73550c76dee4a9578979d84af48James Smartsend_fail_skb:
56384314fd4740ad73550c76dee4a9578979d84af48James Smart	kfree_skb(skb);
56484314fd4740ad73550c76dee4a9578979d84af48James Smartsend_fail:
56584314fd4740ad73550c76dee4a9578979d84af48James Smart	name = get_fc_host_event_code_name(event_code);
56684314fd4740ad73550c76dee4a9578979d84af48James Smart	printk(KERN_WARNING
56784314fd4740ad73550c76dee4a9578979d84af48James Smart		"%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
568cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		__func__, shost->host_no,
56984314fd4740ad73550c76dee4a9578979d84af48James Smart		(name) ? name : "<unknown>", event_data, err);
57084314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
57184314fd4740ad73550c76dee4a9578979d84af48James Smart}
57284314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_host_post_event);
57384314fd4740ad73550c76dee4a9578979d84af48James Smart
57484314fd4740ad73550c76dee4a9578979d84af48James Smart
57584314fd4740ad73550c76dee4a9578979d84af48James Smart/**
576eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
57784314fd4740ad73550c76dee4a9578979d84af48James Smart * @shost:		host the event occurred on
57884314fd4740ad73550c76dee4a9578979d84af48James Smart * @event_number:	fc event number obtained from get_fc_event_number()
57984314fd4740ad73550c76dee4a9578979d84af48James Smart * @data_len:		amount, in bytes, of vendor unique data
58084314fd4740ad73550c76dee4a9578979d84af48James Smart * @data_buf:		pointer to vendor unique data
581eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @vendor_id:          Vendor id
58284314fd4740ad73550c76dee4a9578979d84af48James Smart *
58384314fd4740ad73550c76dee4a9578979d84af48James Smart * Notes:
58484314fd4740ad73550c76dee4a9578979d84af48James Smart *	This routine assumes no locks are held on entry.
585eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
58684314fd4740ad73550c76dee4a9578979d84af48James Smartvoid
58784314fd4740ad73550c76dee4a9578979d84af48James Smartfc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
588f14e2e29cdd07f80de6dec168dc2bb39de37eec3James Smart		u32 data_len, char * data_buf, u64 vendor_id)
58984314fd4740ad73550c76dee4a9578979d84af48James Smart{
59084314fd4740ad73550c76dee4a9578979d84af48James Smart	struct sk_buff *skb;
59184314fd4740ad73550c76dee4a9578979d84af48James Smart	struct nlmsghdr	*nlh;
59284314fd4740ad73550c76dee4a9578979d84af48James Smart	struct fc_nl_event *event;
59384314fd4740ad73550c76dee4a9578979d84af48James Smart	u32 len, skblen;
59484314fd4740ad73550c76dee4a9578979d84af48James Smart	int err;
59584314fd4740ad73550c76dee4a9578979d84af48James Smart
59684314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!scsi_nl_sock) {
59784314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOENT;
59884314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail;
59984314fd4740ad73550c76dee4a9578979d84af48James Smart	}
60084314fd4740ad73550c76dee4a9578979d84af48James Smart
60184314fd4740ad73550c76dee4a9578979d84af48James Smart	len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
60284314fd4740ad73550c76dee4a9578979d84af48James Smart	skblen = NLMSG_SPACE(len);
60384314fd4740ad73550c76dee4a9578979d84af48James Smart
60484314fd4740ad73550c76dee4a9578979d84af48James Smart	skb = alloc_skb(skblen, GFP_KERNEL);
60584314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!skb) {
60684314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
60784314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail;
60884314fd4740ad73550c76dee4a9578979d84af48James Smart	}
60984314fd4740ad73550c76dee4a9578979d84af48James Smart
61084314fd4740ad73550c76dee4a9578979d84af48James Smart	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
61184314fd4740ad73550c76dee4a9578979d84af48James Smart				skblen - sizeof(*nlh), 0);
61284314fd4740ad73550c76dee4a9578979d84af48James Smart	if (!nlh) {
61384314fd4740ad73550c76dee4a9578979d84af48James Smart		err = -ENOBUFS;
61484314fd4740ad73550c76dee4a9578979d84af48James Smart		goto send_vendor_fail_skb;
61584314fd4740ad73550c76dee4a9578979d84af48James Smart	}
61684314fd4740ad73550c76dee4a9578979d84af48James Smart	event = NLMSG_DATA(nlh);
61784314fd4740ad73550c76dee4a9578979d84af48James Smart
61884314fd4740ad73550c76dee4a9578979d84af48James Smart	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
61984314fd4740ad73550c76dee4a9578979d84af48James Smart				FC_NL_ASYNC_EVENT, len);
62084314fd4740ad73550c76dee4a9578979d84af48James Smart	event->seconds = get_seconds();
62184314fd4740ad73550c76dee4a9578979d84af48James Smart	event->vendor_id = vendor_id;
62284314fd4740ad73550c76dee4a9578979d84af48James Smart	event->host_no = shost->host_no;
62384314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_datalen = data_len;	/* bytes */
62484314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_num = event_number;
62584314fd4740ad73550c76dee4a9578979d84af48James Smart	event->event_code = FCH_EVT_VENDOR_UNIQUE;
62684314fd4740ad73550c76dee4a9578979d84af48James Smart	memcpy(&event->event_data, data_buf, data_len);
62784314fd4740ad73550c76dee4a9578979d84af48James Smart
628ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
629ff491a7334acfd74e515c896632e37e401f52676Pablo Neira Ayuso			GFP_KERNEL);
63084314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
63184314fd4740ad73550c76dee4a9578979d84af48James Smart
63284314fd4740ad73550c76dee4a9578979d84af48James Smartsend_vendor_fail_skb:
63384314fd4740ad73550c76dee4a9578979d84af48James Smart	kfree_skb(skb);
63484314fd4740ad73550c76dee4a9578979d84af48James Smartsend_vendor_fail:
63584314fd4740ad73550c76dee4a9578979d84af48James Smart	printk(KERN_WARNING
63684314fd4740ad73550c76dee4a9578979d84af48James Smart		"%s: Dropped Event : host %d vendor_unique - err %d\n",
637cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		__func__, shost->host_no, err);
63884314fd4740ad73550c76dee4a9578979d84af48James Smart	return;
63984314fd4740ad73550c76dee4a9578979d84af48James Smart}
64084314fd4740ad73550c76dee4a9578979d84af48James SmartEXPORT_SYMBOL(fc_host_post_vendor_event);
64184314fd4740ad73550c76dee4a9578979d84af48James Smart
64284314fd4740ad73550c76dee4a9578979d84af48James Smart
6431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic __init int fc_transport_init(void)
6451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
64684314fd4740ad73550c76dee4a9578979d84af48James Smart	int error;
64784314fd4740ad73550c76dee4a9578979d84af48James Smart
64884314fd4740ad73550c76dee4a9578979d84af48James Smart	atomic_set(&fc_event_seq, 0);
64984314fd4740ad73550c76dee4a9578979d84af48James Smart
65084314fd4740ad73550c76dee4a9578979d84af48James Smart	error = transport_class_register(&fc_host_class);
6511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return error;
653a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = transport_class_register(&fc_vport_class);
654a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error)
65548de68a40aef032a2e198437f4781a83bfb938dbMike Christie		goto unreg_host_class;
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = transport_class_register(&fc_rport_class);
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error)
65848de68a40aef032a2e198437f4781a83bfb938dbMike Christie		goto unreg_vport_class;
65948de68a40aef032a2e198437f4781a83bfb938dbMike Christie	error = transport_class_register(&fc_transport_class);
66048de68a40aef032a2e198437f4781a83bfb938dbMike Christie	if (error)
66148de68a40aef032a2e198437f4781a83bfb938dbMike Christie		goto unreg_rport_class;
66248de68a40aef032a2e198437f4781a83bfb938dbMike Christie	return 0;
66348de68a40aef032a2e198437f4781a83bfb938dbMike Christie
66448de68a40aef032a2e198437f4781a83bfb938dbMike Christieunreg_rport_class:
66548de68a40aef032a2e198437f4781a83bfb938dbMike Christie	transport_class_unregister(&fc_rport_class);
66648de68a40aef032a2e198437f4781a83bfb938dbMike Christieunreg_vport_class:
66748de68a40aef032a2e198437f4781a83bfb938dbMike Christie	transport_class_unregister(&fc_vport_class);
66848de68a40aef032a2e198437f4781a83bfb938dbMike Christieunreg_host_class:
66948de68a40aef032a2e198437f4781a83bfb938dbMike Christie	transport_class_unregister(&fc_host_class);
67048de68a40aef032a2e198437f4781a83bfb938dbMike Christie	return error;
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit fc_transport_exit(void)
6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_transport_class);
6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_rport_class);
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_class_unregister(&fc_host_class);
678a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_class_unregister(&fc_vport_class);
6791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC Remote Port Attribute Management
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_show_function(field, format_string, sz, cast)		\
6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
687ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##field (struct device *dev, 				\
688ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
6891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
690ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
69319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((i->f->get_rport_##field) &&				\
69419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    !((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
69542e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_DELETED) ||		\
69619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	      (rport->port_state == FC_PORTSTATE_NOTPRESENT)))		\
6971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_rport_##field(rport);				\
6981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
6991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_store_function(field)					\
7021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
703ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_##field(struct device *dev,				\
704ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
705ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
708ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);		\
7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
7110f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;							\
71219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
71342e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||		\
71419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))		\
71519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;						\
7160f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);				\
7170f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (*cp && (*cp != '\n'))					\
7180f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EINVAL;						\
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_rport_##field(rport, val);				\
7201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
7211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr(field, format_string, sz)			\
7241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
725ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
7271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rd_attr_cast(field, format_string, sz, cast)		\
7291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, (cast))	\
730ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
7321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_rport_rw_attr(field, format_string, sz)			\
7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_show_function(field, format_string, sz, )		\
7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_rport_store_function(field)					\
736ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,		\
7371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##field,				\
7381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_rport_##field)
7391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_show_function(field, format_string, sz, cast)	\
7421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
743ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##field (struct device *dev, 				\
744ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
7451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
746ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast rport->field); 	\
7481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr(field, format_string, sz)		\
7511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, )	\
752ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_rport_##field, NULL)
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)	\
7561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_rport_show_function(field, format_string, sz, (cast)) \
757ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
7581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_rport_##field, NULL)
7591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_rport_rd_enum_attr(title, maxlen)			\
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
763ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_##title (struct device *dev,				\
764ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
766ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);		\
7671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
7681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(rport->title);			\
7691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
7701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
7711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
7721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
773ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, title, S_IRUGO,			\
7741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_rport_##title, NULL)
7751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RD(field)					\
778ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
7801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
7811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
7831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
7841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)				\
786ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
7881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_rport_attrs[count].store = NULL;			\
7891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
7911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_RPORT_ATTRIBUTE_RW(field)					\
793ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
7941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_rport_##field) {					\
7951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].attr.mode = S_IRUGO;	\
7961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_rport_attrs[count].store = NULL;		\
7971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
7981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
7991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_rport_##field)					\
8001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
8011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8020f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field)				\
8030f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{									\
804ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_rport_attrs[count] = device_attr_rport_##field; \
8050f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
8060f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	count++;							\
8070f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
8080f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
8091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport Remote Port Attributes: */
8111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Remote Port Attributes */
8131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
8151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
817ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_supported_classes (struct device *dev,
818ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				 struct device_attribute *attr, char *buf)
8191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
820ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
8211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport->supported_classes == FC_COS_UNSPECIFIED)
8221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
8231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(rport->supported_classes, buf);
8241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
825ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
8261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_supported_classes, NULL);
8271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Remote Port Attributes */
8291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
83019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com/*
83119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * dev_loss_tmo attribute
83219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com */
83319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comfc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
83419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic ssize_t
835ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
836ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			    const char *buf, size_t count)
83719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
83836dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	unsigned long val;
839ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
84019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
84119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *i = to_fc_internal(shost->transportt);
8420f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;
84319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
84442e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_DELETED) ||
84519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
84619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EBUSY;
8470f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);
848f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	if ((*cp && (*cp != '\n')) || (val < 0))
84919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return -EINVAL;
850f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke
851f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	/*
85236dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	 * Check for overflow; dev_loss_tmo is u32
85336dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	 */
85436dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	if (val > UINT_MAX)
85536dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke		return -EINVAL;
85636dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke
85736dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	/*
858f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	 * If fast_io_fail is off we have to cap
859f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
860f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	 */
861f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	if (rport->fast_io_fail_tmo == -1 &&
862f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke	    val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
863f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		return -EINVAL;
864f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke
86519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	i->f->set_rport_dev_loss_tmo(rport, val);
86619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	return count;
86719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
868ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
86919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
8701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Remote Port Attributes */
8731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
8751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
8761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
8771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
879ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_roles (struct device *dev, struct device_attribute *attr,
880ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		     char *buf)
8811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
882ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
8831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* identify any roles that are port_id specific */
8851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((rport->port_id != -1) &&
8861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
8871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					FC_WELLKNOWN_PORTID_MASK) {
8881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
8891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FPORT_PORTID:
8901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Port\n");
8911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_FABCTLR_PORTID:
8921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Fabric Controller\n");
8931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_DIRSRVR_PORTID:
8941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Directory Server\n");
8951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_TIMESRVR_PORTID:
8961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Time Server\n");
8971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FC_MGMTSRVR_PORTID:
8981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Management Server\n");
8991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
9001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 30, "Unknown Fabric Entity\n");
9011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
9021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
903a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (rport->roles == FC_PORT_ROLE_UNKNOWN)
9041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return snprintf(buf, 20, "unknown\n");
905a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return get_fc_port_roles_names(rport->roles, buf);
9061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
9071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
908ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, roles, S_IRUGO,
9091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_rport_roles, NULL);
9101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
9121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
9131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9140f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart/*
9150f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * fast_io_fail_tmo attribute
9160f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart */
9170f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic ssize_t
918ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_rport_fast_io_fail_tmo (struct device *dev,
919ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				struct device_attribute *attr, char *buf)
9200f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
921ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
9220f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
9230f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->fast_io_fail_tmo == -1)
9240f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return snprintf(buf, 5, "off\n");
9250f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
9260f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
9270f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
9280f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic ssize_t
929ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_rport_fast_io_fail_tmo(struct device *dev,
930ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				struct device_attribute *attr, const char *buf,
931ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				size_t count)
9320f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
9330f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	int val;
9340f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;
935ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_rport *rport = transport_class_to_rport(dev);
9360f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
9370f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
9380f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (rport->port_state == FC_PORTSTATE_DELETED) ||
9390f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
9400f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EBUSY;
9410f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (strncmp(buf, "off", 3) == 0)
9420f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		rport->fast_io_fail_tmo = -1;
9430f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	else {
9440f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		val = simple_strtoul(buf, &cp, 0);
945f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		if ((*cp && (*cp != '\n')) || (val < 0))
946f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke			return -EINVAL;
947f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		/*
948f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		 * Cap fast_io_fail by dev_loss_tmo or
949f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
950f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		 */
951f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		if ((val >= rport->dev_loss_tmo) ||
952f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke		    (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
9530f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart			return -EINVAL;
954f2818663c82b7297ff4aa38cbddb870dc02f7104Hannes Reinecke
9550f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		rport->fast_io_fail_tmo = val;
9560f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	}
9570f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	return count;
9580f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
959ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
9600f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
9611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
9641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FC SCSI Target Attribute Management
9651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
9661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
9681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Note: in the target show function we recognize when the remote
969732bee7af3102cad811fb047dee8d15966efe569Uwe Kleine-König *  port is in the hierarchy and do not allow the driver to get
9701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  involved in sysfs functions. The driver only gets involved if
9711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  it's the "old" style that doesn't use rports.
9721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
9731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_show_function(field, format_string, sz, cast)	\
9741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
975ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_starget_##field (struct device *dev, 				\
976ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			 struct device_attribute *attr, char *buf)	\
9771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
978ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct scsi_target *starget = transport_class_to_starget(dev);	\
9791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);	\
9801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
9811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = starget_to_rport(starget);		\
9821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rport)							\
9831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		fc_starget_##field(starget) = rport->field;		\
9841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (i->f->get_starget_##field)				\
9851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_starget_##field(starget);			\
9861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, 			\
9871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cast fc_starget_##field(starget)); 			\
9881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr(field, format_string, sz)			\
9911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, )		\
992ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
9931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_starget_##field, NULL)
9941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_starget_rd_attr_cast(field, format_string, sz, cast)		\
9961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_starget_show_function(field, format_string, sz, (cast))	\
997ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
9981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_starget_##field, NULL)
9991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RD(field)				\
1001ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_starget_attrs[count] = device_attr_starget_##field; \
10021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].attr.mode = S_IRUGO;		\
10031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_starget_attrs[count].store = NULL;			\
10041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
10051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
10061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
10071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_STARGET_ATTRIBUTE_RW(field)				\
1009ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_starget_attrs[count] = device_attr_starget_##field; \
10101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_starget_##field) {				\
10111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].attr.mode = S_IRUGO;	\
10121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_starget_attrs[count].store = NULL;		\
10131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
10141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
10151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_starget_##field)					\
10161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
10171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The FC Transport SCSI Target Attributes: */
10191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
10201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
10211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_starget_rd_attr(port_id, "0x%06x\n", 20);
10221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1025a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * FC Virtual Port Attribute Management
1026a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1027a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1028a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_show_function(field, format_string, sz, cast)		\
1029a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1030ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##field (struct device *dev, 				\
1031ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
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	if ((i->f->get_vport_##field) &&				\
1037a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)))	\
1038a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->f->get_vport_##field(vport);				\
1039a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, sz, format_string, cast vport->field); 	\
1040a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1041a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1042a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_store_function(field)					\
1043a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1044ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1045ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1046ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1047a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1048a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int val;							\
1049ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1050a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1051a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1052a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char *cp;							\
1053a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))	\
1054a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;						\
1055a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	val = simple_strtoul(buf, &cp, 0);				\
1056a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*cp && (*cp != '\n'))					\
1057a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1058a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->f->set_vport_##field(vport, val);				\
1059a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1060a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1061a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1062a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_store_str_function(field, slen)			\
1063a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1064ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1065ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, 			\
1066ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1067a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1068ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1069a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);		\
1070a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1071a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;						\
1072a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart									\
1073a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */			\
1074a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')						\
1075a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;							\
1076a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (cnt > ((slen) - 1))						\
1077a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1078a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memcpy(vport->field, buf, cnt);					\
1079a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->f->set_vport_##field(vport);					\
1080a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1081a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1082a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1083a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rd_attr(field, format_string, sz)			\
1084a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, )		\
1085ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1086a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			 show_fc_vport_##field, NULL)
1087a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1088a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rd_attr_cast(field, format_string, sz, cast)		\
1089a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, (cast))	\
1090ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1091a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			  show_fc_vport_##field, NULL)
1092a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1093a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_vport_rw_attr(field, format_string, sz)			\
1094a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_show_function(field, format_string, sz, )		\
1095a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_vport_store_function(field)					\
1096ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1097a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##field,				\
1098a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_vport_##field)
1099a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1100a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_show_function(field, format_string, sz, cast)	\
1101a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1102ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##field (struct device *dev,				\
1103ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr, char *buf)	\
1104a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1105ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1106a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, sz, format_string, cast vport->field); 	\
1107a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1108a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1109a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_store_u32_function(field)			\
1110a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1111ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_##field(struct device *dev,				\
1112ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1113ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,	size_t count)			\
1114a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1115a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u32 val;							\
1116ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1117a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	char *cp;							\
1118a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))		\
1119a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;						\
1120a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	val = simple_strtoul(buf, &cp, 0);				\
1121a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*cp && (*cp != '\n'))					\
1122a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1123a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->field = val;						\
1124a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;							\
1125a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1126a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1127a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1128a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_attr(field, format_string, sz)		\
1129a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, )	\
1130ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1131a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			 show_fc_vport_##field, NULL)
1132a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1133a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast)	\
1134a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, (cast)) \
1135ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1136a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			  show_fc_vport_##field, NULL)
1137a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1138a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rw_u32_attr(field, format_string, sz)		\
1139a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_show_function(field, format_string, sz, )	\
1140a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_private_vport_store_u32_function(field)			\
1141ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1142a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##field,				\
1143a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_vport_##field)
1144a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1145a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1146a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define fc_private_vport_rd_enum_attr(title, maxlen)			\
1147a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t								\
1148ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_##title (struct device *dev,				\
1149ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       struct device_attribute *attr,			\
1150ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       char *buf)					\
1151a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1152ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);		\
1153a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	const char *name;						\
1154a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	name = get_fc_##title##_name(vport->title);			\
1155a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!name)							\
1156a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;						\
1157a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return snprintf(buf, maxlen, "%s\n", name);			\
1158a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}									\
1159ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, title, S_IRUGO,			\
1160a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			show_fc_vport_##title, NULL)
1161a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1162a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1163a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_RD(field)					\
1164ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1165a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1166a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].store = NULL;			\
1167a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1168a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->get_##field)						\
1169a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		count++
1170a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: checks function not show bit */
1171a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1172a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field)				\
1173ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1175a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_vport_attrs[count].store = NULL;			\
1176a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1177a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1178a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1179a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_WR(field)					\
1180ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1181a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1182a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->field)						\
1183a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		count++
1184a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: checks function */
1185a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1186a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_VPORT_ATTRIBUTE_RW(field)					\
1187ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1188a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!i->f->set_vport_##field) {					\
1189a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->private_vport_attrs[count].attr.mode = S_IRUGO;	\
1190a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		i->private_vport_attrs[count].store = NULL;		\
1191a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}								\
1192a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1193a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1194a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* NOTE: Above MACRO differs: does not check show bit */
1195a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1196a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field)				\
1197a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{									\
1198ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_vport_attrs[count] = device_attr_vport_##field; \
1199a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1200a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++;							\
1201a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1202a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1203a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1204a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* The FC Transport Virtual Port Attributes: */
1205a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1206a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Fixed Virtual Port Attributes */
1207a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1208a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Dynamic Virtual Port Attributes */
1209a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1210a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Private Virtual Port Attributes */
1211a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1212a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1213a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1214a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1215a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1216a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1217a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1218ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1219ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		     char *buf)
1220a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1221ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
1222a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1223a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1224a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return snprintf(buf, 20, "unknown\n");
1225a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return get_fc_port_roles_names(vport->roles, buf);
1226a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1227ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1228a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1229a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1230a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1231a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_vport_show_function(symbolic_name, "%s\n",
1232a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1233a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1234ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1235a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1236a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1237a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1238ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1239ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf, size_t count)
1240a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1241ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
12429ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	struct Scsi_Host *shost = vport_to_shost(vport);
12430d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	unsigned long flags;
12440d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen
12450d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	spin_lock_irqsave(shost->host_lock, flags);
12460d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
12470d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen		spin_unlock_irqrestore(shost->host_lock, flags);
12480d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen		return -EBUSY;
12490d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	}
12500d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	vport->flags |= FC_VPORT_DELETING;
12510d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen	spin_unlock_irqrestore(shost->host_lock, flags);
1252a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
12539ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	fc_queue_work(shost, &vport->vport_delete_work);
1254a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return count;
1255a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1256ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1257a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			NULL, store_fc_vport_delete);
1258a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1259a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1260a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1261a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Enable/Disable vport
1262a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *  Write "1" to disable, write "0" to enable
1263a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1264a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1265ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1266ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		       const char *buf,
1267a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			   size_t count)
1268a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1269ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct fc_vport *vport = transport_class_to_vport(dev);
1270a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);
1271a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
1272a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
1273a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1274a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1275a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EBUSY;
1276a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1277a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (*buf == '0') {
1278a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (vport->vport_state != FC_VPORT_DISABLED)
1279a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EALREADY;
1280a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	} else if (*buf == '1') {
1281a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (vport->vport_state == FC_VPORT_DISABLED)
1282a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EALREADY;
1283a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	} else
1284a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1285a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1286a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1287a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1288a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1289ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1290a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			NULL, store_fc_vport_disable);
1291a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1292a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1293a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
12941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Attribute Management
12951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
12961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_show_function(field, format_string, sz, cast)		\
12981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1299ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##field (struct device *dev,				\
1300ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
13011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1302ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
13031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
13041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##field)					\
13051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##field(shost);				\
13061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
13071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_store_function(field)					\
13101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1311ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_##field(struct device *dev, 				\
1312ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr,			\
1313ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf,	size_t count)			\
13141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
13151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int val;							\
1316ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
13171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
13180f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	char *cp;							\
13191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds									\
13200f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	val = simple_strtoul(buf, &cp, 0);				\
13210f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (*cp && (*cp != '\n'))					\
13220f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return -EINVAL;						\
13231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f->set_host_##field(shost, val);				\
13241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;							\
13251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1327b8d08210126a7b769b857720a59721a453a57a1eJames Smart#define fc_host_store_str_function(field, slen)				\
1328b8d08210126a7b769b857720a59721a453a57a1eJames Smartstatic ssize_t								\
1329ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_##field(struct device *dev,				\
1330ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr,			\
1331ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      const char *buf, size_t count)			\
1332b8d08210126a7b769b857720a59721a453a57a1eJames Smart{									\
1333ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1334b8d08210126a7b769b857720a59721a453a57a1eJames Smart	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1335b8d08210126a7b769b857720a59721a453a57a1eJames Smart	unsigned int cnt=count;						\
1336b8d08210126a7b769b857720a59721a453a57a1eJames Smart									\
1337b8d08210126a7b769b857720a59721a453a57a1eJames Smart	/* count may include a LF at end of string */			\
1338b8d08210126a7b769b857720a59721a453a57a1eJames Smart	if (buf[cnt-1] == '\n')						\
1339b8d08210126a7b769b857720a59721a453a57a1eJames Smart		cnt--;							\
1340b8d08210126a7b769b857720a59721a453a57a1eJames Smart	if (cnt > ((slen) - 1))						\
1341b8d08210126a7b769b857720a59721a453a57a1eJames Smart		return -EINVAL;						\
1342b8d08210126a7b769b857720a59721a453a57a1eJames Smart	memcpy(fc_host_##field(shost), buf, cnt);			\
1343b8d08210126a7b769b857720a59721a453a57a1eJames Smart	i->f->set_host_##field(shost);					\
1344b8d08210126a7b769b857720a59721a453a57a1eJames Smart	return count;							\
1345b8d08210126a7b769b857720a59721a453a57a1eJames Smart}
1346b8d08210126a7b769b857720a59721a453a57a1eJames Smart
13471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr(field, format_string, sz)			\
13481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
1349ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
13511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_attr_cast(field, format_string, sz, cast)		\
13531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, (cast))		\
1354ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
13551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
13561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rw_attr(field, format_string, sz)			\
13581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_show_function(field, format_string, sz, )		\
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_store_function(field)					\
1360ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
13611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_host_##field,				\
13621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_host_##field)
13631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_rd_enum_attr(title, maxlen)				\
13651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1366ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##title (struct device *dev,				\
1367ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
13681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1369ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
13701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);	\
13711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;						\
13721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_##title)					\
13731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_##title(shost);				\
13741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_##title##_name(fc_host_##title(shost));		\
13751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)							\
13761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;						\
13771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, maxlen, "%s\n", name);			\
13781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
1379ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
13801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RD(field)					\
1382ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
13841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
13851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
13861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
13871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
13881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1389a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart#define SETUP_HOST_ATTRIBUTE_RD_NS(field)				\
1390ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
1391a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1392a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->private_host_attrs[count].store = NULL;			\
1393a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->host_attrs[count] = &i->private_host_attrs[count];		\
1394a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count++
1395a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
13961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_HOST_ATTRIBUTE_RW(field)					\
1397ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
13981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i->f->set_host_##field) {					\
13991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].attr.mode = S_IRUGO;	\
14001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->private_host_attrs[count].store = NULL;		\
14011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}								\
14021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
14031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->show_host_##field)					\
14041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count++
14051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_show_function(field, format_string, sz, cast)	\
14081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t								\
1409ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_##field (struct device *dev,				\
1410ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		      struct device_attribute *attr, char *buf)		\
14111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
1412ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
14131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
14141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
14151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr(field, format_string, sz)		\
14171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, )	\
1418ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
14191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 show_fc_host_##field, NULL)
14201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_private_host_rd_attr_cast(field, format_string, sz, cast)	\
14221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_private_host_show_function(field, format_string, sz, (cast)) \
1423ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, field, S_IRUGO,			\
14241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  show_fc_host_##field, NULL)
14251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
1427ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
14281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
14291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->private_host_attrs[count].store = NULL;			\
14301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
14311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count++
14321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)			\
143491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{									\
1435ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	i->private_host_attrs[count] = device_attr_host_##field;	\
14361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = &i->private_host_attrs[count];		\
143791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	count++;							\
143891ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
14391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Fixed Host Attributes */
14421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1444ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_classes (struct device *dev,
1445ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			        struct device_attribute *attr, char *buf)
14461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1447ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
14501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unspecified\n");
14511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
14531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1454ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
14551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_classes, NULL);
14561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1458ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_fc4s (struct device *dev,
1459ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			     struct device_attribute *attr, char *buf)
14601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1461ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
14631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1464ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
14651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_fc4s, NULL);
14661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1468ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_supported_speeds (struct device *dev,
1469ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			       struct device_attribute *attr, char *buf)
14701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1471ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
14741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
14751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
14771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1478ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
14791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_supported_speeds, NULL);
14801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
14831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
14846b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmannfc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
14856b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann			     unsigned long long);
14861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1487a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
14881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
14891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Dynamic Host Attributes */
14921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1494ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_active_fc4s (struct device *dev,
1495ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			  struct device_attribute *attr, char *buf)
14961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1497ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
14981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
14991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_active_fc4s)
15011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_active_fc4s(shost);
15021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
15041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1505ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
15061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_active_fc4s, NULL);
15071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1509ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_host_speed (struct device *dev,
1510ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		    struct device_attribute *attr, char *buf)
15111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1512ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
15141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_host_speed)
15161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->get_host_speed(shost);
15171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
15191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return snprintf(buf, 20, "unknown\n");
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return get_fc_port_speed_names(fc_host_speed(shost), buf);
15221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1523ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, speed, S_IRUGO,
15241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		show_fc_host_speed, NULL);
15251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr(port_id, "0x%06x\n", 20);
15281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
15291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
15301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1531016131b8fffa1085b4ad165ab228116fdc278ebeJames Smartfc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
15321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1533b8d08210126a7b769b857720a59721a453a57a1eJames Smartfc_private_host_show_function(system_hostname, "%s\n",
1534b8d08210126a7b769b857720a59721a453a57a1eJames Smart		FC_SYMBOLIC_NAME_SIZE + 1, )
1535b8d08210126a7b769b857720a59721a453a57a1eJames Smartfc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1536ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1537b8d08210126a7b769b857720a59721a453a57a1eJames Smart		show_fc_host_system_hostname, store_fc_host_system_hostname);
1538b8d08210126a7b769b857720a59721a453a57a1eJames Smart
15391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Private Host Attributes */
15411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1543ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesshow_fc_private_host_tgtid_bind_type(struct device *dev,
1544ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				     struct device_attribute *attr, char *buf)
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1546ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
15471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name;
15481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
15501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!name)
15511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
15521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
15531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1555d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com#define get_list_head_entry(pos, head, member) 		\
1556d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	pos = list_entry((head)->next, typeof(*pos), member)
1557d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com
15581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1559ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_private_host_tgtid_bind_type(struct device *dev,
1560ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *attr, const char *buf, size_t count)
15611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1562ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1563d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com	struct fc_rport *rport;
15641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 	enum fc_tgtid_binding_type val;
15651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
15661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (get_fc_tgtid_bind_type_match(buf, &val))
15681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
15691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if changing bind type, purge all unused consistent bindings */
15711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (val != fc_host_tgtid_bind_type(shost)) {
15721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_lock_irqsave(shost->host_lock, flags);
1573d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		while (!list_empty(&fc_host_rport_bindings(shost))) {
1574d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com			get_list_head_entry(rport,
1575d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com				&fc_host_rport_bindings(shost), peers);
1576aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			list_del(&rport->peers);
1577aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			rport->port_state = FC_PORTSTATE_DELETED;
1578aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			fc_queue_work(shost, &rport->rport_delete_work);
1579d16794f6ac8d9b50f62e02a6e6175ae1a30d0ccdJames.Smart@Emulex.Com		}
15801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_unlock_irqrestore(shost->host_lock, flags);
15811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fc_host_tgtid_bind_type(shost) = val;
15841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;
15851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1587ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
15881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			show_fc_private_host_tgtid_bind_type,
15891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			store_fc_private_host_tgtid_bind_type);
15901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
159191ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquezstatic ssize_t
1592ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_private_host_issue_lip(struct device *dev,
1593ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct device_attribute *attr, const char *buf, size_t count)
159491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez{
1595ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
159691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	struct fc_internal *i = to_fc_internal(shost->transportt);
159791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	int ret;
159891ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
159991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	/* ignore any data value written to the attribute */
160091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (i->f->issue_fc_host_lip) {
160191ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		ret = i->f->issue_fc_host_lip(shost);
160291ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		return ret ? ret: count;
160391ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	}
160491ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
160591ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	return -ENOENT;
160691ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez}
160791ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
1608ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
160991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez			store_fc_private_host_issue_lip);
161091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez
1611a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1612a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1613a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
16141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
16151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host Statistics Management
16161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
16171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Show a given an attribute in the statistics group */
16191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1620ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesfc_stat_show(const struct device *dev, char *buf, unsigned long offset)
16211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1622ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
16231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
16241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_host_statistics *stats;
16251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ssize_t ret = -ENOENT;
16261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (offset > sizeof(struct fc_host_statistics) ||
16281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    offset % sizeof(u64) != 0)
16291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		WARN_ON(1);
16301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->get_fc_host_stats) {
16321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		stats = (i->f->get_fc_host_stats)(shost);
16331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (stats)
16341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = snprintf(buf, 20, "0x%llx\n",
16351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
16361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
16381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
16391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* generate a read-only statistics attribute */
16421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define fc_host_statistic(name)						\
1643ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic ssize_t show_fcstat_##name(struct device *cd,			\
1644ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				  struct device_attribute *attr,	\
1645ee959b00c335d7780136c5abda37809191fe52c3Tony Jones				  char *buf)				\
16461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{									\
16471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return fc_stat_show(cd, buf, 					\
16481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    offsetof(struct fc_host_statistics, name));	\
16491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}									\
1650ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
16511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(seconds_since_last_reset);
16531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_frames);
16541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(tx_words);
16551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_frames);
16561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(rx_words);
16571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(lip_count);
16581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(nos_count);
16591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(error_frames);
16601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(dumped_frames);
16611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(link_failure_count);
16621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_sync_count);
16631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(loss_of_signal_count);
16641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(prim_seq_protocol_err_count);
16651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_tx_word_count);
16661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(invalid_crc_count);
16671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_requests);
16681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_requests);
16691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_control_requests);
16701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_input_megabytes);
16711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_host_statistic(fcp_output_megabytes);
16721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
1674ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesfc_reset_statistics(struct device *dev, struct device_attribute *attr,
1675ee959b00c335d7780136c5abda37809191fe52c3Tony Jones		    const char *buf, size_t count)
16761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1677ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
16781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(shost->transportt);
16791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* ignore any data value written to the attribute */
16811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i->f->reset_fc_host_stats) {
16821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->f->reset_fc_host_stats(shost);
16831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return count;
16841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -ENOENT;
16871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1688ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
16891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				fc_reset_statistics);
16901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute *fc_statistics_attrs[] = {
1692ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_seconds_since_last_reset.attr,
1693ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_tx_frames.attr,
1694ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_tx_words.attr,
1695ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_rx_frames.attr,
1696ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_rx_words.attr,
1697ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_lip_count.attr,
1698ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_nos_count.attr,
1699ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_error_frames.attr,
1700ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_dumped_frames.attr,
1701ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_link_failure_count.attr,
1702ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_loss_of_sync_count.attr,
1703ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_loss_of_signal_count.attr,
1704ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_prim_seq_protocol_err_count.attr,
1705ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_invalid_tx_word_count.attr,
1706ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_invalid_crc_count.attr,
1707ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_input_requests.attr,
1708ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_output_requests.attr,
1709ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_control_requests.attr,
1710ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_input_megabytes.attr,
1711ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_fcp_output_megabytes.attr,
1712ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	&device_attr_host_reset_statistics.attr,
17131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	NULL
17141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
17151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct attribute_group fc_statistics_group = {
17171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name = "statistics",
17181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.attrs = fc_statistics_attrs,
17191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
17201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1721a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1722a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/* Host Vport Attributes */
1723a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1724a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int
1725a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_parse_wwn(const char *ns, u64 *nm)
1726a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1727a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int i, j;
1728a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u8 wwn[8];
1729a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1730a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memset(wwn, 0, sizeof(wwn));
1731a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1732a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* Validate and store the new name */
1733a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	for (i=0, j=0; i < 16; i++) {
1734ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko		int value;
1735ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko
1736ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko		value = hex_to_bin(*ns++);
1737ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko		if (value >= 0)
1738ecc3099002c1cc87e9e4b3dc5fdf7821828f6733Andy Shevchenko			j = (j << 4) | value;
1739a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		else
1740a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			return -EINVAL;
1741a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (i % 2) {
1742a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			wwn[i/2] = j & 0xff;
1743a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			j = 0;
1744a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		}
1745a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
1746a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1747a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*nm = wwn_to_u64(wwn);
1748a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1749a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0;
1750a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1751a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1752a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1753a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1754a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * "Short-cut" sysfs variable to create a new vport on a FC Host.
1755a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1756a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1757a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1758a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1759a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1760ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1761ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   const char *buf, size_t count)
1762a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1763ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1764a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport_identifiers vid;
1765a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1766a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;
1767a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
1768a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1769a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	memset(&vid, 0, sizeof(vid));
1770a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1771a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */
1772a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')
1773a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;
1774a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1775a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* validate we have enough characters for WWPN */
1776a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((cnt != (16+1+16)) || (buf[16] != ':'))
1777a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1778a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1779a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[0], &vid.port_name);
1780a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1781a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1782a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1783a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[17], &vid.node_name);
1784a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1785a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1786a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1787a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1788a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.vport_type = FC_PORTTYPE_NPIV;
1789a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* vid.symbolic_name is already zero/NULL's */
1790a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vid.disable = false;		/* always enabled */
1791a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1792a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* we only allow support on Channel 0 !!! */
1793a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1794a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1795a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1796ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1797a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_host_vport_create);
1798a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1799a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1800a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/*
1801a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * "Short-cut" sysfs variable to delete a vport on a FC Host.
1802a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Vport is identified by a string containing "<WWPN>:<WWNN>".
1803a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * The WWNs are specified as hex characters, and may *not* contain
1804a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * any prefixes (e.g. 0x, x, etc)
1805a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart */
1806a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic ssize_t
1807ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstore_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1808ee959b00c335d7780136c5abda37809191fe52c3Tony Jones			   const char *buf, size_t count)
1809a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1810ee959b00c335d7780136c5abda37809191fe52c3Tony Jones	struct Scsi_Host *shost = transport_class_to_shost(dev);
1811a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1812a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1813a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	u64 wwpn, wwnn;
1814a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
1815a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned int cnt=count;
1816a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat, match;
1817a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1818a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* count may include a LF at end of string */
1819a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (buf[cnt-1] == '\n')
1820a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		cnt--;
1821a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1822a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* validate we have enough characters for WWPN */
1823a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ((cnt != (16+1+16)) || (buf[16] != ':'))
1824a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -EINVAL;
1825a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1826a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[0], &wwpn);
1827a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1828a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1829a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1830a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_parse_wwn(&buf[17], &wwnn);
1831a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
1832a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
1833a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1834a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
1835a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	match = 0;
1836a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* we only allow support on Channel 0 !!! */
1837a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_for_each_entry(vport, &fc_host->vports, peers) {
1838a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if ((vport->channel == 0) &&
1839a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		    (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
18400d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen			if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
18410d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen				break;
18420d9dc7c8b9b7fa0f53647423b41056ee1beed735Gal Rosen			vport->flags |= FC_VPORT_DELETING;
1843a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			match = 1;
1844a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			break;
1845a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		}
1846a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
1847a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
1848a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1849a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!match)
1850a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENODEV;
1851a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1852a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	stat = fc_vport_terminate(vport);
1853a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return stat ? stat : count;
1854a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1855ee959b00c335d7780136c5abda37809191fe52c3Tony Jonesstatic FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1856a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			store_fc_host_vport_delete);
1857a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1858a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
18591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_host_match(struct attribute_container *cont,
18601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  struct device *dev)
18611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
18631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
18641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_host_device(dev))
18661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev);
18691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
18701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
18711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
18741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.host_attrs.ac == cont;
18761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_target_match(struct attribute_container *cont,
18791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
18801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
18821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
18831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_target_device(dev))
18851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
18881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
18891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
18901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
18911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
18931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t.target_attrs.ac == cont;
18951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void fc_rport_dev_release(struct device *dev)
18981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport = dev_to_rport(dev);
19001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
19011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
19021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint scsi_is_fc_rport(const struct device *dev)
19051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return dev->release == fc_rport_dev_release;
19071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(scsi_is_fc_rport);
19091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int fc_rport_match(struct attribute_container *cont,
19111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct device *dev)
19121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost;
19141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i;
19151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!scsi_is_fc_rport(dev))
19171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
19181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	shost = dev_to_shost(dev->parent);
19201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
19211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    != &fc_host_class.class)
19221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
19231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i = to_fc_internal(shost->transportt);
19251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->rport_attr_cont.ac == cont;
19271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19295c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
1930a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic void fc_vport_dev_release(struct device *dev)
1931a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1932a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport = dev_to_vport(dev);
1933a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev->parent);		/* release kobj parent */
1934a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	kfree(vport);
1935a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1936a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1937a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartint scsi_is_fc_vport(const struct device *dev)
1938a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1939a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return dev->release == fc_vport_dev_release;
1940a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1941a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James SmartEXPORT_SYMBOL(scsi_is_fc_vport);
1942a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1943a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int fc_vport_match(struct attribute_container *cont,
1944a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			    struct device *dev)
1945a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
1946a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
1947a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost;
1948a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i;
1949a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1950a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!scsi_is_fc_vport(dev))
1951a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return 0;
1952a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport = dev_to_vport(dev);
1953a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1954a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	shost = vport_to_shost(vport);
1955a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1956a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    != &fc_host_class.class)
1957a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return 0;
1958a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1959a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i = to_fc_internal(shost->transportt);
1960a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return &i->vport_attr_cont.ac == cont;
1961a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
1962a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1963a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
1964c829c394165f981d49f05a9be228404d7a9398d4James Smart/**
1965c829c394165f981d49f05a9be228404d7a9398d4James Smart * fc_timed_out - FC Transport I/O timeout intercept handler
1966c829c394165f981d49f05a9be228404d7a9398d4James Smart * @scmd:	The SCSI command which timed out
1967c829c394165f981d49f05a9be228404d7a9398d4James Smart *
1968c829c394165f981d49f05a9be228404d7a9398d4James Smart * This routine protects against error handlers getting invoked while a
1969c829c394165f981d49f05a9be228404d7a9398d4James Smart * rport is in a blocked state, typically due to a temporarily loss of
1970c829c394165f981d49f05a9be228404d7a9398d4James Smart * connectivity. If the error handlers are allowed to proceed, requests
1971c829c394165f981d49f05a9be228404d7a9398d4James Smart * to abort i/o, reset the target, etc will likely fail as there is no way
1972c829c394165f981d49f05a9be228404d7a9398d4James Smart * to communicate with the device to perform the requested function. These
1973c829c394165f981d49f05a9be228404d7a9398d4James Smart * failures may result in the midlayer taking the device offline, requiring
1974c829c394165f981d49f05a9be228404d7a9398d4James Smart * manual intervention to restore operation.
1975c829c394165f981d49f05a9be228404d7a9398d4James Smart *
1976c829c394165f981d49f05a9be228404d7a9398d4James Smart * This routine, called whenever an i/o times out, validates the state of
1977c829c394165f981d49f05a9be228404d7a9398d4James Smart * the underlying rport. If the rport is blocked, it returns
1978c829c394165f981d49f05a9be228404d7a9398d4James Smart * EH_RESET_TIMER, which will continue to reschedule the timeout.
1979c829c394165f981d49f05a9be228404d7a9398d4James Smart * Eventually, either the device will return, or devloss_tmo will fire,
1980c829c394165f981d49f05a9be228404d7a9398d4James Smart * and when the timeout then fires, it will be handled normally.
1981c829c394165f981d49f05a9be228404d7a9398d4James Smart * If the rport is not blocked, normal error handling continues.
1982c829c394165f981d49f05a9be228404d7a9398d4James Smart *
1983c829c394165f981d49f05a9be228404d7a9398d4James Smart * Notes:
1984c829c394165f981d49f05a9be228404d7a9398d4James Smart *	This routine assumes no locks are held on entry.
1985eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
1986242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboestatic enum blk_eh_timer_return
1987c829c394165f981d49f05a9be228404d7a9398d4James Smartfc_timed_out(struct scsi_cmnd *scmd)
1988c829c394165f981d49f05a9be228404d7a9398d4James Smart{
1989c829c394165f981d49f05a9be228404d7a9398d4James Smart	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1990c829c394165f981d49f05a9be228404d7a9398d4James Smart
1991c829c394165f981d49f05a9be228404d7a9398d4James Smart	if (rport->port_state == FC_PORTSTATE_BLOCKED)
1992242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe		return BLK_EH_RESET_TIMER;
1993c829c394165f981d49f05a9be228404d7a9398d4James Smart
1994242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe	return BLK_EH_NOT_HANDLED;
1995c829c394165f981d49f05a9be228404d7a9398d4James Smart}
1996c829c394165f981d49f05a9be228404d7a9398d4James Smart
19975c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com/*
1998bda232531f0c117921690ee3c060953c8f12e5a1James Smart * Called by fc_user_scan to locate an rport on the shost that
1999bda232531f0c117921690ee3c060953c8f12e5a1James Smart * matches the channel and target id, and invoke scsi_scan_target()
2000bda232531f0c117921690ee3c060953c8f12e5a1James Smart * on the rport.
20015c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com */
2002bda232531f0c117921690ee3c060953c8f12e5a1James Smartstatic void
2003bda232531f0c117921690ee3c060953c8f12e5a1James Smartfc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
20045c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com{
20055c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com	struct fc_rport *rport;
2006bda232531f0c117921690ee3c060953c8f12e5a1James Smart	unsigned long flags;
2007bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2008bda232531f0c117921690ee3c060953c8f12e5a1James Smart	spin_lock_irqsave(shost->host_lock, flags);
20095c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
2010e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2011e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig		if (rport->scsi_target_id == -1)
2012e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig			continue;
20135c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
20140d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke		if (rport->port_state != FC_PORTSTATE_ONLINE)
20150d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke			continue;
20160d2fcd9f9858a34f7056b3b1cb60ce0f883d06b6Hannes Reinecke
2017bda232531f0c117921690ee3c060953c8f12e5a1James Smart		if ((channel == rport->channel) &&
2018bda232531f0c117921690ee3c060953c8f12e5a1James Smart		    (id == rport->scsi_target_id)) {
2019bda232531f0c117921690ee3c060953c8f12e5a1James Smart			spin_unlock_irqrestore(shost->host_lock, flags);
2020bda232531f0c117921690ee3c060953c8f12e5a1James Smart			scsi_scan_target(&rport->dev, channel, id, lun, 1);
2021bda232531f0c117921690ee3c060953c8f12e5a1James Smart			return;
2022e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig		}
2023e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	}
2024e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig
2025bda232531f0c117921690ee3c060953c8f12e5a1James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2026bda232531f0c117921690ee3c060953c8f12e5a1James Smart}
2027bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2028bda232531f0c117921690ee3c060953c8f12e5a1James Smart/*
2029bda232531f0c117921690ee3c060953c8f12e5a1James Smart * Called via sysfs scan routines. Necessary, as the FC transport
2030bda232531f0c117921690ee3c060953c8f12e5a1James Smart * wants to place all target objects below the rport object. So this
2031bda232531f0c117921690ee3c060953c8f12e5a1James Smart * routine must invoke the scsi_scan_target() routine with the rport
2032bda232531f0c117921690ee3c060953c8f12e5a1James Smart * object as the parent.
2033bda232531f0c117921690ee3c060953c8f12e5a1James Smart */
2034bda232531f0c117921690ee3c060953c8f12e5a1James Smartstatic int
2035bda232531f0c117921690ee3c060953c8f12e5a1James Smartfc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2036bda232531f0c117921690ee3c060953c8f12e5a1James Smart{
2037bda232531f0c117921690ee3c060953c8f12e5a1James Smart	uint chlo, chhi;
2038bda232531f0c117921690ee3c060953c8f12e5a1James Smart	uint tgtlo, tgthi;
2039bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2040bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2041bda232531f0c117921690ee3c060953c8f12e5a1James Smart	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2042bda232531f0c117921690ee3c060953c8f12e5a1James Smart	    ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2043bda232531f0c117921690ee3c060953c8f12e5a1James Smart		return -EINVAL;
2044bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2045bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (channel == SCAN_WILD_CARD) {
2046bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chlo = 0;
2047bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chhi = shost->max_channel + 1;
2048bda232531f0c117921690ee3c060953c8f12e5a1James Smart	} else {
2049bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chlo = channel;
2050bda232531f0c117921690ee3c060953c8f12e5a1James Smart		chhi = channel + 1;
2051bda232531f0c117921690ee3c060953c8f12e5a1James Smart	}
2052bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2053bda232531f0c117921690ee3c060953c8f12e5a1James Smart	if (id == SCAN_WILD_CARD) {
2054bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgtlo = 0;
2055bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgthi = shost->max_id;
2056bda232531f0c117921690ee3c060953c8f12e5a1James Smart	} else {
2057bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgtlo = id;
2058bda232531f0c117921690ee3c060953c8f12e5a1James Smart		tgthi = id + 1;
2059bda232531f0c117921690ee3c060953c8f12e5a1James Smart	}
2060bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2061bda232531f0c117921690ee3c060953c8f12e5a1James Smart	for ( ; chlo < chhi; chlo++)
2062bda232531f0c117921690ee3c060953c8f12e5a1James Smart		for ( ; tgtlo < tgthi; tgtlo++)
2063bda232531f0c117921690ee3c060953c8f12e5a1James Smart			fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2064bda232531f0c117921690ee3c060953c8f12e5a1James Smart
2065e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	return 0;
20665c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com}
20675c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
20687525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonoristatic int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
20697525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori				int result)
20707525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori{
20717525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	struct fc_internal *i = to_fc_internal(shost->transportt);
20727525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
20737525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori}
20747525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
20757525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonoristatic int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
20767525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori{
20777525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	struct fc_internal *i = to_fc_internal(shost->transportt);
20787525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	return i->f->it_nexus_response(shost, nexus, result);
20797525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori}
20807525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
20811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_transport_template *
20821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_attach_transport(struct fc_function_template *ft)
20831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int count;
208524669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen	struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
208624669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen					GFP_KERNEL);
20871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!i))
20891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
20901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
20921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.class = &fc_transport_class.class;
20931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_attrs.ac.match = fc_target_match;
20941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.target_size = sizeof(struct fc_starget_attrs);
20951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.target_attrs);
20961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
20981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.class = &fc_host_class.class;
20991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_attrs.ac.match = fc_host_match;
21001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.host_size = sizeof(struct fc_host_attrs);
21011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ft->get_fc_host_stats)
21021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i->t.host_attrs.statistics = &fc_statistics_group;
21031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->t.host_attrs);
21041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
21061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.class = &fc_rport_class.class;
21071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attr_cont.ac.match = fc_rport_match;
21081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_register(&i->rport_attr_cont);
21091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2110a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2111a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.class = &fc_vport_class.class;
2112a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attr_cont.ac.match = fc_vport_match;
2113a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_container_register(&i->vport_attr_cont);
2114a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
21151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->f = ft;
21161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport uses the shost workq for scsi scanning */
21181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->t.create_work_queue = 1;
21195c44cd2afad3f7b015542187e147a820600172f1James.Smart@Emulex.Com
2120c829c394165f981d49f05a9be228404d7a9398d4James Smart	i->t.eh_timed_out = fc_timed_out;
2121c829c394165f981d49f05a9be228404d7a9398d4James Smart
2122e02f3f59225d8c3b2a0ad0dc941a09865e27da61Christoph Hellwig	i->t.user_scan = fc_user_scan;
21239ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
21247525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	/* target-mode drivers' functions */
21257525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
21267525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	i->t.it_nexus_response = fc_it_nexus_response;
21277525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
21281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
21291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Target Attributes.
21301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
21311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count = 0;
21321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(node_name);
21331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_name);
21341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_STARGET_ATTRIBUTE_RD(port_id);
21351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_STARGET_NUM_ATTRS);
21371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->starget_attrs[count] = NULL;
21391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
21421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup SCSI Host Attributes.
21431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
21441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
21451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(node_name);
21461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_name);
21476b7281d0a0f8f99d39808088a036459f6f7906a6Andreas Herrmann	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
21481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
21491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
21501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
21511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2152a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_create) {
2153a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2154a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2155a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
21561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(serial_number);
21571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_id);
21591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_type);
21601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(port_state);
21611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
21621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(speed);
21631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2164016131b8fffa1085b4ad165ab228116fdc278ebeJames Smart	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2165b8d08210126a7b769b857720a59721a453a57a1eJames Smart	SETUP_HOST_ATTRIBUTE_RW(system_hostname);
21661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Transport-managed attributes */
21681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
216991ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez	if (ft->issue_fc_host_lip)
217091ca7b01ecc916632202180569a7ddbfccfc3f05Andrew Vasquez		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2171a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_create)
2172a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2173a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (ft->vport_delete)
2174a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
21751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_HOST_NUM_ATTRS);
21771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->host_attrs[count] = NULL;
21791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
21811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Setup Remote Port Attributes.
21821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
21831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	count=0;
21841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
21851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
21861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
21871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
21881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
21891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
21901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
21911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
21921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2193fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
21941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(count > FC_RPORT_NUM_ATTRS);
21961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i->rport_attrs[count] = NULL;
21981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2199a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
2200a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * Setup Virtual Port Attributes.
2201a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
2202a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	count=0;
2203a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2204a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2205a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2206a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2207a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2208a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2209a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2210a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2211a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2212a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2213a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	BUG_ON(count > FC_VPORT_NUM_ATTRS);
2214a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2215a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	i->vport_attrs[count] = NULL;
2216a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
22171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &i->t;
22181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_attach_transport);
22201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid fc_release_transport(struct scsi_transport_template *t)
22221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *i = to_fc_internal(t);
22241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.target_attrs);
22261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->t.host_attrs);
22271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_container_unregister(&i->rport_attr_cont);
2228a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_container_unregister(&i->vport_attr_cont);
22291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(i);
22311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_release_transport);
22331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2234aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2235aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_queue_work - Queue work to the fc_host workqueue.
2236aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2237aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @work:	Work to queue for execution.
2238aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart *
2239aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * Return value:
2240a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart * 	1 - work queued for execution
2241a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart *	0 - work is already queued
2242a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart *	-EINVAL - work queue doesn't exist
2243eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2244aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int
2245aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2246aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2247aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (unlikely(!fc_host_work_q(shost))) {
2248aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2249aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to queue work, "
2250aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2251aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2252aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2253aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -EINVAL;
2254aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2255aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2256aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	return queue_work(fc_host_work_q(shost), work);
2257aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2258aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2259aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2260aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_flush_work - Flush a fc_host's workqueue.
2261aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2262eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2263aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2264aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_flush_work(struct Scsi_Host *shost)
2265aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2266aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host_work_q(shost)) {
2267aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2268aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to flush work, "
2269aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2270aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2271aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return;
2272aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2273aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2274aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	flush_workqueue(fc_host_work_q(shost));
2275aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2276aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2277aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2278aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2279aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2280aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @work:	Work to queue for execution.
2281aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @delay:	jiffies to delay the work queuing
2282aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart *
2283aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * Return value:
22840f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * 	1 on success / 0 already queued / < 0 for error
2285eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2286aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic int
2287c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2288aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				unsigned long delay)
2289aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2290aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (unlikely(!fc_host_devloss_work_q(shost))) {
2291aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2292aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to queue work, "
2293aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2294aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2295aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2296aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return -EINVAL;
2297aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2298aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2299aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2300aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2301aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2302aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2303aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2304aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * @shost:	Pointer to Scsi_Host bound to fc_host.
2305eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2306aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2307aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartfc_flush_devloss(struct Scsi_Host *shost)
2308aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2309aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (!fc_host_devloss_work_q(shost)) {
2310aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		printk(KERN_ERR
2311aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"ERROR: FC host '%s' attempted to flush work, "
2312aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"when no workqueue created.\n", shost->hostt->name);
2313aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		dump_stack();
2314aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		return;
2315aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2316aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2317aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	flush_workqueue(fc_host_devloss_work_q(shost));
2318aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2319aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
23201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2322eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2323eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @shost:	Which &Scsi_Host
23241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
23251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is expected to be called immediately preceeding the
23261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a driver's call to scsi_remove_host().
23271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
23281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * WARNING: A driver utilizing the fc_transport, which fails to call
2329eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *   this routine prior to scsi_remove_host(), will leave dangling
23301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects in /sys/class/fc_remote_ports. Access to any of these
23311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   objects can result in a system crash !!!
23321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
23331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
23341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2335eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
23361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
23371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remove_host(struct Scsi_Host *shost)
23381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2339a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport = NULL, *next_vport = NULL;
2340a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_rport *rport = NULL, *next_rport = NULL;
2341aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct workqueue_struct *work_q;
2342aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2343a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
2344a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2345a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
2346a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2347a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/* Remove any vports */
23489ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
23499ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		fc_queue_work(shost, &vport->vport_delete_work);
23501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Remove any remote ports */
23521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
2353aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			&fc_host->rports, peers) {
2354aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
2355aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
2356aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
2357aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2358aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
23591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(rport, next_rport,
2360aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			&fc_host->rport_bindings, peers) {
2361aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
2362aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
2363aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
2364aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2365aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2366a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2367a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
2368aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all scan work items */
2369aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	scsi_flush_work(shost);
2370aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2371aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all stgt delete, and rport delete work items, then kill it  */
2372aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->work_q) {
2373aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		work_q = fc_host->work_q;
2374aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->work_q = NULL;
2375aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(work_q);
2376aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
2377aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2378aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* flush all devloss work items, then kill it  */
2379aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->devloss_work_q) {
2380aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		work_q = fc_host->devloss_work_q;
2381aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_host->devloss_work_q = NULL;
2382aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		destroy_workqueue(work_q);
2383aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
23841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
23851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remove_host);
23861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2387fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christiestatic void fc_terminate_rport_io(struct fc_rport *rport)
2388fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie{
2389fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	struct Scsi_Host *shost = rport_to_shost(rport);
2390fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	struct fc_internal *i = to_fc_internal(shost->transportt);
2391fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie
2392fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	/* Involve the LLDD if possible to terminate all io on the rport. */
2393fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	if (i->f->terminate_rport_io)
2394fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie		i->f->terminate_rport_io(rport);
2395fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie
2396fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	/*
2397fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 * must unblock to flush queued IO. The caller will have set
2398fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 * the port_state or flags, so that fc_remote_port_chkready will
2399fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 * fail IO.
2400fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	 */
2401fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	scsi_target_unblock(&rport->dev);
2402fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie}
2403aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2404aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2405aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_starget_delete - called to delete the scsi decendents of an rport
2406c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be operated on.
2407eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
2408eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Deletes target and all sdevs.
2409eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
241019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Comstatic void
2411c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_starget_delete(struct work_struct *work)
241219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com{
2413c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2414c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, stgt_delete_work);
241519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2416fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
241719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_remove_target(&rport->dev);
241819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com}
241919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2420aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2421aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart/**
2422aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart * fc_rport_final_delete - finish rport termination and delete it.
2423c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be deleted.
2424eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
2425aedf349773e5877d716a89368d512b9baa3e8c7bJames Smartstatic void
2426c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_rport_final_delete(struct work_struct *work)
2427aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart{
2428c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
2429c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, rport_delete_work);
2430aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct device *dev = &rport->dev;
2431aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
24320f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
243392740b24ce6ddac6534fae985aab602548692186James Smart	unsigned long flags;
24348798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	int do_callback = 0;
2435aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2436aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/*
24379ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	 * if a scan is pending, flush the SCSI Host work_q so that
2438aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * that we can reclaim the rport scan work element.
2439aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 */
2440aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (rport->flags & FC_RPORT_SCAN_PENDING)
2441aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		scsi_flush_work(shost);
2442aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2443fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
24449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
244592740b24ce6ddac6534fae985aab602548692186James Smart	/*
244692740b24ce6ddac6534fae985aab602548692186James Smart	 * Cancel any outstanding timers. These should really exist
244792740b24ce6ddac6534fae985aab602548692186James Smart	 * only when rmmod'ing the LLDD and we're asking for
244892740b24ce6ddac6534fae985aab602548692186James Smart	 * immediate termination of the rports
244992740b24ce6ddac6534fae985aab602548692186James Smart	 */
245092740b24ce6ddac6534fae985aab602548692186James Smart	spin_lock_irqsave(shost->host_lock, flags);
245192740b24ce6ddac6534fae985aab602548692186James Smart	if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
245292740b24ce6ddac6534fae985aab602548692186James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
245392740b24ce6ddac6534fae985aab602548692186James Smart		if (!cancel_delayed_work(&rport->fail_io_work))
245492740b24ce6ddac6534fae985aab602548692186James Smart			fc_flush_devloss(shost);
245592740b24ce6ddac6534fae985aab602548692186James Smart		if (!cancel_delayed_work(&rport->dev_loss_work))
245692740b24ce6ddac6534fae985aab602548692186James Smart			fc_flush_devloss(shost);
245792740b24ce6ddac6534fae985aab602548692186James Smart		spin_lock_irqsave(shost->host_lock, flags);
245892740b24ce6ddac6534fae985aab602548692186James Smart		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
245992740b24ce6ddac6534fae985aab602548692186James Smart	}
246092740b24ce6ddac6534fae985aab602548692186James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
246192740b24ce6ddac6534fae985aab602548692186James Smart
24620f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	/* Delete SCSI target and sdevs */
24630f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->scsi_target_id != -1)
2464c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		fc_starget_delete(&rport->stgt_delete_work);
246592740b24ce6ddac6534fae985aab602548692186James Smart
246692740b24ce6ddac6534fae985aab602548692186James Smart	/*
246792740b24ce6ddac6534fae985aab602548692186James Smart	 * Notify the driver that the rport is now dead. The LLDD will
246892740b24ce6ddac6534fae985aab602548692186James Smart	 * also guarantee that any communication to the rport is terminated
24694be98c0ca304c8a47998b29a7993664f71791250James Smart	 *
24704be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Avoid this call if we already called it when we preserved the
24714be98c0ca304c8a47998b29a7993664f71791250James Smart	 * rport for the binding.
247292740b24ce6ddac6534fae985aab602548692186James Smart	 */
24738798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_lock_irqsave(shost->host_lock, flags);
24744be98c0ca304c8a47998b29a7993664f71791250James Smart	if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
24758798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	    (i->f->dev_loss_tmo_callbk)) {
24768798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
24778798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		do_callback = 1;
24788798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	}
24798798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_unlock_irqrestore(shost->host_lock, flags);
24808798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
24818798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	if (do_callback)
24820f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		i->f->dev_loss_tmo_callbk(rport);
24830f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
24849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_remove(rport->rqst_q);
24859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
2486aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	transport_remove_device(dev);
2487aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	device_del(dev);
2488aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	transport_destroy_device(dev);
24893bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
24903bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(dev);			/* for self-reference */
2491aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart}
2492aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2493aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
24941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
24951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_rport_create - allocates and creates a remote FC port.
24961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
24971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
24981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
24991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
25001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Allocates and creates the remoter port structure, including the
25021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * class and sysfs creation.
25031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
25041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
25051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2506eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
250744818efbad185eea75adad0e8cab97650a7370abAdrian Bunkstatic struct fc_rport *
25081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_rport_create(struct Scsi_Host *shost, int channel,
25091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
25101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2511aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
25121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_internal *fci = to_fc_internal(shost->transportt);
25131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
25141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct device *dev;
25151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
25161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int error;
25171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t size;
25181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
252024669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen	rport = kzalloc(size, GFP_KERNEL);
25211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(!rport)) {
2522cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		printk(KERN_ERR "%s: allocation failure\n", __func__);
25231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return NULL;
25241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
25251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->maxframe_size = -1;
25271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->supported_classes = FC_COS_UNSPECIFIED;
25281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->dev_loss_tmo = fc_dev_loss_tmo;
25291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
25301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
25311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_id = ids->port_id;
25321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->roles = ids->roles;
25331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->port_state = FC_PORTSTATE_ONLINE;
25341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (fci->f->dd_fcrport_size)
25351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->dd_data = &rport[1];
25361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->channel = channel;
25370f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	rport->fast_io_fail_tmo = -1;
25381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2539c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2540c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2541c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2542c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2543c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
25441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
25461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport->number = fc_host->next_rport_number++;
2548a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
25491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = fc_host->next_target_id++;
25501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
25511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rport->scsi_target_id = -1;
2552aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_add_tail(&rport->peers, &fc_host->rports);
25533bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	get_device(&shost->shost_gendev);	/* for fc_host->rport list */
25541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
25561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev = &rport->dev;
25583bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	device_initialize(dev);			/* takes self reference */
25593bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	dev->parent = get_device(&shost->shost_gendev); /* parent reference */
25601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev->release = fc_rport_dev_release;
256171610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers	dev_set_name(dev, "rport-%d:%d-%d",
256271610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		     shost->host_no, channel, rport->number);
25631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_setup_device(dev);
25641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = device_add(dev);
25661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (error) {
25671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "FC Remote Port device_add failed\n");
25681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto delete_rport;
25691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
25701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_add_device(dev);
25711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_configure_device(dev);
25721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_rportadd(shost, rport);
25749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* ignore any bsg add error - we just can't do sgio */
25759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
2576a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
25771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
2578aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->flags |= FC_RPORT_SCAN_PENDING;
25791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
2580aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	}
25811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
25831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsdelete_rport:
25851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	transport_destroy_device(dev);
25861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
25871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_del(&rport->peers);
25883bdad7bd253f17ead00b4af2e82f84e9522c95acJames Smart	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
25891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(shost->host_lock, flags);
25901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	put_device(dev->parent);
25911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(rport);
25921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
25931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
25941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2596eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
25971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @shost:	scsi host the remote port is connected to.
25981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @channel:	Channel on shost port connected to.
25991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @ids:	The world wide names, fc address, and FC4 port
26001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		roles for the remote port.
26011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
26021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport of the existence
26031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
26041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of the port, it's FC address (port_id), and the FC4 roles that are
26051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * active for the port.
26061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
26071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * For ports that are FCP targets (aka scsi targets), the FC transport
26081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * maintains consistent target id bindings on behalf of the LLDD.
26091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * A consistent target id binding is an assignment of a target id to
26101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * a remote port identifier, which persists while the scsi host is
26111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * attached. The remote port can disappear, then later reappear, and
26121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it's target id assignment remains the same. This allows for shifts
26131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in FC addressing (if binding by wwpn or wwnn) with no apparent
26141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * changes to the scsi subsystem which is based on scsi host number and
26151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * target id values.  Bindings are only valid during the attachment of
26161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the scsi host. If the host detaches, then later re-attaches, target
26171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * id bindings may change.
26181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
26191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This routine is responsible for returning a remote port structure.
26201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The routine will search the list of remote ports it maintains
26211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * internally on behalf of consistent target id mappings. If found, the
26221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * remote port structure will be reused. Otherwise, a new remote port
26231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * structure will be allocated.
26241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
26251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Whenever a remote port is allocated, a new fc_remote_port class
26261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device is created.
26271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
26281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
26291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
26301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
26311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2632eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
26331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct fc_rport *
26341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_add(struct Scsi_Host *shost, int channel,
26351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport_identifiers  *ids)
26361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
263719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct fc_internal *fci = to_fc_internal(shost->transportt);
2638aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
26391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct fc_rport *rport;
26401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
26411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int match = 0;
26421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2643aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/* ensure any stgt delete functions are done */
2644aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_flush_work(shost);
2645aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
264619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
264719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Search the list of "active" rports, for an rport that has been
264819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * deleted, but we've held off the real delete while the target
264919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * is in a "blocked" state.
265019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
265119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
265219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2653aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_for_each_entry(rport, &fc_host->rports, peers) {
265419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
265519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
265619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			(rport->channel == channel)) {
265719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2658aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			switch (fc_host->tgtid_bind_type) {
265919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWPN:
266019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_NONE:
266119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_name == ids->port_name)
266219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
266319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
266419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_WWNN:
266519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->node_name == ids->node_name)
266619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
266719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
266819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			case FC_TGTID_BIND_BY_ID:
266919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (rport->port_id == ids->port_id)
267019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					match = 1;
267119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				break;
267219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
267319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
267419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (match) {
267519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
267619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->node_name, &ids->node_name,
267719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->node_name));
267819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memcpy(&rport->port_name, &ids->port_name,
267919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					sizeof(rport->port_name));
268019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_id = ids->port_id;
268119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
268219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->port_state = FC_PORTSTATE_ONLINE;
268319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				rport->roles = ids->roles;
268419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
268519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				spin_unlock_irqrestore(shost->host_lock, flags);
268619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
268719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				if (fci->f->dd_fcrport_size)
268819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com					memset(rport->dd_data, 0,
268919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
269019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
269119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
269292740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were not a target, cancel the
269392740b24ce6ddac6534fae985aab602548692186James Smart				 * io terminate and rport timers, and
269492740b24ce6ddac6534fae985aab602548692186James Smart				 * we're done.
269592740b24ce6ddac6534fae985aab602548692186James Smart				 *
269692740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were a target, but our new role
269792740b24ce6ddac6534fae985aab602548692186James Smart				 * doesn't indicate a target, leave the
269892740b24ce6ddac6534fae985aab602548692186James Smart				 * timers running expecting the role to
269992740b24ce6ddac6534fae985aab602548692186James Smart				 * change as the target fully logs in. If
270092740b24ce6ddac6534fae985aab602548692186James Smart				 * it doesn't, the target will be torn down.
270192740b24ce6ddac6534fae985aab602548692186James Smart				 *
270292740b24ce6ddac6534fae985aab602548692186James Smart				 * If we were a target, and our role shows
270392740b24ce6ddac6534fae985aab602548692186James Smart				 * we're still a target, cancel the timers
270492740b24ce6ddac6534fae985aab602548692186James Smart				 * and kick off a scan.
270519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
270619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
270792740b24ce6ddac6534fae985aab602548692186James Smart				/* was a target, not in roles */
270892740b24ce6ddac6534fae985aab602548692186James Smart				if ((rport->scsi_target_id != -1) &&
2709a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				    (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
271092740b24ce6ddac6534fae985aab602548692186James Smart					return rport;
271119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
271219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				/*
271392740b24ce6ddac6534fae985aab602548692186James Smart				 * Stop the fail io and dev_loss timers.
271492740b24ce6ddac6534fae985aab602548692186James Smart				 * If they flush, the port_state will
271592740b24ce6ddac6534fae985aab602548692186James Smart				 * be checked and will NOOP the function.
271619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				 */
27170f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart				if (!cancel_delayed_work(&rport->fail_io_work))
27180f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart					fc_flush_devloss(shost);
271992740b24ce6ddac6534fae985aab602548692186James Smart				if (!cancel_delayed_work(&rport->dev_loss_work))
2720aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart					fc_flush_devloss(shost);
2721aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2722aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				spin_lock_irqsave(shost->host_lock, flags);
2723aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2724fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie				rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
27254be98c0ca304c8a47998b29a7993664f71791250James Smart						  FC_RPORT_DEVLOSS_PENDING |
27264be98c0ca304c8a47998b29a7993664f71791250James Smart						  FC_RPORT_DEVLOSS_CALLBK_DONE);
272719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
272892740b24ce6ddac6534fae985aab602548692186James Smart				/* if target, initiate a scan */
272992740b24ce6ddac6534fae985aab602548692186James Smart				if (rport->scsi_target_id != -1) {
273092740b24ce6ddac6534fae985aab602548692186James Smart					rport->flags |= FC_RPORT_SCAN_PENDING;
273192740b24ce6ddac6534fae985aab602548692186James Smart					scsi_queue_work(shost,
273292740b24ce6ddac6534fae985aab602548692186James Smart							&rport->scan_work);
273392740b24ce6ddac6534fae985aab602548692186James Smart					spin_unlock_irqrestore(shost->host_lock,
273492740b24ce6ddac6534fae985aab602548692186James Smart							flags);
273592740b24ce6ddac6534fae985aab602548692186James Smart					scsi_target_unblock(&rport->dev);
273692740b24ce6ddac6534fae985aab602548692186James Smart				} else
273792740b24ce6ddac6534fae985aab602548692186James Smart					spin_unlock_irqrestore(shost->host_lock,
273892740b24ce6ddac6534fae985aab602548692186James Smart							flags);
2739a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart
27409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				fc_bsg_goose_queue(rport);
27419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
274219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				return rport;
274319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			}
274419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		}
274519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
274619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
274792740b24ce6ddac6534fae985aab602548692186James Smart	/*
274892740b24ce6ddac6534fae985aab602548692186James Smart	 * Search the bindings array
274992740b24ce6ddac6534fae985aab602548692186James Smart	 * Note: if never a FCP target, you won't be on this list
275092740b24ce6ddac6534fae985aab602548692186James Smart	 */
2751aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
27521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* search for a matching consistent binding */
27541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2755aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_for_each_entry(rport, &fc_host->rport_bindings,
27561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					peers) {
27571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (rport->channel != channel)
27581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				continue;
27591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2760aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			switch (fc_host->tgtid_bind_type) {
27611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWPN:
27621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_name == ids->port_name)
27631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
27641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_WWNN:
27661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->node_name == ids->node_name)
27671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
27681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_BY_ID:
27701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (rport->port_id == ids->port_id)
27711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					match = 1;
27721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			case FC_TGTID_BIND_NONE: /* to keep compiler happy */
27741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
27761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (match) {
2778aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				list_move_tail(&rport->peers, &fc_host->rports);
27791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
27801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
27811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
27821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (match) {
27841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->node_name, &ids->node_name,
27851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->node_name));
27861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			memcpy(&rport->port_name, &ids->port_name,
27871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				sizeof(rport->port_name));
27881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_id = ids->port_id;
27891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->roles = ids->roles;
27901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rport->port_state = FC_PORTSTATE_ONLINE;
2791fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie			rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
27921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
279319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			if (fci->f->dd_fcrport_size)
279419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com				memset(rport->dd_data, 0,
279519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com						fci->f->dd_fcrport_size);
279619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2797a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
27981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				/* initiate a scan of the target */
2799aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart				rport->flags |= FC_RPORT_SCAN_PENDING;
28001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				scsi_queue_work(shost, &rport->scan_work);
2801a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				spin_unlock_irqrestore(shost->host_lock, flags);
2802a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				scsi_target_unblock(&rport->dev);
2803a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart			} else
2804a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart				spin_unlock_irqrestore(shost->host_lock, flags);
28051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return rport;
28071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
28081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
28091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
281019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
281119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
28121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* No consistent binding found - create new remote port entry */
28131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rport = fc_rport_create(shost, channel, ids);
28141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rport;
28161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
28171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_add);
28181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2821eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
28221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that no longer exists
28231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
28241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The LLDD calls this routine to notify the transport that a remote
28251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * port is no longer part of the topology. Note: Although a port
28261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * may no longer be part of the topology, it may persist in the remote
282719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * ports displayed by the fc_host. We do this under 2 conditions:
2828eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
282919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   This allows the port to temporarily disappear, then reappear without
283019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   disrupting the SCSI device tree attached to it. During the "blocked"
283119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   period the port will still exist.
2832eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * 2) If the port was a scsi target and disappears for longer than we
283319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   expect, we'll delete the port and the tear down the SCSI device tree
283419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   attached to it. However, we want to semi-persist the target id assigned
283519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to that port if it eventually does exist. The port structure will
283619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   remain (although with minimal information) so that the target id
283719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   bindings remails.
28381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
28391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If the remote port is not an FCP Target, it will be fully torn down
28401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and deallocated, including the fc_remote_port class device.
28411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
284219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * If the remote port is an FCP Target, the port will be placed in a
284319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * temporary blocked state. From the LLDD's perspective, the rport no
284419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * longer exists. From the SCSI midlayer's perspective, the SCSI target
284519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * exists, but all sdevs on it are blocked from further I/O. The following
2846eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * is then expected.
2847eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
284819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port does not return (signaled by a LLDD call to
284919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
285019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi target is removed - killing all outstanding i/o and removing the
285119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi devices attached ot it. The port structure will be marked Not
285219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   Present and be partially cleared, leaving only enough information to
285319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   recognize the remote port relative to the scsi target id binding if
285419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   it later appears.  The port will remain as long as there is a valid
285519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   binding (e.g. until the user changes the binding type or unloads the
285619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   scsi host with the binding).
28571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
285819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   If the remote port returns within the dev_loss_tmo value (and matches
285919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   according to the target id binding type), the port structure will be
286019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   reused. If it is no longer a SCSI target, the target will be torn
286119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   down. If it continues to be a SCSI target, then the target will be
286219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   unblocked (allowing i/o to be resumed), and a scan will be activated
286319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *   to ensure that all luns are detected.
286419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com *
286519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com * Called from normal process context only - cannot be called from interrupt.
28661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
28671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
28681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2869eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
28701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
28711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_delete(struct fc_rport  *rport)
28721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2873aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
287436dd288f0f930c154ec6a4d73a6a35f3079418c6Hannes Reinecke	unsigned long timeout = rport->dev_loss_tmo;
2875aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	unsigned long flags;
2876aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2877aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	/*
2878aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * No need to flush the fc_host work_q's, as all adds are synchronous.
2879aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 *
2880aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * We do need to reclaim the rport scan work element, so eventually
2881aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2882aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 * there's still a scan pending.
2883aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	 */
2884aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2885aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_lock_irqsave(shost->host_lock, flags);
28861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
288792740b24ce6ddac6534fae985aab602548692186James Smart	if (rport->port_state != FC_PORTSTATE_ONLINE) {
2888aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
28891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
28901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
28911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
289292740b24ce6ddac6534fae985aab602548692186James Smart	/*
289392740b24ce6ddac6534fae985aab602548692186James Smart	 * In the past, we if this was not an FCP-Target, we would
289492740b24ce6ddac6534fae985aab602548692186James Smart	 * unconditionally just jump to deleting the rport.
289592740b24ce6ddac6534fae985aab602548692186James Smart	 * However, rports can be used as node containers by the LLDD,
289692740b24ce6ddac6534fae985aab602548692186James Smart	 * and its not appropriate to just terminate the rport at the
289792740b24ce6ddac6534fae985aab602548692186James Smart	 * first sign of a loss in connectivity. The LLDD may want to
289892740b24ce6ddac6534fae985aab602548692186James Smart	 * send ELS traffic to re-validate the login. If the rport is
289992740b24ce6ddac6534fae985aab602548692186James Smart	 * immediately deleted, it makes it inappropriate for a node
290092740b24ce6ddac6534fae985aab602548692186James Smart	 * container.
290192740b24ce6ddac6534fae985aab602548692186James Smart	 * So... we now unconditionally wait dev_loss_tmo before
290292740b24ce6ddac6534fae985aab602548692186James Smart	 * destroying an rport.
290392740b24ce6ddac6534fae985aab602548692186James Smart	 */
290492740b24ce6ddac6534fae985aab602548692186James Smart
2905aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->port_state = FC_PORTSTATE_BLOCKED;
2906aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2907aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2908aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2909aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2910aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
29117525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
29127525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	    shost->active_mode & MODE_TARGET)
29137525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
29147525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori
291519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	scsi_target_block(&rport->dev);
29161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29170f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	/* see if we need to kill io faster than waiting for device loss */
29180f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if ((rport->fast_io_fail_tmo != -1) &&
2919fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	    (rport->fast_io_fail_tmo < timeout))
29200f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		fc_queue_devloss_work(shost, &rport->fail_io_work,
29210f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart					rport->fast_io_fail_tmo * HZ);
29220f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
292319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/* cap the length the devices can be blocked until they are deleted */
2924aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
29251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
29261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_delete);
29271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
2929eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
29301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @rport:	The remote port that changed.
2931eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * @roles:      New roles for this port.
29321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2933eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Description: The LLDD calls this routine to notify the transport that the
2934eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * roles on a remote port may have changed. The largest effect of this is
29351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * if a port now becomes a FCP Target, it must be allocated a
29361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id.  If the port is no longer a FCP target, any
29371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi target id value assigned to it will persist in case the
29381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * role changes back to include FCP Target. No changes in the scsi
29391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * midlayer will be invoked if the role changes (in the expectation
29401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that the role will be resumed. If it doesn't normal error processing
29411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * will take place).
29421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
29431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Should not be called from interrupt context.
29441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
29451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notes:
29461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	This routine assumes no locks are held on entry.
2947eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
29481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid
29491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
29501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
29511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *shost = rport_to_shost(rport);
2952aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
29531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
29541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int create = 0;
29557525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	int ret;
29561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(shost->host_lock, flags);
2958a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (roles & FC_PORT_ROLE_FCP_TARGET) {
295919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (rport->scsi_target_id == -1) {
296019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			rport->scsi_target_id = fc_host->next_target_id++;
296119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
2962a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		} else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
296319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com			create = 1;
29647525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori	} else if (shost->active_mode & MODE_TARGET) {
29657525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
29667525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori					     (char *)&rport->node_name);
29677525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori		if (ret)
29687525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori			printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
29697525236d0bc7ad17eb5e0733417896cab745d6c8FUJITA Tomonori			       ret);
29701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
29711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
297219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->roles = roles;
297319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
2974aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_unlock_irqrestore(shost->host_lock, flags);
2975aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
297619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (create) {
297719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		/*
297819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * There may have been a delete timer running on the
297919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * port. Ensure that it is cancelled as we now know
298019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * the port is an FCP Target.
298119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Note: we know the rport is exists and in an online
298219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  state as the LLDD would not have had an rport
298319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *  reference to pass us.
298419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 *
298519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * Take no action on the del_timer failure as the state
298619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * machine state change will validate the
298719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 * transaction.
298819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		 */
29890f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		if (!cancel_delayed_work(&rport->fail_io_work))
29900f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart			fc_flush_devloss(shost);
299119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		if (!cancel_delayed_work(&rport->dev_loss_work))
2992aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			fc_flush_devloss(shost);
2993aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2994aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_lock_irqsave(shost->host_lock, flags);
2995fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie		rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2996fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie				  FC_RPORT_DEVLOSS_PENDING);
2997aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
2998aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
2999aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		/* ensure any stgt delete functions are done */
3000aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_flush_work(shost);
300119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
30021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initiate a scan of the target */
3003aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_lock_irqsave(shost->host_lock, flags);
3004aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->flags |= FC_RPORT_SCAN_PENDING;
30051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		scsi_queue_work(shost, &rport->scan_work);
3006aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3007a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		scsi_target_unblock(&rport->dev);
300819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
30091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
30101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(fc_remote_port_rolechg);
30111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
3013eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
301492740b24ce6ddac6534fae985aab602548692186James Smart * @work:	rport target that failed to reappear in the allotted time.
3015eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *
3016eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * Description: An attempt to delete a remote port blocks, and if it fails
3017eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley *              to return in the allotted time this gets called.
3018eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
30191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
3020c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_timeout_deleted_rport(struct work_struct *work)
30211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3022c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3023c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, dev_loss_work.work);
302419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	struct Scsi_Host *shost = rport_to_shost(rport);
30254be98c0ca304c8a47998b29a7993664f71791250James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
3026aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
302719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	unsigned long flags;
30288798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	int do_callback = 0;
30291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
303019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	spin_lock_irqsave(shost->host_lock, flags);
30311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3032aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3033aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
30341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
303592740b24ce6ddac6534fae985aab602548692186James Smart	 * If the port is ONLINE, then it came back. If it was a SCSI
303692740b24ce6ddac6534fae985aab602548692186James Smart	 * target, validate it still is. If not, tear down the
303792740b24ce6ddac6534fae985aab602548692186James Smart	 * scsi_target on it.
30381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3039aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
304092740b24ce6ddac6534fae985aab602548692186James Smart	    (rport->scsi_target_id != -1) &&
3041a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	    !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
304219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
3043aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			"blocked FC remote port time out: no longer"
3044aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			" a FCP target, removing starget\n");
3045aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3046a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		scsi_target_unblock(&rport->dev);
3047a0785edff7b316cad566bdc369d3d034c4c1a39cJames Smart		fc_queue_work(shost, &rport->stgt_delete_work);
304819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
304919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
30501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
305192740b24ce6ddac6534fae985aab602548692186James Smart	/* NOOP state - we're flushing workq's */
305219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
305319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		spin_unlock_irqrestore(shost->host_lock, flags);
305419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
305592740b24ce6ddac6534fae985aab602548692186James Smart			"blocked FC remote port time out: leaving"
305692740b24ce6ddac6534fae985aab602548692186James Smart			" rport%s alone\n",
305792740b24ce6ddac6534fae985aab602548692186James Smart			(rport->scsi_target_id != -1) ?  " and starget" : "");
305819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
305919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
30601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
306192740b24ce6ddac6534fae985aab602548692186James Smart	if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
306292740b24ce6ddac6534fae985aab602548692186James Smart	    (rport->scsi_target_id == -1)) {
3063aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		list_del(&rport->peers);
3064aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		rport->port_state = FC_PORTSTATE_DELETED;
306519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		dev_printk(KERN_ERR, &rport->dev,
306692740b24ce6ddac6534fae985aab602548692186James Smart			"blocked FC remote port time out: removing"
306792740b24ce6ddac6534fae985aab602548692186James Smart			" rport%s\n",
306892740b24ce6ddac6534fae985aab602548692186James Smart			(rport->scsi_target_id != -1) ?  " and starget" : "");
3069aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		fc_queue_work(shost, &rport->rport_delete_work);
3070aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		spin_unlock_irqrestore(shost->host_lock, flags);
307119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		return;
307219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
30731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
307419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	dev_printk(KERN_ERR, &rport->dev,
307519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"blocked FC remote port time out: removing target and "
307619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com		"saving binding\n");
307719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
3078aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	list_move_tail(&rport->peers, &fc_host->rport_bindings);
30791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
308119a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Note: We do not remove or clear the hostdata area. This allows
308219a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   host-specific target data to persist along with the
308319a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 *   scsi_target_id. It's up to the host to manage it's hostdata area.
30841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
30851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
308619a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	/*
308719a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 * Reinitialize port attributes that may change if the port comes back.
308819a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	 */
308919a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->maxframe_size = -1;
309019a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	rport->supported_classes = FC_COS_UNSPECIFIED;
3091a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	rport->roles = FC_PORT_ROLE_UNKNOWN;
3092aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->port_state = FC_PORTSTATE_NOTPRESENT;
3093fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
30941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3095f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	/*
3096f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * Pre-emptively kill I/O rather than waiting for the work queue
3097f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * item to teardown the starget. (FCOE libFC folks prefer this
3098f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 * and to have the rport_port_id still set when it's done).
3099f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	 */
3100f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3101f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart	fc_terminate_rport_io(rport);
3102f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart
31038798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_lock_irqsave(shost->host_lock, flags);
31048798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
31058798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	if (rport->port_state == FC_PORTSTATE_NOTPRESENT) {	/* still missing */
3106f78badb1ae07e7f8b835ab2ea0b456ed3fc4caf4James Smart
31078798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		/* remove the identifiers that aren't used in the consisting binding */
31088798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		switch (fc_host->tgtid_bind_type) {
31098798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_BY_WWPN:
31108798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->node_name = -1;
31118798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_id = -1;
31128798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
31138798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_BY_WWNN:
31148798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_name = -1;
31158798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_id = -1;
31168798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
31178798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_BY_ID:
31188798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->node_name = -1;
31198798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			rport->port_name = -1;
31208798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
31218798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		case FC_TGTID_BIND_NONE:	/* to keep compiler happy */
31228798a694da59486e4a3ff0abeec183202fb34c20Michael Reed			break;
31238798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		}
31248798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
31258798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		/*
31268798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 * As this only occurs if the remote port (scsi target)
31278798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 * went away and didn't come back - we'll remove
31288798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 * all attached scsi devices.
31298798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		 */
31308798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
31318798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		fc_queue_work(shost, &rport->stgt_delete_work);
31328798a694da59486e4a3ff0abeec183202fb34c20Michael Reed
31338798a694da59486e4a3ff0abeec183202fb34c20Michael Reed		do_callback = 1;
313419a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com	}
313519a7b4aebf9ad435c69a7e39930338499af4d152James.Smart@Emulex.Com
31368798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	spin_unlock_irqrestore(shost->host_lock, flags);
31374be98c0ca304c8a47998b29a7993664f71791250James Smart
31384be98c0ca304c8a47998b29a7993664f71791250James Smart	/*
31394be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Notify the driver that the rport is now dead. The LLDD will
31404be98c0ca304c8a47998b29a7993664f71791250James Smart	 * also guarantee that any communication to the rport is terminated
31414be98c0ca304c8a47998b29a7993664f71791250James Smart	 *
31424be98c0ca304c8a47998b29a7993664f71791250James Smart	 * Note: we set the CALLBK_DONE flag above to correspond
31434be98c0ca304c8a47998b29a7993664f71791250James Smart	 */
31448798a694da59486e4a3ff0abeec183202fb34c20Michael Reed	if (do_callback && i->f->dev_loss_tmo_callbk)
31454be98c0ca304c8a47998b29a7993664f71791250James Smart		i->f->dev_loss_tmo_callbk(rport);
31461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
31471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
31484be98c0ca304c8a47998b29a7993664f71791250James Smart
31491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/**
3150eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3151c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	rport to terminate io on.
31520f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart *
31530f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart * Notes: Only requests the failure of the io, not that all are flushed
31540f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart *    prior to returning.
3155eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
31560f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smartstatic void
3157c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_timeout_fail_rport_io(struct work_struct *work)
31580f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart{
3159c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3160c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, fail_io_work.work);
31610f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
31620f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart	if (rport->port_state != FC_PORTSTATE_BLOCKED)
31630f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart		return;
31640f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
3165fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3166fff9d40ce0eb4b46f3e186823ceab6bc02c3e5d3Mike Christie	fc_terminate_rport_io(rport);
31670f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart}
31680f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart
31690f29b966d60e9a4f5ecff9f3832257b38aea4f13James Smart/**
31701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3171c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells * @work:	remote port to be scanned.
3172eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
31731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
3174c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsfc_scsi_scan_rport(struct work_struct *work)
31751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3176c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct fc_rport *rport =
3177c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		container_of(work, struct fc_rport, scan_work);
3178aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	struct Scsi_Host *shost = rport_to_shost(rport);
317903f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	struct fc_internal *i = to_fc_internal(shost->transportt);
318042e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	unsigned long flags;
318142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
3182aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
318303f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	    (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
318403f002f778e58e9056e8e9a22952c5c6f4d76269Christof Schmitt	    !(i->f->disable_target_scan)) {
3185aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart		scsi_scan_target(&rport->dev, rport->channel,
3186aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart			rport->scsi_target_id, SCAN_WILD_CARD, 1);
318742e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	}
3188aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart
3189aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	spin_lock_irqsave(shost->host_lock, flags);
3190aedf349773e5877d716a89368d512b9baa3e8c7bJames Smart	rport->flags &= ~FC_RPORT_SCAN_PENDING;
319142e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com	spin_unlock_irqrestore(shost->host_lock, flags);
319242e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com}
319342e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
319465d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt/**
319565d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
319665d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * @cmnd: SCSI command that scsi_eh is trying to recover
319765d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt *
319865d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * This routine can be called from a FC LLD scsi_eh callback. It
319965d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt * blocks the scsi_eh thread until the fc_rport leaves the
32002f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
32012f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt * necessary to avoid the scsi_eh failing recovery actions for blocked
32022f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt * rports which would lead to offlined SCSI devices.
32032f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt *
32042f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
32052f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt *	    FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
32062f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt *	    passed back to scsi_eh.
320765d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt */
32082f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmittint fc_block_scsi_eh(struct scsi_cmnd *cmnd)
320965d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt{
321065d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	struct Scsi_Host *shost = cmnd->device->host;
321165d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
321265d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	unsigned long flags;
321365d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt
321465d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	spin_lock_irqsave(shost->host_lock, flags);
32152f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt	while (rport->port_state == FC_PORTSTATE_BLOCKED &&
32162f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt	       !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
321765d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt		spin_unlock_irqrestore(shost->host_lock, flags);
321865d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt		msleep(1000);
321965d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt		spin_lock_irqsave(shost->host_lock, flags);
322065d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	}
322165d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt	spin_unlock_irqrestore(shost->host_lock, flags);
32222f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt
32232f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt	if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
32242f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt		return FAST_IO_FAIL;
32252f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt
32262f2eb58762b4dcddfe25c90800323765c1257ecaChristof Schmitt	return 0;
322765d430fa99cbd0e88d09a3343f697c51fc8a7009Christof Schmitt}
322865d430fa99cbd0e88d09a3343f697c51fc8a7009Christof SchmittEXPORT_SYMBOL(fc_block_scsi_eh);
322942e33148df38c60b99d984b76b302c64397ebe4cJames.Smart@Emulex.Com
3230a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/**
3231a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * fc_vport_setup - allocates and creates a FC virtual port.
3232a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @shost:	scsi host the virtual port is connected to.
3233a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @channel:	Channel on shost port connected to.
3234a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @pdev:	parent device for vport
3235a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @ids:	The world wide names, FC4 port roles, etc for
3236a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *              the virtual port.
3237a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @ret_vport:	The pointer to the created vport.
3238a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3239a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Allocates and creates the vport structure, calls the parent host
3240a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * to instantiate the vport, the completes w/ class and sysfs creation.
3241a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3242a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Notes:
3243a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *	This routine assumes no locks are held on entry.
3244eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
3245a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartstatic int
3246a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezfc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3247a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport_identifiers  *ids, struct fc_vport **ret_vport)
3248a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
3249a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3250a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *fci = to_fc_internal(shost->transportt);
3251a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_vport *vport;
3252a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *dev;
3253a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
3254a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	size_t size;
3255a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int error;
3256a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3257a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*ret_vport = NULL;
3258a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3259a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if ( ! fci->f->vport_create)
3260a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOENT;
3261a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3262a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3263a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport = kzalloc(size, GFP_KERNEL);
3264a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (unlikely(!vport)) {
3265cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison		printk(KERN_ERR "%s: allocation failure\n", __func__);
3266a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOMEM;
3267a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3268a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3269a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_state = FC_VPORT_UNKNOWN;
3270a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_last_state = FC_VPORT_UNKNOWN;
3271a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->node_name = ids->node_name;
3272a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->port_name = ids->port_name;
3273a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->roles = ids->roles;
3274a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->vport_type = ids->vport_type;
3275a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (fci->f->dd_fcvport_size)
3276a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		vport->dd_data = &vport[1];
3277a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->shost = shost;
3278a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->channel = channel;
3279a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags = FC_VPORT_CREATING;
32809ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3281a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3282a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3283a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3284a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3285a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		spin_unlock_irqrestore(shost->host_lock, flags);
3286a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		kfree(vport);
3287a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return -ENOSPC;
3288a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3289a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse++;
3290a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->number = fc_host->next_vport_number++;
3291a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_add_tail(&vport->peers, &fc_host->vports);
3292a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	get_device(&shost->shost_gendev);	/* for fc_host->vport list */
3293a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3294a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3295a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3296a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev = &vport->dev;
3297a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_initialize(dev);			/* takes self reference */
3298a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev->parent = get_device(pdev);		/* takes parent reference */
3299a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev->release = fc_vport_dev_release;
330071610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers	dev_set_name(dev, "vport-%d:%d-%d",
330171610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		     shost->host_no, channel, vport->number);
3302a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_setup_device(dev);
3303a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3304a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = device_add(dev);
3305a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error) {
3306a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		printk(KERN_ERR "FC Virtual Port device_add failed\n");
3307a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		goto delete_vport;
3308a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3309a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_add_device(dev);
3310a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_configure_device(dev);
3311a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3312a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	error = fci->f->vport_create(vport, ids->disable);
3313a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (error) {
3314a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3315a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		goto delete_vport_all;
3316a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3317a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3318a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
3319a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * if the parent isn't the physical adapter's Scsi_Host, ensure
3320a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * the Scsi_Host at least contains ia symlink to the vport.
3321a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
3322a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (pdev != &shost->shost_gendev) {
3323a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		error = sysfs_create_link(&shost->shost_gendev.kobj,
332471610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers				 &dev->kobj, dev_name(dev));
3325a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		if (error)
3326a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			printk(KERN_ERR
3327a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				"%s: Cannot create vport symlinks for "
3328a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart				"%s, err=%d\n",
332971610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers				__func__, dev_name(dev), error);
3330a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3331a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3332a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags &= ~FC_VPORT_CREATING;
3333a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3334a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3335a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	dev_printk(KERN_NOTICE, pdev,
333671610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers			"%s created via shost%d channel %d\n", dev_name(dev),
3337a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart			shost->host_no, channel);
3338a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3339a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	*ret_vport = vport;
3340a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3341a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0;
3342a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3343a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartdelete_vport_all:
3344a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_remove_device(dev);
3345a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_del(dev);
3346a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartdelete_vport:
3347a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_destroy_device(dev);
3348a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3349a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	list_del(&vport->peers);
3350a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(&shost->shost_gendev);	/* for fc_host->vport list */
3351a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	fc_host->npiv_vports_inuse--;
3352a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3353a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev->parent);
3354a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	kfree(vport);
3355a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3356a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return error;
3357a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
3358a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3359a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez/**
3360a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * fc_vport_create - Admin App or LLDD requests creation of a vport
3361a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @shost:	scsi host the virtual port is connected to.
3362a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @channel:	channel on shost port connected to.
3363a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * @ids:	The world wide names, FC4 port roles, etc for
3364a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *              the virtual port.
3365a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *
3366a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez * Notes:
3367a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez *	This routine assumes no locks are held on entry.
3368a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez */
3369a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezstruct fc_vport *
3370a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquezfc_vport_create(struct Scsi_Host *shost, int channel,
3371a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	struct fc_vport_identifiers *ids)
3372a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez{
3373a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	int stat;
3374a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	struct fc_vport *vport;
3375a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez
3376a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3377a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez		 ids, &vport);
3378a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez	return stat ? NULL : vport;
3379a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew Vasquez}
3380a30c3f69e6336cb9b09a989595e417367e4e9b1bAndrew VasquezEXPORT_SYMBOL(fc_vport_create);
3381a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3382a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart/**
3383a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3384a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * @vport:	fc_vport to be terminated
3385a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3386a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Calls the LLDD vport_delete() function, then deallocates and removes
3387a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * the vport from the shost and object tree.
3388a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *
3389a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart * Notes:
3390a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart *	This routine assumes no locks are held on entry.
3391eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
3392a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartint
3393a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smartfc_vport_terminate(struct fc_vport *vport)
3394a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart{
3395a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct Scsi_Host *shost = vport_to_shost(vport);
3396a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3397a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
3398a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	struct device *dev = &vport->dev;
3399a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	unsigned long flags;
3400a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	int stat;
3401a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3402a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (i->f->vport_delete)
3403a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		stat = i->f->vport_delete(vport);
3404a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	else
3405a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		stat = -ENOENT;
3406a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3407a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_lock_irqsave(shost->host_lock, flags);
3408a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	vport->flags &= ~FC_VPORT_DELETING;
3409a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (!stat) {
3410a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		vport->flags |= FC_VPORT_DELETED;
3411a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		list_del(&vport->peers);
3412a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		fc_host->npiv_vports_inuse--;
3413a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		put_device(&shost->shost_gendev);  /* for fc_host->vport list */
3414a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	}
3415a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	spin_unlock_irqrestore(shost->host_lock, flags);
3416a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3417a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (stat)
3418a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart		return stat;
3419a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3420a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	if (dev->parent != &shost->shost_gendev)
342171610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers		sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3422a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_remove_device(dev);
3423a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	device_del(dev);
3424a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	transport_destroy_device(dev);
3425a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3426a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	/*
3427a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * Removing our self-reference should mean our
3428a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * release function gets called, which will drop the remaining
3429a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 * parent reference and free the data structure.
3430a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	 */
3431a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	put_device(dev);			/* for self-reference */
3432a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
3433a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart	return 0; /* SUCCESS */
3434a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart}
3435a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James SmartEXPORT_SYMBOL(fc_vport_terminate);
3436a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
34379ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/**
34389ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart * fc_vport_sched_delete - workq-based delete request for a vport
34399ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart * @work:	vport to be deleted.
3440eb44820c28bc9a042e1157b41c677018a8fdfc74Rob Landley */
34419ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartstatic void
34429ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smartfc_vport_sched_delete(struct work_struct *work)
34439ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart{
34449ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	struct fc_vport *vport =
34459ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		container_of(work, struct fc_vport, vport_delete_work);
34469ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	int stat;
34479ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
34489ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	stat = fc_vport_terminate(vport);
34499ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart	if (stat)
34509ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart		dev_printk(KERN_ERR, vport->dev.parent,
34519ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart			"%s: %s could not be deleted created via "
3452cadbd4a5e36dde7e6c49b587b2c419103c0b7218Harvey Harrison			"shost%d channel %d - error %d\n", __func__,
345371610f55fa4db63dbf5385929a47c9fb2451f332Kay Sievers			dev_name(&vport->dev), vport->shost->host_no,
34549ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart			vport->channel, stat);
34559ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart}
34569ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart
3457a53eb5e060c0ec7245c8f93b9dcd94afa6041e06James Smart
34589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/*
34599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * BSG support
34609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
34619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
34649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
34659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	fc_bsg_job that is to be torn down
34669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
34679e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
34689e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_destroy_bsgjob(struct fc_bsg_job *job)
34699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
34709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	unsigned long flags;
34719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irqsave(&job->job_lock, flags);
34739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->ref_cnt) {
34749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		spin_unlock_irqrestore(&job->job_lock, flags);
34759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
34769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
34779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irqrestore(&job->job_lock, flags);
34789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(job->dev);	/* release reference for the request */
34809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->request_payload.sg_list);
34829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->reply_payload.sg_list);
34839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job);
34849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
34859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
34879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
34889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart *                  completed
34899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	fc_bsg_job that is complete
34909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
34919e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
34929e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_jobdone(struct fc_bsg_job *job)
34939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
34949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req = job->req;
34959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *rsp = req->next_rq;
34969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
34979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
34989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = job->req->errors = job->reply->result;
3499b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali
35009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err < 0)
35019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* we're only returning the result field in the reply */
35029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->req->sense_len = sizeof(uint32_t);
35039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
35049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->req->sense_len = job->reply_len;
35059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* we assume all request payload was transferred, residual == 0 */
35079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	req->resid_len = 0;
35089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rsp) {
35109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
35119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* set reply (bidi) residual */
35139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
35149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				      rsp->resid_len);
35159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
3516b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_complete_request(req);
3517b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali}
3518b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali
3519b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali/**
3520b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
3521fe5d20c818a8c5fe83d9f2223a051fb5bc50d180Randy Dunlap * @rq:        BSG request that holds the job to be destroyed
3522b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali */
3523b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavalistatic void fc_bsg_softirq_done(struct request *rq)
3524b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali{
3525b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	struct fc_bsg_job *job = rq->special;
3526b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	unsigned long flags;
35279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
3528b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	spin_lock_irqsave(&job->job_lock, flags);
3529b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	job->state_flags |= FC_RQST_STATE_DONE;
3530b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	job->ref_cnt--;
3531b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	spin_unlock_irqrestore(&job->job_lock, flags);
35329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
3533b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_end_request_all(rq, rq->errors);
35349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_destroy_bsgjob(job);
35359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
35369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
35389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_job_timeout - handler for when a bsg request timesout
35399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @req:	request that timed out
35409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
35419e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum blk_eh_timer_return
35429e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_job_timeout(struct request *req)
35439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
35449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job = (void *) req->special;
35459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = job->shost;
35469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
35479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	unsigned long flags;
35489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err = 0, done = 0;
35499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
35519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return BLK_EH_RESET_TIMER;
35529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irqsave(&job->job_lock, flags);
35549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->state_flags & FC_RQST_STATE_DONE)
35559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		done = 1;
35569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
35579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->ref_cnt++;
35589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irqrestore(&job->job_lock, flags);
35599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!done && i->f->bsg_timeout) {
35619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* call LLDD to abort the i/o as it has timed out */
35629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		err = i->f->bsg_timeout(job);
3563b8f08645f80a0c93246f3539ce53d3cae6a91fa0Swen Schillig		if (err == -EAGAIN) {
3564b8f08645f80a0c93246f3539ce53d3cae6a91fa0Swen Schillig			job->ref_cnt--;
3565b8f08645f80a0c93246f3539ce53d3cae6a91fa0Swen Schillig			return BLK_EH_RESET_TIMER;
3566b8f08645f80a0c93246f3539ce53d3cae6a91fa0Swen Schillig		} else if (err)
35679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
35689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"abort failed with status %d\n", err);
35699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
35709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* the blk_end_sync_io() doesn't check the error */
357247e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali	if (done)
357347e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali		return BLK_EH_NOT_HANDLED;
357447e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali	else
357547e7e89ed029780adf2cc0cf506fcd4c2d5ca1e2Giridhar Malavali		return BLK_EH_HANDLED;
35769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
35779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35789e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
35799e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
35809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
35819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
35829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(!req->nr_phys_segments);
35849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->sg_list = kzalloc(sz, GFP_KERNEL);
35869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!buf->sg_list)
35879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
35889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	sg_init_table(buf->sg_list, req->nr_phys_segments);
35899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
35909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	buf->payload_len = blk_rq_bytes(req);
35919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
35929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
35939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
35959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
35969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
35979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart *                   bsg request
35989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	SCSI Host corresponding to the bsg object
35999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	(optional) FC Remote Port corresponding to the bsg object
36009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @req:	BSG request that needs a job structure
36019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
36029e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
36039e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
36049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req)
36059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
36069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
36079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *rsp = req->next_rq;
36089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job;
36099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
36109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(req->special);
36129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
36149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			GFP_KERNEL);
36159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!job)
36169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
36179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/*
36199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * Note: this is a bit silly.
36209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * The request gets formatted as a SGIO v4 ioctl request, which
36219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * then gets reformatted as a blk request, which then gets
36229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * reformatted as a fc bsg request. And on completion, we have
36239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * to wrap return results such that SGIO v4 thinks it was a scsi
36249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 * status.  I hope this was all worth it.
36259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	 */
36269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	req->special = job;
36289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->shost = shost;
36299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->rport = rport;
36309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->req = req;
36319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (i->f->dd_bsg_size)
36329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dd_data = (void *)&job[1];
36339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_init(&job->job_lock);
36349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->request = (struct fc_bsg_request *)req->cmd;
36359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->request_len = req->cmd_len;
36369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply = req->sense;
36379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = SCSI_SENSE_BUFFERSIZE;	/* Size of sense buffer
36389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart						 * allocated */
36399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (req->bio) {
36409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_bsg_map_buffer(&job->request_payload, req);
36419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret)
36429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto failjob_rls_job;
36439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
36449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rsp && rsp->bio) {
36459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
36469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret)
36479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto failjob_rls_rqst_payload;
36489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
36499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->job_done = fc_bsg_jobdone;
36509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (rport)
36519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dev = &rport->dev;
36529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	else
36539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job->dev = &shost->shost_gendev;
36549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	get_device(job->dev);		/* take a reference for the request */
36559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->ref_cnt = 1;
36579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
36599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36619e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfailjob_rls_rqst_payload:
36629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job->request_payload.sg_list);
36639e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfailjob_rls_job:
36649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	kfree(job);
36659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return -ENOMEM;
36669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
36679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36699e4f5e29610162fd426366f3b29e3cc6e575b858James Smartenum fc_dispatch_result {
36709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_BREAK,	/* on return, q is locked, break from q loop */
36719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_LOCKED,	/* on return, q is locked, continue on */
36729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	FC_DISPATCH_UNLOCKED,	/* on return, q is unlocked, continue on */
36739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart};
36749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
36779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
36785415907af1f5ef80c95147bacbd321b0d4236dd5Randy Dunlap * @q:		fc host request queue
36799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	scsi host rport attached to
36809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	bsg job to be processed
36819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
36829e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum fc_dispatch_result
36839e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
36849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct fc_bsg_job *job)
36859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
36869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
36879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
36889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
36899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* Validate the host command */
36919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	switch (job->request->msgcode) {
36929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_ADD_RPORT:
36939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_add_rport);
36949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
36969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_DEL_RPORT:
36979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_del_rport);
36989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
36999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_ELS_NOLOGIN:
37019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_els);
37029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be a xmt and rcv payloads */
37039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
37049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
37059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
37069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
37079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
37089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
37099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_CT:
37119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_ct);
37129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be xmt and rcv payloads */
37139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
37149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
37159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
37169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
37179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
37189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
37199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_HST_VENDOR:
37219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_host_vendor);
37229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((shost->hostt->vendor_id == 0L) ||
37239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (job->request->rqst_data.h_vendor.vendor_id !=
37249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			shost->hostt->vendor_id)) {
37259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -ESRCH;
37269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_host_msg;
37279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
37289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
37299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	default:
37319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -EBADR;
37329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_host_msg;
37339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
37349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* check if we really have all the request data needed */
37369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->request_len < cmdlen) {
37379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -ENOMSG;
37389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_host_msg;
37399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
37409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	ret = i->f->bsg_request(job);
37429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!ret)
37439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return FC_DISPATCH_UNLOCKED;
37449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37459e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfail_host_msg:
37469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* return the errno failure code as the only status */
37479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(job->reply_len < sizeof(uint32_t));
374861ec33eb7d8e1e94a93a8243238dc5f9a183001aBrian King	job->reply->reply_payload_rcv_len = 0;
37499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply->result = ret;
37509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = sizeof(uint32_t);
37519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_jobdone(job);
37529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return FC_DISPATCH_UNLOCKED;
37539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
37549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/*
37579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_goose_queue - restart rport queue in case it was stopped
37589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport to be restarted
37599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
37609e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
37619e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_goose_queue(struct fc_rport *rport)
37629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
37639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int flagset;
376439562e783928e3ea9ee2cbce99a756ab48d3c06aChristof Schmitt	unsigned long flags;
37659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!rport->rqst_q)
37679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
37689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	get_device(&rport->dev);
37709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
377139562e783928e3ea9ee2cbce99a756ab48d3c06aChristof Schmitt	spin_lock_irqsave(rport->rqst_q->queue_lock, flags);
37729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	flagset = test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags) &&
37739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		  !test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags);
37749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (flagset)
37759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		queue_flag_set(QUEUE_FLAG_REENTER, rport->rqst_q);
37769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	__blk_run_queue(rport->rqst_q);
37779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (flagset)
37789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		queue_flag_clear(QUEUE_FLAG_REENTER, rport->rqst_q);
377939562e783928e3ea9ee2cbce99a756ab48d3c06aChristof Schmitt	spin_unlock_irqrestore(rport->rqst_q->queue_lock, flags);
37809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(&rport->dev);
37829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
37839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
37859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
37869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
37875415907af1f5ef80c95147bacbd321b0d4236dd5Randy Dunlap * @q:		rport request queue
37889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	scsi host rport attached to
37899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport request destined to
37909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @job:	bsg job to be processed
37919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
37929e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic enum fc_dispatch_result
37939e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
37949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			 struct fc_rport *rport, struct fc_bsg_job *job)
37959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
37969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
37979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
37989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int ret;
37999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* Validate the rport command */
38019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	switch (job->request->msgcode) {
38029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_RPT_ELS:
38039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_rport_els);
38049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto check_bidi;
38059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	case FC_BSG_RPT_CT:
38079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		cmdlen += sizeof(struct fc_bsg_rport_ct);
38089e4f5e29610162fd426366f3b29e3cc6e575b858James Smartcheck_bidi:
38099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* there better be xmt and rcv payloads */
38109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if ((!job->request_payload.payload_len) ||
38119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		    (!job->reply_payload.payload_len)) {
38129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = -EINVAL;
38139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			goto fail_rport_msg;
38149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		break;
38169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	default:
38179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -EBADR;
38189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_rport_msg;
38199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
38209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* check if we really have all the request data needed */
38229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (job->request_len < cmdlen) {
38239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = -ENOMSG;
38249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		goto fail_rport_msg;
38259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
38269e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	ret = i->f->bsg_request(job);
38289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!ret)
38299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return FC_DISPATCH_UNLOCKED;
38309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38319e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfail_rport_msg:
38329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	/* return the errno failure code as the only status */
38339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	BUG_ON(job->reply_len < sizeof(uint32_t));
383461ec33eb7d8e1e94a93a8243238dc5f9a183001aBrian King	job->reply->reply_payload_rcv_len = 0;
38359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply->result = ret;
38369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	job->reply_len = sizeof(uint32_t);
38379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_jobdone(job);
38389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return FC_DISPATCH_UNLOCKED;
38399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
38409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
38439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_request_handler - generic handler for bsg requests
38449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		request queue to manage
38459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	Scsi_Host related to the bsg object
38469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	FC remote port related to the bsg object (optional)
38479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @dev:	device structure for bsg object
38489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
38499e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
38509e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
38519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		       struct fc_rport *rport, struct device *dev)
38529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
38539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request *req;
38549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_bsg_job *job;
38559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	enum fc_dispatch_result ret;
38569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!get_device(dev))
38589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return;
38599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38609e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	while (!blk_queue_plugged(q)) {
38612bc1c59dbdefdb6f9767e06efb86bbdb2923a8beMike Christie		if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
38622bc1c59dbdefdb6f9767e06efb86bbdb2923a8beMike Christie		    !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
38632bc1c59dbdefdb6f9767e06efb86bbdb2923a8beMike Christie			break;
38649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		req = blk_fetch_request(q);
38669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (!req)
38679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			break;
38689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
38709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			req->errors = -ENXIO;
38719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_unlock_irq(q->queue_lock);
3872d88a714bfefa7aed7b9cb6c3721707fcd056b472Sarang Radke			blk_end_request_all(req, -ENXIO);
38739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
38749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
38759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		spin_unlock_irq(q->queue_lock);
38789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		ret = fc_req_to_bsgjob(shost, rport, req);
38809e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret) {
38819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			req->errors = ret;
3882d88a714bfefa7aed7b9cb6c3721707fcd056b472Sarang Radke			blk_end_request_all(req, ret);
38839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
38849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
38859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		job = req->special;
38889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
38899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* check if we have the msgcode value at least */
38909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (job->request_len < sizeof(uint32_t)) {
38919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			BUG_ON(job->reply_len < sizeof(uint32_t));
389261ec33eb7d8e1e94a93a8243238dc5f9a183001aBrian King			job->reply->reply_payload_rcv_len = 0;
38939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			job->reply->result = -ENOMSG;
38949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			job->reply_len = sizeof(uint32_t);
38959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			fc_bsg_jobdone(job);
38969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
38979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			continue;
38989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		}
38999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* the dispatch routines will unlock the queue_lock */
39019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (rport)
39029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = fc_bsg_rport_dispatch(q, shost, rport, job);
39039e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		else
39049e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			ret = fc_bsg_host_dispatch(q, shost, job);
39059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* did dispatcher hit state that can't process any more */
39079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret == FC_DISPATCH_BREAK)
39089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			break;
39099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		/* did dispatcher had released the lock */
39119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		if (ret == FC_DISPATCH_UNLOCKED)
39129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart			spin_lock_irq(q->queue_lock);
39139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_unlock_irq(q->queue_lock);
39169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	put_device(dev);
39179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	spin_lock_irq(q->queue_lock);
39189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
39229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_host_handler - handler for bsg requests for a fc host
39239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		fc host request queue
39249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
39259e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
39269e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_host_handler(struct request_queue *q)
39279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
39289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = q->queuedata;
39299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
39319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
39359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rport_handler - handler for bsg requests for a fc rport
39369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:		rport request queue
39379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
39389e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
39399e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rport_handler(struct request_queue *q)
39409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
39419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_rport *rport = q->queuedata;
39429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct Scsi_Host *shost = rport_to_shost(rport);
39439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_bsg_request_handler(q, shost, rport, &rport->dev);
39459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39489e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
39499e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
39509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	shost for fc_host
39519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @fc_host:	fc_host adding the structures to
39529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
39539e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
39549e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
39559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
39569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct device *dev = &shost->shost_gendev;
39579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
39589e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request_queue *q;
39599e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
39603c559ea8fd003962d9a28c64b2dd5c6d83ca6edbJames Bottomley	char bsg_name[20];
39619e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39629e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_host->rqst_q = NULL;
39639e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39649e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!i->f->bsg_request)
39659e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOTSUPP;
39669e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39679e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	snprintf(bsg_name, sizeof(bsg_name),
39689e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		 "fc_host%d", shost->host_no);
39699e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39709e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
39719e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!q) {
39729e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "fc_host%d: bsg interface failed to "
39739e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - no request queue\n",
39749e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 shost->host_no);
39759e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
39769e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39779e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39789e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q->queuedata = shost;
39799e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
3980b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_queue_softirq_done(q, fc_bsg_softirq_done);
39819e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
39829e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
39839e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39849e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = bsg_register_queue(q, dev, bsg_name, NULL);
39859e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err) {
39869e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "fc_host%d: bsg interface failed to "
39879e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - register queue\n",
39889e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				shost->host_no);
39899e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
39909e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return err;
39919e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
39929e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39939e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	fc_host->rqst_q = q;
39949e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
39959e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
39969e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39979e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
39989e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
39999e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
40009e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @shost:	shost that rport is attached to
40019e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @rport:	rport that the bsg hooks are being attached to
40029e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
40039e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic int
40049e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
40059e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
40069e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct device *dev = &rport->dev;
40079e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct fc_internal *i = to_fc_internal(shost->transportt);
40089e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	struct request_queue *q;
40099e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	int err;
40109e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40119e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	rport->rqst_q = NULL;
40129e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40139e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!i->f->bsg_request)
40149e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOTSUPP;
40159e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40169e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
40179e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (!q) {
40189e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "%s: bsg interface failed to "
40199e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - no request queue\n",
40209e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 dev->kobj.name);
40219e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return -ENOMEM;
40229e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
40239e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40249e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	q->queuedata = rport;
40259e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
4026b5c6f77680f4ff1775838fcedfdd6026bf5ad777Giridhar Malavali	blk_queue_softirq_done(q, fc_bsg_softirq_done);
40279e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
40289e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
40299e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40309e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	err = bsg_register_queue(q, dev, NULL, NULL);
40319e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (err) {
40329e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		printk(KERN_ERR "%s: bsg interface failed to "
40339e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				"initialize - register queue\n",
40349e4f5e29610162fd426366f3b29e3cc6e575b858James Smart				 dev->kobj.name);
40359e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
40369e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		return err;
40379e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
40389e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40399e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	rport->rqst_q = q;
40409e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	return 0;
40419e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
40429e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40439e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40449e4f5e29610162fd426366f3b29e3cc6e575b858James Smart/**
40459e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
40469e4f5e29610162fd426366f3b29e3cc6e575b858James Smart * @q:	the request_queue that is to be torn down.
40479e4f5e29610162fd426366f3b29e3cc6e575b858James Smart */
40489e4f5e29610162fd426366f3b29e3cc6e575b858James Smartstatic void
40499e4f5e29610162fd426366f3b29e3cc6e575b858James Smartfc_bsg_remove(struct request_queue *q)
40509e4f5e29610162fd426366f3b29e3cc6e575b858James Smart{
40519e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	if (q) {
40529e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		bsg_unregister_queue(q);
40539e4f5e29610162fd426366f3b29e3cc6e575b858James Smart		blk_cleanup_queue(q);
40549e4f5e29610162fd426366f3b29e3cc6e575b858James Smart	}
40559e4f5e29610162fd426366f3b29e3cc6e575b858James Smart}
40569e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40579e4f5e29610162fd426366f3b29e3cc6e575b858James Smart
40589ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames Smart/* Original Author:  Martin Hicks */
40599ef3e4a4527e1f65b8776287c6d4fd1fca5ba98fJames SmartMODULE_AUTHOR("James Smart");
40601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("FC Transport Attributes");
40611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
40621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
40631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(fc_transport_init);
40641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(fc_transport_exit);
4065