bfa_svc.c 132.5 KB
Newer Older
K
Krishna Gudipati 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
 * 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.
 */

M
Maggie Zhang 已提交
18
#include "bfad_drv.h"
19
#include "bfad_im.h"
K
Krishna Gudipati 已提交
20 21 22 23 24
#include "bfa_plog.h"
#include "bfa_cs.h"
#include "bfa_modules.h"

BFA_TRC_FILE(HAL, FCXP);
25
BFA_MODULE(fcdiag);
K
Krishna Gudipati 已提交
26 27 28 29 30 31 32
BFA_MODULE(fcxp);
BFA_MODULE(sgpg);
BFA_MODULE(lps);
BFA_MODULE(fcport);
BFA_MODULE(rport);
BFA_MODULE(uf);

J
Jing Huang 已提交
33
/*
K
Krishna Gudipati 已提交
34 35 36 37 38 39 40 41 42 43 44 45
 * LPS related definitions
 */
#define BFA_LPS_MIN_LPORTS      (1)
#define BFA_LPS_MAX_LPORTS      (256)

/*
 * Maximum Vports supported per physical port or vf.
 */
#define BFA_LPS_MAX_VPORTS_SUPP_CB  255
#define BFA_LPS_MAX_VPORTS_SUPP_CT  190


J
Jing Huang 已提交
46
/*
K
Krishna Gudipati 已提交
47 48 49 50 51 52 53 54 55 56
 * FC PORT related definitions
 */
/*
 * The port is considered disabled if corresponding physical port or IOC are
 * disabled explicitly
 */
#define BFA_PORT_IS_DISABLED(bfa) \
	((bfa_fcport_is_disabled(bfa) == BFA_TRUE) || \
	(bfa_ioc_is_disabled(&bfa->ioc) == BFA_TRUE))

J
Jing Huang 已提交
57
/*
K
Krishna Gudipati 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71
 * BFA port state machine events
 */
enum bfa_fcport_sm_event {
	BFA_FCPORT_SM_START	= 1,	/*  start port state machine	*/
	BFA_FCPORT_SM_STOP	= 2,	/*  stop port state machine	*/
	BFA_FCPORT_SM_ENABLE	= 3,	/*  enable port		*/
	BFA_FCPORT_SM_DISABLE	= 4,	/*  disable port state machine */
	BFA_FCPORT_SM_FWRSP	= 5,	/*  firmware enable/disable rsp */
	BFA_FCPORT_SM_LINKUP	= 6,	/*  firmware linkup event	*/
	BFA_FCPORT_SM_LINKDOWN	= 7,	/*  firmware linkup down	*/
	BFA_FCPORT_SM_QRESUME	= 8,	/*  CQ space available	*/
	BFA_FCPORT_SM_HWFAIL	= 9,	/*  IOC h/w failure		*/
};

J
Jing Huang 已提交
72
/*
K
Krishna Gudipati 已提交
73 74 75 76 77 78 79 80 81
 * BFA port link notification state machine events
 */

enum bfa_fcport_ln_sm_event {
	BFA_FCPORT_LN_SM_LINKUP		= 1,	/*  linkup event	*/
	BFA_FCPORT_LN_SM_LINKDOWN	= 2,	/*  linkdown event	*/
	BFA_FCPORT_LN_SM_NOTIFICATION	= 3	/*  done notification	*/
};

J
Jing Huang 已提交
82
/*
K
Krishna Gudipati 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
 * RPORT related definitions
 */
#define bfa_rport_offline_cb(__rp) do {					\
	if ((__rp)->bfa->fcs)						\
		bfa_cb_rport_offline((__rp)->rport_drv);      \
	else {								\
		bfa_cb_queue((__rp)->bfa, &(__rp)->hcb_qe,		\
				__bfa_cb_rport_offline, (__rp));      \
	}								\
} while (0)

#define bfa_rport_online_cb(__rp) do {					\
	if ((__rp)->bfa->fcs)						\
		bfa_cb_rport_online((__rp)->rport_drv);      \
	else {								\
		bfa_cb_queue((__rp)->bfa, &(__rp)->hcb_qe,		\
				  __bfa_cb_rport_online, (__rp));      \
		}							\
} while (0)

J
Jing Huang 已提交
103
/*
K
Krishna Gudipati 已提交
104 105 106 107 108 109 110 111 112 113 114
 * forward declarations FCXP related functions
 */
static void	__bfa_fcxp_send_cbfn(void *cbarg, bfa_boolean_t complete);
static void	hal_fcxp_rx_plog(struct bfa_s *bfa, struct bfa_fcxp_s *fcxp,
				struct bfi_fcxp_send_rsp_s *fcxp_rsp);
static void	hal_fcxp_tx_plog(struct bfa_s *bfa, u32 reqlen,
				struct bfa_fcxp_s *fcxp, struct fchs_s *fchs);
static void	bfa_fcxp_qresume(void *cbarg);
static void	bfa_fcxp_queue(struct bfa_fcxp_s *fcxp,
				struct bfi_fcxp_send_req_s *send_req);

J
Jing Huang 已提交
115
/*
K
Krishna Gudipati 已提交
116 117
 * forward declarations for LPS functions
 */
118 119
static void bfa_lps_meminfo(struct bfa_iocfc_cfg_s *cfg,
		struct bfa_meminfo_s *minfo, struct bfa_s *bfa);
K
Krishna Gudipati 已提交
120 121 122 123 124 125 126 127 128
static void bfa_lps_attach(struct bfa_s *bfa, void *bfad,
				struct bfa_iocfc_cfg_s *cfg,
				struct bfa_pcidev_s *pcidev);
static void bfa_lps_detach(struct bfa_s *bfa);
static void bfa_lps_start(struct bfa_s *bfa);
static void bfa_lps_stop(struct bfa_s *bfa);
static void bfa_lps_iocdisable(struct bfa_s *bfa);
static void bfa_lps_login_rsp(struct bfa_s *bfa,
				struct bfi_lps_login_rsp_s *rsp);
129
static void bfa_lps_no_res(struct bfa_lps_s *first_lps, u8 count);
K
Krishna Gudipati 已提交
130 131 132 133 134 135
static void bfa_lps_logout_rsp(struct bfa_s *bfa,
				struct bfi_lps_logout_rsp_s *rsp);
static void bfa_lps_reqq_resume(void *lps_arg);
static void bfa_lps_free(struct bfa_lps_s *lps);
static void bfa_lps_send_login(struct bfa_lps_s *lps);
static void bfa_lps_send_logout(struct bfa_lps_s *lps);
136
static void bfa_lps_send_set_n2n_pid(struct bfa_lps_s *lps);
K
Krishna Gudipati 已提交
137 138 139 140
static void bfa_lps_login_comp(struct bfa_lps_s *lps);
static void bfa_lps_logout_comp(struct bfa_lps_s *lps);
static void bfa_lps_cvl_event(struct bfa_lps_s *lps);

J
Jing Huang 已提交
141
/*
K
Krishna Gudipati 已提交
142 143 144 145 146 147 148
 * forward declaration for LPS state machine
 */
static void bfa_lps_sm_init(struct bfa_lps_s *lps, enum bfa_lps_event event);
static void bfa_lps_sm_login(struct bfa_lps_s *lps, enum bfa_lps_event event);
static void bfa_lps_sm_loginwait(struct bfa_lps_s *lps, enum bfa_lps_event
					event);
static void bfa_lps_sm_online(struct bfa_lps_s *lps, enum bfa_lps_event event);
149 150
static void bfa_lps_sm_online_n2n_pid_wait(struct bfa_lps_s *lps,
					enum bfa_lps_event event);
K
Krishna Gudipati 已提交
151 152 153 154
static void bfa_lps_sm_logout(struct bfa_lps_s *lps, enum bfa_lps_event event);
static void bfa_lps_sm_logowait(struct bfa_lps_s *lps, enum bfa_lps_event
					event);

J
Jing Huang 已提交
155
/*
K
Krishna Gudipati 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
 * forward declaration for FC Port functions
 */
static bfa_boolean_t bfa_fcport_send_enable(struct bfa_fcport_s *fcport);
static bfa_boolean_t bfa_fcport_send_disable(struct bfa_fcport_s *fcport);
static void bfa_fcport_update_linkinfo(struct bfa_fcport_s *fcport);
static void bfa_fcport_reset_linkinfo(struct bfa_fcport_s *fcport);
static void bfa_fcport_set_wwns(struct bfa_fcport_s *fcport);
static void __bfa_cb_fcport_event(void *cbarg, bfa_boolean_t complete);
static void bfa_fcport_scn(struct bfa_fcport_s *fcport,
			enum bfa_port_linkstate event, bfa_boolean_t trunk);
static void bfa_fcport_queue_cb(struct bfa_fcport_ln_s *ln,
				enum bfa_port_linkstate event);
static void __bfa_cb_fcport_stats_clr(void *cbarg, bfa_boolean_t complete);
static void bfa_fcport_stats_get_timeout(void *cbarg);
static void bfa_fcport_stats_clr_timeout(void *cbarg);
static void bfa_trunk_iocdisable(struct bfa_s *bfa);

J
Jing Huang 已提交
173
/*
K
Krishna Gudipati 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 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 223 224 225 226 227 228 229 230 231
 * forward declaration for FC PORT state machine
 */
static void     bfa_fcport_sm_uninit(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_enabling_qwait(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_enabling(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_linkdown(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_linkup(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_disabling(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_disabling_qwait(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_toggling_qwait(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_disabled(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_stopped(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_iocdown(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);
static void     bfa_fcport_sm_iocfail(struct bfa_fcport_s *fcport,
					enum bfa_fcport_sm_event event);

static void     bfa_fcport_ln_sm_dn(struct bfa_fcport_ln_s *ln,
					enum bfa_fcport_ln_sm_event event);
static void     bfa_fcport_ln_sm_dn_nf(struct bfa_fcport_ln_s *ln,
					enum bfa_fcport_ln_sm_event event);
static void     bfa_fcport_ln_sm_dn_up_nf(struct bfa_fcport_ln_s *ln,
					enum bfa_fcport_ln_sm_event event);
static void     bfa_fcport_ln_sm_up(struct bfa_fcport_ln_s *ln,
					enum bfa_fcport_ln_sm_event event);
static void     bfa_fcport_ln_sm_up_nf(struct bfa_fcport_ln_s *ln,
					enum bfa_fcport_ln_sm_event event);
static void     bfa_fcport_ln_sm_up_dn_nf(struct bfa_fcport_ln_s *ln,
					enum bfa_fcport_ln_sm_event event);
static void     bfa_fcport_ln_sm_up_dn_up_nf(struct bfa_fcport_ln_s *ln,
					enum bfa_fcport_ln_sm_event event);

static struct bfa_sm_table_s hal_port_sm_table[] = {
	{BFA_SM(bfa_fcport_sm_uninit), BFA_PORT_ST_UNINIT},
	{BFA_SM(bfa_fcport_sm_enabling_qwait), BFA_PORT_ST_ENABLING_QWAIT},
	{BFA_SM(bfa_fcport_sm_enabling), BFA_PORT_ST_ENABLING},
	{BFA_SM(bfa_fcport_sm_linkdown), BFA_PORT_ST_LINKDOWN},
	{BFA_SM(bfa_fcport_sm_linkup), BFA_PORT_ST_LINKUP},
	{BFA_SM(bfa_fcport_sm_disabling_qwait), BFA_PORT_ST_DISABLING_QWAIT},
	{BFA_SM(bfa_fcport_sm_toggling_qwait), BFA_PORT_ST_TOGGLING_QWAIT},
	{BFA_SM(bfa_fcport_sm_disabling), BFA_PORT_ST_DISABLING},
	{BFA_SM(bfa_fcport_sm_disabled), BFA_PORT_ST_DISABLED},
	{BFA_SM(bfa_fcport_sm_stopped), BFA_PORT_ST_STOPPED},
	{BFA_SM(bfa_fcport_sm_iocdown), BFA_PORT_ST_IOCDOWN},
	{BFA_SM(bfa_fcport_sm_iocfail), BFA_PORT_ST_IOCDOWN},
};


J
Jing Huang 已提交
232
/*
K
Krishna Gudipati 已提交
233 234 235 236 237 238 239 240 241 242 243 244
 * forward declaration for RPORT related functions
 */
static struct bfa_rport_s *bfa_rport_alloc(struct bfa_rport_mod_s *rp_mod);
static void		bfa_rport_free(struct bfa_rport_s *rport);
static bfa_boolean_t	bfa_rport_send_fwcreate(struct bfa_rport_s *rp);
static bfa_boolean_t	bfa_rport_send_fwdelete(struct bfa_rport_s *rp);
static bfa_boolean_t	bfa_rport_send_fwspeed(struct bfa_rport_s *rp);
static void		__bfa_cb_rport_online(void *cbarg,
						bfa_boolean_t complete);
static void		__bfa_cb_rport_offline(void *cbarg,
						bfa_boolean_t complete);

J
Jing Huang 已提交
245
/*
K
Krishna Gudipati 已提交
246 247 248 249 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
 * forward declaration for RPORT state machine
 */
static void     bfa_rport_sm_uninit(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_created(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_fwcreate(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_online(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_fwdelete(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_offline(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_deleting(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_offline_pending(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_delete_pending(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_iocdisable(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_fwcreate_qfull(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_fwdelete_qfull(struct bfa_rport_s *rp,
					enum bfa_rport_event event);
static void     bfa_rport_sm_deleting_qfull(struct bfa_rport_s *rp,
					enum bfa_rport_event event);

J
Jing Huang 已提交
275
/*
K
Krishna Gudipati 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
 * PLOG related definitions
 */
static int
plkd_validate_logrec(struct bfa_plog_rec_s *pl_rec)
{
	if ((pl_rec->log_type != BFA_PL_LOG_TYPE_INT) &&
		(pl_rec->log_type != BFA_PL_LOG_TYPE_STRING))
		return 1;

	if ((pl_rec->log_type != BFA_PL_LOG_TYPE_INT) &&
		(pl_rec->log_num_ints > BFA_PL_INT_LOG_SZ))
		return 1;

	return 0;
}

M
Maggie Zhang 已提交
292 293 294 295 296 297 298 299 300 301 302 303
static u64
bfa_get_log_time(void)
{
	u64 system_time = 0;
	struct timeval tv;
	do_gettimeofday(&tv);

	/* We are interested in seconds only. */
	system_time = tv.tv_sec;
	return system_time;
}

K
Krishna Gudipati 已提交
304 305 306 307 308 309 310 311 312 313
static void
bfa_plog_add(struct bfa_plog_s *plog, struct bfa_plog_rec_s *pl_rec)
{
	u16 tail;
	struct bfa_plog_rec_s *pl_recp;

	if (plog->plog_enabled == 0)
		return;

	if (plkd_validate_logrec(pl_rec)) {
314
		WARN_ON(1);
K
Krishna Gudipati 已提交
315 316 317 318 319 320 321
		return;
	}

	tail = plog->tail;

	pl_recp = &(plog->plog_recs[tail]);

322
	memcpy(pl_recp, pl_rec, sizeof(struct bfa_plog_rec_s));
K
Krishna Gudipati 已提交
323

M
Maggie Zhang 已提交
324
	pl_recp->tv = bfa_get_log_time();
K
Krishna Gudipati 已提交
325 326 327 328 329 330 331 332 333
	BFA_PL_LOG_REC_INCR(plog->tail);

	if (plog->head == plog->tail)
		BFA_PL_LOG_REC_INCR(plog->head);
}

void
bfa_plog_init(struct bfa_plog_s *plog)
{
334
	memset((char *)plog, 0, sizeof(struct bfa_plog_s));
K
Krishna Gudipati 已提交
335

336
	memcpy(plog->plog_sig, BFA_PL_SIG_STR, BFA_PL_SIG_LEN);
K
Krishna Gudipati 已提交
337 338 339 340 341 342 343 344 345 346 347 348
	plog->head = plog->tail = 0;
	plog->plog_enabled = 1;
}

void
bfa_plog_str(struct bfa_plog_s *plog, enum bfa_plog_mid mid,
		enum bfa_plog_eid event,
		u16 misc, char *log_str)
{
	struct bfa_plog_rec_s  lp;

	if (plog->plog_enabled) {
349
		memset(&lp, 0, sizeof(struct bfa_plog_rec_s));
K
Krishna Gudipati 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
		lp.mid = mid;
		lp.eid = event;
		lp.log_type = BFA_PL_LOG_TYPE_STRING;
		lp.misc = misc;
		strncpy(lp.log_entry.string_log, log_str,
			BFA_PL_STRING_LOG_SZ - 1);
		lp.log_entry.string_log[BFA_PL_STRING_LOG_SZ - 1] = '\0';
		bfa_plog_add(plog, &lp);
	}
}

void
bfa_plog_intarr(struct bfa_plog_s *plog, enum bfa_plog_mid mid,
		enum bfa_plog_eid event,
		u16 misc, u32 *intarr, u32 num_ints)
{
	struct bfa_plog_rec_s  lp;
	u32 i;

	if (num_ints > BFA_PL_INT_LOG_SZ)
		num_ints = BFA_PL_INT_LOG_SZ;

	if (plog->plog_enabled) {
373
		memset(&lp, 0, sizeof(struct bfa_plog_rec_s));
K
Krishna Gudipati 已提交
374 375 376 377 378 379
		lp.mid = mid;
		lp.eid = event;
		lp.log_type = BFA_PL_LOG_TYPE_INT;
		lp.misc = misc;

		for (i = 0; i < num_ints; i++)
380
			lp.log_entry.int_log[i] = intarr[i];
K
Krishna Gudipati 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

		lp.log_num_ints = (u8) num_ints;

		bfa_plog_add(plog, &lp);
	}
}

void
bfa_plog_fchdr(struct bfa_plog_s *plog, enum bfa_plog_mid mid,
			enum bfa_plog_eid event,
			u16 misc, struct fchs_s *fchdr)
{
	struct bfa_plog_rec_s  lp;
	u32	*tmp_int = (u32 *) fchdr;
	u32	ints[BFA_PL_INT_LOG_SZ];

	if (plog->plog_enabled) {
398
		memset(&lp, 0, sizeof(struct bfa_plog_rec_s));
K
Krishna Gudipati 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

		ints[0] = tmp_int[0];
		ints[1] = tmp_int[1];
		ints[2] = tmp_int[4];

		bfa_plog_intarr(plog, mid, event, misc, ints, 3);
	}
}

void
bfa_plog_fchdr_and_pl(struct bfa_plog_s *plog, enum bfa_plog_mid mid,
		      enum bfa_plog_eid event, u16 misc, struct fchs_s *fchdr,
		      u32 pld_w0)
{
	struct bfa_plog_rec_s  lp;
	u32	*tmp_int = (u32 *) fchdr;
	u32	ints[BFA_PL_INT_LOG_SZ];

	if (plog->plog_enabled) {
418
		memset(&lp, 0, sizeof(struct bfa_plog_rec_s));
K
Krishna Gudipati 已提交
419 420 421 422 423 424 425 426 427 428 429

		ints[0] = tmp_int[0];
		ints[1] = tmp_int[1];
		ints[2] = tmp_int[4];
		ints[3] = pld_w0;

		bfa_plog_intarr(plog, mid, event, misc, ints, 4);
	}
}


J
Jing Huang 已提交
430
/*
K
Krishna Gudipati 已提交
431 432 433 434
 *  fcxp_pvt BFA FCXP private functions
 */

static void
435
claim_fcxps_mem(struct bfa_fcxp_mod_s *mod)
K
Krishna Gudipati 已提交
436 437 438 439
{
	u16	i;
	struct bfa_fcxp_s *fcxp;

440
	fcxp = (struct bfa_fcxp_s *) bfa_mem_kva_curp(mod);
441
	memset(fcxp, 0, sizeof(struct bfa_fcxp_s) * mod->num_fcxps);
K
Krishna Gudipati 已提交
442 443 444

	INIT_LIST_HEAD(&mod->fcxp_free_q);
	INIT_LIST_HEAD(&mod->fcxp_active_q);
445
	INIT_LIST_HEAD(&mod->fcxp_unused_q);
K
Krishna Gudipati 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459

	mod->fcxp_list = fcxp;

	for (i = 0; i < mod->num_fcxps; i++) {
		fcxp->fcxp_mod = mod;
		fcxp->fcxp_tag = i;

		list_add_tail(&fcxp->qe, &mod->fcxp_free_q);
		bfa_reqq_winit(&fcxp->reqq_wqe, bfa_fcxp_qresume, fcxp);
		fcxp->reqq_waiting = BFA_FALSE;

		fcxp = fcxp + 1;
	}

460
	bfa_mem_kva_curp(mod) = (void *)fcxp;
K
Krishna Gudipati 已提交
461 462 463
}

static void
464 465
bfa_fcxp_meminfo(struct bfa_iocfc_cfg_s *cfg, struct bfa_meminfo_s *minfo,
		struct bfa_s *bfa)
K
Krishna Gudipati 已提交
466
{
467 468 469 470 471 472
	struct bfa_fcxp_mod_s *fcxp_mod = BFA_FCXP_MOD(bfa);
	struct bfa_mem_kva_s *fcxp_kva = BFA_MEM_FCXP_KVA(bfa);
	struct bfa_mem_dma_s *seg_ptr;
	u16	nsegs, idx, per_seg_fcxp;
	u16	num_fcxps = cfg->fwcfg.num_fcxp_reqs;
	u32	per_fcxp_sz;
K
Krishna Gudipati 已提交
473

474
	if (num_fcxps == 0)
K
Krishna Gudipati 已提交
475 476 477
		return;

	if (cfg->drvcfg.min_cfg)
478
		per_fcxp_sz = 2 * BFA_FCXP_MAX_IBUF_SZ;
K
Krishna Gudipati 已提交
479
	else
480
		per_fcxp_sz = BFA_FCXP_MAX_IBUF_SZ + BFA_FCXP_MAX_LBUF_SZ;
K
Krishna Gudipati 已提交
481

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	/* dma memory */
	nsegs = BFI_MEM_DMA_NSEGS(num_fcxps, per_fcxp_sz);
	per_seg_fcxp = BFI_MEM_NREQS_SEG(per_fcxp_sz);

	bfa_mem_dma_seg_iter(fcxp_mod, seg_ptr, nsegs, idx) {
		if (num_fcxps >= per_seg_fcxp) {
			num_fcxps -= per_seg_fcxp;
			bfa_mem_dma_setup(minfo, seg_ptr,
				per_seg_fcxp * per_fcxp_sz);
		} else
			bfa_mem_dma_setup(minfo, seg_ptr,
				num_fcxps * per_fcxp_sz);
	}

	/* kva memory */
	bfa_mem_kva_setup(minfo, fcxp_kva,
		cfg->fwcfg.num_fcxp_reqs * sizeof(struct bfa_fcxp_s));
K
Krishna Gudipati 已提交
499 500 501 502
}

static void
bfa_fcxp_attach(struct bfa_s *bfa, void *bfad, struct bfa_iocfc_cfg_s *cfg,
503
		struct bfa_pcidev_s *pcidev)
K
Krishna Gudipati 已提交
504 505 506 507 508 509
{
	struct bfa_fcxp_mod_s *mod = BFA_FCXP_MOD(bfa);

	mod->bfa = bfa;
	mod->num_fcxps = cfg->fwcfg.num_fcxp_reqs;

J
Jing Huang 已提交
510
	/*
K
Krishna Gudipati 已提交
511 512 513 514 515 516 517 518
	 * Initialize FCXP request and response payload sizes.
	 */
	mod->req_pld_sz = mod->rsp_pld_sz = BFA_FCXP_MAX_IBUF_SZ;
	if (!cfg->drvcfg.min_cfg)
		mod->rsp_pld_sz = BFA_FCXP_MAX_LBUF_SZ;

	INIT_LIST_HEAD(&mod->wait_q);

519
	claim_fcxps_mem(mod);
K
Krishna Gudipati 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
}

static void
bfa_fcxp_detach(struct bfa_s *bfa)
{
}

static void
bfa_fcxp_start(struct bfa_s *bfa)
{
}

static void
bfa_fcxp_stop(struct bfa_s *bfa)
{
}

static void
bfa_fcxp_iocdisable(struct bfa_s *bfa)
{
	struct bfa_fcxp_mod_s *mod = BFA_FCXP_MOD(bfa);
	struct bfa_fcxp_s *fcxp;
	struct list_head	      *qe, *qen;

544 545 546
	/* Enqueue unused fcxp resources to free_q */
	list_splice_tail_init(&mod->fcxp_unused_q, &mod->fcxp_free_q);

K
Krishna Gudipati 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	list_for_each_safe(qe, qen, &mod->fcxp_active_q) {
		fcxp = (struct bfa_fcxp_s *) qe;
		if (fcxp->caller == NULL) {
			fcxp->send_cbfn(fcxp->caller, fcxp, fcxp->send_cbarg,
					BFA_STATUS_IOC_FAILURE, 0, 0, NULL);
			bfa_fcxp_free(fcxp);
		} else {
			fcxp->rsp_status = BFA_STATUS_IOC_FAILURE;
			bfa_cb_queue(bfa, &fcxp->hcb_qe,
				     __bfa_fcxp_send_cbfn, fcxp);
		}
	}
}

static struct bfa_fcxp_s *
bfa_fcxp_get(struct bfa_fcxp_mod_s *fm)
{
	struct bfa_fcxp_s *fcxp;

	bfa_q_deq(&fm->fcxp_free_q, &fcxp);

	if (fcxp)
		list_add_tail(&fcxp->qe, &fm->fcxp_active_q);

	return fcxp;
}

static void
bfa_fcxp_init_reqrsp(struct bfa_fcxp_s *fcxp,
	       struct bfa_s *bfa,
	       u8 *use_ibuf,
	       u32 *nr_sgles,
	       bfa_fcxp_get_sgaddr_t *r_sga_cbfn,
	       bfa_fcxp_get_sglen_t *r_sglen_cbfn,
	       struct list_head *r_sgpg_q,
	       int n_sgles,
	       bfa_fcxp_get_sgaddr_t sga_cbfn,
	       bfa_fcxp_get_sglen_t sglen_cbfn)
{

587
	WARN_ON(bfa == NULL);
K
Krishna Gudipati 已提交
588 589 590 591 592 593

	bfa_trc(bfa, fcxp->fcxp_tag);

	if (n_sgles == 0) {
		*use_ibuf = 1;
	} else {
594 595
		WARN_ON(*sga_cbfn == NULL);
		WARN_ON(*sglen_cbfn == NULL);
K
Krishna Gudipati 已提交
596 597 598 599 600 601 602 603 604 605 606

		*use_ibuf = 0;
		*r_sga_cbfn = sga_cbfn;
		*r_sglen_cbfn = sglen_cbfn;

		*nr_sgles = n_sgles;

		/*
		 * alloc required sgpgs
		 */
		if (n_sgles > BFI_SGE_INLINE)
607
			WARN_ON(1);
K
Krishna Gudipati 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620
	}

}

static void
bfa_fcxp_init(struct bfa_fcxp_s *fcxp,
	       void *caller, struct bfa_s *bfa, int nreq_sgles,
	       int nrsp_sgles, bfa_fcxp_get_sgaddr_t req_sga_cbfn,
	       bfa_fcxp_get_sglen_t req_sglen_cbfn,
	       bfa_fcxp_get_sgaddr_t rsp_sga_cbfn,
	       bfa_fcxp_get_sglen_t rsp_sglen_cbfn)
{

621
	WARN_ON(bfa == NULL);
K
Krishna Gudipati 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

	bfa_trc(bfa, fcxp->fcxp_tag);

	fcxp->caller = caller;

	bfa_fcxp_init_reqrsp(fcxp, bfa,
		&fcxp->use_ireqbuf, &fcxp->nreq_sgles, &fcxp->req_sga_cbfn,
		&fcxp->req_sglen_cbfn, &fcxp->req_sgpg_q,
		nreq_sgles, req_sga_cbfn, req_sglen_cbfn);

	bfa_fcxp_init_reqrsp(fcxp, bfa,
		&fcxp->use_irspbuf, &fcxp->nrsp_sgles, &fcxp->rsp_sga_cbfn,
		&fcxp->rsp_sglen_cbfn, &fcxp->rsp_sgpg_q,
		nrsp_sgles, rsp_sga_cbfn, rsp_sglen_cbfn);

}

static void
bfa_fcxp_put(struct bfa_fcxp_s *fcxp)
{
	struct bfa_fcxp_mod_s *mod = fcxp->fcxp_mod;
	struct bfa_fcxp_wqe_s *wqe;

	bfa_q_deq(&mod->wait_q, &wqe);
	if (wqe) {
		bfa_trc(mod->bfa, fcxp->fcxp_tag);

		bfa_fcxp_init(fcxp, wqe->caller, wqe->bfa, wqe->nreq_sgles,
			wqe->nrsp_sgles, wqe->req_sga_cbfn,
			wqe->req_sglen_cbfn, wqe->rsp_sga_cbfn,
			wqe->rsp_sglen_cbfn);

		wqe->alloc_cbfn(wqe->alloc_cbarg, fcxp);
		return;
	}

658
	WARN_ON(!bfa_q_is_on_q(&mod->fcxp_active_q, fcxp));
K
Krishna Gudipati 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
	list_del(&fcxp->qe);
	list_add_tail(&fcxp->qe, &mod->fcxp_free_q);
}

static void
bfa_fcxp_null_comp(void *bfad_fcxp, struct bfa_fcxp_s *fcxp, void *cbarg,
		   bfa_status_t req_status, u32 rsp_len,
		   u32 resid_len, struct fchs_s *rsp_fchs)
{
	/* discarded fcxp completion */
}

static void
__bfa_fcxp_send_cbfn(void *cbarg, bfa_boolean_t complete)
{
	struct bfa_fcxp_s *fcxp = cbarg;

	if (complete) {
		fcxp->send_cbfn(fcxp->caller, fcxp, fcxp->send_cbarg,
				fcxp->rsp_status, fcxp->rsp_len,
				fcxp->residue_len, &fcxp->rsp_fchs);
	} else {
		bfa_fcxp_free(fcxp);
	}
}

static void
hal_fcxp_send_comp(struct bfa_s *bfa, struct bfi_fcxp_send_rsp_s *fcxp_rsp)
{
	struct bfa_fcxp_mod_s	*mod = BFA_FCXP_MOD(bfa);
	struct bfa_fcxp_s	*fcxp;
690
	u16		fcxp_tag = be16_to_cpu(fcxp_rsp->fcxp_tag);
K
Krishna Gudipati 已提交
691 692 693

	bfa_trc(bfa, fcxp_tag);

694
	fcxp_rsp->rsp_len = be32_to_cpu(fcxp_rsp->rsp_len);
K
Krishna Gudipati 已提交
695

J
Jing Huang 已提交
696
	/*
K
Krishna Gudipati 已提交
697 698 699 700 701 702
	 * @todo f/w should not set residue to non-0 when everything
	 *	 is received.
	 */
	if (fcxp_rsp->req_status == BFA_STATUS_OK)
		fcxp_rsp->residue_len = 0;
	else
703
		fcxp_rsp->residue_len = be32_to_cpu(fcxp_rsp->residue_len);
K
Krishna Gudipati 已提交
704 705 706

	fcxp = BFA_FCXP_FROM_TAG(mod, fcxp_tag);

707
	WARN_ON(fcxp->send_cbfn == NULL);
K
Krishna Gudipati 已提交
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 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

	hal_fcxp_rx_plog(mod->bfa, fcxp, fcxp_rsp);

	if (fcxp->send_cbfn != NULL) {
		bfa_trc(mod->bfa, (NULL == fcxp->caller));
		if (fcxp->caller == NULL) {
			fcxp->send_cbfn(fcxp->caller, fcxp, fcxp->send_cbarg,
					fcxp_rsp->req_status, fcxp_rsp->rsp_len,
					fcxp_rsp->residue_len, &fcxp_rsp->fchs);
			/*
			 * fcxp automatically freed on return from the callback
			 */
			bfa_fcxp_free(fcxp);
		} else {
			fcxp->rsp_status = fcxp_rsp->req_status;
			fcxp->rsp_len = fcxp_rsp->rsp_len;
			fcxp->residue_len = fcxp_rsp->residue_len;
			fcxp->rsp_fchs = fcxp_rsp->fchs;

			bfa_cb_queue(bfa, &fcxp->hcb_qe,
					__bfa_fcxp_send_cbfn, fcxp);
		}
	} else {
		bfa_trc(bfa, (NULL == fcxp->send_cbfn));
	}
}

static void
hal_fcxp_tx_plog(struct bfa_s *bfa, u32 reqlen, struct bfa_fcxp_s *fcxp,
		 struct fchs_s *fchs)
{
	/*
	 * TODO: TX ox_id
	 */
	if (reqlen > 0) {
		if (fcxp->use_ireqbuf) {
			u32	pld_w0 =
				*((u32 *) BFA_FCXP_REQ_PLD(fcxp));

			bfa_plog_fchdr_and_pl(bfa->plog, BFA_PL_MID_HAL_FCXP,
					BFA_PL_EID_TX,
					reqlen + sizeof(struct fchs_s), fchs,
					pld_w0);
		} else {
			bfa_plog_fchdr(bfa->plog, BFA_PL_MID_HAL_FCXP,
					BFA_PL_EID_TX,
					reqlen + sizeof(struct fchs_s),
					fchs);
		}
	} else {
		bfa_plog_fchdr(bfa->plog, BFA_PL_MID_HAL_FCXP, BFA_PL_EID_TX,
			       reqlen + sizeof(struct fchs_s), fchs);
	}
}

static void
hal_fcxp_rx_plog(struct bfa_s *bfa, struct bfa_fcxp_s *fcxp,
		 struct bfi_fcxp_send_rsp_s *fcxp_rsp)
{
	if (fcxp_rsp->rsp_len > 0) {
		if (fcxp->use_irspbuf) {
			u32	pld_w0 =
				*((u32 *) BFA_FCXP_RSP_PLD(fcxp));

			bfa_plog_fchdr_and_pl(bfa->plog, BFA_PL_MID_HAL_FCXP,
					      BFA_PL_EID_RX,
					      (u16) fcxp_rsp->rsp_len,
					      &fcxp_rsp->fchs, pld_w0);
		} else {
			bfa_plog_fchdr(bfa->plog, BFA_PL_MID_HAL_FCXP,
				       BFA_PL_EID_RX,
				       (u16) fcxp_rsp->rsp_len,
				       &fcxp_rsp->fchs);
		}
	} else {
		bfa_plog_fchdr(bfa->plog, BFA_PL_MID_HAL_FCXP, BFA_PL_EID_RX,
			       (u16) fcxp_rsp->rsp_len, &fcxp_rsp->fchs);
	}
}

J
Jing Huang 已提交
788
/*
K
Krishna Gudipati 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802
 * Handler to resume sending fcxp when space in available in cpe queue.
 */
static void
bfa_fcxp_qresume(void *cbarg)
{
	struct bfa_fcxp_s		*fcxp = cbarg;
	struct bfa_s			*bfa = fcxp->fcxp_mod->bfa;
	struct bfi_fcxp_send_req_s	*send_req;

	fcxp->reqq_waiting = BFA_FALSE;
	send_req = bfa_reqq_next(bfa, BFA_REQQ_FCXP);
	bfa_fcxp_queue(fcxp, send_req);
}

J
Jing Huang 已提交
803
/*
K
Krishna Gudipati 已提交
804 805 806 807 808 809 810 811 812 813 814
 * Queue fcxp send request to foimrware.
 */
static void
bfa_fcxp_queue(struct bfa_fcxp_s *fcxp, struct bfi_fcxp_send_req_s *send_req)
{
	struct bfa_s			*bfa = fcxp->fcxp_mod->bfa;
	struct bfa_fcxp_req_info_s	*reqi = &fcxp->req_info;
	struct bfa_fcxp_rsp_info_s	*rspi = &fcxp->rsp_info;
	struct bfa_rport_s		*rport = reqi->bfa_rport;

	bfi_h2i_set(send_req->mh, BFI_MC_FCXP, BFI_FCXP_H2I_SEND_REQ,
815
		    bfa_fn_lpu(bfa));
K
Krishna Gudipati 已提交
816

817
	send_req->fcxp_tag = cpu_to_be16(fcxp->fcxp_tag);
K
Krishna Gudipati 已提交
818 819
	if (rport) {
		send_req->rport_fw_hndl = rport->fw_handle;
820
		send_req->max_frmsz = cpu_to_be16(rport->rport_info.max_frmsz);
K
Krishna Gudipati 已提交
821
		if (send_req->max_frmsz == 0)
822
			send_req->max_frmsz = cpu_to_be16(FC_MAX_PDUSZ);
K
Krishna Gudipati 已提交
823 824
	} else {
		send_req->rport_fw_hndl = 0;
825
		send_req->max_frmsz = cpu_to_be16(FC_MAX_PDUSZ);
K
Krishna Gudipati 已提交
826 827
	}

828
	send_req->vf_id = cpu_to_be16(reqi->vf_id);
829
	send_req->lp_fwtag = bfa_lps_get_fwtag(bfa, reqi->lp_tag);
K
Krishna Gudipati 已提交
830 831 832 833 834
	send_req->class = reqi->class;
	send_req->rsp_timeout = rspi->rsp_timeout;
	send_req->cts = reqi->cts;
	send_req->fchs = reqi->fchs;

835 836
	send_req->req_len = cpu_to_be32(reqi->req_tot_len);
	send_req->rsp_maxlen = cpu_to_be32(rspi->rsp_maxlen);
K
Krishna Gudipati 已提交
837 838 839 840 841

	/*
	 * setup req sgles
	 */
	if (fcxp->use_ireqbuf == 1) {
842
		bfa_alen_set(&send_req->req_alen, reqi->req_tot_len,
K
Krishna Gudipati 已提交
843 844 845
					BFA_FCXP_REQ_PLD_PA(fcxp));
	} else {
		if (fcxp->nreq_sgles > 0) {
846
			WARN_ON(fcxp->nreq_sgles != 1);
847 848
			bfa_alen_set(&send_req->req_alen, reqi->req_tot_len,
				fcxp->req_sga_cbfn(fcxp->caller, 0));
K
Krishna Gudipati 已提交
849
		} else {
850
			WARN_ON(reqi->req_tot_len != 0);
851
			bfa_alen_set(&send_req->rsp_alen, 0, 0);
K
Krishna Gudipati 已提交
852 853 854 855 856 857 858
		}
	}

	/*
	 * setup rsp sgles
	 */
	if (fcxp->use_irspbuf == 1) {
859
		WARN_ON(rspi->rsp_maxlen > BFA_FCXP_MAX_LBUF_SZ);
K
Krishna Gudipati 已提交
860

861
		bfa_alen_set(&send_req->rsp_alen, rspi->rsp_maxlen,
K
Krishna Gudipati 已提交
862 863 864
					BFA_FCXP_RSP_PLD_PA(fcxp));
	} else {
		if (fcxp->nrsp_sgles > 0) {
865
			WARN_ON(fcxp->nrsp_sgles != 1);
866 867 868
			bfa_alen_set(&send_req->rsp_alen, rspi->rsp_maxlen,
				fcxp->rsp_sga_cbfn(fcxp->caller, 0));

K
Krishna Gudipati 已提交
869
		} else {
870
			WARN_ON(rspi->rsp_maxlen != 0);
871
			bfa_alen_set(&send_req->rsp_alen, 0, 0);
K
Krishna Gudipati 已提交
872 873 874 875 876
		}
	}

	hal_fcxp_tx_plog(bfa, reqi->req_tot_len, fcxp, &reqi->fchs);

877
	bfa_reqq_produce(bfa, BFA_REQQ_FCXP, send_req->mh);
K
Krishna Gudipati 已提交
878 879 880 881 882

	bfa_trc(bfa, bfa_reqq_pi(bfa, BFA_REQQ_FCXP));
	bfa_trc(bfa, bfa_reqq_ci(bfa, BFA_REQQ_FCXP));
}

J
Jing Huang 已提交
883
/*
K
Krishna Gudipati 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
 * Allocate an FCXP instance to send a response or to send a request
 * that has a response. Request/response buffers are allocated by caller.
 *
 * @param[in]	bfa		BFA bfa instance
 * @param[in]	nreq_sgles	Number of SG elements required for request
 *				buffer. 0, if fcxp internal buffers are	used.
 *				Use bfa_fcxp_get_reqbuf() to get the
 *				internal req buffer.
 * @param[in]	req_sgles	SG elements describing request buffer. Will be
 *				copied in by BFA and hence can be freed on
 *				return from this function.
 * @param[in]	get_req_sga	function ptr to be called to get a request SG
 *				Address (given the sge index).
 * @param[in]	get_req_sglen	function ptr to be called to get a request SG
 *				len (given the sge index).
 * @param[in]	get_rsp_sga	function ptr to be called to get a response SG
 *				Address (given the sge index).
 * @param[in]	get_rsp_sglen	function ptr to be called to get a response SG
 *				len (given the sge index).
 *
 * @return FCXP instance. NULL on failure.
 */
struct bfa_fcxp_s *
bfa_fcxp_alloc(void *caller, struct bfa_s *bfa, int nreq_sgles,
	       int nrsp_sgles, bfa_fcxp_get_sgaddr_t req_sga_cbfn,
	       bfa_fcxp_get_sglen_t req_sglen_cbfn,
	       bfa_fcxp_get_sgaddr_t rsp_sga_cbfn,
	       bfa_fcxp_get_sglen_t rsp_sglen_cbfn)
{
	struct bfa_fcxp_s *fcxp = NULL;

915
	WARN_ON(bfa == NULL);
K
Krishna Gudipati 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928

	fcxp = bfa_fcxp_get(BFA_FCXP_MOD(bfa));
	if (fcxp == NULL)
		return NULL;

	bfa_trc(bfa, fcxp->fcxp_tag);

	bfa_fcxp_init(fcxp, caller, bfa, nreq_sgles, nrsp_sgles, req_sga_cbfn,
			req_sglen_cbfn, rsp_sga_cbfn, rsp_sglen_cbfn);

	return fcxp;
}

J
Jing Huang 已提交
929
/*
K
Krishna Gudipati 已提交
930 931 932 933 934 935 936 937 938 939 940 941
 * Get the internal request buffer pointer
 *
 * @param[in]	fcxp	BFA fcxp pointer
 *
 * @return		pointer to the internal request buffer
 */
void *
bfa_fcxp_get_reqbuf(struct bfa_fcxp_s *fcxp)
{
	struct bfa_fcxp_mod_s *mod = fcxp->fcxp_mod;
	void	*reqbuf;

942
	WARN_ON(fcxp->use_ireqbuf != 1);
943 944
	reqbuf = bfa_mem_get_dmabuf_kva(mod, fcxp->fcxp_tag,
				mod->req_pld_sz + mod->rsp_pld_sz);
K
Krishna Gudipati 已提交
945 946 947 948 949 950 951 952 953 954 955
	return reqbuf;
}

u32
bfa_fcxp_get_reqbufsz(struct bfa_fcxp_s *fcxp)
{
	struct bfa_fcxp_mod_s *mod = fcxp->fcxp_mod;

	return mod->req_pld_sz;
}

J
Jing Huang 已提交
956
/*
K
Krishna Gudipati 已提交
957 958 959 960 961 962 963 964 965 966
 * Get the internal response buffer pointer
 *
 * @param[in]	fcxp	BFA fcxp pointer
 *
 * @return		pointer to the internal request buffer
 */
void *
bfa_fcxp_get_rspbuf(struct bfa_fcxp_s *fcxp)
{
	struct bfa_fcxp_mod_s *mod = fcxp->fcxp_mod;
967
	void	*fcxp_buf;
K
Krishna Gudipati 已提交
968

969
	WARN_ON(fcxp->use_irspbuf != 1);
K
Krishna Gudipati 已提交
970

971 972 973 974 975
	fcxp_buf = bfa_mem_get_dmabuf_kva(mod, fcxp->fcxp_tag,
				mod->req_pld_sz + mod->rsp_pld_sz);

	/* fcxp_buf = req_buf + rsp_buf :- add req_buf_sz to get to rsp_buf */
	return ((u8 *) fcxp_buf) + mod->req_pld_sz;
K
Krishna Gudipati 已提交
976 977
}

J
Jing Huang 已提交
978
/*
979
 * Free the BFA FCXP
K
Krishna Gudipati 已提交
980 981 982 983 984 985 986 987 988 989
 *
 * @param[in]	fcxp			BFA fcxp pointer
 *
 * @return		void
 */
void
bfa_fcxp_free(struct bfa_fcxp_s *fcxp)
{
	struct bfa_fcxp_mod_s *mod = fcxp->fcxp_mod;

990
	WARN_ON(fcxp == NULL);
K
Krishna Gudipati 已提交
991 992 993 994
	bfa_trc(mod->bfa, fcxp->fcxp_tag);
	bfa_fcxp_put(fcxp);
}

J
Jing Huang 已提交
995
/*
K
Krishna Gudipati 已提交
996 997 998 999 1000 1001
 * Send a FCXP request
 *
 * @param[in]	fcxp	BFA fcxp pointer
 * @param[in]	rport	BFA rport pointer. Could be left NULL for WKA rports
 * @param[in]	vf_id	virtual Fabric ID
 * @param[in]	lp_tag	lport tag
L
Lucas De Marchi 已提交
1002
 * @param[in]	cts	use Continuous sequence
K
Krishna Gudipati 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
 * @param[in]	cos	fc Class of Service
 * @param[in]	reqlen	request length, does not include FCHS length
 * @param[in]	fchs	fc Header Pointer. The header content will be copied
 *			in by BFA.
 *
 * @param[in]	cbfn	call back function to be called on receiving
 *								the response
 * @param[in]	cbarg	arg for cbfn
 * @param[in]	rsp_timeout
 *			response timeout
 *
 * @return		bfa_status_t
 */
void
bfa_fcxp_send(struct bfa_fcxp_s *fcxp, struct bfa_rport_s *rport,
	      u16 vf_id, u8 lp_tag, bfa_boolean_t cts, enum fc_cos cos,
	      u32 reqlen, struct fchs_s *fchs, bfa_cb_fcxp_send_t cbfn,
	      void *cbarg, u32 rsp_maxlen, u8 rsp_timeout)
{
	struct bfa_s			*bfa  = fcxp->fcxp_mod->bfa;
	struct bfa_fcxp_req_info_s	*reqi = &fcxp->req_info;
	struct bfa_fcxp_rsp_info_s	*rspi = &fcxp->rsp_info;
	struct bfi_fcxp_send_req_s	*send_req;

	bfa_trc(bfa, fcxp->fcxp_tag);

J
Jing Huang 已提交
1029
	/*
K
Krishna Gudipati 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	 * setup request/response info
	 */
	reqi->bfa_rport = rport;
	reqi->vf_id = vf_id;
	reqi->lp_tag = lp_tag;
	reqi->class = cos;
	rspi->rsp_timeout = rsp_timeout;
	reqi->cts = cts;
	reqi->fchs = *fchs;
	reqi->req_tot_len = reqlen;
	rspi->rsp_maxlen = rsp_maxlen;
	fcxp->send_cbfn = cbfn ? cbfn : bfa_fcxp_null_comp;
	fcxp->send_cbarg = cbarg;

J
Jing Huang 已提交
1044
	/*
K
Krishna Gudipati 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	 * If no room in CPE queue, wait for space in request queue
	 */
	send_req = bfa_reqq_next(bfa, BFA_REQQ_FCXP);
	if (!send_req) {
		bfa_trc(bfa, fcxp->fcxp_tag);
		fcxp->reqq_waiting = BFA_TRUE;
		bfa_reqq_wait(bfa, BFA_REQQ_FCXP, &fcxp->reqq_wqe);
		return;
	}

	bfa_fcxp_queue(fcxp, send_req);
}

J
Jing Huang 已提交
1058
/*
K
Krishna Gudipati 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
 * Abort a BFA FCXP
 *
 * @param[in]	fcxp	BFA fcxp pointer
 *
 * @return		void
 */
bfa_status_t
bfa_fcxp_abort(struct bfa_fcxp_s *fcxp)
{
	bfa_trc(fcxp->fcxp_mod->bfa, fcxp->fcxp_tag);
1069
	WARN_ON(1);
K
Krishna Gudipati 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	return BFA_STATUS_OK;
}

void
bfa_fcxp_alloc_wait(struct bfa_s *bfa, struct bfa_fcxp_wqe_s *wqe,
	       bfa_fcxp_alloc_cbfn_t alloc_cbfn, void *alloc_cbarg,
	       void *caller, int nreq_sgles,
	       int nrsp_sgles, bfa_fcxp_get_sgaddr_t req_sga_cbfn,
	       bfa_fcxp_get_sglen_t req_sglen_cbfn,
	       bfa_fcxp_get_sgaddr_t rsp_sga_cbfn,
	       bfa_fcxp_get_sglen_t rsp_sglen_cbfn)
{
	struct bfa_fcxp_mod_s *mod = BFA_FCXP_MOD(bfa);

1084
	WARN_ON(!list_empty(&mod->fcxp_free_q));
K
Krishna Gudipati 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104

	wqe->alloc_cbfn = alloc_cbfn;
	wqe->alloc_cbarg = alloc_cbarg;
	wqe->caller = caller;
	wqe->bfa = bfa;
	wqe->nreq_sgles = nreq_sgles;
	wqe->nrsp_sgles = nrsp_sgles;
	wqe->req_sga_cbfn = req_sga_cbfn;
	wqe->req_sglen_cbfn = req_sglen_cbfn;
	wqe->rsp_sga_cbfn = rsp_sga_cbfn;
	wqe->rsp_sglen_cbfn = rsp_sglen_cbfn;

	list_add_tail(&wqe->qe, &mod->wait_q);
}

void
bfa_fcxp_walloc_cancel(struct bfa_s *bfa, struct bfa_fcxp_wqe_s *wqe)
{
	struct bfa_fcxp_mod_s *mod = BFA_FCXP_MOD(bfa);

1105
	WARN_ON(!bfa_q_is_on_q(&mod->wait_q, wqe));
K
Krishna Gudipati 已提交
1106 1107 1108 1109 1110 1111
	list_del(&wqe->qe);
}

void
bfa_fcxp_discard(struct bfa_fcxp_s *fcxp)
{
J
Jing Huang 已提交
1112
	/*
K
Krishna Gudipati 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	 * If waiting for room in request queue, cancel reqq wait
	 * and free fcxp.
	 */
	if (fcxp->reqq_waiting) {
		fcxp->reqq_waiting = BFA_FALSE;
		bfa_reqq_wcancel(&fcxp->reqq_wqe);
		bfa_fcxp_free(fcxp);
		return;
	}

	fcxp->send_cbfn = bfa_fcxp_null_comp;
}

void
bfa_fcxp_isr(struct bfa_s *bfa, struct bfi_msg_s *msg)
{
	switch (msg->mhdr.msg_id) {
	case BFI_FCXP_I2H_SEND_RSP:
		hal_fcxp_send_comp(bfa, (struct bfi_fcxp_send_rsp_s *) msg);
		break;

	default:
		bfa_trc(bfa, msg->mhdr.msg_id);
1136
		WARN_ON(1);
K
Krishna Gudipati 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	}
}

u32
bfa_fcxp_get_maxrsp(struct bfa_s *bfa)
{
	struct bfa_fcxp_mod_s *mod = BFA_FCXP_MOD(bfa);

	return mod->rsp_pld_sz;
}

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
void
bfa_fcxp_res_recfg(struct bfa_s *bfa, u16 num_fcxp_fw)
{
	struct bfa_fcxp_mod_s	*mod = BFA_FCXP_MOD(bfa);
	struct list_head	*qe;
	int	i;

	for (i = 0; i < (mod->num_fcxps - num_fcxp_fw); i++) {
		bfa_q_deq_tail(&mod->fcxp_free_q, &qe);
		list_add_tail(qe, &mod->fcxp_unused_q);
	}
}
K
Krishna Gudipati 已提交
1160

J
Jing Huang 已提交
1161
/*
K
Krishna Gudipati 已提交
1162 1163 1164
 *  BFA LPS state machine functions
 */

J
Jing Huang 已提交
1165
/*
K
Krishna Gudipati 已提交
1166 1167 1168 1169 1170
 * Init state -- no login
 */
static void
bfa_lps_sm_init(struct bfa_lps_s *lps, enum bfa_lps_event event)
{
1171
	bfa_trc(lps->bfa, lps->bfa_tag);
K
Krishna Gudipati 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
	bfa_trc(lps->bfa, event);

	switch (event) {
	case BFA_LPS_SM_LOGIN:
		if (bfa_reqq_full(lps->bfa, lps->reqq)) {
			bfa_sm_set_state(lps, bfa_lps_sm_loginwait);
			bfa_reqq_wait(lps->bfa, lps->reqq, &lps->wqe);
		} else {
			bfa_sm_set_state(lps, bfa_lps_sm_login);
			bfa_lps_send_login(lps);
		}

		if (lps->fdisc)
			bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
				BFA_PL_EID_LOGIN, 0, "FDISC Request");
		else
			bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
				BFA_PL_EID_LOGIN, 0, "FLOGI Request");
		break;

	case BFA_LPS_SM_LOGOUT:
		bfa_lps_logout_comp(lps);
		break;

	case BFA_LPS_SM_DELETE:
		bfa_lps_free(lps);
		break;

	case BFA_LPS_SM_RX_CVL:
	case BFA_LPS_SM_OFFLINE:
		break;

	case BFA_LPS_SM_FWRSP:
		/*
		 * Could happen when fabric detects loopback and discards
		 * the lps request. Fw will eventually sent out the timeout
		 * Just ignore
		 */
		break;

	default:
		bfa_sm_fault(lps->bfa, event);
	}
}

J
Jing Huang 已提交
1217
/*
K
Krishna Gudipati 已提交
1218 1219 1220 1221 1222
 * login is in progress -- awaiting response from firmware
 */
static void
bfa_lps_sm_login(struct bfa_lps_s *lps, enum bfa_lps_event event)
{
1223
	bfa_trc(lps->bfa, lps->bfa_tag);
K
Krishna Gudipati 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	bfa_trc(lps->bfa, event);

	switch (event) {
	case BFA_LPS_SM_FWRSP:
		if (lps->status == BFA_STATUS_OK) {
			bfa_sm_set_state(lps, bfa_lps_sm_online);
			if (lps->fdisc)
				bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
					BFA_PL_EID_LOGIN, 0, "FDISC Accept");
			else
				bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
					BFA_PL_EID_LOGIN, 0, "FLOGI Accept");
1236 1237 1238 1239 1240 1241
			/* If N2N, send the assigned PID to FW */
			bfa_trc(lps->bfa, lps->fport);
			bfa_trc(lps->bfa, lps->lp_pid);

			if (!lps->fport && lps->lp_pid)
				bfa_sm_send_event(lps, BFA_LPS_SM_SET_N2N_PID);
K
Krishna Gudipati 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
		} else {
			bfa_sm_set_state(lps, bfa_lps_sm_init);
			if (lps->fdisc)
				bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
					BFA_PL_EID_LOGIN, 0,
					"FDISC Fail (RJT or timeout)");
			else
				bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
					BFA_PL_EID_LOGIN, 0,
					"FLOGI Fail (RJT or timeout)");
		}
		bfa_lps_login_comp(lps);
		break;

	case BFA_LPS_SM_OFFLINE:
1257
	case BFA_LPS_SM_DELETE:
K
Krishna Gudipati 已提交
1258 1259 1260
		bfa_sm_set_state(lps, bfa_lps_sm_init);
		break;

1261 1262 1263 1264 1265
	case BFA_LPS_SM_SET_N2N_PID:
		bfa_trc(lps->bfa, lps->fport);
		bfa_trc(lps->bfa, lps->lp_pid);
		break;

K
Krishna Gudipati 已提交
1266 1267 1268 1269 1270
	default:
		bfa_sm_fault(lps->bfa, event);
	}
}

J
Jing Huang 已提交
1271
/*
K
Krishna Gudipati 已提交
1272 1273 1274 1275 1276
 * login pending - awaiting space in request queue
 */
static void
bfa_lps_sm_loginwait(struct bfa_lps_s *lps, enum bfa_lps_event event)
{
1277
	bfa_trc(lps->bfa, lps->bfa_tag);
K
Krishna Gudipati 已提交
1278 1279 1280 1281 1282 1283 1284 1285
	bfa_trc(lps->bfa, event);

	switch (event) {
	case BFA_LPS_SM_RESUME:
		bfa_sm_set_state(lps, bfa_lps_sm_login);
		break;

	case BFA_LPS_SM_OFFLINE:
1286
	case BFA_LPS_SM_DELETE:
K
Krishna Gudipati 已提交
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
		bfa_sm_set_state(lps, bfa_lps_sm_init);
		bfa_reqq_wcancel(&lps->wqe);
		break;

	case BFA_LPS_SM_RX_CVL:
		/*
		 * Login was not even sent out; so when getting out
		 * of this state, it will appear like a login retry
		 * after Clear virtual link
		 */
		break;

	default:
		bfa_sm_fault(lps->bfa, event);
	}
}

J
Jing Huang 已提交
1304
/*
K
Krishna Gudipati 已提交
1305 1306 1307 1308 1309
 * login complete
 */
static void
bfa_lps_sm_online(struct bfa_lps_s *lps, enum bfa_lps_event event)
{
1310
	bfa_trc(lps->bfa, lps->bfa_tag);
K
Krishna Gudipati 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
	bfa_trc(lps->bfa, event);

	switch (event) {
	case BFA_LPS_SM_LOGOUT:
		if (bfa_reqq_full(lps->bfa, lps->reqq)) {
			bfa_sm_set_state(lps, bfa_lps_sm_logowait);
			bfa_reqq_wait(lps->bfa, lps->reqq, &lps->wqe);
		} else {
			bfa_sm_set_state(lps, bfa_lps_sm_logout);
			bfa_lps_send_logout(lps);
		}
		bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
			BFA_PL_EID_LOGO, 0, "Logout");
		break;

	case BFA_LPS_SM_RX_CVL:
		bfa_sm_set_state(lps, bfa_lps_sm_init);

		/* Let the vport module know about this event */
		bfa_lps_cvl_event(lps);
		bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
			BFA_PL_EID_FIP_FCF_CVL, 0, "FCF Clear Virt. Link Rx");
		break;

1335 1336 1337 1338 1339 1340 1341 1342
	case BFA_LPS_SM_SET_N2N_PID:
		if (bfa_reqq_full(lps->bfa, lps->reqq)) {
			bfa_sm_set_state(lps, bfa_lps_sm_online_n2n_pid_wait);
			bfa_reqq_wait(lps->bfa, lps->reqq, &lps->wqe);
		} else
			bfa_lps_send_set_n2n_pid(lps);
		break;

K
Krishna Gudipati 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	case BFA_LPS_SM_OFFLINE:
	case BFA_LPS_SM_DELETE:
		bfa_sm_set_state(lps, bfa_lps_sm_init);
		break;

	default:
		bfa_sm_fault(lps->bfa, event);
	}
}

1353
/*
1354 1355 1356 1357 1358
 * login complete
 */
static void
bfa_lps_sm_online_n2n_pid_wait(struct bfa_lps_s *lps, enum bfa_lps_event event)
{
1359
	bfa_trc(lps->bfa, lps->bfa_tag);
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 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
	bfa_trc(lps->bfa, event);

	switch (event) {
	case BFA_LPS_SM_RESUME:
		bfa_sm_set_state(lps, bfa_lps_sm_online);
		bfa_lps_send_set_n2n_pid(lps);
		break;

	case BFA_LPS_SM_LOGOUT:
		bfa_sm_set_state(lps, bfa_lps_sm_logowait);
		bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
			BFA_PL_EID_LOGO, 0, "Logout");
		break;

	case BFA_LPS_SM_RX_CVL:
		bfa_sm_set_state(lps, bfa_lps_sm_init);
		bfa_reqq_wcancel(&lps->wqe);

		/* Let the vport module know about this event */
		bfa_lps_cvl_event(lps);
		bfa_plog_str(lps->bfa->plog, BFA_PL_MID_LPS,
			BFA_PL_EID_FIP_FCF_CVL, 0, "FCF Clear Virt. Link Rx");
		break;

	case BFA_LPS_SM_OFFLINE:
	case BFA_LPS_SM_DELETE:
		bfa_sm_set_state(lps, bfa_lps_sm_init);
		bfa_reqq_wcancel(&lps->wqe);
		break;

	default:
		bfa_sm_fault(lps->bfa, event);
	}
}

J
Jing Huang 已提交
1395
/*
K
Krishna Gudipati 已提交
1396 1397 1398 1399 1400
 * logout in progress - awaiting firmware response
 */
static void
bfa_lps_sm_logout(struct bfa_lps_s *lps, enum bfa_lps_event event)
{
1401
	bfa_trc(lps->bfa, lps->bfa_tag);
K
Krishna Gudipati 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410
	bfa_trc(lps->bfa, event);

	switch (event) {
	case BFA_LPS_SM_FWRSP:
		bfa_sm_set_state(lps, bfa_lps_sm_init);
		bfa_lps_logout_comp(lps);
		break;

	case BFA_LPS_SM_OFFLINE:
1411
	case BFA_LPS_SM_DELETE:
K
Krishna Gudipati 已提交
1412 1413 1414 1415 1416 1417 1418 1419
		bfa_sm_set_state(lps, bfa_lps_sm_init);
		break;

	default:
		bfa_sm_fault(lps->bfa, event);
	}
}

J
Jing Huang 已提交
1420
/*
K
Krishna Gudipati 已提交
1421 1422 1423 1424 1425
 * logout pending -- awaiting space in request queue
 */
static void
bfa_lps_sm_logowait(struct bfa_lps_s *lps, enum bfa_lps_event event)
{
1426
	bfa_trc(lps->bfa, lps->bfa_tag);
K
Krishna Gudipati 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435
	bfa_trc(lps->bfa, event);

	switch (event) {
	case BFA_LPS_SM_RESUME:
		bfa_sm_set_state(lps, bfa_lps_sm_logout);
		bfa_lps_send_logout(lps);
		break;

	case BFA_LPS_SM_OFFLINE:
1436
	case BFA_LPS_SM_DELETE:
K
Krishna Gudipati 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
		bfa_sm_set_state(lps, bfa_lps_sm_init);
		bfa_reqq_wcancel(&lps->wqe);
		break;

	default:
		bfa_sm_fault(lps->bfa, event);
	}
}



J
Jing Huang 已提交
1448
/*
K
Krishna Gudipati 已提交
1449 1450 1451
 *  lps_pvt BFA LPS private functions
 */

J
Jing Huang 已提交
1452
/*
K
Krishna Gudipati 已提交
1453 1454 1455
 * return memory requirement
 */
static void
1456 1457
bfa_lps_meminfo(struct bfa_iocfc_cfg_s *cfg, struct bfa_meminfo_s *minfo,
		struct bfa_s *bfa)
K
Krishna Gudipati 已提交
1458
{
1459 1460
	struct bfa_mem_kva_s *lps_kva = BFA_MEM_LPS_KVA(bfa);

K
Krishna Gudipati 已提交
1461
	if (cfg->drvcfg.min_cfg)
1462 1463
		bfa_mem_kva_setup(minfo, lps_kva,
			sizeof(struct bfa_lps_s) * BFA_LPS_MIN_LPORTS);
K
Krishna Gudipati 已提交
1464
	else
1465 1466
		bfa_mem_kva_setup(minfo, lps_kva,
			sizeof(struct bfa_lps_s) * BFA_LPS_MAX_LPORTS);
K
Krishna Gudipati 已提交
1467 1468
}

J
Jing Huang 已提交
1469
/*
K
Krishna Gudipati 已提交
1470 1471 1472 1473
 * bfa module attach at initialization time
 */
static void
bfa_lps_attach(struct bfa_s *bfa, void *bfad, struct bfa_iocfc_cfg_s *cfg,
1474
	struct bfa_pcidev_s *pcidev)
K
Krishna Gudipati 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
{
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(bfa);
	struct bfa_lps_s	*lps;
	int			i;

	mod->num_lps = BFA_LPS_MAX_LPORTS;
	if (cfg->drvcfg.min_cfg)
		mod->num_lps = BFA_LPS_MIN_LPORTS;
	else
		mod->num_lps = BFA_LPS_MAX_LPORTS;
1485
	mod->lps_arr = lps = (struct bfa_lps_s *) bfa_mem_kva_curp(mod);
K
Krishna Gudipati 已提交
1486

1487
	bfa_mem_kva_curp(mod) += mod->num_lps * sizeof(struct bfa_lps_s);
K
Krishna Gudipati 已提交
1488 1489 1490

	INIT_LIST_HEAD(&mod->lps_free_q);
	INIT_LIST_HEAD(&mod->lps_active_q);
1491
	INIT_LIST_HEAD(&mod->lps_login_q);
K
Krishna Gudipati 已提交
1492 1493 1494

	for (i = 0; i < mod->num_lps; i++, lps++) {
		lps->bfa	= bfa;
1495
		lps->bfa_tag	= (u8) i;
K
Krishna Gudipati 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
		lps->reqq	= BFA_REQQ_LPS;
		bfa_reqq_winit(&lps->wqe, bfa_lps_reqq_resume, lps);
		list_add_tail(&lps->qe, &mod->lps_free_q);
	}
}

static void
bfa_lps_detach(struct bfa_s *bfa)
{
}

static void
bfa_lps_start(struct bfa_s *bfa)
{
}

static void
bfa_lps_stop(struct bfa_s *bfa)
{
}

J
Jing Huang 已提交
1517
/*
K
Krishna Gudipati 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
 * IOC in disabled state -- consider all lps offline
 */
static void
bfa_lps_iocdisable(struct bfa_s *bfa)
{
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(bfa);
	struct bfa_lps_s	*lps;
	struct list_head		*qe, *qen;

	list_for_each_safe(qe, qen, &mod->lps_active_q) {
		lps = (struct bfa_lps_s *) qe;
		bfa_sm_send_event(lps, BFA_LPS_SM_OFFLINE);
	}
1531 1532 1533 1534 1535
	list_for_each_safe(qe, qen, &mod->lps_login_q) {
		lps = (struct bfa_lps_s *) qe;
		bfa_sm_send_event(lps, BFA_LPS_SM_OFFLINE);
	}
	list_splice_tail_init(&mod->lps_login_q, &mod->lps_active_q);
K
Krishna Gudipati 已提交
1536 1537
}

J
Jing Huang 已提交
1538
/*
K
Krishna Gudipati 已提交
1539 1540 1541 1542 1543 1544 1545 1546
 * Firmware login response
 */
static void
bfa_lps_login_rsp(struct bfa_s *bfa, struct bfi_lps_login_rsp_s *rsp)
{
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(bfa);
	struct bfa_lps_s	*lps;

1547 1548
	WARN_ON(rsp->bfa_tag >= mod->num_lps);
	lps = BFA_LPS_FROM_TAG(mod, rsp->bfa_tag);
K
Krishna Gudipati 已提交
1549 1550 1551 1552

	lps->status = rsp->status;
	switch (rsp->status) {
	case BFA_STATUS_OK:
1553
		lps->fw_tag	= rsp->fw_tag;
K
Krishna Gudipati 已提交
1554
		lps->fport	= rsp->f_port;
1555 1556
		if (lps->fport)
			lps->lp_pid = rsp->lp_pid;
K
Krishna Gudipati 已提交
1557
		lps->npiv_en	= rsp->npiv_en;
1558
		lps->pr_bbcred	= be16_to_cpu(rsp->bb_credit);
K
Krishna Gudipati 已提交
1559 1560 1561 1562 1563 1564
		lps->pr_pwwn	= rsp->port_name;
		lps->pr_nwwn	= rsp->node_name;
		lps->auth_req	= rsp->auth_req;
		lps->lp_mac	= rsp->lp_mac;
		lps->brcd_switch = rsp->brcd_switch;
		lps->fcf_mac	= rsp->fcf_mac;
1565
		lps->pr_bbscn	= rsp->bb_scn;
K
Krishna Gudipati 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579

		break;

	case BFA_STATUS_FABRIC_RJT:
		lps->lsrjt_rsn = rsp->lsrjt_rsn;
		lps->lsrjt_expl = rsp->lsrjt_expl;

		break;

	case BFA_STATUS_EPROTOCOL:
		lps->ext_status = rsp->ext_status;

		break;

1580 1581 1582 1583 1584
	case BFA_STATUS_VPORT_MAX:
		if (!rsp->ext_status)
			bfa_lps_no_res(lps, rsp->ext_status);
		break;

K
Krishna Gudipati 已提交
1585 1586 1587 1588 1589
	default:
		/* Nothing to do with other status */
		break;
	}

1590 1591
	list_del(&lps->qe);
	list_add_tail(&lps->qe, &mod->lps_active_q);
K
Krishna Gudipati 已提交
1592 1593 1594
	bfa_sm_send_event(lps, BFA_LPS_SM_FWRSP);
}

1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
static void
bfa_lps_no_res(struct bfa_lps_s *first_lps, u8 count)
{
	struct bfa_s		*bfa = first_lps->bfa;
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(bfa);
	struct list_head	*qe, *qe_next;
	struct bfa_lps_s	*lps;

	bfa_trc(bfa, count);

	qe = bfa_q_next(first_lps);

	while (count && qe) {
		qe_next = bfa_q_next(qe);
		lps = (struct bfa_lps_s *)qe;
		bfa_trc(bfa, lps->bfa_tag);
		lps->status = first_lps->status;
		list_del(&lps->qe);
		list_add_tail(&lps->qe, &mod->lps_active_q);
		bfa_sm_send_event(lps, BFA_LPS_SM_FWRSP);
		qe = qe_next;
		count--;
	}
}

J
Jing Huang 已提交
1620
/*
K
Krishna Gudipati 已提交
1621 1622 1623 1624 1625 1626 1627 1628
 * Firmware logout response
 */
static void
bfa_lps_logout_rsp(struct bfa_s *bfa, struct bfi_lps_logout_rsp_s *rsp)
{
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(bfa);
	struct bfa_lps_s	*lps;

1629 1630
	WARN_ON(rsp->bfa_tag >= mod->num_lps);
	lps = BFA_LPS_FROM_TAG(mod, rsp->bfa_tag);
K
Krishna Gudipati 已提交
1631 1632 1633 1634

	bfa_sm_send_event(lps, BFA_LPS_SM_FWRSP);
}

J
Jing Huang 已提交
1635
/*
K
Krishna Gudipati 已提交
1636 1637 1638 1639 1640 1641 1642 1643
 * Firmware received a Clear virtual link request (for FCoE)
 */
static void
bfa_lps_rx_cvl_event(struct bfa_s *bfa, struct bfi_lps_cvl_event_s *cvl)
{
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(bfa);
	struct bfa_lps_s	*lps;

1644
	lps = BFA_LPS_FROM_TAG(mod, cvl->bfa_tag);
K
Krishna Gudipati 已提交
1645 1646 1647 1648

	bfa_sm_send_event(lps, BFA_LPS_SM_RX_CVL);
}

J
Jing Huang 已提交
1649
/*
K
Krishna Gudipati 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
 * Space is available in request queue, resume queueing request to firmware.
 */
static void
bfa_lps_reqq_resume(void *lps_arg)
{
	struct bfa_lps_s	*lps = lps_arg;

	bfa_sm_send_event(lps, BFA_LPS_SM_RESUME);
}

J
Jing Huang 已提交
1660
/*
K
Krishna Gudipati 已提交
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
 * lps is freed -- triggered by vport delete
 */
static void
bfa_lps_free(struct bfa_lps_s *lps)
{
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(lps->bfa);

	lps->lp_pid = 0;
	list_del(&lps->qe);
	list_add_tail(&lps->qe, &mod->lps_free_q);
}

J
Jing Huang 已提交
1673
/*
K
Krishna Gudipati 已提交
1674 1675 1676 1677 1678
 * send login request to firmware
 */
static void
bfa_lps_send_login(struct bfa_lps_s *lps)
{
1679
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(lps->bfa);
K
Krishna Gudipati 已提交
1680 1681 1682
	struct bfi_lps_login_req_s	*m;

	m = bfa_reqq_next(lps->bfa, lps->reqq);
1683
	WARN_ON(!m);
K
Krishna Gudipati 已提交
1684 1685

	bfi_h2i_set(m->mh, BFI_MC_LPS, BFI_LPS_H2I_LOGIN_REQ,
1686
		bfa_fn_lpu(lps->bfa));
K
Krishna Gudipati 已提交
1687

1688
	m->bfa_tag	= lps->bfa_tag;
K
Krishna Gudipati 已提交
1689
	m->alpa		= lps->alpa;
1690
	m->pdu_size	= cpu_to_be16(lps->pdusz);
K
Krishna Gudipati 已提交
1691 1692 1693 1694
	m->pwwn		= lps->pwwn;
	m->nwwn		= lps->nwwn;
	m->fdisc	= lps->fdisc;
	m->auth_en	= lps->auth_en;
1695
	m->bb_scn	= lps->bb_scn;
K
Krishna Gudipati 已提交
1696

1697 1698 1699
	bfa_reqq_produce(lps->bfa, lps->reqq, m->mh);
	list_del(&lps->qe);
	list_add_tail(&lps->qe, &mod->lps_login_q);
K
Krishna Gudipati 已提交
1700 1701
}

J
Jing Huang 已提交
1702
/*
K
Krishna Gudipati 已提交
1703 1704 1705 1706 1707 1708 1709 1710
 * send logout request to firmware
 */
static void
bfa_lps_send_logout(struct bfa_lps_s *lps)
{
	struct bfi_lps_logout_req_s *m;

	m = bfa_reqq_next(lps->bfa, lps->reqq);
1711
	WARN_ON(!m);
K
Krishna Gudipati 已提交
1712 1713

	bfi_h2i_set(m->mh, BFI_MC_LPS, BFI_LPS_H2I_LOGOUT_REQ,
1714
		bfa_fn_lpu(lps->bfa));
K
Krishna Gudipati 已提交
1715

1716
	m->fw_tag = lps->fw_tag;
K
Krishna Gudipati 已提交
1717
	m->port_name = lps->pwwn;
1718
	bfa_reqq_produce(lps->bfa, lps->reqq, m->mh);
K
Krishna Gudipati 已提交
1719 1720
}

1721
/*
1722 1723 1724 1725 1726 1727 1728 1729
 * send n2n pid set request to firmware
 */
static void
bfa_lps_send_set_n2n_pid(struct bfa_lps_s *lps)
{
	struct bfi_lps_n2n_pid_req_s *m;

	m = bfa_reqq_next(lps->bfa, lps->reqq);
1730
	WARN_ON(!m);
1731 1732

	bfi_h2i_set(m->mh, BFI_MC_LPS, BFI_LPS_H2I_N2N_PID_REQ,
1733
		bfa_fn_lpu(lps->bfa));
1734

1735
	m->fw_tag = lps->fw_tag;
1736
	m->lp_pid = lps->lp_pid;
1737
	bfa_reqq_produce(lps->bfa, lps->reqq, m->mh);
1738 1739
}

J
Jing Huang 已提交
1740
/*
K
Krishna Gudipati 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
 * Indirect login completion handler for non-fcs
 */
static void
bfa_lps_login_comp_cb(void *arg, bfa_boolean_t complete)
{
	struct bfa_lps_s *lps	= arg;

	if (!complete)
		return;

	if (lps->fdisc)
		bfa_cb_lps_fdisc_comp(lps->bfa->bfad, lps->uarg, lps->status);
	else
		bfa_cb_lps_flogi_comp(lps->bfa->bfad, lps->uarg, lps->status);
}

J
Jing Huang 已提交
1757
/*
K
Krishna Gudipati 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
 * Login completion handler -- direct call for fcs, queue for others
 */
static void
bfa_lps_login_comp(struct bfa_lps_s *lps)
{
	if (!lps->bfa->fcs) {
		bfa_cb_queue(lps->bfa, &lps->hcb_qe, bfa_lps_login_comp_cb,
			lps);
		return;
	}

	if (lps->fdisc)
		bfa_cb_lps_fdisc_comp(lps->bfa->bfad, lps->uarg, lps->status);
	else
		bfa_cb_lps_flogi_comp(lps->bfa->bfad, lps->uarg, lps->status);
}

J
Jing Huang 已提交
1775
/*
K
Krishna Gudipati 已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
 * Indirect logout completion handler for non-fcs
 */
static void
bfa_lps_logout_comp_cb(void *arg, bfa_boolean_t complete)
{
	struct bfa_lps_s *lps	= arg;

	if (!complete)
		return;

	if (lps->fdisc)
		bfa_cb_lps_fdisclogo_comp(lps->bfa->bfad, lps->uarg);
}

J
Jing Huang 已提交
1790
/*
K
Krishna Gudipati 已提交
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
 * Logout completion handler -- direct call for fcs, queue for others
 */
static void
bfa_lps_logout_comp(struct bfa_lps_s *lps)
{
	if (!lps->bfa->fcs) {
		bfa_cb_queue(lps->bfa, &lps->hcb_qe, bfa_lps_logout_comp_cb,
			lps);
		return;
	}
	if (lps->fdisc)
		bfa_cb_lps_fdisclogo_comp(lps->bfa->bfad, lps->uarg);
}

J
Jing Huang 已提交
1805
/*
K
Krishna Gudipati 已提交
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
 * Clear virtual link completion handler for non-fcs
 */
static void
bfa_lps_cvl_event_cb(void *arg, bfa_boolean_t complete)
{
	struct bfa_lps_s *lps	= arg;

	if (!complete)
		return;

	/* Clear virtual link to base port will result in link down */
	if (lps->fdisc)
		bfa_cb_lps_cvl_event(lps->bfa->bfad, lps->uarg);
}

J
Jing Huang 已提交
1821
/*
K
Krishna Gudipati 已提交
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
 * Received Clear virtual link event --direct call for fcs,
 * queue for others
 */
static void
bfa_lps_cvl_event(struct bfa_lps_s *lps)
{
	if (!lps->bfa->fcs) {
		bfa_cb_queue(lps->bfa, &lps->hcb_qe, bfa_lps_cvl_event_cb,
			lps);
		return;
	}

	/* Clear virtual link to base port will result in link down */
	if (lps->fdisc)
		bfa_cb_lps_cvl_event(lps->bfa->bfad, lps->uarg);
}



J
Jing Huang 已提交
1841
/*
K
Krishna Gudipati 已提交
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
 *  lps_public BFA LPS public functions
 */

u32
bfa_lps_get_max_vport(struct bfa_s *bfa)
{
	if (bfa_ioc_devid(&bfa->ioc) == BFA_PCI_DEVICE_ID_CT)
		return BFA_LPS_MAX_VPORTS_SUPP_CT;
	else
		return BFA_LPS_MAX_VPORTS_SUPP_CB;
}

J
Jing Huang 已提交
1854
/*
K
Krishna Gudipati 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
 * Allocate a lport srvice tag.
 */
struct bfa_lps_s  *
bfa_lps_alloc(struct bfa_s *bfa)
{
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(bfa);
	struct bfa_lps_s	*lps = NULL;

	bfa_q_deq(&mod->lps_free_q, &lps);

	if (lps == NULL)
		return NULL;

	list_add_tail(&lps->qe, &mod->lps_active_q);

	bfa_sm_set_state(lps, bfa_lps_sm_init);
	return lps;
}

J
Jing Huang 已提交
1874
/*
K
Krishna Gudipati 已提交
1875 1876 1877 1878 1879 1880 1881 1882 1883
 * Free lport service tag. This can be called anytime after an alloc.
 * No need to wait for any pending login/logout completions.
 */
void
bfa_lps_delete(struct bfa_lps_s *lps)
{
	bfa_sm_send_event(lps, BFA_LPS_SM_DELETE);
}

J
Jing Huang 已提交
1884
/*
K
Krishna Gudipati 已提交
1885 1886 1887 1888
 * Initiate a lport login.
 */
void
bfa_lps_flogi(struct bfa_lps_s *lps, void *uarg, u8 alpa, u16 pdusz,
1889
	wwn_t pwwn, wwn_t nwwn, bfa_boolean_t auth_en, uint8_t bb_scn)
K
Krishna Gudipati 已提交
1890 1891 1892 1893 1894 1895 1896 1897
{
	lps->uarg	= uarg;
	lps->alpa	= alpa;
	lps->pdusz	= pdusz;
	lps->pwwn	= pwwn;
	lps->nwwn	= nwwn;
	lps->fdisc	= BFA_FALSE;
	lps->auth_en	= auth_en;
1898
	lps->bb_scn	= bb_scn;
K
Krishna Gudipati 已提交
1899 1900 1901
	bfa_sm_send_event(lps, BFA_LPS_SM_LOGIN);
}

J
Jing Huang 已提交
1902
/*
K
Krishna Gudipati 已提交
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
 * Initiate a lport fdisc login.
 */
void
bfa_lps_fdisc(struct bfa_lps_s *lps, void *uarg, u16 pdusz, wwn_t pwwn,
	wwn_t nwwn)
{
	lps->uarg	= uarg;
	lps->alpa	= 0;
	lps->pdusz	= pdusz;
	lps->pwwn	= pwwn;
	lps->nwwn	= nwwn;
	lps->fdisc	= BFA_TRUE;
	lps->auth_en	= BFA_FALSE;
	bfa_sm_send_event(lps, BFA_LPS_SM_LOGIN);
}


J
Jing Huang 已提交
1920
/*
K
Krishna Gudipati 已提交
1921 1922 1923 1924 1925 1926 1927 1928
 * Initiate a lport FDSIC logout.
 */
void
bfa_lps_fdisclogo(struct bfa_lps_s *lps)
{
	bfa_sm_send_event(lps, BFA_LPS_SM_LOGOUT);
}

1929 1930 1931 1932 1933 1934 1935
u8
bfa_lps_get_fwtag(struct bfa_s *bfa, u8 lp_tag)
{
	struct bfa_lps_mod_s    *mod = BFA_LPS_MOD(bfa);

	return BFA_LPS_FROM_TAG(mod, lp_tag)->fw_tag;
}
K
Krishna Gudipati 已提交
1936

J
Jing Huang 已提交
1937
/*
K
Krishna Gudipati 已提交
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
 * Return lport services tag given the pid
 */
u8
bfa_lps_get_tag_from_pid(struct bfa_s *bfa, u32 pid)
{
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(bfa);
	struct bfa_lps_s	*lps;
	int			i;

	for (i = 0, lps = mod->lps_arr; i < mod->num_lps; i++, lps++) {
		if (lps->lp_pid == pid)
1949
			return lps->bfa_tag;
K
Krishna Gudipati 已提交
1950 1951 1952 1953 1954 1955 1956
	}

	/* Return base port tag anyway */
	return 0;
}


J
Jing Huang 已提交
1957
/*
K
Krishna Gudipati 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
 * return port id assigned to the base lport
 */
u32
bfa_lps_get_base_pid(struct bfa_s *bfa)
{
	struct bfa_lps_mod_s	*mod = BFA_LPS_MOD(bfa);

	return BFA_LPS_FROM_TAG(mod, 0)->lp_pid;
}

1968
/*
1969 1970 1971 1972 1973
 * Set PID in case of n2n (which is assigned during PLOGI)
 */
void
bfa_lps_set_n2n_pid(struct bfa_lps_s *lps, uint32_t n2n_pid)
{
1974
	bfa_trc(lps->bfa, lps->bfa_tag);
1975 1976 1977 1978 1979 1980
	bfa_trc(lps->bfa, n2n_pid);

	lps->lp_pid = n2n_pid;
	bfa_sm_send_event(lps, BFA_LPS_SM_SET_N2N_PID);
}

J
Jing Huang 已提交
1981
/*
K
Krishna Gudipati 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
 * LPS firmware message class handler.
 */
void
bfa_lps_isr(struct bfa_s *bfa, struct bfi_msg_s *m)
{
	union bfi_lps_i2h_msg_u	msg;

	bfa_trc(bfa, m->mhdr.msg_id);
	msg.msg = m;

	switch (m->mhdr.msg_id) {
1993
	case BFI_LPS_I2H_LOGIN_RSP:
K
Krishna Gudipati 已提交
1994 1995 1996
		bfa_lps_login_rsp(bfa, msg.login_rsp);
		break;

1997
	case BFI_LPS_I2H_LOGOUT_RSP:
K
Krishna Gudipati 已提交
1998 1999 2000
		bfa_lps_logout_rsp(bfa, msg.logout_rsp);
		break;

2001
	case BFI_LPS_I2H_CVL_EVENT:
K
Krishna Gudipati 已提交
2002 2003 2004 2005 2006
		bfa_lps_rx_cvl_event(bfa, msg.cvl_event);
		break;

	default:
		bfa_trc(bfa, m->mhdr.msg_id);
2007
		WARN_ON(1);
K
Krishna Gudipati 已提交
2008 2009 2010
	}
}

2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
static void
bfa_fcport_aen_post(struct bfa_fcport_s *fcport, enum bfa_port_aen_event event)
{
	struct bfad_s *bfad = (struct bfad_s *)fcport->bfa->bfad;
	struct bfa_aen_entry_s  *aen_entry;

	bfad_get_aen_entry(bfad, aen_entry);
	if (!aen_entry)
		return;

	aen_entry->aen_data.port.ioc_type = bfa_get_type(fcport->bfa);
	aen_entry->aen_data.port.pwwn = fcport->pwwn;

	/* Send the AEN notification */
	bfad_im_post_vendor_event(aen_entry, bfad, ++fcport->bfa->bfa_aen_seq,
				  BFA_AEN_CAT_PORT, event);
}

J
Jing Huang 已提交
2029
/*
K
Krishna Gudipati 已提交
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
 * FC PORT state machine functions
 */
static void
bfa_fcport_sm_uninit(struct bfa_fcport_s *fcport,
			enum bfa_fcport_sm_event event)
{
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_START:
J
Jing Huang 已提交
2040
		/*
K
Krishna Gudipati 已提交
2041 2042
		 * Start event after IOC is configured and BFA is started.
		 */
2043 2044
		fcport->use_flash_cfg = BFA_TRUE;

K
Krishna Gudipati 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
		if (bfa_fcport_send_enable(fcport)) {
			bfa_trc(fcport->bfa, BFA_TRUE);
			bfa_sm_set_state(fcport, bfa_fcport_sm_enabling);
		} else {
			bfa_trc(fcport->bfa, BFA_FALSE);
			bfa_sm_set_state(fcport,
					bfa_fcport_sm_enabling_qwait);
		}
		break;

	case BFA_FCPORT_SM_ENABLE:
J
Jing Huang 已提交
2056
		/*
K
Krishna Gudipati 已提交
2057 2058 2059 2060 2061 2062 2063
		 * Port is persistently configured to be in enabled state. Do
		 * not change state. Port enabling is done when START event is
		 * received.
		 */
		break;

	case BFA_FCPORT_SM_DISABLE:
J
Jing Huang 已提交
2064
		/*
K
Krishna Gudipati 已提交
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
		 * If a port is persistently configured to be disabled, the
		 * first event will a port disable request.
		 */
		bfa_sm_set_state(fcport, bfa_fcport_sm_disabled);
		break;

	case BFA_FCPORT_SM_HWFAIL:
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocdown);
		break;

	default:
		bfa_sm_fault(fcport->bfa, event);
	}
}

static void
bfa_fcport_sm_enabling_qwait(struct bfa_fcport_s *fcport,
				enum bfa_fcport_sm_event event)
{
	char pwwn_buf[BFA_STRING_32];
	struct bfad_s *bfad = (struct bfad_s *)fcport->bfa->bfad;
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_QRESUME:
		bfa_sm_set_state(fcport, bfa_fcport_sm_enabling);
		bfa_fcport_send_enable(fcport);
		break;

	case BFA_FCPORT_SM_STOP:
		bfa_reqq_wcancel(&fcport->reqq_wait);
		bfa_sm_set_state(fcport, bfa_fcport_sm_stopped);
		break;

	case BFA_FCPORT_SM_ENABLE:
J
Jing Huang 已提交
2100
		/*
K
Krishna Gudipati 已提交
2101 2102 2103 2104 2105
		 * Already enable is in progress.
		 */
		break;

	case BFA_FCPORT_SM_DISABLE:
J
Jing Huang 已提交
2106
		/*
K
Krishna Gudipati 已提交
2107 2108 2109 2110 2111 2112 2113 2114
		 * Just send disable request to firmware when room becomes
		 * available in request queue.
		 */
		bfa_sm_set_state(fcport, bfa_fcport_sm_disabled);
		bfa_reqq_wcancel(&fcport->reqq_wait);
		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
				BFA_PL_EID_PORT_DISABLE, 0, "Port Disable");
		wwn2str(pwwn_buf, fcport->pwwn);
2115
		BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2116
			"Base port disabled: WWN = %s\n", pwwn_buf);
2117
		bfa_fcport_aen_post(fcport, BFA_PORT_AEN_DISABLE);
K
Krishna Gudipati 已提交
2118 2119 2120 2121
		break;

	case BFA_FCPORT_SM_LINKUP:
	case BFA_FCPORT_SM_LINKDOWN:
J
Jing Huang 已提交
2122
		/*
K
Krishna Gudipati 已提交
2123 2124 2125 2126 2127 2128 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
		 * Possible to get link events when doing back-to-back
		 * enable/disables.
		 */
		break;

	case BFA_FCPORT_SM_HWFAIL:
		bfa_reqq_wcancel(&fcport->reqq_wait);
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocdown);
		break;

	default:
		bfa_sm_fault(fcport->bfa, event);
	}
}

static void
bfa_fcport_sm_enabling(struct bfa_fcport_s *fcport,
						enum bfa_fcport_sm_event event)
{
	char pwwn_buf[BFA_STRING_32];
	struct bfad_s *bfad = (struct bfad_s *)fcport->bfa->bfad;
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_FWRSP:
	case BFA_FCPORT_SM_LINKDOWN:
		bfa_sm_set_state(fcport, bfa_fcport_sm_linkdown);
		break;

	case BFA_FCPORT_SM_LINKUP:
		bfa_fcport_update_linkinfo(fcport);
		bfa_sm_set_state(fcport, bfa_fcport_sm_linkup);

2156
		WARN_ON(!fcport->event_cbfn);
K
Krishna Gudipati 已提交
2157 2158 2159 2160
		bfa_fcport_scn(fcport, BFA_PORT_LINKUP, BFA_FALSE);
		break;

	case BFA_FCPORT_SM_ENABLE:
J
Jing Huang 已提交
2161
		/*
K
Krishna Gudipati 已提交
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
		 * Already being enabled.
		 */
		break;

	case BFA_FCPORT_SM_DISABLE:
		if (bfa_fcport_send_disable(fcport))
			bfa_sm_set_state(fcport, bfa_fcport_sm_disabling);
		else
			bfa_sm_set_state(fcport,
					 bfa_fcport_sm_disabling_qwait);

		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
				BFA_PL_EID_PORT_DISABLE, 0, "Port Disable");
		wwn2str(pwwn_buf, fcport->pwwn);
2176
		BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2177
			"Base port disabled: WWN = %s\n", pwwn_buf);
2178
		bfa_fcport_aen_post(fcport, BFA_PORT_AEN_DISABLE);
K
Krishna Gudipati 已提交
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
		break;

	case BFA_FCPORT_SM_STOP:
		bfa_sm_set_state(fcport, bfa_fcport_sm_stopped);
		break;

	case BFA_FCPORT_SM_HWFAIL:
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocdown);
		break;

	default:
		bfa_sm_fault(fcport->bfa, event);
	}
}

static void
bfa_fcport_sm_linkdown(struct bfa_fcport_s *fcport,
						enum bfa_fcport_sm_event event)
{
	struct bfi_fcport_event_s *pevent = fcport->event_arg.i2hmsg.event;
	char pwwn_buf[BFA_STRING_32];
	struct bfad_s *bfad = (struct bfad_s *)fcport->bfa->bfad;

	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_LINKUP:
		bfa_fcport_update_linkinfo(fcport);
		bfa_sm_set_state(fcport, bfa_fcport_sm_linkup);
2208
		WARN_ON(!fcport->event_cbfn);
K
Krishna Gudipati 已提交
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
				BFA_PL_EID_PORT_ST_CHANGE, 0, "Port Linkup");
		if (!bfa_ioc_get_fcmode(&fcport->bfa->ioc)) {

			bfa_trc(fcport->bfa,
				pevent->link_state.vc_fcf.fcf.fipenabled);
			bfa_trc(fcport->bfa,
				pevent->link_state.vc_fcf.fcf.fipfailed);

			if (pevent->link_state.vc_fcf.fcf.fipfailed)
				bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
					BFA_PL_EID_FIP_FCF_DISC, 0,
					"FIP FCF Discovery Failed");
			else
				bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
					BFA_PL_EID_FIP_FCF_DISC, 0,
					"FIP FCF Discovered");
		}

		bfa_fcport_scn(fcport, BFA_PORT_LINKUP, BFA_FALSE);
		wwn2str(pwwn_buf, fcport->pwwn);
2230
		BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2231
			"Base port online: WWN = %s\n", pwwn_buf);
2232
		bfa_fcport_aen_post(fcport, BFA_PORT_AEN_ONLINE);
K
Krishna Gudipati 已提交
2233 2234 2235
		break;

	case BFA_FCPORT_SM_LINKDOWN:
J
Jing Huang 已提交
2236
		/*
K
Krishna Gudipati 已提交
2237 2238 2239 2240 2241
		 * Possible to get link down event.
		 */
		break;

	case BFA_FCPORT_SM_ENABLE:
J
Jing Huang 已提交
2242
		/*
K
Krishna Gudipati 已提交
2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
		 * Already enabled.
		 */
		break;

	case BFA_FCPORT_SM_DISABLE:
		if (bfa_fcport_send_disable(fcport))
			bfa_sm_set_state(fcport, bfa_fcport_sm_disabling);
		else
			bfa_sm_set_state(fcport,
					 bfa_fcport_sm_disabling_qwait);

		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
				BFA_PL_EID_PORT_DISABLE, 0, "Port Disable");
		wwn2str(pwwn_buf, fcport->pwwn);
2257
		BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2258
			"Base port disabled: WWN = %s\n", pwwn_buf);
2259
		bfa_fcport_aen_post(fcport, BFA_PORT_AEN_DISABLE);
K
Krishna Gudipati 已提交
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
		break;

	case BFA_FCPORT_SM_STOP:
		bfa_sm_set_state(fcport, bfa_fcport_sm_stopped);
		break;

	case BFA_FCPORT_SM_HWFAIL:
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocdown);
		break;

	default:
		bfa_sm_fault(fcport->bfa, event);
	}
}

static void
bfa_fcport_sm_linkup(struct bfa_fcport_s *fcport,
	enum bfa_fcport_sm_event event)
{
	char pwwn_buf[BFA_STRING_32];
	struct bfad_s *bfad = (struct bfad_s *)fcport->bfa->bfad;

	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_ENABLE:
J
Jing Huang 已提交
2286
		/*
K
Krishna Gudipati 已提交
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
		 * Already enabled.
		 */
		break;

	case BFA_FCPORT_SM_DISABLE:
		if (bfa_fcport_send_disable(fcport))
			bfa_sm_set_state(fcport, bfa_fcport_sm_disabling);
		else
			bfa_sm_set_state(fcport,
					 bfa_fcport_sm_disabling_qwait);

		bfa_fcport_reset_linkinfo(fcport);
		bfa_fcport_scn(fcport, BFA_PORT_LINKDOWN, BFA_FALSE);
		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
				BFA_PL_EID_PORT_DISABLE, 0, "Port Disable");
		wwn2str(pwwn_buf, fcport->pwwn);
2303
		BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2304
			"Base port offline: WWN = %s\n", pwwn_buf);
2305
		bfa_fcport_aen_post(fcport, BFA_PORT_AEN_OFFLINE);
2306
		BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2307
			"Base port disabled: WWN = %s\n", pwwn_buf);
2308
		bfa_fcport_aen_post(fcport, BFA_PORT_AEN_DISABLE);
K
Krishna Gudipati 已提交
2309 2310 2311 2312 2313 2314 2315 2316 2317
		break;

	case BFA_FCPORT_SM_LINKDOWN:
		bfa_sm_set_state(fcport, bfa_fcport_sm_linkdown);
		bfa_fcport_reset_linkinfo(fcport);
		bfa_fcport_scn(fcport, BFA_PORT_LINKDOWN, BFA_FALSE);
		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
				BFA_PL_EID_PORT_ST_CHANGE, 0, "Port Linkdown");
		wwn2str(pwwn_buf, fcport->pwwn);
2318
		if (BFA_PORT_IS_DISABLED(fcport->bfa)) {
2319
			BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2320
				"Base port offline: WWN = %s\n", pwwn_buf);
2321 2322
			bfa_fcport_aen_post(fcport, BFA_PORT_AEN_OFFLINE);
		} else {
2323
			BFA_LOG(KERN_ERR, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2324 2325
				"Base port (WWN = %s) "
				"lost fabric connectivity\n", pwwn_buf);
2326 2327
			bfa_fcport_aen_post(fcport, BFA_PORT_AEN_DISCONNECT);
		}
K
Krishna Gudipati 已提交
2328 2329 2330 2331 2332 2333
		break;

	case BFA_FCPORT_SM_STOP:
		bfa_sm_set_state(fcport, bfa_fcport_sm_stopped);
		bfa_fcport_reset_linkinfo(fcport);
		wwn2str(pwwn_buf, fcport->pwwn);
2334
		if (BFA_PORT_IS_DISABLED(fcport->bfa)) {
2335
			BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2336
				"Base port offline: WWN = %s\n", pwwn_buf);
2337 2338
			bfa_fcport_aen_post(fcport, BFA_PORT_AEN_OFFLINE);
		} else {
2339
			BFA_LOG(KERN_ERR, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2340 2341
				"Base port (WWN = %s) "
				"lost fabric connectivity\n", pwwn_buf);
2342 2343
			bfa_fcport_aen_post(fcport, BFA_PORT_AEN_DISCONNECT);
		}
K
Krishna Gudipati 已提交
2344 2345 2346 2347 2348 2349 2350
		break;

	case BFA_FCPORT_SM_HWFAIL:
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocdown);
		bfa_fcport_reset_linkinfo(fcport);
		bfa_fcport_scn(fcport, BFA_PORT_LINKDOWN, BFA_FALSE);
		wwn2str(pwwn_buf, fcport->pwwn);
2351
		if (BFA_PORT_IS_DISABLED(fcport->bfa)) {
2352
			BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2353
				"Base port offline: WWN = %s\n", pwwn_buf);
2354 2355
			bfa_fcport_aen_post(fcport, BFA_PORT_AEN_OFFLINE);
		} else {
2356
			BFA_LOG(KERN_ERR, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2357 2358
				"Base port (WWN = %s) "
				"lost fabric connectivity\n", pwwn_buf);
2359 2360
			bfa_fcport_aen_post(fcport, BFA_PORT_AEN_DISCONNECT);
		}
K
Krishna Gudipati 已提交
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
		break;

	default:
		bfa_sm_fault(fcport->bfa, event);
	}
}

static void
bfa_fcport_sm_disabling_qwait(struct bfa_fcport_s *fcport,
				 enum bfa_fcport_sm_event event)
{
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_QRESUME:
		bfa_sm_set_state(fcport, bfa_fcport_sm_disabling);
		bfa_fcport_send_disable(fcport);
		break;

	case BFA_FCPORT_SM_STOP:
		bfa_sm_set_state(fcport, bfa_fcport_sm_stopped);
		bfa_reqq_wcancel(&fcport->reqq_wait);
		break;

	case BFA_FCPORT_SM_ENABLE:
		bfa_sm_set_state(fcport, bfa_fcport_sm_toggling_qwait);
		break;

	case BFA_FCPORT_SM_DISABLE:
J
Jing Huang 已提交
2390
		/*
K
Krishna Gudipati 已提交
2391 2392 2393 2394 2395 2396
		 * Already being disabled.
		 */
		break;

	case BFA_FCPORT_SM_LINKUP:
	case BFA_FCPORT_SM_LINKDOWN:
J
Jing Huang 已提交
2397
		/*
K
Krishna Gudipati 已提交
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
		 * Possible to get link events when doing back-to-back
		 * enable/disables.
		 */
		break;

	case BFA_FCPORT_SM_HWFAIL:
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocfail);
		bfa_reqq_wcancel(&fcport->reqq_wait);
		break;

	default:
		bfa_sm_fault(fcport->bfa, event);
	}
}

static void
bfa_fcport_sm_toggling_qwait(struct bfa_fcport_s *fcport,
				 enum bfa_fcport_sm_event event)
{
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_QRESUME:
		bfa_sm_set_state(fcport, bfa_fcport_sm_disabling);
		bfa_fcport_send_disable(fcport);
		if (bfa_fcport_send_enable(fcport))
			bfa_sm_set_state(fcport, bfa_fcport_sm_enabling);
		else
			bfa_sm_set_state(fcport,
					 bfa_fcport_sm_enabling_qwait);
		break;

	case BFA_FCPORT_SM_STOP:
		bfa_sm_set_state(fcport, bfa_fcport_sm_stopped);
		bfa_reqq_wcancel(&fcport->reqq_wait);
		break;

	case BFA_FCPORT_SM_ENABLE:
		break;

	case BFA_FCPORT_SM_DISABLE:
		bfa_sm_set_state(fcport, bfa_fcport_sm_disabling_qwait);
		break;

	case BFA_FCPORT_SM_LINKUP:
	case BFA_FCPORT_SM_LINKDOWN:
J
Jing Huang 已提交
2444
		/*
K
Krishna Gudipati 已提交
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
		 * Possible to get link events when doing back-to-back
		 * enable/disables.
		 */
		break;

	case BFA_FCPORT_SM_HWFAIL:
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocfail);
		bfa_reqq_wcancel(&fcport->reqq_wait);
		break;

	default:
		bfa_sm_fault(fcport->bfa, event);
	}
}

static void
bfa_fcport_sm_disabling(struct bfa_fcport_s *fcport,
						enum bfa_fcport_sm_event event)
{
	char pwwn_buf[BFA_STRING_32];
	struct bfad_s *bfad = (struct bfad_s *)fcport->bfa->bfad;
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_FWRSP:
		bfa_sm_set_state(fcport, bfa_fcport_sm_disabled);
		break;

	case BFA_FCPORT_SM_DISABLE:
J
Jing Huang 已提交
2474
		/*
K
Krishna Gudipati 已提交
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
		 * Already being disabled.
		 */
		break;

	case BFA_FCPORT_SM_ENABLE:
		if (bfa_fcport_send_enable(fcport))
			bfa_sm_set_state(fcport, bfa_fcport_sm_enabling);
		else
			bfa_sm_set_state(fcport,
					 bfa_fcport_sm_enabling_qwait);

		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
				BFA_PL_EID_PORT_ENABLE, 0, "Port Enable");
		wwn2str(pwwn_buf, fcport->pwwn);
2489
		BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2490
			"Base port enabled: WWN = %s\n", pwwn_buf);
2491
		bfa_fcport_aen_post(fcport, BFA_PORT_AEN_ENABLE);
K
Krishna Gudipati 已提交
2492 2493 2494 2495 2496 2497 2498 2499
		break;

	case BFA_FCPORT_SM_STOP:
		bfa_sm_set_state(fcport, bfa_fcport_sm_stopped);
		break;

	case BFA_FCPORT_SM_LINKUP:
	case BFA_FCPORT_SM_LINKDOWN:
J
Jing Huang 已提交
2500
		/*
K
Krishna Gudipati 已提交
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
		 * Possible to get link events when doing back-to-back
		 * enable/disables.
		 */
		break;

	case BFA_FCPORT_SM_HWFAIL:
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocfail);
		break;

	default:
		bfa_sm_fault(fcport->bfa, event);
	}
}

static void
bfa_fcport_sm_disabled(struct bfa_fcport_s *fcport,
						enum bfa_fcport_sm_event event)
{
	char pwwn_buf[BFA_STRING_32];
	struct bfad_s *bfad = (struct bfad_s *)fcport->bfa->bfad;
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_START:
J
Jing Huang 已提交
2525
		/*
K
Krishna Gudipati 已提交
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
		 * Ignore start event for a port that is disabled.
		 */
		break;

	case BFA_FCPORT_SM_STOP:
		bfa_sm_set_state(fcport, bfa_fcport_sm_stopped);
		break;

	case BFA_FCPORT_SM_ENABLE:
		if (bfa_fcport_send_enable(fcport))
			bfa_sm_set_state(fcport, bfa_fcport_sm_enabling);
		else
			bfa_sm_set_state(fcport,
					 bfa_fcport_sm_enabling_qwait);

		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
				BFA_PL_EID_PORT_ENABLE, 0, "Port Enable");
		wwn2str(pwwn_buf, fcport->pwwn);
2544
		BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
2545
			"Base port enabled: WWN = %s\n", pwwn_buf);
2546
		bfa_fcport_aen_post(fcport, BFA_PORT_AEN_ENABLE);
K
Krishna Gudipati 已提交
2547 2548 2549
		break;

	case BFA_FCPORT_SM_DISABLE:
J
Jing Huang 已提交
2550
		/*
K
Krishna Gudipati 已提交
2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
		 * Already disabled.
		 */
		break;

	case BFA_FCPORT_SM_HWFAIL:
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocfail);
		break;

	default:
		bfa_sm_fault(fcport->bfa, event);
	}
}

static void
bfa_fcport_sm_stopped(struct bfa_fcport_s *fcport,
			 enum bfa_fcport_sm_event event)
{
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_START:
		if (bfa_fcport_send_enable(fcport))
			bfa_sm_set_state(fcport, bfa_fcport_sm_enabling);
		else
			bfa_sm_set_state(fcport,
					 bfa_fcport_sm_enabling_qwait);
		break;

	default:
J
Jing Huang 已提交
2580
		/*
K
Krishna Gudipati 已提交
2581 2582 2583 2584 2585 2586
		 * Ignore all other events.
		 */
		;
	}
}

J
Jing Huang 已提交
2587
/*
K
Krishna Gudipati 已提交
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
 * Port is enabled. IOC is down/failed.
 */
static void
bfa_fcport_sm_iocdown(struct bfa_fcport_s *fcport,
			 enum bfa_fcport_sm_event event)
{
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_START:
		if (bfa_fcport_send_enable(fcport))
			bfa_sm_set_state(fcport, bfa_fcport_sm_enabling);
		else
			bfa_sm_set_state(fcport,
					 bfa_fcport_sm_enabling_qwait);
		break;

	default:
J
Jing Huang 已提交
2606
		/*
K
Krishna Gudipati 已提交
2607 2608 2609 2610 2611 2612
		 * Ignore all events.
		 */
		;
	}
}

J
Jing Huang 已提交
2613
/*
K
Krishna Gudipati 已提交
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
 * Port is disabled. IOC is down/failed.
 */
static void
bfa_fcport_sm_iocfail(struct bfa_fcport_s *fcport,
			 enum bfa_fcport_sm_event event)
{
	bfa_trc(fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_SM_START:
		bfa_sm_set_state(fcport, bfa_fcport_sm_disabled);
		break;

	case BFA_FCPORT_SM_ENABLE:
		bfa_sm_set_state(fcport, bfa_fcport_sm_iocdown);
		break;

	default:
J
Jing Huang 已提交
2632
		/*
K
Krishna Gudipati 已提交
2633 2634 2635 2636 2637 2638
		 * Ignore all events.
		 */
		;
	}
}

J
Jing Huang 已提交
2639
/*
K
Krishna Gudipati 已提交
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658
 * Link state is down
 */
static void
bfa_fcport_ln_sm_dn(struct bfa_fcport_ln_s *ln,
		enum bfa_fcport_ln_sm_event event)
{
	bfa_trc(ln->fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_LN_SM_LINKUP:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_up_nf);
		bfa_fcport_queue_cb(ln, BFA_PORT_LINKUP);
		break;

	default:
		bfa_sm_fault(ln->fcport->bfa, event);
	}
}

J
Jing Huang 已提交
2659
/*
K
Krishna Gudipati 已提交
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
 * Link state is waiting for down notification
 */
static void
bfa_fcport_ln_sm_dn_nf(struct bfa_fcport_ln_s *ln,
		enum bfa_fcport_ln_sm_event event)
{
	bfa_trc(ln->fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_LN_SM_LINKUP:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_dn_up_nf);
		break;

	case BFA_FCPORT_LN_SM_NOTIFICATION:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_dn);
		break;

	default:
		bfa_sm_fault(ln->fcport->bfa, event);
	}
}

J
Jing Huang 已提交
2682
/*
K
Krishna Gudipati 已提交
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
 * Link state is waiting for down notification and there is a pending up
 */
static void
bfa_fcport_ln_sm_dn_up_nf(struct bfa_fcport_ln_s *ln,
		enum bfa_fcport_ln_sm_event event)
{
	bfa_trc(ln->fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_LN_SM_LINKDOWN:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_dn_nf);
		break;

	case BFA_FCPORT_LN_SM_NOTIFICATION:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_up_nf);
		bfa_fcport_queue_cb(ln, BFA_PORT_LINKUP);
		break;

	default:
		bfa_sm_fault(ln->fcport->bfa, event);
	}
}

J
Jing Huang 已提交
2706
/*
K
Krishna Gudipati 已提交
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
 * Link state is up
 */
static void
bfa_fcport_ln_sm_up(struct bfa_fcport_ln_s *ln,
		enum bfa_fcport_ln_sm_event event)
{
	bfa_trc(ln->fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_LN_SM_LINKDOWN:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_dn_nf);
		bfa_fcport_queue_cb(ln, BFA_PORT_LINKDOWN);
		break;

	default:
		bfa_sm_fault(ln->fcport->bfa, event);
	}
}

J
Jing Huang 已提交
2726
/*
K
Krishna Gudipati 已提交
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748
 * Link state is waiting for up notification
 */
static void
bfa_fcport_ln_sm_up_nf(struct bfa_fcport_ln_s *ln,
		enum bfa_fcport_ln_sm_event event)
{
	bfa_trc(ln->fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_LN_SM_LINKDOWN:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_up_dn_nf);
		break;

	case BFA_FCPORT_LN_SM_NOTIFICATION:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_up);
		break;

	default:
		bfa_sm_fault(ln->fcport->bfa, event);
	}
}

J
Jing Huang 已提交
2749
/*
K
Krishna Gudipati 已提交
2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
 * Link state is waiting for up notification and there is a pending down
 */
static void
bfa_fcport_ln_sm_up_dn_nf(struct bfa_fcport_ln_s *ln,
		enum bfa_fcport_ln_sm_event event)
{
	bfa_trc(ln->fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_LN_SM_LINKUP:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_up_dn_up_nf);
		break;

	case BFA_FCPORT_LN_SM_NOTIFICATION:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_dn_nf);
		bfa_fcport_queue_cb(ln, BFA_PORT_LINKDOWN);
		break;

	default:
		bfa_sm_fault(ln->fcport->bfa, event);
	}
}

J
Jing Huang 已提交
2773
/*
K
Krishna Gudipati 已提交
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
 * Link state is waiting for up notification and there are pending down and up
 */
static void
bfa_fcport_ln_sm_up_dn_up_nf(struct bfa_fcport_ln_s *ln,
			enum bfa_fcport_ln_sm_event event)
{
	bfa_trc(ln->fcport->bfa, event);

	switch (event) {
	case BFA_FCPORT_LN_SM_LINKDOWN:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_up_dn_nf);
		break;

	case BFA_FCPORT_LN_SM_NOTIFICATION:
		bfa_sm_set_state(ln, bfa_fcport_ln_sm_dn_up_nf);
		bfa_fcport_queue_cb(ln, BFA_PORT_LINKDOWN);
		break;

	default:
		bfa_sm_fault(ln->fcport->bfa, event);
	}
}

static void
__bfa_cb_fcport_event(void *cbarg, bfa_boolean_t complete)
{
	struct bfa_fcport_ln_s *ln = cbarg;

	if (complete)
		ln->fcport->event_cbfn(ln->fcport->event_cbarg, ln->ln_event);
	else
		bfa_sm_send_event(ln, BFA_FCPORT_LN_SM_NOTIFICATION);
}

J
Jing Huang 已提交
2808
/*
K
Krishna Gudipati 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
 * Send SCN notification to upper layers.
 * trunk - false if caller is fcport to ignore fcport event in trunked mode
 */
static void
bfa_fcport_scn(struct bfa_fcport_s *fcport, enum bfa_port_linkstate event,
	bfa_boolean_t trunk)
{
	if (fcport->cfg.trunked && !trunk)
		return;

	switch (event) {
	case BFA_PORT_LINKUP:
		bfa_sm_send_event(&fcport->ln, BFA_FCPORT_LN_SM_LINKUP);
		break;
	case BFA_PORT_LINKDOWN:
		bfa_sm_send_event(&fcport->ln, BFA_FCPORT_LN_SM_LINKDOWN);
		break;
	default:
2827
		WARN_ON(1);
K
Krishna Gudipati 已提交
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
	}
}

static void
bfa_fcport_queue_cb(struct bfa_fcport_ln_s *ln, enum bfa_port_linkstate event)
{
	struct bfa_fcport_s *fcport = ln->fcport;

	if (fcport->bfa->fcs) {
		fcport->event_cbfn(fcport->event_cbarg, event);
		bfa_sm_send_event(ln, BFA_FCPORT_LN_SM_NOTIFICATION);
	} else {
		ln->ln_event = event;
		bfa_cb_queue(fcport->bfa, &ln->ln_qe,
			__bfa_cb_fcport_event, ln);
	}
}

#define FCPORT_STATS_DMA_SZ (BFA_ROUNDUP(sizeof(union bfa_fcport_stats_u), \
							BFA_CACHELINE_SZ))

static void
2850 2851
bfa_fcport_meminfo(struct bfa_iocfc_cfg_s *cfg, struct bfa_meminfo_s *minfo,
		   struct bfa_s *bfa)
K
Krishna Gudipati 已提交
2852
{
2853 2854 2855
	struct bfa_mem_dma_s *fcport_dma = BFA_MEM_FCPORT_DMA(bfa);

	bfa_mem_dma_setup(minfo, fcport_dma, FCPORT_STATS_DMA_SZ);
K
Krishna Gudipati 已提交
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
}

static void
bfa_fcport_qresume(void *cbarg)
{
	struct bfa_fcport_s *fcport = cbarg;

	bfa_sm_send_event(fcport, BFA_FCPORT_SM_QRESUME);
}

static void
2867
bfa_fcport_mem_claim(struct bfa_fcport_s *fcport)
K
Krishna Gudipati 已提交
2868
{
2869
	struct bfa_mem_dma_s *fcport_dma = &fcport->fcport_dma;
K
Krishna Gudipati 已提交
2870

2871 2872 2873 2874
	fcport->stats_kva = bfa_mem_dma_virt(fcport_dma);
	fcport->stats_pa  = bfa_mem_dma_phys(fcport_dma);
	fcport->stats = (union bfa_fcport_stats_u *)
				bfa_mem_dma_virt(fcport_dma);
K
Krishna Gudipati 已提交
2875 2876
}

J
Jing Huang 已提交
2877
/*
K
Krishna Gudipati 已提交
2878 2879 2880 2881
 * Memory initialization.
 */
static void
bfa_fcport_attach(struct bfa_s *bfa, void *bfad, struct bfa_iocfc_cfg_s *cfg,
2882
		struct bfa_pcidev_s *pcidev)
K
Krishna Gudipati 已提交
2883 2884 2885 2886
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);
	struct bfa_port_cfg_s *port_cfg = &fcport->cfg;
	struct bfa_fcport_ln_s *ln = &fcport->ln;
M
Maggie Zhang 已提交
2887
	struct timeval tv;
K
Krishna Gudipati 已提交
2888 2889 2890 2891

	fcport->bfa = bfa;
	ln->fcport = fcport;

2892
	bfa_fcport_mem_claim(fcport);
K
Krishna Gudipati 已提交
2893 2894 2895 2896

	bfa_sm_set_state(fcport, bfa_fcport_sm_uninit);
	bfa_sm_set_state(ln, bfa_fcport_ln_sm_dn);

J
Jing Huang 已提交
2897
	/*
K
Krishna Gudipati 已提交
2898 2899
	 * initialize time stamp for stats reset
	 */
M
Maggie Zhang 已提交
2900
	do_gettimeofday(&tv);
K
Krishna Gudipati 已提交
2901 2902
	fcport->stats_reset_time = tv.tv_sec;

J
Jing Huang 已提交
2903
	/*
K
Krishna Gudipati 已提交
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
	 * initialize and set default configuration
	 */
	port_cfg->topology = BFA_PORT_TOPOLOGY_P2P;
	port_cfg->speed = BFA_PORT_SPEED_AUTO;
	port_cfg->trunked = BFA_FALSE;
	port_cfg->maxfrsize = 0;

	port_cfg->trl_def_speed = BFA_PORT_SPEED_1GBPS;

	bfa_reqq_winit(&fcport->reqq_wait, bfa_fcport_qresume, fcport);
}

static void
bfa_fcport_detach(struct bfa_s *bfa)
{
}

J
Jing Huang 已提交
2921
/*
K
Krishna Gudipati 已提交
2922 2923 2924 2925 2926 2927 2928 2929
 * Called when IOC is ready.
 */
static void
bfa_fcport_start(struct bfa_s *bfa)
{
	bfa_sm_send_event(BFA_FCPORT_MOD(bfa), BFA_FCPORT_SM_START);
}

J
Jing Huang 已提交
2930
/*
K
Krishna Gudipati 已提交
2931 2932 2933 2934 2935 2936 2937 2938 2939
 * Called before IOC is stopped.
 */
static void
bfa_fcport_stop(struct bfa_s *bfa)
{
	bfa_sm_send_event(BFA_FCPORT_MOD(bfa), BFA_FCPORT_SM_STOP);
	bfa_trunk_iocdisable(bfa);
}

J
Jing Huang 已提交
2940
/*
K
Krishna Gudipati 已提交
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
 * Called when IOC failure is detected.
 */
static void
bfa_fcport_iocdisable(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	bfa_sm_send_event(fcport, BFA_FCPORT_SM_HWFAIL);
	bfa_trunk_iocdisable(bfa);
}

static void
bfa_fcport_update_linkinfo(struct bfa_fcport_s *fcport)
{
	struct bfi_fcport_event_s *pevent = fcport->event_arg.i2hmsg.event;
	struct bfa_fcport_trunk_s *trunk = &fcport->trunk;

	fcport->speed = pevent->link_state.speed;
	fcport->topology = pevent->link_state.topology;

	if (fcport->topology == BFA_PORT_TOPOLOGY_LOOP)
		fcport->myalpa = 0;

	/* QoS Details */
2965 2966
	fcport->qos_attr = pevent->link_state.qos_attr;
	fcport->qos_vc_attr = pevent->link_state.vc_fcf.qos_vc_attr;
K
Krishna Gudipati 已提交
2967

J
Jing Huang 已提交
2968
	/*
K
Krishna Gudipati 已提交
2969 2970 2971 2972 2973 2974
	 * update trunk state if applicable
	 */
	if (!fcport->cfg.trunked)
		trunk->attr.state = BFA_TRUNK_DISABLED;

	/* update FCoE specific */
2975
	fcport->fcoe_vlan = be16_to_cpu(pevent->link_state.vc_fcf.fcf.vlan);
K
Krishna Gudipati 已提交
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985

	bfa_trc(fcport->bfa, fcport->speed);
	bfa_trc(fcport->bfa, fcport->topology);
}

static void
bfa_fcport_reset_linkinfo(struct bfa_fcport_s *fcport)
{
	fcport->speed = BFA_PORT_SPEED_UNKNOWN;
	fcport->topology = BFA_PORT_TOPOLOGY_NONE;
2986
	fcport->bbsc_op_state = BFA_FALSE;
K
Krishna Gudipati 已提交
2987 2988
}

J
Jing Huang 已提交
2989
/*
K
Krishna Gudipati 已提交
2990 2991 2992 2993 2994 2995 2996
 * Send port enable message to firmware.
 */
static bfa_boolean_t
bfa_fcport_send_enable(struct bfa_fcport_s *fcport)
{
	struct bfi_fcport_enable_req_s *m;

J
Jing Huang 已提交
2997
	/*
K
Krishna Gudipati 已提交
2998 2999 3000 3001 3002
	 * Increment message tag before queue check, so that responses to old
	 * requests are discarded.
	 */
	fcport->msgtag++;

J
Jing Huang 已提交
3003
	/*
K
Krishna Gudipati 已提交
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
	 * check for room in queue to send request now
	 */
	m = bfa_reqq_next(fcport->bfa, BFA_REQQ_PORT);
	if (!m) {
		bfa_reqq_wait(fcport->bfa, BFA_REQQ_PORT,
							&fcport->reqq_wait);
		return BFA_FALSE;
	}

	bfi_h2i_set(m->mh, BFI_MC_FCPORT, BFI_FCPORT_H2I_ENABLE_REQ,
3014
			bfa_fn_lpu(fcport->bfa));
K
Krishna Gudipati 已提交
3015 3016 3017 3018
	m->nwwn = fcport->nwwn;
	m->pwwn = fcport->pwwn;
	m->port_cfg = fcport->cfg;
	m->msgtag = fcport->msgtag;
3019
	m->port_cfg.maxfrsize = cpu_to_be16(fcport->cfg.maxfrsize);
3020
	 m->use_flash_cfg = fcport->use_flash_cfg;
K
Krishna Gudipati 已提交
3021 3022 3023 3024
	bfa_dma_be_addr_set(m->stats_dma_addr, fcport->stats_pa);
	bfa_trc(fcport->bfa, m->stats_dma_addr.a32.addr_lo);
	bfa_trc(fcport->bfa, m->stats_dma_addr.a32.addr_hi);

J
Jing Huang 已提交
3025
	/*
K
Krishna Gudipati 已提交
3026 3027
	 * queue I/O message to firmware
	 */
3028
	bfa_reqq_produce(fcport->bfa, BFA_REQQ_PORT, m->mh);
K
Krishna Gudipati 已提交
3029 3030 3031
	return BFA_TRUE;
}

J
Jing Huang 已提交
3032
/*
K
Krishna Gudipati 已提交
3033 3034 3035 3036 3037 3038 3039
 * Send port disable message to firmware.
 */
static	bfa_boolean_t
bfa_fcport_send_disable(struct bfa_fcport_s *fcport)
{
	struct bfi_fcport_req_s *m;

J
Jing Huang 已提交
3040
	/*
K
Krishna Gudipati 已提交
3041 3042 3043 3044 3045
	 * Increment message tag before queue check, so that responses to old
	 * requests are discarded.
	 */
	fcport->msgtag++;

J
Jing Huang 已提交
3046
	/*
K
Krishna Gudipati 已提交
3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
	 * check for room in queue to send request now
	 */
	m = bfa_reqq_next(fcport->bfa, BFA_REQQ_PORT);
	if (!m) {
		bfa_reqq_wait(fcport->bfa, BFA_REQQ_PORT,
							&fcport->reqq_wait);
		return BFA_FALSE;
	}

	bfi_h2i_set(m->mh, BFI_MC_FCPORT, BFI_FCPORT_H2I_DISABLE_REQ,
3057
			bfa_fn_lpu(fcport->bfa));
K
Krishna Gudipati 已提交
3058 3059
	m->msgtag = fcport->msgtag;

J
Jing Huang 已提交
3060
	/*
K
Krishna Gudipati 已提交
3061 3062
	 * queue I/O message to firmware
	 */
3063
	bfa_reqq_produce(fcport->bfa, BFA_REQQ_PORT, m->mh);
K
Krishna Gudipati 已提交
3064 3065 3066 3067 3068 3069 3070

	return BFA_TRUE;
}

static void
bfa_fcport_set_wwns(struct bfa_fcport_s *fcport)
{
3071 3072
	fcport->pwwn = fcport->bfa->ioc.attr->pwwn;
	fcport->nwwn = fcport->bfa->ioc.attr->nwwn;
K
Krishna Gudipati 已提交
3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084

	bfa_trc(fcport->bfa, fcport->pwwn);
	bfa_trc(fcport->bfa, fcport->nwwn);
}

static void
bfa_fcport_send_txcredit(void *port_cbarg)
{

	struct bfa_fcport_s *fcport = port_cbarg;
	struct bfi_fcport_set_svc_params_req_s *m;

J
Jing Huang 已提交
3085
	/*
K
Krishna Gudipati 已提交
3086 3087 3088 3089 3090 3091 3092 3093 3094
	 * check for room in queue to send request now
	 */
	m = bfa_reqq_next(fcport->bfa, BFA_REQQ_PORT);
	if (!m) {
		bfa_trc(fcport->bfa, fcport->cfg.tx_bbcredit);
		return;
	}

	bfi_h2i_set(m->mh, BFI_MC_FCPORT, BFI_FCPORT_H2I_SET_SVC_PARAMS_REQ,
3095
			bfa_fn_lpu(fcport->bfa));
3096
	m->tx_bbcredit = cpu_to_be16((u16)fcport->cfg.tx_bbcredit);
3097
	m->bb_scn = fcport->cfg.bb_scn;
K
Krishna Gudipati 已提交
3098

J
Jing Huang 已提交
3099
	/*
K
Krishna Gudipati 已提交
3100 3101
	 * queue I/O message to firmware
	 */
3102
	bfa_reqq_produce(fcport->bfa, BFA_REQQ_PORT, m->mh);
K
Krishna Gudipati 已提交
3103 3104 3105 3106 3107 3108 3109
}

static void
bfa_fcport_qos_stats_swap(struct bfa_qos_stats_s *d,
	struct bfa_qos_stats_s *s)
{
	u32	*dip = (u32 *) d;
3110
	__be32	*sip = (__be32 *) s;
K
Krishna Gudipati 已提交
3111 3112 3113 3114
	int		i;

	/* Now swap the 32 bit fields */
	for (i = 0; i < (sizeof(struct bfa_qos_stats_s)/sizeof(u32)); ++i)
3115
		dip[i] = be32_to_cpu(sip[i]);
K
Krishna Gudipati 已提交
3116 3117 3118 3119 3120 3121 3122
}

static void
bfa_fcport_fcoe_stats_swap(struct bfa_fcoe_stats_s *d,
	struct bfa_fcoe_stats_s *s)
{
	u32	*dip = (u32 *) d;
3123
	__be32	*sip = (__be32 *) s;
K
Krishna Gudipati 已提交
3124 3125 3126 3127
	int		i;

	for (i = 0; i < ((sizeof(struct bfa_fcoe_stats_s))/sizeof(u32));
	     i = i + 2) {
M
Maggie Zhang 已提交
3128
#ifdef __BIG_ENDIAN
3129 3130
		dip[i] = be32_to_cpu(sip[i]);
		dip[i + 1] = be32_to_cpu(sip[i + 1]);
K
Krishna Gudipati 已提交
3131
#else
3132 3133
		dip[i] = be32_to_cpu(sip[i + 1]);
		dip[i + 1] = be32_to_cpu(sip[i]);
K
Krishna Gudipati 已提交
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
#endif
	}
}

static void
__bfa_cb_fcport_stats_get(void *cbarg, bfa_boolean_t complete)
{
	struct bfa_fcport_s *fcport = cbarg;

	if (complete) {
		if (fcport->stats_status == BFA_STATUS_OK) {
M
Maggie Zhang 已提交
3145
			struct timeval tv;
K
Krishna Gudipati 已提交
3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156

			/* Swap FC QoS or FCoE stats */
			if (bfa_ioc_get_fcmode(&fcport->bfa->ioc)) {
				bfa_fcport_qos_stats_swap(
					&fcport->stats_ret->fcqos,
					&fcport->stats->fcqos);
			} else {
				bfa_fcport_fcoe_stats_swap(
					&fcport->stats_ret->fcoe,
					&fcport->stats->fcoe);

M
Maggie Zhang 已提交
3157
				do_gettimeofday(&tv);
K
Krishna Gudipati 已提交
3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
				fcport->stats_ret->fcoe.secs_reset =
					tv.tv_sec - fcport->stats_reset_time;
			}
		}
		fcport->stats_cbfn(fcport->stats_cbarg, fcport->stats_status);
	} else {
		fcport->stats_busy = BFA_FALSE;
		fcport->stats_status = BFA_STATUS_OK;
	}
}

static void
bfa_fcport_stats_get_timeout(void *cbarg)
{
	struct bfa_fcport_s *fcport = (struct bfa_fcport_s *) cbarg;

	bfa_trc(fcport->bfa, fcport->stats_qfull);

	if (fcport->stats_qfull) {
		bfa_reqq_wcancel(&fcport->stats_reqq_wait);
		fcport->stats_qfull = BFA_FALSE;
	}

	fcport->stats_status = BFA_STATUS_ETIMER;
	bfa_cb_queue(fcport->bfa, &fcport->hcb_qe, __bfa_cb_fcport_stats_get,
		fcport);
}

static void
bfa_fcport_send_stats_get(void *cbarg)
{
	struct bfa_fcport_s *fcport = (struct bfa_fcport_s *) cbarg;
	struct bfi_fcport_req_s *msg;

	msg = bfa_reqq_next(fcport->bfa, BFA_REQQ_PORT);

	if (!msg) {
		fcport->stats_qfull = BFA_TRUE;
		bfa_reqq_winit(&fcport->stats_reqq_wait,
				bfa_fcport_send_stats_get, fcport);
		bfa_reqq_wait(fcport->bfa, BFA_REQQ_PORT,
				&fcport->stats_reqq_wait);
		return;
	}
	fcport->stats_qfull = BFA_FALSE;

3204
	memset(msg, 0, sizeof(struct bfi_fcport_req_s));
K
Krishna Gudipati 已提交
3205
	bfi_h2i_set(msg->mh, BFI_MC_FCPORT, BFI_FCPORT_H2I_STATS_GET_REQ,
3206 3207
			bfa_fn_lpu(fcport->bfa));
	bfa_reqq_produce(fcport->bfa, BFA_REQQ_PORT, msg->mh);
K
Krishna Gudipati 已提交
3208 3209 3210 3211 3212 3213 3214 3215
}

static void
__bfa_cb_fcport_stats_clr(void *cbarg, bfa_boolean_t complete)
{
	struct bfa_fcport_s *fcport = cbarg;

	if (complete) {
M
Maggie Zhang 已提交
3216
		struct timeval tv;
K
Krishna Gudipati 已提交
3217

J
Jing Huang 已提交
3218
		/*
K
Krishna Gudipati 已提交
3219 3220
		 * re-initialize time stamp for stats reset
		 */
M
Maggie Zhang 已提交
3221
		do_gettimeofday(&tv);
K
Krishna Gudipati 已提交
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265
		fcport->stats_reset_time = tv.tv_sec;

		fcport->stats_cbfn(fcport->stats_cbarg, fcport->stats_status);
	} else {
		fcport->stats_busy = BFA_FALSE;
		fcport->stats_status = BFA_STATUS_OK;
	}
}

static void
bfa_fcport_stats_clr_timeout(void *cbarg)
{
	struct bfa_fcport_s *fcport = (struct bfa_fcport_s *) cbarg;

	bfa_trc(fcport->bfa, fcport->stats_qfull);

	if (fcport->stats_qfull) {
		bfa_reqq_wcancel(&fcport->stats_reqq_wait);
		fcport->stats_qfull = BFA_FALSE;
	}

	fcport->stats_status = BFA_STATUS_ETIMER;
	bfa_cb_queue(fcport->bfa, &fcport->hcb_qe,
			__bfa_cb_fcport_stats_clr, fcport);
}

static void
bfa_fcport_send_stats_clear(void *cbarg)
{
	struct bfa_fcport_s *fcport = (struct bfa_fcport_s *) cbarg;
	struct bfi_fcport_req_s *msg;

	msg = bfa_reqq_next(fcport->bfa, BFA_REQQ_PORT);

	if (!msg) {
		fcport->stats_qfull = BFA_TRUE;
		bfa_reqq_winit(&fcport->stats_reqq_wait,
				bfa_fcport_send_stats_clear, fcport);
		bfa_reqq_wait(fcport->bfa, BFA_REQQ_PORT,
						&fcport->stats_reqq_wait);
		return;
	}
	fcport->stats_qfull = BFA_FALSE;

3266
	memset(msg, 0, sizeof(struct bfi_fcport_req_s));
K
Krishna Gudipati 已提交
3267
	bfi_h2i_set(msg->mh, BFI_MC_FCPORT, BFI_FCPORT_H2I_STATS_CLEAR_REQ,
3268 3269
			bfa_fn_lpu(fcport->bfa));
	bfa_reqq_produce(fcport->bfa, BFA_REQQ_PORT, msg->mh);
K
Krishna Gudipati 已提交
3270 3271
}

J
Jing Huang 已提交
3272
/*
K
Krishna Gudipati 已提交
3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285
 * Handle trunk SCN event from firmware.
 */
static void
bfa_trunk_scn(struct bfa_fcport_s *fcport, struct bfi_fcport_trunk_scn_s *scn)
{
	struct bfa_fcport_trunk_s *trunk = &fcport->trunk;
	struct bfi_fcport_trunk_link_s *tlink;
	struct bfa_trunk_link_attr_s *lattr;
	enum bfa_trunk_state state_prev;
	int i;
	int link_bm = 0;

	bfa_trc(fcport->bfa, fcport->cfg.trunked);
3286 3287
	WARN_ON(scn->trunk_state != BFA_TRUNK_ONLINE &&
		   scn->trunk_state != BFA_TRUNK_OFFLINE);
K
Krishna Gudipati 已提交
3288 3289 3290 3291 3292

	bfa_trc(fcport->bfa, trunk->attr.state);
	bfa_trc(fcport->bfa, scn->trunk_state);
	bfa_trc(fcport->bfa, scn->trunk_speed);

J
Jing Huang 已提交
3293
	/*
K
Krishna Gudipati 已提交
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307
	 * Save off new state for trunk attribute query
	 */
	state_prev = trunk->attr.state;
	if (fcport->cfg.trunked && (trunk->attr.state != BFA_TRUNK_DISABLED))
		trunk->attr.state = scn->trunk_state;
	trunk->attr.speed = scn->trunk_speed;
	for (i = 0; i < BFA_TRUNK_MAX_PORTS; i++) {
		lattr = &trunk->attr.link_attr[i];
		tlink = &scn->tlink[i];

		lattr->link_state = tlink->state;
		lattr->trunk_wwn  = tlink->trunk_wwn;
		lattr->fctl	  = tlink->fctl;
		lattr->speed	  = tlink->speed;
3308
		lattr->deskew	  = be32_to_cpu(tlink->deskew);
K
Krishna Gudipati 已提交
3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340

		if (tlink->state == BFA_TRUNK_LINK_STATE_UP) {
			fcport->speed	 = tlink->speed;
			fcport->topology = BFA_PORT_TOPOLOGY_P2P;
			link_bm |= 1 << i;
		}

		bfa_trc(fcport->bfa, lattr->link_state);
		bfa_trc(fcport->bfa, lattr->trunk_wwn);
		bfa_trc(fcport->bfa, lattr->fctl);
		bfa_trc(fcport->bfa, lattr->speed);
		bfa_trc(fcport->bfa, lattr->deskew);
	}

	switch (link_bm) {
	case 3:
		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
			BFA_PL_EID_TRUNK_SCN, 0, "Trunk up(0,1)");
		break;
	case 2:
		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
			BFA_PL_EID_TRUNK_SCN, 0, "Trunk up(-,1)");
		break;
	case 1:
		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
			BFA_PL_EID_TRUNK_SCN, 0, "Trunk up(0,-)");
		break;
	default:
		bfa_plog_str(fcport->bfa->plog, BFA_PL_MID_HAL,
			BFA_PL_EID_TRUNK_SCN, 0, "Trunk down");
	}

J
Jing Huang 已提交
3341
	/*
K
Krishna Gudipati 已提交
3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356
	 * Notify upper layers if trunk state changed.
	 */
	if ((state_prev != trunk->attr.state) ||
		(scn->trunk_state == BFA_TRUNK_OFFLINE)) {
		bfa_fcport_scn(fcport, (scn->trunk_state == BFA_TRUNK_ONLINE) ?
			BFA_PORT_LINKUP : BFA_PORT_LINKDOWN, BFA_TRUE);
	}
}

static void
bfa_trunk_iocdisable(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);
	int i = 0;

J
Jing Huang 已提交
3357
	/*
K
Krishna Gudipati 已提交
3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378
	 * In trunked mode, notify upper layers that link is down
	 */
	if (fcport->cfg.trunked) {
		if (fcport->trunk.attr.state == BFA_TRUNK_ONLINE)
			bfa_fcport_scn(fcport, BFA_PORT_LINKDOWN, BFA_TRUE);

		fcport->trunk.attr.state = BFA_TRUNK_OFFLINE;
		fcport->trunk.attr.speed = BFA_PORT_SPEED_UNKNOWN;
		for (i = 0; i < BFA_TRUNK_MAX_PORTS; i++) {
			fcport->trunk.attr.link_attr[i].trunk_wwn = 0;
			fcport->trunk.attr.link_attr[i].fctl =
						BFA_TRUNK_LINK_FCTL_NORMAL;
			fcport->trunk.attr.link_attr[i].link_state =
						BFA_TRUNK_LINK_STATE_DN_LINKDN;
			fcport->trunk.attr.link_attr[i].speed =
						BFA_PORT_SPEED_UNKNOWN;
			fcport->trunk.attr.link_attr[i].deskew = 0;
		}
	}
}

J
Jing Huang 已提交
3379
/*
K
Krishna Gudipati 已提交
3380 3381 3382 3383 3384 3385 3386
 * Called to initialize port attributes
 */
void
bfa_fcport_init(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

J
Jing Huang 已提交
3387
	/*
K
Krishna Gudipati 已提交
3388 3389 3390 3391 3392 3393 3394 3395
	 * Initialize port attributes from IOC hardware data.
	 */
	bfa_fcport_set_wwns(fcport);
	if (fcport->cfg.maxfrsize == 0)
		fcport->cfg.maxfrsize = bfa_ioc_maxfrsize(&bfa->ioc);
	fcport->cfg.rx_bbcredit = bfa_ioc_rx_bbcredit(&bfa->ioc);
	fcport->speed_sup = bfa_ioc_speed_sup(&bfa->ioc);

3396 3397 3398
	if (bfa_fcport_is_pbcdisabled(bfa))
		bfa->modules.port.pbc_disabled = BFA_TRUE;

3399 3400 3401
	WARN_ON(!fcport->cfg.maxfrsize);
	WARN_ON(!fcport->cfg.rx_bbcredit);
	WARN_ON(!fcport->speed_sup);
K
Krishna Gudipati 已提交
3402 3403
}

J
Jing Huang 已提交
3404
/*
K
Krishna Gudipati 已提交
3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
 * Firmware message handler.
 */
void
bfa_fcport_isr(struct bfa_s *bfa, struct bfi_msg_s *msg)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);
	union bfi_fcport_i2h_msg_u i2hmsg;

	i2hmsg.msg = msg;
	fcport->event_arg.i2hmsg = i2hmsg;

	bfa_trc(bfa, msg->mhdr.msg_id);
	bfa_trc(bfa, bfa_sm_to_state(hal_port_sm_table, fcport->sm));

	switch (msg->mhdr.msg_id) {
	case BFI_FCPORT_I2H_ENABLE_RSP:
3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440
		if (fcport->msgtag == i2hmsg.penable_rsp->msgtag) {

			if (fcport->use_flash_cfg) {
				fcport->cfg = i2hmsg.penable_rsp->port_cfg;
				fcport->cfg.maxfrsize =
					cpu_to_be16(fcport->cfg.maxfrsize);
				fcport->cfg.path_tov =
					cpu_to_be16(fcport->cfg.path_tov);
				fcport->cfg.q_depth =
					cpu_to_be16(fcport->cfg.q_depth);

				if (fcport->cfg.trunked)
					fcport->trunk.attr.state =
						BFA_TRUNK_OFFLINE;
				else
					fcport->trunk.attr.state =
						BFA_TRUNK_DISABLED;
				fcport->use_flash_cfg = BFA_FALSE;
			}

K
Krishna Gudipati 已提交
3441
			bfa_sm_send_event(fcport, BFA_FCPORT_SM_FWRSP);
3442
		}
K
Krishna Gudipati 已提交
3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497
		break;

	case BFI_FCPORT_I2H_DISABLE_RSP:
		if (fcport->msgtag == i2hmsg.penable_rsp->msgtag)
			bfa_sm_send_event(fcport, BFA_FCPORT_SM_FWRSP);
		break;

	case BFI_FCPORT_I2H_EVENT:
		if (i2hmsg.event->link_state.linkstate == BFA_PORT_LINKUP)
			bfa_sm_send_event(fcport, BFA_FCPORT_SM_LINKUP);
		else
			bfa_sm_send_event(fcport, BFA_FCPORT_SM_LINKDOWN);
		break;

	case BFI_FCPORT_I2H_TRUNK_SCN:
		bfa_trunk_scn(fcport, i2hmsg.trunk_scn);
		break;

	case BFI_FCPORT_I2H_STATS_GET_RSP:
		/*
		 * check for timer pop before processing the rsp
		 */
		if (fcport->stats_busy == BFA_FALSE ||
		    fcport->stats_status == BFA_STATUS_ETIMER)
			break;

		bfa_timer_stop(&fcport->timer);
		fcport->stats_status = i2hmsg.pstatsget_rsp->status;
		bfa_cb_queue(fcport->bfa, &fcport->hcb_qe,
				__bfa_cb_fcport_stats_get, fcport);
		break;

	case BFI_FCPORT_I2H_STATS_CLEAR_RSP:
		/*
		 * check for timer pop before processing the rsp
		 */
		if (fcport->stats_busy == BFA_FALSE ||
		    fcport->stats_status == BFA_STATUS_ETIMER)
			break;

		bfa_timer_stop(&fcport->timer);
		fcport->stats_status = BFA_STATUS_OK;
		bfa_cb_queue(fcport->bfa, &fcport->hcb_qe,
				__bfa_cb_fcport_stats_clr, fcport);
		break;

	case BFI_FCPORT_I2H_ENABLE_AEN:
		bfa_sm_send_event(fcport, BFA_FCPORT_SM_ENABLE);
		break;

	case BFI_FCPORT_I2H_DISABLE_AEN:
		bfa_sm_send_event(fcport, BFA_FCPORT_SM_DISABLE);
		break;

	default:
3498
		WARN_ON(1);
K
Krishna Gudipati 已提交
3499 3500 3501 3502
	break;
	}
}

J
Jing Huang 已提交
3503
/*
K
Krishna Gudipati 已提交
3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
 * Registered callback for port events.
 */
void
bfa_fcport_event_register(struct bfa_s *bfa,
				void (*cbfn) (void *cbarg,
				enum bfa_port_linkstate event),
				void *cbarg)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	fcport->event_cbfn = cbfn;
	fcport->event_cbarg = cbarg;
}

bfa_status_t
bfa_fcport_enable(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

3523 3524 3525
	if (bfa_fcport_is_pbcdisabled(bfa))
		return BFA_STATUS_PBC;

K
Krishna Gudipati 已提交
3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538
	if (bfa_ioc_is_disabled(&bfa->ioc))
		return BFA_STATUS_IOC_DISABLED;

	if (fcport->diag_busy)
		return BFA_STATUS_DIAG_BUSY;

	bfa_sm_send_event(BFA_FCPORT_MOD(bfa), BFA_FCPORT_SM_ENABLE);
	return BFA_STATUS_OK;
}

bfa_status_t
bfa_fcport_disable(struct bfa_s *bfa)
{
3539 3540
	if (bfa_fcport_is_pbcdisabled(bfa))
		return BFA_STATUS_PBC;
K
Krishna Gudipati 已提交
3541 3542 3543 3544 3545 3546 3547 3548

	if (bfa_ioc_is_disabled(&bfa->ioc))
		return BFA_STATUS_IOC_DISABLED;

	bfa_sm_send_event(BFA_FCPORT_MOD(bfa), BFA_FCPORT_SM_DISABLE);
	return BFA_STATUS_OK;
}

3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563
/* If PBC is disabled on port, return error */
bfa_status_t
bfa_fcport_is_pbcdisabled(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);
	struct bfa_iocfc_s *iocfc = &bfa->iocfc;
	struct bfi_iocfc_cfgrsp_s *cfgrsp = iocfc->cfgrsp;

	if (cfgrsp->pbc_cfg.port_enabled == BFI_PBC_PORT_DISABLED) {
		bfa_trc(bfa, fcport->pwwn);
		return BFA_STATUS_PBC;
	}
	return BFA_STATUS_OK;
}

J
Jing Huang 已提交
3564
/*
K
Krishna Gudipati 已提交
3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580
 * Configure port speed.
 */
bfa_status_t
bfa_fcport_cfg_speed(struct bfa_s *bfa, enum bfa_port_speed speed)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	bfa_trc(bfa, speed);

	if (fcport->cfg.trunked == BFA_TRUE)
		return BFA_STATUS_TRUNK_ENABLED;
	if ((speed != BFA_PORT_SPEED_AUTO) && (speed > fcport->speed_sup)) {
		bfa_trc(bfa, fcport->speed_sup);
		return BFA_STATUS_UNSUPP_SPEED;
	}

3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
	/* For Mezz card, port speed entered needs to be checked */
	if (bfa_mfg_is_mezz(fcport->bfa->ioc.attr->card_type)) {
		if (bfa_ioc_get_type(&fcport->bfa->ioc) == BFA_IOC_TYPE_FC) {
			/* For CT2, 1G is not supported */
			if ((speed == BFA_PORT_SPEED_1GBPS) &&
			    (bfa_asic_id_ct2(bfa->ioc.pcidev.device_id)))
				return BFA_STATUS_UNSUPP_SPEED;

			/* Already checked for Auto Speed and Max Speed supp */
			if (!(speed == BFA_PORT_SPEED_1GBPS ||
			      speed == BFA_PORT_SPEED_2GBPS ||
			      speed == BFA_PORT_SPEED_4GBPS ||
			      speed == BFA_PORT_SPEED_8GBPS ||
			      speed == BFA_PORT_SPEED_16GBPS ||
			      speed == BFA_PORT_SPEED_AUTO))
				return BFA_STATUS_UNSUPP_SPEED;
		} else {
			if (speed != BFA_PORT_SPEED_10GBPS)
				return BFA_STATUS_UNSUPP_SPEED;
		}
	}

K
Krishna Gudipati 已提交
3603 3604 3605 3606 3607
	fcport->cfg.speed = speed;

	return BFA_STATUS_OK;
}

J
Jing Huang 已提交
3608
/*
K
Krishna Gudipati 已提交
3609 3610 3611 3612 3613 3614 3615 3616 3617 3618
 * Get current speed.
 */
enum bfa_port_speed
bfa_fcport_get_speed(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return fcport->speed;
}

J
Jing Huang 已提交
3619
/*
K
Krishna Gudipati 已提交
3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
 * Configure port topology.
 */
bfa_status_t
bfa_fcport_cfg_topology(struct bfa_s *bfa, enum bfa_port_topology topology)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	bfa_trc(bfa, topology);
	bfa_trc(bfa, fcport->cfg.topology);

	switch (topology) {
	case BFA_PORT_TOPOLOGY_P2P:
	case BFA_PORT_TOPOLOGY_LOOP:
	case BFA_PORT_TOPOLOGY_AUTO:
		break;

	default:
		return BFA_STATUS_EINVAL;
	}

	fcport->cfg.topology = topology;
	return BFA_STATUS_OK;
}

J
Jing Huang 已提交
3644
/*
K
Krishna Gudipati 已提交
3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 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
 * Get current topology.
 */
enum bfa_port_topology
bfa_fcport_get_topology(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return fcport->topology;
}

bfa_status_t
bfa_fcport_cfg_hardalpa(struct bfa_s *bfa, u8 alpa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	bfa_trc(bfa, alpa);
	bfa_trc(bfa, fcport->cfg.cfg_hardalpa);
	bfa_trc(bfa, fcport->cfg.hardalpa);

	fcport->cfg.cfg_hardalpa = BFA_TRUE;
	fcport->cfg.hardalpa = alpa;

	return BFA_STATUS_OK;
}

bfa_status_t
bfa_fcport_clr_hardalpa(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	bfa_trc(bfa, fcport->cfg.cfg_hardalpa);
	bfa_trc(bfa, fcport->cfg.hardalpa);

	fcport->cfg.cfg_hardalpa = BFA_FALSE;
	return BFA_STATUS_OK;
}

bfa_boolean_t
bfa_fcport_get_hardalpa(struct bfa_s *bfa, u8 *alpa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	*alpa = fcport->cfg.hardalpa;
	return fcport->cfg.cfg_hardalpa;
}

u8
bfa_fcport_get_myalpa(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return fcport->myalpa;
}

bfa_status_t
bfa_fcport_cfg_maxfrsize(struct bfa_s *bfa, u16 maxfrsize)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	bfa_trc(bfa, maxfrsize);
	bfa_trc(bfa, fcport->cfg.maxfrsize);

	/* with in range */
	if ((maxfrsize > FC_MAX_PDUSZ) || (maxfrsize < FC_MIN_PDUSZ))
		return BFA_STATUS_INVLD_DFSZ;

	/* power of 2, if not the max frame size of 2112 */
	if ((maxfrsize != FC_MAX_PDUSZ) && (maxfrsize & (maxfrsize - 1)))
		return BFA_STATUS_INVLD_DFSZ;

	fcport->cfg.maxfrsize = maxfrsize;
	return BFA_STATUS_OK;
}

u16
bfa_fcport_get_maxfrsize(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return fcport->cfg.maxfrsize;
}

u8
bfa_fcport_get_rx_bbcredit(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return fcport->cfg.rx_bbcredit;
}

void
3736
bfa_fcport_set_tx_bbcredit(struct bfa_s *bfa, u16 tx_bbcredit, u8 bb_scn)
K
Krishna Gudipati 已提交
3737 3738 3739 3740
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	fcport->cfg.tx_bbcredit = (u8)tx_bbcredit;
3741 3742 3743
	fcport->cfg.bb_scn = bb_scn;
	if (bb_scn)
		fcport->bbsc_op_state = BFA_TRUE;
K
Krishna Gudipati 已提交
3744 3745 3746
	bfa_fcport_send_txcredit(fcport);
}

J
Jing Huang 已提交
3747
/*
K
Krishna Gudipati 已提交
3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765
 * Get port attributes.
 */

wwn_t
bfa_fcport_get_wwn(struct bfa_s *bfa, bfa_boolean_t node)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);
	if (node)
		return fcport->nwwn;
	else
		return fcport->pwwn;
}

void
bfa_fcport_get_attr(struct bfa_s *bfa, struct bfa_port_attr_s *attr)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

3766
	memset(attr, 0, sizeof(struct bfa_port_attr_s));
K
Krishna Gudipati 已提交
3767 3768 3769 3770

	attr->nwwn = fcport->nwwn;
	attr->pwwn = fcport->pwwn;

3771 3772
	attr->factorypwwn =  bfa->ioc.attr->mfg_pwwn;
	attr->factorynwwn =  bfa->ioc.attr->mfg_nwwn;
K
Krishna Gudipati 已提交
3773

3774
	memcpy(&attr->pport_cfg, &fcport->cfg,
K
Krishna Gudipati 已提交
3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793
		sizeof(struct bfa_port_cfg_s));
	/* speed attributes */
	attr->pport_cfg.speed = fcport->cfg.speed;
	attr->speed_supported = fcport->speed_sup;
	attr->speed = fcport->speed;
	attr->cos_supported = FC_CLASS_3;

	/* topology attributes */
	attr->pport_cfg.topology = fcport->cfg.topology;
	attr->topology = fcport->topology;
	attr->pport_cfg.trunked = fcport->cfg.trunked;

	/* beacon attributes */
	attr->beacon = fcport->beacon;
	attr->link_e2e_beacon = fcport->link_e2e_beacon;

	attr->pport_cfg.path_tov  = bfa_fcpim_path_tov_get(bfa);
	attr->pport_cfg.q_depth  = bfa_fcpim_qdepth_get(bfa);
	attr->port_state = bfa_sm_to_state(hal_port_sm_table, fcport->sm);
3794
	attr->bbsc_op_status =  fcport->bbsc_op_state;
3795 3796 3797 3798 3799 3800 3801 3802 3803

	/* PBC Disabled State */
	if (bfa_fcport_is_pbcdisabled(bfa))
		attr->port_state = BFA_PORT_ST_PREBOOT_DISABLED;
	else {
		if (bfa_ioc_is_disabled(&fcport->bfa->ioc))
			attr->port_state = BFA_PORT_ST_IOCDIS;
		else if (bfa_ioc_fw_mismatch(&fcport->bfa->ioc))
			attr->port_state = BFA_PORT_ST_FWMISMATCH;
3804 3805
		else if (bfa_ioc_is_acq_addr(&fcport->bfa->ioc))
			attr->port_state = BFA_PORT_ST_ACQ_ADDR;
3806
	}
K
Krishna Gudipati 已提交
3807 3808 3809 3810 3811 3812 3813

	/* FCoE vlan */
	attr->fcoe_vlan = fcport->fcoe_vlan;
}

#define BFA_FCPORT_STATS_TOV	1000

J
Jing Huang 已提交
3814
/*
K
Krishna Gudipati 已提交
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
 * Fetch port statistics (FCQoS or FCoE).
 */
bfa_status_t
bfa_fcport_get_stats(struct bfa_s *bfa, union bfa_fcport_stats_u *stats,
	bfa_cb_port_t cbfn, void *cbarg)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	if (fcport->stats_busy) {
		bfa_trc(bfa, fcport->stats_busy);
		return BFA_STATUS_DEVBUSY;
	}

	fcport->stats_busy  = BFA_TRUE;
	fcport->stats_ret   = stats;
	fcport->stats_cbfn  = cbfn;
	fcport->stats_cbarg = cbarg;

	bfa_fcport_send_stats_get(fcport);

	bfa_timer_start(bfa, &fcport->timer, bfa_fcport_stats_get_timeout,
			fcport, BFA_FCPORT_STATS_TOV);
	return BFA_STATUS_OK;
}

J
Jing Huang 已提交
3840
/*
K
Krishna Gudipati 已提交
3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864
 * Reset port statistics (FCQoS or FCoE).
 */
bfa_status_t
bfa_fcport_clear_stats(struct bfa_s *bfa, bfa_cb_port_t cbfn, void *cbarg)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	if (fcport->stats_busy) {
		bfa_trc(bfa, fcport->stats_busy);
		return BFA_STATUS_DEVBUSY;
	}

	fcport->stats_busy  = BFA_TRUE;
	fcport->stats_cbfn  = cbfn;
	fcport->stats_cbarg = cbarg;

	bfa_fcport_send_stats_clear(fcport);

	bfa_timer_start(bfa, &fcport->timer, bfa_fcport_stats_clr_timeout,
			fcport, BFA_FCPORT_STATS_TOV);
	return BFA_STATUS_OK;
}


J
Jing Huang 已提交
3865
/*
K
Krishna Gudipati 已提交
3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886
 * Fetch port attributes.
 */
bfa_boolean_t
bfa_fcport_is_disabled(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return bfa_sm_to_state(hal_port_sm_table, fcport->sm) ==
		BFA_PORT_ST_DISABLED;

}

bfa_boolean_t
bfa_fcport_is_ratelim(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return fcport->cfg.ratelimit ? BFA_TRUE : BFA_FALSE;

}

3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898
/*
 *	Enable/Disable FAA feature in port config
 */
void
bfa_fcport_cfg_faa(struct bfa_s *bfa, u8 state)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	bfa_trc(bfa, state);
	fcport->cfg.faa_state = state;
}

J
Jing Huang 已提交
3899
/*
K
Krishna Gudipati 已提交
3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911
 * Get default minimum ratelim speed
 */
enum bfa_port_speed
bfa_fcport_get_ratelim_speed(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	bfa_trc(bfa, fcport->cfg.trl_def_speed);
	return fcport->cfg.trl_def_speed;

}

3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927
void
bfa_fcport_beacon(void *dev, bfa_boolean_t beacon,
		  bfa_boolean_t link_e2e_beacon)
{
	struct bfa_s *bfa = dev;
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	bfa_trc(bfa, beacon);
	bfa_trc(bfa, link_e2e_beacon);
	bfa_trc(bfa, fcport->beacon);
	bfa_trc(bfa, fcport->link_e2e_beacon);

	fcport->beacon = beacon;
	fcport->link_e2e_beacon = link_e2e_beacon;
}

K
Krishna Gudipati 已提交
3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946
bfa_boolean_t
bfa_fcport_is_linkup(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return	(!fcport->cfg.trunked &&
		 bfa_sm_cmp_state(fcport, bfa_fcport_sm_linkup)) ||
		(fcport->cfg.trunked &&
		 fcport->trunk.attr.state == BFA_TRUNK_ONLINE);
}

bfa_boolean_t
bfa_fcport_is_qos_enabled(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return fcport->cfg.qos_enabled;
}

3947 3948 3949 3950 3951 3952 3953 3954
bfa_boolean_t
bfa_fcport_is_trunk_enabled(struct bfa_s *bfa)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(bfa);

	return fcport->cfg.trunked;
}

J
Jing Huang 已提交
3955
/*
K
Krishna Gudipati 已提交
3956 3957
 * Rport State machine functions
 */
J
Jing Huang 已提交
3958
/*
K
Krishna Gudipati 已提交
3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 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
 * Beginning state, only online event expected.
 */
static void
bfa_rport_sm_uninit(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_CREATE:
		bfa_stats(rp, sm_un_cr);
		bfa_sm_set_state(rp, bfa_rport_sm_created);
		break;

	default:
		bfa_stats(rp, sm_un_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

static void
bfa_rport_sm_created(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_ONLINE:
		bfa_stats(rp, sm_cr_on);
		if (bfa_rport_send_fwcreate(rp))
			bfa_sm_set_state(rp, bfa_rport_sm_fwcreate);
		else
			bfa_sm_set_state(rp, bfa_rport_sm_fwcreate_qfull);
		break;

	case BFA_RPORT_SM_DELETE:
		bfa_stats(rp, sm_cr_del);
		bfa_sm_set_state(rp, bfa_rport_sm_uninit);
		bfa_rport_free(rp);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_cr_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_iocdisable);
		break;

	default:
		bfa_stats(rp, sm_cr_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

J
Jing Huang 已提交
4011
/*
K
Krishna Gudipati 已提交
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 4043 4044 4045 4046 4047
 * Waiting for rport create response from firmware.
 */
static void
bfa_rport_sm_fwcreate(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_FWRSP:
		bfa_stats(rp, sm_fwc_rsp);
		bfa_sm_set_state(rp, bfa_rport_sm_online);
		bfa_rport_online_cb(rp);
		break;

	case BFA_RPORT_SM_DELETE:
		bfa_stats(rp, sm_fwc_del);
		bfa_sm_set_state(rp, bfa_rport_sm_delete_pending);
		break;

	case BFA_RPORT_SM_OFFLINE:
		bfa_stats(rp, sm_fwc_off);
		bfa_sm_set_state(rp, bfa_rport_sm_offline_pending);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_fwc_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_iocdisable);
		break;

	default:
		bfa_stats(rp, sm_fwc_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

J
Jing Huang 已提交
4048
/*
K
Krishna Gudipati 已提交
4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 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
 * Request queue is full, awaiting queue resume to send create request.
 */
static void
bfa_rport_sm_fwcreate_qfull(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_QRESUME:
		bfa_sm_set_state(rp, bfa_rport_sm_fwcreate);
		bfa_rport_send_fwcreate(rp);
		break;

	case BFA_RPORT_SM_DELETE:
		bfa_stats(rp, sm_fwc_del);
		bfa_sm_set_state(rp, bfa_rport_sm_uninit);
		bfa_reqq_wcancel(&rp->reqq_wait);
		bfa_rport_free(rp);
		break;

	case BFA_RPORT_SM_OFFLINE:
		bfa_stats(rp, sm_fwc_off);
		bfa_sm_set_state(rp, bfa_rport_sm_offline);
		bfa_reqq_wcancel(&rp->reqq_wait);
		bfa_rport_offline_cb(rp);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_fwc_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_iocdisable);
		bfa_reqq_wcancel(&rp->reqq_wait);
		break;

	default:
		bfa_stats(rp, sm_fwc_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

J
Jing Huang 已提交
4089
/*
K
Krishna Gudipati 已提交
4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134
 * Online state - normal parking state.
 */
static void
bfa_rport_sm_online(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	struct bfi_rport_qos_scn_s *qos_scn;

	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_OFFLINE:
		bfa_stats(rp, sm_on_off);
		if (bfa_rport_send_fwdelete(rp))
			bfa_sm_set_state(rp, bfa_rport_sm_fwdelete);
		else
			bfa_sm_set_state(rp, bfa_rport_sm_fwdelete_qfull);
		break;

	case BFA_RPORT_SM_DELETE:
		bfa_stats(rp, sm_on_del);
		if (bfa_rport_send_fwdelete(rp))
			bfa_sm_set_state(rp, bfa_rport_sm_deleting);
		else
			bfa_sm_set_state(rp, bfa_rport_sm_deleting_qfull);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_on_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_iocdisable);
		break;

	case BFA_RPORT_SM_SET_SPEED:
		bfa_rport_send_fwspeed(rp);
		break;

	case BFA_RPORT_SM_QOS_SCN:
		qos_scn = (struct bfi_rport_qos_scn_s *) rp->event_arg.fw_msg;
		rp->qos_attr = qos_scn->new_qos_attr;
		bfa_trc(rp->bfa, qos_scn->old_qos_attr.qos_flow_id);
		bfa_trc(rp->bfa, qos_scn->new_qos_attr.qos_flow_id);
		bfa_trc(rp->bfa, qos_scn->old_qos_attr.qos_priority);
		bfa_trc(rp->bfa, qos_scn->new_qos_attr.qos_priority);

		qos_scn->old_qos_attr.qos_flow_id  =
4135
			be32_to_cpu(qos_scn->old_qos_attr.qos_flow_id);
K
Krishna Gudipati 已提交
4136
		qos_scn->new_qos_attr.qos_flow_id  =
4137
			be32_to_cpu(qos_scn->new_qos_attr.qos_flow_id);
K
Krishna Gudipati 已提交
4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156

		if (qos_scn->old_qos_attr.qos_flow_id !=
			qos_scn->new_qos_attr.qos_flow_id)
			bfa_cb_rport_qos_scn_flowid(rp->rport_drv,
						    qos_scn->old_qos_attr,
						    qos_scn->new_qos_attr);
		if (qos_scn->old_qos_attr.qos_priority !=
			qos_scn->new_qos_attr.qos_priority)
			bfa_cb_rport_qos_scn_prio(rp->rport_drv,
						  qos_scn->old_qos_attr,
						  qos_scn->new_qos_attr);
		break;

	default:
		bfa_stats(rp, sm_on_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

J
Jing Huang 已提交
4157
/*
K
Krishna Gudipati 已提交
4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219
 * Firmware rport is being deleted - awaiting f/w response.
 */
static void
bfa_rport_sm_fwdelete(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_FWRSP:
		bfa_stats(rp, sm_fwd_rsp);
		bfa_sm_set_state(rp, bfa_rport_sm_offline);
		bfa_rport_offline_cb(rp);
		break;

	case BFA_RPORT_SM_DELETE:
		bfa_stats(rp, sm_fwd_del);
		bfa_sm_set_state(rp, bfa_rport_sm_deleting);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_fwd_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_iocdisable);
		bfa_rport_offline_cb(rp);
		break;

	default:
		bfa_stats(rp, sm_fwd_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

static void
bfa_rport_sm_fwdelete_qfull(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_QRESUME:
		bfa_sm_set_state(rp, bfa_rport_sm_fwdelete);
		bfa_rport_send_fwdelete(rp);
		break;

	case BFA_RPORT_SM_DELETE:
		bfa_stats(rp, sm_fwd_del);
		bfa_sm_set_state(rp, bfa_rport_sm_deleting_qfull);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_fwd_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_iocdisable);
		bfa_reqq_wcancel(&rp->reqq_wait);
		bfa_rport_offline_cb(rp);
		break;

	default:
		bfa_stats(rp, sm_fwd_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

J
Jing Huang 已提交
4220
/*
K
Krishna Gudipati 已提交
4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254
 * Offline state.
 */
static void
bfa_rport_sm_offline(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_DELETE:
		bfa_stats(rp, sm_off_del);
		bfa_sm_set_state(rp, bfa_rport_sm_uninit);
		bfa_rport_free(rp);
		break;

	case BFA_RPORT_SM_ONLINE:
		bfa_stats(rp, sm_off_on);
		if (bfa_rport_send_fwcreate(rp))
			bfa_sm_set_state(rp, bfa_rport_sm_fwcreate);
		else
			bfa_sm_set_state(rp, bfa_rport_sm_fwcreate_qfull);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_off_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_iocdisable);
		break;

	default:
		bfa_stats(rp, sm_off_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

J
Jing Huang 已提交
4255
/*
K
Krishna Gudipati 已提交
4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306
 * Rport is deleted, waiting for firmware response to delete.
 */
static void
bfa_rport_sm_deleting(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_FWRSP:
		bfa_stats(rp, sm_del_fwrsp);
		bfa_sm_set_state(rp, bfa_rport_sm_uninit);
		bfa_rport_free(rp);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_del_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_uninit);
		bfa_rport_free(rp);
		break;

	default:
		bfa_sm_fault(rp->bfa, event);
	}
}

static void
bfa_rport_sm_deleting_qfull(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_QRESUME:
		bfa_stats(rp, sm_del_fwrsp);
		bfa_sm_set_state(rp, bfa_rport_sm_deleting);
		bfa_rport_send_fwdelete(rp);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_del_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_uninit);
		bfa_reqq_wcancel(&rp->reqq_wait);
		bfa_rport_free(rp);
		break;

	default:
		bfa_sm_fault(rp->bfa, event);
	}
}

J
Jing Huang 已提交
4307
/*
K
Krishna Gudipati 已提交
4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337
 * Waiting for rport create response from firmware. A delete is pending.
 */
static void
bfa_rport_sm_delete_pending(struct bfa_rport_s *rp,
				enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_FWRSP:
		bfa_stats(rp, sm_delp_fwrsp);
		if (bfa_rport_send_fwdelete(rp))
			bfa_sm_set_state(rp, bfa_rport_sm_deleting);
		else
			bfa_sm_set_state(rp, bfa_rport_sm_deleting_qfull);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_delp_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_uninit);
		bfa_rport_free(rp);
		break;

	default:
		bfa_stats(rp, sm_delp_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

J
Jing Huang 已提交
4338
/*
K
Krishna Gudipati 已提交
4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372
 * Waiting for rport create response from firmware. Rport offline is pending.
 */
static void
bfa_rport_sm_offline_pending(struct bfa_rport_s *rp,
				 enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_FWRSP:
		bfa_stats(rp, sm_offp_fwrsp);
		if (bfa_rport_send_fwdelete(rp))
			bfa_sm_set_state(rp, bfa_rport_sm_fwdelete);
		else
			bfa_sm_set_state(rp, bfa_rport_sm_fwdelete_qfull);
		break;

	case BFA_RPORT_SM_DELETE:
		bfa_stats(rp, sm_offp_del);
		bfa_sm_set_state(rp, bfa_rport_sm_delete_pending);
		break;

	case BFA_RPORT_SM_HWFAIL:
		bfa_stats(rp, sm_offp_hwf);
		bfa_sm_set_state(rp, bfa_rport_sm_iocdisable);
		break;

	default:
		bfa_stats(rp, sm_offp_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}

J
Jing Huang 已提交
4373
/*
K
Krishna Gudipati 已提交
4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412
 * IOC h/w failed.
 */
static void
bfa_rport_sm_iocdisable(struct bfa_rport_s *rp, enum bfa_rport_event event)
{
	bfa_trc(rp->bfa, rp->rport_tag);
	bfa_trc(rp->bfa, event);

	switch (event) {
	case BFA_RPORT_SM_OFFLINE:
		bfa_stats(rp, sm_iocd_off);
		bfa_rport_offline_cb(rp);
		break;

	case BFA_RPORT_SM_DELETE:
		bfa_stats(rp, sm_iocd_del);
		bfa_sm_set_state(rp, bfa_rport_sm_uninit);
		bfa_rport_free(rp);
		break;

	case BFA_RPORT_SM_ONLINE:
		bfa_stats(rp, sm_iocd_on);
		if (bfa_rport_send_fwcreate(rp))
			bfa_sm_set_state(rp, bfa_rport_sm_fwcreate);
		else
			bfa_sm_set_state(rp, bfa_rport_sm_fwcreate_qfull);
		break;

	case BFA_RPORT_SM_HWFAIL:
		break;

	default:
		bfa_stats(rp, sm_iocd_unexp);
		bfa_sm_fault(rp->bfa, event);
	}
}



J
Jing Huang 已提交
4413
/*
K
Krishna Gudipati 已提交
4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443
 *  bfa_rport_private BFA rport private functions
 */

static void
__bfa_cb_rport_online(void *cbarg, bfa_boolean_t complete)
{
	struct bfa_rport_s *rp = cbarg;

	if (complete)
		bfa_cb_rport_online(rp->rport_drv);
}

static void
__bfa_cb_rport_offline(void *cbarg, bfa_boolean_t complete)
{
	struct bfa_rport_s *rp = cbarg;

	if (complete)
		bfa_cb_rport_offline(rp->rport_drv);
}

static void
bfa_rport_qresume(void *cbarg)
{
	struct bfa_rport_s	*rp = cbarg;

	bfa_sm_send_event(rp, BFA_RPORT_SM_QRESUME);
}

static void
4444 4445
bfa_rport_meminfo(struct bfa_iocfc_cfg_s *cfg, struct bfa_meminfo_s *minfo,
		struct bfa_s *bfa)
K
Krishna Gudipati 已提交
4446
{
4447 4448
	struct bfa_mem_kva_s *rport_kva = BFA_MEM_RPORT_KVA(bfa);

K
Krishna Gudipati 已提交
4449 4450 4451
	if (cfg->fwcfg.num_rports < BFA_RPORT_MIN)
		cfg->fwcfg.num_rports = BFA_RPORT_MIN;

4452 4453 4454
	/* kva memory */
	bfa_mem_kva_setup(minfo, rport_kva,
		cfg->fwcfg.num_rports * sizeof(struct bfa_rport_s));
K
Krishna Gudipati 已提交
4455 4456 4457 4458
}

static void
bfa_rport_attach(struct bfa_s *bfa, void *bfad, struct bfa_iocfc_cfg_s *cfg,
4459
		struct bfa_pcidev_s *pcidev)
K
Krishna Gudipati 已提交
4460 4461 4462 4463 4464 4465 4466
{
	struct bfa_rport_mod_s *mod = BFA_RPORT_MOD(bfa);
	struct bfa_rport_s *rp;
	u16 i;

	INIT_LIST_HEAD(&mod->rp_free_q);
	INIT_LIST_HEAD(&mod->rp_active_q);
4467
	INIT_LIST_HEAD(&mod->rp_unused_q);
K
Krishna Gudipati 已提交
4468

4469
	rp = (struct bfa_rport_s *) bfa_mem_kva_curp(mod);
K
Krishna Gudipati 已提交
4470 4471 4472
	mod->rps_list = rp;
	mod->num_rports = cfg->fwcfg.num_rports;

4473 4474
	WARN_ON(!mod->num_rports ||
		   (mod->num_rports & (mod->num_rports - 1)));
K
Krishna Gudipati 已提交
4475 4476

	for (i = 0; i < mod->num_rports; i++, rp++) {
4477
		memset(rp, 0, sizeof(struct bfa_rport_s));
K
Krishna Gudipati 已提交
4478 4479 4480 4481
		rp->bfa = bfa;
		rp->rport_tag = i;
		bfa_sm_set_state(rp, bfa_rport_sm_uninit);

J
Jing Huang 已提交
4482
		/*
K
Krishna Gudipati 已提交
4483 4484 4485 4486 4487 4488 4489 4490
		 *  - is unused
		 */
		if (i)
			list_add_tail(&rp->qe, &mod->rp_free_q);

		bfa_reqq_winit(&rp->reqq_wait, bfa_rport_qresume, rp);
	}

J
Jing Huang 已提交
4491
	/*
K
Krishna Gudipati 已提交
4492 4493
	 * consume memory
	 */
4494
	bfa_mem_kva_curp(mod) = (u8 *) rp;
K
Krishna Gudipati 已提交
4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518
}

static void
bfa_rport_detach(struct bfa_s *bfa)
{
}

static void
bfa_rport_start(struct bfa_s *bfa)
{
}

static void
bfa_rport_stop(struct bfa_s *bfa)
{
}

static void
bfa_rport_iocdisable(struct bfa_s *bfa)
{
	struct bfa_rport_mod_s *mod = BFA_RPORT_MOD(bfa);
	struct bfa_rport_s *rport;
	struct list_head *qe, *qen;

4519 4520 4521
	/* Enqueue unused rport resources to free_q */
	list_splice_tail_init(&mod->rp_unused_q, &mod->rp_free_q);

K
Krishna Gudipati 已提交
4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544
	list_for_each_safe(qe, qen, &mod->rp_active_q) {
		rport = (struct bfa_rport_s *) qe;
		bfa_sm_send_event(rport, BFA_RPORT_SM_HWFAIL);
	}
}

static struct bfa_rport_s *
bfa_rport_alloc(struct bfa_rport_mod_s *mod)
{
	struct bfa_rport_s *rport;

	bfa_q_deq(&mod->rp_free_q, &rport);
	if (rport)
		list_add_tail(&rport->qe, &mod->rp_active_q);

	return rport;
}

static void
bfa_rport_free(struct bfa_rport_s *rport)
{
	struct bfa_rport_mod_s *mod = BFA_RPORT_MOD(rport->bfa);

4545
	WARN_ON(!bfa_q_is_on_q(&mod->rp_active_q, rport));
K
Krishna Gudipati 已提交
4546 4547 4548 4549 4550 4551 4552 4553 4554
	list_del(&rport->qe);
	list_add_tail(&rport->qe, &mod->rp_free_q);
}

static bfa_boolean_t
bfa_rport_send_fwcreate(struct bfa_rport_s *rp)
{
	struct bfi_rport_create_req_s *m;

J
Jing Huang 已提交
4555
	/*
K
Krishna Gudipati 已提交
4556 4557 4558 4559 4560 4561 4562 4563 4564
	 * check for room in queue to send request now
	 */
	m = bfa_reqq_next(rp->bfa, BFA_REQQ_RPORT);
	if (!m) {
		bfa_reqq_wait(rp->bfa, BFA_REQQ_RPORT, &rp->reqq_wait);
		return BFA_FALSE;
	}

	bfi_h2i_set(m->mh, BFI_MC_RPORT, BFI_RPORT_H2I_CREATE_REQ,
4565
			bfa_fn_lpu(rp->bfa));
K
Krishna Gudipati 已提交
4566
	m->bfa_handle = rp->rport_tag;
4567
	m->max_frmsz = cpu_to_be16(rp->rport_info.max_frmsz);
K
Krishna Gudipati 已提交
4568
	m->pid = rp->rport_info.pid;
4569
	m->lp_fwtag = bfa_lps_get_fwtag(rp->bfa, (u8)rp->rport_info.lp_tag);
K
Krishna Gudipati 已提交
4570 4571 4572 4573 4574 4575
	m->local_pid = rp->rport_info.local_pid;
	m->fc_class = rp->rport_info.fc_class;
	m->vf_en = rp->rport_info.vf_en;
	m->vf_id = rp->rport_info.vf_id;
	m->cisc = rp->rport_info.cisc;

J
Jing Huang 已提交
4576
	/*
K
Krishna Gudipati 已提交
4577 4578
	 * queue I/O message to firmware
	 */
4579
	bfa_reqq_produce(rp->bfa, BFA_REQQ_RPORT, m->mh);
K
Krishna Gudipati 已提交
4580 4581 4582 4583 4584 4585 4586 4587
	return BFA_TRUE;
}

static bfa_boolean_t
bfa_rport_send_fwdelete(struct bfa_rport_s *rp)
{
	struct bfi_rport_delete_req_s *m;

J
Jing Huang 已提交
4588
	/*
K
Krishna Gudipati 已提交
4589 4590 4591 4592 4593 4594 4595 4596 4597
	 * check for room in queue to send request now
	 */
	m = bfa_reqq_next(rp->bfa, BFA_REQQ_RPORT);
	if (!m) {
		bfa_reqq_wait(rp->bfa, BFA_REQQ_RPORT, &rp->reqq_wait);
		return BFA_FALSE;
	}

	bfi_h2i_set(m->mh, BFI_MC_RPORT, BFI_RPORT_H2I_DELETE_REQ,
4598
			bfa_fn_lpu(rp->bfa));
K
Krishna Gudipati 已提交
4599 4600
	m->fw_handle = rp->fw_handle;

J
Jing Huang 已提交
4601
	/*
K
Krishna Gudipati 已提交
4602 4603
	 * queue I/O message to firmware
	 */
4604
	bfa_reqq_produce(rp->bfa, BFA_REQQ_RPORT, m->mh);
K
Krishna Gudipati 已提交
4605 4606 4607 4608 4609 4610 4611 4612
	return BFA_TRUE;
}

static bfa_boolean_t
bfa_rport_send_fwspeed(struct bfa_rport_s *rp)
{
	struct bfa_rport_speed_req_s *m;

J
Jing Huang 已提交
4613
	/*
K
Krishna Gudipati 已提交
4614 4615 4616 4617 4618 4619 4620 4621 4622
	 * check for room in queue to send request now
	 */
	m = bfa_reqq_next(rp->bfa, BFA_REQQ_RPORT);
	if (!m) {
		bfa_trc(rp->bfa, rp->rport_info.speed);
		return BFA_FALSE;
	}

	bfi_h2i_set(m->mh, BFI_MC_RPORT, BFI_RPORT_H2I_SET_SPEED_REQ,
4623
			bfa_fn_lpu(rp->bfa));
K
Krishna Gudipati 已提交
4624 4625 4626
	m->fw_handle = rp->fw_handle;
	m->speed = (u8)rp->rport_info.speed;

J
Jing Huang 已提交
4627
	/*
K
Krishna Gudipati 已提交
4628 4629
	 * queue I/O message to firmware
	 */
4630
	bfa_reqq_produce(rp->bfa, BFA_REQQ_RPORT, m->mh);
K
Krishna Gudipati 已提交
4631 4632 4633 4634 4635
	return BFA_TRUE;
}



J
Jing Huang 已提交
4636
/*
K
Krishna Gudipati 已提交
4637 4638 4639
 *  bfa_rport_public
 */

J
Jing Huang 已提交
4640
/*
K
Krishna Gudipati 已提交
4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657
 * Rport interrupt processing.
 */
void
bfa_rport_isr(struct bfa_s *bfa, struct bfi_msg_s *m)
{
	union bfi_rport_i2h_msg_u msg;
	struct bfa_rport_s *rp;

	bfa_trc(bfa, m->mhdr.msg_id);

	msg.msg = m;

	switch (m->mhdr.msg_id) {
	case BFI_RPORT_I2H_CREATE_RSP:
		rp = BFA_RPORT_FROM_TAG(bfa, msg.create_rsp->bfa_handle);
		rp->fw_handle = msg.create_rsp->fw_handle;
		rp->qos_attr = msg.create_rsp->qos_attr;
4658
		WARN_ON(msg.create_rsp->status != BFA_STATUS_OK);
K
Krishna Gudipati 已提交
4659 4660 4661 4662 4663
		bfa_sm_send_event(rp, BFA_RPORT_SM_FWRSP);
		break;

	case BFI_RPORT_I2H_DELETE_RSP:
		rp = BFA_RPORT_FROM_TAG(bfa, msg.delete_rsp->bfa_handle);
4664
		WARN_ON(msg.delete_rsp->status != BFA_STATUS_OK);
K
Krishna Gudipati 已提交
4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675
		bfa_sm_send_event(rp, BFA_RPORT_SM_FWRSP);
		break;

	case BFI_RPORT_I2H_QOS_SCN:
		rp = BFA_RPORT_FROM_TAG(bfa, msg.qos_scn_evt->bfa_handle);
		rp->event_arg.fw_msg = msg.qos_scn_evt;
		bfa_sm_send_event(rp, BFA_RPORT_SM_QOS_SCN);
		break;

	default:
		bfa_trc(bfa, m->mhdr.msg_id);
4676
		WARN_ON(1);
K
Krishna Gudipati 已提交
4677 4678 4679
	}
}

4680 4681 4682 4683 4684 4685
void
bfa_rport_res_recfg(struct bfa_s *bfa, u16 num_rport_fw)
{
	struct bfa_rport_mod_s	*mod = BFA_RPORT_MOD(bfa);
	struct list_head	*qe;
	int	i;
K
Krishna Gudipati 已提交
4686

4687 4688 4689 4690 4691
	for (i = 0; i < (mod->num_rports - num_rport_fw); i++) {
		bfa_q_deq_tail(&mod->rp_free_q, &qe);
		list_add_tail(qe, &mod->rp_unused_q);
	}
}
K
Krishna Gudipati 已提交
4692

J
Jing Huang 已提交
4693
/*
K
Krishna Gudipati 已提交
4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708
 *  bfa_rport_api
 */

struct bfa_rport_s *
bfa_rport_create(struct bfa_s *bfa, void *rport_drv)
{
	struct bfa_rport_s *rp;

	rp = bfa_rport_alloc(BFA_RPORT_MOD(bfa));

	if (rp == NULL)
		return NULL;

	rp->bfa = bfa;
	rp->rport_drv = rport_drv;
4709
	memset(&rp->stats, 0, sizeof(rp->stats));
K
Krishna Gudipati 已提交
4710

4711
	WARN_ON(!bfa_sm_cmp_state(rp, bfa_rport_sm_uninit));
K
Krishna Gudipati 已提交
4712 4713 4714 4715 4716 4717 4718 4719
	bfa_sm_send_event(rp, BFA_RPORT_SM_CREATE);

	return rp;
}

void
bfa_rport_online(struct bfa_rport_s *rport, struct bfa_rport_info_s *rport_info)
{
4720
	WARN_ON(rport_info->max_frmsz == 0);
K
Krishna Gudipati 已提交
4721

J
Jing Huang 已提交
4722
	/*
K
Krishna Gudipati 已提交
4723 4724 4725 4726 4727 4728 4729 4730
	 * Some JBODs are seen to be not setting PDU size correctly in PLOGI
	 * responses. Default to minimum size.
	 */
	if (rport_info->max_frmsz == 0) {
		bfa_trc(rport->bfa, rport->rport_tag);
		rport_info->max_frmsz = FC_MIN_PDUSZ;
	}

4731
	rport->rport_info = *rport_info;
K
Krishna Gudipati 已提交
4732 4733 4734 4735 4736 4737
	bfa_sm_send_event(rport, BFA_RPORT_SM_ONLINE);
}

void
bfa_rport_speed(struct bfa_rport_s *rport, enum bfa_port_speed speed)
{
4738 4739
	WARN_ON(speed == 0);
	WARN_ON(speed == BFA_PORT_SPEED_AUTO);
K
Krishna Gudipati 已提交
4740 4741 4742 4743 4744 4745

	rport->rport_info.speed = speed;
	bfa_sm_send_event(rport, BFA_RPORT_SM_SET_SPEED);
}


J
Jing Huang 已提交
4746
/*
K
Krishna Gudipati 已提交
4747 4748 4749
 * SGPG related functions
 */

J
Jing Huang 已提交
4750
/*
K
Krishna Gudipati 已提交
4751 4752 4753
 * Compute and return memory needed by FCP(im) module.
 */
static void
4754 4755
bfa_sgpg_meminfo(struct bfa_iocfc_cfg_s *cfg, struct bfa_meminfo_s *minfo,
		struct bfa_s *bfa)
K
Krishna Gudipati 已提交
4756
{
4757 4758 4759 4760 4761 4762
	struct bfa_sgpg_mod_s *sgpg_mod = BFA_SGPG_MOD(bfa);
	struct bfa_mem_kva_s *sgpg_kva = BFA_MEM_SGPG_KVA(bfa);
	struct bfa_mem_dma_s *seg_ptr;
	u16	nsegs, idx, per_seg_sgpg, num_sgpg;
	u32	sgpg_sz = sizeof(struct bfi_sgpg_s);

K
Krishna Gudipati 已提交
4763 4764
	if (cfg->drvcfg.num_sgpgs < BFA_SGPG_MIN)
		cfg->drvcfg.num_sgpgs = BFA_SGPG_MIN;
4765 4766
	else if (cfg->drvcfg.num_sgpgs > BFA_SGPG_MAX)
		cfg->drvcfg.num_sgpgs = BFA_SGPG_MAX;
K
Krishna Gudipati 已提交
4767

4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781
	num_sgpg = cfg->drvcfg.num_sgpgs;

	nsegs = BFI_MEM_DMA_NSEGS(num_sgpg, sgpg_sz);
	per_seg_sgpg = BFI_MEM_NREQS_SEG(sgpg_sz);

	bfa_mem_dma_seg_iter(sgpg_mod, seg_ptr, nsegs, idx) {
		if (num_sgpg >= per_seg_sgpg) {
			num_sgpg -= per_seg_sgpg;
			bfa_mem_dma_setup(minfo, seg_ptr,
					per_seg_sgpg * sgpg_sz);
		} else
			bfa_mem_dma_setup(minfo, seg_ptr,
					num_sgpg * sgpg_sz);
	}
K
Krishna Gudipati 已提交
4782

4783 4784 4785 4786
	/* kva memory */
	bfa_mem_kva_setup(minfo, sgpg_kva,
		cfg->drvcfg.num_sgpgs * sizeof(struct bfa_sgpg_s));
}
K
Krishna Gudipati 已提交
4787 4788 4789

static void
bfa_sgpg_attach(struct bfa_s *bfa, void *bfad, struct bfa_iocfc_cfg_s *cfg,
4790
		struct bfa_pcidev_s *pcidev)
K
Krishna Gudipati 已提交
4791 4792 4793 4794 4795
{
	struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);
	struct bfa_sgpg_s *hsgpg;
	struct bfi_sgpg_s *sgpg;
	u64 align_len;
4796 4797 4798
	struct bfa_mem_dma_s *seg_ptr;
	u32	sgpg_sz = sizeof(struct bfi_sgpg_s);
	u16	i, idx, nsegs, per_seg_sgpg, num_sgpg;
K
Krishna Gudipati 已提交
4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809

	union {
		u64 pa;
		union bfi_addr_u addr;
	} sgpg_pa, sgpg_pa_tmp;

	INIT_LIST_HEAD(&mod->sgpg_q);
	INIT_LIST_HEAD(&mod->sgpg_wait_q);

	bfa_trc(bfa, cfg->drvcfg.num_sgpgs);

4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845
	mod->free_sgpgs = mod->num_sgpgs = cfg->drvcfg.num_sgpgs;

	num_sgpg = cfg->drvcfg.num_sgpgs;
	nsegs = BFI_MEM_DMA_NSEGS(num_sgpg, sgpg_sz);

	/* dma/kva mem claim */
	hsgpg = (struct bfa_sgpg_s *) bfa_mem_kva_curp(mod);

	bfa_mem_dma_seg_iter(mod, seg_ptr, nsegs, idx) {

		if (!bfa_mem_dma_virt(seg_ptr))
			break;

		align_len = BFA_SGPG_ROUNDUP(bfa_mem_dma_phys(seg_ptr)) -
					     bfa_mem_dma_phys(seg_ptr);

		sgpg = (struct bfi_sgpg_s *)
			(((u8 *) bfa_mem_dma_virt(seg_ptr)) + align_len);
		sgpg_pa.pa = bfa_mem_dma_phys(seg_ptr) + align_len;
		WARN_ON(sgpg_pa.pa & (sgpg_sz - 1));

		per_seg_sgpg = (seg_ptr->mem_len - (u32)align_len) / sgpg_sz;

		for (i = 0; num_sgpg > 0 && i < per_seg_sgpg; i++, num_sgpg--) {
			memset(hsgpg, 0, sizeof(*hsgpg));
			memset(sgpg, 0, sizeof(*sgpg));

			hsgpg->sgpg = sgpg;
			sgpg_pa_tmp.pa = bfa_sgaddr_le(sgpg_pa.pa);
			hsgpg->sgpg_pa = sgpg_pa_tmp.addr;
			list_add_tail(&hsgpg->qe, &mod->sgpg_q);

			sgpg++;
			hsgpg++;
			sgpg_pa.pa += sgpg_sz;
		}
K
Krishna Gudipati 已提交
4846 4847
	}

4848
	bfa_mem_kva_curp(mod) = (u8 *) hsgpg;
K
Krishna Gudipati 已提交
4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882
}

static void
bfa_sgpg_detach(struct bfa_s *bfa)
{
}

static void
bfa_sgpg_start(struct bfa_s *bfa)
{
}

static void
bfa_sgpg_stop(struct bfa_s *bfa)
{
}

static void
bfa_sgpg_iocdisable(struct bfa_s *bfa)
{
}

bfa_status_t
bfa_sgpg_malloc(struct bfa_s *bfa, struct list_head *sgpg_q, int nsgpgs)
{
	struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);
	struct bfa_sgpg_s *hsgpg;
	int i;

	if (mod->free_sgpgs < nsgpgs)
		return BFA_STATUS_ENOMEM;

	for (i = 0; i < nsgpgs; i++) {
		bfa_q_deq(&mod->sgpg_q, &hsgpg);
4883
		WARN_ON(!hsgpg);
K
Krishna Gudipati 已提交
4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897
		list_add_tail(&hsgpg->qe, sgpg_q);
	}

	mod->free_sgpgs -= nsgpgs;
	return BFA_STATUS_OK;
}

void
bfa_sgpg_mfree(struct bfa_s *bfa, struct list_head *sgpg_q, int nsgpg)
{
	struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);
	struct bfa_sgpg_wqe_s *wqe;

	mod->free_sgpgs += nsgpg;
4898
	WARN_ON(mod->free_sgpgs > mod->num_sgpgs);
K
Krishna Gudipati 已提交
4899 4900 4901 4902 4903 4904

	list_splice_tail_init(sgpg_q, &mod->sgpg_q);

	if (list_empty(&mod->sgpg_wait_q))
		return;

J
Jing Huang 已提交
4905
	/*
K
Krishna Gudipati 已提交
4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927
	 * satisfy as many waiting requests as possible
	 */
	do {
		wqe = bfa_q_first(&mod->sgpg_wait_q);
		if (mod->free_sgpgs < wqe->nsgpg)
			nsgpg = mod->free_sgpgs;
		else
			nsgpg = wqe->nsgpg;
		bfa_sgpg_malloc(bfa, &wqe->sgpg_q, nsgpg);
		wqe->nsgpg -= nsgpg;
		if (wqe->nsgpg == 0) {
			list_del(&wqe->qe);
			wqe->cbfn(wqe->cbarg);
		}
	} while (mod->free_sgpgs && !list_empty(&mod->sgpg_wait_q));
}

void
bfa_sgpg_wait(struct bfa_s *bfa, struct bfa_sgpg_wqe_s *wqe, int nsgpg)
{
	struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);

4928 4929
	WARN_ON(nsgpg <= 0);
	WARN_ON(nsgpg <= mod->free_sgpgs);
K
Krishna Gudipati 已提交
4930 4931 4932

	wqe->nsgpg_total = wqe->nsgpg = nsgpg;

J
Jing Huang 已提交
4933
	/*
K
Krishna Gudipati 已提交
4934 4935 4936
	 * allocate any left to this one first
	 */
	if (mod->free_sgpgs) {
J
Jing Huang 已提交
4937
		/*
K
Krishna Gudipati 已提交
4938 4939
		 * no one else is waiting for SGPG
		 */
4940
		WARN_ON(!list_empty(&mod->sgpg_wait_q));
K
Krishna Gudipati 已提交
4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953
		list_splice_tail_init(&mod->sgpg_q, &wqe->sgpg_q);
		wqe->nsgpg -= mod->free_sgpgs;
		mod->free_sgpgs = 0;
	}

	list_add_tail(&wqe->qe, &mod->sgpg_wait_q);
}

void
bfa_sgpg_wcancel(struct bfa_s *bfa, struct bfa_sgpg_wqe_s *wqe)
{
	struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);

4954
	WARN_ON(!bfa_q_is_on_q(&mod->sgpg_wait_q, wqe));
K
Krishna Gudipati 已提交
4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970
	list_del(&wqe->qe);

	if (wqe->nsgpg_total != wqe->nsgpg)
		bfa_sgpg_mfree(bfa, &wqe->sgpg_q,
				   wqe->nsgpg_total - wqe->nsgpg);
}

void
bfa_sgpg_winit(struct bfa_sgpg_wqe_s *wqe, void (*cbfn) (void *cbarg),
		   void *cbarg)
{
	INIT_LIST_HEAD(&wqe->sgpg_q);
	wqe->cbfn = cbfn;
	wqe->cbarg = cbarg;
}

J
Jing Huang 已提交
4971
/*
K
Krishna Gudipati 已提交
4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989
 *  UF related functions
 */
/*
 *****************************************************************************
 * Internal functions
 *****************************************************************************
 */
static void
__bfa_cb_uf_recv(void *cbarg, bfa_boolean_t complete)
{
	struct bfa_uf_s   *uf = cbarg;
	struct bfa_uf_mod_s *ufm = BFA_UF_MOD(uf->bfa);

	if (complete)
		ufm->ufrecv(ufm->cbarg, uf);
}

static void
4990
claim_uf_post_msgs(struct bfa_uf_mod_s *ufm)
K
Krishna Gudipati 已提交
4991 4992 4993 4994 4995
{
	struct bfi_uf_buf_post_s *uf_bp_msg;
	u16 i;
	u16 buf_len;

4996
	ufm->uf_buf_posts = (struct bfi_uf_buf_post_s *) bfa_mem_kva_curp(ufm);
K
Krishna Gudipati 已提交
4997 4998 4999 5000
	uf_bp_msg = ufm->uf_buf_posts;

	for (i = 0, uf_bp_msg = ufm->uf_buf_posts; i < ufm->num_ufs;
	     i++, uf_bp_msg++) {
5001
		memset(uf_bp_msg, 0, sizeof(struct bfi_uf_buf_post_s));
K
Krishna Gudipati 已提交
5002 5003 5004

		uf_bp_msg->buf_tag = i;
		buf_len = sizeof(struct bfa_uf_buf_s);
5005
		uf_bp_msg->buf_len = cpu_to_be16(buf_len);
K
Krishna Gudipati 已提交
5006
		bfi_h2i_set(uf_bp_msg->mh, BFI_MC_UF, BFI_UF_H2I_BUF_POST,
5007
			    bfa_fn_lpu(ufm->bfa));
5008
		bfa_alen_set(&uf_bp_msg->alen, buf_len, ufm_pbs_pa(ufm, i));
K
Krishna Gudipati 已提交
5009 5010
	}

J
Jing Huang 已提交
5011
	/*
K
Krishna Gudipati 已提交
5012 5013
	 * advance pointer beyond consumed memory
	 */
5014
	bfa_mem_kva_curp(ufm) = (u8 *) uf_bp_msg;
K
Krishna Gudipati 已提交
5015 5016 5017
}

static void
5018
claim_ufs(struct bfa_uf_mod_s *ufm)
K
Krishna Gudipati 已提交
5019 5020 5021 5022 5023 5024 5025
{
	u16 i;
	struct bfa_uf_s   *uf;

	/*
	 * Claim block of memory for UF list
	 */
5026
	ufm->uf_list = (struct bfa_uf_s *) bfa_mem_kva_curp(ufm);
K
Krishna Gudipati 已提交
5027 5028 5029 5030 5031

	/*
	 * Initialize UFs and queue it in UF free queue
	 */
	for (i = 0, uf = ufm->uf_list; i < ufm->num_ufs; i++, uf++) {
5032
		memset(uf, 0, sizeof(struct bfa_uf_s));
K
Krishna Gudipati 已提交
5033 5034
		uf->bfa = ufm->bfa;
		uf->uf_tag = i;
5035 5036
		uf->pb_len = BFA_PER_UF_DMA_SZ;
		uf->buf_kva = bfa_mem_get_dmabuf_kva(ufm, i, BFA_PER_UF_DMA_SZ);
K
Krishna Gudipati 已提交
5037 5038 5039 5040
		uf->buf_pa = ufm_pbs_pa(ufm, i);
		list_add_tail(&uf->qe, &ufm->uf_free_q);
	}

J
Jing Huang 已提交
5041
	/*
K
Krishna Gudipati 已提交
5042 5043
	 * advance memory pointer
	 */
5044
	bfa_mem_kva_curp(ufm) = (u8 *) uf;
K
Krishna Gudipati 已提交
5045 5046 5047
}

static void
5048
uf_mem_claim(struct bfa_uf_mod_s *ufm)
K
Krishna Gudipati 已提交
5049
{
5050 5051
	claim_ufs(ufm);
	claim_uf_post_msgs(ufm);
K
Krishna Gudipati 已提交
5052 5053 5054
}

static void
5055 5056
bfa_uf_meminfo(struct bfa_iocfc_cfg_s *cfg, struct bfa_meminfo_s *minfo,
		struct bfa_s *bfa)
K
Krishna Gudipati 已提交
5057
{
5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075
	struct bfa_uf_mod_s *ufm = BFA_UF_MOD(bfa);
	struct bfa_mem_kva_s *uf_kva = BFA_MEM_UF_KVA(bfa);
	u32	num_ufs = cfg->fwcfg.num_uf_bufs;
	struct bfa_mem_dma_s *seg_ptr;
	u16	nsegs, idx, per_seg_uf = 0;

	nsegs = BFI_MEM_DMA_NSEGS(num_ufs, BFA_PER_UF_DMA_SZ);
	per_seg_uf = BFI_MEM_NREQS_SEG(BFA_PER_UF_DMA_SZ);

	bfa_mem_dma_seg_iter(ufm, seg_ptr, nsegs, idx) {
		if (num_ufs >= per_seg_uf) {
			num_ufs -= per_seg_uf;
			bfa_mem_dma_setup(minfo, seg_ptr,
				per_seg_uf * BFA_PER_UF_DMA_SZ);
		} else
			bfa_mem_dma_setup(minfo, seg_ptr,
				num_ufs * BFA_PER_UF_DMA_SZ);
	}
K
Krishna Gudipati 已提交
5076

5077 5078 5079
	/* kva memory */
	bfa_mem_kva_setup(minfo, uf_kva, cfg->fwcfg.num_uf_bufs *
		(sizeof(struct bfa_uf_s) + sizeof(struct bfi_uf_buf_post_s)));
K
Krishna Gudipati 已提交
5080 5081 5082 5083
}

static void
bfa_uf_attach(struct bfa_s *bfa, void *bfad, struct bfa_iocfc_cfg_s *cfg,
5084
		struct bfa_pcidev_s *pcidev)
K
Krishna Gudipati 已提交
5085 5086 5087 5088 5089 5090 5091
{
	struct bfa_uf_mod_s *ufm = BFA_UF_MOD(bfa);

	ufm->bfa = bfa;
	ufm->num_ufs = cfg->fwcfg.num_uf_bufs;
	INIT_LIST_HEAD(&ufm->uf_free_q);
	INIT_LIST_HEAD(&ufm->uf_posted_q);
5092
	INIT_LIST_HEAD(&ufm->uf_unused_q);
K
Krishna Gudipati 已提交
5093

5094
	uf_mem_claim(ufm);
K
Krishna Gudipati 已提交
5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125
}

static void
bfa_uf_detach(struct bfa_s *bfa)
{
}

static struct bfa_uf_s *
bfa_uf_get(struct bfa_uf_mod_s *uf_mod)
{
	struct bfa_uf_s   *uf;

	bfa_q_deq(&uf_mod->uf_free_q, &uf);
	return uf;
}

static void
bfa_uf_put(struct bfa_uf_mod_s *uf_mod, struct bfa_uf_s *uf)
{
	list_add_tail(&uf->qe, &uf_mod->uf_free_q);
}

static bfa_status_t
bfa_uf_post(struct bfa_uf_mod_s *ufm, struct bfa_uf_s *uf)
{
	struct bfi_uf_buf_post_s *uf_post_msg;

	uf_post_msg = bfa_reqq_next(ufm->bfa, BFA_REQQ_FCXP);
	if (!uf_post_msg)
		return BFA_STATUS_FAILED;

5126
	memcpy(uf_post_msg, &ufm->uf_buf_posts[uf->uf_tag],
K
Krishna Gudipati 已提交
5127
		      sizeof(struct bfi_uf_buf_post_s));
5128
	bfa_reqq_produce(ufm->bfa, BFA_REQQ_FCXP, uf_post_msg->mh);
K
Krishna Gudipati 已提交
5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152

	bfa_trc(ufm->bfa, uf->uf_tag);

	list_add_tail(&uf->qe, &ufm->uf_posted_q);
	return BFA_STATUS_OK;
}

static void
bfa_uf_post_all(struct bfa_uf_mod_s *uf_mod)
{
	struct bfa_uf_s   *uf;

	while ((uf = bfa_uf_get(uf_mod)) != NULL) {
		if (bfa_uf_post(uf_mod, uf) != BFA_STATUS_OK)
			break;
	}
}

static void
uf_recv(struct bfa_s *bfa, struct bfi_uf_frm_rcvd_s *m)
{
	struct bfa_uf_mod_s *ufm = BFA_UF_MOD(bfa);
	u16 uf_tag = m->buf_tag;
	struct bfa_uf_s *uf = &ufm->uf_list[uf_tag];
5153 5154
	struct bfa_uf_buf_s *uf_buf;
	uint8_t *buf;
K
Krishna Gudipati 已提交
5155 5156
	struct fchs_s *fchs;

5157 5158 5159 5160
	uf_buf = (struct bfa_uf_buf_s *)
			bfa_mem_get_dmabuf_kva(ufm, uf_tag, uf->pb_len);
	buf = &uf_buf->d[0];

5161 5162
	m->frm_len = be16_to_cpu(m->frm_len);
	m->xfr_len = be16_to_cpu(m->xfr_len);
K
Krishna Gudipati 已提交
5163 5164 5165 5166 5167 5168 5169 5170

	fchs = (struct fchs_s *)uf_buf;

	list_del(&uf->qe);	/* dequeue from posted queue */

	uf->data_ptr = buf;
	uf->data_len = m->xfr_len;

5171
	WARN_ON(uf->data_len < sizeof(struct fchs_s));
K
Krishna Gudipati 已提交
5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200

	if (uf->data_len == sizeof(struct fchs_s)) {
		bfa_plog_fchdr(bfa->plog, BFA_PL_MID_HAL_UF, BFA_PL_EID_RX,
			       uf->data_len, (struct fchs_s *)buf);
	} else {
		u32 pld_w0 = *((u32 *) (buf + sizeof(struct fchs_s)));
		bfa_plog_fchdr_and_pl(bfa->plog, BFA_PL_MID_HAL_UF,
				      BFA_PL_EID_RX, uf->data_len,
				      (struct fchs_s *)buf, pld_w0);
	}

	if (bfa->fcs)
		__bfa_cb_uf_recv(uf, BFA_TRUE);
	else
		bfa_cb_queue(bfa, &uf->hcb_qe, __bfa_cb_uf_recv, uf);
}

static void
bfa_uf_stop(struct bfa_s *bfa)
{
}

static void
bfa_uf_iocdisable(struct bfa_s *bfa)
{
	struct bfa_uf_mod_s *ufm = BFA_UF_MOD(bfa);
	struct bfa_uf_s *uf;
	struct list_head *qe, *qen;

5201 5202 5203
	/* Enqueue unused uf resources to free_q */
	list_splice_tail_init(&ufm->uf_unused_q, &ufm->uf_free_q);

K
Krishna Gudipati 已提交
5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216
	list_for_each_safe(qe, qen, &ufm->uf_posted_q) {
		uf = (struct bfa_uf_s *) qe;
		list_del(&uf->qe);
		bfa_uf_put(ufm, uf);
	}
}

static void
bfa_uf_start(struct bfa_s *bfa)
{
	bfa_uf_post_all(BFA_UF_MOD(bfa));
}

J
Jing Huang 已提交
5217
/*
L
Lucas De Marchi 已提交
5218
 * Register handler for all unsolicted receive frames.
K
Krishna Gudipati 已提交
5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232
 *
 * @param[in]	bfa		BFA instance
 * @param[in]	ufrecv	receive handler function
 * @param[in]	cbarg	receive handler arg
 */
void
bfa_uf_recv_register(struct bfa_s *bfa, bfa_cb_uf_recv_t ufrecv, void *cbarg)
{
	struct bfa_uf_mod_s *ufm = BFA_UF_MOD(bfa);

	ufm->ufrecv = ufrecv;
	ufm->cbarg = cbarg;
}

J
Jing Huang 已提交
5233
/*
K
Krishna Gudipati 已提交
5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248
 *	Free an unsolicited frame back to BFA.
 *
 * @param[in]		uf		unsolicited frame to be freed
 *
 * @return None
 */
void
bfa_uf_free(struct bfa_uf_s *uf)
{
	bfa_uf_put(BFA_UF_MOD(uf->bfa), uf);
	bfa_uf_post_all(BFA_UF_MOD(uf->bfa));
}



J
Jing Huang 已提交
5249
/*
K
Krishna Gudipati 已提交
5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263
 *  uf_pub BFA uf module public functions
 */
void
bfa_uf_isr(struct bfa_s *bfa, struct bfi_msg_s *msg)
{
	bfa_trc(bfa, msg->mhdr.msg_id);

	switch (msg->mhdr.msg_id) {
	case BFI_UF_I2H_FRM_RCVD:
		uf_recv(bfa, (struct bfi_uf_frm_rcvd_s *) msg);
		break;

	default:
		bfa_trc(bfa, msg->mhdr.msg_id);
5264
		WARN_ON(1);
K
Krishna Gudipati 已提交
5265 5266 5267
	}
}

5268 5269 5270 5271 5272 5273
void
bfa_uf_res_recfg(struct bfa_s *bfa, u16 num_uf_fw)
{
	struct bfa_uf_mod_s	*mod = BFA_UF_MOD(bfa);
	struct list_head	*qe;
	int	i;
K
Krishna Gudipati 已提交
5274

5275 5276 5277 5278 5279
	for (i = 0; i < (mod->num_ufs - num_uf_fw); i++) {
		bfa_q_deq_tail(&mod->uf_free_q, &qe);
		list_add_tail(qe, &mod->uf_unused_q);
	}
}
5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555

/*
 *	BFA fcdiag module
 */
#define BFA_DIAG_QTEST_TOV	1000    /* msec */

/*
 *	Set port status to busy
 */
static void
bfa_fcdiag_set_busy_status(struct bfa_fcdiag_s *fcdiag)
{
	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(fcdiag->bfa);

	if (fcdiag->lb.lock)
		fcport->diag_busy = BFA_TRUE;
	else
		fcport->diag_busy = BFA_FALSE;
}

static void
bfa_fcdiag_meminfo(struct bfa_iocfc_cfg_s *cfg, struct bfa_meminfo_s *meminfo,
		struct bfa_s *bfa)
{
}

static void
bfa_fcdiag_attach(struct bfa_s *bfa, void *bfad, struct bfa_iocfc_cfg_s *cfg,
		struct bfa_pcidev_s *pcidev)
{
	struct bfa_fcdiag_s *fcdiag = BFA_FCDIAG_MOD(bfa);
	fcdiag->bfa             = bfa;
	fcdiag->trcmod  = bfa->trcmod;
	/* The common DIAG attach bfa_diag_attach() will do all memory claim */
}

static void
bfa_fcdiag_iocdisable(struct bfa_s *bfa)
{
	struct bfa_fcdiag_s *fcdiag = BFA_FCDIAG_MOD(bfa);
	bfa_trc(fcdiag, fcdiag->lb.lock);
	if (fcdiag->lb.lock) {
		fcdiag->lb.status = BFA_STATUS_IOC_FAILURE;
		fcdiag->lb.cbfn(fcdiag->lb.cbarg, fcdiag->lb.status);
		fcdiag->lb.lock = 0;
		bfa_fcdiag_set_busy_status(fcdiag);
	}
}

static void
bfa_fcdiag_detach(struct bfa_s *bfa)
{
}

static void
bfa_fcdiag_start(struct bfa_s *bfa)
{
}

static void
bfa_fcdiag_stop(struct bfa_s *bfa)
{
}

static void
bfa_fcdiag_queuetest_timeout(void *cbarg)
{
	struct bfa_fcdiag_s       *fcdiag = cbarg;
	struct bfa_diag_qtest_result_s *res = fcdiag->qtest.result;

	bfa_trc(fcdiag, fcdiag->qtest.all);
	bfa_trc(fcdiag, fcdiag->qtest.count);

	fcdiag->qtest.timer_active = 0;

	res->status = BFA_STATUS_ETIMER;
	res->count  = QTEST_CNT_DEFAULT - fcdiag->qtest.count;
	if (fcdiag->qtest.all)
		res->queue  = fcdiag->qtest.all;

	bfa_trc(fcdiag, BFA_STATUS_ETIMER);
	fcdiag->qtest.status = BFA_STATUS_ETIMER;
	fcdiag->qtest.cbfn(fcdiag->qtest.cbarg, fcdiag->qtest.status);
	fcdiag->qtest.lock = 0;
}

static bfa_status_t
bfa_fcdiag_queuetest_send(struct bfa_fcdiag_s *fcdiag)
{
	u32	i;
	struct bfi_diag_qtest_req_s *req;

	req = bfa_reqq_next(fcdiag->bfa, fcdiag->qtest.queue);
	if (!req)
		return BFA_STATUS_DEVBUSY;

	/* build host command */
	bfi_h2i_set(req->mh, BFI_MC_DIAG, BFI_DIAG_H2I_QTEST,
		bfa_fn_lpu(fcdiag->bfa));

	for (i = 0; i < BFI_LMSG_PL_WSZ; i++)
		req->data[i] = QTEST_PAT_DEFAULT;

	bfa_trc(fcdiag, fcdiag->qtest.queue);
	/* ring door bell */
	bfa_reqq_produce(fcdiag->bfa, fcdiag->qtest.queue, req->mh);
	return BFA_STATUS_OK;
}

static void
bfa_fcdiag_queuetest_comp(struct bfa_fcdiag_s *fcdiag,
			bfi_diag_qtest_rsp_t *rsp)
{
	struct bfa_diag_qtest_result_s *res = fcdiag->qtest.result;
	bfa_status_t status = BFA_STATUS_OK;
	int i;

	/* Check timer, should still be active   */
	if (!fcdiag->qtest.timer_active) {
		bfa_trc(fcdiag, fcdiag->qtest.timer_active);
		return;
	}

	/* update count */
	fcdiag->qtest.count--;

	/* Check result */
	for (i = 0; i < BFI_LMSG_PL_WSZ; i++) {
		if (rsp->data[i] != ~(QTEST_PAT_DEFAULT)) {
			res->status = BFA_STATUS_DATACORRUPTED;
			break;
		}
	}

	if (res->status == BFA_STATUS_OK) {
		if (fcdiag->qtest.count > 0) {
			status = bfa_fcdiag_queuetest_send(fcdiag);
			if (status == BFA_STATUS_OK)
				return;
			else
				res->status = status;
		} else if (fcdiag->qtest.all > 0 &&
			fcdiag->qtest.queue < (BFI_IOC_MAX_CQS - 1)) {
			fcdiag->qtest.count = QTEST_CNT_DEFAULT;
			fcdiag->qtest.queue++;
			status = bfa_fcdiag_queuetest_send(fcdiag);
			if (status == BFA_STATUS_OK)
				return;
			else
				res->status = status;
		}
	}

	/* Stop timer when we comp all queue */
	if (fcdiag->qtest.timer_active) {
		bfa_timer_stop(&fcdiag->qtest.timer);
		fcdiag->qtest.timer_active = 0;
	}
	res->queue = fcdiag->qtest.queue;
	res->count = QTEST_CNT_DEFAULT - fcdiag->qtest.count;
	bfa_trc(fcdiag, res->count);
	bfa_trc(fcdiag, res->status);
	fcdiag->qtest.status = res->status;
	fcdiag->qtest.cbfn(fcdiag->qtest.cbarg, fcdiag->qtest.status);
	fcdiag->qtest.lock = 0;
}

static void
bfa_fcdiag_loopback_comp(struct bfa_fcdiag_s *fcdiag,
			struct bfi_diag_lb_rsp_s *rsp)
{
	struct bfa_diag_loopback_result_s *res = fcdiag->lb.result;

	res->numtxmfrm  = be32_to_cpu(rsp->res.numtxmfrm);
	res->numosffrm  = be32_to_cpu(rsp->res.numosffrm);
	res->numrcvfrm  = be32_to_cpu(rsp->res.numrcvfrm);
	res->badfrminf  = be32_to_cpu(rsp->res.badfrminf);
	res->badfrmnum  = be32_to_cpu(rsp->res.badfrmnum);
	res->status     = rsp->res.status;
	fcdiag->lb.status = rsp->res.status;
	bfa_trc(fcdiag, fcdiag->lb.status);
	fcdiag->lb.cbfn(fcdiag->lb.cbarg, fcdiag->lb.status);
	fcdiag->lb.lock = 0;
	bfa_fcdiag_set_busy_status(fcdiag);
}

static bfa_status_t
bfa_fcdiag_loopback_send(struct bfa_fcdiag_s *fcdiag,
			struct bfa_diag_loopback_s *loopback)
{
	struct bfi_diag_lb_req_s *lb_req;

	lb_req = bfa_reqq_next(fcdiag->bfa, BFA_REQQ_DIAG);
	if (!lb_req)
		return BFA_STATUS_DEVBUSY;

	/* build host command */
	bfi_h2i_set(lb_req->mh, BFI_MC_DIAG, BFI_DIAG_H2I_LOOPBACK,
		bfa_fn_lpu(fcdiag->bfa));

	lb_req->lb_mode = loopback->lb_mode;
	lb_req->speed = loopback->speed;
	lb_req->loopcnt = loopback->loopcnt;
	lb_req->pattern = loopback->pattern;

	/* ring door bell */
	bfa_reqq_produce(fcdiag->bfa, BFA_REQQ_DIAG, lb_req->mh);

	bfa_trc(fcdiag, loopback->lb_mode);
	bfa_trc(fcdiag, loopback->speed);
	bfa_trc(fcdiag, loopback->loopcnt);
	bfa_trc(fcdiag, loopback->pattern);
	return BFA_STATUS_OK;
}

/*
 *	cpe/rme intr handler
 */
void
bfa_fcdiag_intr(struct bfa_s *bfa, struct bfi_msg_s *msg)
{
	struct bfa_fcdiag_s *fcdiag = BFA_FCDIAG_MOD(bfa);

	switch (msg->mhdr.msg_id) {
	case BFI_DIAG_I2H_LOOPBACK:
		bfa_fcdiag_loopback_comp(fcdiag,
				(struct bfi_diag_lb_rsp_s *) msg);
		break;
	case BFI_DIAG_I2H_QTEST:
		bfa_fcdiag_queuetest_comp(fcdiag, (bfi_diag_qtest_rsp_t *)msg);
		break;
	default:
		bfa_trc(fcdiag, msg->mhdr.msg_id);
		WARN_ON(1);
	}
}

/*
 *	Loopback test
 *
 *   @param[in] *bfa            - bfa data struct
 *   @param[in] opmode          - port operation mode
 *   @param[in] speed           - port speed
 *   @param[in] lpcnt           - loop count
 *   @param[in] pat                     - pattern to build packet
 *   @param[in] *result         - pt to bfa_diag_loopback_result_t data struct
 *   @param[in] cbfn            - callback function
 *   @param[in] cbarg           - callback functioin arg
 *
 *   @param[out]
 */
bfa_status_t
bfa_fcdiag_loopback(struct bfa_s *bfa, enum bfa_port_opmode opmode,
		enum bfa_port_speed speed, u32 lpcnt, u32 pat,
		struct bfa_diag_loopback_result_s *result, bfa_cb_diag_t cbfn,
		void *cbarg)
{
	struct  bfa_diag_loopback_s loopback;
	struct bfa_port_attr_s attr;
	bfa_status_t status;
	struct bfa_fcdiag_s *fcdiag = BFA_FCDIAG_MOD(bfa);

	if (!bfa_iocfc_is_operational(bfa))
		return BFA_STATUS_IOC_NON_OP;

	/* if port is PBC disabled, return error */
	if (bfa_fcport_is_pbcdisabled(bfa)) {
		bfa_trc(fcdiag, BFA_STATUS_PBC);
		return BFA_STATUS_PBC;
	}

	if (bfa_fcport_is_disabled(bfa) == BFA_FALSE) {
		bfa_trc(fcdiag, opmode);
		return BFA_STATUS_PORT_NOT_DISABLED;
	}

5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578
	/*
	 * Check if input speed is supported by the port mode
	 */
	if (bfa_ioc_get_type(&bfa->ioc) == BFA_IOC_TYPE_FC) {
		if (!(speed == BFA_PORT_SPEED_1GBPS ||
		      speed == BFA_PORT_SPEED_2GBPS ||
		      speed == BFA_PORT_SPEED_4GBPS ||
		      speed == BFA_PORT_SPEED_8GBPS ||
		      speed == BFA_PORT_SPEED_16GBPS ||
		      speed == BFA_PORT_SPEED_AUTO)) {
			bfa_trc(fcdiag, speed);
			return BFA_STATUS_UNSUPP_SPEED;
		}
		bfa_fcport_get_attr(bfa, &attr);
		bfa_trc(fcdiag, attr.speed_supported);
		if (speed > attr.speed_supported)
			return BFA_STATUS_UNSUPP_SPEED;
	} else {
		if (speed != BFA_PORT_SPEED_10GBPS) {
			bfa_trc(fcdiag, speed);
			return BFA_STATUS_UNSUPP_SPEED;
		}
	}
5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697

	/* For Mezz card, port speed entered needs to be checked */
	if (bfa_mfg_is_mezz(bfa->ioc.attr->card_type)) {
		if (bfa_ioc_get_type(&bfa->ioc) == BFA_IOC_TYPE_FC) {
			if ((speed == BFA_PORT_SPEED_1GBPS) &&
			    (bfa_asic_id_ct2(bfa->ioc.pcidev.device_id)))
				return BFA_STATUS_UNSUPP_SPEED;
			if (!(speed == BFA_PORT_SPEED_1GBPS ||
			      speed == BFA_PORT_SPEED_2GBPS ||
			      speed == BFA_PORT_SPEED_4GBPS ||
			      speed == BFA_PORT_SPEED_8GBPS ||
			      speed == BFA_PORT_SPEED_16GBPS ||
			      speed == BFA_PORT_SPEED_AUTO))
				return BFA_STATUS_UNSUPP_SPEED;
		} else {
			if (speed != BFA_PORT_SPEED_10GBPS)
				return BFA_STATUS_UNSUPP_SPEED;
		}
	}

	/* check to see if there is another destructive diag cmd running */
	if (fcdiag->lb.lock) {
		bfa_trc(fcdiag, fcdiag->lb.lock);
		return BFA_STATUS_DEVBUSY;
	}

	fcdiag->lb.lock = 1;
	loopback.lb_mode = opmode;
	loopback.speed = speed;
	loopback.loopcnt = lpcnt;
	loopback.pattern = pat;
	fcdiag->lb.result = result;
	fcdiag->lb.cbfn = cbfn;
	fcdiag->lb.cbarg = cbarg;
	memset(result, 0, sizeof(struct bfa_diag_loopback_result_s));
	bfa_fcdiag_set_busy_status(fcdiag);

	/* Send msg to fw */
	status = bfa_fcdiag_loopback_send(fcdiag, &loopback);
	return status;
}

/*
 *	DIAG queue test command
 *
 *   @param[in] *bfa            - bfa data struct
 *   @param[in] force           - 1: don't do ioc op checking
 *   @param[in] queue           - queue no. to test
 *   @param[in] *result         - pt to bfa_diag_qtest_result_t data struct
 *   @param[in] cbfn            - callback function
 *   @param[in] *cbarg          - callback functioin arg
 *
 *   @param[out]
 */
bfa_status_t
bfa_fcdiag_queuetest(struct bfa_s *bfa, u32 force, u32 queue,
		struct bfa_diag_qtest_result_s *result, bfa_cb_diag_t cbfn,
		void *cbarg)
{
	struct bfa_fcdiag_s *fcdiag = BFA_FCDIAG_MOD(bfa);
	bfa_status_t status;
	bfa_trc(fcdiag, force);
	bfa_trc(fcdiag, queue);

	if (!force && !bfa_iocfc_is_operational(bfa))
		return BFA_STATUS_IOC_NON_OP;

	/* check to see if there is another destructive diag cmd running */
	if (fcdiag->qtest.lock) {
		bfa_trc(fcdiag, fcdiag->qtest.lock);
		return BFA_STATUS_DEVBUSY;
	}

	/* Initialization */
	fcdiag->qtest.lock = 1;
	fcdiag->qtest.cbfn = cbfn;
	fcdiag->qtest.cbarg = cbarg;
	fcdiag->qtest.result = result;
	fcdiag->qtest.count = QTEST_CNT_DEFAULT;

	/* Init test results */
	fcdiag->qtest.result->status = BFA_STATUS_OK;
	fcdiag->qtest.result->count  = 0;

	/* send */
	if (queue < BFI_IOC_MAX_CQS) {
		fcdiag->qtest.result->queue  = (u8)queue;
		fcdiag->qtest.queue = (u8)queue;
		fcdiag->qtest.all   = 0;
	} else {
		fcdiag->qtest.result->queue  = 0;
		fcdiag->qtest.queue = 0;
		fcdiag->qtest.all   = 1;
	}
	status = bfa_fcdiag_queuetest_send(fcdiag);

	/* Start a timer */
	if (status == BFA_STATUS_OK) {
		bfa_timer_start(bfa, &fcdiag->qtest.timer,
				bfa_fcdiag_queuetest_timeout, fcdiag,
				BFA_DIAG_QTEST_TOV);
		fcdiag->qtest.timer_active = 1;
	}
	return status;
}

/*
 * DIAG PLB is running
 *
 *   @param[in] *bfa    - bfa data struct
 *
 *   @param[out]
 */
bfa_status_t
bfa_fcdiag_lb_is_running(struct bfa_s *bfa)
{
	struct bfa_fcdiag_s *fcdiag = BFA_FCDIAG_MOD(bfa);
	return fcdiag->lb.lock ?  BFA_STATUS_DIAG_BUSY : BFA_STATUS_OK;
}