xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_gs.c (revision 6d99a79c)
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 #include "qla_target.h"
9 #include <linux/utsname.h>
10 
11 static int qla2x00_sns_ga_nxt(scsi_qla_host_t *, fc_port_t *);
12 static int qla2x00_sns_gid_pt(scsi_qla_host_t *, sw_info_t *);
13 static int qla2x00_sns_gpn_id(scsi_qla_host_t *, sw_info_t *);
14 static int qla2x00_sns_gnn_id(scsi_qla_host_t *, sw_info_t *);
15 static int qla2x00_sns_rft_id(scsi_qla_host_t *);
16 static int qla2x00_sns_rnn_id(scsi_qla_host_t *);
17 static int qla_async_rftid(scsi_qla_host_t *, port_id_t *);
18 static int qla_async_rffid(scsi_qla_host_t *, port_id_t *, u8, u8);
19 static int qla_async_rnnid(scsi_qla_host_t *, port_id_t *, u8*);
20 static int qla_async_rsnn_nn(scsi_qla_host_t *);
21 
22 /**
23  * qla2x00_prep_ms_iocb() - Prepare common MS/CT IOCB fields for SNS CT query.
24  * @vha: HA context
25  * @arg: CT arguments
26  *
27  * Returns a pointer to the @vha's ms_iocb.
28  */
29 void *
30 qla2x00_prep_ms_iocb(scsi_qla_host_t *vha, struct ct_arg *arg)
31 {
32 	struct qla_hw_data *ha = vha->hw;
33 	ms_iocb_entry_t *ms_pkt;
34 
35 	ms_pkt = (ms_iocb_entry_t *)arg->iocb;
36 	memset(ms_pkt, 0, sizeof(ms_iocb_entry_t));
37 
38 	ms_pkt->entry_type = MS_IOCB_TYPE;
39 	ms_pkt->entry_count = 1;
40 	SET_TARGET_ID(ha, ms_pkt->loop_id, SIMPLE_NAME_SERVER);
41 	ms_pkt->control_flags = cpu_to_le16(CF_READ | CF_HEAD_TAG);
42 	ms_pkt->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
43 	ms_pkt->cmd_dsd_count = cpu_to_le16(1);
44 	ms_pkt->total_dsd_count = cpu_to_le16(2);
45 	ms_pkt->rsp_bytecount = cpu_to_le32(arg->rsp_size);
46 	ms_pkt->req_bytecount = cpu_to_le32(arg->req_size);
47 
48 	ms_pkt->dseg_req_address[0] = cpu_to_le32(LSD(arg->req_dma));
49 	ms_pkt->dseg_req_address[1] = cpu_to_le32(MSD(arg->req_dma));
50 	ms_pkt->dseg_req_length = ms_pkt->req_bytecount;
51 
52 	ms_pkt->dseg_rsp_address[0] = cpu_to_le32(LSD(arg->rsp_dma));
53 	ms_pkt->dseg_rsp_address[1] = cpu_to_le32(MSD(arg->rsp_dma));
54 	ms_pkt->dseg_rsp_length = ms_pkt->rsp_bytecount;
55 
56 	vha->qla_stats.control_requests++;
57 
58 	return (ms_pkt);
59 }
60 
61 /**
62  * qla24xx_prep_ms_iocb() - Prepare common CT IOCB fields for SNS CT query.
63  * @vha: HA context
64  * @arg: CT arguments
65  *
66  * Returns a pointer to the @ha's ms_iocb.
67  */
68 void *
69 qla24xx_prep_ms_iocb(scsi_qla_host_t *vha, struct ct_arg *arg)
70 {
71 	struct qla_hw_data *ha = vha->hw;
72 	struct ct_entry_24xx *ct_pkt;
73 
74 	ct_pkt = (struct ct_entry_24xx *)arg->iocb;
75 	memset(ct_pkt, 0, sizeof(struct ct_entry_24xx));
76 
77 	ct_pkt->entry_type = CT_IOCB_TYPE;
78 	ct_pkt->entry_count = 1;
79 	ct_pkt->nport_handle = cpu_to_le16(arg->nport_handle);
80 	ct_pkt->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
81 	ct_pkt->cmd_dsd_count = cpu_to_le16(1);
82 	ct_pkt->rsp_dsd_count = cpu_to_le16(1);
83 	ct_pkt->rsp_byte_count = cpu_to_le32(arg->rsp_size);
84 	ct_pkt->cmd_byte_count = cpu_to_le32(arg->req_size);
85 
86 	ct_pkt->dseg_0_address[0] = cpu_to_le32(LSD(arg->req_dma));
87 	ct_pkt->dseg_0_address[1] = cpu_to_le32(MSD(arg->req_dma));
88 	ct_pkt->dseg_0_len = ct_pkt->cmd_byte_count;
89 
90 	ct_pkt->dseg_1_address[0] = cpu_to_le32(LSD(arg->rsp_dma));
91 	ct_pkt->dseg_1_address[1] = cpu_to_le32(MSD(arg->rsp_dma));
92 	ct_pkt->dseg_1_len = ct_pkt->rsp_byte_count;
93 	ct_pkt->vp_index = vha->vp_idx;
94 
95 	vha->qla_stats.control_requests++;
96 
97 	return (ct_pkt);
98 }
99 
100 /**
101  * qla2x00_prep_ct_req() - Prepare common CT request fields for SNS query.
102  * @p: CT request buffer
103  * @cmd: GS command
104  * @rsp_size: response size in bytes
105  *
106  * Returns a pointer to the intitialized @ct_req.
107  */
108 static inline struct ct_sns_req *
109 qla2x00_prep_ct_req(struct ct_sns_pkt *p, uint16_t cmd, uint16_t rsp_size)
110 {
111 	memset(p, 0, sizeof(struct ct_sns_pkt));
112 
113 	p->p.req.header.revision = 0x01;
114 	p->p.req.header.gs_type = 0xFC;
115 	p->p.req.header.gs_subtype = 0x02;
116 	p->p.req.command = cpu_to_be16(cmd);
117 	p->p.req.max_rsp_size = cpu_to_be16((rsp_size - 16) / 4);
118 
119 	return &p->p.req;
120 }
121 
122 int
123 qla2x00_chk_ms_status(scsi_qla_host_t *vha, ms_iocb_entry_t *ms_pkt,
124     struct ct_sns_rsp *ct_rsp, const char *routine)
125 {
126 	int rval;
127 	uint16_t comp_status;
128 	struct qla_hw_data *ha = vha->hw;
129 	bool lid_is_sns = false;
130 
131 	rval = QLA_FUNCTION_FAILED;
132 	if (ms_pkt->entry_status != 0) {
133 		ql_dbg(ql_dbg_disc, vha, 0x2031,
134 		    "%s failed, error status (%x) on port_id: %02x%02x%02x.\n",
135 		    routine, ms_pkt->entry_status, vha->d_id.b.domain,
136 		    vha->d_id.b.area, vha->d_id.b.al_pa);
137 	} else {
138 		if (IS_FWI2_CAPABLE(ha))
139 			comp_status = le16_to_cpu(
140 			    ((struct ct_entry_24xx *)ms_pkt)->comp_status);
141 		else
142 			comp_status = le16_to_cpu(ms_pkt->status);
143 		switch (comp_status) {
144 		case CS_COMPLETE:
145 		case CS_DATA_UNDERRUN:
146 		case CS_DATA_OVERRUN:		/* Overrun? */
147 			if (ct_rsp->header.response !=
148 			    cpu_to_be16(CT_ACCEPT_RESPONSE)) {
149 				ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x2077,
150 				    "%s failed rejected request on port_id: %02x%02x%02x Completion status 0x%x, response 0x%x\n",
151 				    routine, vha->d_id.b.domain,
152 				    vha->d_id.b.area, vha->d_id.b.al_pa,
153 				    comp_status, ct_rsp->header.response);
154 				ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha,
155 				    0x2078, (uint8_t *)&ct_rsp->header,
156 				    sizeof(struct ct_rsp_hdr));
157 				rval = QLA_INVALID_COMMAND;
158 			} else
159 				rval = QLA_SUCCESS;
160 			break;
161 		case CS_PORT_LOGGED_OUT:
162 			if (IS_FWI2_CAPABLE(ha)) {
163 				if (le16_to_cpu(ms_pkt->loop_id.extended) ==
164 				    NPH_SNS)
165 					lid_is_sns = true;
166 			} else {
167 				if (le16_to_cpu(ms_pkt->loop_id.extended) ==
168 				    SIMPLE_NAME_SERVER)
169 					lid_is_sns = true;
170 			}
171 			if (lid_is_sns) {
172 				ql_dbg(ql_dbg_async, vha, 0x502b,
173 					"%s failed, Name server has logged out",
174 					routine);
175 				rval = QLA_NOT_LOGGED_IN;
176 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
177 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
178 			}
179 			break;
180 		case CS_TIMEOUT:
181 			rval = QLA_FUNCTION_TIMEOUT;
182 			/* fall through */
183 		default:
184 			ql_dbg(ql_dbg_disc, vha, 0x2033,
185 			    "%s failed, completion status (%x) on port_id: "
186 			    "%02x%02x%02x.\n", routine, comp_status,
187 			    vha->d_id.b.domain, vha->d_id.b.area,
188 			    vha->d_id.b.al_pa);
189 			break;
190 		}
191 	}
192 	return rval;
193 }
194 
195 /**
196  * qla2x00_ga_nxt() - SNS scan for fabric devices via GA_NXT command.
197  * @vha: HA context
198  * @fcport: fcport entry to updated
199  *
200  * Returns 0 on success.
201  */
202 int
203 qla2x00_ga_nxt(scsi_qla_host_t *vha, fc_port_t *fcport)
204 {
205 	int		rval;
206 
207 	ms_iocb_entry_t	*ms_pkt;
208 	struct ct_sns_req	*ct_req;
209 	struct ct_sns_rsp	*ct_rsp;
210 	struct qla_hw_data *ha = vha->hw;
211 	struct ct_arg arg;
212 
213 	if (IS_QLA2100(ha) || IS_QLA2200(ha))
214 		return qla2x00_sns_ga_nxt(vha, fcport);
215 
216 	arg.iocb = ha->ms_iocb;
217 	arg.req_dma = ha->ct_sns_dma;
218 	arg.rsp_dma = ha->ct_sns_dma;
219 	arg.req_size = GA_NXT_REQ_SIZE;
220 	arg.rsp_size = GA_NXT_RSP_SIZE;
221 	arg.nport_handle = NPH_SNS;
222 
223 	/* Issue GA_NXT */
224 	/* Prepare common MS IOCB */
225 	ms_pkt = ha->isp_ops->prep_ms_iocb(vha, &arg);
226 
227 	/* Prepare CT request */
228 	ct_req = qla2x00_prep_ct_req(ha->ct_sns, GA_NXT_CMD,
229 	    GA_NXT_RSP_SIZE);
230 	ct_rsp = &ha->ct_sns->p.rsp;
231 
232 	/* Prepare CT arguments -- port_id */
233 	ct_req->req.port_id.port_id[0] = fcport->d_id.b.domain;
234 	ct_req->req.port_id.port_id[1] = fcport->d_id.b.area;
235 	ct_req->req.port_id.port_id[2] = fcport->d_id.b.al_pa;
236 
237 	/* Execute MS IOCB */
238 	rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
239 	    sizeof(ms_iocb_entry_t));
240 	if (rval != QLA_SUCCESS) {
241 		/*EMPTY*/
242 		ql_dbg(ql_dbg_disc, vha, 0x2062,
243 		    "GA_NXT issue IOCB failed (%d).\n", rval);
244 	} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp, "GA_NXT") !=
245 	    QLA_SUCCESS) {
246 		rval = QLA_FUNCTION_FAILED;
247 	} else {
248 		/* Populate fc_port_t entry. */
249 		fcport->d_id.b.domain = ct_rsp->rsp.ga_nxt.port_id[0];
250 		fcport->d_id.b.area = ct_rsp->rsp.ga_nxt.port_id[1];
251 		fcport->d_id.b.al_pa = ct_rsp->rsp.ga_nxt.port_id[2];
252 
253 		memcpy(fcport->node_name, ct_rsp->rsp.ga_nxt.node_name,
254 		    WWN_SIZE);
255 		memcpy(fcport->port_name, ct_rsp->rsp.ga_nxt.port_name,
256 		    WWN_SIZE);
257 
258 		fcport->fc4_type = (ct_rsp->rsp.ga_nxt.fc4_types[2] & BIT_0) ?
259 		    FC4_TYPE_FCP_SCSI : FC4_TYPE_OTHER;
260 
261 		if (ct_rsp->rsp.ga_nxt.port_type != NS_N_PORT_TYPE &&
262 		    ct_rsp->rsp.ga_nxt.port_type != NS_NL_PORT_TYPE)
263 			fcport->d_id.b.domain = 0xf0;
264 
265 		ql_dbg(ql_dbg_disc, vha, 0x2063,
266 		    "GA_NXT entry - nn %8phN pn %8phN "
267 		    "port_id=%02x%02x%02x.\n",
268 		    fcport->node_name, fcport->port_name,
269 		    fcport->d_id.b.domain, fcport->d_id.b.area,
270 		    fcport->d_id.b.al_pa);
271 	}
272 
273 	return (rval);
274 }
275 
276 static inline int
277 qla2x00_gid_pt_rsp_size(scsi_qla_host_t *vha)
278 {
279 	return vha->hw->max_fibre_devices * 4 + 16;
280 }
281 
282 /**
283  * qla2x00_gid_pt() - SNS scan for fabric devices via GID_PT command.
284  * @vha: HA context
285  * @list: switch info entries to populate
286  *
287  * NOTE: Non-Nx_Ports are not requested.
288  *
289  * Returns 0 on success.
290  */
291 int
292 qla2x00_gid_pt(scsi_qla_host_t *vha, sw_info_t *list)
293 {
294 	int		rval;
295 	uint16_t	i;
296 
297 	ms_iocb_entry_t	*ms_pkt;
298 	struct ct_sns_req	*ct_req;
299 	struct ct_sns_rsp	*ct_rsp;
300 
301 	struct ct_sns_gid_pt_data *gid_data;
302 	struct qla_hw_data *ha = vha->hw;
303 	uint16_t gid_pt_rsp_size;
304 	struct ct_arg arg;
305 
306 	if (IS_QLA2100(ha) || IS_QLA2200(ha))
307 		return qla2x00_sns_gid_pt(vha, list);
308 
309 	gid_data = NULL;
310 	gid_pt_rsp_size = qla2x00_gid_pt_rsp_size(vha);
311 
312 	arg.iocb = ha->ms_iocb;
313 	arg.req_dma = ha->ct_sns_dma;
314 	arg.rsp_dma = ha->ct_sns_dma;
315 	arg.req_size = GID_PT_REQ_SIZE;
316 	arg.rsp_size = gid_pt_rsp_size;
317 	arg.nport_handle = NPH_SNS;
318 
319 	/* Issue GID_PT */
320 	/* Prepare common MS IOCB */
321 	ms_pkt = ha->isp_ops->prep_ms_iocb(vha, &arg);
322 
323 	/* Prepare CT request */
324 	ct_req = qla2x00_prep_ct_req(ha->ct_sns, GID_PT_CMD, gid_pt_rsp_size);
325 	ct_rsp = &ha->ct_sns->p.rsp;
326 
327 	/* Prepare CT arguments -- port_type */
328 	ct_req->req.gid_pt.port_type = NS_NX_PORT_TYPE;
329 
330 	/* Execute MS IOCB */
331 	rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
332 	    sizeof(ms_iocb_entry_t));
333 	if (rval != QLA_SUCCESS) {
334 		/*EMPTY*/
335 		ql_dbg(ql_dbg_disc, vha, 0x2055,
336 		    "GID_PT issue IOCB failed (%d).\n", rval);
337 	} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp, "GID_PT") !=
338 	    QLA_SUCCESS) {
339 		rval = QLA_FUNCTION_FAILED;
340 	} else {
341 		/* Set port IDs in switch info list. */
342 		for (i = 0; i < ha->max_fibre_devices; i++) {
343 			gid_data = &ct_rsp->rsp.gid_pt.entries[i];
344 			list[i].d_id.b.domain = gid_data->port_id[0];
345 			list[i].d_id.b.area = gid_data->port_id[1];
346 			list[i].d_id.b.al_pa = gid_data->port_id[2];
347 			memset(list[i].fabric_port_name, 0, WWN_SIZE);
348 			list[i].fp_speed = PORT_SPEED_UNKNOWN;
349 
350 			/* Last one exit. */
351 			if (gid_data->control_byte & BIT_7) {
352 				list[i].d_id.b.rsvd_1 = gid_data->control_byte;
353 				break;
354 			}
355 		}
356 
357 		/*
358 		 * If we've used all available slots, then the switch is
359 		 * reporting back more devices than we can handle with this
360 		 * single call.  Return a failed status, and let GA_NXT handle
361 		 * the overload.
362 		 */
363 		if (i == ha->max_fibre_devices)
364 			rval = QLA_FUNCTION_FAILED;
365 	}
366 
367 	return (rval);
368 }
369 
370 /**
371  * qla2x00_gpn_id() - SNS Get Port Name (GPN_ID) query.
372  * @vha: HA context
373  * @list: switch info entries to populate
374  *
375  * Returns 0 on success.
376  */
377 int
378 qla2x00_gpn_id(scsi_qla_host_t *vha, sw_info_t *list)
379 {
380 	int		rval = QLA_SUCCESS;
381 	uint16_t	i;
382 
383 	ms_iocb_entry_t	*ms_pkt;
384 	struct ct_sns_req	*ct_req;
385 	struct ct_sns_rsp	*ct_rsp;
386 	struct qla_hw_data *ha = vha->hw;
387 	struct ct_arg arg;
388 
389 	if (IS_QLA2100(ha) || IS_QLA2200(ha))
390 		return qla2x00_sns_gpn_id(vha, list);
391 
392 	arg.iocb = ha->ms_iocb;
393 	arg.req_dma = ha->ct_sns_dma;
394 	arg.rsp_dma = ha->ct_sns_dma;
395 	arg.req_size = GPN_ID_REQ_SIZE;
396 	arg.rsp_size = GPN_ID_RSP_SIZE;
397 	arg.nport_handle = NPH_SNS;
398 
399 	for (i = 0; i < ha->max_fibre_devices; i++) {
400 		/* Issue GPN_ID */
401 		/* Prepare common MS IOCB */
402 		ms_pkt = ha->isp_ops->prep_ms_iocb(vha, &arg);
403 
404 		/* Prepare CT request */
405 		ct_req = qla2x00_prep_ct_req(ha->ct_sns, GPN_ID_CMD,
406 		    GPN_ID_RSP_SIZE);
407 		ct_rsp = &ha->ct_sns->p.rsp;
408 
409 		/* Prepare CT arguments -- port_id */
410 		ct_req->req.port_id.port_id[0] = list[i].d_id.b.domain;
411 		ct_req->req.port_id.port_id[1] = list[i].d_id.b.area;
412 		ct_req->req.port_id.port_id[2] = list[i].d_id.b.al_pa;
413 
414 		/* Execute MS IOCB */
415 		rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
416 		    sizeof(ms_iocb_entry_t));
417 		if (rval != QLA_SUCCESS) {
418 			/*EMPTY*/
419 			ql_dbg(ql_dbg_disc, vha, 0x2056,
420 			    "GPN_ID issue IOCB failed (%d).\n", rval);
421 			break;
422 		} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp,
423 		    "GPN_ID") != QLA_SUCCESS) {
424 			rval = QLA_FUNCTION_FAILED;
425 			break;
426 		} else {
427 			/* Save portname */
428 			memcpy(list[i].port_name,
429 			    ct_rsp->rsp.gpn_id.port_name, WWN_SIZE);
430 		}
431 
432 		/* Last device exit. */
433 		if (list[i].d_id.b.rsvd_1 != 0)
434 			break;
435 	}
436 
437 	return (rval);
438 }
439 
440 /**
441  * qla2x00_gnn_id() - SNS Get Node Name (GNN_ID) query.
442  * @vha: HA context
443  * @list: switch info entries to populate
444  *
445  * Returns 0 on success.
446  */
447 int
448 qla2x00_gnn_id(scsi_qla_host_t *vha, sw_info_t *list)
449 {
450 	int		rval = QLA_SUCCESS;
451 	uint16_t	i;
452 	struct qla_hw_data *ha = vha->hw;
453 	ms_iocb_entry_t	*ms_pkt;
454 	struct ct_sns_req	*ct_req;
455 	struct ct_sns_rsp	*ct_rsp;
456 	struct ct_arg arg;
457 
458 	if (IS_QLA2100(ha) || IS_QLA2200(ha))
459 		return qla2x00_sns_gnn_id(vha, list);
460 
461 	arg.iocb = ha->ms_iocb;
462 	arg.req_dma = ha->ct_sns_dma;
463 	arg.rsp_dma = ha->ct_sns_dma;
464 	arg.req_size = GNN_ID_REQ_SIZE;
465 	arg.rsp_size = GNN_ID_RSP_SIZE;
466 	arg.nport_handle = NPH_SNS;
467 
468 	for (i = 0; i < ha->max_fibre_devices; i++) {
469 		/* Issue GNN_ID */
470 		/* Prepare common MS IOCB */
471 		ms_pkt = ha->isp_ops->prep_ms_iocb(vha, &arg);
472 
473 		/* Prepare CT request */
474 		ct_req = qla2x00_prep_ct_req(ha->ct_sns, GNN_ID_CMD,
475 		    GNN_ID_RSP_SIZE);
476 		ct_rsp = &ha->ct_sns->p.rsp;
477 
478 		/* Prepare CT arguments -- port_id */
479 		ct_req->req.port_id.port_id[0] = list[i].d_id.b.domain;
480 		ct_req->req.port_id.port_id[1] = list[i].d_id.b.area;
481 		ct_req->req.port_id.port_id[2] = list[i].d_id.b.al_pa;
482 
483 		/* Execute MS IOCB */
484 		rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
485 		    sizeof(ms_iocb_entry_t));
486 		if (rval != QLA_SUCCESS) {
487 			/*EMPTY*/
488 			ql_dbg(ql_dbg_disc, vha, 0x2057,
489 			    "GNN_ID issue IOCB failed (%d).\n", rval);
490 			break;
491 		} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp,
492 		    "GNN_ID") != QLA_SUCCESS) {
493 			rval = QLA_FUNCTION_FAILED;
494 			break;
495 		} else {
496 			/* Save nodename */
497 			memcpy(list[i].node_name,
498 			    ct_rsp->rsp.gnn_id.node_name, WWN_SIZE);
499 
500 			ql_dbg(ql_dbg_disc, vha, 0x2058,
501 			    "GID_PT entry - nn %8phN pn %8phN "
502 			    "portid=%02x%02x%02x.\n",
503 			    list[i].node_name, list[i].port_name,
504 			    list[i].d_id.b.domain, list[i].d_id.b.area,
505 			    list[i].d_id.b.al_pa);
506 		}
507 
508 		/* Last device exit. */
509 		if (list[i].d_id.b.rsvd_1 != 0)
510 			break;
511 	}
512 
513 	return (rval);
514 }
515 
516 static void qla2x00_async_sns_sp_done(void *s, int rc)
517 {
518 	struct srb *sp = s;
519 	struct scsi_qla_host *vha = sp->vha;
520 	struct ct_sns_pkt *ct_sns;
521 	struct qla_work_evt *e;
522 
523 	sp->rc = rc;
524 	if (rc == QLA_SUCCESS) {
525 		ql_dbg(ql_dbg_disc, vha, 0x204f,
526 		    "Async done-%s exiting normally.\n",
527 		    sp->name);
528 	} else if (rc == QLA_FUNCTION_TIMEOUT) {
529 		ql_dbg(ql_dbg_disc, vha, 0x204f,
530 		    "Async done-%s timeout\n", sp->name);
531 	} else {
532 		ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.rsp;
533 		memset(ct_sns, 0, sizeof(*ct_sns));
534 		sp->retry_count++;
535 		if (sp->retry_count > 3)
536 			goto err;
537 
538 		ql_dbg(ql_dbg_disc, vha, 0x204f,
539 		    "Async done-%s fail rc %x.  Retry count %d\n",
540 		    sp->name, rc, sp->retry_count);
541 
542 		e = qla2x00_alloc_work(vha, QLA_EVT_SP_RETRY);
543 		if (!e)
544 			goto err2;
545 
546 		del_timer(&sp->u.iocb_cmd.timer);
547 		e->u.iosb.sp = sp;
548 		qla2x00_post_work(vha, e);
549 		return;
550 	}
551 
552 err:
553 	e = qla2x00_alloc_work(vha, QLA_EVT_UNMAP);
554 err2:
555 	if (!e) {
556 		/* please ignore kernel warning. otherwise, we have mem leak. */
557 		if (sp->u.iocb_cmd.u.ctarg.req) {
558 			dma_free_coherent(&vha->hw->pdev->dev,
559 			    sp->u.iocb_cmd.u.ctarg.req_allocated_size,
560 			    sp->u.iocb_cmd.u.ctarg.req,
561 			    sp->u.iocb_cmd.u.ctarg.req_dma);
562 			sp->u.iocb_cmd.u.ctarg.req = NULL;
563 		}
564 
565 		if (sp->u.iocb_cmd.u.ctarg.rsp) {
566 			dma_free_coherent(&vha->hw->pdev->dev,
567 			    sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
568 			    sp->u.iocb_cmd.u.ctarg.rsp,
569 			    sp->u.iocb_cmd.u.ctarg.rsp_dma);
570 			sp->u.iocb_cmd.u.ctarg.rsp = NULL;
571 		}
572 
573 		sp->free(sp);
574 
575 		return;
576 	}
577 
578 	e->u.iosb.sp = sp;
579 	qla2x00_post_work(vha, e);
580 }
581 
582 /**
583  * qla2x00_rft_id() - SNS Register FC-4 TYPEs (RFT_ID) supported by the HBA.
584  * @vha: HA context
585  *
586  * Returns 0 on success.
587  */
588 int
589 qla2x00_rft_id(scsi_qla_host_t *vha)
590 {
591 	struct qla_hw_data *ha = vha->hw;
592 
593 	if (IS_QLA2100(ha) || IS_QLA2200(ha))
594 		return qla2x00_sns_rft_id(vha);
595 
596 	return qla_async_rftid(vha, &vha->d_id);
597 }
598 
599 static int qla_async_rftid(scsi_qla_host_t *vha, port_id_t *d_id)
600 {
601 	int rval = QLA_MEMORY_ALLOC_FAILED;
602 	struct ct_sns_req *ct_req;
603 	srb_t *sp;
604 	struct ct_sns_pkt *ct_sns;
605 
606 	if (!vha->flags.online)
607 		goto done;
608 
609 	sp = qla2x00_get_sp(vha, NULL, GFP_KERNEL);
610 	if (!sp)
611 		goto done;
612 
613 	sp->type = SRB_CT_PTHRU_CMD;
614 	sp->name = "rft_id";
615 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
616 
617 	sp->u.iocb_cmd.u.ctarg.req = dma_alloc_coherent(&vha->hw->pdev->dev,
618 	    sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.req_dma,
619 	    GFP_KERNEL);
620 	sp->u.iocb_cmd.u.ctarg.req_allocated_size = sizeof(struct ct_sns_pkt);
621 	if (!sp->u.iocb_cmd.u.ctarg.req) {
622 		ql_log(ql_log_warn, vha, 0xd041,
623 		    "%s: Failed to allocate ct_sns request.\n",
624 		    __func__);
625 		goto done_free_sp;
626 	}
627 
628 	sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
629 	    sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.rsp_dma,
630 	    GFP_KERNEL);
631 	sp->u.iocb_cmd.u.ctarg.rsp_allocated_size = sizeof(struct ct_sns_pkt);
632 	if (!sp->u.iocb_cmd.u.ctarg.rsp) {
633 		ql_log(ql_log_warn, vha, 0xd042,
634 		    "%s: Failed to allocate ct_sns request.\n",
635 		    __func__);
636 		goto done_free_sp;
637 	}
638 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.rsp;
639 	memset(ct_sns, 0, sizeof(*ct_sns));
640 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
641 
642 	/* Prepare CT request */
643 	ct_req = qla2x00_prep_ct_req(ct_sns, RFT_ID_CMD, RFT_ID_RSP_SIZE);
644 
645 	/* Prepare CT arguments -- port_id, FC-4 types */
646 	ct_req->req.rft_id.port_id[0] = vha->d_id.b.domain;
647 	ct_req->req.rft_id.port_id[1] = vha->d_id.b.area;
648 	ct_req->req.rft_id.port_id[2] = vha->d_id.b.al_pa;
649 	ct_req->req.rft_id.fc4_types[2] = 0x01;		/* FCP-3 */
650 
651 	if (vha->flags.nvme_enabled)
652 		ct_req->req.rft_id.fc4_types[6] = 1;    /* NVMe type 28h */
653 
654 	sp->u.iocb_cmd.u.ctarg.req_size = RFT_ID_REQ_SIZE;
655 	sp->u.iocb_cmd.u.ctarg.rsp_size = RFT_ID_RSP_SIZE;
656 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
657 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
658 	sp->done = qla2x00_async_sns_sp_done;
659 
660 	rval = qla2x00_start_sp(sp);
661 	if (rval != QLA_SUCCESS) {
662 		ql_dbg(ql_dbg_disc, vha, 0x2043,
663 		    "RFT_ID issue IOCB failed (%d).\n", rval);
664 		goto done_free_sp;
665 	}
666 	ql_dbg(ql_dbg_disc, vha, 0xffff,
667 	    "Async-%s - hdl=%x portid %06x.\n",
668 	    sp->name, sp->handle, d_id->b24);
669 	return rval;
670 done_free_sp:
671 	sp->free(sp);
672 done:
673 	return rval;
674 }
675 
676 /**
677  * qla2x00_rff_id() - SNS Register FC-4 Features (RFF_ID) supported by the HBA.
678  * @vha: HA context
679  * @type: not used
680  *
681  * Returns 0 on success.
682  */
683 int
684 qla2x00_rff_id(scsi_qla_host_t *vha, u8 type)
685 {
686 	struct qla_hw_data *ha = vha->hw;
687 
688 	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
689 		ql_dbg(ql_dbg_disc, vha, 0x2046,
690 		    "RFF_ID call not supported on ISP2100/ISP2200.\n");
691 		return (QLA_SUCCESS);
692 	}
693 
694 	return qla_async_rffid(vha, &vha->d_id, qlt_rff_id(vha),
695 	    FC4_TYPE_FCP_SCSI);
696 }
697 
698 static int qla_async_rffid(scsi_qla_host_t *vha, port_id_t *d_id,
699     u8 fc4feature, u8 fc4type)
700 {
701 	int rval = QLA_MEMORY_ALLOC_FAILED;
702 	struct ct_sns_req *ct_req;
703 	srb_t *sp;
704 	struct ct_sns_pkt *ct_sns;
705 
706 	sp = qla2x00_get_sp(vha, NULL, GFP_KERNEL);
707 	if (!sp)
708 		goto done;
709 
710 	sp->type = SRB_CT_PTHRU_CMD;
711 	sp->name = "rff_id";
712 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
713 
714 	sp->u.iocb_cmd.u.ctarg.req = dma_alloc_coherent(&vha->hw->pdev->dev,
715 	    sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.req_dma,
716 	    GFP_KERNEL);
717 	sp->u.iocb_cmd.u.ctarg.req_allocated_size = sizeof(struct ct_sns_pkt);
718 	if (!sp->u.iocb_cmd.u.ctarg.req) {
719 		ql_log(ql_log_warn, vha, 0xd041,
720 		    "%s: Failed to allocate ct_sns request.\n",
721 		    __func__);
722 		goto done_free_sp;
723 	}
724 
725 	sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
726 	    sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.rsp_dma,
727 	    GFP_KERNEL);
728 	sp->u.iocb_cmd.u.ctarg.rsp_allocated_size = sizeof(struct ct_sns_pkt);
729 	if (!sp->u.iocb_cmd.u.ctarg.rsp) {
730 		ql_log(ql_log_warn, vha, 0xd042,
731 		    "%s: Failed to allocate ct_sns request.\n",
732 		    __func__);
733 		goto done_free_sp;
734 	}
735 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.rsp;
736 	memset(ct_sns, 0, sizeof(*ct_sns));
737 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
738 
739 	/* Prepare CT request */
740 	ct_req = qla2x00_prep_ct_req(ct_sns, RFF_ID_CMD, RFF_ID_RSP_SIZE);
741 
742 	/* Prepare CT arguments -- port_id, FC-4 feature, FC-4 type */
743 	ct_req->req.rff_id.port_id[0] = d_id->b.domain;
744 	ct_req->req.rff_id.port_id[1] = d_id->b.area;
745 	ct_req->req.rff_id.port_id[2] = d_id->b.al_pa;
746 	ct_req->req.rff_id.fc4_feature = fc4feature;
747 	ct_req->req.rff_id.fc4_type = fc4type;		/* SCSI - FCP */
748 
749 	sp->u.iocb_cmd.u.ctarg.req_size = RFF_ID_REQ_SIZE;
750 	sp->u.iocb_cmd.u.ctarg.rsp_size = RFF_ID_RSP_SIZE;
751 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
752 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
753 	sp->done = qla2x00_async_sns_sp_done;
754 
755 	rval = qla2x00_start_sp(sp);
756 	if (rval != QLA_SUCCESS) {
757 		ql_dbg(ql_dbg_disc, vha, 0x2047,
758 		    "RFF_ID issue IOCB failed (%d).\n", rval);
759 		goto done_free_sp;
760 	}
761 
762 	ql_dbg(ql_dbg_disc, vha, 0xffff,
763 	    "Async-%s - hdl=%x portid %06x feature %x type %x.\n",
764 	    sp->name, sp->handle, d_id->b24, fc4feature, fc4type);
765 	return rval;
766 
767 done_free_sp:
768 	sp->free(sp);
769 done:
770 	return rval;
771 }
772 
773 /**
774  * qla2x00_rnn_id() - SNS Register Node Name (RNN_ID) of the HBA.
775  * @vha: HA context
776  *
777  * Returns 0 on success.
778  */
779 int
780 qla2x00_rnn_id(scsi_qla_host_t *vha)
781 {
782 	struct qla_hw_data *ha = vha->hw;
783 
784 	if (IS_QLA2100(ha) || IS_QLA2200(ha))
785 		return qla2x00_sns_rnn_id(vha);
786 
787 	return  qla_async_rnnid(vha, &vha->d_id, vha->node_name);
788 }
789 
790 static int qla_async_rnnid(scsi_qla_host_t *vha, port_id_t *d_id,
791 	u8 *node_name)
792 {
793 	int rval = QLA_MEMORY_ALLOC_FAILED;
794 	struct ct_sns_req *ct_req;
795 	srb_t *sp;
796 	struct ct_sns_pkt *ct_sns;
797 
798 	sp = qla2x00_get_sp(vha, NULL, GFP_KERNEL);
799 	if (!sp)
800 		goto done;
801 
802 	sp->type = SRB_CT_PTHRU_CMD;
803 	sp->name = "rnid";
804 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
805 
806 	sp->u.iocb_cmd.u.ctarg.req = dma_alloc_coherent(&vha->hw->pdev->dev,
807 	    sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.req_dma,
808 	    GFP_KERNEL);
809 	sp->u.iocb_cmd.u.ctarg.req_allocated_size = sizeof(struct ct_sns_pkt);
810 	if (!sp->u.iocb_cmd.u.ctarg.req) {
811 		ql_log(ql_log_warn, vha, 0xd041,
812 		    "%s: Failed to allocate ct_sns request.\n",
813 		    __func__);
814 		goto done_free_sp;
815 	}
816 
817 	sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
818 	    sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.rsp_dma,
819 	    GFP_KERNEL);
820 	sp->u.iocb_cmd.u.ctarg.rsp_allocated_size = sizeof(struct ct_sns_pkt);
821 	if (!sp->u.iocb_cmd.u.ctarg.rsp) {
822 		ql_log(ql_log_warn, vha, 0xd042,
823 		    "%s: Failed to allocate ct_sns request.\n",
824 		    __func__);
825 		goto done_free_sp;
826 	}
827 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.rsp;
828 	memset(ct_sns, 0, sizeof(*ct_sns));
829 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
830 
831 	/* Prepare CT request */
832 	ct_req = qla2x00_prep_ct_req(ct_sns, RNN_ID_CMD, RNN_ID_RSP_SIZE);
833 
834 	/* Prepare CT arguments -- port_id, node_name */
835 	ct_req->req.rnn_id.port_id[0] = vha->d_id.b.domain;
836 	ct_req->req.rnn_id.port_id[1] = vha->d_id.b.area;
837 	ct_req->req.rnn_id.port_id[2] = vha->d_id.b.al_pa;
838 	memcpy(ct_req->req.rnn_id.node_name, vha->node_name, WWN_SIZE);
839 
840 	sp->u.iocb_cmd.u.ctarg.req_size = RNN_ID_REQ_SIZE;
841 	sp->u.iocb_cmd.u.ctarg.rsp_size = RNN_ID_RSP_SIZE;
842 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
843 
844 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
845 	sp->done = qla2x00_async_sns_sp_done;
846 
847 	rval = qla2x00_start_sp(sp);
848 	if (rval != QLA_SUCCESS) {
849 		ql_dbg(ql_dbg_disc, vha, 0x204d,
850 		    "RNN_ID issue IOCB failed (%d).\n", rval);
851 		goto done_free_sp;
852 	}
853 	ql_dbg(ql_dbg_disc, vha, 0xffff,
854 	    "Async-%s - hdl=%x portid %06x\n",
855 	    sp->name, sp->handle, d_id->b24);
856 
857 	return rval;
858 
859 done_free_sp:
860 	sp->free(sp);
861 done:
862 	return rval;
863 }
864 
865 void
866 qla2x00_get_sym_node_name(scsi_qla_host_t *vha, uint8_t *snn, size_t size)
867 {
868 	struct qla_hw_data *ha = vha->hw;
869 
870 	if (IS_QLAFX00(ha))
871 		snprintf(snn, size, "%s FW:v%s DVR:v%s", ha->model_number,
872 		    ha->mr.fw_version, qla2x00_version_str);
873 	else
874 		snprintf(snn, size,
875 		    "%s FW:v%d.%02d.%02d DVR:v%s", ha->model_number,
876 		    ha->fw_major_version, ha->fw_minor_version,
877 		    ha->fw_subminor_version, qla2x00_version_str);
878 }
879 
880 /**
881  * qla2x00_rsnn_nn() - SNS Register Symbolic Node Name (RSNN_NN) of the HBA.
882  * @vha: HA context
883  *
884  * Returns 0 on success.
885  */
886 int
887 qla2x00_rsnn_nn(scsi_qla_host_t *vha)
888 {
889 	struct qla_hw_data *ha = vha->hw;
890 
891 	if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
892 		ql_dbg(ql_dbg_disc, vha, 0x2050,
893 		    "RSNN_ID call unsupported on ISP2100/ISP2200.\n");
894 		return (QLA_SUCCESS);
895 	}
896 
897 	return qla_async_rsnn_nn(vha);
898 }
899 
900 static int qla_async_rsnn_nn(scsi_qla_host_t *vha)
901 {
902 	int rval = QLA_MEMORY_ALLOC_FAILED;
903 	struct ct_sns_req *ct_req;
904 	srb_t *sp;
905 	struct ct_sns_pkt *ct_sns;
906 
907 	sp = qla2x00_get_sp(vha, NULL, GFP_KERNEL);
908 	if (!sp)
909 		goto done;
910 
911 	sp->type = SRB_CT_PTHRU_CMD;
912 	sp->name = "rsnn_nn";
913 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
914 
915 	sp->u.iocb_cmd.u.ctarg.req = dma_alloc_coherent(&vha->hw->pdev->dev,
916 	    sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.req_dma,
917 	    GFP_KERNEL);
918 	sp->u.iocb_cmd.u.ctarg.req_allocated_size = sizeof(struct ct_sns_pkt);
919 	if (!sp->u.iocb_cmd.u.ctarg.req) {
920 		ql_log(ql_log_warn, vha, 0xd041,
921 		    "%s: Failed to allocate ct_sns request.\n",
922 		    __func__);
923 		goto done_free_sp;
924 	}
925 
926 	sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
927 	    sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.rsp_dma,
928 	    GFP_KERNEL);
929 	sp->u.iocb_cmd.u.ctarg.rsp_allocated_size = sizeof(struct ct_sns_pkt);
930 	if (!sp->u.iocb_cmd.u.ctarg.rsp) {
931 		ql_log(ql_log_warn, vha, 0xd042,
932 		    "%s: Failed to allocate ct_sns request.\n",
933 		    __func__);
934 		goto done_free_sp;
935 	}
936 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.rsp;
937 	memset(ct_sns, 0, sizeof(*ct_sns));
938 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
939 
940 	/* Prepare CT request */
941 	ct_req = qla2x00_prep_ct_req(ct_sns, RSNN_NN_CMD, RSNN_NN_RSP_SIZE);
942 
943 	/* Prepare CT arguments -- node_name, symbolic node_name, size */
944 	memcpy(ct_req->req.rsnn_nn.node_name, vha->node_name, WWN_SIZE);
945 
946 	/* Prepare the Symbolic Node Name */
947 	qla2x00_get_sym_node_name(vha, ct_req->req.rsnn_nn.sym_node_name,
948 	    sizeof(ct_req->req.rsnn_nn.sym_node_name));
949 	ct_req->req.rsnn_nn.name_len =
950 	    (uint8_t)strlen(ct_req->req.rsnn_nn.sym_node_name);
951 
952 
953 	sp->u.iocb_cmd.u.ctarg.req_size = 24 + 1 + ct_req->req.rsnn_nn.name_len;
954 	sp->u.iocb_cmd.u.ctarg.rsp_size = RSNN_NN_RSP_SIZE;
955 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
956 
957 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
958 	sp->done = qla2x00_async_sns_sp_done;
959 
960 	rval = qla2x00_start_sp(sp);
961 	if (rval != QLA_SUCCESS) {
962 		ql_dbg(ql_dbg_disc, vha, 0x2043,
963 		    "RFT_ID issue IOCB failed (%d).\n", rval);
964 		goto done_free_sp;
965 	}
966 	ql_dbg(ql_dbg_disc, vha, 0xffff,
967 	    "Async-%s - hdl=%x.\n",
968 	    sp->name, sp->handle);
969 
970 	return rval;
971 
972 done_free_sp:
973 	sp->free(sp);
974 done:
975 	return rval;
976 }
977 
978 /**
979  * qla2x00_prep_sns_cmd() - Prepare common SNS command request fields for query.
980  * @vha: HA context
981  * @cmd: GS command
982  * @scmd_len: Subcommand length
983  * @data_size: response size in bytes
984  *
985  * Returns a pointer to the @ha's sns_cmd.
986  */
987 static inline struct sns_cmd_pkt *
988 qla2x00_prep_sns_cmd(scsi_qla_host_t *vha, uint16_t cmd, uint16_t scmd_len,
989     uint16_t data_size)
990 {
991 	uint16_t		wc;
992 	struct sns_cmd_pkt	*sns_cmd;
993 	struct qla_hw_data *ha = vha->hw;
994 
995 	sns_cmd = ha->sns_cmd;
996 	memset(sns_cmd, 0, sizeof(struct sns_cmd_pkt));
997 	wc = data_size / 2;			/* Size in 16bit words. */
998 	sns_cmd->p.cmd.buffer_length = cpu_to_le16(wc);
999 	sns_cmd->p.cmd.buffer_address[0] = cpu_to_le32(LSD(ha->sns_cmd_dma));
1000 	sns_cmd->p.cmd.buffer_address[1] = cpu_to_le32(MSD(ha->sns_cmd_dma));
1001 	sns_cmd->p.cmd.subcommand_length = cpu_to_le16(scmd_len);
1002 	sns_cmd->p.cmd.subcommand = cpu_to_le16(cmd);
1003 	wc = (data_size - 16) / 4;		/* Size in 32bit words. */
1004 	sns_cmd->p.cmd.size = cpu_to_le16(wc);
1005 
1006 	vha->qla_stats.control_requests++;
1007 
1008 	return (sns_cmd);
1009 }
1010 
1011 /**
1012  * qla2x00_sns_ga_nxt() - SNS scan for fabric devices via GA_NXT command.
1013  * @vha: HA context
1014  * @fcport: fcport entry to updated
1015  *
1016  * This command uses the old Exectute SNS Command mailbox routine.
1017  *
1018  * Returns 0 on success.
1019  */
1020 static int
1021 qla2x00_sns_ga_nxt(scsi_qla_host_t *vha, fc_port_t *fcport)
1022 {
1023 	int		rval = QLA_SUCCESS;
1024 	struct qla_hw_data *ha = vha->hw;
1025 	struct sns_cmd_pkt	*sns_cmd;
1026 
1027 	/* Issue GA_NXT. */
1028 	/* Prepare SNS command request. */
1029 	sns_cmd = qla2x00_prep_sns_cmd(vha, GA_NXT_CMD, GA_NXT_SNS_SCMD_LEN,
1030 	    GA_NXT_SNS_DATA_SIZE);
1031 
1032 	/* Prepare SNS command arguments -- port_id. */
1033 	sns_cmd->p.cmd.param[0] = fcport->d_id.b.al_pa;
1034 	sns_cmd->p.cmd.param[1] = fcport->d_id.b.area;
1035 	sns_cmd->p.cmd.param[2] = fcport->d_id.b.domain;
1036 
1037 	/* Execute SNS command. */
1038 	rval = qla2x00_send_sns(vha, ha->sns_cmd_dma, GA_NXT_SNS_CMD_SIZE / 2,
1039 	    sizeof(struct sns_cmd_pkt));
1040 	if (rval != QLA_SUCCESS) {
1041 		/*EMPTY*/
1042 		ql_dbg(ql_dbg_disc, vha, 0x205f,
1043 		    "GA_NXT Send SNS failed (%d).\n", rval);
1044 	} else if (sns_cmd->p.gan_data[8] != 0x80 ||
1045 	    sns_cmd->p.gan_data[9] != 0x02) {
1046 		ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x2084,
1047 		    "GA_NXT failed, rejected request ga_nxt_rsp:\n");
1048 		ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2074,
1049 		    sns_cmd->p.gan_data, 16);
1050 		rval = QLA_FUNCTION_FAILED;
1051 	} else {
1052 		/* Populate fc_port_t entry. */
1053 		fcport->d_id.b.domain = sns_cmd->p.gan_data[17];
1054 		fcport->d_id.b.area = sns_cmd->p.gan_data[18];
1055 		fcport->d_id.b.al_pa = sns_cmd->p.gan_data[19];
1056 
1057 		memcpy(fcport->node_name, &sns_cmd->p.gan_data[284], WWN_SIZE);
1058 		memcpy(fcport->port_name, &sns_cmd->p.gan_data[20], WWN_SIZE);
1059 
1060 		if (sns_cmd->p.gan_data[16] != NS_N_PORT_TYPE &&
1061 		    sns_cmd->p.gan_data[16] != NS_NL_PORT_TYPE)
1062 			fcport->d_id.b.domain = 0xf0;
1063 
1064 		ql_dbg(ql_dbg_disc, vha, 0x2061,
1065 		    "GA_NXT entry - nn %8phN pn %8phN "
1066 		    "port_id=%02x%02x%02x.\n",
1067 		    fcport->node_name, fcport->port_name,
1068 		    fcport->d_id.b.domain, fcport->d_id.b.area,
1069 		    fcport->d_id.b.al_pa);
1070 	}
1071 
1072 	return (rval);
1073 }
1074 
1075 /**
1076  * qla2x00_sns_gid_pt() - SNS scan for fabric devices via GID_PT command.
1077  * @vha: HA context
1078  * @list: switch info entries to populate
1079  *
1080  * This command uses the old Exectute SNS Command mailbox routine.
1081  *
1082  * NOTE: Non-Nx_Ports are not requested.
1083  *
1084  * Returns 0 on success.
1085  */
1086 static int
1087 qla2x00_sns_gid_pt(scsi_qla_host_t *vha, sw_info_t *list)
1088 {
1089 	int		rval;
1090 	struct qla_hw_data *ha = vha->hw;
1091 	uint16_t	i;
1092 	uint8_t		*entry;
1093 	struct sns_cmd_pkt	*sns_cmd;
1094 	uint16_t gid_pt_sns_data_size;
1095 
1096 	gid_pt_sns_data_size = qla2x00_gid_pt_rsp_size(vha);
1097 
1098 	/* Issue GID_PT. */
1099 	/* Prepare SNS command request. */
1100 	sns_cmd = qla2x00_prep_sns_cmd(vha, GID_PT_CMD, GID_PT_SNS_SCMD_LEN,
1101 	    gid_pt_sns_data_size);
1102 
1103 	/* Prepare SNS command arguments -- port_type. */
1104 	sns_cmd->p.cmd.param[0] = NS_NX_PORT_TYPE;
1105 
1106 	/* Execute SNS command. */
1107 	rval = qla2x00_send_sns(vha, ha->sns_cmd_dma, GID_PT_SNS_CMD_SIZE / 2,
1108 	    sizeof(struct sns_cmd_pkt));
1109 	if (rval != QLA_SUCCESS) {
1110 		/*EMPTY*/
1111 		ql_dbg(ql_dbg_disc, vha, 0x206d,
1112 		    "GID_PT Send SNS failed (%d).\n", rval);
1113 	} else if (sns_cmd->p.gid_data[8] != 0x80 ||
1114 	    sns_cmd->p.gid_data[9] != 0x02) {
1115 		ql_dbg(ql_dbg_disc, vha, 0x202f,
1116 		    "GID_PT failed, rejected request, gid_rsp:\n");
1117 		ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2081,
1118 		    sns_cmd->p.gid_data, 16);
1119 		rval = QLA_FUNCTION_FAILED;
1120 	} else {
1121 		/* Set port IDs in switch info list. */
1122 		for (i = 0; i < ha->max_fibre_devices; i++) {
1123 			entry = &sns_cmd->p.gid_data[(i * 4) + 16];
1124 			list[i].d_id.b.domain = entry[1];
1125 			list[i].d_id.b.area = entry[2];
1126 			list[i].d_id.b.al_pa = entry[3];
1127 
1128 			/* Last one exit. */
1129 			if (entry[0] & BIT_7) {
1130 				list[i].d_id.b.rsvd_1 = entry[0];
1131 				break;
1132 			}
1133 		}
1134 
1135 		/*
1136 		 * If we've used all available slots, then the switch is
1137 		 * reporting back more devices that we can handle with this
1138 		 * single call.  Return a failed status, and let GA_NXT handle
1139 		 * the overload.
1140 		 */
1141 		if (i == ha->max_fibre_devices)
1142 			rval = QLA_FUNCTION_FAILED;
1143 	}
1144 
1145 	return (rval);
1146 }
1147 
1148 /**
1149  * qla2x00_sns_gpn_id() - SNS Get Port Name (GPN_ID) query.
1150  * @vha: HA context
1151  * @list: switch info entries to populate
1152  *
1153  * This command uses the old Exectute SNS Command mailbox routine.
1154  *
1155  * Returns 0 on success.
1156  */
1157 static int
1158 qla2x00_sns_gpn_id(scsi_qla_host_t *vha, sw_info_t *list)
1159 {
1160 	int		rval = QLA_SUCCESS;
1161 	struct qla_hw_data *ha = vha->hw;
1162 	uint16_t	i;
1163 	struct sns_cmd_pkt	*sns_cmd;
1164 
1165 	for (i = 0; i < ha->max_fibre_devices; i++) {
1166 		/* Issue GPN_ID */
1167 		/* Prepare SNS command request. */
1168 		sns_cmd = qla2x00_prep_sns_cmd(vha, GPN_ID_CMD,
1169 		    GPN_ID_SNS_SCMD_LEN, GPN_ID_SNS_DATA_SIZE);
1170 
1171 		/* Prepare SNS command arguments -- port_id. */
1172 		sns_cmd->p.cmd.param[0] = list[i].d_id.b.al_pa;
1173 		sns_cmd->p.cmd.param[1] = list[i].d_id.b.area;
1174 		sns_cmd->p.cmd.param[2] = list[i].d_id.b.domain;
1175 
1176 		/* Execute SNS command. */
1177 		rval = qla2x00_send_sns(vha, ha->sns_cmd_dma,
1178 		    GPN_ID_SNS_CMD_SIZE / 2, sizeof(struct sns_cmd_pkt));
1179 		if (rval != QLA_SUCCESS) {
1180 			/*EMPTY*/
1181 			ql_dbg(ql_dbg_disc, vha, 0x2032,
1182 			    "GPN_ID Send SNS failed (%d).\n", rval);
1183 		} else if (sns_cmd->p.gpn_data[8] != 0x80 ||
1184 		    sns_cmd->p.gpn_data[9] != 0x02) {
1185 			ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x207e,
1186 			    "GPN_ID failed, rejected request, gpn_rsp:\n");
1187 			ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x207f,
1188 			    sns_cmd->p.gpn_data, 16);
1189 			rval = QLA_FUNCTION_FAILED;
1190 		} else {
1191 			/* Save portname */
1192 			memcpy(list[i].port_name, &sns_cmd->p.gpn_data[16],
1193 			    WWN_SIZE);
1194 		}
1195 
1196 		/* Last device exit. */
1197 		if (list[i].d_id.b.rsvd_1 != 0)
1198 			break;
1199 	}
1200 
1201 	return (rval);
1202 }
1203 
1204 /**
1205  * qla2x00_sns_gnn_id() - SNS Get Node Name (GNN_ID) query.
1206  * @vha: HA context
1207  * @list: switch info entries to populate
1208  *
1209  * This command uses the old Exectute SNS Command mailbox routine.
1210  *
1211  * Returns 0 on success.
1212  */
1213 static int
1214 qla2x00_sns_gnn_id(scsi_qla_host_t *vha, sw_info_t *list)
1215 {
1216 	int		rval = QLA_SUCCESS;
1217 	struct qla_hw_data *ha = vha->hw;
1218 	uint16_t	i;
1219 	struct sns_cmd_pkt	*sns_cmd;
1220 
1221 	for (i = 0; i < ha->max_fibre_devices; i++) {
1222 		/* Issue GNN_ID */
1223 		/* Prepare SNS command request. */
1224 		sns_cmd = qla2x00_prep_sns_cmd(vha, GNN_ID_CMD,
1225 		    GNN_ID_SNS_SCMD_LEN, GNN_ID_SNS_DATA_SIZE);
1226 
1227 		/* Prepare SNS command arguments -- port_id. */
1228 		sns_cmd->p.cmd.param[0] = list[i].d_id.b.al_pa;
1229 		sns_cmd->p.cmd.param[1] = list[i].d_id.b.area;
1230 		sns_cmd->p.cmd.param[2] = list[i].d_id.b.domain;
1231 
1232 		/* Execute SNS command. */
1233 		rval = qla2x00_send_sns(vha, ha->sns_cmd_dma,
1234 		    GNN_ID_SNS_CMD_SIZE / 2, sizeof(struct sns_cmd_pkt));
1235 		if (rval != QLA_SUCCESS) {
1236 			/*EMPTY*/
1237 			ql_dbg(ql_dbg_disc, vha, 0x203f,
1238 			    "GNN_ID Send SNS failed (%d).\n", rval);
1239 		} else if (sns_cmd->p.gnn_data[8] != 0x80 ||
1240 		    sns_cmd->p.gnn_data[9] != 0x02) {
1241 			ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x2082,
1242 			    "GNN_ID failed, rejected request, gnn_rsp:\n");
1243 			ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x207a,
1244 			    sns_cmd->p.gnn_data, 16);
1245 			rval = QLA_FUNCTION_FAILED;
1246 		} else {
1247 			/* Save nodename */
1248 			memcpy(list[i].node_name, &sns_cmd->p.gnn_data[16],
1249 			    WWN_SIZE);
1250 
1251 			ql_dbg(ql_dbg_disc, vha, 0x206e,
1252 			    "GID_PT entry - nn %8phN pn %8phN "
1253 			    "port_id=%02x%02x%02x.\n",
1254 			    list[i].node_name, list[i].port_name,
1255 			    list[i].d_id.b.domain, list[i].d_id.b.area,
1256 			    list[i].d_id.b.al_pa);
1257 		}
1258 
1259 		/* Last device exit. */
1260 		if (list[i].d_id.b.rsvd_1 != 0)
1261 			break;
1262 	}
1263 
1264 	return (rval);
1265 }
1266 
1267 /**
1268  * qla2x00_snd_rft_id() - SNS Register FC-4 TYPEs (RFT_ID) supported by the HBA.
1269  * @vha: HA context
1270  *
1271  * This command uses the old Exectute SNS Command mailbox routine.
1272  *
1273  * Returns 0 on success.
1274  */
1275 static int
1276 qla2x00_sns_rft_id(scsi_qla_host_t *vha)
1277 {
1278 	int		rval;
1279 	struct qla_hw_data *ha = vha->hw;
1280 	struct sns_cmd_pkt	*sns_cmd;
1281 
1282 	/* Issue RFT_ID. */
1283 	/* Prepare SNS command request. */
1284 	sns_cmd = qla2x00_prep_sns_cmd(vha, RFT_ID_CMD, RFT_ID_SNS_SCMD_LEN,
1285 	    RFT_ID_SNS_DATA_SIZE);
1286 
1287 	/* Prepare SNS command arguments -- port_id, FC-4 types */
1288 	sns_cmd->p.cmd.param[0] = vha->d_id.b.al_pa;
1289 	sns_cmd->p.cmd.param[1] = vha->d_id.b.area;
1290 	sns_cmd->p.cmd.param[2] = vha->d_id.b.domain;
1291 
1292 	sns_cmd->p.cmd.param[5] = 0x01;			/* FCP-3 */
1293 
1294 	/* Execute SNS command. */
1295 	rval = qla2x00_send_sns(vha, ha->sns_cmd_dma, RFT_ID_SNS_CMD_SIZE / 2,
1296 	    sizeof(struct sns_cmd_pkt));
1297 	if (rval != QLA_SUCCESS) {
1298 		/*EMPTY*/
1299 		ql_dbg(ql_dbg_disc, vha, 0x2060,
1300 		    "RFT_ID Send SNS failed (%d).\n", rval);
1301 	} else if (sns_cmd->p.rft_data[8] != 0x80 ||
1302 	    sns_cmd->p.rft_data[9] != 0x02) {
1303 		ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x2083,
1304 		    "RFT_ID failed, rejected request rft_rsp:\n");
1305 		ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2080,
1306 		    sns_cmd->p.rft_data, 16);
1307 		rval = QLA_FUNCTION_FAILED;
1308 	} else {
1309 		ql_dbg(ql_dbg_disc, vha, 0x2073,
1310 		    "RFT_ID exiting normally.\n");
1311 	}
1312 
1313 	return (rval);
1314 }
1315 
1316 /**
1317  * qla2x00_sns_rnn_id() - SNS Register Node Name (RNN_ID) of the HBA.
1318  * @vha: HA context
1319  *
1320  * This command uses the old Exectute SNS Command mailbox routine.
1321  *
1322  * Returns 0 on success.
1323  */
1324 static int
1325 qla2x00_sns_rnn_id(scsi_qla_host_t *vha)
1326 {
1327 	int		rval;
1328 	struct qla_hw_data *ha = vha->hw;
1329 	struct sns_cmd_pkt	*sns_cmd;
1330 
1331 	/* Issue RNN_ID. */
1332 	/* Prepare SNS command request. */
1333 	sns_cmd = qla2x00_prep_sns_cmd(vha, RNN_ID_CMD, RNN_ID_SNS_SCMD_LEN,
1334 	    RNN_ID_SNS_DATA_SIZE);
1335 
1336 	/* Prepare SNS command arguments -- port_id, nodename. */
1337 	sns_cmd->p.cmd.param[0] = vha->d_id.b.al_pa;
1338 	sns_cmd->p.cmd.param[1] = vha->d_id.b.area;
1339 	sns_cmd->p.cmd.param[2] = vha->d_id.b.domain;
1340 
1341 	sns_cmd->p.cmd.param[4] = vha->node_name[7];
1342 	sns_cmd->p.cmd.param[5] = vha->node_name[6];
1343 	sns_cmd->p.cmd.param[6] = vha->node_name[5];
1344 	sns_cmd->p.cmd.param[7] = vha->node_name[4];
1345 	sns_cmd->p.cmd.param[8] = vha->node_name[3];
1346 	sns_cmd->p.cmd.param[9] = vha->node_name[2];
1347 	sns_cmd->p.cmd.param[10] = vha->node_name[1];
1348 	sns_cmd->p.cmd.param[11] = vha->node_name[0];
1349 
1350 	/* Execute SNS command. */
1351 	rval = qla2x00_send_sns(vha, ha->sns_cmd_dma, RNN_ID_SNS_CMD_SIZE / 2,
1352 	    sizeof(struct sns_cmd_pkt));
1353 	if (rval != QLA_SUCCESS) {
1354 		/*EMPTY*/
1355 		ql_dbg(ql_dbg_disc, vha, 0x204a,
1356 		    "RNN_ID Send SNS failed (%d).\n", rval);
1357 	} else if (sns_cmd->p.rnn_data[8] != 0x80 ||
1358 	    sns_cmd->p.rnn_data[9] != 0x02) {
1359 		ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x207b,
1360 		    "RNN_ID failed, rejected request, rnn_rsp:\n");
1361 		ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x207c,
1362 		    sns_cmd->p.rnn_data, 16);
1363 		rval = QLA_FUNCTION_FAILED;
1364 	} else {
1365 		ql_dbg(ql_dbg_disc, vha, 0x204c,
1366 		    "RNN_ID exiting normally.\n");
1367 	}
1368 
1369 	return (rval);
1370 }
1371 
1372 /**
1373  * qla2x00_mgmt_svr_login() - Login to fabric Management Service.
1374  * @vha: HA context
1375  *
1376  * Returns 0 on success.
1377  */
1378 int
1379 qla2x00_mgmt_svr_login(scsi_qla_host_t *vha)
1380 {
1381 	int ret, rval;
1382 	uint16_t mb[MAILBOX_REGISTER_COUNT];
1383 	struct qla_hw_data *ha = vha->hw;
1384 	ret = QLA_SUCCESS;
1385 	if (vha->flags.management_server_logged_in)
1386 		return ret;
1387 
1388 	rval = ha->isp_ops->fabric_login(vha, vha->mgmt_svr_loop_id, 0xff, 0xff,
1389 	    0xfa, mb, BIT_1);
1390 	if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
1391 		if (rval == QLA_MEMORY_ALLOC_FAILED)
1392 			ql_dbg(ql_dbg_disc, vha, 0x2085,
1393 			    "Failed management_server login: loopid=%x "
1394 			    "rval=%d\n", vha->mgmt_svr_loop_id, rval);
1395 		else
1396 			ql_dbg(ql_dbg_disc, vha, 0x2024,
1397 			    "Failed management_server login: loopid=%x "
1398 			    "mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x.\n",
1399 			    vha->mgmt_svr_loop_id, mb[0], mb[1], mb[2], mb[6],
1400 			    mb[7]);
1401 		ret = QLA_FUNCTION_FAILED;
1402 	} else
1403 		vha->flags.management_server_logged_in = 1;
1404 
1405 	return ret;
1406 }
1407 
1408 /**
1409  * qla2x00_prep_ms_fdmi_iocb() - Prepare common MS IOCB fields for FDMI query.
1410  * @vha: HA context
1411  * @req_size: request size in bytes
1412  * @rsp_size: response size in bytes
1413  *
1414  * Returns a pointer to the @ha's ms_iocb.
1415  */
1416 void *
1417 qla2x00_prep_ms_fdmi_iocb(scsi_qla_host_t *vha, uint32_t req_size,
1418     uint32_t rsp_size)
1419 {
1420 	ms_iocb_entry_t *ms_pkt;
1421 	struct qla_hw_data *ha = vha->hw;
1422 	ms_pkt = ha->ms_iocb;
1423 	memset(ms_pkt, 0, sizeof(ms_iocb_entry_t));
1424 
1425 	ms_pkt->entry_type = MS_IOCB_TYPE;
1426 	ms_pkt->entry_count = 1;
1427 	SET_TARGET_ID(ha, ms_pkt->loop_id, vha->mgmt_svr_loop_id);
1428 	ms_pkt->control_flags = cpu_to_le16(CF_READ | CF_HEAD_TAG);
1429 	ms_pkt->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
1430 	ms_pkt->cmd_dsd_count = cpu_to_le16(1);
1431 	ms_pkt->total_dsd_count = cpu_to_le16(2);
1432 	ms_pkt->rsp_bytecount = cpu_to_le32(rsp_size);
1433 	ms_pkt->req_bytecount = cpu_to_le32(req_size);
1434 
1435 	ms_pkt->dseg_req_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1436 	ms_pkt->dseg_req_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1437 	ms_pkt->dseg_req_length = ms_pkt->req_bytecount;
1438 
1439 	ms_pkt->dseg_rsp_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1440 	ms_pkt->dseg_rsp_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1441 	ms_pkt->dseg_rsp_length = ms_pkt->rsp_bytecount;
1442 
1443 	return ms_pkt;
1444 }
1445 
1446 /**
1447  * qla24xx_prep_ms_fdmi_iocb() - Prepare common MS IOCB fields for FDMI query.
1448  * @vha: HA context
1449  * @req_size: request size in bytes
1450  * @rsp_size: response size in bytes
1451  *
1452  * Returns a pointer to the @ha's ms_iocb.
1453  */
1454 void *
1455 qla24xx_prep_ms_fdmi_iocb(scsi_qla_host_t *vha, uint32_t req_size,
1456     uint32_t rsp_size)
1457 {
1458 	struct ct_entry_24xx *ct_pkt;
1459 	struct qla_hw_data *ha = vha->hw;
1460 
1461 	ct_pkt = (struct ct_entry_24xx *)ha->ms_iocb;
1462 	memset(ct_pkt, 0, sizeof(struct ct_entry_24xx));
1463 
1464 	ct_pkt->entry_type = CT_IOCB_TYPE;
1465 	ct_pkt->entry_count = 1;
1466 	ct_pkt->nport_handle = cpu_to_le16(vha->mgmt_svr_loop_id);
1467 	ct_pkt->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
1468 	ct_pkt->cmd_dsd_count = cpu_to_le16(1);
1469 	ct_pkt->rsp_dsd_count = cpu_to_le16(1);
1470 	ct_pkt->rsp_byte_count = cpu_to_le32(rsp_size);
1471 	ct_pkt->cmd_byte_count = cpu_to_le32(req_size);
1472 
1473 	ct_pkt->dseg_0_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1474 	ct_pkt->dseg_0_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1475 	ct_pkt->dseg_0_len = ct_pkt->cmd_byte_count;
1476 
1477 	ct_pkt->dseg_1_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1478 	ct_pkt->dseg_1_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1479 	ct_pkt->dseg_1_len = ct_pkt->rsp_byte_count;
1480 	ct_pkt->vp_index = vha->vp_idx;
1481 
1482 	return ct_pkt;
1483 }
1484 
1485 static inline ms_iocb_entry_t *
1486 qla2x00_update_ms_fdmi_iocb(scsi_qla_host_t *vha, uint32_t req_size)
1487 {
1488 	struct qla_hw_data *ha = vha->hw;
1489 	ms_iocb_entry_t *ms_pkt = ha->ms_iocb;
1490 	struct ct_entry_24xx *ct_pkt = (struct ct_entry_24xx *)ha->ms_iocb;
1491 
1492 	if (IS_FWI2_CAPABLE(ha)) {
1493 		ct_pkt->cmd_byte_count = cpu_to_le32(req_size);
1494 		ct_pkt->dseg_0_len = ct_pkt->cmd_byte_count;
1495 	} else {
1496 		ms_pkt->req_bytecount = cpu_to_le32(req_size);
1497 		ms_pkt->dseg_req_length = ms_pkt->req_bytecount;
1498 	}
1499 
1500 	return ms_pkt;
1501 }
1502 
1503 /**
1504  * qla2x00_prep_ct_req() - Prepare common CT request fields for SNS query.
1505  * @p: CT request buffer
1506  * @cmd: GS command
1507  * @rsp_size: response size in bytes
1508  *
1509  * Returns a pointer to the intitialized @ct_req.
1510  */
1511 static inline struct ct_sns_req *
1512 qla2x00_prep_ct_fdmi_req(struct ct_sns_pkt *p, uint16_t cmd,
1513     uint16_t rsp_size)
1514 {
1515 	memset(p, 0, sizeof(struct ct_sns_pkt));
1516 
1517 	p->p.req.header.revision = 0x01;
1518 	p->p.req.header.gs_type = 0xFA;
1519 	p->p.req.header.gs_subtype = 0x10;
1520 	p->p.req.command = cpu_to_be16(cmd);
1521 	p->p.req.max_rsp_size = cpu_to_be16((rsp_size - 16) / 4);
1522 
1523 	return &p->p.req;
1524 }
1525 
1526 /**
1527  * qla2x00_fdmi_rhba() - perform RHBA FDMI registration
1528  * @vha: HA context
1529  *
1530  * Returns 0 on success.
1531  */
1532 static int
1533 qla2x00_fdmi_rhba(scsi_qla_host_t *vha)
1534 {
1535 	int rval, alen;
1536 	uint32_t size, sn;
1537 
1538 	ms_iocb_entry_t *ms_pkt;
1539 	struct ct_sns_req *ct_req;
1540 	struct ct_sns_rsp *ct_rsp;
1541 	void *entries;
1542 	struct ct_fdmi_hba_attr *eiter;
1543 	struct qla_hw_data *ha = vha->hw;
1544 
1545 	/* Issue RHBA */
1546 	/* Prepare common MS IOCB */
1547 	/*   Request size adjusted after CT preparation */
1548 	ms_pkt = ha->isp_ops->prep_ms_fdmi_iocb(vha, 0, RHBA_RSP_SIZE);
1549 
1550 	/* Prepare CT request */
1551 	ct_req = qla2x00_prep_ct_fdmi_req(ha->ct_sns, RHBA_CMD, RHBA_RSP_SIZE);
1552 	ct_rsp = &ha->ct_sns->p.rsp;
1553 
1554 	/* Prepare FDMI command arguments -- attribute block, attributes. */
1555 	memcpy(ct_req->req.rhba.hba_identifier, vha->port_name, WWN_SIZE);
1556 	ct_req->req.rhba.entry_count = cpu_to_be32(1);
1557 	memcpy(ct_req->req.rhba.port_name, vha->port_name, WWN_SIZE);
1558 	size = 2 * WWN_SIZE + 4 + 4;
1559 
1560 	/* Attributes */
1561 	ct_req->req.rhba.attrs.count =
1562 	    cpu_to_be32(FDMI_HBA_ATTR_COUNT);
1563 	entries = ct_req->req.rhba.hba_identifier;
1564 
1565 	/* Nodename. */
1566 	eiter = entries + size;
1567 	eiter->type = cpu_to_be16(FDMI_HBA_NODE_NAME);
1568 	eiter->len = cpu_to_be16(4 + WWN_SIZE);
1569 	memcpy(eiter->a.node_name, vha->node_name, WWN_SIZE);
1570 	size += 4 + WWN_SIZE;
1571 
1572 	ql_dbg(ql_dbg_disc, vha, 0x2025,
1573 	    "NodeName = %8phN.\n", eiter->a.node_name);
1574 
1575 	/* Manufacturer. */
1576 	eiter = entries + size;
1577 	eiter->type = cpu_to_be16(FDMI_HBA_MANUFACTURER);
1578 	alen = strlen(QLA2XXX_MANUFACTURER);
1579 	snprintf(eiter->a.manufacturer, sizeof(eiter->a.manufacturer),
1580 	    "%s", "QLogic Corporation");
1581 	alen += 4 - (alen & 3);
1582 	eiter->len = cpu_to_be16(4 + alen);
1583 	size += 4 + alen;
1584 
1585 	ql_dbg(ql_dbg_disc, vha, 0x2026,
1586 	    "Manufacturer = %s.\n", eiter->a.manufacturer);
1587 
1588 	/* Serial number. */
1589 	eiter = entries + size;
1590 	eiter->type = cpu_to_be16(FDMI_HBA_SERIAL_NUMBER);
1591 	if (IS_FWI2_CAPABLE(ha))
1592 		qla2xxx_get_vpd_field(vha, "SN", eiter->a.serial_num,
1593 		    sizeof(eiter->a.serial_num));
1594 	else {
1595 		sn = ((ha->serial0 & 0x1f) << 16) |
1596 			(ha->serial2 << 8) | ha->serial1;
1597 		snprintf(eiter->a.serial_num, sizeof(eiter->a.serial_num),
1598 		    "%c%05d", 'A' + sn / 100000, sn % 100000);
1599 	}
1600 	alen = strlen(eiter->a.serial_num);
1601 	alen += 4 - (alen & 3);
1602 	eiter->len = cpu_to_be16(4 + alen);
1603 	size += 4 + alen;
1604 
1605 	ql_dbg(ql_dbg_disc, vha, 0x2027,
1606 	    "Serial no. = %s.\n", eiter->a.serial_num);
1607 
1608 	/* Model name. */
1609 	eiter = entries + size;
1610 	eiter->type = cpu_to_be16(FDMI_HBA_MODEL);
1611 	snprintf(eiter->a.model, sizeof(eiter->a.model),
1612 	    "%s", ha->model_number);
1613 	alen = strlen(eiter->a.model);
1614 	alen += 4 - (alen & 3);
1615 	eiter->len = cpu_to_be16(4 + alen);
1616 	size += 4 + alen;
1617 
1618 	ql_dbg(ql_dbg_disc, vha, 0x2028,
1619 	    "Model Name = %s.\n", eiter->a.model);
1620 
1621 	/* Model description. */
1622 	eiter = entries + size;
1623 	eiter->type = cpu_to_be16(FDMI_HBA_MODEL_DESCRIPTION);
1624 	snprintf(eiter->a.model_desc, sizeof(eiter->a.model_desc),
1625 	    "%s", ha->model_desc);
1626 	alen = strlen(eiter->a.model_desc);
1627 	alen += 4 - (alen & 3);
1628 	eiter->len = cpu_to_be16(4 + alen);
1629 	size += 4 + alen;
1630 
1631 	ql_dbg(ql_dbg_disc, vha, 0x2029,
1632 	    "Model Desc = %s.\n", eiter->a.model_desc);
1633 
1634 	/* Hardware version. */
1635 	eiter = entries + size;
1636 	eiter->type = cpu_to_be16(FDMI_HBA_HARDWARE_VERSION);
1637 	if (!IS_FWI2_CAPABLE(ha)) {
1638 		snprintf(eiter->a.hw_version, sizeof(eiter->a.hw_version),
1639 		    "HW:%s", ha->adapter_id);
1640 	} else if (qla2xxx_get_vpd_field(vha, "MN", eiter->a.hw_version,
1641 		    sizeof(eiter->a.hw_version))) {
1642 		;
1643 	} else if (qla2xxx_get_vpd_field(vha, "EC", eiter->a.hw_version,
1644 		    sizeof(eiter->a.hw_version))) {
1645 		;
1646 	} else {
1647 		snprintf(eiter->a.hw_version, sizeof(eiter->a.hw_version),
1648 		    "HW:%s", ha->adapter_id);
1649 	}
1650 	alen = strlen(eiter->a.hw_version);
1651 	alen += 4 - (alen & 3);
1652 	eiter->len = cpu_to_be16(4 + alen);
1653 	size += 4 + alen;
1654 
1655 	ql_dbg(ql_dbg_disc, vha, 0x202a,
1656 	    "Hardware ver = %s.\n", eiter->a.hw_version);
1657 
1658 	/* Driver version. */
1659 	eiter = entries + size;
1660 	eiter->type = cpu_to_be16(FDMI_HBA_DRIVER_VERSION);
1661 	snprintf(eiter->a.driver_version, sizeof(eiter->a.driver_version),
1662 	    "%s", qla2x00_version_str);
1663 	alen = strlen(eiter->a.driver_version);
1664 	alen += 4 - (alen & 3);
1665 	eiter->len = cpu_to_be16(4 + alen);
1666 	size += 4 + alen;
1667 
1668 	ql_dbg(ql_dbg_disc, vha, 0x202b,
1669 	    "Driver ver = %s.\n", eiter->a.driver_version);
1670 
1671 	/* Option ROM version. */
1672 	eiter = entries + size;
1673 	eiter->type = cpu_to_be16(FDMI_HBA_OPTION_ROM_VERSION);
1674 	snprintf(eiter->a.orom_version, sizeof(eiter->a.orom_version),
1675 	    "%d.%02d", ha->bios_revision[1], ha->bios_revision[0]);
1676 	alen = strlen(eiter->a.orom_version);
1677 	alen += 4 - (alen & 3);
1678 	eiter->len = cpu_to_be16(4 + alen);
1679 	size += 4 + alen;
1680 
1681 	ql_dbg(ql_dbg_disc, vha , 0x202c,
1682 	    "Optrom vers = %s.\n", eiter->a.orom_version);
1683 
1684 	/* Firmware version */
1685 	eiter = entries + size;
1686 	eiter->type = cpu_to_be16(FDMI_HBA_FIRMWARE_VERSION);
1687 	ha->isp_ops->fw_version_str(vha, eiter->a.fw_version,
1688 	    sizeof(eiter->a.fw_version));
1689 	alen = strlen(eiter->a.fw_version);
1690 	alen += 4 - (alen & 3);
1691 	eiter->len = cpu_to_be16(4 + alen);
1692 	size += 4 + alen;
1693 
1694 	ql_dbg(ql_dbg_disc, vha, 0x202d,
1695 	    "Firmware vers = %s.\n", eiter->a.fw_version);
1696 
1697 	/* Update MS request size. */
1698 	qla2x00_update_ms_fdmi_iocb(vha, size + 16);
1699 
1700 	ql_dbg(ql_dbg_disc, vha, 0x202e,
1701 	    "RHBA identifier = %8phN size=%d.\n",
1702 	    ct_req->req.rhba.hba_identifier, size);
1703 	ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2076,
1704 	    entries, size);
1705 
1706 	/* Execute MS IOCB */
1707 	rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
1708 	    sizeof(ms_iocb_entry_t));
1709 	if (rval != QLA_SUCCESS) {
1710 		/*EMPTY*/
1711 		ql_dbg(ql_dbg_disc, vha, 0x2030,
1712 		    "RHBA issue IOCB failed (%d).\n", rval);
1713 	} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp, "RHBA") !=
1714 	    QLA_SUCCESS) {
1715 		rval = QLA_FUNCTION_FAILED;
1716 		if (ct_rsp->header.reason_code == CT_REASON_CANNOT_PERFORM &&
1717 		    ct_rsp->header.explanation_code ==
1718 		    CT_EXPL_ALREADY_REGISTERED) {
1719 			ql_dbg(ql_dbg_disc, vha, 0x2034,
1720 			    "HBA already registered.\n");
1721 			rval = QLA_ALREADY_REGISTERED;
1722 		} else {
1723 			ql_dbg(ql_dbg_disc, vha, 0x20ad,
1724 			    "RHBA FDMI registration failed, CT Reason code: 0x%x, CT Explanation 0x%x\n",
1725 			    ct_rsp->header.reason_code,
1726 			    ct_rsp->header.explanation_code);
1727 		}
1728 	} else {
1729 		ql_dbg(ql_dbg_disc, vha, 0x2035,
1730 		    "RHBA exiting normally.\n");
1731 	}
1732 
1733 	return rval;
1734 }
1735 
1736 /**
1737  * qla2x00_fdmi_rpa() - perform RPA registration
1738  * @vha: HA context
1739  *
1740  * Returns 0 on success.
1741  */
1742 static int
1743 qla2x00_fdmi_rpa(scsi_qla_host_t *vha)
1744 {
1745 	int rval, alen;
1746 	uint32_t size;
1747 	struct qla_hw_data *ha = vha->hw;
1748 	ms_iocb_entry_t *ms_pkt;
1749 	struct ct_sns_req *ct_req;
1750 	struct ct_sns_rsp *ct_rsp;
1751 	void *entries;
1752 	struct ct_fdmi_port_attr *eiter;
1753 	struct init_cb_24xx *icb24 = (struct init_cb_24xx *)ha->init_cb;
1754 	struct new_utsname *p_sysid = NULL;
1755 
1756 	/* Issue RPA */
1757 	/* Prepare common MS IOCB */
1758 	/*   Request size adjusted after CT preparation */
1759 	ms_pkt = ha->isp_ops->prep_ms_fdmi_iocb(vha, 0, RPA_RSP_SIZE);
1760 
1761 	/* Prepare CT request */
1762 	ct_req = qla2x00_prep_ct_fdmi_req(ha->ct_sns, RPA_CMD,
1763 	    RPA_RSP_SIZE);
1764 	ct_rsp = &ha->ct_sns->p.rsp;
1765 
1766 	/* Prepare FDMI command arguments -- attribute block, attributes. */
1767 	memcpy(ct_req->req.rpa.port_name, vha->port_name, WWN_SIZE);
1768 	size = WWN_SIZE + 4;
1769 
1770 	/* Attributes */
1771 	ct_req->req.rpa.attrs.count = cpu_to_be32(FDMI_PORT_ATTR_COUNT);
1772 	entries = ct_req->req.rpa.port_name;
1773 
1774 	/* FC4 types. */
1775 	eiter = entries + size;
1776 	eiter->type = cpu_to_be16(FDMI_PORT_FC4_TYPES);
1777 	eiter->len = cpu_to_be16(4 + 32);
1778 	eiter->a.fc4_types[2] = 0x01;
1779 	size += 4 + 32;
1780 
1781 	ql_dbg(ql_dbg_disc, vha, 0x2039,
1782 	    "FC4_TYPES=%02x %02x.\n",
1783 	    eiter->a.fc4_types[2],
1784 	    eiter->a.fc4_types[1]);
1785 
1786 	/* Supported speed. */
1787 	eiter = entries + size;
1788 	eiter->type = cpu_to_be16(FDMI_PORT_SUPPORT_SPEED);
1789 	eiter->len = cpu_to_be16(4 + 4);
1790 	if (IS_CNA_CAPABLE(ha))
1791 		eiter->a.sup_speed = cpu_to_be32(
1792 		    FDMI_PORT_SPEED_10GB);
1793 	else if (IS_QLA27XX(ha))
1794 		eiter->a.sup_speed = cpu_to_be32(
1795 		    FDMI_PORT_SPEED_32GB|
1796 		    FDMI_PORT_SPEED_16GB|
1797 		    FDMI_PORT_SPEED_8GB);
1798 	else if (IS_QLA2031(ha))
1799 		eiter->a.sup_speed = cpu_to_be32(
1800 		    FDMI_PORT_SPEED_16GB|
1801 		    FDMI_PORT_SPEED_8GB|
1802 		    FDMI_PORT_SPEED_4GB);
1803 	else if (IS_QLA25XX(ha))
1804 		eiter->a.sup_speed = cpu_to_be32(
1805 		    FDMI_PORT_SPEED_8GB|
1806 		    FDMI_PORT_SPEED_4GB|
1807 		    FDMI_PORT_SPEED_2GB|
1808 		    FDMI_PORT_SPEED_1GB);
1809 	else if (IS_QLA24XX_TYPE(ha))
1810 		eiter->a.sup_speed = cpu_to_be32(
1811 		    FDMI_PORT_SPEED_4GB|
1812 		    FDMI_PORT_SPEED_2GB|
1813 		    FDMI_PORT_SPEED_1GB);
1814 	else if (IS_QLA23XX(ha))
1815 		eiter->a.sup_speed = cpu_to_be32(
1816 		    FDMI_PORT_SPEED_2GB|
1817 		    FDMI_PORT_SPEED_1GB);
1818 	else
1819 		eiter->a.sup_speed = cpu_to_be32(
1820 		    FDMI_PORT_SPEED_1GB);
1821 	size += 4 + 4;
1822 
1823 	ql_dbg(ql_dbg_disc, vha, 0x203a,
1824 	    "Supported_Speed=%x.\n", eiter->a.sup_speed);
1825 
1826 	/* Current speed. */
1827 	eiter = entries + size;
1828 	eiter->type = cpu_to_be16(FDMI_PORT_CURRENT_SPEED);
1829 	eiter->len = cpu_to_be16(4 + 4);
1830 	switch (ha->link_data_rate) {
1831 	case PORT_SPEED_1GB:
1832 		eiter->a.cur_speed =
1833 		    cpu_to_be32(FDMI_PORT_SPEED_1GB);
1834 		break;
1835 	case PORT_SPEED_2GB:
1836 		eiter->a.cur_speed =
1837 		    cpu_to_be32(FDMI_PORT_SPEED_2GB);
1838 		break;
1839 	case PORT_SPEED_4GB:
1840 		eiter->a.cur_speed =
1841 		    cpu_to_be32(FDMI_PORT_SPEED_4GB);
1842 		break;
1843 	case PORT_SPEED_8GB:
1844 		eiter->a.cur_speed =
1845 		    cpu_to_be32(FDMI_PORT_SPEED_8GB);
1846 		break;
1847 	case PORT_SPEED_10GB:
1848 		eiter->a.cur_speed =
1849 		    cpu_to_be32(FDMI_PORT_SPEED_10GB);
1850 		break;
1851 	case PORT_SPEED_16GB:
1852 		eiter->a.cur_speed =
1853 		    cpu_to_be32(FDMI_PORT_SPEED_16GB);
1854 		break;
1855 	case PORT_SPEED_32GB:
1856 		eiter->a.cur_speed =
1857 		    cpu_to_be32(FDMI_PORT_SPEED_32GB);
1858 		break;
1859 	default:
1860 		eiter->a.cur_speed =
1861 		    cpu_to_be32(FDMI_PORT_SPEED_UNKNOWN);
1862 		break;
1863 	}
1864 	size += 4 + 4;
1865 
1866 	ql_dbg(ql_dbg_disc, vha, 0x203b,
1867 	    "Current_Speed=%x.\n", eiter->a.cur_speed);
1868 
1869 	/* Max frame size. */
1870 	eiter = entries + size;
1871 	eiter->type = cpu_to_be16(FDMI_PORT_MAX_FRAME_SIZE);
1872 	eiter->len = cpu_to_be16(4 + 4);
1873 	eiter->a.max_frame_size = IS_FWI2_CAPABLE(ha) ?
1874 	    le16_to_cpu(icb24->frame_payload_size) :
1875 	    le16_to_cpu(ha->init_cb->frame_payload_size);
1876 	eiter->a.max_frame_size = cpu_to_be32(eiter->a.max_frame_size);
1877 	size += 4 + 4;
1878 
1879 	ql_dbg(ql_dbg_disc, vha, 0x203c,
1880 	    "Max_Frame_Size=%x.\n", eiter->a.max_frame_size);
1881 
1882 	/* OS device name. */
1883 	eiter = entries + size;
1884 	eiter->type = cpu_to_be16(FDMI_PORT_OS_DEVICE_NAME);
1885 	snprintf(eiter->a.os_dev_name, sizeof(eiter->a.os_dev_name),
1886 	    "%s:host%lu", QLA2XXX_DRIVER_NAME, vha->host_no);
1887 	alen = strlen(eiter->a.os_dev_name);
1888 	alen += 4 - (alen & 3);
1889 	eiter->len = cpu_to_be16(4 + alen);
1890 	size += 4 + alen;
1891 
1892 	ql_dbg(ql_dbg_disc, vha, 0x204b,
1893 	    "OS_Device_Name=%s.\n", eiter->a.os_dev_name);
1894 
1895 	/* Hostname. */
1896 	eiter = entries + size;
1897 	eiter->type = cpu_to_be16(FDMI_PORT_HOST_NAME);
1898 	p_sysid = utsname();
1899 	if (p_sysid) {
1900 		snprintf(eiter->a.host_name, sizeof(eiter->a.host_name),
1901 		    "%s", p_sysid->nodename);
1902 	} else {
1903 		snprintf(eiter->a.host_name, sizeof(eiter->a.host_name),
1904 		    "%s", fc_host_system_hostname(vha->host));
1905 	}
1906 	alen = strlen(eiter->a.host_name);
1907 	alen += 4 - (alen & 3);
1908 	eiter->len = cpu_to_be16(4 + alen);
1909 	size += 4 + alen;
1910 
1911 	ql_dbg(ql_dbg_disc, vha, 0x203d, "HostName=%s.\n", eiter->a.host_name);
1912 
1913 	/* Update MS request size. */
1914 	qla2x00_update_ms_fdmi_iocb(vha, size + 16);
1915 
1916 	ql_dbg(ql_dbg_disc, vha, 0x203e,
1917 	    "RPA portname  %016llx, size = %d.\n",
1918 	    wwn_to_u64(ct_req->req.rpa.port_name), size);
1919 	ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2079,
1920 	    entries, size);
1921 
1922 	/* Execute MS IOCB */
1923 	rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
1924 	    sizeof(ms_iocb_entry_t));
1925 	if (rval != QLA_SUCCESS) {
1926 		/*EMPTY*/
1927 		ql_dbg(ql_dbg_disc, vha, 0x2040,
1928 		    "RPA issue IOCB failed (%d).\n", rval);
1929 	} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp, "RPA") !=
1930 	    QLA_SUCCESS) {
1931 		rval = QLA_FUNCTION_FAILED;
1932 		if (ct_rsp->header.reason_code == CT_REASON_CANNOT_PERFORM &&
1933 		    ct_rsp->header.explanation_code ==
1934 		    CT_EXPL_ALREADY_REGISTERED) {
1935 			ql_dbg(ql_dbg_disc, vha, 0x20cd,
1936 			    "RPA already registered.\n");
1937 			rval = QLA_ALREADY_REGISTERED;
1938 		}
1939 
1940 	} else {
1941 		ql_dbg(ql_dbg_disc, vha, 0x2041,
1942 		    "RPA exiting normally.\n");
1943 	}
1944 
1945 	return rval;
1946 }
1947 
1948 /**
1949  * qla2x00_fdmiv2_rhba() - perform RHBA FDMI v2 registration
1950  * @vha: HA context
1951  *
1952  * Returns 0 on success.
1953  */
1954 static int
1955 qla2x00_fdmiv2_rhba(scsi_qla_host_t *vha)
1956 {
1957 	int rval, alen;
1958 	uint32_t size, sn;
1959 	ms_iocb_entry_t *ms_pkt;
1960 	struct ct_sns_req *ct_req;
1961 	struct ct_sns_rsp *ct_rsp;
1962 	void *entries;
1963 	struct ct_fdmiv2_hba_attr *eiter;
1964 	struct qla_hw_data *ha = vha->hw;
1965 	struct new_utsname *p_sysid = NULL;
1966 
1967 	/* Issue RHBA */
1968 	/* Prepare common MS IOCB */
1969 	/*   Request size adjusted after CT preparation */
1970 	ms_pkt = ha->isp_ops->prep_ms_fdmi_iocb(vha, 0, RHBA_RSP_SIZE);
1971 
1972 	/* Prepare CT request */
1973 	ct_req = qla2x00_prep_ct_fdmi_req(ha->ct_sns, RHBA_CMD,
1974 	    RHBA_RSP_SIZE);
1975 	ct_rsp = &ha->ct_sns->p.rsp;
1976 
1977 	/* Prepare FDMI command arguments -- attribute block, attributes. */
1978 	memcpy(ct_req->req.rhba2.hba_identifier, vha->port_name, WWN_SIZE);
1979 	ct_req->req.rhba2.entry_count = cpu_to_be32(1);
1980 	memcpy(ct_req->req.rhba2.port_name, vha->port_name, WWN_SIZE);
1981 	size = 2 * WWN_SIZE + 4 + 4;
1982 
1983 	/* Attributes */
1984 	ct_req->req.rhba2.attrs.count = cpu_to_be32(FDMIV2_HBA_ATTR_COUNT);
1985 	entries = ct_req->req.rhba2.hba_identifier;
1986 
1987 	/* Nodename. */
1988 	eiter = entries + size;
1989 	eiter->type = cpu_to_be16(FDMI_HBA_NODE_NAME);
1990 	eiter->len = cpu_to_be16(4 + WWN_SIZE);
1991 	memcpy(eiter->a.node_name, vha->node_name, WWN_SIZE);
1992 	size += 4 + WWN_SIZE;
1993 
1994 	ql_dbg(ql_dbg_disc, vha, 0x207d,
1995 	    "NodeName = %016llx.\n", wwn_to_u64(eiter->a.node_name));
1996 
1997 	/* Manufacturer. */
1998 	eiter = entries + size;
1999 	eiter->type = cpu_to_be16(FDMI_HBA_MANUFACTURER);
2000 	snprintf(eiter->a.manufacturer, sizeof(eiter->a.manufacturer),
2001 	    "%s", "QLogic Corporation");
2002 	eiter->a.manufacturer[strlen("QLogic Corporation")] = '\0';
2003 	alen = strlen(eiter->a.manufacturer);
2004 	alen += 4 - (alen & 3);
2005 	eiter->len = cpu_to_be16(4 + alen);
2006 	size += 4 + alen;
2007 
2008 	ql_dbg(ql_dbg_disc, vha, 0x20a5,
2009 	    "Manufacturer = %s.\n", eiter->a.manufacturer);
2010 
2011 	/* Serial number. */
2012 	eiter = entries + size;
2013 	eiter->type = cpu_to_be16(FDMI_HBA_SERIAL_NUMBER);
2014 	if (IS_FWI2_CAPABLE(ha))
2015 		qla2xxx_get_vpd_field(vha, "SN", eiter->a.serial_num,
2016 		    sizeof(eiter->a.serial_num));
2017 	else {
2018 		sn = ((ha->serial0 & 0x1f) << 16) |
2019 			(ha->serial2 << 8) | ha->serial1;
2020 		snprintf(eiter->a.serial_num, sizeof(eiter->a.serial_num),
2021 		    "%c%05d", 'A' + sn / 100000, sn % 100000);
2022 	}
2023 	alen = strlen(eiter->a.serial_num);
2024 	alen += 4 - (alen & 3);
2025 	eiter->len = cpu_to_be16(4 + alen);
2026 	size += 4 + alen;
2027 
2028 	ql_dbg(ql_dbg_disc, vha, 0x20a6,
2029 	    "Serial no. = %s.\n", eiter->a.serial_num);
2030 
2031 	/* Model name. */
2032 	eiter = entries + size;
2033 	eiter->type = cpu_to_be16(FDMI_HBA_MODEL);
2034 	snprintf(eiter->a.model, sizeof(eiter->a.model),
2035 	    "%s", ha->model_number);
2036 	alen = strlen(eiter->a.model);
2037 	alen += 4 - (alen & 3);
2038 	eiter->len = cpu_to_be16(4 + alen);
2039 	size += 4 + alen;
2040 
2041 	ql_dbg(ql_dbg_disc, vha, 0x20a7,
2042 	    "Model Name = %s.\n", eiter->a.model);
2043 
2044 	/* Model description. */
2045 	eiter = entries + size;
2046 	eiter->type = cpu_to_be16(FDMI_HBA_MODEL_DESCRIPTION);
2047 	snprintf(eiter->a.model_desc, sizeof(eiter->a.model_desc),
2048 	    "%s", ha->model_desc);
2049 	alen = strlen(eiter->a.model_desc);
2050 	alen += 4 - (alen & 3);
2051 	eiter->len = cpu_to_be16(4 + alen);
2052 	size += 4 + alen;
2053 
2054 	ql_dbg(ql_dbg_disc, vha, 0x20a8,
2055 	    "Model Desc = %s.\n", eiter->a.model_desc);
2056 
2057 	/* Hardware version. */
2058 	eiter = entries + size;
2059 	eiter->type = cpu_to_be16(FDMI_HBA_HARDWARE_VERSION);
2060 	if (!IS_FWI2_CAPABLE(ha)) {
2061 		snprintf(eiter->a.hw_version, sizeof(eiter->a.hw_version),
2062 		    "HW:%s", ha->adapter_id);
2063 	} else if (qla2xxx_get_vpd_field(vha, "MN", eiter->a.hw_version,
2064 		    sizeof(eiter->a.hw_version))) {
2065 		;
2066 	} else if (qla2xxx_get_vpd_field(vha, "EC", eiter->a.hw_version,
2067 		    sizeof(eiter->a.hw_version))) {
2068 		;
2069 	} else {
2070 		snprintf(eiter->a.hw_version, sizeof(eiter->a.hw_version),
2071 		    "HW:%s", ha->adapter_id);
2072 	}
2073 	alen = strlen(eiter->a.hw_version);
2074 	alen += 4 - (alen & 3);
2075 	eiter->len = cpu_to_be16(4 + alen);
2076 	size += 4 + alen;
2077 
2078 	ql_dbg(ql_dbg_disc, vha, 0x20a9,
2079 	    "Hardware ver = %s.\n", eiter->a.hw_version);
2080 
2081 	/* Driver version. */
2082 	eiter = entries + size;
2083 	eiter->type = cpu_to_be16(FDMI_HBA_DRIVER_VERSION);
2084 	snprintf(eiter->a.driver_version, sizeof(eiter->a.driver_version),
2085 	    "%s", qla2x00_version_str);
2086 	alen = strlen(eiter->a.driver_version);
2087 	alen += 4 - (alen & 3);
2088 	eiter->len = cpu_to_be16(4 + alen);
2089 	size += 4 + alen;
2090 
2091 	ql_dbg(ql_dbg_disc, vha, 0x20aa,
2092 	    "Driver ver = %s.\n", eiter->a.driver_version);
2093 
2094 	/* Option ROM version. */
2095 	eiter = entries + size;
2096 	eiter->type = cpu_to_be16(FDMI_HBA_OPTION_ROM_VERSION);
2097 	snprintf(eiter->a.orom_version, sizeof(eiter->a.orom_version),
2098 	    "%d.%02d", ha->bios_revision[1], ha->bios_revision[0]);
2099 	alen = strlen(eiter->a.orom_version);
2100 	alen += 4 - (alen & 3);
2101 	eiter->len = cpu_to_be16(4 + alen);
2102 	size += 4 + alen;
2103 
2104 	ql_dbg(ql_dbg_disc, vha , 0x20ab,
2105 	    "Optrom version = %d.%02d.\n", eiter->a.orom_version[1],
2106 	    eiter->a.orom_version[0]);
2107 
2108 	/* Firmware version */
2109 	eiter = entries + size;
2110 	eiter->type = cpu_to_be16(FDMI_HBA_FIRMWARE_VERSION);
2111 	ha->isp_ops->fw_version_str(vha, eiter->a.fw_version,
2112 	    sizeof(eiter->a.fw_version));
2113 	alen = strlen(eiter->a.fw_version);
2114 	alen += 4 - (alen & 3);
2115 	eiter->len = cpu_to_be16(4 + alen);
2116 	size += 4 + alen;
2117 
2118 	ql_dbg(ql_dbg_disc, vha, 0x20ac,
2119 	    "Firmware vers = %s.\n", eiter->a.fw_version);
2120 
2121 	/* OS Name and Version */
2122 	eiter = entries + size;
2123 	eiter->type = cpu_to_be16(FDMI_HBA_OS_NAME_AND_VERSION);
2124 	p_sysid = utsname();
2125 	if (p_sysid) {
2126 		snprintf(eiter->a.os_version, sizeof(eiter->a.os_version),
2127 		    "%s %s %s",
2128 		    p_sysid->sysname, p_sysid->release, p_sysid->version);
2129 	} else {
2130 		snprintf(eiter->a.os_version, sizeof(eiter->a.os_version),
2131 		    "%s %s", "Linux", fc_host_system_hostname(vha->host));
2132 	}
2133 	alen = strlen(eiter->a.os_version);
2134 	alen += 4 - (alen & 3);
2135 	eiter->len = cpu_to_be16(4 + alen);
2136 	size += 4 + alen;
2137 
2138 	ql_dbg(ql_dbg_disc, vha, 0x20ae,
2139 	    "OS Name and Version = %s.\n", eiter->a.os_version);
2140 
2141 	/* MAX CT Payload Length */
2142 	eiter = entries + size;
2143 	eiter->type = cpu_to_be16(FDMI_HBA_MAXIMUM_CT_PAYLOAD_LENGTH);
2144 	eiter->a.max_ct_len = cpu_to_be32(ha->frame_payload_size);
2145 	eiter->a.max_ct_len = cpu_to_be32(eiter->a.max_ct_len);
2146 	eiter->len = cpu_to_be16(4 + 4);
2147 	size += 4 + 4;
2148 
2149 	ql_dbg(ql_dbg_disc, vha, 0x20af,
2150 	    "CT Payload Length = 0x%x.\n", eiter->a.max_ct_len);
2151 
2152 	/* Node Sybolic Name */
2153 	eiter = entries + size;
2154 	eiter->type = cpu_to_be16(FDMI_HBA_NODE_SYMBOLIC_NAME);
2155 	qla2x00_get_sym_node_name(vha, eiter->a.sym_name,
2156 	    sizeof(eiter->a.sym_name));
2157 	alen = strlen(eiter->a.sym_name);
2158 	alen += 4 - (alen & 3);
2159 	eiter->len = cpu_to_be16(4 + alen);
2160 	size += 4 + alen;
2161 
2162 	ql_dbg(ql_dbg_disc, vha, 0x20b0,
2163 	    "Symbolic Name = %s.\n", eiter->a.sym_name);
2164 
2165 	/* Vendor Id */
2166 	eiter = entries + size;
2167 	eiter->type = cpu_to_be16(FDMI_HBA_VENDOR_ID);
2168 	eiter->a.vendor_id = cpu_to_be32(0x1077);
2169 	eiter->len = cpu_to_be16(4 + 4);
2170 	size += 4 + 4;
2171 
2172 	ql_dbg(ql_dbg_disc, vha, 0x20b1,
2173 	    "Vendor Id = %x.\n", eiter->a.vendor_id);
2174 
2175 	/* Num Ports */
2176 	eiter = entries + size;
2177 	eiter->type = cpu_to_be16(FDMI_HBA_NUM_PORTS);
2178 	eiter->a.num_ports = cpu_to_be32(1);
2179 	eiter->len = cpu_to_be16(4 + 4);
2180 	size += 4 + 4;
2181 
2182 	ql_dbg(ql_dbg_disc, vha, 0x20b2,
2183 	    "Port Num = %x.\n", eiter->a.num_ports);
2184 
2185 	/* Fabric Name */
2186 	eiter = entries + size;
2187 	eiter->type = cpu_to_be16(FDMI_HBA_FABRIC_NAME);
2188 	memcpy(eiter->a.fabric_name, vha->fabric_node_name, WWN_SIZE);
2189 	eiter->len = cpu_to_be16(4 + WWN_SIZE);
2190 	size += 4 + WWN_SIZE;
2191 
2192 	ql_dbg(ql_dbg_disc, vha, 0x20b3,
2193 	    "Fabric Name = %016llx.\n", wwn_to_u64(eiter->a.fabric_name));
2194 
2195 	/* BIOS Version */
2196 	eiter = entries + size;
2197 	eiter->type = cpu_to_be16(FDMI_HBA_BOOT_BIOS_NAME);
2198 	snprintf(eiter->a.bios_name, sizeof(eiter->a.bios_name),
2199 	    "BIOS %d.%02d", ha->bios_revision[1], ha->bios_revision[0]);
2200 	alen = strlen(eiter->a.bios_name);
2201 	alen += 4 - (alen & 3);
2202 	eiter->len = cpu_to_be16(4 + alen);
2203 	size += 4 + alen;
2204 
2205 	ql_dbg(ql_dbg_disc, vha, 0x20b4,
2206 	    "BIOS Name = %s\n", eiter->a.bios_name);
2207 
2208 	/* Vendor Identifier */
2209 	eiter = entries + size;
2210 	eiter->type = cpu_to_be16(FDMI_HBA_TYPE_VENDOR_IDENTIFIER);
2211 	snprintf(eiter->a.vendor_identifier, sizeof(eiter->a.vendor_identifier),
2212 	    "%s", "QLGC");
2213 	alen = strlen(eiter->a.vendor_identifier);
2214 	alen += 4 - (alen & 3);
2215 	eiter->len = cpu_to_be16(4 + alen);
2216 	size += 4 + alen;
2217 
2218 	ql_dbg(ql_dbg_disc, vha, 0x201b,
2219 	    "Vendor Identifier = %s.\n", eiter->a.vendor_identifier);
2220 
2221 	/* Update MS request size. */
2222 	qla2x00_update_ms_fdmi_iocb(vha, size + 16);
2223 
2224 	ql_dbg(ql_dbg_disc, vha, 0x20b5,
2225 	    "RHBA identifier = %016llx.\n",
2226 	    wwn_to_u64(ct_req->req.rhba2.hba_identifier));
2227 	ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x20b6,
2228 	    entries, size);
2229 
2230 	/* Execute MS IOCB */
2231 	rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
2232 	    sizeof(ms_iocb_entry_t));
2233 	if (rval != QLA_SUCCESS) {
2234 		/*EMPTY*/
2235 		ql_dbg(ql_dbg_disc, vha, 0x20b7,
2236 		    "RHBA issue IOCB failed (%d).\n", rval);
2237 	} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp, "RHBA") !=
2238 	    QLA_SUCCESS) {
2239 		rval = QLA_FUNCTION_FAILED;
2240 
2241 		if (ct_rsp->header.reason_code == CT_REASON_CANNOT_PERFORM &&
2242 		    ct_rsp->header.explanation_code ==
2243 		    CT_EXPL_ALREADY_REGISTERED) {
2244 			ql_dbg(ql_dbg_disc, vha, 0x20b8,
2245 			    "HBA already registered.\n");
2246 			rval = QLA_ALREADY_REGISTERED;
2247 		} else {
2248 			ql_dbg(ql_dbg_disc, vha, 0x2016,
2249 			    "RHBA FDMI v2 failed, CT Reason code: 0x%x, CT Explanation 0x%x\n",
2250 			    ct_rsp->header.reason_code,
2251 			    ct_rsp->header.explanation_code);
2252 		}
2253 	} else {
2254 		ql_dbg(ql_dbg_disc, vha, 0x20b9,
2255 		    "RHBA FDMI V2 exiting normally.\n");
2256 	}
2257 
2258 	return rval;
2259 }
2260 
2261 /**
2262  * qla2x00_fdmi_dhba() -
2263  * @vha: HA context
2264  *
2265  * Returns 0 on success.
2266  */
2267 static int
2268 qla2x00_fdmi_dhba(scsi_qla_host_t *vha)
2269 {
2270 	int rval;
2271 	struct qla_hw_data *ha = vha->hw;
2272 	ms_iocb_entry_t *ms_pkt;
2273 	struct ct_sns_req *ct_req;
2274 	struct ct_sns_rsp *ct_rsp;
2275 
2276 	/* Issue RPA */
2277 	/* Prepare common MS IOCB */
2278 	ms_pkt = ha->isp_ops->prep_ms_fdmi_iocb(vha, DHBA_REQ_SIZE,
2279 	    DHBA_RSP_SIZE);
2280 
2281 	/* Prepare CT request */
2282 	ct_req = qla2x00_prep_ct_fdmi_req(ha->ct_sns, DHBA_CMD, DHBA_RSP_SIZE);
2283 	ct_rsp = &ha->ct_sns->p.rsp;
2284 
2285 	/* Prepare FDMI command arguments -- portname. */
2286 	memcpy(ct_req->req.dhba.port_name, vha->port_name, WWN_SIZE);
2287 
2288 	ql_dbg(ql_dbg_disc, vha, 0x2036,
2289 	    "DHBA portname = %8phN.\n", ct_req->req.dhba.port_name);
2290 
2291 	/* Execute MS IOCB */
2292 	rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
2293 	    sizeof(ms_iocb_entry_t));
2294 	if (rval != QLA_SUCCESS) {
2295 		/*EMPTY*/
2296 		ql_dbg(ql_dbg_disc, vha, 0x2037,
2297 		    "DHBA issue IOCB failed (%d).\n", rval);
2298 	} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp, "DHBA") !=
2299 	    QLA_SUCCESS) {
2300 		rval = QLA_FUNCTION_FAILED;
2301 	} else {
2302 		ql_dbg(ql_dbg_disc, vha, 0x2038,
2303 		    "DHBA exiting normally.\n");
2304 	}
2305 
2306 	return rval;
2307 }
2308 
2309 /**
2310  * qla2x00_fdmiv2_rpa() -
2311  * @vha: HA context
2312  *
2313  * Returns 0 on success.
2314  */
2315 static int
2316 qla2x00_fdmiv2_rpa(scsi_qla_host_t *vha)
2317 {
2318 	int rval, alen;
2319 	uint32_t size;
2320 	struct qla_hw_data *ha = vha->hw;
2321 	ms_iocb_entry_t *ms_pkt;
2322 	struct ct_sns_req *ct_req;
2323 	struct ct_sns_rsp *ct_rsp;
2324 	void *entries;
2325 	struct ct_fdmiv2_port_attr *eiter;
2326 	struct init_cb_24xx *icb24 = (struct init_cb_24xx *)ha->init_cb;
2327 	struct new_utsname *p_sysid = NULL;
2328 
2329 	/* Issue RPA */
2330 	/* Prepare common MS IOCB */
2331 	/*   Request size adjusted after CT preparation */
2332 	ms_pkt = ha->isp_ops->prep_ms_fdmi_iocb(vha, 0, RPA_RSP_SIZE);
2333 
2334 	/* Prepare CT request */
2335 	ct_req = qla2x00_prep_ct_fdmi_req(ha->ct_sns, RPA_CMD, RPA_RSP_SIZE);
2336 	ct_rsp = &ha->ct_sns->p.rsp;
2337 
2338 	/* Prepare FDMI command arguments -- attribute block, attributes. */
2339 	memcpy(ct_req->req.rpa2.port_name, vha->port_name, WWN_SIZE);
2340 	size = WWN_SIZE + 4;
2341 
2342 	/* Attributes */
2343 	ct_req->req.rpa2.attrs.count = cpu_to_be32(FDMIV2_PORT_ATTR_COUNT);
2344 	entries = ct_req->req.rpa2.port_name;
2345 
2346 	/* FC4 types. */
2347 	eiter = entries + size;
2348 	eiter->type = cpu_to_be16(FDMI_PORT_FC4_TYPES);
2349 	eiter->len = cpu_to_be16(4 + 32);
2350 	eiter->a.fc4_types[2] = 0x01;
2351 	size += 4 + 32;
2352 
2353 	ql_dbg(ql_dbg_disc, vha, 0x20ba,
2354 	    "FC4_TYPES=%02x %02x.\n",
2355 	    eiter->a.fc4_types[2],
2356 	    eiter->a.fc4_types[1]);
2357 
2358 	if (vha->flags.nvme_enabled) {
2359 		eiter->a.fc4_types[6] = 1;	/* NVMe type 28h */
2360 		ql_dbg(ql_dbg_disc, vha, 0x211f,
2361 		    "NVME FC4 Type = %02x 0x0 0x0 0x0 0x0 0x0.\n",
2362 		    eiter->a.fc4_types[6]);
2363 	}
2364 
2365 	/* Supported speed. */
2366 	eiter = entries + size;
2367 	eiter->type = cpu_to_be16(FDMI_PORT_SUPPORT_SPEED);
2368 	eiter->len = cpu_to_be16(4 + 4);
2369 	if (IS_CNA_CAPABLE(ha))
2370 		eiter->a.sup_speed = cpu_to_be32(
2371 		    FDMI_PORT_SPEED_10GB);
2372 	else if (IS_QLA27XX(ha))
2373 		eiter->a.sup_speed = cpu_to_be32(
2374 		    FDMI_PORT_SPEED_32GB|
2375 		    FDMI_PORT_SPEED_16GB|
2376 		    FDMI_PORT_SPEED_8GB);
2377 	else if (IS_QLA2031(ha))
2378 		eiter->a.sup_speed = cpu_to_be32(
2379 		    FDMI_PORT_SPEED_16GB|
2380 		    FDMI_PORT_SPEED_8GB|
2381 		    FDMI_PORT_SPEED_4GB);
2382 	else if (IS_QLA25XX(ha))
2383 		eiter->a.sup_speed = cpu_to_be32(
2384 		    FDMI_PORT_SPEED_8GB|
2385 		    FDMI_PORT_SPEED_4GB|
2386 		    FDMI_PORT_SPEED_2GB|
2387 		    FDMI_PORT_SPEED_1GB);
2388 	else if (IS_QLA24XX_TYPE(ha))
2389 		eiter->a.sup_speed = cpu_to_be32(
2390 		    FDMI_PORT_SPEED_4GB|
2391 		    FDMI_PORT_SPEED_2GB|
2392 		    FDMI_PORT_SPEED_1GB);
2393 	else if (IS_QLA23XX(ha))
2394 		eiter->a.sup_speed = cpu_to_be32(
2395 		    FDMI_PORT_SPEED_2GB|
2396 		    FDMI_PORT_SPEED_1GB);
2397 	else
2398 		eiter->a.sup_speed = cpu_to_be32(
2399 		    FDMI_PORT_SPEED_1GB);
2400 	size += 4 + 4;
2401 
2402 	ql_dbg(ql_dbg_disc, vha, 0x20bb,
2403 	    "Supported Port Speed = %x.\n", eiter->a.sup_speed);
2404 
2405 	/* Current speed. */
2406 	eiter = entries + size;
2407 	eiter->type = cpu_to_be16(FDMI_PORT_CURRENT_SPEED);
2408 	eiter->len = cpu_to_be16(4 + 4);
2409 	switch (ha->link_data_rate) {
2410 	case PORT_SPEED_1GB:
2411 		eiter->a.cur_speed = cpu_to_be32(FDMI_PORT_SPEED_1GB);
2412 		break;
2413 	case PORT_SPEED_2GB:
2414 		eiter->a.cur_speed = cpu_to_be32(FDMI_PORT_SPEED_2GB);
2415 		break;
2416 	case PORT_SPEED_4GB:
2417 		eiter->a.cur_speed = cpu_to_be32(FDMI_PORT_SPEED_4GB);
2418 		break;
2419 	case PORT_SPEED_8GB:
2420 		eiter->a.cur_speed = cpu_to_be32(FDMI_PORT_SPEED_8GB);
2421 		break;
2422 	case PORT_SPEED_10GB:
2423 		eiter->a.cur_speed = cpu_to_be32(FDMI_PORT_SPEED_10GB);
2424 		break;
2425 	case PORT_SPEED_16GB:
2426 		eiter->a.cur_speed = cpu_to_be32(FDMI_PORT_SPEED_16GB);
2427 		break;
2428 	case PORT_SPEED_32GB:
2429 		eiter->a.cur_speed = cpu_to_be32(FDMI_PORT_SPEED_32GB);
2430 		break;
2431 	default:
2432 		eiter->a.cur_speed = cpu_to_be32(FDMI_PORT_SPEED_UNKNOWN);
2433 		break;
2434 	}
2435 	size += 4 + 4;
2436 
2437 	ql_dbg(ql_dbg_disc, vha, 0x2017,
2438 	    "Current_Speed = %x.\n", eiter->a.cur_speed);
2439 
2440 	/* Max frame size. */
2441 	eiter = entries + size;
2442 	eiter->type = cpu_to_be16(FDMI_PORT_MAX_FRAME_SIZE);
2443 	eiter->len = cpu_to_be16(4 + 4);
2444 	eiter->a.max_frame_size = IS_FWI2_CAPABLE(ha) ?
2445 	    le16_to_cpu(icb24->frame_payload_size):
2446 	    le16_to_cpu(ha->init_cb->frame_payload_size);
2447 	eiter->a.max_frame_size = cpu_to_be32(eiter->a.max_frame_size);
2448 	size += 4 + 4;
2449 
2450 	ql_dbg(ql_dbg_disc, vha, 0x20bc,
2451 	    "Max_Frame_Size = %x.\n", eiter->a.max_frame_size);
2452 
2453 	/* OS device name. */
2454 	eiter = entries + size;
2455 	eiter->type = cpu_to_be16(FDMI_PORT_OS_DEVICE_NAME);
2456 	alen = strlen(QLA2XXX_DRIVER_NAME);
2457 	snprintf(eiter->a.os_dev_name, sizeof(eiter->a.os_dev_name),
2458 	    "%s:host%lu", QLA2XXX_DRIVER_NAME, vha->host_no);
2459 	alen += 4 - (alen & 3);
2460 	eiter->len = cpu_to_be16(4 + alen);
2461 	size += 4 + alen;
2462 
2463 	ql_dbg(ql_dbg_disc, vha, 0x20be,
2464 	    "OS_Device_Name = %s.\n", eiter->a.os_dev_name);
2465 
2466 	/* Hostname. */
2467 	eiter = entries + size;
2468 	eiter->type = cpu_to_be16(FDMI_PORT_HOST_NAME);
2469 	p_sysid = utsname();
2470 	if (p_sysid) {
2471 		snprintf(eiter->a.host_name, sizeof(eiter->a.host_name),
2472 		    "%s", p_sysid->nodename);
2473 	} else {
2474 		snprintf(eiter->a.host_name, sizeof(eiter->a.host_name),
2475 		    "%s", fc_host_system_hostname(vha->host));
2476 	}
2477 	alen = strlen(eiter->a.host_name);
2478 	alen += 4 - (alen & 3);
2479 	eiter->len = cpu_to_be16(4 + alen);
2480 	size += 4 + alen;
2481 
2482 	ql_dbg(ql_dbg_disc, vha, 0x201a,
2483 	    "HostName=%s.\n", eiter->a.host_name);
2484 
2485 	/* Node Name */
2486 	eiter = entries + size;
2487 	eiter->type = cpu_to_be16(FDMI_PORT_NODE_NAME);
2488 	memcpy(eiter->a.node_name, vha->node_name, WWN_SIZE);
2489 	eiter->len = cpu_to_be16(4 + WWN_SIZE);
2490 	size += 4 + WWN_SIZE;
2491 
2492 	ql_dbg(ql_dbg_disc, vha, 0x20c0,
2493 	    "Node Name = %016llx.\n", wwn_to_u64(eiter->a.node_name));
2494 
2495 	/* Port Name */
2496 	eiter = entries + size;
2497 	eiter->type = cpu_to_be16(FDMI_PORT_NAME);
2498 	memcpy(eiter->a.port_name, vha->port_name, WWN_SIZE);
2499 	eiter->len = cpu_to_be16(4 + WWN_SIZE);
2500 	size += 4 + WWN_SIZE;
2501 
2502 	ql_dbg(ql_dbg_disc, vha, 0x20c1,
2503 	    "Port Name = %016llx.\n", wwn_to_u64(eiter->a.port_name));
2504 
2505 	/* Port Symbolic Name */
2506 	eiter = entries + size;
2507 	eiter->type = cpu_to_be16(FDMI_PORT_SYM_NAME);
2508 	qla2x00_get_sym_node_name(vha, eiter->a.port_sym_name,
2509 	    sizeof(eiter->a.port_sym_name));
2510 	alen = strlen(eiter->a.port_sym_name);
2511 	alen += 4 - (alen & 3);
2512 	eiter->len = cpu_to_be16(4 + alen);
2513 	size += 4 + alen;
2514 
2515 	ql_dbg(ql_dbg_disc, vha, 0x20c2,
2516 	    "port symbolic name = %s\n", eiter->a.port_sym_name);
2517 
2518 	/* Port Type */
2519 	eiter = entries + size;
2520 	eiter->type = cpu_to_be16(FDMI_PORT_TYPE);
2521 	eiter->a.port_type = cpu_to_be32(NS_NX_PORT_TYPE);
2522 	eiter->len = cpu_to_be16(4 + 4);
2523 	size += 4 + 4;
2524 
2525 	ql_dbg(ql_dbg_disc, vha, 0x20c3,
2526 	    "Port Type = %x.\n", eiter->a.port_type);
2527 
2528 	/* Class of Service  */
2529 	eiter = entries + size;
2530 	eiter->type = cpu_to_be16(FDMI_PORT_SUPP_COS);
2531 	eiter->a.port_supported_cos = cpu_to_be32(FC_CLASS_3);
2532 	eiter->len = cpu_to_be16(4 + 4);
2533 	size += 4 + 4;
2534 
2535 	ql_dbg(ql_dbg_disc, vha, 0x20c4,
2536 	    "Supported COS = %08x\n", eiter->a.port_supported_cos);
2537 
2538 	/* Port Fabric Name */
2539 	eiter = entries + size;
2540 	eiter->type = cpu_to_be16(FDMI_PORT_FABRIC_NAME);
2541 	memcpy(eiter->a.fabric_name, vha->fabric_node_name, WWN_SIZE);
2542 	eiter->len = cpu_to_be16(4 + WWN_SIZE);
2543 	size += 4 + WWN_SIZE;
2544 
2545 	ql_dbg(ql_dbg_disc, vha, 0x20c5,
2546 	    "Fabric Name = %016llx.\n", wwn_to_u64(eiter->a.fabric_name));
2547 
2548 	/* FC4_type */
2549 	eiter = entries + size;
2550 	eiter->type = cpu_to_be16(FDMI_PORT_FC4_TYPE);
2551 	eiter->a.port_fc4_type[0] = 0;
2552 	eiter->a.port_fc4_type[1] = 0;
2553 	eiter->a.port_fc4_type[2] = 1;
2554 	eiter->a.port_fc4_type[3] = 0;
2555 	eiter->len = cpu_to_be16(4 + 32);
2556 	size += 4 + 32;
2557 
2558 	ql_dbg(ql_dbg_disc, vha, 0x20c6,
2559 	    "Port Active FC4 Type = %02x %02x.\n",
2560 	    eiter->a.port_fc4_type[2], eiter->a.port_fc4_type[1]);
2561 
2562 	if (vha->flags.nvme_enabled) {
2563 		eiter->a.port_fc4_type[4] = 0;
2564 		eiter->a.port_fc4_type[5] = 0;
2565 		eiter->a.port_fc4_type[6] = 1;	/* NVMe type 28h */
2566 		ql_dbg(ql_dbg_disc, vha, 0x2120,
2567 		    "NVME Port Active FC4 Type = %02x 0x0 0x0 0x0 0x0 0x0.\n",
2568 		    eiter->a.port_fc4_type[6]);
2569 	}
2570 
2571 	/* Port State */
2572 	eiter = entries + size;
2573 	eiter->type = cpu_to_be16(FDMI_PORT_STATE);
2574 	eiter->a.port_state = cpu_to_be32(1);
2575 	eiter->len = cpu_to_be16(4 + 4);
2576 	size += 4 + 4;
2577 
2578 	ql_dbg(ql_dbg_disc, vha, 0x20c7,
2579 	    "Port State = %x.\n", eiter->a.port_state);
2580 
2581 	/* Number of Ports */
2582 	eiter = entries + size;
2583 	eiter->type = cpu_to_be16(FDMI_PORT_COUNT);
2584 	eiter->a.num_ports = cpu_to_be32(1);
2585 	eiter->len = cpu_to_be16(4 + 4);
2586 	size += 4 + 4;
2587 
2588 	ql_dbg(ql_dbg_disc, vha, 0x20c8,
2589 	    "Number of ports = %x.\n", eiter->a.num_ports);
2590 
2591 	/* Port Id */
2592 	eiter = entries + size;
2593 	eiter->type = cpu_to_be16(FDMI_PORT_ID);
2594 	eiter->a.port_id = cpu_to_be32(vha->d_id.b24);
2595 	eiter->len = cpu_to_be16(4 + 4);
2596 	size += 4 + 4;
2597 
2598 	ql_dbg(ql_dbg_disc, vha, 0x201c,
2599 	    "Port Id = %x.\n", eiter->a.port_id);
2600 
2601 	/* Update MS request size. */
2602 	qla2x00_update_ms_fdmi_iocb(vha, size + 16);
2603 
2604 	ql_dbg(ql_dbg_disc, vha, 0x2018,
2605 	    "RPA portname= %8phN size=%d.\n", ct_req->req.rpa.port_name, size);
2606 	ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x20ca,
2607 	    entries, size);
2608 
2609 	/* Execute MS IOCB */
2610 	rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
2611 	    sizeof(ms_iocb_entry_t));
2612 	if (rval != QLA_SUCCESS) {
2613 		/*EMPTY*/
2614 		ql_dbg(ql_dbg_disc, vha, 0x20cb,
2615 		    "RPA FDMI v2 issue IOCB failed (%d).\n", rval);
2616 	} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp, "RPA") !=
2617 	    QLA_SUCCESS) {
2618 		rval = QLA_FUNCTION_FAILED;
2619 		if (ct_rsp->header.reason_code == CT_REASON_CANNOT_PERFORM &&
2620 		    ct_rsp->header.explanation_code ==
2621 		    CT_EXPL_ALREADY_REGISTERED) {
2622 			ql_dbg(ql_dbg_disc, vha, 0x20ce,
2623 			    "RPA FDMI v2 already registered\n");
2624 			rval = QLA_ALREADY_REGISTERED;
2625 		} else {
2626 			ql_dbg(ql_dbg_disc, vha, 0x2020,
2627 			    "RPA FDMI v2 failed, CT Reason code: 0x%x, CT Explanation 0x%x\n",
2628 			    ct_rsp->header.reason_code,
2629 			    ct_rsp->header.explanation_code);
2630 		}
2631 	} else {
2632 		ql_dbg(ql_dbg_disc, vha, 0x20cc,
2633 		    "RPA FDMI V2 exiting normally.\n");
2634 	}
2635 
2636 	return rval;
2637 }
2638 
2639 /**
2640  * qla2x00_fdmi_register() -
2641  * @vha: HA context
2642  *
2643  * Returns 0 on success.
2644  */
2645 int
2646 qla2x00_fdmi_register(scsi_qla_host_t *vha)
2647 {
2648 	int rval = QLA_FUNCTION_FAILED;
2649 	struct qla_hw_data *ha = vha->hw;
2650 
2651 	if (IS_QLA2100(ha) || IS_QLA2200(ha) ||
2652 	    IS_QLAFX00(ha))
2653 		return QLA_FUNCTION_FAILED;
2654 
2655 	rval = qla2x00_mgmt_svr_login(vha);
2656 	if (rval)
2657 		return rval;
2658 
2659 	rval = qla2x00_fdmiv2_rhba(vha);
2660 	if (rval) {
2661 		if (rval != QLA_ALREADY_REGISTERED)
2662 			goto try_fdmi;
2663 
2664 		rval = qla2x00_fdmi_dhba(vha);
2665 		if (rval)
2666 			goto try_fdmi;
2667 
2668 		rval = qla2x00_fdmiv2_rhba(vha);
2669 		if (rval)
2670 			goto try_fdmi;
2671 	}
2672 	rval = qla2x00_fdmiv2_rpa(vha);
2673 	if (rval)
2674 		goto try_fdmi;
2675 
2676 	goto out;
2677 
2678 try_fdmi:
2679 	rval = qla2x00_fdmi_rhba(vha);
2680 	if (rval) {
2681 		if (rval != QLA_ALREADY_REGISTERED)
2682 			return rval;
2683 
2684 		rval = qla2x00_fdmi_dhba(vha);
2685 		if (rval)
2686 			return rval;
2687 
2688 		rval = qla2x00_fdmi_rhba(vha);
2689 		if (rval)
2690 			return rval;
2691 	}
2692 	rval = qla2x00_fdmi_rpa(vha);
2693 out:
2694 	return rval;
2695 }
2696 
2697 /**
2698  * qla2x00_gfpn_id() - SNS Get Fabric Port Name (GFPN_ID) query.
2699  * @vha: HA context
2700  * @list: switch info entries to populate
2701  *
2702  * Returns 0 on success.
2703  */
2704 int
2705 qla2x00_gfpn_id(scsi_qla_host_t *vha, sw_info_t *list)
2706 {
2707 	int		rval = QLA_SUCCESS;
2708 	uint16_t	i;
2709 	struct qla_hw_data *ha = vha->hw;
2710 	ms_iocb_entry_t	*ms_pkt;
2711 	struct ct_sns_req	*ct_req;
2712 	struct ct_sns_rsp	*ct_rsp;
2713 	struct ct_arg arg;
2714 
2715 	if (!IS_IIDMA_CAPABLE(ha))
2716 		return QLA_FUNCTION_FAILED;
2717 
2718 	arg.iocb = ha->ms_iocb;
2719 	arg.req_dma = ha->ct_sns_dma;
2720 	arg.rsp_dma = ha->ct_sns_dma;
2721 	arg.req_size = GFPN_ID_REQ_SIZE;
2722 	arg.rsp_size = GFPN_ID_RSP_SIZE;
2723 	arg.nport_handle = NPH_SNS;
2724 
2725 	for (i = 0; i < ha->max_fibre_devices; i++) {
2726 		/* Issue GFPN_ID */
2727 		/* Prepare common MS IOCB */
2728 		ms_pkt = ha->isp_ops->prep_ms_iocb(vha, &arg);
2729 
2730 		/* Prepare CT request */
2731 		ct_req = qla2x00_prep_ct_req(ha->ct_sns, GFPN_ID_CMD,
2732 		    GFPN_ID_RSP_SIZE);
2733 		ct_rsp = &ha->ct_sns->p.rsp;
2734 
2735 		/* Prepare CT arguments -- port_id */
2736 		ct_req->req.port_id.port_id[0] = list[i].d_id.b.domain;
2737 		ct_req->req.port_id.port_id[1] = list[i].d_id.b.area;
2738 		ct_req->req.port_id.port_id[2] = list[i].d_id.b.al_pa;
2739 
2740 		/* Execute MS IOCB */
2741 		rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
2742 		    sizeof(ms_iocb_entry_t));
2743 		if (rval != QLA_SUCCESS) {
2744 			/*EMPTY*/
2745 			ql_dbg(ql_dbg_disc, vha, 0x2023,
2746 			    "GFPN_ID issue IOCB failed (%d).\n", rval);
2747 			break;
2748 		} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp,
2749 		    "GFPN_ID") != QLA_SUCCESS) {
2750 			rval = QLA_FUNCTION_FAILED;
2751 			break;
2752 		} else {
2753 			/* Save fabric portname */
2754 			memcpy(list[i].fabric_port_name,
2755 			    ct_rsp->rsp.gfpn_id.port_name, WWN_SIZE);
2756 		}
2757 
2758 		/* Last device exit. */
2759 		if (list[i].d_id.b.rsvd_1 != 0)
2760 			break;
2761 	}
2762 
2763 	return (rval);
2764 }
2765 
2766 
2767 static inline struct ct_sns_req *
2768 qla24xx_prep_ct_fm_req(struct ct_sns_pkt *p, uint16_t cmd,
2769     uint16_t rsp_size)
2770 {
2771 	memset(p, 0, sizeof(struct ct_sns_pkt));
2772 
2773 	p->p.req.header.revision = 0x01;
2774 	p->p.req.header.gs_type = 0xFA;
2775 	p->p.req.header.gs_subtype = 0x01;
2776 	p->p.req.command = cpu_to_be16(cmd);
2777 	p->p.req.max_rsp_size = cpu_to_be16((rsp_size - 16) / 4);
2778 
2779 	return &p->p.req;
2780 }
2781 
2782 /**
2783  * qla2x00_gpsc() - FCS Get Port Speed Capabilities (GPSC) query.
2784  * @vha: HA context
2785  * @list: switch info entries to populate
2786  *
2787  * Returns 0 on success.
2788  */
2789 int
2790 qla2x00_gpsc(scsi_qla_host_t *vha, sw_info_t *list)
2791 {
2792 	int		rval;
2793 	uint16_t	i;
2794 	struct qla_hw_data *ha = vha->hw;
2795 	ms_iocb_entry_t *ms_pkt;
2796 	struct ct_sns_req	*ct_req;
2797 	struct ct_sns_rsp	*ct_rsp;
2798 	struct ct_arg arg;
2799 
2800 	if (!IS_IIDMA_CAPABLE(ha))
2801 		return QLA_FUNCTION_FAILED;
2802 	if (!ha->flags.gpsc_supported)
2803 		return QLA_FUNCTION_FAILED;
2804 
2805 	rval = qla2x00_mgmt_svr_login(vha);
2806 	if (rval)
2807 		return rval;
2808 
2809 	arg.iocb = ha->ms_iocb;
2810 	arg.req_dma = ha->ct_sns_dma;
2811 	arg.rsp_dma = ha->ct_sns_dma;
2812 	arg.req_size = GPSC_REQ_SIZE;
2813 	arg.rsp_size = GPSC_RSP_SIZE;
2814 	arg.nport_handle = vha->mgmt_svr_loop_id;
2815 
2816 	for (i = 0; i < ha->max_fibre_devices; i++) {
2817 		/* Issue GFPN_ID */
2818 		/* Prepare common MS IOCB */
2819 		ms_pkt = qla24xx_prep_ms_iocb(vha, &arg);
2820 
2821 		/* Prepare CT request */
2822 		ct_req = qla24xx_prep_ct_fm_req(ha->ct_sns, GPSC_CMD,
2823 		    GPSC_RSP_SIZE);
2824 		ct_rsp = &ha->ct_sns->p.rsp;
2825 
2826 		/* Prepare CT arguments -- port_name */
2827 		memcpy(ct_req->req.gpsc.port_name, list[i].fabric_port_name,
2828 		    WWN_SIZE);
2829 
2830 		/* Execute MS IOCB */
2831 		rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
2832 		    sizeof(ms_iocb_entry_t));
2833 		if (rval != QLA_SUCCESS) {
2834 			/*EMPTY*/
2835 			ql_dbg(ql_dbg_disc, vha, 0x2059,
2836 			    "GPSC issue IOCB failed (%d).\n", rval);
2837 		} else if ((rval = qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp,
2838 		    "GPSC")) != QLA_SUCCESS) {
2839 			/* FM command unsupported? */
2840 			if (rval == QLA_INVALID_COMMAND &&
2841 			    (ct_rsp->header.reason_code ==
2842 				CT_REASON_INVALID_COMMAND_CODE ||
2843 			     ct_rsp->header.reason_code ==
2844 				CT_REASON_COMMAND_UNSUPPORTED)) {
2845 				ql_dbg(ql_dbg_disc, vha, 0x205a,
2846 				    "GPSC command unsupported, disabling "
2847 				    "query.\n");
2848 				ha->flags.gpsc_supported = 0;
2849 				rval = QLA_FUNCTION_FAILED;
2850 				break;
2851 			}
2852 			rval = QLA_FUNCTION_FAILED;
2853 		} else {
2854 			/* Save port-speed */
2855 			switch (be16_to_cpu(ct_rsp->rsp.gpsc.speed)) {
2856 			case BIT_15:
2857 				list[i].fp_speed = PORT_SPEED_1GB;
2858 				break;
2859 			case BIT_14:
2860 				list[i].fp_speed = PORT_SPEED_2GB;
2861 				break;
2862 			case BIT_13:
2863 				list[i].fp_speed = PORT_SPEED_4GB;
2864 				break;
2865 			case BIT_12:
2866 				list[i].fp_speed = PORT_SPEED_10GB;
2867 				break;
2868 			case BIT_11:
2869 				list[i].fp_speed = PORT_SPEED_8GB;
2870 				break;
2871 			case BIT_10:
2872 				list[i].fp_speed = PORT_SPEED_16GB;
2873 				break;
2874 			case BIT_8:
2875 				list[i].fp_speed = PORT_SPEED_32GB;
2876 				break;
2877 			}
2878 
2879 			ql_dbg(ql_dbg_disc, vha, 0x205b,
2880 			    "GPSC ext entry - fpn "
2881 			    "%8phN speeds=%04x speed=%04x.\n",
2882 			    list[i].fabric_port_name,
2883 			    be16_to_cpu(ct_rsp->rsp.gpsc.speeds),
2884 			    be16_to_cpu(ct_rsp->rsp.gpsc.speed));
2885 		}
2886 
2887 		/* Last device exit. */
2888 		if (list[i].d_id.b.rsvd_1 != 0)
2889 			break;
2890 	}
2891 
2892 	return (rval);
2893 }
2894 
2895 /**
2896  * qla2x00_gff_id() - SNS Get FC-4 Features (GFF_ID) query.
2897  *
2898  * @vha: HA context
2899  * @list: switch info entries to populate
2900  *
2901  */
2902 void
2903 qla2x00_gff_id(scsi_qla_host_t *vha, sw_info_t *list)
2904 {
2905 	int		rval;
2906 	uint16_t	i;
2907 
2908 	ms_iocb_entry_t	*ms_pkt;
2909 	struct ct_sns_req	*ct_req;
2910 	struct ct_sns_rsp	*ct_rsp;
2911 	struct qla_hw_data *ha = vha->hw;
2912 	uint8_t fcp_scsi_features = 0;
2913 	struct ct_arg arg;
2914 
2915 	for (i = 0; i < ha->max_fibre_devices; i++) {
2916 		/* Set default FC4 Type as UNKNOWN so the default is to
2917 		 * Process this port */
2918 		list[i].fc4_type = FC4_TYPE_UNKNOWN;
2919 
2920 		/* Do not attempt GFF_ID if we are not FWI_2 capable */
2921 		if (!IS_FWI2_CAPABLE(ha))
2922 			continue;
2923 
2924 		arg.iocb = ha->ms_iocb;
2925 		arg.req_dma = ha->ct_sns_dma;
2926 		arg.rsp_dma = ha->ct_sns_dma;
2927 		arg.req_size = GFF_ID_REQ_SIZE;
2928 		arg.rsp_size = GFF_ID_RSP_SIZE;
2929 		arg.nport_handle = NPH_SNS;
2930 
2931 		/* Prepare common MS IOCB */
2932 		ms_pkt = ha->isp_ops->prep_ms_iocb(vha, &arg);
2933 
2934 		/* Prepare CT request */
2935 		ct_req = qla2x00_prep_ct_req(ha->ct_sns, GFF_ID_CMD,
2936 		    GFF_ID_RSP_SIZE);
2937 		ct_rsp = &ha->ct_sns->p.rsp;
2938 
2939 		/* Prepare CT arguments -- port_id */
2940 		ct_req->req.port_id.port_id[0] = list[i].d_id.b.domain;
2941 		ct_req->req.port_id.port_id[1] = list[i].d_id.b.area;
2942 		ct_req->req.port_id.port_id[2] = list[i].d_id.b.al_pa;
2943 
2944 		/* Execute MS IOCB */
2945 		rval = qla2x00_issue_iocb(vha, ha->ms_iocb, ha->ms_iocb_dma,
2946 		   sizeof(ms_iocb_entry_t));
2947 
2948 		if (rval != QLA_SUCCESS) {
2949 			ql_dbg(ql_dbg_disc, vha, 0x205c,
2950 			    "GFF_ID issue IOCB failed (%d).\n", rval);
2951 		} else if (qla2x00_chk_ms_status(vha, ms_pkt, ct_rsp,
2952 			       "GFF_ID") != QLA_SUCCESS) {
2953 			ql_dbg(ql_dbg_disc, vha, 0x205d,
2954 			    "GFF_ID IOCB status had a failure status code.\n");
2955 		} else {
2956 			fcp_scsi_features =
2957 			   ct_rsp->rsp.gff_id.fc4_features[GFF_FCP_SCSI_OFFSET];
2958 			fcp_scsi_features &= 0x0f;
2959 
2960 			if (fcp_scsi_features)
2961 				list[i].fc4_type = FC4_TYPE_FCP_SCSI;
2962 			else
2963 				list[i].fc4_type = FC4_TYPE_OTHER;
2964 
2965 			list[i].fc4f_nvme =
2966 			    ct_rsp->rsp.gff_id.fc4_features[GFF_NVME_OFFSET];
2967 			list[i].fc4f_nvme &= 0xf;
2968 		}
2969 
2970 		/* Last device exit. */
2971 		if (list[i].d_id.b.rsvd_1 != 0)
2972 			break;
2973 	}
2974 }
2975 
2976 int qla24xx_post_gpsc_work(struct scsi_qla_host *vha, fc_port_t *fcport)
2977 {
2978 	struct qla_work_evt *e;
2979 
2980 	e = qla2x00_alloc_work(vha, QLA_EVT_GPSC);
2981 	if (!e)
2982 		return QLA_FUNCTION_FAILED;
2983 
2984 	e->u.fcport.fcport = fcport;
2985 	fcport->flags |= FCF_ASYNC_ACTIVE;
2986 	return qla2x00_post_work(vha, e);
2987 }
2988 
2989 void qla24xx_handle_gpsc_event(scsi_qla_host_t *vha, struct event_arg *ea)
2990 {
2991 	struct fc_port *fcport = ea->fcport;
2992 
2993 	ql_dbg(ql_dbg_disc, vha, 0x20d8,
2994 	    "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
2995 	    __func__, fcport->port_name, fcport->disc_state,
2996 	    fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
2997 	    ea->sp->gen2, fcport->rscn_gen|ea->sp->gen1, fcport->loop_id);
2998 
2999 	if (fcport->disc_state == DSC_DELETE_PEND)
3000 		return;
3001 
3002 	if (ea->sp->gen2 != fcport->login_gen) {
3003 		/* target side must have changed it. */
3004 		ql_dbg(ql_dbg_disc, vha, 0x20d3,
3005 		    "%s %8phC generation changed\n",
3006 		    __func__, fcport->port_name);
3007 		return;
3008 	} else if (ea->sp->gen1 != fcport->rscn_gen) {
3009 		return;
3010 	}
3011 
3012 	qla_post_iidma_work(vha, fcport);
3013 }
3014 
3015 static void qla24xx_async_gpsc_sp_done(void *s, int res)
3016 {
3017 	struct srb *sp = s;
3018 	struct scsi_qla_host *vha = sp->vha;
3019 	struct qla_hw_data *ha = vha->hw;
3020 	fc_port_t *fcport = sp->fcport;
3021 	struct ct_sns_rsp       *ct_rsp;
3022 	struct event_arg ea;
3023 
3024 	ct_rsp = &fcport->ct_desc.ct_sns->p.rsp;
3025 
3026 	ql_dbg(ql_dbg_disc, vha, 0x2053,
3027 	    "Async done-%s res %x, WWPN %8phC \n",
3028 	    sp->name, res, fcport->port_name);
3029 
3030 	if (res == QLA_FUNCTION_TIMEOUT)
3031 		return;
3032 
3033 	if (res == (DID_ERROR << 16)) {
3034 		/* entry status error */
3035 		goto done;
3036 	} else if (res) {
3037 		if ((ct_rsp->header.reason_code ==
3038 			 CT_REASON_INVALID_COMMAND_CODE) ||
3039 			(ct_rsp->header.reason_code ==
3040 			CT_REASON_COMMAND_UNSUPPORTED)) {
3041 			ql_dbg(ql_dbg_disc, vha, 0x2019,
3042 			    "GPSC command unsupported, disabling query.\n");
3043 			ha->flags.gpsc_supported = 0;
3044 			goto done;
3045 		}
3046 	} else {
3047 		switch (be16_to_cpu(ct_rsp->rsp.gpsc.speed)) {
3048 		case BIT_15:
3049 			fcport->fp_speed = PORT_SPEED_1GB;
3050 			break;
3051 		case BIT_14:
3052 			fcport->fp_speed = PORT_SPEED_2GB;
3053 			break;
3054 		case BIT_13:
3055 			fcport->fp_speed = PORT_SPEED_4GB;
3056 			break;
3057 		case BIT_12:
3058 			fcport->fp_speed = PORT_SPEED_10GB;
3059 			break;
3060 		case BIT_11:
3061 			fcport->fp_speed = PORT_SPEED_8GB;
3062 			break;
3063 		case BIT_10:
3064 			fcport->fp_speed = PORT_SPEED_16GB;
3065 			break;
3066 		case BIT_8:
3067 			fcport->fp_speed = PORT_SPEED_32GB;
3068 			break;
3069 		}
3070 
3071 		ql_dbg(ql_dbg_disc, vha, 0x2054,
3072 		    "Async-%s OUT WWPN %8phC speeds=%04x speed=%04x.\n",
3073 		    sp->name, fcport->fabric_port_name,
3074 		    be16_to_cpu(ct_rsp->rsp.gpsc.speeds),
3075 		    be16_to_cpu(ct_rsp->rsp.gpsc.speed));
3076 	}
3077 	memset(&ea, 0, sizeof(ea));
3078 	ea.event = FCME_GPSC_DONE;
3079 	ea.rc = res;
3080 	ea.fcport = fcport;
3081 	ea.sp = sp;
3082 	qla2x00_fcport_event_handler(vha, &ea);
3083 
3084 done:
3085 	sp->free(sp);
3086 }
3087 
3088 int qla24xx_async_gpsc(scsi_qla_host_t *vha, fc_port_t *fcport)
3089 {
3090 	int rval = QLA_FUNCTION_FAILED;
3091 	struct ct_sns_req       *ct_req;
3092 	srb_t *sp;
3093 
3094 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
3095 		return rval;
3096 
3097 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
3098 	if (!sp)
3099 		goto done;
3100 
3101 	sp->type = SRB_CT_PTHRU_CMD;
3102 	sp->name = "gpsc";
3103 	sp->gen1 = fcport->rscn_gen;
3104 	sp->gen2 = fcport->login_gen;
3105 
3106 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
3107 
3108 	/* CT_IU preamble  */
3109 	ct_req = qla24xx_prep_ct_fm_req(fcport->ct_desc.ct_sns, GPSC_CMD,
3110 		GPSC_RSP_SIZE);
3111 
3112 	/* GPSC req */
3113 	memcpy(ct_req->req.gpsc.port_name, fcport->fabric_port_name,
3114 		WWN_SIZE);
3115 
3116 	sp->u.iocb_cmd.u.ctarg.req = fcport->ct_desc.ct_sns;
3117 	sp->u.iocb_cmd.u.ctarg.req_dma = fcport->ct_desc.ct_sns_dma;
3118 	sp->u.iocb_cmd.u.ctarg.rsp = fcport->ct_desc.ct_sns;
3119 	sp->u.iocb_cmd.u.ctarg.rsp_dma = fcport->ct_desc.ct_sns_dma;
3120 	sp->u.iocb_cmd.u.ctarg.req_size = GPSC_REQ_SIZE;
3121 	sp->u.iocb_cmd.u.ctarg.rsp_size = GPSC_RSP_SIZE;
3122 	sp->u.iocb_cmd.u.ctarg.nport_handle = vha->mgmt_svr_loop_id;
3123 
3124 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
3125 	sp->done = qla24xx_async_gpsc_sp_done;
3126 
3127 	ql_dbg(ql_dbg_disc, vha, 0x205e,
3128 	    "Async-%s %8phC hdl=%x loopid=%x portid=%02x%02x%02x.\n",
3129 	    sp->name, fcport->port_name, sp->handle,
3130 	    fcport->loop_id, fcport->d_id.b.domain,
3131 	    fcport->d_id.b.area, fcport->d_id.b.al_pa);
3132 
3133 	rval = qla2x00_start_sp(sp);
3134 	if (rval != QLA_SUCCESS)
3135 		goto done_free_sp;
3136 	return rval;
3137 
3138 done_free_sp:
3139 	sp->free(sp);
3140 	fcport->flags &= ~FCF_ASYNC_SENT;
3141 done:
3142 	fcport->flags &= ~FCF_ASYNC_ACTIVE;
3143 	return rval;
3144 }
3145 
3146 int qla24xx_post_gpnid_work(struct scsi_qla_host *vha, port_id_t *id)
3147 {
3148 	struct qla_work_evt *e;
3149 
3150 	if (test_bit(UNLOADING, &vha->dpc_flags))
3151 		return 0;
3152 
3153 	e = qla2x00_alloc_work(vha, QLA_EVT_GPNID);
3154 	if (!e)
3155 		return QLA_FUNCTION_FAILED;
3156 
3157 	e->u.gpnid.id = *id;
3158 	return qla2x00_post_work(vha, e);
3159 }
3160 
3161 void qla24xx_sp_unmap(scsi_qla_host_t *vha, srb_t *sp)
3162 {
3163 	struct srb_iocb *c = &sp->u.iocb_cmd;
3164 
3165 	switch (sp->type) {
3166 	case SRB_ELS_DCMD:
3167 		if (c->u.els_plogi.els_plogi_pyld)
3168 			dma_free_coherent(&vha->hw->pdev->dev,
3169 			    c->u.els_plogi.tx_size,
3170 			    c->u.els_plogi.els_plogi_pyld,
3171 			    c->u.els_plogi.els_plogi_pyld_dma);
3172 
3173 		if (c->u.els_plogi.els_resp_pyld)
3174 			dma_free_coherent(&vha->hw->pdev->dev,
3175 			    c->u.els_plogi.rx_size,
3176 			    c->u.els_plogi.els_resp_pyld,
3177 			    c->u.els_plogi.els_resp_pyld_dma);
3178 		break;
3179 	case SRB_CT_PTHRU_CMD:
3180 	default:
3181 		if (sp->u.iocb_cmd.u.ctarg.req) {
3182 			dma_free_coherent(&vha->hw->pdev->dev,
3183 			    sp->u.iocb_cmd.u.ctarg.req_allocated_size,
3184 			    sp->u.iocb_cmd.u.ctarg.req,
3185 			    sp->u.iocb_cmd.u.ctarg.req_dma);
3186 			sp->u.iocb_cmd.u.ctarg.req = NULL;
3187 		}
3188 
3189 		if (sp->u.iocb_cmd.u.ctarg.rsp) {
3190 			dma_free_coherent(&vha->hw->pdev->dev,
3191 			    sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
3192 			    sp->u.iocb_cmd.u.ctarg.rsp,
3193 			    sp->u.iocb_cmd.u.ctarg.rsp_dma);
3194 			sp->u.iocb_cmd.u.ctarg.rsp = NULL;
3195 		}
3196 		break;
3197 	}
3198 
3199 	sp->free(sp);
3200 }
3201 
3202 void qla24xx_handle_gpnid_event(scsi_qla_host_t *vha, struct event_arg *ea)
3203 {
3204 	fc_port_t *fcport, *conflict, *t;
3205 	u16 data[2];
3206 
3207 	ql_dbg(ql_dbg_disc, vha, 0xffff,
3208 	    "%s %d port_id: %06x\n",
3209 	    __func__, __LINE__, ea->id.b24);
3210 
3211 	if (ea->rc) {
3212 		/* cable is disconnected */
3213 		list_for_each_entry_safe(fcport, t, &vha->vp_fcports, list) {
3214 			if (fcport->d_id.b24 == ea->id.b24)
3215 				fcport->scan_state = QLA_FCPORT_SCAN;
3216 
3217 			qlt_schedule_sess_for_deletion(fcport);
3218 		}
3219 	} else {
3220 		/* cable is connected */
3221 		fcport = qla2x00_find_fcport_by_wwpn(vha, ea->port_name, 1);
3222 		if (fcport) {
3223 			list_for_each_entry_safe(conflict, t, &vha->vp_fcports,
3224 			    list) {
3225 				if ((conflict->d_id.b24 == ea->id.b24) &&
3226 				    (fcport != conflict))
3227 					/*
3228 					 * 2 fcports with conflict Nport ID or
3229 					 * an existing fcport is having nport ID
3230 					 * conflict with new fcport.
3231 					 */
3232 
3233 					conflict->scan_state = QLA_FCPORT_SCAN;
3234 
3235 				qlt_schedule_sess_for_deletion(conflict);
3236 			}
3237 
3238 			fcport->scan_needed = 0;
3239 			fcport->rscn_gen++;
3240 			fcport->scan_state = QLA_FCPORT_FOUND;
3241 			fcport->flags |= FCF_FABRIC_DEVICE;
3242 			if (fcport->login_retry == 0) {
3243 				fcport->login_retry =
3244 					vha->hw->login_retry_count;
3245 				ql_dbg(ql_dbg_disc, vha, 0xffff,
3246 				    "Port login retry %8phN, lid 0x%04x cnt=%d.\n",
3247 				    fcport->port_name, fcport->loop_id,
3248 				    fcport->login_retry);
3249 			}
3250 			switch (fcport->disc_state) {
3251 			case DSC_LOGIN_COMPLETE:
3252 				/* recheck session is still intact. */
3253 				ql_dbg(ql_dbg_disc, vha, 0x210d,
3254 				    "%s %d %8phC revalidate session with ADISC\n",
3255 				    __func__, __LINE__, fcport->port_name);
3256 				data[0] = data[1] = 0;
3257 				qla2x00_post_async_adisc_work(vha, fcport,
3258 				    data);
3259 				break;
3260 			case DSC_DELETED:
3261 				ql_dbg(ql_dbg_disc, vha, 0x210d,
3262 				    "%s %d %8phC login\n", __func__, __LINE__,
3263 				    fcport->port_name);
3264 				fcport->d_id = ea->id;
3265 				qla24xx_fcport_handle_login(vha, fcport);
3266 				break;
3267 			case DSC_DELETE_PEND:
3268 				fcport->d_id = ea->id;
3269 				break;
3270 			default:
3271 				fcport->d_id = ea->id;
3272 				break;
3273 			}
3274 		} else {
3275 			list_for_each_entry_safe(conflict, t, &vha->vp_fcports,
3276 			    list) {
3277 				if (conflict->d_id.b24 == ea->id.b24) {
3278 					/* 2 fcports with conflict Nport ID or
3279 					 * an existing fcport is having nport ID
3280 					 * conflict with new fcport.
3281 					 */
3282 					ql_dbg(ql_dbg_disc, vha, 0xffff,
3283 					    "%s %d %8phC DS %d\n",
3284 					    __func__, __LINE__,
3285 					    conflict->port_name,
3286 					    conflict->disc_state);
3287 
3288 					conflict->scan_state = QLA_FCPORT_SCAN;
3289 					qlt_schedule_sess_for_deletion(conflict);
3290 				}
3291 			}
3292 
3293 			/* create new fcport */
3294 			ql_dbg(ql_dbg_disc, vha, 0x2065,
3295 			    "%s %d %8phC post new sess\n",
3296 			    __func__, __LINE__, ea->port_name);
3297 			qla24xx_post_newsess_work(vha, &ea->id,
3298 			    ea->port_name, NULL, NULL, FC4_TYPE_UNKNOWN);
3299 		}
3300 	}
3301 }
3302 
3303 static void qla2x00_async_gpnid_sp_done(void *s, int res)
3304 {
3305 	struct srb *sp = s;
3306 	struct scsi_qla_host *vha = sp->vha;
3307 	struct ct_sns_req *ct_req =
3308 	    (struct ct_sns_req *)sp->u.iocb_cmd.u.ctarg.req;
3309 	struct ct_sns_rsp *ct_rsp =
3310 	    (struct ct_sns_rsp *)sp->u.iocb_cmd.u.ctarg.rsp;
3311 	struct event_arg ea;
3312 	struct qla_work_evt *e;
3313 	unsigned long flags;
3314 
3315 	if (res)
3316 		ql_dbg(ql_dbg_disc, vha, 0x2066,
3317 		    "Async done-%s fail res %x rscn gen %d ID %3phC. %8phC\n",
3318 		    sp->name, res, sp->gen1, ct_req->req.port_id.port_id,
3319 		    ct_rsp->rsp.gpn_id.port_name);
3320 	else
3321 		ql_dbg(ql_dbg_disc, vha, 0x2066,
3322 		    "Async done-%s good rscn gen %d ID %3phC. %8phC\n",
3323 		    sp->name, sp->gen1, ct_req->req.port_id.port_id,
3324 		    ct_rsp->rsp.gpn_id.port_name);
3325 
3326 	memset(&ea, 0, sizeof(ea));
3327 	memcpy(ea.port_name, ct_rsp->rsp.gpn_id.port_name, WWN_SIZE);
3328 	ea.sp = sp;
3329 	ea.id.b.domain = ct_req->req.port_id.port_id[0];
3330 	ea.id.b.area = ct_req->req.port_id.port_id[1];
3331 	ea.id.b.al_pa = ct_req->req.port_id.port_id[2];
3332 	ea.rc = res;
3333 	ea.event = FCME_GPNID_DONE;
3334 
3335 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
3336 	list_del(&sp->elem);
3337 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
3338 
3339 	if (res) {
3340 		if (res == QLA_FUNCTION_TIMEOUT) {
3341 			qla24xx_post_gpnid_work(sp->vha, &ea.id);
3342 			sp->free(sp);
3343 			return;
3344 		}
3345 	} else if (sp->gen1) {
3346 		/* There was another RSCN for this Nport ID */
3347 		qla24xx_post_gpnid_work(sp->vha, &ea.id);
3348 		sp->free(sp);
3349 		return;
3350 	}
3351 
3352 	qla2x00_fcport_event_handler(vha, &ea);
3353 
3354 	e = qla2x00_alloc_work(vha, QLA_EVT_UNMAP);
3355 	if (!e) {
3356 		/* please ignore kernel warning. otherwise, we have mem leak. */
3357 		if (sp->u.iocb_cmd.u.ctarg.req) {
3358 			dma_free_coherent(&vha->hw->pdev->dev,
3359 				sp->u.iocb_cmd.u.ctarg.req_allocated_size,
3360 				sp->u.iocb_cmd.u.ctarg.req,
3361 				sp->u.iocb_cmd.u.ctarg.req_dma);
3362 			sp->u.iocb_cmd.u.ctarg.req = NULL;
3363 		}
3364 		if (sp->u.iocb_cmd.u.ctarg.rsp) {
3365 			dma_free_coherent(&vha->hw->pdev->dev,
3366 				sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
3367 				sp->u.iocb_cmd.u.ctarg.rsp,
3368 				sp->u.iocb_cmd.u.ctarg.rsp_dma);
3369 			sp->u.iocb_cmd.u.ctarg.rsp = NULL;
3370 		}
3371 
3372 		sp->free(sp);
3373 		return;
3374 	}
3375 
3376 	e->u.iosb.sp = sp;
3377 	qla2x00_post_work(vha, e);
3378 }
3379 
3380 /* Get WWPN with Nport ID. */
3381 int qla24xx_async_gpnid(scsi_qla_host_t *vha, port_id_t *id)
3382 {
3383 	int rval = QLA_FUNCTION_FAILED;
3384 	struct ct_sns_req       *ct_req;
3385 	srb_t *sp, *tsp;
3386 	struct ct_sns_pkt *ct_sns;
3387 	unsigned long flags;
3388 
3389 	if (!vha->flags.online)
3390 		goto done;
3391 
3392 	sp = qla2x00_get_sp(vha, NULL, GFP_KERNEL);
3393 	if (!sp)
3394 		goto done;
3395 
3396 	sp->type = SRB_CT_PTHRU_CMD;
3397 	sp->name = "gpnid";
3398 	sp->u.iocb_cmd.u.ctarg.id = *id;
3399 	sp->gen1 = 0;
3400 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
3401 
3402 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
3403 	list_for_each_entry(tsp, &vha->gpnid_list, elem) {
3404 		if (tsp->u.iocb_cmd.u.ctarg.id.b24 == id->b24) {
3405 			tsp->gen1++;
3406 			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
3407 			sp->free(sp);
3408 			goto done;
3409 		}
3410 	}
3411 	list_add_tail(&sp->elem, &vha->gpnid_list);
3412 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
3413 
3414 	sp->u.iocb_cmd.u.ctarg.req = dma_alloc_coherent(&vha->hw->pdev->dev,
3415 		sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.req_dma,
3416 		GFP_KERNEL);
3417 	sp->u.iocb_cmd.u.ctarg.req_allocated_size = sizeof(struct ct_sns_pkt);
3418 	if (!sp->u.iocb_cmd.u.ctarg.req) {
3419 		ql_log(ql_log_warn, vha, 0xd041,
3420 		    "Failed to allocate ct_sns request.\n");
3421 		goto done_free_sp;
3422 	}
3423 
3424 	sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
3425 		sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.rsp_dma,
3426 		GFP_KERNEL);
3427 	sp->u.iocb_cmd.u.ctarg.rsp_allocated_size = sizeof(struct ct_sns_pkt);
3428 	if (!sp->u.iocb_cmd.u.ctarg.rsp) {
3429 		ql_log(ql_log_warn, vha, 0xd042,
3430 		    "Failed to allocate ct_sns request.\n");
3431 		goto done_free_sp;
3432 	}
3433 
3434 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.rsp;
3435 	memset(ct_sns, 0, sizeof(*ct_sns));
3436 
3437 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
3438 	/* CT_IU preamble  */
3439 	ct_req = qla2x00_prep_ct_req(ct_sns, GPN_ID_CMD, GPN_ID_RSP_SIZE);
3440 
3441 	/* GPN_ID req */
3442 	ct_req->req.port_id.port_id[0] = id->b.domain;
3443 	ct_req->req.port_id.port_id[1] = id->b.area;
3444 	ct_req->req.port_id.port_id[2] = id->b.al_pa;
3445 
3446 	sp->u.iocb_cmd.u.ctarg.req_size = GPN_ID_REQ_SIZE;
3447 	sp->u.iocb_cmd.u.ctarg.rsp_size = GPN_ID_RSP_SIZE;
3448 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
3449 
3450 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
3451 	sp->done = qla2x00_async_gpnid_sp_done;
3452 
3453 	ql_dbg(ql_dbg_disc, vha, 0x2067,
3454 	    "Async-%s hdl=%x ID %3phC.\n", sp->name,
3455 	    sp->handle, ct_req->req.port_id.port_id);
3456 
3457 	rval = qla2x00_start_sp(sp);
3458 	if (rval != QLA_SUCCESS)
3459 		goto done_free_sp;
3460 
3461 	return rval;
3462 
3463 done_free_sp:
3464 	spin_lock_irqsave(&vha->hw->vport_slock, flags);
3465 	list_del(&sp->elem);
3466 	spin_unlock_irqrestore(&vha->hw->vport_slock, flags);
3467 
3468 	if (sp->u.iocb_cmd.u.ctarg.req) {
3469 		dma_free_coherent(&vha->hw->pdev->dev,
3470 			sizeof(struct ct_sns_pkt),
3471 			sp->u.iocb_cmd.u.ctarg.req,
3472 			sp->u.iocb_cmd.u.ctarg.req_dma);
3473 		sp->u.iocb_cmd.u.ctarg.req = NULL;
3474 	}
3475 	if (sp->u.iocb_cmd.u.ctarg.rsp) {
3476 		dma_free_coherent(&vha->hw->pdev->dev,
3477 			sizeof(struct ct_sns_pkt),
3478 			sp->u.iocb_cmd.u.ctarg.rsp,
3479 			sp->u.iocb_cmd.u.ctarg.rsp_dma);
3480 		sp->u.iocb_cmd.u.ctarg.rsp = NULL;
3481 	}
3482 
3483 	sp->free(sp);
3484 done:
3485 	return rval;
3486 }
3487 
3488 void qla24xx_handle_gffid_event(scsi_qla_host_t *vha, struct event_arg *ea)
3489 {
3490        fc_port_t *fcport = ea->fcport;
3491 
3492        qla24xx_post_gnl_work(vha, fcport);
3493 }
3494 
3495 void qla24xx_async_gffid_sp_done(void *s, int res)
3496 {
3497        struct srb *sp = s;
3498        struct scsi_qla_host *vha = sp->vha;
3499        fc_port_t *fcport = sp->fcport;
3500        struct ct_sns_rsp *ct_rsp;
3501        struct event_arg ea;
3502 
3503        ql_dbg(ql_dbg_disc, vha, 0x2133,
3504 	   "Async done-%s res %x ID %x. %8phC\n",
3505 	   sp->name, res, fcport->d_id.b24, fcport->port_name);
3506 
3507        fcport->flags &= ~FCF_ASYNC_SENT;
3508        ct_rsp = &fcport->ct_desc.ct_sns->p.rsp;
3509        /*
3510 	* FC-GS-7, 5.2.3.12 FC-4 Features - format
3511 	* The format of the FC-4 Features object, as defined by the FC-4,
3512 	* Shall be an array of 4-bit values, one for each type code value
3513 	*/
3514        if (!res) {
3515 	       if (ct_rsp->rsp.gff_id.fc4_features[GFF_FCP_SCSI_OFFSET] & 0xf) {
3516 		       /* w1 b00:03 */
3517 		       fcport->fc4_type =
3518 			   ct_rsp->rsp.gff_id.fc4_features[GFF_FCP_SCSI_OFFSET];
3519 		       fcport->fc4_type &= 0xf;
3520 	       }
3521 
3522 	       if (ct_rsp->rsp.gff_id.fc4_features[GFF_NVME_OFFSET] & 0xf) {
3523 		       /* w5 [00:03]/28h */
3524 		       fcport->fc4f_nvme =
3525 			   ct_rsp->rsp.gff_id.fc4_features[GFF_NVME_OFFSET];
3526 		       fcport->fc4f_nvme &= 0xf;
3527 	       }
3528        }
3529 
3530        memset(&ea, 0, sizeof(ea));
3531        ea.sp = sp;
3532        ea.fcport = sp->fcport;
3533        ea.rc = res;
3534        ea.event = FCME_GFFID_DONE;
3535 
3536        qla2x00_fcport_event_handler(vha, &ea);
3537        sp->free(sp);
3538 }
3539 
3540 /* Get FC4 Feature with Nport ID. */
3541 int qla24xx_async_gffid(scsi_qla_host_t *vha, fc_port_t *fcport)
3542 {
3543 	int rval = QLA_FUNCTION_FAILED;
3544 	struct ct_sns_req       *ct_req;
3545 	srb_t *sp;
3546 
3547 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
3548 		return rval;
3549 
3550 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
3551 	if (!sp)
3552 		return rval;
3553 
3554 	fcport->flags |= FCF_ASYNC_SENT;
3555 	sp->type = SRB_CT_PTHRU_CMD;
3556 	sp->name = "gffid";
3557 	sp->gen1 = fcport->rscn_gen;
3558 	sp->gen2 = fcport->login_gen;
3559 
3560 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
3561 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
3562 
3563 	/* CT_IU preamble  */
3564 	ct_req = qla2x00_prep_ct_req(fcport->ct_desc.ct_sns, GFF_ID_CMD,
3565 	    GFF_ID_RSP_SIZE);
3566 
3567 	ct_req->req.gff_id.port_id[0] = fcport->d_id.b.domain;
3568 	ct_req->req.gff_id.port_id[1] = fcport->d_id.b.area;
3569 	ct_req->req.gff_id.port_id[2] = fcport->d_id.b.al_pa;
3570 
3571 	sp->u.iocb_cmd.u.ctarg.req = fcport->ct_desc.ct_sns;
3572 	sp->u.iocb_cmd.u.ctarg.req_dma = fcport->ct_desc.ct_sns_dma;
3573 	sp->u.iocb_cmd.u.ctarg.rsp = fcport->ct_desc.ct_sns;
3574 	sp->u.iocb_cmd.u.ctarg.rsp_dma = fcport->ct_desc.ct_sns_dma;
3575 	sp->u.iocb_cmd.u.ctarg.req_size = GFF_ID_REQ_SIZE;
3576 	sp->u.iocb_cmd.u.ctarg.rsp_size = GFF_ID_RSP_SIZE;
3577 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
3578 
3579 	sp->done = qla24xx_async_gffid_sp_done;
3580 
3581 	rval = qla2x00_start_sp(sp);
3582 	if (rval != QLA_SUCCESS)
3583 		goto done_free_sp;
3584 
3585 	ql_dbg(ql_dbg_disc, vha, 0x2132,
3586 	    "Async-%s hdl=%x  %8phC.\n", sp->name,
3587 	    sp->handle, fcport->port_name);
3588 
3589 	return rval;
3590 done_free_sp:
3591 	sp->free(sp);
3592 	fcport->flags &= ~FCF_ASYNC_SENT;
3593 	return rval;
3594 }
3595 
3596 /* GPN_FT + GNN_FT*/
3597 static int qla2x00_is_a_vp(scsi_qla_host_t *vha, u64 wwn)
3598 {
3599 	struct qla_hw_data *ha = vha->hw;
3600 	scsi_qla_host_t *vp;
3601 	unsigned long flags;
3602 	u64 twwn;
3603 	int rc = 0;
3604 
3605 	if (!ha->num_vhosts)
3606 		return 0;
3607 
3608 	spin_lock_irqsave(&ha->vport_slock, flags);
3609 	list_for_each_entry(vp, &ha->vp_list, list) {
3610 		twwn = wwn_to_u64(vp->port_name);
3611 		if (wwn == twwn) {
3612 			rc = 1;
3613 			break;
3614 		}
3615 	}
3616 	spin_unlock_irqrestore(&ha->vport_slock, flags);
3617 
3618 	return rc;
3619 }
3620 
3621 void qla24xx_async_gnnft_done(scsi_qla_host_t *vha, srb_t *sp)
3622 {
3623 	fc_port_t *fcport;
3624 	u32 i, rc;
3625 	bool found;
3626 	struct fab_scan_rp *rp, *trp;
3627 	unsigned long flags;
3628 	u8 recheck = 0;
3629 	u16 dup = 0, dup_cnt = 0;
3630 
3631 	ql_dbg(ql_dbg_disc, vha, 0xffff,
3632 	    "%s enter\n", __func__);
3633 
3634 	if (sp->gen1 != vha->hw->base_qpair->chip_reset) {
3635 		ql_dbg(ql_dbg_disc, vha, 0xffff,
3636 		    "%s scan stop due to chip reset %x/%x\n",
3637 		    sp->name, sp->gen1, vha->hw->base_qpair->chip_reset);
3638 		goto out;
3639 	}
3640 
3641 	rc = sp->rc;
3642 	if (rc) {
3643 		vha->scan.scan_retry++;
3644 		if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
3645 			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
3646 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
3647 		} else {
3648 			ql_dbg(ql_dbg_disc, vha, 0xffff,
3649 			    "Fabric scan failed on all retries.\n");
3650 		}
3651 		goto out;
3652 	}
3653 	vha->scan.scan_retry = 0;
3654 
3655 	list_for_each_entry(fcport, &vha->vp_fcports, list)
3656 		fcport->scan_state = QLA_FCPORT_SCAN;
3657 
3658 	for (i = 0; i < vha->hw->max_fibre_devices; i++) {
3659 		u64 wwn;
3660 		int k;
3661 
3662 		rp = &vha->scan.l[i];
3663 		found = false;
3664 
3665 		wwn = wwn_to_u64(rp->port_name);
3666 		if (wwn == 0)
3667 			continue;
3668 
3669 		/* Remove duplicate NPORT ID entries from switch data base */
3670 		for (k = i + 1; k < vha->hw->max_fibre_devices; k++) {
3671 			trp = &vha->scan.l[k];
3672 			if (rp->id.b24 == trp->id.b24) {
3673 				dup = 1;
3674 				dup_cnt++;
3675 				ql_dbg(ql_dbg_disc + ql_dbg_verbose,
3676 				    vha, 0xffff,
3677 				    "Detected duplicate NPORT ID from switch data base: ID %06x WWN %8phN WWN %8phN\n",
3678 				    rp->id.b24, rp->port_name, trp->port_name);
3679 				memset(trp, 0, sizeof(*trp));
3680 			}
3681 		}
3682 
3683 		if (!memcmp(rp->port_name, vha->port_name, WWN_SIZE))
3684 			continue;
3685 
3686 		/* Bypass reserved domain fields. */
3687 		if ((rp->id.b.domain & 0xf0) == 0xf0)
3688 			continue;
3689 
3690 		/* Bypass virtual ports of the same host. */
3691 		if (qla2x00_is_a_vp(vha, wwn))
3692 			continue;
3693 
3694 		list_for_each_entry(fcport, &vha->vp_fcports, list) {
3695 			if (memcmp(rp->port_name, fcport->port_name, WWN_SIZE))
3696 				continue;
3697 			fcport->scan_needed = 0;
3698 			fcport->scan_state = QLA_FCPORT_FOUND;
3699 			found = true;
3700 			/*
3701 			 * If device was not a fabric device before.
3702 			 */
3703 			if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
3704 				qla2x00_clear_loop_id(fcport);
3705 				fcport->flags |= FCF_FABRIC_DEVICE;
3706 			} else if (fcport->d_id.b24 != rp->id.b24) {
3707 				qlt_schedule_sess_for_deletion(fcport);
3708 			}
3709 			fcport->d_id.b24 = rp->id.b24;
3710 			break;
3711 		}
3712 
3713 		if (!found) {
3714 			ql_dbg(ql_dbg_disc, vha, 0xffff,
3715 			    "%s %d %8phC post new sess\n",
3716 			    __func__, __LINE__, rp->port_name);
3717 			qla24xx_post_newsess_work(vha, &rp->id, rp->port_name,
3718 			    rp->node_name, NULL, rp->fc4type);
3719 		}
3720 	}
3721 
3722 	if (dup) {
3723 		ql_log(ql_log_warn, vha, 0xffff,
3724 		    "Detected %d duplicate NPORT ID(s) from switch data base\n",
3725 		    dup_cnt);
3726 	}
3727 
3728 	/*
3729 	 * Logout all previous fabric dev marked lost, except FCP2 devices.
3730 	 */
3731 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
3732 		if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
3733 			fcport->scan_needed = 0;
3734 			continue;
3735 		}
3736 
3737 		if (fcport->scan_state != QLA_FCPORT_FOUND) {
3738 			fcport->scan_needed = 0;
3739 			if ((qla_dual_mode_enabled(vha) ||
3740 				qla_ini_mode_enabled(vha)) &&
3741 			    atomic_read(&fcport->state) == FCS_ONLINE) {
3742 				if (fcport->loop_id != FC_NO_LOOP_ID) {
3743 					if (fcport->flags & FCF_FCP2_DEVICE)
3744 						fcport->logout_on_delete = 0;
3745 
3746 					ql_dbg(ql_dbg_disc, vha, 0x20f0,
3747 					    "%s %d %8phC post del sess\n",
3748 					    __func__, __LINE__,
3749 					    fcport->port_name);
3750 
3751 					qlt_schedule_sess_for_deletion(fcport);
3752 					continue;
3753 				}
3754 			}
3755 		} else {
3756 			if (fcport->scan_needed ||
3757 			    fcport->disc_state != DSC_LOGIN_COMPLETE) {
3758 				if (fcport->login_retry == 0) {
3759 					fcport->login_retry =
3760 						vha->hw->login_retry_count;
3761 					ql_dbg(ql_dbg_disc, vha, 0x20a3,
3762 					    "Port login retry %8phN, lid 0x%04x retry cnt=%d.\n",
3763 					    fcport->port_name, fcport->loop_id,
3764 					    fcport->login_retry);
3765 				}
3766 				fcport->scan_needed = 0;
3767 				qla24xx_fcport_handle_login(vha, fcport);
3768 			}
3769 		}
3770 	}
3771 
3772 	recheck = 1;
3773 out:
3774 	qla24xx_sp_unmap(vha, sp);
3775 	spin_lock_irqsave(&vha->work_lock, flags);
3776 	vha->scan.scan_flags &= ~SF_SCANNING;
3777 	spin_unlock_irqrestore(&vha->work_lock, flags);
3778 
3779 	if (recheck) {
3780 		list_for_each_entry(fcport, &vha->vp_fcports, list) {
3781 			if (fcport->scan_needed) {
3782 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
3783 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
3784 				break;
3785 			}
3786 		}
3787 	}
3788 }
3789 
3790 static int qla2x00_post_gnnft_gpnft_done_work(struct scsi_qla_host *vha,
3791     srb_t *sp, int cmd)
3792 {
3793 	struct qla_work_evt *e;
3794 
3795 	if (cmd != QLA_EVT_GPNFT_DONE && cmd != QLA_EVT_GNNFT_DONE)
3796 		return QLA_PARAMETER_ERROR;
3797 
3798 	e = qla2x00_alloc_work(vha, cmd);
3799 	if (!e)
3800 		return QLA_FUNCTION_FAILED;
3801 
3802 	e->u.iosb.sp = sp;
3803 
3804 	return qla2x00_post_work(vha, e);
3805 }
3806 
3807 static int qla2x00_post_nvme_gpnft_work(struct scsi_qla_host *vha,
3808     srb_t *sp, int cmd)
3809 {
3810 	struct qla_work_evt *e;
3811 
3812 	if (cmd != QLA_EVT_GPNFT)
3813 		return QLA_PARAMETER_ERROR;
3814 
3815 	e = qla2x00_alloc_work(vha, cmd);
3816 	if (!e)
3817 		return QLA_FUNCTION_FAILED;
3818 
3819 	e->u.gpnft.fc4_type = FC4_TYPE_NVME;
3820 	e->u.gpnft.sp = sp;
3821 
3822 	return qla2x00_post_work(vha, e);
3823 }
3824 
3825 static void qla2x00_find_free_fcp_nvme_slot(struct scsi_qla_host *vha,
3826 	struct srb *sp)
3827 {
3828 	struct qla_hw_data *ha = vha->hw;
3829 	int num_fibre_dev = ha->max_fibre_devices;
3830 	struct ct_sns_req *ct_req =
3831 		(struct ct_sns_req *)sp->u.iocb_cmd.u.ctarg.req;
3832 	struct ct_sns_gpnft_rsp *ct_rsp =
3833 		(struct ct_sns_gpnft_rsp *)sp->u.iocb_cmd.u.ctarg.rsp;
3834 	struct ct_sns_gpn_ft_data *d;
3835 	struct fab_scan_rp *rp;
3836 	u16 cmd = be16_to_cpu(ct_req->command);
3837 	u8 fc4_type = sp->gen2;
3838 	int i, j, k;
3839 	port_id_t id;
3840 	u8 found;
3841 	u64 wwn;
3842 
3843 	j = 0;
3844 	for (i = 0; i < num_fibre_dev; i++) {
3845 		d  = &ct_rsp->entries[i];
3846 
3847 		id.b.rsvd_1 = 0;
3848 		id.b.domain = d->port_id[0];
3849 		id.b.area   = d->port_id[1];
3850 		id.b.al_pa  = d->port_id[2];
3851 		wwn = wwn_to_u64(d->port_name);
3852 
3853 		if (id.b24 == 0 || wwn == 0)
3854 			continue;
3855 
3856 		if (fc4_type == FC4_TYPE_FCP_SCSI) {
3857 			if (cmd == GPN_FT_CMD) {
3858 				rp = &vha->scan.l[j];
3859 				rp->id = id;
3860 				memcpy(rp->port_name, d->port_name, 8);
3861 				j++;
3862 				rp->fc4type = FS_FC4TYPE_FCP;
3863 			} else {
3864 				for (k = 0; k < num_fibre_dev; k++) {
3865 					rp = &vha->scan.l[k];
3866 					if (id.b24 == rp->id.b24) {
3867 						memcpy(rp->node_name,
3868 						    d->port_name, 8);
3869 						break;
3870 					}
3871 				}
3872 			}
3873 		} else {
3874 			/* Search if the fibre device supports FC4_TYPE_NVME */
3875 			if (cmd == GPN_FT_CMD) {
3876 				found = 0;
3877 
3878 				for (k = 0; k < num_fibre_dev; k++) {
3879 					rp = &vha->scan.l[k];
3880 					if (!memcmp(rp->port_name,
3881 					    d->port_name, 8)) {
3882 						/*
3883 						 * Supports FC-NVMe & FCP
3884 						 */
3885 						rp->fc4type |= FS_FC4TYPE_NVME;
3886 						found = 1;
3887 						break;
3888 					}
3889 				}
3890 
3891 				/* We found new FC-NVMe only port */
3892 				if (!found) {
3893 					for (k = 0; k < num_fibre_dev; k++) {
3894 						rp = &vha->scan.l[k];
3895 						if (wwn_to_u64(rp->port_name)) {
3896 							continue;
3897 						} else {
3898 							rp->id = id;
3899 							memcpy(rp->port_name,
3900 							    d->port_name, 8);
3901 							rp->fc4type =
3902 							    FS_FC4TYPE_NVME;
3903 							break;
3904 						}
3905 					}
3906 				}
3907 			} else {
3908 				for (k = 0; k < num_fibre_dev; k++) {
3909 					rp = &vha->scan.l[k];
3910 					if (id.b24 == rp->id.b24) {
3911 						memcpy(rp->node_name,
3912 						    d->port_name, 8);
3913 						break;
3914 					}
3915 				}
3916 			}
3917 		}
3918 	}
3919 }
3920 
3921 static void qla2x00_async_gpnft_gnnft_sp_done(void *s, int res)
3922 {
3923 	struct srb *sp = s;
3924 	struct scsi_qla_host *vha = sp->vha;
3925 	struct ct_sns_req *ct_req =
3926 		(struct ct_sns_req *)sp->u.iocb_cmd.u.ctarg.req;
3927 	u16 cmd = be16_to_cpu(ct_req->command);
3928 	u8 fc4_type = sp->gen2;
3929 	unsigned long flags;
3930 	int rc;
3931 
3932 	/* gen2 field is holding the fc4type */
3933 	ql_dbg(ql_dbg_disc, vha, 0xffff,
3934 	    "Async done-%s res %x FC4Type %x\n",
3935 	    sp->name, res, sp->gen2);
3936 
3937 	del_timer(&sp->u.iocb_cmd.timer);
3938 	sp->rc = res;
3939 	if (res) {
3940 		unsigned long flags;
3941 		const char *name = sp->name;
3942 
3943 		/*
3944 		 * We are in an Interrupt context, queue up this
3945 		 * sp for GNNFT_DONE work. This will allow all
3946 		 * the resource to get freed up.
3947 		 */
3948 		rc = qla2x00_post_gnnft_gpnft_done_work(vha, sp,
3949 		    QLA_EVT_GNNFT_DONE);
3950 		if (rc) {
3951 			/* Cleanup here to prevent memory leak */
3952 			qla24xx_sp_unmap(vha, sp);
3953 
3954 			spin_lock_irqsave(&vha->work_lock, flags);
3955 			vha->scan.scan_flags &= ~SF_SCANNING;
3956 			vha->scan.scan_retry++;
3957 			spin_unlock_irqrestore(&vha->work_lock, flags);
3958 
3959 			if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
3960 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
3961 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
3962 				qla2xxx_wake_dpc(vha);
3963 			} else {
3964 				ql_dbg(ql_dbg_disc, vha, 0xffff,
3965 				    "Async done-%s rescan failed on all retries.\n",
3966 				    name);
3967 			}
3968 		}
3969 		return;
3970 	}
3971 
3972 	qla2x00_find_free_fcp_nvme_slot(vha, sp);
3973 
3974 	if ((fc4_type == FC4_TYPE_FCP_SCSI) && vha->flags.nvme_enabled &&
3975 	    cmd == GNN_FT_CMD) {
3976 		spin_lock_irqsave(&vha->work_lock, flags);
3977 		vha->scan.scan_flags &= ~SF_SCANNING;
3978 		spin_unlock_irqrestore(&vha->work_lock, flags);
3979 
3980 		sp->rc = res;
3981 		rc = qla2x00_post_nvme_gpnft_work(vha, sp, QLA_EVT_GPNFT);
3982 		if (rc) {
3983 			qla24xx_sp_unmap(vha, sp);
3984 			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
3985 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
3986 		}
3987 		return;
3988 	}
3989 
3990 	if (cmd == GPN_FT_CMD) {
3991 		rc = qla2x00_post_gnnft_gpnft_done_work(vha, sp,
3992 		    QLA_EVT_GPNFT_DONE);
3993 	} else {
3994 		rc = qla2x00_post_gnnft_gpnft_done_work(vha, sp,
3995 		    QLA_EVT_GNNFT_DONE);
3996 	}
3997 
3998 	if (rc) {
3999 		qla24xx_sp_unmap(vha, sp);
4000 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4001 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4002 		return;
4003 	}
4004 }
4005 
4006 /*
4007  * Get WWNN list for fc4_type
4008  *
4009  * It is assumed the same SRB is re-used from GPNFT to avoid
4010  * mem free & re-alloc
4011  */
4012 static int qla24xx_async_gnnft(scsi_qla_host_t *vha, struct srb *sp,
4013     u8 fc4_type)
4014 {
4015 	int rval = QLA_FUNCTION_FAILED;
4016 	struct ct_sns_req *ct_req;
4017 	struct ct_sns_pkt *ct_sns;
4018 	unsigned long flags;
4019 
4020 	if (!vha->flags.online) {
4021 		spin_lock_irqsave(&vha->work_lock, flags);
4022 		vha->scan.scan_flags &= ~SF_SCANNING;
4023 		spin_unlock_irqrestore(&vha->work_lock, flags);
4024 		goto done_free_sp;
4025 	}
4026 
4027 	if (!sp->u.iocb_cmd.u.ctarg.req || !sp->u.iocb_cmd.u.ctarg.rsp) {
4028 		ql_log(ql_log_warn, vha, 0xffff,
4029 		    "%s: req %p rsp %p are not setup\n",
4030 		    __func__, sp->u.iocb_cmd.u.ctarg.req,
4031 		    sp->u.iocb_cmd.u.ctarg.rsp);
4032 		spin_lock_irqsave(&vha->work_lock, flags);
4033 		vha->scan.scan_flags &= ~SF_SCANNING;
4034 		spin_unlock_irqrestore(&vha->work_lock, flags);
4035 		WARN_ON(1);
4036 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4037 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4038 		goto done_free_sp;
4039 	}
4040 
4041 	ql_dbg(ql_dbg_disc, vha, 0xfffff,
4042 	    "%s: FC4Type %x, CT-PASSTHRU %s command ctarg rsp size %d, ctarg req size %d\n",
4043 	    __func__, fc4_type, sp->name, sp->u.iocb_cmd.u.ctarg.rsp_size,
4044 	     sp->u.iocb_cmd.u.ctarg.req_size);
4045 
4046 	sp->type = SRB_CT_PTHRU_CMD;
4047 	sp->name = "gnnft";
4048 	sp->gen1 = vha->hw->base_qpair->chip_reset;
4049 	sp->gen2 = fc4_type;
4050 
4051 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
4052 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
4053 
4054 	memset(sp->u.iocb_cmd.u.ctarg.rsp, 0, sp->u.iocb_cmd.u.ctarg.rsp_size);
4055 	memset(sp->u.iocb_cmd.u.ctarg.req, 0, sp->u.iocb_cmd.u.ctarg.req_size);
4056 
4057 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
4058 	/* CT_IU preamble  */
4059 	ct_req = qla2x00_prep_ct_req(ct_sns, GNN_FT_CMD,
4060 	    sp->u.iocb_cmd.u.ctarg.rsp_size);
4061 
4062 	/* GPN_FT req */
4063 	ct_req->req.gpn_ft.port_type = fc4_type;
4064 
4065 	sp->u.iocb_cmd.u.ctarg.req_size = GNN_FT_REQ_SIZE;
4066 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
4067 
4068 	sp->done = qla2x00_async_gpnft_gnnft_sp_done;
4069 
4070 	rval = qla2x00_start_sp(sp);
4071 	if (rval != QLA_SUCCESS) {
4072 		spin_lock_irqsave(&vha->work_lock, flags);
4073 		vha->scan.scan_flags &= ~SF_SCANNING;
4074 		spin_unlock_irqrestore(&vha->work_lock, flags);
4075 		goto done_free_sp;
4076 	}
4077 
4078 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4079 	    "Async-%s hdl=%x FC4Type %x.\n", sp->name,
4080 	    sp->handle, ct_req->req.gpn_ft.port_type);
4081 	return rval;
4082 
4083 done_free_sp:
4084 	if (sp->u.iocb_cmd.u.ctarg.req) {
4085 		dma_free_coherent(&vha->hw->pdev->dev,
4086 		    sp->u.iocb_cmd.u.ctarg.req_allocated_size,
4087 		    sp->u.iocb_cmd.u.ctarg.req,
4088 		    sp->u.iocb_cmd.u.ctarg.req_dma);
4089 		sp->u.iocb_cmd.u.ctarg.req = NULL;
4090 	}
4091 	if (sp->u.iocb_cmd.u.ctarg.rsp) {
4092 		dma_free_coherent(&vha->hw->pdev->dev,
4093 		    sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
4094 		    sp->u.iocb_cmd.u.ctarg.rsp,
4095 		    sp->u.iocb_cmd.u.ctarg.rsp_dma);
4096 		sp->u.iocb_cmd.u.ctarg.rsp = NULL;
4097 	}
4098 
4099 	sp->free(sp);
4100 
4101 	return rval;
4102 } /* GNNFT */
4103 
4104 void qla24xx_async_gpnft_done(scsi_qla_host_t *vha, srb_t *sp)
4105 {
4106 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4107 	    "%s enter\n", __func__);
4108 	qla24xx_async_gnnft(vha, sp, sp->gen2);
4109 }
4110 
4111 /* Get WWPN list for certain fc4_type */
4112 int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp)
4113 {
4114 	int rval = QLA_FUNCTION_FAILED;
4115 	struct ct_sns_req       *ct_req;
4116 	struct ct_sns_pkt *ct_sns;
4117 	u32 rspsz;
4118 	unsigned long flags;
4119 
4120 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4121 	    "%s enter\n", __func__);
4122 
4123 	if (!vha->flags.online)
4124 		return rval;
4125 
4126 	spin_lock_irqsave(&vha->work_lock, flags);
4127 	if (vha->scan.scan_flags & SF_SCANNING) {
4128 		spin_unlock_irqrestore(&vha->work_lock, flags);
4129 		ql_dbg(ql_dbg_disc, vha, 0xffff, "scan active\n");
4130 		return rval;
4131 	}
4132 	vha->scan.scan_flags |= SF_SCANNING;
4133 	spin_unlock_irqrestore(&vha->work_lock, flags);
4134 
4135 	if (fc4_type == FC4_TYPE_FCP_SCSI) {
4136 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4137 		    "%s: Performing FCP Scan\n", __func__);
4138 
4139 		if (sp)
4140 			sp->free(sp); /* should not happen */
4141 
4142 		sp = qla2x00_get_sp(vha, NULL, GFP_KERNEL);
4143 		if (!sp) {
4144 			spin_lock_irqsave(&vha->work_lock, flags);
4145 			vha->scan.scan_flags &= ~SF_SCANNING;
4146 			spin_unlock_irqrestore(&vha->work_lock, flags);
4147 			return rval;
4148 		}
4149 
4150 		sp->u.iocb_cmd.u.ctarg.req = dma_zalloc_coherent(
4151 			&vha->hw->pdev->dev, sizeof(struct ct_sns_pkt),
4152 			&sp->u.iocb_cmd.u.ctarg.req_dma, GFP_KERNEL);
4153 		sp->u.iocb_cmd.u.ctarg.req_allocated_size = sizeof(struct ct_sns_pkt);
4154 		if (!sp->u.iocb_cmd.u.ctarg.req) {
4155 			ql_log(ql_log_warn, vha, 0xffff,
4156 			    "Failed to allocate ct_sns request.\n");
4157 			spin_lock_irqsave(&vha->work_lock, flags);
4158 			vha->scan.scan_flags &= ~SF_SCANNING;
4159 			spin_unlock_irqrestore(&vha->work_lock, flags);
4160 			goto done_free_sp;
4161 		}
4162 		sp->u.iocb_cmd.u.ctarg.req_size = GPN_FT_REQ_SIZE;
4163 
4164 		rspsz = sizeof(struct ct_sns_gpnft_rsp) +
4165 			((vha->hw->max_fibre_devices - 1) *
4166 			    sizeof(struct ct_sns_gpn_ft_data));
4167 
4168 		sp->u.iocb_cmd.u.ctarg.rsp = dma_zalloc_coherent(
4169 			&vha->hw->pdev->dev, rspsz,
4170 			&sp->u.iocb_cmd.u.ctarg.rsp_dma, GFP_KERNEL);
4171 		sp->u.iocb_cmd.u.ctarg.rsp_allocated_size = sizeof(struct ct_sns_pkt);
4172 		if (!sp->u.iocb_cmd.u.ctarg.rsp) {
4173 			ql_log(ql_log_warn, vha, 0xffff,
4174 			    "Failed to allocate ct_sns request.\n");
4175 			spin_lock_irqsave(&vha->work_lock, flags);
4176 			vha->scan.scan_flags &= ~SF_SCANNING;
4177 			spin_unlock_irqrestore(&vha->work_lock, flags);
4178 			goto done_free_sp;
4179 		}
4180 		sp->u.iocb_cmd.u.ctarg.rsp_size = rspsz;
4181 
4182 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4183 		    "%s scan list size %d\n", __func__, vha->scan.size);
4184 
4185 		memset(vha->scan.l, 0, vha->scan.size);
4186 	} else if (!sp) {
4187 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4188 		    "NVME scan did not provide SP\n");
4189 		return rval;
4190 	}
4191 
4192 	sp->type = SRB_CT_PTHRU_CMD;
4193 	sp->name = "gpnft";
4194 	sp->gen1 = vha->hw->base_qpair->chip_reset;
4195 	sp->gen2 = fc4_type;
4196 
4197 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
4198 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
4199 
4200 	rspsz = sp->u.iocb_cmd.u.ctarg.rsp_size;
4201 	memset(sp->u.iocb_cmd.u.ctarg.rsp, 0, sp->u.iocb_cmd.u.ctarg.rsp_size);
4202 	memset(sp->u.iocb_cmd.u.ctarg.req, 0, sp->u.iocb_cmd.u.ctarg.req_size);
4203 
4204 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
4205 	/* CT_IU preamble  */
4206 	ct_req = qla2x00_prep_ct_req(ct_sns, GPN_FT_CMD, rspsz);
4207 
4208 	/* GPN_FT req */
4209 	ct_req->req.gpn_ft.port_type = fc4_type;
4210 
4211 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
4212 
4213 	sp->done = qla2x00_async_gpnft_gnnft_sp_done;
4214 
4215 	rval = qla2x00_start_sp(sp);
4216 	if (rval != QLA_SUCCESS) {
4217 		spin_lock_irqsave(&vha->work_lock, flags);
4218 		vha->scan.scan_flags &= ~SF_SCANNING;
4219 		spin_unlock_irqrestore(&vha->work_lock, flags);
4220 		goto done_free_sp;
4221 	}
4222 
4223 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4224 	    "Async-%s hdl=%x FC4Type %x.\n", sp->name,
4225 	    sp->handle, ct_req->req.gpn_ft.port_type);
4226 	return rval;
4227 
4228 done_free_sp:
4229 	if (sp->u.iocb_cmd.u.ctarg.req) {
4230 		dma_free_coherent(&vha->hw->pdev->dev,
4231 		    sp->u.iocb_cmd.u.ctarg.req_allocated_size,
4232 		    sp->u.iocb_cmd.u.ctarg.req,
4233 		    sp->u.iocb_cmd.u.ctarg.req_dma);
4234 		sp->u.iocb_cmd.u.ctarg.req = NULL;
4235 	}
4236 	if (sp->u.iocb_cmd.u.ctarg.rsp) {
4237 		dma_free_coherent(&vha->hw->pdev->dev,
4238 		    sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
4239 		    sp->u.iocb_cmd.u.ctarg.rsp,
4240 		    sp->u.iocb_cmd.u.ctarg.rsp_dma);
4241 		sp->u.iocb_cmd.u.ctarg.rsp = NULL;
4242 	}
4243 
4244 	sp->free(sp);
4245 
4246 	return rval;
4247 }
4248 
4249 void qla_scan_work_fn(struct work_struct *work)
4250 {
4251 	struct fab_scan *s = container_of(to_delayed_work(work),
4252 	    struct fab_scan, scan_work);
4253 	struct scsi_qla_host *vha = container_of(s, struct scsi_qla_host,
4254 	    scan);
4255 	unsigned long flags;
4256 
4257 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4258 	    "%s: schedule loop resync\n", __func__);
4259 	set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4260 	set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4261 	qla2xxx_wake_dpc(vha);
4262 	spin_lock_irqsave(&vha->work_lock, flags);
4263 	vha->scan.scan_flags &= ~SF_QUEUED;
4264 	spin_unlock_irqrestore(&vha->work_lock, flags);
4265 }
4266 
4267 /* GNN_ID */
4268 void qla24xx_handle_gnnid_event(scsi_qla_host_t *vha, struct event_arg *ea)
4269 {
4270 	qla24xx_post_gnl_work(vha, ea->fcport);
4271 }
4272 
4273 static void qla2x00_async_gnnid_sp_done(void *s, int res)
4274 {
4275 	struct srb *sp = s;
4276 	struct scsi_qla_host *vha = sp->vha;
4277 	fc_port_t *fcport = sp->fcport;
4278 	u8 *node_name = fcport->ct_desc.ct_sns->p.rsp.rsp.gnn_id.node_name;
4279 	struct event_arg ea;
4280 	u64 wwnn;
4281 
4282 	fcport->flags &= ~FCF_ASYNC_SENT;
4283 	wwnn = wwn_to_u64(node_name);
4284 	if (wwnn)
4285 		memcpy(fcport->node_name, node_name, WWN_SIZE);
4286 
4287 	memset(&ea, 0, sizeof(ea));
4288 	ea.fcport = fcport;
4289 	ea.sp = sp;
4290 	ea.rc = res;
4291 	ea.event = FCME_GNNID_DONE;
4292 
4293 	ql_dbg(ql_dbg_disc, vha, 0x204f,
4294 	    "Async done-%s res %x, WWPN %8phC %8phC\n",
4295 	    sp->name, res, fcport->port_name, fcport->node_name);
4296 
4297 	qla2x00_fcport_event_handler(vha, &ea);
4298 
4299 	sp->free(sp);
4300 }
4301 
4302 int qla24xx_async_gnnid(scsi_qla_host_t *vha, fc_port_t *fcport)
4303 {
4304 	int rval = QLA_FUNCTION_FAILED;
4305 	struct ct_sns_req       *ct_req;
4306 	srb_t *sp;
4307 
4308 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
4309 		return rval;
4310 
4311 	fcport->disc_state = DSC_GNN_ID;
4312 	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
4313 	if (!sp)
4314 		goto done;
4315 
4316 	fcport->flags |= FCF_ASYNC_SENT;
4317 	sp->type = SRB_CT_PTHRU_CMD;
4318 	sp->name = "gnnid";
4319 	sp->gen1 = fcport->rscn_gen;
4320 	sp->gen2 = fcport->login_gen;
4321 
4322 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
4323 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
4324 
4325 	/* CT_IU preamble  */
4326 	ct_req = qla2x00_prep_ct_req(fcport->ct_desc.ct_sns, GNN_ID_CMD,
4327 	    GNN_ID_RSP_SIZE);
4328 
4329 	/* GNN_ID req */
4330 	ct_req->req.port_id.port_id[0] = fcport->d_id.b.domain;
4331 	ct_req->req.port_id.port_id[1] = fcport->d_id.b.area;
4332 	ct_req->req.port_id.port_id[2] = fcport->d_id.b.al_pa;
4333 
4334 
4335 	/* req & rsp use the same buffer */
4336 	sp->u.iocb_cmd.u.ctarg.req = fcport->ct_desc.ct_sns;
4337 	sp->u.iocb_cmd.u.ctarg.req_dma = fcport->ct_desc.ct_sns_dma;
4338 	sp->u.iocb_cmd.u.ctarg.rsp = fcport->ct_desc.ct_sns;
4339 	sp->u.iocb_cmd.u.ctarg.rsp_dma = fcport->ct_desc.ct_sns_dma;
4340 	sp->u.iocb_cmd.u.ctarg.req_size = GNN_ID_REQ_SIZE;
4341 	sp->u.iocb_cmd.u.ctarg.rsp_size = GNN_ID_RSP_SIZE;
4342 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
4343 
4344 	sp->done = qla2x00_async_gnnid_sp_done;
4345 
4346 	rval = qla2x00_start_sp(sp);
4347 	if (rval != QLA_SUCCESS)
4348 		goto done_free_sp;
4349 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4350 	    "Async-%s - %8phC hdl=%x loopid=%x portid %06x.\n",
4351 	    sp->name, fcport->port_name,
4352 	    sp->handle, fcport->loop_id, fcport->d_id.b24);
4353 	return rval;
4354 
4355 done_free_sp:
4356 	sp->free(sp);
4357 done:
4358 	return rval;
4359 }
4360 
4361 int qla24xx_post_gnnid_work(struct scsi_qla_host *vha, fc_port_t *fcport)
4362 {
4363 	struct qla_work_evt *e;
4364 	int ls;
4365 
4366 	ls = atomic_read(&vha->loop_state);
4367 	if (((ls != LOOP_READY) && (ls != LOOP_UP)) ||
4368 		test_bit(UNLOADING, &vha->dpc_flags))
4369 		return 0;
4370 
4371 	e = qla2x00_alloc_work(vha, QLA_EVT_GNNID);
4372 	if (!e)
4373 		return QLA_FUNCTION_FAILED;
4374 
4375 	e->u.fcport.fcport = fcport;
4376 	return qla2x00_post_work(vha, e);
4377 }
4378 
4379 /* GPFN_ID */
4380 void qla24xx_handle_gfpnid_event(scsi_qla_host_t *vha, struct event_arg *ea)
4381 {
4382 	fc_port_t *fcport = ea->fcport;
4383 
4384 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4385 	    "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d fcpcnt %d\n",
4386 	    __func__, fcport->port_name, fcport->disc_state,
4387 	    fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
4388 	    fcport->rscn_gen, ea->sp->gen1, vha->fcport_count);
4389 
4390 	if (fcport->disc_state == DSC_DELETE_PEND)
4391 		return;
4392 
4393 	if (ea->sp->gen2 != fcport->login_gen) {
4394 		/* target side must have changed it. */
4395 		ql_dbg(ql_dbg_disc, vha, 0x20d3,
4396 		    "%s %8phC generation changed\n",
4397 		    __func__, fcport->port_name);
4398 		return;
4399 	} else if (ea->sp->gen1 != fcport->rscn_gen) {
4400 		return;
4401 	}
4402 
4403 	qla24xx_post_gpsc_work(vha, fcport);
4404 }
4405 
4406 static void qla2x00_async_gfpnid_sp_done(void *s, int res)
4407 {
4408 	struct srb *sp = s;
4409 	struct scsi_qla_host *vha = sp->vha;
4410 	fc_port_t *fcport = sp->fcport;
4411 	u8 *fpn = fcport->ct_desc.ct_sns->p.rsp.rsp.gfpn_id.port_name;
4412 	struct event_arg ea;
4413 	u64 wwn;
4414 
4415 	wwn = wwn_to_u64(fpn);
4416 	if (wwn)
4417 		memcpy(fcport->fabric_port_name, fpn, WWN_SIZE);
4418 
4419 	memset(&ea, 0, sizeof(ea));
4420 	ea.fcport = fcport;
4421 	ea.sp = sp;
4422 	ea.rc = res;
4423 	ea.event = FCME_GFPNID_DONE;
4424 
4425 	ql_dbg(ql_dbg_disc, vha, 0x204f,
4426 	    "Async done-%s res %x, WWPN %8phC %8phC\n",
4427 	    sp->name, res, fcport->port_name, fcport->fabric_port_name);
4428 
4429 	qla2x00_fcport_event_handler(vha, &ea);
4430 
4431 	sp->free(sp);
4432 }
4433 
4434 int qla24xx_async_gfpnid(scsi_qla_host_t *vha, fc_port_t *fcport)
4435 {
4436 	int rval = QLA_FUNCTION_FAILED;
4437 	struct ct_sns_req       *ct_req;
4438 	srb_t *sp;
4439 
4440 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
4441 		return rval;
4442 
4443 	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
4444 	if (!sp)
4445 		goto done;
4446 
4447 	sp->type = SRB_CT_PTHRU_CMD;
4448 	sp->name = "gfpnid";
4449 	sp->gen1 = fcport->rscn_gen;
4450 	sp->gen2 = fcport->login_gen;
4451 
4452 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
4453 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
4454 
4455 	/* CT_IU preamble  */
4456 	ct_req = qla2x00_prep_ct_req(fcport->ct_desc.ct_sns, GFPN_ID_CMD,
4457 	    GFPN_ID_RSP_SIZE);
4458 
4459 	/* GFPN_ID req */
4460 	ct_req->req.port_id.port_id[0] = fcport->d_id.b.domain;
4461 	ct_req->req.port_id.port_id[1] = fcport->d_id.b.area;
4462 	ct_req->req.port_id.port_id[2] = fcport->d_id.b.al_pa;
4463 
4464 
4465 	/* req & rsp use the same buffer */
4466 	sp->u.iocb_cmd.u.ctarg.req = fcport->ct_desc.ct_sns;
4467 	sp->u.iocb_cmd.u.ctarg.req_dma = fcport->ct_desc.ct_sns_dma;
4468 	sp->u.iocb_cmd.u.ctarg.rsp = fcport->ct_desc.ct_sns;
4469 	sp->u.iocb_cmd.u.ctarg.rsp_dma = fcport->ct_desc.ct_sns_dma;
4470 	sp->u.iocb_cmd.u.ctarg.req_size = GFPN_ID_REQ_SIZE;
4471 	sp->u.iocb_cmd.u.ctarg.rsp_size = GFPN_ID_RSP_SIZE;
4472 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
4473 
4474 	sp->done = qla2x00_async_gfpnid_sp_done;
4475 
4476 	rval = qla2x00_start_sp(sp);
4477 	if (rval != QLA_SUCCESS)
4478 		goto done_free_sp;
4479 
4480 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4481 	    "Async-%s - %8phC hdl=%x loopid=%x portid %06x.\n",
4482 	    sp->name, fcport->port_name,
4483 	    sp->handle, fcport->loop_id, fcport->d_id.b24);
4484 	return rval;
4485 
4486 done_free_sp:
4487 	sp->free(sp);
4488 	fcport->flags &= ~FCF_ASYNC_SENT;
4489 done:
4490 	return rval;
4491 }
4492 
4493 int qla24xx_post_gfpnid_work(struct scsi_qla_host *vha, fc_port_t *fcport)
4494 {
4495 	struct qla_work_evt *e;
4496 	int ls;
4497 
4498 	ls = atomic_read(&vha->loop_state);
4499 	if (((ls != LOOP_READY) && (ls != LOOP_UP)) ||
4500 		test_bit(UNLOADING, &vha->dpc_flags))
4501 		return 0;
4502 
4503 	e = qla2x00_alloc_work(vha, QLA_EVT_GFPNID);
4504 	if (!e)
4505 		return QLA_FUNCTION_FAILED;
4506 
4507 	e->u.fcport.fcport = fcport;
4508 	return qla2x00_post_work(vha, e);
4509 }
4510