scsi_transport_fc.c 120.4 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
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
 *
 *  ========
 *
22
 *  Copyright (C) 2004-2007   James Smart, Emulex Corporation
L
Linus Torvalds 已提交
23 24
 *    Rewrite for host, target, device, and remote port attributes,
 *    statistics, and service functions...
25
 *    Add vports, etc
L
Linus Torvalds 已提交
26 27 28 29
 *
 */
#include <linux/module.h>
#include <linux/init.h>
30
#include <linux/slab.h>
31
#include <linux/delay.h>
32
#include <linux/kernel.h>
L
Linus Torvalds 已提交
33 34 35 36
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_transport.h>
#include <scsi/scsi_transport_fc.h>
37
#include <scsi/scsi_cmnd.h>
38 39
#include <net/netlink.h>
#include <scsi/scsi_netlink_fc.h>
J
James Smart 已提交
40
#include <scsi/scsi_bsg_fc.h>
L
Linus Torvalds 已提交
41 42
#include "scsi_priv.h"

43
static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
44
static void fc_vport_sched_delete(struct work_struct *work);
45
static int fc_vport_setup(struct Scsi_Host *shost, int channel,
46 47
	struct device *pdev, struct fc_vport_identifiers  *ids,
	struct fc_vport **vport);
J
James Smart 已提交
48 49 50 51
static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
static void fc_bsg_remove(struct request_queue *);
static void fc_bsg_goose_queue(struct fc_rport *);
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/*
 * 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.
 */
static unsigned int fc_dev_loss_tmo = 60;		/* seconds */

module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
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 if"
		 " fast_io_fail_tmo is not set.");

L
Linus Torvalds 已提交
72 73 74 75
/*
 * Redefine so that we can have same named attributes in the
 * sdev/starget/host objects.
 */
76 77
#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)		\
struct device_attribute device_attr_##_prefix##_##_name = 	\
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85
	__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;						\
									\
86
	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100
		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;								\
									\
101
	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		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)" },
123
	{ FC_PORTTYPE_PTP,	"Point-To-Point (direct nport connection)" },
124
	{ FC_PORTTYPE_NPIV,		"NPIV VPORT" },
L
Linus Torvalds 已提交
125 126 127 128
};
fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
#define FC_PORTTYPE_MAX_NAMELEN		50

129 130 131
/* Reuse fc_port_type enum function for vport_type */
#define get_fc_vport_type_name get_fc_port_type_name

L
Linus Torvalds 已提交
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/* 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


L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
/* 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" },
171
	{ FC_PORTSTATE_DELETED,		"Deleted" },
L
Linus Torvalds 已提交
172 173 174 175 176
};
fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
#define FC_PORTSTATE_MAX_NAMELEN	20


177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/* 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


L
Linus Torvalds 已提交
200
/* Convert fc_tgtid_binding_type values to ascii string name */
201
static const struct {
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	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;							\
								\
226
	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234 235 236 237 238
		if (table[i].value & table_key) {		\
			len += sprintf(buf + len, "%s%s",	\
				prefix, table[i].name);		\
			prefix = ", ";				\
		}						\
	}							\
	len += sprintf(buf + len, "\n");			\
	return len;						\
}


/* Convert FC_COS bit values to ascii string name */
239
static const struct {
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252
	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 */
253
static const struct {
L
Linus Torvalds 已提交
254 255 256 257 258 259 260
	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" },
261 262
	{ FC_PORTSPEED_8GBIT,		"8 Gbit" },
	{ FC_PORTSPEED_16GBIT,		"16 Gbit" },
263
	{ FC_PORTSPEED_32GBIT,		"32 Gbit" },
264 265 266 267
	{ FC_PORTSPEED_20GBIT,		"20 Gbit" },
	{ FC_PORTSPEED_40GBIT,		"40 Gbit" },
	{ FC_PORTSPEED_50GBIT,		"50 Gbit" },
	{ FC_PORTSPEED_100GBIT,		"100 Gbit" },
268
	{ FC_PORTSPEED_25GBIT,		"25 Gbit" },
L
Linus Torvalds 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	{ 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, "\n");
	return len;
}


286
/* Convert FC_PORT_ROLE bit values to ascii string name */
287
static const struct {
L
Linus Torvalds 已提交
288 289
	u32 			value;
	char			*name;
290 291 292 293
} 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" },
L
Linus Torvalds 已提交
294
};
295
fc_bitfield_name_search(port_roles, fc_port_role_names)
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308

/*
 * 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


D
David Howells 已提交
309 310 311
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);
L
Linus Torvalds 已提交
312 313 314 315 316 317

/*
 * Attribute counts pre object type...
 * Increase these values if you add attributes
 */
#define FC_STARGET_NUM_ATTRS 	3
318
#define FC_RPORT_NUM_ATTRS	10
319
#define FC_VPORT_NUM_ATTRS	9
320
#define FC_HOST_NUM_ATTRS	29
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

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.
	 */
336
	struct device_attribute private_starget_attrs[
L
Linus Torvalds 已提交
337
							FC_STARGET_NUM_ATTRS];
338
	struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
L
Linus Torvalds 已提交
339

340 341
	struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
	struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
L
Linus Torvalds 已提交
342 343

	struct transport_container rport_attr_cont;
344 345
	struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
	struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
346 347

	struct transport_container vport_attr_cont;
348 349
	struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
	struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
L
Linus Torvalds 已提交
350 351 352 353
};

#define to_fc_internal(tmpl)	container_of(tmpl, struct fc_internal, t)

354
static int fc_target_setup(struct transport_container *tc, struct device *dev,
355
			   struct device *cdev)
L
Linus Torvalds 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
{
	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);

384
static int fc_host_setup(struct transport_container *tc, struct device *dev,
385
			 struct device *cdev)
L
Linus Torvalds 已提交
386 387
{
	struct Scsi_Host *shost = dev_to_shost(dev);
388
	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
L
Linus Torvalds 已提交
389

390
	/*
L
Linus Torvalds 已提交
391 392 393 394
	 * 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.
	 */
395 396 397 398 399 400 401 402
	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));
	fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
	fc_host->maxframe_size = -1;
403
	fc_host->max_npiv_vports = 0;
404 405
	memset(fc_host->serial_number, 0,
		sizeof(fc_host->serial_number));
406 407 408 409 410 411 412 413 414 415 416 417 418 419
	memset(fc_host->manufacturer, 0,
		sizeof(fc_host->manufacturer));
	memset(fc_host->model, 0,
		sizeof(fc_host->model));
	memset(fc_host->model_description, 0,
		sizeof(fc_host->model_description));
	memset(fc_host->hardware_version, 0,
		sizeof(fc_host->hardware_version));
	memset(fc_host->driver_version, 0,
		sizeof(fc_host->driver_version));
	memset(fc_host->firmware_version, 0,
		sizeof(fc_host->firmware_version));
	memset(fc_host->optionrom_version, 0,
		sizeof(fc_host->optionrom_version));
420 421 422 423 424 425 426 427

	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;
428 429
	memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
	memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
430 431 432 433 434

	fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;

	INIT_LIST_HEAD(&fc_host->rports);
	INIT_LIST_HEAD(&fc_host->rport_bindings);
435
	INIT_LIST_HEAD(&fc_host->vports);
436 437
	fc_host->next_rport_number = 0;
	fc_host->next_target_id = 0;
438 439
	fc_host->next_vport_number = 0;
	fc_host->npiv_vports_inuse = 0;
440

441 442
	snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
		 "fc_wq_%d", shost->host_no);
443
	fc_host->work_q = alloc_workqueue("%s", 0, 0, fc_host->work_q_name);
444 445 446
	if (!fc_host->work_q)
		return -ENOMEM;

447
	fc_host->dev_loss_tmo = fc_dev_loss_tmo;
448 449 450
	snprintf(fc_host->devloss_work_q_name,
		 sizeof(fc_host->devloss_work_q_name),
		 "fc_dl_%d", shost->host_no);
451 452
	fc_host->devloss_work_q = alloc_workqueue("%s", 0, 0,
					fc_host->devloss_work_q_name);
453 454 455 456 457 458
	if (!fc_host->devloss_work_q) {
		destroy_workqueue(fc_host->work_q);
		fc_host->work_q = NULL;
		return -ENOMEM;
	}

J
James Smart 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471
	fc_bsg_hostadd(shost, fc_host);
	/* ignore any bsg add error - we just can't do sgio */

	return 0;
}

static int fc_host_remove(struct transport_container *tc, struct device *dev,
			 struct device *cdev)
{
	struct Scsi_Host *shost = dev_to_shost(dev);
	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);

	fc_bsg_remove(fc_host->rqst_q);
L
Linus Torvalds 已提交
472 473 474 475 476 477
	return 0;
}

static DECLARE_TRANSPORT_CLASS(fc_host_class,
			       "fc_host",
			       fc_host_setup,
J
James Smart 已提交
478
			       fc_host_remove,
L
Linus Torvalds 已提交
479 480 481 482 483 484 485 486 487 488 489 490
			       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);

491 492 493 494 495 496 497 498 499 500
/*
 * 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);

501
/*
502
 * Netlink Infrastructure
503
 */
504 505 506 507 508 509 510

static atomic_t fc_event_seq;

/**
 * fc_get_event_number - Obtain the next sequential FC event number
 *
 * Notes:
511
 *   We could have inlined this, but it would have required fc_event_seq to
512 513
 *   be exposed. For now, live with the subroutine call.
 *   Atomic used to avoid lock/unlock...
514
 */
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
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.
 * @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.
532
 */
533 534 535 536 537 538 539 540
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;
541
	u32 len;
542 543 544 545 546 547 548 549 550
	int err;

	if (!scsi_nl_sock) {
		err = -ENOENT;
		goto send_fail;
	}

	len = FC_NL_MSGALIGN(sizeof(*event));

551
	skb = nlmsg_new(len, GFP_KERNEL);
552 553 554 555 556
	if (!skb) {
		err = -ENOBUFS;
		goto send_fail;
	}

557
	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
558 559 560 561
	if (!nlh) {
		err = -ENOBUFS;
		goto send_fail_skb;
	}
562
	event = nlmsg_data(nlh);
563 564 565 566 567 568 569 570 571 572 573

	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;

574 575
	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
			GFP_KERNEL);
576 577 578 579 580 581 582 583
	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\n",
584
		__func__, shost->host_no,
585 586 587 588 589 590 591
		(name) ? name : "<unknown>", event_data, err);
	return;
}
EXPORT_SYMBOL(fc_host_post_event);


/**
592
 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
593 594 595 596
 * @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
597
 * @vendor_id:          Vendor id
598 599 600
 *
 * Notes:
 *	This routine assumes no locks are held on entry.
601
 */
602 603
void
fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
604
		u32 data_len, char * data_buf, u64 vendor_id)
605 606 607 608
{
	struct sk_buff *skb;
	struct nlmsghdr	*nlh;
	struct fc_nl_event *event;
609
	u32 len;
610 611 612 613 614 615 616 617 618
	int err;

	if (!scsi_nl_sock) {
		err = -ENOENT;
		goto send_vendor_fail;
	}

	len = FC_NL_MSGALIGN(sizeof(*event) + data_len);

619
	skb = nlmsg_new(len, GFP_KERNEL);
620 621 622 623 624
	if (!skb) {
		err = -ENOBUFS;
		goto send_vendor_fail;
	}

625
	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
626 627 628 629
	if (!nlh) {
		err = -ENOBUFS;
		goto send_vendor_fail_skb;
	}
630
	event = nlmsg_data(nlh);
631 632 633 634 635 636 637 638 639 640 641

	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);

642 643
	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
			GFP_KERNEL);
644 645 646 647 648 649 650
	return;

send_vendor_fail_skb:
	kfree_skb(skb);
send_vendor_fail:
	printk(KERN_WARNING
		"%s: Dropped Event : host %d vendor_unique - err %d\n",
651
		__func__, shost->host_no, err);
652 653 654 655 656
	return;
}
EXPORT_SYMBOL(fc_host_post_vendor_event);


L
Linus Torvalds 已提交
657 658 659

static __init int fc_transport_init(void)
{
660 661 662 663 664
	int error;

	atomic_set(&fc_event_seq, 0);

	error = transport_class_register(&fc_host_class);
665 666 667
	if (error)
		return error;
	error = transport_class_register(&fc_vport_class);
L
Linus Torvalds 已提交
668
	if (error)
669
		goto unreg_host_class;
L
Linus Torvalds 已提交
670 671
	error = transport_class_register(&fc_rport_class);
	if (error)
672 673 674 675 676 677 678 679 680 681 682 683 684
		goto unreg_vport_class;
	error = transport_class_register(&fc_transport_class);
	if (error)
		goto unreg_rport_class;
	return 0;

unreg_rport_class:
	transport_class_unregister(&fc_rport_class);
unreg_vport_class:
	transport_class_unregister(&fc_vport_class);
unreg_host_class:
	transport_class_unregister(&fc_host_class);
	return error;
L
Linus Torvalds 已提交
685 686 687 688 689 690 691
}

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);
692
	transport_class_unregister(&fc_vport_class);
L
Linus Torvalds 已提交
693 694 695 696 697 698 699 700
}

/*
 * FC Remote Port Attribute Management
 */

#define fc_rport_show_function(field, format_string, sz, cast)		\
static ssize_t								\
701 702
show_fc_rport_##field (struct device *dev, 				\
		       struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
703
{									\
704
	struct fc_rport *rport = transport_class_to_rport(dev);		\
L
Linus Torvalds 已提交
705 706
	struct Scsi_Host *shost = rport_to_shost(rport);		\
	struct fc_internal *i = to_fc_internal(shost->transportt);	\
707 708
	if ((i->f->get_rport_##field) &&				\
	    !((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
709
	      (rport->port_state == FC_PORTSTATE_DELETED) ||		\
710
	      (rport->port_state == FC_PORTSTATE_NOTPRESENT)))		\
L
Linus Torvalds 已提交
711 712 713 714 715 716
		i->f->get_rport_##field(rport);				\
	return snprintf(buf, sz, format_string, cast rport->field); 	\
}

#define fc_rport_store_function(field)					\
static ssize_t								\
717 718 719
store_fc_rport_##field(struct device *dev,				\
		       struct device_attribute *attr,			\
		       const char *buf,	size_t count)			\
L
Linus Torvalds 已提交
720 721
{									\
	int val;							\
722
	struct fc_rport *rport = transport_class_to_rport(dev);		\
L
Linus Torvalds 已提交
723 724
	struct Scsi_Host *shost = rport_to_shost(rport);		\
	struct fc_internal *i = to_fc_internal(shost->transportt);	\
725
	char *cp;							\
726
	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
727
	    (rport->port_state == FC_PORTSTATE_DELETED) ||		\
728 729
	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))		\
		return -EBUSY;						\
730 731 732
	val = simple_strtoul(buf, &cp, 0);				\
	if (*cp && (*cp != '\n'))					\
		return -EINVAL;						\
L
Linus Torvalds 已提交
733 734 735 736 737 738
	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, )		\
739
static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
L
Linus Torvalds 已提交
740 741 742 743
			 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))	\
744
static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
L
Linus Torvalds 已提交
745 746 747 748 749
			  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)					\
750
static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,		\
L
Linus Torvalds 已提交
751 752 753 754 755 756
			show_fc_rport_##field,				\
			store_fc_rport_##field)


#define fc_private_rport_show_function(field, format_string, sz, cast)	\
static ssize_t								\
757 758
show_fc_rport_##field (struct device *dev, 				\
		       struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
759
{									\
760
	struct fc_rport *rport = transport_class_to_rport(dev);		\
L
Linus Torvalds 已提交
761 762 763 764 765
	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, )	\
766
static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
L
Linus Torvalds 已提交
767 768 769 770
			 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)) \
771
static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
L
Linus Torvalds 已提交
772 773 774 775 776
			  show_fc_rport_##field, NULL)


#define fc_private_rport_rd_enum_attr(title, maxlen)			\
static ssize_t								\
777 778
show_fc_rport_##title (struct device *dev,				\
		       struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
779
{									\
780
	struct fc_rport *rport = transport_class_to_rport(dev);		\
L
Linus Torvalds 已提交
781 782 783 784 785 786
	const char *name;						\
	name = get_fc_##title##_name(rport->title);			\
	if (!name)							\
		return -EINVAL;						\
	return snprintf(buf, maxlen, "%s\n", name);			\
}									\
787
static FC_DEVICE_ATTR(rport, title, S_IRUGO,			\
L
Linus Torvalds 已提交
788 789 790 791
			show_fc_rport_##title, NULL)


#define SETUP_RPORT_ATTRIBUTE_RD(field)					\
792
	i->private_rport_attrs[count] = device_attr_rport_##field; \
L
Linus Torvalds 已提交
793 794 795 796 797 798 799
	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)				\
800
	i->private_rport_attrs[count] = device_attr_rport_##field; \
L
Linus Torvalds 已提交
801 802 803 804 805 806
	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)					\
807
	i->private_rport_attrs[count] = device_attr_rport_##field; \
L
Linus Torvalds 已提交
808 809 810 811 812 813 814 815
	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++

816 817
#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field)				\
{									\
818
	i->private_rport_attrs[count] = device_attr_rport_##field; \
819 820 821 822
	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
	count++;							\
}

L
Linus Torvalds 已提交
823 824 825 826 827 828 829 830

/* The FC Transport Remote Port Attributes: */

/* Fixed Remote Port Attributes */

fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);

static ssize_t
831 832
show_fc_rport_supported_classes (struct device *dev,
				 struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
833
{
834
	struct fc_rport *rport = transport_class_to_rport(dev);
L
Linus Torvalds 已提交
835 836 837 838
	if (rport->supported_classes == FC_COS_UNSPECIFIED)
		return snprintf(buf, 20, "unspecified\n");
	return get_fc_cos_names(rport->supported_classes, buf);
}
839
static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
L
Linus Torvalds 已提交
840 841 842 843
		show_fc_rport_supported_classes, NULL);

/* Dynamic Remote Port Attributes */

844 845 846
/*
 * dev_loss_tmo attribute
 */
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
{
	char *cp;

	*val = simple_strtoul(buf, &cp, 0);
	if ((*cp && (*cp != '\n')) || (*val < 0))
		return -EINVAL;
	/*
	 * Check for overflow; dev_loss_tmo is u32
	 */
	if (*val > UINT_MAX)
		return -EINVAL;

	return 0;
}

static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
				     unsigned long val)
865 866 867
{
	struct Scsi_Host *shost = rport_to_shost(rport);
	struct fc_internal *i = to_fc_internal(shost->transportt);
868

869
	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
870
	    (rport->port_state == FC_PORTSTATE_DELETED) ||
871 872
	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
		return -EBUSY;
873 874 875 876 877 878
	/*
	 * Check for overflow; dev_loss_tmo is u32
	 */
	if (val > UINT_MAX)
		return -EINVAL;

879 880 881 882 883 884 885 886
	/*
	 * If fast_io_fail is off we have to cap
	 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
	 */
	if (rport->fast_io_fail_tmo == -1 &&
	    val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
		return -EINVAL;

887
	i->f->set_rport_dev_loss_tmo(rport, val);
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
	return 0;
}

fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
static ssize_t
store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	struct fc_rport *rport = transport_class_to_rport(dev);
	unsigned long val;
	int rc;

	rc = fc_str_to_dev_loss(buf, &val);
	if (rc)
		return rc;

	rc = fc_rport_set_dev_loss_tmo(rport, val);
	if (rc)
		return rc;
907 908
	return count;
}
909
static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
910
		show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
L
Linus Torvalds 已提交
911 912 913 914 915 916 917 918 919


/* Private Remote Port Attributes */

fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);

static ssize_t
920 921
show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
		     char *buf)
L
Linus Torvalds 已提交
922
{
923
	struct fc_rport *rport = transport_class_to_rport(dev);
L
Linus Torvalds 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

	/* 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\n");
		case FC_FABCTLR_PORTID:
			return snprintf(buf, 30, "Fabric Controller\n");
		case FC_DIRSRVR_PORTID:
			return snprintf(buf, 30, "Directory Server\n");
		case FC_TIMESRVR_PORTID:
			return snprintf(buf, 30, "Time Server\n");
		case FC_MGMTSRVR_PORTID:
			return snprintf(buf, 30, "Management Server\n");
		default:
			return snprintf(buf, 30, "Unknown Fabric Entity\n");
		}
	} else {
944
		if (rport->roles == FC_PORT_ROLE_UNKNOWN)
L
Linus Torvalds 已提交
945
			return snprintf(buf, 20, "unknown\n");
946
		return get_fc_port_roles_names(rport->roles, buf);
L
Linus Torvalds 已提交
947 948
	}
}
949
static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
L
Linus Torvalds 已提交
950 951 952 953 954
		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\n", 20);

955 956 957 958
/*
 * fast_io_fail_tmo attribute
 */
static ssize_t
959 960
show_fc_rport_fast_io_fail_tmo (struct device *dev,
				struct device_attribute *attr, char *buf)
961
{
962
	struct fc_rport *rport = transport_class_to_rport(dev);
963 964 965 966 967 968 969

	if (rport->fast_io_fail_tmo == -1)
		return snprintf(buf, 5, "off\n");
	return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
}

static ssize_t
970 971 972
store_fc_rport_fast_io_fail_tmo(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
973 974 975
{
	int val;
	char *cp;
976
	struct fc_rport *rport = transport_class_to_rport(dev);
977 978 979 980 981 982 983 984 985

	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);
986 987 988 989 990 991 992 993
		if ((*cp && (*cp != '\n')) || (val < 0))
			return -EINVAL;
		/*
		 * Cap fast_io_fail by dev_loss_tmo or
		 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
		 */
		if ((val >= rport->dev_loss_tmo) ||
		    (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
994
			return -EINVAL;
995

996 997 998 999
		rport->fast_io_fail_tmo = val;
	}
	return count;
}
1000
static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
1001
	show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
L
Linus Torvalds 已提交
1002 1003 1004 1005 1006 1007 1008 1009


/*
 * FC SCSI Target Attribute Management
 */

/*
 * Note: in the target show function we recognize when the remote
1010
 *  port is in the hierarchy and do not allow the driver to get
L
Linus Torvalds 已提交
1011 1012 1013 1014 1015
 *  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								\
1016 1017
show_fc_starget_##field (struct device *dev, 				\
			 struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
1018
{									\
1019
	struct scsi_target *starget = transport_class_to_starget(dev);	\
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	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, )		\
1033
static FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
L
Linus Torvalds 已提交
1034 1035 1036 1037
			 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))	\
1038
static FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
L
Linus Torvalds 已提交
1039 1040 1041
			  show_fc_starget_##field, NULL)

#define SETUP_STARGET_ATTRIBUTE_RD(field)				\
1042
	i->private_starget_attrs[count] = device_attr_starget_##field; \
L
Linus Torvalds 已提交
1043 1044 1045 1046 1047 1048 1049
	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)				\
1050
	i->private_starget_attrs[count] = device_attr_starget_##field; \
L
Linus Torvalds 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
	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\n", 20, unsigned long long);
fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
fc_starget_rd_attr(port_id, "0x%06x\n", 20);


1065 1066 1067 1068 1069 1070
/*
 * FC Virtual Port Attribute Management
 */

#define fc_vport_show_function(field, format_string, sz, cast)		\
static ssize_t								\
1071 1072
show_fc_vport_##field (struct device *dev, 				\
		       struct device_attribute *attr, char *buf)	\
1073
{									\
1074
	struct fc_vport *vport = transport_class_to_vport(dev);		\
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
	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								\
1085 1086 1087
store_fc_vport_##field(struct device *dev,				\
		       struct device_attribute *attr,			\
		       const char *buf,	size_t count)			\
1088 1089
{									\
	int val;							\
1090
	struct fc_vport *vport = transport_class_to_vport(dev);		\
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	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 != '\n'))					\
		return -EINVAL;						\
	i->f->set_vport_##field(vport, val);				\
	return count;							\
}

#define fc_vport_store_str_function(field, slen)			\
static ssize_t								\
1105 1106 1107
store_fc_vport_##field(struct device *dev,				\
		       struct device_attribute *attr, 			\
		       const char *buf,	size_t count)			\
1108
{									\
1109
	struct fc_vport *vport = transport_class_to_vport(dev);		\
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	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] == '\n')						\
		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, )		\
1126
static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1127 1128 1129 1130
			 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))	\
1131
static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1132 1133 1134 1135 1136
			  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)					\
1137
static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1138 1139 1140 1141 1142
			show_fc_vport_##field,				\
			store_fc_vport_##field)

#define fc_private_vport_show_function(field, format_string, sz, cast)	\
static ssize_t								\
1143 1144
show_fc_vport_##field (struct device *dev,				\
		       struct device_attribute *attr, char *buf)	\
1145
{									\
1146
	struct fc_vport *vport = transport_class_to_vport(dev);		\
1147 1148 1149 1150 1151
	return snprintf(buf, sz, format_string, cast vport->field); 	\
}

#define fc_private_vport_store_u32_function(field)			\
static ssize_t								\
1152 1153 1154
store_fc_vport_##field(struct device *dev,				\
		       struct device_attribute *attr,			\
		       const char *buf,	size_t count)			\
1155 1156
{									\
	u32 val;							\
1157
	struct fc_vport *vport = transport_class_to_vport(dev);		\
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	char *cp;							\
	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))		\
		return -EBUSY;						\
	val = simple_strtoul(buf, &cp, 0);				\
	if (*cp && (*cp != '\n'))					\
		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, )	\
1171
static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1172 1173 1174 1175
			 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)) \
1176
static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1177 1178 1179 1180 1181
			  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)			\
1182
static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1183 1184 1185 1186 1187 1188
			show_fc_vport_##field,				\
			store_fc_vport_##field)


#define fc_private_vport_rd_enum_attr(title, maxlen)			\
static ssize_t								\
1189 1190 1191
show_fc_vport_##title (struct device *dev,				\
		       struct device_attribute *attr,			\
		       char *buf)					\
1192
{									\
1193
	struct fc_vport *vport = transport_class_to_vport(dev);		\
1194 1195 1196 1197 1198 1199
	const char *name;						\
	name = get_fc_##title##_name(vport->title);			\
	if (!name)							\
		return -EINVAL;						\
	return snprintf(buf, maxlen, "%s\n", name);			\
}									\
1200
static FC_DEVICE_ATTR(vport, title, S_IRUGO,			\
1201 1202 1203 1204
			show_fc_vport_##title, NULL)


#define SETUP_VPORT_ATTRIBUTE_RD(field)					\
1205
	i->private_vport_attrs[count] = device_attr_vport_##field; \
1206 1207 1208 1209 1210 1211 1212 1213
	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)				\
1214
	i->private_vport_attrs[count] = device_attr_vport_##field; \
1215 1216 1217 1218 1219 1220
	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)					\
1221
	i->private_vport_attrs[count] = device_attr_vport_##field; \
1222 1223 1224 1225 1226 1227
	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)					\
1228
	i->private_vport_attrs[count] = device_attr_vport_##field; \
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
	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)				\
{									\
1239
	i->private_vport_attrs[count] = device_attr_vport_##field; \
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	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\n", 20, unsigned long long);
fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);

static ssize_t
1259 1260
show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
		     char *buf)
1261
{
1262
	struct fc_vport *vport = transport_class_to_vport(dev);
1263 1264 1265 1266 1267

	if (vport->roles == FC_PORT_ROLE_UNKNOWN)
		return snprintf(buf, 20, "unknown\n");
	return get_fc_port_roles_names(vport->roles, buf);
}
1268
static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1269 1270 1271 1272 1273 1274

fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);

fc_private_vport_show_function(symbolic_name, "%s\n",
		FC_VPORT_SYMBOLIC_NAMELEN + 1, )
fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1275
static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1276 1277 1278
		show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);

static ssize_t
1279 1280
store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
		      const char *buf, size_t count)
1281
{
1282
	struct fc_vport *vport = transport_class_to_vport(dev);
1283
	struct Scsi_Host *shost = vport_to_shost(vport);
1284 1285 1286 1287 1288 1289 1290 1291 1292
	unsigned long flags;

	spin_lock_irqsave(shost->host_lock, flags);
	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
		spin_unlock_irqrestore(shost->host_lock, flags);
		return -EBUSY;
	}
	vport->flags |= FC_VPORT_DELETING;
	spin_unlock_irqrestore(shost->host_lock, flags);
1293

1294
	fc_queue_work(shost, &vport->vport_delete_work);
1295 1296
	return count;
}
1297
static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1298 1299 1300 1301 1302 1303 1304 1305
			NULL, store_fc_vport_delete);


/*
 * Enable/Disable vport
 *  Write "1" to disable, write "0" to enable
 */
static ssize_t
1306 1307
store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
		       const char *buf,
1308 1309
			   size_t count)
{
1310
	struct fc_vport *vport = transport_class_to_vport(dev);
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	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;
}
1330
static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1331 1332 1333
			NULL, store_fc_vport_disable);


L
Linus Torvalds 已提交
1334 1335 1336 1337 1338 1339
/*
 * Host Attribute Management
 */

#define fc_host_show_function(field, format_string, sz, cast)		\
static ssize_t								\
1340 1341
show_fc_host_##field (struct device *dev,				\
		      struct device_attribute *attr, char *buf)		\
L
Linus Torvalds 已提交
1342
{									\
1343
	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
L
Linus Torvalds 已提交
1344 1345 1346 1347 1348 1349 1350 1351
	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								\
1352 1353 1354
store_fc_host_##field(struct device *dev, 				\
		      struct device_attribute *attr,			\
		      const char *buf,	size_t count)			\
L
Linus Torvalds 已提交
1355 1356
{									\
	int val;							\
1357
	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
L
Linus Torvalds 已提交
1358
	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1359
	char *cp;							\
L
Linus Torvalds 已提交
1360
									\
1361 1362 1363
	val = simple_strtoul(buf, &cp, 0);				\
	if (*cp && (*cp != '\n'))					\
		return -EINVAL;						\
L
Linus Torvalds 已提交
1364 1365 1366 1367
	i->f->set_host_##field(shost, val);				\
	return count;							\
}

1368 1369
#define fc_host_store_str_function(field, slen)				\
static ssize_t								\
1370 1371 1372
store_fc_host_##field(struct device *dev,				\
		      struct device_attribute *attr,			\
		      const char *buf, size_t count)			\
1373
{									\
1374
	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
	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] == '\n')						\
		cnt--;							\
	if (cnt > ((slen) - 1))						\
		return -EINVAL;						\
	memcpy(fc_host_##field(shost), buf, cnt);			\
	i->f->set_host_##field(shost);					\
	return count;							\
}

L
Linus Torvalds 已提交
1388 1389
#define fc_host_rd_attr(field, format_string, sz)			\
	fc_host_show_function(field, format_string, sz, )		\
1390
static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
L
Linus Torvalds 已提交
1391 1392 1393 1394
			 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))		\
1395
static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
L
Linus Torvalds 已提交
1396 1397 1398 1399 1400
			  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)					\
1401
static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
L
Linus Torvalds 已提交
1402 1403 1404 1405 1406
			show_fc_host_##field,				\
			store_fc_host_##field)

#define fc_host_rd_enum_attr(title, maxlen)				\
static ssize_t								\
1407 1408
show_fc_host_##title (struct device *dev,				\
		      struct device_attribute *attr, char *buf)		\
L
Linus Torvalds 已提交
1409
{									\
1410
	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
L
Linus Torvalds 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419
	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\n", name);			\
}									\
1420
static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
L
Linus Torvalds 已提交
1421 1422

#define SETUP_HOST_ATTRIBUTE_RD(field)					\
1423
	i->private_host_attrs[count] = device_attr_host_##field;	\
L
Linus Torvalds 已提交
1424 1425 1426 1427 1428 1429
	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++

1430
#define SETUP_HOST_ATTRIBUTE_RD_NS(field)				\
1431
	i->private_host_attrs[count] = device_attr_host_##field;	\
1432 1433 1434 1435 1436
	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++

L
Linus Torvalds 已提交
1437
#define SETUP_HOST_ATTRIBUTE_RW(field)					\
1438
	i->private_host_attrs[count] = device_attr_host_##field;	\
L
Linus Torvalds 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
	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								\
1450 1451
show_fc_host_##field (struct device *dev,				\
		      struct device_attribute *attr, char *buf)		\
L
Linus Torvalds 已提交
1452
{									\
1453
	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
L
Linus Torvalds 已提交
1454 1455 1456 1457 1458
	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, )	\
1459
static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
L
Linus Torvalds 已提交
1460 1461 1462 1463
			 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)) \
1464
static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
L
Linus Torvalds 已提交
1465 1466 1467
			  show_fc_host_##field, NULL)

#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
1468
	i->private_host_attrs[count] = device_attr_host_##field;	\
L
Linus Torvalds 已提交
1469 1470 1471 1472 1473 1474
	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)			\
1475
{									\
1476
	i->private_host_attrs[count] = device_attr_host_##field;	\
L
Linus Torvalds 已提交
1477
	i->host_attrs[count] = &i->private_host_attrs[count];		\
1478 1479
	count++;							\
}
L
Linus Torvalds 已提交
1480 1481 1482 1483 1484


/* Fixed Host Attributes */

static ssize_t
1485 1486
show_fc_host_supported_classes (struct device *dev,
			        struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1487
{
1488
	struct Scsi_Host *shost = transport_class_to_shost(dev);
L
Linus Torvalds 已提交
1489 1490 1491 1492 1493 1494

	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
		return snprintf(buf, 20, "unspecified\n");

	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
}
1495
static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
L
Linus Torvalds 已提交
1496 1497 1498
		show_fc_host_supported_classes, NULL);

static ssize_t
1499 1500
show_fc_host_supported_fc4s (struct device *dev,
			     struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1501
{
1502
	struct Scsi_Host *shost = transport_class_to_shost(dev);
L
Linus Torvalds 已提交
1503 1504
	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
}
1505
static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
L
Linus Torvalds 已提交
1506 1507 1508
		show_fc_host_supported_fc4s, NULL);

static ssize_t
1509 1510
show_fc_host_supported_speeds (struct device *dev,
			       struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1511
{
1512
	struct Scsi_Host *shost = transport_class_to_shost(dev);
L
Linus Torvalds 已提交
1513 1514 1515 1516 1517 1518

	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
		return snprintf(buf, 20, "unknown\n");

	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
}
1519
static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
L
Linus Torvalds 已提交
1520 1521 1522 1523 1524
		show_fc_host_supported_speeds, NULL);


fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1525 1526
fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
			     unsigned long long);
L
Linus Torvalds 已提交
1527
fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1528
fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
L
Linus Torvalds 已提交
1529
fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1530 1531 1532 1533 1534 1535 1536
fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
L
Linus Torvalds 已提交
1537 1538 1539 1540 1541


/* Dynamic Host Attributes */

static ssize_t
1542 1543
show_fc_host_active_fc4s (struct device *dev,
			  struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1544
{
1545
	struct Scsi_Host *shost = transport_class_to_shost(dev);
L
Linus Torvalds 已提交
1546 1547 1548 1549 1550 1551 1552
	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));
}
1553
static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
L
Linus Torvalds 已提交
1554 1555 1556
		show_fc_host_active_fc4s, NULL);

static ssize_t
1557 1558
show_fc_host_speed (struct device *dev,
		    struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1559
{
1560
	struct Scsi_Host *shost = transport_class_to_shost(dev);
L
Linus Torvalds 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
	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\n");

	return get_fc_port_speed_names(fc_host_speed(shost), buf);
}
1571
static FC_DEVICE_ATTR(host, speed, S_IRUGO,
L
Linus Torvalds 已提交
1572 1573 1574 1575 1576 1577 1578
		show_fc_host_speed, NULL);


fc_host_rd_attr(port_id, "0x%06x\n", 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\n", 20, unsigned long long);
1579
fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
L
Linus Torvalds 已提交
1580

1581 1582 1583
fc_private_host_show_function(system_hostname, "%s\n",
		FC_SYMBOLIC_NAME_SIZE + 1, )
fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1584
static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1585 1586
		show_fc_host_system_hostname, store_fc_host_system_hostname);

L
Linus Torvalds 已提交
1587 1588 1589 1590

/* Private Host Attributes */

static ssize_t
1591 1592
show_fc_private_host_tgtid_bind_type(struct device *dev,
				     struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1593
{
1594
	struct Scsi_Host *shost = transport_class_to_shost(dev);
L
Linus Torvalds 已提交
1595 1596 1597 1598 1599 1600 1601 1602
	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\n", name);
}

1603 1604 1605
#define get_list_head_entry(pos, head, member) 		\
	pos = list_entry((head)->next, typeof(*pos), member)

L
Linus Torvalds 已提交
1606
static ssize_t
1607 1608
store_fc_private_host_tgtid_bind_type(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
1609
{
1610
	struct Scsi_Host *shost = transport_class_to_shost(dev);
1611
	struct fc_rport *rport;
L
Linus Torvalds 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620
 	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);
1621 1622 1623
		while (!list_empty(&fc_host_rport_bindings(shost))) {
			get_list_head_entry(rport,
				&fc_host_rport_bindings(shost), peers);
1624 1625 1626
			list_del(&rport->peers);
			rport->port_state = FC_PORTSTATE_DELETED;
			fc_queue_work(shost, &rport->rport_delete_work);
1627
		}
L
Linus Torvalds 已提交
1628 1629 1630 1631 1632 1633 1634
		spin_unlock_irqrestore(shost->host_lock, flags);
	}

	fc_host_tgtid_bind_type(shost) = val;
	return count;
}

1635
static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
L
Linus Torvalds 已提交
1636 1637 1638
			show_fc_private_host_tgtid_bind_type,
			store_fc_private_host_tgtid_bind_type);

1639
static ssize_t
1640 1641
store_fc_private_host_issue_lip(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
1642
{
1643
	struct Scsi_Host *shost = transport_class_to_shost(dev);
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
	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;
}

1656
static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1657 1658
			store_fc_private_host_issue_lip);

1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
static ssize_t
store_fc_private_host_dev_loss_tmo(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count)
{
	struct Scsi_Host *shost = transport_class_to_shost(dev);
	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
	struct fc_rport *rport;
	unsigned long val, flags;
	int rc;

	rc = fc_str_to_dev_loss(buf, &val);
	if (rc)
		return rc;

	fc_host_dev_loss_tmo(shost) = val;
	spin_lock_irqsave(shost->host_lock, flags);
	list_for_each_entry(rport, &fc_host->rports, peers)
		fc_rport_set_dev_loss_tmo(rport, val);
	spin_unlock_irqrestore(shost->host_lock, flags);
	return count;
}
1681

1682 1683 1684 1685 1686 1687
fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
		      show_fc_host_dev_loss_tmo,
		      store_fc_private_host_dev_loss_tmo);

fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1688

L
Linus Torvalds 已提交
1689 1690 1691 1692 1693 1694
/*
 * Host Statistics Management
 */

/* Show a given an attribute in the statistics group */
static ssize_t
1695
fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
L
Linus Torvalds 已提交
1696
{
1697
	struct Scsi_Host *shost = transport_class_to_shost(dev);
L
Linus Torvalds 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
	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\n",
			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
	}
	return ret;
}


/* generate a read-only statistics attribute */
#define fc_host_statistic(name)						\
1718 1719 1720
static ssize_t show_fcstat_##name(struct device *cd,			\
				  struct device_attribute *attr,	\
				  char *buf)				\
L
Linus Torvalds 已提交
1721 1722 1723 1724
{									\
	return fc_stat_show(cd, buf, 					\
			    offsetof(struct fc_host_statistics, name));	\
}									\
1725
static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
L
Linus Torvalds 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746

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);
1747 1748 1749 1750 1751 1752 1753 1754 1755
fc_host_statistic(fcp_packet_alloc_failures);
fc_host_statistic(fcp_packet_aborts);
fc_host_statistic(fcp_frame_alloc_failures);
fc_host_statistic(fc_no_free_exch);
fc_host_statistic(fc_no_free_exch_xid);
fc_host_statistic(fc_xid_not_found);
fc_host_statistic(fc_xid_busy);
fc_host_statistic(fc_seq_not_found);
fc_host_statistic(fc_non_bls_resp);
L
Linus Torvalds 已提交
1756 1757

static ssize_t
1758 1759
fc_reset_statistics(struct device *dev, struct device_attribute *attr,
		    const char *buf, size_t count)
L
Linus Torvalds 已提交
1760
{
1761
	struct Scsi_Host *shost = transport_class_to_shost(dev);
L
Linus Torvalds 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
	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;
}
1772
static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
L
Linus Torvalds 已提交
1773 1774 1775
				fc_reset_statistics);

static struct attribute *fc_statistics_attrs[] = {
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
	&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,
1796 1797 1798 1799 1800 1801 1802 1803 1804
	&device_attr_host_fcp_packet_alloc_failures.attr,
	&device_attr_host_fcp_packet_aborts.attr,
	&device_attr_host_fcp_frame_alloc_failures.attr,
	&device_attr_host_fc_no_free_exch.attr,
	&device_attr_host_fc_no_free_exch_xid.attr,
	&device_attr_host_fc_xid_not_found.attr,
	&device_attr_host_fc_xid_busy.attr,
	&device_attr_host_fc_seq_not_found.attr,
	&device_attr_host_fc_non_bls_resp.attr,
1805
	&device_attr_host_reset_statistics.attr,
L
Linus Torvalds 已提交
1806 1807 1808 1809 1810 1811 1812 1813
	NULL
};

static struct attribute_group fc_statistics_group = {
	.name = "statistics",
	.attrs = fc_statistics_attrs,
};

1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826

/* 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++) {
1827 1828 1829 1830 1831
		int value;

		value = hex_to_bin(*ns++);
		if (value >= 0)
			j = (j << 4) | value;
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
		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
1853 1854
store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
1855
{
1856
	struct Scsi_Host *shost = transport_class_to_shost(dev);
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
	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] == '\n')
		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 !!! */
1886
	stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1887 1888
	return stat ? stat : count;
}
1889
static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
			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
1900 1901
store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
1902
{
1903
	struct Scsi_Host *shost = transport_class_to_shost(dev);
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
	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] == '\n')
		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)) {
1933 1934 1935
			if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
				break;
			vport->flags |= FC_VPORT_DELETING;
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
			match = 1;
			break;
		}
	}
	spin_unlock_irqrestore(shost->host_lock, flags);

	if (!match)
		return -ENODEV;

	stat = fc_vport_terminate(vport);
	return stat ? stat : count;
}
1948
static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1949 1950 1951
			store_fc_host_vport_delete);


L
Linus Torvalds 已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 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 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
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;
}

2022

2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
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;
}


2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
/**
 * fc_timed_out - FC Transport I/O timeout intercept handler
 * @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.
2078
 */
J
Jens Axboe 已提交
2079
static enum blk_eh_timer_return
2080 2081 2082 2083 2084
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)
J
Jens Axboe 已提交
2085
		return BLK_EH_RESET_TIMER;
2086

J
Jens Axboe 已提交
2087
	return BLK_EH_NOT_HANDLED;
2088 2089
}

2090
/*
2091 2092 2093
 * 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.
2094
 */
2095
static void
H
Hannes Reinecke 已提交
2096
fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2097 2098
{
	struct fc_rport *rport;
2099 2100 2101
	unsigned long flags;

	spin_lock_irqsave(shost->host_lock, flags);
2102

2103 2104 2105
	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
		if (rport->scsi_target_id == -1)
			continue;
2106

2107 2108 2109
		if (rport->port_state != FC_PORTSTATE_ONLINE)
			continue;

2110 2111 2112 2113 2114
		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;
2115 2116 2117
		}
	}

2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
	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
H
Hannes Reinecke 已提交
2128
fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
{
	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);

2158
	return 0;
2159 2160
}

2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
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);
}

L
Linus Torvalds 已提交
2174 2175 2176 2177
struct scsi_transport_template *
fc_attach_transport(struct fc_function_template *ft)
{
	int count;
J
Jes Sorensen 已提交
2178 2179
	struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
					GFP_KERNEL);
L
Linus Torvalds 已提交
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202

	if (unlikely(!i))
		return NULL;

	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);

2203 2204 2205 2206 2207
	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);

L
Linus Torvalds 已提交
2208 2209 2210 2211
	i->f = ft;

	/* Transport uses the shost workq for scsi scanning */
	i->t.create_work_queue = 1;
2212

2213 2214
	i->t.eh_timed_out = fc_timed_out;

2215
	i->t.user_scan = fc_user_scan;
2216

2217 2218 2219 2220
	/* target-mode drivers' functions */
	i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
	i->t.it_nexus_response = fc_it_nexus_response;

L
Linus Torvalds 已提交
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
	/*
	 * 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);
2240
	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
L
Linus Torvalds 已提交
2241 2242 2243 2244
	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2245 2246 2247 2248
	if (ft->vport_create) {
		SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
		SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
	}
L
Linus Torvalds 已提交
2249
	SETUP_HOST_ATTRIBUTE_RD(serial_number);
2250 2251 2252 2253 2254 2255 2256
	SETUP_HOST_ATTRIBUTE_RD(manufacturer);
	SETUP_HOST_ATTRIBUTE_RD(model);
	SETUP_HOST_ATTRIBUTE_RD(model_description);
	SETUP_HOST_ATTRIBUTE_RD(hardware_version);
	SETUP_HOST_ATTRIBUTE_RD(driver_version);
	SETUP_HOST_ATTRIBUTE_RD(firmware_version);
	SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
L
Linus Torvalds 已提交
2257 2258 2259 2260 2261 2262 2263

	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);
2264
	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2265
	SETUP_HOST_ATTRIBUTE_RW(system_hostname);
L
Linus Torvalds 已提交
2266 2267

	/* Transport-managed attributes */
2268
	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
L
Linus Torvalds 已提交
2269
	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2270 2271
	if (ft->issue_fc_host_lip)
		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2272 2273 2274 2275
	if (ft->vport_create)
		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
	if (ft->vport_delete)
		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
L
Linus Torvalds 已提交
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293

	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);
2294
	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
L
Linus Torvalds 已提交
2295 2296 2297 2298 2299

	BUG_ON(count > FC_RPORT_NUM_ATTRS);

	i->rport_attrs[count] = NULL;

2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
	/*
	 * 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;

L
Linus Torvalds 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
	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);
2329
	transport_container_unregister(&i->vport_attr_cont);
L
Linus Torvalds 已提交
2330 2331 2332 2333 2334

	kfree(i);
}
EXPORT_SYMBOL(fc_release_transport);

2335 2336 2337 2338 2339 2340
/**
 * 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:
2341 2342 2343
 * 	1 - work queued for execution
 *	0 - work is already queued
 *	-EINVAL - work queue doesn't exist
2344
 */
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
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.\n", 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.
2363
 */
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
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.\n", 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:
2385
 * 	1 on success / 0 already queued / < 0 for error
2386
 */
2387
static int
D
David Howells 已提交
2388
fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
				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.\n", 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.
2406
 */
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
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.\n", shost->hostt->name);
		dump_stack();
		return;
	}

	flush_workqueue(fc_host_devloss_work_q(shost));
}

L
Linus Torvalds 已提交
2421 2422

/**
2423 2424
 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
 * @shost:	Which &Scsi_Host
L
Linus Torvalds 已提交
2425
 *
L
Lucas De Marchi 已提交
2426
 * This routine is expected to be called immediately preceding the
L
Linus Torvalds 已提交
2427 2428 2429
 * a driver's call to scsi_remove_host().
 *
 * WARNING: A driver utilizing the fc_transport, which fails to call
2430
 *   this routine prior to scsi_remove_host(), will leave dangling
L
Linus Torvalds 已提交
2431 2432 2433 2434 2435
 *   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.
2436
 */
L
Linus Torvalds 已提交
2437 2438 2439
void
fc_remove_host(struct Scsi_Host *shost)
{
2440 2441
	struct fc_vport *vport = NULL, *next_vport = NULL;
	struct fc_rport *rport = NULL, *next_rport = NULL;
2442 2443
	struct workqueue_struct *work_q;
	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2444 2445 2446 2447 2448
	unsigned long flags;

	spin_lock_irqsave(shost->host_lock, flags);

	/* Remove any vports */
2449 2450
	list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
		fc_queue_work(shost, &vport->vport_delete_work);
L
Linus Torvalds 已提交
2451 2452 2453

	/* Remove any remote ports */
	list_for_each_entry_safe(rport, next_rport,
2454 2455 2456 2457 2458 2459
			&fc_host->rports, peers) {
		list_del(&rport->peers);
		rport->port_state = FC_PORTSTATE_DELETED;
		fc_queue_work(shost, &rport->rport_delete_work);
	}

L
Linus Torvalds 已提交
2460
	list_for_each_entry_safe(rport, next_rport,
2461 2462 2463 2464 2465 2466
			&fc_host->rport_bindings, peers) {
		list_del(&rport->peers);
		rport->port_state = FC_PORTSTATE_DELETED;
		fc_queue_work(shost, &rport->rport_delete_work);
	}

2467 2468
	spin_unlock_irqrestore(shost->host_lock, flags);

2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
	/* 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);
	}
L
Linus Torvalds 已提交
2485 2486 2487
}
EXPORT_SYMBOL(fc_remove_host);

2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
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);

	/*
2498
	 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
2499
	 */
2500
	scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
2501
}
2502 2503

/**
L
Lucas De Marchi 已提交
2504
 * fc_starget_delete - called to delete the scsi descendants of an rport
D
David Howells 已提交
2505
 * @work:	remote port to be operated on.
2506 2507 2508
 *
 * Deletes target and all sdevs.
 */
2509
static void
D
David Howells 已提交
2510
fc_starget_delete(struct work_struct *work)
2511
{
D
David Howells 已提交
2512 2513
	struct fc_rport *rport =
		container_of(work, struct fc_rport, stgt_delete_work);
2514

2515
	fc_terminate_rport_io(rport);
2516 2517 2518
	scsi_remove_target(&rport->dev);
}

2519 2520 2521

/**
 * fc_rport_final_delete - finish rport termination and delete it.
D
David Howells 已提交
2522
 * @work:	remote port to be deleted.
2523
 */
2524
static void
D
David Howells 已提交
2525
fc_rport_final_delete(struct work_struct *work)
2526
{
D
David Howells 已提交
2527 2528
	struct fc_rport *rport =
		container_of(work, struct fc_rport, rport_delete_work);
2529 2530
	struct device *dev = &rport->dev;
	struct Scsi_Host *shost = rport_to_shost(rport);
2531
	struct fc_internal *i = to_fc_internal(shost->transportt);
2532
	unsigned long flags;
2533
	int do_callback = 0;
2534

2535 2536
	fc_terminate_rport_io(rport);

2537
	/*
2538
	 * if a scan is pending, flush the SCSI Host work_q so that
2539 2540 2541 2542 2543
	 * that we can reclaim the rport scan work element.
	 */
	if (rport->flags & FC_RPORT_SCAN_PENDING)
		scsi_flush_work(shost);

2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
	/*
	 * 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);
2556
		cancel_work_sync(&rport->scan_work);
2557 2558 2559 2560 2561
		spin_lock_irqsave(shost->host_lock, flags);
		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
	}
	spin_unlock_irqrestore(shost->host_lock, flags);

2562 2563
	/* Delete SCSI target and sdevs */
	if (rport->scsi_target_id != -1)
D
David Howells 已提交
2564
		fc_starget_delete(&rport->stgt_delete_work);
2565 2566 2567 2568

	/*
	 * Notify the driver that the rport is now dead. The LLDD will
	 * also guarantee that any communication to the rport is terminated
2569 2570 2571
	 *
	 * Avoid this call if we already called it when we preserved the
	 * rport for the binding.
2572
	 */
2573
	spin_lock_irqsave(shost->host_lock, flags);
2574
	if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
2575 2576 2577 2578 2579 2580 2581
	    (i->f->dev_loss_tmo_callbk)) {
		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
		do_callback = 1;
	}
	spin_unlock_irqrestore(shost->host_lock, flags);

	if (do_callback)
2582 2583
		i->f->dev_loss_tmo_callbk(rport);

J
James Smart 已提交
2584 2585
	fc_bsg_remove(rport->rqst_q);

2586 2587 2588
	transport_remove_device(dev);
	device_del(dev);
	transport_destroy_device(dev);
2589
	scsi_host_put(shost);			/* for fc_host->rport list */
2590
	put_device(dev);			/* for self-reference */
2591 2592 2593
}


L
Linus Torvalds 已提交
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
/**
 * 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.
2606
 */
A
Adrian Bunk 已提交
2607
static struct fc_rport *
L
Linus Torvalds 已提交
2608 2609 2610
fc_rport_create(struct Scsi_Host *shost, int channel,
	struct fc_rport_identifiers  *ids)
{
2611
	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
L
Linus Torvalds 已提交
2612 2613 2614 2615 2616 2617 2618 2619
	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);
J
Jes Sorensen 已提交
2620
	rport = kzalloc(size, GFP_KERNEL);
L
Linus Torvalds 已提交
2621
	if (unlikely(!rport)) {
2622
		printk(KERN_ERR "%s: allocation failure\n", __func__);
L
Linus Torvalds 已提交
2623 2624 2625 2626 2627
		return NULL;
	}

	rport->maxframe_size = -1;
	rport->supported_classes = FC_COS_UNSPECIFIED;
2628
	rport->dev_loss_tmo = fc_host->dev_loss_tmo;
L
Linus Torvalds 已提交
2629 2630 2631 2632 2633 2634 2635 2636
	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;
2637
	rport->fast_io_fail_tmo = -1;
L
Linus Torvalds 已提交
2638

D
David Howells 已提交
2639 2640 2641 2642 2643
	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);
L
Linus Torvalds 已提交
2644 2645 2646 2647

	spin_lock_irqsave(shost->host_lock, flags);

	rport->number = fc_host->next_rport_number++;
2648
	if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
L
Linus Torvalds 已提交
2649 2650 2651
		rport->scsi_target_id = fc_host->next_target_id++;
	else
		rport->scsi_target_id = -1;
2652
	list_add_tail(&rport->peers, &fc_host->rports);
2653
	scsi_host_get(shost);			/* for fc_host->rport list */
L
Linus Torvalds 已提交
2654 2655 2656 2657

	spin_unlock_irqrestore(shost->host_lock, flags);

	dev = &rport->dev;
2658 2659
	device_initialize(dev);			/* takes self reference */
	dev->parent = get_device(&shost->shost_gendev); /* parent reference */
L
Linus Torvalds 已提交
2660
	dev->release = fc_rport_dev_release;
2661 2662
	dev_set_name(dev, "rport-%d:%d-%d",
		     shost->host_no, channel, rport->number);
L
Linus Torvalds 已提交
2663 2664 2665 2666 2667 2668 2669 2670 2671 2672
	transport_setup_device(dev);

	error = device_add(dev);
	if (error) {
		printk(KERN_ERR "FC Remote Port device_add failed\n");
		goto delete_rport;
	}
	transport_add_device(dev);
	transport_configure_device(dev);

J
James Smart 已提交
2673 2674 2675
	fc_bsg_rportadd(shost, rport);
	/* ignore any bsg add error - we just can't do sgio */

2676
	if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
L
Linus Torvalds 已提交
2677
		/* initiate a scan of the target */
2678
		rport->flags |= FC_RPORT_SCAN_PENDING;
L
Linus Torvalds 已提交
2679
		scsi_queue_work(shost, &rport->scan_work);
2680
	}
L
Linus Torvalds 已提交
2681 2682 2683 2684 2685 2686 2687

	return rport;

delete_rport:
	transport_destroy_device(dev);
	spin_lock_irqsave(shost->host_lock, flags);
	list_del(&rport->peers);
2688
	scsi_host_put(shost);			/* for fc_host->rport list */
L
Linus Torvalds 已提交
2689 2690 2691 2692 2693 2694 2695
	spin_unlock_irqrestore(shost->host_lock, flags);
	put_device(dev->parent);
	kfree(rport);
	return NULL;
}

/**
2696
 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
L
Linus Torvalds 已提交
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731
 * @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.
2732
 */
L
Linus Torvalds 已提交
2733 2734 2735 2736
struct fc_rport *
fc_remote_port_add(struct Scsi_Host *shost, int channel,
	struct fc_rport_identifiers  *ids)
{
2737
	struct fc_internal *fci = to_fc_internal(shost->transportt);
2738
	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
L
Linus Torvalds 已提交
2739 2740 2741 2742
	struct fc_rport *rport;
	unsigned long flags;
	int match = 0;

2743 2744 2745
	/* ensure any stgt delete functions are done */
	fc_flush_work(shost);

2746 2747 2748 2749 2750 2751 2752
	/*
	 * 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);

2753
	list_for_each_entry(rport, &fc_host->rports, peers) {
2754 2755 2756 2757

		if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
			(rport->channel == channel)) {

2758
			switch (fc_host->tgtid_bind_type) {
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
			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) {

				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);

				/*
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
				 * 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.
2805 2806
				 */

2807 2808
				/* was a target, not in roles */
				if ((rport->scsi_target_id != -1) &&
2809
				    (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
2810
					return rport;
2811 2812

				/*
2813 2814 2815
				 * Stop the fail io and dev_loss timers.
				 * If they flush, the port_state will
				 * be checked and will NOOP the function.
2816
				 */
2817 2818
				if (!cancel_delayed_work(&rport->fail_io_work))
					fc_flush_devloss(shost);
2819
				if (!cancel_delayed_work(&rport->dev_loss_work))
2820 2821 2822 2823
					fc_flush_devloss(shost);

				spin_lock_irqsave(shost->host_lock, flags);

2824
				rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2825 2826
						  FC_RPORT_DEVLOSS_PENDING |
						  FC_RPORT_DEVLOSS_CALLBK_DONE);
2827

2828 2829
				spin_unlock_irqrestore(shost->host_lock, flags);

2830 2831
				/* if target, initiate a scan */
				if (rport->scsi_target_id != -1) {
2832 2833
					scsi_target_unblock(&rport->dev,
							    SDEV_RUNNING);
2834 2835
					spin_lock_irqsave(shost->host_lock,
							  flags);
2836 2837 2838 2839 2840
					rport->flags |= FC_RPORT_SCAN_PENDING;
					scsi_queue_work(shost,
							&rport->scan_work);
					spin_unlock_irqrestore(shost->host_lock,
							flags);
2841
				}
2842

J
James Smart 已提交
2843 2844
				fc_bsg_goose_queue(rport);

2845 2846 2847 2848 2849
				return rport;
			}
		}
	}

2850 2851 2852 2853
	/*
	 * Search the bindings array
	 * Note: if never a FCP target, you won't be on this list
	 */
2854
	if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
L
Linus Torvalds 已提交
2855 2856 2857

		/* search for a matching consistent binding */

2858
		list_for_each_entry(rport, &fc_host->rport_bindings,
L
Linus Torvalds 已提交
2859 2860 2861 2862
					peers) {
			if (rport->channel != channel)
				continue;

2863
			switch (fc_host->tgtid_bind_type) {
L
Linus Torvalds 已提交
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880
			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) {
2881
				list_move_tail(&rport->peers, &fc_host->rports);
L
Linus Torvalds 已提交
2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893
				break;
			}
		}

		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;
2894
			rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
L
Linus Torvalds 已提交
2895

2896 2897 2898
			if (fci->f->dd_fcrport_size)
				memset(rport->dd_data, 0,
						fci->f->dd_fcrport_size);
2899 2900 2901
			spin_unlock_irqrestore(shost->host_lock, flags);

			if (ids->roles & FC_PORT_ROLE_FCP_TARGET) {
2902
				scsi_target_unblock(&rport->dev, SDEV_RUNNING);
2903

L
Linus Torvalds 已提交
2904
				/* initiate a scan of the target */
2905
				spin_lock_irqsave(shost->host_lock, flags);
2906
				rport->flags |= FC_RPORT_SCAN_PENDING;
L
Linus Torvalds 已提交
2907
				scsi_queue_work(shost, &rport->scan_work);
2908
				spin_unlock_irqrestore(shost->host_lock, flags);
2909
			}
L
Linus Torvalds 已提交
2910 2911 2912 2913
			return rport;
		}
	}

2914 2915
	spin_unlock_irqrestore(shost->host_lock, flags);

L
Linus Torvalds 已提交
2916 2917 2918 2919 2920 2921 2922 2923 2924
	/* No consistent binding found - create new remote port entry */
	rport = fc_rport_create(shost, channel, ids);

	return rport;
}
EXPORT_SYMBOL(fc_remote_port_add);


/**
2925
 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
L
Linus Torvalds 已提交
2926 2927 2928 2929 2930
 * @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
2931
 * ports displayed by the fc_host. We do this under 2 conditions:
2932
 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2933 2934 2935
 *   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.
2936
 * 2) If the port was a scsi target and disappears for longer than we
2937 2938 2939 2940 2941
 *   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.
L
Linus Torvalds 已提交
2942 2943 2944 2945
 *
 * If the remote port is not an FCP Target, it will be fully torn down
 * and deallocated, including the fc_remote_port class device.
 *
2946 2947 2948 2949
 * 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
2950 2951
 * is then expected.
 *
2952 2953 2954 2955 2956 2957 2958 2959 2960
 *   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).
L
Linus Torvalds 已提交
2961
 *
2962 2963 2964 2965 2966 2967 2968 2969
 *   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.
L
Linus Torvalds 已提交
2970 2971 2972
 *
 * Notes:
 *	This routine assumes no locks are held on entry.
2973
 */
L
Linus Torvalds 已提交
2974 2975 2976
void
fc_remote_port_delete(struct fc_rport  *rport)
{
2977
	struct Scsi_Host *shost = rport_to_shost(rport);
2978
	unsigned long timeout = rport->dev_loss_tmo;
2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989
	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);
L
Linus Torvalds 已提交
2990

2991
	if (rport->port_state != FC_PORTSTATE_ONLINE) {
2992
		spin_unlock_irqrestore(shost->host_lock, flags);
L
Linus Torvalds 已提交
2993 2994 2995
		return;
	}

2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
	/*
	 * 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.
	 */

3009 3010 3011 3012 3013 3014
	rport->port_state = FC_PORTSTATE_BLOCKED;

	rport->flags |= FC_RPORT_DEVLOSS_PENDING;

	spin_unlock_irqrestore(shost->host_lock, flags);

3015
	scsi_target_block(&rport->dev);
L
Linus Torvalds 已提交
3016

3017 3018
	/* see if we need to kill io faster than waiting for device loss */
	if ((rport->fast_io_fail_tmo != -1) &&
3019
	    (rport->fast_io_fail_tmo < timeout))
3020 3021 3022
		fc_queue_devloss_work(shost, &rport->fail_io_work,
					rport->fast_io_fail_tmo * HZ);

3023
	/* cap the length the devices can be blocked until they are deleted */
3024
	fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
L
Linus Torvalds 已提交
3025 3026 3027 3028
}
EXPORT_SYMBOL(fc_remote_port_delete);

/**
3029
 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
L
Linus Torvalds 已提交
3030
 * @rport:	The remote port that changed.
3031
 * @roles:      New roles for this port.
L
Linus Torvalds 已提交
3032
 *
3033 3034
 * 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
L
Linus Torvalds 已提交
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
 * 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.
3047
 */
L
Linus Torvalds 已提交
3048 3049 3050 3051
void
fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
{
	struct Scsi_Host *shost = rport_to_shost(rport);
3052
	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
L
Linus Torvalds 已提交
3053 3054 3055 3056
	unsigned long flags;
	int create = 0;

	spin_lock_irqsave(shost->host_lock, flags);
3057
	if (roles & FC_PORT_ROLE_FCP_TARGET) {
3058 3059 3060
		if (rport->scsi_target_id == -1) {
			rport->scsi_target_id = fc_host->next_target_id++;
			create = 1;
3061
		} else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
3062
			create = 1;
L
Linus Torvalds 已提交
3063 3064
	}

3065 3066
	rport->roles = roles;

3067 3068
	spin_unlock_irqrestore(shost->host_lock, flags);

3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
	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.
		 */
3082 3083
		if (!cancel_delayed_work(&rport->fail_io_work))
			fc_flush_devloss(shost);
3084
		if (!cancel_delayed_work(&rport->dev_loss_work))
3085 3086 3087
			fc_flush_devloss(shost);

		spin_lock_irqsave(shost->host_lock, flags);
3088
		rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
3089 3090
				  FC_RPORT_DEVLOSS_PENDING |
				  FC_RPORT_DEVLOSS_CALLBK_DONE);
3091 3092 3093 3094
		spin_unlock_irqrestore(shost->host_lock, flags);

		/* ensure any stgt delete functions are done */
		fc_flush_work(shost);
3095

3096
		scsi_target_unblock(&rport->dev, SDEV_RUNNING);
L
Linus Torvalds 已提交
3097
		/* initiate a scan of the target */
3098 3099
		spin_lock_irqsave(shost->host_lock, flags);
		rport->flags |= FC_RPORT_SCAN_PENDING;
L
Linus Torvalds 已提交
3100
		scsi_queue_work(shost, &rport->scan_work);
3101
		spin_unlock_irqrestore(shost->host_lock, flags);
3102
	}
L
Linus Torvalds 已提交
3103 3104 3105 3106
}
EXPORT_SYMBOL(fc_remote_port_rolechg);

/**
3107
 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
3108
 * @work:	rport target that failed to reappear in the allotted time.
3109 3110 3111 3112
 *
 * Description: An attempt to delete a remote port blocks, and if it fails
 *              to return in the allotted time this gets called.
 */
L
Linus Torvalds 已提交
3113
static void
D
David Howells 已提交
3114
fc_timeout_deleted_rport(struct work_struct *work)
L
Linus Torvalds 已提交
3115
{
D
David Howells 已提交
3116 3117
	struct fc_rport *rport =
		container_of(work, struct fc_rport, dev_loss_work.work);
3118
	struct Scsi_Host *shost = rport_to_shost(rport);
3119
	struct fc_internal *i = to_fc_internal(shost->transportt);
3120
	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3121
	unsigned long flags;
3122
	int do_callback = 0;
L
Linus Torvalds 已提交
3123

3124
	spin_lock_irqsave(shost->host_lock, flags);
L
Linus Torvalds 已提交
3125

3126 3127
	rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;

L
Linus Torvalds 已提交
3128
	/*
3129 3130 3131
	 * 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.
L
Linus Torvalds 已提交
3132
	 */
3133
	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3134
	    (rport->scsi_target_id != -1) &&
3135
	    !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
3136
		dev_printk(KERN_ERR, &rport->dev,
3137 3138 3139
			"blocked FC remote port time out: no longer"
			" a FCP target, removing starget\n");
		spin_unlock_irqrestore(shost->host_lock, flags);
3140
		scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
3141
		fc_queue_work(shost, &rport->stgt_delete_work);
3142 3143
		return;
	}
L
Linus Torvalds 已提交
3144

3145
	/* NOOP state - we're flushing workq's */
3146 3147 3148
	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
		spin_unlock_irqrestore(shost->host_lock, flags);
		dev_printk(KERN_ERR, &rport->dev,
3149 3150 3151
			"blocked FC remote port time out: leaving"
			" rport%s alone\n",
			(rport->scsi_target_id != -1) ?  " and starget" : "");
3152 3153
		return;
	}
L
Linus Torvalds 已提交
3154

3155 3156
	if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
	    (rport->scsi_target_id == -1)) {
3157 3158
		list_del(&rport->peers);
		rport->port_state = FC_PORTSTATE_DELETED;
3159
		dev_printk(KERN_ERR, &rport->dev,
3160 3161 3162
			"blocked FC remote port time out: removing"
			" rport%s\n",
			(rport->scsi_target_id != -1) ?  " and starget" : "");
3163 3164
		fc_queue_work(shost, &rport->rport_delete_work);
		spin_unlock_irqrestore(shost->host_lock, flags);
3165 3166
		return;
	}
L
Linus Torvalds 已提交
3167

3168 3169 3170 3171
	dev_printk(KERN_ERR, &rport->dev,
		"blocked FC remote port time out: removing target and "
		"saving binding\n");

3172
	list_move_tail(&rport->peers, &fc_host->rport_bindings);
L
Linus Torvalds 已提交
3173 3174

	/*
3175 3176 3177
	 * 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.
L
Linus Torvalds 已提交
3178 3179
	 */

3180 3181 3182 3183 3184
	/*
	 * Reinitialize port attributes that may change if the port comes back.
	 */
	rport->maxframe_size = -1;
	rport->supported_classes = FC_COS_UNSPECIFIED;
3185
	rport->roles = FC_PORT_ROLE_UNKNOWN;
3186
	rport->port_state = FC_PORTSTATE_NOTPRESENT;
3187
	rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
L
Linus Torvalds 已提交
3188

3189 3190 3191 3192 3193 3194 3195 3196
	/*
	 * 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);

3197 3198 3199
	spin_lock_irqsave(shost->host_lock, flags);

	if (rport->port_state == FC_PORTSTATE_NOTPRESENT) {	/* still missing */
3200

3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
		/* remove the identifiers that aren't used in the consisting binding */
		switch (fc_host->tgtid_bind_type) {
		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;
		}

		/*
		 * As this only occurs if the remote port (scsi target)
		 * went away and didn't come back - we'll remove
		 * all attached scsi devices.
		 */
		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
		fc_queue_work(shost, &rport->stgt_delete_work);

		do_callback = 1;
3228 3229
	}

3230
	spin_unlock_irqrestore(shost->host_lock, flags);
3231 3232 3233 3234 3235 3236 3237

	/*
	 * 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
	 */
3238
	if (do_callback && i->f->dev_loss_tmo_callbk)
3239
		i->f->dev_loss_tmo_callbk(rport);
L
Linus Torvalds 已提交
3240 3241
}

3242

3243
/**
3244
 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
D
David Howells 已提交
3245
 * @work:	rport to terminate io on.
3246 3247 3248
 *
 * Notes: Only requests the failure of the io, not that all are flushed
 *    prior to returning.
3249
 */
3250
static void
D
David Howells 已提交
3251
fc_timeout_fail_rport_io(struct work_struct *work)
3252
{
D
David Howells 已提交
3253 3254
	struct fc_rport *rport =
		container_of(work, struct fc_rport, fail_io_work.work);
3255 3256 3257 3258

	if (rport->port_state != FC_PORTSTATE_BLOCKED)
		return;

3259 3260
	rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
	fc_terminate_rport_io(rport);
3261 3262
}

L
Linus Torvalds 已提交
3263 3264
/**
 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
D
David Howells 已提交
3265
 * @work:	remote port to be scanned.
3266
 */
L
Linus Torvalds 已提交
3267
static void
D
David Howells 已提交
3268
fc_scsi_scan_rport(struct work_struct *work)
L
Linus Torvalds 已提交
3269
{
D
David Howells 已提交
3270 3271
	struct fc_rport *rport =
		container_of(work, struct fc_rport, scan_work);
3272
	struct Scsi_Host *shost = rport_to_shost(rport);
3273
	struct fc_internal *i = to_fc_internal(shost->transportt);
3274 3275
	unsigned long flags;

3276
	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3277 3278
	    (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
	    !(i->f->disable_target_scan)) {
3279 3280
		scsi_scan_target(&rport->dev, rport->channel,
			rport->scsi_target_id, SCAN_WILD_CARD, 1);
3281
	}
3282 3283 3284

	spin_lock_irqsave(shost->host_lock, flags);
	rport->flags &= ~FC_RPORT_SCAN_PENDING;
3285 3286 3287
	spin_unlock_irqrestore(shost->host_lock, flags);
}

3288 3289 3290 3291 3292 3293
/**
 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
 * @cmnd: SCSI command that scsi_eh is trying to recover
 *
 * This routine can be called from a FC LLD scsi_eh callback. It
 * blocks the scsi_eh thread until the fc_rport leaves the
3294 3295 3296 3297 3298 3299 3300
 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
 * necessary to avoid the scsi_eh failing recovery actions for blocked
 * rports which would lead to offlined SCSI devices.
 *
 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
 *	    FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
 *	    passed back to scsi_eh.
3301
 */
3302
int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3303 3304 3305 3306 3307 3308
{
	struct Scsi_Host *shost = cmnd->device->host;
	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
	unsigned long flags;

	spin_lock_irqsave(shost->host_lock, flags);
3309 3310
	while (rport->port_state == FC_PORTSTATE_BLOCKED &&
	       !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
3311 3312 3313 3314 3315
		spin_unlock_irqrestore(shost->host_lock, flags);
		msleep(1000);
		spin_lock_irqsave(shost->host_lock, flags);
	}
	spin_unlock_irqrestore(shost->host_lock, flags);
3316 3317 3318 3319 3320

	if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
		return FAST_IO_FAIL;

	return 0;
3321 3322
}
EXPORT_SYMBOL(fc_block_scsi_eh);
3323

3324
/**
3325
 * fc_vport_setup - allocates and creates a FC virtual port.
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337
 * @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.
3338
 */
3339
static int
3340
fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358
	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)) {
3359
		printk(KERN_ERR "%s: allocation failure\n", __func__);
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373
		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;
3374
	INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385

	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);
3386
	scsi_host_get(shost);			/* for fc_host->vport list */
3387 3388 3389 3390 3391 3392 3393

	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;
3394 3395
	dev_set_name(dev, "vport-%d:%d-%d",
		     shost->host_no, channel, vport->number);
3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417
	transport_setup_device(dev);

	error = device_add(dev);
	if (error) {
		printk(KERN_ERR "FC Virtual Port device_add failed\n");
		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\n");
		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,
3418
				 &dev->kobj, dev_name(dev));
3419 3420 3421 3422
		if (error)
			printk(KERN_ERR
				"%s: Cannot create vport symlinks for "
				"%s, err=%d\n",
3423
				__func__, dev_name(dev), error);
3424 3425 3426 3427 3428 3429
	}
	spin_lock_irqsave(shost->host_lock, flags);
	vport->flags &= ~FC_VPORT_CREATING;
	spin_unlock_irqrestore(shost->host_lock, flags);

	dev_printk(KERN_NOTICE, pdev,
3430
			"%s created via shost%d channel %d\n", dev_name(dev),
3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443
			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);
3444
	scsi_host_put(shost);			/* for fc_host->vport list */
3445 3446 3447 3448 3449 3450 3451 3452
	fc_host->npiv_vports_inuse--;
	spin_unlock_irqrestore(shost->host_lock, flags);
	put_device(dev->parent);
	kfree(vport);

	return error;
}

3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474
/**
 * 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);
3475 3476 3477 3478 3479 3480 3481 3482 3483 3484

/**
 * 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.
3485
 */
3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506
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;

	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--;
3507
		scsi_host_put(shost);		/* for fc_host->vport list */
3508 3509 3510 3511 3512 3513 3514
	}
	spin_unlock_irqrestore(shost->host_lock, flags);

	if (stat)
		return stat;

	if (dev->parent != &shost->shost_gendev)
3515
		sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530
	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);

3531 3532 3533
/**
 * fc_vport_sched_delete - workq-based delete request for a vport
 * @work:	vport to be deleted.
3534
 */
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545
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 "
3546
			"shost%d channel %d - error %d\n", __func__,
3547
			dev_name(&vport->dev), vport->shost->host_no,
3548 3549 3550
			vport->channel, stat);
}

3551

J
James Smart 已提交
3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592
/*
 * BSG support
 */


/**
 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
 * @job:	fc_bsg_job that is to be torn down
 */
static void
fc_destroy_bsgjob(struct fc_bsg_job *job)
{
	unsigned long flags;

	spin_lock_irqsave(&job->job_lock, flags);
	if (job->ref_cnt) {
		spin_unlock_irqrestore(&job->job_lock, flags);
		return;
	}
	spin_unlock_irqrestore(&job->job_lock, flags);

	put_device(job->dev);	/* release reference for the request */

	kfree(job->request_payload.sg_list);
	kfree(job->reply_payload.sg_list);
	kfree(job);
}

/**
 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
 *                  completed
 * @job:	fc_bsg_job that is complete
 */
static void
fc_bsg_jobdone(struct fc_bsg_job *job)
{
	struct request *req = job->req;
	struct request *rsp = req->next_rq;
	int err;

	err = job->req->errors = job->reply->result;
3593

J
James Smart 已提交
3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
	if (err < 0)
		/* we're only returning the result field in the reply */
		job->req->sense_len = sizeof(uint32_t);
	else
		job->req->sense_len = job->reply_len;

	/* we assume all request payload was transferred, residual == 0 */
	req->resid_len = 0;

	if (rsp) {
		WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);

		/* set reply (bidi) residual */
		rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
				      rsp->resid_len);
	}
3610 3611 3612 3613 3614
	blk_complete_request(req);
}

/**
 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
3615
 * @rq:        BSG request that holds the job to be destroyed
3616 3617 3618 3619 3620
 */
static void fc_bsg_softirq_done(struct request *rq)
{
	struct fc_bsg_job *job = rq->special;
	unsigned long flags;
J
James Smart 已提交
3621

3622 3623 3624 3625
	spin_lock_irqsave(&job->job_lock, flags);
	job->state_flags |= FC_RQST_STATE_DONE;
	job->ref_cnt--;
	spin_unlock_irqrestore(&job->job_lock, flags);
J
James Smart 已提交
3626

3627
	blk_end_request_all(rq, rq->errors);
J
James Smart 已提交
3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656
	fc_destroy_bsgjob(job);
}

/**
 * fc_bsg_job_timeout - handler for when a bsg request timesout
 * @req:	request that timed out
 */
static enum blk_eh_timer_return
fc_bsg_job_timeout(struct request *req)
{
	struct fc_bsg_job *job = (void *) req->special;
	struct Scsi_Host *shost = job->shost;
	struct fc_internal *i = to_fc_internal(shost->transportt);
	unsigned long flags;
	int err = 0, done = 0;

	if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
		return BLK_EH_RESET_TIMER;

	spin_lock_irqsave(&job->job_lock, flags);
	if (job->state_flags & FC_RQST_STATE_DONE)
		done = 1;
	else
		job->ref_cnt++;
	spin_unlock_irqrestore(&job->job_lock, flags);

	if (!done && i->f->bsg_timeout) {
		/* call LLDD to abort the i/o as it has timed out */
		err = i->f->bsg_timeout(job);
3657 3658 3659 3660
		if (err == -EAGAIN) {
			job->ref_cnt--;
			return BLK_EH_RESET_TIMER;
		} else if (err)
J
James Smart 已提交
3661 3662 3663 3664 3665
			printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
				"abort failed with status %d\n", err);
	}

	/* the blk_end_sync_io() doesn't check the error */
3666 3667 3668 3669
	if (done)
		return BLK_EH_NOT_HANDLED;
	else
		return BLK_EH_HANDLED;
J
James Smart 已提交
3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771
}

static int
fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
{
	size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);

	BUG_ON(!req->nr_phys_segments);

	buf->sg_list = kzalloc(sz, GFP_KERNEL);
	if (!buf->sg_list)
		return -ENOMEM;
	sg_init_table(buf->sg_list, req->nr_phys_segments);
	buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
	buf->payload_len = blk_rq_bytes(req);
	return 0;
}


/**
 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
 *                   bsg request
 * @shost:	SCSI Host corresponding to the bsg object
 * @rport:	(optional) FC Remote Port corresponding to the bsg object
 * @req:	BSG request that needs a job structure
 */
static int
fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
	struct request *req)
{
	struct fc_internal *i = to_fc_internal(shost->transportt);
	struct request *rsp = req->next_rq;
	struct fc_bsg_job *job;
	int ret;

	BUG_ON(req->special);

	job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
			GFP_KERNEL);
	if (!job)
		return -ENOMEM;

	/*
	 * Note: this is a bit silly.
	 * The request gets formatted as a SGIO v4 ioctl request, which
	 * then gets reformatted as a blk request, which then gets
	 * reformatted as a fc bsg request. And on completion, we have
	 * to wrap return results such that SGIO v4 thinks it was a scsi
	 * status.  I hope this was all worth it.
	 */

	req->special = job;
	job->shost = shost;
	job->rport = rport;
	job->req = req;
	if (i->f->dd_bsg_size)
		job->dd_data = (void *)&job[1];
	spin_lock_init(&job->job_lock);
	job->request = (struct fc_bsg_request *)req->cmd;
	job->request_len = req->cmd_len;
	job->reply = req->sense;
	job->reply_len = SCSI_SENSE_BUFFERSIZE;	/* Size of sense buffer
						 * allocated */
	if (req->bio) {
		ret = fc_bsg_map_buffer(&job->request_payload, req);
		if (ret)
			goto failjob_rls_job;
	}
	if (rsp && rsp->bio) {
		ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
		if (ret)
			goto failjob_rls_rqst_payload;
	}
	job->job_done = fc_bsg_jobdone;
	if (rport)
		job->dev = &rport->dev;
	else
		job->dev = &shost->shost_gendev;
	get_device(job->dev);		/* take a reference for the request */

	job->ref_cnt = 1;

	return 0;


failjob_rls_rqst_payload:
	kfree(job->request_payload.sg_list);
failjob_rls_job:
	kfree(job);
	return -ENOMEM;
}


enum fc_dispatch_result {
	FC_DISPATCH_BREAK,	/* on return, q is locked, break from q loop */
	FC_DISPATCH_LOCKED,	/* on return, q is locked, continue on */
	FC_DISPATCH_UNLOCKED,	/* on return, q is unlocked, continue on */
};


/**
 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
3772
 * @q:		fc host request queue
J
James Smart 已提交
3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841
 * @shost:	scsi host rport attached to
 * @job:	bsg job to be processed
 */
static enum fc_dispatch_result
fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
			 struct fc_bsg_job *job)
{
	struct fc_internal *i = to_fc_internal(shost->transportt);
	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
	int ret;

	/* Validate the host command */
	switch (job->request->msgcode) {
	case FC_BSG_HST_ADD_RPORT:
		cmdlen += sizeof(struct fc_bsg_host_add_rport);
		break;

	case FC_BSG_HST_DEL_RPORT:
		cmdlen += sizeof(struct fc_bsg_host_del_rport);
		break;

	case FC_BSG_HST_ELS_NOLOGIN:
		cmdlen += sizeof(struct fc_bsg_host_els);
		/* there better be a xmt and rcv payloads */
		if ((!job->request_payload.payload_len) ||
		    (!job->reply_payload.payload_len)) {
			ret = -EINVAL;
			goto fail_host_msg;
		}
		break;

	case FC_BSG_HST_CT:
		cmdlen += sizeof(struct fc_bsg_host_ct);
		/* there better be xmt and rcv payloads */
		if ((!job->request_payload.payload_len) ||
		    (!job->reply_payload.payload_len)) {
			ret = -EINVAL;
			goto fail_host_msg;
		}
		break;

	case FC_BSG_HST_VENDOR:
		cmdlen += sizeof(struct fc_bsg_host_vendor);
		if ((shost->hostt->vendor_id == 0L) ||
		    (job->request->rqst_data.h_vendor.vendor_id !=
			shost->hostt->vendor_id)) {
			ret = -ESRCH;
			goto fail_host_msg;
		}
		break;

	default:
		ret = -EBADR;
		goto fail_host_msg;
	}

	/* check if we really have all the request data needed */
	if (job->request_len < cmdlen) {
		ret = -ENOMSG;
		goto fail_host_msg;
	}

	ret = i->f->bsg_request(job);
	if (!ret)
		return FC_DISPATCH_UNLOCKED;

fail_host_msg:
	/* return the errno failure code as the only status */
	BUG_ON(job->reply_len < sizeof(uint32_t));
3842
	job->reply->reply_payload_rcv_len = 0;
J
James Smart 已提交
3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859
	job->reply->result = ret;
	job->reply_len = sizeof(uint32_t);
	fc_bsg_jobdone(job);
	return FC_DISPATCH_UNLOCKED;
}


/*
 * fc_bsg_goose_queue - restart rport queue in case it was stopped
 * @rport:	rport to be restarted
 */
static void
fc_bsg_goose_queue(struct fc_rport *rport)
{
	if (!rport->rqst_q)
		return;

3860 3861 3862
	/*
	 * This get/put dance makes no sense
	 */
J
James Smart 已提交
3863
	get_device(&rport->dev);
3864
	blk_run_queue_async(rport->rqst_q);
J
James Smart 已提交
3865 3866 3867 3868 3869
	put_device(&rport->dev);
}

/**
 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
3870
 * @q:		rport request queue
J
James Smart 已提交
3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916
 * @shost:	scsi host rport attached to
 * @rport:	rport request destined to
 * @job:	bsg job to be processed
 */
static enum fc_dispatch_result
fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
			 struct fc_rport *rport, struct fc_bsg_job *job)
{
	struct fc_internal *i = to_fc_internal(shost->transportt);
	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
	int ret;

	/* Validate the rport command */
	switch (job->request->msgcode) {
	case FC_BSG_RPT_ELS:
		cmdlen += sizeof(struct fc_bsg_rport_els);
		goto check_bidi;

	case FC_BSG_RPT_CT:
		cmdlen += sizeof(struct fc_bsg_rport_ct);
check_bidi:
		/* there better be xmt and rcv payloads */
		if ((!job->request_payload.payload_len) ||
		    (!job->reply_payload.payload_len)) {
			ret = -EINVAL;
			goto fail_rport_msg;
		}
		break;
	default:
		ret = -EBADR;
		goto fail_rport_msg;
	}

	/* check if we really have all the request data needed */
	if (job->request_len < cmdlen) {
		ret = -ENOMSG;
		goto fail_rport_msg;
	}

	ret = i->f->bsg_request(job);
	if (!ret)
		return FC_DISPATCH_UNLOCKED;

fail_rport_msg:
	/* return the errno failure code as the only status */
	BUG_ON(job->reply_len < sizeof(uint32_t));
3917
	job->reply->reply_payload_rcv_len = 0;
J
James Smart 已提交
3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942
	job->reply->result = ret;
	job->reply_len = sizeof(uint32_t);
	fc_bsg_jobdone(job);
	return FC_DISPATCH_UNLOCKED;
}


/**
 * fc_bsg_request_handler - generic handler for bsg requests
 * @q:		request queue to manage
 * @shost:	Scsi_Host related to the bsg object
 * @rport:	FC remote port related to the bsg object (optional)
 * @dev:	device structure for bsg object
 */
static void
fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
		       struct fc_rport *rport, struct device *dev)
{
	struct request *req;
	struct fc_bsg_job *job;
	enum fc_dispatch_result ret;

	if (!get_device(dev))
		return;

J
Jens Axboe 已提交
3943
	while (1) {
3944 3945 3946
		if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
		    !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
			break;
J
James Smart 已提交
3947 3948 3949 3950 3951 3952 3953 3954

		req = blk_fetch_request(q);
		if (!req)
			break;

		if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
			req->errors = -ENXIO;
			spin_unlock_irq(q->queue_lock);
3955
			blk_end_request_all(req, -ENXIO);
J
James Smart 已提交
3956 3957 3958 3959 3960 3961 3962 3963 3964
			spin_lock_irq(q->queue_lock);
			continue;
		}

		spin_unlock_irq(q->queue_lock);

		ret = fc_req_to_bsgjob(shost, rport, req);
		if (ret) {
			req->errors = ret;
3965
			blk_end_request_all(req, ret);
J
James Smart 已提交
3966 3967 3968 3969 3970 3971 3972 3973 3974
			spin_lock_irq(q->queue_lock);
			continue;
		}

		job = req->special;

		/* check if we have the msgcode value at least */
		if (job->request_len < sizeof(uint32_t)) {
			BUG_ON(job->reply_len < sizeof(uint32_t));
3975
			job->reply->reply_payload_rcv_len = 0;
J
James Smart 已提交
3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042
			job->reply->result = -ENOMSG;
			job->reply_len = sizeof(uint32_t);
			fc_bsg_jobdone(job);
			spin_lock_irq(q->queue_lock);
			continue;
		}

		/* the dispatch routines will unlock the queue_lock */
		if (rport)
			ret = fc_bsg_rport_dispatch(q, shost, rport, job);
		else
			ret = fc_bsg_host_dispatch(q, shost, job);

		/* did dispatcher hit state that can't process any more */
		if (ret == FC_DISPATCH_BREAK)
			break;

		/* did dispatcher had released the lock */
		if (ret == FC_DISPATCH_UNLOCKED)
			spin_lock_irq(q->queue_lock);
	}

	spin_unlock_irq(q->queue_lock);
	put_device(dev);
	spin_lock_irq(q->queue_lock);
}


/**
 * fc_bsg_host_handler - handler for bsg requests for a fc host
 * @q:		fc host request queue
 */
static void
fc_bsg_host_handler(struct request_queue *q)
{
	struct Scsi_Host *shost = q->queuedata;

	fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
}


/**
 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
 * @q:		rport request queue
 */
static void
fc_bsg_rport_handler(struct request_queue *q)
{
	struct fc_rport *rport = q->queuedata;
	struct Scsi_Host *shost = rport_to_shost(rport);

	fc_bsg_request_handler(q, shost, rport, &rport->dev);
}


/**
 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
 * @shost:	shost for fc_host
 * @fc_host:	fc_host adding the structures to
 */
static int
fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
{
	struct device *dev = &shost->shost_gendev;
	struct fc_internal *i = to_fc_internal(shost->transportt);
	struct request_queue *q;
	int err;
4043
	char bsg_name[20];
J
James Smart 已提交
4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062

	fc_host->rqst_q = NULL;

	if (!i->f->bsg_request)
		return -ENOTSUPP;

	snprintf(bsg_name, sizeof(bsg_name),
		 "fc_host%d", shost->host_no);

	q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
	if (!q) {
		printk(KERN_ERR "fc_host%d: bsg interface failed to "
				"initialize - no request queue\n",
				 shost->host_no);
		return -ENOMEM;
	}

	q->queuedata = shost;
	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
4063
	blk_queue_softirq_done(q, fc_bsg_softirq_done);
J
James Smart 已提交
4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108
	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
	blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);

	err = bsg_register_queue(q, dev, bsg_name, NULL);
	if (err) {
		printk(KERN_ERR "fc_host%d: bsg interface failed to "
				"initialize - register queue\n",
				shost->host_no);
		blk_cleanup_queue(q);
		return err;
	}

	fc_host->rqst_q = q;
	return 0;
}


/**
 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
 * @shost:	shost that rport is attached to
 * @rport:	rport that the bsg hooks are being attached to
 */
static int
fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
{
	struct device *dev = &rport->dev;
	struct fc_internal *i = to_fc_internal(shost->transportt);
	struct request_queue *q;
	int err;

	rport->rqst_q = NULL;

	if (!i->f->bsg_request)
		return -ENOTSUPP;

	q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
	if (!q) {
		printk(KERN_ERR "%s: bsg interface failed to "
				"initialize - no request queue\n",
				 dev->kobj.name);
		return -ENOMEM;
	}

	q->queuedata = rport;
	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
4109
	blk_queue_softirq_done(q, fc_bsg_softirq_done);
J
James Smart 已提交
4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129
	blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
	blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);

	err = bsg_register_queue(q, dev, NULL, NULL);
	if (err) {
		printk(KERN_ERR "%s: bsg interface failed to "
				"initialize - register queue\n",
				 dev->kobj.name);
		blk_cleanup_queue(q);
		return err;
	}

	rport->rqst_q = q;
	return 0;
}


/**
 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
 * @q:	the request_queue that is to be torn down.
4130 4131 4132 4133 4134
 *
 * Notes:
 *   Before unregistering the queue empty any requests that are blocked
 *
 *
J
James Smart 已提交
4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145
 */
static void
fc_bsg_remove(struct request_queue *q)
{
	if (q) {
		bsg_unregister_queue(q);
		blk_cleanup_queue(q);
	}
}


4146 4147
/* Original Author:  Martin Hicks */
MODULE_AUTHOR("James Smart");
L
Linus Torvalds 已提交
4148 4149 4150 4151 4152
MODULE_DESCRIPTION("FC Transport Attributes");
MODULE_LICENSE("GPL");

module_init(fc_transport_init);
module_exit(fc_transport_exit);