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