bfa_fcs.c 35.9 KB
Newer Older
1
/*
K
Krishna Gudipati 已提交
2
 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * All rights reserved
 * www.brocade.com
 *
 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License (GPL) Version 2 as
 * published by the Free Software Foundation
 *
 * 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.
 */

J
Jing Huang 已提交
18
/*
19 20 21
 *  bfa_fcs.c BFA FCS main
 */

M
Maggie Zhang 已提交
22
#include "bfad_drv.h"
K
Krishna Gudipati 已提交
23 24 25 26
#include "bfa_fcs.h"
#include "bfa_fcbuild.h"

BFA_TRC_FILE(FCS, FCS);
27

J
Jing Huang 已提交
28
/*
29 30 31
 * FCS sub-modules
 */
struct bfa_fcs_mod_s {
32
	void		(*attach) (struct bfa_fcs_s *fcs);
K
Krishna Gudipati 已提交
33 34
	void		(*modinit) (struct bfa_fcs_s *fcs);
	void		(*modexit) (struct bfa_fcs_s *fcs);
35 36 37 38 39
};

#define BFA_FCS_MODULE(_mod) { _mod ## _modinit, _mod ## _modexit }

static struct bfa_fcs_mod_s fcs_modules[] = {
K
Krishna Gudipati 已提交
40
	{ bfa_fcs_port_attach, NULL, NULL },
41 42
	{ bfa_fcs_uf_attach, NULL, NULL },
	{ bfa_fcs_fabric_attach, bfa_fcs_fabric_modinit,
K
Krishna Gudipati 已提交
43
	  bfa_fcs_fabric_modexit },
44 45
};

J
Jing Huang 已提交
46
/*
47 48 49 50 51 52
 *  fcs_api BFA FCS API
 */

static void
bfa_fcs_exit_comp(void *fcs_cbarg)
{
K
Krishna Gudipati 已提交
53 54
	struct bfa_fcs_s      *fcs = fcs_cbarg;
	struct bfad_s         *bfad = fcs->bfad;
55 56 57 58 59 60

	complete(&bfad->comp);
}



J
Jing Huang 已提交
61
/*
62 63 64
 *  fcs_api BFA FCS API
 */

J
Jing Huang 已提交
65
/*
66
 * fcs attach -- called once to initialize data structures at driver attach time
67 68
 */
void
69
bfa_fcs_attach(struct bfa_fcs_s *fcs, struct bfa_s *bfa, struct bfad_s *bfad,
K
Krishna Gudipati 已提交
70
	       bfa_boolean_t min_cfg)
71
{
K
Krishna Gudipati 已提交
72
	int		i;
73 74 75 76 77 78
	struct bfa_fcs_mod_s  *mod;

	fcs->bfa = bfa;
	fcs->bfad = bfad;
	fcs->min_cfg = min_cfg;

79
	bfa->fcs = BFA_TRUE;
80 81
	fcbuild_init();

K
Krishna Gudipati 已提交
82
	for (i = 0; i < sizeof(fcs_modules) / sizeof(fcs_modules[0]); i++) {
83
		mod = &fcs_modules[i];
84 85 86 87 88
		if (mod->attach)
			mod->attach(fcs);
	}
}

J
Jing Huang 已提交
89
/*
90 91 92 93 94
 * fcs initialization, called once after bfa initialization is complete
 */
void
bfa_fcs_init(struct bfa_fcs_s *fcs)
{
95
	int	i;
96 97
	struct bfa_fcs_mod_s  *mod;

K
Krishna Gudipati 已提交
98
	for (i = 0; i < sizeof(fcs_modules) / sizeof(fcs_modules[0]); i++) {
99 100 101
		mod = &fcs_modules[i];
		if (mod->modinit)
			mod->modinit(fcs);
102
	}
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
}

/*
 * FCS update cfg - reset the pwwn/nwwn of fabric base logical port
 * with values learned during bfa_init firmware GETATTR REQ.
 */
void
bfa_fcs_update_cfg(struct bfa_fcs_s *fcs)
{
	struct bfa_fcs_fabric_s *fabric = &fcs->fabric;
	struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
	struct bfa_ioc_s *ioc = &fabric->fcs->bfa->ioc;

	port_cfg->nwwn = ioc->attr->nwwn;
	port_cfg->pwwn = ioc->attr->pwwn;
}

/*
 * fcs pbc vport initialization
 */
void
bfa_fcs_pbc_vport_init(struct bfa_fcs_s *fcs)
{
	int i, npbc_vports;
	struct bfi_pbc_vport_s pbc_vports[BFI_PBC_MAX_VPORTS];

J
Jing Huang 已提交
129 130 131
	/* Initialize pbc vports */
	if (!fcs->min_cfg) {
		npbc_vports =
132
			bfa_iocfc_get_pbc_vports(fcs->bfa, pbc_vports);
J
Jing Huang 已提交
133 134 135
		for (i = 0; i < npbc_vports; i++)
			bfa_fcb_pbc_vport_create(fcs->bfa->bfad, pbc_vports[i]);
	}
136 137
}

J
Jing Huang 已提交
138
/*
K
Krishna Gudipati 已提交
139 140
 *	brief
 *		FCS driver details initialization.
141
 *
K
Krishna Gudipati 已提交
142 143
 *	param[in]		fcs		FCS instance
 *	param[in]		driver_info	Driver Details
144
 *
K
Krishna Gudipati 已提交
145
 *	return None
146 147 148 149 150 151 152 153 154 155 156
 */
void
bfa_fcs_driver_info_init(struct bfa_fcs_s *fcs,
			struct bfa_fcs_driver_info_s *driver_info)
{

	fcs->driver_info = *driver_info;

	bfa_fcs_fabric_psymb_init(&fcs->fabric);
}

J
Jing Huang 已提交
157
/*
K
Krishna Gudipati 已提交
158 159
 *	brief
 *		FCS instance cleanup and exit.
160
 *
K
Krishna Gudipati 已提交
161 162
 *	param[in]		fcs			FCS instance
 *	return None
163 164 165 166 167
 */
void
bfa_fcs_exit(struct bfa_fcs_s *fcs)
{
	struct bfa_fcs_mod_s  *mod;
K
Krishna Gudipati 已提交
168
	int		nmods, i;
169 170 171

	bfa_wc_init(&fcs->wc, bfa_fcs_exit_comp, fcs);

K
Krishna Gudipati 已提交
172 173 174
	nmods = sizeof(fcs_modules) / sizeof(fcs_modules[0]);

	for (i = 0; i < nmods; i++) {
175 176

		mod = &fcs_modules[i];
177 178 179 180
		if (mod->modexit) {
			bfa_wc_up(&fcs->wc);
			mod->modexit(fcs);
		}
181 182 183 184 185 186
	}

	bfa_wc_wait(&fcs->wc);
}


J
Jing Huang 已提交
187
/*
K
Krishna Gudipati 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
 * Fabric module implementation.
 */

#define BFA_FCS_FABRIC_RETRY_DELAY	(2000)	/* Milliseconds */
#define BFA_FCS_FABRIC_CLEANUP_DELAY	(10000)	/* Milliseconds */

#define bfa_fcs_fabric_set_opertype(__fabric) do {			\
		if (bfa_fcport_get_topology((__fabric)->fcs->bfa)	\
		    == BFA_PORT_TOPOLOGY_P2P)				\
			(__fabric)->oper_type = BFA_PORT_TYPE_NPORT;	\
		else							\
			(__fabric)->oper_type = BFA_PORT_TYPE_NLPORT;	\
} while (0)

/*
 * forward declarations
 */
static void bfa_fcs_fabric_init(struct bfa_fcs_fabric_s *fabric);
static void bfa_fcs_fabric_login(struct bfa_fcs_fabric_s *fabric);
static void bfa_fcs_fabric_notify_online(struct bfa_fcs_fabric_s *fabric);
static void bfa_fcs_fabric_notify_offline(struct bfa_fcs_fabric_s *fabric);
static void bfa_fcs_fabric_delay(void *cbarg);
static void bfa_fcs_fabric_delete(struct bfa_fcs_fabric_s *fabric);
static void bfa_fcs_fabric_delete_comp(void *cbarg);
static void bfa_fcs_fabric_process_uf(struct bfa_fcs_fabric_s *fabric,
				      struct fchs_s *fchs, u16 len);
static void bfa_fcs_fabric_process_flogi(struct bfa_fcs_fabric_s *fabric,
					 struct fchs_s *fchs, u16 len);
static void bfa_fcs_fabric_send_flogi_acc(struct bfa_fcs_fabric_s *fabric);
static void bfa_fcs_fabric_flogiacc_comp(void *fcsarg,
					 struct bfa_fcxp_s *fcxp, void *cbarg,
					 bfa_status_t status,
					 u32 rsp_len,
					 u32 resid_len,
					 struct fchs_s *rspfchs);
223 224 225
static u8 bfa_fcs_fabric_oper_bbscn(struct bfa_fcs_fabric_s *fabric);
static bfa_boolean_t bfa_fcs_fabric_is_bbscn_enabled(
				struct bfa_fcs_fabric_s *fabric);
K
Krishna Gudipati 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

static void	bfa_fcs_fabric_sm_uninit(struct bfa_fcs_fabric_s *fabric,
					 enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_created(struct bfa_fcs_fabric_s *fabric,
					  enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_linkdown(struct bfa_fcs_fabric_s *fabric,
					   enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_flogi(struct bfa_fcs_fabric_s *fabric,
					enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_flogi_retry(struct bfa_fcs_fabric_s *fabric,
					      enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_auth(struct bfa_fcs_fabric_s *fabric,
				       enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_nofabric(struct bfa_fcs_fabric_s *fabric,
					   enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_evfp(struct bfa_fcs_fabric_s *fabric,
				       enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_evfp_done(struct bfa_fcs_fabric_s *fabric,
					    enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_isolated(struct bfa_fcs_fabric_s *fabric,
					   enum bfa_fcs_fabric_event event);
static void	bfa_fcs_fabric_sm_deleting(struct bfa_fcs_fabric_s *fabric,
					   enum bfa_fcs_fabric_event event);
J
Jing Huang 已提交
249
/*
K
Krishna Gudipati 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
 *   Beginning state before fabric creation.
 */
static void
bfa_fcs_fabric_sm_uninit(struct bfa_fcs_fabric_s *fabric,
			 enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_CREATE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_created);
		bfa_fcs_fabric_init(fabric);
		bfa_fcs_lport_init(&fabric->bport, &fabric->bport.port_cfg);
		break;

	case BFA_FCS_FABRIC_SM_LINK_UP:
	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
275
/*
K
Krishna Gudipati 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
 *   Beginning state before fabric creation.
 */
static void
bfa_fcs_fabric_sm_created(struct bfa_fcs_fabric_s *fabric,
			  enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_START:
		if (bfa_fcport_is_linkup(fabric->fcs->bfa)) {
			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi);
			bfa_fcs_fabric_login(fabric);
		} else
			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
		break;

	case BFA_FCS_FABRIC_SM_LINK_UP:
	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		break;

	case BFA_FCS_FABRIC_SM_DELETE:
299 300
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
		bfa_fcs_fabric_delete(fabric);
K
Krishna Gudipati 已提交
301 302 303 304 305 306 307
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
308
/*
K
Krishna Gudipati 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
 *   Link is down, awaiting LINK UP event from port. This is also the
 *   first state at fabric creation.
 */
static void
bfa_fcs_fabric_sm_linkdown(struct bfa_fcs_fabric_s *fabric,
			   enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_LINK_UP:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi);
		bfa_fcs_fabric_login(fabric);
		break;

	case BFA_FCS_FABRIC_SM_RETRY_OP:
		break;

	case BFA_FCS_FABRIC_SM_DELETE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
		bfa_fcs_fabric_delete(fabric);
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
338
/*
K
Krishna Gudipati 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351
 *   FLOGI is in progress, awaiting FLOGI reply.
 */
static void
bfa_fcs_fabric_sm_flogi(struct bfa_fcs_fabric_s *fabric,
			enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_CONT_OP:

		bfa_fcport_set_tx_bbcredit(fabric->fcs->bfa,
352 353
					   fabric->bb_credit,
					   bfa_fcs_fabric_oper_bbscn(fabric));
K
Krishna Gudipati 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
		fabric->fab_type = BFA_FCS_FABRIC_SWITCHED;

		if (fabric->auth_reqd && fabric->is_auth) {
			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_auth);
			bfa_trc(fabric->fcs, event);
		} else {
			bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_online);
			bfa_fcs_fabric_notify_online(fabric);
		}
		break;

	case BFA_FCS_FABRIC_SM_RETRY_OP:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi_retry);
		bfa_timer_start(fabric->fcs->bfa, &fabric->delay_timer,
				bfa_fcs_fabric_delay, fabric,
				BFA_FCS_FABRIC_RETRY_DELAY);
		break;

	case BFA_FCS_FABRIC_SM_LOOPBACK:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_loopback);
374
		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
K
Krishna Gudipati 已提交
375 376 377 378 379 380
		bfa_fcs_fabric_set_opertype(fabric);
		break;

	case BFA_FCS_FABRIC_SM_NO_FABRIC:
		fabric->fab_type = BFA_FCS_FABRIC_N2N;
		bfa_fcport_set_tx_bbcredit(fabric->fcs->bfa,
381 382
					   fabric->bb_credit,
					   bfa_fcs_fabric_oper_bbscn(fabric));
K
Krishna Gudipati 已提交
383 384 385 386 387 388
		bfa_fcs_fabric_notify_online(fabric);
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_nofabric);
		break;

	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
389
		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
K
Krishna Gudipati 已提交
390 391 392 393
		break;

	case BFA_FCS_FABRIC_SM_DELETE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
394
		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
K
Krishna Gudipati 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
		bfa_fcs_fabric_delete(fabric);
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}


static void
bfa_fcs_fabric_sm_flogi_retry(struct bfa_fcs_fabric_s *fabric,
			      enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_DELAYED:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_flogi);
		bfa_fcs_fabric_login(fabric);
		break;

	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
		bfa_timer_stop(&fabric->delay_timer);
		break;

	case BFA_FCS_FABRIC_SM_DELETE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
		bfa_timer_stop(&fabric->delay_timer);
		bfa_fcs_fabric_delete(fabric);
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
433
/*
K
Krishna Gudipati 已提交
434 435 436 437 438 439 440 441 442 443 444 445
 *   Authentication is in progress, awaiting authentication results.
 */
static void
bfa_fcs_fabric_sm_auth(struct bfa_fcs_fabric_s *fabric,
		       enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_AUTH_FAILED:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_auth_failed);
446
		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
K
Krishna Gudipati 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459
		break;

	case BFA_FCS_FABRIC_SM_AUTH_SUCCESS:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_online);
		bfa_fcs_fabric_notify_online(fabric);
		break;

	case BFA_FCS_FABRIC_SM_PERF_EVFP:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_evfp);
		break;

	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
460
		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
K
Krishna Gudipati 已提交
461 462 463 464 465 466 467 468 469 470 471 472
		break;

	case BFA_FCS_FABRIC_SM_DELETE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
		bfa_fcs_fabric_delete(fabric);
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
473
/*
K
Krishna Gudipati 已提交
474 475
 *   Authentication failed
 */
476
void
K
Krishna Gudipati 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
bfa_fcs_fabric_sm_auth_failed(struct bfa_fcs_fabric_s *fabric,
			      enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
		bfa_fcs_fabric_notify_offline(fabric);
		break;

	case BFA_FCS_FABRIC_SM_DELETE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
		bfa_fcs_fabric_delete(fabric);
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
499
/*
K
Krishna Gudipati 已提交
500 501
 *   Port is in loopback mode.
 */
502
void
K
Krishna Gudipati 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
bfa_fcs_fabric_sm_loopback(struct bfa_fcs_fabric_s *fabric,
			   enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
		bfa_fcs_fabric_notify_offline(fabric);
		break;

	case BFA_FCS_FABRIC_SM_DELETE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
		bfa_fcs_fabric_delete(fabric);
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
525
/*
K
Krishna Gudipati 已提交
526 527 528 529 530 531 532 533 534 535 536 537
 *   There is no attached fabric - private loop or NPort-to-NPort topology.
 */
static void
bfa_fcs_fabric_sm_nofabric(struct bfa_fcs_fabric_s *fabric,
			   enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
538
		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
K
Krishna Gudipati 已提交
539 540 541 542 543 544 545 546 547 548 549
		bfa_fcs_fabric_notify_offline(fabric);
		break;

	case BFA_FCS_FABRIC_SM_DELETE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
		bfa_fcs_fabric_delete(fabric);
		break;

	case BFA_FCS_FABRIC_SM_NO_FABRIC:
		bfa_trc(fabric->fcs, fabric->bb_credit);
		bfa_fcport_set_tx_bbcredit(fabric->fcs->bfa,
550 551
					   fabric->bb_credit,
					   bfa_fcs_fabric_oper_bbscn(fabric));
K
Krishna Gudipati 已提交
552 553 554 555 556 557 558
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
559
/*
K
Krishna Gudipati 已提交
560 561
 *   Fabric is online - normal operating state.
 */
562
void
K
Krishna Gudipati 已提交
563 564 565 566 567 568 569 570 571
bfa_fcs_fabric_sm_online(struct bfa_fcs_fabric_s *fabric,
			 enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_linkdown);
572
		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
K
Krishna Gudipati 已提交
573 574 575 576 577 578 579 580 581 582
		bfa_fcs_fabric_notify_offline(fabric);
		break;

	case BFA_FCS_FABRIC_SM_DELETE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_deleting);
		bfa_fcs_fabric_delete(fabric);
		break;

	case BFA_FCS_FABRIC_SM_AUTH_FAILED:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_auth_failed);
583
		bfa_sm_send_event(fabric->lps, BFA_LPS_SM_OFFLINE);
K
Krishna Gudipati 已提交
584 585 586 587 588 589 590 591 592 593
		break;

	case BFA_FCS_FABRIC_SM_AUTH_SUCCESS:
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
594
/*
K
Krishna Gudipati 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
 *   Exchanging virtual fabric parameters.
 */
static void
bfa_fcs_fabric_sm_evfp(struct bfa_fcs_fabric_s *fabric,
		       enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_CONT_OP:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_evfp_done);
		break;

	case BFA_FCS_FABRIC_SM_ISOLATE:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_isolated);
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
}

J
Jing Huang 已提交
618
/*
K
Krishna Gudipati 已提交
619 620 621 622 623 624 625 626 627 628
 *   EVFP exchange complete and VFT tagging is enabled.
 */
static void
bfa_fcs_fabric_sm_evfp_done(struct bfa_fcs_fabric_s *fabric,
			    enum bfa_fcs_fabric_event event)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);
}

J
Jing Huang 已提交
629
/*
K
Krishna Gudipati 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642
 *   Port is isolated after EVFP exchange due to VF_ID mismatch (N and F).
 */
static void
bfa_fcs_fabric_sm_isolated(struct bfa_fcs_fabric_s *fabric,
			   enum bfa_fcs_fabric_event event)
{
	struct bfad_s *bfad = (struct bfad_s *)fabric->fcs->bfad;
	char	pwwn_ptr[BFA_STRING_32];

	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);
	wwn2str(pwwn_ptr, fabric->bport.port_cfg.pwwn);

643
	BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
644 645 646 647 648 649
		"Port is isolated due to VF_ID mismatch. "
		"PWWN: %s Port VF_ID: %04x switch port VF_ID: %04x.",
		pwwn_ptr, fabric->fcs->port_vfid,
		fabric->event_arg.swp_vfid);
}

J
Jing Huang 已提交
650
/*
K
Krishna Gudipati 已提交
651 652 653 654 655
 *   Fabric is being deleted, awaiting vport delete completions.
 */
static void
bfa_fcs_fabric_sm_deleting(struct bfa_fcs_fabric_s *fabric,
			   enum bfa_fcs_fabric_event event)
656
{
K
Krishna Gudipati 已提交
657 658 659 660 661 662
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, event);

	switch (event) {
	case BFA_FCS_FABRIC_SM_DELCOMP:
		bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_uninit);
663
		bfa_wc_down(&fabric->fcs->wc);
K
Krishna Gudipati 已提交
664 665 666 667 668 669 670 671 672 673 674 675
		break;

	case BFA_FCS_FABRIC_SM_LINK_UP:
		break;

	case BFA_FCS_FABRIC_SM_LINK_DOWN:
		bfa_fcs_fabric_notify_offline(fabric);
		break;

	default:
		bfa_sm_fault(fabric->fcs, event);
	}
676 677 678
}


K
Krishna Gudipati 已提交
679

J
Jing Huang 已提交
680
/*
K
Krishna Gudipati 已提交
681 682 683 684 685 686 687 688 689
 *  fcs_fabric_private fabric private functions
 */

static void
bfa_fcs_fabric_init(struct bfa_fcs_fabric_s *fabric)
{
	struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;

	port_cfg->roles = BFA_LPORT_ROLE_FCP_IM;
690 691
	port_cfg->nwwn = fabric->fcs->bfa->ioc.attr->nwwn;
	port_cfg->pwwn = fabric->fcs->bfa->ioc.attr->pwwn;
K
Krishna Gudipati 已提交
692 693
}

J
Jing Huang 已提交
694
/*
K
Krishna Gudipati 已提交
695 696
 * Port Symbolic Name Creation for base port.
 */
697
void
K
Krishna Gudipati 已提交
698
bfa_fcs_fabric_psymb_init(struct bfa_fcs_fabric_s *fabric)
699
{
K
Krishna Gudipati 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	struct bfa_lport_cfg_s *port_cfg = &fabric->bport.port_cfg;
	char model[BFA_ADAPTER_MODEL_NAME_LEN] = {0};
	struct bfa_fcs_driver_info_s *driver_info = &fabric->fcs->driver_info;

	bfa_ioc_get_adapter_model(&fabric->fcs->bfa->ioc, model);

	/* Model name/number */
	strncpy((char *)&port_cfg->sym_name, model,
		BFA_FCS_PORT_SYMBNAME_MODEL_SZ);
	strncat((char *)&port_cfg->sym_name, BFA_FCS_PORT_SYMBNAME_SEPARATOR,
		sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));

	/* Driver Version */
	strncat((char *)&port_cfg->sym_name, (char *)driver_info->version,
		BFA_FCS_PORT_SYMBNAME_VERSION_SZ);
	strncat((char *)&port_cfg->sym_name, BFA_FCS_PORT_SYMBNAME_SEPARATOR,
		sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));

	/* Host machine name */
	strncat((char *)&port_cfg->sym_name,
		(char *)driver_info->host_machine_name,
		BFA_FCS_PORT_SYMBNAME_MACHINENAME_SZ);
	strncat((char *)&port_cfg->sym_name, BFA_FCS_PORT_SYMBNAME_SEPARATOR,
		sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));

	/*
	 * Host OS Info :
	 * If OS Patch Info is not there, do not truncate any bytes from the
	 * OS name string and instead copy the entire OS info string (64 bytes).
	 */
	if (driver_info->host_os_patch[0] == '\0') {
		strncat((char *)&port_cfg->sym_name,
			(char *)driver_info->host_os_name,
			BFA_FCS_OS_STR_LEN);
		strncat((char *)&port_cfg->sym_name,
			BFA_FCS_PORT_SYMBNAME_SEPARATOR,
			sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));
	} else {
		strncat((char *)&port_cfg->sym_name,
			(char *)driver_info->host_os_name,
			BFA_FCS_PORT_SYMBNAME_OSINFO_SZ);
		strncat((char *)&port_cfg->sym_name,
			BFA_FCS_PORT_SYMBNAME_SEPARATOR,
			sizeof(BFA_FCS_PORT_SYMBNAME_SEPARATOR));

		/* Append host OS Patch Info */
		strncat((char *)&port_cfg->sym_name,
			(char *)driver_info->host_os_patch,
			BFA_FCS_PORT_SYMBNAME_OSPATCH_SZ);
	}

	/* null terminate */
	port_cfg->sym_name.symname[BFA_SYMNAME_MAXLEN - 1] = 0;
753 754
}

J
Jing Huang 已提交
755
/*
K
Krishna Gudipati 已提交
756 757
 * bfa lps login completion callback
 */
758
void
K
Krishna Gudipati 已提交
759
bfa_cb_lps_flogi_comp(void *bfad, void *uarg, bfa_status_t status)
760
{
K
Krishna Gudipati 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	struct bfa_fcs_fabric_s *fabric = uarg;

	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_trc(fabric->fcs, status);

	switch (status) {
	case BFA_STATUS_OK:
		fabric->stats.flogi_accepts++;
		break;

	case BFA_STATUS_INVALID_MAC:
		/* Only for CNA */
		fabric->stats.flogi_acc_err++;
		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);

		return;

	case BFA_STATUS_EPROTOCOL:
779
		switch (fabric->lps->ext_status) {
K
Krishna Gudipati 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
		case BFA_EPROTO_BAD_ACCEPT:
			fabric->stats.flogi_acc_err++;
			break;

		case BFA_EPROTO_UNKNOWN_RSP:
			fabric->stats.flogi_unknown_rsp++;
			break;

		default:
			break;
		}
		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);

		return;

	case BFA_STATUS_FABRIC_RJT:
		fabric->stats.flogi_rejects++;
797 798 799 800
		if (fabric->lps->lsrjt_rsn == FC_LS_RJT_RSN_LOGICAL_ERROR &&
		    fabric->lps->lsrjt_expl == FC_LS_RJT_EXP_NO_ADDL_INFO)
			fabric->fcs->bbscn_flogi_rjt = BFA_TRUE;

K
Krishna Gudipati 已提交
801 802 803 804 805 806 807 808 809
		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
		return;

	default:
		fabric->stats.flogi_rsp_err++;
		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_RETRY_OP);
		return;
	}

810
	fabric->bb_credit = fabric->lps->pr_bbcred;
K
Krishna Gudipati 已提交
811 812
	bfa_trc(fabric->fcs, fabric->bb_credit);

813 814
	if (!(fabric->lps->brcd_switch))
		fabric->fabric_name =  fabric->lps->pr_nwwn;
K
Krishna Gudipati 已提交
815 816 817 818

	/*
	 * Check port type. It should be 1 = F-port.
	 */
819 820 821 822
	if (fabric->lps->fport) {
		fabric->bport.pid = fabric->lps->lp_pid;
		fabric->is_npiv = fabric->lps->npiv_en;
		fabric->is_auth = fabric->lps->auth_req;
K
Krishna Gudipati 已提交
823 824 825 826 827 828
		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_CONT_OP);
	} else {
		/*
		 * Nport-2-Nport direct attached
		 */
		fabric->bport.port_topo.pn2n.rem_port_wwn =
829
			fabric->lps->pr_pwwn;
K
Krishna Gudipati 已提交
830 831 832 833 834 835 836
		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_NO_FABRIC);
	}

	bfa_trc(fabric->fcs, fabric->bport.pid);
	bfa_trc(fabric->fcs, fabric->is_npiv);
	bfa_trc(fabric->fcs, fabric->is_auth);
}
J
Jing Huang 已提交
837
/*
K
Krishna Gudipati 已提交
838 839 840 841 842 843 844
 *		Allocate and send FLOGI.
 */
static void
bfa_fcs_fabric_login(struct bfa_fcs_fabric_s *fabric)
{
	struct bfa_s		*bfa = fabric->fcs->bfa;
	struct bfa_lport_cfg_s	*pcfg = &fabric->bport.port_cfg;
845
	u8			alpa = 0, bb_scn = 0;
K
Krishna Gudipati 已提交
846 847 848 849

	if (bfa_fcport_get_topology(bfa) == BFA_PORT_TOPOLOGY_LOOP)
		alpa = bfa_fcport_get_myalpa(bfa);

850 851 852 853
	if (bfa_fcs_fabric_is_bbscn_enabled(fabric) &&
	    (!fabric->fcs->bbscn_flogi_rjt))
		bb_scn = BFA_FCS_PORT_DEF_BB_SCN;

K
Krishna Gudipati 已提交
854
	bfa_lps_flogi(fabric->lps, fabric, alpa, bfa_fcport_get_maxfrsize(bfa),
855
		      pcfg->pwwn, pcfg->nwwn, fabric->auth_reqd, bb_scn);
K
Krishna Gudipati 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

	fabric->stats.flogi_sent++;
}

static void
bfa_fcs_fabric_notify_online(struct bfa_fcs_fabric_s *fabric)
{
	struct bfa_fcs_vport_s *vport;
	struct list_head	      *qe, *qen;

	bfa_trc(fabric->fcs, fabric->fabric_name);

	bfa_fcs_fabric_set_opertype(fabric);
	fabric->stats.fabric_onlines++;

J
Jing Huang 已提交
871
	/*
K
Krishna Gudipati 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
	 * notify online event to base and then virtual ports
	 */
	bfa_fcs_lport_online(&fabric->bport);

	list_for_each_safe(qe, qen, &fabric->vport_q) {
		vport = (struct bfa_fcs_vport_s *) qe;
		bfa_fcs_vport_online(vport);
	}
}

static void
bfa_fcs_fabric_notify_offline(struct bfa_fcs_fabric_s *fabric)
{
	struct bfa_fcs_vport_s *vport;
	struct list_head	      *qe, *qen;

	bfa_trc(fabric->fcs, fabric->fabric_name);
	fabric->stats.fabric_offlines++;

J
Jing Huang 已提交
891
	/*
K
Krishna Gudipati 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
	 * notify offline event first to vports and then base port.
	 */
	list_for_each_safe(qe, qen, &fabric->vport_q) {
		vport = (struct bfa_fcs_vport_s *) qe;
		bfa_fcs_vport_offline(vport);
	}

	bfa_fcs_lport_offline(&fabric->bport);

	fabric->fabric_name = 0;
	fabric->fabric_ip_addr[0] = 0;
}

static void
bfa_fcs_fabric_delay(void *cbarg)
{
	struct bfa_fcs_fabric_s *fabric = cbarg;

	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_DELAYED);
}

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
/*
 * Computes operating BB_SCN value
 */
static u8
bfa_fcs_fabric_oper_bbscn(struct bfa_fcs_fabric_s *fabric)
{
	u8	pr_bbscn = fabric->lps->pr_bbscn;

	if (!(fabric->fcs->bbscn_enabled && pr_bbscn))
		return 0;

	/* return max of local/remote bb_scn values */
	return ((pr_bbscn > BFA_FCS_PORT_DEF_BB_SCN) ?
		pr_bbscn : BFA_FCS_PORT_DEF_BB_SCN);
}

/*
 * Check if BB_SCN can be enabled.
 */
static bfa_boolean_t
bfa_fcs_fabric_is_bbscn_enabled(struct bfa_fcs_fabric_s *fabric)
{
	if (bfa_ioc_get_fcmode(&fabric->fcs->bfa->ioc) &&
			fabric->fcs->bbscn_enabled &&
			!bfa_fcport_is_qos_enabled(fabric->fcs->bfa) &&
			!bfa_fcport_is_trunk_enabled(fabric->fcs->bfa))
		return BFA_TRUE;
	else
		return BFA_FALSE;
}

J
Jing Huang 已提交
944
/*
K
Krishna Gudipati 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
 * Delete all vports and wait for vport delete completions.
 */
static void
bfa_fcs_fabric_delete(struct bfa_fcs_fabric_s *fabric)
{
	struct bfa_fcs_vport_s *vport;
	struct list_head	      *qe, *qen;

	list_for_each_safe(qe, qen, &fabric->vport_q) {
		vport = (struct bfa_fcs_vport_s *) qe;
		bfa_fcs_vport_fcs_delete(vport);
	}

	bfa_fcs_lport_delete(&fabric->bport);
	bfa_wc_wait(&fabric->wc);
960 961
}

K
Krishna Gudipati 已提交
962 963 964 965 966 967 968
static void
bfa_fcs_fabric_delete_comp(void *cbarg)
{
	struct bfa_fcs_fabric_s *fabric = cbarg;

	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_DELCOMP);
}
969

J
Jing Huang 已提交
970
/*
K
Krishna Gudipati 已提交
971 972 973
 *  fcs_fabric_public fabric public functions
 */

J
Jing Huang 已提交
974
/*
K
Krishna Gudipati 已提交
975 976 977 978 979 980 981 982
 * Attach time initialization.
 */
void
bfa_fcs_fabric_attach(struct bfa_fcs_s *fcs)
{
	struct bfa_fcs_fabric_s *fabric;

	fabric = &fcs->fabric;
983
	memset(fabric, 0, sizeof(struct bfa_fcs_fabric_s));
K
Krishna Gudipati 已提交
984

J
Jing Huang 已提交
985
	/*
K
Krishna Gudipati 已提交
986 987 988 989 990 991
	 * Initialize base fabric.
	 */
	fabric->fcs = fcs;
	INIT_LIST_HEAD(&fabric->vport_q);
	INIT_LIST_HEAD(&fabric->vf_q);
	fabric->lps = bfa_lps_alloc(fcs->bfa);
992
	WARN_ON(!fabric->lps);
K
Krishna Gudipati 已提交
993

J
Jing Huang 已提交
994
	/*
K
Krishna Gudipati 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	 * Initialize fabric delete completion handler. Fabric deletion is
	 * complete when the last vport delete is complete.
	 */
	bfa_wc_init(&fabric->wc, bfa_fcs_fabric_delete_comp, fabric);
	bfa_wc_up(&fabric->wc); /* For the base port */

	bfa_sm_set_state(fabric, bfa_fcs_fabric_sm_uninit);
	bfa_fcs_lport_attach(&fabric->bport, fabric->fcs, FC_VF_ID_NULL, NULL);
}

void
bfa_fcs_fabric_modinit(struct bfa_fcs_s *fcs)
{
	bfa_sm_send_event(&fcs->fabric, BFA_FCS_FABRIC_SM_CREATE);
	bfa_trc(fcs, 0);
}

J
Jing Huang 已提交
1012
/*
K
Krishna Gudipati 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021
 *   Module cleanup
 */
void
bfa_fcs_fabric_modexit(struct bfa_fcs_s *fcs)
{
	struct bfa_fcs_fabric_s *fabric;

	bfa_trc(fcs, 0);

J
Jing Huang 已提交
1022
	/*
K
Krishna Gudipati 已提交
1023 1024 1025 1026 1027 1028 1029
	 * Cleanup base fabric.
	 */
	fabric = &fcs->fabric;
	bfa_lps_delete(fabric->lps);
	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_DELETE);
}

J
Jing Huang 已提交
1030
/*
K
Krishna Gudipati 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
 * Fabric module start -- kick starts FCS actions
 */
void
bfa_fcs_fabric_modstart(struct bfa_fcs_s *fcs)
{
	struct bfa_fcs_fabric_s *fabric;

	bfa_trc(fcs, 0);
	fabric = &fcs->fabric;
	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_START);
}


J
Jing Huang 已提交
1044
/*
K
Krishna Gudipati 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053
 *   Link up notification from BFA physical port module.
 */
void
bfa_fcs_fabric_link_up(struct bfa_fcs_fabric_s *fabric)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LINK_UP);
}

J
Jing Huang 已提交
1054
/*
K
Krishna Gudipati 已提交
1055 1056 1057 1058 1059 1060
 *   Link down notification from BFA physical port module.
 */
void
bfa_fcs_fabric_link_down(struct bfa_fcs_fabric_s *fabric)
{
	bfa_trc(fabric->fcs, fabric->bport.port_cfg.pwwn);
1061
	fabric->fcs->bbscn_flogi_rjt = BFA_FALSE;
K
Krishna Gudipati 已提交
1062 1063 1064
	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LINK_DOWN);
}

J
Jing Huang 已提交
1065
/*
K
Krishna Gudipati 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
 *   A child vport is being created in the fabric.
 *
 *   Call from vport module at vport creation. A list of base port and vports
 *   belonging to a fabric is maintained to propagate link events.
 *
 *   param[in] fabric - Fabric instance. This can be a base fabric or vf.
 *   param[in] vport  - Vport being created.
 *
 *   @return None (always succeeds)
 */
void
bfa_fcs_fabric_addvport(struct bfa_fcs_fabric_s *fabric,
			struct bfa_fcs_vport_s *vport)
{
J
Jing Huang 已提交
1080
	/*
K
Krishna Gudipati 已提交
1081 1082 1083 1084 1085 1086 1087 1088 1089
	 * - add vport to fabric's vport_q
	 */
	bfa_trc(fabric->fcs, fabric->vf_id);

	list_add_tail(&vport->qe, &fabric->vport_q);
	fabric->num_vports++;
	bfa_wc_up(&fabric->wc);
}

J
Jing Huang 已提交
1090
/*
K
Krishna Gudipati 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
 *   A child vport is being deleted from fabric.
 *
 *   Vport is being deleted.
 */
void
bfa_fcs_fabric_delvport(struct bfa_fcs_fabric_s *fabric,
			struct bfa_fcs_vport_s *vport)
{
	list_del(&vport->qe);
	fabric->num_vports--;
	bfa_wc_down(&fabric->wc);
}


J
Jing Huang 已提交
1105
/*
L
Lucas De Marchi 已提交
1106
 * Lookup for a vport within a fabric given its pwwn
K
Krishna Gudipati 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
 */
struct bfa_fcs_vport_s *
bfa_fcs_fabric_vport_lookup(struct bfa_fcs_fabric_s *fabric, wwn_t pwwn)
{
	struct bfa_fcs_vport_s *vport;
	struct list_head	      *qe;

	list_for_each(qe, &fabric->vport_q) {
		vport = (struct bfa_fcs_vport_s *) qe;
		if (bfa_fcs_lport_get_pwwn(&vport->lport) == pwwn)
			return vport;
	}

	return NULL;
}


/*
 *  Get OUI of the attached switch.
 *
 *  Note : Use of this function should be avoided as much as possible.
 *         This function should be used only if there is any requirement
*          to check for FOS version below 6.3.
 *         To check if the attached fabric is a brocade fabric, use
 *         bfa_lps_is_brcd_fabric() which works for FOS versions 6.3
 *         or above only.
 */

u16
bfa_fcs_fabric_get_switch_oui(struct bfa_fcs_fabric_s *fabric)
{
	wwn_t fab_nwwn;
	u8 *tmp;
	u16 oui;

1142
	fab_nwwn = fabric->lps->pr_nwwn;
K
Krishna Gudipati 已提交
1143 1144 1145 1146 1147 1148

	tmp = (u8 *)&fab_nwwn;
	oui = (tmp[3] << 8) | tmp[4];

	return oui;
}
J
Jing Huang 已提交
1149
/*
K
Krishna Gudipati 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
 *		Unsolicited frame receive handling.
 */
void
bfa_fcs_fabric_uf_recv(struct bfa_fcs_fabric_s *fabric, struct fchs_s *fchs,
		       u16 len)
{
	u32	pid = fchs->d_id;
	struct bfa_fcs_vport_s *vport;
	struct list_head	      *qe;
	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
	struct fc_logi_s *flogi = (struct fc_logi_s *) els_cmd;

	bfa_trc(fabric->fcs, len);
	bfa_trc(fabric->fcs, pid);

J
Jing Huang 已提交
1165
	/*
K
Krishna Gudipati 已提交
1166 1167 1168 1169
	 * Look for our own FLOGI frames being looped back. This means an
	 * external loopback cable is in place. Our own FLOGI frames are
	 * sometimes looped back when switch port gets temporarily bypassed.
	 */
M
Maggie Zhang 已提交
1170
	if ((pid == bfa_ntoh3b(FC_FABRIC_PORT)) &&
K
Krishna Gudipati 已提交
1171 1172 1173 1174 1175 1176
	    (els_cmd->els_code == FC_ELS_FLOGI) &&
	    (flogi->port_name == bfa_fcs_lport_get_pwwn(&fabric->bport))) {
		bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_LOOPBACK);
		return;
	}

J
Jing Huang 已提交
1177
	/*
K
Krishna Gudipati 已提交
1178 1179
	 * FLOGI/EVFP exchanges should be consumed by base fabric.
	 */
M
Maggie Zhang 已提交
1180
	if (fchs->d_id == bfa_hton3b(FC_FABRIC_PORT)) {
K
Krishna Gudipati 已提交
1181 1182 1183 1184 1185 1186
		bfa_trc(fabric->fcs, pid);
		bfa_fcs_fabric_process_uf(fabric, fchs, len);
		return;
	}

	if (fabric->bport.pid == pid) {
J
Jing Huang 已提交
1187
		/*
K
Krishna Gudipati 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
		 * All authentication frames should be routed to auth
		 */
		bfa_trc(fabric->fcs, els_cmd->els_code);
		if (els_cmd->els_code == FC_ELS_AUTH) {
			bfa_trc(fabric->fcs, els_cmd->els_code);
			return;
		}

		bfa_trc(fabric->fcs, *(u8 *) ((u8 *) fchs));
		bfa_fcs_lport_uf_recv(&fabric->bport, fchs, len);
		return;
	}

J
Jing Huang 已提交
1201
	/*
K
Krishna Gudipati 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	 * look for a matching local port ID
	 */
	list_for_each(qe, &fabric->vport_q) {
		vport = (struct bfa_fcs_vport_s *) qe;
		if (vport->lport.pid == pid) {
			bfa_fcs_lport_uf_recv(&vport->lport, fchs, len);
			return;
		}
	}
	bfa_trc(fabric->fcs, els_cmd->els_code);
	bfa_fcs_lport_uf_recv(&fabric->bport, fchs, len);
}

J
Jing Huang 已提交
1215
/*
K
Krishna Gudipati 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
 *		Unsolicited frames to be processed by fabric.
 */
static void
bfa_fcs_fabric_process_uf(struct bfa_fcs_fabric_s *fabric, struct fchs_s *fchs,
			  u16 len)
{
	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);

	bfa_trc(fabric->fcs, els_cmd->els_code);

	switch (els_cmd->els_code) {
	case FC_ELS_FLOGI:
		bfa_fcs_fabric_process_flogi(fabric, fchs, len);
		break;

	default:
		/*
		 * need to generate a LS_RJT
		 */
		break;
	}
}

J
Jing Huang 已提交
1239
/*
K
Krishna Gudipati 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
 *	Process	incoming FLOGI
 */
static void
bfa_fcs_fabric_process_flogi(struct bfa_fcs_fabric_s *fabric,
			struct fchs_s *fchs, u16 len)
{
	struct fc_logi_s *flogi = (struct fc_logi_s *) (fchs + 1);
	struct bfa_fcs_lport_s *bport = &fabric->bport;

	bfa_trc(fabric->fcs, fchs->s_id);

	fabric->stats.flogi_rcvd++;
	/*
	 * Check port type. It should be 0 = n-port.
	 */
	if (flogi->csp.port_type) {
		/*
		 * @todo: may need to send a LS_RJT
		 */
		bfa_trc(fabric->fcs, flogi->port_name);
		fabric->stats.flogi_rejected++;
		return;
	}

1264
	fabric->bb_credit = be16_to_cpu(flogi->csp.bbcred);
1265
	fabric->lps->pr_bbscn = (be16_to_cpu(flogi->csp.rxsz) >> 12);
K
Krishna Gudipati 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
	bport->port_topo.pn2n.rem_port_wwn = flogi->port_name;
	bport->port_topo.pn2n.reply_oxid = fchs->ox_id;

	/*
	 * Send a Flogi Acc
	 */
	bfa_fcs_fabric_send_flogi_acc(fabric);
	bfa_sm_send_event(fabric, BFA_FCS_FABRIC_SM_NO_FABRIC);
}

static void
bfa_fcs_fabric_send_flogi_acc(struct bfa_fcs_fabric_s *fabric)
{
	struct bfa_lport_cfg_s *pcfg = &fabric->bport.port_cfg;
	struct bfa_fcs_lport_n2n_s *n2n_port = &fabric->bport.port_topo.pn2n;
	struct bfa_s	  *bfa = fabric->fcs->bfa;
	struct bfa_fcxp_s *fcxp;
	u16	reqlen;
	struct fchs_s	fchs;

	fcxp = bfa_fcs_fcxp_alloc(fabric->fcs);
J
Jing Huang 已提交
1287
	/*
K
Krishna Gudipati 已提交
1288 1289 1290 1291 1292 1293
	 * Do not expect this failure -- expect remote node to retry
	 */
	if (!fcxp)
		return;

	reqlen = fc_flogi_acc_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
M
Maggie Zhang 已提交
1294
				    bfa_hton3b(FC_FABRIC_PORT),
K
Krishna Gudipati 已提交
1295 1296 1297
				    n2n_port->reply_oxid, pcfg->pwwn,
				    pcfg->nwwn,
				    bfa_fcport_get_maxfrsize(bfa),
1298 1299
				    bfa_fcport_get_rx_bbcredit(bfa),
				    bfa_fcs_fabric_oper_bbscn(fabric));
K
Krishna Gudipati 已提交
1300

1301
	bfa_fcxp_send(fcxp, NULL, fabric->vf_id, fabric->lps->lp_tag,
K
Krishna Gudipati 已提交
1302 1303 1304 1305 1306
		      BFA_FALSE, FC_CLASS_3,
		      reqlen, &fchs, bfa_fcs_fabric_flogiacc_comp, fabric,
		      FC_MAX_PDUSZ, 0);
}

J
Jing Huang 已提交
1307
/*
K
Krishna Gudipati 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
 *   Flogi Acc completion callback.
 */
static void
bfa_fcs_fabric_flogiacc_comp(void *fcsarg, struct bfa_fcxp_s *fcxp, void *cbarg,
			     bfa_status_t status, u32 rsp_len,
			     u32 resid_len, struct fchs_s *rspfchs)
{
	struct bfa_fcs_fabric_s *fabric = cbarg;

	bfa_trc(fabric->fcs, status);
}

/*
 *
 * @param[in] fabric - fabric
 * @param[in] wwn_t - new fabric name
 *
 * @return - none
 */
void
bfa_fcs_fabric_set_fabric_name(struct bfa_fcs_fabric_s *fabric,
			       wwn_t fabric_name)
{
	struct bfad_s *bfad = (struct bfad_s *)fabric->fcs->bfad;
	char	pwwn_ptr[BFA_STRING_32];
	char	fwwn_ptr[BFA_STRING_32];

	bfa_trc(fabric->fcs, fabric_name);

	if (fabric->fabric_name == 0) {
		/*
		 * With BRCD switches, we don't get Fabric Name in FLOGI.
		 * Don't generate a fabric name change event in this case.
		 */
		fabric->fabric_name = fabric_name;
	} else {
		fabric->fabric_name = fabric_name;
		wwn2str(pwwn_ptr, bfa_fcs_lport_get_pwwn(&fabric->bport));
		wwn2str(fwwn_ptr,
			bfa_fcs_lport_get_fabric_name(&fabric->bport));
1348
		BFA_LOG(KERN_WARNING, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
1349 1350 1351 1352 1353
			"Base port WWN = %s Fabric WWN = %s\n",
			pwwn_ptr, fwwn_ptr);
	}
}

J
Jing Huang 已提交
1354
/*
K
Krishna Gudipati 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
 *	Returns FCS vf structure for a given vf_id.
 *
 *	param[in]	vf_id - VF_ID
 *
 *	return
 *	If lookup succeeds, retuns fcs vf object, otherwise returns NULL
 */
bfa_fcs_vf_t   *
bfa_fcs_vf_lookup(struct bfa_fcs_s *fcs, u16 vf_id)
{
	bfa_trc(fcs, vf_id);
	if (vf_id == FC_VF_ID_NULL)
		return &fcs->fabric;

	return NULL;
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
}

/*
 *	Return the list of local logical ports present in the given VF.
 *
 *	@param[in]	vf	vf for which logical ports are returned
 *	@param[out]	lpwwn	returned logical port wwn list
 *	@param[in,out]	nlports in:size of lpwwn list;
 *				out:total elements present,
 *				actual elements returned is limited by the size
 */
void
bfa_fcs_vf_get_ports(bfa_fcs_vf_t *vf, wwn_t lpwwn[], int *nlports)
{
	struct list_head *qe;
	struct bfa_fcs_vport_s *vport;
	int	i = 0;
	struct bfa_fcs_s	*fcs;

	if (vf == NULL || lpwwn == NULL || *nlports == 0)
		return;

	fcs = vf->fcs;

	bfa_trc(fcs, vf->vf_id);
	bfa_trc(fcs, (uint32_t) *nlports);

	lpwwn[i++] = vf->bport.port_cfg.pwwn;

	list_for_each(qe, &vf->vport_q) {
		if (i >= *nlports)
			break;

		vport = (struct bfa_fcs_vport_s *) qe;
		lpwwn[i++] = vport->lport.port_cfg.pwwn;
	}

	bfa_trc(fcs, i);
	*nlports = i;
K
Krishna Gudipati 已提交
1409 1410
}

J
Jing Huang 已提交
1411
/*
K
Krishna Gudipati 已提交
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
 * BFA FCS PPORT ( physical port)
 */
static void
bfa_fcs_port_event_handler(void *cbarg, enum bfa_port_linkstate event)
{
	struct bfa_fcs_s      *fcs = cbarg;

	bfa_trc(fcs, event);

	switch (event) {
	case BFA_PORT_LINKUP:
		bfa_fcs_fabric_link_up(&fcs->fabric);
		break;

	case BFA_PORT_LINKDOWN:
		bfa_fcs_fabric_link_down(&fcs->fabric);
		break;

	default:
1431
		WARN_ON(1);
K
Krishna Gudipati 已提交
1432 1433 1434 1435 1436 1437 1438 1439 1440
	}
}

void
bfa_fcs_port_attach(struct bfa_fcs_s *fcs)
{
	bfa_fcport_event_register(fcs->bfa, bfa_fcs_port_event_handler, fcs);
}

J
Jing Huang 已提交
1441
/*
K
Krishna Gudipati 已提交
1442 1443 1444
 * BFA FCS UF ( Unsolicited Frames)
 */

J
Jing Huang 已提交
1445
/*
K
Krishna Gudipati 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
 *		BFA callback for unsolicited frame receive handler.
 *
 * @param[in]		cbarg		callback arg for receive handler
 * @param[in]		uf		unsolicited frame descriptor
 *
 * @return None
 */
static void
bfa_fcs_uf_recv(void *cbarg, struct bfa_uf_s *uf)
{
	struct bfa_fcs_s	*fcs = (struct bfa_fcs_s *) cbarg;
	struct fchs_s	*fchs = bfa_uf_get_frmbuf(uf);
	u16	len = bfa_uf_get_frmlen(uf);
	struct fc_vft_s *vft;
	struct bfa_fcs_fabric_s *fabric;

J
Jing Huang 已提交
1462
	/*
K
Krishna Gudipati 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
	 * check for VFT header
	 */
	if (fchs->routing == FC_RTG_EXT_HDR &&
	    fchs->cat_info == FC_CAT_VFT_HDR) {
		bfa_stats(fcs, uf.tagged);
		vft = bfa_uf_get_frmbuf(uf);
		if (fcs->port_vfid == vft->vf_id)
			fabric = &fcs->fabric;
		else
			fabric = bfa_fcs_vf_lookup(fcs, (u16) vft->vf_id);

J
Jing Huang 已提交
1474
		/*
K
Krishna Gudipati 已提交
1475 1476 1477
		 * drop frame if vfid is unknown
		 */
		if (!fabric) {
1478
			WARN_ON(1);
K
Krishna Gudipati 已提交
1479 1480 1481 1482 1483
			bfa_stats(fcs, uf.vfid_unknown);
			bfa_uf_free(uf);
			return;
		}

J
Jing Huang 已提交
1484
		/*
K
Krishna Gudipati 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
		 * skip vft header
		 */
		fchs = (struct fchs_s *) (vft + 1);
		len -= sizeof(struct fc_vft_s);

		bfa_trc(fcs, vft->vf_id);
	} else {
		bfa_stats(fcs, uf.untagged);
		fabric = &fcs->fabric;
	}

	bfa_trc(fcs, ((u32 *) fchs)[0]);
	bfa_trc(fcs, ((u32 *) fchs)[1]);
	bfa_trc(fcs, ((u32 *) fchs)[2]);
	bfa_trc(fcs, ((u32 *) fchs)[3]);
	bfa_trc(fcs, ((u32 *) fchs)[4]);
	bfa_trc(fcs, ((u32 *) fchs)[5]);
	bfa_trc(fcs, len);

	bfa_fcs_fabric_uf_recv(fabric, fchs, len);
	bfa_uf_free(uf);
}

void
bfa_fcs_uf_attach(struct bfa_fcs_s *fcs)
{
	bfa_uf_recv_register(fcs->bfa, bfa_fcs_uf_recv, fcs);
}