xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_edif.c (revision e8254a8e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Marvell Fibre Channel HBA Driver
4  * Copyright (c)  2021     Marvell
5  */
6 #include "qla_def.h"
7 #include "qla_edif.h"
8 
9 #include <linux/kthread.h>
10 #include <linux/vmalloc.h>
11 #include <linux/delay.h>
12 #include <scsi/scsi_tcq.h>
13 
14 static struct edif_sa_index_entry *qla_edif_sadb_find_sa_index_entry(uint16_t nport_handle,
15 		struct list_head *sa_list);
16 static uint16_t qla_edif_sadb_get_sa_index(fc_port_t *fcport,
17 		struct qla_sa_update_frame *sa_frame);
18 static int qla_edif_sadb_delete_sa_index(fc_port_t *fcport, uint16_t nport_handle,
19 		uint16_t sa_index);
20 static int qla_pur_get_pending(scsi_qla_host_t *, fc_port_t *, struct bsg_job *);
21 
22 struct edb_node {
23 	struct  list_head	list;
24 	uint32_t		ntype;
25 	union {
26 		port_id_t	plogi_did;
27 		uint32_t	async;
28 		port_id_t	els_sid;
29 		struct edif_sa_update_aen	sa_aen;
30 	} u;
31 };
32 
33 static struct els_sub_cmd {
34 	uint16_t cmd;
35 	const char *str;
36 } sc_str[] = {
37 	{SEND_ELS, "send ELS"},
38 	{SEND_ELS_REPLY, "send ELS Reply"},
39 	{PULL_ELS, "retrieve ELS"},
40 };
41 
42 const char *sc_to_str(uint16_t cmd)
43 {
44 	int i;
45 	struct els_sub_cmd *e;
46 
47 	for (i = 0; i < ARRAY_SIZE(sc_str); i++) {
48 		e = sc_str + i;
49 		if (cmd == e->cmd)
50 			return e->str;
51 	}
52 	return "unknown";
53 }
54 
55 static struct edb_node *qla_edb_getnext(scsi_qla_host_t *vha)
56 {
57 	unsigned long   flags;
58 	struct edb_node *edbnode = NULL;
59 
60 	spin_lock_irqsave(&vha->e_dbell.db_lock, flags);
61 
62 	/* db nodes are fifo - no qualifications done */
63 	if (!list_empty(&vha->e_dbell.head)) {
64 		edbnode = list_first_entry(&vha->e_dbell.head,
65 					   struct edb_node, list);
66 		list_del_init(&edbnode->list);
67 	}
68 
69 	spin_unlock_irqrestore(&vha->e_dbell.db_lock, flags);
70 
71 	return edbnode;
72 }
73 
74 static void qla_edb_node_free(scsi_qla_host_t *vha, struct edb_node *node)
75 {
76 	list_del_init(&node->list);
77 	kfree(node);
78 }
79 
80 static struct edif_list_entry *qla_edif_list_find_sa_index(fc_port_t *fcport,
81 		uint16_t handle)
82 {
83 	struct edif_list_entry *entry;
84 	struct edif_list_entry *tentry;
85 	struct list_head *indx_list = &fcport->edif.edif_indx_list;
86 
87 	list_for_each_entry_safe(entry, tentry, indx_list, next) {
88 		if (entry->handle == handle)
89 			return entry;
90 	}
91 	return NULL;
92 }
93 
94 /* timeout called when no traffic and delayed rx sa_index delete */
95 static void qla2x00_sa_replace_iocb_timeout(struct timer_list *t)
96 {
97 	struct edif_list_entry *edif_entry = from_timer(edif_entry, t, timer);
98 	fc_port_t *fcport = edif_entry->fcport;
99 	struct scsi_qla_host *vha = fcport->vha;
100 	struct  edif_sa_ctl *sa_ctl;
101 	uint16_t nport_handle;
102 	unsigned long flags = 0;
103 
104 	ql_dbg(ql_dbg_edif, vha, 0x3069,
105 	    "%s:  nport_handle 0x%x,  SA REPL Delay Timeout, %8phC portid=%06x\n",
106 	    __func__, edif_entry->handle, fcport->port_name, fcport->d_id.b24);
107 
108 	/*
109 	 * if delete_sa_index is valid then no one has serviced this
110 	 * delayed delete
111 	 */
112 	spin_lock_irqsave(&fcport->edif.indx_list_lock, flags);
113 
114 	/*
115 	 * delete_sa_index is invalidated when we find the new sa_index in
116 	 * the incoming data stream.  If it is not invalidated then we are
117 	 * still looking for the new sa_index because there is no I/O and we
118 	 * need to just force the rx delete and move on.  Otherwise
119 	 * we could get another rekey which will result in an error 66.
120 	 */
121 	if (edif_entry->delete_sa_index != INVALID_EDIF_SA_INDEX) {
122 		uint16_t delete_sa_index = edif_entry->delete_sa_index;
123 
124 		edif_entry->delete_sa_index = INVALID_EDIF_SA_INDEX;
125 		nport_handle = edif_entry->handle;
126 		spin_unlock_irqrestore(&fcport->edif.indx_list_lock, flags);
127 
128 		sa_ctl = qla_edif_find_sa_ctl_by_index(fcport,
129 		    delete_sa_index, 0);
130 
131 		if (sa_ctl) {
132 			ql_dbg(ql_dbg_edif, vha, 0x3063,
133 			    "%s: sa_ctl: %p, delete index %d, update index: %d, lid: 0x%x\n",
134 			    __func__, sa_ctl, delete_sa_index, edif_entry->update_sa_index,
135 			    nport_handle);
136 
137 			sa_ctl->flags = EDIF_SA_CTL_FLG_DEL;
138 			set_bit(EDIF_SA_CTL_REPL, &sa_ctl->state);
139 			qla_post_sa_replace_work(fcport->vha, fcport,
140 			    nport_handle, sa_ctl);
141 
142 		} else {
143 			ql_dbg(ql_dbg_edif, vha, 0x3063,
144 			    "%s: sa_ctl not found for delete_sa_index: %d\n",
145 			    __func__, edif_entry->delete_sa_index);
146 		}
147 	} else {
148 		spin_unlock_irqrestore(&fcport->edif.indx_list_lock, flags);
149 	}
150 }
151 
152 /*
153  * create a new list entry for this nport handle and
154  * add an sa_update index to the list - called for sa_update
155  */
156 static int qla_edif_list_add_sa_update_index(fc_port_t *fcport,
157 		uint16_t sa_index, uint16_t handle)
158 {
159 	struct edif_list_entry *entry;
160 	unsigned long flags = 0;
161 
162 	/* if the entry exists, then just update the sa_index */
163 	entry = qla_edif_list_find_sa_index(fcport, handle);
164 	if (entry) {
165 		entry->update_sa_index = sa_index;
166 		entry->count = 0;
167 		return 0;
168 	}
169 
170 	/*
171 	 * This is the normal path - there should be no existing entry
172 	 * when update is called.  The exception is at startup
173 	 * when update is called for the first two sa_indexes
174 	 * followed by a delete of the first sa_index
175 	 */
176 	entry = kzalloc((sizeof(struct edif_list_entry)), GFP_ATOMIC);
177 	if (!entry)
178 		return -ENOMEM;
179 
180 	INIT_LIST_HEAD(&entry->next);
181 	entry->handle = handle;
182 	entry->update_sa_index = sa_index;
183 	entry->delete_sa_index = INVALID_EDIF_SA_INDEX;
184 	entry->count = 0;
185 	entry->flags = 0;
186 	timer_setup(&entry->timer, qla2x00_sa_replace_iocb_timeout, 0);
187 	spin_lock_irqsave(&fcport->edif.indx_list_lock, flags);
188 	list_add_tail(&entry->next, &fcport->edif.edif_indx_list);
189 	spin_unlock_irqrestore(&fcport->edif.indx_list_lock, flags);
190 	return 0;
191 }
192 
193 /* remove an entry from the list */
194 static void qla_edif_list_delete_sa_index(fc_port_t *fcport, struct edif_list_entry *entry)
195 {
196 	unsigned long flags = 0;
197 
198 	spin_lock_irqsave(&fcport->edif.indx_list_lock, flags);
199 	list_del(&entry->next);
200 	spin_unlock_irqrestore(&fcport->edif.indx_list_lock, flags);
201 }
202 
203 int qla_post_sa_replace_work(struct scsi_qla_host *vha,
204 	 fc_port_t *fcport, uint16_t nport_handle, struct edif_sa_ctl *sa_ctl)
205 {
206 	struct qla_work_evt *e;
207 
208 	e = qla2x00_alloc_work(vha, QLA_EVT_SA_REPLACE);
209 	if (!e)
210 		return QLA_FUNCTION_FAILED;
211 
212 	e->u.sa_update.fcport = fcport;
213 	e->u.sa_update.sa_ctl = sa_ctl;
214 	e->u.sa_update.nport_handle = nport_handle;
215 	fcport->flags |= FCF_ASYNC_ACTIVE;
216 	return qla2x00_post_work(vha, e);
217 }
218 
219 static void
220 qla_edif_sa_ctl_init(scsi_qla_host_t *vha, struct fc_port  *fcport)
221 {
222 	ql_dbg(ql_dbg_edif, vha, 0x2058,
223 	    "Init SA_CTL List for fcport - nn %8phN pn %8phN portid=%06x.\n",
224 	    fcport->node_name, fcport->port_name, fcport->d_id.b24);
225 
226 	fcport->edif.tx_rekey_cnt = 0;
227 	fcport->edif.rx_rekey_cnt = 0;
228 
229 	fcport->edif.tx_bytes = 0;
230 	fcport->edif.rx_bytes = 0;
231 }
232 
233 static int qla_bsg_check(scsi_qla_host_t *vha, struct bsg_job *bsg_job,
234 fc_port_t *fcport)
235 {
236 	struct extra_auth_els *p;
237 	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
238 	struct qla_bsg_auth_els_request *req =
239 	    (struct qla_bsg_auth_els_request *)bsg_job->request;
240 
241 	if (!vha->hw->flags.edif_enabled) {
242 		ql_dbg(ql_dbg_edif, vha, 0x9105,
243 		    "%s edif not enabled\n", __func__);
244 		goto done;
245 	}
246 	if (DBELL_INACTIVE(vha)) {
247 		ql_dbg(ql_dbg_edif, vha, 0x09102,
248 		    "%s doorbell not enabled\n", __func__);
249 		goto done;
250 	}
251 
252 	p = &req->e;
253 
254 	/* Get response */
255 	if (p->sub_cmd == PULL_ELS) {
256 		struct qla_bsg_auth_els_reply *rpl =
257 			(struct qla_bsg_auth_els_reply *)bsg_job->reply;
258 
259 		qla_pur_get_pending(vha, fcport, bsg_job);
260 
261 		ql_dbg(ql_dbg_edif, vha, 0x911d,
262 			"%s %s %8phN sid=%x. xchg %x, nb=%xh bsg ptr %p\n",
263 			__func__, sc_to_str(p->sub_cmd), fcport->port_name,
264 			fcport->d_id.b24, rpl->rx_xchg_address,
265 			rpl->r.reply_payload_rcv_len, bsg_job);
266 
267 		goto done;
268 	}
269 	return 0;
270 
271 done:
272 
273 	bsg_job_done(bsg_job, bsg_reply->result,
274 			bsg_reply->reply_payload_rcv_len);
275 	return -EIO;
276 }
277 
278 fc_port_t *
279 qla2x00_find_fcport_by_pid(scsi_qla_host_t *vha, port_id_t *id)
280 {
281 	fc_port_t *f, *tf;
282 
283 	f = NULL;
284 	list_for_each_entry_safe(f, tf, &vha->vp_fcports, list) {
285 		if (f->d_id.b24 == id->b24)
286 			return f;
287 	}
288 	return NULL;
289 }
290 
291 /**
292  * qla_edif_app_check(): check for valid application id.
293  * @vha: host adapter pointer
294  * @appid: application id
295  * Return: false = fail, true = pass
296  */
297 static bool
298 qla_edif_app_check(scsi_qla_host_t *vha, struct app_id appid)
299 {
300 	/* check that the app is allow/known to the driver */
301 
302 	if (appid.app_vid != EDIF_APP_ID) {
303 		ql_dbg(ql_dbg_edif, vha, 0x911d, "%s app id not ok (%x)",
304 		    __func__, appid.app_vid);
305 		return false;
306 	}
307 
308 	if (appid.version != EDIF_VERSION1) {
309 		ql_dbg(ql_dbg_edif, vha, 0x911d, "%s app version is not ok (%x)",
310 		    __func__, appid.version);
311 		return false;
312 	}
313 
314 	return true;
315 }
316 
317 static void
318 qla_edif_free_sa_ctl(fc_port_t *fcport, struct edif_sa_ctl *sa_ctl,
319 	int index)
320 {
321 	unsigned long flags = 0;
322 
323 	spin_lock_irqsave(&fcport->edif.sa_list_lock, flags);
324 	list_del(&sa_ctl->next);
325 	spin_unlock_irqrestore(&fcport->edif.sa_list_lock, flags);
326 	if (index >= 512)
327 		fcport->edif.tx_rekey_cnt--;
328 	else
329 		fcport->edif.rx_rekey_cnt--;
330 	kfree(sa_ctl);
331 }
332 
333 /* return an index to the freepool */
334 static void qla_edif_add_sa_index_to_freepool(fc_port_t *fcport, int dir,
335 		uint16_t sa_index)
336 {
337 	void *sa_id_map;
338 	struct scsi_qla_host *vha = fcport->vha;
339 	struct qla_hw_data *ha = vha->hw;
340 	unsigned long flags = 0;
341 	u16 lsa_index = sa_index;
342 
343 	ql_dbg(ql_dbg_edif + ql_dbg_verbose, vha, 0x3063,
344 	    "%s: entry\n", __func__);
345 
346 	if (dir) {
347 		sa_id_map = ha->edif_tx_sa_id_map;
348 		lsa_index -= EDIF_TX_SA_INDEX_BASE;
349 	} else {
350 		sa_id_map = ha->edif_rx_sa_id_map;
351 	}
352 
353 	spin_lock_irqsave(&ha->sadb_fp_lock, flags);
354 	clear_bit(lsa_index, sa_id_map);
355 	spin_unlock_irqrestore(&ha->sadb_fp_lock, flags);
356 	ql_dbg(ql_dbg_edif, vha, 0x3063,
357 	    "%s: index %d added to free pool\n", __func__, sa_index);
358 }
359 
360 static void __qla2x00_release_all_sadb(struct scsi_qla_host *vha,
361 	struct fc_port *fcport, struct edif_sa_index_entry *entry,
362 	int pdir)
363 {
364 	struct edif_list_entry *edif_entry;
365 	struct  edif_sa_ctl *sa_ctl;
366 	int i, dir;
367 	int key_cnt = 0;
368 
369 	for (i = 0; i < 2; i++) {
370 		if (entry->sa_pair[i].sa_index == INVALID_EDIF_SA_INDEX)
371 			continue;
372 
373 		if (fcport->loop_id != entry->handle) {
374 			ql_dbg(ql_dbg_edif, vha, 0x3063,
375 			    "%s: ** WARNING %d** entry handle: 0x%x, lid: 0x%x, sa_index: %d\n",
376 			    __func__, i, entry->handle, fcport->loop_id,
377 			    entry->sa_pair[i].sa_index);
378 		}
379 
380 		/* release the sa_ctl */
381 		sa_ctl = qla_edif_find_sa_ctl_by_index(fcport,
382 				entry->sa_pair[i].sa_index, pdir);
383 		if (sa_ctl &&
384 		    qla_edif_find_sa_ctl_by_index(fcport, sa_ctl->index, pdir)) {
385 			ql_dbg(ql_dbg_edif, vha, 0x3063,
386 			    "%s: freeing sa_ctl for index %d\n", __func__, sa_ctl->index);
387 			qla_edif_free_sa_ctl(fcport, sa_ctl, sa_ctl->index);
388 		} else {
389 			ql_dbg(ql_dbg_edif, vha, 0x3063,
390 			    "%s: sa_ctl NOT freed, sa_ctl: %p\n", __func__, sa_ctl);
391 		}
392 
393 		/* Release the index */
394 		ql_dbg(ql_dbg_edif, vha, 0x3063,
395 			"%s: freeing sa_index %d, nph: 0x%x\n",
396 			__func__, entry->sa_pair[i].sa_index, entry->handle);
397 
398 		dir = (entry->sa_pair[i].sa_index <
399 			EDIF_TX_SA_INDEX_BASE) ? 0 : 1;
400 		qla_edif_add_sa_index_to_freepool(fcport, dir,
401 			entry->sa_pair[i].sa_index);
402 
403 		/* Delete timer on RX */
404 		if (pdir != SAU_FLG_TX) {
405 			edif_entry =
406 				qla_edif_list_find_sa_index(fcport, entry->handle);
407 			if (edif_entry) {
408 				ql_dbg(ql_dbg_edif, vha, 0x5033,
409 				    "%s: remove edif_entry %p, update_sa_index: 0x%x, delete_sa_index: 0x%x\n",
410 				    __func__, edif_entry, edif_entry->update_sa_index,
411 				    edif_entry->delete_sa_index);
412 				qla_edif_list_delete_sa_index(fcport, edif_entry);
413 				/*
414 				 * valid delete_sa_index indicates there is a rx
415 				 * delayed delete queued
416 				 */
417 				if (edif_entry->delete_sa_index !=
418 						INVALID_EDIF_SA_INDEX) {
419 					timer_shutdown(&edif_entry->timer);
420 
421 					/* build and send the aen */
422 					fcport->edif.rx_sa_set = 1;
423 					fcport->edif.rx_sa_pending = 0;
424 					qla_edb_eventcreate(vha,
425 							VND_CMD_AUTH_STATE_SAUPDATE_COMPL,
426 							QL_VND_SA_STAT_SUCCESS,
427 							QL_VND_RX_SA_KEY, fcport);
428 				}
429 				ql_dbg(ql_dbg_edif, vha, 0x5033,
430 				    "%s: release edif_entry %p, update_sa_index: 0x%x, delete_sa_index: 0x%x\n",
431 				    __func__, edif_entry, edif_entry->update_sa_index,
432 				    edif_entry->delete_sa_index);
433 
434 				kfree(edif_entry);
435 			}
436 		}
437 		key_cnt++;
438 	}
439 	ql_dbg(ql_dbg_edif, vha, 0x3063,
440 	    "%s: %d %s keys released\n",
441 	    __func__, key_cnt, pdir ? "tx" : "rx");
442 }
443 
444 /* find an release all outstanding sadb sa_indicies */
445 void qla2x00_release_all_sadb(struct scsi_qla_host *vha, struct fc_port *fcport)
446 {
447 	struct edif_sa_index_entry *entry, *tmp;
448 	struct qla_hw_data *ha = vha->hw;
449 	unsigned long flags;
450 
451 	ql_dbg(ql_dbg_edif + ql_dbg_verbose, vha, 0x3063,
452 	    "%s: Starting...\n", __func__);
453 
454 	spin_lock_irqsave(&ha->sadb_lock, flags);
455 
456 	list_for_each_entry_safe(entry, tmp, &ha->sadb_rx_index_list, next) {
457 		if (entry->fcport == fcport) {
458 			list_del(&entry->next);
459 			spin_unlock_irqrestore(&ha->sadb_lock, flags);
460 			__qla2x00_release_all_sadb(vha, fcport, entry, 0);
461 			kfree(entry);
462 			spin_lock_irqsave(&ha->sadb_lock, flags);
463 			break;
464 		}
465 	}
466 
467 	list_for_each_entry_safe(entry, tmp, &ha->sadb_tx_index_list, next) {
468 		if (entry->fcport == fcport) {
469 			list_del(&entry->next);
470 			spin_unlock_irqrestore(&ha->sadb_lock, flags);
471 
472 			__qla2x00_release_all_sadb(vha, fcport, entry, SAU_FLG_TX);
473 
474 			kfree(entry);
475 			spin_lock_irqsave(&ha->sadb_lock, flags);
476 			break;
477 		}
478 	}
479 	spin_unlock_irqrestore(&ha->sadb_lock, flags);
480 }
481 
482 /**
483  * qla_edif_app_start:  application has announce its present
484  * @vha: host adapter pointer
485  * @bsg_job: user request
486  *
487  * Set/activate doorbell.  Reset current sessions and re-login with
488  * secure flag.
489  */
490 static int
491 qla_edif_app_start(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
492 {
493 	int32_t			rval = 0;
494 	struct fc_bsg_reply	*bsg_reply = bsg_job->reply;
495 	struct app_start	appstart;
496 	struct app_start_reply	appreply;
497 	struct fc_port  *fcport, *tf;
498 
499 	ql_log(ql_log_info, vha, 0x1313,
500 	       "EDIF application registration with driver, FC device connections will be re-established.\n");
501 
502 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
503 	    bsg_job->request_payload.sg_cnt, &appstart,
504 	    sizeof(struct app_start));
505 
506 	ql_dbg(ql_dbg_edif, vha, 0x911d, "%s app_vid=%x app_start_flags %x\n",
507 	     __func__, appstart.app_info.app_vid, appstart.app_start_flags);
508 
509 	if (DBELL_INACTIVE(vha)) {
510 		/* mark doorbell as active since an app is now present */
511 		vha->e_dbell.db_flags |= EDB_ACTIVE;
512 	} else {
513 		goto out;
514 	}
515 
516 	if (N2N_TOPO(vha->hw)) {
517 		list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list)
518 			fcport->n2n_link_reset_cnt = 0;
519 
520 		if (vha->hw->flags.n2n_fw_acc_sec) {
521 			list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list)
522 				qla_edif_sa_ctl_init(vha, fcport);
523 
524 			/*
525 			 * While authentication app was not running, remote device
526 			 * could still try to login with this local port.  Let's
527 			 * clear the state and try again.
528 			 */
529 			qla2x00_wait_for_sess_deletion(vha);
530 
531 			/* bounce the link to get the other guy to relogin */
532 			if (!vha->hw->flags.n2n_bigger) {
533 				set_bit(N2N_LINK_RESET, &vha->dpc_flags);
534 				qla2xxx_wake_dpc(vha);
535 			}
536 		} else {
537 			qla2x00_wait_for_hba_online(vha);
538 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
539 			qla2xxx_wake_dpc(vha);
540 			qla2x00_wait_for_hba_online(vha);
541 		}
542 	} else {
543 		list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
544 			ql_dbg(ql_dbg_edif, vha, 0x2058,
545 			       "FCSP - nn %8phN pn %8phN portid=%06x.\n",
546 			       fcport->node_name, fcport->port_name,
547 			       fcport->d_id.b24);
548 			ql_dbg(ql_dbg_edif, vha, 0xf084,
549 			       "%s: se_sess %p / sess %p from port %8phC "
550 			       "loop_id %#04x s_id %06x logout %d "
551 			       "keep %d els_logo %d disc state %d auth state %d"
552 			       "stop state %d\n",
553 			       __func__, fcport->se_sess, fcport,
554 			       fcport->port_name, fcport->loop_id,
555 			       fcport->d_id.b24, fcport->logout_on_delete,
556 			       fcport->keep_nport_handle, fcport->send_els_logo,
557 			       fcport->disc_state, fcport->edif.auth_state,
558 			       fcport->edif.app_stop);
559 
560 			if (atomic_read(&vha->loop_state) == LOOP_DOWN)
561 				break;
562 
563 			fcport->login_retry = vha->hw->login_retry_count;
564 
565 			fcport->edif.app_stop = 0;
566 			fcport->edif.app_sess_online = 0;
567 
568 			if (fcport->scan_state != QLA_FCPORT_FOUND)
569 				continue;
570 
571 			if (fcport->port_type == FCT_UNKNOWN &&
572 			    !fcport->fc4_features)
573 				rval = qla24xx_async_gffid(vha, fcport, true);
574 
575 			if (!rval && !(fcport->fc4_features & FC4_FF_TARGET ||
576 			    fcport->port_type & (FCT_TARGET|FCT_NVME_TARGET)))
577 				continue;
578 
579 			rval = 0;
580 
581 			ql_dbg(ql_dbg_edif, vha, 0x911e,
582 			       "%s wwpn %8phC calling qla_edif_reset_auth_wait\n",
583 			       __func__, fcport->port_name);
584 			qlt_schedule_sess_for_deletion(fcport);
585 			qla_edif_sa_ctl_init(vha, fcport);
586 		}
587 		set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
588 	}
589 
590 	if (vha->pur_cinfo.enode_flags != ENODE_ACTIVE) {
591 		/* mark as active since an app is now present */
592 		vha->pur_cinfo.enode_flags = ENODE_ACTIVE;
593 	} else {
594 		ql_dbg(ql_dbg_edif, vha, 0x911f, "%s enode already active\n",
595 		     __func__);
596 	}
597 
598 out:
599 	appreply.host_support_edif = vha->hw->flags.edif_enabled;
600 	appreply.edif_enode_active = vha->pur_cinfo.enode_flags;
601 	appreply.edif_edb_active = vha->e_dbell.db_flags;
602 	appreply.version = EDIF_VERSION1;
603 
604 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
605 
606 	SET_DID_STATUS(bsg_reply->result, DID_OK);
607 
608 	bsg_reply->reply_payload_rcv_len = sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
609 							       bsg_job->reply_payload.sg_cnt,
610 							       &appreply,
611 							       sizeof(struct app_start_reply));
612 
613 	ql_dbg(ql_dbg_edif, vha, 0x911d,
614 	    "%s app start completed with 0x%x\n",
615 	    __func__, rval);
616 
617 	return rval;
618 }
619 
620 /**
621  * qla_edif_app_stop - app has announced it's exiting.
622  * @vha: host adapter pointer
623  * @bsg_job: user space command pointer
624  *
625  * Free any in flight messages, clear all doorbell events
626  * to application. Reject any message relate to security.
627  */
628 static int
629 qla_edif_app_stop(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
630 {
631 	struct app_stop         appstop;
632 	struct fc_bsg_reply     *bsg_reply = bsg_job->reply;
633 	struct fc_port  *fcport, *tf;
634 
635 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
636 	    bsg_job->request_payload.sg_cnt, &appstop,
637 	    sizeof(struct app_stop));
638 
639 	ql_dbg(ql_dbg_edif, vha, 0x911d, "%s Stopping APP: app_vid=%x\n",
640 	    __func__, appstop.app_info.app_vid);
641 
642 	/* Call db stop and enode stop functions */
643 
644 	/* if we leave this running short waits are operational < 16 secs */
645 	qla_enode_stop(vha);        /* stop enode */
646 	qla_edb_stop(vha);          /* stop db */
647 
648 	list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
649 		if (!(fcport->flags & FCF_FCSP_DEVICE))
650 			continue;
651 
652 		if (fcport->flags & FCF_FCSP_DEVICE) {
653 			ql_dbg(ql_dbg_edif, vha, 0xf084,
654 			    "%s: sess %p from port %8phC lid %#04x s_id %06x logout %d keep %d els_logo %d\n",
655 			    __func__, fcport,
656 			    fcport->port_name, fcport->loop_id, fcport->d_id.b24,
657 			    fcport->logout_on_delete, fcport->keep_nport_handle,
658 			    fcport->send_els_logo);
659 
660 			if (atomic_read(&vha->loop_state) == LOOP_DOWN)
661 				break;
662 
663 			fcport->edif.app_stop = 1;
664 			ql_dbg(ql_dbg_edif, vha, 0x911e,
665 				"%s wwpn %8phC calling qla_edif_reset_auth_wait\n",
666 				__func__, fcport->port_name);
667 
668 			fcport->send_els_logo = 1;
669 			qlt_schedule_sess_for_deletion(fcport);
670 		}
671 	}
672 
673 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
674 	SET_DID_STATUS(bsg_reply->result, DID_OK);
675 
676 	/* no return interface to app - it assumes we cleaned up ok */
677 
678 	return 0;
679 }
680 
681 static int
682 qla_edif_app_chk_sa_update(scsi_qla_host_t *vha, fc_port_t *fcport,
683 		struct app_plogi_reply *appplogireply)
684 {
685 	int	ret = 0;
686 
687 	if (!(fcport->edif.rx_sa_set && fcport->edif.tx_sa_set)) {
688 		ql_dbg(ql_dbg_edif, vha, 0x911e,
689 		    "%s: wwpn %8phC Both SA indexes has not been SET TX %d, RX %d.\n",
690 		    __func__, fcport->port_name, fcport->edif.tx_sa_set,
691 		    fcport->edif.rx_sa_set);
692 		appplogireply->prli_status = 0;
693 		ret = 1;
694 	} else  {
695 		ql_dbg(ql_dbg_edif, vha, 0x911e,
696 		    "%s wwpn %8phC Both SA(s) updated.\n", __func__,
697 		    fcport->port_name);
698 		fcport->edif.rx_sa_set = fcport->edif.tx_sa_set = 0;
699 		fcport->edif.rx_sa_pending = fcport->edif.tx_sa_pending = 0;
700 		appplogireply->prli_status = 1;
701 	}
702 	return ret;
703 }
704 
705 /**
706  * qla_edif_app_authok - authentication by app succeeded.  Driver can proceed
707  *   with prli
708  * @vha: host adapter pointer
709  * @bsg_job: user request
710  */
711 static int
712 qla_edif_app_authok(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
713 {
714 	struct auth_complete_cmd appplogiok;
715 	struct app_plogi_reply	appplogireply = {0};
716 	struct fc_bsg_reply	*bsg_reply = bsg_job->reply;
717 	fc_port_t		*fcport = NULL;
718 	port_id_t		portid = {0};
719 
720 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
721 	    bsg_job->request_payload.sg_cnt, &appplogiok,
722 	    sizeof(struct auth_complete_cmd));
723 
724 	/* silent unaligned access warning */
725 	portid.b.domain = appplogiok.u.d_id.b.domain;
726 	portid.b.area   = appplogiok.u.d_id.b.area;
727 	portid.b.al_pa  = appplogiok.u.d_id.b.al_pa;
728 
729 	appplogireply.version = EDIF_VERSION1;
730 	switch (appplogiok.type) {
731 	case PL_TYPE_WWPN:
732 		fcport = qla2x00_find_fcport_by_wwpn(vha,
733 		    appplogiok.u.wwpn, 0);
734 		if (!fcport)
735 			ql_dbg(ql_dbg_edif, vha, 0x911d,
736 			    "%s wwpn lookup failed: %8phC\n",
737 			    __func__, appplogiok.u.wwpn);
738 		break;
739 	case PL_TYPE_DID:
740 		fcport = qla2x00_find_fcport_by_pid(vha, &portid);
741 		if (!fcport)
742 			ql_dbg(ql_dbg_edif, vha, 0x911d,
743 			    "%s d_id lookup failed: %x\n", __func__,
744 			    portid.b24);
745 		break;
746 	default:
747 		ql_dbg(ql_dbg_edif, vha, 0x911d,
748 		    "%s undefined type: %x\n", __func__,
749 		    appplogiok.type);
750 		break;
751 	}
752 
753 	if (!fcport) {
754 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
755 		goto errstate_exit;
756 	}
757 
758 	/*
759 	 * if port is online then this is a REKEY operation
760 	 * Only do sa update checking
761 	 */
762 	if (atomic_read(&fcport->state) == FCS_ONLINE) {
763 		ql_dbg(ql_dbg_edif, vha, 0x911d,
764 		    "%s Skipping PRLI complete based on rekey\n", __func__);
765 		appplogireply.prli_status = 1;
766 		SET_DID_STATUS(bsg_reply->result, DID_OK);
767 		qla_edif_app_chk_sa_update(vha, fcport, &appplogireply);
768 		goto errstate_exit;
769 	}
770 
771 	/* make sure in AUTH_PENDING or else reject */
772 	if (fcport->disc_state != DSC_LOGIN_AUTH_PEND) {
773 		ql_dbg(ql_dbg_edif, vha, 0x911e,
774 		    "%s wwpn %8phC is not in auth pending state (%x)\n",
775 		    __func__, fcport->port_name, fcport->disc_state);
776 		SET_DID_STATUS(bsg_reply->result, DID_OK);
777 		appplogireply.prli_status = 0;
778 		goto errstate_exit;
779 	}
780 
781 	SET_DID_STATUS(bsg_reply->result, DID_OK);
782 	appplogireply.prli_status = 1;
783 	fcport->edif.authok = 1;
784 	if (!(fcport->edif.rx_sa_set && fcport->edif.tx_sa_set)) {
785 		ql_dbg(ql_dbg_edif, vha, 0x911e,
786 		    "%s: wwpn %8phC Both SA indexes has not been SET TX %d, RX %d.\n",
787 		    __func__, fcport->port_name, fcport->edif.tx_sa_set,
788 		    fcport->edif.rx_sa_set);
789 		SET_DID_STATUS(bsg_reply->result, DID_OK);
790 		appplogireply.prli_status = 0;
791 		goto errstate_exit;
792 
793 	} else {
794 		ql_dbg(ql_dbg_edif, vha, 0x911e,
795 		    "%s wwpn %8phC Both SA(s) updated.\n", __func__,
796 		    fcport->port_name);
797 		fcport->edif.rx_sa_set = fcport->edif.tx_sa_set = 0;
798 		fcport->edif.rx_sa_pending = fcport->edif.tx_sa_pending = 0;
799 	}
800 
801 	if (qla_ini_mode_enabled(vha)) {
802 		ql_dbg(ql_dbg_edif, vha, 0x911e,
803 		    "%s AUTH complete - RESUME with prli for wwpn %8phC\n",
804 		    __func__, fcport->port_name);
805 		qla24xx_post_prli_work(vha, fcport);
806 	}
807 
808 errstate_exit:
809 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
810 	bsg_reply->reply_payload_rcv_len = sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
811 							       bsg_job->reply_payload.sg_cnt,
812 							       &appplogireply,
813 							       sizeof(struct app_plogi_reply));
814 
815 	return 0;
816 }
817 
818 /**
819  * qla_edif_app_authfail - authentication by app has failed.  Driver is given
820  *   notice to tear down current session.
821  * @vha: host adapter pointer
822  * @bsg_job: user request
823  */
824 static int
825 qla_edif_app_authfail(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
826 {
827 	int32_t			rval = 0;
828 	struct auth_complete_cmd appplogifail;
829 	struct fc_bsg_reply	*bsg_reply = bsg_job->reply;
830 	fc_port_t		*fcport = NULL;
831 	port_id_t		portid = {0};
832 
833 	ql_dbg(ql_dbg_edif, vha, 0x911d, "%s app auth fail\n", __func__);
834 
835 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
836 	    bsg_job->request_payload.sg_cnt, &appplogifail,
837 	    sizeof(struct auth_complete_cmd));
838 
839 	/* silent unaligned access warning */
840 	portid.b.domain = appplogifail.u.d_id.b.domain;
841 	portid.b.area   = appplogifail.u.d_id.b.area;
842 	portid.b.al_pa  = appplogifail.u.d_id.b.al_pa;
843 
844 	/*
845 	 * TODO: edif: app has failed this plogi. Inform driver to
846 	 * take any action (if any).
847 	 */
848 	switch (appplogifail.type) {
849 	case PL_TYPE_WWPN:
850 		fcport = qla2x00_find_fcport_by_wwpn(vha,
851 		    appplogifail.u.wwpn, 0);
852 		SET_DID_STATUS(bsg_reply->result, DID_OK);
853 		break;
854 	case PL_TYPE_DID:
855 		fcport = qla2x00_find_fcport_by_pid(vha, &portid);
856 		if (!fcport)
857 			ql_dbg(ql_dbg_edif, vha, 0x911d,
858 			    "%s d_id lookup failed: %x\n", __func__,
859 			    portid.b24);
860 		SET_DID_STATUS(bsg_reply->result, DID_OK);
861 		break;
862 	default:
863 		ql_dbg(ql_dbg_edif, vha, 0x911e,
864 		    "%s undefined type: %x\n", __func__,
865 		    appplogifail.type);
866 		bsg_job->reply_len = sizeof(struct fc_bsg_reply);
867 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
868 		rval = -1;
869 		break;
870 	}
871 
872 	ql_dbg(ql_dbg_edif, vha, 0x911d,
873 	    "%s fcport is 0x%p\n", __func__, fcport);
874 
875 	if (fcport) {
876 		/* set/reset edif values and flags */
877 		ql_dbg(ql_dbg_edif, vha, 0x911e,
878 		    "%s reset the auth process - %8phC, loopid=%x portid=%06x.\n",
879 		    __func__, fcport->port_name, fcport->loop_id, fcport->d_id.b24);
880 
881 		if (qla_ini_mode_enabled(fcport->vha)) {
882 			fcport->send_els_logo = 1;
883 			qlt_schedule_sess_for_deletion(fcport);
884 		}
885 	}
886 
887 	return rval;
888 }
889 
890 /**
891  * qla_edif_app_getfcinfo - app would like to read session info (wwpn, nportid,
892  *   [initiator|target] mode.  It can specific session with specific nport id or
893  *   all sessions.
894  * @vha: host adapter pointer
895  * @bsg_job: user request pointer
896  */
897 static int
898 qla_edif_app_getfcinfo(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
899 {
900 	int32_t			rval = 0;
901 	int32_t			pcnt = 0;
902 	struct fc_bsg_reply	*bsg_reply = bsg_job->reply;
903 	struct app_pinfo_req	app_req;
904 	struct app_pinfo_reply	*app_reply;
905 	port_id_t		tdid;
906 
907 	ql_dbg(ql_dbg_edif, vha, 0x911d, "%s app get fcinfo\n", __func__);
908 
909 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
910 	    bsg_job->request_payload.sg_cnt, &app_req,
911 	    sizeof(struct app_pinfo_req));
912 
913 	app_reply = kzalloc((sizeof(struct app_pinfo_reply) +
914 	    sizeof(struct app_pinfo) * app_req.num_ports), GFP_KERNEL);
915 
916 	if (!app_reply) {
917 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
918 		rval = -1;
919 	} else {
920 		struct fc_port	*fcport = NULL, *tf;
921 
922 		app_reply->version = EDIF_VERSION1;
923 
924 		list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
925 			if (!(fcport->flags & FCF_FCSP_DEVICE))
926 				continue;
927 
928 			tdid = app_req.remote_pid;
929 
930 			ql_dbg(ql_dbg_edif, vha, 0x2058,
931 			    "APP request entry - portid=%06x.\n", tdid.b24);
932 
933 			/* Ran out of space */
934 			if (pcnt >= app_req.num_ports)
935 				break;
936 
937 			if (tdid.b24 != 0 && tdid.b24 != fcport->d_id.b24)
938 				continue;
939 
940 			if (!N2N_TOPO(vha->hw)) {
941 				if (fcport->scan_state != QLA_FCPORT_FOUND)
942 					continue;
943 
944 				if (fcport->port_type == FCT_UNKNOWN &&
945 				    !fcport->fc4_features)
946 					rval = qla24xx_async_gffid(vha, fcport,
947 								   true);
948 
949 				if (!rval &&
950 				    !(fcport->fc4_features & FC4_FF_TARGET ||
951 				      fcport->port_type &
952 				      (FCT_TARGET | FCT_NVME_TARGET)))
953 					continue;
954 			}
955 
956 			rval = 0;
957 
958 			app_reply->ports[pcnt].version = EDIF_VERSION1;
959 			app_reply->ports[pcnt].remote_type =
960 				VND_CMD_RTYPE_UNKNOWN;
961 			if (fcport->port_type & (FCT_NVME_TARGET | FCT_TARGET))
962 				app_reply->ports[pcnt].remote_type |=
963 					VND_CMD_RTYPE_TARGET;
964 			if (fcport->port_type & (FCT_NVME_INITIATOR | FCT_INITIATOR))
965 				app_reply->ports[pcnt].remote_type |=
966 					VND_CMD_RTYPE_INITIATOR;
967 
968 			app_reply->ports[pcnt].remote_pid = fcport->d_id;
969 
970 			ql_dbg(ql_dbg_edif, vha, 0x2058,
971 			    "Found FC_SP fcport - nn %8phN pn %8phN pcnt %d portid=%06x secure %d.\n",
972 			    fcport->node_name, fcport->port_name, pcnt,
973 			    fcport->d_id.b24, fcport->flags & FCF_FCSP_DEVICE);
974 
975 			switch (fcport->edif.auth_state) {
976 			case VND_CMD_AUTH_STATE_ELS_RCVD:
977 				if (fcport->disc_state == DSC_LOGIN_AUTH_PEND) {
978 					fcport->edif.auth_state = VND_CMD_AUTH_STATE_NEEDED;
979 					app_reply->ports[pcnt].auth_state =
980 						VND_CMD_AUTH_STATE_NEEDED;
981 				} else {
982 					app_reply->ports[pcnt].auth_state =
983 						VND_CMD_AUTH_STATE_ELS_RCVD;
984 				}
985 				break;
986 			default:
987 				app_reply->ports[pcnt].auth_state = fcport->edif.auth_state;
988 				break;
989 			}
990 
991 			memcpy(app_reply->ports[pcnt].remote_wwpn,
992 			    fcport->port_name, 8);
993 
994 			app_reply->ports[pcnt].remote_state =
995 				(atomic_read(&fcport->state) ==
996 				    FCS_ONLINE ? 1 : 0);
997 
998 			pcnt++;
999 
1000 			if (tdid.b24 != 0)
1001 				break;
1002 		}
1003 		app_reply->port_count = pcnt;
1004 		SET_DID_STATUS(bsg_reply->result, DID_OK);
1005 	}
1006 
1007 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1008 	bsg_reply->reply_payload_rcv_len = sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1009 							       bsg_job->reply_payload.sg_cnt,
1010 							       app_reply,
1011 							       sizeof(struct app_pinfo_reply) + sizeof(struct app_pinfo) * pcnt);
1012 
1013 	kfree(app_reply);
1014 
1015 	return rval;
1016 }
1017 
1018 /**
1019  * qla_edif_app_getstats - app would like to read various statistics info
1020  * @vha: host adapter pointer
1021  * @bsg_job: user request
1022  */
1023 static int32_t
1024 qla_edif_app_getstats(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
1025 {
1026 	int32_t			rval = 0;
1027 	struct fc_bsg_reply	*bsg_reply = bsg_job->reply;
1028 	uint32_t size;
1029 
1030 	struct app_sinfo_req	app_req;
1031 	struct app_stats_reply	*app_reply;
1032 	uint32_t pcnt = 0;
1033 
1034 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1035 	    bsg_job->request_payload.sg_cnt, &app_req,
1036 	    sizeof(struct app_sinfo_req));
1037 	if (app_req.num_ports == 0) {
1038 		ql_dbg(ql_dbg_async, vha, 0x911d,
1039 		   "%s app did not indicate number of ports to return\n",
1040 		    __func__);
1041 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
1042 		rval = -1;
1043 	}
1044 
1045 	size = sizeof(struct app_stats_reply) +
1046 	    (sizeof(struct app_sinfo) * app_req.num_ports);
1047 
1048 	app_reply = kzalloc(size, GFP_KERNEL);
1049 	if (!app_reply) {
1050 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
1051 		rval = -1;
1052 	} else {
1053 		struct fc_port	*fcport = NULL, *tf;
1054 
1055 		app_reply->version = EDIF_VERSION1;
1056 
1057 		list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
1058 			if (fcport->edif.enable) {
1059 				if (pcnt > app_req.num_ports)
1060 					break;
1061 
1062 				app_reply->elem[pcnt].rekey_count =
1063 				    fcport->edif.rekey_cnt;
1064 				app_reply->elem[pcnt].tx_bytes =
1065 				    fcport->edif.tx_bytes;
1066 				app_reply->elem[pcnt].rx_bytes =
1067 				    fcport->edif.rx_bytes;
1068 
1069 				memcpy(app_reply->elem[pcnt].remote_wwpn,
1070 				    fcport->port_name, 8);
1071 
1072 				pcnt++;
1073 			}
1074 		}
1075 		app_reply->elem_count = pcnt;
1076 		SET_DID_STATUS(bsg_reply->result, DID_OK);
1077 	}
1078 
1079 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1080 	bsg_reply->reply_payload_rcv_len =
1081 	    sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1082 	       bsg_job->reply_payload.sg_cnt, app_reply,
1083 	       sizeof(struct app_stats_reply) + (sizeof(struct app_sinfo) * pcnt));
1084 
1085 	kfree(app_reply);
1086 
1087 	return rval;
1088 }
1089 
1090 static int32_t
1091 qla_edif_ack(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
1092 {
1093 	struct fc_port *fcport;
1094 	struct aen_complete_cmd ack;
1095 	struct fc_bsg_reply     *bsg_reply = bsg_job->reply;
1096 
1097 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1098 			  bsg_job->request_payload.sg_cnt, &ack, sizeof(ack));
1099 
1100 	ql_dbg(ql_dbg_edif, vha, 0x70cf,
1101 	       "%s: %06x event_code %x\n",
1102 	       __func__, ack.port_id.b24, ack.event_code);
1103 
1104 	fcport = qla2x00_find_fcport_by_pid(vha, &ack.port_id);
1105 	SET_DID_STATUS(bsg_reply->result, DID_OK);
1106 
1107 	if (!fcport) {
1108 		ql_dbg(ql_dbg_edif, vha, 0x70cf,
1109 		       "%s: unable to find fcport %06x \n",
1110 		       __func__, ack.port_id.b24);
1111 		return 0;
1112 	}
1113 
1114 	switch (ack.event_code) {
1115 	case VND_CMD_AUTH_STATE_SESSION_SHUTDOWN:
1116 		fcport->edif.sess_down_acked = 1;
1117 		break;
1118 	default:
1119 		break;
1120 	}
1121 	return 0;
1122 }
1123 
1124 static int qla_edif_consume_dbell(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
1125 {
1126 	struct fc_bsg_reply	*bsg_reply = bsg_job->reply;
1127 	u32 sg_skip, reply_payload_len;
1128 	bool keep;
1129 	struct edb_node *dbnode = NULL;
1130 	struct edif_app_dbell ap;
1131 	int dat_size = 0;
1132 
1133 	sg_skip = 0;
1134 	reply_payload_len = bsg_job->reply_payload.payload_len;
1135 
1136 	while ((reply_payload_len - sg_skip) >= sizeof(struct edb_node)) {
1137 		dbnode = qla_edb_getnext(vha);
1138 		if (dbnode) {
1139 			keep = true;
1140 			dat_size = 0;
1141 			ap.event_code = dbnode->ntype;
1142 			switch (dbnode->ntype) {
1143 			case VND_CMD_AUTH_STATE_SESSION_SHUTDOWN:
1144 			case VND_CMD_AUTH_STATE_NEEDED:
1145 				ap.port_id = dbnode->u.plogi_did;
1146 				dat_size += sizeof(ap.port_id);
1147 				break;
1148 			case VND_CMD_AUTH_STATE_ELS_RCVD:
1149 				ap.port_id = dbnode->u.els_sid;
1150 				dat_size += sizeof(ap.port_id);
1151 				break;
1152 			case VND_CMD_AUTH_STATE_SAUPDATE_COMPL:
1153 				ap.port_id = dbnode->u.sa_aen.port_id;
1154 				memcpy(&ap.event_data, &dbnode->u,
1155 				    sizeof(struct edif_sa_update_aen));
1156 				dat_size += sizeof(struct edif_sa_update_aen);
1157 				break;
1158 			default:
1159 				keep = false;
1160 				ql_log(ql_log_warn, vha, 0x09102,
1161 					"%s unknown DB type=%d %p\n",
1162 					__func__, dbnode->ntype, dbnode);
1163 				break;
1164 			}
1165 			ap.event_data_size = dat_size;
1166 			/* 8 = sizeof(ap.event_code + ap.event_data_size) */
1167 			dat_size += 8;
1168 			if (keep)
1169 				sg_skip += sg_copy_buffer(bsg_job->reply_payload.sg_list,
1170 						bsg_job->reply_payload.sg_cnt,
1171 						&ap, dat_size, sg_skip, false);
1172 
1173 			ql_dbg(ql_dbg_edif, vha, 0x09102,
1174 				"%s Doorbell consumed : type=%d %p\n",
1175 				__func__, dbnode->ntype, dbnode);
1176 
1177 			kfree(dbnode);
1178 		} else {
1179 			break;
1180 		}
1181 	}
1182 
1183 	SET_DID_STATUS(bsg_reply->result, DID_OK);
1184 	bsg_reply->reply_payload_rcv_len = sg_skip;
1185 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1186 
1187 	return 0;
1188 }
1189 
1190 static void __qla_edif_dbell_bsg_done(scsi_qla_host_t *vha, struct bsg_job *bsg_job,
1191 	u32 delay)
1192 {
1193 	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
1194 
1195 	/* small sleep for doorbell events to accumulate */
1196 	if (delay)
1197 		msleep(delay);
1198 
1199 	qla_edif_consume_dbell(vha, bsg_job);
1200 
1201 	bsg_job_done(bsg_job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);
1202 }
1203 
1204 static void qla_edif_dbell_bsg_done(scsi_qla_host_t *vha)
1205 {
1206 	unsigned long flags;
1207 	struct bsg_job *prev_bsg_job = NULL;
1208 
1209 	spin_lock_irqsave(&vha->e_dbell.db_lock, flags);
1210 	if (vha->e_dbell.dbell_bsg_job) {
1211 		prev_bsg_job = vha->e_dbell.dbell_bsg_job;
1212 		vha->e_dbell.dbell_bsg_job = NULL;
1213 	}
1214 	spin_unlock_irqrestore(&vha->e_dbell.db_lock, flags);
1215 
1216 	if (prev_bsg_job)
1217 		__qla_edif_dbell_bsg_done(vha, prev_bsg_job, 0);
1218 }
1219 
1220 static int
1221 qla_edif_dbell_bsg(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
1222 {
1223 	unsigned long flags;
1224 	bool return_bsg = false;
1225 
1226 	/* flush previous dbell bsg */
1227 	qla_edif_dbell_bsg_done(vha);
1228 
1229 	spin_lock_irqsave(&vha->e_dbell.db_lock, flags);
1230 	if (list_empty(&vha->e_dbell.head) && DBELL_ACTIVE(vha)) {
1231 		/*
1232 		 * when the next db event happens, bsg_job will return.
1233 		 * Otherwise, timer will return it.
1234 		 */
1235 		vha->e_dbell.dbell_bsg_job = bsg_job;
1236 		vha->e_dbell.bsg_expire = jiffies + 10 * HZ;
1237 	} else {
1238 		return_bsg = true;
1239 	}
1240 	spin_unlock_irqrestore(&vha->e_dbell.db_lock, flags);
1241 
1242 	if (return_bsg)
1243 		__qla_edif_dbell_bsg_done(vha, bsg_job, 1);
1244 
1245 	return 0;
1246 }
1247 
1248 int32_t
1249 qla_edif_app_mgmt(struct bsg_job *bsg_job)
1250 {
1251 	struct fc_bsg_request	*bsg_request = bsg_job->request;
1252 	struct fc_bsg_reply	*bsg_reply = bsg_job->reply;
1253 	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1254 	scsi_qla_host_t		*vha = shost_priv(host);
1255 	struct app_id		appcheck;
1256 	bool done = true;
1257 	int32_t         rval = 0;
1258 	uint32_t	vnd_sc = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
1259 	u32 level = ql_dbg_edif;
1260 
1261 	/* doorbell is high traffic */
1262 	if (vnd_sc == QL_VND_SC_READ_DBELL)
1263 		level = 0;
1264 
1265 	ql_dbg(level, vha, 0x911d, "%s vnd subcmd=%x\n",
1266 	    __func__, vnd_sc);
1267 
1268 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1269 	    bsg_job->request_payload.sg_cnt, &appcheck,
1270 	    sizeof(struct app_id));
1271 
1272 	if (!vha->hw->flags.edif_enabled ||
1273 		test_bit(VPORT_DELETE, &vha->dpc_flags)) {
1274 		ql_dbg(level, vha, 0x911d,
1275 		    "%s edif not enabled or vp delete. bsg ptr done %p. dpc_flags %lx\n",
1276 		    __func__, bsg_job, vha->dpc_flags);
1277 
1278 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
1279 		goto done;
1280 	}
1281 
1282 	if (!qla_edif_app_check(vha, appcheck)) {
1283 		ql_dbg(level, vha, 0x911d,
1284 		    "%s app checked failed.\n",
1285 		    __func__);
1286 
1287 		bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1288 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
1289 		goto done;
1290 	}
1291 
1292 	switch (vnd_sc) {
1293 	case QL_VND_SC_SA_UPDATE:
1294 		done = false;
1295 		rval = qla24xx_sadb_update(bsg_job);
1296 		break;
1297 	case QL_VND_SC_APP_START:
1298 		rval = qla_edif_app_start(vha, bsg_job);
1299 		break;
1300 	case QL_VND_SC_APP_STOP:
1301 		rval = qla_edif_app_stop(vha, bsg_job);
1302 		break;
1303 	case QL_VND_SC_AUTH_OK:
1304 		rval = qla_edif_app_authok(vha, bsg_job);
1305 		break;
1306 	case QL_VND_SC_AUTH_FAIL:
1307 		rval = qla_edif_app_authfail(vha, bsg_job);
1308 		break;
1309 	case QL_VND_SC_GET_FCINFO:
1310 		rval = qla_edif_app_getfcinfo(vha, bsg_job);
1311 		break;
1312 	case QL_VND_SC_GET_STATS:
1313 		rval = qla_edif_app_getstats(vha, bsg_job);
1314 		break;
1315 	case QL_VND_SC_AEN_COMPLETE:
1316 		rval = qla_edif_ack(vha, bsg_job);
1317 		break;
1318 	case QL_VND_SC_READ_DBELL:
1319 		rval = qla_edif_dbell_bsg(vha, bsg_job);
1320 		done = false;
1321 		break;
1322 	default:
1323 		ql_dbg(ql_dbg_edif, vha, 0x911d, "%s unknown cmd=%x\n",
1324 		    __func__,
1325 		    bsg_request->rqst_data.h_vendor.vendor_cmd[1]);
1326 		rval = EXT_STATUS_INVALID_PARAM;
1327 		done = false;
1328 		break;
1329 	}
1330 
1331 done:
1332 	if (done) {
1333 		ql_dbg(level, vha, 0x7009,
1334 		    "%s: %d  bsg ptr done %p\n", __func__, __LINE__, bsg_job);
1335 		bsg_job_done(bsg_job, bsg_reply->result,
1336 		    bsg_reply->reply_payload_rcv_len);
1337 	}
1338 
1339 	return rval;
1340 }
1341 
1342 static struct edif_sa_ctl *
1343 qla_edif_add_sa_ctl(fc_port_t *fcport, struct qla_sa_update_frame *sa_frame,
1344 	int dir)
1345 {
1346 	struct	edif_sa_ctl *sa_ctl;
1347 	struct qla_sa_update_frame *sap;
1348 	int	index = sa_frame->fast_sa_index;
1349 	unsigned long flags = 0;
1350 
1351 	sa_ctl = kzalloc(sizeof(*sa_ctl), GFP_KERNEL);
1352 	if (!sa_ctl) {
1353 		/* couldn't get space */
1354 		ql_dbg(ql_dbg_edif, fcport->vha, 0x9100,
1355 		    "unable to allocate SA CTL\n");
1356 		return NULL;
1357 	}
1358 
1359 	/*
1360 	 * need to allocate sa_index here and save it
1361 	 * in both sa_ctl->index and sa_frame->fast_sa_index;
1362 	 * If alloc fails then delete sa_ctl and return NULL
1363 	 */
1364 	INIT_LIST_HEAD(&sa_ctl->next);
1365 	sap = &sa_ctl->sa_frame;
1366 	*sap = *sa_frame;
1367 	sa_ctl->index = index;
1368 	sa_ctl->fcport = fcport;
1369 	sa_ctl->flags = 0;
1370 	sa_ctl->state = 0L;
1371 	ql_dbg(ql_dbg_edif, fcport->vha, 0x9100,
1372 	    "%s: Added sa_ctl %p, index %d, state 0x%lx\n",
1373 	    __func__, sa_ctl, sa_ctl->index, sa_ctl->state);
1374 	spin_lock_irqsave(&fcport->edif.sa_list_lock, flags);
1375 	if (dir == SAU_FLG_TX)
1376 		list_add_tail(&sa_ctl->next, &fcport->edif.tx_sa_list);
1377 	else
1378 		list_add_tail(&sa_ctl->next, &fcport->edif.rx_sa_list);
1379 	spin_unlock_irqrestore(&fcport->edif.sa_list_lock, flags);
1380 
1381 	return sa_ctl;
1382 }
1383 
1384 void
1385 qla_edif_flush_sa_ctl_lists(fc_port_t *fcport)
1386 {
1387 	struct edif_sa_ctl *sa_ctl, *tsa_ctl;
1388 	unsigned long flags = 0;
1389 
1390 	spin_lock_irqsave(&fcport->edif.sa_list_lock, flags);
1391 
1392 	list_for_each_entry_safe(sa_ctl, tsa_ctl, &fcport->edif.tx_sa_list,
1393 	    next) {
1394 		list_del(&sa_ctl->next);
1395 		kfree(sa_ctl);
1396 	}
1397 
1398 	list_for_each_entry_safe(sa_ctl, tsa_ctl, &fcport->edif.rx_sa_list,
1399 	    next) {
1400 		list_del(&sa_ctl->next);
1401 		kfree(sa_ctl);
1402 	}
1403 
1404 	spin_unlock_irqrestore(&fcport->edif.sa_list_lock, flags);
1405 }
1406 
1407 struct edif_sa_ctl *
1408 qla_edif_find_sa_ctl_by_index(fc_port_t *fcport, int index, int dir)
1409 {
1410 	struct edif_sa_ctl *sa_ctl, *tsa_ctl;
1411 	struct list_head *sa_list;
1412 
1413 	if (dir == SAU_FLG_TX)
1414 		sa_list = &fcport->edif.tx_sa_list;
1415 	else
1416 		sa_list = &fcport->edif.rx_sa_list;
1417 
1418 	list_for_each_entry_safe(sa_ctl, tsa_ctl, sa_list, next) {
1419 		if (test_bit(EDIF_SA_CTL_USED, &sa_ctl->state) &&
1420 		    sa_ctl->index == index)
1421 			return sa_ctl;
1422 	}
1423 	return NULL;
1424 }
1425 
1426 /* add the sa to the correct list */
1427 static int
1428 qla24xx_check_sadb_avail_slot(struct bsg_job *bsg_job, fc_port_t *fcport,
1429 	struct qla_sa_update_frame *sa_frame)
1430 {
1431 	struct edif_sa_ctl *sa_ctl = NULL;
1432 	int dir;
1433 	uint16_t sa_index;
1434 
1435 	dir = (sa_frame->flags & SAU_FLG_TX);
1436 
1437 	/* map the spi to an sa_index */
1438 	sa_index = qla_edif_sadb_get_sa_index(fcport, sa_frame);
1439 	if (sa_index == RX_DELETE_NO_EDIF_SA_INDEX) {
1440 		/* process rx delete */
1441 		ql_dbg(ql_dbg_edif, fcport->vha, 0x3063,
1442 		    "%s: rx delete for lid 0x%x, spi 0x%x, no entry found\n",
1443 		    __func__, fcport->loop_id, sa_frame->spi);
1444 
1445 		/* build and send the aen */
1446 		fcport->edif.rx_sa_set = 1;
1447 		fcport->edif.rx_sa_pending = 0;
1448 		qla_edb_eventcreate(fcport->vha,
1449 		    VND_CMD_AUTH_STATE_SAUPDATE_COMPL,
1450 		    QL_VND_SA_STAT_SUCCESS,
1451 		    QL_VND_RX_SA_KEY, fcport);
1452 
1453 		/* force a return of good bsg status; */
1454 		return RX_DELETE_NO_EDIF_SA_INDEX;
1455 	} else if (sa_index == INVALID_EDIF_SA_INDEX) {
1456 		ql_dbg(ql_dbg_edif, fcport->vha, 0x9100,
1457 		    "%s: Failed to get sa_index for spi 0x%x, dir: %d\n",
1458 		    __func__, sa_frame->spi, dir);
1459 		return INVALID_EDIF_SA_INDEX;
1460 	}
1461 
1462 	ql_dbg(ql_dbg_edif, fcport->vha, 0x9100,
1463 	    "%s: index %d allocated to spi 0x%x, dir: %d, nport_handle: 0x%x\n",
1464 	    __func__, sa_index, sa_frame->spi, dir, fcport->loop_id);
1465 
1466 	/* This is a local copy of sa_frame. */
1467 	sa_frame->fast_sa_index = sa_index;
1468 	/* create the sa_ctl */
1469 	sa_ctl = qla_edif_add_sa_ctl(fcport, sa_frame, dir);
1470 	if (!sa_ctl) {
1471 		ql_dbg(ql_dbg_edif, fcport->vha, 0x9100,
1472 		    "%s: Failed to add sa_ctl for spi 0x%x, dir: %d, sa_index: %d\n",
1473 		    __func__, sa_frame->spi, dir, sa_index);
1474 		return -1;
1475 	}
1476 
1477 	set_bit(EDIF_SA_CTL_USED, &sa_ctl->state);
1478 
1479 	if (dir == SAU_FLG_TX)
1480 		fcport->edif.tx_rekey_cnt++;
1481 	else
1482 		fcport->edif.rx_rekey_cnt++;
1483 
1484 	ql_dbg(ql_dbg_edif, fcport->vha, 0x9100,
1485 	    "%s: Found sa_ctl %p, index %d, state 0x%lx, tx_cnt %d, rx_cnt %d, nport_handle: 0x%x\n",
1486 	    __func__, sa_ctl, sa_ctl->index, sa_ctl->state,
1487 	    fcport->edif.tx_rekey_cnt,
1488 	    fcport->edif.rx_rekey_cnt, fcport->loop_id);
1489 
1490 	return 0;
1491 }
1492 
1493 #define QLA_SA_UPDATE_FLAGS_RX_KEY      0x0
1494 #define QLA_SA_UPDATE_FLAGS_TX_KEY      0x2
1495 #define EDIF_MSLEEP_INTERVAL 100
1496 #define EDIF_RETRY_COUNT  50
1497 
1498 int
1499 qla24xx_sadb_update(struct bsg_job *bsg_job)
1500 {
1501 	struct	fc_bsg_reply	*bsg_reply = bsg_job->reply;
1502 	struct Scsi_Host *host = fc_bsg_to_shost(bsg_job);
1503 	scsi_qla_host_t *vha = shost_priv(host);
1504 	fc_port_t		*fcport = NULL;
1505 	srb_t			*sp = NULL;
1506 	struct edif_list_entry *edif_entry = NULL;
1507 	int			found = 0;
1508 	int			rval = 0;
1509 	int result = 0, cnt;
1510 	struct qla_sa_update_frame sa_frame;
1511 	struct srb_iocb *iocb_cmd;
1512 	port_id_t portid;
1513 
1514 	ql_dbg(ql_dbg_edif + ql_dbg_verbose, vha, 0x911d,
1515 	    "%s entered, vha: 0x%p\n", __func__, vha);
1516 
1517 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1518 	    bsg_job->request_payload.sg_cnt, &sa_frame,
1519 	    sizeof(struct qla_sa_update_frame));
1520 
1521 	/* Check if host is online */
1522 	if (!vha->flags.online) {
1523 		ql_log(ql_log_warn, vha, 0x70a1, "Host is not online\n");
1524 		rval = -EIO;
1525 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
1526 		goto done;
1527 	}
1528 
1529 	if (DBELL_INACTIVE(vha)) {
1530 		ql_log(ql_log_warn, vha, 0x70a1, "App not started\n");
1531 		rval = -EIO;
1532 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
1533 		goto done;
1534 	}
1535 
1536 	/* silent unaligned access warning */
1537 	portid.b.domain = sa_frame.port_id.b.domain;
1538 	portid.b.area   = sa_frame.port_id.b.area;
1539 	portid.b.al_pa  = sa_frame.port_id.b.al_pa;
1540 
1541 	fcport = qla2x00_find_fcport_by_pid(vha, &portid);
1542 	if (fcport) {
1543 		found = 1;
1544 		if (sa_frame.flags == QLA_SA_UPDATE_FLAGS_TX_KEY)
1545 			fcport->edif.tx_bytes = 0;
1546 		if (sa_frame.flags == QLA_SA_UPDATE_FLAGS_RX_KEY)
1547 			fcport->edif.rx_bytes = 0;
1548 	}
1549 
1550 	if (!found) {
1551 		ql_dbg(ql_dbg_edif, vha, 0x70a3, "Failed to find port= %06x\n",
1552 		    sa_frame.port_id.b24);
1553 		rval = -EINVAL;
1554 		SET_DID_STATUS(bsg_reply->result, DID_NO_CONNECT);
1555 		goto done;
1556 	}
1557 
1558 	/* make sure the nport_handle is valid */
1559 	if (fcport->loop_id == FC_NO_LOOP_ID) {
1560 		ql_dbg(ql_dbg_edif, vha, 0x70e1,
1561 		    "%s: %8phN lid=FC_NO_LOOP_ID, spi: 0x%x, DS %d, returning NO_CONNECT\n",
1562 		    __func__, fcport->port_name, sa_frame.spi,
1563 		    fcport->disc_state);
1564 		rval = -EINVAL;
1565 		SET_DID_STATUS(bsg_reply->result, DID_NO_CONNECT);
1566 		goto done;
1567 	}
1568 
1569 	/* allocate and queue an sa_ctl */
1570 	result = qla24xx_check_sadb_avail_slot(bsg_job, fcport, &sa_frame);
1571 
1572 	/* failure of bsg */
1573 	if (result == INVALID_EDIF_SA_INDEX) {
1574 		ql_dbg(ql_dbg_edif, vha, 0x70e1,
1575 		    "%s: %8phN, skipping update.\n",
1576 		    __func__, fcport->port_name);
1577 		rval = -EINVAL;
1578 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
1579 		goto done;
1580 
1581 	/* rx delete failure */
1582 	} else if (result == RX_DELETE_NO_EDIF_SA_INDEX) {
1583 		ql_dbg(ql_dbg_edif, vha, 0x70e1,
1584 		    "%s: %8phN, skipping rx delete.\n",
1585 		    __func__, fcport->port_name);
1586 		SET_DID_STATUS(bsg_reply->result, DID_OK);
1587 		goto done;
1588 	}
1589 
1590 	ql_dbg(ql_dbg_edif, vha, 0x70e1,
1591 	    "%s: %8phN, sa_index in sa_frame: %d flags %xh\n",
1592 	    __func__, fcport->port_name, sa_frame.fast_sa_index,
1593 	    sa_frame.flags);
1594 
1595 	/* looking for rx index and delete */
1596 	if (((sa_frame.flags & SAU_FLG_TX) == 0) &&
1597 	    (sa_frame.flags & SAU_FLG_INV)) {
1598 		uint16_t nport_handle = fcport->loop_id;
1599 		uint16_t sa_index = sa_frame.fast_sa_index;
1600 
1601 		/*
1602 		 * make sure we have an existing rx key, otherwise just process
1603 		 * this as a straight delete just like TX
1604 		 * This is NOT a normal case, it indicates an error recovery or key cleanup
1605 		 * by the ipsec code above us.
1606 		 */
1607 		edif_entry = qla_edif_list_find_sa_index(fcport, fcport->loop_id);
1608 		if (!edif_entry) {
1609 			ql_dbg(ql_dbg_edif, vha, 0x911d,
1610 			    "%s: WARNING: no active sa_index for nport_handle 0x%x, forcing delete for sa_index 0x%x\n",
1611 			    __func__, fcport->loop_id, sa_index);
1612 			goto force_rx_delete;
1613 		}
1614 
1615 		/*
1616 		 * if we have a forced delete for rx, remove the sa_index from the edif list
1617 		 * and proceed with normal delete.  The rx delay timer should not be running
1618 		 */
1619 		if ((sa_frame.flags & SAU_FLG_FORCE_DELETE) == SAU_FLG_FORCE_DELETE) {
1620 			qla_edif_list_delete_sa_index(fcport, edif_entry);
1621 			ql_dbg(ql_dbg_edif, vha, 0x911d,
1622 			    "%s: FORCE DELETE flag found for nport_handle 0x%x, sa_index 0x%x, forcing DELETE\n",
1623 			    __func__, fcport->loop_id, sa_index);
1624 			kfree(edif_entry);
1625 			goto force_rx_delete;
1626 		}
1627 
1628 		/*
1629 		 * delayed rx delete
1630 		 *
1631 		 * if delete_sa_index is not invalid then there is already
1632 		 * a delayed index in progress, return bsg bad status
1633 		 */
1634 		if (edif_entry->delete_sa_index != INVALID_EDIF_SA_INDEX) {
1635 			struct edif_sa_ctl *sa_ctl;
1636 
1637 			ql_dbg(ql_dbg_edif, vha, 0x911d,
1638 			    "%s: delete for lid 0x%x, delete_sa_index %d is pending\n",
1639 			    __func__, edif_entry->handle, edif_entry->delete_sa_index);
1640 
1641 			/* free up the sa_ctl that was allocated with the sa_index */
1642 			sa_ctl = qla_edif_find_sa_ctl_by_index(fcport, sa_index,
1643 			    (sa_frame.flags & SAU_FLG_TX));
1644 			if (sa_ctl) {
1645 				ql_dbg(ql_dbg_edif, vha, 0x3063,
1646 				    "%s: freeing sa_ctl for index %d\n",
1647 				    __func__, sa_ctl->index);
1648 				qla_edif_free_sa_ctl(fcport, sa_ctl, sa_ctl->index);
1649 			}
1650 
1651 			/* release the sa_index */
1652 			ql_dbg(ql_dbg_edif, vha, 0x3063,
1653 			    "%s: freeing sa_index %d, nph: 0x%x\n",
1654 			    __func__, sa_index, nport_handle);
1655 			qla_edif_sadb_delete_sa_index(fcport, nport_handle, sa_index);
1656 
1657 			rval = -EINVAL;
1658 			SET_DID_STATUS(bsg_reply->result, DID_ERROR);
1659 			goto done;
1660 		}
1661 
1662 		fcport->edif.rekey_cnt++;
1663 
1664 		/* configure and start the rx delay timer */
1665 		edif_entry->fcport = fcport;
1666 		edif_entry->timer.expires = jiffies + RX_DELAY_DELETE_TIMEOUT * HZ;
1667 
1668 		ql_dbg(ql_dbg_edif, vha, 0x911d,
1669 		    "%s: adding timer, entry: %p, delete sa_index %d, lid 0x%x to edif_list\n",
1670 		    __func__, edif_entry, sa_index, nport_handle);
1671 
1672 		/*
1673 		 * Start the timer when we queue the delayed rx delete.
1674 		 * This is an activity timer that goes off if we have not
1675 		 * received packets with the new sa_index
1676 		 */
1677 		add_timer(&edif_entry->timer);
1678 
1679 		/*
1680 		 * sa_delete for rx key with an active rx key including this one
1681 		 * add the delete rx sa index to the hash so we can look for it
1682 		 * in the rsp queue.  Do this after making any changes to the
1683 		 * edif_entry as part of the rx delete.
1684 		 */
1685 
1686 		ql_dbg(ql_dbg_edif, vha, 0x911d,
1687 		    "%s: delete sa_index %d, lid 0x%x to edif_list. bsg done ptr %p\n",
1688 		    __func__, sa_index, nport_handle, bsg_job);
1689 
1690 		edif_entry->delete_sa_index = sa_index;
1691 
1692 		bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1693 		bsg_reply->result = DID_OK << 16;
1694 
1695 		goto done;
1696 
1697 	/*
1698 	 * rx index and update
1699 	 * add the index to the list and continue with normal update
1700 	 */
1701 	} else if (((sa_frame.flags & SAU_FLG_TX) == 0) &&
1702 	    ((sa_frame.flags & SAU_FLG_INV) == 0)) {
1703 		/* sa_update for rx key */
1704 		uint32_t nport_handle = fcport->loop_id;
1705 		uint16_t sa_index = sa_frame.fast_sa_index;
1706 		int result;
1707 
1708 		/*
1709 		 * add the update rx sa index to the hash so we can look for it
1710 		 * in the rsp queue and continue normally
1711 		 */
1712 
1713 		ql_dbg(ql_dbg_edif, vha, 0x911d,
1714 		    "%s:  adding update sa_index %d, lid 0x%x to edif_list\n",
1715 		    __func__, sa_index, nport_handle);
1716 
1717 		result = qla_edif_list_add_sa_update_index(fcport, sa_index,
1718 		    nport_handle);
1719 		if (result) {
1720 			ql_dbg(ql_dbg_edif, vha, 0x911d,
1721 			    "%s: SA_UPDATE failed to add new sa index %d to list for lid 0x%x\n",
1722 			    __func__, sa_index, nport_handle);
1723 		}
1724 	}
1725 	if (sa_frame.flags & SAU_FLG_GMAC_MODE)
1726 		fcport->edif.aes_gmac = 1;
1727 	else
1728 		fcport->edif.aes_gmac = 0;
1729 
1730 force_rx_delete:
1731 	/*
1732 	 * sa_update for both rx and tx keys, sa_delete for tx key
1733 	 * immediately process the request
1734 	 */
1735 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1736 	if (!sp) {
1737 		rval = -ENOMEM;
1738 		SET_DID_STATUS(bsg_reply->result, DID_IMM_RETRY);
1739 		goto done;
1740 	}
1741 
1742 	sp->type = SRB_SA_UPDATE;
1743 	sp->name = "bsg_sa_update";
1744 	sp->u.bsg_job = bsg_job;
1745 	/* sp->free = qla2x00_bsg_sp_free; */
1746 	sp->free = qla2x00_rel_sp;
1747 	sp->done = qla2x00_bsg_job_done;
1748 	iocb_cmd = &sp->u.iocb_cmd;
1749 	iocb_cmd->u.sa_update.sa_frame  = sa_frame;
1750 	cnt = 0;
1751 retry:
1752 	rval = qla2x00_start_sp(sp);
1753 	switch (rval) {
1754 	case QLA_SUCCESS:
1755 		break;
1756 	case EAGAIN:
1757 		msleep(EDIF_MSLEEP_INTERVAL);
1758 		cnt++;
1759 		if (cnt < EDIF_RETRY_COUNT)
1760 			goto retry;
1761 
1762 		fallthrough;
1763 	default:
1764 		ql_log(ql_dbg_edif, vha, 0x70e3,
1765 		       "%s qla2x00_start_sp failed=%d.\n",
1766 		       __func__, rval);
1767 
1768 		qla2x00_rel_sp(sp);
1769 		rval = -EIO;
1770 		SET_DID_STATUS(bsg_reply->result, DID_IMM_RETRY);
1771 		goto done;
1772 	}
1773 
1774 	ql_dbg(ql_dbg_edif, vha, 0x911d,
1775 	    "%s:  %s sent, hdl=%x, portid=%06x.\n",
1776 	    __func__, sp->name, sp->handle, fcport->d_id.b24);
1777 
1778 	fcport->edif.rekey_cnt++;
1779 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1780 	SET_DID_STATUS(bsg_reply->result, DID_OK);
1781 
1782 	return 0;
1783 
1784 /*
1785  * send back error status
1786  */
1787 done:
1788 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1789 	ql_dbg(ql_dbg_edif, vha, 0x911d,
1790 	    "%s:status: FAIL, result: 0x%x, bsg ptr done %p\n",
1791 	    __func__, bsg_reply->result, bsg_job);
1792 	bsg_job_done(bsg_job, bsg_reply->result,
1793 	    bsg_reply->reply_payload_rcv_len);
1794 
1795 	return 0;
1796 }
1797 
1798 static void
1799 qla_enode_free(scsi_qla_host_t *vha, struct enode *node)
1800 {
1801 	node->ntype = N_UNDEF;
1802 	kfree(node);
1803 }
1804 
1805 /**
1806  * qla_enode_init - initialize enode structs & lock
1807  * @vha: host adapter pointer
1808  *
1809  * should only be called when driver attaching
1810  */
1811 void
1812 qla_enode_init(scsi_qla_host_t *vha)
1813 {
1814 	struct	qla_hw_data *ha = vha->hw;
1815 	char	name[32];
1816 
1817 	if (vha->pur_cinfo.enode_flags == ENODE_ACTIVE) {
1818 		/* list still active - error */
1819 		ql_dbg(ql_dbg_edif, vha, 0x09102, "%s enode still active\n",
1820 		    __func__);
1821 		return;
1822 	}
1823 
1824 	/* initialize lock which protects pur_core & init list */
1825 	spin_lock_init(&vha->pur_cinfo.pur_lock);
1826 	INIT_LIST_HEAD(&vha->pur_cinfo.head);
1827 
1828 	snprintf(name, sizeof(name), "%s_%d_purex", QLA2XXX_DRIVER_NAME,
1829 	    ha->pdev->device);
1830 }
1831 
1832 /**
1833  * qla_enode_stop - stop and clear and enode data
1834  * @vha: host adapter pointer
1835  *
1836  * called when app notified it is exiting
1837  */
1838 void
1839 qla_enode_stop(scsi_qla_host_t *vha)
1840 {
1841 	unsigned long flags;
1842 	struct enode *node, *q;
1843 
1844 	if (vha->pur_cinfo.enode_flags != ENODE_ACTIVE) {
1845 		/* doorbell list not enabled */
1846 		ql_dbg(ql_dbg_edif, vha, 0x09102,
1847 		    "%s enode not active\n", __func__);
1848 		return;
1849 	}
1850 
1851 	/* grab lock so list doesn't move */
1852 	spin_lock_irqsave(&vha->pur_cinfo.pur_lock, flags);
1853 
1854 	vha->pur_cinfo.enode_flags &= ~ENODE_ACTIVE; /* mark it not active */
1855 
1856 	/* hopefully this is a null list at this point */
1857 	list_for_each_entry_safe(node, q, &vha->pur_cinfo.head, list) {
1858 		ql_dbg(ql_dbg_edif, vha, 0x910f,
1859 		    "%s freeing enode type=%x, cnt=%x\n", __func__, node->ntype,
1860 		    node->dinfo.nodecnt);
1861 		list_del_init(&node->list);
1862 		qla_enode_free(vha, node);
1863 	}
1864 	spin_unlock_irqrestore(&vha->pur_cinfo.pur_lock, flags);
1865 }
1866 
1867 static void qla_enode_clear(scsi_qla_host_t *vha, port_id_t portid)
1868 {
1869 	unsigned    long flags;
1870 	struct enode    *e, *tmp;
1871 	struct purexevent   *purex;
1872 	LIST_HEAD(enode_list);
1873 
1874 	if (vha->pur_cinfo.enode_flags != ENODE_ACTIVE) {
1875 		ql_dbg(ql_dbg_edif, vha, 0x09102,
1876 		       "%s enode not active\n", __func__);
1877 		return;
1878 	}
1879 	spin_lock_irqsave(&vha->pur_cinfo.pur_lock, flags);
1880 	list_for_each_entry_safe(e, tmp, &vha->pur_cinfo.head, list) {
1881 		purex = &e->u.purexinfo;
1882 		if (purex->pur_info.pur_sid.b24 == portid.b24) {
1883 			ql_dbg(ql_dbg_edif, vha, 0x911d,
1884 			    "%s free ELS sid=%06x. xchg %x, nb=%xh\n",
1885 			    __func__, portid.b24,
1886 			    purex->pur_info.pur_rx_xchg_address,
1887 			    purex->pur_info.pur_bytes_rcvd);
1888 
1889 			list_del_init(&e->list);
1890 			list_add_tail(&e->list, &enode_list);
1891 		}
1892 	}
1893 	spin_unlock_irqrestore(&vha->pur_cinfo.pur_lock, flags);
1894 
1895 	list_for_each_entry_safe(e, tmp, &enode_list, list) {
1896 		list_del_init(&e->list);
1897 		qla_enode_free(vha, e);
1898 	}
1899 }
1900 
1901 /*
1902  *  allocate enode struct and populate buffer
1903  *  returns: enode pointer with buffers
1904  *           NULL on error
1905  */
1906 static struct enode *
1907 qla_enode_alloc(scsi_qla_host_t *vha, uint32_t ntype)
1908 {
1909 	struct enode		*node;
1910 	struct purexevent	*purex;
1911 
1912 	node = kzalloc(RX_ELS_SIZE, GFP_ATOMIC);
1913 	if (!node)
1914 		return NULL;
1915 
1916 	purex = &node->u.purexinfo;
1917 	purex->msgp = (u8 *)(node + 1);
1918 	purex->msgp_len = ELS_MAX_PAYLOAD;
1919 
1920 	node->ntype = ntype;
1921 	INIT_LIST_HEAD(&node->list);
1922 	return node;
1923 }
1924 
1925 static void
1926 qla_enode_add(scsi_qla_host_t *vha, struct enode *ptr)
1927 {
1928 	unsigned long flags;
1929 
1930 	ql_dbg(ql_dbg_edif + ql_dbg_verbose, vha, 0x9109,
1931 	    "%s add enode for type=%x, cnt=%x\n",
1932 	    __func__, ptr->ntype, ptr->dinfo.nodecnt);
1933 
1934 	spin_lock_irqsave(&vha->pur_cinfo.pur_lock, flags);
1935 	list_add_tail(&ptr->list, &vha->pur_cinfo.head);
1936 	spin_unlock_irqrestore(&vha->pur_cinfo.pur_lock, flags);
1937 
1938 	return;
1939 }
1940 
1941 static struct enode *
1942 qla_enode_find(scsi_qla_host_t *vha, uint32_t ntype, uint32_t p1, uint32_t p2)
1943 {
1944 	struct enode		*node_rtn = NULL;
1945 	struct enode		*list_node, *q;
1946 	unsigned long		flags;
1947 	uint32_t		sid;
1948 	struct purexevent	*purex;
1949 
1950 	/* secure the list from moving under us */
1951 	spin_lock_irqsave(&vha->pur_cinfo.pur_lock, flags);
1952 
1953 	list_for_each_entry_safe(list_node, q, &vha->pur_cinfo.head, list) {
1954 
1955 		/* node type determines what p1 and p2 are */
1956 		purex = &list_node->u.purexinfo;
1957 		sid = p1;
1958 
1959 		if (purex->pur_info.pur_sid.b24 == sid) {
1960 			/* found it and its complete */
1961 			node_rtn = list_node;
1962 			list_del(&list_node->list);
1963 			break;
1964 		}
1965 	}
1966 
1967 	spin_unlock_irqrestore(&vha->pur_cinfo.pur_lock, flags);
1968 
1969 	return node_rtn;
1970 }
1971 
1972 /**
1973  * qla_pur_get_pending - read/return authentication message sent
1974  *  from remote port
1975  * @vha: host adapter pointer
1976  * @fcport: session pointer
1977  * @bsg_job: user request where the message is copy to.
1978  */
1979 static int
1980 qla_pur_get_pending(scsi_qla_host_t *vha, fc_port_t *fcport,
1981 	struct bsg_job *bsg_job)
1982 {
1983 	struct enode		*ptr;
1984 	struct purexevent	*purex;
1985 	struct qla_bsg_auth_els_reply *rpl =
1986 	    (struct qla_bsg_auth_els_reply *)bsg_job->reply;
1987 
1988 	bsg_job->reply_len = sizeof(*rpl);
1989 
1990 	ptr = qla_enode_find(vha, N_PUREX, fcport->d_id.b24, PUR_GET);
1991 	if (!ptr) {
1992 		ql_dbg(ql_dbg_edif, vha, 0x9111,
1993 		    "%s no enode data found for %8phN sid=%06x\n",
1994 		    __func__, fcport->port_name, fcport->d_id.b24);
1995 		SET_DID_STATUS(rpl->r.result, DID_IMM_RETRY);
1996 		return -EIO;
1997 	}
1998 
1999 	/*
2000 	 * enode is now off the linked list and is ours to deal with
2001 	 */
2002 	purex = &ptr->u.purexinfo;
2003 
2004 	/* Copy info back to caller */
2005 	rpl->rx_xchg_address = purex->pur_info.pur_rx_xchg_address;
2006 
2007 	SET_DID_STATUS(rpl->r.result, DID_OK);
2008 	rpl->r.reply_payload_rcv_len =
2009 	    sg_pcopy_from_buffer(bsg_job->reply_payload.sg_list,
2010 		bsg_job->reply_payload.sg_cnt, purex->msgp,
2011 		purex->pur_info.pur_bytes_rcvd, 0);
2012 
2013 	/* data copy / passback completed - destroy enode */
2014 	qla_enode_free(vha, ptr);
2015 
2016 	return 0;
2017 }
2018 
2019 /* it is assume qpair lock is held */
2020 static int
2021 qla_els_reject_iocb(scsi_qla_host_t *vha, struct qla_qpair *qp,
2022 	struct qla_els_pt_arg *a)
2023 {
2024 	struct els_entry_24xx *els_iocb;
2025 
2026 	els_iocb = __qla2x00_alloc_iocbs(qp, NULL);
2027 	if (!els_iocb) {
2028 		ql_log(ql_log_warn, vha, 0x700c,
2029 		    "qla2x00_alloc_iocbs failed.\n");
2030 		return QLA_FUNCTION_FAILED;
2031 	}
2032 
2033 	qla_els_pt_iocb(vha, els_iocb, a);
2034 
2035 	ql_dbg(ql_dbg_edif, vha, 0x0183,
2036 	    "Sending ELS reject ox_id %04x s:%06x -> d:%06x\n",
2037 	    a->ox_id, a->sid.b24, a->did.b24);
2038 	ql_dump_buffer(ql_dbg_edif + ql_dbg_verbose, vha, 0x0185,
2039 	    vha->hw->elsrej.c, sizeof(*vha->hw->elsrej.c));
2040 	/* flush iocb to mem before notifying hw doorbell */
2041 	wmb();
2042 	qla2x00_start_iocbs(vha, qp->req);
2043 	return 0;
2044 }
2045 
2046 void
2047 qla_edb_init(scsi_qla_host_t *vha)
2048 {
2049 	if (DBELL_ACTIVE(vha)) {
2050 		/* list already init'd - error */
2051 		ql_dbg(ql_dbg_edif, vha, 0x09102,
2052 		    "edif db already initialized, cannot reinit\n");
2053 		return;
2054 	}
2055 
2056 	/* initialize lock which protects doorbell & init list */
2057 	spin_lock_init(&vha->e_dbell.db_lock);
2058 	INIT_LIST_HEAD(&vha->e_dbell.head);
2059 }
2060 
2061 static void qla_edb_clear(scsi_qla_host_t *vha, port_id_t portid)
2062 {
2063 	unsigned long flags;
2064 	struct edb_node *e, *tmp;
2065 	port_id_t sid;
2066 	LIST_HEAD(edb_list);
2067 
2068 	if (DBELL_INACTIVE(vha)) {
2069 		/* doorbell list not enabled */
2070 		ql_dbg(ql_dbg_edif, vha, 0x09102,
2071 		       "%s doorbell not enabled\n", __func__);
2072 		return;
2073 	}
2074 
2075 	/* grab lock so list doesn't move */
2076 	spin_lock_irqsave(&vha->e_dbell.db_lock, flags);
2077 	list_for_each_entry_safe(e, tmp, &vha->e_dbell.head, list) {
2078 		switch (e->ntype) {
2079 		case VND_CMD_AUTH_STATE_NEEDED:
2080 		case VND_CMD_AUTH_STATE_SESSION_SHUTDOWN:
2081 			sid = e->u.plogi_did;
2082 			break;
2083 		case VND_CMD_AUTH_STATE_ELS_RCVD:
2084 			sid = e->u.els_sid;
2085 			break;
2086 		case VND_CMD_AUTH_STATE_SAUPDATE_COMPL:
2087 			/* app wants to see this  */
2088 			continue;
2089 		default:
2090 			ql_log(ql_log_warn, vha, 0x09102,
2091 			       "%s unknown node type: %x\n", __func__, e->ntype);
2092 			sid.b24 = 0;
2093 			break;
2094 		}
2095 		if (sid.b24 == portid.b24) {
2096 			ql_dbg(ql_dbg_edif, vha, 0x910f,
2097 			       "%s free doorbell event : node type = %x %p\n",
2098 			       __func__, e->ntype, e);
2099 			list_del_init(&e->list);
2100 			list_add_tail(&e->list, &edb_list);
2101 		}
2102 	}
2103 	spin_unlock_irqrestore(&vha->e_dbell.db_lock, flags);
2104 
2105 	list_for_each_entry_safe(e, tmp, &edb_list, list)
2106 		qla_edb_node_free(vha, e);
2107 }
2108 
2109 /* function called when app is stopping */
2110 
2111 void
2112 qla_edb_stop(scsi_qla_host_t *vha)
2113 {
2114 	unsigned long flags;
2115 	struct edb_node *node, *q;
2116 
2117 	if (DBELL_INACTIVE(vha)) {
2118 		/* doorbell list not enabled */
2119 		ql_dbg(ql_dbg_edif, vha, 0x09102,
2120 		    "%s doorbell not enabled\n", __func__);
2121 		return;
2122 	}
2123 
2124 	/* grab lock so list doesn't move */
2125 	spin_lock_irqsave(&vha->e_dbell.db_lock, flags);
2126 
2127 	vha->e_dbell.db_flags &= ~EDB_ACTIVE; /* mark it not active */
2128 	/* hopefully this is a null list at this point */
2129 	list_for_each_entry_safe(node, q, &vha->e_dbell.head, list) {
2130 		ql_dbg(ql_dbg_edif, vha, 0x910f,
2131 		    "%s freeing edb_node type=%x\n",
2132 		    __func__, node->ntype);
2133 		qla_edb_node_free(vha, node);
2134 	}
2135 	spin_unlock_irqrestore(&vha->e_dbell.db_lock, flags);
2136 
2137 	qla_edif_dbell_bsg_done(vha);
2138 }
2139 
2140 static struct edb_node *
2141 qla_edb_node_alloc(scsi_qla_host_t *vha, uint32_t ntype)
2142 {
2143 	struct edb_node	*node;
2144 
2145 	node = kzalloc(sizeof(*node), GFP_ATOMIC);
2146 	if (!node) {
2147 		/* couldn't get space */
2148 		ql_dbg(ql_dbg_edif, vha, 0x9100,
2149 		    "edb node unable to be allocated\n");
2150 		return NULL;
2151 	}
2152 
2153 	node->ntype = ntype;
2154 	INIT_LIST_HEAD(&node->list);
2155 	return node;
2156 }
2157 
2158 /* adds a already allocated enode to the linked list */
2159 static bool
2160 qla_edb_node_add(scsi_qla_host_t *vha, struct edb_node *ptr)
2161 {
2162 	unsigned long		flags;
2163 
2164 	if (DBELL_INACTIVE(vha)) {
2165 		/* doorbell list not enabled */
2166 		ql_dbg(ql_dbg_edif, vha, 0x09102,
2167 		    "%s doorbell not enabled\n", __func__);
2168 		return false;
2169 	}
2170 
2171 	spin_lock_irqsave(&vha->e_dbell.db_lock, flags);
2172 	list_add_tail(&ptr->list, &vha->e_dbell.head);
2173 	spin_unlock_irqrestore(&vha->e_dbell.db_lock, flags);
2174 
2175 	return true;
2176 }
2177 
2178 /* adds event to doorbell list */
2179 void
2180 qla_edb_eventcreate(scsi_qla_host_t *vha, uint32_t dbtype,
2181 	uint32_t data, uint32_t data2, fc_port_t	*sfcport)
2182 {
2183 	struct edb_node	*edbnode;
2184 	fc_port_t *fcport = sfcport;
2185 	port_id_t id;
2186 
2187 	if (!vha->hw->flags.edif_enabled) {
2188 		/* edif not enabled */
2189 		return;
2190 	}
2191 
2192 	if (DBELL_INACTIVE(vha)) {
2193 		if (fcport)
2194 			fcport->edif.auth_state = dbtype;
2195 		/* doorbell list not enabled */
2196 		ql_dbg(ql_dbg_edif, vha, 0x09102,
2197 		    "%s doorbell not enabled (type=%d\n", __func__, dbtype);
2198 		return;
2199 	}
2200 
2201 	edbnode = qla_edb_node_alloc(vha, dbtype);
2202 	if (!edbnode) {
2203 		ql_dbg(ql_dbg_edif, vha, 0x09102,
2204 		    "%s unable to alloc db node\n", __func__);
2205 		return;
2206 	}
2207 
2208 	if (!fcport) {
2209 		id.b.domain = (data >> 16) & 0xff;
2210 		id.b.area = (data >> 8) & 0xff;
2211 		id.b.al_pa = data & 0xff;
2212 		ql_dbg(ql_dbg_edif, vha, 0x09222,
2213 		    "%s: Arrived s_id: %06x\n", __func__,
2214 		    id.b24);
2215 		fcport = qla2x00_find_fcport_by_pid(vha, &id);
2216 		if (!fcport) {
2217 			ql_dbg(ql_dbg_edif, vha, 0x09102,
2218 			    "%s can't find fcport for sid= 0x%x - ignoring\n",
2219 			__func__, id.b24);
2220 			kfree(edbnode);
2221 			return;
2222 		}
2223 	}
2224 
2225 	/* populate the edb node */
2226 	switch (dbtype) {
2227 	case VND_CMD_AUTH_STATE_NEEDED:
2228 	case VND_CMD_AUTH_STATE_SESSION_SHUTDOWN:
2229 		edbnode->u.plogi_did.b24 = fcport->d_id.b24;
2230 		break;
2231 	case VND_CMD_AUTH_STATE_ELS_RCVD:
2232 		edbnode->u.els_sid.b24 = fcport->d_id.b24;
2233 		break;
2234 	case VND_CMD_AUTH_STATE_SAUPDATE_COMPL:
2235 		edbnode->u.sa_aen.port_id = fcport->d_id;
2236 		edbnode->u.sa_aen.status =  data;
2237 		edbnode->u.sa_aen.key_type =  data2;
2238 		edbnode->u.sa_aen.version = EDIF_VERSION1;
2239 		break;
2240 	default:
2241 		ql_dbg(ql_dbg_edif, vha, 0x09102,
2242 			"%s unknown type: %x\n", __func__, dbtype);
2243 		kfree(edbnode);
2244 		edbnode = NULL;
2245 		break;
2246 	}
2247 
2248 	if (edbnode) {
2249 		if (!qla_edb_node_add(vha, edbnode)) {
2250 			ql_dbg(ql_dbg_edif, vha, 0x09102,
2251 			    "%s unable to add dbnode\n", __func__);
2252 			kfree(edbnode);
2253 			return;
2254 		}
2255 		ql_dbg(ql_dbg_edif, vha, 0x09102,
2256 		    "%s Doorbell produced : type=%d %p\n", __func__, dbtype, edbnode);
2257 		qla_edif_dbell_bsg_done(vha);
2258 		if (fcport)
2259 			fcport->edif.auth_state = dbtype;
2260 	}
2261 }
2262 
2263 void
2264 qla_edif_timer(scsi_qla_host_t *vha)
2265 {
2266 	struct qla_hw_data *ha = vha->hw;
2267 
2268 	if (!vha->vp_idx && N2N_TOPO(ha) && ha->flags.n2n_fw_acc_sec) {
2269 		if (DBELL_INACTIVE(vha) &&
2270 		    ha->edif_post_stop_cnt_down) {
2271 			ha->edif_post_stop_cnt_down--;
2272 
2273 			/*
2274 			 * turn off auto 'Plogi Acc + secure=1' feature
2275 			 * Set Add FW option[3]
2276 			 * BIT_15, if.
2277 			 */
2278 			if (ha->edif_post_stop_cnt_down == 0) {
2279 				ql_dbg(ql_dbg_async, vha, 0x911d,
2280 				       "%s chip reset to turn off PLOGI ACC + secure\n",
2281 				       __func__);
2282 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2283 			}
2284 		} else {
2285 			ha->edif_post_stop_cnt_down = 60;
2286 		}
2287 	}
2288 
2289 	if (vha->e_dbell.dbell_bsg_job && time_after_eq(jiffies, vha->e_dbell.bsg_expire))
2290 		qla_edif_dbell_bsg_done(vha);
2291 }
2292 
2293 static void qla_noop_sp_done(srb_t *sp, int res)
2294 {
2295 	sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
2296 	/* ref: INIT */
2297 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
2298 }
2299 
2300 /*
2301  * Called from work queue
2302  * build and send the sa_update iocb to delete an rx sa_index
2303  */
2304 int
2305 qla24xx_issue_sa_replace_iocb(scsi_qla_host_t *vha, struct qla_work_evt *e)
2306 {
2307 	srb_t *sp;
2308 	fc_port_t	*fcport = NULL;
2309 	struct srb_iocb *iocb_cmd = NULL;
2310 	int rval = QLA_SUCCESS;
2311 	struct	edif_sa_ctl *sa_ctl = e->u.sa_update.sa_ctl;
2312 	uint16_t nport_handle = e->u.sa_update.nport_handle;
2313 
2314 	ql_dbg(ql_dbg_edif, vha, 0x70e6,
2315 	    "%s: starting,  sa_ctl: %p\n", __func__, sa_ctl);
2316 
2317 	if (!sa_ctl) {
2318 		ql_dbg(ql_dbg_edif, vha, 0x70e6,
2319 		    "sa_ctl allocation failed\n");
2320 		rval =  -ENOMEM;
2321 		goto done;
2322 	}
2323 
2324 	fcport = sa_ctl->fcport;
2325 
2326 	/* Alloc SRB structure */
2327 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
2328 	if (!sp) {
2329 		ql_dbg(ql_dbg_edif, vha, 0x70e6,
2330 		 "SRB allocation failed\n");
2331 		rval = -ENOMEM;
2332 		goto done;
2333 	}
2334 
2335 	fcport->flags |= FCF_ASYNC_SENT;
2336 	iocb_cmd = &sp->u.iocb_cmd;
2337 	iocb_cmd->u.sa_update.sa_ctl = sa_ctl;
2338 
2339 	ql_dbg(ql_dbg_edif, vha, 0x3073,
2340 	    "Enter: SA REPL portid=%06x, sa_ctl %p, index %x, nport_handle: 0x%x\n",
2341 	    fcport->d_id.b24, sa_ctl, sa_ctl->index, nport_handle);
2342 	/*
2343 	 * if this is a sadb cleanup delete, mark it so the isr can
2344 	 * take the correct action
2345 	 */
2346 	if (sa_ctl->flags & EDIF_SA_CTL_FLG_CLEANUP_DEL) {
2347 		/* mark this srb as a cleanup delete */
2348 		sp->flags |= SRB_EDIF_CLEANUP_DELETE;
2349 		ql_dbg(ql_dbg_edif, vha, 0x70e6,
2350 		    "%s: sp 0x%p flagged as cleanup delete\n", __func__, sp);
2351 	}
2352 
2353 	sp->type = SRB_SA_REPLACE;
2354 	sp->name = "SA_REPLACE";
2355 	sp->fcport = fcport;
2356 	sp->free = qla2x00_rel_sp;
2357 	sp->done = qla_noop_sp_done;
2358 
2359 	rval = qla2x00_start_sp(sp);
2360 
2361 	if (rval != QLA_SUCCESS) {
2362 		goto done_free_sp;
2363 	}
2364 
2365 	return rval;
2366 done_free_sp:
2367 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
2368 	fcport->flags &= ~FCF_ASYNC_SENT;
2369 done:
2370 	fcport->flags &= ~FCF_ASYNC_ACTIVE;
2371 	return rval;
2372 }
2373 
2374 void qla24xx_sa_update_iocb(srb_t *sp, struct sa_update_28xx *sa_update_iocb)
2375 {
2376 	int	itr = 0;
2377 	struct	scsi_qla_host		*vha = sp->vha;
2378 	struct	qla_sa_update_frame	*sa_frame =
2379 		&sp->u.iocb_cmd.u.sa_update.sa_frame;
2380 	u8 flags = 0;
2381 
2382 	switch (sa_frame->flags & (SAU_FLG_INV | SAU_FLG_TX)) {
2383 	case 0:
2384 		ql_dbg(ql_dbg_edif, vha, 0x911d,
2385 		    "%s: EDIF SA UPDATE RX IOCB  vha: 0x%p  index: %d\n",
2386 		    __func__, vha, sa_frame->fast_sa_index);
2387 		break;
2388 	case 1:
2389 		ql_dbg(ql_dbg_edif, vha, 0x911d,
2390 		    "%s: EDIF SA DELETE RX IOCB  vha: 0x%p  index: %d\n",
2391 		    __func__, vha, sa_frame->fast_sa_index);
2392 		flags |= SA_FLAG_INVALIDATE;
2393 		break;
2394 	case 2:
2395 		ql_dbg(ql_dbg_edif, vha, 0x911d,
2396 		    "%s: EDIF SA UPDATE TX IOCB  vha: 0x%p  index: %d\n",
2397 		    __func__, vha, sa_frame->fast_sa_index);
2398 		flags |= SA_FLAG_TX;
2399 		break;
2400 	case 3:
2401 		ql_dbg(ql_dbg_edif, vha, 0x911d,
2402 		    "%s: EDIF SA DELETE TX IOCB  vha: 0x%p  index: %d\n",
2403 		    __func__, vha, sa_frame->fast_sa_index);
2404 		flags |= SA_FLAG_TX | SA_FLAG_INVALIDATE;
2405 		break;
2406 	}
2407 
2408 	sa_update_iocb->entry_type = SA_UPDATE_IOCB_TYPE;
2409 	sa_update_iocb->entry_count = 1;
2410 	sa_update_iocb->sys_define = 0;
2411 	sa_update_iocb->entry_status = 0;
2412 	sa_update_iocb->handle = sp->handle;
2413 	sa_update_iocb->u.nport_handle = cpu_to_le16(sp->fcport->loop_id);
2414 	sa_update_iocb->vp_index = sp->fcport->vha->vp_idx;
2415 	sa_update_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2416 	sa_update_iocb->port_id[1] = sp->fcport->d_id.b.area;
2417 	sa_update_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2418 
2419 	sa_update_iocb->flags = flags;
2420 	sa_update_iocb->salt = cpu_to_le32(sa_frame->salt);
2421 	sa_update_iocb->spi = cpu_to_le32(sa_frame->spi);
2422 	sa_update_iocb->sa_index = cpu_to_le16(sa_frame->fast_sa_index);
2423 
2424 	sa_update_iocb->sa_control |= SA_CNTL_ENC_FCSP;
2425 	if (sp->fcport->edif.aes_gmac)
2426 		sa_update_iocb->sa_control |= SA_CNTL_AES_GMAC;
2427 
2428 	if (sa_frame->flags & SAU_FLG_KEY256) {
2429 		sa_update_iocb->sa_control |= SA_CNTL_KEY256;
2430 		for (itr = 0; itr < 32; itr++)
2431 			sa_update_iocb->sa_key[itr] = sa_frame->sa_key[itr];
2432 	} else {
2433 		sa_update_iocb->sa_control |= SA_CNTL_KEY128;
2434 		for (itr = 0; itr < 16; itr++)
2435 			sa_update_iocb->sa_key[itr] = sa_frame->sa_key[itr];
2436 	}
2437 
2438 	ql_dbg(ql_dbg_edif, vha, 0x921d,
2439 	    "%s SAU Port ID = %02x%02x%02x, flags=%xh, index=%u, ctl=%xh, SPI 0x%x flags 0x%x hdl=%x gmac %d\n",
2440 	    __func__, sa_update_iocb->port_id[2], sa_update_iocb->port_id[1],
2441 	    sa_update_iocb->port_id[0], sa_update_iocb->flags, sa_update_iocb->sa_index,
2442 	    sa_update_iocb->sa_control, sa_update_iocb->spi, sa_frame->flags, sp->handle,
2443 	    sp->fcport->edif.aes_gmac);
2444 
2445 	if (sa_frame->flags & SAU_FLG_TX)
2446 		sp->fcport->edif.tx_sa_pending = 1;
2447 	else
2448 		sp->fcport->edif.rx_sa_pending = 1;
2449 
2450 	sp->fcport->vha->qla_stats.control_requests++;
2451 }
2452 
2453 void
2454 qla24xx_sa_replace_iocb(srb_t *sp, struct sa_update_28xx *sa_update_iocb)
2455 {
2456 	struct	scsi_qla_host		*vha = sp->vha;
2457 	struct srb_iocb *srb_iocb = &sp->u.iocb_cmd;
2458 	struct	edif_sa_ctl		*sa_ctl = srb_iocb->u.sa_update.sa_ctl;
2459 	uint16_t nport_handle = sp->fcport->loop_id;
2460 
2461 	sa_update_iocb->entry_type = SA_UPDATE_IOCB_TYPE;
2462 	sa_update_iocb->entry_count = 1;
2463 	sa_update_iocb->sys_define = 0;
2464 	sa_update_iocb->entry_status = 0;
2465 	sa_update_iocb->handle = sp->handle;
2466 
2467 	sa_update_iocb->u.nport_handle = cpu_to_le16(nport_handle);
2468 
2469 	sa_update_iocb->vp_index = sp->fcport->vha->vp_idx;
2470 	sa_update_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2471 	sa_update_iocb->port_id[1] = sp->fcport->d_id.b.area;
2472 	sa_update_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2473 
2474 	/* Invalidate the index. salt, spi, control & key are ignore */
2475 	sa_update_iocb->flags = SA_FLAG_INVALIDATE;
2476 	sa_update_iocb->salt = 0;
2477 	sa_update_iocb->spi = 0;
2478 	sa_update_iocb->sa_index = cpu_to_le16(sa_ctl->index);
2479 	sa_update_iocb->sa_control = 0;
2480 
2481 	ql_dbg(ql_dbg_edif, vha, 0x921d,
2482 	    "%s SAU DELETE RX Port ID = %02x:%02x:%02x, lid %d flags=%xh, index=%u, hdl=%x\n",
2483 	    __func__, sa_update_iocb->port_id[2], sa_update_iocb->port_id[1],
2484 	    sa_update_iocb->port_id[0], nport_handle, sa_update_iocb->flags,
2485 	    sa_update_iocb->sa_index, sp->handle);
2486 
2487 	sp->fcport->vha->qla_stats.control_requests++;
2488 }
2489 
2490 void qla24xx_auth_els(scsi_qla_host_t *vha, void **pkt, struct rsp_que **rsp)
2491 {
2492 	struct purex_entry_24xx *p = *pkt;
2493 	struct enode		*ptr;
2494 	int		sid;
2495 	u16 totlen;
2496 	struct purexevent	*purex;
2497 	struct scsi_qla_host *host = NULL;
2498 	int rc;
2499 	struct fc_port *fcport;
2500 	struct qla_els_pt_arg a;
2501 	be_id_t beid;
2502 
2503 	memset(&a, 0, sizeof(a));
2504 
2505 	a.els_opcode = ELS_AUTH_ELS;
2506 	a.nport_handle = p->nport_handle;
2507 	a.rx_xchg_address = p->rx_xchg_addr;
2508 	a.did.b.domain = p->s_id[2];
2509 	a.did.b.area   = p->s_id[1];
2510 	a.did.b.al_pa  = p->s_id[0];
2511 	a.tx_byte_count = a.tx_len = sizeof(struct fc_els_ls_rjt);
2512 	a.tx_addr = vha->hw->elsrej.cdma;
2513 	a.vp_idx = vha->vp_idx;
2514 	a.control_flags = EPD_ELS_RJT;
2515 	a.ox_id = le16_to_cpu(p->ox_id);
2516 
2517 	sid = p->s_id[0] | (p->s_id[1] << 8) | (p->s_id[2] << 16);
2518 
2519 	totlen = (le16_to_cpu(p->frame_size) & 0x0fff) - PURX_ELS_HEADER_SIZE;
2520 	if (le16_to_cpu(p->status_flags) & 0x8000) {
2521 		totlen = le16_to_cpu(p->trunc_frame_size);
2522 		qla_els_reject_iocb(vha, (*rsp)->qpair, &a);
2523 		__qla_consume_iocb(vha, pkt, rsp);
2524 		return;
2525 	}
2526 
2527 	if (totlen > ELS_MAX_PAYLOAD) {
2528 		ql_dbg(ql_dbg_edif, vha, 0x0910d,
2529 		    "%s WARNING: verbose ELS frame received (totlen=%x)\n",
2530 		    __func__, totlen);
2531 		qla_els_reject_iocb(vha, (*rsp)->qpair, &a);
2532 		__qla_consume_iocb(vha, pkt, rsp);
2533 		return;
2534 	}
2535 
2536 	if (!vha->hw->flags.edif_enabled) {
2537 		/* edif support not enabled */
2538 		ql_dbg(ql_dbg_edif, vha, 0x910e, "%s edif not enabled\n",
2539 		    __func__);
2540 		qla_els_reject_iocb(vha, (*rsp)->qpair, &a);
2541 		__qla_consume_iocb(vha, pkt, rsp);
2542 		return;
2543 	}
2544 
2545 	ptr = qla_enode_alloc(vha, N_PUREX);
2546 	if (!ptr) {
2547 		ql_dbg(ql_dbg_edif, vha, 0x09109,
2548 		    "WARNING: enode alloc failed for sid=%x\n",
2549 		    sid);
2550 		qla_els_reject_iocb(vha, (*rsp)->qpair, &a);
2551 		__qla_consume_iocb(vha, pkt, rsp);
2552 		return;
2553 	}
2554 
2555 	purex = &ptr->u.purexinfo;
2556 	purex->pur_info.pur_sid = a.did;
2557 	purex->pur_info.pur_bytes_rcvd = totlen;
2558 	purex->pur_info.pur_rx_xchg_address = le32_to_cpu(p->rx_xchg_addr);
2559 	purex->pur_info.pur_nphdl = le16_to_cpu(p->nport_handle);
2560 	purex->pur_info.pur_did.b.domain =  p->d_id[2];
2561 	purex->pur_info.pur_did.b.area =  p->d_id[1];
2562 	purex->pur_info.pur_did.b.al_pa =  p->d_id[0];
2563 	purex->pur_info.vp_idx = p->vp_idx;
2564 
2565 	a.sid = purex->pur_info.pur_did;
2566 
2567 	rc = __qla_copy_purex_to_buffer(vha, pkt, rsp, purex->msgp,
2568 		purex->msgp_len);
2569 	if (rc) {
2570 		qla_els_reject_iocb(vha, (*rsp)->qpair, &a);
2571 		qla_enode_free(vha, ptr);
2572 		return;
2573 	}
2574 	beid.al_pa = purex->pur_info.pur_did.b.al_pa;
2575 	beid.area   = purex->pur_info.pur_did.b.area;
2576 	beid.domain = purex->pur_info.pur_did.b.domain;
2577 	host = qla_find_host_by_d_id(vha, beid);
2578 	if (!host) {
2579 		ql_log(ql_log_fatal, vha, 0x508b,
2580 		    "%s Drop ELS due to unable to find host %06x\n",
2581 		    __func__, purex->pur_info.pur_did.b24);
2582 
2583 		qla_els_reject_iocb(vha, (*rsp)->qpair, &a);
2584 		qla_enode_free(vha, ptr);
2585 		return;
2586 	}
2587 
2588 	fcport = qla2x00_find_fcport_by_pid(host, &purex->pur_info.pur_sid);
2589 
2590 	if (DBELL_INACTIVE(vha)) {
2591 		ql_dbg(ql_dbg_edif, host, 0x0910c, "%s e_dbell.db_flags =%x %06x\n",
2592 		    __func__, host->e_dbell.db_flags,
2593 		    fcport ? fcport->d_id.b24 : 0);
2594 
2595 		qla_els_reject_iocb(host, (*rsp)->qpair, &a);
2596 		qla_enode_free(host, ptr);
2597 		return;
2598 	}
2599 
2600 	if (fcport && EDIF_SESSION_DOWN(fcport)) {
2601 		ql_dbg(ql_dbg_edif, host, 0x13b6,
2602 		    "%s terminate exchange. Send logo to 0x%x\n",
2603 		    __func__, a.did.b24);
2604 
2605 		a.tx_byte_count = a.tx_len = 0;
2606 		a.tx_addr = 0;
2607 		a.control_flags = EPD_RX_XCHG;  /* EPD_RX_XCHG = terminate cmd */
2608 		qla_els_reject_iocb(host, (*rsp)->qpair, &a);
2609 		qla_enode_free(host, ptr);
2610 		/* send logo to let remote port knows to tear down session */
2611 		fcport->send_els_logo = 1;
2612 		qlt_schedule_sess_for_deletion(fcport);
2613 		return;
2614 	}
2615 
2616 	/* add the local enode to the list */
2617 	qla_enode_add(host, ptr);
2618 
2619 	ql_dbg(ql_dbg_edif, host, 0x0910c,
2620 	    "%s COMPLETE purex->pur_info.pur_bytes_rcvd =%xh s:%06x -> d:%06x xchg=%xh\n",
2621 	    __func__, purex->pur_info.pur_bytes_rcvd, purex->pur_info.pur_sid.b24,
2622 	    purex->pur_info.pur_did.b24, purex->pur_info.pur_rx_xchg_address);
2623 
2624 	qla_edb_eventcreate(host, VND_CMD_AUTH_STATE_ELS_RCVD, sid, 0, NULL);
2625 }
2626 
2627 static uint16_t  qla_edif_get_sa_index_from_freepool(fc_port_t *fcport, int dir)
2628 {
2629 	struct scsi_qla_host *vha = fcport->vha;
2630 	struct qla_hw_data *ha = vha->hw;
2631 	void *sa_id_map;
2632 	unsigned long flags = 0;
2633 	u16 sa_index;
2634 
2635 	ql_dbg(ql_dbg_edif + ql_dbg_verbose, vha, 0x3063,
2636 	    "%s: entry\n", __func__);
2637 
2638 	if (dir)
2639 		sa_id_map = ha->edif_tx_sa_id_map;
2640 	else
2641 		sa_id_map = ha->edif_rx_sa_id_map;
2642 
2643 	spin_lock_irqsave(&ha->sadb_fp_lock, flags);
2644 	sa_index = find_first_zero_bit(sa_id_map, EDIF_NUM_SA_INDEX);
2645 	if (sa_index >=  EDIF_NUM_SA_INDEX) {
2646 		spin_unlock_irqrestore(&ha->sadb_fp_lock, flags);
2647 		return INVALID_EDIF_SA_INDEX;
2648 	}
2649 	set_bit(sa_index, sa_id_map);
2650 	spin_unlock_irqrestore(&ha->sadb_fp_lock, flags);
2651 
2652 	if (dir)
2653 		sa_index += EDIF_TX_SA_INDEX_BASE;
2654 
2655 	ql_dbg(ql_dbg_edif, vha, 0x3063,
2656 	    "%s: index retrieved from free pool %d\n", __func__, sa_index);
2657 
2658 	return sa_index;
2659 }
2660 
2661 /* find an sadb entry for an nport_handle */
2662 static struct edif_sa_index_entry *
2663 qla_edif_sadb_find_sa_index_entry(uint16_t nport_handle,
2664 		struct list_head *sa_list)
2665 {
2666 	struct edif_sa_index_entry *entry;
2667 	struct edif_sa_index_entry *tentry;
2668 	struct list_head *indx_list = sa_list;
2669 
2670 	list_for_each_entry_safe(entry, tentry, indx_list, next) {
2671 		if (entry->handle == nport_handle)
2672 			return entry;
2673 	}
2674 	return NULL;
2675 }
2676 
2677 /* remove an sa_index from the nport_handle and return it to the free pool */
2678 static int qla_edif_sadb_delete_sa_index(fc_port_t *fcport, uint16_t nport_handle,
2679 		uint16_t sa_index)
2680 {
2681 	struct edif_sa_index_entry *entry;
2682 	struct list_head *sa_list;
2683 	int dir = (sa_index < EDIF_TX_SA_INDEX_BASE) ? 0 : 1;
2684 	int slot = 0;
2685 	int free_slot_count = 0;
2686 	scsi_qla_host_t *vha = fcport->vha;
2687 	struct qla_hw_data *ha = vha->hw;
2688 	unsigned long flags = 0;
2689 
2690 	ql_dbg(ql_dbg_edif, vha, 0x3063,
2691 	    "%s: entry\n", __func__);
2692 
2693 	if (dir)
2694 		sa_list = &ha->sadb_tx_index_list;
2695 	else
2696 		sa_list = &ha->sadb_rx_index_list;
2697 
2698 	entry = qla_edif_sadb_find_sa_index_entry(nport_handle, sa_list);
2699 	if (!entry) {
2700 		ql_dbg(ql_dbg_edif, vha, 0x3063,
2701 		    "%s: no entry found for nport_handle 0x%x\n",
2702 		    __func__, nport_handle);
2703 		return -1;
2704 	}
2705 
2706 	spin_lock_irqsave(&ha->sadb_lock, flags);
2707 	/*
2708 	 * each tx/rx direction has up to 2 sa indexes/slots. 1 slot for in flight traffic
2709 	 * the other is use at re-key time.
2710 	 */
2711 	for (slot = 0; slot < 2; slot++) {
2712 		if (entry->sa_pair[slot].sa_index == sa_index) {
2713 			entry->sa_pair[slot].sa_index = INVALID_EDIF_SA_INDEX;
2714 			entry->sa_pair[slot].spi = 0;
2715 			free_slot_count++;
2716 			qla_edif_add_sa_index_to_freepool(fcport, dir, sa_index);
2717 		} else if (entry->sa_pair[slot].sa_index == INVALID_EDIF_SA_INDEX) {
2718 			free_slot_count++;
2719 		}
2720 	}
2721 
2722 	if (free_slot_count == 2) {
2723 		list_del(&entry->next);
2724 		kfree(entry);
2725 	}
2726 	spin_unlock_irqrestore(&ha->sadb_lock, flags);
2727 
2728 	ql_dbg(ql_dbg_edif, vha, 0x3063,
2729 	    "%s: sa_index %d removed, free_slot_count: %d\n",
2730 	    __func__, sa_index, free_slot_count);
2731 
2732 	return 0;
2733 }
2734 
2735 void
2736 qla28xx_sa_update_iocb_entry(scsi_qla_host_t *v, struct req_que *req,
2737 	struct sa_update_28xx *pkt)
2738 {
2739 	const char *func = "SA_UPDATE_RESPONSE_IOCB";
2740 	srb_t *sp;
2741 	struct edif_sa_ctl *sa_ctl;
2742 	int old_sa_deleted = 1;
2743 	uint16_t nport_handle;
2744 	struct scsi_qla_host *vha;
2745 
2746 	sp = qla2x00_get_sp_from_handle(v, func, req, pkt);
2747 
2748 	if (!sp) {
2749 		ql_dbg(ql_dbg_edif, v, 0x3063,
2750 			"%s: no sp found for pkt\n", __func__);
2751 		return;
2752 	}
2753 	/* use sp->vha due to npiv */
2754 	vha = sp->vha;
2755 
2756 	switch (pkt->flags & (SA_FLAG_INVALIDATE | SA_FLAG_TX)) {
2757 	case 0:
2758 		ql_dbg(ql_dbg_edif, vha, 0x3063,
2759 		    "%s: EDIF SA UPDATE RX IOCB  vha: 0x%p  index: %d\n",
2760 		    __func__, vha, pkt->sa_index);
2761 		break;
2762 	case 1:
2763 		ql_dbg(ql_dbg_edif, vha, 0x3063,
2764 		    "%s: EDIF SA DELETE RX IOCB  vha: 0x%p  index: %d\n",
2765 		    __func__, vha, pkt->sa_index);
2766 		break;
2767 	case 2:
2768 		ql_dbg(ql_dbg_edif, vha, 0x3063,
2769 		    "%s: EDIF SA UPDATE TX IOCB  vha: 0x%p  index: %d\n",
2770 		    __func__, vha, pkt->sa_index);
2771 		break;
2772 	case 3:
2773 		ql_dbg(ql_dbg_edif, vha, 0x3063,
2774 		    "%s: EDIF SA DELETE TX IOCB  vha: 0x%p  index: %d\n",
2775 		    __func__, vha, pkt->sa_index);
2776 		break;
2777 	}
2778 
2779 	/*
2780 	 * dig the nport handle out of the iocb, fcport->loop_id can not be trusted
2781 	 * to be correct during cleanup sa_update iocbs.
2782 	 */
2783 	nport_handle = sp->fcport->loop_id;
2784 
2785 	ql_dbg(ql_dbg_edif, vha, 0x3063,
2786 	    "%s: %8phN comp status=%x old_sa_info=%x new_sa_info=%x lid %d, index=0x%x pkt_flags %xh hdl=%x\n",
2787 	    __func__, sp->fcport->port_name, pkt->u.comp_sts, pkt->old_sa_info, pkt->new_sa_info,
2788 	    nport_handle, pkt->sa_index, pkt->flags, sp->handle);
2789 
2790 	/* if rx delete, remove the timer */
2791 	if ((pkt->flags & (SA_FLAG_INVALIDATE | SA_FLAG_TX)) ==  SA_FLAG_INVALIDATE) {
2792 		struct edif_list_entry *edif_entry;
2793 
2794 		sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
2795 
2796 		edif_entry = qla_edif_list_find_sa_index(sp->fcport, nport_handle);
2797 		if (edif_entry) {
2798 			ql_dbg(ql_dbg_edif, vha, 0x5033,
2799 			    "%s: removing edif_entry %p, new sa_index: 0x%x\n",
2800 			    __func__, edif_entry, pkt->sa_index);
2801 			qla_edif_list_delete_sa_index(sp->fcport, edif_entry);
2802 			timer_shutdown(&edif_entry->timer);
2803 
2804 			ql_dbg(ql_dbg_edif, vha, 0x5033,
2805 			    "%s: releasing edif_entry %p, new sa_index: 0x%x\n",
2806 			    __func__, edif_entry, pkt->sa_index);
2807 
2808 			kfree(edif_entry);
2809 		}
2810 	}
2811 
2812 	/*
2813 	 * if this is a delete for either tx or rx, make sure it succeeded.
2814 	 * The new_sa_info field should be 0xffff on success
2815 	 */
2816 	if (pkt->flags & SA_FLAG_INVALIDATE)
2817 		old_sa_deleted = (le16_to_cpu(pkt->new_sa_info) == 0xffff) ? 1 : 0;
2818 
2819 	/* Process update and delete the same way */
2820 
2821 	/* If this is an sadb cleanup delete, bypass sending events to IPSEC */
2822 	if (sp->flags & SRB_EDIF_CLEANUP_DELETE) {
2823 		sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
2824 		ql_dbg(ql_dbg_edif, vha, 0x3063,
2825 		    "%s: nph 0x%x, sa_index %d removed from fw\n",
2826 		    __func__, sp->fcport->loop_id, pkt->sa_index);
2827 
2828 	} else if ((pkt->entry_status == 0) && (pkt->u.comp_sts == 0) &&
2829 	    old_sa_deleted) {
2830 		/*
2831 		 * Note: Wa are only keeping track of latest SA,
2832 		 * so we know when we can start enableing encryption per I/O.
2833 		 * If all SA's get deleted, let FW reject the IOCB.
2834 
2835 		 * TODO: edif: don't set enabled here I think
2836 		 * TODO: edif: prli complete is where it should be set
2837 		 */
2838 		ql_dbg(ql_dbg_edif + ql_dbg_verbose, vha, 0x3063,
2839 			"SA(%x)updated for s_id %02x%02x%02x\n",
2840 			pkt->new_sa_info,
2841 			pkt->port_id[2], pkt->port_id[1], pkt->port_id[0]);
2842 		sp->fcport->edif.enable = 1;
2843 		if (pkt->flags & SA_FLAG_TX) {
2844 			sp->fcport->edif.tx_sa_set = 1;
2845 			sp->fcport->edif.tx_sa_pending = 0;
2846 			qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_SAUPDATE_COMPL,
2847 				QL_VND_SA_STAT_SUCCESS,
2848 				QL_VND_TX_SA_KEY, sp->fcport);
2849 		} else {
2850 			sp->fcport->edif.rx_sa_set = 1;
2851 			sp->fcport->edif.rx_sa_pending = 0;
2852 			qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_SAUPDATE_COMPL,
2853 				QL_VND_SA_STAT_SUCCESS,
2854 				QL_VND_RX_SA_KEY, sp->fcport);
2855 		}
2856 	} else {
2857 		ql_dbg(ql_dbg_edif, vha, 0x3063,
2858 		    "%s: %8phN SA update FAILED: sa_index: %d, new_sa_info %d, %02x%02x%02x\n",
2859 		    __func__, sp->fcport->port_name, pkt->sa_index, pkt->new_sa_info,
2860 		    pkt->port_id[2], pkt->port_id[1], pkt->port_id[0]);
2861 
2862 		if (pkt->flags & SA_FLAG_TX)
2863 			qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_SAUPDATE_COMPL,
2864 				(le16_to_cpu(pkt->u.comp_sts) << 16) | QL_VND_SA_STAT_FAILED,
2865 				QL_VND_TX_SA_KEY, sp->fcport);
2866 		else
2867 			qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_SAUPDATE_COMPL,
2868 				(le16_to_cpu(pkt->u.comp_sts) << 16) | QL_VND_SA_STAT_FAILED,
2869 				QL_VND_RX_SA_KEY, sp->fcport);
2870 	}
2871 
2872 	/* for delete, release sa_ctl, sa_index */
2873 	if (pkt->flags & SA_FLAG_INVALIDATE) {
2874 		/* release the sa_ctl */
2875 		sa_ctl = qla_edif_find_sa_ctl_by_index(sp->fcport,
2876 		    le16_to_cpu(pkt->sa_index), (pkt->flags & SA_FLAG_TX));
2877 		if (sa_ctl &&
2878 		    qla_edif_find_sa_ctl_by_index(sp->fcport, sa_ctl->index,
2879 			(pkt->flags & SA_FLAG_TX)) != NULL) {
2880 			ql_dbg(ql_dbg_edif + ql_dbg_verbose, vha, 0x3063,
2881 			    "%s: freeing sa_ctl for index %d\n",
2882 			    __func__, sa_ctl->index);
2883 			qla_edif_free_sa_ctl(sp->fcport, sa_ctl, sa_ctl->index);
2884 		} else {
2885 			ql_dbg(ql_dbg_edif, vha, 0x3063,
2886 			    "%s: sa_ctl NOT freed, sa_ctl: %p\n",
2887 			    __func__, sa_ctl);
2888 		}
2889 		ql_dbg(ql_dbg_edif, vha, 0x3063,
2890 		    "%s: freeing sa_index %d, nph: 0x%x\n",
2891 		    __func__, le16_to_cpu(pkt->sa_index), nport_handle);
2892 		qla_edif_sadb_delete_sa_index(sp->fcport, nport_handle,
2893 		    le16_to_cpu(pkt->sa_index));
2894 	/*
2895 	 * check for a failed sa_update and remove
2896 	 * the sadb entry.
2897 	 */
2898 	} else if (pkt->u.comp_sts) {
2899 		ql_dbg(ql_dbg_edif, vha, 0x3063,
2900 		    "%s: freeing sa_index %d, nph: 0x%x\n",
2901 		    __func__, pkt->sa_index, nport_handle);
2902 		qla_edif_sadb_delete_sa_index(sp->fcport, nport_handle,
2903 		    le16_to_cpu(pkt->sa_index));
2904 		switch (le16_to_cpu(pkt->u.comp_sts)) {
2905 		case CS_PORT_EDIF_UNAVAIL:
2906 		case CS_PORT_EDIF_LOGOUT:
2907 			qlt_schedule_sess_for_deletion(sp->fcport);
2908 			break;
2909 		default:
2910 			break;
2911 		}
2912 	}
2913 
2914 	sp->done(sp, 0);
2915 }
2916 
2917 /**
2918  * qla28xx_start_scsi_edif() - Send a SCSI type 6 command to the ISP
2919  * @sp: command to send to the ISP
2920  *
2921  * Return: non-zero if a failure occurred, else zero.
2922  */
2923 int
2924 qla28xx_start_scsi_edif(srb_t *sp)
2925 {
2926 	int             nseg;
2927 	unsigned long   flags;
2928 	struct scsi_cmnd *cmd;
2929 	uint32_t        *clr_ptr;
2930 	uint32_t        index, i;
2931 	uint32_t        handle;
2932 	uint16_t        cnt;
2933 	int16_t        req_cnt;
2934 	uint16_t        tot_dsds;
2935 	__be32 *fcp_dl;
2936 	uint8_t additional_cdb_len;
2937 	struct ct6_dsd *ctx;
2938 	struct scsi_qla_host *vha = sp->vha;
2939 	struct qla_hw_data *ha = vha->hw;
2940 	struct cmd_type_6 *cmd_pkt;
2941 	struct dsd64	*cur_dsd;
2942 	uint8_t		avail_dsds = 0;
2943 	struct scatterlist *sg;
2944 	struct req_que *req = sp->qpair->req;
2945 	spinlock_t *lock = sp->qpair->qp_lock_ptr;
2946 
2947 	/* Setup device pointers. */
2948 	cmd = GET_CMD_SP(sp);
2949 
2950 	/* So we know we haven't pci_map'ed anything yet */
2951 	tot_dsds = 0;
2952 
2953 	/* Send marker if required */
2954 	if (vha->marker_needed != 0) {
2955 		if (qla2x00_marker(vha, sp->qpair, 0, 0, MK_SYNC_ALL) !=
2956 			QLA_SUCCESS) {
2957 			ql_log(ql_log_warn, vha, 0x300c,
2958 			    "qla2x00_marker failed for cmd=%p.\n", cmd);
2959 			return QLA_FUNCTION_FAILED;
2960 		}
2961 		vha->marker_needed = 0;
2962 	}
2963 
2964 	/* Acquire ring specific lock */
2965 	spin_lock_irqsave(lock, flags);
2966 
2967 	/* Check for room in outstanding command list. */
2968 	handle = req->current_outstanding_cmd;
2969 	for (index = 1; index < req->num_outstanding_cmds; index++) {
2970 		handle++;
2971 		if (handle == req->num_outstanding_cmds)
2972 			handle = 1;
2973 		if (!req->outstanding_cmds[handle])
2974 			break;
2975 	}
2976 	if (index == req->num_outstanding_cmds)
2977 		goto queuing_error;
2978 
2979 	/* Map the sg table so we have an accurate count of sg entries needed */
2980 	if (scsi_sg_count(cmd)) {
2981 		nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2982 		    scsi_sg_count(cmd), cmd->sc_data_direction);
2983 		if (unlikely(!nseg))
2984 			goto queuing_error;
2985 	} else {
2986 		nseg = 0;
2987 	}
2988 
2989 	tot_dsds = nseg;
2990 	req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2991 
2992 	sp->iores.res_type = RESOURCE_INI;
2993 	sp->iores.iocb_cnt = req_cnt;
2994 	if (qla_get_iocbs(sp->qpair, &sp->iores))
2995 		goto queuing_error;
2996 
2997 	if (req->cnt < (req_cnt + 2)) {
2998 		cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
2999 		    rd_reg_dword(req->req_q_out);
3000 		if (req->ring_index < cnt)
3001 			req->cnt = cnt - req->ring_index;
3002 		else
3003 			req->cnt = req->length -
3004 			    (req->ring_index - cnt);
3005 		if (req->cnt < (req_cnt + 2))
3006 			goto queuing_error;
3007 	}
3008 
3009 	ctx = sp->u.scmd.ct6_ctx =
3010 	    mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
3011 	if (!ctx) {
3012 		ql_log(ql_log_fatal, vha, 0x3010,
3013 		    "Failed to allocate ctx for cmd=%p.\n", cmd);
3014 		goto queuing_error;
3015 	}
3016 
3017 	memset(ctx, 0, sizeof(struct ct6_dsd));
3018 	ctx->fcp_cmnd = dma_pool_zalloc(ha->fcp_cmnd_dma_pool,
3019 	    GFP_ATOMIC, &ctx->fcp_cmnd_dma);
3020 	if (!ctx->fcp_cmnd) {
3021 		ql_log(ql_log_fatal, vha, 0x3011,
3022 		    "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
3023 		goto queuing_error;
3024 	}
3025 
3026 	/* Initialize the DSD list and dma handle */
3027 	INIT_LIST_HEAD(&ctx->dsd_list);
3028 	ctx->dsd_use_cnt = 0;
3029 
3030 	if (cmd->cmd_len > 16) {
3031 		additional_cdb_len = cmd->cmd_len - 16;
3032 		if ((cmd->cmd_len % 4) != 0) {
3033 			/*
3034 			 * SCSI command bigger than 16 bytes must be
3035 			 * multiple of 4
3036 			 */
3037 			ql_log(ql_log_warn, vha, 0x3012,
3038 			    "scsi cmd len %d not multiple of 4 for cmd=%p.\n",
3039 			    cmd->cmd_len, cmd);
3040 			goto queuing_error_fcp_cmnd;
3041 		}
3042 		ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
3043 	} else {
3044 		additional_cdb_len = 0;
3045 		ctx->fcp_cmnd_len = 12 + 16 + 4;
3046 	}
3047 
3048 	cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
3049 	cmd_pkt->handle = make_handle(req->id, handle);
3050 
3051 	/*
3052 	 * Zero out remaining portion of packet.
3053 	 * tagged queuing modifier -- default is TSK_SIMPLE (0).
3054 	 */
3055 	clr_ptr = (uint32_t *)cmd_pkt + 2;
3056 	memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
3057 	cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
3058 
3059 	/* No data transfer */
3060 	if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
3061 		cmd_pkt->byte_count = cpu_to_le32(0);
3062 		goto no_dsds;
3063 	}
3064 
3065 	/* Set transfer direction */
3066 	if (cmd->sc_data_direction == DMA_TO_DEVICE) {
3067 		cmd_pkt->control_flags = cpu_to_le16(CF_WRITE_DATA);
3068 		vha->qla_stats.output_bytes += scsi_bufflen(cmd);
3069 		vha->qla_stats.output_requests++;
3070 		sp->fcport->edif.tx_bytes += scsi_bufflen(cmd);
3071 	} else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
3072 		cmd_pkt->control_flags = cpu_to_le16(CF_READ_DATA);
3073 		vha->qla_stats.input_bytes += scsi_bufflen(cmd);
3074 		vha->qla_stats.input_requests++;
3075 		sp->fcport->edif.rx_bytes += scsi_bufflen(cmd);
3076 	}
3077 
3078 	cmd_pkt->control_flags |= cpu_to_le16(CF_EN_EDIF);
3079 	cmd_pkt->control_flags &= ~(cpu_to_le16(CF_NEW_SA));
3080 
3081 	/* One DSD is available in the Command Type 6 IOCB */
3082 	avail_dsds = 1;
3083 	cur_dsd = &cmd_pkt->fcp_dsd;
3084 
3085 	/* Load data segments */
3086 	scsi_for_each_sg(cmd, sg, tot_dsds, i) {
3087 		dma_addr_t      sle_dma;
3088 		cont_a64_entry_t *cont_pkt;
3089 
3090 		/* Allocate additional continuation packets? */
3091 		if (avail_dsds == 0) {
3092 			/*
3093 			 * Five DSDs are available in the Continuation
3094 			 * Type 1 IOCB.
3095 			 */
3096 			cont_pkt = qla2x00_prep_cont_type1_iocb(vha, req);
3097 			cur_dsd = cont_pkt->dsd;
3098 			avail_dsds = 5;
3099 		}
3100 
3101 		sle_dma = sg_dma_address(sg);
3102 		put_unaligned_le64(sle_dma, &cur_dsd->address);
3103 		cur_dsd->length = cpu_to_le32(sg_dma_len(sg));
3104 		cur_dsd++;
3105 		avail_dsds--;
3106 	}
3107 
3108 no_dsds:
3109 	/* Set NPORT-ID and LUN number*/
3110 	cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3111 	cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
3112 	cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
3113 	cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
3114 	cmd_pkt->vp_index = sp->vha->vp_idx;
3115 
3116 	cmd_pkt->entry_type = COMMAND_TYPE_6;
3117 
3118 	/* Set total data segment count. */
3119 	cmd_pkt->entry_count = (uint8_t)req_cnt;
3120 
3121 	int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
3122 	host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
3123 
3124 	/* build FCP_CMND IU */
3125 	int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
3126 	ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
3127 
3128 	if (cmd->sc_data_direction == DMA_TO_DEVICE)
3129 		ctx->fcp_cmnd->additional_cdb_len |= 1;
3130 	else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
3131 		ctx->fcp_cmnd->additional_cdb_len |= 2;
3132 
3133 	/* Populate the FCP_PRIO. */
3134 	if (ha->flags.fcp_prio_enabled)
3135 		ctx->fcp_cmnd->task_attribute |=
3136 		    sp->fcport->fcp_prio << 3;
3137 
3138 	memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
3139 
3140 	fcp_dl = (__be32 *)(ctx->fcp_cmnd->cdb + 16 +
3141 	    additional_cdb_len);
3142 	*fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
3143 
3144 	cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
3145 	put_unaligned_le64(ctx->fcp_cmnd_dma, &cmd_pkt->fcp_cmnd_dseg_address);
3146 
3147 	sp->flags |= SRB_FCP_CMND_DMA_VALID;
3148 	cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
3149 	/* Set total data segment count. */
3150 	cmd_pkt->entry_count = (uint8_t)req_cnt;
3151 	cmd_pkt->entry_status = 0;
3152 
3153 	/* Build command packet. */
3154 	req->current_outstanding_cmd = handle;
3155 	req->outstanding_cmds[handle] = sp;
3156 	sp->handle = handle;
3157 	cmd->host_scribble = (unsigned char *)(unsigned long)handle;
3158 	req->cnt -= req_cnt;
3159 
3160 	/* Adjust ring index. */
3161 	wmb();
3162 	req->ring_index++;
3163 	if (req->ring_index == req->length) {
3164 		req->ring_index = 0;
3165 		req->ring_ptr = req->ring;
3166 	} else {
3167 		req->ring_ptr++;
3168 	}
3169 
3170 	sp->qpair->cmd_cnt++;
3171 	/* Set chip new ring index. */
3172 	wrt_reg_dword(req->req_q_in, req->ring_index);
3173 
3174 	spin_unlock_irqrestore(lock, flags);
3175 
3176 	return QLA_SUCCESS;
3177 
3178 queuing_error_fcp_cmnd:
3179 	dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
3180 queuing_error:
3181 	if (tot_dsds)
3182 		scsi_dma_unmap(cmd);
3183 
3184 	if (sp->u.scmd.ct6_ctx) {
3185 		mempool_free(sp->u.scmd.ct6_ctx, ha->ctx_mempool);
3186 		sp->u.scmd.ct6_ctx = NULL;
3187 	}
3188 	qla_put_iocbs(sp->qpair, &sp->iores);
3189 	spin_unlock_irqrestore(lock, flags);
3190 
3191 	return QLA_FUNCTION_FAILED;
3192 }
3193 
3194 /**********************************************
3195  * edif update/delete sa_index list functions *
3196  **********************************************/
3197 
3198 /* clear the edif_indx_list for this port */
3199 void qla_edif_list_del(fc_port_t *fcport)
3200 {
3201 	struct edif_list_entry *indx_lst;
3202 	struct edif_list_entry *tindx_lst;
3203 	struct list_head *indx_list = &fcport->edif.edif_indx_list;
3204 	unsigned long flags = 0;
3205 
3206 	spin_lock_irqsave(&fcport->edif.indx_list_lock, flags);
3207 	list_for_each_entry_safe(indx_lst, tindx_lst, indx_list, next) {
3208 		list_del(&indx_lst->next);
3209 		kfree(indx_lst);
3210 	}
3211 	spin_unlock_irqrestore(&fcport->edif.indx_list_lock, flags);
3212 }
3213 
3214 /******************
3215  * SADB functions *
3216  ******************/
3217 
3218 /* allocate/retrieve an sa_index for a given spi */
3219 static uint16_t qla_edif_sadb_get_sa_index(fc_port_t *fcport,
3220 		struct qla_sa_update_frame *sa_frame)
3221 {
3222 	struct edif_sa_index_entry *entry;
3223 	struct list_head *sa_list;
3224 	uint16_t sa_index;
3225 	int dir = sa_frame->flags & SAU_FLG_TX;
3226 	int slot = 0;
3227 	int free_slot = -1;
3228 	scsi_qla_host_t *vha = fcport->vha;
3229 	struct qla_hw_data *ha = vha->hw;
3230 	unsigned long flags = 0;
3231 	uint16_t nport_handle = fcport->loop_id;
3232 
3233 	ql_dbg(ql_dbg_edif, vha, 0x3063,
3234 	    "%s: entry  fc_port: %p, nport_handle: 0x%x\n",
3235 	    __func__, fcport, nport_handle);
3236 
3237 	if (dir)
3238 		sa_list = &ha->sadb_tx_index_list;
3239 	else
3240 		sa_list = &ha->sadb_rx_index_list;
3241 
3242 	entry = qla_edif_sadb_find_sa_index_entry(nport_handle, sa_list);
3243 	if (!entry) {
3244 		if ((sa_frame->flags & (SAU_FLG_TX | SAU_FLG_INV)) == SAU_FLG_INV) {
3245 			ql_dbg(ql_dbg_edif, vha, 0x3063,
3246 			    "%s: rx delete request with no entry\n", __func__);
3247 			return RX_DELETE_NO_EDIF_SA_INDEX;
3248 		}
3249 
3250 		/* if there is no entry for this nport, add one */
3251 		entry = kzalloc((sizeof(struct edif_sa_index_entry)), GFP_ATOMIC);
3252 		if (!entry)
3253 			return INVALID_EDIF_SA_INDEX;
3254 
3255 		sa_index = qla_edif_get_sa_index_from_freepool(fcport, dir);
3256 		if (sa_index == INVALID_EDIF_SA_INDEX) {
3257 			kfree(entry);
3258 			return INVALID_EDIF_SA_INDEX;
3259 		}
3260 
3261 		INIT_LIST_HEAD(&entry->next);
3262 		entry->handle = nport_handle;
3263 		entry->fcport = fcport;
3264 		entry->sa_pair[0].spi = sa_frame->spi;
3265 		entry->sa_pair[0].sa_index = sa_index;
3266 		entry->sa_pair[1].spi = 0;
3267 		entry->sa_pair[1].sa_index = INVALID_EDIF_SA_INDEX;
3268 		spin_lock_irqsave(&ha->sadb_lock, flags);
3269 		list_add_tail(&entry->next, sa_list);
3270 		spin_unlock_irqrestore(&ha->sadb_lock, flags);
3271 		ql_dbg(ql_dbg_edif, vha, 0x3063,
3272 		    "%s: Created new sadb entry for nport_handle 0x%x, spi 0x%x, returning sa_index %d\n",
3273 		    __func__, nport_handle, sa_frame->spi, sa_index);
3274 
3275 		return sa_index;
3276 	}
3277 
3278 	spin_lock_irqsave(&ha->sadb_lock, flags);
3279 
3280 	/* see if we already have an entry for this spi */
3281 	for (slot = 0; slot < 2; slot++) {
3282 		if (entry->sa_pair[slot].sa_index == INVALID_EDIF_SA_INDEX) {
3283 			free_slot = slot;
3284 		} else {
3285 			if (entry->sa_pair[slot].spi == sa_frame->spi) {
3286 				spin_unlock_irqrestore(&ha->sadb_lock, flags);
3287 				ql_dbg(ql_dbg_edif, vha, 0x3063,
3288 				    "%s: sadb slot %d entry for lid 0x%x, spi 0x%x found, sa_index %d\n",
3289 				    __func__, slot, entry->handle, sa_frame->spi,
3290 				    entry->sa_pair[slot].sa_index);
3291 				return entry->sa_pair[slot].sa_index;
3292 			}
3293 		}
3294 	}
3295 	spin_unlock_irqrestore(&ha->sadb_lock, flags);
3296 
3297 	/* both slots are used */
3298 	if (free_slot == -1) {
3299 		ql_dbg(ql_dbg_edif, vha, 0x3063,
3300 		    "%s: WARNING: No free slots in sadb for nport_handle 0x%x, spi: 0x%x\n",
3301 		    __func__, entry->handle, sa_frame->spi);
3302 		ql_dbg(ql_dbg_edif, vha, 0x3063,
3303 		    "%s: Slot 0  spi: 0x%x  sa_index: %d,  Slot 1  spi: 0x%x  sa_index: %d\n",
3304 		    __func__, entry->sa_pair[0].spi, entry->sa_pair[0].sa_index,
3305 		    entry->sa_pair[1].spi, entry->sa_pair[1].sa_index);
3306 
3307 		return INVALID_EDIF_SA_INDEX;
3308 	}
3309 
3310 	/* there is at least one free slot, use it */
3311 	sa_index = qla_edif_get_sa_index_from_freepool(fcport, dir);
3312 	if (sa_index == INVALID_EDIF_SA_INDEX) {
3313 		ql_dbg(ql_dbg_edif, fcport->vha, 0x3063,
3314 		    "%s: empty freepool!!\n", __func__);
3315 		return INVALID_EDIF_SA_INDEX;
3316 	}
3317 
3318 	spin_lock_irqsave(&ha->sadb_lock, flags);
3319 	entry->sa_pair[free_slot].spi = sa_frame->spi;
3320 	entry->sa_pair[free_slot].sa_index = sa_index;
3321 	spin_unlock_irqrestore(&ha->sadb_lock, flags);
3322 	ql_dbg(ql_dbg_edif, fcport->vha, 0x3063,
3323 	    "%s: sadb slot %d entry for nport_handle 0x%x, spi 0x%x added, returning sa_index %d\n",
3324 	    __func__, free_slot, entry->handle, sa_frame->spi, sa_index);
3325 
3326 	return sa_index;
3327 }
3328 
3329 /* release any sadb entries -- only done at teardown */
3330 void qla_edif_sadb_release(struct qla_hw_data *ha)
3331 {
3332 	struct edif_sa_index_entry *entry, *tmp;
3333 
3334 	list_for_each_entry_safe(entry, tmp, &ha->sadb_rx_index_list, next) {
3335 		list_del(&entry->next);
3336 		kfree(entry);
3337 	}
3338 
3339 	list_for_each_entry_safe(entry, tmp, &ha->sadb_tx_index_list, next) {
3340 		list_del(&entry->next);
3341 		kfree(entry);
3342 	}
3343 }
3344 
3345 /**************************
3346  * sadb freepool functions
3347  **************************/
3348 
3349 /* build the rx and tx sa_index free pools -- only done at fcport init */
3350 int qla_edif_sadb_build_free_pool(struct qla_hw_data *ha)
3351 {
3352 	ha->edif_tx_sa_id_map =
3353 	    kcalloc(BITS_TO_LONGS(EDIF_NUM_SA_INDEX), sizeof(long), GFP_KERNEL);
3354 
3355 	if (!ha->edif_tx_sa_id_map) {
3356 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0009,
3357 		    "Unable to allocate memory for sadb tx.\n");
3358 		return -ENOMEM;
3359 	}
3360 
3361 	ha->edif_rx_sa_id_map =
3362 	    kcalloc(BITS_TO_LONGS(EDIF_NUM_SA_INDEX), sizeof(long), GFP_KERNEL);
3363 	if (!ha->edif_rx_sa_id_map) {
3364 		kfree(ha->edif_tx_sa_id_map);
3365 		ha->edif_tx_sa_id_map = NULL;
3366 		ql_log_pci(ql_log_fatal, ha->pdev, 0x0009,
3367 		    "Unable to allocate memory for sadb rx.\n");
3368 		return -ENOMEM;
3369 	}
3370 	return 0;
3371 }
3372 
3373 /* release the free pool - only done during fcport teardown */
3374 void qla_edif_sadb_release_free_pool(struct qla_hw_data *ha)
3375 {
3376 	kfree(ha->edif_tx_sa_id_map);
3377 	ha->edif_tx_sa_id_map = NULL;
3378 	kfree(ha->edif_rx_sa_id_map);
3379 	ha->edif_rx_sa_id_map = NULL;
3380 }
3381 
3382 static void __chk_edif_rx_sa_delete_pending(scsi_qla_host_t *vha,
3383 		fc_port_t *fcport, uint32_t handle, uint16_t sa_index)
3384 {
3385 	struct edif_list_entry *edif_entry;
3386 	struct edif_sa_ctl *sa_ctl;
3387 	uint16_t delete_sa_index = INVALID_EDIF_SA_INDEX;
3388 	unsigned long flags = 0;
3389 	uint16_t nport_handle = fcport->loop_id;
3390 	uint16_t cached_nport_handle;
3391 
3392 	spin_lock_irqsave(&fcport->edif.indx_list_lock, flags);
3393 	edif_entry = qla_edif_list_find_sa_index(fcport, nport_handle);
3394 	if (!edif_entry) {
3395 		spin_unlock_irqrestore(&fcport->edif.indx_list_lock, flags);
3396 		return;		/* no pending delete for this handle */
3397 	}
3398 
3399 	/*
3400 	 * check for no pending delete for this index or iocb does not
3401 	 * match rx sa_index
3402 	 */
3403 	if (edif_entry->delete_sa_index == INVALID_EDIF_SA_INDEX ||
3404 	    edif_entry->update_sa_index != sa_index) {
3405 		spin_unlock_irqrestore(&fcport->edif.indx_list_lock, flags);
3406 		return;
3407 	}
3408 
3409 	/*
3410 	 * wait until we have seen at least EDIF_DELAY_COUNT transfers before
3411 	 * queueing RX delete
3412 	 */
3413 	if (edif_entry->count++ < EDIF_RX_DELETE_FILTER_COUNT) {
3414 		spin_unlock_irqrestore(&fcport->edif.indx_list_lock, flags);
3415 		return;
3416 	}
3417 
3418 	ql_dbg(ql_dbg_edif, vha, 0x5033,
3419 	    "%s: invalidating delete_sa_index,  update_sa_index: 0x%x sa_index: 0x%x, delete_sa_index: 0x%x\n",
3420 	    __func__, edif_entry->update_sa_index, sa_index, edif_entry->delete_sa_index);
3421 
3422 	delete_sa_index = edif_entry->delete_sa_index;
3423 	edif_entry->delete_sa_index = INVALID_EDIF_SA_INDEX;
3424 	cached_nport_handle = edif_entry->handle;
3425 	spin_unlock_irqrestore(&fcport->edif.indx_list_lock, flags);
3426 
3427 	/* sanity check on the nport handle */
3428 	if (nport_handle != cached_nport_handle) {
3429 		ql_dbg(ql_dbg_edif, vha, 0x3063,
3430 		    "%s: POST SA DELETE nport_handle mismatch: lid: 0x%x, edif_entry nph: 0x%x\n",
3431 		    __func__, nport_handle, cached_nport_handle);
3432 	}
3433 
3434 	/* find the sa_ctl for the delete and schedule the delete */
3435 	sa_ctl = qla_edif_find_sa_ctl_by_index(fcport, delete_sa_index, 0);
3436 	if (sa_ctl) {
3437 		ql_dbg(ql_dbg_edif, vha, 0x3063,
3438 		    "%s: POST SA DELETE sa_ctl: %p, index recvd %d\n",
3439 		    __func__, sa_ctl, sa_index);
3440 		ql_dbg(ql_dbg_edif, vha, 0x3063,
3441 		    "delete index %d, update index: %d, nport handle: 0x%x, handle: 0x%x\n",
3442 		    delete_sa_index,
3443 		    edif_entry->update_sa_index, nport_handle, handle);
3444 
3445 		sa_ctl->flags = EDIF_SA_CTL_FLG_DEL;
3446 		set_bit(EDIF_SA_CTL_REPL, &sa_ctl->state);
3447 		qla_post_sa_replace_work(fcport->vha, fcport,
3448 		    nport_handle, sa_ctl);
3449 	} else {
3450 		ql_dbg(ql_dbg_edif, vha, 0x3063,
3451 		    "%s: POST SA DELETE sa_ctl not found for delete_sa_index: %d\n",
3452 		    __func__, delete_sa_index);
3453 	}
3454 }
3455 
3456 void qla_chk_edif_rx_sa_delete_pending(scsi_qla_host_t *vha,
3457 		srb_t *sp, struct sts_entry_24xx *sts24)
3458 {
3459 	fc_port_t *fcport = sp->fcport;
3460 	/* sa_index used by this iocb */
3461 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
3462 	uint32_t handle;
3463 
3464 	handle = (uint32_t)LSW(sts24->handle);
3465 
3466 	/* find out if this status iosb is for a scsi read */
3467 	if (cmd->sc_data_direction != DMA_FROM_DEVICE)
3468 		return;
3469 
3470 	return __chk_edif_rx_sa_delete_pending(vha, fcport, handle,
3471 	   le16_to_cpu(sts24->edif_sa_index));
3472 }
3473 
3474 void qlt_chk_edif_rx_sa_delete_pending(scsi_qla_host_t *vha, fc_port_t *fcport,
3475 		struct ctio7_from_24xx *pkt)
3476 {
3477 	__chk_edif_rx_sa_delete_pending(vha, fcport,
3478 	    pkt->handle, le16_to_cpu(pkt->edif_sa_index));
3479 }
3480 
3481 static void qla_parse_auth_els_ctl(struct srb *sp)
3482 {
3483 	struct qla_els_pt_arg *a = &sp->u.bsg_cmd.u.els_arg;
3484 	struct bsg_job *bsg_job = sp->u.bsg_cmd.bsg_job;
3485 	struct fc_bsg_request *request = bsg_job->request;
3486 	struct qla_bsg_auth_els_request *p =
3487 	    (struct qla_bsg_auth_els_request *)bsg_job->request;
3488 
3489 	a->tx_len = a->tx_byte_count = sp->remap.req.len;
3490 	a->tx_addr = sp->remap.req.dma;
3491 	a->rx_len = a->rx_byte_count = sp->remap.rsp.len;
3492 	a->rx_addr = sp->remap.rsp.dma;
3493 
3494 	if (p->e.sub_cmd == SEND_ELS_REPLY) {
3495 		a->control_flags = p->e.extra_control_flags << 13;
3496 		a->rx_xchg_address = cpu_to_le32(p->e.extra_rx_xchg_address);
3497 		if (p->e.extra_control_flags == BSG_CTL_FLAG_LS_ACC)
3498 			a->els_opcode = ELS_LS_ACC;
3499 		else if (p->e.extra_control_flags == BSG_CTL_FLAG_LS_RJT)
3500 			a->els_opcode = ELS_LS_RJT;
3501 	}
3502 	a->did = sp->fcport->d_id;
3503 	a->els_opcode =  request->rqst_data.h_els.command_code;
3504 	a->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3505 	a->vp_idx = sp->vha->vp_idx;
3506 }
3507 
3508 int qla_edif_process_els(scsi_qla_host_t *vha, struct bsg_job *bsg_job)
3509 {
3510 	struct fc_bsg_request *bsg_request = bsg_job->request;
3511 	struct fc_bsg_reply *bsg_reply = bsg_job->reply;
3512 	fc_port_t *fcport = NULL;
3513 	struct qla_hw_data *ha = vha->hw;
3514 	srb_t *sp;
3515 	int rval =  (DID_ERROR << 16), cnt;
3516 	port_id_t d_id;
3517 	struct qla_bsg_auth_els_request *p =
3518 	    (struct qla_bsg_auth_els_request *)bsg_job->request;
3519 	struct qla_bsg_auth_els_reply *rpl =
3520 	    (struct qla_bsg_auth_els_reply *)bsg_job->reply;
3521 
3522 	rpl->version = EDIF_VERSION1;
3523 
3524 	d_id.b.al_pa = bsg_request->rqst_data.h_els.port_id[2];
3525 	d_id.b.area = bsg_request->rqst_data.h_els.port_id[1];
3526 	d_id.b.domain = bsg_request->rqst_data.h_els.port_id[0];
3527 
3528 	/* find matching d_id in fcport list */
3529 	fcport = qla2x00_find_fcport_by_pid(vha, &d_id);
3530 	if (!fcport) {
3531 		ql_dbg(ql_dbg_edif, vha, 0x911a,
3532 		    "%s fcport not find online portid=%06x.\n",
3533 		    __func__, d_id.b24);
3534 		SET_DID_STATUS(bsg_reply->result, DID_ERROR);
3535 		return -EIO;
3536 	}
3537 
3538 	if (qla_bsg_check(vha, bsg_job, fcport))
3539 		return 0;
3540 
3541 	if (EDIF_SESS_DELETE(fcport)) {
3542 		ql_dbg(ql_dbg_edif, vha, 0x910d,
3543 		    "%s ELS code %x, no loop id.\n", __func__,
3544 		    bsg_request->rqst_data.r_els.els_code);
3545 		SET_DID_STATUS(bsg_reply->result, DID_BAD_TARGET);
3546 		return -ENXIO;
3547 	}
3548 
3549 	if (!vha->flags.online) {
3550 		ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
3551 		SET_DID_STATUS(bsg_reply->result, DID_BAD_TARGET);
3552 		rval = -EIO;
3553 		goto done;
3554 	}
3555 
3556 	/* pass through is supported only for ISP 4Gb or higher */
3557 	if (!IS_FWI2_CAPABLE(ha)) {
3558 		ql_dbg(ql_dbg_user, vha, 0x7001,
3559 		    "ELS passthru not supported for ISP23xx based adapters.\n");
3560 		SET_DID_STATUS(bsg_reply->result, DID_BAD_TARGET);
3561 		rval = -EPERM;
3562 		goto done;
3563 	}
3564 
3565 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
3566 	if (!sp) {
3567 		ql_dbg(ql_dbg_user, vha, 0x7004,
3568 		    "Failed get sp pid=%06x\n", fcport->d_id.b24);
3569 		rval = -ENOMEM;
3570 		SET_DID_STATUS(bsg_reply->result, DID_IMM_RETRY);
3571 		goto done;
3572 	}
3573 
3574 	sp->remap.req.len = bsg_job->request_payload.payload_len;
3575 	sp->remap.req.buf = dma_pool_alloc(ha->purex_dma_pool,
3576 	    GFP_KERNEL, &sp->remap.req.dma);
3577 	if (!sp->remap.req.buf) {
3578 		ql_dbg(ql_dbg_user, vha, 0x7005,
3579 		    "Failed allocate request dma len=%x\n",
3580 		    bsg_job->request_payload.payload_len);
3581 		rval = -ENOMEM;
3582 		SET_DID_STATUS(bsg_reply->result, DID_IMM_RETRY);
3583 		goto done_free_sp;
3584 	}
3585 
3586 	sp->remap.rsp.len = bsg_job->reply_payload.payload_len;
3587 	sp->remap.rsp.buf = dma_pool_alloc(ha->purex_dma_pool,
3588 	    GFP_KERNEL, &sp->remap.rsp.dma);
3589 	if (!sp->remap.rsp.buf) {
3590 		ql_dbg(ql_dbg_user, vha, 0x7006,
3591 		    "Failed allocate response dma len=%x\n",
3592 		    bsg_job->reply_payload.payload_len);
3593 		rval = -ENOMEM;
3594 		SET_DID_STATUS(bsg_reply->result, DID_IMM_RETRY);
3595 		goto done_free_remap_req;
3596 	}
3597 	sg_copy_to_buffer(bsg_job->request_payload.sg_list,
3598 	    bsg_job->request_payload.sg_cnt, sp->remap.req.buf,
3599 	    sp->remap.req.len);
3600 	sp->remap.remapped = true;
3601 
3602 	sp->type = SRB_ELS_CMD_HST_NOLOGIN;
3603 	sp->name = "SPCN_BSG_HST_NOLOGIN";
3604 	sp->u.bsg_cmd.bsg_job = bsg_job;
3605 	qla_parse_auth_els_ctl(sp);
3606 
3607 	sp->free = qla2x00_bsg_sp_free;
3608 	sp->done = qla2x00_bsg_job_done;
3609 
3610 	cnt = 0;
3611 retry:
3612 	rval = qla2x00_start_sp(sp);
3613 	switch (rval) {
3614 	case QLA_SUCCESS:
3615 		ql_dbg(ql_dbg_edif, vha, 0x700a,
3616 		       "%s %s %8phN xchg %x ctlflag %x hdl %x reqlen %xh bsg ptr %p\n",
3617 		       __func__, sc_to_str(p->e.sub_cmd), fcport->port_name,
3618 		       p->e.extra_rx_xchg_address, p->e.extra_control_flags,
3619 		       sp->handle, sp->remap.req.len, bsg_job);
3620 		break;
3621 	case EAGAIN:
3622 		msleep(EDIF_MSLEEP_INTERVAL);
3623 		cnt++;
3624 		if (cnt < EDIF_RETRY_COUNT)
3625 			goto retry;
3626 		fallthrough;
3627 	default:
3628 		ql_log(ql_log_warn, vha, 0x700e,
3629 		    "%s qla2x00_start_sp failed = %d\n", __func__, rval);
3630 		SET_DID_STATUS(bsg_reply->result, DID_IMM_RETRY);
3631 		rval = -EIO;
3632 		goto done_free_remap_rsp;
3633 	}
3634 	return rval;
3635 
3636 done_free_remap_rsp:
3637 	dma_pool_free(ha->purex_dma_pool, sp->remap.rsp.buf,
3638 	    sp->remap.rsp.dma);
3639 done_free_remap_req:
3640 	dma_pool_free(ha->purex_dma_pool, sp->remap.req.buf,
3641 	    sp->remap.req.dma);
3642 done_free_sp:
3643 	qla2x00_rel_sp(sp);
3644 
3645 done:
3646 	return rval;
3647 }
3648 
3649 void qla_edif_sess_down(struct scsi_qla_host *vha, struct fc_port *sess)
3650 {
3651 	u16 cnt = 0;
3652 
3653 	if (sess->edif.app_sess_online && DBELL_ACTIVE(vha)) {
3654 		ql_dbg(ql_dbg_disc, vha, 0xf09c,
3655 			"%s: sess %8phN send port_offline event\n",
3656 			__func__, sess->port_name);
3657 		sess->edif.app_sess_online = 0;
3658 		sess->edif.sess_down_acked = 0;
3659 		qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_SESSION_SHUTDOWN,
3660 		    sess->d_id.b24, 0, sess);
3661 		qla2x00_post_aen_work(vha, FCH_EVT_PORT_OFFLINE, sess->d_id.b24);
3662 
3663 		while (!READ_ONCE(sess->edif.sess_down_acked) &&
3664 		       !test_bit(VPORT_DELETE, &vha->dpc_flags)) {
3665 			msleep(100);
3666 			cnt++;
3667 			if (cnt > 100)
3668 				break;
3669 		}
3670 		sess->edif.sess_down_acked = 0;
3671 		ql_dbg(ql_dbg_disc, vha, 0xf09c,
3672 		       "%s: sess %8phN port_offline event completed\n",
3673 		       __func__, sess->port_name);
3674 	}
3675 }
3676 
3677 void qla_edif_clear_appdata(struct scsi_qla_host *vha, struct fc_port *fcport)
3678 {
3679 	if (!(fcport->flags & FCF_FCSP_DEVICE))
3680 		return;
3681 
3682 	qla_edb_clear(vha, fcport->d_id);
3683 	qla_enode_clear(vha, fcport->d_id);
3684 }
3685