Blame view

drivers/scsi/scsi_transport_fc.c 100 KB
9ef3e4a45   James Smart   [SCSI] fc_transpo...
1
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   *  FiberChannel transport specific attributes exported to sysfs.
   *
   *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 2 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   *
   *  ========
   *
a53eb5e06   James Smart   [SCSI] FC Transpo...
22
   *  Copyright (C) 2004-2007   James Smart, Emulex Corporation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
   *    Rewrite for host, target, device, and remote port attributes,
   *    statistics, and service functions...
9ef3e4a45   James Smart   [SCSI] fc_transpo...
25
   *    Add vports, etc
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
30
31
32
33
   *
   */
  #include <linux/module.h>
  #include <linux/init.h>
  #include <scsi/scsi_device.h>
  #include <scsi/scsi_host.h>
  #include <scsi/scsi_transport.h>
  #include <scsi/scsi_transport_fc.h>
c829c3941   James Smart   [SCSI] FC transpo...
34
  #include <scsi/scsi_cmnd.h>
84314fd47   James Smart   [SCSI] SCSI and F...
35
36
37
  #include <linux/netlink.h>
  #include <net/netlink.h>
  #include <scsi/scsi_netlink_fc.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  #include "scsi_priv.h"
7525236d0   FUJITA Tomonori   [SCSI] fc_transpo...
39
  #include "scsi_transport_fc_internal.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40

aedf34977   James Smart   [SCSI] FC transpo...
41
  static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
9ef3e4a45   James Smart   [SCSI] fc_transpo...
42
  static void fc_vport_sched_delete(struct work_struct *work);
a30c3f69e   Andrew Vasquez   [SCSI] fc_transpo...
43
  static int fc_vport_setup(struct Scsi_Host *shost, int channel,
a53eb5e06   James Smart   [SCSI] FC Transpo...
44
45
46
47
  	struct device *pdev, struct fc_vport_identifiers  *ids,
  	struct fc_vport **vport);
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
50
   * Redefine so that we can have same named attributes in the
   * sdev/starget/host objects.
   */
ee959b00c   Tony Jones   SCSI: convert str...
51
52
  #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)		\
  struct device_attribute device_attr_##_prefix##_##_name = 	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
54
55
56
57
58
59
60
  	__ATTR(_name,_mode,_show,_store)
  
  #define fc_enum_name_search(title, table_type, table)			\
  static const char *get_fc_##title##_name(enum table_type table_key)	\
  {									\
  	int i;								\
  	char *name = NULL;						\
  									\
6391a1137   Tobias Klauser   [SCSI] drivers/sc...
61
  	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  		if (table[i].value == table_key) {			\
  			name = table[i].name;				\
  			break;						\
  		}							\
  	}								\
  	return name;							\
  }
  
  #define fc_enum_name_match(title, table_type, table)			\
  static int get_fc_##title##_match(const char *table_key,		\
  		enum table_type *value)					\
  {									\
  	int i;								\
  									\
6391a1137   Tobias Klauser   [SCSI] drivers/sc...
76
  	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  		if (strncmp(table_key, table[i].name,			\
  				table[i].matchlen) == 0) {		\
  			*value = table[i].value;			\
  			return 0; /* success */				\
  		}							\
  	}								\
  	return 1; /* failure */						\
  }
  
  
  /* Convert fc_port_type values to ascii string name */
  static struct {
  	enum fc_port_type	value;
  	char			*name;
  } fc_port_type_names[] = {
  	{ FC_PORTTYPE_UNKNOWN,		"Unknown" },
  	{ FC_PORTTYPE_OTHER,		"Other" },
  	{ FC_PORTTYPE_NOTPRESENT,	"Not Present" },
  	{ FC_PORTTYPE_NPORT,	"NPort (fabric via point-to-point)" },
  	{ FC_PORTTYPE_NLPORT,	"NLPort (fabric via loop)" },
  	{ FC_PORTTYPE_LPORT,	"LPort (private loop)" },
951948a39   Christof Schmitt   [SCSI] scsi_trans...
98
  	{ FC_PORTTYPE_PTP,	"Point-To-Point (direct nport connection)" },
a53eb5e06   James Smart   [SCSI] FC Transpo...
99
  	{ FC_PORTTYPE_NPIV,		"NPIV VPORT" },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
  };
  fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
  #define FC_PORTTYPE_MAX_NAMELEN		50
a53eb5e06   James Smart   [SCSI] FC Transpo...
103
104
  /* Reuse fc_port_type enum function for vport_type */
  #define get_fc_vport_type_name get_fc_port_type_name
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105

84314fd47   James Smart   [SCSI] SCSI and F...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  /* Convert fc_host_event_code values to ascii string name */
  static const struct {
  	enum fc_host_event_code		value;
  	char				*name;
  } fc_host_event_code_names[] = {
  	{ FCH_EVT_LIP,			"lip" },
  	{ FCH_EVT_LINKUP,		"link_up" },
  	{ FCH_EVT_LINKDOWN,		"link_down" },
  	{ FCH_EVT_LIPRESET,		"lip_reset" },
  	{ FCH_EVT_RSCN,			"rscn" },
  	{ FCH_EVT_ADAPTER_CHANGE,	"adapter_chg" },
  	{ FCH_EVT_PORT_UNKNOWN,		"port_unknown" },
  	{ FCH_EVT_PORT_ONLINE,		"port_online" },
  	{ FCH_EVT_PORT_OFFLINE,		"port_offline" },
  	{ FCH_EVT_PORT_FABRIC,		"port_fabric" },
  	{ FCH_EVT_LINK_UNKNOWN,		"link_unknown" },
  	{ FCH_EVT_VENDOR_UNIQUE,	"vendor_unique" },
  };
  fc_enum_name_search(host_event_code, fc_host_event_code,
  		fc_host_event_code_names)
  #define FC_HOST_EVENT_CODE_MAX_NAMELEN	30
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  /* Convert fc_port_state values to ascii string name */
  static struct {
  	enum fc_port_state	value;
  	char			*name;
  } fc_port_state_names[] = {
  	{ FC_PORTSTATE_UNKNOWN,		"Unknown" },
  	{ FC_PORTSTATE_NOTPRESENT,	"Not Present" },
  	{ FC_PORTSTATE_ONLINE,		"Online" },
  	{ FC_PORTSTATE_OFFLINE,		"Offline" },
  	{ FC_PORTSTATE_BLOCKED,		"Blocked" },
  	{ FC_PORTSTATE_BYPASSED,	"Bypassed" },
  	{ FC_PORTSTATE_DIAGNOSTICS,	"Diagnostics" },
  	{ FC_PORTSTATE_LINKDOWN,	"Linkdown" },
  	{ FC_PORTSTATE_ERROR,		"Error" },
  	{ FC_PORTSTATE_LOOPBACK,	"Loopback" },
42e33148d   James.Smart@Emulex.Com   [SCSI] fix for fc...
142
  	{ FC_PORTSTATE_DELETED,		"Deleted" },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
145
  };
  fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
  #define FC_PORTSTATE_MAX_NAMELEN	20
a53eb5e06   James Smart   [SCSI] FC Transpo...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  /* Convert fc_vport_state values to ascii string name */
  static struct {
  	enum fc_vport_state	value;
  	char			*name;
  } fc_vport_state_names[] = {
  	{ FC_VPORT_UNKNOWN,		"Unknown" },
  	{ FC_VPORT_ACTIVE,		"Active" },
  	{ FC_VPORT_DISABLED,		"Disabled" },
  	{ FC_VPORT_LINKDOWN,		"Linkdown" },
  	{ FC_VPORT_INITIALIZING,	"Initializing" },
  	{ FC_VPORT_NO_FABRIC_SUPP,	"No Fabric Support" },
  	{ FC_VPORT_NO_FABRIC_RSCS,	"No Fabric Resources" },
  	{ FC_VPORT_FABRIC_LOGOUT,	"Fabric Logout" },
  	{ FC_VPORT_FABRIC_REJ_WWN,	"Fabric Rejected WWN" },
  	{ FC_VPORT_FAILED,		"VPort Failed" },
  };
  fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
  #define FC_VPORTSTATE_MAX_NAMELEN	24
  
  /* Reuse fc_vport_state enum function for vport_last_state */
  #define get_fc_vport_last_state_name get_fc_vport_state_name
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
  /* Convert fc_tgtid_binding_type values to ascii string name */
0ad78200b   Arjan van de Ven   [SCSI] Mark some ...
168
  static const struct {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  	enum fc_tgtid_binding_type	value;
  	char				*name;
  	int				matchlen;
  } fc_tgtid_binding_type_names[] = {
  	{ FC_TGTID_BIND_NONE, "none", 4 },
  	{ FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
  	{ FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
  	{ FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
  };
  fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
  		fc_tgtid_binding_type_names)
  fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
  		fc_tgtid_binding_type_names)
  #define FC_BINDTYPE_MAX_NAMELEN	30
  
  
  #define fc_bitfield_name_search(title, table)			\
  static ssize_t							\
  get_fc_##title##_names(u32 table_key, char *buf)		\
  {								\
  	char *prefix = "";					\
  	ssize_t len = 0;					\
  	int i;							\
  								\
6391a1137   Tobias Klauser   [SCSI] drivers/sc...
193
  	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
196
197
198
199
200
201
202
203
204
205
206
  		if (table[i].value & table_key) {		\
  			len += sprintf(buf + len, "%s%s",	\
  				prefix, table[i].name);		\
  			prefix = ", ";				\
  		}						\
  	}							\
  	len += sprintf(buf + len, "
  ");			\
  	return len;						\
  }
  
  
  /* Convert FC_COS bit values to ascii string name */
0ad78200b   Arjan van de Ven   [SCSI] Mark some ...
207
  static const struct {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
213
214
215
216
217
218
219
220
  	u32 			value;
  	char			*name;
  } fc_cos_names[] = {
  	{ FC_COS_CLASS1,	"Class 1" },
  	{ FC_COS_CLASS2,	"Class 2" },
  	{ FC_COS_CLASS3,	"Class 3" },
  	{ FC_COS_CLASS4,	"Class 4" },
  	{ FC_COS_CLASS6,	"Class 6" },
  };
  fc_bitfield_name_search(cos, fc_cos_names)
  
  
  /* Convert FC_PORTSPEED bit values to ascii string name */
0ad78200b   Arjan van de Ven   [SCSI] Mark some ...
221
  static const struct {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222
223
224
225
226
227
228
  	u32 			value;
  	char			*name;
  } fc_port_speed_names[] = {
  	{ FC_PORTSPEED_1GBIT,		"1 Gbit" },
  	{ FC_PORTSPEED_2GBIT,		"2 Gbit" },
  	{ FC_PORTSPEED_4GBIT,		"4 Gbit" },
  	{ FC_PORTSPEED_10GBIT,		"10 Gbit" },
c3d2350a8   James Smart   [SCSI] fc_transpo...
229
230
  	{ FC_PORTSPEED_8GBIT,		"8 Gbit" },
  	{ FC_PORTSPEED_16GBIT,		"16 Gbit" },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  	{ FC_PORTSPEED_NOT_NEGOTIATED,	"Not Negotiated" },
  };
  fc_bitfield_name_search(port_speed, fc_port_speed_names)
  
  
  static int
  show_fc_fc4s (char *buf, u8 *fc4_list)
  {
  	int i, len=0;
  
  	for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
  		len += sprintf(buf + len , "0x%02x ", *fc4_list);
  	len += sprintf(buf + len, "
  ");
  	return len;
  }
a53eb5e06   James Smart   [SCSI] FC Transpo...
247
  /* Convert FC_PORT_ROLE bit values to ascii string name */
0ad78200b   Arjan van de Ven   [SCSI] Mark some ...
248
  static const struct {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
  	u32 			value;
  	char			*name;
a53eb5e06   James Smart   [SCSI] FC Transpo...
251
252
253
254
  } fc_port_role_names[] = {
  	{ FC_PORT_ROLE_FCP_TARGET,	"FCP Target" },
  	{ FC_PORT_ROLE_FCP_INITIATOR,	"FCP Initiator" },
  	{ FC_PORT_ROLE_IP_PORT,		"IP Port" },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
  };
a53eb5e06   James Smart   [SCSI] FC Transpo...
256
  fc_bitfield_name_search(port_roles, fc_port_role_names)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
258
259
260
261
262
263
264
265
266
267
  
  /*
   * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
   */
  #define FC_WELLKNOWN_PORTID_MASK	0xfffff0
  #define FC_WELLKNOWN_ROLE_MASK  	0x00000f
  #define FC_FPORT_PORTID			0x00000e
  #define FC_FABCTLR_PORTID		0x00000d
  #define FC_DIRSRVR_PORTID		0x00000c
  #define FC_TIMESRVR_PORTID		0x00000b
  #define FC_MGMTSRVR_PORTID		0x00000a
c4028958b   David Howells   WorkStruct: make ...
268
269
270
  static void fc_timeout_deleted_rport(struct work_struct *work);
  static void fc_timeout_fail_rport_io(struct work_struct *work);
  static void fc_scsi_scan_rport(struct work_struct *work);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
273
274
275
276
  
  /*
   * Attribute counts pre object type...
   * Increase these values if you add attributes
   */
  #define FC_STARGET_NUM_ATTRS 	3
0f29b966d   James Smart   [SCSI] FC transpo...
277
  #define FC_RPORT_NUM_ATTRS	10
a53eb5e06   James Smart   [SCSI] FC Transpo...
278
279
  #define FC_VPORT_NUM_ATTRS	9
  #define FC_HOST_NUM_ATTRS	21
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  
  struct fc_internal {
  	struct scsi_transport_template t;
  	struct fc_function_template *f;
  
  	/*
  	 * For attributes : each object has :
  	 *   An array of the actual attributes structures
  	 *   An array of null-terminated pointers to the attribute
  	 *     structures - used for mid-layer interaction.
  	 *
  	 * The attribute containers for the starget and host are are
  	 * part of the midlayer. As the remote port is specific to the
  	 * fc transport, we must provide the attribute container.
  	 */
ee959b00c   Tony Jones   SCSI: convert str...
295
  	struct device_attribute private_starget_attrs[
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
  							FC_STARGET_NUM_ATTRS];
ee959b00c   Tony Jones   SCSI: convert str...
297
  	struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298

ee959b00c   Tony Jones   SCSI: convert str...
299
300
  	struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
  	struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
302
  
  	struct transport_container rport_attr_cont;
ee959b00c   Tony Jones   SCSI: convert str...
303
304
  	struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
  	struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
a53eb5e06   James Smart   [SCSI] FC Transpo...
305
306
  
  	struct transport_container vport_attr_cont;
ee959b00c   Tony Jones   SCSI: convert str...
307
308
  	struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
  	struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
310
311
  };
  
  #define to_fc_internal(tmpl)	container_of(tmpl, struct fc_internal, t)
d0a7e5740   James Bottomley   [SCSI] correct tr...
312
  static int fc_target_setup(struct transport_container *tc, struct device *dev,
ee959b00c   Tony Jones   SCSI: convert str...
313
  			   struct device *cdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  {
  	struct scsi_target *starget = to_scsi_target(dev);
  	struct fc_rport *rport = starget_to_rport(starget);
  
  	/*
  	 * if parent is remote port, use values from remote port.
  	 * Otherwise, this host uses the fc_transport, but not the
  	 * remote port interface. As such, initialize to known non-values.
  	 */
  	if (rport) {
  		fc_starget_node_name(starget) = rport->node_name;
  		fc_starget_port_name(starget) = rport->port_name;
  		fc_starget_port_id(starget) = rport->port_id;
  	} else {
  		fc_starget_node_name(starget) = -1;
  		fc_starget_port_name(starget) = -1;
  		fc_starget_port_id(starget) = -1;
  	}
  
  	return 0;
  }
  
  static DECLARE_TRANSPORT_CLASS(fc_transport_class,
  			       "fc_transport",
  			       fc_target_setup,
  			       NULL,
  			       NULL);
d0a7e5740   James Bottomley   [SCSI] correct tr...
341
  static int fc_host_setup(struct transport_container *tc, struct device *dev,
ee959b00c   Tony Jones   SCSI: convert str...
342
  			 struct device *cdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
  {
  	struct Scsi_Host *shost = dev_to_shost(dev);
aedf34977   James Smart   [SCSI] FC transpo...
345
  	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346

9ef3e4a45   James Smart   [SCSI] fc_transpo...
347
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
349
350
351
  	 * Set default values easily detected by the midlayer as
  	 * failure cases.  The scsi lldd is responsible for initializing
  	 * all transport attributes to valid values per host.
  	 */
aedf34977   James Smart   [SCSI] FC transpo...
352
353
354
355
356
357
  	fc_host->node_name = -1;
  	fc_host->port_name = -1;
  	fc_host->permanent_port_name = -1;
  	fc_host->supported_classes = FC_COS_UNSPECIFIED;
  	memset(fc_host->supported_fc4s, 0,
  		sizeof(fc_host->supported_fc4s));
aedf34977   James Smart   [SCSI] FC transpo...
358
359
  	fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
  	fc_host->maxframe_size = -1;
a53eb5e06   James Smart   [SCSI] FC Transpo...
360
  	fc_host->max_npiv_vports = 0;
aedf34977   James Smart   [SCSI] FC transpo...
361
362
363
364
365
366
367
368
369
370
  	memset(fc_host->serial_number, 0,
  		sizeof(fc_host->serial_number));
  
  	fc_host->port_id = -1;
  	fc_host->port_type = FC_PORTTYPE_UNKNOWN;
  	fc_host->port_state = FC_PORTSTATE_UNKNOWN;
  	memset(fc_host->active_fc4s, 0,
  		sizeof(fc_host->active_fc4s));
  	fc_host->speed = FC_PORTSPEED_UNKNOWN;
  	fc_host->fabric_name = -1;
b8d082101   James Smart   [SCSI] fc transpo...
371
372
  	memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
  	memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
aedf34977   James Smart   [SCSI] FC transpo...
373
374
375
376
377
  
  	fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
  
  	INIT_LIST_HEAD(&fc_host->rports);
  	INIT_LIST_HEAD(&fc_host->rport_bindings);
a53eb5e06   James Smart   [SCSI] FC Transpo...
378
  	INIT_LIST_HEAD(&fc_host->vports);
aedf34977   James Smart   [SCSI] FC transpo...
379
380
  	fc_host->next_rport_number = 0;
  	fc_host->next_target_id = 0;
a53eb5e06   James Smart   [SCSI] FC Transpo...
381
382
  	fc_host->next_vport_number = 0;
  	fc_host->npiv_vports_inuse = 0;
aedf34977   James Smart   [SCSI] FC transpo...
383

aab0de245   Kay Sievers   driver core: remo...
384
385
  	snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
  		 "fc_wq_%d", shost->host_no);
aedf34977   James Smart   [SCSI] FC transpo...
386
387
388
389
  	fc_host->work_q = create_singlethread_workqueue(
  					fc_host->work_q_name);
  	if (!fc_host->work_q)
  		return -ENOMEM;
aab0de245   Kay Sievers   driver core: remo...
390
391
392
  	snprintf(fc_host->devloss_work_q_name,
  		 sizeof(fc_host->devloss_work_q_name),
  		 "fc_dl_%d", shost->host_no);
aedf34977   James Smart   [SCSI] FC transpo...
393
394
395
396
397
398
399
  	fc_host->devloss_work_q = create_singlethread_workqueue(
  					fc_host->devloss_work_q_name);
  	if (!fc_host->devloss_work_q) {
  		destroy_workqueue(fc_host->work_q);
  		fc_host->work_q = NULL;
  		return -ENOMEM;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  	return 0;
  }
  
  static DECLARE_TRANSPORT_CLASS(fc_host_class,
  			       "fc_host",
  			       fc_host_setup,
  			       NULL,
  			       NULL);
  
  /*
   * Setup and Remove actions for remote ports are handled
   * in the service functions below.
   */
  static DECLARE_TRANSPORT_CLASS(fc_rport_class,
  			       "fc_remote_ports",
  			       NULL,
  			       NULL,
  			       NULL);
  
  /*
a53eb5e06   James Smart   [SCSI] FC Transpo...
420
421
422
423
424
425
426
427
428
429
   * Setup and Remove actions for virtual ports are handled
   * in the service functions below.
   */
  static DECLARE_TRANSPORT_CLASS(fc_vport_class,
  			       "fc_vports",
  			       NULL,
  			       NULL,
  			       NULL);
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
431
432
433
434
435
436
437
   * Module Parameters
   */
  
  /*
   * dev_loss_tmo: the default number of seconds that the FC transport
   *   should insulate the loss of a remote port.
   *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
   */
1c9e16e47   James Smart   [SCSI] update max...
438
  static unsigned int fc_dev_loss_tmo = 60;		/* seconds */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439

10f4b89a0   Masatake YAMATO   [SCSI] Fix signne...
440
  module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
441
442
443
444
445
  MODULE_PARM_DESC(dev_loss_tmo,
  		 "Maximum number of seconds that the FC transport should"
  		 " insulate the loss of a remote port. Once this value is"
  		 " exceeded, the scsi target is removed. Value should be"
  		 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
eb44820c2   Rob Landley   [SCSI] Add Docume...
446
  /*
84314fd47   James Smart   [SCSI] SCSI and F...
447
   * Netlink Infrastructure
eb44820c2   Rob Landley   [SCSI] Add Docume...
448
   */
84314fd47   James Smart   [SCSI] SCSI and F...
449
450
451
452
453
454
455
  
  static atomic_t fc_event_seq;
  
  /**
   * fc_get_event_number - Obtain the next sequential FC event number
   *
   * Notes:
eb44820c2   Rob Landley   [SCSI] Add Docume...
456
   *   We could have inlined this, but it would have required fc_event_seq to
84314fd47   James Smart   [SCSI] SCSI and F...
457
458
   *   be exposed. For now, live with the subroutine call.
   *   Atomic used to avoid lock/unlock...
eb44820c2   Rob Landley   [SCSI] Add Docume...
459
   */
84314fd47   James Smart   [SCSI] SCSI and F...
460
461
462
463
464
465
466
467
468
469
  u32
  fc_get_event_number(void)
  {
  	return atomic_add_return(1, &fc_event_seq);
  }
  EXPORT_SYMBOL(fc_get_event_number);
  
  
  /**
   * fc_host_post_event - called to post an even on an fc_host.
84314fd47   James Smart   [SCSI] SCSI and F...
470
471
472
473
474
475
476
   * @shost:		host the event occurred on
   * @event_number:	fc event number obtained from get_fc_event_number()
   * @event_code:		fc_host event being posted
   * @event_data:		32bits of data for the event being posted
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
477
   */
84314fd47   James Smart   [SCSI] SCSI and F...
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
  void
  fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
  		enum fc_host_event_code event_code, u32 event_data)
  {
  	struct sk_buff *skb;
  	struct nlmsghdr	*nlh;
  	struct fc_nl_event *event;
  	const char *name;
  	u32 len, skblen;
  	int err;
  
  	if (!scsi_nl_sock) {
  		err = -ENOENT;
  		goto send_fail;
  	}
  
  	len = FC_NL_MSGALIGN(sizeof(*event));
  	skblen = NLMSG_SPACE(len);
  
  	skb = alloc_skb(skblen, GFP_KERNEL);
  	if (!skb) {
  		err = -ENOBUFS;
  		goto send_fail;
  	}
  
  	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
  				skblen - sizeof(*nlh), 0);
  	if (!nlh) {
  		err = -ENOBUFS;
  		goto send_fail_skb;
  	}
  	event = NLMSG_DATA(nlh);
  
  	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
  				FC_NL_ASYNC_EVENT, len);
  	event->seconds = get_seconds();
  	event->vendor_id = 0;
  	event->host_no = shost->host_no;
  	event->event_datalen = sizeof(u32);	/* bytes */
  	event->event_num = event_number;
  	event->event_code = event_code;
  	event->event_data = event_data;
ff491a733   Pablo Neira Ayuso   netlink: change r...
520
521
  	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
  			GFP_KERNEL);
84314fd47   James Smart   [SCSI] SCSI and F...
522
523
524
525
526
527
528
529
530
  	return;
  
  send_fail_skb:
  	kfree_skb(skb);
  send_fail:
  	name = get_fc_host_event_code_name(event_code);
  	printk(KERN_WARNING
  		"%s: Dropped Event : host %d %s data 0x%08x - err %d
  ",
cadbd4a5e   Harvey Harrison   [SCSI] replace __...
531
  		__func__, shost->host_no,
84314fd47   James Smart   [SCSI] SCSI and F...
532
533
534
535
536
537
538
  		(name) ? name : "<unknown>", event_data, err);
  	return;
  }
  EXPORT_SYMBOL(fc_host_post_event);
  
  
  /**
eb44820c2   Rob Landley   [SCSI] Add Docume...
539
   * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
84314fd47   James Smart   [SCSI] SCSI and F...
540
541
542
543
   * @shost:		host the event occurred on
   * @event_number:	fc event number obtained from get_fc_event_number()
   * @data_len:		amount, in bytes, of vendor unique data
   * @data_buf:		pointer to vendor unique data
eb44820c2   Rob Landley   [SCSI] Add Docume...
544
   * @vendor_id:          Vendor id
84314fd47   James Smart   [SCSI] SCSI and F...
545
546
547
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
548
   */
84314fd47   James Smart   [SCSI] SCSI and F...
549
550
  void
  fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
f14e2e29c   James Smart   [SCSI] SCSI & FC ...
551
  		u32 data_len, char * data_buf, u64 vendor_id)
84314fd47   James Smart   [SCSI] SCSI and F...
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
  {
  	struct sk_buff *skb;
  	struct nlmsghdr	*nlh;
  	struct fc_nl_event *event;
  	u32 len, skblen;
  	int err;
  
  	if (!scsi_nl_sock) {
  		err = -ENOENT;
  		goto send_vendor_fail;
  	}
  
  	len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
  	skblen = NLMSG_SPACE(len);
  
  	skb = alloc_skb(skblen, GFP_KERNEL);
  	if (!skb) {
  		err = -ENOBUFS;
  		goto send_vendor_fail;
  	}
  
  	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
  				skblen - sizeof(*nlh), 0);
  	if (!nlh) {
  		err = -ENOBUFS;
  		goto send_vendor_fail_skb;
  	}
  	event = NLMSG_DATA(nlh);
  
  	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
  				FC_NL_ASYNC_EVENT, len);
  	event->seconds = get_seconds();
  	event->vendor_id = vendor_id;
  	event->host_no = shost->host_no;
  	event->event_datalen = data_len;	/* bytes */
  	event->event_num = event_number;
  	event->event_code = FCH_EVT_VENDOR_UNIQUE;
  	memcpy(&event->event_data, data_buf, data_len);
ff491a733   Pablo Neira Ayuso   netlink: change r...
590
591
  	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
  			GFP_KERNEL);
84314fd47   James Smart   [SCSI] SCSI and F...
592
593
594
595
596
597
598
599
  	return;
  
  send_vendor_fail_skb:
  	kfree_skb(skb);
  send_vendor_fail:
  	printk(KERN_WARNING
  		"%s: Dropped Event : host %d vendor_unique - err %d
  ",
cadbd4a5e   Harvey Harrison   [SCSI] replace __...
600
  		__func__, shost->host_no, err);
84314fd47   James Smart   [SCSI] SCSI and F...
601
602
603
  	return;
  }
  EXPORT_SYMBOL(fc_host_post_vendor_event);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
605
606
  
  static __init int fc_transport_init(void)
  {
84314fd47   James Smart   [SCSI] SCSI and F...
607
608
609
610
611
  	int error;
  
  	atomic_set(&fc_event_seq, 0);
  
  	error = transport_class_register(&fc_host_class);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
613
  	if (error)
  		return error;
a53eb5e06   James Smart   [SCSI] FC Transpo...
614
615
616
  	error = transport_class_register(&fc_vport_class);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
617
618
619
620
621
622
623
624
625
626
627
  	error = transport_class_register(&fc_rport_class);
  	if (error)
  		return error;
  	return transport_class_register(&fc_transport_class);
  }
  
  static void __exit fc_transport_exit(void)
  {
  	transport_class_unregister(&fc_transport_class);
  	transport_class_unregister(&fc_rport_class);
  	transport_class_unregister(&fc_host_class);
a53eb5e06   James Smart   [SCSI] FC Transpo...
628
  	transport_class_unregister(&fc_vport_class);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
629
630
631
632
633
634
635
636
  }
  
  /*
   * FC Remote Port Attribute Management
   */
  
  #define fc_rport_show_function(field, format_string, sz, cast)		\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
637
638
  show_fc_rport_##field (struct device *dev, 				\
  		       struct device_attribute *attr, char *buf)	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
639
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
640
  	struct fc_rport *rport = transport_class_to_rport(dev);		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
  	struct Scsi_Host *shost = rport_to_shost(rport);		\
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
643
644
  	if ((i->f->get_rport_##field) &&				\
  	    !((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
42e33148d   James.Smart@Emulex.Com   [SCSI] fix for fc...
645
  	      (rport->port_state == FC_PORTSTATE_DELETED) ||		\
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
646
  	      (rport->port_state == FC_PORTSTATE_NOTPRESENT)))		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
647
648
649
650
651
652
  		i->f->get_rport_##field(rport);				\
  	return snprintf(buf, sz, format_string, cast rport->field); 	\
  }
  
  #define fc_rport_store_function(field)					\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
653
654
655
  store_fc_rport_##field(struct device *dev,				\
  		       struct device_attribute *attr,			\
  		       const char *buf,	size_t count)			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
656
657
  {									\
  	int val;							\
ee959b00c   Tony Jones   SCSI: convert str...
658
  	struct fc_rport *rport = transport_class_to_rport(dev);		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
660
  	struct Scsi_Host *shost = rport_to_shost(rport);		\
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
0f29b966d   James Smart   [SCSI] FC transpo...
661
  	char *cp;							\
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
662
  	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
42e33148d   James.Smart@Emulex.Com   [SCSI] fix for fc...
663
  	    (rport->port_state == FC_PORTSTATE_DELETED) ||		\
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
664
665
  	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))		\
  		return -EBUSY;						\
0f29b966d   James Smart   [SCSI] FC transpo...
666
667
668
669
  	val = simple_strtoul(buf, &cp, 0);				\
  	if (*cp && (*cp != '
  '))					\
  		return -EINVAL;						\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670
671
672
673
674
675
  	i->f->set_rport_##field(rport, val);				\
  	return count;							\
  }
  
  #define fc_rport_rd_attr(field, format_string, sz)			\
  	fc_rport_show_function(field, format_string, sz, )		\
ee959b00c   Tony Jones   SCSI: convert str...
676
  static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677
678
679
680
  			 show_fc_rport_##field, NULL)
  
  #define fc_rport_rd_attr_cast(field, format_string, sz, cast)		\
  	fc_rport_show_function(field, format_string, sz, (cast))	\
ee959b00c   Tony Jones   SCSI: convert str...
681
  static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
683
684
685
686
  			  show_fc_rport_##field, NULL)
  
  #define fc_rport_rw_attr(field, format_string, sz)			\
  	fc_rport_show_function(field, format_string, sz, )		\
  	fc_rport_store_function(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
687
  static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
692
693
  			show_fc_rport_##field,				\
  			store_fc_rport_##field)
  
  
  #define fc_private_rport_show_function(field, format_string, sz, cast)	\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
694
695
  show_fc_rport_##field (struct device *dev, 				\
  		       struct device_attribute *attr, char *buf)	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
697
  	struct fc_rport *rport = transport_class_to_rport(dev);		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
698
699
700
701
702
  	return snprintf(buf, sz, format_string, cast rport->field); 	\
  }
  
  #define fc_private_rport_rd_attr(field, format_string, sz)		\
  	fc_private_rport_show_function(field, format_string, sz, )	\
ee959b00c   Tony Jones   SCSI: convert str...
703
  static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
705
706
707
  			 show_fc_rport_##field, NULL)
  
  #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)	\
  	fc_private_rport_show_function(field, format_string, sz, (cast)) \
ee959b00c   Tony Jones   SCSI: convert str...
708
  static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
711
712
713
  			  show_fc_rport_##field, NULL)
  
  
  #define fc_private_rport_rd_enum_attr(title, maxlen)			\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
714
715
  show_fc_rport_##title (struct device *dev,				\
  		       struct device_attribute *attr, char *buf)	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
716
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
717
  	struct fc_rport *rport = transport_class_to_rport(dev);		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
718
719
720
721
722
723
724
  	const char *name;						\
  	name = get_fc_##title##_name(rport->title);			\
  	if (!name)							\
  		return -EINVAL;						\
  	return snprintf(buf, maxlen, "%s
  ", name);			\
  }									\
ee959b00c   Tony Jones   SCSI: convert str...
725
  static FC_DEVICE_ATTR(rport, title, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
728
729
  			show_fc_rport_##title, NULL)
  
  
  #define SETUP_RPORT_ATTRIBUTE_RD(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
730
  	i->private_rport_attrs[count] = device_attr_rport_##field; \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731
732
733
734
735
736
737
  	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
  	i->private_rport_attrs[count].store = NULL;			\
  	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
  	if (i->f->show_rport_##field)					\
  		count++
  
  #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)				\
ee959b00c   Tony Jones   SCSI: convert str...
738
  	i->private_rport_attrs[count] = device_attr_rport_##field; \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
739
740
741
742
743
744
  	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
  	i->private_rport_attrs[count].store = NULL;			\
  	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
  	count++
  
  #define SETUP_RPORT_ATTRIBUTE_RW(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
745
  	i->private_rport_attrs[count] = device_attr_rport_##field; \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
747
748
749
750
751
752
  	if (!i->f->set_rport_##field) {					\
  		i->private_rport_attrs[count].attr.mode = S_IRUGO;	\
  		i->private_rport_attrs[count].store = NULL;		\
  	}								\
  	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
  	if (i->f->show_rport_##field)					\
  		count++
0f29b966d   James Smart   [SCSI] FC transpo...
753
754
  #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field)				\
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
755
  	i->private_rport_attrs[count] = device_attr_rport_##field; \
0f29b966d   James Smart   [SCSI] FC transpo...
756
757
758
  	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
  	count++;							\
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759
760
761
762
763
764
765
766
767
  
  /* The FC Transport Remote Port Attributes: */
  
  /* Fixed Remote Port Attributes */
  
  fc_private_rport_rd_attr(maxframe_size, "%u bytes
  ", 20);
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
768
769
  show_fc_rport_supported_classes (struct device *dev,
  				 struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
770
  {
ee959b00c   Tony Jones   SCSI: convert str...
771
  	struct fc_rport *rport = transport_class_to_rport(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
772
773
774
775
776
  	if (rport->supported_classes == FC_COS_UNSPECIFIED)
  		return snprintf(buf, 20, "unspecified
  ");
  	return get_fc_cos_names(rport->supported_classes, buf);
  }
ee959b00c   Tony Jones   SCSI: convert str...
777
  static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
778
779
780
  		show_fc_rport_supported_classes, NULL);
  
  /* Dynamic Remote Port Attributes */
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
781
782
783
784
785
786
  /*
   * dev_loss_tmo attribute
   */
  fc_rport_show_function(dev_loss_tmo, "%d
  ", 20, )
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
787
788
  store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
  			    const char *buf, size_t count)
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
789
790
  {
  	int val;
ee959b00c   Tony Jones   SCSI: convert str...
791
  	struct fc_rport *rport = transport_class_to_rport(dev);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
792
793
  	struct Scsi_Host *shost = rport_to_shost(rport);
  	struct fc_internal *i = to_fc_internal(shost->transportt);
0f29b966d   James Smart   [SCSI] FC transpo...
794
  	char *cp;
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
795
  	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
42e33148d   James.Smart@Emulex.Com   [SCSI] fix for fc...
796
  	    (rport->port_state == FC_PORTSTATE_DELETED) ||
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
797
798
  	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  		return -EBUSY;
0f29b966d   James Smart   [SCSI] FC transpo...
799
800
801
802
  	val = simple_strtoul(buf, &cp, 0);
  	if ((*cp && (*cp != '
  ')) ||
  	    (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
803
804
805
806
  		return -EINVAL;
  	i->f->set_rport_dev_loss_tmo(rport, val);
  	return count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
807
  static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
808
  		show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
809
810
811
812
813
814
815
816
817
818
819
820
  
  
  /* Private Remote Port Attributes */
  
  fc_private_rport_rd_attr_cast(node_name, "0x%llx
  ", 20, unsigned long long);
  fc_private_rport_rd_attr_cast(port_name, "0x%llx
  ", 20, unsigned long long);
  fc_private_rport_rd_attr(port_id, "0x%06x
  ", 20);
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
821
822
  show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
  		     char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
  {
ee959b00c   Tony Jones   SCSI: convert str...
824
  	struct fc_rport *rport = transport_class_to_rport(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
  
  	/* identify any roles that are port_id specific */
  	if ((rport->port_id != -1) &&
  	    (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
  					FC_WELLKNOWN_PORTID_MASK) {
  		switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
  		case FC_FPORT_PORTID:
  			return snprintf(buf, 30, "Fabric Port
  ");
  		case FC_FABCTLR_PORTID:
  			return snprintf(buf, 30, "Fabric Controller
  ");
  		case FC_DIRSRVR_PORTID:
  			return snprintf(buf, 30, "Directory Server
  ");
  		case FC_TIMESRVR_PORTID:
  			return snprintf(buf, 30, "Time Server
  ");
  		case FC_MGMTSRVR_PORTID:
  			return snprintf(buf, 30, "Management Server
  ");
  		default:
  			return snprintf(buf, 30, "Unknown Fabric Entity
  ");
  		}
  	} else {
a53eb5e06   James Smart   [SCSI] FC Transpo...
851
  		if (rport->roles == FC_PORT_ROLE_UNKNOWN)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
852
853
  			return snprintf(buf, 20, "unknown
  ");
a53eb5e06   James Smart   [SCSI] FC Transpo...
854
  		return get_fc_port_roles_names(rport->roles, buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
856
  	}
  }
ee959b00c   Tony Jones   SCSI: convert str...
857
  static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
858
859
860
861
862
  		show_fc_rport_roles, NULL);
  
  fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  fc_private_rport_rd_attr(scsi_target_id, "%d
  ", 20);
0f29b966d   James Smart   [SCSI] FC transpo...
863
864
865
866
  /*
   * fast_io_fail_tmo attribute
   */
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
867
868
  show_fc_rport_fast_io_fail_tmo (struct device *dev,
  				struct device_attribute *attr, char *buf)
0f29b966d   James Smart   [SCSI] FC transpo...
869
  {
ee959b00c   Tony Jones   SCSI: convert str...
870
  	struct fc_rport *rport = transport_class_to_rport(dev);
0f29b966d   James Smart   [SCSI] FC transpo...
871
872
873
874
875
876
877
878
879
  
  	if (rport->fast_io_fail_tmo == -1)
  		return snprintf(buf, 5, "off
  ");
  	return snprintf(buf, 20, "%d
  ", rport->fast_io_fail_tmo);
  }
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
880
881
882
  store_fc_rport_fast_io_fail_tmo(struct device *dev,
  				struct device_attribute *attr, const char *buf,
  				size_t count)
0f29b966d   James Smart   [SCSI] FC transpo...
883
884
885
  {
  	int val;
  	char *cp;
ee959b00c   Tony Jones   SCSI: convert str...
886
  	struct fc_rport *rport = transport_class_to_rport(dev);
0f29b966d   James Smart   [SCSI] FC transpo...
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
  
  	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
  	    (rport->port_state == FC_PORTSTATE_DELETED) ||
  	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  		return -EBUSY;
  	if (strncmp(buf, "off", 3) == 0)
  		rport->fast_io_fail_tmo = -1;
  	else {
  		val = simple_strtoul(buf, &cp, 0);
  		if ((*cp && (*cp != '
  ')) ||
  		    (val < 0) || (val >= rport->dev_loss_tmo))
  			return -EINVAL;
  		rport->fast_io_fail_tmo = val;
  	}
  	return count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
904
  static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
0f29b966d   James Smart   [SCSI] FC transpo...
905
  	show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
906
907
908
909
910
911
912
913
  
  
  /*
   * FC SCSI Target Attribute Management
   */
  
  /*
   * Note: in the target show function we recognize when the remote
a53eb5e06   James Smart   [SCSI] FC Transpo...
914
   *  port is in the heirarchy and do not allow the driver to get
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
915
916
917
918
919
   *  involved in sysfs functions. The driver only gets involved if
   *  it's the "old" style that doesn't use rports.
   */
  #define fc_starget_show_function(field, format_string, sz, cast)	\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
920
921
  show_fc_starget_##field (struct device *dev, 				\
  			 struct device_attribute *attr, char *buf)	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
922
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
923
  	struct scsi_target *starget = transport_class_to_starget(dev);	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
926
927
928
929
930
931
932
933
934
935
936
  	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);	\
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
  	struct fc_rport *rport = starget_to_rport(starget);		\
  	if (rport)							\
  		fc_starget_##field(starget) = rport->field;		\
  	else if (i->f->get_starget_##field)				\
  		i->f->get_starget_##field(starget);			\
  	return snprintf(buf, sz, format_string, 			\
  		cast fc_starget_##field(starget)); 			\
  }
  
  #define fc_starget_rd_attr(field, format_string, sz)			\
  	fc_starget_show_function(field, format_string, sz, )		\
ee959b00c   Tony Jones   SCSI: convert str...
937
  static FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
938
939
940
941
  			 show_fc_starget_##field, NULL)
  
  #define fc_starget_rd_attr_cast(field, format_string, sz, cast)		\
  	fc_starget_show_function(field, format_string, sz, (cast))	\
ee959b00c   Tony Jones   SCSI: convert str...
942
  static FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943
944
945
  			  show_fc_starget_##field, NULL)
  
  #define SETUP_STARGET_ATTRIBUTE_RD(field)				\
ee959b00c   Tony Jones   SCSI: convert str...
946
  	i->private_starget_attrs[count] = device_attr_starget_##field; \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
947
948
949
950
951
952
953
  	i->private_starget_attrs[count].attr.mode = S_IRUGO;		\
  	i->private_starget_attrs[count].store = NULL;			\
  	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
  	if (i->f->show_starget_##field)					\
  		count++
  
  #define SETUP_STARGET_ATTRIBUTE_RW(field)				\
ee959b00c   Tony Jones   SCSI: convert str...
954
  	i->private_starget_attrs[count] = device_attr_starget_##field; \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
  	if (!i->f->set_starget_##field) {				\
  		i->private_starget_attrs[count].attr.mode = S_IRUGO;	\
  		i->private_starget_attrs[count].store = NULL;		\
  	}								\
  	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
  	if (i->f->show_starget_##field)					\
  		count++
  
  /* The FC Transport SCSI Target Attributes: */
  fc_starget_rd_attr_cast(node_name, "0x%llx
  ", 20, unsigned long long);
  fc_starget_rd_attr_cast(port_name, "0x%llx
  ", 20, unsigned long long);
  fc_starget_rd_attr(port_id, "0x%06x
  ", 20);
  
  
  /*
a53eb5e06   James Smart   [SCSI] FC Transpo...
973
974
975
976
977
   * FC Virtual Port Attribute Management
   */
  
  #define fc_vport_show_function(field, format_string, sz, cast)		\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
978
979
  show_fc_vport_##field (struct device *dev, 				\
  		       struct device_attribute *attr, char *buf)	\
a53eb5e06   James Smart   [SCSI] FC Transpo...
980
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
981
  	struct fc_vport *vport = transport_class_to_vport(dev);		\
a53eb5e06   James Smart   [SCSI] FC Transpo...
982
983
984
985
986
987
988
989
990
991
  	struct Scsi_Host *shost = vport_to_shost(vport);		\
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
  	if ((i->f->get_vport_##field) &&				\
  	    !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)))	\
  		i->f->get_vport_##field(vport);				\
  	return snprintf(buf, sz, format_string, cast vport->field); 	\
  }
  
  #define fc_vport_store_function(field)					\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
992
993
994
  store_fc_vport_##field(struct device *dev,				\
  		       struct device_attribute *attr,			\
  		       const char *buf,	size_t count)			\
a53eb5e06   James Smart   [SCSI] FC Transpo...
995
996
  {									\
  	int val;							\
ee959b00c   Tony Jones   SCSI: convert str...
997
  	struct fc_vport *vport = transport_class_to_vport(dev);		\
a53eb5e06   James Smart   [SCSI] FC Transpo...
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
  	struct Scsi_Host *shost = vport_to_shost(vport);		\
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
  	char *cp;							\
  	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))	\
  		return -EBUSY;						\
  	val = simple_strtoul(buf, &cp, 0);				\
  	if (*cp && (*cp != '
  '))					\
  		return -EINVAL;						\
  	i->f->set_vport_##field(vport, val);				\
  	return count;							\
  }
  
  #define fc_vport_store_str_function(field, slen)			\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
1013
1014
1015
  store_fc_vport_##field(struct device *dev,				\
  		       struct device_attribute *attr, 			\
  		       const char *buf,	size_t count)			\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1016
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
1017
  	struct fc_vport *vport = transport_class_to_vport(dev);		\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
  	struct Scsi_Host *shost = vport_to_shost(vport);		\
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
  	unsigned int cnt=count;						\
  									\
  	/* count may include a LF at end of string */			\
  	if (buf[cnt-1] == '
  ')						\
  		cnt--;							\
  	if (cnt > ((slen) - 1))						\
  		return -EINVAL;						\
  	memcpy(vport->field, buf, cnt);					\
  	i->f->set_vport_##field(vport);					\
  	return count;							\
  }
  
  #define fc_vport_rd_attr(field, format_string, sz)			\
  	fc_vport_show_function(field, format_string, sz, )		\
ee959b00c   Tony Jones   SCSI: convert str...
1035
  static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1036
1037
1038
1039
  			 show_fc_vport_##field, NULL)
  
  #define fc_vport_rd_attr_cast(field, format_string, sz, cast)		\
  	fc_vport_show_function(field, format_string, sz, (cast))	\
ee959b00c   Tony Jones   SCSI: convert str...
1040
  static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1041
1042
1043
1044
1045
  			  show_fc_vport_##field, NULL)
  
  #define fc_vport_rw_attr(field, format_string, sz)			\
  	fc_vport_show_function(field, format_string, sz, )		\
  	fc_vport_store_function(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
1046
  static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1047
1048
1049
1050
1051
  			show_fc_vport_##field,				\
  			store_fc_vport_##field)
  
  #define fc_private_vport_show_function(field, format_string, sz, cast)	\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
1052
1053
  show_fc_vport_##field (struct device *dev,				\
  		       struct device_attribute *attr, char *buf)	\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1054
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
1055
  	struct fc_vport *vport = transport_class_to_vport(dev);		\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1056
1057
1058
1059
1060
  	return snprintf(buf, sz, format_string, cast vport->field); 	\
  }
  
  #define fc_private_vport_store_u32_function(field)			\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
1061
1062
1063
  store_fc_vport_##field(struct device *dev,				\
  		       struct device_attribute *attr,			\
  		       const char *buf,	size_t count)			\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1064
1065
  {									\
  	u32 val;							\
ee959b00c   Tony Jones   SCSI: convert str...
1066
  	struct fc_vport *vport = transport_class_to_vport(dev);		\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
  	char *cp;							\
  	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))		\
  		return -EBUSY;						\
  	val = simple_strtoul(buf, &cp, 0);				\
  	if (*cp && (*cp != '
  '))					\
  		return -EINVAL;						\
  	vport->field = val;						\
  	return count;							\
  }
  
  
  #define fc_private_vport_rd_attr(field, format_string, sz)		\
  	fc_private_vport_show_function(field, format_string, sz, )	\
ee959b00c   Tony Jones   SCSI: convert str...
1081
  static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1082
1083
1084
1085
  			 show_fc_vport_##field, NULL)
  
  #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast)	\
  	fc_private_vport_show_function(field, format_string, sz, (cast)) \
ee959b00c   Tony Jones   SCSI: convert str...
1086
  static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1087
1088
1089
1090
1091
  			  show_fc_vport_##field, NULL)
  
  #define fc_private_vport_rw_u32_attr(field, format_string, sz)		\
  	fc_private_vport_show_function(field, format_string, sz, )	\
  	fc_private_vport_store_u32_function(field)			\
ee959b00c   Tony Jones   SCSI: convert str...
1092
  static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1093
1094
1095
1096
1097
1098
  			show_fc_vport_##field,				\
  			store_fc_vport_##field)
  
  
  #define fc_private_vport_rd_enum_attr(title, maxlen)			\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
1099
1100
1101
  show_fc_vport_##title (struct device *dev,				\
  		       struct device_attribute *attr,			\
  		       char *buf)					\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1102
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
1103
  	struct fc_vport *vport = transport_class_to_vport(dev);		\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1104
1105
1106
1107
1108
1109
1110
  	const char *name;						\
  	name = get_fc_##title##_name(vport->title);			\
  	if (!name)							\
  		return -EINVAL;						\
  	return snprintf(buf, maxlen, "%s
  ", name);			\
  }									\
ee959b00c   Tony Jones   SCSI: convert str...
1111
  static FC_DEVICE_ATTR(vport, title, S_IRUGO,			\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1112
1113
1114
1115
  			show_fc_vport_##title, NULL)
  
  
  #define SETUP_VPORT_ATTRIBUTE_RD(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
1116
  	i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e06   James Smart   [SCSI] FC Transpo...
1117
1118
1119
1120
1121
1122
1123
1124
  	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
  	i->private_vport_attrs[count].store = NULL;			\
  	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
  	if (i->f->get_##field)						\
  		count++
  	/* NOTE: Above MACRO differs: checks function not show bit */
  
  #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field)				\
ee959b00c   Tony Jones   SCSI: convert str...
1125
  	i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e06   James Smart   [SCSI] FC Transpo...
1126
1127
1128
1129
1130
1131
  	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
  	i->private_vport_attrs[count].store = NULL;			\
  	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
  	count++
  
  #define SETUP_VPORT_ATTRIBUTE_WR(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
1132
  	i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e06   James Smart   [SCSI] FC Transpo...
1133
1134
1135
1136
1137
1138
  	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
  	if (i->f->field)						\
  		count++
  	/* NOTE: Above MACRO differs: checks function */
  
  #define SETUP_VPORT_ATTRIBUTE_RW(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
1139
  	i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e06   James Smart   [SCSI] FC Transpo...
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
  	if (!i->f->set_vport_##field) {					\
  		i->private_vport_attrs[count].attr.mode = S_IRUGO;	\
  		i->private_vport_attrs[count].store = NULL;		\
  	}								\
  	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
  	count++
  	/* NOTE: Above MACRO differs: does not check show bit */
  
  #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field)				\
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
1150
  	i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e06   James Smart   [SCSI] FC Transpo...
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
  	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
  	count++;							\
  }
  
  
  /* The FC Transport Virtual Port Attributes: */
  
  /* Fixed Virtual Port Attributes */
  
  /* Dynamic Virtual Port Attributes */
  
  /* Private Virtual Port Attributes */
  
  fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
  fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
  fc_private_vport_rd_attr_cast(node_name, "0x%llx
  ", 20, unsigned long long);
  fc_private_vport_rd_attr_cast(port_name, "0x%llx
  ", 20, unsigned long long);
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1172
1173
  show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
  		     char *buf)
a53eb5e06   James Smart   [SCSI] FC Transpo...
1174
  {
ee959b00c   Tony Jones   SCSI: convert str...
1175
  	struct fc_vport *vport = transport_class_to_vport(dev);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1176
1177
1178
1179
1180
1181
  
  	if (vport->roles == FC_PORT_ROLE_UNKNOWN)
  		return snprintf(buf, 20, "unknown
  ");
  	return get_fc_port_roles_names(vport->roles, buf);
  }
ee959b00c   Tony Jones   SCSI: convert str...
1182
  static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1183
1184
1185
1186
1187
1188
1189
  
  fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
  
  fc_private_vport_show_function(symbolic_name, "%s
  ",
  		FC_VPORT_SYMBOLIC_NAMELEN + 1, )
  fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
ee959b00c   Tony Jones   SCSI: convert str...
1190
  static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
a53eb5e06   James Smart   [SCSI] FC Transpo...
1191
1192
1193
  		show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1194
1195
  store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
  		      const char *buf, size_t count)
a53eb5e06   James Smart   [SCSI] FC Transpo...
1196
  {
ee959b00c   Tony Jones   SCSI: convert str...
1197
  	struct fc_vport *vport = transport_class_to_vport(dev);
9ef3e4a45   James Smart   [SCSI] fc_transpo...
1198
  	struct Scsi_Host *shost = vport_to_shost(vport);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1199

9ef3e4a45   James Smart   [SCSI] fc_transpo...
1200
  	fc_queue_work(shost, &vport->vport_delete_work);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1201
1202
  	return count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
1203
  static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
a53eb5e06   James Smart   [SCSI] FC Transpo...
1204
1205
1206
1207
1208
1209
1210
1211
  			NULL, store_fc_vport_delete);
  
  
  /*
   * Enable/Disable vport
   *  Write "1" to disable, write "0" to enable
   */
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1212
1213
  store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
  		       const char *buf,
a53eb5e06   James Smart   [SCSI] FC Transpo...
1214
1215
  			   size_t count)
  {
ee959b00c   Tony Jones   SCSI: convert str...
1216
  	struct fc_vport *vport = transport_class_to_vport(dev);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
  	struct Scsi_Host *shost = vport_to_shost(vport);
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  	int stat;
  
  	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
  		return -EBUSY;
  
  	if (*buf == '0') {
  		if (vport->vport_state != FC_VPORT_DISABLED)
  			return -EALREADY;
  	} else if (*buf == '1') {
  		if (vport->vport_state == FC_VPORT_DISABLED)
  			return -EALREADY;
  	} else
  		return -EINVAL;
  
  	stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
  	return stat ? stat : count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
1236
  static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
a53eb5e06   James Smart   [SCSI] FC Transpo...
1237
1238
1239
1240
  			NULL, store_fc_vport_disable);
  
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1241
1242
1243
1244
1245
   * Host Attribute Management
   */
  
  #define fc_host_show_function(field, format_string, sz, cast)		\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
1246
1247
  show_fc_host_##field (struct device *dev,				\
  		      struct device_attribute *attr, char *buf)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1248
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
1249
  	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250
1251
1252
1253
1254
1255
1256
1257
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
  	if (i->f->get_host_##field)					\
  		i->f->get_host_##field(shost);				\
  	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  }
  
  #define fc_host_store_function(field)					\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
1258
1259
1260
  store_fc_host_##field(struct device *dev, 				\
  		      struct device_attribute *attr,			\
  		      const char *buf,	size_t count)			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1261
1262
  {									\
  	int val;							\
ee959b00c   Tony Jones   SCSI: convert str...
1263
  	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1264
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
0f29b966d   James Smart   [SCSI] FC transpo...
1265
  	char *cp;							\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1266
  									\
0f29b966d   James Smart   [SCSI] FC transpo...
1267
1268
1269
1270
  	val = simple_strtoul(buf, &cp, 0);				\
  	if (*cp && (*cp != '
  '))					\
  		return -EINVAL;						\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1271
1272
1273
  	i->f->set_host_##field(shost, val);				\
  	return count;							\
  }
b8d082101   James Smart   [SCSI] fc transpo...
1274
1275
  #define fc_host_store_str_function(field, slen)				\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
1276
1277
1278
  store_fc_host_##field(struct device *dev,				\
  		      struct device_attribute *attr,			\
  		      const char *buf, size_t count)			\
b8d082101   James Smart   [SCSI] fc transpo...
1279
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
1280
  	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
b8d082101   James Smart   [SCSI] fc transpo...
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
  	unsigned int cnt=count;						\
  									\
  	/* count may include a LF at end of string */			\
  	if (buf[cnt-1] == '
  ')						\
  		cnt--;							\
  	if (cnt > ((slen) - 1))						\
  		return -EINVAL;						\
  	memcpy(fc_host_##field(shost), buf, cnt);			\
  	i->f->set_host_##field(shost);					\
  	return count;							\
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1294
1295
  #define fc_host_rd_attr(field, format_string, sz)			\
  	fc_host_show_function(field, format_string, sz, )		\
ee959b00c   Tony Jones   SCSI: convert str...
1296
  static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1297
1298
1299
1300
  			 show_fc_host_##field, NULL)
  
  #define fc_host_rd_attr_cast(field, format_string, sz, cast)		\
  	fc_host_show_function(field, format_string, sz, (cast))		\
ee959b00c   Tony Jones   SCSI: convert str...
1301
  static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1302
1303
1304
1305
1306
  			  show_fc_host_##field, NULL)
  
  #define fc_host_rw_attr(field, format_string, sz)			\
  	fc_host_show_function(field, format_string, sz, )		\
  	fc_host_store_function(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
1307
  static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1308
1309
1310
1311
1312
  			show_fc_host_##field,				\
  			store_fc_host_##field)
  
  #define fc_host_rd_enum_attr(title, maxlen)				\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
1313
1314
  show_fc_host_##title (struct device *dev,				\
  		      struct device_attribute *attr, char *buf)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1315
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
1316
  	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
  	struct fc_internal *i = to_fc_internal(shost->transportt);	\
  	const char *name;						\
  	if (i->f->get_host_##title)					\
  		i->f->get_host_##title(shost);				\
  	name = get_fc_##title##_name(fc_host_##title(shost));		\
  	if (!name)							\
  		return -EINVAL;						\
  	return snprintf(buf, maxlen, "%s
  ", name);			\
  }									\
ee959b00c   Tony Jones   SCSI: convert str...
1327
  static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1328
1329
  
  #define SETUP_HOST_ATTRIBUTE_RD(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
1330
  	i->private_host_attrs[count] = device_attr_host_##field;	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1331
1332
1333
1334
1335
  	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
  	i->private_host_attrs[count].store = NULL;			\
  	i->host_attrs[count] = &i->private_host_attrs[count];		\
  	if (i->f->show_host_##field)					\
  		count++
a53eb5e06   James Smart   [SCSI] FC Transpo...
1336
  #define SETUP_HOST_ATTRIBUTE_RD_NS(field)				\
ee959b00c   Tony Jones   SCSI: convert str...
1337
  	i->private_host_attrs[count] = device_attr_host_##field;	\
a53eb5e06   James Smart   [SCSI] FC Transpo...
1338
1339
1340
1341
  	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
  	i->private_host_attrs[count].store = NULL;			\
  	i->host_attrs[count] = &i->private_host_attrs[count];		\
  	count++
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1342
  #define SETUP_HOST_ATTRIBUTE_RW(field)					\
ee959b00c   Tony Jones   SCSI: convert str...
1343
  	i->private_host_attrs[count] = device_attr_host_##field;	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
  	if (!i->f->set_host_##field) {					\
  		i->private_host_attrs[count].attr.mode = S_IRUGO;	\
  		i->private_host_attrs[count].store = NULL;		\
  	}								\
  	i->host_attrs[count] = &i->private_host_attrs[count];		\
  	if (i->f->show_host_##field)					\
  		count++
  
  
  #define fc_private_host_show_function(field, format_string, sz, cast)	\
  static ssize_t								\
ee959b00c   Tony Jones   SCSI: convert str...
1355
1356
  show_fc_host_##field (struct device *dev,				\
  		      struct device_attribute *attr, char *buf)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1357
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
1358
  	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1359
1360
1361
1362
1363
  	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  }
  
  #define fc_private_host_rd_attr(field, format_string, sz)		\
  	fc_private_host_show_function(field, format_string, sz, )	\
ee959b00c   Tony Jones   SCSI: convert str...
1364
  static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1365
1366
1367
1368
  			 show_fc_host_##field, NULL)
  
  #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)	\
  	fc_private_host_show_function(field, format_string, sz, (cast)) \
ee959b00c   Tony Jones   SCSI: convert str...
1369
  static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1370
1371
1372
  			  show_fc_host_##field, NULL)
  
  #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
ee959b00c   Tony Jones   SCSI: convert str...
1373
  	i->private_host_attrs[count] = device_attr_host_##field;	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1374
1375
1376
1377
1378
1379
  	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
  	i->private_host_attrs[count].store = NULL;			\
  	i->host_attrs[count] = &i->private_host_attrs[count];		\
  	count++
  
  #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)			\
91ca7b01e   Andrew Vasquez   [SCSI] Add an 'Is...
1380
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
1381
  	i->private_host_attrs[count] = device_attr_host_##field;	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1382
  	i->host_attrs[count] = &i->private_host_attrs[count];		\
91ca7b01e   Andrew Vasquez   [SCSI] Add an 'Is...
1383
1384
  	count++;							\
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1385
1386
1387
1388
1389
  
  
  /* Fixed Host Attributes */
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1390
1391
  show_fc_host_supported_classes (struct device *dev,
  			        struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1392
  {
ee959b00c   Tony Jones   SCSI: convert str...
1393
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1394
1395
1396
1397
1398
1399
1400
  
  	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
  		return snprintf(buf, 20, "unspecified
  ");
  
  	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
  }
ee959b00c   Tony Jones   SCSI: convert str...
1401
  static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1402
1403
1404
  		show_fc_host_supported_classes, NULL);
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1405
1406
  show_fc_host_supported_fc4s (struct device *dev,
  			     struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1407
  {
ee959b00c   Tony Jones   SCSI: convert str...
1408
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1409
1410
  	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
  }
ee959b00c   Tony Jones   SCSI: convert str...
1411
  static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1412
1413
1414
  		show_fc_host_supported_fc4s, NULL);
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1415
1416
  show_fc_host_supported_speeds (struct device *dev,
  			       struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1417
  {
ee959b00c   Tony Jones   SCSI: convert str...
1418
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1419
1420
1421
1422
1423
1424
1425
  
  	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
  		return snprintf(buf, 20, "unknown
  ");
  
  	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
  }
ee959b00c   Tony Jones   SCSI: convert str...
1426
  static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1427
1428
1429
1430
1431
1432
1433
  		show_fc_host_supported_speeds, NULL);
  
  
  fc_private_host_rd_attr_cast(node_name, "0x%llx
  ", 20, unsigned long long);
  fc_private_host_rd_attr_cast(port_name, "0x%llx
  ", 20, unsigned long long);
6b7281d0a   Andreas Herrmann   [SCSI] fc transpo...
1434
1435
1436
  fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx
  ", 20,
  			     unsigned long long);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1437
1438
  fc_private_host_rd_attr(maxframe_size, "%u bytes
  ", 20);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1439
1440
  fc_private_host_rd_attr(max_npiv_vports, "%u
  ", 20);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1441
1442
1443
1444
1445
1446
1447
  fc_private_host_rd_attr(serial_number, "%s
  ", (FC_SERIAL_NUMBER_SIZE +1));
  
  
  /* Dynamic Host Attributes */
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1448
1449
  show_fc_host_active_fc4s (struct device *dev,
  			  struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1450
  {
ee959b00c   Tony Jones   SCSI: convert str...
1451
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1452
1453
1454
1455
1456
1457
1458
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  
  	if (i->f->get_host_active_fc4s)
  		i->f->get_host_active_fc4s(shost);
  
  	return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
  }
ee959b00c   Tony Jones   SCSI: convert str...
1459
  static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1460
1461
1462
  		show_fc_host_active_fc4s, NULL);
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1463
1464
  show_fc_host_speed (struct device *dev,
  		    struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1465
  {
ee959b00c   Tony Jones   SCSI: convert str...
1466
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  
  	if (i->f->get_host_speed)
  		i->f->get_host_speed(shost);
  
  	if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
  		return snprintf(buf, 20, "unknown
  ");
  
  	return get_fc_port_speed_names(fc_host_speed(shost), buf);
  }
ee959b00c   Tony Jones   SCSI: convert str...
1478
  static FC_DEVICE_ATTR(host, speed, S_IRUGO,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1479
1480
1481
1482
1483
1484
1485
1486
1487
  		show_fc_host_speed, NULL);
  
  
  fc_host_rd_attr(port_id, "0x%06x
  ", 20);
  fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
  fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  fc_host_rd_attr_cast(fabric_name, "0x%llx
  ", 20, unsigned long long);
016131b8f   James Smart   [SCSI] fc transpo...
1488
1489
  fc_host_rd_attr(symbolic_name, "%s
  ", FC_SYMBOLIC_NAME_SIZE + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1490

b8d082101   James Smart   [SCSI] fc transpo...
1491
1492
1493
1494
  fc_private_host_show_function(system_hostname, "%s
  ",
  		FC_SYMBOLIC_NAME_SIZE + 1, )
  fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
ee959b00c   Tony Jones   SCSI: convert str...
1495
  static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
b8d082101   James Smart   [SCSI] fc transpo...
1496
  		show_fc_host_system_hostname, store_fc_host_system_hostname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1497
1498
1499
1500
  
  /* Private Host Attributes */
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1501
1502
  show_fc_private_host_tgtid_bind_type(struct device *dev,
  				     struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1503
  {
ee959b00c   Tony Jones   SCSI: convert str...
1504
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1505
1506
1507
1508
1509
1510
1511
1512
  	const char *name;
  
  	name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
  	if (!name)
  		return -EINVAL;
  	return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s
  ", name);
  }
d16794f6a   James.Smart@Emulex.Com   [SCSI] FW: [PATCH...
1513
1514
  #define get_list_head_entry(pos, head, member) 		\
  	pos = list_entry((head)->next, typeof(*pos), member)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1515
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1516
1517
  store_fc_private_host_tgtid_bind_type(struct device *dev,
  	struct device_attribute *attr, const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1518
  {
ee959b00c   Tony Jones   SCSI: convert str...
1519
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
d16794f6a   James.Smart@Emulex.Com   [SCSI] FW: [PATCH...
1520
  	struct fc_rport *rport;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1521
1522
1523
1524
1525
1526
1527
1528
1529
   	enum fc_tgtid_binding_type val;
  	unsigned long flags;
  
  	if (get_fc_tgtid_bind_type_match(buf, &val))
  		return -EINVAL;
  
  	/* if changing bind type, purge all unused consistent bindings */
  	if (val != fc_host_tgtid_bind_type(shost)) {
  		spin_lock_irqsave(shost->host_lock, flags);
d16794f6a   James.Smart@Emulex.Com   [SCSI] FW: [PATCH...
1530
1531
1532
  		while (!list_empty(&fc_host_rport_bindings(shost))) {
  			get_list_head_entry(rport,
  				&fc_host_rport_bindings(shost), peers);
aedf34977   James Smart   [SCSI] FC transpo...
1533
1534
1535
  			list_del(&rport->peers);
  			rport->port_state = FC_PORTSTATE_DELETED;
  			fc_queue_work(shost, &rport->rport_delete_work);
d16794f6a   James.Smart@Emulex.Com   [SCSI] FW: [PATCH...
1536
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1537
1538
1539
1540
1541
1542
  		spin_unlock_irqrestore(shost->host_lock, flags);
  	}
  
  	fc_host_tgtid_bind_type(shost) = val;
  	return count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
1543
  static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1544
1545
  			show_fc_private_host_tgtid_bind_type,
  			store_fc_private_host_tgtid_bind_type);
91ca7b01e   Andrew Vasquez   [SCSI] Add an 'Is...
1546
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1547
1548
  store_fc_private_host_issue_lip(struct device *dev,
  	struct device_attribute *attr, const char *buf, size_t count)
91ca7b01e   Andrew Vasquez   [SCSI] Add an 'Is...
1549
  {
ee959b00c   Tony Jones   SCSI: convert str...
1550
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
91ca7b01e   Andrew Vasquez   [SCSI] Add an 'Is...
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  	int ret;
  
  	/* ignore any data value written to the attribute */
  	if (i->f->issue_fc_host_lip) {
  		ret = i->f->issue_fc_host_lip(shost);
  		return ret ? ret: count;
  	}
  
  	return -ENOENT;
  }
ee959b00c   Tony Jones   SCSI: convert str...
1562
  static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
91ca7b01e   Andrew Vasquez   [SCSI] Add an 'Is...
1563
  			store_fc_private_host_issue_lip);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1564
1565
  fc_private_host_rd_attr(npiv_vports_inuse, "%u
  ", 20);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1566
1567
1568
1569
1570
1571
  /*
   * Host Statistics Management
   */
  
  /* Show a given an attribute in the statistics group */
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1572
  fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1573
  {
ee959b00c   Tony Jones   SCSI: convert str...
1574
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  	struct fc_host_statistics *stats;
  	ssize_t ret = -ENOENT;
  
  	if (offset > sizeof(struct fc_host_statistics) ||
  	    offset % sizeof(u64) != 0)
  		WARN_ON(1);
  
  	if (i->f->get_fc_host_stats) {
  		stats = (i->f->get_fc_host_stats)(shost);
  		if (stats)
  			ret = snprintf(buf, 20, "0x%llx
  ",
  			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
  	}
  	return ret;
  }
  
  
  /* generate a read-only statistics attribute */
  #define fc_host_statistic(name)						\
ee959b00c   Tony Jones   SCSI: convert str...
1596
1597
1598
  static ssize_t show_fcstat_##name(struct device *cd,			\
  				  struct device_attribute *attr,	\
  				  char *buf)				\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1599
1600
1601
1602
  {									\
  	return fc_stat_show(cd, buf, 					\
  			    offsetof(struct fc_host_statistics, name));	\
  }									\
ee959b00c   Tony Jones   SCSI: convert str...
1603
  static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
  
  fc_host_statistic(seconds_since_last_reset);
  fc_host_statistic(tx_frames);
  fc_host_statistic(tx_words);
  fc_host_statistic(rx_frames);
  fc_host_statistic(rx_words);
  fc_host_statistic(lip_count);
  fc_host_statistic(nos_count);
  fc_host_statistic(error_frames);
  fc_host_statistic(dumped_frames);
  fc_host_statistic(link_failure_count);
  fc_host_statistic(loss_of_sync_count);
  fc_host_statistic(loss_of_signal_count);
  fc_host_statistic(prim_seq_protocol_err_count);
  fc_host_statistic(invalid_tx_word_count);
  fc_host_statistic(invalid_crc_count);
  fc_host_statistic(fcp_input_requests);
  fc_host_statistic(fcp_output_requests);
  fc_host_statistic(fcp_control_requests);
  fc_host_statistic(fcp_input_megabytes);
  fc_host_statistic(fcp_output_megabytes);
  
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1627
1628
  fc_reset_statistics(struct device *dev, struct device_attribute *attr,
  		    const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1629
  {
ee959b00c   Tony Jones   SCSI: convert str...
1630
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  
  	/* ignore any data value written to the attribute */
  	if (i->f->reset_fc_host_stats) {
  		i->f->reset_fc_host_stats(shost);
  		return count;
  	}
  
  	return -ENOENT;
  }
ee959b00c   Tony Jones   SCSI: convert str...
1641
  static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1642
  				fc_reset_statistics);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1643
  static struct attribute *fc_statistics_attrs[] = {
ee959b00c   Tony Jones   SCSI: convert str...
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
  	&device_attr_host_seconds_since_last_reset.attr,
  	&device_attr_host_tx_frames.attr,
  	&device_attr_host_tx_words.attr,
  	&device_attr_host_rx_frames.attr,
  	&device_attr_host_rx_words.attr,
  	&device_attr_host_lip_count.attr,
  	&device_attr_host_nos_count.attr,
  	&device_attr_host_error_frames.attr,
  	&device_attr_host_dumped_frames.attr,
  	&device_attr_host_link_failure_count.attr,
  	&device_attr_host_loss_of_sync_count.attr,
  	&device_attr_host_loss_of_signal_count.attr,
  	&device_attr_host_prim_seq_protocol_err_count.attr,
  	&device_attr_host_invalid_tx_word_count.attr,
  	&device_attr_host_invalid_crc_count.attr,
  	&device_attr_host_fcp_input_requests.attr,
  	&device_attr_host_fcp_output_requests.attr,
  	&device_attr_host_fcp_control_requests.attr,
  	&device_attr_host_fcp_input_megabytes.attr,
  	&device_attr_host_fcp_output_megabytes.attr,
  	&device_attr_host_reset_statistics.attr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1665
1666
1667
1668
1669
1670
1671
  	NULL
  };
  
  static struct attribute_group fc_statistics_group = {
  	.name = "statistics",
  	.attrs = fc_statistics_attrs,
  };
a53eb5e06   James Smart   [SCSI] FC Transpo...
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
  
  /* Host Vport Attributes */
  
  static int
  fc_parse_wwn(const char *ns, u64 *nm)
  {
  	unsigned int i, j;
  	u8 wwn[8];
  
  	memset(wwn, 0, sizeof(wwn));
  
  	/* Validate and store the new name */
  	for (i=0, j=0; i < 16; i++) {
  		if ((*ns >= 'a') && (*ns <= 'f'))
  			j = ((j << 4) | ((*ns++ -'a') + 10));
  		else if ((*ns >= 'A') && (*ns <= 'F'))
  			j = ((j << 4) | ((*ns++ -'A') + 10));
  		else if ((*ns >= '0') && (*ns <= '9'))
  			j = ((j << 4) | (*ns++ -'0'));
  		else
  			return -EINVAL;
  		if (i % 2) {
  			wwn[i/2] = j & 0xff;
  			j = 0;
  		}
  	}
  
  	*nm = wwn_to_u64(wwn);
  
  	return 0;
  }
  
  
  /*
   * "Short-cut" sysfs variable to create a new vport on a FC Host.
   * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
   * will default to a NPIV-based FCP_Initiator; The WWNs are specified
   * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
   */
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1712
1713
  store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
  			   const char *buf, size_t count)
a53eb5e06   James Smart   [SCSI] FC Transpo...
1714
  {
ee959b00c   Tony Jones   SCSI: convert str...
1715
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
  	struct fc_vport_identifiers vid;
  	struct fc_vport *vport;
  	unsigned int cnt=count;
  	int stat;
  
  	memset(&vid, 0, sizeof(vid));
  
  	/* count may include a LF at end of string */
  	if (buf[cnt-1] == '
  ')
  		cnt--;
  
  	/* validate we have enough characters for WWPN */
  	if ((cnt != (16+1+16)) || (buf[16] != ':'))
  		return -EINVAL;
  
  	stat = fc_parse_wwn(&buf[0], &vid.port_name);
  	if (stat)
  		return stat;
  
  	stat = fc_parse_wwn(&buf[17], &vid.node_name);
  	if (stat)
  		return stat;
  
  	vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
  	vid.vport_type = FC_PORTTYPE_NPIV;
  	/* vid.symbolic_name is already zero/NULL's */
  	vid.disable = false;		/* always enabled */
  
  	/* we only allow support on Channel 0 !!! */
a30c3f69e   Andrew Vasquez   [SCSI] fc_transpo...
1746
  	stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1747
1748
  	return stat ? stat : count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
1749
  static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
a53eb5e06   James Smart   [SCSI] FC Transpo...
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
  			store_fc_host_vport_create);
  
  
  /*
   * "Short-cut" sysfs variable to delete a vport on a FC Host.
   * Vport is identified by a string containing "<WWPN>:<WWNN>".
   * The WWNs are specified as hex characters, and may *not* contain
   * any prefixes (e.g. 0x, x, etc)
   */
  static ssize_t
ee959b00c   Tony Jones   SCSI: convert str...
1760
1761
  store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
  			   const char *buf, size_t count)
a53eb5e06   James Smart   [SCSI] FC Transpo...
1762
  {
ee959b00c   Tony Jones   SCSI: convert str...
1763
  	struct Scsi_Host *shost = transport_class_to_shost(dev);
a53eb5e06   James Smart   [SCSI] FC Transpo...
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
  	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  	struct fc_vport *vport;
  	u64 wwpn, wwnn;
  	unsigned long flags;
  	unsigned int cnt=count;
  	int stat, match;
  
  	/* count may include a LF at end of string */
  	if (buf[cnt-1] == '
  ')
  		cnt--;
  
  	/* validate we have enough characters for WWPN */
  	if ((cnt != (16+1+16)) || (buf[16] != ':'))
  		return -EINVAL;
  
  	stat = fc_parse_wwn(&buf[0], &wwpn);
  	if (stat)
  		return stat;
  
  	stat = fc_parse_wwn(&buf[17], &wwnn);
  	if (stat)
  		return stat;
  
  	spin_lock_irqsave(shost->host_lock, flags);
  	match = 0;
  	/* we only allow support on Channel 0 !!! */
  	list_for_each_entry(vport, &fc_host->vports, peers) {
  		if ((vport->channel == 0) &&
  		    (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
  			match = 1;
  			break;
  		}
  	}
  	spin_unlock_irqrestore(shost->host_lock, flags);
  
  	if (!match)
  		return -ENODEV;
  
  	stat = fc_vport_terminate(vport);
  	return stat ? stat : count;
  }
ee959b00c   Tony Jones   SCSI: convert str...
1806
  static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
a53eb5e06   James Smart   [SCSI] FC Transpo...
1807
  			store_fc_host_vport_delete);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
  static int fc_host_match(struct attribute_container *cont,
  			  struct device *dev)
  {
  	struct Scsi_Host *shost;
  	struct fc_internal *i;
  
  	if (!scsi_is_host_device(dev))
  		return 0;
  
  	shost = dev_to_shost(dev);
  	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
  	    != &fc_host_class.class)
  		return 0;
  
  	i = to_fc_internal(shost->transportt);
  
  	return &i->t.host_attrs.ac == cont;
  }
  
  static int fc_target_match(struct attribute_container *cont,
  			    struct device *dev)
  {
  	struct Scsi_Host *shost;
  	struct fc_internal *i;
  
  	if (!scsi_is_target_device(dev))
  		return 0;
  
  	shost = dev_to_shost(dev->parent);
  	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
  	    != &fc_host_class.class)
  		return 0;
  
  	i = to_fc_internal(shost->transportt);
  
  	return &i->t.target_attrs.ac == cont;
  }
  
  static void fc_rport_dev_release(struct device *dev)
  {
  	struct fc_rport *rport = dev_to_rport(dev);
  	put_device(dev->parent);
  	kfree(rport);
  }
  
  int scsi_is_fc_rport(const struct device *dev)
  {
  	return dev->release == fc_rport_dev_release;
  }
  EXPORT_SYMBOL(scsi_is_fc_rport);
  
  static int fc_rport_match(struct attribute_container *cont,
  			    struct device *dev)
  {
  	struct Scsi_Host *shost;
  	struct fc_internal *i;
  
  	if (!scsi_is_fc_rport(dev))
  		return 0;
  
  	shost = dev_to_shost(dev->parent);
  	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
  	    != &fc_host_class.class)
  		return 0;
  
  	i = to_fc_internal(shost->transportt);
  
  	return &i->rport_attr_cont.ac == cont;
  }
5c44cd2af   James.Smart@Emulex.Com   [SCSI] fix target...
1877

a53eb5e06   James Smart   [SCSI] FC Transpo...
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
  static void fc_vport_dev_release(struct device *dev)
  {
  	struct fc_vport *vport = dev_to_vport(dev);
  	put_device(dev->parent);		/* release kobj parent */
  	kfree(vport);
  }
  
  int scsi_is_fc_vport(const struct device *dev)
  {
  	return dev->release == fc_vport_dev_release;
  }
  EXPORT_SYMBOL(scsi_is_fc_vport);
  
  static int fc_vport_match(struct attribute_container *cont,
  			    struct device *dev)
  {
  	struct fc_vport *vport;
  	struct Scsi_Host *shost;
  	struct fc_internal *i;
  
  	if (!scsi_is_fc_vport(dev))
  		return 0;
  	vport = dev_to_vport(dev);
  
  	shost = vport_to_shost(vport);
  	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
  	    != &fc_host_class.class)
  		return 0;
  
  	i = to_fc_internal(shost->transportt);
  	return &i->vport_attr_cont.ac == cont;
  }
c829c3941   James Smart   [SCSI] FC transpo...
1910
1911
  /**
   * fc_timed_out - FC Transport I/O timeout intercept handler
c829c3941   James Smart   [SCSI] FC transpo...
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
   * @scmd:	The SCSI command which timed out
   *
   * This routine protects against error handlers getting invoked while a
   * rport is in a blocked state, typically due to a temporarily loss of
   * connectivity. If the error handlers are allowed to proceed, requests
   * to abort i/o, reset the target, etc will likely fail as there is no way
   * to communicate with the device to perform the requested function. These
   * failures may result in the midlayer taking the device offline, requiring
   * manual intervention to restore operation.
   *
   * This routine, called whenever an i/o times out, validates the state of
   * the underlying rport. If the rport is blocked, it returns
   * EH_RESET_TIMER, which will continue to reschedule the timeout.
   * Eventually, either the device will return, or devloss_tmo will fire,
   * and when the timeout then fires, it will be handled normally.
   * If the rport is not blocked, normal error handling continues.
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
1931
   */
242f9dcb8   Jens Axboe   block: unify requ...
1932
  static enum blk_eh_timer_return
c829c3941   James Smart   [SCSI] FC transpo...
1933
1934
1935
1936
1937
  fc_timed_out(struct scsi_cmnd *scmd)
  {
  	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
  
  	if (rport->port_state == FC_PORTSTATE_BLOCKED)
242f9dcb8   Jens Axboe   block: unify requ...
1938
  		return BLK_EH_RESET_TIMER;
c829c3941   James Smart   [SCSI] FC transpo...
1939

242f9dcb8   Jens Axboe   block: unify requ...
1940
  	return BLK_EH_NOT_HANDLED;
c829c3941   James Smart   [SCSI] FC transpo...
1941
  }
5c44cd2af   James.Smart@Emulex.Com   [SCSI] fix target...
1942
  /*
bda232531   James Smart   [SCSI] scsi_trans...
1943
1944
1945
   * Called by fc_user_scan to locate an rport on the shost that
   * matches the channel and target id, and invoke scsi_scan_target()
   * on the rport.
5c44cd2af   James.Smart@Emulex.Com   [SCSI] fix target...
1946
   */
bda232531   James Smart   [SCSI] scsi_trans...
1947
1948
  static void
  fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
5c44cd2af   James.Smart@Emulex.Com   [SCSI] fix target...
1949
1950
  {
  	struct fc_rport *rport;
bda232531   James Smart   [SCSI] scsi_trans...
1951
1952
1953
  	unsigned long flags;
  
  	spin_lock_irqsave(shost->host_lock, flags);
5c44cd2af   James.Smart@Emulex.Com   [SCSI] fix target...
1954

e02f3f592   Christoph Hellwig   [SCSI] remove tar...
1955
1956
1957
  	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
  		if (rport->scsi_target_id == -1)
  			continue;
5c44cd2af   James.Smart@Emulex.Com   [SCSI] fix target...
1958

0d2fcd9f9   Hannes Reinecke   [SCSI] fc_transpo...
1959
1960
  		if (rport->port_state != FC_PORTSTATE_ONLINE)
  			continue;
bda232531   James Smart   [SCSI] scsi_trans...
1961
1962
1963
1964
1965
  		if ((channel == rport->channel) &&
  		    (id == rport->scsi_target_id)) {
  			spin_unlock_irqrestore(shost->host_lock, flags);
  			scsi_scan_target(&rport->dev, channel, id, lun, 1);
  			return;
e02f3f592   Christoph Hellwig   [SCSI] remove tar...
1966
1967
  		}
  	}
bda232531   James Smart   [SCSI] scsi_trans...
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
  	spin_unlock_irqrestore(shost->host_lock, flags);
  }
  
  /*
   * Called via sysfs scan routines. Necessary, as the FC transport
   * wants to place all target objects below the rport object. So this
   * routine must invoke the scsi_scan_target() routine with the rport
   * object as the parent.
   */
  static int
  fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
  {
  	uint chlo, chhi;
  	uint tgtlo, tgthi;
  
  	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
  	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
  	    ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
  		return -EINVAL;
  
  	if (channel == SCAN_WILD_CARD) {
  		chlo = 0;
  		chhi = shost->max_channel + 1;
  	} else {
  		chlo = channel;
  		chhi = channel + 1;
  	}
  
  	if (id == SCAN_WILD_CARD) {
  		tgtlo = 0;
  		tgthi = shost->max_id;
  	} else {
  		tgtlo = id;
  		tgthi = id + 1;
  	}
  
  	for ( ; chlo < chhi; chlo++)
  		for ( ; tgtlo < tgthi; tgtlo++)
  			fc_user_scan_tgt(shost, chlo, tgtlo, lun);
e02f3f592   Christoph Hellwig   [SCSI] remove tar...
2007
  	return 0;
5c44cd2af   James.Smart@Emulex.Com   [SCSI] fix target...
2008
  }
7525236d0   FUJITA Tomonori   [SCSI] fc_transpo...
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
  static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
  				int result)
  {
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  	return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
  }
  
  static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
  {
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  	return i->f->it_nexus_response(shost, nexus, result);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2021
2022
2023
  struct scsi_transport_template *
  fc_attach_transport(struct fc_function_template *ft)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2024
  	int count;
24669f75a   Jes Sorensen   [SCSI] SCSI core ...
2025
2026
  	struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
  					GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2027
2028
2029
  
  	if (unlikely(!i))
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
  	i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
  	i->t.target_attrs.ac.class = &fc_transport_class.class;
  	i->t.target_attrs.ac.match = fc_target_match;
  	i->t.target_size = sizeof(struct fc_starget_attrs);
  	transport_container_register(&i->t.target_attrs);
  
  	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  	i->t.host_attrs.ac.class = &fc_host_class.class;
  	i->t.host_attrs.ac.match = fc_host_match;
  	i->t.host_size = sizeof(struct fc_host_attrs);
  	if (ft->get_fc_host_stats)
  		i->t.host_attrs.statistics = &fc_statistics_group;
  	transport_container_register(&i->t.host_attrs);
  
  	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  	i->rport_attr_cont.ac.class = &fc_rport_class.class;
  	i->rport_attr_cont.ac.match = fc_rport_match;
  	transport_container_register(&i->rport_attr_cont);
a53eb5e06   James Smart   [SCSI] FC Transpo...
2048
2049
2050
2051
  	i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
  	i->vport_attr_cont.ac.class = &fc_vport_class.class;
  	i->vport_attr_cont.ac.match = fc_vport_match;
  	transport_container_register(&i->vport_attr_cont);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2052
2053
2054
2055
  	i->f = ft;
  
  	/* Transport uses the shost workq for scsi scanning */
  	i->t.create_work_queue = 1;
5c44cd2af   James.Smart@Emulex.Com   [SCSI] fix target...
2056

c829c3941   James Smart   [SCSI] FC transpo...
2057
  	i->t.eh_timed_out = fc_timed_out;
e02f3f592   Christoph Hellwig   [SCSI] remove tar...
2058
  	i->t.user_scan = fc_user_scan;
9ef3e4a45   James Smart   [SCSI] fc_transpo...
2059

7525236d0   FUJITA Tomonori   [SCSI] fc_transpo...
2060
2061
2062
  	/* target-mode drivers' functions */
  	i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
  	i->t.it_nexus_response = fc_it_nexus_response;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
  	/*
  	 * Setup SCSI Target Attributes.
  	 */
  	count = 0;
  	SETUP_STARGET_ATTRIBUTE_RD(node_name);
  	SETUP_STARGET_ATTRIBUTE_RD(port_name);
  	SETUP_STARGET_ATTRIBUTE_RD(port_id);
  
  	BUG_ON(count > FC_STARGET_NUM_ATTRS);
  
  	i->starget_attrs[count] = NULL;
  
  
  	/*
  	 * Setup SCSI Host Attributes.
  	 */
  	count=0;
  	SETUP_HOST_ATTRIBUTE_RD(node_name);
  	SETUP_HOST_ATTRIBUTE_RD(port_name);
6b7281d0a   Andreas Herrmann   [SCSI] fc transpo...
2082
  	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2083
2084
  	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
  	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2085
2086
  	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
  	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
a53eb5e06   James Smart   [SCSI] FC Transpo...
2087
2088
2089
2090
  	if (ft->vport_create) {
  		SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
  		SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2091
2092
2093
2094
2095
2096
2097
2098
  	SETUP_HOST_ATTRIBUTE_RD(serial_number);
  
  	SETUP_HOST_ATTRIBUTE_RD(port_id);
  	SETUP_HOST_ATTRIBUTE_RD(port_type);
  	SETUP_HOST_ATTRIBUTE_RD(port_state);
  	SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
  	SETUP_HOST_ATTRIBUTE_RD(speed);
  	SETUP_HOST_ATTRIBUTE_RD(fabric_name);
016131b8f   James Smart   [SCSI] fc transpo...
2099
  	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
b8d082101   James Smart   [SCSI] fc transpo...
2100
  	SETUP_HOST_ATTRIBUTE_RW(system_hostname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2101
2102
2103
  
  	/* Transport-managed attributes */
  	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
91ca7b01e   Andrew Vasquez   [SCSI] Add an 'Is...
2104
2105
  	if (ft->issue_fc_host_lip)
  		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
a53eb5e06   James Smart   [SCSI] FC Transpo...
2106
2107
2108
2109
  	if (ft->vport_create)
  		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
  	if (ft->vport_delete)
  		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
  
  	BUG_ON(count > FC_HOST_NUM_ATTRS);
  
  	i->host_attrs[count] = NULL;
  
  	/*
  	 * Setup Remote Port Attributes.
  	 */
  	count=0;
  	SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
  	SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
  	SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
  	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
  	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
  	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
  	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
  	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
  	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
fff9d40ce   Mike Christie   [SCSI] fc class: ...
2128
  	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2129
2130
2131
2132
  
  	BUG_ON(count > FC_RPORT_NUM_ATTRS);
  
  	i->rport_attrs[count] = NULL;
a53eb5e06   James Smart   [SCSI] FC Transpo...
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
  	/*
  	 * Setup Virtual Port Attributes.
  	 */
  	count=0;
  	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
  	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
  	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
  	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
  	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
  	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
  	SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
  	SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
  	SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
  
  	BUG_ON(count > FC_VPORT_NUM_ATTRS);
  
  	i->vport_attrs[count] = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
  	return &i->t;
  }
  EXPORT_SYMBOL(fc_attach_transport);
  
  void fc_release_transport(struct scsi_transport_template *t)
  {
  	struct fc_internal *i = to_fc_internal(t);
  
  	transport_container_unregister(&i->t.target_attrs);
  	transport_container_unregister(&i->t.host_attrs);
  	transport_container_unregister(&i->rport_attr_cont);
a53eb5e06   James Smart   [SCSI] FC Transpo...
2161
  	transport_container_unregister(&i->vport_attr_cont);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2162
2163
2164
2165
  
  	kfree(i);
  }
  EXPORT_SYMBOL(fc_release_transport);
aedf34977   James Smart   [SCSI] FC transpo...
2166
2167
2168
2169
2170
2171
  /**
   * fc_queue_work - Queue work to the fc_host workqueue.
   * @shost:	Pointer to Scsi_Host bound to fc_host.
   * @work:	Work to queue for execution.
   *
   * Return value:
a0785edff   James Smart   [SCSI] fc transpo...
2172
2173
2174
   * 	1 - work queued for execution
   *	0 - work is already queued
   *	-EINVAL - work queue doesn't exist
eb44820c2   Rob Landley   [SCSI] Add Docume...
2175
   */
aedf34977   James Smart   [SCSI] FC transpo...
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
  static int
  fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
  {
  	if (unlikely(!fc_host_work_q(shost))) {
  		printk(KERN_ERR
  			"ERROR: FC host '%s' attempted to queue work, "
  			"when no workqueue created.
  ", shost->hostt->name);
  		dump_stack();
  
  		return -EINVAL;
  	}
  
  	return queue_work(fc_host_work_q(shost), work);
  }
  
  /**
   * fc_flush_work - Flush a fc_host's workqueue.
   * @shost:	Pointer to Scsi_Host bound to fc_host.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2195
   */
aedf34977   James Smart   [SCSI] FC transpo...
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
  static void
  fc_flush_work(struct Scsi_Host *shost)
  {
  	if (!fc_host_work_q(shost)) {
  		printk(KERN_ERR
  			"ERROR: FC host '%s' attempted to flush work, "
  			"when no workqueue created.
  ", shost->hostt->name);
  		dump_stack();
  		return;
  	}
  
  	flush_workqueue(fc_host_work_q(shost));
  }
  
  /**
   * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
   * @shost:	Pointer to Scsi_Host bound to fc_host.
   * @work:	Work to queue for execution.
   * @delay:	jiffies to delay the work queuing
   *
   * Return value:
0f29b966d   James Smart   [SCSI] FC transpo...
2218
   * 	1 on success / 0 already queued / < 0 for error
eb44820c2   Rob Landley   [SCSI] Add Docume...
2219
   */
aedf34977   James Smart   [SCSI] FC transpo...
2220
  static int
c4028958b   David Howells   WorkStruct: make ...
2221
  fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
aedf34977   James Smart   [SCSI] FC transpo...
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
  				unsigned long delay)
  {
  	if (unlikely(!fc_host_devloss_work_q(shost))) {
  		printk(KERN_ERR
  			"ERROR: FC host '%s' attempted to queue work, "
  			"when no workqueue created.
  ", shost->hostt->name);
  		dump_stack();
  
  		return -EINVAL;
  	}
  
  	return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
  }
  
  /**
   * fc_flush_devloss - Flush a fc_host's devloss workqueue.
   * @shost:	Pointer to Scsi_Host bound to fc_host.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2240
   */
aedf34977   James Smart   [SCSI] FC transpo...
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
  static void
  fc_flush_devloss(struct Scsi_Host *shost)
  {
  	if (!fc_host_devloss_work_q(shost)) {
  		printk(KERN_ERR
  			"ERROR: FC host '%s' attempted to flush work, "
  			"when no workqueue created.
  ", shost->hostt->name);
  		dump_stack();
  		return;
  	}
  
  	flush_workqueue(fc_host_devloss_work_q(shost));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2255
2256
  
  /**
eb44820c2   Rob Landley   [SCSI] Add Docume...
2257
2258
   * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
   * @shost:	Which &Scsi_Host
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2259
2260
2261
2262
2263
   *
   * This routine is expected to be called immediately preceeding the
   * a driver's call to scsi_remove_host().
   *
   * WARNING: A driver utilizing the fc_transport, which fails to call
eb44820c2   Rob Landley   [SCSI] Add Docume...
2264
   *   this routine prior to scsi_remove_host(), will leave dangling
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2265
2266
2267
2268
2269
   *   objects in /sys/class/fc_remote_ports. Access to any of these
   *   objects can result in a system crash !!!
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2270
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2271
2272
2273
  void
  fc_remove_host(struct Scsi_Host *shost)
  {
a53eb5e06   James Smart   [SCSI] FC Transpo...
2274
2275
  	struct fc_vport *vport = NULL, *next_vport = NULL;
  	struct fc_rport *rport = NULL, *next_rport = NULL;
aedf34977   James Smart   [SCSI] FC transpo...
2276
2277
  	struct workqueue_struct *work_q;
  	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
a53eb5e06   James Smart   [SCSI] FC Transpo...
2278
  	unsigned long flags;
a53eb5e06   James Smart   [SCSI] FC Transpo...
2279
2280
2281
2282
  
  	spin_lock_irqsave(shost->host_lock, flags);
  
  	/* Remove any vports */
9ef3e4a45   James Smart   [SCSI] fc_transpo...
2283
2284
  	list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
  		fc_queue_work(shost, &vport->vport_delete_work);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2285
2286
2287
  
  	/* Remove any remote ports */
  	list_for_each_entry_safe(rport, next_rport,
aedf34977   James Smart   [SCSI] FC transpo...
2288
2289
2290
2291
2292
  			&fc_host->rports, peers) {
  		list_del(&rport->peers);
  		rport->port_state = FC_PORTSTATE_DELETED;
  		fc_queue_work(shost, &rport->rport_delete_work);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2293
  	list_for_each_entry_safe(rport, next_rport,
aedf34977   James Smart   [SCSI] FC transpo...
2294
2295
2296
2297
2298
  			&fc_host->rport_bindings, peers) {
  		list_del(&rport->peers);
  		rport->port_state = FC_PORTSTATE_DELETED;
  		fc_queue_work(shost, &rport->rport_delete_work);
  	}
a53eb5e06   James Smart   [SCSI] FC Transpo...
2299
  	spin_unlock_irqrestore(shost->host_lock, flags);
aedf34977   James Smart   [SCSI] FC transpo...
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
  	/* flush all scan work items */
  	scsi_flush_work(shost);
  
  	/* flush all stgt delete, and rport delete work items, then kill it  */
  	if (fc_host->work_q) {
  		work_q = fc_host->work_q;
  		fc_host->work_q = NULL;
  		destroy_workqueue(work_q);
  	}
  
  	/* flush all devloss work items, then kill it  */
  	if (fc_host->devloss_work_q) {
  		work_q = fc_host->devloss_work_q;
  		fc_host->devloss_work_q = NULL;
  		destroy_workqueue(work_q);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2316
2317
  }
  EXPORT_SYMBOL(fc_remove_host);
fff9d40ce   Mike Christie   [SCSI] fc class: ...
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
  static void fc_terminate_rport_io(struct fc_rport *rport)
  {
  	struct Scsi_Host *shost = rport_to_shost(rport);
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  
  	/* Involve the LLDD if possible to terminate all io on the rport. */
  	if (i->f->terminate_rport_io)
  		i->f->terminate_rport_io(rport);
  
  	/*
  	 * must unblock to flush queued IO. The caller will have set
  	 * the port_state or flags, so that fc_remote_port_chkready will
  	 * fail IO.
  	 */
  	scsi_target_unblock(&rport->dev);
  }
aedf34977   James Smart   [SCSI] FC transpo...
2334
2335
2336
  
  /**
   * fc_starget_delete - called to delete the scsi decendents of an rport
c4028958b   David Howells   WorkStruct: make ...
2337
   * @work:	remote port to be operated on.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2338
2339
2340
   *
   * Deletes target and all sdevs.
   */
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2341
  static void
c4028958b   David Howells   WorkStruct: make ...
2342
  fc_starget_delete(struct work_struct *work)
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2343
  {
c4028958b   David Howells   WorkStruct: make ...
2344
2345
  	struct fc_rport *rport =
  		container_of(work, struct fc_rport, stgt_delete_work);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2346

fff9d40ce   Mike Christie   [SCSI] fc class: ...
2347
  	fc_terminate_rport_io(rport);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2348
2349
  	scsi_remove_target(&rport->dev);
  }
aedf34977   James Smart   [SCSI] FC transpo...
2350
2351
2352
  
  /**
   * fc_rport_final_delete - finish rport termination and delete it.
c4028958b   David Howells   WorkStruct: make ...
2353
   * @work:	remote port to be deleted.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2354
   */
aedf34977   James Smart   [SCSI] FC transpo...
2355
  static void
c4028958b   David Howells   WorkStruct: make ...
2356
  fc_rport_final_delete(struct work_struct *work)
aedf34977   James Smart   [SCSI] FC transpo...
2357
  {
c4028958b   David Howells   WorkStruct: make ...
2358
2359
  	struct fc_rport *rport =
  		container_of(work, struct fc_rport, rport_delete_work);
aedf34977   James Smart   [SCSI] FC transpo...
2360
2361
  	struct device *dev = &rport->dev;
  	struct Scsi_Host *shost = rport_to_shost(rport);
0f29b966d   James Smart   [SCSI] FC transpo...
2362
  	struct fc_internal *i = to_fc_internal(shost->transportt);
92740b24c   James Smart   [SCSI] fc_transpo...
2363
  	unsigned long flags;
aedf34977   James Smart   [SCSI] FC transpo...
2364
2365
  
  	/*
9ef3e4a45   James Smart   [SCSI] fc_transpo...
2366
  	 * if a scan is pending, flush the SCSI Host work_q so that
aedf34977   James Smart   [SCSI] FC transpo...
2367
2368
2369
2370
  	 * that we can reclaim the rport scan work element.
  	 */
  	if (rport->flags & FC_RPORT_SCAN_PENDING)
  		scsi_flush_work(shost);
fff9d40ce   Mike Christie   [SCSI] fc class: ...
2371
  	fc_terminate_rport_io(rport);
92740b24c   James Smart   [SCSI] fc_transpo...
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
  	/*
  	 * Cancel any outstanding timers. These should really exist
  	 * only when rmmod'ing the LLDD and we're asking for
  	 * immediate termination of the rports
  	 */
  	spin_lock_irqsave(shost->host_lock, flags);
  	if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
  		spin_unlock_irqrestore(shost->host_lock, flags);
  		if (!cancel_delayed_work(&rport->fail_io_work))
  			fc_flush_devloss(shost);
  		if (!cancel_delayed_work(&rport->dev_loss_work))
  			fc_flush_devloss(shost);
  		spin_lock_irqsave(shost->host_lock, flags);
  		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  	}
  	spin_unlock_irqrestore(shost->host_lock, flags);
0f29b966d   James Smart   [SCSI] FC transpo...
2388
2389
  	/* Delete SCSI target and sdevs */
  	if (rport->scsi_target_id != -1)
c4028958b   David Howells   WorkStruct: make ...
2390
  		fc_starget_delete(&rport->stgt_delete_work);
92740b24c   James Smart   [SCSI] fc_transpo...
2391
2392
2393
2394
  
  	/*
  	 * Notify the driver that the rport is now dead. The LLDD will
  	 * also guarantee that any communication to the rport is terminated
4be98c0ca   James Smart   [SCSI] fc transpo...
2395
2396
2397
  	 *
  	 * Avoid this call if we already called it when we preserved the
  	 * rport for the binding.
92740b24c   James Smart   [SCSI] fc_transpo...
2398
  	 */
4be98c0ca   James Smart   [SCSI] fc transpo...
2399
2400
  	if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
  	    (i->f->dev_loss_tmo_callbk))
0f29b966d   James Smart   [SCSI] FC transpo...
2401
  		i->f->dev_loss_tmo_callbk(rport);
0f29b966d   James Smart   [SCSI] FC transpo...
2402

aedf34977   James Smart   [SCSI] FC transpo...
2403
2404
2405
  	transport_remove_device(dev);
  	device_del(dev);
  	transport_destroy_device(dev);
3bdad7bd2   James Smart   [SCSI] fc transpo...
2406
2407
  	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
  	put_device(dev);			/* for self-reference */
aedf34977   James Smart   [SCSI] FC transpo...
2408
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
  /**
   * fc_rport_create - allocates and creates a remote FC port.
   * @shost:	scsi host the remote port is connected to.
   * @channel:	Channel on shost port connected to.
   * @ids:	The world wide names, fc address, and FC4 port
   *		roles for the remote port.
   *
   * Allocates and creates the remoter port structure, including the
   * class and sysfs creation.
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2421
   */
44818efba   Adrian Bunk   [SCSI] small clea...
2422
  static struct fc_rport *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2423
2424
2425
  fc_rport_create(struct Scsi_Host *shost, int channel,
  	struct fc_rport_identifiers  *ids)
  {
aedf34977   James Smart   [SCSI] FC transpo...
2426
  	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2427
2428
2429
2430
2431
2432
2433
2434
  	struct fc_internal *fci = to_fc_internal(shost->transportt);
  	struct fc_rport *rport;
  	struct device *dev;
  	unsigned long flags;
  	int error;
  	size_t size;
  
  	size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
24669f75a   Jes Sorensen   [SCSI] SCSI core ...
2435
  	rport = kzalloc(size, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2436
  	if (unlikely(!rport)) {
cadbd4a5e   Harvey Harrison   [SCSI] replace __...
2437
2438
  		printk(KERN_ERR "%s: allocation failure
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2439
2440
  		return NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
  
  	rport->maxframe_size = -1;
  	rport->supported_classes = FC_COS_UNSPECIFIED;
  	rport->dev_loss_tmo = fc_dev_loss_tmo;
  	memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
  	memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
  	rport->port_id = ids->port_id;
  	rport->roles = ids->roles;
  	rport->port_state = FC_PORTSTATE_ONLINE;
  	if (fci->f->dd_fcrport_size)
  		rport->dd_data = &rport[1];
  	rport->channel = channel;
0f29b966d   James Smart   [SCSI] FC transpo...
2453
  	rport->fast_io_fail_tmo = -1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2454

c4028958b   David Howells   WorkStruct: make ...
2455
2456
2457
2458
2459
  	INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
  	INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
  	INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
  	INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
  	INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2460
2461
2462
2463
  
  	spin_lock_irqsave(shost->host_lock, flags);
  
  	rport->number = fc_host->next_rport_number++;
a53eb5e06   James Smart   [SCSI] FC Transpo...
2464
  	if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2465
2466
2467
  		rport->scsi_target_id = fc_host->next_target_id++;
  	else
  		rport->scsi_target_id = -1;
aedf34977   James Smart   [SCSI] FC transpo...
2468
  	list_add_tail(&rport->peers, &fc_host->rports);
3bdad7bd2   James Smart   [SCSI] fc transpo...
2469
  	get_device(&shost->shost_gendev);	/* for fc_host->rport list */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2470
2471
2472
2473
  
  	spin_unlock_irqrestore(shost->host_lock, flags);
  
  	dev = &rport->dev;
3bdad7bd2   James Smart   [SCSI] fc transpo...
2474
2475
  	device_initialize(dev);			/* takes self reference */
  	dev->parent = get_device(&shost->shost_gendev); /* parent reference */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2476
  	dev->release = fc_rport_dev_release;
71610f55f   Kay Sievers   [SCSI] struct dev...
2477
2478
  	dev_set_name(dev, "rport-%d:%d-%d",
  		     shost->host_no, channel, rport->number);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
  	transport_setup_device(dev);
  
  	error = device_add(dev);
  	if (error) {
  		printk(KERN_ERR "FC Remote Port device_add failed
  ");
  		goto delete_rport;
  	}
  	transport_add_device(dev);
  	transport_configure_device(dev);
a53eb5e06   James Smart   [SCSI] FC Transpo...
2489
  	if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2490
  		/* initiate a scan of the target */
aedf34977   James Smart   [SCSI] FC transpo...
2491
  		rport->flags |= FC_RPORT_SCAN_PENDING;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2492
  		scsi_queue_work(shost, &rport->scan_work);
aedf34977   James Smart   [SCSI] FC transpo...
2493
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2494
2495
2496
2497
2498
  
  	return rport;
  
  delete_rport:
  	transport_destroy_device(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2499
2500
  	spin_lock_irqsave(shost->host_lock, flags);
  	list_del(&rport->peers);
3bdad7bd2   James Smart   [SCSI] fc transpo...
2501
  	put_device(&shost->shost_gendev);	/* for fc_host->rport list */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2502
2503
2504
2505
2506
2507
2508
  	spin_unlock_irqrestore(shost->host_lock, flags);
  	put_device(dev->parent);
  	kfree(rport);
  	return NULL;
  }
  
  /**
eb44820c2   Rob Landley   [SCSI] Add Docume...
2509
   * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
   * @shost:	scsi host the remote port is connected to.
   * @channel:	Channel on shost port connected to.
   * @ids:	The world wide names, fc address, and FC4 port
   *		roles for the remote port.
   *
   * The LLDD calls this routine to notify the transport of the existence
   * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
   * of the port, it's FC address (port_id), and the FC4 roles that are
   * active for the port.
   *
   * For ports that are FCP targets (aka scsi targets), the FC transport
   * maintains consistent target id bindings on behalf of the LLDD.
   * A consistent target id binding is an assignment of a target id to
   * a remote port identifier, which persists while the scsi host is
   * attached. The remote port can disappear, then later reappear, and
   * it's target id assignment remains the same. This allows for shifts
   * in FC addressing (if binding by wwpn or wwnn) with no apparent
   * changes to the scsi subsystem which is based on scsi host number and
   * target id values.  Bindings are only valid during the attachment of
   * the scsi host. If the host detaches, then later re-attaches, target
   * id bindings may change.
   *
   * This routine is responsible for returning a remote port structure.
   * The routine will search the list of remote ports it maintains
   * internally on behalf of consistent target id mappings. If found, the
   * remote port structure will be reused. Otherwise, a new remote port
   * structure will be allocated.
   *
   * Whenever a remote port is allocated, a new fc_remote_port class
   * device is created.
   *
   * Should not be called from interrupt context.
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2545
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2546
2547
2548
2549
  struct fc_rport *
  fc_remote_port_add(struct Scsi_Host *shost, int channel,
  	struct fc_rport_identifiers  *ids)
  {
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2550
  	struct fc_internal *fci = to_fc_internal(shost->transportt);
aedf34977   James Smart   [SCSI] FC transpo...
2551
  	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2552
2553
2554
  	struct fc_rport *rport;
  	unsigned long flags;
  	int match = 0;
aedf34977   James Smart   [SCSI] FC transpo...
2555
2556
  	/* ensure any stgt delete functions are done */
  	fc_flush_work(shost);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2557
2558
2559
2560
2561
2562
  	/*
  	 * Search the list of "active" rports, for an rport that has been
  	 * deleted, but we've held off the real delete while the target
  	 * is in a "blocked" state.
  	 */
  	spin_lock_irqsave(shost->host_lock, flags);
aedf34977   James Smart   [SCSI] FC transpo...
2563
  	list_for_each_entry(rport, &fc_host->rports, peers) {
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2564
2565
2566
  
  		if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
  			(rport->channel == channel)) {
aedf34977   James Smart   [SCSI] FC transpo...
2567
  			switch (fc_host->tgtid_bind_type) {
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
  			case FC_TGTID_BIND_BY_WWPN:
  			case FC_TGTID_BIND_NONE:
  				if (rport->port_name == ids->port_name)
  					match = 1;
  				break;
  			case FC_TGTID_BIND_BY_WWNN:
  				if (rport->node_name == ids->node_name)
  					match = 1;
  				break;
  			case FC_TGTID_BIND_BY_ID:
  				if (rport->port_id == ids->port_id)
  					match = 1;
  				break;
  			}
  
  			if (match) {
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
  
  				memcpy(&rport->node_name, &ids->node_name,
  					sizeof(rport->node_name));
  				memcpy(&rport->port_name, &ids->port_name,
  					sizeof(rport->port_name));
  				rport->port_id = ids->port_id;
  
  				rport->port_state = FC_PORTSTATE_ONLINE;
  				rport->roles = ids->roles;
  
  				spin_unlock_irqrestore(shost->host_lock, flags);
  
  				if (fci->f->dd_fcrport_size)
  					memset(rport->dd_data, 0,
  						fci->f->dd_fcrport_size);
  
  				/*
92740b24c   James Smart   [SCSI] fc_transpo...
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
  				 * If we were not a target, cancel the
  				 * io terminate and rport timers, and
  				 * we're done.
  				 *
  				 * If we were a target, but our new role
  				 * doesn't indicate a target, leave the
  				 * timers running expecting the role to
  				 * change as the target fully logs in. If
  				 * it doesn't, the target will be torn down.
  				 *
  				 * If we were a target, and our role shows
  				 * we're still a target, cancel the timers
  				 * and kick off a scan.
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2614
  				 */
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2615

92740b24c   James Smart   [SCSI] fc_transpo...
2616
2617
  				/* was a target, not in roles */
  				if ((rport->scsi_target_id != -1) &&
a53eb5e06   James Smart   [SCSI] FC Transpo...
2618
  				    (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
92740b24c   James Smart   [SCSI] fc_transpo...
2619
  					return rport;
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2620
2621
  
  				/*
92740b24c   James Smart   [SCSI] fc_transpo...
2622
2623
2624
  				 * Stop the fail io and dev_loss timers.
  				 * If they flush, the port_state will
  				 * be checked and will NOOP the function.
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2625
  				 */
0f29b966d   James Smart   [SCSI] FC transpo...
2626
2627
  				if (!cancel_delayed_work(&rport->fail_io_work))
  					fc_flush_devloss(shost);
92740b24c   James Smart   [SCSI] fc_transpo...
2628
  				if (!cancel_delayed_work(&rport->dev_loss_work))
aedf34977   James Smart   [SCSI] FC transpo...
2629
2630
2631
  					fc_flush_devloss(shost);
  
  				spin_lock_irqsave(shost->host_lock, flags);
fff9d40ce   Mike Christie   [SCSI] fc class: ...
2632
  				rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
4be98c0ca   James Smart   [SCSI] fc transpo...
2633
2634
  						  FC_RPORT_DEVLOSS_PENDING |
  						  FC_RPORT_DEVLOSS_CALLBK_DONE);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2635

92740b24c   James Smart   [SCSI] fc_transpo...
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
  				/* if target, initiate a scan */
  				if (rport->scsi_target_id != -1) {
  					rport->flags |= FC_RPORT_SCAN_PENDING;
  					scsi_queue_work(shost,
  							&rport->scan_work);
  					spin_unlock_irqrestore(shost->host_lock,
  							flags);
  					scsi_target_unblock(&rport->dev);
  				} else
  					spin_unlock_irqrestore(shost->host_lock,
  							flags);
a0785edff   James Smart   [SCSI] fc transpo...
2647

19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2648
2649
2650
2651
  				return rport;
  			}
  		}
  	}
92740b24c   James Smart   [SCSI] fc_transpo...
2652
2653
2654
2655
  	/*
  	 * Search the bindings array
  	 * Note: if never a FCP target, you won't be on this list
  	 */
aedf34977   James Smart   [SCSI] FC transpo...
2656
  	if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2657
2658
  
  		/* search for a matching consistent binding */
aedf34977   James Smart   [SCSI] FC transpo...
2659
  		list_for_each_entry(rport, &fc_host->rport_bindings,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2660
2661
2662
  					peers) {
  			if (rport->channel != channel)
  				continue;
aedf34977   James Smart   [SCSI] FC transpo...
2663
  			switch (fc_host->tgtid_bind_type) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
  			case FC_TGTID_BIND_BY_WWPN:
  				if (rport->port_name == ids->port_name)
  					match = 1;
  				break;
  			case FC_TGTID_BIND_BY_WWNN:
  				if (rport->node_name == ids->node_name)
  					match = 1;
  				break;
  			case FC_TGTID_BIND_BY_ID:
  				if (rport->port_id == ids->port_id)
  					match = 1;
  				break;
  			case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  				break;
  			}
  
  			if (match) {
aedf34977   James Smart   [SCSI] FC transpo...
2681
  				list_move_tail(&rport->peers, &fc_host->rports);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2682
2683
2684
  				break;
  			}
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2685
2686
2687
2688
2689
2690
2691
2692
  		if (match) {
  			memcpy(&rport->node_name, &ids->node_name,
  				sizeof(rport->node_name));
  			memcpy(&rport->port_name, &ids->port_name,
  				sizeof(rport->port_name));
  			rport->port_id = ids->port_id;
  			rport->roles = ids->roles;
  			rport->port_state = FC_PORTSTATE_ONLINE;
fff9d40ce   Mike Christie   [SCSI] fc class: ...
2693
  			rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2694

19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2695
2696
2697
  			if (fci->f->dd_fcrport_size)
  				memset(rport->dd_data, 0,
  						fci->f->dd_fcrport_size);
a53eb5e06   James Smart   [SCSI] FC Transpo...
2698
  			if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2699
  				/* initiate a scan of the target */
aedf34977   James Smart   [SCSI] FC transpo...
2700
  				rport->flags |= FC_RPORT_SCAN_PENDING;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2701
  				scsi_queue_work(shost, &rport->scan_work);
a0785edff   James Smart   [SCSI] fc transpo...
2702
2703
2704
2705
  				spin_unlock_irqrestore(shost->host_lock, flags);
  				scsi_target_unblock(&rport->dev);
  			} else
  				spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2706
2707
2708
2709
  
  			return rport;
  		}
  	}
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2710
  	spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2711
2712
2713
2714
2715
2716
  	/* No consistent binding found - create new remote port entry */
  	rport = fc_rport_create(shost, channel, ids);
  
  	return rport;
  }
  EXPORT_SYMBOL(fc_remote_port_add);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2717
2718
  
  /**
eb44820c2   Rob Landley   [SCSI] Add Docume...
2719
   * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2720
2721
2722
2723
2724
   * @rport:	The remote port that no longer exists
   *
   * The LLDD calls this routine to notify the transport that a remote
   * port is no longer part of the topology. Note: Although a port
   * may no longer be part of the topology, it may persist in the remote
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2725
   * ports displayed by the fc_host. We do this under 2 conditions:
eb44820c2   Rob Landley   [SCSI] Add Docume...
2726
   * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2727
2728
2729
   *   This allows the port to temporarily disappear, then reappear without
   *   disrupting the SCSI device tree attached to it. During the "blocked"
   *   period the port will still exist.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2730
   * 2) If the port was a scsi target and disappears for longer than we
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2731
2732
2733
2734
2735
   *   expect, we'll delete the port and the tear down the SCSI device tree
   *   attached to it. However, we want to semi-persist the target id assigned
   *   to that port if it eventually does exist. The port structure will
   *   remain (although with minimal information) so that the target id
   *   bindings remails.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2736
2737
2738
2739
   *
   * If the remote port is not an FCP Target, it will be fully torn down
   * and deallocated, including the fc_remote_port class device.
   *
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2740
2741
2742
2743
   * If the remote port is an FCP Target, the port will be placed in a
   * temporary blocked state. From the LLDD's perspective, the rport no
   * longer exists. From the SCSI midlayer's perspective, the SCSI target
   * exists, but all sdevs on it are blocked from further I/O. The following
eb44820c2   Rob Landley   [SCSI] Add Docume...
2744
2745
   * is then expected.
   *
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2746
2747
2748
2749
2750
2751
2752
2753
2754
   *   If the remote port does not return (signaled by a LLDD call to
   *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
   *   scsi target is removed - killing all outstanding i/o and removing the
   *   scsi devices attached ot it. The port structure will be marked Not
   *   Present and be partially cleared, leaving only enough information to
   *   recognize the remote port relative to the scsi target id binding if
   *   it later appears.  The port will remain as long as there is a valid
   *   binding (e.g. until the user changes the binding type or unloads the
   *   scsi host with the binding).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2755
   *
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2756
2757
2758
2759
2760
2761
2762
2763
   *   If the remote port returns within the dev_loss_tmo value (and matches
   *   according to the target id binding type), the port structure will be
   *   reused. If it is no longer a SCSI target, the target will be torn
   *   down. If it continues to be a SCSI target, then the target will be
   *   unblocked (allowing i/o to be resumed), and a scan will be activated
   *   to ensure that all luns are detected.
   *
   * Called from normal process context only - cannot be called from interrupt.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2764
2765
2766
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2767
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2768
2769
2770
  void
  fc_remote_port_delete(struct fc_rport  *rport)
  {
aedf34977   James Smart   [SCSI] FC transpo...
2771
  	struct Scsi_Host *shost = rport_to_shost(rport);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2772
  	int timeout = rport->dev_loss_tmo;
aedf34977   James Smart   [SCSI] FC transpo...
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
  	unsigned long flags;
  
  	/*
  	 * No need to flush the fc_host work_q's, as all adds are synchronous.
  	 *
  	 * We do need to reclaim the rport scan work element, so eventually
  	 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
  	 * there's still a scan pending.
  	 */
  
  	spin_lock_irqsave(shost->host_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2784

92740b24c   James Smart   [SCSI] fc_transpo...
2785
  	if (rport->port_state != FC_PORTSTATE_ONLINE) {
aedf34977   James Smart   [SCSI] FC transpo...
2786
  		spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2787
2788
  		return;
  	}
92740b24c   James Smart   [SCSI] fc_transpo...
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
  	/*
  	 * In the past, we if this was not an FCP-Target, we would
  	 * unconditionally just jump to deleting the rport.
  	 * However, rports can be used as node containers by the LLDD,
  	 * and its not appropriate to just terminate the rport at the
  	 * first sign of a loss in connectivity. The LLDD may want to
  	 * send ELS traffic to re-validate the login. If the rport is
  	 * immediately deleted, it makes it inappropriate for a node
  	 * container.
  	 * So... we now unconditionally wait dev_loss_tmo before
  	 * destroying an rport.
  	 */
aedf34977   James Smart   [SCSI] FC transpo...
2801
2802
2803
2804
2805
  	rport->port_state = FC_PORTSTATE_BLOCKED;
  
  	rport->flags |= FC_RPORT_DEVLOSS_PENDING;
  
  	spin_unlock_irqrestore(shost->host_lock, flags);
7525236d0   FUJITA Tomonori   [SCSI] fc_transpo...
2806
2807
2808
  	if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
  	    shost->active_mode & MODE_TARGET)
  		fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2809
  	scsi_target_block(&rport->dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2810

0f29b966d   James Smart   [SCSI] FC transpo...
2811
2812
  	/* see if we need to kill io faster than waiting for device loss */
  	if ((rport->fast_io_fail_tmo != -1) &&
fff9d40ce   Mike Christie   [SCSI] fc class: ...
2813
  	    (rport->fast_io_fail_tmo < timeout))
0f29b966d   James Smart   [SCSI] FC transpo...
2814
2815
  		fc_queue_devloss_work(shost, &rport->fail_io_work,
  					rport->fast_io_fail_tmo * HZ);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2816
  	/* cap the length the devices can be blocked until they are deleted */
aedf34977   James Smart   [SCSI] FC transpo...
2817
  	fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2818
2819
2820
2821
  }
  EXPORT_SYMBOL(fc_remote_port_delete);
  
  /**
eb44820c2   Rob Landley   [SCSI] Add Docume...
2822
   * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2823
   * @rport:	The remote port that changed.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2824
   * @roles:      New roles for this port.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2825
   *
eb44820c2   Rob Landley   [SCSI] Add Docume...
2826
2827
   * Description: The LLDD calls this routine to notify the transport that the
   * roles on a remote port may have changed. The largest effect of this is
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
   * if a port now becomes a FCP Target, it must be allocated a
   * scsi target id.  If the port is no longer a FCP target, any
   * scsi target id value assigned to it will persist in case the
   * role changes back to include FCP Target. No changes in the scsi
   * midlayer will be invoked if the role changes (in the expectation
   * that the role will be resumed. If it doesn't normal error processing
   * will take place).
   *
   * Should not be called from interrupt context.
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2840
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2841
2842
2843
2844
  void
  fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
  {
  	struct Scsi_Host *shost = rport_to_shost(rport);
aedf34977   James Smart   [SCSI] FC transpo...
2845
  	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2846
2847
  	unsigned long flags;
  	int create = 0;
7525236d0   FUJITA Tomonori   [SCSI] fc_transpo...
2848
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2849

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2850
  	spin_lock_irqsave(shost->host_lock, flags);
a53eb5e06   James Smart   [SCSI] FC Transpo...
2851
  	if (roles & FC_PORT_ROLE_FCP_TARGET) {
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2852
2853
2854
  		if (rport->scsi_target_id == -1) {
  			rport->scsi_target_id = fc_host->next_target_id++;
  			create = 1;
a53eb5e06   James Smart   [SCSI] FC Transpo...
2855
  		} else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2856
  			create = 1;
7525236d0   FUJITA Tomonori   [SCSI] fc_transpo...
2857
2858
2859
2860
2861
2862
2863
  	} else if (shost->active_mode & MODE_TARGET) {
  		ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
  					     (char *)&rport->node_name);
  		if (ret)
  			printk(KERN_ERR "FC Remore Port tgt nexus failed %d
  ",
  			       ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2864
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2865

19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2866
  	rport->roles = roles;
aedf34977   James Smart   [SCSI] FC transpo...
2867
  	spin_unlock_irqrestore(shost->host_lock, flags);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
  	if (create) {
  		/*
  		 * There may have been a delete timer running on the
  		 * port. Ensure that it is cancelled as we now know
  		 * the port is an FCP Target.
  		 * Note: we know the rport is exists and in an online
  		 *  state as the LLDD would not have had an rport
  		 *  reference to pass us.
  		 *
  		 * Take no action on the del_timer failure as the state
  		 * machine state change will validate the
  		 * transaction.
  		 */
0f29b966d   James Smart   [SCSI] FC transpo...
2881
2882
  		if (!cancel_delayed_work(&rport->fail_io_work))
  			fc_flush_devloss(shost);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2883
  		if (!cancel_delayed_work(&rport->dev_loss_work))
aedf34977   James Smart   [SCSI] FC transpo...
2884
2885
2886
  			fc_flush_devloss(shost);
  
  		spin_lock_irqsave(shost->host_lock, flags);
fff9d40ce   Mike Christie   [SCSI] fc class: ...
2887
2888
  		rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
  				  FC_RPORT_DEVLOSS_PENDING);
aedf34977   James Smart   [SCSI] FC transpo...
2889
2890
2891
2892
  		spin_unlock_irqrestore(shost->host_lock, flags);
  
  		/* ensure any stgt delete functions are done */
  		fc_flush_work(shost);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2893

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2894
  		/* initiate a scan of the target */
aedf34977   James Smart   [SCSI] FC transpo...
2895
2896
  		spin_lock_irqsave(shost->host_lock, flags);
  		rport->flags |= FC_RPORT_SCAN_PENDING;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2897
  		scsi_queue_work(shost, &rport->scan_work);
aedf34977   James Smart   [SCSI] FC transpo...
2898
  		spin_unlock_irqrestore(shost->host_lock, flags);
a0785edff   James Smart   [SCSI] fc transpo...
2899
  		scsi_target_unblock(&rport->dev);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2900
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2901
2902
2903
2904
  }
  EXPORT_SYMBOL(fc_remote_port_rolechg);
  
  /**
eb44820c2   Rob Landley   [SCSI] Add Docume...
2905
   * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
92740b24c   James Smart   [SCSI] fc_transpo...
2906
   * @work:	rport target that failed to reappear in the allotted time.
eb44820c2   Rob Landley   [SCSI] Add Docume...
2907
2908
2909
2910
   *
   * Description: An attempt to delete a remote port blocks, and if it fails
   *              to return in the allotted time this gets called.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2911
  static void
c4028958b   David Howells   WorkStruct: make ...
2912
  fc_timeout_deleted_rport(struct work_struct *work)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2913
  {
c4028958b   David Howells   WorkStruct: make ...
2914
2915
  	struct fc_rport *rport =
  		container_of(work, struct fc_rport, dev_loss_work.work);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2916
  	struct Scsi_Host *shost = rport_to_shost(rport);
4be98c0ca   James Smart   [SCSI] fc transpo...
2917
  	struct fc_internal *i = to_fc_internal(shost->transportt);
aedf34977   James Smart   [SCSI] FC transpo...
2918
  	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2919
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2920

19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2921
  	spin_lock_irqsave(shost->host_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2922

aedf34977   James Smart   [SCSI] FC transpo...
2923
  	rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2924
  	/*
92740b24c   James Smart   [SCSI] fc_transpo...
2925
2926
2927
  	 * If the port is ONLINE, then it came back. If it was a SCSI
  	 * target, validate it still is. If not, tear down the
  	 * scsi_target on it.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2928
  	 */
aedf34977   James Smart   [SCSI] FC transpo...
2929
  	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
92740b24c   James Smart   [SCSI] fc_transpo...
2930
  	    (rport->scsi_target_id != -1) &&
a53eb5e06   James Smart   [SCSI] FC Transpo...
2931
  	    !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2932
  		dev_printk(KERN_ERR, &rport->dev,
aedf34977   James Smart   [SCSI] FC transpo...
2933
2934
2935
  			"blocked FC remote port time out: no longer"
  			" a FCP target, removing starget
  ");
aedf34977   James Smart   [SCSI] FC transpo...
2936
  		spin_unlock_irqrestore(shost->host_lock, flags);
a0785edff   James Smart   [SCSI] fc transpo...
2937
2938
  		scsi_target_unblock(&rport->dev);
  		fc_queue_work(shost, &rport->stgt_delete_work);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2939
2940
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2941

92740b24c   James Smart   [SCSI] fc_transpo...
2942
  	/* NOOP state - we're flushing workq's */
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2943
2944
2945
  	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
  		spin_unlock_irqrestore(shost->host_lock, flags);
  		dev_printk(KERN_ERR, &rport->dev,
92740b24c   James Smart   [SCSI] fc_transpo...
2946
2947
2948
2949
  			"blocked FC remote port time out: leaving"
  			" rport%s alone
  ",
  			(rport->scsi_target_id != -1) ?  " and starget" : "");
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2950
2951
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2952

92740b24c   James Smart   [SCSI] fc_transpo...
2953
2954
  	if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
  	    (rport->scsi_target_id == -1)) {
aedf34977   James Smart   [SCSI] FC transpo...
2955
2956
  		list_del(&rport->peers);
  		rport->port_state = FC_PORTSTATE_DELETED;
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2957
  		dev_printk(KERN_ERR, &rport->dev,
92740b24c   James Smart   [SCSI] fc_transpo...
2958
2959
2960
2961
  			"blocked FC remote port time out: removing"
  			" rport%s
  ",
  			(rport->scsi_target_id != -1) ?  " and starget" : "");
aedf34977   James Smart   [SCSI] FC transpo...
2962
2963
  		fc_queue_work(shost, &rport->rport_delete_work);
  		spin_unlock_irqrestore(shost->host_lock, flags);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2964
2965
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2966

19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2967
2968
2969
2970
  	dev_printk(KERN_ERR, &rport->dev,
  		"blocked FC remote port time out: removing target and "
  		"saving binding
  ");
aedf34977   James Smart   [SCSI] FC transpo...
2971
  	list_move_tail(&rport->peers, &fc_host->rport_bindings);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2972
2973
  
  	/*
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2974
2975
2976
  	 * Note: We do not remove or clear the hostdata area. This allows
  	 *   host-specific target data to persist along with the
  	 *   scsi_target_id. It's up to the host to manage it's hostdata area.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2977
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2978

19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2979
2980
2981
2982
2983
  	/*
  	 * Reinitialize port attributes that may change if the port comes back.
  	 */
  	rport->maxframe_size = -1;
  	rport->supported_classes = FC_COS_UNSPECIFIED;
a53eb5e06   James Smart   [SCSI] FC Transpo...
2984
  	rport->roles = FC_PORT_ROLE_UNKNOWN;
aedf34977   James Smart   [SCSI] FC transpo...
2985
  	rport->port_state = FC_PORTSTATE_NOTPRESENT;
fff9d40ce   Mike Christie   [SCSI] fc class: ...
2986
  	rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
4be98c0ca   James Smart   [SCSI] fc transpo...
2987
  	rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2988

f78badb1a   James Smart   [SCSI] fc transpo...
2989
2990
2991
2992
2993
2994
2995
2996
2997
  	/*
  	 * Pre-emptively kill I/O rather than waiting for the work queue
  	 * item to teardown the starget. (FCOE libFC folks prefer this
  	 * and to have the rport_port_id still set when it's done).
  	 */
  	spin_unlock_irqrestore(shost->host_lock, flags);
  	fc_terminate_rport_io(rport);
  
  	BUG_ON(rport->port_state != FC_PORTSTATE_NOTPRESENT);
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
2998
  	/* remove the identifiers that aren't used in the consisting binding */
aedf34977   James Smart   [SCSI] FC transpo...
2999
  	switch (fc_host->tgtid_bind_type) {
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
  	case FC_TGTID_BIND_BY_WWPN:
  		rport->node_name = -1;
  		rport->port_id = -1;
  		break;
  	case FC_TGTID_BIND_BY_WWNN:
  		rport->port_name = -1;
  		rport->port_id = -1;
  		break;
  	case FC_TGTID_BIND_BY_ID:
  		rport->node_name = -1;
  		rport->port_name = -1;
  		break;
  	case FC_TGTID_BIND_NONE:	/* to keep compiler happy */
  		break;
  	}
19a7b4aeb   James.Smart@Emulex.Com   [SCSI] update fc_...
3015
3016
3017
3018
3019
  	/*
  	 * As this only occurs if the remote port (scsi target)
  	 * went away and didn't come back - we'll remove
  	 * all attached scsi devices.
  	 */
a0785edff   James Smart   [SCSI] fc transpo...
3020
  	fc_queue_work(shost, &rport->stgt_delete_work);
4be98c0ca   James Smart   [SCSI] fc transpo...
3021
3022
3023
3024
3025
3026
3027
3028
3029
  
  	/*
  	 * Notify the driver that the rport is now dead. The LLDD will
  	 * also guarantee that any communication to the rport is terminated
  	 *
  	 * Note: we set the CALLBK_DONE flag above to correspond
  	 */
  	if (i->f->dev_loss_tmo_callbk)
  		i->f->dev_loss_tmo_callbk(rport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3030
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3031

4be98c0ca   James Smart   [SCSI] fc transpo...
3032

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3033
  /**
eb44820c2   Rob Landley   [SCSI] Add Docume...
3034
   * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
c4028958b   David Howells   WorkStruct: make ...
3035
   * @work:	rport to terminate io on.
0f29b966d   James Smart   [SCSI] FC transpo...
3036
3037
3038
   *
   * Notes: Only requests the failure of the io, not that all are flushed
   *    prior to returning.
eb44820c2   Rob Landley   [SCSI] Add Docume...
3039
   */
0f29b966d   James Smart   [SCSI] FC transpo...
3040
  static void
c4028958b   David Howells   WorkStruct: make ...
3041
  fc_timeout_fail_rport_io(struct work_struct *work)
0f29b966d   James Smart   [SCSI] FC transpo...
3042
  {
c4028958b   David Howells   WorkStruct: make ...
3043
3044
  	struct fc_rport *rport =
  		container_of(work, struct fc_rport, fail_io_work.work);
0f29b966d   James Smart   [SCSI] FC transpo...
3045
3046
3047
  
  	if (rport->port_state != FC_PORTSTATE_BLOCKED)
  		return;
fff9d40ce   Mike Christie   [SCSI] fc class: ...
3048
3049
  	rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
  	fc_terminate_rport_io(rport);
0f29b966d   James Smart   [SCSI] FC transpo...
3050
3051
3052
  }
  
  /**
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3053
   * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
c4028958b   David Howells   WorkStruct: make ...
3054
   * @work:	remote port to be scanned.
eb44820c2   Rob Landley   [SCSI] Add Docume...
3055
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3056
  static void
c4028958b   David Howells   WorkStruct: make ...
3057
  fc_scsi_scan_rport(struct work_struct *work)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3058
  {
c4028958b   David Howells   WorkStruct: make ...
3059
3060
  	struct fc_rport *rport =
  		container_of(work, struct fc_rport, scan_work);
aedf34977   James Smart   [SCSI] FC transpo...
3061
  	struct Scsi_Host *shost = rport_to_shost(rport);
03f002f77   Christof Schmitt   [SCSI] scsi_trans...
3062
  	struct fc_internal *i = to_fc_internal(shost->transportt);
42e33148d   James.Smart@Emulex.Com   [SCSI] fix for fc...
3063
  	unsigned long flags;
aedf34977   James Smart   [SCSI] FC transpo...
3064
  	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
03f002f77   Christof Schmitt   [SCSI] scsi_trans...
3065
3066
  	    (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
  	    !(i->f->disable_target_scan)) {
aedf34977   James Smart   [SCSI] FC transpo...
3067
3068
  		scsi_scan_target(&rport->dev, rport->channel,
  			rport->scsi_target_id, SCAN_WILD_CARD, 1);
42e33148d   James.Smart@Emulex.Com   [SCSI] fix for fc...
3069
  	}
aedf34977   James Smart   [SCSI] FC transpo...
3070
3071
3072
  
  	spin_lock_irqsave(shost->host_lock, flags);
  	rport->flags &= ~FC_RPORT_SCAN_PENDING;
42e33148d   James.Smart@Emulex.Com   [SCSI] fix for fc...
3073
3074
  	spin_unlock_irqrestore(shost->host_lock, flags);
  }
a53eb5e06   James Smart   [SCSI] FC Transpo...
3075
  /**
a30c3f69e   Andrew Vasquez   [SCSI] fc_transpo...
3076
   * fc_vport_setup - allocates and creates a FC virtual port.
a53eb5e06   James Smart   [SCSI] FC Transpo...
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
   * @shost:	scsi host the virtual port is connected to.
   * @channel:	Channel on shost port connected to.
   * @pdev:	parent device for vport
   * @ids:	The world wide names, FC4 port roles, etc for
   *              the virtual port.
   * @ret_vport:	The pointer to the created vport.
   *
   * Allocates and creates the vport structure, calls the parent host
   * to instantiate the vport, the completes w/ class and sysfs creation.
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
3089
   */
a53eb5e06   James Smart   [SCSI] FC Transpo...
3090
  static int
a30c3f69e   Andrew Vasquez   [SCSI] fc_transpo...
3091
  fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
a53eb5e06   James Smart   [SCSI] FC Transpo...
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
  	struct fc_vport_identifiers  *ids, struct fc_vport **ret_vport)
  {
  	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  	struct fc_internal *fci = to_fc_internal(shost->transportt);
  	struct fc_vport *vport;
  	struct device *dev;
  	unsigned long flags;
  	size_t size;
  	int error;
  
  	*ret_vport = NULL;
  
  	if ( ! fci->f->vport_create)
  		return -ENOENT;
  
  	size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
  	vport = kzalloc(size, GFP_KERNEL);
  	if (unlikely(!vport)) {
cadbd4a5e   Harvey Harrison   [SCSI] replace __...
3110
3111
  		printk(KERN_ERR "%s: allocation failure
  ", __func__);
a53eb5e06   James Smart   [SCSI] FC Transpo...
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
  		return -ENOMEM;
  	}
  
  	vport->vport_state = FC_VPORT_UNKNOWN;
  	vport->vport_last_state = FC_VPORT_UNKNOWN;
  	vport->node_name = ids->node_name;
  	vport->port_name = ids->port_name;
  	vport->roles = ids->roles;
  	vport->vport_type = ids->vport_type;
  	if (fci->f->dd_fcvport_size)
  		vport->dd_data = &vport[1];
  	vport->shost = shost;
  	vport->channel = channel;
  	vport->flags = FC_VPORT_CREATING;
9ef3e4a45   James Smart   [SCSI] fc_transpo...
3126
  	INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
a53eb5e06   James Smart   [SCSI] FC Transpo...
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
  
  	spin_lock_irqsave(shost->host_lock, flags);
  
  	if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
  		spin_unlock_irqrestore(shost->host_lock, flags);
  		kfree(vport);
  		return -ENOSPC;
  	}
  	fc_host->npiv_vports_inuse++;
  	vport->number = fc_host->next_vport_number++;
  	list_add_tail(&vport->peers, &fc_host->vports);
  	get_device(&shost->shost_gendev);	/* for fc_host->vport list */
  
  	spin_unlock_irqrestore(shost->host_lock, flags);
  
  	dev = &vport->dev;
  	device_initialize(dev);			/* takes self reference */
  	dev->parent = get_device(pdev);		/* takes parent reference */
  	dev->release = fc_vport_dev_release;
71610f55f   Kay Sievers   [SCSI] struct dev...
3146
3147
  	dev_set_name(dev, "vport-%d:%d-%d",
  		     shost->host_no, channel, vport->number);
a53eb5e06   James Smart   [SCSI] FC Transpo...
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
  	transport_setup_device(dev);
  
  	error = device_add(dev);
  	if (error) {
  		printk(KERN_ERR "FC Virtual Port device_add failed
  ");
  		goto delete_vport;
  	}
  	transport_add_device(dev);
  	transport_configure_device(dev);
  
  	error = fci->f->vport_create(vport, ids->disable);
  	if (error) {
  		printk(KERN_ERR "FC Virtual Port LLDD Create failed
  ");
  		goto delete_vport_all;
  	}
  
  	/*
  	 * if the parent isn't the physical adapter's Scsi_Host, ensure
  	 * the Scsi_Host at least contains ia symlink to the vport.
  	 */
  	if (pdev != &shost->shost_gendev) {
  		error = sysfs_create_link(&shost->shost_gendev.kobj,
71610f55f   Kay Sievers   [SCSI] struct dev...
3172
  				 &dev->kobj, dev_name(dev));
a53eb5e06   James Smart   [SCSI] FC Transpo...
3173
3174
3175
3176
3177
  		if (error)
  			printk(KERN_ERR
  				"%s: Cannot create vport symlinks for "
  				"%s, err=%d
  ",
71610f55f   Kay Sievers   [SCSI] struct dev...
3178
  				__func__, dev_name(dev), error);
a53eb5e06   James Smart   [SCSI] FC Transpo...
3179
3180
3181
3182
3183
3184
  	}
  	spin_lock_irqsave(shost->host_lock, flags);
  	vport->flags &= ~FC_VPORT_CREATING;
  	spin_unlock_irqrestore(shost->host_lock, flags);
  
  	dev_printk(KERN_NOTICE, pdev,
71610f55f   Kay Sievers   [SCSI] struct dev...
3185
3186
  			"%s created via shost%d channel %d
  ", dev_name(dev),
a53eb5e06   James Smart   [SCSI] FC Transpo...
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
  			shost->host_no, channel);
  
  	*ret_vport = vport;
  
  	return 0;
  
  delete_vport_all:
  	transport_remove_device(dev);
  	device_del(dev);
  delete_vport:
  	transport_destroy_device(dev);
  	spin_lock_irqsave(shost->host_lock, flags);
  	list_del(&vport->peers);
  	put_device(&shost->shost_gendev);	/* for fc_host->vport list */
  	fc_host->npiv_vports_inuse--;
  	spin_unlock_irqrestore(shost->host_lock, flags);
  	put_device(dev->parent);
  	kfree(vport);
  
  	return error;
  }
a30c3f69e   Andrew Vasquez   [SCSI] fc_transpo...
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
  /**
   * fc_vport_create - Admin App or LLDD requests creation of a vport
   * @shost:	scsi host the virtual port is connected to.
   * @channel:	channel on shost port connected to.
   * @ids:	The world wide names, FC4 port roles, etc for
   *              the virtual port.
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
   */
  struct fc_vport *
  fc_vport_create(struct Scsi_Host *shost, int channel,
  	struct fc_vport_identifiers *ids)
  {
  	int stat;
  	struct fc_vport *vport;
  
  	stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
  		 ids, &vport);
  	return stat ? NULL : vport;
  }
  EXPORT_SYMBOL(fc_vport_create);
a53eb5e06   James Smart   [SCSI] FC Transpo...
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
  
  /**
   * fc_vport_terminate - Admin App or LLDD requests termination of a vport
   * @vport:	fc_vport to be terminated
   *
   * Calls the LLDD vport_delete() function, then deallocates and removes
   * the vport from the shost and object tree.
   *
   * Notes:
   *	This routine assumes no locks are held on entry.
eb44820c2   Rob Landley   [SCSI] Add Docume...
3240
   */
a53eb5e06   James Smart   [SCSI] FC Transpo...
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
  int
  fc_vport_terminate(struct fc_vport *vport)
  {
  	struct Scsi_Host *shost = vport_to_shost(vport);
  	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  	struct fc_internal *i = to_fc_internal(shost->transportt);
  	struct device *dev = &vport->dev;
  	unsigned long flags;
  	int stat;
  
  	spin_lock_irqsave(shost->host_lock, flags);
  	if (vport->flags & FC_VPORT_CREATING) {
  		spin_unlock_irqrestore(shost->host_lock, flags);
  		return -EBUSY;
  	}
  	if (vport->flags & (FC_VPORT_DEL)) {
  		spin_unlock_irqrestore(shost->host_lock, flags);
  		return -EALREADY;
  	}
  	vport->flags |= FC_VPORT_DELETING;
  	spin_unlock_irqrestore(shost->host_lock, flags);
  
  	if (i->f->vport_delete)
  		stat = i->f->vport_delete(vport);
  	else
  		stat = -ENOENT;
  
  	spin_lock_irqsave(shost->host_lock, flags);
  	vport->flags &= ~FC_VPORT_DELETING;
  	if (!stat) {
  		vport->flags |= FC_VPORT_DELETED;
  		list_del(&vport->peers);
  		fc_host->npiv_vports_inuse--;
  		put_device(&shost->shost_gendev);  /* for fc_host->vport list */
  	}
  	spin_unlock_irqrestore(shost->host_lock, flags);
  
  	if (stat)
  		return stat;
  
  	if (dev->parent != &shost->shost_gendev)
71610f55f   Kay Sievers   [SCSI] struct dev...
3282
  		sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
a53eb5e06   James Smart   [SCSI] FC Transpo...
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
  	transport_remove_device(dev);
  	device_del(dev);
  	transport_destroy_device(dev);
  
  	/*
  	 * Removing our self-reference should mean our
  	 * release function gets called, which will drop the remaining
  	 * parent reference and free the data structure.
  	 */
  	put_device(dev);			/* for self-reference */
  
  	return 0; /* SUCCESS */
  }
  EXPORT_SYMBOL(fc_vport_terminate);
9ef3e4a45   James Smart   [SCSI] fc_transpo...
3297
3298
  /**
   * fc_vport_sched_delete - workq-based delete request for a vport
9ef3e4a45   James Smart   [SCSI] fc_transpo...
3299
   * @work:	vport to be deleted.
eb44820c2   Rob Landley   [SCSI] Add Docume...
3300
   */
9ef3e4a45   James Smart   [SCSI] fc_transpo...
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
  static void
  fc_vport_sched_delete(struct work_struct *work)
  {
  	struct fc_vport *vport =
  		container_of(work, struct fc_vport, vport_delete_work);
  	int stat;
  
  	stat = fc_vport_terminate(vport);
  	if (stat)
  		dev_printk(KERN_ERR, vport->dev.parent,
  			"%s: %s could not be deleted created via "
cadbd4a5e   Harvey Harrison   [SCSI] replace __...
3312
3313
  			"shost%d channel %d - error %d
  ", __func__,
71610f55f   Kay Sievers   [SCSI] struct dev...
3314
  			dev_name(&vport->dev), vport->shost->host_no,
9ef3e4a45   James Smart   [SCSI] fc_transpo...
3315
3316
  			vport->channel, stat);
  }
a53eb5e06   James Smart   [SCSI] FC Transpo...
3317

9ef3e4a45   James Smart   [SCSI] fc_transpo...
3318
3319
  /* Original Author:  Martin Hicks */
  MODULE_AUTHOR("James Smart");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3320
3321
3322
3323
3324
  MODULE_DESCRIPTION("FC Transport Attributes");
  MODULE_LICENSE("GPL");
  
  module_init(fc_transport_init);
  module_exit(fc_transport_exit);