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