bfa_fcbuild.c 36.3 KB
Newer Older
1
/*
K
Krishna Gudipati 已提交
2
 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */
/*
 * fcbuild.c - FC link service frame building and parsing routines
 */

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

/*
 * static build functions
 */
K
Krishna Gudipati 已提交
27
static void     fc_els_rsp_build(struct fchs_s *fchs, u32 d_id, u32 s_id,
28
				 __be16 ox_id);
K
Krishna Gudipati 已提交
29
static void     fc_bls_rsp_build(struct fchs_s *fchs, u32 d_id, u32 s_id,
30
				 __be16 ox_id);
K
Krishna Gudipati 已提交
31 32 33 34
static struct fchs_s fc_els_req_tmpl;
static struct fchs_s fc_els_rsp_tmpl;
static struct fchs_s fc_bls_req_tmpl;
static struct fchs_s fc_bls_rsp_tmpl;
35 36 37 38
static struct fc_ba_acc_s ba_acc_tmpl;
static struct fc_logi_s plogi_tmpl;
static struct fc_prli_s prli_tmpl;
static struct fc_rrq_s rrq_tmpl;
K
Krishna Gudipati 已提交
39
static struct fchs_s fcp_fchs_tmpl;
40 41 42 43 44 45 46 47 48 49 50

void
fcbuild_init(void)
{
	/*
	 * fc_els_req_tmpl
	 */
	fc_els_req_tmpl.routing = FC_RTG_EXT_LINK;
	fc_els_req_tmpl.cat_info = FC_CAT_LD_REQUEST;
	fc_els_req_tmpl.type = FC_TYPE_ELS;
	fc_els_req_tmpl.f_ctl =
M
Maggie Zhang 已提交
51
		bfa_hton3b(FCTL_SEQ_INI | FCTL_FS_EXCH | FCTL_END_SEQ |
52 53 54 55 56 57 58 59 60 61
			      FCTL_SI_XFER);
	fc_els_req_tmpl.rx_id = FC_RXID_ANY;

	/*
	 * fc_els_rsp_tmpl
	 */
	fc_els_rsp_tmpl.routing = FC_RTG_EXT_LINK;
	fc_els_rsp_tmpl.cat_info = FC_CAT_LD_REPLY;
	fc_els_rsp_tmpl.type = FC_TYPE_ELS;
	fc_els_rsp_tmpl.f_ctl =
M
Maggie Zhang 已提交
62
		bfa_hton3b(FCTL_EC_RESP | FCTL_SEQ_INI | FCTL_LS_EXCH |
63 64 65 66 67 68 69 70
			      FCTL_END_SEQ | FCTL_SI_XFER);
	fc_els_rsp_tmpl.rx_id = FC_RXID_ANY;

	/*
	 * fc_bls_req_tmpl
	 */
	fc_bls_req_tmpl.routing = FC_RTG_BASIC_LINK;
	fc_bls_req_tmpl.type = FC_TYPE_BLS;
M
Maggie Zhang 已提交
71
	fc_bls_req_tmpl.f_ctl = bfa_hton3b(FCTL_END_SEQ | FCTL_SI_XFER);
72 73 74 75 76 77 78 79 80
	fc_bls_req_tmpl.rx_id = FC_RXID_ANY;

	/*
	 * fc_bls_rsp_tmpl
	 */
	fc_bls_rsp_tmpl.routing = FC_RTG_BASIC_LINK;
	fc_bls_rsp_tmpl.cat_info = FC_CAT_BA_ACC;
	fc_bls_rsp_tmpl.type = FC_TYPE_BLS;
	fc_bls_rsp_tmpl.f_ctl =
M
Maggie Zhang 已提交
81
		bfa_hton3b(FCTL_EC_RESP | FCTL_SEQ_INI | FCTL_LS_EXCH |
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
			      FCTL_END_SEQ | FCTL_SI_XFER);
	fc_bls_rsp_tmpl.rx_id = FC_RXID_ANY;

	/*
	 * ba_acc_tmpl
	 */
	ba_acc_tmpl.seq_id_valid = 0;
	ba_acc_tmpl.low_seq_cnt = 0;
	ba_acc_tmpl.high_seq_cnt = 0xFFFF;

	/*
	 * plogi_tmpl
	 */
	plogi_tmpl.csp.verhi = FC_PH_VER_PH_3;
	plogi_tmpl.csp.verlo = FC_PH_VER_4_3;
97
	plogi_tmpl.csp.bbcred = cpu_to_be16(0x0004);
98 99 100
	plogi_tmpl.csp.ciro = 0x1;
	plogi_tmpl.csp.cisc = 0x0;
	plogi_tmpl.csp.altbbcred = 0x0;
101 102 103
	plogi_tmpl.csp.conseq = cpu_to_be16(0x00FF);
	plogi_tmpl.csp.ro_bitmap = cpu_to_be16(0x0002);
	plogi_tmpl.csp.e_d_tov = cpu_to_be32(2000);
104 105 106 107 108 109 110 111 112 113 114

	plogi_tmpl.class3.class_valid = 1;
	plogi_tmpl.class3.sequential = 1;
	plogi_tmpl.class3.conseq = 0xFF;
	plogi_tmpl.class3.ospx = 1;

	/*
	 * prli_tmpl
	 */
	prli_tmpl.command = FC_ELS_PRLI;
	prli_tmpl.pglen = 0x10;
115
	prli_tmpl.pagebytes = cpu_to_be16(0x0014);
116 117 118 119 120 121 122 123 124 125
	prli_tmpl.parampage.type = FC_TYPE_FCP;
	prli_tmpl.parampage.imagepair = 1;
	prli_tmpl.parampage.servparams.rxrdisab = 1;

	/*
	 * rrq_tmpl
	 */
	rrq_tmpl.els_cmd.els_code = FC_ELS_RRQ;

	/*
K
Krishna Gudipati 已提交
126
	 * fcp_struct fchs_s mpl
127 128 129 130 131
	 */
	fcp_fchs_tmpl.routing = FC_RTG_FC4_DEV_DATA;
	fcp_fchs_tmpl.cat_info = FC_CAT_UNSOLICIT_CMD;
	fcp_fchs_tmpl.type = FC_TYPE_FCP;
	fcp_fchs_tmpl.f_ctl =
M
Maggie Zhang 已提交
132
		bfa_hton3b(FCTL_FS_EXCH | FCTL_END_SEQ | FCTL_SI_XFER);
133 134 135 136 137
	fcp_fchs_tmpl.seq_id = 1;
	fcp_fchs_tmpl.rx_id = FC_RXID_ANY;
}

static void
K
Krishna Gudipati 已提交
138
fc_gs_fchdr_build(struct fchs_s *fchs, u32 d_id, u32 s_id, u32 ox_id)
139
{
140
	memset(fchs, 0, sizeof(struct fchs_s));
141 142 143 144 145

	fchs->routing = FC_RTG_FC4_DEV_DATA;
	fchs->cat_info = FC_CAT_UNSOLICIT_CTRL;
	fchs->type = FC_TYPE_SERVICES;
	fchs->f_ctl =
M
Maggie Zhang 已提交
146
		bfa_hton3b(FCTL_SEQ_INI | FCTL_FS_EXCH | FCTL_END_SEQ |
147 148 149 150
			      FCTL_SI_XFER);
	fchs->rx_id = FC_RXID_ANY;
	fchs->d_id = (d_id);
	fchs->s_id = (s_id);
151
	fchs->ox_id = cpu_to_be16(ox_id);
152

J
Jing Huang 已提交
153
	/*
154 155 156 157 158 159
	 * @todo no need to set ox_id for request
	 *       no need to set rx_id for response
	 */
}

void
160
fc_els_req_build(struct fchs_s *fchs, u32 d_id, u32 s_id, __be16 ox_id)
161
{
162
	memcpy(fchs, &fc_els_req_tmpl, sizeof(struct fchs_s));
163 164
	fchs->d_id = (d_id);
	fchs->s_id = (s_id);
165
	fchs->ox_id = cpu_to_be16(ox_id);
166 167 168
}

static void
169
fc_els_rsp_build(struct fchs_s *fchs, u32 d_id, u32 s_id, __be16 ox_id)
170
{
171
	memcpy(fchs, &fc_els_rsp_tmpl, sizeof(struct fchs_s));
172 173 174 175 176 177 178 179
	fchs->d_id = d_id;
	fchs->s_id = s_id;
	fchs->ox_id = ox_id;
}

enum fc_parse_status
fc_els_rsp_parse(struct fchs_s *fchs, int len)
{
K
Krishna Gudipati 已提交
180 181
	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
	struct fc_ls_rjt_s *ls_rjt = (struct fc_ls_rjt_s *) els_cmd;
182 183 184 185 186 187

	len = len;

	switch (els_cmd->els_code) {
	case FC_ELS_LS_RJT:
		if (ls_rjt->reason_code == FC_LS_RJT_RSN_LOGICAL_BUSY)
188
			return FC_PARSE_BUSY;
189
		else
190
			return FC_PARSE_FAILURE;
191 192

	case FC_ELS_ACC:
193
		return FC_PARSE_OK;
194
	}
195
	return FC_PARSE_OK;
196 197 198
}

static void
199
fc_bls_rsp_build(struct fchs_s *fchs, u32 d_id, u32 s_id, __be16 ox_id)
200
{
201
	memcpy(fchs, &fc_bls_rsp_tmpl, sizeof(struct fchs_s));
202 203 204 205 206 207 208
	fchs->d_id = d_id;
	fchs->s_id = s_id;
	fchs->ox_id = ox_id;
}

static          u16
fc_plogi_x_build(struct fchs_s *fchs, void *pld, u32 d_id, u32 s_id,
209
		 __be16 ox_id, wwn_t port_name, wwn_t node_name,
210 211
		 u16 pdu_size, u8 els_code)
{
K
Krishna Gudipati 已提交
212
	struct fc_logi_s *plogi = (struct fc_logi_s *) (pld);
213

214
	memcpy(plogi, &plogi_tmpl, sizeof(struct fc_logi_s));
215 216 217 218 219 220 221

	plogi->els_cmd.els_code = els_code;
	if (els_code == FC_ELS_PLOGI)
		fc_els_req_build(fchs, d_id, s_id, ox_id);
	else
		fc_els_rsp_build(fchs, d_id, s_id, ox_id);

222
	plogi->csp.rxsz = plogi->class3.rxsz = cpu_to_be16(pdu_size);
223

224 225
	memcpy(&plogi->port_name, &port_name, sizeof(wwn_t));
	memcpy(&plogi->node_name, &node_name, sizeof(wwn_t));
226

227
	return sizeof(struct fc_logi_s);
228 229 230 231
}

u16
fc_flogi_build(struct fchs_s *fchs, struct fc_logi_s *flogi, u32 s_id,
K
Krishna Gudipati 已提交
232 233
		u16 ox_id, wwn_t port_name, wwn_t node_name, u16 pdu_size,
	       u8 set_npiv, u8 set_auth, u16 local_bb_credits)
234
{
M
Maggie Zhang 已提交
235
	u32        d_id = bfa_hton3b(FC_FABRIC_PORT);
236
	__be32	*vvl_info;
237

238
	memcpy(flogi, &plogi_tmpl, sizeof(struct fc_logi_s));
239 240 241 242

	flogi->els_cmd.els_code = FC_ELS_FLOGI;
	fc_els_req_build(fchs, d_id, s_id, ox_id);

243
	flogi->csp.rxsz = flogi->class3.rxsz = cpu_to_be16(pdu_size);
244 245 246 247 248 249 250 251 252 253 254 255
	flogi->port_name = port_name;
	flogi->node_name = node_name;

	/*
	 * Set the NPIV Capability Bit ( word 1, bit 31) of Common
	 * Service Parameters.
	 */
	flogi->csp.ciro = set_npiv;

	/* set AUTH capability */
	flogi->csp.security = set_auth;

256
	flogi->csp.bbcred = cpu_to_be16(local_bb_credits);
257 258 259 260 261 262

	/* Set brcd token in VVL */
	vvl_info = (u32 *)&flogi->vvl[0];

	/* set the flag to indicate the presence of VVL */
	flogi->csp.npiv_supp    = 1; /* @todo. field name is not correct */
263
	vvl_info[0]	= cpu_to_be32(FLOGI_VVL_BRCD);
264

265
	return sizeof(struct fc_logi_s);
266 267 268 269
}

u16
fc_flogi_acc_build(struct fchs_s *fchs, struct fc_logi_s *flogi, u32 s_id,
270
		   __be16 ox_id, wwn_t port_name, wwn_t node_name,
271 272 273 274
		   u16 pdu_size, u16 local_bb_credits)
{
	u32        d_id = 0;

275
	memcpy(flogi, &plogi_tmpl, sizeof(struct fc_logi_s));
276 277 278
	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

	flogi->els_cmd.els_code = FC_ELS_ACC;
279
	flogi->csp.rxsz = flogi->class3.rxsz = cpu_to_be16(pdu_size);
280 281 282
	flogi->port_name = port_name;
	flogi->node_name = node_name;

283
	flogi->csp.bbcred = cpu_to_be16(local_bb_credits);
284

285
	return sizeof(struct fc_logi_s);
286 287 288 289
}

u16
fc_fdisc_build(struct fchs_s *fchs, struct fc_logi_s *flogi, u32 s_id,
K
Krishna Gudipati 已提交
290
		u16 ox_id, wwn_t port_name, wwn_t node_name, u16 pdu_size)
291
{
M
Maggie Zhang 已提交
292
	u32        d_id = bfa_hton3b(FC_FABRIC_PORT);
293

294
	memcpy(flogi, &plogi_tmpl, sizeof(struct fc_logi_s));
295 296 297 298

	flogi->els_cmd.els_code = FC_ELS_FDISC;
	fc_els_req_build(fchs, d_id, s_id, ox_id);

299
	flogi->csp.rxsz = flogi->class3.rxsz = cpu_to_be16(pdu_size);
300 301 302
	flogi->port_name = port_name;
	flogi->node_name = node_name;

303
	return sizeof(struct fc_logi_s);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
}

u16
fc_plogi_build(struct fchs_s *fchs, void *pld, u32 d_id, u32 s_id,
	       u16 ox_id, wwn_t port_name, wwn_t node_name,
	       u16 pdu_size)
{
	return fc_plogi_x_build(fchs, pld, d_id, s_id, ox_id, port_name,
				node_name, pdu_size, FC_ELS_PLOGI);
}

u16
fc_plogi_acc_build(struct fchs_s *fchs, void *pld, u32 d_id, u32 s_id,
		   u16 ox_id, wwn_t port_name, wwn_t node_name,
		   u16 pdu_size)
{
	return fc_plogi_x_build(fchs, pld, d_id, s_id, ox_id, port_name,
				node_name, pdu_size, FC_ELS_ACC);
}

enum fc_parse_status
fc_plogi_rsp_parse(struct fchs_s *fchs, int len, wwn_t port_name)
{
K
Krishna Gudipati 已提交
327 328 329
	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
	struct fc_logi_s *plogi;
	struct fc_ls_rjt_s *ls_rjt;
330 331 332 333 334

	switch (els_cmd->els_code) {
	case FC_ELS_LS_RJT:
		ls_rjt = (struct fc_ls_rjt_s *) (fchs + 1);
		if (ls_rjt->reason_code == FC_LS_RJT_RSN_LOGICAL_BUSY)
335
			return FC_PARSE_BUSY;
336
		else
337
			return FC_PARSE_FAILURE;
338 339 340
	case FC_ELS_ACC:
		plogi = (struct fc_logi_s *) (fchs + 1);
		if (len < sizeof(struct fc_logi_s))
341
			return FC_PARSE_FAILURE;
342 343

		if (!wwn_is_equal(plogi->port_name, port_name))
344
			return FC_PARSE_FAILURE;
345 346

		if (!plogi->class3.class_valid)
347
			return FC_PARSE_FAILURE;
348

349
		if (be16_to_cpu(plogi->class3.rxsz) < (FC_MIN_PDUSZ))
350
			return FC_PARSE_FAILURE;
351

352
		return FC_PARSE_OK;
353
	default:
354
		return FC_PARSE_FAILURE;
355 356 357 358 359 360
	}
}

enum fc_parse_status
fc_plogi_parse(struct fchs_s *fchs)
{
K
Krishna Gudipati 已提交
361
	struct fc_logi_s *plogi = (struct fc_logi_s *) (fchs + 1);
362 363 364 365

	if (plogi->class3.class_valid != 1)
		return FC_PARSE_FAILURE;

366 367
	if ((be16_to_cpu(plogi->class3.rxsz) < FC_MIN_PDUSZ)
	    || (be16_to_cpu(plogi->class3.rxsz) > FC_MAX_PDUSZ)
368
	    || (plogi->class3.rxsz == 0))
369
		return FC_PARSE_FAILURE;
370 371 372 373 374 375 376 377

	return FC_PARSE_OK;
}

u16
fc_prli_build(struct fchs_s *fchs, void *pld, u32 d_id, u32 s_id,
	      u16 ox_id)
{
K
Krishna Gudipati 已提交
378
	struct fc_prli_s *prli = (struct fc_prli_s *) (pld);
379 380

	fc_els_req_build(fchs, d_id, s_id, ox_id);
381
	memcpy(prli, &prli_tmpl, sizeof(struct fc_prli_s));
382 383 384 385 386 387 388 389

	prli->command = FC_ELS_PRLI;
	prli->parampage.servparams.initiator     = 1;
	prli->parampage.servparams.retry         = 1;
	prli->parampage.servparams.rec_support   = 1;
	prli->parampage.servparams.task_retry_id = 0;
	prli->parampage.servparams.confirm       = 1;

390
	return sizeof(struct fc_prli_s);
391 392 393 394
}

u16
fc_prli_acc_build(struct fchs_s *fchs, void *pld, u32 d_id, u32 s_id,
395
		  __be16 ox_id, enum bfa_lport_role role)
396
{
K
Krishna Gudipati 已提交
397
	struct fc_prli_s *prli = (struct fc_prli_s *) (pld);
398 399

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);
400
	memcpy(prli, &prli_tmpl, sizeof(struct fc_prli_s));
401 402 403

	prli->command = FC_ELS_ACC;

K
Krishna Gudipati 已提交
404
	prli->parampage.servparams.initiator = 1;
405 406 407

	prli->parampage.rspcode = FC_PRLI_ACC_XQTD;

408
	return sizeof(struct fc_prli_s);
409 410 411 412 413 414
}

enum fc_parse_status
fc_prli_rsp_parse(struct fc_prli_s *prli, int len)
{
	if (len < sizeof(struct fc_prli_s))
415
		return FC_PARSE_FAILURE;
416 417

	if (prli->command != FC_ELS_ACC)
418
		return FC_PARSE_FAILURE;
419 420 421

	if ((prli->parampage.rspcode != FC_PRLI_ACC_XQTD)
	    && (prli->parampage.rspcode != FC_PRLI_ACC_PREDEF_IMG))
422
		return FC_PARSE_FAILURE;
423 424

	if (prli->parampage.servparams.target != 1)
425
		return FC_PARSE_FAILURE;
426

427
	return FC_PARSE_OK;
428 429 430 431 432 433
}

enum fc_parse_status
fc_prli_parse(struct fc_prli_s *prli)
{
	if (prli->parampage.type != FC_TYPE_FCP)
434
		return FC_PARSE_FAILURE;
435 436

	if (!prli->parampage.imagepair)
437
		return FC_PARSE_FAILURE;
438 439

	if (!prli->parampage.servparams.initiator)
440
		return FC_PARSE_FAILURE;
441

442
	return FC_PARSE_OK;
443 444 445
}

u16
K
Krishna Gudipati 已提交
446 447
fc_logo_build(struct fchs_s *fchs, struct fc_logo_s *logo, u32 d_id, u32 s_id,
	      u16 ox_id, wwn_t port_name)
448 449 450
{
	fc_els_req_build(fchs, d_id, s_id, ox_id);

451
	memset(logo, '\0', sizeof(struct fc_logo_s));
452 453 454 455
	logo->els_cmd.els_code = FC_ELS_LOGO;
	logo->nport_id = (s_id);
	logo->orig_port_name = port_name;

456
	return sizeof(struct fc_logo_s);
457 458 459 460
}

static          u16
fc_adisc_x_build(struct fchs_s *fchs, struct fc_adisc_s *adisc, u32 d_id,
461
		 u32 s_id, __be16 ox_id, wwn_t port_name,
462 463
		 wwn_t node_name, u8 els_code)
{
464
	memset(adisc, '\0', sizeof(struct fc_adisc_s));
465 466 467 468 469 470 471 472 473 474 475 476 477

	adisc->els_cmd.els_code = els_code;

	if (els_code == FC_ELS_ADISC)
		fc_els_req_build(fchs, d_id, s_id, ox_id);
	else
		fc_els_rsp_build(fchs, d_id, s_id, ox_id);

	adisc->orig_HA = 0;
	adisc->orig_port_name = port_name;
	adisc->orig_node_name = node_name;
	adisc->nport_id = (s_id);

478
	return sizeof(struct fc_adisc_s);
479 480 481 482
}

u16
fc_adisc_build(struct fchs_s *fchs, struct fc_adisc_s *adisc, u32 d_id,
483
		u32 s_id, __be16 ox_id, wwn_t port_name, wwn_t node_name)
484 485 486 487 488 489 490
{
	return fc_adisc_x_build(fchs, adisc, d_id, s_id, ox_id, port_name,
				node_name, FC_ELS_ADISC);
}

u16
fc_adisc_acc_build(struct fchs_s *fchs, struct fc_adisc_s *adisc, u32 d_id,
491
		   u32 s_id, __be16 ox_id, wwn_t port_name,
492 493 494 495 496 497 498 499 500 501 502 503
		   wwn_t node_name)
{
	return fc_adisc_x_build(fchs, adisc, d_id, s_id, ox_id, port_name,
				node_name, FC_ELS_ACC);
}

enum fc_parse_status
fc_adisc_rsp_parse(struct fc_adisc_s *adisc, int len, wwn_t port_name,
				 wwn_t node_name)
{

	if (len < sizeof(struct fc_adisc_s))
504
		return FC_PARSE_FAILURE;
505 506

	if (adisc->els_cmd.els_code != FC_ELS_ACC)
507
		return FC_PARSE_FAILURE;
508 509

	if (!wwn_is_equal(adisc->orig_port_name, port_name))
510
		return FC_PARSE_FAILURE;
511

512
	return FC_PARSE_OK;
513 514 515
}

enum fc_parse_status
K
Krishna Gudipati 已提交
516 517
fc_adisc_parse(struct fchs_s *fchs, void *pld, u32 host_dap, wwn_t node_name,
	       wwn_t port_name)
518
{
K
Krishna Gudipati 已提交
519
	struct fc_adisc_s *adisc = (struct fc_adisc_s *) pld;
520 521

	if (adisc->els_cmd.els_code != FC_ELS_ACC)
522
		return FC_PARSE_FAILURE;
523 524 525 526

	if ((adisc->nport_id == (host_dap))
	    && wwn_is_equal(adisc->orig_port_name, port_name)
	    && wwn_is_equal(adisc->orig_node_name, node_name))
527
		return FC_PARSE_OK;
528

529
	return FC_PARSE_FAILURE;
530 531 532 533 534
}

enum fc_parse_status
fc_pdisc_parse(struct fchs_s *fchs, wwn_t node_name, wwn_t port_name)
{
K
Krishna Gudipati 已提交
535
	struct fc_logi_s *pdisc = (struct fc_logi_s *) (fchs + 1);
536 537 538 539

	if (pdisc->class3.class_valid != 1)
		return FC_PARSE_FAILURE;

540
	if ((be16_to_cpu(pdisc->class3.rxsz) <
K
Krishna Gudipati 已提交
541
		(FC_MIN_PDUSZ - sizeof(struct fchs_s)))
542
	    || (pdisc->class3.rxsz == 0))
543
		return FC_PARSE_FAILURE;
544 545

	if (!wwn_is_equal(pdisc->port_name, port_name))
546
		return FC_PARSE_FAILURE;
547 548

	if (!wwn_is_equal(pdisc->node_name, node_name))
549
		return FC_PARSE_FAILURE;
550 551 552 553 554 555 556

	return FC_PARSE_OK;
}

u16
fc_abts_build(struct fchs_s *fchs, u32 d_id, u32 s_id, u16 ox_id)
{
557
	memcpy(fchs, &fc_bls_req_tmpl, sizeof(struct fchs_s));
558 559 560
	fchs->cat_info = FC_CAT_ABTS;
	fchs->d_id = (d_id);
	fchs->s_id = (s_id);
561
	fchs->ox_id = cpu_to_be16(ox_id);
562

563
	return sizeof(struct fchs_s);
564 565 566 567 568 569 570
}

enum fc_parse_status
fc_abts_rsp_parse(struct fchs_s *fchs, int len)
{
	if ((fchs->cat_info == FC_CAT_BA_ACC)
	    || (fchs->cat_info == FC_CAT_BA_RJT))
571
		return FC_PARSE_OK;
572

573
	return FC_PARSE_FAILURE;
574 575 576
}

u16
K
Krishna Gudipati 已提交
577 578
fc_rrq_build(struct fchs_s *fchs, struct fc_rrq_s *rrq, u32 d_id, u32 s_id,
	     u16 ox_id, u16 rrq_oxid)
579 580 581 582 583 584
{
	fc_els_req_build(fchs, d_id, s_id, ox_id);

	/*
	 * build rrq payload
	 */
585
	memcpy(rrq, &rrq_tmpl, sizeof(struct fc_rrq_s));
586
	rrq->s_id = (s_id);
587
	rrq->ox_id = cpu_to_be16(rrq_oxid);
588 589
	rrq->rx_id = FC_RXID_ANY;

590
	return sizeof(struct fc_rrq_s);
591 592 593 594
}

u16
fc_logo_acc_build(struct fchs_s *fchs, void *pld, u32 d_id, u32 s_id,
595
		  __be16 ox_id)
596
{
K
Krishna Gudipati 已提交
597
	struct fc_els_cmd_s *acc = pld;
598 599 600

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

601
	memset(acc, 0, sizeof(struct fc_els_cmd_s));
602 603
	acc->els_code = FC_ELS_ACC;

604
	return sizeof(struct fc_els_cmd_s);
605 606 607 608
}

u16
fc_ls_rjt_build(struct fchs_s *fchs, struct fc_ls_rjt_s *ls_rjt, u32 d_id,
609
		u32 s_id, __be16 ox_id, u8 reason_code,
610 611 612
		u8 reason_code_expl)
{
	fc_els_rsp_build(fchs, d_id, s_id, ox_id);
613
	memset(ls_rjt, 0, sizeof(struct fc_ls_rjt_s));
614 615 616 617 618 619

	ls_rjt->els_cmd.els_code = FC_ELS_LS_RJT;
	ls_rjt->reason_code = reason_code;
	ls_rjt->reason_code_expl = reason_code_expl;
	ls_rjt->vendor_unique = 0x00;

620
	return sizeof(struct fc_ls_rjt_s);
621 622 623 624
}

u16
fc_ba_acc_build(struct fchs_s *fchs, struct fc_ba_acc_s *ba_acc, u32 d_id,
625
		u32 s_id, __be16 ox_id, u16 rx_id)
626 627 628
{
	fc_bls_rsp_build(fchs, d_id, s_id, ox_id);

629
	memcpy(ba_acc, &ba_acc_tmpl, sizeof(struct fc_ba_acc_s));
630 631 632 633 634 635

	fchs->rx_id = rx_id;

	ba_acc->ox_id = fchs->ox_id;
	ba_acc->rx_id = fchs->rx_id;

636
	return sizeof(struct fc_ba_acc_s);
637 638 639
}

u16
K
Krishna Gudipati 已提交
640
fc_ls_acc_build(struct fchs_s *fchs, struct fc_els_cmd_s *els_cmd, u32 d_id,
641
		u32 s_id, __be16 ox_id)
642 643
{
	fc_els_rsp_build(fchs, d_id, s_id, ox_id);
644
	memset(els_cmd, 0, sizeof(struct fc_els_cmd_s));
645 646
	els_cmd->els_code = FC_ELS_ACC;

647
	return sizeof(struct fc_els_cmd_s);
648 649 650 651 652 653
}

int
fc_logout_params_pages(struct fchs_s *fc_frame, u8 els_code)
{
	int             num_pages = 0;
K
Krishna Gudipati 已提交
654 655
	struct fc_prlo_s *prlo;
	struct fc_tprlo_s *tprlo;
656 657 658

	if (els_code == FC_ELS_PRLO) {
		prlo = (struct fc_prlo_s *) (fc_frame + 1);
659
		num_pages = (be16_to_cpu(prlo->payload_len) - 4) / 16;
660 661
	} else {
		tprlo = (struct fc_tprlo_s *) (fc_frame + 1);
662
		num_pages = (be16_to_cpu(tprlo->payload_len) - 4) / 16;
663 664 665 666 667 668
	}
	return num_pages;
}

u16
fc_tprlo_acc_build(struct fchs_s *fchs, struct fc_tprlo_acc_s *tprlo_acc,
669
		u32 d_id, u32 s_id, __be16 ox_id, int num_pages)
670 671 672 673 674
{
	int             page;

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

675
	memset(tprlo_acc, 0, (num_pages * 16) + 4);
676 677 678
	tprlo_acc->command = FC_ELS_ACC;

	tprlo_acc->page_len = 0x10;
679
	tprlo_acc->payload_len = cpu_to_be16((num_pages * 16) + 4);
680 681 682 683 684 685 686 687

	for (page = 0; page < num_pages; page++) {
		tprlo_acc->tprlo_acc_params[page].opa_valid = 0;
		tprlo_acc->tprlo_acc_params[page].rpa_valid = 0;
		tprlo_acc->tprlo_acc_params[page].fc4type_csp = FC_TYPE_FCP;
		tprlo_acc->tprlo_acc_params[page].orig_process_assc = 0;
		tprlo_acc->tprlo_acc_params[page].resp_process_assc = 0;
	}
688
	return be16_to_cpu(tprlo_acc->payload_len);
689 690 691
}

u16
K
Krishna Gudipati 已提交
692
fc_prlo_acc_build(struct fchs_s *fchs, struct fc_prlo_acc_s *prlo_acc, u32 d_id,
693
		  u32 s_id, __be16 ox_id, int num_pages)
694 695 696 697 698
{
	int             page;

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

699
	memset(prlo_acc, 0, (num_pages * 16) + 4);
700 701
	prlo_acc->command = FC_ELS_ACC;
	prlo_acc->page_len = 0x10;
702
	prlo_acc->payload_len = cpu_to_be16((num_pages * 16) + 4);
703 704 705 706 707 708 709 710 711

	for (page = 0; page < num_pages; page++) {
		prlo_acc->prlo_acc_params[page].opa_valid = 0;
		prlo_acc->prlo_acc_params[page].rpa_valid = 0;
		prlo_acc->prlo_acc_params[page].fc4type_csp = FC_TYPE_FCP;
		prlo_acc->prlo_acc_params[page].orig_process_assc = 0;
		prlo_acc->prlo_acc_params[page].resp_process_assc = 0;
	}

712
	return be16_to_cpu(prlo_acc->payload_len);
713 714 715 716
}

u16
fc_rnid_build(struct fchs_s *fchs, struct fc_rnid_cmd_s *rnid, u32 d_id,
K
Krishna Gudipati 已提交
717
		u32 s_id, u16 ox_id, u32 data_format)
718 719 720
{
	fc_els_req_build(fchs, d_id, s_id, ox_id);

721
	memset(rnid, 0, sizeof(struct fc_rnid_cmd_s));
722 723 724 725

	rnid->els_cmd.els_code = FC_ELS_RNID;
	rnid->node_id_data_format = data_format;

726
	return sizeof(struct fc_rnid_cmd_s);
727 728 729
}

u16
K
Krishna Gudipati 已提交
730
fc_rnid_acc_build(struct fchs_s *fchs, struct fc_rnid_acc_s *rnid_acc, u32 d_id,
731
		  u32 s_id, __be16 ox_id, u32 data_format,
K
Krishna Gudipati 已提交
732 733
		  struct fc_rnid_common_id_data_s *common_id_data,
		  struct fc_rnid_general_topology_data_s *gen_topo_data)
734
{
735
	memset(rnid_acc, 0, sizeof(struct fc_rnid_acc_s));
736 737 738 739 740 741 742 743 744 745 746 747

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

	rnid_acc->els_cmd.els_code = FC_ELS_ACC;
	rnid_acc->node_id_data_format = data_format;
	rnid_acc->common_id_data_length =
			sizeof(struct fc_rnid_common_id_data_s);
	rnid_acc->common_id_data = *common_id_data;

	if (data_format == RNID_NODEID_DATA_FORMAT_DISCOVERY) {
		rnid_acc->specific_id_data_length =
			sizeof(struct fc_rnid_general_topology_data_s);
748
		rnid_acc->gen_topology_data = *gen_topo_data;
749
		return sizeof(struct fc_rnid_acc_s);
750
	} else {
751 752
		return sizeof(struct fc_rnid_acc_s) -
			sizeof(struct fc_rnid_general_topology_data_s);
753 754 755 756 757 758
	}

}

u16
fc_rpsc_build(struct fchs_s *fchs, struct fc_rpsc_cmd_s *rpsc, u32 d_id,
K
Krishna Gudipati 已提交
759
		u32 s_id, u16 ox_id)
760 761 762
{
	fc_els_req_build(fchs, d_id, s_id, ox_id);

763
	memset(rpsc, 0, sizeof(struct fc_rpsc_cmd_s));
764 765

	rpsc->els_cmd.els_code = FC_ELS_RPSC;
766
	return sizeof(struct fc_rpsc_cmd_s);
767 768 769
}

u16
K
Krishna Gudipati 已提交
770 771
fc_rpsc2_build(struct fchs_s *fchs, struct fc_rpsc2_cmd_s *rpsc2, u32 d_id,
		u32 s_id, u32 *pid_list, u16 npids)
772
{
M
Maggie Zhang 已提交
773
	u32 dctlr_id = FC_DOMAIN_CTRLR(bfa_hton3b(d_id));
774 775
	int i = 0;

M
Maggie Zhang 已提交
776
	fc_els_req_build(fchs, bfa_hton3b(dctlr_id), s_id, 0);
777

778
	memset(rpsc2, 0, sizeof(struct fc_rpsc2_cmd_s));
779 780

	rpsc2->els_cmd.els_code = FC_ELS_RPSC;
781 782
	rpsc2->token = cpu_to_be32(FC_BRCD_TOKEN);
	rpsc2->num_pids  = cpu_to_be16(npids);
783 784 785
	for (i = 0; i < npids; i++)
		rpsc2->pid_list[i].pid = pid_list[i];

K
Krishna Gudipati 已提交
786
	return sizeof(struct fc_rpsc2_cmd_s) + ((npids - 1) * (sizeof(u32)));
787 788 789 790
}

u16
fc_rpsc_acc_build(struct fchs_s *fchs, struct fc_rpsc_acc_s *rpsc_acc,
791
		u32 d_id, u32 s_id, __be16 ox_id,
K
Krishna Gudipati 已提交
792
		  struct fc_rpsc_speed_info_s *oper_speed)
793
{
794
	memset(rpsc_acc, 0, sizeof(struct fc_rpsc_acc_s));
795 796 797 798

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

	rpsc_acc->command = FC_ELS_ACC;
799
	rpsc_acc->num_entries = cpu_to_be16(1);
800 801

	rpsc_acc->speed_info[0].port_speed_cap =
802
		cpu_to_be16(oper_speed->port_speed_cap);
803 804

	rpsc_acc->speed_info[0].port_op_speed =
805
		cpu_to_be16(oper_speed->port_op_speed);
806

807
	return sizeof(struct fc_rpsc_acc_s);
808 809 810 811 812 813 814 815 816 817
}

/*
 * TBD -
 * . get rid of unnecessary memsets
 */

u16
fc_logo_rsp_parse(struct fchs_s *fchs, int len)
{
K
Krishna Gudipati 已提交
818
	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
819 820 821 822 823 824 825 826 827

	len = len;
	if (els_cmd->els_code != FC_ELS_ACC)
		return FC_PARSE_FAILURE;

	return FC_PARSE_OK;
}

u16
K
Krishna Gudipati 已提交
828 829
fc_pdisc_build(struct fchs_s *fchs, u32 d_id, u32 s_id, u16 ox_id,
	       wwn_t port_name, wwn_t node_name, u16 pdu_size)
830
{
K
Krishna Gudipati 已提交
831
	struct fc_logi_s *pdisc = (struct fc_logi_s *) (fchs + 1);
832

833
	memcpy(pdisc, &plogi_tmpl, sizeof(struct fc_logi_s));
834 835 836 837

	pdisc->els_cmd.els_code = FC_ELS_PDISC;
	fc_els_req_build(fchs, d_id, s_id, ox_id);

838
	pdisc->csp.rxsz = pdisc->class3.rxsz = cpu_to_be16(pdu_size);
839 840 841
	pdisc->port_name = port_name;
	pdisc->node_name = node_name;

842
	return sizeof(struct fc_logi_s);
843 844 845 846 847
}

u16
fc_pdisc_rsp_parse(struct fchs_s *fchs, int len, wwn_t port_name)
{
K
Krishna Gudipati 已提交
848
	struct fc_logi_s *pdisc = (struct fc_logi_s *) (fchs + 1);
849 850

	if (len < sizeof(struct fc_logi_s))
851
		return FC_PARSE_LEN_INVAL;
852 853

	if (pdisc->els_cmd.els_code != FC_ELS_ACC)
854
		return FC_PARSE_ACC_INVAL;
855 856

	if (!wwn_is_equal(pdisc->port_name, port_name))
857
		return FC_PARSE_PWWN_NOT_EQUAL;
858 859

	if (!pdisc->class3.class_valid)
860
		return FC_PARSE_NWWN_NOT_EQUAL;
861

862
	if (be16_to_cpu(pdisc->class3.rxsz) < (FC_MIN_PDUSZ))
863
		return FC_PARSE_RXSZ_INVAL;
864

865
	return FC_PARSE_OK;
866 867 868 869 870 871
}

u16
fc_prlo_build(struct fchs_s *fchs, u32 d_id, u32 s_id, u16 ox_id,
	      int num_pages)
{
K
Krishna Gudipati 已提交
872
	struct fc_prlo_s *prlo = (struct fc_prlo_s *) (fchs + 1);
873 874 875
	int             page;

	fc_els_req_build(fchs, d_id, s_id, ox_id);
876
	memset(prlo, 0, (num_pages * 16) + 4);
877 878
	prlo->command = FC_ELS_PRLO;
	prlo->page_len = 0x10;
879
	prlo->payload_len = cpu_to_be16((num_pages * 16) + 4);
880 881 882 883 884 885 886 887 888

	for (page = 0; page < num_pages; page++) {
		prlo->prlo_params[page].type = FC_TYPE_FCP;
		prlo->prlo_params[page].opa_valid = 0;
		prlo->prlo_params[page].rpa_valid = 0;
		prlo->prlo_params[page].orig_process_assc = 0;
		prlo->prlo_params[page].resp_process_assc = 0;
	}

889
	return be16_to_cpu(prlo->payload_len);
890 891 892 893 894
}

u16
fc_prlo_rsp_parse(struct fchs_s *fchs, int len)
{
K
Krishna Gudipati 已提交
895
	struct fc_prlo_acc_s *prlo = (struct fc_prlo_acc_s *) (fchs + 1);
896 897 898 899 900 901
	int             num_pages = 0;
	int             page = 0;

	len = len;

	if (prlo->command != FC_ELS_ACC)
902
		return FC_PARSE_FAILURE;
903

904
	num_pages = ((be16_to_cpu(prlo->payload_len)) - 4) / 16;
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921

	for (page = 0; page < num_pages; page++) {
		if (prlo->prlo_acc_params[page].type != FC_TYPE_FCP)
			return FC_PARSE_FAILURE;

		if (prlo->prlo_acc_params[page].opa_valid != 0)
			return FC_PARSE_FAILURE;

		if (prlo->prlo_acc_params[page].rpa_valid != 0)
			return FC_PARSE_FAILURE;

		if (prlo->prlo_acc_params[page].orig_process_assc != 0)
			return FC_PARSE_FAILURE;

		if (prlo->prlo_acc_params[page].resp_process_assc != 0)
			return FC_PARSE_FAILURE;
	}
922
	return FC_PARSE_OK;
923 924 925 926

}

u16
K
Krishna Gudipati 已提交
927 928
fc_tprlo_build(struct fchs_s *fchs, u32 d_id, u32 s_id, u16 ox_id,
	       int num_pages, enum fc_tprlo_type tprlo_type, u32 tpr_id)
929
{
K
Krishna Gudipati 已提交
930
	struct fc_tprlo_s *tprlo = (struct fc_tprlo_s *) (fchs + 1);
931 932 933
	int             page;

	fc_els_req_build(fchs, d_id, s_id, ox_id);
934
	memset(tprlo, 0, (num_pages * 16) + 4);
935 936
	tprlo->command = FC_ELS_TPRLO;
	tprlo->page_len = 0x10;
937
	tprlo->payload_len = cpu_to_be16((num_pages * 16) + 4);
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

	for (page = 0; page < num_pages; page++) {
		tprlo->tprlo_params[page].type = FC_TYPE_FCP;
		tprlo->tprlo_params[page].opa_valid = 0;
		tprlo->tprlo_params[page].rpa_valid = 0;
		tprlo->tprlo_params[page].orig_process_assc = 0;
		tprlo->tprlo_params[page].resp_process_assc = 0;
		if (tprlo_type == FC_GLOBAL_LOGO) {
			tprlo->tprlo_params[page].global_process_logout = 1;
		} else if (tprlo_type == FC_TPR_LOGO) {
			tprlo->tprlo_params[page].tpo_nport_valid = 1;
			tprlo->tprlo_params[page].tpo_nport_id = (tpr_id);
		}
	}

953
	return be16_to_cpu(tprlo->payload_len);
954 955 956 957 958 959 960 961 962 963 964 965
}

u16
fc_tprlo_rsp_parse(struct fchs_s *fchs, int len)
{
	struct fc_tprlo_acc_s *tprlo = (struct fc_tprlo_acc_s *) (fchs + 1);
	int             num_pages = 0;
	int             page = 0;

	len = len;

	if (tprlo->command != FC_ELS_ACC)
966
		return FC_PARSE_ACC_INVAL;
967

968
	num_pages = (be16_to_cpu(tprlo->payload_len) - 4) / 16;
969 970 971

	for (page = 0; page < num_pages; page++) {
		if (tprlo->tprlo_acc_params[page].type != FC_TYPE_FCP)
972
			return FC_PARSE_NOT_FCP;
973
		if (tprlo->tprlo_acc_params[page].opa_valid != 0)
974
			return FC_PARSE_OPAFLAG_INVAL;
975
		if (tprlo->tprlo_acc_params[page].rpa_valid != 0)
976
			return FC_PARSE_RPAFLAG_INVAL;
977
		if (tprlo->tprlo_acc_params[page].orig_process_assc != 0)
978
			return FC_PARSE_OPA_INVAL;
979
		if (tprlo->tprlo_acc_params[page].resp_process_assc != 0)
980
			return FC_PARSE_RPA_INVAL;
981
	}
982
	return FC_PARSE_OK;
983 984 985 986 987
}

enum fc_parse_status
fc_rrq_rsp_parse(struct fchs_s *fchs, int len)
{
K
Krishna Gudipati 已提交
988
	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
989 990 991 992 993 994 995 996 997

	len = len;
	if (els_cmd->els_code != FC_ELS_ACC)
		return FC_PARSE_FAILURE;

	return FC_PARSE_OK;
}

u16
998
fc_ba_rjt_build(struct fchs_s *fchs, u32 d_id, u32 s_id, __be16 ox_id,
K
Krishna Gudipati 已提交
999
		u32 reason_code, u32 reason_expl)
1000
{
K
Krishna Gudipati 已提交
1001
	struct fc_ba_rjt_s *ba_rjt = (struct fc_ba_rjt_s *) (fchs + 1);
1002 1003 1004 1005 1006 1007

	fc_bls_rsp_build(fchs, d_id, s_id, ox_id);

	fchs->cat_info = FC_CAT_BA_RJT;
	ba_rjt->reason_code = reason_code;
	ba_rjt->reason_expl = reason_expl;
1008
	return sizeof(struct fc_ba_rjt_s);
1009 1010 1011 1012 1013
}

static void
fc_gs_cthdr_build(struct ct_hdr_s *cthdr, u32 s_id, u16 cmd_code)
{
1014
	memset(cthdr, 0, sizeof(struct ct_hdr_s));
1015 1016 1017
	cthdr->rev_id = CT_GS3_REVISION;
	cthdr->gs_type = CT_GSTYPE_DIRSERVICE;
	cthdr->gs_sub_type = CT_GSSUBTYPE_NAMESERVER;
1018
	cthdr->cmd_rsp_code = cpu_to_be16(cmd_code);
1019 1020 1021 1022 1023
}

static void
fc_gs_fdmi_cthdr_build(struct ct_hdr_s *cthdr, u32 s_id, u16 cmd_code)
{
1024
	memset(cthdr, 0, sizeof(struct ct_hdr_s));
1025 1026 1027
	cthdr->rev_id = CT_GS3_REVISION;
	cthdr->gs_type = CT_GSTYPE_MGMTSERVICE;
	cthdr->gs_sub_type = CT_GSSUBTYPE_HBA_MGMTSERVER;
1028
	cthdr->cmd_rsp_code = cpu_to_be16(cmd_code);
1029 1030 1031 1032 1033 1034
}

static void
fc_gs_ms_cthdr_build(struct ct_hdr_s *cthdr, u32 s_id, u16 cmd_code,
					 u8 sub_type)
{
1035
	memset(cthdr, 0, sizeof(struct ct_hdr_s));
1036 1037 1038
	cthdr->rev_id = CT_GS3_REVISION;
	cthdr->gs_type = CT_GSTYPE_MGMTSERVICE;
	cthdr->gs_sub_type = sub_type;
1039
	cthdr->cmd_rsp_code = cpu_to_be16(cmd_code);
1040 1041 1042 1043 1044 1045
}

u16
fc_gidpn_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
	       wwn_t port_name)
{
K
Krishna Gudipati 已提交
1046 1047
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
	struct fcgs_gidpn_req_s *gidpn = (struct fcgs_gidpn_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1048
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1049 1050 1051 1052

	fc_gs_fchdr_build(fchs, d_id, s_id, ox_id);
	fc_gs_cthdr_build(cthdr, s_id, GS_GID_PN);

1053
	memset(gidpn, 0, sizeof(struct fcgs_gidpn_req_s));
1054
	gidpn->port_name = port_name;
1055
	return sizeof(struct fcgs_gidpn_req_s) + sizeof(struct ct_hdr_s);
1056 1057 1058 1059 1060 1061
}

u16
fc_gpnid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
	       u32 port_id)
{
K
Krishna Gudipati 已提交
1062
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1063
	fcgs_gpnid_req_t *gpnid = (fcgs_gpnid_req_t *) (cthdr + 1);
M
Maggie Zhang 已提交
1064
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1065 1066 1067 1068

	fc_gs_fchdr_build(fchs, d_id, s_id, ox_id);
	fc_gs_cthdr_build(cthdr, s_id, GS_GPN_ID);

1069
	memset(gpnid, 0, sizeof(fcgs_gpnid_req_t));
1070
	gpnid->dap = port_id;
1071
	return sizeof(fcgs_gpnid_req_t) + sizeof(struct ct_hdr_s);
1072 1073 1074 1075 1076 1077
}

u16
fc_gnnid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
	       u32 port_id)
{
K
Krishna Gudipati 已提交
1078
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1079
	fcgs_gnnid_req_t *gnnid = (fcgs_gnnid_req_t *) (cthdr + 1);
M
Maggie Zhang 已提交
1080
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1081 1082 1083 1084

	fc_gs_fchdr_build(fchs, d_id, s_id, ox_id);
	fc_gs_cthdr_build(cthdr, s_id, GS_GNN_ID);

1085
	memset(gnnid, 0, sizeof(fcgs_gnnid_req_t));
1086
	gnnid->dap = port_id;
1087
	return sizeof(fcgs_gnnid_req_t) + sizeof(struct ct_hdr_s);
1088 1089 1090 1091 1092
}

u16
fc_ct_rsp_parse(struct ct_hdr_s *cthdr)
{
1093
	if (be16_to_cpu(cthdr->cmd_rsp_code) != CT_RSP_ACCEPT) {
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
		if (cthdr->reason_code == CT_RSN_LOGICAL_BUSY)
			return FC_PARSE_BUSY;
		else
			return FC_PARSE_FAILURE;
	}

	return FC_PARSE_OK;
}

u16
K
Krishna Gudipati 已提交
1104 1105
fc_scr_build(struct fchs_s *fchs, struct fc_scr_s *scr,
		u8 set_br_reg, u32 s_id, u16 ox_id)
1106
{
M
Maggie Zhang 已提交
1107
	u32        d_id = bfa_hton3b(FC_FABRIC_CONTROLLER);
1108 1109 1110

	fc_els_req_build(fchs, d_id, s_id, ox_id);

1111
	memset(scr, 0, sizeof(struct fc_scr_s));
1112 1113 1114 1115 1116
	scr->command = FC_ELS_SCR;
	scr->reg_func = FC_SCR_REG_FUNC_FULL;
	if (set_br_reg)
		scr->vu_reg_func = FC_VU_SCR_REG_FUNC_FABRIC_NAME_CHANGE;

1117
	return sizeof(struct fc_scr_s);
1118 1119 1120
}

u16
K
Krishna Gudipati 已提交
1121 1122
fc_rscn_build(struct fchs_s *fchs, struct fc_rscn_pl_s *rscn,
		u32 s_id, u16 ox_id)
1123
{
M
Maggie Zhang 已提交
1124
	u32        d_id = bfa_hton3b(FC_FABRIC_CONTROLLER);
1125 1126 1127 1128 1129 1130 1131
	u16        payldlen;

	fc_els_req_build(fchs, d_id, s_id, ox_id);
	rscn->command = FC_ELS_RSCN;
	rscn->pagelen = sizeof(rscn->event[0]);

	payldlen = sizeof(u32) + rscn->pagelen;
1132
	rscn->payldlen = cpu_to_be16(payldlen);
1133 1134 1135 1136

	rscn->event[0].format = FC_RSCN_FORMAT_PORTID;
	rscn->event[0].portid = s_id;

1137
	return sizeof(struct fc_rscn_pl_s);
1138 1139 1140 1141
}

u16
fc_rftid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
K
Krishna Gudipati 已提交
1142
	       enum bfa_lport_role roles)
1143
{
K
Krishna Gudipati 已提交
1144 1145
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
	struct fcgs_rftid_req_s *rftid = (struct fcgs_rftid_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1146
	u32        type_value, d_id = bfa_hton3b(FC_NAME_SERVER);
1147 1148 1149 1150 1151
	u8         index;

	fc_gs_fchdr_build(fchs, d_id, s_id, ox_id);
	fc_gs_cthdr_build(cthdr, s_id, GS_RFT_ID);

1152
	memset(rftid, 0, sizeof(struct fcgs_rftid_req_s));
1153 1154 1155 1156 1157 1158

	rftid->dap = s_id;

	/* By default, FCP FC4 Type is registered */
	index = FC_TYPE_FCP >> 5;
	type_value = 1 << (FC_TYPE_FCP % 32);
1159
	rftid->fc4_type[index] = cpu_to_be32(type_value);
1160

1161
	return sizeof(struct fcgs_rftid_req_s) + sizeof(struct ct_hdr_s);
1162 1163 1164
}

u16
K
Krishna Gudipati 已提交
1165 1166
fc_rftid_build_sol(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
		   u8 *fc4_bitmap, u32 bitmap_size)
1167
{
K
Krishna Gudipati 已提交
1168 1169
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
	struct fcgs_rftid_req_s *rftid = (struct fcgs_rftid_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1170
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1171 1172 1173 1174

	fc_gs_fchdr_build(fchs, d_id, s_id, ox_id);
	fc_gs_cthdr_build(cthdr, s_id, GS_RFT_ID);

1175
	memset(rftid, 0, sizeof(struct fcgs_rftid_req_s));
1176 1177

	rftid->dap = s_id;
1178
	memcpy((void *)rftid->fc4_type, (void *)fc4_bitmap,
K
Krishna Gudipati 已提交
1179
		(bitmap_size < 32 ? bitmap_size : 32));
1180

1181
	return sizeof(struct fcgs_rftid_req_s) + sizeof(struct ct_hdr_s);
1182 1183 1184 1185 1186 1187
}

u16
fc_rffid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
	       u8 fc4_type, u8 fc4_ftrs)
{
K
Krishna Gudipati 已提交
1188 1189
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
	struct fcgs_rffid_req_s *rffid = (struct fcgs_rffid_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1190
	u32         d_id = bfa_hton3b(FC_NAME_SERVER);
1191 1192 1193 1194

	fc_gs_fchdr_build(fchs, d_id, s_id, ox_id);
	fc_gs_cthdr_build(cthdr, s_id, GS_RFF_ID);

1195
	memset(rffid, 0, sizeof(struct fcgs_rffid_req_s));
1196

K
Krishna Gudipati 已提交
1197
	rffid->dap	    = s_id;
1198
	rffid->fc4ftr_bits  = fc4_ftrs;
K
Krishna Gudipati 已提交
1199
	rffid->fc4_type	    = fc4_type;
1200

1201
	return sizeof(struct fcgs_rffid_req_s) + sizeof(struct ct_hdr_s);
1202 1203 1204 1205 1206 1207 1208
}

u16
fc_rspnid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
		u8 *name)
{

K
Krishna Gudipati 已提交
1209
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1210
	struct fcgs_rspnid_req_s *rspnid =
K
Krishna Gudipati 已提交
1211
			(struct fcgs_rspnid_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1212
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1213 1214 1215 1216

	fc_gs_fchdr_build(fchs, d_id, s_id, ox_id);
	fc_gs_cthdr_build(cthdr, s_id, GS_RSPN_ID);

1217
	memset(rspnid, 0, sizeof(struct fcgs_rspnid_req_s));
1218 1219 1220 1221 1222

	rspnid->dap = s_id;
	rspnid->spn_len = (u8) strlen((char *)name);
	strncpy((char *)rspnid->spn, (char *)name, rspnid->spn_len);

1223
	return sizeof(struct fcgs_rspnid_req_s) + sizeof(struct ct_hdr_s);
1224 1225 1226
}

u16
K
Krishna Gudipati 已提交
1227
fc_gid_ft_build(struct fchs_s *fchs, void *pyld, u32 s_id, u8 fc4_type)
1228 1229
{

K
Krishna Gudipati 已提交
1230 1231
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
	struct fcgs_gidft_req_s *gidft = (struct fcgs_gidft_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1232
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1233 1234 1235 1236 1237

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);

	fc_gs_cthdr_build(cthdr, s_id, GS_GID_FT);

1238
	memset(gidft, 0, sizeof(struct fcgs_gidft_req_s));
1239 1240 1241 1242
	gidft->fc4_type = fc4_type;
	gidft->domain_id = 0;
	gidft->area_id = 0;

1243
	return sizeof(struct fcgs_gidft_req_s) + sizeof(struct ct_hdr_s);
1244 1245 1246 1247 1248 1249
}

u16
fc_rpnid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id,
	       wwn_t port_name)
{
K
Krishna Gudipati 已提交
1250 1251
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
	struct fcgs_rpnid_req_s *rpnid = (struct fcgs_rpnid_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1252
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1253 1254 1255 1256

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);
	fc_gs_cthdr_build(cthdr, s_id, GS_RPN_ID);

1257
	memset(rpnid, 0, sizeof(struct fcgs_rpnid_req_s));
1258 1259 1260
	rpnid->port_id = port_id;
	rpnid->port_name = port_name;

1261
	return sizeof(struct fcgs_rpnid_req_s) + sizeof(struct ct_hdr_s);
1262 1263 1264 1265 1266 1267
}

u16
fc_rnnid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id,
	       wwn_t node_name)
{
K
Krishna Gudipati 已提交
1268 1269
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
	struct fcgs_rnnid_req_s *rnnid = (struct fcgs_rnnid_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1270
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1271 1272 1273 1274

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);
	fc_gs_cthdr_build(cthdr, s_id, GS_RNN_ID);

1275
	memset(rnnid, 0, sizeof(struct fcgs_rnnid_req_s));
1276 1277 1278
	rnnid->port_id = port_id;
	rnnid->node_name = node_name;

1279
	return sizeof(struct fcgs_rnnid_req_s) + sizeof(struct ct_hdr_s);
1280 1281 1282 1283 1284 1285
}

u16
fc_rcsid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id,
	       u32 cos)
{
K
Krishna Gudipati 已提交
1286
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1287 1288
	struct fcgs_rcsid_req_s *rcsid =
			(struct fcgs_rcsid_req_s *) (cthdr + 1);
M
Maggie Zhang 已提交
1289
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1290 1291 1292 1293

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);
	fc_gs_cthdr_build(cthdr, s_id, GS_RCS_ID);

1294
	memset(rcsid, 0, sizeof(struct fcgs_rcsid_req_s));
1295 1296 1297
	rcsid->port_id = port_id;
	rcsid->cos = cos;

1298
	return sizeof(struct fcgs_rcsid_req_s) + sizeof(struct ct_hdr_s);
1299 1300 1301 1302 1303 1304
}

u16
fc_rptid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id,
	       u8 port_type)
{
K
Krishna Gudipati 已提交
1305 1306
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
	struct fcgs_rptid_req_s *rptid = (struct fcgs_rptid_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1307
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1308 1309 1310 1311

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);
	fc_gs_cthdr_build(cthdr, s_id, GS_RPT_ID);

1312
	memset(rptid, 0, sizeof(struct fcgs_rptid_req_s));
1313 1314 1315
	rptid->port_id = port_id;
	rptid->port_type = port_type;

1316
	return sizeof(struct fcgs_rptid_req_s) + sizeof(struct ct_hdr_s);
1317 1318 1319 1320 1321
}

u16
fc_ganxt_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id)
{
K
Krishna Gudipati 已提交
1322 1323
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
	struct fcgs_ganxt_req_s *ganxt = (struct fcgs_ganxt_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1324
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1325 1326 1327 1328

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);
	fc_gs_cthdr_build(cthdr, s_id, GS_GA_NXT);

1329
	memset(ganxt, 0, sizeof(struct fcgs_ganxt_req_s));
1330 1331
	ganxt->port_id = port_id;

1332
	return sizeof(struct ct_hdr_s) + sizeof(struct fcgs_ganxt_req_s);
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
}

/*
 * Builds fc hdr and ct hdr for FDMI requests.
 */
u16
fc_fdmi_reqhdr_build(struct fchs_s *fchs, void *pyld, u32 s_id,
		     u16 cmd_code)
{

K
Krishna Gudipati 已提交
1343
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
M
Maggie Zhang 已提交
1344
	u32        d_id = bfa_hton3b(FC_MGMT_SERVER);
1345 1346 1347 1348

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);
	fc_gs_fdmi_cthdr_build(cthdr, s_id, cmd_code);

1349
	return sizeof(struct ct_hdr_s);
1350 1351 1352 1353 1354 1355 1356 1357 1358
}

/*
 * Given a FC4 Type, this function returns a fc4 type bitmask
 */
void
fc_get_fc4type_bitmask(u8 fc4_type, u8 *bit_mask)
{
	u8         index;
1359
	__be32       *ptr = (__be32 *) bit_mask;
1360 1361 1362 1363 1364 1365 1366 1367
	u32        type_value;

	/*
	 * @todo : Check for bitmask size
	 */

	index = fc4_type >> 5;
	type_value = 1 << (fc4_type % 32);
1368
	ptr[index] = cpu_to_be32(type_value);
1369 1370 1371 1372

}

/*
K
Krishna Gudipati 已提交
1373
 *	GMAL Request
1374 1375 1376 1377
 */
u16
fc_gmal_req_build(struct fchs_s *fchs, void *pyld, u32 s_id, wwn_t wwn)
{
K
Krishna Gudipati 已提交
1378
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1379
	fcgs_gmal_req_t *gmal = (fcgs_gmal_req_t *) (cthdr + 1);
M
Maggie Zhang 已提交
1380
	u32        d_id = bfa_hton3b(FC_MGMT_SERVER);
1381 1382 1383 1384 1385

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);
	fc_gs_ms_cthdr_build(cthdr, s_id, GS_FC_GMAL_CMD,
			CT_GSSUBTYPE_CFGSERVER);

1386
	memset(gmal, 0, sizeof(fcgs_gmal_req_t));
1387 1388
	gmal->wwn = wwn;

1389
	return sizeof(struct ct_hdr_s) + sizeof(fcgs_gmal_req_t);
1390 1391 1392 1393 1394 1395 1396 1397
}

/*
 * GFN (Get Fabric Name) Request
 */
u16
fc_gfn_req_build(struct fchs_s *fchs, void *pyld, u32 s_id, wwn_t wwn)
{
K
Krishna Gudipati 已提交
1398
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1399
	fcgs_gfn_req_t *gfn = (fcgs_gfn_req_t *) (cthdr + 1);
M
Maggie Zhang 已提交
1400
	u32        d_id = bfa_hton3b(FC_MGMT_SERVER);
1401 1402 1403 1404 1405

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);
	fc_gs_ms_cthdr_build(cthdr, s_id, GS_FC_GFN_CMD,
			CT_GSSUBTYPE_CFGSERVER);

1406
	memset(gfn, 0, sizeof(fcgs_gfn_req_t));
1407 1408
	gfn->wwn = wwn;

1409
	return sizeof(struct ct_hdr_s) + sizeof(fcgs_gfn_req_t);
1410
}