1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
4  *
5  *  based on qla2x00t.c code:
6  *
7  *  Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
8  *  Copyright (C) 2004 - 2005 Leonid Stoljar
9  *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
10  *  Copyright (C) 2006 - 2010 ID7 Ltd.
11  *
12  *  Forward port and refactoring to modern qla2xxx and target/configfs
13  *
14  *  Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
15  */
16 
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/blkdev.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/list.h>
25 #include <linux/workqueue.h>
26 #include <asm/unaligned.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tcq.h>
30 
31 #include "qla_def.h"
32 #include "qla_target.h"
33 
34 static int ql2xtgt_tape_enable;
35 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(ql2xtgt_tape_enable,
37 		"Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
38 
39 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
40 module_param(qlini_mode, charp, S_IRUGO);
41 MODULE_PARM_DESC(qlini_mode,
42 	"Determines when initiator mode will be enabled. Possible values: "
43 	"\"exclusive\" - initiator mode will be enabled on load, "
44 	"disabled on enabling target mode and then on disabling target mode "
45 	"enabled back; "
46 	"\"disabled\" - initiator mode will never be enabled; "
47 	"\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
48 	"when ready "
49 	"\"enabled\" (default) - initiator mode will always stay enabled.");
50 
51 int ql2xuctrlirq = 1;
52 module_param(ql2xuctrlirq, int, 0644);
53 MODULE_PARM_DESC(ql2xuctrlirq,
54     "User to control IRQ placement via smp_affinity."
55     "Valid with qlini_mode=disabled."
56     "1(default): enable");
57 
58 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
59 
60 static int qla_sam_status = SAM_STAT_BUSY;
61 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
62 
63 /*
64  * From scsi/fc/fc_fcp.h
65  */
66 enum fcp_resp_rsp_codes {
67 	FCP_TMF_CMPL = 0,
68 	FCP_DATA_LEN_INVALID = 1,
69 	FCP_CMND_FIELDS_INVALID = 2,
70 	FCP_DATA_PARAM_MISMATCH = 3,
71 	FCP_TMF_REJECTED = 4,
72 	FCP_TMF_FAILED = 5,
73 	FCP_TMF_INVALID_LUN = 9,
74 };
75 
76 /*
77  * fc_pri_ta from scsi/fc/fc_fcp.h
78  */
79 #define FCP_PTA_SIMPLE      0   /* simple task attribute */
80 #define FCP_PTA_HEADQ       1   /* head of queue task attribute */
81 #define FCP_PTA_ORDERED     2   /* ordered task attribute */
82 #define FCP_PTA_ACA         4   /* auto. contingent allegiance */
83 #define FCP_PTA_MASK        7   /* mask for task attribute field */
84 #define FCP_PRI_SHIFT       3   /* priority field starts in bit 3 */
85 #define FCP_PRI_RESVD_MASK  0x80        /* reserved bits in priority field */
86 
87 /*
88  * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
89  * must be called under HW lock and could unlock/lock it inside.
90  * It isn't an issue, since in the current implementation on the time when
91  * those functions are called:
92  *
93  *   - Either context is IRQ and only IRQ handler can modify HW data,
94  *     including rings related fields,
95  *
96  *   - Or access to target mode variables from struct qla_tgt doesn't
97  *     cross those functions boundaries, except tgt_stop, which
98  *     additionally protected by irq_cmd_count.
99  */
100 /* Predefs for callbacks handed to qla2xxx LLD */
101 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
102 	struct atio_from_isp *pkt, uint8_t);
103 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
104 	response_t *pkt);
105 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
106 	int fn, void *iocb, int flags);
107 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
108 	*cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
109 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
110 	struct atio_from_isp *atio, uint16_t status, int qfull);
111 static void qlt_disable_vha(struct scsi_qla_host *vha);
112 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
113 static void qlt_send_notify_ack(struct qla_qpair *qpair,
114 	struct imm_ntfy_from_isp *ntfy,
115 	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
116 	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
117 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
118 	struct imm_ntfy_from_isp *imm, int ha_locked);
119 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
120 	fc_port_t *fcport, bool local);
121 void qlt_unreg_sess(struct fc_port *sess);
122 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
123 	struct abts_recv_from_24xx *);
124 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
125     uint16_t);
126 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
127 static inline uint32_t qlt_make_handle(struct qla_qpair *);
128 
129 /*
130  * Global Variables
131  */
132 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
133 struct kmem_cache *qla_tgt_plogi_cachep;
134 static mempool_t *qla_tgt_mgmt_cmd_mempool;
135 static struct workqueue_struct *qla_tgt_wq;
136 static DEFINE_MUTEX(qla_tgt_mutex);
137 static LIST_HEAD(qla_tgt_glist);
138 
prot_op_str(u32 prot_op)139 static const char *prot_op_str(u32 prot_op)
140 {
141 	switch (prot_op) {
142 	case TARGET_PROT_NORMAL:	return "NORMAL";
143 	case TARGET_PROT_DIN_INSERT:	return "DIN_INSERT";
144 	case TARGET_PROT_DOUT_INSERT:	return "DOUT_INSERT";
145 	case TARGET_PROT_DIN_STRIP:	return "DIN_STRIP";
146 	case TARGET_PROT_DOUT_STRIP:	return "DOUT_STRIP";
147 	case TARGET_PROT_DIN_PASS:	return "DIN_PASS";
148 	case TARGET_PROT_DOUT_PASS:	return "DOUT_PASS";
149 	default:			return "UNKNOWN";
150 	}
151 }
152 
153 /* This API intentionally takes dest as a parameter, rather than returning
154  * int value to avoid caller forgetting to issue wmb() after the store */
qlt_do_generation_tick(struct scsi_qla_host * vha,int * dest)155 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
156 {
157 	scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
158 	*dest = atomic_inc_return(&base_vha->generation_tick);
159 	/* memory barrier */
160 	wmb();
161 }
162 
163 /* Might release hw lock, then reaquire!! */
qlt_issue_marker(struct scsi_qla_host * vha,int vha_locked)164 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
165 {
166 	/* Send marker if required */
167 	if (unlikely(vha->marker_needed != 0)) {
168 		int rc = qla2x00_issue_marker(vha, vha_locked);
169 
170 		if (rc != QLA_SUCCESS) {
171 			ql_dbg(ql_dbg_tgt, vha, 0xe03d,
172 			    "qla_target(%d): issue_marker() failed\n",
173 			    vha->vp_idx);
174 		}
175 		return rc;
176 	}
177 	return QLA_SUCCESS;
178 }
179 
qla_find_host_by_d_id(struct scsi_qla_host * vha,be_id_t d_id)180 struct scsi_qla_host *qla_find_host_by_d_id(struct scsi_qla_host *vha,
181 					    be_id_t d_id)
182 {
183 	struct scsi_qla_host *host;
184 	uint32_t key;
185 
186 	if (vha->d_id.b.area == d_id.area &&
187 	    vha->d_id.b.domain == d_id.domain &&
188 	    vha->d_id.b.al_pa == d_id.al_pa)
189 		return vha;
190 
191 	key = be_to_port_id(d_id).b24;
192 
193 	host = btree_lookup32(&vha->hw->host_map, key);
194 	if (!host)
195 		ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
196 		    "Unable to find host %06x\n", key);
197 
198 	return host;
199 }
200 
qlt_incr_num_pend_cmds(struct scsi_qla_host * vha)201 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
202 {
203 	unsigned long flags;
204 
205 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
206 
207 	vha->hw->tgt.num_pend_cmds++;
208 	if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
209 		vha->qla_stats.stat_max_pend_cmds =
210 			vha->hw->tgt.num_pend_cmds;
211 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
212 }
qlt_decr_num_pend_cmds(struct scsi_qla_host * vha)213 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
214 {
215 	unsigned long flags;
216 
217 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
218 	vha->hw->tgt.num_pend_cmds--;
219 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
220 }
221 
222 
qlt_queue_unknown_atio(scsi_qla_host_t * vha,struct atio_from_isp * atio,uint8_t ha_locked)223 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
224 	struct atio_from_isp *atio, uint8_t ha_locked)
225 {
226 	struct qla_tgt_sess_op *u;
227 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
228 	unsigned long flags;
229 
230 	if (tgt->tgt_stop) {
231 		ql_dbg(ql_dbg_async, vha, 0x502c,
232 		    "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
233 		    vha->vp_idx);
234 		goto out_term;
235 	}
236 
237 	u = kzalloc(sizeof(*u), GFP_ATOMIC);
238 	if (u == NULL)
239 		goto out_term;
240 
241 	u->vha = vha;
242 	memcpy(&u->atio, atio, sizeof(*atio));
243 	INIT_LIST_HEAD(&u->cmd_list);
244 
245 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
246 	list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
247 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
248 
249 	schedule_delayed_work(&vha->unknown_atio_work, 1);
250 
251 out:
252 	return;
253 
254 out_term:
255 	qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
256 	goto out;
257 }
258 
qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host * vha,uint8_t ha_locked)259 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
260 	uint8_t ha_locked)
261 {
262 	struct qla_tgt_sess_op *u, *t;
263 	scsi_qla_host_t *host;
264 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
265 	unsigned long flags;
266 	uint8_t queued = 0;
267 
268 	list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
269 		if (u->aborted) {
270 			ql_dbg(ql_dbg_async, vha, 0x502e,
271 			    "Freeing unknown %s %p, because of Abort\n",
272 			    "ATIO_TYPE7", u);
273 			qlt_send_term_exchange(vha->hw->base_qpair, NULL,
274 			    &u->atio, ha_locked, 0);
275 			goto abort;
276 		}
277 
278 		host = qla_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
279 		if (host != NULL) {
280 			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
281 			    "Requeuing unknown ATIO_TYPE7 %p\n", u);
282 			qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
283 		} else if (tgt->tgt_stop) {
284 			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
285 			    "Freeing unknown %s %p, because tgt is being stopped\n",
286 			    "ATIO_TYPE7", u);
287 			qlt_send_term_exchange(vha->hw->base_qpair, NULL,
288 			    &u->atio, ha_locked, 0);
289 		} else {
290 			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
291 			    "Reschedule u %p, vha %p, host %p\n", u, vha, host);
292 			if (!queued) {
293 				queued = 1;
294 				schedule_delayed_work(&vha->unknown_atio_work,
295 				    1);
296 			}
297 			continue;
298 		}
299 
300 abort:
301 		spin_lock_irqsave(&vha->cmd_list_lock, flags);
302 		list_del(&u->cmd_list);
303 		spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
304 		kfree(u);
305 	}
306 }
307 
qlt_unknown_atio_work_fn(struct work_struct * work)308 void qlt_unknown_atio_work_fn(struct work_struct *work)
309 {
310 	struct scsi_qla_host *vha = container_of(to_delayed_work(work),
311 	    struct scsi_qla_host, unknown_atio_work);
312 
313 	qlt_try_to_dequeue_unknown_atios(vha, 0);
314 }
315 
qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host * vha,struct atio_from_isp * atio,uint8_t ha_locked)316 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
317 	struct atio_from_isp *atio, uint8_t ha_locked)
318 {
319 	ql_dbg(ql_dbg_tgt, vha, 0xe072,
320 		"%s: qla_target(%d): type %x ox_id %04x\n",
321 		__func__, vha->vp_idx, atio->u.raw.entry_type,
322 		be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
323 
324 	switch (atio->u.raw.entry_type) {
325 	case ATIO_TYPE7:
326 	{
327 		struct scsi_qla_host *host = qla_find_host_by_d_id(vha,
328 		    atio->u.isp24.fcp_hdr.d_id);
329 		if (unlikely(NULL == host)) {
330 			ql_dbg(ql_dbg_tgt, vha, 0xe03e,
331 			    "qla_target(%d): Received ATIO_TYPE7 "
332 			    "with unknown d_id %x:%x:%x\n", vha->vp_idx,
333 			    atio->u.isp24.fcp_hdr.d_id.domain,
334 			    atio->u.isp24.fcp_hdr.d_id.area,
335 			    atio->u.isp24.fcp_hdr.d_id.al_pa);
336 
337 
338 			qlt_queue_unknown_atio(vha, atio, ha_locked);
339 			break;
340 		}
341 		if (unlikely(!list_empty(&vha->unknown_atio_list)))
342 			qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
343 
344 		qlt_24xx_atio_pkt(host, atio, ha_locked);
345 		break;
346 	}
347 
348 	case IMMED_NOTIFY_TYPE:
349 	{
350 		struct scsi_qla_host *host = vha;
351 		struct imm_ntfy_from_isp *entry =
352 		    (struct imm_ntfy_from_isp *)atio;
353 
354 		qlt_issue_marker(vha, ha_locked);
355 
356 		if ((entry->u.isp24.vp_index != 0xFF) &&
357 		    (entry->u.isp24.nport_handle != cpu_to_le16(0xFFFF))) {
358 			host = qla_find_host_by_vp_idx(vha,
359 			    entry->u.isp24.vp_index);
360 			if (unlikely(!host)) {
361 				ql_dbg(ql_dbg_tgt, vha, 0xe03f,
362 				    "qla_target(%d): Received "
363 				    "ATIO (IMMED_NOTIFY_TYPE) "
364 				    "with unknown vp_index %d\n",
365 				    vha->vp_idx, entry->u.isp24.vp_index);
366 				break;
367 			}
368 		}
369 		qlt_24xx_atio_pkt(host, atio, ha_locked);
370 		break;
371 	}
372 
373 	case VP_RPT_ID_IOCB_TYPE:
374 		qla24xx_report_id_acquisition(vha,
375 			(struct vp_rpt_id_entry_24xx *)atio);
376 		break;
377 
378 	case ABTS_RECV_24XX:
379 	{
380 		struct abts_recv_from_24xx *entry =
381 			(struct abts_recv_from_24xx *)atio;
382 		struct scsi_qla_host *host = qla_find_host_by_vp_idx(vha,
383 			entry->vp_index);
384 		unsigned long flags;
385 
386 		if (unlikely(!host)) {
387 			ql_dbg(ql_dbg_tgt, vha, 0xe00a,
388 			    "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
389 			    "received, with unknown vp_index %d\n",
390 			    vha->vp_idx, entry->vp_index);
391 			break;
392 		}
393 		if (!ha_locked)
394 			spin_lock_irqsave(&host->hw->hardware_lock, flags);
395 		qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
396 		if (!ha_locked)
397 			spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
398 		break;
399 	}
400 
401 	/* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
402 
403 	default:
404 		ql_dbg(ql_dbg_tgt, vha, 0xe040,
405 		    "qla_target(%d): Received unknown ATIO atio "
406 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
407 		break;
408 	}
409 
410 	return false;
411 }
412 
qlt_response_pkt_all_vps(struct scsi_qla_host * vha,struct rsp_que * rsp,response_t * pkt)413 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
414 	struct rsp_que *rsp, response_t *pkt)
415 {
416 	switch (pkt->entry_type) {
417 	case CTIO_CRC2:
418 		ql_dbg(ql_dbg_tgt, vha, 0xe073,
419 			"qla_target(%d):%s: CRC2 Response pkt\n",
420 			vha->vp_idx, __func__);
421 		fallthrough;
422 	case CTIO_TYPE7:
423 	{
424 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
425 		struct scsi_qla_host *host = qla_find_host_by_vp_idx(vha,
426 		    entry->vp_index);
427 		if (unlikely(!host)) {
428 			ql_dbg(ql_dbg_tgt, vha, 0xe041,
429 			    "qla_target(%d): Response pkt (CTIO_TYPE7) "
430 			    "received, with unknown vp_index %d\n",
431 			    vha->vp_idx, entry->vp_index);
432 			break;
433 		}
434 		qlt_response_pkt(host, rsp, pkt);
435 		break;
436 	}
437 
438 	case IMMED_NOTIFY_TYPE:
439 	{
440 		struct scsi_qla_host *host;
441 		struct imm_ntfy_from_isp *entry =
442 		    (struct imm_ntfy_from_isp *)pkt;
443 
444 		host = qla_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
445 		if (unlikely(!host)) {
446 			ql_dbg(ql_dbg_tgt, vha, 0xe042,
447 			    "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
448 			    "received, with unknown vp_index %d\n",
449 			    vha->vp_idx, entry->u.isp24.vp_index);
450 			break;
451 		}
452 		qlt_response_pkt(host, rsp, pkt);
453 		break;
454 	}
455 
456 	case NOTIFY_ACK_TYPE:
457 	{
458 		struct scsi_qla_host *host = vha;
459 		struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
460 
461 		if (0xFF != entry->u.isp24.vp_index) {
462 			host = qla_find_host_by_vp_idx(vha,
463 			    entry->u.isp24.vp_index);
464 			if (unlikely(!host)) {
465 				ql_dbg(ql_dbg_tgt, vha, 0xe043,
466 				    "qla_target(%d): Response "
467 				    "pkt (NOTIFY_ACK_TYPE) "
468 				    "received, with unknown "
469 				    "vp_index %d\n", vha->vp_idx,
470 				    entry->u.isp24.vp_index);
471 				break;
472 			}
473 		}
474 		qlt_response_pkt(host, rsp, pkt);
475 		break;
476 	}
477 
478 	case ABTS_RECV_24XX:
479 	{
480 		struct abts_recv_from_24xx *entry =
481 		    (struct abts_recv_from_24xx *)pkt;
482 		struct scsi_qla_host *host = qla_find_host_by_vp_idx(vha,
483 		    entry->vp_index);
484 		if (unlikely(!host)) {
485 			ql_dbg(ql_dbg_tgt, vha, 0xe044,
486 			    "qla_target(%d): Response pkt "
487 			    "(ABTS_RECV_24XX) received, with unknown "
488 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
489 			break;
490 		}
491 		qlt_response_pkt(host, rsp, pkt);
492 		break;
493 	}
494 
495 	case ABTS_RESP_24XX:
496 	{
497 		struct abts_resp_to_24xx *entry =
498 		    (struct abts_resp_to_24xx *)pkt;
499 		struct scsi_qla_host *host = qla_find_host_by_vp_idx(vha,
500 		    entry->vp_index);
501 		if (unlikely(!host)) {
502 			ql_dbg(ql_dbg_tgt, vha, 0xe045,
503 			    "qla_target(%d): Response pkt "
504 			    "(ABTS_RECV_24XX) received, with unknown "
505 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
506 			break;
507 		}
508 		qlt_response_pkt(host, rsp, pkt);
509 		break;
510 	}
511 	default:
512 		qlt_response_pkt(vha, rsp, pkt);
513 		break;
514 	}
515 
516 }
517 
518 /*
519  * All qlt_plogi_ack_t operations are protected by hardware_lock
520  */
qla24xx_post_nack_work(struct scsi_qla_host * vha,fc_port_t * fcport,struct imm_ntfy_from_isp * ntfy,int type)521 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
522 	struct imm_ntfy_from_isp *ntfy, int type)
523 {
524 	struct qla_work_evt *e;
525 
526 	e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
527 	if (!e)
528 		return QLA_FUNCTION_FAILED;
529 
530 	e->u.nack.fcport = fcport;
531 	e->u.nack.type = type;
532 	memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
533 	return qla2x00_post_work(vha, e);
534 }
535 
qla2x00_async_nack_sp_done(srb_t * sp,int res)536 static void qla2x00_async_nack_sp_done(srb_t *sp, int res)
537 {
538 	struct scsi_qla_host *vha = sp->vha;
539 	unsigned long flags;
540 
541 	ql_dbg(ql_dbg_disc, vha, 0x20f2,
542 	    "Async done-%s res %x %8phC  type %d\n",
543 	    sp->name, res, sp->fcport->port_name, sp->type);
544 
545 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
546 	sp->fcport->flags &= ~FCF_ASYNC_SENT;
547 	sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
548 
549 	switch (sp->type) {
550 	case SRB_NACK_PLOGI:
551 		sp->fcport->login_gen++;
552 		sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
553 		sp->fcport->logout_on_delete = 1;
554 		sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
555 		sp->fcport->send_els_logo = 0;
556 
557 		if (sp->fcport->flags & FCF_FCSP_DEVICE) {
558 			ql_dbg(ql_dbg_edif, vha, 0x20ef,
559 			    "%s %8phC edif: PLOGI- AUTH WAIT\n", __func__,
560 			    sp->fcport->port_name);
561 			qla2x00_set_fcport_disc_state(sp->fcport,
562 			    DSC_LOGIN_AUTH_PEND);
563 			qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
564 			    sp->fcport->d_id.b24);
565 			qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_NEEDED, sp->fcport->d_id.b24,
566 			    0, sp->fcport);
567 		}
568 		break;
569 
570 	case SRB_NACK_PRLI:
571 		sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
572 		sp->fcport->deleted = 0;
573 		sp->fcport->send_els_logo = 0;
574 
575 		if (!sp->fcport->login_succ &&
576 		    !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
577 			sp->fcport->login_succ = 1;
578 
579 			vha->fcport_count++;
580 			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
581 			qla24xx_sched_upd_fcport(sp->fcport);
582 			spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
583 		} else {
584 			sp->fcport->login_retry = 0;
585 			qla2x00_set_fcport_disc_state(sp->fcport,
586 			    DSC_LOGIN_COMPLETE);
587 			sp->fcport->deleted = 0;
588 			sp->fcport->logout_on_delete = 1;
589 		}
590 		break;
591 
592 	case SRB_NACK_LOGO:
593 		sp->fcport->login_gen++;
594 		sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
595 		qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
596 		break;
597 	}
598 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
599 
600 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
601 }
602 
qla24xx_async_notify_ack(scsi_qla_host_t * vha,fc_port_t * fcport,struct imm_ntfy_from_isp * ntfy,int type)603 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
604 	struct imm_ntfy_from_isp *ntfy, int type)
605 {
606 	int rval = QLA_FUNCTION_FAILED;
607 	srb_t *sp;
608 	char *c = NULL;
609 
610 	fcport->flags |= FCF_ASYNC_SENT;
611 	switch (type) {
612 	case SRB_NACK_PLOGI:
613 		fcport->fw_login_state = DSC_LS_PLOGI_PEND;
614 		c = "PLOGI";
615 		if (vha->hw->flags.edif_enabled &&
616 		    (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP))
617 			fcport->flags |= FCF_FCSP_DEVICE;
618 		break;
619 	case SRB_NACK_PRLI:
620 		fcport->fw_login_state = DSC_LS_PRLI_PEND;
621 		fcport->deleted = 0;
622 		c = "PRLI";
623 		break;
624 	case SRB_NACK_LOGO:
625 		fcport->fw_login_state = DSC_LS_LOGO_PEND;
626 		c = "LOGO";
627 		break;
628 	}
629 
630 	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
631 	if (!sp)
632 		goto done;
633 
634 	sp->type = type;
635 	sp->name = "nack";
636 	qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
637 			      qla2x00_async_nack_sp_done);
638 
639 	sp->u.iocb_cmd.u.nack.ntfy = ntfy;
640 
641 	ql_dbg(ql_dbg_disc, vha, 0x20f4,
642 	    "Async-%s %8phC hndl %x %s\n",
643 	    sp->name, fcport->port_name, sp->handle, c);
644 
645 	rval = qla2x00_start_sp(sp);
646 	if (rval != QLA_SUCCESS)
647 		goto done_free_sp;
648 
649 	return rval;
650 
651 done_free_sp:
652 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
653 done:
654 	fcport->flags &= ~FCF_ASYNC_SENT;
655 	return rval;
656 }
657 
qla24xx_do_nack_work(struct scsi_qla_host * vha,struct qla_work_evt * e)658 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
659 {
660 	fc_port_t *t;
661 
662 	switch (e->u.nack.type) {
663 	case SRB_NACK_PRLI:
664 		t = e->u.nack.fcport;
665 		flush_work(&t->del_work);
666 		flush_work(&t->free_work);
667 		mutex_lock(&vha->vha_tgt.tgt_mutex);
668 		t = qlt_create_sess(vha, e->u.nack.fcport, 0);
669 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
670 		if (t) {
671 			ql_log(ql_log_info, vha, 0xd034,
672 			    "%s create sess success %p", __func__, t);
673 			/* create sess has an extra kref */
674 			vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
675 		}
676 		break;
677 	}
678 	qla24xx_async_notify_ack(vha, e->u.nack.fcport,
679 	    (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
680 }
681 
qla24xx_delete_sess_fn(struct work_struct * work)682 void qla24xx_delete_sess_fn(struct work_struct *work)
683 {
684 	fc_port_t *fcport = container_of(work, struct fc_port, del_work);
685 	struct qla_hw_data *ha = NULL;
686 
687 	if (!fcport || !fcport->vha || !fcport->vha->hw)
688 		return;
689 
690 	ha = fcport->vha->hw;
691 
692 	if (fcport->se_sess) {
693 		ha->tgt.tgt_ops->shutdown_sess(fcport);
694 		ha->tgt.tgt_ops->put_sess(fcport);
695 	} else {
696 		qlt_unreg_sess(fcport);
697 	}
698 }
699 
700 /*
701  * Called from qla2x00_reg_remote_port()
702  */
qlt_fc_port_added(struct scsi_qla_host * vha,fc_port_t * fcport)703 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
704 {
705 	struct qla_hw_data *ha = vha->hw;
706 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
707 	struct fc_port *sess = fcport;
708 	unsigned long flags;
709 
710 	if (!vha->hw->tgt.tgt_ops)
711 		return;
712 
713 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
714 	if (tgt->tgt_stop) {
715 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
716 		return;
717 	}
718 
719 	if (fcport->disc_state == DSC_DELETE_PEND) {
720 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
721 		return;
722 	}
723 
724 	if (!sess->se_sess) {
725 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
726 
727 		mutex_lock(&vha->vha_tgt.tgt_mutex);
728 		sess = qlt_create_sess(vha, fcport, false);
729 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
730 
731 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
732 	} else {
733 		if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
734 			spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
735 			return;
736 		}
737 
738 		if (!kref_get_unless_zero(&sess->sess_kref)) {
739 			ql_dbg(ql_dbg_disc, vha, 0x2107,
740 			    "%s: kref_get fail sess %8phC \n",
741 			    __func__, sess->port_name);
742 			spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
743 			return;
744 		}
745 
746 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
747 		    "qla_target(%u): %ssession for port %8phC "
748 		    "(loop ID %d) reappeared\n", vha->vp_idx,
749 		    sess->local ? "local " : "", sess->port_name, sess->loop_id);
750 
751 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
752 		    "Reappeared sess %p\n", sess);
753 
754 		ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
755 		    fcport->loop_id,
756 		    (fcport->flags & FCF_CONF_COMP_SUPPORTED));
757 	}
758 
759 	if (sess && sess->local) {
760 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
761 		    "qla_target(%u): local session for "
762 		    "port %8phC (loop ID %d) became global\n", vha->vp_idx,
763 		    fcport->port_name, sess->loop_id);
764 		sess->local = 0;
765 	}
766 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
767 
768 	ha->tgt.tgt_ops->put_sess(sess);
769 }
770 
771 /*
772  * This is a zero-base ref-counting solution, since hardware_lock
773  * guarantees that ref_count is not modified concurrently.
774  * Upon successful return content of iocb is undefined
775  */
776 static struct qlt_plogi_ack_t *
qlt_plogi_ack_find_add(struct scsi_qla_host * vha,port_id_t * id,struct imm_ntfy_from_isp * iocb)777 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
778 		       struct imm_ntfy_from_isp *iocb)
779 {
780 	struct qlt_plogi_ack_t *pla;
781 
782 	lockdep_assert_held(&vha->hw->hardware_lock);
783 
784 	list_for_each_entry(pla, &vha->plogi_ack_list, list) {
785 		if (pla->id.b24 == id->b24) {
786 			ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
787 			    "%s %d %8phC Term INOT due to new INOT",
788 			    __func__, __LINE__,
789 			    pla->iocb.u.isp24.port_name);
790 			qlt_send_term_imm_notif(vha, &pla->iocb, 1);
791 			memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
792 			return pla;
793 		}
794 	}
795 
796 	pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
797 	if (!pla) {
798 		ql_dbg(ql_dbg_async, vha, 0x5088,
799 		       "qla_target(%d): Allocation of plogi_ack failed\n",
800 		       vha->vp_idx);
801 		return NULL;
802 	}
803 
804 	memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
805 	pla->id = *id;
806 	list_add_tail(&pla->list, &vha->plogi_ack_list);
807 
808 	return pla;
809 }
810 
qlt_plogi_ack_unref(struct scsi_qla_host * vha,struct qlt_plogi_ack_t * pla)811 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
812     struct qlt_plogi_ack_t *pla)
813 {
814 	struct imm_ntfy_from_isp *iocb = &pla->iocb;
815 	port_id_t port_id;
816 	uint16_t loop_id;
817 	fc_port_t *fcport = pla->fcport;
818 
819 	BUG_ON(!pla->ref_count);
820 	pla->ref_count--;
821 
822 	if (pla->ref_count)
823 		return;
824 
825 	ql_dbg(ql_dbg_disc, vha, 0x5089,
826 	    "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
827 	    " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
828 	    iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
829 	    iocb->u.isp24.port_id[0],
830 	    le16_to_cpu(iocb->u.isp24.nport_handle),
831 	    iocb->u.isp24.exchange_address, iocb->ox_id);
832 
833 	port_id.b.domain = iocb->u.isp24.port_id[2];
834 	port_id.b.area   = iocb->u.isp24.port_id[1];
835 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
836 	port_id.b.rsvd_1 = 0;
837 
838 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
839 
840 	fcport->loop_id = loop_id;
841 	fcport->d_id = port_id;
842 	if (iocb->u.isp24.status_subcode == ELS_PLOGI)
843 		qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
844 	else
845 		qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
846 
847 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
848 		if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
849 			fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
850 		if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
851 			fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
852 	}
853 
854 	list_del(&pla->list);
855 	kmem_cache_free(qla_tgt_plogi_cachep, pla);
856 }
857 
858 void
qlt_plogi_ack_link(struct scsi_qla_host * vha,struct qlt_plogi_ack_t * pla,struct fc_port * sess,enum qlt_plogi_link_t link)859 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
860     struct fc_port *sess, enum qlt_plogi_link_t link)
861 {
862 	struct imm_ntfy_from_isp *iocb = &pla->iocb;
863 	/* Inc ref_count first because link might already be pointing at pla */
864 	pla->ref_count++;
865 
866 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
867 		"Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
868 		" s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
869 		sess, link, sess->port_name,
870 		iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
871 		iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
872 		pla->ref_count, pla, link);
873 
874 	if (link == QLT_PLOGI_LINK_CONFLICT) {
875 		switch (sess->disc_state) {
876 		case DSC_DELETED:
877 		case DSC_DELETE_PEND:
878 			pla->ref_count--;
879 			return;
880 		default:
881 			break;
882 		}
883 	}
884 
885 	if (sess->plogi_link[link])
886 		qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
887 
888 	if (link == QLT_PLOGI_LINK_SAME_WWN)
889 		pla->fcport = sess;
890 
891 	sess->plogi_link[link] = pla;
892 }
893 
894 typedef struct {
895 	/* These fields must be initialized by the caller */
896 	port_id_t id;
897 	/*
898 	 * number of cmds dropped while we were waiting for
899 	 * initiator to ack LOGO initialize to 1 if LOGO is
900 	 * triggered by a command, otherwise, to 0
901 	 */
902 	int cmd_count;
903 
904 	/* These fields are used by callee */
905 	struct list_head list;
906 } qlt_port_logo_t;
907 
908 static void
qlt_send_first_logo(struct scsi_qla_host * vha,qlt_port_logo_t * logo)909 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
910 {
911 	qlt_port_logo_t *tmp;
912 	int res;
913 
914 	if (test_bit(PFLG_DRIVER_REMOVING, &vha->pci_flags)) {
915 		res = 0;
916 		goto out;
917 	}
918 
919 	mutex_lock(&vha->vha_tgt.tgt_mutex);
920 
921 	list_for_each_entry(tmp, &vha->logo_list, list) {
922 		if (tmp->id.b24 == logo->id.b24) {
923 			tmp->cmd_count += logo->cmd_count;
924 			mutex_unlock(&vha->vha_tgt.tgt_mutex);
925 			return;
926 		}
927 	}
928 
929 	list_add_tail(&logo->list, &vha->logo_list);
930 
931 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
932 
933 	res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
934 
935 	mutex_lock(&vha->vha_tgt.tgt_mutex);
936 	list_del(&logo->list);
937 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
938 
939 out:
940 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
941 	    "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
942 	    logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
943 	    logo->cmd_count, res);
944 }
945 
qlt_free_session_done(struct work_struct * work)946 void qlt_free_session_done(struct work_struct *work)
947 {
948 	struct fc_port *sess = container_of(work, struct fc_port,
949 	    free_work);
950 	struct qla_tgt *tgt = sess->tgt;
951 	struct scsi_qla_host *vha = sess->vha;
952 	struct qla_hw_data *ha = vha->hw;
953 	unsigned long flags;
954 	bool logout_started = false;
955 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
956 	struct qlt_plogi_ack_t *own =
957 		sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
958 
959 	ql_dbg(ql_dbg_disc, vha, 0xf084,
960 		"%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
961 		" s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
962 		__func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
963 		sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
964 		sess->logout_on_delete, sess->keep_nport_handle,
965 		sess->send_els_logo);
966 
967 	if (!IS_SW_RESV_ADDR(sess->d_id)) {
968 		qla2x00_mark_device_lost(vha, sess, 0);
969 
970 		if (sess->send_els_logo) {
971 			qlt_port_logo_t logo;
972 
973 			logo.id = sess->d_id;
974 			logo.cmd_count = 0;
975 			INIT_LIST_HEAD(&logo.list);
976 			if (!own)
977 				qlt_send_first_logo(vha, &logo);
978 			sess->send_els_logo = 0;
979 		}
980 
981 		if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
982 			int rc;
983 
984 			if (!own ||
985 			     (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
986 				sess->logout_completed = 0;
987 				rc = qla2x00_post_async_logout_work(vha, sess,
988 				    NULL);
989 				if (rc != QLA_SUCCESS)
990 					ql_log(ql_log_warn, vha, 0xf085,
991 					    "Schedule logo failed sess %p rc %d\n",
992 					    sess, rc);
993 				else
994 					logout_started = true;
995 			} else if (own && (own->iocb.u.isp24.status_subcode ==
996 				ELS_PRLI) && ha->flags.rida_fmt2) {
997 				rc = qla2x00_post_async_prlo_work(vha, sess,
998 				    NULL);
999 				if (rc != QLA_SUCCESS)
1000 					ql_log(ql_log_warn, vha, 0xf085,
1001 					    "Schedule PRLO failed sess %p rc %d\n",
1002 					    sess, rc);
1003 				else
1004 					logout_started = true;
1005 			}
1006 		} /* if sess->logout_on_delete */
1007 
1008 		if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1009 		    !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1010 			sess->nvme_flag |= NVME_FLAG_DELETING;
1011 			qla_nvme_unregister_remote_port(sess);
1012 		}
1013 
1014 		if (ha->flags.edif_enabled &&
1015 				(!own || own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
1016 			sess->edif.authok = 0;
1017 			if (!ha->flags.host_shutting_down) {
1018 				ql_dbg(ql_dbg_edif, vha, 0x911e,
1019 				       "%s wwpn %8phC calling qla2x00_release_all_sadb\n",
1020 				       __func__, sess->port_name);
1021 				qla2x00_release_all_sadb(vha, sess);
1022 			} else {
1023 				ql_dbg(ql_dbg_edif, vha, 0x911e,
1024 				       "%s bypassing release_all_sadb\n",
1025 				       __func__);
1026 			}
1027 
1028 			qla_edif_clear_appdata(vha, sess);
1029 			qla_edif_sess_down(vha, sess);
1030 		}
1031 	}
1032 
1033 	/*
1034 	 * Release the target session for FC Nexus from fabric module code.
1035 	 */
1036 	if (sess->se_sess != NULL)
1037 		ha->tgt.tgt_ops->free_session(sess);
1038 
1039 	if (logout_started) {
1040 		bool traced = false;
1041 		u16 cnt = 0;
1042 
1043 		while (!READ_ONCE(sess->logout_completed)) {
1044 			if (!traced) {
1045 				ql_dbg(ql_dbg_disc, vha, 0xf086,
1046 					"%s: waiting for sess %p logout\n",
1047 					__func__, sess);
1048 				traced = true;
1049 			}
1050 			msleep(100);
1051 			cnt++;
1052 			/*
1053 			 * Driver timeout is set to 22 Sec, update count value to loop
1054 			 * long enough for log-out to complete before advancing. Otherwise,
1055 			 * straddling logout can interfere with re-login attempt.
1056 			 */
1057 			if (cnt > 230)
1058 				break;
1059 		}
1060 
1061 		ql_dbg(ql_dbg_disc, vha, 0xf087,
1062 		    "%s: sess %p logout completed\n", __func__, sess);
1063 	}
1064 
1065 	/* check for any straggling io left behind */
1066 	if (!(sess->flags & FCF_FCP2_DEVICE) &&
1067 	    qla2x00_eh_wait_for_pending_commands(sess->vha, sess->d_id.b24, 0, WAIT_TARGET)) {
1068 		ql_log(ql_log_warn, vha, 0x3027,
1069 		    "IO not return. Resetting.\n");
1070 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1071 		qla2xxx_wake_dpc(vha);
1072 		qla2x00_wait_for_chip_reset(vha);
1073 	}
1074 
1075 	if (sess->logo_ack_needed) {
1076 		sess->logo_ack_needed = 0;
1077 		qla24xx_async_notify_ack(vha, sess,
1078 			(struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1079 	}
1080 
1081 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1082 	if (sess->se_sess) {
1083 		sess->se_sess = NULL;
1084 		if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1085 			tgt->sess_count--;
1086 	}
1087 
1088 	qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1089 	sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1090 
1091 	if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1092 		vha->fcport_count--;
1093 		sess->login_succ = 0;
1094 	}
1095 
1096 	qla2x00_clear_loop_id(sess);
1097 
1098 	if (sess->conflict) {
1099 		sess->conflict->login_pause = 0;
1100 		sess->conflict = NULL;
1101 		if (!test_bit(UNLOADING, &vha->dpc_flags))
1102 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1103 	}
1104 
1105 	{
1106 		struct qlt_plogi_ack_t *con =
1107 		    sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1108 		struct imm_ntfy_from_isp *iocb;
1109 
1110 		own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1111 
1112 		if (con) {
1113 			iocb = &con->iocb;
1114 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1115 				 "se_sess %p / sess %p port %8phC is gone,"
1116 				 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1117 				 sess->se_sess, sess, sess->port_name,
1118 				 own ? "releasing own PLOGI" : "no own PLOGI pending",
1119 				 own ? own->ref_count : -1,
1120 				 iocb->u.isp24.port_name, con->ref_count);
1121 			qlt_plogi_ack_unref(vha, con);
1122 			sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1123 		} else {
1124 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1125 			    "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1126 			    sess->se_sess, sess, sess->port_name,
1127 			    own ? "releasing own PLOGI" :
1128 			    "no own PLOGI pending",
1129 			    own ? own->ref_count : -1);
1130 		}
1131 
1132 		if (own) {
1133 			sess->fw_login_state = DSC_LS_PLOGI_PEND;
1134 			qlt_plogi_ack_unref(vha, own);
1135 			sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1136 		}
1137 	}
1138 
1139 	sess->explicit_logout = 0;
1140 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1141 
1142 	qla2x00_dfs_remove_rport(vha, sess);
1143 
1144 	spin_lock_irqsave(&vha->work_lock, flags);
1145 	sess->flags &= ~FCF_ASYNC_SENT;
1146 	sess->deleted = QLA_SESS_DELETED;
1147 	sess->free_pending = 0;
1148 	spin_unlock_irqrestore(&vha->work_lock, flags);
1149 
1150 	ql_dbg(ql_dbg_disc, vha, 0xf001,
1151 	    "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1152 		sess, sess->port_name, vha->fcport_count);
1153 
1154 	if (tgt && (tgt->sess_count == 0))
1155 		wake_up_all(&tgt->waitQ);
1156 
1157 	if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1158 	    !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1159 	    (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1160 		switch (vha->host->active_mode) {
1161 		case MODE_INITIATOR:
1162 		case MODE_DUAL:
1163 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1164 			qla2xxx_wake_dpc(vha);
1165 			break;
1166 		case MODE_TARGET:
1167 		default:
1168 			/* no-op */
1169 			break;
1170 		}
1171 	}
1172 
1173 	if (vha->fcport_count == 0)
1174 		wake_up_all(&vha->fcport_waitQ);
1175 }
1176 
1177 /* ha->tgt.sess_lock supposed to be held on entry */
qlt_unreg_sess(struct fc_port * sess)1178 void qlt_unreg_sess(struct fc_port *sess)
1179 {
1180 	struct scsi_qla_host *vha = sess->vha;
1181 	unsigned long flags;
1182 
1183 	ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1184 	    "%s sess %p for deletion %8phC\n",
1185 	    __func__, sess, sess->port_name);
1186 
1187 	spin_lock_irqsave(&sess->vha->work_lock, flags);
1188 	if (sess->free_pending) {
1189 		spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1190 		return;
1191 	}
1192 	sess->free_pending = 1;
1193 	/*
1194 	 * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1195 	 * management from being sent.
1196 	 */
1197 	sess->flags |= FCF_ASYNC_SENT;
1198 	sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1199 	spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1200 
1201 	if (sess->se_sess)
1202 		vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1203 
1204 	qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1205 	sess->last_rscn_gen = sess->rscn_gen;
1206 	sess->last_login_gen = sess->login_gen;
1207 
1208 	queue_work(sess->vha->hw->wq, &sess->free_work);
1209 }
1210 EXPORT_SYMBOL(qlt_unreg_sess);
1211 
qlt_reset(struct scsi_qla_host * vha,void * iocb,int mcmd)1212 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1213 {
1214 	struct qla_hw_data *ha = vha->hw;
1215 	struct fc_port *sess = NULL;
1216 	uint16_t loop_id;
1217 	int res = 0;
1218 	struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1219 	unsigned long flags;
1220 
1221 	loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1222 	if (loop_id == 0xFFFF) {
1223 		/* Global event */
1224 		atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1225 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1226 		qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1227 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1228 	} else {
1229 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1230 		sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1231 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1232 	}
1233 
1234 	ql_dbg(ql_dbg_tgt, vha, 0xe000,
1235 	    "Using sess for qla_tgt_reset: %p\n", sess);
1236 	if (!sess) {
1237 		res = -ESRCH;
1238 		return res;
1239 	}
1240 
1241 	ql_dbg(ql_dbg_tgt, vha, 0xe047,
1242 	    "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1243 	    "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1244 	    mcmd, loop_id);
1245 
1246 	return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1247 }
1248 
qla24xx_chk_fcp_state(struct fc_port * sess)1249 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1250 {
1251 	if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1252 		sess->logout_on_delete = 0;
1253 		sess->logo_ack_needed = 0;
1254 		sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1255 	}
1256 }
1257 
qlt_schedule_sess_for_deletion(struct fc_port * sess)1258 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1259 {
1260 	struct qla_tgt *tgt = sess->tgt;
1261 	unsigned long flags;
1262 	u16 sec;
1263 
1264 	switch (sess->disc_state) {
1265 	case DSC_DELETE_PEND:
1266 		return;
1267 	case DSC_DELETED:
1268 		if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1269 			!sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1270 			if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1271 				wake_up_all(&tgt->waitQ);
1272 
1273 			if (sess->vha->fcport_count == 0)
1274 				wake_up_all(&sess->vha->fcport_waitQ);
1275 			return;
1276 		}
1277 		break;
1278 	case DSC_UPD_FCPORT:
1279 		/*
1280 		 * This port is not done reporting to upper layer.
1281 		 * let it finish
1282 		 */
1283 		sess->next_disc_state = DSC_DELETE_PEND;
1284 		sec = jiffies_to_msecs(jiffies -
1285 		    sess->jiffies_at_registration)/1000;
1286 		if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1287 			sess->sec_since_registration = sec;
1288 			ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1289 			    "%s %8phC : Slow Rport registration(%d Sec)\n",
1290 			    __func__, sess->port_name, sec);
1291 		}
1292 		return;
1293 	default:
1294 		break;
1295 	}
1296 
1297 	spin_lock_irqsave(&sess->vha->work_lock, flags);
1298 	if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1299 		spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1300 		return;
1301 	}
1302 	sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1303 	spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1304 
1305 	sess->prli_pend_timer = 0;
1306 	qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1307 
1308 	qla24xx_chk_fcp_state(sess);
1309 
1310 	ql_dbg(ql_log_warn, sess->vha, 0xe001,
1311 	    "Scheduling sess %p for deletion %8phC fc4_type %x\n",
1312 	    sess, sess->port_name, sess->fc4_type);
1313 
1314 	WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1315 }
1316 
qlt_clear_tgt_db(struct qla_tgt * tgt)1317 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1318 {
1319 	struct fc_port *sess;
1320 	scsi_qla_host_t *vha = tgt->vha;
1321 
1322 	list_for_each_entry(sess, &vha->vp_fcports, list) {
1323 		if (sess->se_sess)
1324 			qlt_schedule_sess_for_deletion(sess);
1325 	}
1326 
1327 	/* At this point tgt could be already dead */
1328 }
1329 
qla24xx_get_loop_id(struct scsi_qla_host * vha,be_id_t s_id,uint16_t * loop_id)1330 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1331 	uint16_t *loop_id)
1332 {
1333 	struct qla_hw_data *ha = vha->hw;
1334 	dma_addr_t gid_list_dma;
1335 	struct gid_list_info *gid_list, *gid;
1336 	int res, rc, i;
1337 	uint16_t entries;
1338 
1339 	gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1340 	    &gid_list_dma, GFP_KERNEL);
1341 	if (!gid_list) {
1342 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1343 		    "qla_target(%d): DMA Alloc failed of %u\n",
1344 		    vha->vp_idx, qla2x00_gid_list_size(ha));
1345 		return -ENOMEM;
1346 	}
1347 
1348 	/* Get list of logged in devices */
1349 	rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1350 	if (rc != QLA_SUCCESS) {
1351 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1352 		    "qla_target(%d): get_id_list() failed: %x\n",
1353 		    vha->vp_idx, rc);
1354 		res = -EBUSY;
1355 		goto out_free_id_list;
1356 	}
1357 
1358 	gid = gid_list;
1359 	res = -ENOENT;
1360 	for (i = 0; i < entries; i++) {
1361 		if (gid->al_pa == s_id.al_pa &&
1362 		    gid->area == s_id.area &&
1363 		    gid->domain == s_id.domain) {
1364 			*loop_id = le16_to_cpu(gid->loop_id);
1365 			res = 0;
1366 			break;
1367 		}
1368 		gid = (void *)gid + ha->gid_list_info_size;
1369 	}
1370 
1371 out_free_id_list:
1372 	dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1373 	    gid_list, gid_list_dma);
1374 	return res;
1375 }
1376 
1377 /*
1378  * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1379  * Caller must put it.
1380  */
qlt_create_sess(struct scsi_qla_host * vha,fc_port_t * fcport,bool local)1381 static struct fc_port *qlt_create_sess(
1382 	struct scsi_qla_host *vha,
1383 	fc_port_t *fcport,
1384 	bool local)
1385 {
1386 	struct qla_hw_data *ha = vha->hw;
1387 	struct fc_port *sess = fcport;
1388 	unsigned long flags;
1389 
1390 	if (vha->vha_tgt.qla_tgt->tgt_stop)
1391 		return NULL;
1392 
1393 	if (fcport->se_sess) {
1394 		if (!kref_get_unless_zero(&sess->sess_kref)) {
1395 			ql_dbg(ql_dbg_disc, vha, 0x20f6,
1396 			    "%s: kref_get_unless_zero failed for %8phC\n",
1397 			    __func__, sess->port_name);
1398 			return NULL;
1399 		}
1400 		return fcport;
1401 	}
1402 	sess->tgt = vha->vha_tgt.qla_tgt;
1403 	sess->local = local;
1404 
1405 	/*
1406 	 * Under normal circumstances we want to logout from firmware when
1407 	 * session eventually ends and release corresponding nport handle.
1408 	 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1409 	 * code will adjust these flags as necessary.
1410 	 */
1411 	sess->logout_on_delete = 1;
1412 	sess->keep_nport_handle = 0;
1413 	sess->logout_completed = 0;
1414 
1415 	if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1416 	    &fcport->port_name[0], sess) < 0) {
1417 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1418 		    "(%d) %8phC check_initiator_node_acl failed\n",
1419 		    vha->vp_idx, fcport->port_name);
1420 		return NULL;
1421 	} else {
1422 		kref_init(&fcport->sess_kref);
1423 		/*
1424 		 * Take an extra reference to ->sess_kref here to handle
1425 		 * fc_port access across ->tgt.sess_lock reaquire.
1426 		 */
1427 		if (!kref_get_unless_zero(&sess->sess_kref)) {
1428 			ql_dbg(ql_dbg_disc, vha, 0x20f7,
1429 			    "%s: kref_get_unless_zero failed for %8phC\n",
1430 			    __func__, sess->port_name);
1431 			return NULL;
1432 		}
1433 
1434 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1435 		if (!IS_SW_RESV_ADDR(sess->d_id))
1436 			vha->vha_tgt.qla_tgt->sess_count++;
1437 
1438 		qlt_do_generation_tick(vha, &sess->generation);
1439 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1440 	}
1441 
1442 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1443 	    "Adding sess %p se_sess %p  to tgt %p sess_count %d\n",
1444 	    sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1445 	    vha->vha_tgt.qla_tgt->sess_count);
1446 
1447 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1448 	    "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1449 	    "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1450 	    vha->vp_idx, local ?  "local " : "", fcport->port_name,
1451 	    fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1452 	    sess->d_id.b.al_pa, sess->conf_compl_supported ?  "" : "not ");
1453 
1454 	return sess;
1455 }
1456 
1457 /*
1458  * max_gen - specifies maximum session generation
1459  * at which this deletion requestion is still valid
1460  */
1461 void
qlt_fc_port_deleted(struct scsi_qla_host * vha,fc_port_t * fcport,int max_gen)1462 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1463 {
1464 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1465 	struct fc_port *sess = fcport;
1466 	unsigned long flags;
1467 
1468 	if (!vha->hw->tgt.tgt_ops)
1469 		return;
1470 
1471 	if (!tgt)
1472 		return;
1473 
1474 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1475 	if (tgt->tgt_stop) {
1476 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1477 		return;
1478 	}
1479 	if (!sess->se_sess) {
1480 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1481 		return;
1482 	}
1483 
1484 	if (max_gen - sess->generation < 0) {
1485 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1486 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1487 		    "Ignoring stale deletion request for se_sess %p / sess %p"
1488 		    " for port %8phC, req_gen %d, sess_gen %d\n",
1489 		    sess->se_sess, sess, sess->port_name, max_gen,
1490 		    sess->generation);
1491 		return;
1492 	}
1493 
1494 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1495 
1496 	sess->local = 1;
1497 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1498 	qlt_schedule_sess_for_deletion(sess);
1499 }
1500 
test_tgt_sess_count(struct qla_tgt * tgt)1501 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1502 {
1503 	struct qla_hw_data *ha = tgt->ha;
1504 	unsigned long flags;
1505 	int res;
1506 	/*
1507 	 * We need to protect against race, when tgt is freed before or
1508 	 * inside wake_up()
1509 	 */
1510 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1511 	ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1512 	    "tgt %p, sess_count=%d\n",
1513 	    tgt, tgt->sess_count);
1514 	res = (tgt->sess_count == 0);
1515 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1516 
1517 	return res;
1518 }
1519 
1520 /* Called by tcm_qla2xxx configfs code */
qlt_stop_phase1(struct qla_tgt * tgt)1521 int qlt_stop_phase1(struct qla_tgt *tgt)
1522 {
1523 	struct scsi_qla_host *vha = tgt->vha;
1524 	struct qla_hw_data *ha = tgt->ha;
1525 	unsigned long flags;
1526 
1527 	mutex_lock(&ha->optrom_mutex);
1528 	mutex_lock(&qla_tgt_mutex);
1529 
1530 	if (tgt->tgt_stop || tgt->tgt_stopped) {
1531 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1532 		    "Already in tgt->tgt_stop or tgt_stopped state\n");
1533 		mutex_unlock(&qla_tgt_mutex);
1534 		mutex_unlock(&ha->optrom_mutex);
1535 		return -EPERM;
1536 	}
1537 
1538 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1539 	    vha->host_no, vha);
1540 	/*
1541 	 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1542 	 * Lock is needed, because we still can get an incoming packet.
1543 	 */
1544 	mutex_lock(&vha->vha_tgt.tgt_mutex);
1545 	tgt->tgt_stop = 1;
1546 	qlt_clear_tgt_db(tgt);
1547 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
1548 	mutex_unlock(&qla_tgt_mutex);
1549 
1550 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1551 	    "Waiting for sess works (tgt %p)", tgt);
1552 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
1553 	do {
1554 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1555 		flush_work(&tgt->sess_work);
1556 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
1557 	} while (!list_empty(&tgt->sess_works_list));
1558 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1559 
1560 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1561 	    "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1562 
1563 	wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1564 
1565 	/* Big hammer */
1566 	if (!ha->flags.host_shutting_down &&
1567 	    (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1568 		qlt_disable_vha(vha);
1569 
1570 	/* Wait for sessions to clear out (just in case) */
1571 	wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1572 	mutex_unlock(&ha->optrom_mutex);
1573 
1574 	return 0;
1575 }
1576 EXPORT_SYMBOL(qlt_stop_phase1);
1577 
1578 /* Called by tcm_qla2xxx configfs code */
qlt_stop_phase2(struct qla_tgt * tgt)1579 void qlt_stop_phase2(struct qla_tgt *tgt)
1580 {
1581 	scsi_qla_host_t *vha = tgt->vha;
1582 
1583 	if (tgt->tgt_stopped) {
1584 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1585 		    "Already in tgt->tgt_stopped state\n");
1586 		dump_stack();
1587 		return;
1588 	}
1589 	if (!tgt->tgt_stop) {
1590 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1591 		    "%s: phase1 stop is not completed\n", __func__);
1592 		dump_stack();
1593 		return;
1594 	}
1595 
1596 	mutex_lock(&tgt->ha->optrom_mutex);
1597 	mutex_lock(&vha->vha_tgt.tgt_mutex);
1598 	tgt->tgt_stop = 0;
1599 	tgt->tgt_stopped = 1;
1600 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
1601 	mutex_unlock(&tgt->ha->optrom_mutex);
1602 
1603 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1604 	    tgt);
1605 
1606 	switch (vha->qlini_mode) {
1607 	case QLA2XXX_INI_MODE_EXCLUSIVE:
1608 		vha->flags.online = 1;
1609 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1610 		break;
1611 	default:
1612 		break;
1613 	}
1614 }
1615 EXPORT_SYMBOL(qlt_stop_phase2);
1616 
1617 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
qlt_release(struct qla_tgt * tgt)1618 static void qlt_release(struct qla_tgt *tgt)
1619 {
1620 	scsi_qla_host_t *vha = tgt->vha;
1621 	void *node;
1622 	u64 key = 0;
1623 	u16 i;
1624 	struct qla_qpair_hint *h;
1625 	struct qla_hw_data *ha = vha->hw;
1626 
1627 	if (!tgt->tgt_stop && !tgt->tgt_stopped)
1628 		qlt_stop_phase1(tgt);
1629 
1630 	if (!tgt->tgt_stopped)
1631 		qlt_stop_phase2(tgt);
1632 
1633 	for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1634 		unsigned long flags;
1635 
1636 		h = &tgt->qphints[i];
1637 		if (h->qpair) {
1638 			spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1639 			list_del(&h->hint_elem);
1640 			spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1641 			h->qpair = NULL;
1642 		}
1643 	}
1644 	kfree(tgt->qphints);
1645 	mutex_lock(&qla_tgt_mutex);
1646 	list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1647 	mutex_unlock(&qla_tgt_mutex);
1648 
1649 	btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1650 		btree_remove64(&tgt->lun_qpair_map, key);
1651 
1652 	btree_destroy64(&tgt->lun_qpair_map);
1653 
1654 	if (vha->vp_idx)
1655 		if (ha->tgt.tgt_ops &&
1656 		    ha->tgt.tgt_ops->remove_target &&
1657 		    vha->vha_tgt.target_lport_ptr)
1658 			ha->tgt.tgt_ops->remove_target(vha);
1659 
1660 	vha->vha_tgt.qla_tgt = NULL;
1661 
1662 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1663 	    "Release of tgt %p finished\n", tgt);
1664 
1665 	kfree(tgt);
1666 }
1667 
1668 /* ha->hardware_lock supposed to be held on entry */
qlt_sched_sess_work(struct qla_tgt * tgt,int type,const void * param,unsigned int param_size)1669 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1670 	const void *param, unsigned int param_size)
1671 {
1672 	struct qla_tgt_sess_work_param *prm;
1673 	unsigned long flags;
1674 
1675 	prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1676 	if (!prm) {
1677 		ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1678 		    "qla_target(%d): Unable to create session "
1679 		    "work, command will be refused", 0);
1680 		return -ENOMEM;
1681 	}
1682 
1683 	ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1684 	    "Scheduling work (type %d, prm %p)"
1685 	    " to find session for param %p (size %d, tgt %p)\n",
1686 	    type, prm, param, param_size, tgt);
1687 
1688 	prm->type = type;
1689 	memcpy(&prm->tm_iocb, param, param_size);
1690 
1691 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
1692 	list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1693 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1694 
1695 	schedule_work(&tgt->sess_work);
1696 
1697 	return 0;
1698 }
1699 
1700 /*
1701  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1702  */
qlt_send_notify_ack(struct qla_qpair * qpair,struct imm_ntfy_from_isp * ntfy,uint32_t add_flags,uint16_t resp_code,int resp_code_valid,uint16_t srr_flags,uint16_t srr_reject_code,uint8_t srr_explan)1703 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1704 	struct imm_ntfy_from_isp *ntfy,
1705 	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1706 	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1707 {
1708 	struct scsi_qla_host *vha = qpair->vha;
1709 	struct qla_hw_data *ha = vha->hw;
1710 	request_t *pkt;
1711 	struct nack_to_isp *nack;
1712 
1713 	if (!ha->flags.fw_started)
1714 		return;
1715 
1716 	ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1717 
1718 	pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1719 	if (!pkt) {
1720 		ql_dbg(ql_dbg_tgt, vha, 0xe049,
1721 		    "qla_target(%d): %s failed: unable to allocate "
1722 		    "request packet\n", vha->vp_idx, __func__);
1723 		return;
1724 	}
1725 
1726 	if (vha->vha_tgt.qla_tgt != NULL)
1727 		vha->vha_tgt.qla_tgt->notify_ack_expected++;
1728 
1729 	pkt->entry_type = NOTIFY_ACK_TYPE;
1730 	pkt->entry_count = 1;
1731 
1732 	nack = (struct nack_to_isp *)pkt;
1733 	nack->ox_id = ntfy->ox_id;
1734 
1735 	nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1736 	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1737 	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1738 		nack->u.isp24.flags = ntfy->u.isp24.flags &
1739 			cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1740 	}
1741 	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1742 	nack->u.isp24.status = ntfy->u.isp24.status;
1743 	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1744 	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1745 	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1746 	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1747 	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1748 	nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1749 	nack->u.isp24.srr_reject_code = srr_reject_code;
1750 	nack->u.isp24.srr_reject_code_expl = srr_explan;
1751 	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1752 
1753 	/* TODO qualify this with EDIF enable */
1754 	if (ntfy->u.isp24.status_subcode == ELS_PLOGI &&
1755 	    (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
1756 		nack->u.isp24.flags |= cpu_to_le16(NOTIFY_ACK_FLAGS_FCSP);
1757 	}
1758 
1759 	ql_dbg(ql_dbg_tgt, vha, 0xe005,
1760 	    "qla_target(%d): Sending 24xx Notify Ack %d\n",
1761 	    vha->vp_idx, nack->u.isp24.status);
1762 
1763 	/* Memory Barrier */
1764 	wmb();
1765 	qla2x00_start_iocbs(vha, qpair->req);
1766 }
1767 
qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd * mcmd)1768 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1769 {
1770 	struct scsi_qla_host *vha = mcmd->vha;
1771 	struct qla_hw_data *ha = vha->hw;
1772 	struct abts_resp_to_24xx *resp;
1773 	__le32 f_ctl;
1774 	uint32_t h;
1775 	uint8_t *p;
1776 	int rc;
1777 	struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1778 	struct qla_qpair *qpair = mcmd->qpair;
1779 
1780 	ql_dbg(ql_dbg_tgt, vha, 0xe006,
1781 	    "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1782 	    ha, mcmd->fc_tm_rsp);
1783 
1784 	rc = qlt_check_reserve_free_req(qpair, 1);
1785 	if (rc) {
1786 		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1787 		    "qla_target(%d): %s failed: unable to allocate request packet\n",
1788 		    vha->vp_idx, __func__);
1789 		return -EAGAIN;
1790 	}
1791 
1792 	resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1793 	memset(resp, 0, sizeof(*resp));
1794 
1795 	h = qlt_make_handle(qpair);
1796 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1797 		/*
1798 		 * CTIO type 7 from the firmware doesn't provide a way to
1799 		 * know the initiator's LOOP ID, hence we can't find
1800 		 * the session and, so, the command.
1801 		 */
1802 		return -EAGAIN;
1803 	} else {
1804 		qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1805 	}
1806 
1807 	resp->handle = make_handle(qpair->req->id, h);
1808 	resp->entry_type = ABTS_RESP_24XX;
1809 	resp->entry_count = 1;
1810 	resp->nport_handle = abts->nport_handle;
1811 	resp->vp_index = vha->vp_idx;
1812 	resp->sof_type = abts->sof_type;
1813 	resp->exchange_address = abts->exchange_address;
1814 	resp->fcp_hdr_le = abts->fcp_hdr_le;
1815 	f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1816 	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1817 	    F_CTL_SEQ_INITIATIVE);
1818 	p = (uint8_t *)&f_ctl;
1819 	resp->fcp_hdr_le.f_ctl[0] = *p++;
1820 	resp->fcp_hdr_le.f_ctl[1] = *p++;
1821 	resp->fcp_hdr_le.f_ctl[2] = *p;
1822 
1823 	resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1824 	resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1825 
1826 	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1827 	if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1828 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1829 		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1830 		resp->payload.ba_acct.low_seq_cnt = 0x0000;
1831 		resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1832 		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1833 		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1834 	} else {
1835 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1836 		resp->payload.ba_rjt.reason_code =
1837 			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1838 		/* Other bytes are zero */
1839 	}
1840 
1841 	vha->vha_tgt.qla_tgt->abts_resp_expected++;
1842 
1843 	/* Memory Barrier */
1844 	wmb();
1845 	if (qpair->reqq_start_iocbs)
1846 		qpair->reqq_start_iocbs(qpair);
1847 	else
1848 		qla2x00_start_iocbs(vha, qpair->req);
1849 
1850 	return rc;
1851 }
1852 
1853 /*
1854  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1855  */
qlt_24xx_send_abts_resp(struct qla_qpair * qpair,struct abts_recv_from_24xx * abts,uint32_t status,bool ids_reversed)1856 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1857 	struct abts_recv_from_24xx *abts, uint32_t status,
1858 	bool ids_reversed)
1859 {
1860 	struct scsi_qla_host *vha = qpair->vha;
1861 	struct qla_hw_data *ha = vha->hw;
1862 	struct abts_resp_to_24xx *resp;
1863 	__le32 f_ctl;
1864 	uint8_t *p;
1865 
1866 	ql_dbg(ql_dbg_tgt, vha, 0xe006,
1867 	    "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1868 	    ha, abts, status);
1869 
1870 	resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1871 	    NULL);
1872 	if (!resp) {
1873 		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1874 		    "qla_target(%d): %s failed: unable to allocate "
1875 		    "request packet", vha->vp_idx, __func__);
1876 		return;
1877 	}
1878 
1879 	resp->entry_type = ABTS_RESP_24XX;
1880 	resp->handle = QLA_TGT_SKIP_HANDLE;
1881 	resp->entry_count = 1;
1882 	resp->nport_handle = abts->nport_handle;
1883 	resp->vp_index = vha->vp_idx;
1884 	resp->sof_type = abts->sof_type;
1885 	resp->exchange_address = abts->exchange_address;
1886 	resp->fcp_hdr_le = abts->fcp_hdr_le;
1887 	f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1888 	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1889 	    F_CTL_SEQ_INITIATIVE);
1890 	p = (uint8_t *)&f_ctl;
1891 	resp->fcp_hdr_le.f_ctl[0] = *p++;
1892 	resp->fcp_hdr_le.f_ctl[1] = *p++;
1893 	resp->fcp_hdr_le.f_ctl[2] = *p;
1894 	if (ids_reversed) {
1895 		resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1896 		resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1897 	} else {
1898 		resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1899 		resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1900 	}
1901 	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1902 	if (status == FCP_TMF_CMPL) {
1903 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1904 		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1905 		resp->payload.ba_acct.low_seq_cnt = 0x0000;
1906 		resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1907 		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1908 		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1909 	} else {
1910 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1911 		resp->payload.ba_rjt.reason_code =
1912 			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1913 		/* Other bytes are zero */
1914 	}
1915 
1916 	vha->vha_tgt.qla_tgt->abts_resp_expected++;
1917 
1918 	/* Memory Barrier */
1919 	wmb();
1920 	if (qpair->reqq_start_iocbs)
1921 		qpair->reqq_start_iocbs(qpair);
1922 	else
1923 		qla2x00_start_iocbs(vha, qpair->req);
1924 }
1925 
1926 /*
1927  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1928  */
qlt_24xx_retry_term_exchange(struct scsi_qla_host * vha,struct qla_qpair * qpair,response_t * pkt,struct qla_tgt_mgmt_cmd * mcmd)1929 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1930     struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1931 {
1932 	struct ctio7_to_24xx *ctio;
1933 	u16 tmp;
1934 	struct abts_recv_from_24xx *entry;
1935 
1936 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1937 	if (ctio == NULL) {
1938 		ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1939 		    "qla_target(%d): %s failed: unable to allocate "
1940 		    "request packet\n", vha->vp_idx, __func__);
1941 		return;
1942 	}
1943 
1944 	if (mcmd)
1945 		/* abts from remote port */
1946 		entry = &mcmd->orig_iocb.abts;
1947 	else
1948 		/* abts from this driver.  */
1949 		entry = (struct abts_recv_from_24xx *)pkt;
1950 
1951 	/*
1952 	 * We've got on entrance firmware's response on by us generated
1953 	 * ABTS response. So, in it ID fields are reversed.
1954 	 */
1955 
1956 	ctio->entry_type = CTIO_TYPE7;
1957 	ctio->entry_count = 1;
1958 	ctio->nport_handle = entry->nport_handle;
1959 	ctio->handle = QLA_TGT_SKIP_HANDLE |	CTIO_COMPLETION_HANDLE_MARK;
1960 	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1961 	ctio->vp_index = vha->vp_idx;
1962 	ctio->exchange_addr = entry->exchange_addr_to_abort;
1963 	tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1964 
1965 	if (mcmd) {
1966 		ctio->initiator_id = entry->fcp_hdr_le.s_id;
1967 
1968 		if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1969 			tmp |= (mcmd->abort_io_attr << 9);
1970 		else if (qpair->retry_term_cnt & 1)
1971 			tmp |= (0x4 << 9);
1972 	} else {
1973 		ctio->initiator_id = entry->fcp_hdr_le.d_id;
1974 
1975 		if (qpair->retry_term_cnt & 1)
1976 			tmp |= (0x4 << 9);
1977 	}
1978 	ctio->u.status1.flags = cpu_to_le16(tmp);
1979 	ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1980 
1981 	ql_dbg(ql_dbg_tgt, vha, 0xe007,
1982 	    "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1983 	    le16_to_cpu(ctio->u.status1.flags),
1984 	    le16_to_cpu(ctio->u.status1.ox_id),
1985 	    (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1986 
1987 	/* Memory Barrier */
1988 	wmb();
1989 	if (qpair->reqq_start_iocbs)
1990 		qpair->reqq_start_iocbs(qpair);
1991 	else
1992 		qla2x00_start_iocbs(vha, qpair->req);
1993 
1994 	if (mcmd)
1995 		qlt_build_abts_resp_iocb(mcmd);
1996 	else
1997 		qlt_24xx_send_abts_resp(qpair,
1998 		    (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1999 
2000 }
2001 
2002 /* drop cmds for the given lun
2003  * XXX only looks for cmds on the port through which lun reset was recieved
2004  * XXX does not go through the list of other port (which may have cmds
2005  *     for the same lun)
2006  */
abort_cmds_for_lun(struct scsi_qla_host * vha,u64 lun,be_id_t s_id)2007 static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
2008 {
2009 	struct qla_tgt_sess_op *op;
2010 	struct qla_tgt_cmd *cmd;
2011 	uint32_t key;
2012 	unsigned long flags;
2013 
2014 	key = sid_to_key(s_id);
2015 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
2016 	list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2017 		uint32_t op_key;
2018 		u64 op_lun;
2019 
2020 		op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2021 		op_lun = scsilun_to_int(
2022 			(struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2023 		if (op_key == key && op_lun == lun)
2024 			op->aborted = true;
2025 	}
2026 
2027 	list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2028 		uint32_t cmd_key;
2029 		u64 cmd_lun;
2030 
2031 		cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2032 		cmd_lun = scsilun_to_int(
2033 			(struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2034 		if (cmd_key == key && cmd_lun == lun)
2035 			cmd->aborted = 1;
2036 	}
2037 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2038 }
2039 
qlt_find_qphint(struct scsi_qla_host * vha,uint64_t unpacked_lun)2040 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2041     uint64_t unpacked_lun)
2042 {
2043 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2044 	struct qla_qpair_hint *h = NULL;
2045 
2046 	if (vha->flags.qpairs_available) {
2047 		h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2048 		if (!h)
2049 			h = &tgt->qphints[0];
2050 	} else {
2051 		h = &tgt->qphints[0];
2052 	}
2053 
2054 	return h;
2055 }
2056 
qlt_do_tmr_work(struct work_struct * work)2057 static void qlt_do_tmr_work(struct work_struct *work)
2058 {
2059 	struct qla_tgt_mgmt_cmd *mcmd =
2060 		container_of(work, struct qla_tgt_mgmt_cmd, work);
2061 	struct qla_hw_data *ha = mcmd->vha->hw;
2062 	int rc;
2063 	uint32_t tag;
2064 	unsigned long flags;
2065 
2066 	switch (mcmd->tmr_func) {
2067 	case QLA_TGT_ABTS:
2068 		tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2069 		break;
2070 	default:
2071 		tag = 0;
2072 		break;
2073 	}
2074 
2075 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2076 	    mcmd->tmr_func, tag);
2077 
2078 	if (rc != 0) {
2079 		spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2080 		switch (mcmd->tmr_func) {
2081 		case QLA_TGT_ABTS:
2082 			mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2083 			qlt_build_abts_resp_iocb(mcmd);
2084 			break;
2085 		case QLA_TGT_LUN_RESET:
2086 		case QLA_TGT_CLEAR_TS:
2087 		case QLA_TGT_ABORT_TS:
2088 		case QLA_TGT_CLEAR_ACA:
2089 		case QLA_TGT_TARGET_RESET:
2090 			qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2091 			    qla_sam_status);
2092 			break;
2093 
2094 		case QLA_TGT_ABORT_ALL:
2095 		case QLA_TGT_NEXUS_LOSS_SESS:
2096 		case QLA_TGT_NEXUS_LOSS:
2097 			qlt_send_notify_ack(mcmd->qpair,
2098 			    &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2099 			break;
2100 		}
2101 		spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2102 
2103 		ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2104 		    "qla_target(%d):  tgt_ops->handle_tmr() failed: %d\n",
2105 		    mcmd->vha->vp_idx, rc);
2106 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2107 	}
2108 }
2109 
2110 /* ha->hardware_lock supposed to be held on entry */
__qlt_24xx_handle_abts(struct scsi_qla_host * vha,struct abts_recv_from_24xx * abts,struct fc_port * sess)2111 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2112 	struct abts_recv_from_24xx *abts, struct fc_port *sess)
2113 {
2114 	struct qla_hw_data *ha = vha->hw;
2115 	struct qla_tgt_mgmt_cmd *mcmd;
2116 	struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2117 	struct qla_tgt_cmd *abort_cmd;
2118 
2119 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2120 	    "qla_target(%d): task abort (tag=%d)\n",
2121 	    vha->vp_idx, abts->exchange_addr_to_abort);
2122 
2123 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2124 	if (mcmd == NULL) {
2125 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2126 		    "qla_target(%d): %s: Allocation of ABORT cmd failed",
2127 		    vha->vp_idx, __func__);
2128 		return -ENOMEM;
2129 	}
2130 	memset(mcmd, 0, sizeof(*mcmd));
2131 	mcmd->cmd_type = TYPE_TGT_TMCMD;
2132 	mcmd->sess = sess;
2133 	memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2134 	mcmd->reset_count = ha->base_qpair->chip_reset;
2135 	mcmd->tmr_func = QLA_TGT_ABTS;
2136 	mcmd->qpair = h->qpair;
2137 	mcmd->vha = vha;
2138 
2139 	/*
2140 	 * LUN is looked up by target-core internally based on the passed
2141 	 * abts->exchange_addr_to_abort tag.
2142 	 */
2143 	mcmd->se_cmd.cpuid = h->cpuid;
2144 
2145 	abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2146 				le32_to_cpu(abts->exchange_addr_to_abort));
2147 	if (!abort_cmd) {
2148 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2149 		return -EIO;
2150 	}
2151 	mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2152 
2153 	if (abort_cmd->qpair) {
2154 		mcmd->qpair = abort_cmd->qpair;
2155 		mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2156 		mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2157 		mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2158 	}
2159 
2160 	INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2161 	queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2162 
2163 	return 0;
2164 }
2165 
2166 /*
2167  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2168  */
qlt_24xx_handle_abts(struct scsi_qla_host * vha,struct abts_recv_from_24xx * abts)2169 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2170 	struct abts_recv_from_24xx *abts)
2171 {
2172 	struct qla_hw_data *ha = vha->hw;
2173 	struct fc_port *sess;
2174 	uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2175 	be_id_t s_id;
2176 	int rc;
2177 	unsigned long flags;
2178 
2179 	if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2180 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2181 		    "qla_target(%d): ABTS: Abort Sequence not "
2182 		    "supported\n", vha->vp_idx);
2183 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2184 		    false);
2185 		return;
2186 	}
2187 
2188 	if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2189 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2190 		    "qla_target(%d): ABTS: Unknown Exchange "
2191 		    "Address received\n", vha->vp_idx);
2192 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2193 		    false);
2194 		return;
2195 	}
2196 
2197 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2198 	    "qla_target(%d): task abort (s_id=%x:%x:%x, "
2199 	    "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2200 	    abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2201 	    le32_to_cpu(abts->fcp_hdr_le.parameter));
2202 
2203 	s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2204 
2205 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2206 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2207 	if (!sess) {
2208 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2209 		    "qla_target(%d): task abort for non-existent session\n",
2210 		    vha->vp_idx);
2211 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2212 
2213 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2214 			    false);
2215 		return;
2216 	}
2217 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2218 
2219 
2220 	if (sess->deleted) {
2221 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2222 		    false);
2223 		return;
2224 	}
2225 
2226 	rc = __qlt_24xx_handle_abts(vha, abts, sess);
2227 	if (rc != 0) {
2228 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2229 		    "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2230 		    vha->vp_idx, rc);
2231 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2232 		    false);
2233 		return;
2234 	}
2235 }
2236 
2237 /*
2238  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2239  */
qlt_24xx_send_task_mgmt_ctio(struct qla_qpair * qpair,struct qla_tgt_mgmt_cmd * mcmd,uint32_t resp_code)2240 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2241 	struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2242 {
2243 	struct scsi_qla_host *ha = mcmd->vha;
2244 	struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2245 	struct ctio7_to_24xx *ctio;
2246 	uint16_t temp;
2247 
2248 	ql_dbg(ql_dbg_tgt, ha, 0xe008,
2249 	    "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2250 	    ha, atio, resp_code);
2251 
2252 
2253 	ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2254 	if (ctio == NULL) {
2255 		ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2256 		    "qla_target(%d): %s failed: unable to allocate "
2257 		    "request packet\n", ha->vp_idx, __func__);
2258 		return;
2259 	}
2260 
2261 	ctio->entry_type = CTIO_TYPE7;
2262 	ctio->entry_count = 1;
2263 	ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2264 	ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2265 	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2266 	ctio->vp_index = ha->vp_idx;
2267 	ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2268 	ctio->exchange_addr = atio->u.isp24.exchange_addr;
2269 	temp = (atio->u.isp24.attr << 9)|
2270 		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2271 	ctio->u.status1.flags = cpu_to_le16(temp);
2272 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2273 	ctio->u.status1.ox_id = cpu_to_le16(temp);
2274 	ctio->u.status1.scsi_status =
2275 	    cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2276 	ctio->u.status1.response_len = cpu_to_le16(8);
2277 	ctio->u.status1.sense_data[0] = resp_code;
2278 
2279 	/* Memory Barrier */
2280 	wmb();
2281 	if (qpair->reqq_start_iocbs)
2282 		qpair->reqq_start_iocbs(qpair);
2283 	else
2284 		qla2x00_start_iocbs(ha, qpair->req);
2285 }
2286 
qlt_free_mcmd(struct qla_tgt_mgmt_cmd * mcmd)2287 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2288 {
2289 	mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2290 }
2291 EXPORT_SYMBOL(qlt_free_mcmd);
2292 
2293 /*
2294  * ha->hardware_lock supposed to be held on entry. Might drop it, then
2295  * reacquire
2296  */
qlt_send_resp_ctio(struct qla_qpair * qpair,struct qla_tgt_cmd * cmd,uint8_t scsi_status,uint8_t sense_key,uint8_t asc,uint8_t ascq)2297 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2298     uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2299 {
2300 	struct atio_from_isp *atio = &cmd->atio;
2301 	struct ctio7_to_24xx *ctio;
2302 	uint16_t temp;
2303 	struct scsi_qla_host *vha = cmd->vha;
2304 
2305 	ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2306 	    "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2307 	    "sense_key=%02x, asc=%02x, ascq=%02x",
2308 	    vha, atio, scsi_status, sense_key, asc, ascq);
2309 
2310 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2311 	if (!ctio) {
2312 		ql_dbg(ql_dbg_async, vha, 0x3067,
2313 		    "qla2x00t(%ld): %s failed: unable to allocate request packet",
2314 		    vha->host_no, __func__);
2315 		goto out;
2316 	}
2317 
2318 	ctio->entry_type = CTIO_TYPE7;
2319 	ctio->entry_count = 1;
2320 	ctio->handle = QLA_TGT_SKIP_HANDLE;
2321 	ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2322 	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2323 	ctio->vp_index = vha->vp_idx;
2324 	ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2325 	ctio->exchange_addr = atio->u.isp24.exchange_addr;
2326 	temp = (atio->u.isp24.attr << 9) |
2327 	    CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2328 	ctio->u.status1.flags = cpu_to_le16(temp);
2329 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2330 	ctio->u.status1.ox_id = cpu_to_le16(temp);
2331 	ctio->u.status1.scsi_status =
2332 	    cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2333 	ctio->u.status1.response_len = cpu_to_le16(18);
2334 	ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2335 
2336 	if (ctio->u.status1.residual != 0)
2337 		ctio->u.status1.scsi_status |=
2338 		    cpu_to_le16(SS_RESIDUAL_UNDER);
2339 
2340 	/* Fixed format sense data. */
2341 	ctio->u.status1.sense_data[0] = 0x70;
2342 	ctio->u.status1.sense_data[2] = sense_key;
2343 	/* Additional sense length */
2344 	ctio->u.status1.sense_data[7] = 0xa;
2345 	/* ASC and ASCQ */
2346 	ctio->u.status1.sense_data[12] = asc;
2347 	ctio->u.status1.sense_data[13] = ascq;
2348 
2349 	/* Memory Barrier */
2350 	wmb();
2351 
2352 	if (qpair->reqq_start_iocbs)
2353 		qpair->reqq_start_iocbs(qpair);
2354 	else
2355 		qla2x00_start_iocbs(vha, qpair->req);
2356 
2357 out:
2358 	return;
2359 }
2360 
2361 /* callback from target fabric module code */
qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd * mcmd)2362 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2363 {
2364 	struct scsi_qla_host *vha = mcmd->sess->vha;
2365 	struct qla_hw_data *ha = vha->hw;
2366 	unsigned long flags;
2367 	struct qla_qpair *qpair = mcmd->qpair;
2368 	bool free_mcmd = true;
2369 
2370 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2371 	    "TM response mcmd (%p) status %#x state %#x",
2372 	    mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2373 
2374 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2375 
2376 	if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2377 		/*
2378 		 * Either the port is not online or this request was from
2379 		 * previous life, just abort the processing.
2380 		 */
2381 		ql_dbg(ql_dbg_async, vha, 0xe100,
2382 			"RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2383 			vha->flags.online, qla2x00_reset_active(vha),
2384 			mcmd->reset_count, qpair->chip_reset);
2385 		ha->tgt.tgt_ops->free_mcmd(mcmd);
2386 		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2387 		return;
2388 	}
2389 
2390 	if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2391 		switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2392 		case ELS_LOGO:
2393 		case ELS_PRLO:
2394 		case ELS_TPRLO:
2395 			ql_dbg(ql_dbg_disc, vha, 0x2106,
2396 			    "TM response logo %8phC status %#x state %#x",
2397 			    mcmd->sess->port_name, mcmd->fc_tm_rsp,
2398 			    mcmd->flags);
2399 			qlt_schedule_sess_for_deletion(mcmd->sess);
2400 			break;
2401 		default:
2402 			qlt_send_notify_ack(vha->hw->base_qpair,
2403 			    &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2404 			break;
2405 		}
2406 	} else {
2407 		if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2408 			qlt_build_abts_resp_iocb(mcmd);
2409 			free_mcmd = false;
2410 		} else
2411 			qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2412 			    mcmd->fc_tm_rsp);
2413 	}
2414 	/*
2415 	 * Make the callback for ->free_mcmd() to queue_work() and invoke
2416 	 * target_put_sess_cmd() to drop cmd_kref to 1.  The final
2417 	 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2418 	 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2419 	 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2420 	 * qlt_xmit_tm_rsp() returns here..
2421 	 */
2422 	if (free_mcmd)
2423 		ha->tgt.tgt_ops->free_mcmd(mcmd);
2424 
2425 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2426 }
2427 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2428 
2429 /* No locks */
qlt_pci_map_calc_cnt(struct qla_tgt_prm * prm)2430 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2431 {
2432 	struct qla_tgt_cmd *cmd = prm->cmd;
2433 
2434 	BUG_ON(cmd->sg_cnt == 0);
2435 
2436 	prm->sg = (struct scatterlist *)cmd->sg;
2437 	prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2438 	    cmd->sg_cnt, cmd->dma_data_direction);
2439 	if (unlikely(prm->seg_cnt == 0))
2440 		goto out_err;
2441 
2442 	prm->cmd->sg_mapped = 1;
2443 
2444 	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2445 		/*
2446 		 * If greater than four sg entries then we need to allocate
2447 		 * the continuation entries
2448 		 */
2449 		if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2450 			prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2451 			QLA_TGT_DATASEGS_PER_CMD_24XX,
2452 			QLA_TGT_DATASEGS_PER_CONT_24XX);
2453 	} else {
2454 		/* DIF */
2455 		if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2456 		    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2457 			prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2458 			prm->tot_dsds = prm->seg_cnt;
2459 		} else
2460 			prm->tot_dsds = prm->seg_cnt;
2461 
2462 		if (cmd->prot_sg_cnt) {
2463 			prm->prot_sg      = cmd->prot_sg;
2464 			prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2465 				cmd->prot_sg, cmd->prot_sg_cnt,
2466 				cmd->dma_data_direction);
2467 			if (unlikely(prm->prot_seg_cnt == 0))
2468 				goto out_err;
2469 
2470 			if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2471 			    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2472 				/* Dif Bundling not support here */
2473 				prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2474 								cmd->blk_sz);
2475 				prm->tot_dsds += prm->prot_seg_cnt;
2476 			} else
2477 				prm->tot_dsds += prm->prot_seg_cnt;
2478 		}
2479 	}
2480 
2481 	return 0;
2482 
2483 out_err:
2484 	ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2485 	    "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2486 	    0, prm->cmd->sg_cnt);
2487 	return -1;
2488 }
2489 
qlt_unmap_sg(struct scsi_qla_host * vha,struct qla_tgt_cmd * cmd)2490 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2491 {
2492 	struct qla_hw_data *ha;
2493 	struct qla_qpair *qpair;
2494 
2495 	if (!cmd->sg_mapped)
2496 		return;
2497 
2498 	qpair = cmd->qpair;
2499 
2500 	dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2501 	    cmd->dma_data_direction);
2502 	cmd->sg_mapped = 0;
2503 
2504 	if (cmd->prot_sg_cnt)
2505 		dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2506 			cmd->dma_data_direction);
2507 
2508 	if (!cmd->ctx)
2509 		return;
2510 	ha = vha->hw;
2511 	if (cmd->ctx_dsd_alloced)
2512 		qla2x00_clean_dsd_pool(ha, cmd->ctx);
2513 
2514 	dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2515 }
2516 
qlt_check_reserve_free_req(struct qla_qpair * qpair,uint32_t req_cnt)2517 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2518 	uint32_t req_cnt)
2519 {
2520 	uint32_t cnt;
2521 	struct req_que *req = qpair->req;
2522 
2523 	if (req->cnt < (req_cnt + 2)) {
2524 		cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2525 		    rd_reg_dword_relaxed(req->req_q_out));
2526 
2527 		if  (req->ring_index < cnt)
2528 			req->cnt = cnt - req->ring_index;
2529 		else
2530 			req->cnt = req->length - (req->ring_index - cnt);
2531 
2532 		if (unlikely(req->cnt < (req_cnt + 2)))
2533 			return -EAGAIN;
2534 	}
2535 
2536 	req->cnt -= req_cnt;
2537 
2538 	return 0;
2539 }
2540 
2541 /*
2542  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2543  */
qlt_get_req_pkt(struct req_que * req)2544 static inline void *qlt_get_req_pkt(struct req_que *req)
2545 {
2546 	/* Adjust ring index. */
2547 	req->ring_index++;
2548 	if (req->ring_index == req->length) {
2549 		req->ring_index = 0;
2550 		req->ring_ptr = req->ring;
2551 	} else {
2552 		req->ring_ptr++;
2553 	}
2554 	return (cont_entry_t *)req->ring_ptr;
2555 }
2556 
2557 /* ha->hardware_lock supposed to be held on entry */
qlt_make_handle(struct qla_qpair * qpair)2558 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2559 {
2560 	uint32_t h;
2561 	int index;
2562 	uint8_t found = 0;
2563 	struct req_que *req = qpair->req;
2564 
2565 	h = req->current_outstanding_cmd;
2566 
2567 	for (index = 1; index < req->num_outstanding_cmds; index++) {
2568 		h++;
2569 		if (h == req->num_outstanding_cmds)
2570 			h = 1;
2571 
2572 		if (h == QLA_TGT_SKIP_HANDLE)
2573 			continue;
2574 
2575 		if (!req->outstanding_cmds[h]) {
2576 			found = 1;
2577 			break;
2578 		}
2579 	}
2580 
2581 	if (found) {
2582 		req->current_outstanding_cmd = h;
2583 	} else {
2584 		ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2585 		    "qla_target(%d): Ran out of empty cmd slots\n",
2586 		    qpair->vha->vp_idx);
2587 		h = QLA_TGT_NULL_HANDLE;
2588 	}
2589 
2590 	return h;
2591 }
2592 
2593 /* ha->hardware_lock supposed to be held on entry */
qlt_24xx_build_ctio_pkt(struct qla_qpair * qpair,struct qla_tgt_prm * prm)2594 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2595 	struct qla_tgt_prm *prm)
2596 {
2597 	uint32_t h;
2598 	struct ctio7_to_24xx *pkt;
2599 	struct atio_from_isp *atio = &prm->cmd->atio;
2600 	uint16_t temp;
2601 	struct qla_tgt_cmd      *cmd = prm->cmd;
2602 
2603 	pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2604 	prm->pkt = pkt;
2605 	memset(pkt, 0, sizeof(*pkt));
2606 
2607 	pkt->entry_type = CTIO_TYPE7;
2608 	pkt->entry_count = (uint8_t)prm->req_cnt;
2609 	pkt->vp_index = prm->cmd->vp_idx;
2610 
2611 	h = qlt_make_handle(qpair);
2612 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2613 		/*
2614 		 * CTIO type 7 from the firmware doesn't provide a way to
2615 		 * know the initiator's LOOP ID, hence we can't find
2616 		 * the session and, so, the command.
2617 		 */
2618 		return -EAGAIN;
2619 	} else
2620 		qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2621 
2622 	pkt->handle = make_handle(qpair->req->id, h);
2623 	pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2624 	pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2625 	pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2626 	pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2627 	pkt->exchange_addr = atio->u.isp24.exchange_addr;
2628 	temp = atio->u.isp24.attr << 9;
2629 	pkt->u.status0.flags |= cpu_to_le16(temp);
2630 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2631 	pkt->u.status0.ox_id = cpu_to_le16(temp);
2632 	pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2633 
2634 	if (cmd->edif) {
2635 		if (cmd->dma_data_direction == DMA_TO_DEVICE)
2636 			prm->cmd->sess->edif.rx_bytes += cmd->bufflen;
2637 		if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2638 			prm->cmd->sess->edif.tx_bytes += cmd->bufflen;
2639 
2640 		pkt->u.status0.edif_flags |= EF_EN_EDIF;
2641 	}
2642 
2643 	return 0;
2644 }
2645 
2646 /*
2647  * ha->hardware_lock supposed to be held on entry. We have already made sure
2648  * that there is sufficient amount of request entries to not drop it.
2649  */
qlt_load_cont_data_segments(struct qla_tgt_prm * prm)2650 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2651 {
2652 	int cnt;
2653 	struct dsd64 *cur_dsd;
2654 
2655 	/* Build continuation packets */
2656 	while (prm->seg_cnt > 0) {
2657 		cont_a64_entry_t *cont_pkt64 =
2658 			(cont_a64_entry_t *)qlt_get_req_pkt(
2659 			   prm->cmd->qpair->req);
2660 
2661 		/*
2662 		 * Make sure that from cont_pkt64 none of
2663 		 * 64-bit specific fields used for 32-bit
2664 		 * addressing. Cast to (cont_entry_t *) for
2665 		 * that.
2666 		 */
2667 
2668 		memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2669 
2670 		cont_pkt64->entry_count = 1;
2671 		cont_pkt64->sys_define = 0;
2672 
2673 		cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2674 		cur_dsd = cont_pkt64->dsd;
2675 
2676 		/* Load continuation entry data segments */
2677 		for (cnt = 0;
2678 		    cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2679 		    cnt++, prm->seg_cnt--) {
2680 			append_dsd64(&cur_dsd, prm->sg);
2681 			prm->sg = sg_next(prm->sg);
2682 		}
2683 	}
2684 }
2685 
2686 /*
2687  * ha->hardware_lock supposed to be held on entry. We have already made sure
2688  * that there is sufficient amount of request entries to not drop it.
2689  */
qlt_load_data_segments(struct qla_tgt_prm * prm)2690 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2691 {
2692 	int cnt;
2693 	struct dsd64 *cur_dsd;
2694 	struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2695 
2696 	pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2697 
2698 	/* Setup packet address segment pointer */
2699 	cur_dsd = &pkt24->u.status0.dsd;
2700 
2701 	/* Set total data segment count */
2702 	if (prm->seg_cnt)
2703 		pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2704 
2705 	if (prm->seg_cnt == 0) {
2706 		/* No data transfer */
2707 		cur_dsd->address = 0;
2708 		cur_dsd->length = 0;
2709 		return;
2710 	}
2711 
2712 	/* If scatter gather */
2713 
2714 	/* Load command entry data segments */
2715 	for (cnt = 0;
2716 	    (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2717 	    cnt++, prm->seg_cnt--) {
2718 		append_dsd64(&cur_dsd, prm->sg);
2719 		prm->sg = sg_next(prm->sg);
2720 	}
2721 
2722 	qlt_load_cont_data_segments(prm);
2723 }
2724 
qlt_has_data(struct qla_tgt_cmd * cmd)2725 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2726 {
2727 	return cmd->bufflen > 0;
2728 }
2729 
qlt_print_dif_err(struct qla_tgt_prm * prm)2730 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2731 {
2732 	struct qla_tgt_cmd *cmd;
2733 	struct scsi_qla_host *vha;
2734 
2735 	/* asc 0x10=dif error */
2736 	if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2737 		cmd = prm->cmd;
2738 		vha = cmd->vha;
2739 		/* ASCQ */
2740 		switch (prm->sense_buffer[13]) {
2741 		case 1:
2742 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2743 			    "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2744 			    "se_cmd=%p tag[%x]",
2745 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2746 			    cmd->atio.u.isp24.exchange_addr);
2747 			break;
2748 		case 2:
2749 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2750 			    "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2751 			    "se_cmd=%p tag[%x]",
2752 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2753 			    cmd->atio.u.isp24.exchange_addr);
2754 			break;
2755 		case 3:
2756 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2757 			    "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2758 			    "se_cmd=%p tag[%x]",
2759 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2760 			    cmd->atio.u.isp24.exchange_addr);
2761 			break;
2762 		default:
2763 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2764 			    "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2765 			    "se_cmd=%p tag[%x]",
2766 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2767 			    cmd->atio.u.isp24.exchange_addr);
2768 			break;
2769 		}
2770 		ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2771 	}
2772 }
2773 
2774 /*
2775  * Called without ha->hardware_lock held
2776  */
qlt_pre_xmit_response(struct qla_tgt_cmd * cmd,struct qla_tgt_prm * prm,int xmit_type,uint8_t scsi_status,uint32_t * full_req_cnt)2777 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2778 	struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2779 	uint32_t *full_req_cnt)
2780 {
2781 	struct se_cmd *se_cmd = &cmd->se_cmd;
2782 	struct qla_qpair *qpair = cmd->qpair;
2783 
2784 	prm->cmd = cmd;
2785 	prm->tgt = cmd->tgt;
2786 	prm->pkt = NULL;
2787 	prm->rq_result = scsi_status;
2788 	prm->sense_buffer = &cmd->sense_buffer[0];
2789 	prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2790 	prm->sg = NULL;
2791 	prm->seg_cnt = -1;
2792 	prm->req_cnt = 1;
2793 	prm->residual = 0;
2794 	prm->add_status_pkt = 0;
2795 	prm->prot_sg = NULL;
2796 	prm->prot_seg_cnt = 0;
2797 	prm->tot_dsds = 0;
2798 
2799 	if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2800 		if  (qlt_pci_map_calc_cnt(prm) != 0)
2801 			return -EAGAIN;
2802 	}
2803 
2804 	*full_req_cnt = prm->req_cnt;
2805 
2806 	if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2807 		prm->residual = se_cmd->residual_count;
2808 		ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2809 		    "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2810 		       prm->residual, se_cmd->tag,
2811 		       se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2812 		       cmd->bufflen, prm->rq_result);
2813 		prm->rq_result |= SS_RESIDUAL_UNDER;
2814 	} else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2815 		prm->residual = se_cmd->residual_count;
2816 		ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2817 		    "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2818 		       prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2819 		       se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2820 		prm->rq_result |= SS_RESIDUAL_OVER;
2821 	}
2822 
2823 	if (xmit_type & QLA_TGT_XMIT_STATUS) {
2824 		/*
2825 		 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2826 		 * ignored in *xmit_response() below
2827 		 */
2828 		if (qlt_has_data(cmd)) {
2829 			if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2830 			    (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2831 			    (prm->rq_result != 0))) {
2832 				prm->add_status_pkt = 1;
2833 				(*full_req_cnt)++;
2834 			}
2835 		}
2836 	}
2837 
2838 	return 0;
2839 }
2840 
qlt_need_explicit_conf(struct qla_tgt_cmd * cmd,int sending_sense)2841 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2842     int sending_sense)
2843 {
2844 	if (cmd->qpair->enable_class_2)
2845 		return 0;
2846 
2847 	if (sending_sense)
2848 		return cmd->conf_compl_supported;
2849 	else
2850 		return cmd->qpair->enable_explicit_conf &&
2851                     cmd->conf_compl_supported;
2852 }
2853 
qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx * ctio,struct qla_tgt_prm * prm)2854 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2855 	struct qla_tgt_prm *prm)
2856 {
2857 	prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2858 	    (uint32_t)sizeof(ctio->u.status1.sense_data));
2859 	ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2860 	if (qlt_need_explicit_conf(prm->cmd, 0)) {
2861 		ctio->u.status0.flags |= cpu_to_le16(
2862 		    CTIO7_FLAGS_EXPLICIT_CONFORM |
2863 		    CTIO7_FLAGS_CONFORM_REQ);
2864 	}
2865 	ctio->u.status0.residual = cpu_to_le32(prm->residual);
2866 	ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2867 	if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2868 		int i;
2869 
2870 		if (qlt_need_explicit_conf(prm->cmd, 1)) {
2871 			if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2872 				ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2873 				    "Skipping EXPLICIT_CONFORM and "
2874 				    "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2875 				    "non GOOD status\n");
2876 				goto skip_explict_conf;
2877 			}
2878 			ctio->u.status1.flags |= cpu_to_le16(
2879 			    CTIO7_FLAGS_EXPLICIT_CONFORM |
2880 			    CTIO7_FLAGS_CONFORM_REQ);
2881 		}
2882 skip_explict_conf:
2883 		ctio->u.status1.flags &=
2884 		    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2885 		ctio->u.status1.flags |=
2886 		    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2887 		ctio->u.status1.scsi_status |=
2888 		    cpu_to_le16(SS_SENSE_LEN_VALID);
2889 		ctio->u.status1.sense_length =
2890 		    cpu_to_le16(prm->sense_buffer_len);
2891 		for (i = 0; i < prm->sense_buffer_len/4; i++) {
2892 			uint32_t v;
2893 
2894 			v = get_unaligned_be32(
2895 					&((uint32_t *)prm->sense_buffer)[i]);
2896 			put_unaligned_le32(v,
2897 				&((uint32_t *)ctio->u.status1.sense_data)[i]);
2898 		}
2899 		qlt_print_dif_err(prm);
2900 
2901 	} else {
2902 		ctio->u.status1.flags &=
2903 		    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2904 		ctio->u.status1.flags |=
2905 		    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2906 		ctio->u.status1.sense_length = 0;
2907 		memset(ctio->u.status1.sense_data, 0,
2908 		    sizeof(ctio->u.status1.sense_data));
2909 	}
2910 
2911 	/* Sense with len > 24, is it possible ??? */
2912 }
2913 
2914 static inline int
qlt_hba_err_chk_enabled(struct se_cmd * se_cmd)2915 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2916 {
2917 	switch (se_cmd->prot_op) {
2918 	case TARGET_PROT_DOUT_INSERT:
2919 	case TARGET_PROT_DIN_STRIP:
2920 		if (ql2xenablehba_err_chk >= 1)
2921 			return 1;
2922 		break;
2923 	case TARGET_PROT_DOUT_PASS:
2924 	case TARGET_PROT_DIN_PASS:
2925 		if (ql2xenablehba_err_chk >= 2)
2926 			return 1;
2927 		break;
2928 	case TARGET_PROT_DIN_INSERT:
2929 	case TARGET_PROT_DOUT_STRIP:
2930 		return 1;
2931 	default:
2932 		break;
2933 	}
2934 	return 0;
2935 }
2936 
2937 static inline int
qla_tgt_ref_mask_check(struct se_cmd * se_cmd)2938 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2939 {
2940 	switch (se_cmd->prot_op) {
2941 	case TARGET_PROT_DIN_INSERT:
2942 	case TARGET_PROT_DOUT_INSERT:
2943 	case TARGET_PROT_DIN_STRIP:
2944 	case TARGET_PROT_DOUT_STRIP:
2945 	case TARGET_PROT_DIN_PASS:
2946 	case TARGET_PROT_DOUT_PASS:
2947 	    return 1;
2948 	default:
2949 	    return 0;
2950 	}
2951 	return 0;
2952 }
2953 
2954 /*
2955  * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2956  */
2957 static void
qla_tgt_set_dif_tags(struct qla_tgt_cmd * cmd,struct crc_context * ctx,uint16_t * pfw_prot_opts)2958 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2959     uint16_t *pfw_prot_opts)
2960 {
2961 	struct se_cmd *se_cmd = &cmd->se_cmd;
2962 	uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2963 	scsi_qla_host_t *vha = cmd->tgt->vha;
2964 	struct qla_hw_data *ha = vha->hw;
2965 	uint32_t t32 = 0;
2966 
2967 	/*
2968 	 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2969 	 * have been immplemented by TCM, before AppTag is avail.
2970 	 * Look for modesense_handlers[]
2971 	 */
2972 	ctx->app_tag = 0;
2973 	ctx->app_tag_mask[0] = 0x0;
2974 	ctx->app_tag_mask[1] = 0x0;
2975 
2976 	if (IS_PI_UNINIT_CAPABLE(ha)) {
2977 		if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2978 		    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2979 			*pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2980 		else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2981 			*pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2982 	}
2983 
2984 	t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2985 
2986 	switch (se_cmd->prot_type) {
2987 	case TARGET_DIF_TYPE0_PROT:
2988 		/*
2989 		 * No check for ql2xenablehba_err_chk, as it
2990 		 * would be an I/O error if hba tag generation
2991 		 * is not done.
2992 		 */
2993 		ctx->ref_tag = cpu_to_le32(lba);
2994 		/* enable ALL bytes of the ref tag */
2995 		ctx->ref_tag_mask[0] = 0xff;
2996 		ctx->ref_tag_mask[1] = 0xff;
2997 		ctx->ref_tag_mask[2] = 0xff;
2998 		ctx->ref_tag_mask[3] = 0xff;
2999 		break;
3000 	case TARGET_DIF_TYPE1_PROT:
3001 	    /*
3002 	     * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
3003 	     * REF tag, and 16 bit app tag.
3004 	     */
3005 	    ctx->ref_tag = cpu_to_le32(lba);
3006 	    if (!qla_tgt_ref_mask_check(se_cmd) ||
3007 		!(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3008 		    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3009 		    break;
3010 	    }
3011 	    /* enable ALL bytes of the ref tag */
3012 	    ctx->ref_tag_mask[0] = 0xff;
3013 	    ctx->ref_tag_mask[1] = 0xff;
3014 	    ctx->ref_tag_mask[2] = 0xff;
3015 	    ctx->ref_tag_mask[3] = 0xff;
3016 	    break;
3017 	case TARGET_DIF_TYPE2_PROT:
3018 	    /*
3019 	     * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
3020 	     * tag has to match LBA in CDB + N
3021 	     */
3022 	    ctx->ref_tag = cpu_to_le32(lba);
3023 	    if (!qla_tgt_ref_mask_check(se_cmd) ||
3024 		!(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3025 		    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3026 		    break;
3027 	    }
3028 	    /* enable ALL bytes of the ref tag */
3029 	    ctx->ref_tag_mask[0] = 0xff;
3030 	    ctx->ref_tag_mask[1] = 0xff;
3031 	    ctx->ref_tag_mask[2] = 0xff;
3032 	    ctx->ref_tag_mask[3] = 0xff;
3033 	    break;
3034 	case TARGET_DIF_TYPE3_PROT:
3035 	    /* For TYPE 3 protection: 16 bit GUARD only */
3036 	    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3037 	    ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3038 		ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3039 	    break;
3040 	}
3041 }
3042 
3043 static inline int
qlt_build_ctio_crc2_pkt(struct qla_qpair * qpair,struct qla_tgt_prm * prm)3044 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3045 {
3046 	struct dsd64		*cur_dsd;
3047 	uint32_t		transfer_length = 0;
3048 	uint32_t		data_bytes;
3049 	uint32_t		dif_bytes;
3050 	uint8_t			bundling = 1;
3051 	struct crc_context	*crc_ctx_pkt = NULL;
3052 	struct qla_hw_data	*ha;
3053 	struct ctio_crc2_to_fw	*pkt;
3054 	dma_addr_t		crc_ctx_dma;
3055 	uint16_t		fw_prot_opts = 0;
3056 	struct qla_tgt_cmd	*cmd = prm->cmd;
3057 	struct se_cmd		*se_cmd = &cmd->se_cmd;
3058 	uint32_t h;
3059 	struct atio_from_isp *atio = &prm->cmd->atio;
3060 	struct qla_tc_param	tc;
3061 	uint16_t t16;
3062 	scsi_qla_host_t *vha = cmd->vha;
3063 
3064 	ha = vha->hw;
3065 
3066 	pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3067 	prm->pkt = pkt;
3068 	memset(pkt, 0, sizeof(*pkt));
3069 
3070 	ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3071 		"qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3072 		cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3073 		prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3074 
3075 	if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3076 	    (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3077 		bundling = 0;
3078 
3079 	/* Compute dif len and adjust data len to incude protection */
3080 	data_bytes = cmd->bufflen;
3081 	dif_bytes  = (data_bytes / cmd->blk_sz) * 8;
3082 
3083 	switch (se_cmd->prot_op) {
3084 	case TARGET_PROT_DIN_INSERT:
3085 	case TARGET_PROT_DOUT_STRIP:
3086 		transfer_length = data_bytes;
3087 		if (cmd->prot_sg_cnt)
3088 			data_bytes += dif_bytes;
3089 		break;
3090 	case TARGET_PROT_DIN_STRIP:
3091 	case TARGET_PROT_DOUT_INSERT:
3092 	case TARGET_PROT_DIN_PASS:
3093 	case TARGET_PROT_DOUT_PASS:
3094 		transfer_length = data_bytes + dif_bytes;
3095 		break;
3096 	default:
3097 		BUG();
3098 		break;
3099 	}
3100 
3101 	if (!qlt_hba_err_chk_enabled(se_cmd))
3102 		fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3103 	/* HBA error checking enabled */
3104 	else if (IS_PI_UNINIT_CAPABLE(ha)) {
3105 		if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3106 		    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3107 			fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3108 		else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3109 			fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3110 	}
3111 
3112 	switch (se_cmd->prot_op) {
3113 	case TARGET_PROT_DIN_INSERT:
3114 	case TARGET_PROT_DOUT_INSERT:
3115 		fw_prot_opts |= PO_MODE_DIF_INSERT;
3116 		break;
3117 	case TARGET_PROT_DIN_STRIP:
3118 	case TARGET_PROT_DOUT_STRIP:
3119 		fw_prot_opts |= PO_MODE_DIF_REMOVE;
3120 		break;
3121 	case TARGET_PROT_DIN_PASS:
3122 	case TARGET_PROT_DOUT_PASS:
3123 		fw_prot_opts |= PO_MODE_DIF_PASS;
3124 		/* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3125 		break;
3126 	default:/* Normal Request */
3127 		fw_prot_opts |= PO_MODE_DIF_PASS;
3128 		break;
3129 	}
3130 
3131 	/* ---- PKT ---- */
3132 	/* Update entry type to indicate Command Type CRC_2 IOCB */
3133 	pkt->entry_type  = CTIO_CRC2;
3134 	pkt->entry_count = 1;
3135 	pkt->vp_index = cmd->vp_idx;
3136 
3137 	h = qlt_make_handle(qpair);
3138 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3139 		/*
3140 		 * CTIO type 7 from the firmware doesn't provide a way to
3141 		 * know the initiator's LOOP ID, hence we can't find
3142 		 * the session and, so, the command.
3143 		 */
3144 		return -EAGAIN;
3145 	} else
3146 		qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3147 
3148 	pkt->handle  = make_handle(qpair->req->id, h);
3149 	pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3150 	pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3151 	pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3152 	pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3153 	pkt->exchange_addr   = atio->u.isp24.exchange_addr;
3154 
3155 	/* silence compile warning */
3156 	t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3157 	pkt->ox_id  = cpu_to_le16(t16);
3158 
3159 	t16 = (atio->u.isp24.attr << 9);
3160 	pkt->flags |= cpu_to_le16(t16);
3161 	pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3162 
3163 	/* Set transfer direction */
3164 	if (cmd->dma_data_direction == DMA_TO_DEVICE)
3165 		pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3166 	else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3167 		pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3168 
3169 	pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3170 	/* Fibre channel byte count */
3171 	pkt->transfer_length = cpu_to_le32(transfer_length);
3172 
3173 	/* ----- CRC context -------- */
3174 
3175 	/* Allocate CRC context from global pool */
3176 	crc_ctx_pkt = cmd->ctx =
3177 	    dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3178 
3179 	if (!crc_ctx_pkt)
3180 		goto crc_queuing_error;
3181 
3182 	crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3183 	INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3184 
3185 	/* Set handle */
3186 	crc_ctx_pkt->handle = pkt->handle;
3187 
3188 	qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3189 
3190 	put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3191 	pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3192 
3193 	if (!bundling) {
3194 		cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3195 	} else {
3196 		/*
3197 		 * Configure Bundling if we need to fetch interlaving
3198 		 * protection PCI accesses
3199 		 */
3200 		fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3201 		crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3202 		crc_ctx_pkt->u.bundling.dseg_count =
3203 			cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3204 		cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3205 	}
3206 
3207 	/* Finish the common fields of CRC pkt */
3208 	crc_ctx_pkt->blk_size   = cpu_to_le16(cmd->blk_sz);
3209 	crc_ctx_pkt->prot_opts  = cpu_to_le16(fw_prot_opts);
3210 	crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3211 	crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3212 
3213 	memset((uint8_t *)&tc, 0 , sizeof(tc));
3214 	tc.vha = vha;
3215 	tc.blk_sz = cmd->blk_sz;
3216 	tc.bufflen = cmd->bufflen;
3217 	tc.sg = cmd->sg;
3218 	tc.prot_sg = cmd->prot_sg;
3219 	tc.ctx = crc_ctx_pkt;
3220 	tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3221 
3222 	/* Walks data segments */
3223 	pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3224 
3225 	if (!bundling && prm->prot_seg_cnt) {
3226 		if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3227 			prm->tot_dsds, &tc))
3228 			goto crc_queuing_error;
3229 	} else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3230 		(prm->tot_dsds - prm->prot_seg_cnt), &tc))
3231 		goto crc_queuing_error;
3232 
3233 	if (bundling && prm->prot_seg_cnt) {
3234 		/* Walks dif segments */
3235 		pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3236 
3237 		cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3238 		if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3239 			prm->prot_seg_cnt, cmd))
3240 			goto crc_queuing_error;
3241 	}
3242 	return QLA_SUCCESS;
3243 
3244 crc_queuing_error:
3245 	/* Cleanup will be performed by the caller */
3246 	qpair->req->outstanding_cmds[h] = NULL;
3247 
3248 	return QLA_FUNCTION_FAILED;
3249 }
3250 
3251 /*
3252  * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3253  * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3254  */
qlt_xmit_response(struct qla_tgt_cmd * cmd,int xmit_type,uint8_t scsi_status)3255 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3256 	uint8_t scsi_status)
3257 {
3258 	struct scsi_qla_host *vha = cmd->vha;
3259 	struct qla_qpair *qpair = cmd->qpair;
3260 	struct ctio7_to_24xx *pkt;
3261 	struct qla_tgt_prm prm;
3262 	uint32_t full_req_cnt = 0;
3263 	unsigned long flags = 0;
3264 	int res;
3265 
3266 	if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3267 	    (cmd->sess && cmd->sess->deleted)) {
3268 		cmd->state = QLA_TGT_STATE_PROCESSED;
3269 		return 0;
3270 	}
3271 
3272 	ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3273 	    "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3274 	    (xmit_type & QLA_TGT_XMIT_STATUS) ?
3275 	    1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3276 	    &cmd->se_cmd, qpair->id);
3277 
3278 	res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3279 	    &full_req_cnt);
3280 	if (unlikely(res != 0)) {
3281 		return res;
3282 	}
3283 
3284 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3285 
3286 	if (xmit_type == QLA_TGT_XMIT_STATUS)
3287 		qpair->tgt_counters.core_qla_snd_status++;
3288 	else
3289 		qpair->tgt_counters.core_qla_que_buf++;
3290 
3291 	if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3292 		/*
3293 		 * Either the port is not online or this request was from
3294 		 * previous life, just abort the processing.
3295 		 */
3296 		cmd->state = QLA_TGT_STATE_PROCESSED;
3297 		ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3298 			"RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3299 			vha->flags.online, qla2x00_reset_active(vha),
3300 			cmd->reset_count, qpair->chip_reset);
3301 		res = 0;
3302 		goto out_unmap_unlock;
3303 	}
3304 
3305 	/* Does F/W have an IOCBs for this request */
3306 	res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3307 	if (unlikely(res))
3308 		goto out_unmap_unlock;
3309 
3310 	if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3311 		res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3312 	else
3313 		res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3314 	if (unlikely(res != 0)) {
3315 		qpair->req->cnt += full_req_cnt;
3316 		goto out_unmap_unlock;
3317 	}
3318 
3319 	pkt = (struct ctio7_to_24xx *)prm.pkt;
3320 
3321 	if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3322 		pkt->u.status0.flags |=
3323 		    cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3324 			CTIO7_FLAGS_STATUS_MODE_0);
3325 
3326 		if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3327 			qlt_load_data_segments(&prm);
3328 
3329 		if (prm.add_status_pkt == 0) {
3330 			if (xmit_type & QLA_TGT_XMIT_STATUS) {
3331 				pkt->u.status0.scsi_status =
3332 				    cpu_to_le16(prm.rq_result);
3333 				if (!cmd->edif)
3334 					pkt->u.status0.residual =
3335 						cpu_to_le32(prm.residual);
3336 
3337 				pkt->u.status0.flags |= cpu_to_le16(
3338 				    CTIO7_FLAGS_SEND_STATUS);
3339 				if (qlt_need_explicit_conf(cmd, 0)) {
3340 					pkt->u.status0.flags |=
3341 					    cpu_to_le16(
3342 						CTIO7_FLAGS_EXPLICIT_CONFORM |
3343 						CTIO7_FLAGS_CONFORM_REQ);
3344 				}
3345 			}
3346 
3347 		} else {
3348 			/*
3349 			 * We have already made sure that there is sufficient
3350 			 * amount of request entries to not drop HW lock in
3351 			 * req_pkt().
3352 			 */
3353 			struct ctio7_to_24xx *ctio =
3354 				(struct ctio7_to_24xx *)qlt_get_req_pkt(
3355 				    qpair->req);
3356 
3357 			ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3358 			    "Building additional status packet 0x%p.\n",
3359 			    ctio);
3360 
3361 			/*
3362 			 * T10Dif: ctio_crc2_to_fw overlay ontop of
3363 			 * ctio7_to_24xx
3364 			 */
3365 			memcpy(ctio, pkt, sizeof(*ctio));
3366 			/* reset back to CTIO7 */
3367 			ctio->entry_count = 1;
3368 			ctio->entry_type = CTIO_TYPE7;
3369 			ctio->dseg_count = 0;
3370 			ctio->u.status1.flags &= ~cpu_to_le16(
3371 			    CTIO7_FLAGS_DATA_IN);
3372 
3373 			/* Real finish is ctio_m1's finish */
3374 			pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3375 			pkt->u.status0.flags |= cpu_to_le16(
3376 			    CTIO7_FLAGS_DONT_RET_CTIO);
3377 
3378 			/* qlt_24xx_init_ctio_to_isp will correct
3379 			 * all neccessary fields that's part of CTIO7.
3380 			 * There should be no residual of CTIO-CRC2 data.
3381 			 */
3382 			qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3383 			    &prm);
3384 		}
3385 	} else
3386 		qlt_24xx_init_ctio_to_isp(pkt, &prm);
3387 
3388 
3389 	cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3390 	cmd->cmd_sent_to_fw = 1;
3391 	cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3392 
3393 	/* Memory Barrier */
3394 	wmb();
3395 	if (qpair->reqq_start_iocbs)
3396 		qpair->reqq_start_iocbs(qpair);
3397 	else
3398 		qla2x00_start_iocbs(vha, qpair->req);
3399 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3400 
3401 	return 0;
3402 
3403 out_unmap_unlock:
3404 	qlt_unmap_sg(vha, cmd);
3405 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3406 
3407 	return res;
3408 }
3409 EXPORT_SYMBOL(qlt_xmit_response);
3410 
qlt_rdy_to_xfer(struct qla_tgt_cmd * cmd)3411 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3412 {
3413 	struct ctio7_to_24xx *pkt;
3414 	struct scsi_qla_host *vha = cmd->vha;
3415 	struct qla_tgt *tgt = cmd->tgt;
3416 	struct qla_tgt_prm prm;
3417 	unsigned long flags = 0;
3418 	int res = 0;
3419 	struct qla_qpair *qpair = cmd->qpair;
3420 
3421 	memset(&prm, 0, sizeof(prm));
3422 	prm.cmd = cmd;
3423 	prm.tgt = tgt;
3424 	prm.sg = NULL;
3425 	prm.req_cnt = 1;
3426 
3427 	if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3428 	    (cmd->sess && cmd->sess->deleted)) {
3429 		/*
3430 		 * Either the port is not online or this request was from
3431 		 * previous life, just abort the processing.
3432 		 */
3433 		cmd->aborted = 1;
3434 		cmd->write_data_transferred = 0;
3435 		cmd->state = QLA_TGT_STATE_DATA_IN;
3436 		vha->hw->tgt.tgt_ops->handle_data(cmd);
3437 		ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3438 			"RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3439 			vha->flags.online, qla2x00_reset_active(vha),
3440 			cmd->reset_count, qpair->chip_reset);
3441 		return 0;
3442 	}
3443 
3444 	/* Calculate number of entries and segments required */
3445 	if (qlt_pci_map_calc_cnt(&prm) != 0)
3446 		return -EAGAIN;
3447 
3448 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3449 	/* Does F/W have an IOCBs for this request */
3450 	res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3451 	if (res != 0)
3452 		goto out_unlock_free_unmap;
3453 	if (cmd->se_cmd.prot_op)
3454 		res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3455 	else
3456 		res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3457 
3458 	if (unlikely(res != 0)) {
3459 		qpair->req->cnt += prm.req_cnt;
3460 		goto out_unlock_free_unmap;
3461 	}
3462 
3463 	pkt = (struct ctio7_to_24xx *)prm.pkt;
3464 	pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3465 	    CTIO7_FLAGS_STATUS_MODE_0);
3466 
3467 	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3468 		qlt_load_data_segments(&prm);
3469 
3470 	cmd->state = QLA_TGT_STATE_NEED_DATA;
3471 	cmd->cmd_sent_to_fw = 1;
3472 	cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3473 
3474 	/* Memory Barrier */
3475 	wmb();
3476 	if (qpair->reqq_start_iocbs)
3477 		qpair->reqq_start_iocbs(qpair);
3478 	else
3479 		qla2x00_start_iocbs(vha, qpair->req);
3480 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3481 
3482 	return res;
3483 
3484 out_unlock_free_unmap:
3485 	qlt_unmap_sg(vha, cmd);
3486 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3487 
3488 	return res;
3489 }
3490 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3491 
3492 
3493 /*
3494  * it is assumed either hardware_lock or qpair lock is held.
3495  */
3496 static void
qlt_handle_dif_error(struct qla_qpair * qpair,struct qla_tgt_cmd * cmd,struct ctio_crc_from_fw * sts)3497 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3498 	struct ctio_crc_from_fw *sts)
3499 {
3500 	uint8_t		*ap = &sts->actual_dif[0];
3501 	uint8_t		*ep = &sts->expected_dif[0];
3502 	uint64_t	lba = cmd->se_cmd.t_task_lba;
3503 	uint8_t scsi_status, sense_key, asc, ascq;
3504 	unsigned long flags;
3505 	struct scsi_qla_host *vha = cmd->vha;
3506 
3507 	cmd->trc_flags |= TRC_DIF_ERR;
3508 
3509 	cmd->a_guard   = get_unaligned_be16(ap + 0);
3510 	cmd->a_app_tag = get_unaligned_be16(ap + 2);
3511 	cmd->a_ref_tag = get_unaligned_be32(ap + 4);
3512 
3513 	cmd->e_guard   = get_unaligned_be16(ep + 0);
3514 	cmd->e_app_tag = get_unaligned_be16(ep + 2);
3515 	cmd->e_ref_tag = get_unaligned_be32(ep + 4);
3516 
3517 	ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3518 	    "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3519 
3520 	scsi_status = sense_key = asc = ascq = 0;
3521 
3522 	/* check appl tag */
3523 	if (cmd->e_app_tag != cmd->a_app_tag) {
3524 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3525 		    "App Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3526 		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3527 		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3528 		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3529 		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3530 
3531 		cmd->dif_err_code = DIF_ERR_APP;
3532 		scsi_status = SAM_STAT_CHECK_CONDITION;
3533 		sense_key = ABORTED_COMMAND;
3534 		asc = 0x10;
3535 		ascq = 0x2;
3536 	}
3537 
3538 	/* check ref tag */
3539 	if (cmd->e_ref_tag != cmd->a_ref_tag) {
3540 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3541 		    "Ref Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard[%x|%x] cmd=%p ox_id[%04x] ",
3542 		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3543 		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3544 		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3545 		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3546 
3547 		cmd->dif_err_code = DIF_ERR_REF;
3548 		scsi_status = SAM_STAT_CHECK_CONDITION;
3549 		sense_key = ABORTED_COMMAND;
3550 		asc = 0x10;
3551 		ascq = 0x3;
3552 		goto out;
3553 	}
3554 
3555 	/* check guard */
3556 	if (cmd->e_guard != cmd->a_guard) {
3557 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3558 		    "Guard ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3559 		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3560 		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3561 		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3562 		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3563 
3564 		cmd->dif_err_code = DIF_ERR_GRD;
3565 		scsi_status = SAM_STAT_CHECK_CONDITION;
3566 		sense_key = ABORTED_COMMAND;
3567 		asc = 0x10;
3568 		ascq = 0x1;
3569 	}
3570 out:
3571 	switch (cmd->state) {
3572 	case QLA_TGT_STATE_NEED_DATA:
3573 		/* handle_data will load DIF error code  */
3574 		cmd->state = QLA_TGT_STATE_DATA_IN;
3575 		vha->hw->tgt.tgt_ops->handle_data(cmd);
3576 		break;
3577 	default:
3578 		spin_lock_irqsave(&cmd->cmd_lock, flags);
3579 		if (cmd->aborted) {
3580 			spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3581 			vha->hw->tgt.tgt_ops->free_cmd(cmd);
3582 			break;
3583 		}
3584 		spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3585 
3586 		qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3587 		    ascq);
3588 		/* assume scsi status gets out on the wire.
3589 		 * Will not wait for completion.
3590 		 */
3591 		vha->hw->tgt.tgt_ops->free_cmd(cmd);
3592 		break;
3593 	}
3594 }
3595 
3596 /* If hardware_lock held on entry, might drop it, then reaquire */
3597 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
__qlt_send_term_imm_notif(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * ntfy)3598 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3599 	struct imm_ntfy_from_isp *ntfy)
3600 {
3601 	struct nack_to_isp *nack;
3602 	struct qla_hw_data *ha = vha->hw;
3603 	request_t *pkt;
3604 	int ret = 0;
3605 
3606 	ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3607 	    "Sending TERM ELS CTIO (ha=%p)\n", ha);
3608 
3609 	pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3610 	if (pkt == NULL) {
3611 		ql_dbg(ql_dbg_tgt, vha, 0xe080,
3612 		    "qla_target(%d): %s failed: unable to allocate "
3613 		    "request packet\n", vha->vp_idx, __func__);
3614 		return -ENOMEM;
3615 	}
3616 
3617 	pkt->entry_type = NOTIFY_ACK_TYPE;
3618 	pkt->entry_count = 1;
3619 	pkt->handle = QLA_TGT_SKIP_HANDLE;
3620 
3621 	nack = (struct nack_to_isp *)pkt;
3622 	nack->ox_id = ntfy->ox_id;
3623 
3624 	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3625 	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3626 		nack->u.isp24.flags = ntfy->u.isp24.flags &
3627 			cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3628 	}
3629 
3630 	/* terminate */
3631 	nack->u.isp24.flags |=
3632 		__constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3633 
3634 	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3635 	nack->u.isp24.status = ntfy->u.isp24.status;
3636 	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3637 	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3638 	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3639 	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3640 	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3641 	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3642 
3643 	qla2x00_start_iocbs(vha, vha->req);
3644 	return ret;
3645 }
3646 
qlt_send_term_imm_notif(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * imm,int ha_locked)3647 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3648 	struct imm_ntfy_from_isp *imm, int ha_locked)
3649 {
3650 	int rc;
3651 
3652 	WARN_ON_ONCE(!ha_locked);
3653 	rc = __qlt_send_term_imm_notif(vha, imm);
3654 	pr_debug("rc = %d\n", rc);
3655 }
3656 
3657 /*
3658  * If hardware_lock held on entry, might drop it, then reaquire
3659  * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3660  */
__qlt_send_term_exchange(struct qla_qpair * qpair,struct qla_tgt_cmd * cmd,struct atio_from_isp * atio)3661 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3662 	struct qla_tgt_cmd *cmd,
3663 	struct atio_from_isp *atio)
3664 {
3665 	struct scsi_qla_host *vha = qpair->vha;
3666 	struct ctio7_to_24xx *ctio24;
3667 	struct qla_hw_data *ha = vha->hw;
3668 	request_t *pkt;
3669 	int ret = 0;
3670 	uint16_t temp;
3671 
3672 	ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3673 
3674 	if (cmd)
3675 		vha = cmd->vha;
3676 
3677 	pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3678 	if (pkt == NULL) {
3679 		ql_dbg(ql_dbg_tgt, vha, 0xe050,
3680 		    "qla_target(%d): %s failed: unable to allocate "
3681 		    "request packet\n", vha->vp_idx, __func__);
3682 		return -ENOMEM;
3683 	}
3684 
3685 	if (cmd != NULL) {
3686 		if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3687 			ql_dbg(ql_dbg_tgt, vha, 0xe051,
3688 			    "qla_target(%d): Terminating cmd %p with "
3689 			    "incorrect state %d\n", vha->vp_idx, cmd,
3690 			    cmd->state);
3691 		} else
3692 			ret = 1;
3693 	}
3694 
3695 	qpair->tgt_counters.num_term_xchg_sent++;
3696 	pkt->entry_count = 1;
3697 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3698 
3699 	ctio24 = (struct ctio7_to_24xx *)pkt;
3700 	ctio24->entry_type = CTIO_TYPE7;
3701 	ctio24->nport_handle = cpu_to_le16(CTIO7_NHANDLE_UNRECOGNIZED);
3702 	ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3703 	ctio24->vp_index = vha->vp_idx;
3704 	ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3705 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3706 	temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3707 		CTIO7_FLAGS_TERMINATE;
3708 	ctio24->u.status1.flags = cpu_to_le16(temp);
3709 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3710 	ctio24->u.status1.ox_id = cpu_to_le16(temp);
3711 
3712 	/* Memory Barrier */
3713 	wmb();
3714 	if (qpair->reqq_start_iocbs)
3715 		qpair->reqq_start_iocbs(qpair);
3716 	else
3717 		qla2x00_start_iocbs(vha, qpair->req);
3718 	return ret;
3719 }
3720 
qlt_send_term_exchange(struct qla_qpair * qpair,struct qla_tgt_cmd * cmd,struct atio_from_isp * atio,int ha_locked,int ul_abort)3721 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3722 	struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3723 	int ul_abort)
3724 {
3725 	struct scsi_qla_host *vha;
3726 	unsigned long flags = 0;
3727 	int rc;
3728 
3729 	/* why use different vha? NPIV */
3730 	if (cmd)
3731 		vha = cmd->vha;
3732 	else
3733 		vha = qpair->vha;
3734 
3735 	if (ha_locked) {
3736 		rc = __qlt_send_term_exchange(qpair, cmd, atio);
3737 		if (rc == -ENOMEM)
3738 			qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3739 		goto done;
3740 	}
3741 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3742 	rc = __qlt_send_term_exchange(qpair, cmd, atio);
3743 	if (rc == -ENOMEM)
3744 		qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3745 
3746 done:
3747 	if (cmd && !ul_abort && !cmd->aborted) {
3748 		if (cmd->sg_mapped)
3749 			qlt_unmap_sg(vha, cmd);
3750 		vha->hw->tgt.tgt_ops->free_cmd(cmd);
3751 	}
3752 
3753 	if (!ha_locked)
3754 		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3755 
3756 	return;
3757 }
3758 
qlt_init_term_exchange(struct scsi_qla_host * vha)3759 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3760 {
3761 	struct list_head free_list;
3762 	struct qla_tgt_cmd *cmd, *tcmd;
3763 
3764 	vha->hw->tgt.leak_exchg_thresh_hold =
3765 	    (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3766 
3767 	cmd = tcmd = NULL;
3768 	if (!list_empty(&vha->hw->tgt.q_full_list)) {
3769 		INIT_LIST_HEAD(&free_list);
3770 		list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3771 
3772 		list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3773 			list_del(&cmd->cmd_list);
3774 			/* This cmd was never sent to TCM.  There is no need
3775 			 * to schedule free or call free_cmd
3776 			 */
3777 			qlt_free_cmd(cmd);
3778 			vha->hw->tgt.num_qfull_cmds_alloc--;
3779 		}
3780 	}
3781 	vha->hw->tgt.num_qfull_cmds_dropped = 0;
3782 }
3783 
qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host * vha)3784 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3785 {
3786 	uint32_t total_leaked;
3787 
3788 	total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3789 
3790 	if (vha->hw->tgt.leak_exchg_thresh_hold &&
3791 	    (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3792 
3793 		ql_dbg(ql_dbg_tgt, vha, 0xe079,
3794 		    "Chip reset due to exchange starvation: %d/%d.\n",
3795 		    total_leaked, vha->hw->cur_fw_xcb_count);
3796 
3797 		if (IS_P3P_TYPE(vha->hw))
3798 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3799 		else
3800 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3801 		qla2xxx_wake_dpc(vha);
3802 	}
3803 
3804 }
3805 
qlt_abort_cmd(struct qla_tgt_cmd * cmd)3806 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3807 {
3808 	struct qla_tgt *tgt = cmd->tgt;
3809 	struct scsi_qla_host *vha = tgt->vha;
3810 	struct se_cmd *se_cmd = &cmd->se_cmd;
3811 	unsigned long flags;
3812 
3813 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3814 	    "qla_target(%d): terminating exchange for aborted cmd=%p "
3815 	    "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3816 	    se_cmd->tag);
3817 
3818 	spin_lock_irqsave(&cmd->cmd_lock, flags);
3819 	if (cmd->aborted) {
3820 		if (cmd->sg_mapped)
3821 			qlt_unmap_sg(vha, cmd);
3822 
3823 		spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3824 		/*
3825 		 * It's normal to see 2 calls in this path:
3826 		 *  1) XFER Rdy completion + CMD_T_ABORT
3827 		 *  2) TCM TMR - drain_state_list
3828 		 */
3829 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3830 		    "multiple abort. %p transport_state %x, t_state %x, "
3831 		    "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3832 		    cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3833 		return -EIO;
3834 	}
3835 	cmd->aborted = 1;
3836 	cmd->trc_flags |= TRC_ABORT;
3837 	spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3838 
3839 	qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3840 	return 0;
3841 }
3842 EXPORT_SYMBOL(qlt_abort_cmd);
3843 
qlt_free_cmd(struct qla_tgt_cmd * cmd)3844 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3845 {
3846 	struct fc_port *sess = cmd->sess;
3847 
3848 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3849 	    "%s: se_cmd[%p] ox_id %04x\n",
3850 	    __func__, &cmd->se_cmd,
3851 	    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3852 
3853 	BUG_ON(cmd->cmd_in_wq);
3854 
3855 	if (!cmd->q_full)
3856 		qlt_decr_num_pend_cmds(cmd->vha);
3857 
3858 	BUG_ON(cmd->sg_mapped);
3859 	cmd->jiffies_at_free = get_jiffies_64();
3860 
3861 	if (!sess || !sess->se_sess) {
3862 		WARN_ON(1);
3863 		return;
3864 	}
3865 	cmd->jiffies_at_free = get_jiffies_64();
3866 	cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3867 }
3868 EXPORT_SYMBOL(qlt_free_cmd);
3869 
3870 /*
3871  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3872  */
qlt_term_ctio_exchange(struct qla_qpair * qpair,void * ctio,struct qla_tgt_cmd * cmd,uint32_t status)3873 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3874 	struct qla_tgt_cmd *cmd, uint32_t status)
3875 {
3876 	int term = 0;
3877 	struct scsi_qla_host *vha = qpair->vha;
3878 
3879 	if (cmd->se_cmd.prot_op)
3880 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3881 		    "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3882 		    "se_cmd=%p tag[%x] op %#x/%s",
3883 		     cmd->lba, cmd->lba,
3884 		     cmd->num_blks, &cmd->se_cmd,
3885 		     cmd->atio.u.isp24.exchange_addr,
3886 		     cmd->se_cmd.prot_op,
3887 		     prot_op_str(cmd->se_cmd.prot_op));
3888 
3889 	if (ctio != NULL) {
3890 		struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3891 
3892 		term = !(c->flags &
3893 		    cpu_to_le16(OF_TERM_EXCH));
3894 	} else
3895 		term = 1;
3896 
3897 	if (term)
3898 		qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3899 
3900 	return term;
3901 }
3902 
3903 
3904 /* ha->hardware_lock supposed to be held on entry */
qlt_ctio_to_cmd(struct scsi_qla_host * vha,struct rsp_que * rsp,uint32_t handle,void * ctio)3905 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3906 	struct rsp_que *rsp, uint32_t handle, void *ctio)
3907 {
3908 	void *cmd = NULL;
3909 	struct req_que *req;
3910 	int qid = GET_QID(handle);
3911 	uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3912 
3913 	if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3914 		return NULL;
3915 
3916 	if (qid == rsp->req->id) {
3917 		req = rsp->req;
3918 	} else if (vha->hw->req_q_map[qid]) {
3919 		ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3920 		    "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3921 		    vha->vp_idx, rsp->id, handle);
3922 		req = vha->hw->req_q_map[qid];
3923 	} else {
3924 		return NULL;
3925 	}
3926 
3927 	h &= QLA_CMD_HANDLE_MASK;
3928 
3929 	if (h != QLA_TGT_NULL_HANDLE) {
3930 		if (unlikely(h >= req->num_outstanding_cmds)) {
3931 			ql_dbg(ql_dbg_tgt, vha, 0xe052,
3932 			    "qla_target(%d): Wrong handle %x received\n",
3933 			    vha->vp_idx, handle);
3934 			return NULL;
3935 		}
3936 
3937 		cmd = req->outstanding_cmds[h];
3938 		if (unlikely(cmd == NULL)) {
3939 			ql_dbg(ql_dbg_async, vha, 0xe053,
3940 			    "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3941 				vha->vp_idx, handle, req->id, rsp->id);
3942 			return NULL;
3943 		}
3944 		req->outstanding_cmds[h] = NULL;
3945 	} else if (ctio != NULL) {
3946 		/* We can't get loop ID from CTIO7 */
3947 		ql_dbg(ql_dbg_tgt, vha, 0xe054,
3948 		    "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3949 		    "support NULL handles\n", vha->vp_idx);
3950 		return NULL;
3951 	}
3952 
3953 	return cmd;
3954 }
3955 
3956 /*
3957  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3958  */
qlt_do_ctio_completion(struct scsi_qla_host * vha,struct rsp_que * rsp,uint32_t handle,uint32_t status,void * ctio)3959 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3960     struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3961 {
3962 	struct qla_hw_data *ha = vha->hw;
3963 	struct se_cmd *se_cmd;
3964 	struct qla_tgt_cmd *cmd;
3965 	struct qla_qpair *qpair = rsp->qpair;
3966 
3967 	if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3968 		/* That could happen only in case of an error/reset/abort */
3969 		if (status != CTIO_SUCCESS) {
3970 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3971 			    "Intermediate CTIO received"
3972 			    " (status %x)\n", status);
3973 		}
3974 		return;
3975 	}
3976 
3977 	cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3978 	if (cmd == NULL)
3979 		return;
3980 
3981 	if ((le16_to_cpu(((struct ctio7_from_24xx *)ctio)->flags) & CTIO7_FLAGS_DATA_OUT) &&
3982 	    cmd->sess) {
3983 		qlt_chk_edif_rx_sa_delete_pending(vha, cmd->sess,
3984 		    (struct ctio7_from_24xx *)ctio);
3985 	}
3986 
3987 	se_cmd = &cmd->se_cmd;
3988 	cmd->cmd_sent_to_fw = 0;
3989 
3990 	qlt_unmap_sg(vha, cmd);
3991 
3992 	if (unlikely(status != CTIO_SUCCESS)) {
3993 		switch (status & 0xFFFF) {
3994 		case CTIO_INVALID_RX_ID:
3995 			if (printk_ratelimit())
3996 				dev_info(&vha->hw->pdev->dev,
3997 				    "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
3998 				    vha->vp_idx, cmd->atio.u.isp24.attr,
3999 				    ((cmd->ctio_flags >> 9) & 0xf),
4000 				    cmd->ctio_flags);
4001 
4002 			break;
4003 		case CTIO_LIP_RESET:
4004 		case CTIO_TARGET_RESET:
4005 		case CTIO_ABORTED:
4006 			/* driver request abort via Terminate exchange */
4007 		case CTIO_TIMEOUT:
4008 			/* They are OK */
4009 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4010 			    "qla_target(%d): CTIO with "
4011 			    "status %#x received, state %x, se_cmd %p, "
4012 			    "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4013 			    "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4014 			    status, cmd->state, se_cmd);
4015 			break;
4016 
4017 		case CTIO_PORT_LOGGED_OUT:
4018 		case CTIO_PORT_UNAVAILABLE:
4019 		{
4020 			int logged_out =
4021 				(status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4022 
4023 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
4024 			    "qla_target(%d): CTIO with %s status %x "
4025 			    "received (state %x, se_cmd %p)\n", vha->vp_idx,
4026 			    logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
4027 			    status, cmd->state, se_cmd);
4028 
4029 			if (logged_out && cmd->sess) {
4030 				/*
4031 				 * Session is already logged out, but we need
4032 				 * to notify initiator, who's not aware of this
4033 				 */
4034 				cmd->sess->send_els_logo = 1;
4035 				ql_dbg(ql_dbg_disc, vha, 0x20f8,
4036 				    "%s %d %8phC post del sess\n",
4037 				    __func__, __LINE__, cmd->sess->port_name);
4038 
4039 				qlt_schedule_sess_for_deletion(cmd->sess);
4040 			}
4041 			break;
4042 		}
4043 		case CTIO_DIF_ERROR: {
4044 			struct ctio_crc_from_fw *crc =
4045 				(struct ctio_crc_from_fw *)ctio;
4046 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4047 			    "qla_target(%d): CTIO with DIF_ERROR status %x "
4048 			    "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4049 			    "expect_dif[0x%llx]\n",
4050 			    vha->vp_idx, status, cmd->state, se_cmd,
4051 			    *((u64 *)&crc->actual_dif[0]),
4052 			    *((u64 *)&crc->expected_dif[0]));
4053 
4054 			qlt_handle_dif_error(qpair, cmd, ctio);
4055 			return;
4056 		}
4057 
4058 		case CTIO_FAST_AUTH_ERR:
4059 		case CTIO_FAST_INCOMP_PAD_LEN:
4060 		case CTIO_FAST_INVALID_REQ:
4061 		case CTIO_FAST_SPI_ERR:
4062 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4063 			    "qla_target(%d): CTIO with EDIF error status 0x%x received (state %x, se_cmd %p\n",
4064 			    vha->vp_idx, status, cmd->state, se_cmd);
4065 			break;
4066 
4067 		default:
4068 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4069 			    "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4070 			    vha->vp_idx, status, cmd->state, se_cmd);
4071 			break;
4072 		}
4073 
4074 
4075 		/* "cmd->aborted" means
4076 		 * cmd is already aborted/terminated, we don't
4077 		 * need to terminate again.  The exchange is already
4078 		 * cleaned up/freed at FW level.  Just cleanup at driver
4079 		 * level.
4080 		 */
4081 		if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4082 		    (!cmd->aborted)) {
4083 			cmd->trc_flags |= TRC_CTIO_ERR;
4084 			if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4085 				return;
4086 		}
4087 	}
4088 
4089 	if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4090 		cmd->trc_flags |= TRC_CTIO_DONE;
4091 	} else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4092 		cmd->state = QLA_TGT_STATE_DATA_IN;
4093 
4094 		if (status == CTIO_SUCCESS)
4095 			cmd->write_data_transferred = 1;
4096 
4097 		ha->tgt.tgt_ops->handle_data(cmd);
4098 		return;
4099 	} else if (cmd->aborted) {
4100 		cmd->trc_flags |= TRC_CTIO_ABORTED;
4101 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4102 		  "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4103 	} else {
4104 		cmd->trc_flags |= TRC_CTIO_STRANGE;
4105 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4106 		    "qla_target(%d): A command in state (%d) should "
4107 		    "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4108 	}
4109 
4110 	if (unlikely(status != CTIO_SUCCESS) &&
4111 		!cmd->aborted) {
4112 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4113 		dump_stack();
4114 	}
4115 
4116 	ha->tgt.tgt_ops->free_cmd(cmd);
4117 }
4118 
qlt_get_fcp_task_attr(struct scsi_qla_host * vha,uint8_t task_codes)4119 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4120 	uint8_t task_codes)
4121 {
4122 	int fcp_task_attr;
4123 
4124 	switch (task_codes) {
4125 	case ATIO_SIMPLE_QUEUE:
4126 		fcp_task_attr = TCM_SIMPLE_TAG;
4127 		break;
4128 	case ATIO_HEAD_OF_QUEUE:
4129 		fcp_task_attr = TCM_HEAD_TAG;
4130 		break;
4131 	case ATIO_ORDERED_QUEUE:
4132 		fcp_task_attr = TCM_ORDERED_TAG;
4133 		break;
4134 	case ATIO_ACA_QUEUE:
4135 		fcp_task_attr = TCM_ACA_TAG;
4136 		break;
4137 	case ATIO_UNTAGGED:
4138 		fcp_task_attr = TCM_SIMPLE_TAG;
4139 		break;
4140 	default:
4141 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4142 		    "qla_target: unknown task code %x, use ORDERED instead\n",
4143 		    task_codes);
4144 		fcp_task_attr = TCM_ORDERED_TAG;
4145 		break;
4146 	}
4147 
4148 	return fcp_task_attr;
4149 }
4150 
4151 /*
4152  * Process context for I/O path into tcm_qla2xxx code
4153  */
__qlt_do_work(struct qla_tgt_cmd * cmd)4154 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4155 {
4156 	scsi_qla_host_t *vha = cmd->vha;
4157 	struct qla_hw_data *ha = vha->hw;
4158 	struct fc_port *sess = cmd->sess;
4159 	struct atio_from_isp *atio = &cmd->atio;
4160 	unsigned char *cdb;
4161 	unsigned long flags;
4162 	uint32_t data_length;
4163 	int ret, fcp_task_attr, data_dir, bidi = 0;
4164 	struct qla_qpair *qpair = cmd->qpair;
4165 
4166 	cmd->cmd_in_wq = 0;
4167 	cmd->trc_flags |= TRC_DO_WORK;
4168 
4169 	if (cmd->aborted) {
4170 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4171 		    "cmd with tag %u is aborted\n",
4172 		    cmd->atio.u.isp24.exchange_addr);
4173 		goto out_term;
4174 	}
4175 
4176 	spin_lock_init(&cmd->cmd_lock);
4177 	cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4178 	cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4179 
4180 	if (atio->u.isp24.fcp_cmnd.rddata &&
4181 	    atio->u.isp24.fcp_cmnd.wrdata) {
4182 		bidi = 1;
4183 		data_dir = DMA_TO_DEVICE;
4184 	} else if (atio->u.isp24.fcp_cmnd.rddata)
4185 		data_dir = DMA_FROM_DEVICE;
4186 	else if (atio->u.isp24.fcp_cmnd.wrdata)
4187 		data_dir = DMA_TO_DEVICE;
4188 	else
4189 		data_dir = DMA_NONE;
4190 
4191 	fcp_task_attr = qlt_get_fcp_task_attr(vha,
4192 	    atio->u.isp24.fcp_cmnd.task_attr);
4193 	data_length = get_datalen_for_atio(atio);
4194 
4195 	ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4196 				          fcp_task_attr, data_dir, bidi);
4197 	if (ret != 0)
4198 		goto out_term;
4199 	/*
4200 	 * Drop extra session reference from qlt_handle_cmd_for_atio().
4201 	 */
4202 	ha->tgt.tgt_ops->put_sess(sess);
4203 	return;
4204 
4205 out_term:
4206 	ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4207 	/*
4208 	 * cmd has not sent to target yet, so pass NULL as the second
4209 	 * argument to qlt_send_term_exchange() and free the memory here.
4210 	 */
4211 	cmd->trc_flags |= TRC_DO_WORK_ERR;
4212 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4213 	qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4214 
4215 	qlt_decr_num_pend_cmds(vha);
4216 	cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4217 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4218 
4219 	ha->tgt.tgt_ops->put_sess(sess);
4220 }
4221 
qlt_do_work(struct work_struct * work)4222 static void qlt_do_work(struct work_struct *work)
4223 {
4224 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4225 	scsi_qla_host_t *vha = cmd->vha;
4226 	unsigned long flags;
4227 
4228 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4229 	list_del(&cmd->cmd_list);
4230 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4231 
4232 	__qlt_do_work(cmd);
4233 }
4234 
qlt_clr_qp_table(struct scsi_qla_host * vha)4235 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4236 {
4237 	unsigned long flags;
4238 	struct qla_hw_data *ha = vha->hw;
4239 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4240 	void *node;
4241 	u64 key = 0;
4242 
4243 	ql_log(ql_log_info, vha, 0x706c,
4244 	    "User update Number of Active Qpairs %d\n",
4245 	    ha->tgt.num_act_qpairs);
4246 
4247 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4248 
4249 	btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4250 		btree_remove64(&tgt->lun_qpair_map, key);
4251 
4252 	ha->base_qpair->lun_cnt = 0;
4253 	for (key = 0; key < ha->max_qpairs; key++)
4254 		if (ha->queue_pair_map[key])
4255 			ha->queue_pair_map[key]->lun_cnt = 0;
4256 
4257 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4258 }
4259 
qlt_assign_qpair(struct scsi_qla_host * vha,struct qla_tgt_cmd * cmd)4260 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4261 	struct qla_tgt_cmd *cmd)
4262 {
4263 	struct qla_qpair *qpair, *qp;
4264 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4265 	struct qla_qpair_hint *h;
4266 
4267 	if (vha->flags.qpairs_available) {
4268 		h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4269 		if (unlikely(!h)) {
4270 			/* spread lun to qpair ratio evently */
4271 			int lcnt = 0, rc;
4272 			struct scsi_qla_host *base_vha =
4273 				pci_get_drvdata(vha->hw->pdev);
4274 
4275 			qpair = vha->hw->base_qpair;
4276 			if (qpair->lun_cnt == 0) {
4277 				qpair->lun_cnt++;
4278 				h = qla_qpair_to_hint(tgt, qpair);
4279 				BUG_ON(!h);
4280 				rc = btree_insert64(&tgt->lun_qpair_map,
4281 					cmd->unpacked_lun, h, GFP_ATOMIC);
4282 				if (rc) {
4283 					qpair->lun_cnt--;
4284 					ql_log(ql_log_info, vha, 0xd037,
4285 					    "Unable to insert lun %llx into lun_qpair_map\n",
4286 					    cmd->unpacked_lun);
4287 				}
4288 				goto out;
4289 			} else {
4290 				lcnt = qpair->lun_cnt;
4291 			}
4292 
4293 			h = NULL;
4294 			list_for_each_entry(qp, &base_vha->qp_list,
4295 			    qp_list_elem) {
4296 				if (qp->lun_cnt == 0) {
4297 					qp->lun_cnt++;
4298 					h = qla_qpair_to_hint(tgt, qp);
4299 					BUG_ON(!h);
4300 					rc = btree_insert64(&tgt->lun_qpair_map,
4301 					    cmd->unpacked_lun, h, GFP_ATOMIC);
4302 					if (rc) {
4303 						qp->lun_cnt--;
4304 						ql_log(ql_log_info, vha, 0xd038,
4305 							"Unable to insert lun %llx into lun_qpair_map\n",
4306 							cmd->unpacked_lun);
4307 					}
4308 					qpair = qp;
4309 					goto out;
4310 				} else {
4311 					if (qp->lun_cnt < lcnt) {
4312 						lcnt = qp->lun_cnt;
4313 						qpair = qp;
4314 						continue;
4315 					}
4316 				}
4317 			}
4318 			BUG_ON(!qpair);
4319 			qpair->lun_cnt++;
4320 			h = qla_qpair_to_hint(tgt, qpair);
4321 			BUG_ON(!h);
4322 			rc = btree_insert64(&tgt->lun_qpair_map,
4323 				cmd->unpacked_lun, h, GFP_ATOMIC);
4324 			if (rc) {
4325 				qpair->lun_cnt--;
4326 				ql_log(ql_log_info, vha, 0xd039,
4327 				   "Unable to insert lun %llx into lun_qpair_map\n",
4328 				   cmd->unpacked_lun);
4329 			}
4330 		}
4331 	} else {
4332 		h = &tgt->qphints[0];
4333 	}
4334 out:
4335 	cmd->qpair = h->qpair;
4336 	cmd->se_cmd.cpuid = h->cpuid;
4337 }
4338 
qlt_get_tag(scsi_qla_host_t * vha,struct fc_port * sess,struct atio_from_isp * atio)4339 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4340 				       struct fc_port *sess,
4341 				       struct atio_from_isp *atio)
4342 {
4343 	struct qla_tgt_cmd *cmd;
4344 
4345 	cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4346 	if (!cmd)
4347 		return NULL;
4348 
4349 	cmd->cmd_type = TYPE_TGT_CMD;
4350 	memcpy(&cmd->atio, atio, sizeof(*atio));
4351 	INIT_LIST_HEAD(&cmd->sess_cmd_list);
4352 	cmd->state = QLA_TGT_STATE_NEW;
4353 	cmd->tgt = vha->vha_tgt.qla_tgt;
4354 	qlt_incr_num_pend_cmds(vha);
4355 	cmd->vha = vha;
4356 	cmd->sess = sess;
4357 	cmd->loop_id = sess->loop_id;
4358 	cmd->conf_compl_supported = sess->conf_compl_supported;
4359 
4360 	cmd->trc_flags = 0;
4361 	cmd->jiffies_at_alloc = get_jiffies_64();
4362 
4363 	cmd->unpacked_lun = scsilun_to_int(
4364 	    (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4365 	qlt_assign_qpair(vha, cmd);
4366 	cmd->reset_count = vha->hw->base_qpair->chip_reset;
4367 	cmd->vp_idx = vha->vp_idx;
4368 	cmd->edif = sess->edif.enable;
4369 
4370 	return cmd;
4371 }
4372 
4373 /* ha->hardware_lock supposed to be held on entry */
qlt_handle_cmd_for_atio(struct scsi_qla_host * vha,struct atio_from_isp * atio)4374 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4375 	struct atio_from_isp *atio)
4376 {
4377 	struct qla_hw_data *ha = vha->hw;
4378 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4379 	struct fc_port *sess;
4380 	struct qla_tgt_cmd *cmd;
4381 	unsigned long flags;
4382 	port_id_t id;
4383 
4384 	if (unlikely(tgt->tgt_stop)) {
4385 		ql_dbg(ql_dbg_io, vha, 0x3061,
4386 		    "New command while device %p is shutting down\n", tgt);
4387 		return -ENODEV;
4388 	}
4389 
4390 	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4391 	if (IS_SW_RESV_ADDR(id))
4392 		return -EBUSY;
4393 
4394 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4395 	if (unlikely(!sess))
4396 		return -EFAULT;
4397 
4398 	/* Another WWN used to have our s_id. Our PLOGI scheduled its
4399 	 * session deletion, but it's still in sess_del_work wq */
4400 	if (sess->deleted) {
4401 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4402 		    "New command while old session %p is being deleted\n",
4403 		    sess);
4404 		return -EFAULT;
4405 	}
4406 
4407 	/*
4408 	 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4409 	 */
4410 	if (!kref_get_unless_zero(&sess->sess_kref)) {
4411 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4412 		    "%s: kref_get fail, %8phC oxid %x \n",
4413 		    __func__, sess->port_name,
4414 		     be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4415 		return -EFAULT;
4416 	}
4417 
4418 	cmd = qlt_get_tag(vha, sess, atio);
4419 	if (!cmd) {
4420 		ql_dbg(ql_dbg_io, vha, 0x3062,
4421 		    "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4422 		ha->tgt.tgt_ops->put_sess(sess);
4423 		return -EBUSY;
4424 	}
4425 
4426 	cmd->cmd_in_wq = 1;
4427 	cmd->trc_flags |= TRC_NEW_CMD;
4428 
4429 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4430 	list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4431 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4432 
4433 	INIT_WORK(&cmd->work, qlt_do_work);
4434 	if (vha->flags.qpairs_available) {
4435 		queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4436 	} else if (ha->msix_count) {
4437 		if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4438 			queue_work(qla_tgt_wq, &cmd->work);
4439 		else
4440 			queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4441 			    &cmd->work);
4442 	} else {
4443 		queue_work(qla_tgt_wq, &cmd->work);
4444 	}
4445 
4446 	return 0;
4447 }
4448 
4449 /* ha->hardware_lock supposed to be held on entry */
qlt_issue_task_mgmt(struct fc_port * sess,u64 lun,int fn,void * iocb,int flags)4450 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4451 	int fn, void *iocb, int flags)
4452 {
4453 	struct scsi_qla_host *vha = sess->vha;
4454 	struct qla_hw_data *ha = vha->hw;
4455 	struct qla_tgt_mgmt_cmd *mcmd;
4456 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4457 	struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4458 
4459 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4460 	if (!mcmd) {
4461 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4462 		    "qla_target(%d): Allocation of management "
4463 		    "command failed, some commands and their data could "
4464 		    "leak\n", vha->vp_idx);
4465 		return -ENOMEM;
4466 	}
4467 	memset(mcmd, 0, sizeof(*mcmd));
4468 	mcmd->sess = sess;
4469 
4470 	if (iocb) {
4471 		memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4472 		    sizeof(mcmd->orig_iocb.imm_ntfy));
4473 	}
4474 	mcmd->tmr_func = fn;
4475 	mcmd->flags = flags;
4476 	mcmd->reset_count = ha->base_qpair->chip_reset;
4477 	mcmd->qpair = h->qpair;
4478 	mcmd->vha = vha;
4479 	mcmd->se_cmd.cpuid = h->cpuid;
4480 	mcmd->unpacked_lun = lun;
4481 
4482 	switch (fn) {
4483 	case QLA_TGT_LUN_RESET:
4484 	case QLA_TGT_CLEAR_TS:
4485 	case QLA_TGT_ABORT_TS:
4486 		abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4487 		fallthrough;
4488 	case QLA_TGT_CLEAR_ACA:
4489 		h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4490 		mcmd->qpair = h->qpair;
4491 		mcmd->se_cmd.cpuid = h->cpuid;
4492 		break;
4493 
4494 	case QLA_TGT_TARGET_RESET:
4495 	case QLA_TGT_NEXUS_LOSS_SESS:
4496 	case QLA_TGT_NEXUS_LOSS:
4497 	case QLA_TGT_ABORT_ALL:
4498 	default:
4499 		/* no-op */
4500 		break;
4501 	}
4502 
4503 	INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4504 	queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4505 	    &mcmd->work);
4506 
4507 	return 0;
4508 }
4509 
4510 /* ha->hardware_lock supposed to be held on entry */
qlt_handle_task_mgmt(struct scsi_qla_host * vha,void * iocb)4511 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4512 {
4513 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4514 	struct qla_hw_data *ha = vha->hw;
4515 	struct fc_port *sess;
4516 	u64 unpacked_lun;
4517 	int fn;
4518 	unsigned long flags;
4519 
4520 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4521 
4522 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4523 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4524 	    a->u.isp24.fcp_hdr.s_id);
4525 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4526 
4527 	unpacked_lun =
4528 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4529 
4530 	if (sess == NULL || sess->deleted)
4531 		return -EFAULT;
4532 
4533 	return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4534 }
4535 
4536 /* ha->hardware_lock supposed to be held on entry */
__qlt_abort_task(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb,struct fc_port * sess)4537 static int __qlt_abort_task(struct scsi_qla_host *vha,
4538 	struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4539 {
4540 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4541 	struct qla_hw_data *ha = vha->hw;
4542 	struct qla_tgt_mgmt_cmd *mcmd;
4543 	u64 unpacked_lun;
4544 	int rc;
4545 
4546 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4547 	if (mcmd == NULL) {
4548 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4549 		    "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4550 		    vha->vp_idx, __func__);
4551 		return -ENOMEM;
4552 	}
4553 	memset(mcmd, 0, sizeof(*mcmd));
4554 
4555 	mcmd->sess = sess;
4556 	memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4557 	    sizeof(mcmd->orig_iocb.imm_ntfy));
4558 
4559 	unpacked_lun =
4560 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4561 	mcmd->reset_count = ha->base_qpair->chip_reset;
4562 	mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4563 	mcmd->qpair = ha->base_qpair;
4564 
4565 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4566 	    le16_to_cpu(iocb->u.isp2x.seq_id));
4567 	if (rc != 0) {
4568 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4569 		    "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4570 		    vha->vp_idx, rc);
4571 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4572 		return -EFAULT;
4573 	}
4574 
4575 	return 0;
4576 }
4577 
4578 /* ha->hardware_lock supposed to be held on entry */
qlt_abort_task(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb)4579 static int qlt_abort_task(struct scsi_qla_host *vha,
4580 	struct imm_ntfy_from_isp *iocb)
4581 {
4582 	struct qla_hw_data *ha = vha->hw;
4583 	struct fc_port *sess;
4584 	int loop_id;
4585 	unsigned long flags;
4586 
4587 	loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4588 
4589 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4590 	sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4591 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4592 
4593 	if (sess == NULL) {
4594 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4595 		    "qla_target(%d): task abort for unexisting "
4596 		    "session\n", vha->vp_idx);
4597 		return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4598 		    QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4599 	}
4600 
4601 	return __qlt_abort_task(vha, iocb, sess);
4602 }
4603 
qlt_logo_completion_handler(fc_port_t * fcport,int rc)4604 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4605 {
4606 	if (rc != MBS_COMMAND_COMPLETE) {
4607 		ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4608 			"%s: se_sess %p / sess %p from"
4609 			" port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4610 			" LOGO failed: %#x\n",
4611 			__func__,
4612 			fcport->se_sess,
4613 			fcport,
4614 			fcport->port_name, fcport->loop_id,
4615 			fcport->d_id.b.domain, fcport->d_id.b.area,
4616 			fcport->d_id.b.al_pa, rc);
4617 	}
4618 
4619 	fcport->logout_completed = 1;
4620 }
4621 
4622 /*
4623 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4624 *
4625 * Schedules sessions with matching port_id/loop_id but different wwn for
4626 * deletion. Returns existing session with matching wwn if present.
4627 * Null otherwise.
4628 */
4629 struct fc_port *
qlt_find_sess_invalidate_other(scsi_qla_host_t * vha,uint64_t wwn,port_id_t port_id,uint16_t loop_id,struct fc_port ** conflict_sess)4630 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4631     port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4632 {
4633 	struct fc_port *sess = NULL, *other_sess;
4634 	uint64_t other_wwn;
4635 
4636 	*conflict_sess = NULL;
4637 
4638 	list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4639 
4640 		other_wwn = wwn_to_u64(other_sess->port_name);
4641 
4642 		if (wwn == other_wwn) {
4643 			WARN_ON(sess);
4644 			sess = other_sess;
4645 			continue;
4646 		}
4647 
4648 		/* find other sess with nport_id collision */
4649 		if (port_id.b24 == other_sess->d_id.b24) {
4650 			if (loop_id != other_sess->loop_id) {
4651 				ql_dbg(ql_dbg_disc, vha, 0x1000c,
4652 				    "Invalidating sess %p loop_id %d wwn %llx.\n",
4653 				    other_sess, other_sess->loop_id, other_wwn);
4654 
4655 				/*
4656 				 * logout_on_delete is set by default, but another
4657 				 * session that has the same s_id/loop_id combo
4658 				 * might have cleared it when requested this session
4659 				 * deletion, so don't touch it
4660 				 */
4661 				qlt_schedule_sess_for_deletion(other_sess);
4662 			} else {
4663 				/*
4664 				 * Another wwn used to have our s_id/loop_id
4665 				 * kill the session, but don't free the loop_id
4666 				 */
4667 				ql_dbg(ql_dbg_disc, vha, 0xf01b,
4668 				    "Invalidating sess %p loop_id %d wwn %llx.\n",
4669 				    other_sess, other_sess->loop_id, other_wwn);
4670 
4671 				other_sess->keep_nport_handle = 1;
4672 				if (other_sess->disc_state != DSC_DELETED)
4673 					*conflict_sess = other_sess;
4674 				qlt_schedule_sess_for_deletion(other_sess);
4675 			}
4676 			continue;
4677 		}
4678 
4679 		/* find other sess with nport handle collision */
4680 		if ((loop_id == other_sess->loop_id) &&
4681 			(loop_id != FC_NO_LOOP_ID)) {
4682 			ql_dbg(ql_dbg_disc, vha, 0x1000d,
4683 			       "Invalidating sess %p loop_id %d wwn %llx.\n",
4684 			       other_sess, other_sess->loop_id, other_wwn);
4685 
4686 			/* Same loop_id but different s_id
4687 			 * Ok to kill and logout */
4688 			qlt_schedule_sess_for_deletion(other_sess);
4689 		}
4690 	}
4691 
4692 	return sess;
4693 }
4694 
4695 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
abort_cmds_for_s_id(struct scsi_qla_host * vha,port_id_t * s_id)4696 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4697 {
4698 	struct qla_tgt_sess_op *op;
4699 	struct qla_tgt_cmd *cmd;
4700 	uint32_t key;
4701 	int count = 0;
4702 	unsigned long flags;
4703 
4704 	key = (((u32)s_id->b.domain << 16) |
4705 	       ((u32)s_id->b.area   <<  8) |
4706 	       ((u32)s_id->b.al_pa));
4707 
4708 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4709 	list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4710 		uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4711 
4712 		if (op_key == key) {
4713 			op->aborted = true;
4714 			count++;
4715 		}
4716 	}
4717 
4718 	list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4719 		uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4720 
4721 		if (cmd_key == key) {
4722 			cmd->aborted = 1;
4723 			count++;
4724 		}
4725 	}
4726 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4727 
4728 	return count;
4729 }
4730 
qlt_handle_login(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb)4731 static int qlt_handle_login(struct scsi_qla_host *vha,
4732     struct imm_ntfy_from_isp *iocb)
4733 {
4734 	struct fc_port *sess = NULL, *conflict_sess = NULL;
4735 	uint64_t wwn;
4736 	port_id_t port_id;
4737 	uint16_t loop_id, wd3_lo;
4738 	int res = 0;
4739 	struct qlt_plogi_ack_t *pla;
4740 	unsigned long flags;
4741 
4742 	lockdep_assert_held(&vha->hw->hardware_lock);
4743 
4744 	wwn = wwn_to_u64(iocb->u.isp24.port_name);
4745 
4746 	port_id.b.domain = iocb->u.isp24.port_id[2];
4747 	port_id.b.area   = iocb->u.isp24.port_id[1];
4748 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4749 	port_id.b.rsvd_1 = 0;
4750 
4751 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4752 
4753 	/* Mark all stale commands sitting in qla_tgt_wq for deletion */
4754 	abort_cmds_for_s_id(vha, &port_id);
4755 
4756 	if (wwn) {
4757 		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4758 		sess = qlt_find_sess_invalidate_other(vha, wwn,
4759 		    port_id, loop_id, &conflict_sess);
4760 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4761 	} else {
4762 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4763 		    "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4764 		    __func__, __LINE__, loop_id, port_id.b24);
4765 		qlt_send_term_imm_notif(vha, iocb, 1);
4766 		goto out;
4767 	}
4768 
4769 	if (IS_SW_RESV_ADDR(port_id)) {
4770 		res = 1;
4771 		goto out;
4772 	}
4773 
4774 	if (vha->hw->flags.edif_enabled &&
4775 	    !(vha->e_dbell.db_flags & EDB_ACTIVE) &&
4776 	    iocb->u.isp24.status_subcode == ELS_PLOGI &&
4777 	    !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4778 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4779 			"%s %d Term INOT due to app not available lid=%d, NportID %06X ",
4780 			__func__, __LINE__, loop_id, port_id.b24);
4781 		qlt_send_term_imm_notif(vha, iocb, 1);
4782 		goto out;
4783 	}
4784 
4785 	if (vha->hw->flags.edif_enabled) {
4786 		if (DBELL_INACTIVE(vha)) {
4787 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4788 			       "%s %d Term INOT due to app not started lid=%d, NportID %06X ",
4789 			       __func__, __LINE__, loop_id, port_id.b24);
4790 			qlt_send_term_imm_notif(vha, iocb, 1);
4791 			goto out;
4792 		} else if (iocb->u.isp24.status_subcode == ELS_PLOGI &&
4793 			   !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4794 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4795 			       "%s %d Term INOT due to unsecure lid=%d, NportID %06X ",
4796 			       __func__, __LINE__, loop_id, port_id.b24);
4797 			qlt_send_term_imm_notif(vha, iocb, 1);
4798 			goto out;
4799 		}
4800 	}
4801 
4802 	pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4803 	if (!pla) {
4804 		ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4805 		    "%s %d %8phC Term INOT due to mem alloc fail",
4806 		    __func__, __LINE__,
4807 		    iocb->u.isp24.port_name);
4808 		qlt_send_term_imm_notif(vha, iocb, 1);
4809 		goto out;
4810 	}
4811 
4812 	if (conflict_sess) {
4813 		conflict_sess->login_gen++;
4814 		qlt_plogi_ack_link(vha, pla, conflict_sess,
4815 		    QLT_PLOGI_LINK_CONFLICT);
4816 	}
4817 
4818 	if (!sess) {
4819 		pla->ref_count++;
4820 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4821 		    "%s %d %8phC post new sess\n",
4822 		    __func__, __LINE__, iocb->u.isp24.port_name);
4823 		if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4824 			qla24xx_post_newsess_work(vha, &port_id,
4825 			    iocb->u.isp24.port_name,
4826 			    iocb->u.isp24.u.plogi.node_name,
4827 			    pla, 0);
4828 		else
4829 			qla24xx_post_newsess_work(vha, &port_id,
4830 			    iocb->u.isp24.port_name, NULL,
4831 			    pla, 0);
4832 
4833 		goto out;
4834 	}
4835 
4836 	if (sess->disc_state == DSC_UPD_FCPORT) {
4837 		u16 sec;
4838 
4839 		/*
4840 		 * Remote port registration is still going on from
4841 		 * previous login. Allow it to finish before we
4842 		 * accept the new login.
4843 		 */
4844 		sess->next_disc_state = DSC_DELETE_PEND;
4845 		sec = jiffies_to_msecs(jiffies -
4846 		    sess->jiffies_at_registration) / 1000;
4847 		if (sess->sec_since_registration < sec && sec &&
4848 		    !(sec % 5)) {
4849 			sess->sec_since_registration = sec;
4850 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4851 			    "%s %8phC - Slow Rport registration (%d Sec)\n",
4852 			    __func__, sess->port_name, sec);
4853 		}
4854 
4855 		if (!conflict_sess) {
4856 			list_del(&pla->list);
4857 			kmem_cache_free(qla_tgt_plogi_cachep, pla);
4858 		}
4859 
4860 		qlt_send_term_imm_notif(vha, iocb, 1);
4861 		goto out;
4862 	}
4863 
4864 	qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4865 	sess->d_id = port_id;
4866 	sess->login_gen++;
4867 	sess->loop_id = loop_id;
4868 
4869 	if (iocb->u.isp24.status_subcode == ELS_PLOGI) {
4870 		/* remote port has assigned Port ID */
4871 		if (N2N_TOPO(vha->hw) && fcport_is_bigger(sess))
4872 			vha->d_id = sess->d_id;
4873 
4874 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4875 		    "%s %8phC - send port online\n",
4876 		    __func__, sess->port_name);
4877 
4878 		qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
4879 		    sess->d_id.b24);
4880 	}
4881 
4882 	if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4883 		sess->fw_login_state = DSC_LS_PRLI_PEND;
4884 		sess->local = 0;
4885 		sess->loop_id = loop_id;
4886 		sess->d_id = port_id;
4887 		sess->fw_login_state = DSC_LS_PRLI_PEND;
4888 		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4889 
4890 		if (wd3_lo & BIT_7)
4891 			sess->conf_compl_supported = 1;
4892 
4893 		if ((wd3_lo & BIT_4) == 0)
4894 			sess->port_type = FCT_INITIATOR;
4895 		else
4896 			sess->port_type = FCT_TARGET;
4897 
4898 	} else
4899 		sess->fw_login_state = DSC_LS_PLOGI_PEND;
4900 
4901 
4902 	ql_dbg(ql_dbg_disc, vha, 0x20f9,
4903 	    "%s %d %8phC  DS %d\n",
4904 	    __func__, __LINE__, sess->port_name, sess->disc_state);
4905 
4906 	switch (sess->disc_state) {
4907 	case DSC_DELETED:
4908 	case DSC_LOGIN_PEND:
4909 		qlt_plogi_ack_unref(vha, pla);
4910 		break;
4911 
4912 	default:
4913 		/*
4914 		 * Under normal circumstances we want to release nport handle
4915 		 * during LOGO process to avoid nport handle leaks inside FW.
4916 		 * The exception is when LOGO is done while another PLOGI with
4917 		 * the same nport handle is waiting as might be the case here.
4918 		 * Note: there is always a possibily of a race where session
4919 		 * deletion has already started for other reasons (e.g. ACL
4920 		 * removal) and now PLOGI arrives:
4921 		 * 1. if PLOGI arrived in FW after nport handle has been freed,
4922 		 *    FW must have assigned this PLOGI a new/same handle and we
4923 		 *    can proceed ACK'ing it as usual when session deletion
4924 		 *    completes.
4925 		 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4926 		 *    bit reached it, the handle has now been released. We'll
4927 		 *    get an error when we ACK this PLOGI. Nothing will be sent
4928 		 *    back to initiator. Initiator should eventually retry
4929 		 *    PLOGI and situation will correct itself.
4930 		 */
4931 		sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4932 		    (sess->d_id.b24 == port_id.b24));
4933 
4934 		ql_dbg(ql_dbg_disc, vha, 0x20f9,
4935 		    "%s %d %8phC post del sess\n",
4936 		    __func__, __LINE__, sess->port_name);
4937 
4938 
4939 		qlt_schedule_sess_for_deletion(sess);
4940 		break;
4941 	}
4942 out:
4943 	return res;
4944 }
4945 
4946 /*
4947  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4948  */
qlt_24xx_handle_els(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb)4949 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4950 	struct imm_ntfy_from_isp *iocb)
4951 {
4952 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4953 	struct qla_hw_data *ha = vha->hw;
4954 	struct fc_port *sess = NULL, *conflict_sess = NULL;
4955 	uint64_t wwn;
4956 	port_id_t port_id;
4957 	uint16_t loop_id;
4958 	uint16_t wd3_lo;
4959 	int res = 0;
4960 	unsigned long flags;
4961 
4962 	lockdep_assert_held(&ha->hardware_lock);
4963 
4964 	wwn = wwn_to_u64(iocb->u.isp24.port_name);
4965 
4966 	port_id.b.domain = iocb->u.isp24.port_id[2];
4967 	port_id.b.area   = iocb->u.isp24.port_id[1];
4968 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4969 	port_id.b.rsvd_1 = 0;
4970 
4971 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4972 
4973 	ql_dbg(ql_dbg_disc, vha, 0xf026,
4974 	    "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4975 	    vha->vp_idx, iocb->u.isp24.port_id[2],
4976 		iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4977 		   iocb->u.isp24.status_subcode, loop_id,
4978 		iocb->u.isp24.port_name);
4979 
4980 	/* res = 1 means ack at the end of thread
4981 	 * res = 0 means ack async/later.
4982 	 */
4983 	switch (iocb->u.isp24.status_subcode) {
4984 	case ELS_PLOGI:
4985 		res = qlt_handle_login(vha, iocb);
4986 		break;
4987 
4988 	case ELS_PRLI:
4989 		if (N2N_TOPO(ha)) {
4990 			sess = qla2x00_find_fcport_by_wwpn(vha,
4991 			    iocb->u.isp24.port_name, 1);
4992 
4993 			if (vha->hw->flags.edif_enabled && sess &&
4994 			    (!(sess->flags & FCF_FCSP_DEVICE) ||
4995 			     !sess->edif.authok)) {
4996 				ql_dbg(ql_dbg_disc, vha, 0xffff,
4997 				       "%s %d %8phC Term PRLI due to unauthorize PRLI\n",
4998 				       __func__, __LINE__, iocb->u.isp24.port_name);
4999 				qlt_send_term_imm_notif(vha, iocb, 1);
5000 				break;
5001 			}
5002 
5003 			if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5004 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5005 				    "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5006 				    __func__, __LINE__,
5007 				    iocb->u.isp24.port_name);
5008 				qlt_send_term_imm_notif(vha, iocb, 1);
5009 				break;
5010 			}
5011 
5012 			res = qlt_handle_login(vha, iocb);
5013 			break;
5014 		}
5015 
5016 		if (IS_SW_RESV_ADDR(port_id)) {
5017 			res = 1;
5018 			break;
5019 		}
5020 
5021 		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5022 
5023 		if (wwn) {
5024 			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5025 			sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5026 				loop_id, &conflict_sess);
5027 			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5028 		}
5029 
5030 		if (conflict_sess) {
5031 			switch (conflict_sess->disc_state) {
5032 			case DSC_DELETED:
5033 			case DSC_DELETE_PEND:
5034 				break;
5035 			default:
5036 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5037 				    "PRLI with conflicting sess %p port %8phC\n",
5038 				    conflict_sess, conflict_sess->port_name);
5039 				conflict_sess->fw_login_state =
5040 				    DSC_LS_PORT_UNAVAIL;
5041 				qlt_send_term_imm_notif(vha, iocb, 1);
5042 				res = 0;
5043 				break;
5044 			}
5045 		}
5046 
5047 		if (sess != NULL) {
5048 			bool delete = false;
5049 			int sec;
5050 
5051 			if (vha->hw->flags.edif_enabled && sess &&
5052 			    (!(sess->flags & FCF_FCSP_DEVICE) ||
5053 			     !sess->edif.authok)) {
5054 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5055 				       "%s %d %8phC Term PRLI due to unauthorize prli\n",
5056 				       __func__, __LINE__, iocb->u.isp24.port_name);
5057 				qlt_send_term_imm_notif(vha, iocb, 1);
5058 				break;
5059 			}
5060 
5061 			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5062 			switch (sess->fw_login_state) {
5063 			case DSC_LS_PLOGI_PEND:
5064 			case DSC_LS_PLOGI_COMP:
5065 			case DSC_LS_PRLI_COMP:
5066 				break;
5067 			default:
5068 				delete = true;
5069 				break;
5070 			}
5071 
5072 			switch (sess->disc_state) {
5073 			case DSC_UPD_FCPORT:
5074 				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5075 				    flags);
5076 
5077 				sec = jiffies_to_msecs(jiffies -
5078 				    sess->jiffies_at_registration)/1000;
5079 				if (sess->sec_since_registration < sec && sec &&
5080 				    !(sec % 5)) {
5081 					sess->sec_since_registration = sec;
5082 					ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5083 					    "%s %8phC : Slow Rport registration(%d Sec)\n",
5084 					    __func__, sess->port_name, sec);
5085 				}
5086 				qlt_send_term_imm_notif(vha, iocb, 1);
5087 				return 0;
5088 
5089 			case DSC_LOGIN_PEND:
5090 			case DSC_GPDB:
5091 			case DSC_LOGIN_COMPLETE:
5092 			case DSC_ADISC:
5093 				delete = false;
5094 				break;
5095 			default:
5096 				break;
5097 			}
5098 
5099 			if (delete) {
5100 				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5101 				    flags);
5102 				/*
5103 				 * Impatient initiator sent PRLI before last
5104 				 * PLOGI could finish. Will force him to re-try,
5105 				 * while last one finishes.
5106 				 */
5107 				ql_log(ql_log_warn, sess->vha, 0xf095,
5108 				    "sess %p PRLI received, before plogi ack.\n",
5109 				    sess);
5110 				qlt_send_term_imm_notif(vha, iocb, 1);
5111 				res = 0;
5112 				break;
5113 			}
5114 
5115 			/*
5116 			 * This shouldn't happen under normal circumstances,
5117 			 * since we have deleted the old session during PLOGI
5118 			 */
5119 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5120 			    "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5121 			    sess->loop_id, sess, iocb->u.isp24.nport_handle);
5122 
5123 			sess->local = 0;
5124 			sess->loop_id = loop_id;
5125 			sess->d_id = port_id;
5126 			sess->fw_login_state = DSC_LS_PRLI_PEND;
5127 
5128 			if (wd3_lo & BIT_7)
5129 				sess->conf_compl_supported = 1;
5130 
5131 			if ((wd3_lo & BIT_4) == 0)
5132 				sess->port_type = FCT_INITIATOR;
5133 			else
5134 				sess->port_type = FCT_TARGET;
5135 
5136 			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5137 		}
5138 		res = 1; /* send notify ack */
5139 
5140 		/* Make session global (not used in fabric mode) */
5141 		if (ha->current_topology != ISP_CFG_F) {
5142 			if (sess) {
5143 				ql_dbg(ql_dbg_disc, vha, 0x20fa,
5144 				    "%s %d %8phC post nack\n",
5145 				    __func__, __LINE__, sess->port_name);
5146 				qla24xx_post_nack_work(vha, sess, iocb,
5147 					SRB_NACK_PRLI);
5148 				res = 0;
5149 			} else {
5150 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5151 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5152 				qla2xxx_wake_dpc(vha);
5153 			}
5154 		} else {
5155 			if (sess) {
5156 				ql_dbg(ql_dbg_disc, vha, 0x20fb,
5157 				    "%s %d %8phC post nack\n",
5158 				    __func__, __LINE__, sess->port_name);
5159 				qla24xx_post_nack_work(vha, sess, iocb,
5160 					SRB_NACK_PRLI);
5161 				res = 0;
5162 			}
5163 		}
5164 		break;
5165 
5166 	case ELS_TPRLO:
5167 		if (le16_to_cpu(iocb->u.isp24.flags) &
5168 			NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5169 			loop_id = 0xFFFF;
5170 			qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5171 			res = 1;
5172 			break;
5173 		}
5174 		fallthrough;
5175 	case ELS_LOGO:
5176 	case ELS_PRLO:
5177 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5178 		sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5179 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5180 
5181 		if (sess) {
5182 			sess->login_gen++;
5183 			sess->fw_login_state = DSC_LS_LOGO_PEND;
5184 			sess->logo_ack_needed = 1;
5185 			memcpy(sess->iocb, iocb, IOCB_SIZE);
5186 		}
5187 
5188 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5189 
5190 		ql_dbg(ql_dbg_disc, vha, 0x20fc,
5191 		    "%s: logo %llx res %d sess %p ",
5192 		    __func__, wwn, res, sess);
5193 		if (res == 0) {
5194 			/*
5195 			 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5196 			 * for LOGO_ACK & sess delete
5197 			 */
5198 			BUG_ON(!sess);
5199 			res = 0;
5200 		} else {
5201 			/* cmd did not go to upper layer. */
5202 			if (sess) {
5203 				qlt_schedule_sess_for_deletion(sess);
5204 				res = 0;
5205 			}
5206 			/* else logo will be ack */
5207 		}
5208 		break;
5209 	case ELS_PDISC:
5210 	case ELS_ADISC:
5211 	{
5212 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5213 
5214 		if (tgt->link_reinit_iocb_pending) {
5215 			qlt_send_notify_ack(ha->base_qpair,
5216 			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5217 			tgt->link_reinit_iocb_pending = 0;
5218 		}
5219 
5220 		sess = qla2x00_find_fcport_by_wwpn(vha,
5221 		    iocb->u.isp24.port_name, 1);
5222 		if (sess) {
5223 			ql_dbg(ql_dbg_disc, vha, 0x20fd,
5224 				"sess %p lid %d|%d DS %d LS %d\n",
5225 				sess, sess->loop_id, loop_id,
5226 				sess->disc_state, sess->fw_login_state);
5227 		}
5228 
5229 		res = 1; /* send notify ack */
5230 		break;
5231 	}
5232 
5233 	case ELS_FLOGI:	/* should never happen */
5234 	default:
5235 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5236 		    "qla_target(%d): Unsupported ELS command %x "
5237 		    "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5238 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5239 		break;
5240 	}
5241 
5242 	ql_dbg(ql_dbg_disc, vha, 0xf026,
5243 	    "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5244 	    vha->vp_idx, iocb->u.isp24.status_subcode, res);
5245 
5246 	return res;
5247 }
5248 
5249 /*
5250  * ha->hardware_lock supposed to be held on entry.
5251  * Might drop it, then reacquire.
5252  */
qlt_handle_imm_notify(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb)5253 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5254 	struct imm_ntfy_from_isp *iocb)
5255 {
5256 	struct qla_hw_data *ha = vha->hw;
5257 	uint32_t add_flags = 0;
5258 	int send_notify_ack = 1;
5259 	uint16_t status;
5260 
5261 	lockdep_assert_held(&ha->hardware_lock);
5262 
5263 	status = le16_to_cpu(iocb->u.isp2x.status);
5264 	switch (status) {
5265 	case IMM_NTFY_LIP_RESET:
5266 	{
5267 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5268 		    "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5269 		    vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5270 		    iocb->u.isp24.status_subcode);
5271 
5272 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5273 			send_notify_ack = 0;
5274 		break;
5275 	}
5276 
5277 	case IMM_NTFY_LIP_LINK_REINIT:
5278 	{
5279 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5280 
5281 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5282 		    "qla_target(%d): LINK REINIT (loop %#x, "
5283 		    "subcode %x)\n", vha->vp_idx,
5284 		    le16_to_cpu(iocb->u.isp24.nport_handle),
5285 		    iocb->u.isp24.status_subcode);
5286 		if (tgt->link_reinit_iocb_pending) {
5287 			qlt_send_notify_ack(ha->base_qpair,
5288 			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5289 		}
5290 		memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5291 		tgt->link_reinit_iocb_pending = 1;
5292 		/*
5293 		 * QLogic requires to wait after LINK REINIT for possible
5294 		 * PDISC or ADISC ELS commands
5295 		 */
5296 		send_notify_ack = 0;
5297 		break;
5298 	}
5299 
5300 	case IMM_NTFY_PORT_LOGOUT:
5301 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5302 		    "qla_target(%d): Port logout (loop "
5303 		    "%#x, subcode %x)\n", vha->vp_idx,
5304 		    le16_to_cpu(iocb->u.isp24.nport_handle),
5305 		    iocb->u.isp24.status_subcode);
5306 
5307 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5308 			send_notify_ack = 0;
5309 		/* The sessions will be cleared in the callback, if needed */
5310 		break;
5311 
5312 	case IMM_NTFY_GLBL_TPRLO:
5313 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5314 		    "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5315 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5316 			send_notify_ack = 0;
5317 		/* The sessions will be cleared in the callback, if needed */
5318 		break;
5319 
5320 	case IMM_NTFY_PORT_CONFIG:
5321 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5322 		    "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5323 		    status);
5324 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5325 			send_notify_ack = 0;
5326 		/* The sessions will be cleared in the callback, if needed */
5327 		break;
5328 
5329 	case IMM_NTFY_GLBL_LOGO:
5330 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5331 		    "qla_target(%d): Link failure detected\n",
5332 		    vha->vp_idx);
5333 		/* I_T nexus loss */
5334 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5335 			send_notify_ack = 0;
5336 		break;
5337 
5338 	case IMM_NTFY_IOCB_OVERFLOW:
5339 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5340 		    "qla_target(%d): Cannot provide requested "
5341 		    "capability (IOCB overflowed the immediate notify "
5342 		    "resource count)\n", vha->vp_idx);
5343 		break;
5344 
5345 	case IMM_NTFY_ABORT_TASK:
5346 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5347 		    "qla_target(%d): Abort Task (S %08x I %#x -> "
5348 		    "L %#x)\n", vha->vp_idx,
5349 		    le16_to_cpu(iocb->u.isp2x.seq_id),
5350 		    GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5351 		    le16_to_cpu(iocb->u.isp2x.lun));
5352 		if (qlt_abort_task(vha, iocb) == 0)
5353 			send_notify_ack = 0;
5354 		break;
5355 
5356 	case IMM_NTFY_RESOURCE:
5357 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5358 		    "qla_target(%d): Out of resources, host %ld\n",
5359 		    vha->vp_idx, vha->host_no);
5360 		break;
5361 
5362 	case IMM_NTFY_MSG_RX:
5363 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5364 		    "qla_target(%d): Immediate notify task %x\n",
5365 		    vha->vp_idx, iocb->u.isp2x.task_flags);
5366 		break;
5367 
5368 	case IMM_NTFY_ELS:
5369 		if (qlt_24xx_handle_els(vha, iocb) == 0)
5370 			send_notify_ack = 0;
5371 		break;
5372 	default:
5373 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5374 		    "qla_target(%d): Received unknown immediate "
5375 		    "notify status %x\n", vha->vp_idx, status);
5376 		break;
5377 	}
5378 
5379 	if (send_notify_ack)
5380 		qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5381 		    0, 0);
5382 }
5383 
5384 /*
5385  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5386  * This function sends busy to ISP 2xxx or 24xx.
5387  */
__qlt_send_busy(struct qla_qpair * qpair,struct atio_from_isp * atio,uint16_t status)5388 static int __qlt_send_busy(struct qla_qpair *qpair,
5389 	struct atio_from_isp *atio, uint16_t status)
5390 {
5391 	struct scsi_qla_host *vha = qpair->vha;
5392 	struct ctio7_to_24xx *ctio24;
5393 	struct qla_hw_data *ha = vha->hw;
5394 	request_t *pkt;
5395 	struct fc_port *sess = NULL;
5396 	unsigned long flags;
5397 	u16 temp;
5398 	port_id_t id;
5399 
5400 	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5401 
5402 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5403 	sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5404 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5405 	if (!sess) {
5406 		qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5407 		return 0;
5408 	}
5409 	/* Sending marker isn't necessary, since we called from ISR */
5410 
5411 	pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5412 	if (!pkt) {
5413 		ql_dbg(ql_dbg_io, vha, 0x3063,
5414 		    "qla_target(%d): %s failed: unable to allocate "
5415 		    "request packet", vha->vp_idx, __func__);
5416 		return -ENOMEM;
5417 	}
5418 
5419 	qpair->tgt_counters.num_q_full_sent++;
5420 	pkt->entry_count = 1;
5421 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5422 
5423 	ctio24 = (struct ctio7_to_24xx *)pkt;
5424 	ctio24->entry_type = CTIO_TYPE7;
5425 	ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5426 	ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5427 	ctio24->vp_index = vha->vp_idx;
5428 	ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5429 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5430 	temp = (atio->u.isp24.attr << 9) |
5431 		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5432 		CTIO7_FLAGS_DONT_RET_CTIO;
5433 	ctio24->u.status1.flags = cpu_to_le16(temp);
5434 	/*
5435 	 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5436 	 * if the explicit conformation is used.
5437 	 */
5438 	ctio24->u.status1.ox_id =
5439 		cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5440 	ctio24->u.status1.scsi_status = cpu_to_le16(status);
5441 
5442 	ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5443 
5444 	if (ctio24->u.status1.residual != 0)
5445 		ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5446 
5447 	/* Memory Barrier */
5448 	wmb();
5449 	if (qpair->reqq_start_iocbs)
5450 		qpair->reqq_start_iocbs(qpair);
5451 	else
5452 		qla2x00_start_iocbs(vha, qpair->req);
5453 	return 0;
5454 }
5455 
5456 /*
5457  * This routine is used to allocate a command for either a QFull condition
5458  * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5459  * out previously.
5460  */
5461 static void
qlt_alloc_qfull_cmd(struct scsi_qla_host * vha,struct atio_from_isp * atio,uint16_t status,int qfull)5462 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5463 	struct atio_from_isp *atio, uint16_t status, int qfull)
5464 {
5465 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5466 	struct qla_hw_data *ha = vha->hw;
5467 	struct fc_port *sess;
5468 	struct qla_tgt_cmd *cmd;
5469 	unsigned long flags;
5470 
5471 	if (unlikely(tgt->tgt_stop)) {
5472 		ql_dbg(ql_dbg_io, vha, 0x300a,
5473 			"New command while device %p is shutting down\n", tgt);
5474 		return;
5475 	}
5476 
5477 	if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5478 		vha->hw->tgt.num_qfull_cmds_dropped++;
5479 		if (vha->hw->tgt.num_qfull_cmds_dropped >
5480 			vha->qla_stats.stat_max_qfull_cmds_dropped)
5481 			vha->qla_stats.stat_max_qfull_cmds_dropped =
5482 				vha->hw->tgt.num_qfull_cmds_dropped;
5483 
5484 		ql_dbg(ql_dbg_io, vha, 0x3068,
5485 			"qla_target(%d): %s: QFull CMD dropped[%d]\n",
5486 			vha->vp_idx, __func__,
5487 			vha->hw->tgt.num_qfull_cmds_dropped);
5488 
5489 		qlt_chk_exch_leak_thresh_hold(vha);
5490 		return;
5491 	}
5492 
5493 	sess = ha->tgt.tgt_ops->find_sess_by_s_id
5494 		(vha, atio->u.isp24.fcp_hdr.s_id);
5495 	if (!sess)
5496 		return;
5497 
5498 	cmd = ha->tgt.tgt_ops->get_cmd(sess);
5499 	if (!cmd) {
5500 		ql_dbg(ql_dbg_io, vha, 0x3009,
5501 			"qla_target(%d): %s: Allocation of cmd failed\n",
5502 			vha->vp_idx, __func__);
5503 
5504 		vha->hw->tgt.num_qfull_cmds_dropped++;
5505 		if (vha->hw->tgt.num_qfull_cmds_dropped >
5506 			vha->qla_stats.stat_max_qfull_cmds_dropped)
5507 			vha->qla_stats.stat_max_qfull_cmds_dropped =
5508 				vha->hw->tgt.num_qfull_cmds_dropped;
5509 
5510 		qlt_chk_exch_leak_thresh_hold(vha);
5511 		return;
5512 	}
5513 
5514 	qlt_incr_num_pend_cmds(vha);
5515 	INIT_LIST_HEAD(&cmd->cmd_list);
5516 	memcpy(&cmd->atio, atio, sizeof(*atio));
5517 
5518 	cmd->tgt = vha->vha_tgt.qla_tgt;
5519 	cmd->vha = vha;
5520 	cmd->reset_count = ha->base_qpair->chip_reset;
5521 	cmd->q_full = 1;
5522 	cmd->qpair = ha->base_qpair;
5523 
5524 	if (qfull) {
5525 		cmd->q_full = 1;
5526 		/* NOTE: borrowing the state field to carry the status */
5527 		cmd->state = status;
5528 	} else
5529 		cmd->term_exchg = 1;
5530 
5531 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5532 	list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5533 
5534 	vha->hw->tgt.num_qfull_cmds_alloc++;
5535 	if (vha->hw->tgt.num_qfull_cmds_alloc >
5536 		vha->qla_stats.stat_max_qfull_cmds_alloc)
5537 		vha->qla_stats.stat_max_qfull_cmds_alloc =
5538 			vha->hw->tgt.num_qfull_cmds_alloc;
5539 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5540 }
5541 
5542 int
qlt_free_qfull_cmds(struct qla_qpair * qpair)5543 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5544 {
5545 	struct scsi_qla_host *vha = qpair->vha;
5546 	struct qla_hw_data *ha = vha->hw;
5547 	unsigned long flags;
5548 	struct qla_tgt_cmd *cmd, *tcmd;
5549 	struct list_head free_list, q_full_list;
5550 	int rc = 0;
5551 
5552 	if (list_empty(&ha->tgt.q_full_list))
5553 		return 0;
5554 
5555 	INIT_LIST_HEAD(&free_list);
5556 	INIT_LIST_HEAD(&q_full_list);
5557 
5558 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5559 	if (list_empty(&ha->tgt.q_full_list)) {
5560 		spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5561 		return 0;
5562 	}
5563 
5564 	list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5565 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5566 
5567 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5568 	list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5569 		if (cmd->q_full)
5570 			/* cmd->state is a borrowed field to hold status */
5571 			rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5572 		else if (cmd->term_exchg)
5573 			rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5574 
5575 		if (rc == -ENOMEM)
5576 			break;
5577 
5578 		if (cmd->q_full)
5579 			ql_dbg(ql_dbg_io, vha, 0x3006,
5580 			    "%s: busy sent for ox_id[%04x]\n", __func__,
5581 			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5582 		else if (cmd->term_exchg)
5583 			ql_dbg(ql_dbg_io, vha, 0x3007,
5584 			    "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5585 			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5586 		else
5587 			ql_dbg(ql_dbg_io, vha, 0x3008,
5588 			    "%s: Unexpected cmd in QFull list %p\n", __func__,
5589 			    cmd);
5590 
5591 		list_move_tail(&cmd->cmd_list, &free_list);
5592 
5593 		/* piggy back on hardware_lock for protection */
5594 		vha->hw->tgt.num_qfull_cmds_alloc--;
5595 	}
5596 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5597 
5598 	cmd = NULL;
5599 
5600 	list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5601 		list_del(&cmd->cmd_list);
5602 		/* This cmd was never sent to TCM.  There is no need
5603 		 * to schedule free or call free_cmd
5604 		 */
5605 		qlt_free_cmd(cmd);
5606 	}
5607 
5608 	if (!list_empty(&q_full_list)) {
5609 		spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5610 		list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5611 		spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5612 	}
5613 
5614 	return rc;
5615 }
5616 
5617 static void
qlt_send_busy(struct qla_qpair * qpair,struct atio_from_isp * atio,uint16_t status)5618 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5619     uint16_t status)
5620 {
5621 	int rc = 0;
5622 	struct scsi_qla_host *vha = qpair->vha;
5623 
5624 	rc = __qlt_send_busy(qpair, atio, status);
5625 	if (rc == -ENOMEM)
5626 		qlt_alloc_qfull_cmd(vha, atio, status, 1);
5627 }
5628 
5629 static int
qlt_chk_qfull_thresh_hold(struct scsi_qla_host * vha,struct qla_qpair * qpair,struct atio_from_isp * atio,uint8_t ha_locked)5630 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5631 	struct atio_from_isp *atio, uint8_t ha_locked)
5632 {
5633 	struct qla_hw_data *ha = vha->hw;
5634 	unsigned long flags;
5635 
5636 	if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5637 		return 0;
5638 
5639 	if (!ha_locked)
5640 		spin_lock_irqsave(&ha->hardware_lock, flags);
5641 	qlt_send_busy(qpair, atio, qla_sam_status);
5642 	if (!ha_locked)
5643 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
5644 
5645 	return 1;
5646 }
5647 
5648 /* ha->hardware_lock supposed to be held on entry */
5649 /* called via callback from qla2xxx */
qlt_24xx_atio_pkt(struct scsi_qla_host * vha,struct atio_from_isp * atio,uint8_t ha_locked)5650 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5651 	struct atio_from_isp *atio, uint8_t ha_locked)
5652 {
5653 	struct qla_hw_data *ha = vha->hw;
5654 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5655 	int rc;
5656 	unsigned long flags = 0;
5657 
5658 	if (unlikely(tgt == NULL)) {
5659 		ql_dbg(ql_dbg_tgt, vha, 0x3064,
5660 		    "ATIO pkt, but no tgt (ha %p)", ha);
5661 		return;
5662 	}
5663 	/*
5664 	 * In tgt_stop mode we also should allow all requests to pass.
5665 	 * Otherwise, some commands can stuck.
5666 	 */
5667 
5668 	tgt->atio_irq_cmd_count++;
5669 
5670 	switch (atio->u.raw.entry_type) {
5671 	case ATIO_TYPE7:
5672 		if (unlikely(atio->u.isp24.exchange_addr ==
5673 			     cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5674 			ql_dbg(ql_dbg_io, vha, 0x3065,
5675 			    "qla_target(%d): ATIO_TYPE7 "
5676 			    "received with UNKNOWN exchange address, "
5677 			    "sending QUEUE_FULL\n", vha->vp_idx);
5678 			if (!ha_locked)
5679 				spin_lock_irqsave(&ha->hardware_lock, flags);
5680 			qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5681 			if (!ha_locked)
5682 				spin_unlock_irqrestore(&ha->hardware_lock,
5683 				    flags);
5684 			break;
5685 		}
5686 
5687 		if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5688 			rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5689 			    atio, ha_locked);
5690 			if (rc != 0) {
5691 				tgt->atio_irq_cmd_count--;
5692 				return;
5693 			}
5694 			rc = qlt_handle_cmd_for_atio(vha, atio);
5695 		} else {
5696 			rc = qlt_handle_task_mgmt(vha, atio);
5697 		}
5698 		if (unlikely(rc != 0)) {
5699 			if (!ha_locked)
5700 				spin_lock_irqsave(&ha->hardware_lock, flags);
5701 			switch (rc) {
5702 			case -ENODEV:
5703 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5704 				    "qla_target: Unable to send command to target\n");
5705 				break;
5706 			case -EBADF:
5707 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5708 				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5709 				qlt_send_term_exchange(ha->base_qpair, NULL,
5710 				    atio, 1, 0);
5711 				break;
5712 			case -EBUSY:
5713 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5714 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5715 				    vha->vp_idx);
5716 				qlt_send_busy(ha->base_qpair, atio,
5717 				    tc_sam_status);
5718 				break;
5719 			default:
5720 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5721 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5722 				    vha->vp_idx);
5723 				qlt_send_busy(ha->base_qpair, atio,
5724 				    qla_sam_status);
5725 				break;
5726 			}
5727 			if (!ha_locked)
5728 				spin_unlock_irqrestore(&ha->hardware_lock,
5729 				    flags);
5730 		}
5731 		break;
5732 
5733 	case IMMED_NOTIFY_TYPE:
5734 	{
5735 		if (unlikely(atio->u.isp2x.entry_status != 0)) {
5736 			ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5737 			    "qla_target(%d): Received ATIO packet %x "
5738 			    "with error status %x\n", vha->vp_idx,
5739 			    atio->u.raw.entry_type,
5740 			    atio->u.isp2x.entry_status);
5741 			break;
5742 		}
5743 		ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5744 
5745 		if (!ha_locked)
5746 			spin_lock_irqsave(&ha->hardware_lock, flags);
5747 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5748 		if (!ha_locked)
5749 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
5750 		break;
5751 	}
5752 
5753 	default:
5754 		ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5755 		    "qla_target(%d): Received unknown ATIO atio "
5756 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5757 		break;
5758 	}
5759 
5760 	tgt->atio_irq_cmd_count--;
5761 }
5762 
5763 /*
5764  * qpair lock is assume to be held
5765  * rc = 0 : send terminate & abts respond
5766  * rc != 0: do not send term & abts respond
5767  */
qlt_chk_unresolv_exchg(struct scsi_qla_host * vha,struct qla_qpair * qpair,struct abts_resp_from_24xx_fw * entry)5768 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5769     struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5770 {
5771 	struct qla_hw_data *ha = vha->hw;
5772 	int rc = 0;
5773 
5774 	/*
5775 	 * Detect unresolved exchange. If the same ABTS is unable
5776 	 * to terminate an existing command and the same ABTS loops
5777 	 * between FW & Driver, then force FW dump. Under 1 jiff,
5778 	 * we should see multiple loops.
5779 	 */
5780 	if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5781 	    qpair->retry_term_jiff == jiffies) {
5782 		/* found existing exchange */
5783 		qpair->retry_term_cnt++;
5784 		if (qpair->retry_term_cnt >= 5) {
5785 			rc = -EIO;
5786 			qpair->retry_term_cnt = 0;
5787 			ql_log(ql_log_warn, vha, 0xffff,
5788 			    "Unable to send ABTS Respond. Dumping firmware.\n");
5789 			ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5790 			    vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5791 
5792 			if (qpair == ha->base_qpair)
5793 				ha->isp_ops->fw_dump(vha);
5794 			else
5795 				qla2xxx_dump_fw(vha);
5796 
5797 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5798 			qla2xxx_wake_dpc(vha);
5799 		}
5800 	} else if (qpair->retry_term_jiff != jiffies) {
5801 		qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5802 		qpair->retry_term_cnt = 0;
5803 		qpair->retry_term_jiff = jiffies;
5804 	}
5805 
5806 	return rc;
5807 }
5808 
5809 
qlt_handle_abts_completion(struct scsi_qla_host * vha,struct rsp_que * rsp,response_t * pkt)5810 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5811 	struct rsp_que *rsp, response_t *pkt)
5812 {
5813 	struct abts_resp_from_24xx_fw *entry =
5814 		(struct abts_resp_from_24xx_fw *)pkt;
5815 	u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5816 	struct qla_tgt_mgmt_cmd *mcmd;
5817 	struct qla_hw_data *ha = vha->hw;
5818 
5819 	mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5820 	if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5821 		ql_dbg(ql_dbg_async, vha, 0xe064,
5822 		    "qla_target(%d): ABTS Comp without mcmd\n",
5823 		    vha->vp_idx);
5824 		return;
5825 	}
5826 
5827 	if (mcmd)
5828 		vha  = mcmd->vha;
5829 	vha->vha_tgt.qla_tgt->abts_resp_expected--;
5830 
5831 	ql_dbg(ql_dbg_tgt, vha, 0xe038,
5832 	    "ABTS_RESP_24XX: compl_status %x\n",
5833 	    entry->compl_status);
5834 
5835 	if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5836 		if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5837 		    le32_to_cpu(entry->error_subcode2) == 0) {
5838 			if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5839 				ha->tgt.tgt_ops->free_mcmd(mcmd);
5840 				return;
5841 			}
5842 			qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5843 			    pkt, mcmd);
5844 		} else {
5845 			ql_dbg(ql_dbg_tgt, vha, 0xe063,
5846 			    "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5847 			    vha->vp_idx, entry->compl_status,
5848 			    entry->error_subcode1,
5849 			    entry->error_subcode2);
5850 			ha->tgt.tgt_ops->free_mcmd(mcmd);
5851 		}
5852 	} else if (mcmd) {
5853 		ha->tgt.tgt_ops->free_mcmd(mcmd);
5854 	}
5855 }
5856 
5857 /* ha->hardware_lock supposed to be held on entry */
5858 /* called via callback from qla2xxx */
qlt_response_pkt(struct scsi_qla_host * vha,struct rsp_que * rsp,response_t * pkt)5859 static void qlt_response_pkt(struct scsi_qla_host *vha,
5860 	struct rsp_que *rsp, response_t *pkt)
5861 {
5862 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5863 
5864 	if (unlikely(tgt == NULL)) {
5865 		ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5866 		    "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5867 		    vha->vp_idx, pkt->entry_type, vha->hw);
5868 		return;
5869 	}
5870 
5871 	/*
5872 	 * In tgt_stop mode we also should allow all requests to pass.
5873 	 * Otherwise, some commands can stuck.
5874 	 */
5875 
5876 	switch (pkt->entry_type) {
5877 	case CTIO_CRC2:
5878 	case CTIO_TYPE7:
5879 	{
5880 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5881 
5882 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5883 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5884 		    entry);
5885 		break;
5886 	}
5887 
5888 	case ACCEPT_TGT_IO_TYPE:
5889 	{
5890 		struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5891 		int rc;
5892 
5893 		if (atio->u.isp2x.status !=
5894 		    cpu_to_le16(ATIO_CDB_VALID)) {
5895 			ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5896 			    "qla_target(%d): ATIO with error "
5897 			    "status %x received\n", vha->vp_idx,
5898 			    le16_to_cpu(atio->u.isp2x.status));
5899 			break;
5900 		}
5901 
5902 		rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5903 		if (rc != 0)
5904 			return;
5905 
5906 		rc = qlt_handle_cmd_for_atio(vha, atio);
5907 		if (unlikely(rc != 0)) {
5908 			switch (rc) {
5909 			case -ENODEV:
5910 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5911 				    "qla_target: Unable to send command to target\n");
5912 				break;
5913 			case -EBADF:
5914 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5915 				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5916 				qlt_send_term_exchange(rsp->qpair, NULL,
5917 				    atio, 1, 0);
5918 				break;
5919 			case -EBUSY:
5920 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5921 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5922 				    vha->vp_idx);
5923 				qlt_send_busy(rsp->qpair, atio,
5924 				    tc_sam_status);
5925 				break;
5926 			default:
5927 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5928 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5929 				    vha->vp_idx);
5930 				qlt_send_busy(rsp->qpair, atio,
5931 				    qla_sam_status);
5932 				break;
5933 			}
5934 		}
5935 	}
5936 	break;
5937 
5938 	case CONTINUE_TGT_IO_TYPE:
5939 	{
5940 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5941 
5942 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5943 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5944 		    entry);
5945 		break;
5946 	}
5947 
5948 	case CTIO_A64_TYPE:
5949 	{
5950 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5951 
5952 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5953 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5954 		    entry);
5955 		break;
5956 	}
5957 
5958 	case IMMED_NOTIFY_TYPE:
5959 		ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5960 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5961 		break;
5962 
5963 	case NOTIFY_ACK_TYPE:
5964 		if (tgt->notify_ack_expected > 0) {
5965 			struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5966 
5967 			ql_dbg(ql_dbg_tgt, vha, 0xe036,
5968 			    "NOTIFY_ACK seq %08x status %x\n",
5969 			    le16_to_cpu(entry->u.isp2x.seq_id),
5970 			    le16_to_cpu(entry->u.isp2x.status));
5971 			tgt->notify_ack_expected--;
5972 			if (entry->u.isp2x.status !=
5973 			    cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5974 				ql_dbg(ql_dbg_tgt, vha, 0xe061,
5975 				    "qla_target(%d): NOTIFY_ACK "
5976 				    "failed %x\n", vha->vp_idx,
5977 				    le16_to_cpu(entry->u.isp2x.status));
5978 			}
5979 		} else {
5980 			ql_dbg(ql_dbg_tgt, vha, 0xe062,
5981 			    "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5982 			    vha->vp_idx);
5983 		}
5984 		break;
5985 
5986 	case ABTS_RECV_24XX:
5987 		ql_dbg(ql_dbg_tgt, vha, 0xe037,
5988 		    "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5989 		qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5990 		break;
5991 
5992 	case ABTS_RESP_24XX:
5993 		if (tgt->abts_resp_expected > 0) {
5994 			qlt_handle_abts_completion(vha, rsp, pkt);
5995 		} else {
5996 			ql_dbg(ql_dbg_tgt, vha, 0xe064,
5997 			    "qla_target(%d): Unexpected ABTS_RESP_24XX "
5998 			    "received\n", vha->vp_idx);
5999 		}
6000 		break;
6001 
6002 	default:
6003 		ql_dbg(ql_dbg_tgt, vha, 0xe065,
6004 		    "qla_target(%d): Received unknown response pkt "
6005 		    "type %x\n", vha->vp_idx, pkt->entry_type);
6006 		break;
6007 	}
6008 
6009 }
6010 
6011 /*
6012  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6013  */
qlt_async_event(uint16_t code,struct scsi_qla_host * vha,uint16_t * mailbox)6014 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6015 	uint16_t *mailbox)
6016 {
6017 	struct qla_hw_data *ha = vha->hw;
6018 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6019 	int login_code;
6020 
6021 	if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
6022 		return;
6023 
6024 	if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6025 	    IS_QLA2100(ha))
6026 		return;
6027 	/*
6028 	 * In tgt_stop mode we also should allow all requests to pass.
6029 	 * Otherwise, some commands can stuck.
6030 	 */
6031 
6032 
6033 	switch (code) {
6034 	case MBA_RESET:			/* Reset */
6035 	case MBA_SYSTEM_ERR:		/* System Error */
6036 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
6037 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
6038 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6039 		    "qla_target(%d): System error async event %#x "
6040 		    "occurred", vha->vp_idx, code);
6041 		break;
6042 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up. */
6043 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6044 		break;
6045 
6046 	case MBA_LOOP_UP:
6047 	{
6048 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
6049 		    "qla_target(%d): Async LOOP_UP occurred "
6050 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6051 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6052 		if (tgt->link_reinit_iocb_pending) {
6053 			qlt_send_notify_ack(ha->base_qpair,
6054 			    &tgt->link_reinit_iocb,
6055 			    0, 0, 0, 0, 0, 0);
6056 			tgt->link_reinit_iocb_pending = 0;
6057 		}
6058 		break;
6059 	}
6060 
6061 	case MBA_LIP_OCCURRED:
6062 	case MBA_LOOP_DOWN:
6063 	case MBA_LIP_RESET:
6064 	case MBA_RSCN_UPDATE:
6065 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
6066 		    "qla_target(%d): Async event %#x occurred "
6067 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6068 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6069 		break;
6070 
6071 	case MBA_REJECTED_FCP_CMD:
6072 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6073 		    "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6074 		    vha->vp_idx,
6075 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6076 
6077 		if (mailbox[3] == 1) {
6078 			/* exchange starvation. */
6079 			vha->hw->exch_starvation++;
6080 			if (vha->hw->exch_starvation > 5) {
6081 				ql_log(ql_log_warn, vha, 0xd03a,
6082 				    "Exchange starvation-. Resetting RISC\n");
6083 
6084 				vha->hw->exch_starvation = 0;
6085 				if (IS_P3P_TYPE(vha->hw))
6086 					set_bit(FCOE_CTX_RESET_NEEDED,
6087 					    &vha->dpc_flags);
6088 				else
6089 					set_bit(ISP_ABORT_NEEDED,
6090 					    &vha->dpc_flags);
6091 				qla2xxx_wake_dpc(vha);
6092 			}
6093 		}
6094 		break;
6095 
6096 	case MBA_PORT_UPDATE:
6097 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6098 		    "qla_target(%d): Port update async event %#x "
6099 		    "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6100 		    "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6101 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6102 
6103 		login_code = mailbox[2];
6104 		if (login_code == 0x4) {
6105 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6106 			    "Async MB 2: Got PLOGI Complete\n");
6107 			vha->hw->exch_starvation = 0;
6108 		} else if (login_code == 0x7)
6109 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6110 			    "Async MB 2: Port Logged Out\n");
6111 		break;
6112 	default:
6113 		break;
6114 	}
6115 
6116 }
6117 
qlt_get_port_database(struct scsi_qla_host * vha,uint16_t loop_id)6118 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6119 	uint16_t loop_id)
6120 {
6121 	fc_port_t *fcport, *tfcp, *del;
6122 	int rc;
6123 	unsigned long flags;
6124 	u8 newfcport = 0;
6125 
6126 	fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6127 	if (!fcport) {
6128 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6129 		    "qla_target(%d): Allocation of tmp FC port failed",
6130 		    vha->vp_idx);
6131 		return NULL;
6132 	}
6133 
6134 	fcport->loop_id = loop_id;
6135 
6136 	rc = qla24xx_gpdb_wait(vha, fcport, 0);
6137 	if (rc != QLA_SUCCESS) {
6138 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6139 		    "qla_target(%d): Failed to retrieve fcport "
6140 		    "information -- get_port_database() returned %x "
6141 		    "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6142 		kfree(fcport);
6143 		return NULL;
6144 	}
6145 
6146 	del = NULL;
6147 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6148 	tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6149 
6150 	if (tfcp) {
6151 		tfcp->d_id = fcport->d_id;
6152 		tfcp->port_type = fcport->port_type;
6153 		tfcp->supported_classes = fcport->supported_classes;
6154 		tfcp->flags |= fcport->flags;
6155 		tfcp->scan_state = QLA_FCPORT_FOUND;
6156 
6157 		del = fcport;
6158 		fcport = tfcp;
6159 	} else {
6160 		if (vha->hw->current_topology == ISP_CFG_F)
6161 			fcport->flags |= FCF_FABRIC_DEVICE;
6162 
6163 		list_add_tail(&fcport->list, &vha->vp_fcports);
6164 		if (!IS_SW_RESV_ADDR(fcport->d_id))
6165 		   vha->fcport_count++;
6166 		fcport->login_gen++;
6167 		qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6168 		fcport->login_succ = 1;
6169 		newfcport = 1;
6170 	}
6171 
6172 	fcport->deleted = 0;
6173 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6174 
6175 	switch (vha->host->active_mode) {
6176 	case MODE_INITIATOR:
6177 	case MODE_DUAL:
6178 		if (newfcport) {
6179 			if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6180 				qla24xx_sched_upd_fcport(fcport);
6181 			} else {
6182 				ql_dbg(ql_dbg_disc, vha, 0x20ff,
6183 				   "%s %d %8phC post gpsc fcp_cnt %d\n",
6184 				   __func__, __LINE__, fcport->port_name, vha->fcport_count);
6185 				qla24xx_post_gpsc_work(vha, fcport);
6186 			}
6187 		}
6188 		break;
6189 
6190 	case MODE_TARGET:
6191 	default:
6192 		break;
6193 	}
6194 	if (del)
6195 		qla2x00_free_fcport(del);
6196 
6197 	return fcport;
6198 }
6199 
6200 /* Must be called under tgt_mutex */
qlt_make_local_sess(struct scsi_qla_host * vha,be_id_t s_id)6201 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6202 					   be_id_t s_id)
6203 {
6204 	struct fc_port *sess = NULL;
6205 	fc_port_t *fcport = NULL;
6206 	int rc, global_resets;
6207 	uint16_t loop_id = 0;
6208 
6209 	if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6210 		/*
6211 		 * This is Domain Controller, so it should be
6212 		 * OK to drop SCSI commands from it.
6213 		 */
6214 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6215 		    "Unable to find initiator with S_ID %x:%x:%x",
6216 		    s_id.domain, s_id.area, s_id.al_pa);
6217 		return NULL;
6218 	}
6219 
6220 	mutex_lock(&vha->vha_tgt.tgt_mutex);
6221 
6222 retry:
6223 	global_resets =
6224 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6225 
6226 	rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6227 	if (rc != 0) {
6228 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
6229 
6230 		ql_log(ql_log_info, vha, 0xf071,
6231 		    "qla_target(%d): Unable to find "
6232 		    "initiator with S_ID %x:%x:%x",
6233 		    vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6234 
6235 		if (rc == -ENOENT) {
6236 			qlt_port_logo_t logo;
6237 
6238 			logo.id = be_to_port_id(s_id);
6239 			logo.cmd_count = 1;
6240 			qlt_send_first_logo(vha, &logo);
6241 		}
6242 
6243 		return NULL;
6244 	}
6245 
6246 	fcport = qlt_get_port_database(vha, loop_id);
6247 	if (!fcport) {
6248 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
6249 		return NULL;
6250 	}
6251 
6252 	if (global_resets !=
6253 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6254 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6255 		    "qla_target(%d): global reset during session discovery "
6256 		    "(counter was %d, new %d), retrying", vha->vp_idx,
6257 		    global_resets,
6258 		    atomic_read(&vha->vha_tgt.
6259 			qla_tgt->tgt_global_resets_count));
6260 		goto retry;
6261 	}
6262 
6263 	sess = qlt_create_sess(vha, fcport, true);
6264 
6265 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
6266 
6267 	return sess;
6268 }
6269 
qlt_abort_work(struct qla_tgt * tgt,struct qla_tgt_sess_work_param * prm)6270 static void qlt_abort_work(struct qla_tgt *tgt,
6271 	struct qla_tgt_sess_work_param *prm)
6272 {
6273 	struct scsi_qla_host *vha = tgt->vha;
6274 	struct qla_hw_data *ha = vha->hw;
6275 	struct fc_port *sess = NULL;
6276 	unsigned long flags = 0, flags2 = 0;
6277 	be_id_t s_id;
6278 	int rc;
6279 
6280 	spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6281 
6282 	if (tgt->tgt_stop)
6283 		goto out_term2;
6284 
6285 	s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6286 
6287 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6288 	if (!sess) {
6289 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6290 
6291 		sess = qlt_make_local_sess(vha, s_id);
6292 		/* sess has got an extra creation ref */
6293 
6294 		spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6295 		if (!sess)
6296 			goto out_term2;
6297 	} else {
6298 		if (sess->deleted) {
6299 			sess = NULL;
6300 			goto out_term2;
6301 		}
6302 
6303 		if (!kref_get_unless_zero(&sess->sess_kref)) {
6304 			ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6305 			    "%s: kref_get fail %8phC \n",
6306 			     __func__, sess->port_name);
6307 			sess = NULL;
6308 			goto out_term2;
6309 		}
6310 	}
6311 
6312 	rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6313 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6314 
6315 	ha->tgt.tgt_ops->put_sess(sess);
6316 
6317 	if (rc != 0)
6318 		goto out_term;
6319 	return;
6320 
6321 out_term2:
6322 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6323 
6324 out_term:
6325 	spin_lock_irqsave(&ha->hardware_lock, flags);
6326 	qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6327 	    FCP_TMF_REJECTED, false);
6328 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6329 }
6330 
qlt_sess_work_fn(struct work_struct * work)6331 static void qlt_sess_work_fn(struct work_struct *work)
6332 {
6333 	struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6334 	struct scsi_qla_host *vha = tgt->vha;
6335 	unsigned long flags;
6336 
6337 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6338 
6339 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
6340 	while (!list_empty(&tgt->sess_works_list)) {
6341 		struct qla_tgt_sess_work_param *prm = list_entry(
6342 		    tgt->sess_works_list.next, typeof(*prm),
6343 		    sess_works_list_entry);
6344 
6345 		/*
6346 		 * This work can be scheduled on several CPUs at time, so we
6347 		 * must delete the entry to eliminate double processing
6348 		 */
6349 		list_del(&prm->sess_works_list_entry);
6350 
6351 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6352 
6353 		switch (prm->type) {
6354 		case QLA_TGT_SESS_WORK_ABORT:
6355 			qlt_abort_work(tgt, prm);
6356 			break;
6357 		default:
6358 			BUG_ON(1);
6359 			break;
6360 		}
6361 
6362 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
6363 
6364 		kfree(prm);
6365 	}
6366 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6367 }
6368 
6369 /* Must be called under tgt_host_action_mutex */
qlt_add_target(struct qla_hw_data * ha,struct scsi_qla_host * base_vha)6370 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6371 {
6372 	struct qla_tgt *tgt;
6373 	int rc, i;
6374 	struct qla_qpair_hint *h;
6375 
6376 	if (!QLA_TGT_MODE_ENABLED())
6377 		return 0;
6378 
6379 	if (!IS_TGT_MODE_CAPABLE(ha)) {
6380 		ql_log(ql_log_warn, base_vha, 0xe070,
6381 		    "This adapter does not support target mode.\n");
6382 		return 0;
6383 	}
6384 
6385 	ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6386 	    "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6387 
6388 	BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6389 
6390 	tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6391 	if (!tgt) {
6392 		ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6393 		    "Unable to allocate struct qla_tgt\n");
6394 		return -ENOMEM;
6395 	}
6396 
6397 	tgt->qphints = kcalloc(ha->max_qpairs + 1,
6398 			       sizeof(struct qla_qpair_hint),
6399 			       GFP_KERNEL);
6400 	if (!tgt->qphints) {
6401 		kfree(tgt);
6402 		ql_log(ql_log_warn, base_vha, 0x0197,
6403 		    "Unable to allocate qpair hints.\n");
6404 		return -ENOMEM;
6405 	}
6406 
6407 	qla2xxx_driver_template.supported_mode |= MODE_TARGET;
6408 
6409 	rc = btree_init64(&tgt->lun_qpair_map);
6410 	if (rc) {
6411 		kfree(tgt->qphints);
6412 		kfree(tgt);
6413 		ql_log(ql_log_info, base_vha, 0x0198,
6414 			"Unable to initialize lun_qpair_map btree\n");
6415 		return -EIO;
6416 	}
6417 	h = &tgt->qphints[0];
6418 	h->qpair = ha->base_qpair;
6419 	INIT_LIST_HEAD(&h->hint_elem);
6420 	h->cpuid = ha->base_qpair->cpuid;
6421 	list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6422 
6423 	for (i = 0; i < ha->max_qpairs; i++) {
6424 		unsigned long flags;
6425 
6426 		struct qla_qpair *qpair = ha->queue_pair_map[i];
6427 
6428 		h = &tgt->qphints[i + 1];
6429 		INIT_LIST_HEAD(&h->hint_elem);
6430 		if (qpair) {
6431 			h->qpair = qpair;
6432 			spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6433 			list_add_tail(&h->hint_elem, &qpair->hints_list);
6434 			spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6435 			h->cpuid = qpair->cpuid;
6436 		}
6437 	}
6438 
6439 	tgt->ha = ha;
6440 	tgt->vha = base_vha;
6441 	init_waitqueue_head(&tgt->waitQ);
6442 	spin_lock_init(&tgt->sess_work_lock);
6443 	INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6444 	INIT_LIST_HEAD(&tgt->sess_works_list);
6445 	atomic_set(&tgt->tgt_global_resets_count, 0);
6446 
6447 	base_vha->vha_tgt.qla_tgt = tgt;
6448 
6449 	ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6450 		"qla_target(%d): using 64 Bit PCI addressing",
6451 		base_vha->vp_idx);
6452 	/* 3 is reserved */
6453 	tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6454 
6455 	mutex_lock(&qla_tgt_mutex);
6456 	list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6457 	mutex_unlock(&qla_tgt_mutex);
6458 
6459 	if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6460 		ha->tgt.tgt_ops->add_target(base_vha);
6461 
6462 	return 0;
6463 }
6464 
6465 /* Must be called under tgt_host_action_mutex */
qlt_remove_target(struct qla_hw_data * ha,struct scsi_qla_host * vha)6466 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6467 {
6468 	if (!vha->vha_tgt.qla_tgt)
6469 		return 0;
6470 
6471 	if (vha->fc_vport) {
6472 		qlt_release(vha->vha_tgt.qla_tgt);
6473 		return 0;
6474 	}
6475 
6476 	/* free left over qfull cmds */
6477 	qlt_init_term_exchange(vha);
6478 
6479 	ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6480 	    vha->host_no, ha);
6481 	qlt_release(vha->vha_tgt.qla_tgt);
6482 
6483 	return 0;
6484 }
6485 
qla_remove_hostmap(struct qla_hw_data * ha)6486 void qla_remove_hostmap(struct qla_hw_data *ha)
6487 {
6488 	struct scsi_qla_host *node;
6489 	u32 key = 0;
6490 
6491 	btree_for_each_safe32(&ha->host_map, key, node)
6492 		btree_remove32(&ha->host_map, key);
6493 
6494 	btree_destroy32(&ha->host_map);
6495 }
6496 
qlt_lport_dump(struct scsi_qla_host * vha,u64 wwpn,unsigned char * b)6497 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6498 	unsigned char *b)
6499 {
6500 	pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6501 	pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6502 	put_unaligned_be64(wwpn, b);
6503 	pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6504 }
6505 
6506 /**
6507  * qlt_lport_register - register lport with external module
6508  *
6509  * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6510  * @phys_wwpn: physical port WWPN
6511  * @npiv_wwpn: NPIV WWPN
6512  * @npiv_wwnn: NPIV WWNN
6513  * @callback:  lport initialization callback for tcm_qla2xxx code
6514  */
qlt_lport_register(void * target_lport_ptr,u64 phys_wwpn,u64 npiv_wwpn,u64 npiv_wwnn,int (* callback)(struct scsi_qla_host *,void *,u64,u64))6515 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6516 		       u64 npiv_wwpn, u64 npiv_wwnn,
6517 		       int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6518 {
6519 	struct qla_tgt *tgt;
6520 	struct scsi_qla_host *vha;
6521 	struct qla_hw_data *ha;
6522 	struct Scsi_Host *host;
6523 	unsigned long flags;
6524 	int rc;
6525 	u8 b[WWN_SIZE];
6526 
6527 	mutex_lock(&qla_tgt_mutex);
6528 	list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6529 		vha = tgt->vha;
6530 		ha = vha->hw;
6531 
6532 		host = vha->host;
6533 		if (!host)
6534 			continue;
6535 
6536 		if (!(host->hostt->supported_mode & MODE_TARGET))
6537 			continue;
6538 
6539 		if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6540 			continue;
6541 
6542 		spin_lock_irqsave(&ha->hardware_lock, flags);
6543 		if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6544 			pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6545 			    host->host_no);
6546 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
6547 			continue;
6548 		}
6549 		if (tgt->tgt_stop) {
6550 			pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6551 				 host->host_no);
6552 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
6553 			continue;
6554 		}
6555 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
6556 
6557 		if (!scsi_host_get(host)) {
6558 			ql_dbg(ql_dbg_tgt, vha, 0xe068,
6559 			    "Unable to scsi_host_get() for"
6560 			    " qla2xxx scsi_host\n");
6561 			continue;
6562 		}
6563 		qlt_lport_dump(vha, phys_wwpn, b);
6564 
6565 		if (memcmp(vha->port_name, b, WWN_SIZE)) {
6566 			scsi_host_put(host);
6567 			continue;
6568 		}
6569 		rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6570 		if (rc != 0)
6571 			scsi_host_put(host);
6572 
6573 		mutex_unlock(&qla_tgt_mutex);
6574 		return rc;
6575 	}
6576 	mutex_unlock(&qla_tgt_mutex);
6577 
6578 	return -ENODEV;
6579 }
6580 EXPORT_SYMBOL(qlt_lport_register);
6581 
6582 /**
6583  * qlt_lport_deregister - Degister lport
6584  *
6585  * @vha:  Registered scsi_qla_host pointer
6586  */
qlt_lport_deregister(struct scsi_qla_host * vha)6587 void qlt_lport_deregister(struct scsi_qla_host *vha)
6588 {
6589 	struct qla_hw_data *ha = vha->hw;
6590 	struct Scsi_Host *sh = vha->host;
6591 	/*
6592 	 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6593 	 */
6594 	vha->vha_tgt.target_lport_ptr = NULL;
6595 	ha->tgt.tgt_ops = NULL;
6596 	/*
6597 	 * Release the Scsi_Host reference for the underlying qla2xxx host
6598 	 */
6599 	scsi_host_put(sh);
6600 }
6601 EXPORT_SYMBOL(qlt_lport_deregister);
6602 
6603 /* Must be called under HW lock */
qlt_set_mode(struct scsi_qla_host * vha)6604 void qlt_set_mode(struct scsi_qla_host *vha)
6605 {
6606 	switch (vha->qlini_mode) {
6607 	case QLA2XXX_INI_MODE_DISABLED:
6608 	case QLA2XXX_INI_MODE_EXCLUSIVE:
6609 		vha->host->active_mode = MODE_TARGET;
6610 		break;
6611 	case QLA2XXX_INI_MODE_ENABLED:
6612 		vha->host->active_mode = MODE_INITIATOR;
6613 		break;
6614 	case QLA2XXX_INI_MODE_DUAL:
6615 		vha->host->active_mode = MODE_DUAL;
6616 		break;
6617 	default:
6618 		break;
6619 	}
6620 }
6621 
6622 /* Must be called under HW lock */
qlt_clear_mode(struct scsi_qla_host * vha)6623 static void qlt_clear_mode(struct scsi_qla_host *vha)
6624 {
6625 	switch (vha->qlini_mode) {
6626 	case QLA2XXX_INI_MODE_DISABLED:
6627 		vha->host->active_mode = MODE_UNKNOWN;
6628 		break;
6629 	case QLA2XXX_INI_MODE_EXCLUSIVE:
6630 		vha->host->active_mode = MODE_INITIATOR;
6631 		break;
6632 	case QLA2XXX_INI_MODE_ENABLED:
6633 	case QLA2XXX_INI_MODE_DUAL:
6634 		vha->host->active_mode = MODE_INITIATOR;
6635 		break;
6636 	default:
6637 		break;
6638 	}
6639 }
6640 
6641 /*
6642  * qla_tgt_enable_vha - NO LOCK HELD
6643  *
6644  * host_reset, bring up w/ Target Mode Enabled
6645  */
6646 void
qlt_enable_vha(struct scsi_qla_host * vha)6647 qlt_enable_vha(struct scsi_qla_host *vha)
6648 {
6649 	struct qla_hw_data *ha = vha->hw;
6650 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6651 	unsigned long flags;
6652 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6653 
6654 	if (!tgt) {
6655 		ql_dbg(ql_dbg_tgt, vha, 0xe069,
6656 		    "Unable to locate qla_tgt pointer from"
6657 		    " struct qla_hw_data\n");
6658 		dump_stack();
6659 		return;
6660 	}
6661 	if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6662 		return;
6663 
6664 	if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6665 		ha->tgt.num_act_qpairs = ha->max_qpairs;
6666 	spin_lock_irqsave(&ha->hardware_lock, flags);
6667 	tgt->tgt_stopped = 0;
6668 	qlt_set_mode(vha);
6669 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6670 
6671 	mutex_lock(&ha->optrom_mutex);
6672 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6673 	    "%s.\n", __func__);
6674 	if (vha->vp_idx) {
6675 		qla24xx_disable_vp(vha);
6676 		qla24xx_enable_vp(vha);
6677 	} else {
6678 		set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6679 		qla2xxx_wake_dpc(base_vha);
6680 		WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6681 			     QLA_SUCCESS);
6682 	}
6683 	mutex_unlock(&ha->optrom_mutex);
6684 }
6685 EXPORT_SYMBOL(qlt_enable_vha);
6686 
6687 /*
6688  * qla_tgt_disable_vha - NO LOCK HELD
6689  *
6690  * Disable Target Mode and reset the adapter
6691  */
qlt_disable_vha(struct scsi_qla_host * vha)6692 static void qlt_disable_vha(struct scsi_qla_host *vha)
6693 {
6694 	struct qla_hw_data *ha = vha->hw;
6695 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6696 	unsigned long flags;
6697 
6698 	if (!tgt) {
6699 		ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6700 		    "Unable to locate qla_tgt pointer from"
6701 		    " struct qla_hw_data\n");
6702 		dump_stack();
6703 		return;
6704 	}
6705 
6706 	spin_lock_irqsave(&ha->hardware_lock, flags);
6707 	qlt_clear_mode(vha);
6708 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6709 
6710 	set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6711 	qla2xxx_wake_dpc(vha);
6712 
6713 	/*
6714 	 * We are expecting the offline state.
6715 	 * QLA_FUNCTION_FAILED means that adapter is offline.
6716 	 */
6717 	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6718 		ql_dbg(ql_dbg_tgt, vha, 0xe081,
6719 		       "adapter is offline\n");
6720 }
6721 
6722 /*
6723  * Called from qla_init.c:qla24xx_vport_create() contex to setup
6724  * the target mode specific struct scsi_qla_host and struct qla_hw_data
6725  * members.
6726  */
6727 void
qlt_vport_create(struct scsi_qla_host * vha,struct qla_hw_data * ha)6728 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6729 {
6730 	vha->vha_tgt.qla_tgt = NULL;
6731 
6732 	mutex_init(&vha->vha_tgt.tgt_mutex);
6733 	mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6734 
6735 	INIT_LIST_HEAD(&vha->unknown_atio_list);
6736 	INIT_DELAYED_WORK(&vha->unknown_atio_work, qlt_unknown_atio_work_fn);
6737 
6738 	qlt_clear_mode(vha);
6739 
6740 	/*
6741 	 * NOTE: Currently the value is kept the same for <24xx and
6742 	 * >=24xx ISPs. If it is necessary to change it,
6743 	 * the check should be added for specific ISPs,
6744 	 * assigning the value appropriately.
6745 	 */
6746 	ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6747 
6748 	qlt_add_target(ha, vha);
6749 }
6750 
6751 u8
qlt_rff_id(struct scsi_qla_host * vha)6752 qlt_rff_id(struct scsi_qla_host *vha)
6753 {
6754 	u8 fc4_feature = 0;
6755 	/*
6756 	 * FC-4 Feature bit 0 indicates target functionality to the name server.
6757 	 */
6758 	if (qla_tgt_mode_enabled(vha)) {
6759 		fc4_feature = BIT_0;
6760 	} else if (qla_ini_mode_enabled(vha)) {
6761 		fc4_feature = BIT_1;
6762 	} else if (qla_dual_mode_enabled(vha))
6763 		fc4_feature = BIT_0 | BIT_1;
6764 
6765 	return fc4_feature;
6766 }
6767 
6768 /*
6769  * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6770  * @ha: HA context
6771  *
6772  * Beginning of ATIO ring has initialization control block already built
6773  * by nvram config routine.
6774  *
6775  * Returns 0 on success.
6776  */
6777 void
qlt_init_atio_q_entries(struct scsi_qla_host * vha)6778 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6779 {
6780 	struct qla_hw_data *ha = vha->hw;
6781 	uint16_t cnt;
6782 	struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6783 
6784 	if (qla_ini_mode_enabled(vha))
6785 		return;
6786 
6787 	for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6788 		pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6789 		pkt++;
6790 	}
6791 
6792 }
6793 
6794 /*
6795  * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6796  * @ha: SCSI driver HA context
6797  */
6798 void
qlt_24xx_process_atio_queue(struct scsi_qla_host * vha,uint8_t ha_locked)6799 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6800 {
6801 	struct qla_hw_data *ha = vha->hw;
6802 	struct atio_from_isp *pkt;
6803 	int cnt, i;
6804 
6805 	if (!ha->flags.fw_started)
6806 		return;
6807 
6808 	while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6809 	    fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6810 		pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6811 		cnt = pkt->u.raw.entry_count;
6812 
6813 		if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6814 			/*
6815 			 * This packet is corrupted. The header + payload
6816 			 * can not be trusted. There is no point in passing
6817 			 * it further up.
6818 			 */
6819 			ql_log(ql_log_warn, vha, 0xd03c,
6820 			    "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6821 			    &pkt->u.isp24.fcp_hdr.s_id,
6822 			    be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6823 			    pkt->u.isp24.exchange_addr, pkt);
6824 
6825 			adjust_corrupted_atio(pkt);
6826 			qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6827 			    ha_locked, 0);
6828 		} else {
6829 			qlt_24xx_atio_pkt_all_vps(vha,
6830 			    (struct atio_from_isp *)pkt, ha_locked);
6831 		}
6832 
6833 		for (i = 0; i < cnt; i++) {
6834 			ha->tgt.atio_ring_index++;
6835 			if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6836 				ha->tgt.atio_ring_index = 0;
6837 				ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6838 			} else
6839 				ha->tgt.atio_ring_ptr++;
6840 
6841 			pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6842 			pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6843 		}
6844 		wmb();
6845 	}
6846 
6847 	/* Adjust ring index */
6848 	wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6849 }
6850 
6851 void
qlt_24xx_config_rings(struct scsi_qla_host * vha)6852 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6853 {
6854 	struct qla_hw_data *ha = vha->hw;
6855 	struct qla_msix_entry *msix = &ha->msix_entries[2];
6856 	struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6857 
6858 	if (!QLA_TGT_MODE_ENABLED())
6859 		return;
6860 
6861 	wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6862 	wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6863 	rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6864 
6865 	if (ha->flags.msix_enabled) {
6866 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6867 			icb->msix_atio = cpu_to_le16(msix->entry);
6868 			icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6869 			ql_dbg(ql_dbg_init, vha, 0xf072,
6870 			    "Registering ICB vector 0x%x for atio que.\n",
6871 			    msix->entry);
6872 		}
6873 	} else {
6874 		/* INTx|MSI */
6875 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6876 			icb->msix_atio = 0;
6877 			icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6878 			ql_dbg(ql_dbg_init, vha, 0xf072,
6879 			    "%s: Use INTx for ATIOQ.\n", __func__);
6880 		}
6881 	}
6882 }
6883 
6884 void
qlt_24xx_config_nvram_stage1(struct scsi_qla_host * vha,struct nvram_24xx * nv)6885 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6886 {
6887 	struct qla_hw_data *ha = vha->hw;
6888 	u32 tmp;
6889 
6890 	if (!QLA_TGT_MODE_ENABLED())
6891 		return;
6892 
6893 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6894 		if (!ha->tgt.saved_set) {
6895 			/* We save only once */
6896 			ha->tgt.saved_exchange_count = nv->exchange_count;
6897 			ha->tgt.saved_firmware_options_1 =
6898 			    nv->firmware_options_1;
6899 			ha->tgt.saved_firmware_options_2 =
6900 			    nv->firmware_options_2;
6901 			ha->tgt.saved_firmware_options_3 =
6902 			    nv->firmware_options_3;
6903 			ha->tgt.saved_set = 1;
6904 		}
6905 
6906 		if (qla_tgt_mode_enabled(vha))
6907 			nv->exchange_count = cpu_to_le16(0xFFFF);
6908 		else			/* dual */
6909 			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6910 
6911 		/* Enable target mode */
6912 		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6913 
6914 		/* Disable ini mode, if requested */
6915 		if (qla_tgt_mode_enabled(vha))
6916 			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6917 
6918 		/* Disable Full Login after LIP */
6919 		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6920 		/* Enable initial LIP */
6921 		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6922 		if (ql2xtgt_tape_enable)
6923 			/* Enable FC Tape support */
6924 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6925 		else
6926 			/* Disable FC Tape support */
6927 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6928 
6929 		/* Disable Full Login after LIP */
6930 		nv->host_p &= cpu_to_le32(~BIT_10);
6931 
6932 		/*
6933 		 * clear BIT 15 explicitly as we have seen at least
6934 		 * a couple of instances where this was set and this
6935 		 * was causing the firmware to not be initialized.
6936 		 */
6937 		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6938 		/* Enable target PRLI control */
6939 		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6940 
6941 		if (IS_QLA25XX(ha)) {
6942 			/* Change Loop-prefer to Pt-Pt */
6943 			tmp = ~(BIT_4|BIT_5|BIT_6);
6944 			nv->firmware_options_2 &= cpu_to_le32(tmp);
6945 			tmp = P2P << 4;
6946 			nv->firmware_options_2 |= cpu_to_le32(tmp);
6947 		}
6948 	} else {
6949 		if (ha->tgt.saved_set) {
6950 			nv->exchange_count = ha->tgt.saved_exchange_count;
6951 			nv->firmware_options_1 =
6952 			    ha->tgt.saved_firmware_options_1;
6953 			nv->firmware_options_2 =
6954 			    ha->tgt.saved_firmware_options_2;
6955 			nv->firmware_options_3 =
6956 			    ha->tgt.saved_firmware_options_3;
6957 		}
6958 		return;
6959 	}
6960 
6961 	if (ha->base_qpair->enable_class_2) {
6962 		if (vha->flags.init_done)
6963 			fc_host_supported_classes(vha->host) =
6964 				FC_COS_CLASS2 | FC_COS_CLASS3;
6965 
6966 		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6967 	} else {
6968 		if (vha->flags.init_done)
6969 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6970 
6971 		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6972 	}
6973 }
6974 
6975 void
qlt_24xx_config_nvram_stage2(struct scsi_qla_host * vha,struct init_cb_24xx * icb)6976 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6977 	struct init_cb_24xx *icb)
6978 {
6979 	struct qla_hw_data *ha = vha->hw;
6980 
6981 	if (!QLA_TGT_MODE_ENABLED())
6982 		return;
6983 
6984 	if (ha->tgt.node_name_set) {
6985 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6986 		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6987 	}
6988 }
6989 
6990 void
qlt_81xx_config_nvram_stage1(struct scsi_qla_host * vha,struct nvram_81xx * nv)6991 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6992 {
6993 	struct qla_hw_data *ha = vha->hw;
6994 	u32 tmp;
6995 
6996 	if (!QLA_TGT_MODE_ENABLED())
6997 		return;
6998 
6999 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7000 		if (!ha->tgt.saved_set) {
7001 			/* We save only once */
7002 			ha->tgt.saved_exchange_count = nv->exchange_count;
7003 			ha->tgt.saved_firmware_options_1 =
7004 			    nv->firmware_options_1;
7005 			ha->tgt.saved_firmware_options_2 =
7006 			    nv->firmware_options_2;
7007 			ha->tgt.saved_firmware_options_3 =
7008 			    nv->firmware_options_3;
7009 			ha->tgt.saved_set = 1;
7010 		}
7011 
7012 		if (qla_tgt_mode_enabled(vha))
7013 			nv->exchange_count = cpu_to_le16(0xFFFF);
7014 		else			/* dual */
7015 			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7016 
7017 		/* Enable target mode */
7018 		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7019 
7020 		/* Disable ini mode, if requested */
7021 		if (qla_tgt_mode_enabled(vha))
7022 			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7023 		/* Disable Full Login after LIP */
7024 		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7025 		/* Enable initial LIP */
7026 		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7027 		/*
7028 		 * clear BIT 15 explicitly as we have seen at
7029 		 * least a couple of instances where this was set
7030 		 * and this was causing the firmware to not be
7031 		 * initialized.
7032 		 */
7033 		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7034 		if (ql2xtgt_tape_enable)
7035 			/* Enable FC tape support */
7036 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7037 		else
7038 			/* Disable FC tape support */
7039 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7040 
7041 		/* Disable Full Login after LIP */
7042 		nv->host_p &= cpu_to_le32(~BIT_10);
7043 		/* Enable target PRLI control */
7044 		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7045 
7046 		/* Change Loop-prefer to Pt-Pt */
7047 		tmp = ~(BIT_4|BIT_5|BIT_6);
7048 		nv->firmware_options_2 &= cpu_to_le32(tmp);
7049 		tmp = P2P << 4;
7050 		nv->firmware_options_2 |= cpu_to_le32(tmp);
7051 	} else {
7052 		if (ha->tgt.saved_set) {
7053 			nv->exchange_count = ha->tgt.saved_exchange_count;
7054 			nv->firmware_options_1 =
7055 			    ha->tgt.saved_firmware_options_1;
7056 			nv->firmware_options_2 =
7057 			    ha->tgt.saved_firmware_options_2;
7058 			nv->firmware_options_3 =
7059 			    ha->tgt.saved_firmware_options_3;
7060 		}
7061 		return;
7062 	}
7063 
7064 	if (ha->base_qpair->enable_class_2) {
7065 		if (vha->flags.init_done)
7066 			fc_host_supported_classes(vha->host) =
7067 				FC_COS_CLASS2 | FC_COS_CLASS3;
7068 
7069 		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7070 	} else {
7071 		if (vha->flags.init_done)
7072 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7073 
7074 		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7075 	}
7076 }
7077 
7078 void
qlt_81xx_config_nvram_stage2(struct scsi_qla_host * vha,struct init_cb_81xx * icb)7079 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7080 	struct init_cb_81xx *icb)
7081 {
7082 	struct qla_hw_data *ha = vha->hw;
7083 
7084 	if (!QLA_TGT_MODE_ENABLED())
7085 		return;
7086 
7087 	if (ha->tgt.node_name_set) {
7088 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7089 		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7090 	}
7091 }
7092 
7093 void
qlt_83xx_iospace_config(struct qla_hw_data * ha)7094 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7095 {
7096 	if (!QLA_TGT_MODE_ENABLED())
7097 		return;
7098 
7099 	ha->msix_count += 1; /* For ATIO Q */
7100 }
7101 
7102 
7103 void
qlt_modify_vp_config(struct scsi_qla_host * vha,struct vp_config_entry_24xx * vpmod)7104 qlt_modify_vp_config(struct scsi_qla_host *vha,
7105 	struct vp_config_entry_24xx *vpmod)
7106 {
7107 	/* enable target mode.  Bit5 = 1 => disable */
7108 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7109 		vpmod->options_idx1 &= ~BIT_5;
7110 
7111 	/* Disable ini mode, if requested.  bit4 = 1 => disable */
7112 	if (qla_tgt_mode_enabled(vha))
7113 		vpmod->options_idx1 &= ~BIT_4;
7114 }
7115 
7116 void
qlt_probe_one_stage1(struct scsi_qla_host * base_vha,struct qla_hw_data * ha)7117 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7118 {
7119 	mutex_init(&base_vha->vha_tgt.tgt_mutex);
7120 	if (!QLA_TGT_MODE_ENABLED())
7121 		return;
7122 
7123 	if  (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
7124 		ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7125 		ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7126 	} else {
7127 		ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7128 		ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7129 	}
7130 
7131 	mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7132 
7133 	INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7134 	INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7135 	    qlt_unknown_atio_work_fn);
7136 
7137 	qlt_clear_mode(base_vha);
7138 
7139 	qla_update_vp_map(base_vha, SET_VP_IDX);
7140 }
7141 
7142 irqreturn_t
qla83xx_msix_atio_q(int irq,void * dev_id)7143 qla83xx_msix_atio_q(int irq, void *dev_id)
7144 {
7145 	struct rsp_que *rsp;
7146 	scsi_qla_host_t	*vha;
7147 	struct qla_hw_data *ha;
7148 	unsigned long flags;
7149 
7150 	rsp = (struct rsp_que *) dev_id;
7151 	ha = rsp->hw;
7152 	vha = pci_get_drvdata(ha->pdev);
7153 
7154 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7155 
7156 	qlt_24xx_process_atio_queue(vha, 0);
7157 
7158 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7159 
7160 	return IRQ_HANDLED;
7161 }
7162 
7163 static void
qlt_handle_abts_recv_work(struct work_struct * work)7164 qlt_handle_abts_recv_work(struct work_struct *work)
7165 {
7166 	struct qla_tgt_sess_op *op = container_of(work,
7167 		struct qla_tgt_sess_op, work);
7168 	scsi_qla_host_t *vha = op->vha;
7169 	struct qla_hw_data *ha = vha->hw;
7170 	unsigned long flags;
7171 
7172 	if (qla2x00_reset_active(vha) ||
7173 	    (op->chip_reset != ha->base_qpair->chip_reset))
7174 		return;
7175 
7176 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7177 	qlt_24xx_process_atio_queue(vha, 0);
7178 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7179 
7180 	spin_lock_irqsave(&ha->hardware_lock, flags);
7181 	qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7182 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7183 
7184 	kfree(op);
7185 }
7186 
7187 void
qlt_handle_abts_recv(struct scsi_qla_host * vha,struct rsp_que * rsp,response_t * pkt)7188 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7189     response_t *pkt)
7190 {
7191 	struct qla_tgt_sess_op *op;
7192 
7193 	op = kzalloc(sizeof(*op), GFP_ATOMIC);
7194 
7195 	if (!op) {
7196 		/* do not reach for ATIO queue here.  This is best effort err
7197 		 * recovery at this point.
7198 		 */
7199 		qlt_response_pkt_all_vps(vha, rsp, pkt);
7200 		return;
7201 	}
7202 
7203 	memcpy(&op->atio, pkt, sizeof(*pkt));
7204 	op->vha = vha;
7205 	op->chip_reset = vha->hw->base_qpair->chip_reset;
7206 	op->rsp = rsp;
7207 	INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7208 	queue_work(qla_tgt_wq, &op->work);
7209 	return;
7210 }
7211 
7212 int
qlt_mem_alloc(struct qla_hw_data * ha)7213 qlt_mem_alloc(struct qla_hw_data *ha)
7214 {
7215 	if (!QLA_TGT_MODE_ENABLED())
7216 		return 0;
7217 
7218 	ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7219 	    (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7220 	    &ha->tgt.atio_dma, GFP_KERNEL);
7221 	if (!ha->tgt.atio_ring) {
7222 		return -ENOMEM;
7223 	}
7224 	return 0;
7225 }
7226 
7227 void
qlt_mem_free(struct qla_hw_data * ha)7228 qlt_mem_free(struct qla_hw_data *ha)
7229 {
7230 	if (!QLA_TGT_MODE_ENABLED())
7231 		return;
7232 
7233 	if (ha->tgt.atio_ring) {
7234 		dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7235 		    sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7236 		    ha->tgt.atio_dma);
7237 	}
7238 	ha->tgt.atio_ring = NULL;
7239 	ha->tgt.atio_dma = 0;
7240 }
7241 
qlt_parse_ini_mode(void)7242 static int __init qlt_parse_ini_mode(void)
7243 {
7244 	if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7245 		ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7246 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7247 		ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7248 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7249 		ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7250 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7251 		ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7252 	else
7253 		return false;
7254 
7255 	return true;
7256 }
7257 
qlt_init(void)7258 int __init qlt_init(void)
7259 {
7260 	int ret;
7261 
7262 	BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7263 	BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7264 
7265 	if (!qlt_parse_ini_mode()) {
7266 		ql_log(ql_log_fatal, NULL, 0xe06b,
7267 		    "qlt_parse_ini_mode() failed\n");
7268 		return -EINVAL;
7269 	}
7270 
7271 	if (!QLA_TGT_MODE_ENABLED())
7272 		return 0;
7273 
7274 	qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7275 	    sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7276 	    qla_tgt_mgmt_cmd), 0, NULL);
7277 	if (!qla_tgt_mgmt_cmd_cachep) {
7278 		ql_log(ql_log_fatal, NULL, 0xd04b,
7279 		    "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7280 		return -ENOMEM;
7281 	}
7282 
7283 	qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7284 	    sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7285 	    0, NULL);
7286 
7287 	if (!qla_tgt_plogi_cachep) {
7288 		ql_log(ql_log_fatal, NULL, 0xe06d,
7289 		    "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7290 		ret = -ENOMEM;
7291 		goto out_mgmt_cmd_cachep;
7292 	}
7293 
7294 	qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7295 	    mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7296 	if (!qla_tgt_mgmt_cmd_mempool) {
7297 		ql_log(ql_log_fatal, NULL, 0xe06e,
7298 		    "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7299 		ret = -ENOMEM;
7300 		goto out_plogi_cachep;
7301 	}
7302 
7303 	qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7304 	if (!qla_tgt_wq) {
7305 		ql_log(ql_log_fatal, NULL, 0xe06f,
7306 		    "alloc_workqueue for qla_tgt_wq failed\n");
7307 		ret = -ENOMEM;
7308 		goto out_cmd_mempool;
7309 	}
7310 	/*
7311 	 * Return 1 to signal that initiator-mode is being disabled
7312 	 */
7313 	return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7314 
7315 out_cmd_mempool:
7316 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7317 out_plogi_cachep:
7318 	kmem_cache_destroy(qla_tgt_plogi_cachep);
7319 out_mgmt_cmd_cachep:
7320 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7321 	return ret;
7322 }
7323 
qlt_exit(void)7324 void qlt_exit(void)
7325 {
7326 	if (!QLA_TGT_MODE_ENABLED())
7327 		return;
7328 
7329 	destroy_workqueue(qla_tgt_wq);
7330 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7331 	kmem_cache_destroy(qla_tgt_plogi_cachep);
7332 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7333 }
7334