xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_gs.c (revision a99237af)
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 /* GID_PN completion processing. */
2977 void qla24xx_handle_gidpn_event(scsi_qla_host_t *vha, struct event_arg *ea)
2978 {
2979 	fc_port_t *fcport = ea->fcport;
2980 
2981 	ql_dbg(ql_dbg_disc, vha, 0x201d,
2982 	    "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
2983 	    __func__, fcport->port_name, fcport->disc_state,
2984 	    fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
2985 	    fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
2986 
2987 	if (fcport->disc_state == DSC_DELETE_PEND)
2988 		return;
2989 
2990 	if (ea->sp->gen2 != fcport->login_gen) {
2991 		/* PLOGI/PRLI/LOGO came in while cmd was out.*/
2992 		ql_dbg(ql_dbg_disc, vha, 0x201e,
2993 		    "%s %8phC generation changed rscn %d|%d n",
2994 		    __func__, fcport->port_name, fcport->last_rscn_gen,
2995 		    fcport->rscn_gen);
2996 		return;
2997 	}
2998 
2999 	if (!ea->rc) {
3000 		if (ea->sp->gen1 == fcport->rscn_gen) {
3001 			fcport->scan_state = QLA_FCPORT_FOUND;
3002 			fcport->flags |= FCF_FABRIC_DEVICE;
3003 
3004 			if (fcport->d_id.b24 == ea->id.b24) {
3005 				/* cable plugged into the same place */
3006 				switch (vha->host->active_mode) {
3007 				case MODE_TARGET:
3008 					if (fcport->fw_login_state ==
3009 					    DSC_LS_PRLI_COMP) {
3010 						u16 data[2];
3011 						/*
3012 						 * Late RSCN was delivered.
3013 						 * Remote port already login'ed.
3014 						 */
3015 						ql_dbg(ql_dbg_disc, vha, 0x201f,
3016 						    "%s %d %8phC post adisc\n",
3017 						    __func__, __LINE__,
3018 						    fcport->port_name);
3019 						data[0] = data[1] = 0;
3020 						qla2x00_post_async_adisc_work(
3021 						    vha, fcport, data);
3022 					}
3023 					break;
3024 				case MODE_INITIATOR:
3025 				case MODE_DUAL:
3026 				default:
3027 					ql_dbg(ql_dbg_disc, vha, 0x201f,
3028 					    "%s %d %8phC post %s\n", __func__,
3029 					    __LINE__, fcport->port_name,
3030 					    (atomic_read(&fcport->state) ==
3031 					    FCS_ONLINE) ? "adisc" : "gnl");
3032 
3033 					if (atomic_read(&fcport->state) ==
3034 					    FCS_ONLINE) {
3035 						u16 data[2];
3036 
3037 						data[0] = data[1] = 0;
3038 						qla2x00_post_async_adisc_work(
3039 						    vha, fcport, data);
3040 					} else {
3041 						qla24xx_post_gnl_work(vha,
3042 						    fcport);
3043 					}
3044 					break;
3045 				}
3046 			} else { /* fcport->d_id.b24 != ea->id.b24 */
3047 				fcport->d_id.b24 = ea->id.b24;
3048 				fcport->id_changed = 1;
3049 				if (fcport->deleted != QLA_SESS_DELETED) {
3050 					ql_dbg(ql_dbg_disc, vha, 0x2021,
3051 					    "%s %d %8phC post del sess\n",
3052 					    __func__, __LINE__, fcport->port_name);
3053 					qlt_schedule_sess_for_deletion(fcport);
3054 				}
3055 			}
3056 		} else { /* ea->sp->gen1 != fcport->rscn_gen */
3057 			ql_dbg(ql_dbg_disc, vha, 0x2022,
3058 			    "%s %d %8phC post gidpn\n",
3059 			    __func__, __LINE__, fcport->port_name);
3060 			/* rscn came in while cmd was out */
3061 			qla24xx_post_gidpn_work(vha, fcport);
3062 		}
3063 	} else { /* ea->rc */
3064 		/* cable pulled */
3065 		if (ea->sp->gen1 == fcport->rscn_gen) {
3066 			if (ea->sp->gen2 == fcport->login_gen) {
3067 				ql_dbg(ql_dbg_disc, vha, 0x2042,
3068 				    "%s %d %8phC post del sess\n", __func__,
3069 				    __LINE__, fcport->port_name);
3070 				qlt_schedule_sess_for_deletion(fcport);
3071 			} else {
3072 				ql_dbg(ql_dbg_disc, vha, 0x2045,
3073 				    "%s %d %8phC login\n", __func__, __LINE__,
3074 				    fcport->port_name);
3075 				qla24xx_fcport_handle_login(vha, fcport);
3076 			}
3077 		} else {
3078 			ql_dbg(ql_dbg_disc, vha, 0x2049,
3079 			    "%s %d %8phC post gidpn\n", __func__, __LINE__,
3080 			    fcport->port_name);
3081 			qla24xx_post_gidpn_work(vha, fcport);
3082 		}
3083 	}
3084 } /* gidpn_event */
3085 
3086 static void qla2x00_async_gidpn_sp_done(void *s, int res)
3087 {
3088 	struct srb *sp = s;
3089 	struct scsi_qla_host *vha = sp->vha;
3090 	fc_port_t *fcport = sp->fcport;
3091 	u8 *id = fcport->ct_desc.ct_sns->p.rsp.rsp.gid_pn.port_id;
3092 	struct event_arg ea;
3093 
3094 	fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
3095 
3096 	memset(&ea, 0, sizeof(ea));
3097 	ea.fcport = fcport;
3098 	ea.id.b.domain = id[0];
3099 	ea.id.b.area = id[1];
3100 	ea.id.b.al_pa = id[2];
3101 	ea.sp = sp;
3102 	ea.rc = res;
3103 	ea.event = FCME_GIDPN_DONE;
3104 
3105 	if (res == QLA_FUNCTION_TIMEOUT) {
3106 		ql_dbg(ql_dbg_disc, sp->vha, 0xffff,
3107 		    "Async done-%s WWPN %8phC timed out.\n",
3108 		    sp->name, fcport->port_name);
3109 		qla24xx_post_gidpn_work(sp->vha, fcport);
3110 		sp->free(sp);
3111 		return;
3112 	} else if (res) {
3113 		ql_dbg(ql_dbg_disc, sp->vha, 0xffff,
3114 		    "Async done-%s fail res %x, WWPN %8phC\n",
3115 		    sp->name, res, fcport->port_name);
3116 	} else {
3117 		ql_dbg(ql_dbg_disc, vha, 0x204f,
3118 		    "Async done-%s good WWPN %8phC ID %3phC\n",
3119 		    sp->name, fcport->port_name, id);
3120 	}
3121 
3122 	qla2x00_fcport_event_handler(vha, &ea);
3123 
3124 	sp->free(sp);
3125 }
3126 
3127 int qla24xx_async_gidpn(scsi_qla_host_t *vha, fc_port_t *fcport)
3128 {
3129 	int rval = QLA_FUNCTION_FAILED;
3130 	struct ct_sns_req       *ct_req;
3131 	srb_t *sp;
3132 
3133 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
3134 		return rval;
3135 
3136 	fcport->disc_state = DSC_GID_PN;
3137 	fcport->scan_state = QLA_FCPORT_SCAN;
3138 	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
3139 	if (!sp)
3140 		goto done;
3141 
3142 	fcport->flags |= FCF_ASYNC_SENT;
3143 	sp->type = SRB_CT_PTHRU_CMD;
3144 	sp->name = "gidpn";
3145 	sp->gen1 = fcport->rscn_gen;
3146 	sp->gen2 = fcport->login_gen;
3147 
3148 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
3149 
3150 	/* CT_IU preamble  */
3151 	ct_req = qla2x00_prep_ct_req(fcport->ct_desc.ct_sns, GID_PN_CMD,
3152 		GID_PN_RSP_SIZE);
3153 
3154 	/* GIDPN req */
3155 	memcpy(ct_req->req.gid_pn.port_name, fcport->port_name,
3156 		WWN_SIZE);
3157 
3158 	/* req & rsp use the same buffer */
3159 	sp->u.iocb_cmd.u.ctarg.req = fcport->ct_desc.ct_sns;
3160 	sp->u.iocb_cmd.u.ctarg.req_dma = fcport->ct_desc.ct_sns_dma;
3161 	sp->u.iocb_cmd.u.ctarg.rsp = fcport->ct_desc.ct_sns;
3162 	sp->u.iocb_cmd.u.ctarg.rsp_dma = fcport->ct_desc.ct_sns_dma;
3163 	sp->u.iocb_cmd.u.ctarg.req_size = GID_PN_REQ_SIZE;
3164 	sp->u.iocb_cmd.u.ctarg.rsp_size = GID_PN_RSP_SIZE;
3165 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
3166 
3167 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
3168 	sp->done = qla2x00_async_gidpn_sp_done;
3169 
3170 	rval = qla2x00_start_sp(sp);
3171 	if (rval != QLA_SUCCESS)
3172 		goto done_free_sp;
3173 
3174 	ql_dbg(ql_dbg_disc, vha, 0x20a4,
3175 	    "Async-%s - %8phC hdl=%x loopid=%x portid %02x%02x%02x.\n",
3176 	    sp->name, fcport->port_name,
3177 	    sp->handle, fcport->loop_id, fcport->d_id.b.domain,
3178 	    fcport->d_id.b.area, fcport->d_id.b.al_pa);
3179 	return rval;
3180 
3181 done_free_sp:
3182 	sp->free(sp);
3183 done:
3184 	fcport->flags &= ~FCF_ASYNC_ACTIVE;
3185 	return rval;
3186 }
3187 
3188 int qla24xx_post_gidpn_work(struct scsi_qla_host *vha, fc_port_t *fcport)
3189 {
3190 	struct qla_work_evt *e;
3191 	int ls;
3192 
3193 	ls = atomic_read(&vha->loop_state);
3194 	if (((ls != LOOP_READY) && (ls != LOOP_UP)) ||
3195 		test_bit(UNLOADING, &vha->dpc_flags))
3196 		return 0;
3197 
3198 	e = qla2x00_alloc_work(vha, QLA_EVT_GIDPN);
3199 	if (!e)
3200 		return QLA_FUNCTION_FAILED;
3201 
3202 	e->u.fcport.fcport = fcport;
3203 	fcport->flags |= FCF_ASYNC_ACTIVE;
3204 	return qla2x00_post_work(vha, e);
3205 }
3206 
3207 int qla24xx_post_gpsc_work(struct scsi_qla_host *vha, fc_port_t *fcport)
3208 {
3209 	struct qla_work_evt *e;
3210 
3211 	e = qla2x00_alloc_work(vha, QLA_EVT_GPSC);
3212 	if (!e)
3213 		return QLA_FUNCTION_FAILED;
3214 
3215 	e->u.fcport.fcport = fcport;
3216 	fcport->flags |= FCF_ASYNC_ACTIVE;
3217 	return qla2x00_post_work(vha, e);
3218 }
3219 
3220 void qla24xx_handle_gpsc_event(scsi_qla_host_t *vha, struct event_arg *ea)
3221 {
3222 	struct fc_port *fcport = ea->fcport;
3223 
3224 	ql_dbg(ql_dbg_disc, vha, 0x20d8,
3225 	    "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
3226 	    __func__, fcport->port_name, fcport->disc_state,
3227 	    fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
3228 	    ea->sp->gen2, fcport->rscn_gen|ea->sp->gen1, fcport->loop_id);
3229 
3230 	if (fcport->disc_state == DSC_DELETE_PEND)
3231 		return;
3232 
3233 	if (ea->sp->gen2 != fcport->login_gen) {
3234 		/* target side must have changed it. */
3235 		ql_dbg(ql_dbg_disc, vha, 0x20d3,
3236 		    "%s %8phC generation changed\n",
3237 		    __func__, fcport->port_name);
3238 		return;
3239 	} else if (ea->sp->gen1 != fcport->rscn_gen) {
3240 		ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
3241 		    __func__, __LINE__, fcport->port_name);
3242 		qla24xx_post_gidpn_work(vha, fcport);
3243 		return;
3244 	}
3245 
3246 	qla_post_iidma_work(vha, fcport);
3247 }
3248 
3249 static void qla24xx_async_gpsc_sp_done(void *s, int res)
3250 {
3251 	struct srb *sp = s;
3252 	struct scsi_qla_host *vha = sp->vha;
3253 	struct qla_hw_data *ha = vha->hw;
3254 	fc_port_t *fcport = sp->fcport;
3255 	struct ct_sns_rsp       *ct_rsp;
3256 	struct event_arg ea;
3257 
3258 	ct_rsp = &fcport->ct_desc.ct_sns->p.rsp;
3259 
3260 	ql_dbg(ql_dbg_disc, vha, 0x2053,
3261 	    "Async done-%s res %x, WWPN %8phC \n",
3262 	    sp->name, res, fcport->port_name);
3263 
3264 	if (res == (DID_ERROR << 16)) {
3265 		/* entry status error */
3266 		goto done;
3267 	} else if (res) {
3268 		if ((ct_rsp->header.reason_code ==
3269 			 CT_REASON_INVALID_COMMAND_CODE) ||
3270 			(ct_rsp->header.reason_code ==
3271 			CT_REASON_COMMAND_UNSUPPORTED)) {
3272 			ql_dbg(ql_dbg_disc, vha, 0x2019,
3273 			    "GPSC command unsupported, disabling query.\n");
3274 			ha->flags.gpsc_supported = 0;
3275 			res = QLA_SUCCESS;
3276 		}
3277 	} else {
3278 		switch (be16_to_cpu(ct_rsp->rsp.gpsc.speed)) {
3279 		case BIT_15:
3280 			fcport->fp_speed = PORT_SPEED_1GB;
3281 			break;
3282 		case BIT_14:
3283 			fcport->fp_speed = PORT_SPEED_2GB;
3284 			break;
3285 		case BIT_13:
3286 			fcport->fp_speed = PORT_SPEED_4GB;
3287 			break;
3288 		case BIT_12:
3289 			fcport->fp_speed = PORT_SPEED_10GB;
3290 			break;
3291 		case BIT_11:
3292 			fcport->fp_speed = PORT_SPEED_8GB;
3293 			break;
3294 		case BIT_10:
3295 			fcport->fp_speed = PORT_SPEED_16GB;
3296 			break;
3297 		case BIT_8:
3298 			fcport->fp_speed = PORT_SPEED_32GB;
3299 			break;
3300 		}
3301 
3302 		ql_dbg(ql_dbg_disc, vha, 0x2054,
3303 		    "Async-%s OUT WWPN %8phC speeds=%04x speed=%04x.\n",
3304 		    sp->name, fcport->fabric_port_name,
3305 		    be16_to_cpu(ct_rsp->rsp.gpsc.speeds),
3306 		    be16_to_cpu(ct_rsp->rsp.gpsc.speed));
3307 	}
3308 done:
3309 	memset(&ea, 0, sizeof(ea));
3310 	ea.event = FCME_GPSC_DONE;
3311 	ea.rc = res;
3312 	ea.fcport = fcport;
3313 	ea.sp = sp;
3314 	qla2x00_fcport_event_handler(vha, &ea);
3315 
3316 	sp->free(sp);
3317 }
3318 
3319 int qla24xx_async_gpsc(scsi_qla_host_t *vha, fc_port_t *fcport)
3320 {
3321 	int rval = QLA_FUNCTION_FAILED;
3322 	struct ct_sns_req       *ct_req;
3323 	srb_t *sp;
3324 
3325 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
3326 		return rval;
3327 
3328 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
3329 	if (!sp)
3330 		goto done;
3331 
3332 	sp->type = SRB_CT_PTHRU_CMD;
3333 	sp->name = "gpsc";
3334 	sp->gen1 = fcport->rscn_gen;
3335 	sp->gen2 = fcport->login_gen;
3336 
3337 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
3338 
3339 	/* CT_IU preamble  */
3340 	ct_req = qla24xx_prep_ct_fm_req(fcport->ct_desc.ct_sns, GPSC_CMD,
3341 		GPSC_RSP_SIZE);
3342 
3343 	/* GPSC req */
3344 	memcpy(ct_req->req.gpsc.port_name, fcport->fabric_port_name,
3345 		WWN_SIZE);
3346 
3347 	sp->u.iocb_cmd.u.ctarg.req = fcport->ct_desc.ct_sns;
3348 	sp->u.iocb_cmd.u.ctarg.req_dma = fcport->ct_desc.ct_sns_dma;
3349 	sp->u.iocb_cmd.u.ctarg.rsp = fcport->ct_desc.ct_sns;
3350 	sp->u.iocb_cmd.u.ctarg.rsp_dma = fcport->ct_desc.ct_sns_dma;
3351 	sp->u.iocb_cmd.u.ctarg.req_size = GPSC_REQ_SIZE;
3352 	sp->u.iocb_cmd.u.ctarg.rsp_size = GPSC_RSP_SIZE;
3353 	sp->u.iocb_cmd.u.ctarg.nport_handle = vha->mgmt_svr_loop_id;
3354 
3355 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
3356 	sp->done = qla24xx_async_gpsc_sp_done;
3357 
3358 	rval = qla2x00_start_sp(sp);
3359 	if (rval != QLA_SUCCESS)
3360 		goto done_free_sp;
3361 
3362 	ql_dbg(ql_dbg_disc, vha, 0x205e,
3363 	    "Async-%s %8phC hdl=%x loopid=%x portid=%02x%02x%02x.\n",
3364 	    sp->name, fcport->port_name, sp->handle,
3365 	    fcport->loop_id, fcport->d_id.b.domain,
3366 	    fcport->d_id.b.area, fcport->d_id.b.al_pa);
3367 	return rval;
3368 
3369 done_free_sp:
3370 	sp->free(sp);
3371 	fcport->flags &= ~FCF_ASYNC_SENT;
3372 done:
3373 	fcport->flags &= ~FCF_ASYNC_ACTIVE;
3374 	return rval;
3375 }
3376 
3377 int qla24xx_post_gpnid_work(struct scsi_qla_host *vha, port_id_t *id)
3378 {
3379 	struct qla_work_evt *e;
3380 
3381 	if (test_bit(UNLOADING, &vha->dpc_flags))
3382 		return 0;
3383 
3384 	e = qla2x00_alloc_work(vha, QLA_EVT_GPNID);
3385 	if (!e)
3386 		return QLA_FUNCTION_FAILED;
3387 
3388 	e->u.gpnid.id = *id;
3389 	return qla2x00_post_work(vha, e);
3390 }
3391 
3392 void qla24xx_sp_unmap(scsi_qla_host_t *vha, srb_t *sp)
3393 {
3394 	struct srb_iocb *c = &sp->u.iocb_cmd;
3395 
3396 	switch (sp->type) {
3397 	case SRB_ELS_DCMD:
3398 		if (c->u.els_plogi.els_plogi_pyld)
3399 			dma_free_coherent(&vha->hw->pdev->dev,
3400 			    c->u.els_plogi.tx_size,
3401 			    c->u.els_plogi.els_plogi_pyld,
3402 			    c->u.els_plogi.els_plogi_pyld_dma);
3403 
3404 		if (c->u.els_plogi.els_resp_pyld)
3405 			dma_free_coherent(&vha->hw->pdev->dev,
3406 			    c->u.els_plogi.rx_size,
3407 			    c->u.els_plogi.els_resp_pyld,
3408 			    c->u.els_plogi.els_resp_pyld_dma);
3409 		break;
3410 	case SRB_CT_PTHRU_CMD:
3411 	default:
3412 		if (sp->u.iocb_cmd.u.ctarg.req) {
3413 			dma_free_coherent(&vha->hw->pdev->dev,
3414 			    sp->u.iocb_cmd.u.ctarg.req_allocated_size,
3415 			    sp->u.iocb_cmd.u.ctarg.req,
3416 			    sp->u.iocb_cmd.u.ctarg.req_dma);
3417 			sp->u.iocb_cmd.u.ctarg.req = NULL;
3418 		}
3419 
3420 		if (sp->u.iocb_cmd.u.ctarg.rsp) {
3421 			dma_free_coherent(&vha->hw->pdev->dev,
3422 			    sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
3423 			    sp->u.iocb_cmd.u.ctarg.rsp,
3424 			    sp->u.iocb_cmd.u.ctarg.rsp_dma);
3425 			sp->u.iocb_cmd.u.ctarg.rsp = NULL;
3426 		}
3427 		break;
3428 	}
3429 
3430 	sp->free(sp);
3431 }
3432 
3433 void qla24xx_handle_gpnid_event(scsi_qla_host_t *vha, struct event_arg *ea)
3434 {
3435 	fc_port_t *fcport, *conflict, *t;
3436 	u16 data[2];
3437 
3438 	ql_dbg(ql_dbg_disc, vha, 0xffff,
3439 	    "%s %d port_id: %06x\n",
3440 	    __func__, __LINE__, ea->id.b24);
3441 
3442 	if (ea->rc) {
3443 		/* cable is disconnected */
3444 		list_for_each_entry_safe(fcport, t, &vha->vp_fcports, list) {
3445 			if (fcport->d_id.b24 == ea->id.b24) {
3446 				ql_dbg(ql_dbg_disc, vha, 0xffff,
3447 				    "%s %d %8phC DS %d\n",
3448 				    __func__, __LINE__,
3449 				    fcport->port_name,
3450 				    fcport->disc_state);
3451 				fcport->scan_state = QLA_FCPORT_SCAN;
3452 				switch (fcport->disc_state) {
3453 				case DSC_DELETED:
3454 				case DSC_DELETE_PEND:
3455 					break;
3456 				default:
3457 					ql_dbg(ql_dbg_disc, vha, 0xffff,
3458 					    "%s %d %8phC post del sess\n",
3459 					    __func__, __LINE__,
3460 					    fcport->port_name);
3461 					qlt_schedule_sess_for_deletion(fcport);
3462 					break;
3463 				}
3464 			}
3465 		}
3466 	} else {
3467 		/* cable is connected */
3468 		fcport = qla2x00_find_fcport_by_wwpn(vha, ea->port_name, 1);
3469 		if (fcport) {
3470 			list_for_each_entry_safe(conflict, t, &vha->vp_fcports,
3471 			    list) {
3472 				if ((conflict->d_id.b24 == ea->id.b24) &&
3473 				    (fcport != conflict)) {
3474 					/* 2 fcports with conflict Nport ID or
3475 					 * an existing fcport is having nport ID
3476 					 * conflict with new fcport.
3477 					 */
3478 
3479 					ql_dbg(ql_dbg_disc, vha, 0xffff,
3480 					    "%s %d %8phC DS %d\n",
3481 					    __func__, __LINE__,
3482 					    conflict->port_name,
3483 					    conflict->disc_state);
3484 					conflict->scan_state = QLA_FCPORT_SCAN;
3485 					switch (conflict->disc_state) {
3486 					case DSC_DELETED:
3487 					case DSC_DELETE_PEND:
3488 						break;
3489 					default:
3490 						ql_dbg(ql_dbg_disc, vha, 0xffff,
3491 						    "%s %d %8phC post del sess\n",
3492 						    __func__, __LINE__,
3493 						    conflict->port_name);
3494 						qlt_schedule_sess_for_deletion
3495 							(conflict);
3496 						break;
3497 					}
3498 				}
3499 			}
3500 
3501 			fcport->rscn_gen++;
3502 			fcport->scan_state = QLA_FCPORT_FOUND;
3503 			fcport->flags |= FCF_FABRIC_DEVICE;
3504 			if (fcport->login_retry == 0) {
3505 				fcport->login_retry =
3506 					vha->hw->login_retry_count;
3507 				ql_dbg(ql_dbg_disc, vha, 0xffff,
3508 				    "Port login retry %8phN, lid 0x%04x cnt=%d.\n",
3509 				    fcport->port_name, fcport->loop_id,
3510 				    fcport->login_retry);
3511 			}
3512 			switch (fcport->disc_state) {
3513 			case DSC_LOGIN_COMPLETE:
3514 				/* recheck session is still intact. */
3515 				ql_dbg(ql_dbg_disc, vha, 0x210d,
3516 				    "%s %d %8phC revalidate session with ADISC\n",
3517 				    __func__, __LINE__, fcport->port_name);
3518 				data[0] = data[1] = 0;
3519 				qla2x00_post_async_adisc_work(vha, fcport,
3520 				    data);
3521 				break;
3522 			case DSC_DELETED:
3523 				ql_dbg(ql_dbg_disc, vha, 0x210d,
3524 				    "%s %d %8phC login\n", __func__, __LINE__,
3525 				    fcport->port_name);
3526 				fcport->d_id = ea->id;
3527 				qla24xx_fcport_handle_login(vha, fcport);
3528 				break;
3529 			case DSC_DELETE_PEND:
3530 				fcport->d_id = ea->id;
3531 				break;
3532 			default:
3533 				fcport->d_id = ea->id;
3534 				break;
3535 			}
3536 		} else {
3537 			list_for_each_entry_safe(conflict, t, &vha->vp_fcports,
3538 			    list) {
3539 				if (conflict->d_id.b24 == ea->id.b24) {
3540 					/* 2 fcports with conflict Nport ID or
3541 					 * an existing fcport is having nport ID
3542 					 * conflict with new fcport.
3543 					 */
3544 					ql_dbg(ql_dbg_disc, vha, 0xffff,
3545 					    "%s %d %8phC DS %d\n",
3546 					    __func__, __LINE__,
3547 					    conflict->port_name,
3548 					    conflict->disc_state);
3549 
3550 					conflict->scan_state = QLA_FCPORT_SCAN;
3551 					switch (conflict->disc_state) {
3552 					case DSC_DELETED:
3553 					case DSC_DELETE_PEND:
3554 						break;
3555 					default:
3556 						ql_dbg(ql_dbg_disc, vha, 0xffff,
3557 						    "%s %d %8phC post del sess\n",
3558 						    __func__, __LINE__,
3559 						    conflict->port_name);
3560 						qlt_schedule_sess_for_deletion
3561 							(conflict);
3562 						break;
3563 					}
3564 				}
3565 			}
3566 
3567 			/* create new fcport */
3568 			ql_dbg(ql_dbg_disc, vha, 0x2065,
3569 			    "%s %d %8phC post new sess\n",
3570 			    __func__, __LINE__, ea->port_name);
3571 			qla24xx_post_newsess_work(vha, &ea->id,
3572 			    ea->port_name, NULL, NULL, FC4_TYPE_UNKNOWN);
3573 		}
3574 	}
3575 }
3576 
3577 static void qla2x00_async_gpnid_sp_done(void *s, int res)
3578 {
3579 	struct srb *sp = s;
3580 	struct scsi_qla_host *vha = sp->vha;
3581 	struct ct_sns_req *ct_req =
3582 	    (struct ct_sns_req *)sp->u.iocb_cmd.u.ctarg.req;
3583 	struct ct_sns_rsp *ct_rsp =
3584 	    (struct ct_sns_rsp *)sp->u.iocb_cmd.u.ctarg.rsp;
3585 	struct event_arg ea;
3586 	struct qla_work_evt *e;
3587 	unsigned long flags;
3588 
3589 	if (res)
3590 		ql_dbg(ql_dbg_disc, vha, 0x2066,
3591 		    "Async done-%s fail res %x rscn gen %d ID %3phC. %8phC\n",
3592 		    sp->name, res, sp->gen1, ct_req->req.port_id.port_id,
3593 		    ct_rsp->rsp.gpn_id.port_name);
3594 	else
3595 		ql_dbg(ql_dbg_disc, vha, 0x2066,
3596 		    "Async done-%s good rscn gen %d ID %3phC. %8phC\n",
3597 		    sp->name, sp->gen1, ct_req->req.port_id.port_id,
3598 		    ct_rsp->rsp.gpn_id.port_name);
3599 
3600 	memset(&ea, 0, sizeof(ea));
3601 	memcpy(ea.port_name, ct_rsp->rsp.gpn_id.port_name, WWN_SIZE);
3602 	ea.sp = sp;
3603 	ea.id.b.domain = ct_req->req.port_id.port_id[0];
3604 	ea.id.b.area = ct_req->req.port_id.port_id[1];
3605 	ea.id.b.al_pa = ct_req->req.port_id.port_id[2];
3606 	ea.rc = res;
3607 	ea.event = FCME_GPNID_DONE;
3608 
3609 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
3610 	list_del(&sp->elem);
3611 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
3612 
3613 	if (res) {
3614 		if (res == QLA_FUNCTION_TIMEOUT) {
3615 			qla24xx_post_gpnid_work(sp->vha, &ea.id);
3616 			sp->free(sp);
3617 			return;
3618 		}
3619 	} else if (sp->gen1) {
3620 		/* There was another RSCN for this Nport ID */
3621 		qla24xx_post_gpnid_work(sp->vha, &ea.id);
3622 		sp->free(sp);
3623 		return;
3624 	}
3625 
3626 	qla2x00_fcport_event_handler(vha, &ea);
3627 
3628 	e = qla2x00_alloc_work(vha, QLA_EVT_UNMAP);
3629 	if (!e) {
3630 		/* please ignore kernel warning. otherwise, we have mem leak. */
3631 		if (sp->u.iocb_cmd.u.ctarg.req) {
3632 			dma_free_coherent(&vha->hw->pdev->dev,
3633 				sp->u.iocb_cmd.u.ctarg.req_allocated_size,
3634 				sp->u.iocb_cmd.u.ctarg.req,
3635 				sp->u.iocb_cmd.u.ctarg.req_dma);
3636 			sp->u.iocb_cmd.u.ctarg.req = NULL;
3637 		}
3638 		if (sp->u.iocb_cmd.u.ctarg.rsp) {
3639 			dma_free_coherent(&vha->hw->pdev->dev,
3640 				sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
3641 				sp->u.iocb_cmd.u.ctarg.rsp,
3642 				sp->u.iocb_cmd.u.ctarg.rsp_dma);
3643 			sp->u.iocb_cmd.u.ctarg.rsp = NULL;
3644 		}
3645 
3646 		sp->free(sp);
3647 		return;
3648 	}
3649 
3650 	e->u.iosb.sp = sp;
3651 	qla2x00_post_work(vha, e);
3652 }
3653 
3654 /* Get WWPN with Nport ID. */
3655 int qla24xx_async_gpnid(scsi_qla_host_t *vha, port_id_t *id)
3656 {
3657 	int rval = QLA_FUNCTION_FAILED;
3658 	struct ct_sns_req       *ct_req;
3659 	srb_t *sp, *tsp;
3660 	struct ct_sns_pkt *ct_sns;
3661 	unsigned long flags;
3662 
3663 	if (!vha->flags.online)
3664 		goto done;
3665 
3666 	sp = qla2x00_get_sp(vha, NULL, GFP_KERNEL);
3667 	if (!sp)
3668 		goto done;
3669 
3670 	sp->type = SRB_CT_PTHRU_CMD;
3671 	sp->name = "gpnid";
3672 	sp->u.iocb_cmd.u.ctarg.id = *id;
3673 	sp->gen1 = 0;
3674 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
3675 
3676 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
3677 	list_for_each_entry(tsp, &vha->gpnid_list, elem) {
3678 		if (tsp->u.iocb_cmd.u.ctarg.id.b24 == id->b24) {
3679 			tsp->gen1++;
3680 			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
3681 			sp->free(sp);
3682 			goto done;
3683 		}
3684 	}
3685 	list_add_tail(&sp->elem, &vha->gpnid_list);
3686 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
3687 
3688 	sp->u.iocb_cmd.u.ctarg.req = dma_alloc_coherent(&vha->hw->pdev->dev,
3689 		sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.req_dma,
3690 		GFP_KERNEL);
3691 	sp->u.iocb_cmd.u.ctarg.req_allocated_size = sizeof(struct ct_sns_pkt);
3692 	if (!sp->u.iocb_cmd.u.ctarg.req) {
3693 		ql_log(ql_log_warn, vha, 0xd041,
3694 		    "Failed to allocate ct_sns request.\n");
3695 		goto done_free_sp;
3696 	}
3697 
3698 	sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
3699 		sizeof(struct ct_sns_pkt), &sp->u.iocb_cmd.u.ctarg.rsp_dma,
3700 		GFP_KERNEL);
3701 	sp->u.iocb_cmd.u.ctarg.rsp_allocated_size = sizeof(struct ct_sns_pkt);
3702 	if (!sp->u.iocb_cmd.u.ctarg.rsp) {
3703 		ql_log(ql_log_warn, vha, 0xd042,
3704 		    "Failed to allocate ct_sns request.\n");
3705 		goto done_free_sp;
3706 	}
3707 
3708 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.rsp;
3709 	memset(ct_sns, 0, sizeof(*ct_sns));
3710 
3711 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
3712 	/* CT_IU preamble  */
3713 	ct_req = qla2x00_prep_ct_req(ct_sns, GPN_ID_CMD, GPN_ID_RSP_SIZE);
3714 
3715 	/* GPN_ID req */
3716 	ct_req->req.port_id.port_id[0] = id->b.domain;
3717 	ct_req->req.port_id.port_id[1] = id->b.area;
3718 	ct_req->req.port_id.port_id[2] = id->b.al_pa;
3719 
3720 	sp->u.iocb_cmd.u.ctarg.req_size = GPN_ID_REQ_SIZE;
3721 	sp->u.iocb_cmd.u.ctarg.rsp_size = GPN_ID_RSP_SIZE;
3722 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
3723 
3724 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
3725 	sp->done = qla2x00_async_gpnid_sp_done;
3726 
3727 	rval = qla2x00_start_sp(sp);
3728 	if (rval != QLA_SUCCESS)
3729 		goto done_free_sp;
3730 
3731 	ql_dbg(ql_dbg_disc, vha, 0x2067,
3732 	    "Async-%s hdl=%x ID %3phC.\n", sp->name,
3733 	    sp->handle, ct_req->req.port_id.port_id);
3734 	return rval;
3735 
3736 done_free_sp:
3737 	spin_lock_irqsave(&vha->hw->vport_slock, flags);
3738 	list_del(&sp->elem);
3739 	spin_unlock_irqrestore(&vha->hw->vport_slock, flags);
3740 
3741 	if (sp->u.iocb_cmd.u.ctarg.req) {
3742 		dma_free_coherent(&vha->hw->pdev->dev,
3743 			sizeof(struct ct_sns_pkt),
3744 			sp->u.iocb_cmd.u.ctarg.req,
3745 			sp->u.iocb_cmd.u.ctarg.req_dma);
3746 		sp->u.iocb_cmd.u.ctarg.req = NULL;
3747 	}
3748 	if (sp->u.iocb_cmd.u.ctarg.rsp) {
3749 		dma_free_coherent(&vha->hw->pdev->dev,
3750 			sizeof(struct ct_sns_pkt),
3751 			sp->u.iocb_cmd.u.ctarg.rsp,
3752 			sp->u.iocb_cmd.u.ctarg.rsp_dma);
3753 		sp->u.iocb_cmd.u.ctarg.rsp = NULL;
3754 	}
3755 
3756 	sp->free(sp);
3757 done:
3758 	return rval;
3759 }
3760 
3761 void qla24xx_handle_gffid_event(scsi_qla_host_t *vha, struct event_arg *ea)
3762 {
3763        fc_port_t *fcport = ea->fcport;
3764 
3765        qla24xx_post_gnl_work(vha, fcport);
3766 }
3767 
3768 void qla24xx_async_gffid_sp_done(void *s, int res)
3769 {
3770        struct srb *sp = s;
3771        struct scsi_qla_host *vha = sp->vha;
3772        fc_port_t *fcport = sp->fcport;
3773        struct ct_sns_rsp *ct_rsp;
3774        struct event_arg ea;
3775 
3776        ql_dbg(ql_dbg_disc, vha, 0x2133,
3777 	   "Async done-%s res %x ID %x. %8phC\n",
3778 	   sp->name, res, fcport->d_id.b24, fcport->port_name);
3779 
3780        fcport->flags &= ~FCF_ASYNC_SENT;
3781        ct_rsp = &fcport->ct_desc.ct_sns->p.rsp;
3782        /*
3783 	* FC-GS-7, 5.2.3.12 FC-4 Features - format
3784 	* The format of the FC-4 Features object, as defined by the FC-4,
3785 	* Shall be an array of 4-bit values, one for each type code value
3786 	*/
3787        if (!res) {
3788 	       if (ct_rsp->rsp.gff_id.fc4_features[GFF_FCP_SCSI_OFFSET] & 0xf) {
3789 		       /* w1 b00:03 */
3790 		       fcport->fc4_type =
3791 			   ct_rsp->rsp.gff_id.fc4_features[GFF_FCP_SCSI_OFFSET];
3792 		       fcport->fc4_type &= 0xf;
3793 	       }
3794 
3795 	       if (ct_rsp->rsp.gff_id.fc4_features[GFF_NVME_OFFSET] & 0xf) {
3796 		       /* w5 [00:03]/28h */
3797 		       fcport->fc4f_nvme =
3798 			   ct_rsp->rsp.gff_id.fc4_features[GFF_NVME_OFFSET];
3799 		       fcport->fc4f_nvme &= 0xf;
3800 	       }
3801        }
3802 
3803        memset(&ea, 0, sizeof(ea));
3804        ea.sp = sp;
3805        ea.fcport = sp->fcport;
3806        ea.rc = res;
3807        ea.event = FCME_GFFID_DONE;
3808 
3809        qla2x00_fcport_event_handler(vha, &ea);
3810        sp->free(sp);
3811 }
3812 
3813 /* Get FC4 Feature with Nport ID. */
3814 int qla24xx_async_gffid(scsi_qla_host_t *vha, fc_port_t *fcport)
3815 {
3816 	int rval = QLA_FUNCTION_FAILED;
3817 	struct ct_sns_req       *ct_req;
3818 	srb_t *sp;
3819 
3820 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
3821 		return rval;
3822 
3823 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
3824 	if (!sp)
3825 		return rval;
3826 
3827 	fcport->flags |= FCF_ASYNC_SENT;
3828 	sp->type = SRB_CT_PTHRU_CMD;
3829 	sp->name = "gffid";
3830 	sp->gen1 = fcport->rscn_gen;
3831 	sp->gen2 = fcport->login_gen;
3832 
3833 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
3834 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
3835 
3836 	/* CT_IU preamble  */
3837 	ct_req = qla2x00_prep_ct_req(fcport->ct_desc.ct_sns, GFF_ID_CMD,
3838 	    GFF_ID_RSP_SIZE);
3839 
3840 	ct_req->req.gff_id.port_id[0] = fcport->d_id.b.domain;
3841 	ct_req->req.gff_id.port_id[1] = fcport->d_id.b.area;
3842 	ct_req->req.gff_id.port_id[2] = fcport->d_id.b.al_pa;
3843 
3844 	sp->u.iocb_cmd.u.ctarg.req = fcport->ct_desc.ct_sns;
3845 	sp->u.iocb_cmd.u.ctarg.req_dma = fcport->ct_desc.ct_sns_dma;
3846 	sp->u.iocb_cmd.u.ctarg.rsp = fcport->ct_desc.ct_sns;
3847 	sp->u.iocb_cmd.u.ctarg.rsp_dma = fcport->ct_desc.ct_sns_dma;
3848 	sp->u.iocb_cmd.u.ctarg.req_size = GFF_ID_REQ_SIZE;
3849 	sp->u.iocb_cmd.u.ctarg.rsp_size = GFF_ID_RSP_SIZE;
3850 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
3851 
3852 	sp->done = qla24xx_async_gffid_sp_done;
3853 
3854 	rval = qla2x00_start_sp(sp);
3855 	if (rval != QLA_SUCCESS)
3856 		goto done_free_sp;
3857 
3858 	ql_dbg(ql_dbg_disc, vha, 0x2132,
3859 	    "Async-%s hdl=%x  %8phC.\n", sp->name,
3860 	    sp->handle, fcport->port_name);
3861 
3862 	return rval;
3863 done_free_sp:
3864 	sp->free(sp);
3865 	fcport->flags &= ~FCF_ASYNC_SENT;
3866 	return rval;
3867 }
3868 
3869 /* GPN_FT + GNN_FT*/
3870 static int qla2x00_is_a_vp(scsi_qla_host_t *vha, u64 wwn)
3871 {
3872 	struct qla_hw_data *ha = vha->hw;
3873 	scsi_qla_host_t *vp;
3874 	unsigned long flags;
3875 	u64 twwn;
3876 	int rc = 0;
3877 
3878 	if (!ha->num_vhosts)
3879 		return 0;
3880 
3881 	spin_lock_irqsave(&ha->vport_slock, flags);
3882 	list_for_each_entry(vp, &ha->vp_list, list) {
3883 		twwn = wwn_to_u64(vp->port_name);
3884 		if (wwn == twwn) {
3885 			rc = 1;
3886 			break;
3887 		}
3888 	}
3889 	spin_unlock_irqrestore(&ha->vport_slock, flags);
3890 
3891 	return rc;
3892 }
3893 
3894 void qla24xx_async_gnnft_done(scsi_qla_host_t *vha, srb_t *sp)
3895 {
3896 	fc_port_t *fcport;
3897 	u32 i, rc;
3898 	bool found;
3899 	struct fab_scan_rp *rp;
3900 	unsigned long flags;
3901 	u8 recheck = 0;
3902 
3903 	ql_dbg(ql_dbg_disc, vha, 0xffff,
3904 	    "%s enter\n", __func__);
3905 
3906 	if (sp->gen1 != vha->hw->base_qpair->chip_reset) {
3907 		ql_dbg(ql_dbg_disc, vha, 0xffff,
3908 		    "%s scan stop due to chip reset %x/%x\n",
3909 		    sp->name, sp->gen1, vha->hw->base_qpair->chip_reset);
3910 		goto out;
3911 	}
3912 
3913 	rc = sp->rc;
3914 	if (rc) {
3915 		vha->scan.scan_retry++;
3916 		if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
3917 			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
3918 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
3919 		} else {
3920 			ql_dbg(ql_dbg_disc, vha, 0xffff,
3921 			    "Fabric scan failed on all retries.\n");
3922 		}
3923 		goto out;
3924 	}
3925 	vha->scan.scan_retry = 0;
3926 
3927 	list_for_each_entry(fcport, &vha->vp_fcports, list)
3928 		fcport->scan_state = QLA_FCPORT_SCAN;
3929 
3930 	for (i = 0; i < vha->hw->max_fibre_devices; i++) {
3931 		u64 wwn;
3932 
3933 		rp = &vha->scan.l[i];
3934 		found = false;
3935 
3936 		wwn = wwn_to_u64(rp->port_name);
3937 		if (wwn == 0)
3938 			continue;
3939 
3940 		if (!memcmp(rp->port_name, vha->port_name, WWN_SIZE))
3941 			continue;
3942 
3943 		/* Bypass reserved domain fields. */
3944 		if ((rp->id.b.domain & 0xf0) == 0xf0)
3945 			continue;
3946 
3947 		/* Bypass virtual ports of the same host. */
3948 		if (qla2x00_is_a_vp(vha, wwn))
3949 			continue;
3950 
3951 		list_for_each_entry(fcport, &vha->vp_fcports, list) {
3952 			if (memcmp(rp->port_name, fcport->port_name, WWN_SIZE))
3953 				continue;
3954 			fcport->rscn_rcvd = 0;
3955 			fcport->scan_state = QLA_FCPORT_FOUND;
3956 			found = true;
3957 			/*
3958 			 * If device was not a fabric device before.
3959 			 */
3960 			if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
3961 				qla2x00_clear_loop_id(fcport);
3962 				fcport->flags |= FCF_FABRIC_DEVICE;
3963 			} else if (fcport->d_id.b24 != rp->id.b24) {
3964 				qlt_schedule_sess_for_deletion(fcport);
3965 			}
3966 			fcport->d_id.b24 = rp->id.b24;
3967 			break;
3968 		}
3969 
3970 		if (!found) {
3971 			ql_dbg(ql_dbg_disc, vha, 0xffff,
3972 			    "%s %d %8phC post new sess\n",
3973 			    __func__, __LINE__, rp->port_name);
3974 			qla24xx_post_newsess_work(vha, &rp->id, rp->port_name,
3975 			    rp->node_name, NULL, rp->fc4type);
3976 		}
3977 	}
3978 
3979 	/*
3980 	 * Logout all previous fabric dev marked lost, except FCP2 devices.
3981 	 */
3982 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
3983 		if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
3984 			fcport->rscn_rcvd = 0;
3985 			continue;
3986 		}
3987 
3988 		if (fcport->scan_state != QLA_FCPORT_FOUND) {
3989 			fcport->rscn_rcvd = 0;
3990 			if ((qla_dual_mode_enabled(vha) ||
3991 				qla_ini_mode_enabled(vha)) &&
3992 			    atomic_read(&fcport->state) == FCS_ONLINE) {
3993 				qla2x00_mark_device_lost(vha, fcport,
3994 				    ql2xplogiabsentdevice, 0);
3995 
3996 				if (fcport->loop_id != FC_NO_LOOP_ID &&
3997 				    (fcport->flags & FCF_FCP2_DEVICE) == 0) {
3998 					ql_dbg(ql_dbg_disc, vha, 0x20f0,
3999 					    "%s %d %8phC post del sess\n",
4000 					    __func__, __LINE__,
4001 					    fcport->port_name);
4002 
4003 					qlt_schedule_sess_for_deletion(fcport);
4004 					continue;
4005 				}
4006 			}
4007 		} else {
4008 			if (fcport->rscn_rcvd ||
4009 			    fcport->disc_state != DSC_LOGIN_COMPLETE) {
4010 				if (fcport->login_retry == 0) {
4011 					fcport->login_retry =
4012 						vha->hw->login_retry_count;
4013 					ql_dbg(ql_dbg_disc, vha, 0x20a3,
4014 					    "Port login retry %8phN, lid 0x%04x retry cnt=%d.\n",
4015 					    fcport->port_name, fcport->loop_id,
4016 					    fcport->login_retry);
4017 				}
4018 				fcport->rscn_rcvd = 0;
4019 				qla24xx_fcport_handle_login(vha, fcport);
4020 			}
4021 		}
4022 	}
4023 
4024 	recheck = 1;
4025 out:
4026 	qla24xx_sp_unmap(vha, sp);
4027 	spin_lock_irqsave(&vha->work_lock, flags);
4028 	vha->scan.scan_flags &= ~SF_SCANNING;
4029 	spin_unlock_irqrestore(&vha->work_lock, flags);
4030 
4031 	if (recheck) {
4032 		list_for_each_entry(fcport, &vha->vp_fcports, list) {
4033 			if (fcport->rscn_rcvd) {
4034 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4035 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4036 				break;
4037 			}
4038 		}
4039 	}
4040 }
4041 
4042 static void qla2x00_find_free_fcp_nvme_slot(struct scsi_qla_host *vha,
4043 	struct srb *sp)
4044 {
4045 	struct qla_hw_data *ha = vha->hw;
4046 	int num_fibre_dev = ha->max_fibre_devices;
4047 	struct ct_sns_req *ct_req =
4048 		(struct ct_sns_req *)sp->u.iocb_cmd.u.ctarg.req;
4049 	struct ct_sns_gpnft_rsp *ct_rsp =
4050 		(struct ct_sns_gpnft_rsp *)sp->u.iocb_cmd.u.ctarg.rsp;
4051 	struct ct_sns_gpn_ft_data *d;
4052 	struct fab_scan_rp *rp;
4053 	u16 cmd = be16_to_cpu(ct_req->command);
4054 	u8 fc4_type = sp->gen2;
4055 	int i, j, k;
4056 	port_id_t id;
4057 	u8 found;
4058 	u64 wwn;
4059 
4060 	j = 0;
4061 	for (i = 0; i < num_fibre_dev; i++) {
4062 		d  = &ct_rsp->entries[i];
4063 
4064 		id.b.rsvd_1 = 0;
4065 		id.b.domain = d->port_id[0];
4066 		id.b.area   = d->port_id[1];
4067 		id.b.al_pa  = d->port_id[2];
4068 		wwn = wwn_to_u64(d->port_name);
4069 
4070 		if (id.b24 == 0 || wwn == 0)
4071 			continue;
4072 
4073 		if (fc4_type == FC4_TYPE_FCP_SCSI) {
4074 			if (cmd == GPN_FT_CMD) {
4075 				rp = &vha->scan.l[j];
4076 				rp->id = id;
4077 				memcpy(rp->port_name, d->port_name, 8);
4078 				j++;
4079 				rp->fc4type = FS_FC4TYPE_FCP;
4080 			} else {
4081 				for (k = 0; k < num_fibre_dev; k++) {
4082 					rp = &vha->scan.l[k];
4083 					if (id.b24 == rp->id.b24) {
4084 						memcpy(rp->node_name,
4085 						    d->port_name, 8);
4086 						break;
4087 					}
4088 				}
4089 			}
4090 		} else {
4091 			/* Search if the fibre device supports FC4_TYPE_NVME */
4092 			if (cmd == GPN_FT_CMD) {
4093 				found = 0;
4094 
4095 				for (k = 0; k < num_fibre_dev; k++) {
4096 					rp = &vha->scan.l[k];
4097 					if (!memcmp(rp->port_name,
4098 					    d->port_name, 8)) {
4099 						/*
4100 						 * Supports FC-NVMe & FCP
4101 						 */
4102 						rp->fc4type |= FS_FC4TYPE_NVME;
4103 						found = 1;
4104 						break;
4105 					}
4106 				}
4107 
4108 				/* We found new FC-NVMe only port */
4109 				if (!found) {
4110 					for (k = 0; k < num_fibre_dev; k++) {
4111 						rp = &vha->scan.l[k];
4112 						if (wwn_to_u64(rp->port_name)) {
4113 							continue;
4114 						} else {
4115 							rp->id = id;
4116 							memcpy(rp->port_name,
4117 							    d->port_name, 8);
4118 							rp->fc4type =
4119 							    FS_FC4TYPE_NVME;
4120 							break;
4121 						}
4122 					}
4123 				}
4124 			} else {
4125 				for (k = 0; k < num_fibre_dev; k++) {
4126 					rp = &vha->scan.l[k];
4127 					if (id.b24 == rp->id.b24) {
4128 						memcpy(rp->node_name,
4129 						    d->port_name, 8);
4130 						break;
4131 					}
4132 				}
4133 			}
4134 		}
4135 	}
4136 }
4137 
4138 static void qla2x00_async_gpnft_gnnft_sp_done(void *s, int res)
4139 {
4140 	struct srb *sp = s;
4141 	struct scsi_qla_host *vha = sp->vha;
4142 	struct qla_work_evt *e;
4143 	struct ct_sns_req *ct_req =
4144 		(struct ct_sns_req *)sp->u.iocb_cmd.u.ctarg.req;
4145 	u16 cmd = be16_to_cpu(ct_req->command);
4146 	u8 fc4_type = sp->gen2;
4147 	unsigned long flags;
4148 
4149 	/* gen2 field is holding the fc4type */
4150 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4151 	    "Async done-%s res %x FC4Type %x\n",
4152 	    sp->name, res, sp->gen2);
4153 
4154 	if (res) {
4155 		unsigned long flags;
4156 
4157 		sp->free(sp);
4158 		spin_lock_irqsave(&vha->work_lock, flags);
4159 		vha->scan.scan_flags &= ~SF_SCANNING;
4160 		vha->scan.scan_retry++;
4161 		spin_unlock_irqrestore(&vha->work_lock, flags);
4162 
4163 		if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
4164 			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4165 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4166 			qla2xxx_wake_dpc(vha);
4167 		} else {
4168 			ql_dbg(ql_dbg_disc, sp->vha, 0xffff,
4169 			    "Async done-%s rescan failed on all retries\n",
4170 			    sp->name);
4171 		}
4172 		return;
4173 	}
4174 
4175 	if (!res)
4176 		qla2x00_find_free_fcp_nvme_slot(vha, sp);
4177 
4178 	if ((fc4_type == FC4_TYPE_FCP_SCSI) && vha->flags.nvme_enabled &&
4179 	    cmd == GNN_FT_CMD) {
4180 		del_timer(&sp->u.iocb_cmd.timer);
4181 		spin_lock_irqsave(&vha->work_lock, flags);
4182 		vha->scan.scan_flags &= ~SF_SCANNING;
4183 		spin_unlock_irqrestore(&vha->work_lock, flags);
4184 
4185 		e = qla2x00_alloc_work(vha, QLA_EVT_GPNFT);
4186 		if (!e) {
4187 			/*
4188 			 * please ignore kernel warning. Otherwise,
4189 			 * we have mem leak.
4190 			 */
4191 			if (sp->u.iocb_cmd.u.ctarg.req) {
4192 				dma_free_coherent(&vha->hw->pdev->dev,
4193 				    sp->u.iocb_cmd.u.ctarg.req_allocated_size,
4194 				    sp->u.iocb_cmd.u.ctarg.req,
4195 				    sp->u.iocb_cmd.u.ctarg.req_dma);
4196 				sp->u.iocb_cmd.u.ctarg.req = NULL;
4197 			}
4198 			if (sp->u.iocb_cmd.u.ctarg.rsp) {
4199 				dma_free_coherent(&vha->hw->pdev->dev,
4200 				    sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
4201 				    sp->u.iocb_cmd.u.ctarg.rsp,
4202 				    sp->u.iocb_cmd.u.ctarg.rsp_dma);
4203 				sp->u.iocb_cmd.u.ctarg.rsp = NULL;
4204 			}
4205 
4206 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4207 			    "Async done-%s unable to alloc work element\n",
4208 			    sp->name);
4209 			sp->free(sp);
4210 			set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4211 			set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4212 			return;
4213 		}
4214 		e->u.gpnft.fc4_type = FC4_TYPE_NVME;
4215 		sp->rc = res;
4216 		e->u.gpnft.sp = sp;
4217 
4218 		qla2x00_post_work(vha, e);
4219 		return;
4220 	}
4221 
4222 	if (cmd == GPN_FT_CMD)
4223 		e = qla2x00_alloc_work(vha, QLA_EVT_GPNFT_DONE);
4224 	else
4225 		e = qla2x00_alloc_work(vha, QLA_EVT_GNNFT_DONE);
4226 	if (!e) {
4227 		/* please ignore kernel warning. Otherwise, we have mem leak. */
4228 		if (sp->u.iocb_cmd.u.ctarg.req) {
4229 			dma_free_coherent(&vha->hw->pdev->dev,
4230 			    sp->u.iocb_cmd.u.ctarg.req_allocated_size,
4231 			    sp->u.iocb_cmd.u.ctarg.req,
4232 			    sp->u.iocb_cmd.u.ctarg.req_dma);
4233 			sp->u.iocb_cmd.u.ctarg.req = NULL;
4234 		}
4235 		if (sp->u.iocb_cmd.u.ctarg.rsp) {
4236 			dma_free_coherent(&vha->hw->pdev->dev,
4237 			    sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
4238 			    sp->u.iocb_cmd.u.ctarg.rsp,
4239 			    sp->u.iocb_cmd.u.ctarg.rsp_dma);
4240 			sp->u.iocb_cmd.u.ctarg.rsp = NULL;
4241 		}
4242 
4243 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4244 		    "Async done-%s unable to alloc work element\n",
4245 		    sp->name);
4246 		sp->free(sp);
4247 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4248 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4249 		return;
4250 	}
4251 
4252 	sp->rc = res;
4253 	e->u.iosb.sp = sp;
4254 
4255 	qla2x00_post_work(vha, e);
4256 }
4257 
4258 /*
4259  * Get WWNN list for fc4_type
4260  *
4261  * It is assumed the same SRB is re-used from GPNFT to avoid
4262  * mem free & re-alloc
4263  */
4264 static int qla24xx_async_gnnft(scsi_qla_host_t *vha, struct srb *sp,
4265     u8 fc4_type)
4266 {
4267 	int rval = QLA_FUNCTION_FAILED;
4268 	struct ct_sns_req *ct_req;
4269 	struct ct_sns_pkt *ct_sns;
4270 	unsigned long flags;
4271 
4272 	if (!vha->flags.online) {
4273 		spin_lock_irqsave(&vha->work_lock, flags);
4274 		vha->scan.scan_flags &= ~SF_SCANNING;
4275 		spin_unlock_irqrestore(&vha->work_lock, flags);
4276 		goto done_free_sp;
4277 	}
4278 
4279 	if (!sp->u.iocb_cmd.u.ctarg.req || !sp->u.iocb_cmd.u.ctarg.rsp) {
4280 		ql_log(ql_log_warn, vha, 0xffff,
4281 		    "%s: req %p rsp %p are not setup\n",
4282 		    __func__, sp->u.iocb_cmd.u.ctarg.req,
4283 		    sp->u.iocb_cmd.u.ctarg.rsp);
4284 		spin_lock_irqsave(&vha->work_lock, flags);
4285 		vha->scan.scan_flags &= ~SF_SCANNING;
4286 		spin_unlock_irqrestore(&vha->work_lock, flags);
4287 		WARN_ON(1);
4288 		goto done_free_sp;
4289 	}
4290 
4291 	ql_dbg(ql_dbg_disc, vha, 0xfffff,
4292 	    "%s: FC4Type %x, CT-PASSTRHU %s command ctarg rsp size %d, ctarg req size %d\n",
4293 	    __func__, fc4_type, sp->name, sp->u.iocb_cmd.u.ctarg.rsp_size,
4294 	     sp->u.iocb_cmd.u.ctarg.req_size);
4295 
4296 	sp->type = SRB_CT_PTHRU_CMD;
4297 	sp->name = "gnnft";
4298 	sp->gen1 = vha->hw->base_qpair->chip_reset;
4299 	sp->gen2 = fc4_type;
4300 
4301 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
4302 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
4303 
4304 	memset(sp->u.iocb_cmd.u.ctarg.rsp, 0, sp->u.iocb_cmd.u.ctarg.rsp_size);
4305 	memset(sp->u.iocb_cmd.u.ctarg.req, 0, sp->u.iocb_cmd.u.ctarg.req_size);
4306 
4307 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
4308 	/* CT_IU preamble  */
4309 	ct_req = qla2x00_prep_ct_req(ct_sns, GNN_FT_CMD,
4310 	    sp->u.iocb_cmd.u.ctarg.rsp_size);
4311 
4312 	/* GPN_FT req */
4313 	ct_req->req.gpn_ft.port_type = fc4_type;
4314 
4315 	sp->u.iocb_cmd.u.ctarg.req_size = GNN_FT_REQ_SIZE;
4316 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
4317 
4318 	sp->done = qla2x00_async_gpnft_gnnft_sp_done;
4319 
4320 	rval = qla2x00_start_sp(sp);
4321 	if (rval != QLA_SUCCESS)
4322 		goto done_free_sp;
4323 
4324 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4325 	    "Async-%s hdl=%x FC4Type %x.\n", sp->name,
4326 	    sp->handle, ct_req->req.gpn_ft.port_type);
4327 	return rval;
4328 
4329 done_free_sp:
4330 	if (sp->u.iocb_cmd.u.ctarg.req) {
4331 		dma_free_coherent(&vha->hw->pdev->dev,
4332 		    sp->u.iocb_cmd.u.ctarg.req_allocated_size,
4333 		    sp->u.iocb_cmd.u.ctarg.req,
4334 		    sp->u.iocb_cmd.u.ctarg.req_dma);
4335 		sp->u.iocb_cmd.u.ctarg.req = NULL;
4336 	}
4337 	if (sp->u.iocb_cmd.u.ctarg.rsp) {
4338 		dma_free_coherent(&vha->hw->pdev->dev,
4339 		    sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
4340 		    sp->u.iocb_cmd.u.ctarg.rsp,
4341 		    sp->u.iocb_cmd.u.ctarg.rsp_dma);
4342 		sp->u.iocb_cmd.u.ctarg.rsp = NULL;
4343 	}
4344 
4345 	sp->free(sp);
4346 
4347 	return rval;
4348 } /* GNNFT */
4349 
4350 void qla24xx_async_gpnft_done(scsi_qla_host_t *vha, srb_t *sp)
4351 {
4352 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4353 	    "%s enter\n", __func__);
4354 	del_timer(&sp->u.iocb_cmd.timer);
4355 	qla24xx_async_gnnft(vha, sp, sp->gen2);
4356 }
4357 
4358 /* Get WWPN list for certain fc4_type */
4359 int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp)
4360 {
4361 	int rval = QLA_FUNCTION_FAILED;
4362 	struct ct_sns_req       *ct_req;
4363 	struct ct_sns_pkt *ct_sns;
4364 	u32 rspsz;
4365 	unsigned long flags;
4366 
4367 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4368 	    "%s enter\n", __func__);
4369 
4370 	if (!vha->flags.online)
4371 		return rval;
4372 
4373 	spin_lock_irqsave(&vha->work_lock, flags);
4374 	if (vha->scan.scan_flags & SF_SCANNING) {
4375 		spin_unlock_irqrestore(&vha->work_lock, flags);
4376 		ql_dbg(ql_dbg_disc, vha, 0xffff, "scan active\n");
4377 		return rval;
4378 	}
4379 	vha->scan.scan_flags |= SF_SCANNING;
4380 	spin_unlock_irqrestore(&vha->work_lock, flags);
4381 
4382 	if (fc4_type == FC4_TYPE_FCP_SCSI) {
4383 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4384 		    "%s: Performing FCP Scan\n", __func__);
4385 
4386 		if (sp)
4387 			sp->free(sp); /* should not happen */
4388 
4389 		sp = qla2x00_get_sp(vha, NULL, GFP_KERNEL);
4390 		if (!sp) {
4391 			spin_lock_irqsave(&vha->work_lock, flags);
4392 			vha->scan.scan_flags &= ~SF_SCANNING;
4393 			spin_unlock_irqrestore(&vha->work_lock, flags);
4394 			return rval;
4395 		}
4396 
4397 		sp->u.iocb_cmd.u.ctarg.req = dma_zalloc_coherent(
4398 			&vha->hw->pdev->dev, sizeof(struct ct_sns_pkt),
4399 			&sp->u.iocb_cmd.u.ctarg.req_dma, GFP_KERNEL);
4400 		sp->u.iocb_cmd.u.ctarg.req_allocated_size = sizeof(struct ct_sns_pkt);
4401 		if (!sp->u.iocb_cmd.u.ctarg.req) {
4402 			ql_log(ql_log_warn, vha, 0xffff,
4403 			    "Failed to allocate ct_sns request.\n");
4404 			spin_lock_irqsave(&vha->work_lock, flags);
4405 			vha->scan.scan_flags &= ~SF_SCANNING;
4406 			spin_unlock_irqrestore(&vha->work_lock, flags);
4407 			goto done_free_sp;
4408 		}
4409 		sp->u.iocb_cmd.u.ctarg.req_size = GPN_FT_REQ_SIZE;
4410 
4411 		rspsz = sizeof(struct ct_sns_gpnft_rsp) +
4412 			((vha->hw->max_fibre_devices - 1) *
4413 			    sizeof(struct ct_sns_gpn_ft_data));
4414 
4415 		sp->u.iocb_cmd.u.ctarg.rsp = dma_zalloc_coherent(
4416 			&vha->hw->pdev->dev, rspsz,
4417 			&sp->u.iocb_cmd.u.ctarg.rsp_dma, GFP_KERNEL);
4418 		sp->u.iocb_cmd.u.ctarg.rsp_allocated_size = sizeof(struct ct_sns_pkt);
4419 		if (!sp->u.iocb_cmd.u.ctarg.rsp) {
4420 			ql_log(ql_log_warn, vha, 0xffff,
4421 			    "Failed to allocate ct_sns request.\n");
4422 			spin_lock_irqsave(&vha->work_lock, flags);
4423 			vha->scan.scan_flags &= ~SF_SCANNING;
4424 			spin_unlock_irqrestore(&vha->work_lock, flags);
4425 			goto done_free_sp;
4426 		}
4427 		sp->u.iocb_cmd.u.ctarg.rsp_size = rspsz;
4428 
4429 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4430 		    "%s scan list size %d\n", __func__, vha->scan.size);
4431 
4432 		memset(vha->scan.l, 0, vha->scan.size);
4433 	} else if (!sp) {
4434 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4435 		    "NVME scan did not provide SP\n");
4436 		return rval;
4437 	}
4438 
4439 	sp->type = SRB_CT_PTHRU_CMD;
4440 	sp->name = "gpnft";
4441 	sp->gen1 = vha->hw->base_qpair->chip_reset;
4442 	sp->gen2 = fc4_type;
4443 
4444 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
4445 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
4446 
4447 	rspsz = sizeof(struct ct_sns_gpnft_rsp) +
4448 		((vha->hw->max_fibre_devices - 1) *
4449 		    sizeof(struct ct_sns_gpn_ft_data));
4450 
4451 	ct_sns = (struct ct_sns_pkt *)sp->u.iocb_cmd.u.ctarg.req;
4452 	/* CT_IU preamble  */
4453 	ct_req = qla2x00_prep_ct_req(ct_sns, GPN_FT_CMD, rspsz);
4454 
4455 	/* GPN_FT req */
4456 	ct_req->req.gpn_ft.port_type = fc4_type;
4457 
4458 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
4459 
4460 	sp->done = qla2x00_async_gpnft_gnnft_sp_done;
4461 
4462 	rval = qla2x00_start_sp(sp);
4463 	if (rval != QLA_SUCCESS) {
4464 		spin_lock_irqsave(&vha->work_lock, flags);
4465 		vha->scan.scan_flags &= ~SF_SCANNING;
4466 		spin_unlock_irqrestore(&vha->work_lock, flags);
4467 		goto done_free_sp;
4468 	}
4469 
4470 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4471 	    "Async-%s hdl=%x FC4Type %x.\n", sp->name,
4472 	    sp->handle, ct_req->req.gpn_ft.port_type);
4473 	return rval;
4474 
4475 done_free_sp:
4476 	if (sp->u.iocb_cmd.u.ctarg.req) {
4477 		dma_free_coherent(&vha->hw->pdev->dev,
4478 		    sp->u.iocb_cmd.u.ctarg.req_allocated_size,
4479 		    sp->u.iocb_cmd.u.ctarg.req,
4480 		    sp->u.iocb_cmd.u.ctarg.req_dma);
4481 		sp->u.iocb_cmd.u.ctarg.req = NULL;
4482 	}
4483 	if (sp->u.iocb_cmd.u.ctarg.rsp) {
4484 		dma_free_coherent(&vha->hw->pdev->dev,
4485 		    sp->u.iocb_cmd.u.ctarg.rsp_allocated_size,
4486 		    sp->u.iocb_cmd.u.ctarg.rsp,
4487 		    sp->u.iocb_cmd.u.ctarg.rsp_dma);
4488 		sp->u.iocb_cmd.u.ctarg.rsp = NULL;
4489 	}
4490 
4491 	sp->free(sp);
4492 
4493 	return rval;
4494 }
4495 
4496 void qla_scan_work_fn(struct work_struct *work)
4497 {
4498 	struct fab_scan *s = container_of(to_delayed_work(work),
4499 	    struct fab_scan, scan_work);
4500 	struct scsi_qla_host *vha = container_of(s, struct scsi_qla_host,
4501 	    scan);
4502 	unsigned long flags;
4503 
4504 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4505 	    "%s: schedule loop resync\n", __func__);
4506 	set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4507 	set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4508 	qla2xxx_wake_dpc(vha);
4509 	spin_lock_irqsave(&vha->work_lock, flags);
4510 	vha->scan.scan_flags &= ~SF_QUEUED;
4511 	spin_unlock_irqrestore(&vha->work_lock, flags);
4512 }
4513 
4514 /* GNN_ID */
4515 void qla24xx_handle_gnnid_event(scsi_qla_host_t *vha, struct event_arg *ea)
4516 {
4517 	qla24xx_post_gnl_work(vha, ea->fcport);
4518 }
4519 
4520 static void qla2x00_async_gnnid_sp_done(void *s, int res)
4521 {
4522 	struct srb *sp = s;
4523 	struct scsi_qla_host *vha = sp->vha;
4524 	fc_port_t *fcport = sp->fcport;
4525 	u8 *node_name = fcport->ct_desc.ct_sns->p.rsp.rsp.gnn_id.node_name;
4526 	struct event_arg ea;
4527 	u64 wwnn;
4528 
4529 	fcport->flags &= ~FCF_ASYNC_SENT;
4530 	wwnn = wwn_to_u64(node_name);
4531 	if (wwnn)
4532 		memcpy(fcport->node_name, node_name, WWN_SIZE);
4533 
4534 	memset(&ea, 0, sizeof(ea));
4535 	ea.fcport = fcport;
4536 	ea.sp = sp;
4537 	ea.rc = res;
4538 	ea.event = FCME_GNNID_DONE;
4539 
4540 	ql_dbg(ql_dbg_disc, vha, 0x204f,
4541 	    "Async done-%s res %x, WWPN %8phC %8phC\n",
4542 	    sp->name, res, fcport->port_name, fcport->node_name);
4543 
4544 	qla2x00_fcport_event_handler(vha, &ea);
4545 
4546 	sp->free(sp);
4547 }
4548 
4549 int qla24xx_async_gnnid(scsi_qla_host_t *vha, fc_port_t *fcport)
4550 {
4551 	int rval = QLA_FUNCTION_FAILED;
4552 	struct ct_sns_req       *ct_req;
4553 	srb_t *sp;
4554 
4555 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
4556 		return rval;
4557 
4558 	fcport->disc_state = DSC_GNN_ID;
4559 	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
4560 	if (!sp)
4561 		goto done;
4562 
4563 	fcport->flags |= FCF_ASYNC_SENT;
4564 	sp->type = SRB_CT_PTHRU_CMD;
4565 	sp->name = "gnnid";
4566 	sp->gen1 = fcport->rscn_gen;
4567 	sp->gen2 = fcport->login_gen;
4568 
4569 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
4570 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
4571 
4572 	/* CT_IU preamble  */
4573 	ct_req = qla2x00_prep_ct_req(fcport->ct_desc.ct_sns, GNN_ID_CMD,
4574 	    GNN_ID_RSP_SIZE);
4575 
4576 	/* GNN_ID req */
4577 	ct_req->req.port_id.port_id[0] = fcport->d_id.b.domain;
4578 	ct_req->req.port_id.port_id[1] = fcport->d_id.b.area;
4579 	ct_req->req.port_id.port_id[2] = fcport->d_id.b.al_pa;
4580 
4581 
4582 	/* req & rsp use the same buffer */
4583 	sp->u.iocb_cmd.u.ctarg.req = fcport->ct_desc.ct_sns;
4584 	sp->u.iocb_cmd.u.ctarg.req_dma = fcport->ct_desc.ct_sns_dma;
4585 	sp->u.iocb_cmd.u.ctarg.rsp = fcport->ct_desc.ct_sns;
4586 	sp->u.iocb_cmd.u.ctarg.rsp_dma = fcport->ct_desc.ct_sns_dma;
4587 	sp->u.iocb_cmd.u.ctarg.req_size = GNN_ID_REQ_SIZE;
4588 	sp->u.iocb_cmd.u.ctarg.rsp_size = GNN_ID_RSP_SIZE;
4589 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
4590 
4591 	sp->done = qla2x00_async_gnnid_sp_done;
4592 
4593 	rval = qla2x00_start_sp(sp);
4594 	if (rval != QLA_SUCCESS)
4595 		goto done_free_sp;
4596 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4597 	    "Async-%s - %8phC hdl=%x loopid=%x portid %06x.\n",
4598 	    sp->name, fcport->port_name,
4599 	    sp->handle, fcport->loop_id, fcport->d_id.b24);
4600 	return rval;
4601 
4602 done_free_sp:
4603 	sp->free(sp);
4604 done:
4605 	return rval;
4606 }
4607 
4608 int qla24xx_post_gnnid_work(struct scsi_qla_host *vha, fc_port_t *fcport)
4609 {
4610 	struct qla_work_evt *e;
4611 	int ls;
4612 
4613 	ls = atomic_read(&vha->loop_state);
4614 	if (((ls != LOOP_READY) && (ls != LOOP_UP)) ||
4615 		test_bit(UNLOADING, &vha->dpc_flags))
4616 		return 0;
4617 
4618 	e = qla2x00_alloc_work(vha, QLA_EVT_GNNID);
4619 	if (!e)
4620 		return QLA_FUNCTION_FAILED;
4621 
4622 	e->u.fcport.fcport = fcport;
4623 	return qla2x00_post_work(vha, e);
4624 }
4625 
4626 /* GPFN_ID */
4627 void qla24xx_handle_gfpnid_event(scsi_qla_host_t *vha, struct event_arg *ea)
4628 {
4629 	fc_port_t *fcport = ea->fcport;
4630 
4631 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4632 	    "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d fcpcnt %d\n",
4633 	    __func__, fcport->port_name, fcport->disc_state,
4634 	    fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
4635 	    fcport->rscn_gen, ea->sp->gen1, vha->fcport_count);
4636 
4637 	if (fcport->disc_state == DSC_DELETE_PEND)
4638 		return;
4639 
4640 	if (ea->sp->gen2 != fcport->login_gen) {
4641 		/* target side must have changed it. */
4642 		ql_dbg(ql_dbg_disc, vha, 0x20d3,
4643 		    "%s %8phC generation changed\n",
4644 		    __func__, fcport->port_name);
4645 		return;
4646 	} else if (ea->sp->gen1 != fcport->rscn_gen) {
4647 		ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
4648 		    __func__, __LINE__, fcport->port_name);
4649 		qla24xx_post_gidpn_work(vha, fcport);
4650 		return;
4651 	}
4652 
4653 	qla24xx_post_gpsc_work(vha, fcport);
4654 }
4655 
4656 static void qla2x00_async_gfpnid_sp_done(void *s, int res)
4657 {
4658 	struct srb *sp = s;
4659 	struct scsi_qla_host *vha = sp->vha;
4660 	fc_port_t *fcport = sp->fcport;
4661 	u8 *fpn = fcport->ct_desc.ct_sns->p.rsp.rsp.gfpn_id.port_name;
4662 	struct event_arg ea;
4663 	u64 wwn;
4664 
4665 	wwn = wwn_to_u64(fpn);
4666 	if (wwn)
4667 		memcpy(fcport->fabric_port_name, fpn, WWN_SIZE);
4668 
4669 	memset(&ea, 0, sizeof(ea));
4670 	ea.fcport = fcport;
4671 	ea.sp = sp;
4672 	ea.rc = res;
4673 	ea.event = FCME_GFPNID_DONE;
4674 
4675 	ql_dbg(ql_dbg_disc, vha, 0x204f,
4676 	    "Async done-%s res %x, WWPN %8phC %8phC\n",
4677 	    sp->name, res, fcport->port_name, fcport->fabric_port_name);
4678 
4679 	qla2x00_fcport_event_handler(vha, &ea);
4680 
4681 	sp->free(sp);
4682 }
4683 
4684 int qla24xx_async_gfpnid(scsi_qla_host_t *vha, fc_port_t *fcport)
4685 {
4686 	int rval = QLA_FUNCTION_FAILED;
4687 	struct ct_sns_req       *ct_req;
4688 	srb_t *sp;
4689 
4690 	if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
4691 		return rval;
4692 
4693 	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
4694 	if (!sp)
4695 		goto done;
4696 
4697 	sp->type = SRB_CT_PTHRU_CMD;
4698 	sp->name = "gfpnid";
4699 	sp->gen1 = fcport->rscn_gen;
4700 	sp->gen2 = fcport->login_gen;
4701 
4702 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
4703 	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
4704 
4705 	/* CT_IU preamble  */
4706 	ct_req = qla2x00_prep_ct_req(fcport->ct_desc.ct_sns, GFPN_ID_CMD,
4707 	    GFPN_ID_RSP_SIZE);
4708 
4709 	/* GFPN_ID req */
4710 	ct_req->req.port_id.port_id[0] = fcport->d_id.b.domain;
4711 	ct_req->req.port_id.port_id[1] = fcport->d_id.b.area;
4712 	ct_req->req.port_id.port_id[2] = fcport->d_id.b.al_pa;
4713 
4714 
4715 	/* req & rsp use the same buffer */
4716 	sp->u.iocb_cmd.u.ctarg.req = fcport->ct_desc.ct_sns;
4717 	sp->u.iocb_cmd.u.ctarg.req_dma = fcport->ct_desc.ct_sns_dma;
4718 	sp->u.iocb_cmd.u.ctarg.rsp = fcport->ct_desc.ct_sns;
4719 	sp->u.iocb_cmd.u.ctarg.rsp_dma = fcport->ct_desc.ct_sns_dma;
4720 	sp->u.iocb_cmd.u.ctarg.req_size = GFPN_ID_REQ_SIZE;
4721 	sp->u.iocb_cmd.u.ctarg.rsp_size = GFPN_ID_RSP_SIZE;
4722 	sp->u.iocb_cmd.u.ctarg.nport_handle = NPH_SNS;
4723 
4724 	sp->done = qla2x00_async_gfpnid_sp_done;
4725 
4726 	rval = qla2x00_start_sp(sp);
4727 	if (rval != QLA_SUCCESS)
4728 		goto done_free_sp;
4729 
4730 	ql_dbg(ql_dbg_disc, vha, 0xffff,
4731 	    "Async-%s - %8phC hdl=%x loopid=%x portid %06x.\n",
4732 	    sp->name, fcport->port_name,
4733 	    sp->handle, fcport->loop_id, fcport->d_id.b24);
4734 	return rval;
4735 
4736 done_free_sp:
4737 	sp->free(sp);
4738 	fcport->flags &= ~FCF_ASYNC_SENT;
4739 done:
4740 	return rval;
4741 }
4742 
4743 int qla24xx_post_gfpnid_work(struct scsi_qla_host *vha, fc_port_t *fcport)
4744 {
4745 	struct qla_work_evt *e;
4746 	int ls;
4747 
4748 	ls = atomic_read(&vha->loop_state);
4749 	if (((ls != LOOP_READY) && (ls != LOOP_UP)) ||
4750 		test_bit(UNLOADING, &vha->dpc_flags))
4751 		return 0;
4752 
4753 	e = qla2x00_alloc_work(vha, QLA_EVT_GFPNID);
4754 	if (!e)
4755 		return QLA_FUNCTION_FAILED;
4756 
4757 	e->u.fcport.fcport = fcport;
4758 	return qla2x00_post_work(vha, e);
4759 }
4760