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