bfa_fcbuild.c 37.2 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 97 98 99
			      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;
	plogi_tmpl.csp.ciro = 0x1;
	plogi_tmpl.csp.cisc = 0x0;
	plogi_tmpl.csp.altbbcred = 0x0;
100 101 102
	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);
103 104 105 106 107 108 109 110 111 112 113

	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;
114
	prli_tmpl.pagebytes = cpu_to_be16(0x0014);
115 116 117 118 119 120 121 122 123 124
	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 已提交
125
	 * fcp_struct fchs_s mpl
126 127 128 129 130
	 */
	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 已提交
131
		bfa_hton3b(FCTL_FS_EXCH | FCTL_END_SEQ | FCTL_SI_XFER);
132 133 134 135 136
	fcp_fchs_tmpl.seq_id = 1;
	fcp_fchs_tmpl.rx_id = FC_RXID_ANY;
}

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

	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 已提交
145
		bfa_hton3b(FCTL_SEQ_INI | FCTL_FS_EXCH | FCTL_END_SEQ |
146 147 148 149
			      FCTL_SI_XFER);
	fchs->rx_id = FC_RXID_ANY;
	fchs->d_id = (d_id);
	fchs->s_id = (s_id);
150
	fchs->ox_id = cpu_to_be16(ox_id);
151

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

K
Krishna Gudipati 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
static void
fc_gsresp_fchdr_build(struct fchs_s *fchs, u32 d_id, u32 s_id, u16 ox_id)
{
	memset(fchs, 0, sizeof(struct fchs_s));

	fchs->routing = FC_RTG_FC4_DEV_DATA;
	fchs->cat_info = FC_CAT_SOLICIT_CTRL;
	fchs->type = FC_TYPE_SERVICES;
	fchs->f_ctl =
		bfa_hton3b(FCTL_EC_RESP | FCTL_SEQ_INI | FCTL_LS_EXCH |
			   FCTL_END_SEQ | FCTL_SI_XFER);
	fchs->d_id = d_id;
	fchs->s_id = s_id;
	fchs->ox_id = ox_id;
}

174
void
175
fc_els_req_build(struct fchs_s *fchs, u32 d_id, u32 s_id, __be16 ox_id)
176
{
177
	memcpy(fchs, &fc_els_req_tmpl, sizeof(struct fchs_s));
178 179
	fchs->d_id = (d_id);
	fchs->s_id = (s_id);
180
	fchs->ox_id = cpu_to_be16(ox_id);
181 182 183
}

static void
184
fc_els_rsp_build(struct fchs_s *fchs, u32 d_id, u32 s_id, __be16 ox_id)
185
{
186
	memcpy(fchs, &fc_els_rsp_tmpl, sizeof(struct fchs_s));
187 188 189 190 191 192 193 194
	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 已提交
195 196
	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;
197 198 199 200 201 202

	len = len;

	switch (els_cmd->els_code) {
	case FC_ELS_LS_RJT:
		if (ls_rjt->reason_code == FC_LS_RJT_RSN_LOGICAL_BUSY)
203
			return FC_PARSE_BUSY;
204
		else
205
			return FC_PARSE_FAILURE;
206 207

	case FC_ELS_ACC:
208
		return FC_PARSE_OK;
209
	}
210
	return FC_PARSE_OK;
211 212 213
}

static void
214
fc_bls_rsp_build(struct fchs_s *fchs, u32 d_id, u32 s_id, __be16 ox_id)
215
{
216
	memcpy(fchs, &fc_bls_rsp_tmpl, sizeof(struct fchs_s));
217 218 219 220 221 222 223
	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,
224
		 __be16 ox_id, wwn_t port_name, wwn_t node_name,
225
		 u16 pdu_size, u16 bb_cr, u8 els_code)
226
{
K
Krishna Gudipati 已提交
227
	struct fc_logi_s *plogi = (struct fc_logi_s *) (pld);
228

229
	memcpy(plogi, &plogi_tmpl, sizeof(struct fc_logi_s));
230 231 232 233 234 235 236

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

237
	plogi->csp.rxsz = plogi->class3.rxsz = cpu_to_be16(pdu_size);
238
	plogi->csp.bbcred  = cpu_to_be16(bb_cr);
239

240 241
	memcpy(&plogi->port_name, &port_name, sizeof(wwn_t));
	memcpy(&plogi->node_name, &node_name, sizeof(wwn_t));
242

243
	return sizeof(struct fc_logi_s);
244 245 246 247
}

u16
fc_flogi_build(struct fchs_s *fchs, struct fc_logi_s *flogi, u32 s_id,
K
Krishna Gudipati 已提交
248 249
		u16 ox_id, wwn_t port_name, wwn_t node_name, u16 pdu_size,
	       u8 set_npiv, u8 set_auth, u16 local_bb_credits)
250
{
M
Maggie Zhang 已提交
251
	u32        d_id = bfa_hton3b(FC_FABRIC_PORT);
252
	__be32	*vvl_info;
253

254
	memcpy(flogi, &plogi_tmpl, sizeof(struct fc_logi_s));
255 256 257 258

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

259
	flogi->csp.rxsz = flogi->class3.rxsz = cpu_to_be16(pdu_size);
260 261 262 263 264 265 266 267 268 269 270 271
	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;

272
	flogi->csp.bbcred = cpu_to_be16(local_bb_credits);
273 274 275 276 277 278

	/* 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 */
279
	vvl_info[0]	= cpu_to_be32(FLOGI_VVL_BRCD);
280

281
	return sizeof(struct fc_logi_s);
282 283 284 285
}

u16
fc_flogi_acc_build(struct fchs_s *fchs, struct fc_logi_s *flogi, u32 s_id,
286
		   __be16 ox_id, wwn_t port_name, wwn_t node_name,
287
		   u16 pdu_size, u16 local_bb_credits, u8 bb_scn)
288 289
{
	u32        d_id = 0;
290
	u16	   bbscn_rxsz = (bb_scn << 12) | pdu_size;
291

292
	memcpy(flogi, &plogi_tmpl, sizeof(struct fc_logi_s));
293 294 295
	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

	flogi->els_cmd.els_code = FC_ELS_ACC;
296 297
	flogi->class3.rxsz = cpu_to_be16(pdu_size);
	flogi->csp.rxsz  = cpu_to_be16(bbscn_rxsz);	/* bb_scn/rxsz */
298 299 300
	flogi->port_name = port_name;
	flogi->node_name = node_name;

301
	flogi->csp.bbcred = cpu_to_be16(local_bb_credits);
302

303
	return sizeof(struct fc_logi_s);
304 305 306 307
}

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

312
	memcpy(flogi, &plogi_tmpl, sizeof(struct fc_logi_s));
313 314 315 316

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

317
	flogi->csp.rxsz = flogi->class3.rxsz = cpu_to_be16(pdu_size);
318 319 320
	flogi->port_name = port_name;
	flogi->node_name = node_name;

321
	return sizeof(struct fc_logi_s);
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,
327
	       u16 pdu_size, u16 bb_cr)
328 329
{
	return fc_plogi_x_build(fchs, pld, d_id, s_id, ox_id, port_name,
330
				node_name, pdu_size, bb_cr, FC_ELS_PLOGI);
331 332 333 334 335
}

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,
336
		   u16 pdu_size, u16 bb_cr)
337 338
{
	return fc_plogi_x_build(fchs, pld, d_id, s_id, ox_id, port_name,
339
				node_name, pdu_size, bb_cr, FC_ELS_ACC);
340 341 342 343 344
}

enum fc_parse_status
fc_plogi_rsp_parse(struct fchs_s *fchs, int len, wwn_t port_name)
{
K
Krishna Gudipati 已提交
345 346 347
	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;
348 349 350 351 352

	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)
353
			return FC_PARSE_BUSY;
354
		else
355
			return FC_PARSE_FAILURE;
356 357 358
	case FC_ELS_ACC:
		plogi = (struct fc_logi_s *) (fchs + 1);
		if (len < sizeof(struct fc_logi_s))
359
			return FC_PARSE_FAILURE;
360 361

		if (!wwn_is_equal(plogi->port_name, port_name))
362
			return FC_PARSE_FAILURE;
363 364

		if (!plogi->class3.class_valid)
365
			return FC_PARSE_FAILURE;
366

367
		if (be16_to_cpu(plogi->class3.rxsz) < (FC_MIN_PDUSZ))
368
			return FC_PARSE_FAILURE;
369

370
		return FC_PARSE_OK;
371
	default:
372
		return FC_PARSE_FAILURE;
373 374 375 376 377 378
	}
}

enum fc_parse_status
fc_plogi_parse(struct fchs_s *fchs)
{
K
Krishna Gudipati 已提交
379
	struct fc_logi_s *plogi = (struct fc_logi_s *) (fchs + 1);
380 381 382 383

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

384 385
	if ((be16_to_cpu(plogi->class3.rxsz) < FC_MIN_PDUSZ)
	    || (be16_to_cpu(plogi->class3.rxsz) > FC_MAX_PDUSZ)
386
	    || (plogi->class3.rxsz == 0))
387
		return FC_PARSE_FAILURE;
388 389 390 391 392 393 394 395

	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 已提交
396
	struct fc_prli_s *prli = (struct fc_prli_s *) (pld);
397 398

	fc_els_req_build(fchs, d_id, s_id, ox_id);
399
	memcpy(prli, &prli_tmpl, sizeof(struct fc_prli_s));
400 401 402 403 404 405 406 407

	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;

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

u16
fc_prli_acc_build(struct fchs_s *fchs, void *pld, u32 d_id, u32 s_id,
413
		  __be16 ox_id, enum bfa_lport_role role)
414
{
K
Krishna Gudipati 已提交
415
	struct fc_prli_s *prli = (struct fc_prli_s *) (pld);
416 417

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);
418
	memcpy(prli, &prli_tmpl, sizeof(struct fc_prli_s));
419 420 421

	prli->command = FC_ELS_ACC;

K
Krishna Gudipati 已提交
422
	prli->parampage.servparams.initiator = 1;
423 424 425

	prli->parampage.rspcode = FC_PRLI_ACC_XQTD;

426
	return sizeof(struct fc_prli_s);
427 428 429 430 431 432
}

enum fc_parse_status
fc_prli_rsp_parse(struct fc_prli_s *prli, int len)
{
	if (len < sizeof(struct fc_prli_s))
433
		return FC_PARSE_FAILURE;
434 435

	if (prli->command != FC_ELS_ACC)
436
		return FC_PARSE_FAILURE;
437 438 439

	if ((prli->parampage.rspcode != FC_PRLI_ACC_XQTD)
	    && (prli->parampage.rspcode != FC_PRLI_ACC_PREDEF_IMG))
440
		return FC_PARSE_FAILURE;
441 442

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

445
	return FC_PARSE_OK;
446 447 448 449 450 451
}

enum fc_parse_status
fc_prli_parse(struct fc_prli_s *prli)
{
	if (prli->parampage.type != FC_TYPE_FCP)
452
		return FC_PARSE_FAILURE;
453 454

	if (!prli->parampage.imagepair)
455
		return FC_PARSE_FAILURE;
456 457

	if (!prli->parampage.servparams.initiator)
458
		return FC_PARSE_FAILURE;
459

460
	return FC_PARSE_OK;
461 462 463
}

u16
K
Krishna Gudipati 已提交
464 465
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)
466 467 468
{
	fc_els_req_build(fchs, d_id, s_id, ox_id);

469
	memset(logo, '\0', sizeof(struct fc_logo_s));
470 471 472 473
	logo->els_cmd.els_code = FC_ELS_LOGO;
	logo->nport_id = (s_id);
	logo->orig_port_name = port_name;

474
	return sizeof(struct fc_logo_s);
475 476
}

477
static u16
478
fc_adisc_x_build(struct fchs_s *fchs, struct fc_adisc_s *adisc, u32 d_id,
479
		 u32 s_id, __be16 ox_id, wwn_t port_name,
480 481
		 wwn_t node_name, u8 els_code)
{
482
	memset(adisc, '\0', sizeof(struct fc_adisc_s));
483 484 485 486 487 488 489 490 491 492 493 494 495

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

496
	return sizeof(struct fc_adisc_s);
497 498 499 500
}

u16
fc_adisc_build(struct fchs_s *fchs, struct fc_adisc_s *adisc, u32 d_id,
501
		u32 s_id, __be16 ox_id, wwn_t port_name, wwn_t node_name)
502 503 504 505 506 507 508
{
	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,
509
		   u32 s_id, __be16 ox_id, wwn_t port_name,
510 511 512 513 514 515 516 517 518 519 520 521
		   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))
522
		return FC_PARSE_FAILURE;
523 524

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

	if (!wwn_is_equal(adisc->orig_port_name, port_name))
528
		return FC_PARSE_FAILURE;
529

530
	return FC_PARSE_OK;
531 532 533
}

enum fc_parse_status
K
Krishna Gudipati 已提交
534 535
fc_adisc_parse(struct fchs_s *fchs, void *pld, u32 host_dap, wwn_t node_name,
	       wwn_t port_name)
536
{
K
Krishna Gudipati 已提交
537
	struct fc_adisc_s *adisc = (struct fc_adisc_s *) pld;
538 539

	if (adisc->els_cmd.els_code != FC_ELS_ACC)
540
		return FC_PARSE_FAILURE;
541 542 543 544

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

547
	return FC_PARSE_FAILURE;
548 549 550 551 552
}

enum fc_parse_status
fc_pdisc_parse(struct fchs_s *fchs, wwn_t node_name, wwn_t port_name)
{
K
Krishna Gudipati 已提交
553
	struct fc_logi_s *pdisc = (struct fc_logi_s *) (fchs + 1);
554 555 556 557

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

558
	if ((be16_to_cpu(pdisc->class3.rxsz) <
K
Krishna Gudipati 已提交
559
		(FC_MIN_PDUSZ - sizeof(struct fchs_s)))
560
	    || (pdisc->class3.rxsz == 0))
561
		return FC_PARSE_FAILURE;
562 563

	if (!wwn_is_equal(pdisc->port_name, port_name))
564
		return FC_PARSE_FAILURE;
565 566

	if (!wwn_is_equal(pdisc->node_name, node_name))
567
		return FC_PARSE_FAILURE;
568 569 570 571 572 573 574

	return FC_PARSE_OK;
}

u16
fc_abts_build(struct fchs_s *fchs, u32 d_id, u32 s_id, u16 ox_id)
{
575
	memcpy(fchs, &fc_bls_req_tmpl, sizeof(struct fchs_s));
576 577 578
	fchs->cat_info = FC_CAT_ABTS;
	fchs->d_id = (d_id);
	fchs->s_id = (s_id);
579
	fchs->ox_id = cpu_to_be16(ox_id);
580

581
	return sizeof(struct fchs_s);
582 583 584 585 586 587 588
}

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))
589
		return FC_PARSE_OK;
590

591
	return FC_PARSE_FAILURE;
592 593 594
}

u16
K
Krishna Gudipati 已提交
595 596
fc_rrq_build(struct fchs_s *fchs, struct fc_rrq_s *rrq, u32 d_id, u32 s_id,
	     u16 ox_id, u16 rrq_oxid)
597 598 599 600 601 602
{
	fc_els_req_build(fchs, d_id, s_id, ox_id);

	/*
	 * build rrq payload
	 */
603
	memcpy(rrq, &rrq_tmpl, sizeof(struct fc_rrq_s));
604
	rrq->s_id = (s_id);
605
	rrq->ox_id = cpu_to_be16(rrq_oxid);
606 607
	rrq->rx_id = FC_RXID_ANY;

608
	return sizeof(struct fc_rrq_s);
609 610 611 612
}

u16
fc_logo_acc_build(struct fchs_s *fchs, void *pld, u32 d_id, u32 s_id,
613
		  __be16 ox_id)
614
{
K
Krishna Gudipati 已提交
615
	struct fc_els_cmd_s *acc = pld;
616 617 618

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

619
	memset(acc, 0, sizeof(struct fc_els_cmd_s));
620 621
	acc->els_code = FC_ELS_ACC;

622
	return sizeof(struct fc_els_cmd_s);
623 624 625 626
}

u16
fc_ls_rjt_build(struct fchs_s *fchs, struct fc_ls_rjt_s *ls_rjt, u32 d_id,
627
		u32 s_id, __be16 ox_id, u8 reason_code,
628 629 630
		u8 reason_code_expl)
{
	fc_els_rsp_build(fchs, d_id, s_id, ox_id);
631
	memset(ls_rjt, 0, sizeof(struct fc_ls_rjt_s));
632 633 634 635 636 637

	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;

638
	return sizeof(struct fc_ls_rjt_s);
639 640 641 642
}

u16
fc_ba_acc_build(struct fchs_s *fchs, struct fc_ba_acc_s *ba_acc, u32 d_id,
643
		u32 s_id, __be16 ox_id, u16 rx_id)
644 645 646
{
	fc_bls_rsp_build(fchs, d_id, s_id, ox_id);

647
	memcpy(ba_acc, &ba_acc_tmpl, sizeof(struct fc_ba_acc_s));
648 649 650 651 652 653

	fchs->rx_id = rx_id;

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

654
	return sizeof(struct fc_ba_acc_s);
655 656 657
}

u16
K
Krishna Gudipati 已提交
658
fc_ls_acc_build(struct fchs_s *fchs, struct fc_els_cmd_s *els_cmd, u32 d_id,
659
		u32 s_id, __be16 ox_id)
660 661
{
	fc_els_rsp_build(fchs, d_id, s_id, ox_id);
662
	memset(els_cmd, 0, sizeof(struct fc_els_cmd_s));
663 664
	els_cmd->els_code = FC_ELS_ACC;

665
	return sizeof(struct fc_els_cmd_s);
666 667 668 669 670 671
}

int
fc_logout_params_pages(struct fchs_s *fc_frame, u8 els_code)
{
	int             num_pages = 0;
K
Krishna Gudipati 已提交
672 673
	struct fc_prlo_s *prlo;
	struct fc_tprlo_s *tprlo;
674 675 676

	if (els_code == FC_ELS_PRLO) {
		prlo = (struct fc_prlo_s *) (fc_frame + 1);
677
		num_pages = (be16_to_cpu(prlo->payload_len) - 4) / 16;
678 679
	} else {
		tprlo = (struct fc_tprlo_s *) (fc_frame + 1);
680
		num_pages = (be16_to_cpu(tprlo->payload_len) - 4) / 16;
681 682 683 684 685 686
	}
	return num_pages;
}

u16
fc_tprlo_acc_build(struct fchs_s *fchs, struct fc_tprlo_acc_s *tprlo_acc,
687
		u32 d_id, u32 s_id, __be16 ox_id, int num_pages)
688 689 690 691 692
{
	int             page;

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

693
	memset(tprlo_acc, 0, (num_pages * 16) + 4);
694 695 696
	tprlo_acc->command = FC_ELS_ACC;

	tprlo_acc->page_len = 0x10;
697
	tprlo_acc->payload_len = cpu_to_be16((num_pages * 16) + 4);
698 699 700 701 702 703 704 705

	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;
	}
706
	return be16_to_cpu(tprlo_acc->payload_len);
707 708 709
}

u16
K
Krishna Gudipati 已提交
710
fc_prlo_acc_build(struct fchs_s *fchs, struct fc_prlo_acc_s *prlo_acc, u32 d_id,
711
		  u32 s_id, __be16 ox_id, int num_pages)
712 713 714 715 716
{
	int             page;

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

717
	memset(prlo_acc, 0, (num_pages * 16) + 4);
718 719
	prlo_acc->command = FC_ELS_ACC;
	prlo_acc->page_len = 0x10;
720
	prlo_acc->payload_len = cpu_to_be16((num_pages * 16) + 4);
721 722 723 724 725 726 727 728 729

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

730
	return be16_to_cpu(prlo_acc->payload_len);
731 732 733 734
}

u16
fc_rnid_build(struct fchs_s *fchs, struct fc_rnid_cmd_s *rnid, u32 d_id,
K
Krishna Gudipati 已提交
735
		u32 s_id, u16 ox_id, u32 data_format)
736 737 738
{
	fc_els_req_build(fchs, d_id, s_id, ox_id);

739
	memset(rnid, 0, sizeof(struct fc_rnid_cmd_s));
740 741 742 743

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

744
	return sizeof(struct fc_rnid_cmd_s);
745 746 747
}

u16
K
Krishna Gudipati 已提交
748
fc_rnid_acc_build(struct fchs_s *fchs, struct fc_rnid_acc_s *rnid_acc, u32 d_id,
749
		  u32 s_id, __be16 ox_id, u32 data_format,
K
Krishna Gudipati 已提交
750 751
		  struct fc_rnid_common_id_data_s *common_id_data,
		  struct fc_rnid_general_topology_data_s *gen_topo_data)
752
{
753
	memset(rnid_acc, 0, sizeof(struct fc_rnid_acc_s));
754 755 756 757 758 759 760 761 762 763 764 765

	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);
766
		rnid_acc->gen_topology_data = *gen_topo_data;
767
		return sizeof(struct fc_rnid_acc_s);
768
	} else {
769 770
		return sizeof(struct fc_rnid_acc_s) -
			sizeof(struct fc_rnid_general_topology_data_s);
771 772 773 774 775 776
	}

}

u16
fc_rpsc_build(struct fchs_s *fchs, struct fc_rpsc_cmd_s *rpsc, u32 d_id,
K
Krishna Gudipati 已提交
777
		u32 s_id, u16 ox_id)
778 779 780
{
	fc_els_req_build(fchs, d_id, s_id, ox_id);

781
	memset(rpsc, 0, sizeof(struct fc_rpsc_cmd_s));
782 783

	rpsc->els_cmd.els_code = FC_ELS_RPSC;
784
	return sizeof(struct fc_rpsc_cmd_s);
785 786 787
}

u16
K
Krishna Gudipati 已提交
788 789
fc_rpsc2_build(struct fchs_s *fchs, struct fc_rpsc2_cmd_s *rpsc2, u32 d_id,
		u32 s_id, u32 *pid_list, u16 npids)
790
{
M
Maggie Zhang 已提交
791
	u32 dctlr_id = FC_DOMAIN_CTRLR(bfa_hton3b(d_id));
792 793
	int i = 0;

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

796
	memset(rpsc2, 0, sizeof(struct fc_rpsc2_cmd_s));
797 798

	rpsc2->els_cmd.els_code = FC_ELS_RPSC;
799 800
	rpsc2->token = cpu_to_be32(FC_BRCD_TOKEN);
	rpsc2->num_pids  = cpu_to_be16(npids);
801 802 803
	for (i = 0; i < npids; i++)
		rpsc2->pid_list[i].pid = pid_list[i];

K
Krishna Gudipati 已提交
804
	return sizeof(struct fc_rpsc2_cmd_s) + ((npids - 1) * (sizeof(u32)));
805 806 807 808
}

u16
fc_rpsc_acc_build(struct fchs_s *fchs, struct fc_rpsc_acc_s *rpsc_acc,
809
		u32 d_id, u32 s_id, __be16 ox_id,
K
Krishna Gudipati 已提交
810
		  struct fc_rpsc_speed_info_s *oper_speed)
811
{
812
	memset(rpsc_acc, 0, sizeof(struct fc_rpsc_acc_s));
813 814 815 816

	fc_els_rsp_build(fchs, d_id, s_id, ox_id);

	rpsc_acc->command = FC_ELS_ACC;
817
	rpsc_acc->num_entries = cpu_to_be16(1);
818 819

	rpsc_acc->speed_info[0].port_speed_cap =
820
		cpu_to_be16(oper_speed->port_speed_cap);
821 822

	rpsc_acc->speed_info[0].port_op_speed =
823
		cpu_to_be16(oper_speed->port_op_speed);
824

825
	return sizeof(struct fc_rpsc_acc_s);
826 827 828 829 830
}

u16
fc_logo_rsp_parse(struct fchs_s *fchs, int len)
{
K
Krishna Gudipati 已提交
831
	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
832 833 834 835 836 837 838 839 840

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

	return FC_PARSE_OK;
}

u16
K
Krishna Gudipati 已提交
841 842
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)
843
{
K
Krishna Gudipati 已提交
844
	struct fc_logi_s *pdisc = (struct fc_logi_s *) (fchs + 1);
845

846
	memcpy(pdisc, &plogi_tmpl, sizeof(struct fc_logi_s));
847 848 849 850

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

851
	pdisc->csp.rxsz = pdisc->class3.rxsz = cpu_to_be16(pdu_size);
852 853 854
	pdisc->port_name = port_name;
	pdisc->node_name = node_name;

855
	return sizeof(struct fc_logi_s);
856 857 858 859 860
}

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

	if (len < sizeof(struct fc_logi_s))
864
		return FC_PARSE_LEN_INVAL;
865 866

	if (pdisc->els_cmd.els_code != FC_ELS_ACC)
867
		return FC_PARSE_ACC_INVAL;
868 869

	if (!wwn_is_equal(pdisc->port_name, port_name))
870
		return FC_PARSE_PWWN_NOT_EQUAL;
871 872

	if (!pdisc->class3.class_valid)
873
		return FC_PARSE_NWWN_NOT_EQUAL;
874

875
	if (be16_to_cpu(pdisc->class3.rxsz) < (FC_MIN_PDUSZ))
876
		return FC_PARSE_RXSZ_INVAL;
877

878
	return FC_PARSE_OK;
879 880 881 882 883 884
}

u16
fc_prlo_build(struct fchs_s *fchs, u32 d_id, u32 s_id, u16 ox_id,
	      int num_pages)
{
K
Krishna Gudipati 已提交
885
	struct fc_prlo_s *prlo = (struct fc_prlo_s *) (fchs + 1);
886 887 888
	int             page;

	fc_els_req_build(fchs, d_id, s_id, ox_id);
889
	memset(prlo, 0, (num_pages * 16) + 4);
890 891
	prlo->command = FC_ELS_PRLO;
	prlo->page_len = 0x10;
892
	prlo->payload_len = cpu_to_be16((num_pages * 16) + 4);
893 894 895 896 897 898 899 900 901

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

902
	return be16_to_cpu(prlo->payload_len);
903 904 905 906 907
}

u16
fc_prlo_rsp_parse(struct fchs_s *fchs, int len)
{
K
Krishna Gudipati 已提交
908
	struct fc_prlo_acc_s *prlo = (struct fc_prlo_acc_s *) (fchs + 1);
909 910 911 912 913 914
	int             num_pages = 0;
	int             page = 0;

	len = len;

	if (prlo->command != FC_ELS_ACC)
915
		return FC_PARSE_FAILURE;
916

917
	num_pages = ((be16_to_cpu(prlo->payload_len)) - 4) / 16;
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934

	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;
	}
935
	return FC_PARSE_OK;
936 937 938 939

}

u16
K
Krishna Gudipati 已提交
940 941
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)
942
{
K
Krishna Gudipati 已提交
943
	struct fc_tprlo_s *tprlo = (struct fc_tprlo_s *) (fchs + 1);
944 945 946
	int             page;

	fc_els_req_build(fchs, d_id, s_id, ox_id);
947
	memset(tprlo, 0, (num_pages * 16) + 4);
948 949
	tprlo->command = FC_ELS_TPRLO;
	tprlo->page_len = 0x10;
950
	tprlo->payload_len = cpu_to_be16((num_pages * 16) + 4);
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965

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

966
	return be16_to_cpu(tprlo->payload_len);
967 968 969 970 971 972 973 974 975 976 977 978
}

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)
979
		return FC_PARSE_ACC_INVAL;
980

981
	num_pages = (be16_to_cpu(tprlo->payload_len) - 4) / 16;
982 983 984

	for (page = 0; page < num_pages; page++) {
		if (tprlo->tprlo_acc_params[page].type != FC_TYPE_FCP)
985
			return FC_PARSE_NOT_FCP;
986
		if (tprlo->tprlo_acc_params[page].opa_valid != 0)
987
			return FC_PARSE_OPAFLAG_INVAL;
988
		if (tprlo->tprlo_acc_params[page].rpa_valid != 0)
989
			return FC_PARSE_RPAFLAG_INVAL;
990
		if (tprlo->tprlo_acc_params[page].orig_process_assc != 0)
991
			return FC_PARSE_OPA_INVAL;
992
		if (tprlo->tprlo_acc_params[page].resp_process_assc != 0)
993
			return FC_PARSE_RPA_INVAL;
994
	}
995
	return FC_PARSE_OK;
996 997 998 999 1000
}

enum fc_parse_status
fc_rrq_rsp_parse(struct fchs_s *fchs, int len)
{
K
Krishna Gudipati 已提交
1001
	struct fc_els_cmd_s *els_cmd = (struct fc_els_cmd_s *) (fchs + 1);
1002 1003 1004 1005 1006 1007 1008 1009 1010

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

	return FC_PARSE_OK;
}

u16
1011
fc_ba_rjt_build(struct fchs_s *fchs, u32 d_id, u32 s_id, __be16 ox_id,
K
Krishna Gudipati 已提交
1012
		u32 reason_code, u32 reason_expl)
1013
{
K
Krishna Gudipati 已提交
1014
	struct fc_ba_rjt_s *ba_rjt = (struct fc_ba_rjt_s *) (fchs + 1);
1015 1016 1017 1018 1019 1020

	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;
1021
	return sizeof(struct fc_ba_rjt_s);
1022 1023 1024 1025 1026
}

static void
fc_gs_cthdr_build(struct ct_hdr_s *cthdr, u32 s_id, u16 cmd_code)
{
1027
	memset(cthdr, 0, sizeof(struct ct_hdr_s));
1028 1029 1030
	cthdr->rev_id = CT_GS3_REVISION;
	cthdr->gs_type = CT_GSTYPE_DIRSERVICE;
	cthdr->gs_sub_type = CT_GSSUBTYPE_NAMESERVER;
1031
	cthdr->cmd_rsp_code = cpu_to_be16(cmd_code);
1032 1033 1034 1035 1036
}

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

static void
fc_gs_ms_cthdr_build(struct ct_hdr_s *cthdr, u32 s_id, u16 cmd_code,
					 u8 sub_type)
{
1048
	memset(cthdr, 0, sizeof(struct ct_hdr_s));
1049 1050 1051
	cthdr->rev_id = CT_GS3_REVISION;
	cthdr->gs_type = CT_GSTYPE_MGMTSERVICE;
	cthdr->gs_sub_type = sub_type;
1052
	cthdr->cmd_rsp_code = cpu_to_be16(cmd_code);
1053 1054 1055 1056 1057 1058
}

u16
fc_gidpn_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
	       wwn_t port_name)
{
K
Krishna Gudipati 已提交
1059 1060
	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 已提交
1061
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1062 1063 1064 1065

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

1066
	memset(gidpn, 0, sizeof(struct fcgs_gidpn_req_s));
1067
	gidpn->port_name = port_name;
1068
	return sizeof(struct fcgs_gidpn_req_s) + sizeof(struct ct_hdr_s);
1069 1070 1071 1072 1073 1074
}

u16
fc_gpnid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
	       u32 port_id)
{
K
Krishna Gudipati 已提交
1075
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1076
	fcgs_gpnid_req_t *gpnid = (fcgs_gpnid_req_t *) (cthdr + 1);
M
Maggie Zhang 已提交
1077
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1078 1079 1080 1081

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

1082
	memset(gpnid, 0, sizeof(fcgs_gpnid_req_t));
1083
	gpnid->dap = port_id;
1084
	return sizeof(fcgs_gpnid_req_t) + sizeof(struct ct_hdr_s);
1085 1086 1087 1088 1089 1090
}

u16
fc_gnnid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
	       u32 port_id)
{
K
Krishna Gudipati 已提交
1091
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1092
	fcgs_gnnid_req_t *gnnid = (fcgs_gnnid_req_t *) (cthdr + 1);
M
Maggie Zhang 已提交
1093
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1094 1095 1096 1097

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

1098
	memset(gnnid, 0, sizeof(fcgs_gnnid_req_t));
1099
	gnnid->dap = port_id;
1100
	return sizeof(fcgs_gnnid_req_t) + sizeof(struct ct_hdr_s);
1101 1102 1103 1104 1105
}

u16
fc_ct_rsp_parse(struct ct_hdr_s *cthdr)
{
1106
	if (be16_to_cpu(cthdr->cmd_rsp_code) != CT_RSP_ACCEPT) {
1107 1108 1109 1110 1111 1112 1113 1114 1115
		if (cthdr->reason_code == CT_RSN_LOGICAL_BUSY)
			return FC_PARSE_BUSY;
		else
			return FC_PARSE_FAILURE;
	}

	return FC_PARSE_OK;
}

K
Krishna Gudipati 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
u16
fc_gs_rjt_build(struct fchs_s *fchs,  struct ct_hdr_s *cthdr,
		u32 d_id, u32 s_id, u16 ox_id, u8 reason_code,
		u8 reason_code_expl)
{
	fc_gsresp_fchdr_build(fchs, d_id, s_id, ox_id);

	cthdr->cmd_rsp_code = cpu_to_be16(CT_RSP_REJECT);
	cthdr->rev_id = CT_GS3_REVISION;

	cthdr->reason_code = reason_code;
	cthdr->exp_code    = reason_code_expl;
	return sizeof(struct ct_hdr_s);
}

1131
u16
K
Krishna Gudipati 已提交
1132 1133
fc_scr_build(struct fchs_s *fchs, struct fc_scr_s *scr,
		u8 set_br_reg, u32 s_id, u16 ox_id)
1134
{
M
Maggie Zhang 已提交
1135
	u32        d_id = bfa_hton3b(FC_FABRIC_CONTROLLER);
1136 1137 1138

	fc_els_req_build(fchs, d_id, s_id, ox_id);

1139
	memset(scr, 0, sizeof(struct fc_scr_s));
1140 1141 1142 1143 1144
	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;

1145
	return sizeof(struct fc_scr_s);
1146 1147 1148
}

u16
K
Krishna Gudipati 已提交
1149 1150
fc_rscn_build(struct fchs_s *fchs, struct fc_rscn_pl_s *rscn,
		u32 s_id, u16 ox_id)
1151
{
M
Maggie Zhang 已提交
1152
	u32        d_id = bfa_hton3b(FC_FABRIC_CONTROLLER);
1153 1154 1155 1156 1157 1158 1159
	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;
1160
	rscn->payldlen = cpu_to_be16(payldlen);
1161 1162 1163 1164

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

1165
	return sizeof(struct fc_rscn_pl_s);
1166 1167 1168 1169
}

u16
fc_rftid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
K
Krishna Gudipati 已提交
1170
	       enum bfa_lport_role roles)
1171
{
K
Krishna Gudipati 已提交
1172 1173
	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 已提交
1174
	u32        type_value, d_id = bfa_hton3b(FC_NAME_SERVER);
1175 1176 1177 1178 1179
	u8         index;

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

1180
	memset(rftid, 0, sizeof(struct fcgs_rftid_req_s));
1181 1182 1183 1184 1185 1186

	rftid->dap = s_id;

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

1189
	return sizeof(struct fcgs_rftid_req_s) + sizeof(struct ct_hdr_s);
1190 1191 1192
}

u16
K
Krishna Gudipati 已提交
1193 1194
fc_rftid_build_sol(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
		   u8 *fc4_bitmap, u32 bitmap_size)
1195
{
K
Krishna Gudipati 已提交
1196 1197
	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 已提交
1198
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1199 1200 1201 1202

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

1203
	memset(rftid, 0, sizeof(struct fcgs_rftid_req_s));
1204 1205

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

1209
	return sizeof(struct fcgs_rftid_req_s) + sizeof(struct ct_hdr_s);
1210 1211 1212 1213 1214 1215
}

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 已提交
1216 1217
	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 已提交
1218
	u32         d_id = bfa_hton3b(FC_NAME_SERVER);
1219 1220 1221 1222

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

1223
	memset(rffid, 0, sizeof(struct fcgs_rffid_req_s));
1224

K
Krishna Gudipati 已提交
1225
	rffid->dap	    = s_id;
1226
	rffid->fc4ftr_bits  = fc4_ftrs;
K
Krishna Gudipati 已提交
1227
	rffid->fc4_type	    = fc4_type;
1228

1229
	return sizeof(struct fcgs_rffid_req_s) + sizeof(struct ct_hdr_s);
1230 1231 1232 1233 1234 1235 1236
}

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

K
Krishna Gudipati 已提交
1237
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1238
	struct fcgs_rspnid_req_s *rspnid =
K
Krishna Gudipati 已提交
1239
			(struct fcgs_rspnid_req_s *)(cthdr + 1);
M
Maggie Zhang 已提交
1240
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1241 1242 1243 1244

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

1245
	memset(rspnid, 0, sizeof(struct fcgs_rspnid_req_s));
1246 1247 1248 1249 1250

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

1251
	return sizeof(struct fcgs_rspnid_req_s) + sizeof(struct ct_hdr_s);
1252 1253 1254
}

u16
K
Krishna Gudipati 已提交
1255
fc_gid_ft_build(struct fchs_s *fchs, void *pyld, u32 s_id, u8 fc4_type)
1256 1257
{

K
Krishna Gudipati 已提交
1258 1259
	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 已提交
1260
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1261 1262 1263 1264 1265

	fc_gs_fchdr_build(fchs, d_id, s_id, 0);

	fc_gs_cthdr_build(cthdr, s_id, GS_GID_FT);

1266
	memset(gidft, 0, sizeof(struct fcgs_gidft_req_s));
1267 1268 1269 1270
	gidft->fc4_type = fc4_type;
	gidft->domain_id = 0;
	gidft->area_id = 0;

1271
	return sizeof(struct fcgs_gidft_req_s) + sizeof(struct ct_hdr_s);
1272 1273 1274 1275 1276 1277
}

u16
fc_rpnid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id,
	       wwn_t port_name)
{
K
Krishna Gudipati 已提交
1278 1279
	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 已提交
1280
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1281 1282 1283 1284

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

1285
	memset(rpnid, 0, sizeof(struct fcgs_rpnid_req_s));
1286 1287 1288
	rpnid->port_id = port_id;
	rpnid->port_name = port_name;

1289
	return sizeof(struct fcgs_rpnid_req_s) + sizeof(struct ct_hdr_s);
1290 1291 1292 1293 1294 1295
}

u16
fc_rnnid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id,
	       wwn_t node_name)
{
K
Krishna Gudipati 已提交
1296 1297
	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 已提交
1298
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1299 1300 1301 1302

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

1303
	memset(rnnid, 0, sizeof(struct fcgs_rnnid_req_s));
1304 1305 1306
	rnnid->port_id = port_id;
	rnnid->node_name = node_name;

1307
	return sizeof(struct fcgs_rnnid_req_s) + sizeof(struct ct_hdr_s);
1308 1309 1310 1311 1312 1313
}

u16
fc_rcsid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id,
	       u32 cos)
{
K
Krishna Gudipati 已提交
1314
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1315 1316
	struct fcgs_rcsid_req_s *rcsid =
			(struct fcgs_rcsid_req_s *) (cthdr + 1);
M
Maggie Zhang 已提交
1317
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1318 1319 1320 1321

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

1322
	memset(rcsid, 0, sizeof(struct fcgs_rcsid_req_s));
1323 1324 1325
	rcsid->port_id = port_id;
	rcsid->cos = cos;

1326
	return sizeof(struct fcgs_rcsid_req_s) + sizeof(struct ct_hdr_s);
1327 1328 1329 1330 1331 1332
}

u16
fc_rptid_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id,
	       u8 port_type)
{
K
Krishna Gudipati 已提交
1333 1334
	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 已提交
1335
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1336 1337 1338 1339

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

1340
	memset(rptid, 0, sizeof(struct fcgs_rptid_req_s));
1341 1342 1343
	rptid->port_id = port_id;
	rptid->port_type = port_type;

1344
	return sizeof(struct fcgs_rptid_req_s) + sizeof(struct ct_hdr_s);
1345 1346 1347 1348 1349
}

u16
fc_ganxt_build(struct fchs_s *fchs, void *pyld, u32 s_id, u32 port_id)
{
K
Krishna Gudipati 已提交
1350 1351
	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 已提交
1352
	u32        d_id = bfa_hton3b(FC_NAME_SERVER);
1353 1354 1355 1356

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

1357
	memset(ganxt, 0, sizeof(struct fcgs_ganxt_req_s));
1358 1359
	ganxt->port_id = port_id;

1360
	return sizeof(struct ct_hdr_s) + sizeof(struct fcgs_ganxt_req_s);
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
}

/*
 * 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 已提交
1371
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
M
Maggie Zhang 已提交
1372
	u32        d_id = bfa_hton3b(FC_MGMT_SERVER);
1373 1374 1375 1376

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

1377
	return sizeof(struct ct_hdr_s);
1378 1379 1380 1381 1382 1383 1384 1385 1386
}

/*
 * Given a FC4 Type, this function returns a fc4 type bitmask
 */
void
fc_get_fc4type_bitmask(u8 fc4_type, u8 *bit_mask)
{
	u8         index;
1387
	__be32       *ptr = (__be32 *) bit_mask;
1388 1389 1390 1391 1392 1393 1394 1395
	u32        type_value;

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

	index = fc4_type >> 5;
	type_value = 1 << (fc4_type % 32);
1396
	ptr[index] = cpu_to_be32(type_value);
1397 1398 1399 1400

}

/*
K
Krishna Gudipati 已提交
1401
 *	GMAL Request
1402 1403 1404 1405
 */
u16
fc_gmal_req_build(struct fchs_s *fchs, void *pyld, u32 s_id, wwn_t wwn)
{
K
Krishna Gudipati 已提交
1406
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1407
	fcgs_gmal_req_t *gmal = (fcgs_gmal_req_t *) (cthdr + 1);
M
Maggie Zhang 已提交
1408
	u32        d_id = bfa_hton3b(FC_MGMT_SERVER);
1409 1410 1411 1412 1413

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

1414
	memset(gmal, 0, sizeof(fcgs_gmal_req_t));
1415 1416
	gmal->wwn = wwn;

1417
	return sizeof(struct ct_hdr_s) + sizeof(fcgs_gmal_req_t);
1418 1419 1420 1421 1422 1423 1424 1425
}

/*
 * 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 已提交
1426
	struct ct_hdr_s *cthdr = (struct ct_hdr_s *) pyld;
1427
	fcgs_gfn_req_t *gfn = (fcgs_gfn_req_t *) (cthdr + 1);
M
Maggie Zhang 已提交
1428
	u32        d_id = bfa_hton3b(FC_MGMT_SERVER);
1429 1430 1431 1432 1433

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

1434
	memset(gfn, 0, sizeof(fcgs_gfn_req_t));
1435 1436
	gfn->wwn = wwn;

1437
	return sizeof(struct ct_hdr_s) + sizeof(fcgs_gfn_req_t);
1438
}