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