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