blob: ea22e62257cba64b54ef56ba3cc155f9b8511185 [file] [log] [blame]
Thomas Gleixner8e8e69d2019-05-29 07:17:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002/*
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 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -070014 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040015 */
16
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/types.h>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040020#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#include <target/target_core_base.h>
31#include <target/target_core_fabric.h>
32
33#include "qla_def.h"
34#include "qla_target.h"
35
Arun Easid154f352014-09-25 06:14:48 -040036static int ql2xtgt_tape_enable;
37module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
38MODULE_PARM_DESC(ql2xtgt_tape_enable,
39 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
40
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040041static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
42module_param(qlini_mode, charp, S_IRUGO);
43MODULE_PARM_DESC(qlini_mode,
44 "Determines when initiator mode will be enabled. Possible values: "
45 "\"exclusive\" - initiator mode will be enabled on load, "
46 "disabled on enabling target mode and then on disabling target mode "
47 "enabled back; "
48 "\"disabled\" - initiator mode will never be enabled; "
Quinn Tranead03852017-01-19 22:28:01 -080049 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
50 "when ready "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040051 "\"enabled\" (default) - initiator mode will always stay enabled.");
52
Quinn Tran99e1b682017-06-02 09:12:03 -070053static int ql_dm_tgt_ex_pct = 0;
Quinn Tranead03852017-01-19 22:28:01 -080054module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
55MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
56 "For Dual Mode (qlini_mode=dual), this parameter determines "
57 "the percentage of exchanges/cmds FW will allocate resources "
58 "for Target mode.");
59
Quinn Tran09620ee2017-06-13 20:47:20 -070060int ql2xuctrlirq = 1;
61module_param(ql2xuctrlirq, int, 0644);
62MODULE_PARM_DESC(ql2xuctrlirq,
63 "User to control IRQ placement via smp_affinity."
64 "Valid with qlini_mode=disabled."
65 "1(default): enable");
66
Arun Easiaa230bc2013-01-30 03:34:39 -050067int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040068
Quinn Tran7cf95f72017-12-28 12:33:28 -080069static int qla_sam_status = SAM_STAT_BUSY;
70static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
Quinn Tran33e79972014-09-25 06:14:55 -040071
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040072/*
73 * From scsi/fc/fc_fcp.h
74 */
75enum fcp_resp_rsp_codes {
76 FCP_TMF_CMPL = 0,
77 FCP_DATA_LEN_INVALID = 1,
78 FCP_CMND_FIELDS_INVALID = 2,
79 FCP_DATA_PARAM_MISMATCH = 3,
80 FCP_TMF_REJECTED = 4,
81 FCP_TMF_FAILED = 5,
82 FCP_TMF_INVALID_LUN = 9,
83};
84
85/*
86 * fc_pri_ta from scsi/fc/fc_fcp.h
87 */
88#define FCP_PTA_SIMPLE 0 /* simple task attribute */
89#define FCP_PTA_HEADQ 1 /* head of queue task attribute */
90#define FCP_PTA_ORDERED 2 /* ordered task attribute */
Masanari Iida6efb3c0a2012-10-26 22:10:54 +090091#define FCP_PTA_ACA 4 /* auto. contingent allegiance */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040092#define FCP_PTA_MASK 7 /* mask for task attribute field */
93#define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
94#define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
95
96/*
97 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
98 * must be called under HW lock and could unlock/lock it inside.
99 * It isn't an issue, since in the current implementation on the time when
100 * those functions are called:
101 *
102 * - Either context is IRQ and only IRQ handler can modify HW data,
103 * including rings related fields,
104 *
105 * - Or access to target mode variables from struct qla_tgt doesn't
106 * cross those functions boundaries, except tgt_stop, which
107 * additionally protected by irq_cmd_count.
108 */
109/* Predefs for callbacks handed to qla2xxx LLD */
110static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500111 struct atio_from_isp *pkt, uint8_t);
Quinn Tran82de8022017-06-13 20:47:17 -0700112static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
113 response_t *pkt);
Quinn Tran5d964832017-01-19 22:27:59 -0800114static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400115 int fn, void *iocb, int flags);
Quinn Tran82de8022017-06-13 20:47:17 -0700116static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
Quinn Trana07100e2015-12-07 19:48:57 -0500117 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
Quinn Tran33e79972014-09-25 06:14:55 -0400118static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
119 struct atio_from_isp *atio, uint16_t status, int qfull);
Joern Engel55a90662014-09-16 16:23:15 -0400120static void qlt_disable_vha(struct scsi_qla_host *vha);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400121static void qlt_clear_tgt_db(struct qla_tgt *tgt);
Quinn Tran82de8022017-06-13 20:47:17 -0700122static void qlt_send_notify_ack(struct qla_qpair *qpair,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400123 struct imm_ntfy_from_isp *ntfy,
124 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
125 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500126static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
127 struct imm_ntfy_from_isp *imm, int ha_locked);
Quinn Tran726b8542017-01-19 22:28:00 -0800128static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
129 fc_port_t *fcport, bool local);
130void qlt_unreg_sess(struct fc_port *sess);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700131static void qlt_24xx_handle_abts(struct scsi_qla_host *,
132 struct abts_recv_from_24xx *);
Quinn Tran82de8022017-06-13 20:47:17 -0700133static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
134 uint16_t);
Quinn Tran6b0431d2018-09-04 14:19:13 -0700135static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
136static inline uint32_t qlt_make_handle(struct qla_qpair *);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700137
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400138/*
139 * Global Variables
140 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400141static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
Quinn Tranb5d15312017-08-30 10:16:49 -0700142struct kmem_cache *qla_tgt_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400143static mempool_t *qla_tgt_mgmt_cmd_mempool;
144static struct workqueue_struct *qla_tgt_wq;
145static DEFINE_MUTEX(qla_tgt_mutex);
146static LIST_HEAD(qla_tgt_glist);
147
Quinn Tranbe251522017-03-15 09:48:49 -0700148static const char *prot_op_str(u32 prot_op)
149{
150 switch (prot_op) {
151 case TARGET_PROT_NORMAL: return "NORMAL";
152 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
153 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
154 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
155 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
156 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
157 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
158 default: return "UNKNOWN";
159 }
160}
161
Alexei Potashnikdf673272015-07-14 16:00:46 -0400162/* This API intentionally takes dest as a parameter, rather than returning
163 * int value to avoid caller forgetting to issue wmb() after the store */
164void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
165{
166 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
167 *dest = atomic_inc_return(&base_vha->generation_tick);
168 /* memory barrier */
169 wmb();
170}
171
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400172/* Might release hw lock, then reaquire!! */
173static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
174{
175 /* Send marker if required */
176 if (unlikely(vha->marker_needed != 0)) {
177 int rc = qla2x00_issue_marker(vha, vha_locked);
Bart Van Asschebd432bb2019-04-11 14:53:17 -0700178
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400179 if (rc != QLA_SUCCESS) {
180 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
181 "qla_target(%d): issue_marker() failed\n",
182 vha->vp_idx);
183 }
184 return rc;
185 }
186 return QLA_SUCCESS;
187}
188
189static inline
190struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
191 uint8_t *d_id)
192{
Quinn Tran482c9dc2017-03-15 09:48:54 -0700193 struct scsi_qla_host *host;
194 uint32_t key = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400195
Quinn Tran482c9dc2017-03-15 09:48:54 -0700196 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
197 (vha->d_id.b.al_pa == d_id[2]))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400198 return vha;
199
Quinn Tran482c9dc2017-03-15 09:48:54 -0700200 key = (uint32_t)d_id[0] << 16;
201 key |= (uint32_t)d_id[1] << 8;
202 key |= (uint32_t)d_id[2];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400203
Quinn Tran482c9dc2017-03-15 09:48:54 -0700204 host = btree_lookup32(&vha->hw->tgt.host_map, key);
205 if (!host)
Quinn Tran604e2e52017-12-28 12:33:37 -0800206 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
Quinn Tran83548fe2017-06-02 09:12:01 -0700207 "Unable to find host %06x\n", key);
Quinn Tran482c9dc2017-03-15 09:48:54 -0700208
209 return host;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400210}
211
212static inline
213struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
214 uint16_t vp_idx)
215{
216 struct qla_hw_data *ha = vha->hw;
217
218 if (vha->vp_idx == vp_idx)
219 return vha;
220
221 BUG_ON(ha->tgt.tgt_vp_map == NULL);
222 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
223 return ha->tgt.tgt_vp_map[vp_idx].vha;
224
225 return NULL;
226}
227
Quinn Tran33e79972014-09-25 06:14:55 -0400228static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
229{
230 unsigned long flags;
231
232 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
233
234 vha->hw->tgt.num_pend_cmds++;
Joe Carnucciofc90ada2016-07-06 11:14:23 -0400235 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
236 vha->qla_stats.stat_max_pend_cmds =
Quinn Tran33e79972014-09-25 06:14:55 -0400237 vha->hw->tgt.num_pend_cmds;
238 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
239}
240static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
241{
242 unsigned long flags;
243
244 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
245 vha->hw->tgt.num_pend_cmds--;
246 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
247}
248
Quinn Tran41dc5292017-01-19 22:28:03 -0800249
250static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
Quinn Tran82de8022017-06-13 20:47:17 -0700251 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran41dc5292017-01-19 22:28:03 -0800252{
253 struct qla_tgt_sess_op *u;
254 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
255 unsigned long flags;
256
257 if (tgt->tgt_stop) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700258 ql_dbg(ql_dbg_async, vha, 0x502c,
259 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
260 vha->vp_idx);
Quinn Tran41dc5292017-01-19 22:28:03 -0800261 goto out_term;
262 }
263
264 u = kzalloc(sizeof(*u), GFP_ATOMIC);
Quinn Tran83548fe2017-06-02 09:12:01 -0700265 if (u == NULL)
Quinn Tran41dc5292017-01-19 22:28:03 -0800266 goto out_term;
Quinn Tran41dc5292017-01-19 22:28:03 -0800267
268 u->vha = vha;
269 memcpy(&u->atio, atio, sizeof(*atio));
270 INIT_LIST_HEAD(&u->cmd_list);
271
272 spin_lock_irqsave(&vha->cmd_list_lock, flags);
273 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
274 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
275
276 schedule_delayed_work(&vha->unknown_atio_work, 1);
277
278out:
279 return;
280
281out_term:
Quinn Tran82de8022017-06-13 20:47:17 -0700282 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800283 goto out;
284}
285
286static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
287 uint8_t ha_locked)
288{
289 struct qla_tgt_sess_op *u, *t;
290 scsi_qla_host_t *host;
291 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
292 unsigned long flags;
293 uint8_t queued = 0;
294
295 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
296 if (u->aborted) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700297 ql_dbg(ql_dbg_async, vha, 0x502e,
298 "Freeing unknown %s %p, because of Abort\n",
Quinn Tran41dc5292017-01-19 22:28:03 -0800299 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700300 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
301 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800302 goto abort;
303 }
304
305 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
306 if (host != NULL) {
Quinn Tran604e2e52017-12-28 12:33:37 -0800307 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
Quinn Tran83548fe2017-06-02 09:12:01 -0700308 "Requeuing unknown ATIO_TYPE7 %p\n", u);
Quinn Tran41dc5292017-01-19 22:28:03 -0800309 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
310 } else if (tgt->tgt_stop) {
Quinn Tran604e2e52017-12-28 12:33:37 -0800311 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
Quinn Tran83548fe2017-06-02 09:12:01 -0700312 "Freeing unknown %s %p, because tgt is being stopped\n",
313 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700314 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
315 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800316 } else {
Quinn Tran604e2e52017-12-28 12:33:37 -0800317 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
Quinn Tran83548fe2017-06-02 09:12:01 -0700318 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
Quinn Tran41dc5292017-01-19 22:28:03 -0800319 if (!queued) {
320 queued = 1;
321 schedule_delayed_work(&vha->unknown_atio_work,
322 1);
323 }
324 continue;
325 }
326
327abort:
328 spin_lock_irqsave(&vha->cmd_list_lock, flags);
329 list_del(&u->cmd_list);
330 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
331 kfree(u);
332 }
333}
334
335void qlt_unknown_atio_work_fn(struct work_struct *work)
336{
337 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
338 struct scsi_qla_host, unknown_atio_work);
339
340 qlt_try_to_dequeue_unknown_atios(vha, 0);
341}
342
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500343static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500344 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400345{
Quinn Tranf83adb62014-04-11 16:54:43 -0400346 ql_dbg(ql_dbg_tgt, vha, 0xe072,
347 "%s: qla_target(%d): type %x ox_id %04x\n",
348 __func__, vha->vp_idx, atio->u.raw.entry_type,
349 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
350
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400351 switch (atio->u.raw.entry_type) {
352 case ATIO_TYPE7:
353 {
354 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
355 atio->u.isp24.fcp_hdr.d_id);
356 if (unlikely(NULL == host)) {
357 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
358 "qla_target(%d): Received ATIO_TYPE7 "
359 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
360 atio->u.isp24.fcp_hdr.d_id[0],
361 atio->u.isp24.fcp_hdr.d_id[1],
362 atio->u.isp24.fcp_hdr.d_id[2]);
Quinn Tran41dc5292017-01-19 22:28:03 -0800363
364
365 qlt_queue_unknown_atio(vha, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400366 break;
367 }
Quinn Tran41dc5292017-01-19 22:28:03 -0800368 if (unlikely(!list_empty(&vha->unknown_atio_list)))
369 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
370
Quinn Tran2f424b92015-12-17 14:57:07 -0500371 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400372 break;
373 }
374
375 case IMMED_NOTIFY_TYPE:
376 {
377 struct scsi_qla_host *host = vha;
378 struct imm_ntfy_from_isp *entry =
379 (struct imm_ntfy_from_isp *)atio;
380
Quinn Tran82de8022017-06-13 20:47:17 -0700381 qlt_issue_marker(vha, ha_locked);
382
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400383 if ((entry->u.isp24.vp_index != 0xFF) &&
384 (entry->u.isp24.nport_handle != 0xFFFF)) {
385 host = qlt_find_host_by_vp_idx(vha,
386 entry->u.isp24.vp_index);
387 if (unlikely(!host)) {
388 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
389 "qla_target(%d): Received "
390 "ATIO (IMMED_NOTIFY_TYPE) "
391 "with unknown vp_index %d\n",
392 vha->vp_idx, entry->u.isp24.vp_index);
393 break;
394 }
395 }
Quinn Tran2f424b92015-12-17 14:57:07 -0500396 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400397 break;
398 }
399
Quinn Tran41dc5292017-01-19 22:28:03 -0800400 case VP_RPT_ID_IOCB_TYPE:
401 qla24xx_report_id_acquisition(vha,
402 (struct vp_rpt_id_entry_24xx *)atio);
403 break;
404
405 case ABTS_RECV_24XX:
406 {
407 struct abts_recv_from_24xx *entry =
408 (struct abts_recv_from_24xx *)atio;
409 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
410 entry->vp_index);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700411 unsigned long flags;
412
Quinn Tran41dc5292017-01-19 22:28:03 -0800413 if (unlikely(!host)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700414 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
Quinn Tran41dc5292017-01-19 22:28:03 -0800415 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
416 "received, with unknown vp_index %d\n",
417 vha->vp_idx, entry->vp_index);
418 break;
419 }
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700420 if (!ha_locked)
421 spin_lock_irqsave(&host->hw->hardware_lock, flags);
422 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
423 if (!ha_locked)
424 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -0800425 break;
Quinn Tran41dc5292017-01-19 22:28:03 -0800426 }
427
428 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
429
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400430 default:
431 ql_dbg(ql_dbg_tgt, vha, 0xe040,
432 "qla_target(%d): Received unknown ATIO atio "
433 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
434 break;
435 }
436
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500437 return false;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400438}
439
Quinn Tran82de8022017-06-13 20:47:17 -0700440void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
441 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400442{
443 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -0400444 case CTIO_CRC2:
445 ql_dbg(ql_dbg_tgt, vha, 0xe073,
446 "qla_target(%d):%s: CRC2 Response pkt\n",
447 vha->vp_idx, __func__);
Bart Van Assche81881862017-12-07 16:02:46 -0800448 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400449 case CTIO_TYPE7:
450 {
451 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
452 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
453 entry->vp_index);
454 if (unlikely(!host)) {
455 ql_dbg(ql_dbg_tgt, vha, 0xe041,
456 "qla_target(%d): Response pkt (CTIO_TYPE7) "
457 "received, with unknown vp_index %d\n",
458 vha->vp_idx, entry->vp_index);
459 break;
460 }
Quinn Tran82de8022017-06-13 20:47:17 -0700461 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400462 break;
463 }
464
465 case IMMED_NOTIFY_TYPE:
466 {
467 struct scsi_qla_host *host = vha;
468 struct imm_ntfy_from_isp *entry =
469 (struct imm_ntfy_from_isp *)pkt;
470
471 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
472 if (unlikely(!host)) {
473 ql_dbg(ql_dbg_tgt, vha, 0xe042,
474 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
475 "received, with unknown vp_index %d\n",
476 vha->vp_idx, entry->u.isp24.vp_index);
477 break;
478 }
Quinn Tran82de8022017-06-13 20:47:17 -0700479 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400480 break;
481 }
482
483 case NOTIFY_ACK_TYPE:
484 {
485 struct scsi_qla_host *host = vha;
486 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
487
488 if (0xFF != entry->u.isp24.vp_index) {
489 host = qlt_find_host_by_vp_idx(vha,
490 entry->u.isp24.vp_index);
491 if (unlikely(!host)) {
492 ql_dbg(ql_dbg_tgt, vha, 0xe043,
493 "qla_target(%d): Response "
494 "pkt (NOTIFY_ACK_TYPE) "
495 "received, with unknown "
496 "vp_index %d\n", vha->vp_idx,
497 entry->u.isp24.vp_index);
498 break;
499 }
500 }
Quinn Tran82de8022017-06-13 20:47:17 -0700501 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400502 break;
503 }
504
505 case ABTS_RECV_24XX:
506 {
507 struct abts_recv_from_24xx *entry =
508 (struct abts_recv_from_24xx *)pkt;
509 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
510 entry->vp_index);
511 if (unlikely(!host)) {
512 ql_dbg(ql_dbg_tgt, vha, 0xe044,
513 "qla_target(%d): Response pkt "
514 "(ABTS_RECV_24XX) received, with unknown "
515 "vp_index %d\n", vha->vp_idx, entry->vp_index);
516 break;
517 }
Quinn Tran82de8022017-06-13 20:47:17 -0700518 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400519 break;
520 }
521
522 case ABTS_RESP_24XX:
523 {
524 struct abts_resp_to_24xx *entry =
525 (struct abts_resp_to_24xx *)pkt;
526 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
527 entry->vp_index);
528 if (unlikely(!host)) {
529 ql_dbg(ql_dbg_tgt, vha, 0xe045,
530 "qla_target(%d): Response pkt "
531 "(ABTS_RECV_24XX) received, with unknown "
532 "vp_index %d\n", vha->vp_idx, entry->vp_index);
533 break;
534 }
Quinn Tran82de8022017-06-13 20:47:17 -0700535 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400536 break;
537 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400538 default:
Quinn Tran82de8022017-06-13 20:47:17 -0700539 qlt_response_pkt(vha, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400540 break;
541 }
542
543}
544
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500545/*
546 * All qlt_plogi_ack_t operations are protected by hardware_lock
547 */
Quinn Tran726b8542017-01-19 22:28:00 -0800548static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
549 struct imm_ntfy_from_isp *ntfy, int type)
550{
551 struct qla_work_evt *e;
Bart Van Asschebd432bb2019-04-11 14:53:17 -0700552
Quinn Tran726b8542017-01-19 22:28:00 -0800553 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
554 if (!e)
555 return QLA_FUNCTION_FAILED;
556
557 e->u.nack.fcport = fcport;
558 e->u.nack.type = type;
559 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
560 return qla2x00_post_work(vha, e);
561}
562
563static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800564void qla2x00_async_nack_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800565{
Quinn Tran726b8542017-01-19 22:28:00 -0800566 struct srb *sp = (struct srb *)s;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800567 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800568 unsigned long flags;
569
Quinn Tran83548fe2017-06-02 09:12:01 -0700570 ql_dbg(ql_dbg_disc, vha, 0x20f2,
571 "Async done-%s res %x %8phC type %d\n",
572 sp->name, res, sp->fcport->port_name, sp->type);
Quinn Tran726b8542017-01-19 22:28:00 -0800573
574 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
575 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -0700576 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran726b8542017-01-19 22:28:00 -0800577
578 switch (sp->type) {
579 case SRB_NACK_PLOGI:
580 sp->fcport->login_gen++;
581 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
582 sp->fcport->logout_on_delete = 1;
Quinn Tran5b334692017-03-15 09:48:48 -0700583 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
Quinn Tran35158322017-08-30 10:16:50 -0700584 sp->fcport->send_els_logo = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800585 break;
586
587 case SRB_NACK_PRLI:
588 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
589 sp->fcport->deleted = 0;
Quinn Tran35158322017-08-30 10:16:50 -0700590 sp->fcport->send_els_logo = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800591
592 if (!sp->fcport->login_succ &&
593 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
594 sp->fcport->login_succ = 1;
595
596 vha->fcport_count++;
Quinn Tran0aca7782018-09-04 14:19:16 -0700597 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Trancd4ed6b2018-08-31 11:24:31 -0700598 qla24xx_sched_upd_fcport(sp->fcport);
Quinn Tran0aca7782018-09-04 14:19:16 -0700599 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tran29528492018-05-01 09:01:45 -0700600 } else {
601 sp->fcport->login_retry = 0;
602 sp->fcport->disc_state = DSC_LOGIN_COMPLETE;
603 sp->fcport->deleted = 0;
604 sp->fcport->logout_on_delete = 1;
Quinn Tran726b8542017-01-19 22:28:00 -0800605 }
606 break;
607
608 case SRB_NACK_LOGO:
609 sp->fcport->login_gen++;
610 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
611 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
612 break;
613 }
614 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
615
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800616 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800617}
618
619int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
620 struct imm_ntfy_from_isp *ntfy, int type)
621{
622 int rval = QLA_FUNCTION_FAILED;
623 srb_t *sp;
624 char *c = NULL;
625
626 fcport->flags |= FCF_ASYNC_SENT;
627 switch (type) {
628 case SRB_NACK_PLOGI:
629 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
630 c = "PLOGI";
631 break;
632 case SRB_NACK_PRLI:
633 fcport->fw_login_state = DSC_LS_PRLI_PEND;
Quinn Tranec7193e2017-03-15 09:48:55 -0700634 fcport->deleted = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800635 c = "PRLI";
636 break;
637 case SRB_NACK_LOGO:
638 fcport->fw_login_state = DSC_LS_LOGO_PEND;
639 c = "LOGO";
640 break;
641 }
642
643 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
644 if (!sp)
645 goto done;
646
647 sp->type = type;
648 sp->name = "nack";
649
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000650 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
Quinn Tran726b8542017-01-19 22:28:00 -0800651 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
652
653 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
Quinn Tran726b8542017-01-19 22:28:00 -0800654 sp->done = qla2x00_async_nack_sp_done;
655
Quinn Tran83548fe2017-06-02 09:12:01 -0700656 ql_dbg(ql_dbg_disc, vha, 0x20f4,
657 "Async-%s %8phC hndl %x %s\n",
658 sp->name, fcport->port_name, sp->handle, c);
Quinn Tran726b8542017-01-19 22:28:00 -0800659
Bill Kuzejaf233e8c2019-02-14 10:52:29 -0500660 rval = qla2x00_start_sp(sp);
661 if (rval != QLA_SUCCESS)
662 goto done_free_sp;
663
Quinn Tran726b8542017-01-19 22:28:00 -0800664 return rval;
665
666done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800667 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800668done:
669 fcport->flags &= ~FCF_ASYNC_SENT;
670 return rval;
671}
672
673void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
674{
675 fc_port_t *t;
Quinn Tran726b8542017-01-19 22:28:00 -0800676
677 switch (e->u.nack.type) {
678 case SRB_NACK_PRLI:
Quinn Tran1021f0b2019-01-24 23:23:45 -0800679 t = e->u.nack.fcport;
680 flush_work(&t->del_work);
681 flush_work(&t->free_work);
Quinn Tran726b8542017-01-19 22:28:00 -0800682 mutex_lock(&vha->vha_tgt.tgt_mutex);
683 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
684 mutex_unlock(&vha->vha_tgt.tgt_mutex);
685 if (t) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700686 ql_log(ql_log_info, vha, 0xd034,
Quinn Tran726b8542017-01-19 22:28:00 -0800687 "%s create sess success %p", __func__, t);
Quinn Tran726b8542017-01-19 22:28:00 -0800688 /* create sess has an extra kref */
689 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800690 }
691 break;
692 }
693 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
Bart Van Assche845bbb02019-04-11 14:53:18 -0700694 (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
Quinn Tran726b8542017-01-19 22:28:00 -0800695}
696
697void qla24xx_delete_sess_fn(struct work_struct *work)
698{
699 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
700 struct qla_hw_data *ha = fcport->vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800701
702 if (fcport->se_sess) {
703 ha->tgt.tgt_ops->shutdown_sess(fcport);
704 ha->tgt.tgt_ops->put_sess(fcport);
705 } else {
706 qlt_unreg_sess(fcport);
707 }
Quinn Tran726b8542017-01-19 22:28:00 -0800708}
709
710/*
711 * Called from qla2x00_reg_remote_port()
712 */
713void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
714{
715 struct qla_hw_data *ha = vha->hw;
716 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
717 struct fc_port *sess = fcport;
718 unsigned long flags;
719
720 if (!vha->hw->tgt.tgt_ops)
721 return;
722
723 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
724 if (tgt->tgt_stop) {
725 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
726 return;
727 }
728
729 if (fcport->disc_state == DSC_DELETE_PEND) {
730 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
731 return;
732 }
733
734 if (!sess->se_sess) {
735 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
736
737 mutex_lock(&vha->vha_tgt.tgt_mutex);
738 sess = qlt_create_sess(vha, fcport, false);
739 mutex_unlock(&vha->vha_tgt.tgt_mutex);
740
741 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
742 } else {
743 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
744 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
745 return;
746 }
747
748 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700749 ql_dbg(ql_dbg_disc, vha, 0x2107,
Quinn Tran726b8542017-01-19 22:28:00 -0800750 "%s: kref_get fail sess %8phC \n",
751 __func__, sess->port_name);
752 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
753 return;
754 }
755
756 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
757 "qla_target(%u): %ssession for port %8phC "
758 "(loop ID %d) reappeared\n", vha->vp_idx,
759 sess->local ? "local " : "", sess->port_name, sess->loop_id);
760
761 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
762 "Reappeared sess %p\n", sess);
763
764 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
765 fcport->loop_id,
766 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
767 }
768
769 if (sess && sess->local) {
770 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
771 "qla_target(%u): local session for "
772 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
773 fcport->port_name, sess->loop_id);
774 sess->local = 0;
775 }
Quinn Tran726b8542017-01-19 22:28:00 -0800776 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Bart Van Assche300ec742019-04-17 14:44:41 -0700777
778 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran726b8542017-01-19 22:28:00 -0800779}
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500780
781/*
782 * This is a zero-base ref-counting solution, since hardware_lock
783 * guarantees that ref_count is not modified concurrently.
784 * Upon successful return content of iocb is undefined
785 */
Quinn Tran5d964832017-01-19 22:27:59 -0800786static struct qlt_plogi_ack_t *
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500787qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
788 struct imm_ntfy_from_isp *iocb)
789{
Quinn Tran5d964832017-01-19 22:27:59 -0800790 struct qlt_plogi_ack_t *pla;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500791
Bart Van Assche57bf5952019-08-08 20:01:34 -0700792 lockdep_assert_held(&vha->hw->hardware_lock);
793
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500794 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
795 if (pla->id.b24 == id->b24) {
Quinn Tran48acad02018-08-02 13:16:44 -0700796 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
797 "%s %d %8phC Term INOT due to new INOT",
798 __func__, __LINE__,
799 pla->iocb.u.isp24.port_name);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500800 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
Quinn Tran5d964832017-01-19 22:27:59 -0800801 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500802 return pla;
803 }
804 }
805
806 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
807 if (!pla) {
808 ql_dbg(ql_dbg_async, vha, 0x5088,
809 "qla_target(%d): Allocation of plogi_ack failed\n",
810 vha->vp_idx);
811 return NULL;
812 }
813
Quinn Tran5d964832017-01-19 22:27:59 -0800814 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500815 pla->id = *id;
816 list_add_tail(&pla->list, &vha->plogi_ack_list);
817
818 return pla;
819}
820
Quinn Tran726b8542017-01-19 22:28:00 -0800821void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -0800822 struct qlt_plogi_ack_t *pla)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500823{
Quinn Tran5d964832017-01-19 22:27:59 -0800824 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Quinn Tran726b8542017-01-19 22:28:00 -0800825 port_id_t port_id;
826 uint16_t loop_id;
827 fc_port_t *fcport = pla->fcport;
828
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500829 BUG_ON(!pla->ref_count);
830 pla->ref_count--;
831
832 if (pla->ref_count)
833 return;
834
Quinn Tran726b8542017-01-19 22:28:00 -0800835 ql_dbg(ql_dbg_disc, vha, 0x5089,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500836 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
Quinn Tran5d964832017-01-19 22:27:59 -0800837 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
838 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
839 iocb->u.isp24.port_id[0],
840 le16_to_cpu(iocb->u.isp24.nport_handle),
841 iocb->u.isp24.exchange_address, iocb->ox_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800842
843 port_id.b.domain = iocb->u.isp24.port_id[2];
844 port_id.b.area = iocb->u.isp24.port_id[1];
845 port_id.b.al_pa = iocb->u.isp24.port_id[0];
846 port_id.b.rsvd_1 = 0;
847
848 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
849
850 fcport->loop_id = loop_id;
851 fcport->d_id = port_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800852 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
853 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
854 else
855 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
Quinn Tran726b8542017-01-19 22:28:00 -0800856
857 list_for_each_entry(fcport, &vha->vp_fcports, list) {
858 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
859 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
860 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
861 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
862 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500863
864 list_del(&pla->list);
865 kmem_cache_free(qla_tgt_plogi_cachep, pla);
866}
867
Quinn Tran726b8542017-01-19 22:28:00 -0800868void
Quinn Tran5d964832017-01-19 22:27:59 -0800869qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
870 struct fc_port *sess, enum qlt_plogi_link_t link)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500871{
Quinn Tran5d964832017-01-19 22:27:59 -0800872 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500873 /* Inc ref_count first because link might already be pointing at pla */
874 pla->ref_count++;
875
Quinn Tran726b8542017-01-19 22:28:00 -0800876 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
877 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
878 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
879 sess, link, sess->port_name,
880 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
881 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
882 pla->ref_count, pla, link);
883
Quinn Tran5ef696a2017-12-04 14:45:05 -0800884 if (link == QLT_PLOGI_LINK_CONFLICT) {
885 switch (sess->disc_state) {
886 case DSC_DELETED:
887 case DSC_DELETE_PEND:
888 pla->ref_count--;
889 return;
890 default:
891 break;
892 }
893 }
894
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500895 if (sess->plogi_link[link])
896 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
897
Quinn Tran726b8542017-01-19 22:28:00 -0800898 if (link == QLT_PLOGI_LINK_SAME_WWN)
899 pla->fcport = sess;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500900
901 sess->plogi_link[link] = pla;
902}
903
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500904typedef struct {
905 /* These fields must be initialized by the caller */
906 port_id_t id;
907 /*
908 * number of cmds dropped while we were waiting for
909 * initiator to ack LOGO initialize to 1 if LOGO is
910 * triggered by a command, otherwise, to 0
911 */
912 int cmd_count;
913
914 /* These fields are used by callee */
915 struct list_head list;
916} qlt_port_logo_t;
917
918static void
919qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
920{
921 qlt_port_logo_t *tmp;
922 int res;
923
924 mutex_lock(&vha->vha_tgt.tgt_mutex);
925
926 list_for_each_entry(tmp, &vha->logo_list, list) {
927 if (tmp->id.b24 == logo->id.b24) {
928 tmp->cmd_count += logo->cmd_count;
929 mutex_unlock(&vha->vha_tgt.tgt_mutex);
930 return;
931 }
932 }
933
934 list_add_tail(&logo->list, &vha->logo_list);
935
936 mutex_unlock(&vha->vha_tgt.tgt_mutex);
937
938 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
939
940 mutex_lock(&vha->vha_tgt.tgt_mutex);
941 list_del(&logo->list);
942 mutex_unlock(&vha->vha_tgt.tgt_mutex);
943
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500944 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
945 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
946 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
947 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500948}
949
Darren Trapp9dd96862018-03-20 23:09:32 -0700950void qlt_free_session_done(struct work_struct *work)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400951{
Quinn Tran5d964832017-01-19 22:27:59 -0800952 struct fc_port *sess = container_of(work, struct fc_port,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400953 free_work);
954 struct qla_tgt *tgt = sess->tgt;
955 struct scsi_qla_host *vha = sess->vha;
956 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400957 unsigned long flags;
958 bool logout_started = false;
Quinn Tran41dc5292017-01-19 22:28:03 -0800959 scsi_qla_host_t *base_vha;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800960 struct qlt_plogi_ack_t *own =
961 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400962
963 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
964 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500965 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400966 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
Quinn Tran37cacc02017-01-19 22:27:58 -0800967 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400968 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500969 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400970
Quinn Tran726b8542017-01-19 22:28:00 -0800971 if (!IS_SW_RESV_ADDR(sess->d_id)) {
Quinn Tran21374902019-04-23 14:52:35 -0700972 qla2x00_mark_device_lost(vha, sess, 0, 0);
973
Quinn Tran41dc5292017-01-19 22:28:03 -0800974 if (sess->send_els_logo) {
975 qlt_port_logo_t logo;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500976
Quinn Tran41dc5292017-01-19 22:28:03 -0800977 logo.id = sess->d_id;
978 logo.cmd_count = 0;
Quinn Trancb97f2c2018-08-02 13:16:51 -0700979 if (!own)
980 qlt_send_first_logo(vha, &logo);
Himanshu Madhania2390342018-01-22 12:04:20 -0800981 sess->send_els_logo = 0;
Quinn Tran41dc5292017-01-19 22:28:03 -0800982 }
983
Quinn Tranba743f92017-12-04 14:45:12 -0800984 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800985 int rc;
986
Quinn Tran9cd883f2017-12-28 12:33:24 -0800987 if (!own ||
988 (own &&
989 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
990 rc = qla2x00_post_async_logout_work(vha, sess,
991 NULL);
992 if (rc != QLA_SUCCESS)
993 ql_log(ql_log_warn, vha, 0xf085,
994 "Schedule logo failed sess %p rc %d\n",
995 sess, rc);
996 else
997 logout_started = true;
998 } else if (own && (own->iocb.u.isp24.status_subcode ==
999 ELS_PRLI) && ha->flags.rida_fmt2) {
1000 rc = qla2x00_post_async_prlo_work(vha, sess,
1001 NULL);
1002 if (rc != QLA_SUCCESS)
1003 ql_log(ql_log_warn, vha, 0xf085,
1004 "Schedule PRLO failed sess %p rc %d\n",
1005 sess, rc);
1006 else
1007 logout_started = true;
1008 }
Quinn Tranbaf23ed2019-06-16 08:05:53 -07001009 } /* if sess->logout_on_delete */
1010
1011 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1012 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1013 sess->nvme_flag |= NVME_FLAG_DELETING;
1014 qla_nvme_unregister_remote_port(sess);
Quinn Tran41dc5292017-01-19 22:28:03 -08001015 }
Quinn Tran726b8542017-01-19 22:28:00 -08001016 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001017
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001018 /*
1019 * Release the target session for FC Nexus from fabric module code.
1020 */
1021 if (sess->se_sess != NULL)
1022 ha->tgt.tgt_ops->free_session(sess);
1023
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001024 if (logout_started) {
1025 bool traced = false;
1026
Mark Rutland6aa7de02017-10-23 14:07:29 -07001027 while (!READ_ONCE(sess->logout_completed)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001028 if (!traced) {
1029 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1030 "%s: waiting for sess %p logout\n",
1031 __func__, sess);
1032 traced = true;
1033 }
1034 msleep(100);
1035 }
1036
Quinn Tran726b8542017-01-19 22:28:00 -08001037 ql_dbg(ql_dbg_disc, vha, 0xf087,
Quinn Tran9cd883f2017-12-28 12:33:24 -08001038 "%s: sess %p logout completed\n", __func__, sess);
Quinn Tran41dc5292017-01-19 22:28:03 -08001039 }
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);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001045 }
1046
Quinn Tran726b8542017-01-19 22:28:00 -08001047 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1048 if (sess->se_sess) {
1049 sess->se_sess = NULL;
1050 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1051 tgt->sess_count--;
1052 }
1053
1054 sess->disc_state = DSC_DELETED;
1055 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1056 sess->deleted = QLA_SESS_DELETED;
Quinn Tran726b8542017-01-19 22:28:00 -08001057
1058 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1059 vha->fcport_count--;
1060 sess->login_succ = 0;
1061 }
1062
Quinn Tranba743f92017-12-04 14:45:12 -08001063 qla2x00_clear_loop_id(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08001064
1065 if (sess->conflict) {
1066 sess->conflict->login_pause = 0;
1067 sess->conflict = NULL;
1068 if (!test_bit(UNLOADING, &vha->dpc_flags))
1069 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1070 }
1071
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001072 {
Quinn Tran5d964832017-01-19 22:27:59 -08001073 struct qlt_plogi_ack_t *con =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001074 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
Quinn Tran5d964832017-01-19 22:27:59 -08001075 struct imm_ntfy_from_isp *iocb;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07001076
Quinn Tran48acad02018-08-02 13:16:44 -07001077 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001078
1079 if (con) {
Quinn Tran5d964832017-01-19 22:27:59 -08001080 iocb = &con->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001081 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
Quinn Tran5d964832017-01-19 22:27:59 -08001082 "se_sess %p / sess %p port %8phC is gone,"
1083 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1084 sess->se_sess, sess, sess->port_name,
1085 own ? "releasing own PLOGI" : "no own PLOGI pending",
1086 own ? own->ref_count : -1,
1087 iocb->u.isp24.port_name, con->ref_count);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001088 qlt_plogi_ack_unref(vha, con);
Quinn Tran726b8542017-01-19 22:28:00 -08001089 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001090 } else {
1091 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1092 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1093 sess->se_sess, sess, sess->port_name,
1094 own ? "releasing own PLOGI" :
1095 "no own PLOGI pending",
1096 own ? own->ref_count : -1);
1097 }
1098
Quinn Tran726b8542017-01-19 22:28:00 -08001099 if (own) {
1100 sess->fw_login_state = DSC_LS_PLOGI_PEND;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001101 qlt_plogi_ack_unref(vha, own);
Quinn Tran726b8542017-01-19 22:28:00 -08001102 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1103 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001104 }
Quinn Tran1ae634e2017-12-28 12:33:44 -08001105
Quinn Tran5d964832017-01-19 22:27:59 -08001106 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1107
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001108 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
Quinn Tran726b8542017-01-19 22:28:00 -08001109 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1110 sess, sess->port_name, vha->fcport_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001111
Quinn Tran726b8542017-01-19 22:28:00 -08001112 if (tgt && (tgt->sess_count == 0))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001113 wake_up_all(&tgt->waitQ);
Quinn Tran726b8542017-01-19 22:28:00 -08001114
1115 if (vha->fcport_count == 0)
1116 wake_up_all(&vha->fcport_waitQ);
1117
Quinn Tran41dc5292017-01-19 22:28:03 -08001118 base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran1ae634e2017-12-28 12:33:44 -08001119
1120 sess->free_pending = 0;
1121
Quinn Tran41dc5292017-01-19 22:28:03 -08001122 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1123 return;
1124
Quinn Trana4239942017-12-28 12:33:26 -08001125 if ((!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 }
Quinn Tran726b8542017-01-19 22:28:00 -08001137 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001138}
1139
Quinn Tran75601512015-12-17 14:57:04 -05001140/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08001141void qlt_unreg_sess(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001142{
1143 struct scsi_qla_host *vha = sess->vha;
Quinn Tran1ae634e2017-12-28 12:33:44 -08001144 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001145
Quinn Tran83548fe2017-06-02 09:12:01 -07001146 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
Quinn Tran5d964832017-01-19 22:27:59 -08001147 "%s sess %p for deletion %8phC\n",
1148 __func__, sess, sess->port_name);
1149
Quinn Tran1ae634e2017-12-28 12:33:44 -08001150 spin_lock_irqsave(&sess->vha->work_lock, flags);
1151 if (sess->free_pending) {
1152 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1153 return;
1154 }
1155 sess->free_pending = 1;
1156 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1157
Quinn Tran36c78452016-02-04 11:45:18 -05001158 if (sess->se_sess)
1159 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001160
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001161 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Tran726b8542017-01-19 22:28:00 -08001162 sess->disc_state = DSC_DELETE_PEND;
1163 sess->last_rscn_gen = sess->rscn_gen;
1164 sess->last_login_gen = sess->login_gen;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001165
Quinn Tranbaf23ed2019-06-16 08:05:53 -07001166 INIT_WORK(&sess->free_work, qlt_free_session_done);
1167 schedule_work(&sess->free_work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001168}
Quinn Tran5d964832017-01-19 22:27:59 -08001169EXPORT_SYMBOL(qlt_unreg_sess);
Quinn Tran75601512015-12-17 14:57:04 -05001170
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001171static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1172{
1173 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001174 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001175 uint16_t loop_id;
1176 int res = 0;
1177 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -05001178 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001179
1180 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1181 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001182 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -04001183 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -05001184 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -04001185 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001186 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001187 } else {
Quinn Tran75601512015-12-17 14:57:04 -05001188 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001189 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05001190 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001191 }
1192
1193 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1194 "Using sess for qla_tgt_reset: %p\n", sess);
1195 if (!sess) {
1196 res = -ESRCH;
1197 return res;
1198 }
1199
1200 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001201 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1202 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001203 mcmd, loop_id);
1204
Quinn Tranbb1181c2016-12-23 18:06:05 -08001205 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001206}
1207
Quinn Tran726b8542017-01-19 22:28:00 -08001208static void qla24xx_chk_fcp_state(struct fc_port *sess)
1209{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001210 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
Quinn Tran726b8542017-01-19 22:28:00 -08001211 sess->logout_on_delete = 0;
1212 sess->logo_ack_needed = 0;
1213 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
Quinn Tran726b8542017-01-19 22:28:00 -08001214 }
1215}
1216
Quinn Tran94cff6e2017-12-28 12:33:42 -08001217void qlt_schedule_sess_for_deletion(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001218{
1219 struct qla_tgt *tgt = sess->tgt;
Quinn Trand8630bb2017-12-28 12:33:43 -08001220 unsigned long flags;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001221 u16 sec;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001222
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001223 switch (sess->disc_state) {
1224 case DSC_DELETE_PEND:
Quinn Tran726b8542017-01-19 22:28:00 -08001225 return;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001226 case DSC_DELETED:
Quinn Tran726b8542017-01-19 22:28:00 -08001227 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1228 wake_up_all(&tgt->waitQ);
1229 if (sess->vha->fcport_count == 0)
1230 wake_up_all(&sess->vha->fcport_waitQ);
1231
1232 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1233 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001234 return;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001235 break;
1236 case DSC_UPD_FCPORT:
1237 /*
1238 * This port is not done reporting to upper layer.
1239 * let it finish
1240 */
1241 sess->next_disc_state = DSC_DELETE_PEND;
1242 sec = jiffies_to_msecs(jiffies -
1243 sess->jiffies_at_registration)/1000;
1244 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1245 sess->sec_since_registration = sec;
1246 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1247 "%s %8phC : Slow Rport registration(%d Sec)\n",
1248 __func__, sess->port_name, sec);
1249 }
1250 return;
1251 default:
1252 break;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001253 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001254
Mikhail Malygin49d7bd32018-06-13 13:05:57 +00001255 spin_lock_irqsave(&sess->vha->work_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001256 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
Mikhail Malygin49d7bd32018-06-13 13:05:57 +00001257 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001258 return;
1259 }
Quinn Tran726b8542017-01-19 22:28:00 -08001260 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Mikhail Malygin49d7bd32018-06-13 13:05:57 +00001261 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001262
1263 sess->disc_state = DSC_DELETE_PEND;
1264
Quinn Tran726b8542017-01-19 22:28:00 -08001265 qla24xx_chk_fcp_state(sess);
1266
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001267 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
Quinn Tran5c640052018-09-26 22:05:14 -07001268 "Scheduling sess %p for deletion %8phC\n",
1269 sess, sess->port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001270
Quinn Trana01c77d2017-12-04 14:44:58 -08001271 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001272 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
Quinn Tran726b8542017-01-19 22:28:00 -08001273}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001274
Joern Engelc5701042014-09-16 16:23:14 -04001275static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001276{
Quinn Tran5d964832017-01-19 22:27:59 -08001277 struct fc_port *sess;
1278 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001279
Quinn Tran5d964832017-01-19 22:27:59 -08001280 list_for_each_entry(sess, &vha->vp_fcports, list) {
1281 if (sess->se_sess)
Quinn Tran94cff6e2017-12-28 12:33:42 -08001282 qlt_schedule_sess_for_deletion(sess);
Quinn Tran5d964832017-01-19 22:27:59 -08001283 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001284
1285 /* At this point tgt could be already dead */
1286}
1287
1288static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1289 uint16_t *loop_id)
1290{
1291 struct qla_hw_data *ha = vha->hw;
1292 dma_addr_t gid_list_dma;
Bart Van Assche9c17c3b2019-08-08 20:01:35 -07001293 struct gid_list_info *gid_list, *gid;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001294 int res, rc, i;
1295 uint16_t entries;
1296
1297 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1298 &gid_list_dma, GFP_KERNEL);
1299 if (!gid_list) {
1300 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1301 "qla_target(%d): DMA Alloc failed of %u\n",
1302 vha->vp_idx, qla2x00_gid_list_size(ha));
1303 return -ENOMEM;
1304 }
1305
1306 /* Get list of logged in devices */
Quinn Tran15f30a52017-03-15 09:48:52 -07001307 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001308 if (rc != QLA_SUCCESS) {
1309 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1310 "qla_target(%d): get_id_list() failed: %x\n",
1311 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001312 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001313 goto out_free_id_list;
1314 }
1315
Bart Van Assche9c17c3b2019-08-08 20:01:35 -07001316 gid = gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001317 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001318 for (i = 0; i < entries; i++) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001319 if ((gid->al_pa == s_id[2]) &&
1320 (gid->area == s_id[1]) &&
1321 (gid->domain == s_id[0])) {
1322 *loop_id = le16_to_cpu(gid->loop_id);
1323 res = 0;
1324 break;
1325 }
Bart Van Assche9c17c3b2019-08-08 20:01:35 -07001326 gid = (void *)gid + ha->gid_list_info_size;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001327 }
1328
1329out_free_id_list:
1330 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1331 gid_list, gid_list_dma);
1332 return res;
1333}
1334
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001335/*
1336 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1337 * Caller must put it.
1338 */
Quinn Tran5d964832017-01-19 22:27:59 -08001339static struct fc_port *qlt_create_sess(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001340 struct scsi_qla_host *vha,
1341 fc_port_t *fcport,
1342 bool local)
1343{
1344 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -08001345 struct fc_port *sess = fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001346 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001347
Quinn Tran726b8542017-01-19 22:28:00 -08001348 if (vha->vha_tgt.qla_tgt->tgt_stop)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001349 return NULL;
Quinn Tran726b8542017-01-19 22:28:00 -08001350
1351 if (fcport->se_sess) {
1352 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001353 ql_dbg(ql_dbg_disc, vha, 0x20f6,
Quinn Tran726b8542017-01-19 22:28:00 -08001354 "%s: kref_get_unless_zero failed for %8phC\n",
1355 __func__, sess->port_name);
1356 return NULL;
1357 }
1358 return fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001359 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001360 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001361 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001362
Quinn Tran726b8542017-01-19 22:28:00 -08001363 /*
1364 * Under normal circumstances we want to logout from firmware when
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001365 * session eventually ends and release corresponding nport handle.
1366 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
Quinn Tran726b8542017-01-19 22:28:00 -08001367 * code will adjust these flags as necessary.
1368 */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001369 sess->logout_on_delete = 1;
1370 sess->keep_nport_handle = 0;
Quinn Tran726b8542017-01-19 22:28:00 -08001371 sess->logout_completed = 0;
1372
1373 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1374 &fcport->port_name[0], sess) < 0) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001375 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
Quinn Tran726b8542017-01-19 22:28:00 -08001376 "(%d) %8phC check_initiator_node_acl failed\n",
1377 vha->vp_idx, fcport->port_name);
1378 return NULL;
1379 } else {
1380 kref_init(&fcport->sess_kref);
1381 /*
1382 * Take an extra reference to ->sess_kref here to handle
1383 * fc_port access across ->tgt.sess_lock reaquire.
1384 */
1385 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001386 ql_dbg(ql_dbg_disc, vha, 0x20f7,
Quinn Tran726b8542017-01-19 22:28:00 -08001387 "%s: kref_get_unless_zero failed for %8phC\n",
1388 __func__, sess->port_name);
1389 return NULL;
1390 }
1391
1392 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1393 if (!IS_SW_RESV_ADDR(sess->d_id))
1394 vha->vha_tgt.qla_tgt->sess_count++;
1395
1396 qlt_do_generation_tick(vha, &sess->generation);
1397 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1398 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001399
1400 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
Quinn Tran726b8542017-01-19 22:28:00 -08001401 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1402 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1403 vha->vha_tgt.qla_tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001404
1405 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001406 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1407 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1408 vha->vp_idx, local ? "local " : "", fcport->port_name,
Quinn Tran37cacc02017-01-19 22:27:58 -08001409 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1410 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001411
1412 return sess;
1413}
1414
1415/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001416 * max_gen - specifies maximum session generation
1417 * at which this deletion requestion is still valid
1418 */
1419void
1420qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001421{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001422 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08001423 struct fc_port *sess = fcport;
Quinn Tran75601512015-12-17 14:57:04 -05001424 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001425
1426 if (!vha->hw->tgt.tgt_ops)
1427 return;
1428
Roland Dreierb2032fd2015-07-14 16:00:42 -04001429 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001430 return;
1431
Quinn Tran75601512015-12-17 14:57:04 -05001432 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001433 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001434 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001435 return;
1436 }
Quinn Tran5d964832017-01-19 22:27:59 -08001437 if (!sess->se_sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001438 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001439 return;
1440 }
1441
Alexei Potashnikdf673272015-07-14 16:00:46 -04001442 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001443 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001444 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1445 "Ignoring stale deletion request for se_sess %p / sess %p"
1446 " for port %8phC, req_gen %d, sess_gen %d\n",
1447 sess->se_sess, sess, sess->port_name, max_gen,
1448 sess->generation);
1449 return;
1450 }
1451
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001452 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1453
1454 sess->local = 1;
Quinn Tran75601512015-12-17 14:57:04 -05001455 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001456 qlt_schedule_sess_for_deletion(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001457}
1458
1459static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1460{
1461 struct qla_hw_data *ha = tgt->ha;
1462 unsigned long flags;
1463 int res;
1464 /*
1465 * We need to protect against race, when tgt is freed before or
1466 * inside wake_up()
1467 */
Quinn Tran726b8542017-01-19 22:28:00 -08001468 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001469 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
Quinn Tran5d964832017-01-19 22:27:59 -08001470 "tgt %p, sess_count=%d\n",
1471 tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001472 res = (tgt->sess_count == 0);
Quinn Tran726b8542017-01-19 22:28:00 -08001473 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001474
1475 return res;
1476}
1477
1478/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001479int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001480{
1481 struct scsi_qla_host *vha = tgt->vha;
1482 struct qla_hw_data *ha = tgt->ha;
1483 unsigned long flags;
1484
Quinn Tranb6faaaf2018-09-04 14:19:09 -07001485 mutex_lock(&ha->optrom_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001486 mutex_lock(&qla_tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001487
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001488 if (tgt->tgt_stop || tgt->tgt_stopped) {
1489 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1490 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001491 mutex_unlock(&qla_tgt_mutex);
Quinn Tranb6faaaf2018-09-04 14:19:09 -07001492 mutex_unlock(&ha->optrom_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001493 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001494 }
1495
Quinn Tran3a33dc92017-06-02 09:12:04 -07001496 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001497 vha->host_no, vha);
1498 /*
1499 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1500 * Lock is needed, because we still can get an incoming packet.
1501 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001502 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001503 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001504 qlt_clear_tgt_db(tgt);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001505 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001506 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001507
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001508 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1509 "Waiting for sess works (tgt %p)", tgt);
1510 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1511 while (!list_empty(&tgt->sess_works_list)) {
1512 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1513 flush_scheduled_work();
1514 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1515 }
1516 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1517
1518 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001519 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001520
Joe Carnucciob85e0952017-08-23 15:05:11 -07001521 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001522
1523 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001524 if (!ha->flags.host_shutting_down &&
1525 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001526 qlt_disable_vha(vha);
1527
1528 /* Wait for sessions to clear out (just in case) */
Joe Carnucciob85e0952017-08-23 15:05:11 -07001529 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Quinn Tranb6faaaf2018-09-04 14:19:09 -07001530 mutex_unlock(&ha->optrom_mutex);
1531
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001532 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001533}
1534EXPORT_SYMBOL(qlt_stop_phase1);
1535
1536/* Called by tcm_qla2xxx configfs code */
1537void qlt_stop_phase2(struct qla_tgt *tgt)
1538{
Quinn Tran3a33dc92017-06-02 09:12:04 -07001539 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001540
1541 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001542 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001543 "Already in tgt->tgt_stopped state\n");
1544 dump_stack();
1545 return;
1546 }
Quinn Tran3a33dc92017-06-02 09:12:04 -07001547 if (!tgt->tgt_stop) {
1548 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1549 "%s: phase1 stop is not completed\n", __func__);
1550 dump_stack();
1551 return;
1552 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001553
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001554 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001555 tgt->tgt_stop = 0;
1556 tgt->tgt_stopped = 1;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001557 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001558
Quinn Tran3a33dc92017-06-02 09:12:04 -07001559 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001560 tgt);
Quinn Tran0645cb82018-09-11 10:18:18 -07001561
1562 switch (vha->qlini_mode) {
1563 case QLA2XXX_INI_MODE_EXCLUSIVE:
1564 vha->flags.online = 1;
1565 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1566 break;
1567 default:
1568 break;
1569 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001570}
1571EXPORT_SYMBOL(qlt_stop_phase2);
1572
1573/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001574static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001575{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001576 scsi_qla_host_t *vha = tgt->vha;
Quinn Trane326d222017-06-13 20:47:18 -07001577 void *node;
1578 u64 key = 0;
1579 u16 i;
1580 struct qla_qpair_hint *h;
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001581 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001582
Bart Van Assche8b219002019-08-08 20:01:38 -07001583 if (!tgt->tgt_stop && !tgt->tgt_stopped)
Quinn Tran3a33dc92017-06-02 09:12:04 -07001584 qlt_stop_phase1(tgt);
1585
Bart Van Assche8b219002019-08-08 20:01:38 -07001586 if (!tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001587 qlt_stop_phase2(tgt);
1588
Quinn Trane326d222017-06-13 20:47:18 -07001589 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1590 unsigned long flags;
1591
1592 h = &tgt->qphints[i];
1593 if (h->qpair) {
1594 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1595 list_del(&h->hint_elem);
1596 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1597 h->qpair = NULL;
1598 }
1599 }
1600 kfree(tgt->qphints);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001601 mutex_lock(&qla_tgt_mutex);
1602 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1603 mutex_unlock(&qla_tgt_mutex);
Quinn Trane326d222017-06-13 20:47:18 -07001604
1605 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1606 btree_remove64(&tgt->lun_qpair_map, key);
1607
1608 btree_destroy64(&tgt->lun_qpair_map);
1609
Sawan Chandak3be63b1e2017-12-04 14:45:04 -08001610 if (vha->vp_idx)
1611 if (ha->tgt.tgt_ops &&
1612 ha->tgt.tgt_ops->remove_target &&
1613 vha->vha_tgt.target_lport_ptr)
1614 ha->tgt.tgt_ops->remove_target(vha);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001615
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001616 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001617
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001618 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001619 "Release of tgt %p finished\n", tgt);
1620
1621 kfree(tgt);
1622}
1623
1624/* ha->hardware_lock supposed to be held on entry */
1625static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1626 const void *param, unsigned int param_size)
1627{
1628 struct qla_tgt_sess_work_param *prm;
1629 unsigned long flags;
1630
1631 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1632 if (!prm) {
1633 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1634 "qla_target(%d): Unable to create session "
1635 "work, command will be refused", 0);
1636 return -ENOMEM;
1637 }
1638
1639 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1640 "Scheduling work (type %d, prm %p)"
1641 " to find session for param %p (size %d, tgt %p)\n",
1642 type, prm, param, param_size, tgt);
1643
1644 prm->type = type;
1645 memcpy(&prm->tm_iocb, param, param_size);
1646
1647 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1648 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1649 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1650
1651 schedule_work(&tgt->sess_work);
1652
1653 return 0;
1654}
1655
1656/*
1657 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1658 */
Quinn Tran82de8022017-06-13 20:47:17 -07001659static void qlt_send_notify_ack(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001660 struct imm_ntfy_from_isp *ntfy,
1661 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1662 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1663{
Quinn Tran82de8022017-06-13 20:47:17 -07001664 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001665 struct qla_hw_data *ha = vha->hw;
1666 request_t *pkt;
1667 struct nack_to_isp *nack;
1668
Quinn Tranec7193e2017-03-15 09:48:55 -07001669 if (!ha->flags.fw_started)
1670 return;
1671
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001672 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1673
Quinn Tran82de8022017-06-13 20:47:17 -07001674 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001675 if (!pkt) {
1676 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1677 "qla_target(%d): %s failed: unable to allocate "
1678 "request packet\n", vha->vp_idx, __func__);
1679 return;
1680 }
1681
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001682 if (vha->vha_tgt.qla_tgt != NULL)
1683 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001684
1685 pkt->entry_type = NOTIFY_ACK_TYPE;
1686 pkt->entry_count = 1;
1687
1688 nack = (struct nack_to_isp *)pkt;
1689 nack->ox_id = ntfy->ox_id;
1690
Quinn Tran726b8542017-01-19 22:28:00 -08001691 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001692 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1693 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1694 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001695 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001696 }
1697 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1698 nack->u.isp24.status = ntfy->u.isp24.status;
1699 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001700 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001701 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1702 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1703 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1704 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1705 nack->u.isp24.srr_reject_code = srr_reject_code;
1706 nack->u.isp24.srr_reject_code_expl = srr_explan;
1707 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1708
1709 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1710 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1711 vha->vp_idx, nack->u.isp24.status);
1712
Himanshu Madhani63163e02014-09-25 06:14:59 -04001713 /* Memory Barrier */
1714 wmb();
Quinn Tran82de8022017-06-13 20:47:17 -07001715 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001716}
1717
Quinn Tran6b0431d2018-09-04 14:19:13 -07001718static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1719{
1720 struct scsi_qla_host *vha = mcmd->vha;
1721 struct qla_hw_data *ha = vha->hw;
1722 struct abts_resp_to_24xx *resp;
1723 uint32_t f_ctl, h;
1724 uint8_t *p;
1725 int rc;
1726 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1727 struct qla_qpair *qpair = mcmd->qpair;
1728
1729 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1730 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1731 ha, mcmd->fc_tm_rsp);
1732
1733 rc = qlt_check_reserve_free_req(qpair, 1);
1734 if (rc) {
1735 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1736 "qla_target(%d): %s failed: unable to allocate request packet\n",
1737 vha->vp_idx, __func__);
1738 return -EAGAIN;
1739 }
1740
1741 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1742 memset(resp, 0, sizeof(*resp));
1743
1744 h = qlt_make_handle(qpair);
1745 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1746 /*
1747 * CTIO type 7 from the firmware doesn't provide a way to
1748 * know the initiator's LOOP ID, hence we can't find
1749 * the session and, so, the command.
1750 */
1751 return -EAGAIN;
1752 } else {
1753 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1754 }
1755
1756 resp->handle = MAKE_HANDLE(qpair->req->id, h);
1757 resp->entry_type = ABTS_RESP_24XX;
1758 resp->entry_count = 1;
1759 resp->nport_handle = abts->nport_handle;
1760 resp->vp_index = vha->vp_idx;
1761 resp->sof_type = abts->sof_type;
1762 resp->exchange_address = abts->exchange_address;
1763 resp->fcp_hdr_le = abts->fcp_hdr_le;
1764 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1765 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1766 F_CTL_SEQ_INITIATIVE);
1767 p = (uint8_t *)&f_ctl;
1768 resp->fcp_hdr_le.f_ctl[0] = *p++;
1769 resp->fcp_hdr_le.f_ctl[1] = *p++;
1770 resp->fcp_hdr_le.f_ctl[2] = *p;
1771
1772 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1773 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1774 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1775 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1776 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1777 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1778
1779 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1780 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1781 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1782 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1783 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1784 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1785 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1786 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1787 } else {
1788 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1789 resp->payload.ba_rjt.reason_code =
1790 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1791 /* Other bytes are zero */
1792 }
1793
1794 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1795
1796 /* Memory Barrier */
1797 wmb();
1798 if (qpair->reqq_start_iocbs)
1799 qpair->reqq_start_iocbs(qpair);
1800 else
1801 qla2x00_start_iocbs(vha, qpair->req);
1802
1803 return rc;
1804}
1805
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001806/*
1807 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1808 */
Quinn Tran82de8022017-06-13 20:47:17 -07001809static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001810 struct abts_recv_from_24xx *abts, uint32_t status,
1811 bool ids_reversed)
1812{
Quinn Tran82de8022017-06-13 20:47:17 -07001813 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001814 struct qla_hw_data *ha = vha->hw;
1815 struct abts_resp_to_24xx *resp;
1816 uint32_t f_ctl;
1817 uint8_t *p;
1818
1819 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1820 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1821 ha, abts, status);
1822
Quinn Tran82de8022017-06-13 20:47:17 -07001823 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1824 NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001825 if (!resp) {
1826 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1827 "qla_target(%d): %s failed: unable to allocate "
1828 "request packet", vha->vp_idx, __func__);
1829 return;
1830 }
1831
1832 resp->entry_type = ABTS_RESP_24XX;
Quinn Tran6b0431d2018-09-04 14:19:13 -07001833 resp->handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001834 resp->entry_count = 1;
1835 resp->nport_handle = abts->nport_handle;
1836 resp->vp_index = vha->vp_idx;
1837 resp->sof_type = abts->sof_type;
1838 resp->exchange_address = abts->exchange_address;
1839 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001840 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001841 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1842 F_CTL_SEQ_INITIATIVE);
1843 p = (uint8_t *)&f_ctl;
1844 resp->fcp_hdr_le.f_ctl[0] = *p++;
1845 resp->fcp_hdr_le.f_ctl[1] = *p++;
1846 resp->fcp_hdr_le.f_ctl[2] = *p;
1847 if (ids_reversed) {
1848 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1849 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1850 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1851 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1852 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1853 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1854 } else {
1855 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1856 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1857 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1858 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1859 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1860 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1861 }
1862 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1863 if (status == FCP_TMF_CMPL) {
1864 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1865 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1866 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1867 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1868 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1869 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1870 } else {
1871 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1872 resp->payload.ba_rjt.reason_code =
1873 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1874 /* Other bytes are zero */
1875 }
1876
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001877 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001878
Himanshu Madhani63163e02014-09-25 06:14:59 -04001879 /* Memory Barrier */
1880 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07001881 if (qpair->reqq_start_iocbs)
1882 qpair->reqq_start_iocbs(qpair);
1883 else
1884 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001885}
1886
1887/*
1888 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1889 */
1890static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
Quinn Tran6b0431d2018-09-04 14:19:13 -07001891 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001892{
1893 struct ctio7_to_24xx *ctio;
Quinn Tran06910942018-09-04 14:19:12 -07001894 u16 tmp;
Quinn Tran6b0431d2018-09-04 14:19:13 -07001895 struct abts_recv_from_24xx *entry;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001896
Quinn Tran06910942018-09-04 14:19:12 -07001897 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001898 if (ctio == NULL) {
1899 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1900 "qla_target(%d): %s failed: unable to allocate "
1901 "request packet\n", vha->vp_idx, __func__);
1902 return;
1903 }
1904
Quinn Tran6b0431d2018-09-04 14:19:13 -07001905 if (mcmd)
1906 /* abts from remote port */
1907 entry = &mcmd->orig_iocb.abts;
1908 else
1909 /* abts from this driver. */
1910 entry = (struct abts_recv_from_24xx *)pkt;
1911
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001912 /*
1913 * We've got on entrance firmware's response on by us generated
1914 * ABTS response. So, in it ID fields are reversed.
1915 */
1916
1917 ctio->entry_type = CTIO_TYPE7;
1918 ctio->entry_count = 1;
1919 ctio->nport_handle = entry->nport_handle;
1920 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001921 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001922 ctio->vp_index = vha->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001923 ctio->exchange_addr = entry->exchange_addr_to_abort;
Quinn Tran06910942018-09-04 14:19:12 -07001924 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
Quinn Tran06910942018-09-04 14:19:12 -07001925
Quinn Tran6b0431d2018-09-04 14:19:13 -07001926 if (mcmd) {
1927 ctio->initiator_id[0] = entry->fcp_hdr_le.s_id[0];
1928 ctio->initiator_id[1] = entry->fcp_hdr_le.s_id[1];
1929 ctio->initiator_id[2] = entry->fcp_hdr_le.s_id[2];
1930
1931 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1932 tmp |= (mcmd->abort_io_attr << 9);
1933 else if (qpair->retry_term_cnt & 1)
1934 tmp |= (0x4 << 9);
1935 } else {
1936 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1937 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1938 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1939
1940 if (qpair->retry_term_cnt & 1)
1941 tmp |= (0x4 << 9);
1942 }
1943 ctio->u.status1.flags = cpu_to_le16(tmp);
1944 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1945
1946 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1947 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1948 le16_to_cpu(ctio->u.status1.flags),
1949 le16_to_cpu(ctio->u.status1.ox_id),
1950 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001951
Himanshu Madhani63163e02014-09-25 06:14:59 -04001952 /* Memory Barrier */
1953 wmb();
Quinn Tran06910942018-09-04 14:19:12 -07001954 if (qpair->reqq_start_iocbs)
1955 qpair->reqq_start_iocbs(qpair);
1956 else
1957 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001958
Quinn Tran6b0431d2018-09-04 14:19:13 -07001959 if (mcmd)
1960 qlt_build_abts_resp_iocb(mcmd);
1961 else
1962 qlt_24xx_send_abts_resp(qpair,
1963 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001964
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001965}
1966
1967/* drop cmds for the given lun
1968 * XXX only looks for cmds on the port through which lun reset was recieved
1969 * XXX does not go through the list of other port (which may have cmds
1970 * for the same lun)
1971 */
1972static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001973 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001974{
1975 struct qla_tgt_sess_op *op;
1976 struct qla_tgt_cmd *cmd;
1977 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001978 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001979
1980 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001981 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001982 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1983 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001984 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001985
1986 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1987 op_lun = scsilun_to_int(
1988 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1989 if (op_key == key && op_lun == lun)
1990 op->aborted = true;
1991 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001992
1993 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1994 uint32_t op_key;
1995 u64 op_lun;
1996
1997 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1998 op_lun = scsilun_to_int(
1999 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2000 if (op_key == key && op_lun == lun)
2001 op->aborted = true;
2002 }
2003
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002004 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2005 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07002006 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002007
2008 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2009 cmd_lun = scsilun_to_int(
2010 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2011 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05002012 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002013 }
Quinn Tran8b631d82017-06-02 09:11:54 -07002014 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002015}
2016
Quinn Tran84905df2018-05-01 09:01:53 -07002017static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2018 uint64_t unpacked_lun)
2019{
2020 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2021 struct qla_qpair_hint *h = NULL;
2022
2023 if (vha->flags.qpairs_available) {
2024 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2025 if (!h)
2026 h = &tgt->qphints[0];
2027 } else {
2028 h = &tgt->qphints[0];
2029 }
2030
2031 return h;
2032}
2033
2034static void qlt_do_tmr_work(struct work_struct *work)
2035{
2036 struct qla_tgt_mgmt_cmd *mcmd =
2037 container_of(work, struct qla_tgt_mgmt_cmd, work);
2038 struct qla_hw_data *ha = mcmd->vha->hw;
2039 int rc = EIO;
2040 uint32_t tag;
2041 unsigned long flags;
2042
2043 switch (mcmd->tmr_func) {
2044 case QLA_TGT_ABTS:
2045 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
2046 break;
2047 default:
2048 tag = 0;
2049 break;
2050 }
2051
2052 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2053 mcmd->tmr_func, tag);
2054
2055 if (rc != 0) {
2056 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2057 switch (mcmd->tmr_func) {
2058 case QLA_TGT_ABTS:
Quinn Tran6b0431d2018-09-04 14:19:13 -07002059 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2060 qlt_build_abts_resp_iocb(mcmd);
Quinn Tran84905df2018-05-01 09:01:53 -07002061 break;
2062 case QLA_TGT_LUN_RESET:
2063 case QLA_TGT_CLEAR_TS:
2064 case QLA_TGT_ABORT_TS:
2065 case QLA_TGT_CLEAR_ACA:
2066 case QLA_TGT_TARGET_RESET:
2067 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2068 qla_sam_status);
2069 break;
2070
2071 case QLA_TGT_ABORT_ALL:
2072 case QLA_TGT_NEXUS_LOSS_SESS:
2073 case QLA_TGT_NEXUS_LOSS:
2074 qlt_send_notify_ack(mcmd->qpair,
2075 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2076 break;
2077 }
2078 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2079
2080 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2081 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2082 mcmd->vha->vp_idx, rc);
2083 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2084 }
2085}
2086
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002087/* ha->hardware_lock supposed to be held on entry */
2088static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08002089 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002090{
2091 struct qla_hw_data *ha = vha->hw;
2092 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tran84905df2018-05-01 09:01:53 -07002093 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
Steve Hodgson06e97b42012-11-16 08:06:17 -08002094
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002095 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2096 "qla_target(%d): task abort (tag=%d)\n",
2097 vha->vp_idx, abts->exchange_addr_to_abort);
2098
2099 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2100 if (mcmd == NULL) {
2101 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2102 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2103 vha->vp_idx, __func__);
2104 return -ENOMEM;
2105 }
2106 memset(mcmd, 0, sizeof(*mcmd));
Quinn Tran6b0431d2018-09-04 14:19:13 -07002107 mcmd->cmd_type = TYPE_TGT_TMCMD;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002108 mcmd->sess = sess;
2109 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002110 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08002111 mcmd->tmr_func = QLA_TGT_ABTS;
Quinn Tran84905df2018-05-01 09:01:53 -07002112 mcmd->qpair = h->qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002113 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002114
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07002115 /*
2116 * LUN is looked up by target-core internally based on the passed
2117 * abts->exchange_addr_to_abort tag.
2118 */
Quinn Tran84905df2018-05-01 09:01:53 -07002119 mcmd->se_cmd.cpuid = h->cpuid;
2120
2121 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2122 struct qla_tgt_cmd *abort_cmd;
2123
2124 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2125 abts->exchange_addr_to_abort);
2126 if (abort_cmd && abort_cmd->qpair) {
2127 mcmd->qpair = abort_cmd->qpair;
2128 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
Quinn Tran6b0431d2018-09-04 14:19:13 -07002129 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2130 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
Quinn Tran84905df2018-05-01 09:01:53 -07002131 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002132 }
2133
Quinn Tran84905df2018-05-01 09:01:53 -07002134 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2135 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2136
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002137 return 0;
2138}
2139
2140/*
2141 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2142 */
2143static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2144 struct abts_recv_from_24xx *abts)
2145{
2146 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08002147 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002148 uint32_t tag = abts->exchange_addr_to_abort;
2149 uint8_t s_id[3];
2150 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05002151 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002152
2153 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2154 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2155 "qla_target(%d): ABTS: Abort Sequence not "
2156 "supported\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002157 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2158 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002159 return;
2160 }
2161
2162 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2163 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2164 "qla_target(%d): ABTS: Unknown Exchange "
2165 "Address received\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002166 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2167 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002168 return;
2169 }
2170
2171 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2172 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2173 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2174 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2175 le32_to_cpu(abts->fcp_hdr_le.parameter));
2176
2177 s_id[0] = abts->fcp_hdr_le.s_id[2];
2178 s_id[1] = abts->fcp_hdr_le.s_id[1];
2179 s_id[2] = abts->fcp_hdr_le.s_id[0];
2180
Quinn Tran75601512015-12-17 14:57:04 -05002181 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002182 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2183 if (!sess) {
2184 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
Colin Ian King03fea732018-03-19 10:53:41 +00002185 "qla_target(%d): task abort for non-existent session\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002186 vha->vp_idx);
Quinn Tran75601512015-12-17 14:57:04 -05002187 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2188
Quinn Tranfb352652017-12-28 12:33:32 -08002189 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2190 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002191 return;
2192 }
Quinn Tran75601512015-12-17 14:57:04 -05002193 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2194
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002195
Quinn Tran726b8542017-01-19 22:28:00 -08002196 if (sess->deleted) {
Quinn Tran82de8022017-06-13 20:47:17 -07002197 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2198 false);
Alexei Potashnike52a8b42015-07-14 16:00:48 -04002199 return;
2200 }
2201
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002202 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2203 if (rc != 0) {
2204 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2205 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2206 vha->vp_idx, rc);
Quinn Tran82de8022017-06-13 20:47:17 -07002207 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2208 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002209 return;
2210 }
2211}
2212
2213/*
2214 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2215 */
Quinn Tran82de8022017-06-13 20:47:17 -07002216static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002217 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2218{
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002219 struct scsi_qla_host *ha = mcmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002220 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2221 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002222 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002223
2224 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2225 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2226 ha, atio, resp_code);
2227
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002228
Quinn Tran82de8022017-06-13 20:47:17 -07002229 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002230 if (ctio == NULL) {
2231 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2232 "qla_target(%d): %s failed: unable to allocate "
2233 "request packet\n", ha->vp_idx, __func__);
2234 return;
2235 }
2236
2237 ctio->entry_type = CTIO_TYPE7;
2238 ctio->entry_count = 1;
2239 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2240 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002241 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002242 ctio->vp_index = ha->vp_idx;
2243 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2244 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2245 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2246 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002247 temp = (atio->u.isp24.attr << 9)|
2248 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2249 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002250 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2251 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002252 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002253 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2254 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002255 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002256
Himanshu Madhani63163e02014-09-25 06:14:59 -04002257 /* Memory Barrier */
2258 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002259 if (qpair->reqq_start_iocbs)
2260 qpair->reqq_start_iocbs(qpair);
2261 else
2262 qla2x00_start_iocbs(ha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002263}
2264
2265void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2266{
2267 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2268}
2269EXPORT_SYMBOL(qlt_free_mcmd);
2270
Quinn Tranbe251522017-03-15 09:48:49 -07002271/*
2272 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2273 * reacquire
2274 */
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002275void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07002276 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2277{
2278 struct atio_from_isp *atio = &cmd->atio;
2279 struct ctio7_to_24xx *ctio;
2280 uint16_t temp;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002281 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranbe251522017-03-15 09:48:49 -07002282
2283 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2284 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2285 "sense_key=%02x, asc=%02x, ascq=%02x",
2286 vha, atio, scsi_status, sense_key, asc, ascq);
2287
2288 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2289 if (!ctio) {
2290 ql_dbg(ql_dbg_async, vha, 0x3067,
2291 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2292 vha->host_no, __func__);
2293 goto out;
2294 }
2295
2296 ctio->entry_type = CTIO_TYPE7;
2297 ctio->entry_count = 1;
2298 ctio->handle = QLA_TGT_SKIP_HANDLE;
2299 ctio->nport_handle = cmd->sess->loop_id;
2300 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2301 ctio->vp_index = vha->vp_idx;
2302 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2303 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2304 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2305 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002306 temp = (atio->u.isp24.attr << 9) |
2307 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2308 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002309 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2310 ctio->u.status1.ox_id = cpu_to_le16(temp);
2311 ctio->u.status1.scsi_status =
2312 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2313 ctio->u.status1.response_len = cpu_to_le16(18);
2314 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2315
2316 if (ctio->u.status1.residual != 0)
2317 ctio->u.status1.scsi_status |=
2318 cpu_to_le16(SS_RESIDUAL_UNDER);
2319
Bart Van Asschea861b492019-04-17 14:44:42 -07002320 /* Fixed format sense data. */
2321 ctio->u.status1.sense_data[0] = 0x70;
2322 ctio->u.status1.sense_data[2] = sense_key;
Quinn Tranbe251522017-03-15 09:48:49 -07002323 /* Additional sense length */
Bart Van Asschea861b492019-04-17 14:44:42 -07002324 ctio->u.status1.sense_data[7] = 0xa;
Quinn Tranbe251522017-03-15 09:48:49 -07002325 /* ASC and ASCQ */
Bart Van Asschea861b492019-04-17 14:44:42 -07002326 ctio->u.status1.sense_data[12] = asc;
2327 ctio->u.status1.sense_data[13] = ascq;
Quinn Tranbe251522017-03-15 09:48:49 -07002328
2329 /* Memory Barrier */
2330 wmb();
2331
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002332 if (qpair->reqq_start_iocbs)
2333 qpair->reqq_start_iocbs(qpair);
2334 else
2335 qla2x00_start_iocbs(vha, qpair->req);
2336
Quinn Tranbe251522017-03-15 09:48:49 -07002337out:
2338 return;
2339}
2340
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002341/* callback from target fabric module code */
2342void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2343{
2344 struct scsi_qla_host *vha = mcmd->sess->vha;
2345 struct qla_hw_data *ha = vha->hw;
2346 unsigned long flags;
Quinn Tran82de8022017-06-13 20:47:17 -07002347 struct qla_qpair *qpair = mcmd->qpair;
Quinn Tran6b0431d2018-09-04 14:19:13 -07002348 bool free_mcmd = true;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002349
2350 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2351 "TM response mcmd (%p) status %#x state %#x",
2352 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2353
Quinn Tran82de8022017-06-13 20:47:17 -07002354 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002355
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002356 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002357 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002358 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002359 * previous life, just abort the processing.
2360 */
2361 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002362 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2363 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002364 mcmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002365 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002366 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002367 return;
2368 }
2369
Quinn Tran726b8542017-01-19 22:28:00 -08002370 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
Bart Van Assche8837aa82018-11-27 15:04:54 -08002371 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2372 case ELS_LOGO:
2373 case ELS_PRLO:
2374 case ELS_TPRLO:
Quinn Tran83548fe2017-06-02 09:12:01 -07002375 ql_dbg(ql_dbg_disc, vha, 0x2106,
Bart Van Assche19ce1922019-04-17 14:44:11 -07002376 "TM response logo %8phC status %#x state %#x",
Quinn Tran726b8542017-01-19 22:28:00 -08002377 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2378 mcmd->flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08002379 qlt_schedule_sess_for_deletion(mcmd->sess);
Bart Van Assche8837aa82018-11-27 15:04:54 -08002380 break;
2381 default:
Quinn Tran82de8022017-06-13 20:47:17 -07002382 qlt_send_notify_ack(vha->hw->base_qpair,
2383 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
Bart Van Assche8837aa82018-11-27 15:04:54 -08002384 break;
Quinn Tran726b8542017-01-19 22:28:00 -08002385 }
2386 } else {
Quinn Tran6b0431d2018-09-04 14:19:13 -07002387 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2388 qlt_build_abts_resp_iocb(mcmd);
2389 free_mcmd = false;
2390 } else
Quinn Tran82de8022017-06-13 20:47:17 -07002391 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002392 mcmd->fc_tm_rsp);
2393 }
2394 /*
2395 * Make the callback for ->free_mcmd() to queue_work() and invoke
2396 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2397 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2398 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2399 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2400 * qlt_xmit_tm_rsp() returns here..
2401 */
Quinn Tran6b0431d2018-09-04 14:19:13 -07002402 if (free_mcmd)
2403 ha->tgt.tgt_ops->free_mcmd(mcmd);
2404
Quinn Tran82de8022017-06-13 20:47:17 -07002405 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002406}
2407EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2408
2409/* No locks */
2410static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2411{
2412 struct qla_tgt_cmd *cmd = prm->cmd;
2413
2414 BUG_ON(cmd->sg_cnt == 0);
2415
2416 prm->sg = (struct scatterlist *)cmd->sg;
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002417 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002418 cmd->sg_cnt, cmd->dma_data_direction);
2419 if (unlikely(prm->seg_cnt == 0))
2420 goto out_err;
2421
2422 prm->cmd->sg_mapped = 1;
2423
Quinn Tranf83adb62014-04-11 16:54:43 -04002424 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2425 /*
2426 * If greater than four sg entries then we need to allocate
2427 * the continuation entries
2428 */
Quinn Tranb5399f72017-06-13 20:47:27 -07002429 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
Quinn Tranf83adb62014-04-11 16:54:43 -04002430 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
Quinn Tranb5399f72017-06-13 20:47:27 -07002431 QLA_TGT_DATASEGS_PER_CMD_24XX,
2432 QLA_TGT_DATASEGS_PER_CONT_24XX);
Quinn Tranf83adb62014-04-11 16:54:43 -04002433 } else {
2434 /* DIF */
2435 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2436 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2437 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2438 prm->tot_dsds = prm->seg_cnt;
2439 } else
2440 prm->tot_dsds = prm->seg_cnt;
2441
2442 if (cmd->prot_sg_cnt) {
2443 prm->prot_sg = cmd->prot_sg;
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002444 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
Quinn Tranf83adb62014-04-11 16:54:43 -04002445 cmd->prot_sg, cmd->prot_sg_cnt,
2446 cmd->dma_data_direction);
2447 if (unlikely(prm->prot_seg_cnt == 0))
2448 goto out_err;
2449
2450 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2451 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2452 /* Dif Bundling not support here */
2453 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2454 cmd->blk_sz);
2455 prm->tot_dsds += prm->prot_seg_cnt;
2456 } else
2457 prm->tot_dsds += prm->prot_seg_cnt;
2458 }
2459 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002460
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002461 return 0;
2462
2463out_err:
Quinn Tran22d84722017-06-13 20:47:25 -07002464 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002465 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2466 0, prm->cmd->sg_cnt);
2467 return -1;
2468}
2469
Joern Engelf9b67212014-09-16 16:23:18 -04002470static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002471{
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002472 struct qla_hw_data *ha;
2473 struct qla_qpair *qpair;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07002474
Joern Engelf9b67212014-09-16 16:23:18 -04002475 if (!cmd->sg_mapped)
2476 return;
2477
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002478 qpair = cmd->qpair;
2479
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002480 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002481 cmd->dma_data_direction);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002482 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002483
2484 if (cmd->prot_sg_cnt)
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002485 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
Quinn Tranf83adb62014-04-11 16:54:43 -04002486 cmd->dma_data_direction);
2487
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002488 if (!cmd->ctx)
2489 return;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002490 ha = vha->hw;
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002491 if (cmd->ctx_dsd_alloced)
2492 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2493
2494 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002495}
2496
Quinn Tran82de8022017-06-13 20:47:17 -07002497static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002498 uint32_t req_cnt)
2499{
Quinn Trand63b3282017-06-02 09:12:07 -07002500 uint32_t cnt;
Quinn Tran82de8022017-06-13 20:47:17 -07002501 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002502
Quinn Tran82de8022017-06-13 20:47:17 -07002503 if (req->cnt < (req_cnt + 2)) {
Quinn Tranaf7bb382017-06-13 20:47:23 -07002504 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2505 RD_REG_DWORD_RELAXED(req->req_q_out));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002506
Quinn Tran82de8022017-06-13 20:47:17 -07002507 if (req->ring_index < cnt)
2508 req->cnt = cnt - req->ring_index;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002509 else
Quinn Tran82de8022017-06-13 20:47:17 -07002510 req->cnt = req->length - (req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002511
Quinn Tran82de8022017-06-13 20:47:17 -07002512 if (unlikely(req->cnt < (req_cnt + 2)))
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002513 return -EAGAIN;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002514 }
2515
Quinn Tran82de8022017-06-13 20:47:17 -07002516 req->cnt -= req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002517
2518 return 0;
2519}
2520
2521/*
2522 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2523 */
Quinn Tran82de8022017-06-13 20:47:17 -07002524static inline void *qlt_get_req_pkt(struct req_que *req)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002525{
2526 /* Adjust ring index. */
Quinn Tran82de8022017-06-13 20:47:17 -07002527 req->ring_index++;
2528 if (req->ring_index == req->length) {
2529 req->ring_index = 0;
2530 req->ring_ptr = req->ring;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002531 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002532 req->ring_ptr++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002533 }
Quinn Tran82de8022017-06-13 20:47:17 -07002534 return (cont_entry_t *)req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002535}
2536
2537/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002538static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002539{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002540 uint32_t h;
Quinn Tranc5419e22017-06-13 20:47:16 -07002541 int index;
2542 uint8_t found = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07002543 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002544
Quinn Tranc5419e22017-06-13 20:47:16 -07002545 h = req->current_outstanding_cmd;
2546
2547 for (index = 1; index < req->num_outstanding_cmds; index++) {
2548 h++;
2549 if (h == req->num_outstanding_cmds)
2550 h = 1;
2551
2552 if (h == QLA_TGT_SKIP_HANDLE)
2553 continue;
2554
2555 if (!req->outstanding_cmds[h]) {
2556 found = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002557 break;
2558 }
Quinn Tranc5419e22017-06-13 20:47:16 -07002559 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002560
Quinn Tranc5419e22017-06-13 20:47:16 -07002561 if (found) {
2562 req->current_outstanding_cmd = h;
2563 } else {
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002564 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2565 "qla_target(%d): Ran out of empty cmd slots\n",
2566 qpair->vha->vp_idx);
Quinn Tranc5419e22017-06-13 20:47:16 -07002567 h = QLA_TGT_NULL_HANDLE;
2568 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002569
2570 return h;
2571}
2572
2573/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002574static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2575 struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002576{
2577 uint32_t h;
2578 struct ctio7_to_24xx *pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002579 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002580 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002581
Quinn Tran82de8022017-06-13 20:47:17 -07002582 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002583 prm->pkt = pkt;
2584 memset(pkt, 0, sizeof(*pkt));
2585
2586 pkt->entry_type = CTIO_TYPE7;
2587 pkt->entry_count = (uint8_t)prm->req_cnt;
Quinn Tran22d84722017-06-13 20:47:25 -07002588 pkt->vp_index = prm->cmd->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002589
Quinn Tran82de8022017-06-13 20:47:17 -07002590 h = qlt_make_handle(qpair);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002591 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2592 /*
2593 * CTIO type 7 from the firmware doesn't provide a way to
2594 * know the initiator's LOOP ID, hence we can't find
2595 * the session and, so, the command.
2596 */
2597 return -EAGAIN;
Quinn Trane326d222017-06-13 20:47:18 -07002598 } else
2599 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002600
Quinn Tran82de8022017-06-13 20:47:17 -07002601 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2602 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2603 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002604 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002605 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2606 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2607 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2608 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002609 temp = atio->u.isp24.attr << 9;
2610 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002611 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2612 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002613 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2614
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002615 return 0;
2616}
2617
2618/*
2619 * ha->hardware_lock supposed to be held on entry. We have already made sure
2620 * that there is sufficient amount of request entries to not drop it.
2621 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002622static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002623{
2624 int cnt;
Bart Van Assche15b7a682019-04-17 14:44:38 -07002625 struct dsd64 *cur_dsd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002626
2627 /* Build continuation packets */
2628 while (prm->seg_cnt > 0) {
2629 cont_a64_entry_t *cont_pkt64 =
Quinn Tran82de8022017-06-13 20:47:17 -07002630 (cont_a64_entry_t *)qlt_get_req_pkt(
2631 prm->cmd->qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002632
2633 /*
2634 * Make sure that from cont_pkt64 none of
2635 * 64-bit specific fields used for 32-bit
2636 * addressing. Cast to (cont_entry_t *) for
2637 * that.
2638 */
2639
2640 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2641
2642 cont_pkt64->entry_count = 1;
2643 cont_pkt64->sys_define = 0;
2644
Quinn Tran32d29b42017-06-13 20:47:26 -07002645 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
Bart Van Assche15b7a682019-04-17 14:44:38 -07002646 cur_dsd = cont_pkt64->dsd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002647
2648 /* Load continuation entry data segments */
2649 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002650 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002651 cnt++, prm->seg_cnt--) {
Bart Van Assche15b7a682019-04-17 14:44:38 -07002652 append_dsd64(&cur_dsd, prm->sg);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002653 prm->sg = sg_next(prm->sg);
2654 }
2655 }
2656}
2657
2658/*
2659 * ha->hardware_lock supposed to be held on entry. We have already made sure
2660 * that there is sufficient amount of request entries to not drop it.
2661 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002662static void qlt_load_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002663{
2664 int cnt;
Bart Van Assche15b7a682019-04-17 14:44:38 -07002665 struct dsd64 *cur_dsd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002666 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2667
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002668 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2669
2670 /* Setup packet address segment pointer */
Bart Van Assche15b7a682019-04-17 14:44:38 -07002671 cur_dsd = &pkt24->u.status0.dsd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002672
2673 /* Set total data segment count */
2674 if (prm->seg_cnt)
2675 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2676
2677 if (prm->seg_cnt == 0) {
2678 /* No data transfer */
Bart Van Assche15b7a682019-04-17 14:44:38 -07002679 cur_dsd->address = 0;
2680 cur_dsd->length = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002681 return;
2682 }
2683
2684 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002685
2686 /* Load command entry data segments */
2687 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002688 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002689 cnt++, prm->seg_cnt--) {
Bart Van Assche15b7a682019-04-17 14:44:38 -07002690 append_dsd64(&cur_dsd, prm->sg);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002691 prm->sg = sg_next(prm->sg);
2692 }
2693
Quinn Tran32d29b42017-06-13 20:47:26 -07002694 qlt_load_cont_data_segments(prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002695}
2696
2697static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2698{
2699 return cmd->bufflen > 0;
2700}
2701
Quinn Tranbe251522017-03-15 09:48:49 -07002702static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2703{
2704 struct qla_tgt_cmd *cmd;
2705 struct scsi_qla_host *vha;
2706
2707 /* asc 0x10=dif error */
2708 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2709 cmd = prm->cmd;
2710 vha = cmd->vha;
2711 /* ASCQ */
2712 switch (prm->sense_buffer[13]) {
2713 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002714 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002715 "BE detected Guard 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 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002721 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002722 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%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 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002728 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002729 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2730 "se_cmd=%p tag[%x]",
2731 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2732 cmd->atio.u.isp24.exchange_addr);
2733 break;
2734 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002735 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002736 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2737 "se_cmd=%p tag[%x]",
2738 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2739 cmd->atio.u.isp24.exchange_addr);
2740 break;
2741 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002742 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002743 }
2744}
2745
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002746/*
2747 * Called without ha->hardware_lock held
2748 */
2749static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2750 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2751 uint32_t *full_req_cnt)
2752{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002753 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Tran22d84722017-06-13 20:47:25 -07002754 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002755
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002756 prm->cmd = cmd;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002757 prm->tgt = cmd->tgt;
2758 prm->pkt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002759 prm->rq_result = scsi_status;
2760 prm->sense_buffer = &cmd->sense_buffer[0];
2761 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2762 prm->sg = NULL;
2763 prm->seg_cnt = -1;
2764 prm->req_cnt = 1;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002765 prm->residual = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002766 prm->add_status_pkt = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002767 prm->prot_sg = NULL;
2768 prm->prot_seg_cnt = 0;
2769 prm->tot_dsds = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002770
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002771 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2772 if (qlt_pci_map_calc_cnt(prm) != 0)
2773 return -EAGAIN;
2774 }
2775
2776 *full_req_cnt = prm->req_cnt;
2777
2778 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2779 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002780 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002781 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2782 prm->residual, se_cmd->tag,
2783 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2784 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002785 prm->rq_result |= SS_RESIDUAL_UNDER;
2786 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2787 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002788 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002789 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2790 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2791 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002792 prm->rq_result |= SS_RESIDUAL_OVER;
2793 }
2794
2795 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2796 /*
2797 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2798 * ignored in *xmit_response() below
2799 */
2800 if (qlt_has_data(cmd)) {
2801 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002802 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002803 (prm->rq_result != 0))) {
2804 prm->add_status_pkt = 1;
2805 (*full_req_cnt)++;
2806 }
2807 }
2808 }
2809
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002810 return 0;
2811}
2812
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002813static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2814 int sending_sense)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002815{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002816 if (cmd->qpair->enable_class_2)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002817 return 0;
2818
2819 if (sending_sense)
2820 return cmd->conf_compl_supported;
2821 else
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002822 return cmd->qpair->enable_explicit_conf &&
2823 cmd->conf_compl_supported;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002824}
2825
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002826static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2827 struct qla_tgt_prm *prm)
2828{
2829 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2830 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002831 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002832 if (qlt_need_explicit_conf(prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002833 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002834 CTIO7_FLAGS_EXPLICIT_CONFORM |
2835 CTIO7_FLAGS_CONFORM_REQ);
2836 }
2837 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2838 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2839 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2840 int i;
2841
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002842 if (qlt_need_explicit_conf(prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002843 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Quinn Tran22d84722017-06-13 20:47:25 -07002844 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002845 "Skipping EXPLICIT_CONFORM and "
2846 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2847 "non GOOD status\n");
2848 goto skip_explict_conf;
2849 }
Bart Van Asschead950362015-07-09 07:24:08 -07002850 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002851 CTIO7_FLAGS_EXPLICIT_CONFORM |
2852 CTIO7_FLAGS_CONFORM_REQ);
2853 }
2854skip_explict_conf:
2855 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002856 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002857 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002858 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002859 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002860 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002861 ctio->u.status1.sense_length =
2862 cpu_to_le16(prm->sense_buffer_len);
2863 for (i = 0; i < prm->sense_buffer_len/4; i++)
2864 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2865 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002866
2867 qlt_print_dif_err(prm);
2868
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002869 } else {
2870 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002871 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002872 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002873 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002874 ctio->u.status1.sense_length = 0;
2875 memset(ctio->u.status1.sense_data, 0,
2876 sizeof(ctio->u.status1.sense_data));
2877 }
2878
2879 /* Sense with len > 24, is it possible ??? */
2880}
2881
Quinn Tranf83adb62014-04-11 16:54:43 -04002882static inline int
2883qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2884{
Quinn Tranf83adb62014-04-11 16:54:43 -04002885 switch (se_cmd->prot_op) {
2886 case TARGET_PROT_DOUT_INSERT:
2887 case TARGET_PROT_DIN_STRIP:
2888 if (ql2xenablehba_err_chk >= 1)
2889 return 1;
2890 break;
2891 case TARGET_PROT_DOUT_PASS:
2892 case TARGET_PROT_DIN_PASS:
2893 if (ql2xenablehba_err_chk >= 2)
2894 return 1;
2895 break;
2896 case TARGET_PROT_DIN_INSERT:
2897 case TARGET_PROT_DOUT_STRIP:
2898 return 1;
2899 default:
2900 break;
2901 }
2902 return 0;
2903}
2904
Quinn Tranbe251522017-03-15 09:48:49 -07002905static inline int
2906qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002907{
Quinn Tranbe251522017-03-15 09:48:49 -07002908 switch (se_cmd->prot_op) {
2909 case TARGET_PROT_DIN_INSERT:
2910 case TARGET_PROT_DOUT_INSERT:
2911 case TARGET_PROT_DIN_STRIP:
2912 case TARGET_PROT_DOUT_STRIP:
2913 case TARGET_PROT_DIN_PASS:
2914 case TARGET_PROT_DOUT_PASS:
2915 return 1;
2916 default:
2917 return 0;
2918 }
2919 return 0;
2920}
Quinn Tranf83adb62014-04-11 16:54:43 -04002921
Quinn Tranbe251522017-03-15 09:48:49 -07002922/*
2923 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2924 */
2925static void
2926qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2927 uint16_t *pfw_prot_opts)
2928{
2929 struct se_cmd *se_cmd = &cmd->se_cmd;
2930 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2931 scsi_qla_host_t *vha = cmd->tgt->vha;
2932 struct qla_hw_data *ha = vha->hw;
2933 uint32_t t32 = 0;
2934
2935 /*
2936 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002937 * have been immplemented by TCM, before AppTag is avail.
2938 * Look for modesense_handlers[]
2939 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002940 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002941 ctx->app_tag_mask[0] = 0x0;
2942 ctx->app_tag_mask[1] = 0x0;
2943
Quinn Tranbe251522017-03-15 09:48:49 -07002944 if (IS_PI_UNINIT_CAPABLE(ha)) {
2945 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2946 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2947 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2948 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2949 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2950 }
2951
2952 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2953
Quinn Tranf83adb62014-04-11 16:54:43 -04002954 switch (se_cmd->prot_type) {
2955 case TARGET_DIF_TYPE0_PROT:
2956 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002957 * No check for ql2xenablehba_err_chk, as it
2958 * would be an I/O error if hba tag generation
2959 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002960 */
2961 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002962 /* enable ALL bytes of the ref tag */
2963 ctx->ref_tag_mask[0] = 0xff;
2964 ctx->ref_tag_mask[1] = 0xff;
2965 ctx->ref_tag_mask[2] = 0xff;
2966 ctx->ref_tag_mask[3] = 0xff;
2967 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002968 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002969 /*
2970 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2971 * REF tag, and 16 bit app tag.
2972 */
2973 ctx->ref_tag = cpu_to_le32(lba);
2974 if (!qla_tgt_ref_mask_check(se_cmd) ||
2975 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2976 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2977 break;
2978 }
2979 /* enable ALL bytes of the ref tag */
2980 ctx->ref_tag_mask[0] = 0xff;
2981 ctx->ref_tag_mask[1] = 0xff;
2982 ctx->ref_tag_mask[2] = 0xff;
2983 ctx->ref_tag_mask[3] = 0xff;
2984 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002985 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002986 /*
2987 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2988 * tag has to match LBA in CDB + N
2989 */
2990 ctx->ref_tag = cpu_to_le32(lba);
2991 if (!qla_tgt_ref_mask_check(se_cmd) ||
2992 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2993 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2994 break;
2995 }
2996 /* enable ALL bytes of the ref tag */
2997 ctx->ref_tag_mask[0] = 0xff;
2998 ctx->ref_tag_mask[1] = 0xff;
2999 ctx->ref_tag_mask[2] = 0xff;
3000 ctx->ref_tag_mask[3] = 0xff;
3001 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003002 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07003003 /* For TYPE 3 protection: 16 bit GUARD only */
3004 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3005 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3006 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3007 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003008 }
3009}
3010
Quinn Tranf83adb62014-04-11 16:54:43 -04003011static inline int
Quinn Tran82de8022017-06-13 20:47:17 -07003012qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
Quinn Tranf83adb62014-04-11 16:54:43 -04003013{
Bart Van Assche15b7a682019-04-17 14:44:38 -07003014 struct dsd64 *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003015 uint32_t transfer_length = 0;
3016 uint32_t data_bytes;
3017 uint32_t dif_bytes;
3018 uint8_t bundling = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003019 struct crc_context *crc_ctx_pkt = NULL;
3020 struct qla_hw_data *ha;
3021 struct ctio_crc2_to_fw *pkt;
3022 dma_addr_t crc_ctx_dma;
3023 uint16_t fw_prot_opts = 0;
3024 struct qla_tgt_cmd *cmd = prm->cmd;
3025 struct se_cmd *se_cmd = &cmd->se_cmd;
3026 uint32_t h;
3027 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07003028 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003029 uint16_t t16;
Quinn Tran82de8022017-06-13 20:47:17 -07003030 scsi_qla_host_t *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003031
Quinn Tranf83adb62014-04-11 16:54:43 -04003032 ha = vha->hw;
3033
Quinn Tran82de8022017-06-13 20:47:17 -07003034 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
Quinn Tranf83adb62014-04-11 16:54:43 -04003035 prm->pkt = pkt;
3036 memset(pkt, 0, sizeof(*pkt));
3037
Quinn Tran22d84722017-06-13 20:47:25 -07003038 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
Quinn Tranf83adb62014-04-11 16:54:43 -04003039 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
Quinn Tran22d84722017-06-13 20:47:25 -07003040 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
Quinn Tranf83adb62014-04-11 16:54:43 -04003041 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3042
3043 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3044 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3045 bundling = 0;
3046
3047 /* Compute dif len and adjust data len to incude protection */
3048 data_bytes = cmd->bufflen;
3049 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3050
3051 switch (se_cmd->prot_op) {
3052 case TARGET_PROT_DIN_INSERT:
3053 case TARGET_PROT_DOUT_STRIP:
3054 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07003055 if (cmd->prot_sg_cnt)
3056 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04003057 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003058 case TARGET_PROT_DIN_STRIP:
3059 case TARGET_PROT_DOUT_INSERT:
3060 case TARGET_PROT_DIN_PASS:
3061 case TARGET_PROT_DOUT_PASS:
3062 transfer_length = data_bytes + dif_bytes;
3063 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003064 default:
3065 BUG();
3066 break;
3067 }
3068
3069 if (!qlt_hba_err_chk_enabled(se_cmd))
3070 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3071 /* HBA error checking enabled */
3072 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3073 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3074 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3075 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3076 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3077 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3078 }
3079
3080 switch (se_cmd->prot_op) {
3081 case TARGET_PROT_DIN_INSERT:
3082 case TARGET_PROT_DOUT_INSERT:
3083 fw_prot_opts |= PO_MODE_DIF_INSERT;
3084 break;
3085 case TARGET_PROT_DIN_STRIP:
3086 case TARGET_PROT_DOUT_STRIP:
3087 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3088 break;
3089 case TARGET_PROT_DIN_PASS:
3090 case TARGET_PROT_DOUT_PASS:
3091 fw_prot_opts |= PO_MODE_DIF_PASS;
3092 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3093 break;
3094 default:/* Normal Request */
3095 fw_prot_opts |= PO_MODE_DIF_PASS;
3096 break;
3097 }
3098
Quinn Tranf83adb62014-04-11 16:54:43 -04003099 /* ---- PKT ---- */
3100 /* Update entry type to indicate Command Type CRC_2 IOCB */
3101 pkt->entry_type = CTIO_CRC2;
3102 pkt->entry_count = 1;
Quinn Tran22d84722017-06-13 20:47:25 -07003103 pkt->vp_index = cmd->vp_idx;
Quinn Tranf83adb62014-04-11 16:54:43 -04003104
Quinn Tran82de8022017-06-13 20:47:17 -07003105 h = qlt_make_handle(qpair);
Quinn Tranf83adb62014-04-11 16:54:43 -04003106 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3107 /*
3108 * CTIO type 7 from the firmware doesn't provide a way to
3109 * know the initiator's LOOP ID, hence we can't find
3110 * the session and, so, the command.
3111 */
3112 return -EAGAIN;
3113 } else
Quinn Tran82de8022017-06-13 20:47:17 -07003114 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003115
Quinn Tran82de8022017-06-13 20:47:17 -07003116 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3117 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07003118 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07003119 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04003120 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3121 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3122 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3123 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003124
3125 /* silence compile warning */
3126 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3127 pkt->ox_id = cpu_to_le16(t16);
3128
3129 t16 = (atio->u.isp24.attr << 9);
3130 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04003131 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3132
3133 /* Set transfer direction */
3134 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07003135 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04003136 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07003137 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04003138
Quinn Tranf83adb62014-04-11 16:54:43 -04003139 pkt->dseg_count = prm->tot_dsds;
3140 /* Fibre channel byte count */
3141 pkt->transfer_length = cpu_to_le32(transfer_length);
3142
Quinn Tranf83adb62014-04-11 16:54:43 -04003143 /* ----- CRC context -------- */
3144
3145 /* Allocate CRC context from global pool */
3146 crc_ctx_pkt = cmd->ctx =
Souptick Joarder501017f2018-02-15 01:40:38 +05303147 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
Quinn Tranf83adb62014-04-11 16:54:43 -04003148
3149 if (!crc_ctx_pkt)
3150 goto crc_queuing_error;
3151
Quinn Tranf83adb62014-04-11 16:54:43 -04003152 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3153 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3154
3155 /* Set handle */
3156 crc_ctx_pkt->handle = pkt->handle;
3157
Quinn Tranbe251522017-03-15 09:48:49 -07003158 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04003159
Bart Van Assched4556a42019-04-17 14:44:39 -07003160 put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
Quinn Tranf83adb62014-04-11 16:54:43 -04003161 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3162
Quinn Tranf83adb62014-04-11 16:54:43 -04003163 if (!bundling) {
Bart Van Assche9e75b5e2019-08-08 20:01:33 -07003164 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003165 } else {
3166 /*
3167 * Configure Bundling if we need to fetch interlaving
3168 * protection PCI accesses
3169 */
3170 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3171 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3172 crc_ctx_pkt->u.bundling.dseg_count =
3173 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
Bart Van Assche9e75b5e2019-08-08 20:01:33 -07003174 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003175 }
3176
3177 /* Finish the common fields of CRC pkt */
3178 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3179 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3180 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07003181 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003182
Quinn Tranbe251522017-03-15 09:48:49 -07003183 memset((uint8_t *)&tc, 0 , sizeof(tc));
3184 tc.vha = vha;
3185 tc.blk_sz = cmd->blk_sz;
3186 tc.bufflen = cmd->bufflen;
3187 tc.sg = cmd->sg;
3188 tc.prot_sg = cmd->prot_sg;
3189 tc.ctx = crc_ctx_pkt;
3190 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04003191
3192 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07003193 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04003194
3195 if (!bundling && prm->prot_seg_cnt) {
3196 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003197 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003198 goto crc_queuing_error;
3199 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003200 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003201 goto crc_queuing_error;
3202
3203 if (bundling && prm->prot_seg_cnt) {
3204 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003205 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04003206
Bart Van Assche15b7a682019-04-17 14:44:38 -07003207 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003208 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Giridhar Malavali50b81272018-12-21 09:33:45 -08003209 prm->prot_seg_cnt, cmd))
Quinn Tranf83adb62014-04-11 16:54:43 -04003210 goto crc_queuing_error;
3211 }
3212 return QLA_SUCCESS;
3213
3214crc_queuing_error:
3215 /* Cleanup will be performed by the caller */
Quinn Tran82de8022017-06-13 20:47:17 -07003216 qpair->req->outstanding_cmds[h] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003217
3218 return QLA_FUNCTION_FAILED;
3219}
3220
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003221/*
3222 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3223 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3224 */
3225int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3226 uint8_t scsi_status)
3227{
3228 struct scsi_qla_host *vha = cmd->vha;
Quinn Tran82de8022017-06-13 20:47:17 -07003229 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003230 struct ctio7_to_24xx *pkt;
3231 struct qla_tgt_prm prm;
3232 uint32_t full_req_cnt = 0;
3233 unsigned long flags = 0;
3234 int res;
3235
Quinn Tran51fd6e62019-01-24 23:23:43 -08003236 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3237 (cmd->sess && cmd->sess->deleted)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003238 cmd->state = QLA_TGT_STATE_PROCESSED;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003239 return 0;
3240 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003241
Quinn Tran22d84722017-06-13 20:47:25 -07003242 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
Quinn Tran82de8022017-06-13 20:47:17 -07003243 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003244 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3245 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
Quinn Tran82de8022017-06-13 20:47:17 -07003246 &cmd->se_cmd, qpair->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003247
3248 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3249 &full_req_cnt);
3250 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003251 return res;
3252 }
3253
Quinn Tran82de8022017-06-13 20:47:17 -07003254 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003255
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003256 if (xmit_type == QLA_TGT_XMIT_STATUS)
Quinn Tran60a9ead2017-06-13 20:47:28 -07003257 qpair->tgt_counters.core_qla_snd_status++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003258 else
Quinn Tran60a9ead2017-06-13 20:47:28 -07003259 qpair->tgt_counters.core_qla_que_buf++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003260
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003261 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003262 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003263 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003264 * previous life, just abort the processing.
3265 */
3266 cmd->state = QLA_TGT_STATE_PROCESSED;
Quinn Tran22d84722017-06-13 20:47:25 -07003267 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003268 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3269 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003270 cmd->reset_count, qpair->chip_reset);
Quinn Tran82de8022017-06-13 20:47:17 -07003271 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04003272 return 0;
3273 }
3274
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003275 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003276 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003277 if (unlikely(res))
3278 goto out_unmap_unlock;
3279
Quinn Tranf83adb62014-04-11 16:54:43 -04003280 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
Quinn Tran82de8022017-06-13 20:47:17 -07003281 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003282 else
Quinn Tran82de8022017-06-13 20:47:17 -07003283 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Quinn Tran810e30b2015-06-10 11:05:20 -04003284 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003285 qpair->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003286 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003287 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003288
3289 pkt = (struct ctio7_to_24xx *)prm.pkt;
3290
3291 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3292 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003293 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003294 CTIO7_FLAGS_STATUS_MODE_0);
3295
Quinn Tranf83adb62014-04-11 16:54:43 -04003296 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003297 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003298
3299 if (prm.add_status_pkt == 0) {
3300 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3301 pkt->u.status0.scsi_status =
3302 cpu_to_le16(prm.rq_result);
3303 pkt->u.status0.residual =
3304 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003305 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003306 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003307 if (qlt_need_explicit_conf(cmd, 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003308 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003309 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003310 CTIO7_FLAGS_EXPLICIT_CONFORM |
3311 CTIO7_FLAGS_CONFORM_REQ);
3312 }
3313 }
3314
3315 } else {
3316 /*
3317 * We have already made sure that there is sufficient
3318 * amount of request entries to not drop HW lock in
3319 * req_pkt().
3320 */
3321 struct ctio7_to_24xx *ctio =
Quinn Tran82de8022017-06-13 20:47:17 -07003322 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3323 qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003324
Quinn Tran22d84722017-06-13 20:47:25 -07003325 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
Arun Easi667024a2014-09-25 06:14:47 -04003326 "Building additional status packet 0x%p.\n",
3327 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003328
Quinn Tranf83adb62014-04-11 16:54:43 -04003329 /*
3330 * T10Dif: ctio_crc2_to_fw overlay ontop of
3331 * ctio7_to_24xx
3332 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003333 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003334 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003335 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003336 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003337 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003338 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003339 CTIO7_FLAGS_DATA_IN);
3340
3341 /* Real finish is ctio_m1's finish */
3342 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003343 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003344 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003345
3346 /* qlt_24xx_init_ctio_to_isp will correct
3347 * all neccessary fields that's part of CTIO7.
3348 * There should be no residual of CTIO-CRC2 data.
3349 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003350 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3351 &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003352 }
3353 } else
3354 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3355
3356
3357 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003358 cmd->cmd_sent_to_fw = 1;
Quinn Tran079a3a32018-09-04 14:19:11 -07003359 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003360
Himanshu Madhani63163e02014-09-25 06:14:59 -04003361 /* Memory Barrier */
3362 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003363 if (qpair->reqq_start_iocbs)
3364 qpair->reqq_start_iocbs(qpair);
3365 else
3366 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003367 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003368
3369 return 0;
3370
3371out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003372 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003373 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003374
3375 return res;
3376}
3377EXPORT_SYMBOL(qlt_xmit_response);
3378
3379int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3380{
3381 struct ctio7_to_24xx *pkt;
3382 struct scsi_qla_host *vha = cmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003383 struct qla_tgt *tgt = cmd->tgt;
3384 struct qla_tgt_prm prm;
Quinn Tran82de8022017-06-13 20:47:17 -07003385 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003386 int res = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003387 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003388
3389 memset(&prm, 0, sizeof(prm));
3390 prm.cmd = cmd;
3391 prm.tgt = tgt;
3392 prm.sg = NULL;
3393 prm.req_cnt = 1;
3394
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003395 /* Calculate number of entries and segments required */
3396 if (qlt_pci_map_calc_cnt(&prm) != 0)
3397 return -EAGAIN;
3398
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003399 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003400 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003401 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003402 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003403 * previous life, just abort the processing.
3404 */
Bart Van Asscheaefed3e2019-04-17 14:44:29 -07003405 cmd->aborted = 1;
3406 cmd->write_data_transferred = 0;
3407 cmd->state = QLA_TGT_STATE_DATA_IN;
3408 vha->hw->tgt.tgt_ops->handle_data(cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003409 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003410 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3411 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003412 cmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003413 return 0;
3414 }
3415
Quinn Tran82de8022017-06-13 20:47:17 -07003416 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003417 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003418 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003419 if (res != 0)
3420 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003421 if (cmd->se_cmd.prot_op)
Quinn Tran82de8022017-06-13 20:47:17 -07003422 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003423 else
Quinn Tran82de8022017-06-13 20:47:17 -07003424 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003425
Quinn Tran810e30b2015-06-10 11:05:20 -04003426 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003427 qpair->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003428 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003429 }
3430
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003431 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003432 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003433 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003434
3435 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003436 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003437
3438 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003439 cmd->cmd_sent_to_fw = 1;
Quinn Tran079a3a32018-09-04 14:19:11 -07003440 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003441
Himanshu Madhani63163e02014-09-25 06:14:59 -04003442 /* Memory Barrier */
3443 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003444 if (qpair->reqq_start_iocbs)
3445 qpair->reqq_start_iocbs(qpair);
3446 else
3447 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003448 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003449
3450 return res;
3451
3452out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003453 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003454 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003455
3456 return res;
3457}
3458EXPORT_SYMBOL(qlt_rdy_to_xfer);
3459
Quinn Tranf83adb62014-04-11 16:54:43 -04003460
3461/*
Quinn Tranbe251522017-03-15 09:48:49 -07003462 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003463 */
Quinn Tranbe251522017-03-15 09:48:49 -07003464static void
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003465qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003466 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003467{
3468 uint8_t *ap = &sts->actual_dif[0];
3469 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003470 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003471 uint8_t scsi_status, sense_key, asc, ascq;
3472 unsigned long flags;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003473 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003474
Quinn Tranbe251522017-03-15 09:48:49 -07003475 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003476
Quinn Tranbe251522017-03-15 09:48:49 -07003477 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3478 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3479 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003480
Quinn Tranbe251522017-03-15 09:48:49 -07003481 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3482 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3483 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003484
Quinn Tranbe251522017-03-15 09:48:49 -07003485 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3486 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003487
Quinn Tranbe251522017-03-15 09:48:49 -07003488 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003489
Quinn Tranbe251522017-03-15 09:48:49 -07003490 /* check appl tag */
3491 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003492 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3493 "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]",
3494 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3495 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3496 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3497 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003498
Quinn Tranbe251522017-03-15 09:48:49 -07003499 cmd->dif_err_code = DIF_ERR_APP;
3500 scsi_status = SAM_STAT_CHECK_CONDITION;
3501 sense_key = ABORTED_COMMAND;
3502 asc = 0x10;
3503 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003504 }
3505
3506 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003507 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003508 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3509 "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] ",
3510 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3511 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3512 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3513 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003514
Quinn Tranbe251522017-03-15 09:48:49 -07003515 cmd->dif_err_code = DIF_ERR_REF;
3516 scsi_status = SAM_STAT_CHECK_CONDITION;
3517 sense_key = ABORTED_COMMAND;
3518 asc = 0x10;
3519 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003520 goto out;
3521 }
3522
Quinn Tranbe251522017-03-15 09:48:49 -07003523 /* check guard */
3524 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003525 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3526 "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]",
3527 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3528 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3529 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3530 cmd->atio.u.isp24.fcp_hdr.ox_id);
3531
Quinn Tranbe251522017-03-15 09:48:49 -07003532 cmd->dif_err_code = DIF_ERR_GRD;
3533 scsi_status = SAM_STAT_CHECK_CONDITION;
3534 sense_key = ABORTED_COMMAND;
3535 asc = 0x10;
3536 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003537 }
3538out:
Quinn Tranbe251522017-03-15 09:48:49 -07003539 switch (cmd->state) {
3540 case QLA_TGT_STATE_NEED_DATA:
3541 /* handle_data will load DIF error code */
3542 cmd->state = QLA_TGT_STATE_DATA_IN;
3543 vha->hw->tgt.tgt_ops->handle_data(cmd);
3544 break;
3545 default:
3546 spin_lock_irqsave(&cmd->cmd_lock, flags);
3547 if (cmd->aborted) {
3548 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3549 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3550 break;
3551 }
3552 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003553
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003554 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3555 ascq);
Quinn Tranbe251522017-03-15 09:48:49 -07003556 /* assume scsi status gets out on the wire.
3557 * Will not wait for completion.
3558 */
3559 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3560 break;
3561 }
3562}
Quinn Tranf83adb62014-04-11 16:54:43 -04003563
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003564/* If hardware_lock held on entry, might drop it, then reaquire */
3565/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003566static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3567 struct imm_ntfy_from_isp *ntfy)
3568{
3569 struct nack_to_isp *nack;
3570 struct qla_hw_data *ha = vha->hw;
3571 request_t *pkt;
3572 int ret = 0;
3573
3574 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3575 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3576
Quinn Tranec7193e2017-03-15 09:48:55 -07003577 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003578 if (pkt == NULL) {
3579 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3580 "qla_target(%d): %s failed: unable to allocate "
3581 "request packet\n", vha->vp_idx, __func__);
3582 return -ENOMEM;
3583 }
3584
3585 pkt->entry_type = NOTIFY_ACK_TYPE;
3586 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003587 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003588
3589 nack = (struct nack_to_isp *)pkt;
3590 nack->ox_id = ntfy->ox_id;
3591
3592 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3593 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3594 nack->u.isp24.flags = ntfy->u.isp24.flags &
3595 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3596 }
3597
3598 /* terminate */
3599 nack->u.isp24.flags |=
3600 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3601
3602 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3603 nack->u.isp24.status = ntfy->u.isp24.status;
3604 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3605 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3606 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3607 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3608 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3609 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3610
3611 qla2x00_start_iocbs(vha, vha->req);
3612 return ret;
3613}
3614
3615static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3616 struct imm_ntfy_from_isp *imm, int ha_locked)
3617{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003618 int rc;
3619
Bart Van Asschee65449a2019-04-17 14:44:26 -07003620 WARN_ON_ONCE(!ha_locked);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003621 rc = __qlt_send_term_imm_notif(vha, imm);
Bart Van Asschee65449a2019-04-17 14:44:26 -07003622 pr_debug("rc = %d\n", rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003623}
3624
Quinn Tran83548fe2017-06-02 09:12:01 -07003625/*
3626 * If hardware_lock held on entry, might drop it, then reaquire
3627 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3628 */
Quinn Tran82de8022017-06-13 20:47:17 -07003629static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003630 struct qla_tgt_cmd *cmd,
3631 struct atio_from_isp *atio)
3632{
Quinn Tran82de8022017-06-13 20:47:17 -07003633 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003634 struct ctio7_to_24xx *ctio24;
3635 struct qla_hw_data *ha = vha->hw;
3636 request_t *pkt;
3637 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003638 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003639
Quinn Tran83548fe2017-06-02 09:12:01 -07003640 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003641
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003642 if (cmd)
3643 vha = cmd->vha;
3644
Quinn Tran82de8022017-06-13 20:47:17 -07003645 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003646 if (pkt == NULL) {
3647 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3648 "qla_target(%d): %s failed: unable to allocate "
3649 "request packet\n", vha->vp_idx, __func__);
3650 return -ENOMEM;
3651 }
3652
3653 if (cmd != NULL) {
3654 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3655 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3656 "qla_target(%d): Terminating cmd %p with "
3657 "incorrect state %d\n", vha->vp_idx, cmd,
3658 cmd->state);
3659 } else
3660 ret = 1;
3661 }
3662
Quinn Tran60a9ead2017-06-13 20:47:28 -07003663 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003664 pkt->entry_count = 1;
3665 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3666
3667 ctio24 = (struct ctio7_to_24xx *)pkt;
3668 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003669 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003670 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003671 ctio24->vp_index = vha->vp_idx;
3672 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3673 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3674 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3675 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003676 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3677 CTIO7_FLAGS_TERMINATE;
3678 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003679 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3680 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003681
Himanshu Madhani63163e02014-09-25 06:14:59 -04003682 /* Memory Barrier */
3683 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003684 if (qpair->reqq_start_iocbs)
3685 qpair->reqq_start_iocbs(qpair);
3686 else
3687 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003688 return ret;
3689}
3690
Quinn Tran82de8022017-06-13 20:47:17 -07003691static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003692 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3693 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003694{
Quinn Tran82de8022017-06-13 20:47:17 -07003695 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003696 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003697 int rc;
3698
Quinn Tran82de8022017-06-13 20:47:17 -07003699 /* why use different vha? NPIV */
3700 if (cmd)
3701 vha = cmd->vha;
3702 else
3703 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003704
3705 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003706 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003707 if (rc == -ENOMEM)
3708 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003709 goto done;
3710 }
Quinn Tran82de8022017-06-13 20:47:17 -07003711 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3712 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003713 if (rc == -ENOMEM)
3714 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003715
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003716done:
Quinn Trana07100e2015-12-07 19:48:57 -05003717 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003718 if (cmd->sg_mapped)
3719 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003720 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3721 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003722
3723 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003724 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003725
Quinn Tran7b898542014-04-11 16:54:44 -04003726 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003727}
3728
Quinn Tran33e79972014-09-25 06:14:55 -04003729static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3730{
3731 struct list_head free_list;
3732 struct qla_tgt_cmd *cmd, *tcmd;
3733
3734 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003735 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003736
3737 cmd = tcmd = NULL;
3738 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3739 INIT_LIST_HEAD(&free_list);
3740 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3741
3742 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3743 list_del(&cmd->cmd_list);
3744 /* This cmd was never sent to TCM. There is no need
3745 * to schedule free or call free_cmd
3746 */
3747 qlt_free_cmd(cmd);
3748 vha->hw->tgt.num_qfull_cmds_alloc--;
3749 }
3750 }
3751 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3752}
3753
3754static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3755{
3756 uint32_t total_leaked;
3757
3758 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3759
3760 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3761 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3762
3763 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3764 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003765 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003766
3767 if (IS_P3P_TYPE(vha->hw))
3768 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3769 else
3770 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3771 qla2xxx_wake_dpc(vha);
3772 }
3773
3774}
3775
Quinn Trana07100e2015-12-07 19:48:57 -05003776int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003777{
3778 struct qla_tgt *tgt = cmd->tgt;
3779 struct scsi_qla_host *vha = tgt->vha;
3780 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003781 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003782
3783 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3784 "qla_target(%d): terminating exchange for aborted cmd=%p "
3785 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3786 se_cmd->tag);
3787
Quinn Trana07100e2015-12-07 19:48:57 -05003788 spin_lock_irqsave(&cmd->cmd_lock, flags);
3789 if (cmd->aborted) {
3790 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3791 /*
3792 * It's normal to see 2 calls in this path:
3793 * 1) XFER Rdy completion + CMD_T_ABORT
3794 * 2) TCM TMR - drain_state_list
3795 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003796 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3797 "multiple abort. %p transport_state %x, t_state %x, "
3798 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3799 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003800 return EIO;
3801 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003802 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003803 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003804 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003805
Quinn Tran82de8022017-06-13 20:47:17 -07003806 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003807 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003808}
3809EXPORT_SYMBOL(qlt_abort_cmd);
3810
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003811void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3812{
Quinn Tran5d964832017-01-19 22:27:59 -08003813 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003814
Quinn Tranf83adb62014-04-11 16:54:43 -04003815 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3816 "%s: se_cmd[%p] ox_id %04x\n",
3817 __func__, &cmd->se_cmd,
3818 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003819
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003820 BUG_ON(cmd->cmd_in_wq);
3821
Quinn Trana07100e2015-12-07 19:48:57 -05003822 if (cmd->sg_mapped)
3823 qlt_unmap_sg(cmd->vha, cmd);
3824
Quinn Tran33e79972014-09-25 06:14:55 -04003825 if (!cmd->q_full)
3826 qlt_decr_num_pend_cmds(cmd->vha);
3827
Quinn Tranf83adb62014-04-11 16:54:43 -04003828 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003829 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003830 if (unlikely(cmd->free_sg))
3831 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003832
3833 if (!sess || !sess->se_sess) {
3834 WARN_ON(1);
3835 return;
3836 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003837 cmd->jiffies_at_free = get_jiffies_64();
Matthew Wilcox83c2b542018-06-12 12:05:43 -07003838 target_free_tag(sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003839}
3840EXPORT_SYMBOL(qlt_free_cmd);
3841
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003842/*
3843 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3844 */
Quinn Tran82de8022017-06-13 20:47:17 -07003845static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003846 struct qla_tgt_cmd *cmd, uint32_t status)
3847{
3848 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003849 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003850
Quinn Tranbe251522017-03-15 09:48:49 -07003851 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003852 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003853 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3854 "se_cmd=%p tag[%x] op %#x/%s",
3855 cmd->lba, cmd->lba,
3856 cmd->num_blks, &cmd->se_cmd,
3857 cmd->atio.u.isp24.exchange_addr,
3858 cmd->se_cmd.prot_op,
3859 prot_op_str(cmd->se_cmd.prot_op));
3860
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003861 if (ctio != NULL) {
3862 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07003863
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003864 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003865 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003866 } else
3867 term = 1;
3868
3869 if (term)
himanshu.madhani@cavium.com3efc31f2018-01-15 20:46:47 -08003870 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003871
3872 return term;
3873}
3874
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003875
3876/* ha->hardware_lock supposed to be held on entry */
Quinn Tran6b0431d2018-09-04 14:19:13 -07003877static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003878 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003879{
Quinn Tran6b0431d2018-09-04 14:19:13 -07003880 void *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003881 struct req_que *req;
3882 int qid = GET_QID(handle);
3883 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003884
Quinn Tran82de8022017-06-13 20:47:17 -07003885 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3886 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003887
Quinn Tran82de8022017-06-13 20:47:17 -07003888 if (qid == rsp->req->id) {
3889 req = rsp->req;
3890 } else if (vha->hw->req_q_map[qid]) {
3891 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3892 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3893 vha->vp_idx, rsp->id, handle);
3894 req = vha->hw->req_q_map[qid];
3895 } else {
3896 return NULL;
3897 }
Arun Easi667024a2014-09-25 06:14:47 -04003898
Quinn Tran82de8022017-06-13 20:47:17 -07003899 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003900
Quinn Tran82de8022017-06-13 20:47:17 -07003901 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003902 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003903 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3904 "qla_target(%d): Wrong handle %x received\n",
3905 vha->vp_idx, handle);
3906 return NULL;
3907 }
Quinn Tran82de8022017-06-13 20:47:17 -07003908
Quinn Tran6b0431d2018-09-04 14:19:13 -07003909 cmd = (void *) req->outstanding_cmds[h];
Quinn Tran82de8022017-06-13 20:47:17 -07003910 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003911 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003912 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3913 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003914 return NULL;
3915 }
Quinn Tran82de8022017-06-13 20:47:17 -07003916 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003917 } else if (ctio != NULL) {
3918 /* We can't get loop ID from CTIO7 */
3919 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3920 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3921 "support NULL handles\n", vha->vp_idx);
3922 return NULL;
3923 }
3924
3925 return cmd;
3926}
3927
3928/*
3929 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3930 */
Quinn Tran82de8022017-06-13 20:47:17 -07003931static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3932 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003933{
3934 struct qla_hw_data *ha = vha->hw;
3935 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003936 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07003937 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003938
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003939 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3940 /* That could happen only in case of an error/reset/abort */
3941 if (status != CTIO_SUCCESS) {
3942 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3943 "Intermediate CTIO received"
3944 " (status %x)\n", status);
3945 }
3946 return;
3947 }
3948
Bart Van Assche81bcf1c2019-04-11 14:53:24 -07003949 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003950 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003951 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003952
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003953 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003954 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003955
Joern Engelf9b67212014-09-16 16:23:18 -04003956 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003957
3958 if (unlikely(status != CTIO_SUCCESS)) {
3959 switch (status & 0xFFFF) {
Quinn Tran079a3a32018-09-04 14:19:11 -07003960 case CTIO_INVALID_RX_ID:
3961 if (printk_ratelimit())
3962 dev_info(&vha->hw->pdev->dev,
3963 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
3964 vha->vp_idx, cmd->atio.u.isp24.attr,
3965 ((cmd->ctio_flags >> 9) & 0xf),
3966 cmd->ctio_flags);
3967
3968 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003969 case CTIO_LIP_RESET:
3970 case CTIO_TARGET_RESET:
3971 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003972 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003973 case CTIO_TIMEOUT:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003974 /* They are OK */
3975 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3976 "qla_target(%d): CTIO with "
3977 "status %#x received, state %x, se_cmd %p, "
3978 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3979 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3980 status, cmd->state, se_cmd);
3981 break;
3982
3983 case CTIO_PORT_LOGGED_OUT:
3984 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003985 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003986 int logged_out =
3987 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3988
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003989 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003990 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003991 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003992 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003993 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003994
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003995 if (logged_out && cmd->sess) {
3996 /*
3997 * Session is already logged out, but we need
3998 * to notify initiator, who's not aware of this
3999 */
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004000 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07004001 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08004002 "%s %d %8phC post del sess\n",
4003 __func__, __LINE__, cmd->sess->port_name);
4004
Quinn Trand8630bb2017-12-28 12:33:43 -08004005 qlt_schedule_sess_for_deletion(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004006 }
4007 break;
4008 }
Quinn Tranf83adb62014-04-11 16:54:43 -04004009 case CTIO_DIF_ERROR: {
4010 struct ctio_crc_from_fw *crc =
4011 (struct ctio_crc_from_fw *)ctio;
4012 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07004013 "qla_target(%d): CTIO with DIF_ERROR status %x "
4014 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4015 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04004016 vha->vp_idx, status, cmd->state, se_cmd,
4017 *((u64 *)&crc->actual_dif[0]),
4018 *((u64 *)&crc->expected_dif[0]));
4019
Quinn Tran8abfa9e2017-06-13 20:47:24 -07004020 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07004021 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04004022 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004023 default:
4024 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04004025 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004026 vha->vp_idx, status, cmd->state, se_cmd);
4027 break;
4028 }
4029
Quinn Tran7b898542014-04-11 16:54:44 -04004030
Quinn Tran193b50b2015-12-17 14:57:03 -05004031 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04004032 * cmd is already aborted/terminated, we don't
4033 * need to terminate again. The exchange is already
4034 * cleaned up/freed at FW level. Just cleanup at driver
4035 * level.
4036 */
4037 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004038 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004039 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004040 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004041 return;
Quinn Tran7b898542014-04-11 16:54:44 -04004042 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004043 }
4044
4045 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004046 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004047 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004048 cmd->state = QLA_TGT_STATE_DATA_IN;
4049
Bart Van Assche52c82822015-07-09 07:23:26 -07004050 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004051 cmd->write_data_transferred = 1;
4052
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004053 ha->tgt.tgt_ops->handle_data(cmd);
4054 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05004055 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004056 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004057 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02004058 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004059 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004060 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004061 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4062 "qla_target(%d): A command in state (%d) should "
4063 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4064 }
4065
Quinn Tran7b898542014-04-11 16:54:44 -04004066 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004067 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004068 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4069 dump_stack();
4070 }
4071
4072 ha->tgt.tgt_ops->free_cmd(cmd);
4073}
4074
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004075static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4076 uint8_t task_codes)
4077{
4078 int fcp_task_attr;
4079
4080 switch (task_codes) {
4081 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004082 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004083 break;
4084 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004085 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004086 break;
4087 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004088 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004089 break;
4090 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004091 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004092 break;
4093 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004094 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004095 break;
4096 default:
4097 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4098 "qla_target: unknown task code %x, use ORDERED instead\n",
4099 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004100 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004101 break;
4102 }
4103
4104 return fcp_task_attr;
4105}
4106
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004107/*
4108 * Process context for I/O path into tcm_qla2xxx code
4109 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004110static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004111{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004112 scsi_qla_host_t *vha = cmd->vha;
4113 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004114 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004115 struct atio_from_isp *atio = &cmd->atio;
4116 unsigned char *cdb;
4117 unsigned long flags;
4118 uint32_t data_length;
4119 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07004120 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004121
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004122 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004123 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004124
Quinn Tran193b50b2015-12-17 14:57:03 -05004125 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004126 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4127 "cmd with tag %u is aborted\n",
4128 cmd->atio.u.isp24.exchange_addr);
4129 goto out_term;
4130 }
4131
Quinn Trana07100e2015-12-07 19:48:57 -05004132 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004133 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004134 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004135
4136 if (atio->u.isp24.fcp_cmnd.rddata &&
4137 atio->u.isp24.fcp_cmnd.wrdata) {
4138 bidi = 1;
4139 data_dir = DMA_TO_DEVICE;
4140 } else if (atio->u.isp24.fcp_cmnd.rddata)
4141 data_dir = DMA_FROM_DEVICE;
4142 else if (atio->u.isp24.fcp_cmnd.wrdata)
4143 data_dir = DMA_TO_DEVICE;
4144 else
4145 data_dir = DMA_NONE;
4146
4147 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4148 atio->u.isp24.fcp_cmnd.task_attr);
Quinn Trane25f7652018-05-01 09:01:50 -07004149 data_length = get_datalen_for_atio(atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004150
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004151 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4152 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004153 if (ret != 0)
4154 goto out_term;
4155 /*
Bart Van Assche67eb4a602019-04-17 14:44:10 -07004156 * Drop extra session reference from qlt_handle_cmd_for_atio().
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004157 */
Quinn Tran5d964832017-01-19 22:27:59 -08004158 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004159 return;
4160
4161out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004162 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004163 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004164 * cmd has not sent to target yet, so pass NULL as the second
4165 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004166 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004167 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004168 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4169 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004170
4171 qlt_decr_num_pend_cmds(vha);
Matthew Wilcox83c2b542018-06-12 12:05:43 -07004172 target_free_tag(sess->se_sess, &cmd->se_cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07004173 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004174
Quinn Tran5d964832017-01-19 22:27:59 -08004175 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004176}
4177
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004178static void qlt_do_work(struct work_struct *work)
4179{
4180 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004181 scsi_qla_host_t *vha = cmd->vha;
4182 unsigned long flags;
4183
4184 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4185 list_del(&cmd->cmd_list);
4186 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004187
4188 __qlt_do_work(cmd);
4189}
4190
Quinn Tran09620ee2017-06-13 20:47:20 -07004191void qlt_clr_qp_table(struct scsi_qla_host *vha)
4192{
4193 unsigned long flags;
4194 struct qla_hw_data *ha = vha->hw;
4195 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4196 void *node;
4197 u64 key = 0;
4198
4199 ql_log(ql_log_info, vha, 0x706c,
4200 "User update Number of Active Qpairs %d\n",
4201 ha->tgt.num_act_qpairs);
4202
4203 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4204
4205 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4206 btree_remove64(&tgt->lun_qpair_map, key);
4207
4208 ha->base_qpair->lun_cnt = 0;
4209 for (key = 0; key < ha->max_qpairs; key++)
4210 if (ha->queue_pair_map[key])
4211 ha->queue_pair_map[key]->lun_cnt = 0;
4212
4213 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4214}
4215
Quinn Trane326d222017-06-13 20:47:18 -07004216static void qlt_assign_qpair(struct scsi_qla_host *vha,
4217 struct qla_tgt_cmd *cmd)
4218{
4219 struct qla_qpair *qpair, *qp;
4220 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4221 struct qla_qpair_hint *h;
4222
4223 if (vha->flags.qpairs_available) {
4224 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4225 if (unlikely(!h)) {
4226 /* spread lun to qpair ratio evently */
4227 int lcnt = 0, rc;
4228 struct scsi_qla_host *base_vha =
4229 pci_get_drvdata(vha->hw->pdev);
4230
4231 qpair = vha->hw->base_qpair;
4232 if (qpair->lun_cnt == 0) {
4233 qpair->lun_cnt++;
4234 h = qla_qpair_to_hint(tgt, qpair);
4235 BUG_ON(!h);
4236 rc = btree_insert64(&tgt->lun_qpair_map,
4237 cmd->unpacked_lun, h, GFP_ATOMIC);
4238 if (rc) {
4239 qpair->lun_cnt--;
4240 ql_log(ql_log_info, vha, 0xd037,
4241 "Unable to insert lun %llx into lun_qpair_map\n",
4242 cmd->unpacked_lun);
4243 }
4244 goto out;
4245 } else {
4246 lcnt = qpair->lun_cnt;
4247 }
4248
4249 h = NULL;
4250 list_for_each_entry(qp, &base_vha->qp_list,
4251 qp_list_elem) {
4252 if (qp->lun_cnt == 0) {
4253 qp->lun_cnt++;
4254 h = qla_qpair_to_hint(tgt, qp);
4255 BUG_ON(!h);
4256 rc = btree_insert64(&tgt->lun_qpair_map,
4257 cmd->unpacked_lun, h, GFP_ATOMIC);
4258 if (rc) {
4259 qp->lun_cnt--;
4260 ql_log(ql_log_info, vha, 0xd038,
4261 "Unable to insert lun %llx into lun_qpair_map\n",
4262 cmd->unpacked_lun);
4263 }
4264 qpair = qp;
4265 goto out;
4266 } else {
4267 if (qp->lun_cnt < lcnt) {
4268 lcnt = qp->lun_cnt;
4269 qpair = qp;
4270 continue;
4271 }
4272 }
4273 }
4274 BUG_ON(!qpair);
4275 qpair->lun_cnt++;
4276 h = qla_qpair_to_hint(tgt, qpair);
4277 BUG_ON(!h);
4278 rc = btree_insert64(&tgt->lun_qpair_map,
4279 cmd->unpacked_lun, h, GFP_ATOMIC);
4280 if (rc) {
4281 qpair->lun_cnt--;
4282 ql_log(ql_log_info, vha, 0xd039,
4283 "Unable to insert lun %llx into lun_qpair_map\n",
4284 cmd->unpacked_lun);
4285 }
4286 }
4287 } else {
4288 h = &tgt->qphints[0];
4289 }
4290out:
4291 cmd->qpair = h->qpair;
4292 cmd->se_cmd.cpuid = h->cpuid;
4293}
4294
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004295static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004296 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004297 struct atio_from_isp *atio)
4298{
4299 struct se_session *se_sess = sess->se_sess;
4300 struct qla_tgt_cmd *cmd;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004301 int tag, cpu;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004302
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004303 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004304 if (tag < 0)
4305 return NULL;
4306
4307 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4308 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004309 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004310 memcpy(&cmd->atio, atio, sizeof(*atio));
4311 cmd->state = QLA_TGT_STATE_NEW;
4312 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004313 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004314 cmd->vha = vha;
4315 cmd->se_cmd.map_tag = tag;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004316 cmd->se_cmd.map_cpu = cpu;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004317 cmd->sess = sess;
4318 cmd->loop_id = sess->loop_id;
4319 cmd->conf_compl_supported = sess->conf_compl_supported;
4320
Quinn Tran1eb42f92017-01-19 22:27:55 -08004321 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004322 cmd->jiffies_at_alloc = get_jiffies_64();
4323
Quinn Trane326d222017-06-13 20:47:18 -07004324 cmd->unpacked_lun = scsilun_to_int(
4325 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4326 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004327 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004328 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004329
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004330 return cmd;
4331}
4332
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004333/* ha->hardware_lock supposed to be held on entry */
4334static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4335 struct atio_from_isp *atio)
4336{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004337 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004338 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004339 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004340 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004341 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08004342 port_id_t id;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004343
4344 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004345 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004346 "New command while device %p is shutting down\n", tgt);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004347 return -ENODEV;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004348 }
4349
Quinn Trana4239942017-12-28 12:33:26 -08004350 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4351 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4352 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4353 if (IS_SW_RESV_ADDR(id))
4354 return -EBUSY;
4355
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004356 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
Quinn Tranfb352652017-12-28 12:33:32 -08004357 if (unlikely(!sess))
4358 return -EFAULT;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004359
4360 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4361 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004362 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004363 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004364 "New command while old session %p is being deleted\n",
4365 sess);
4366 return -EFAULT;
4367 }
4368
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004369 /*
4370 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4371 */
Quinn Tran726b8542017-01-19 22:28:00 -08004372 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004373 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004374 "%s: kref_get fail, %8phC oxid %x \n",
4375 __func__, sess->port_name,
4376 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4377 return -EFAULT;
4378 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004379
4380 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004381 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004382 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004383 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004384 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004385 return -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004386 }
4387
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004388 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004389 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004390
Quinn Tran726b8542017-01-19 22:28:00 -08004391 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004392 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004393 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004394
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004395 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004396 if (vha->flags.qpairs_available) {
4397 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4398 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004399 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4400 queue_work_on(smp_processor_id(), qla_tgt_wq,
4401 &cmd->work);
4402 else
4403 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4404 &cmd->work);
4405 } else {
4406 queue_work(qla_tgt_wq, &cmd->work);
4407 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004408
Quinn Tran82de8022017-06-13 20:47:17 -07004409 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004410}
4411
4412/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004413static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004414 int fn, void *iocb, int flags)
4415{
4416 struct scsi_qla_host *vha = sess->vha;
4417 struct qla_hw_data *ha = vha->hw;
4418 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004419 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Quinn Tran84905df2018-05-01 09:01:53 -07004420 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004421
4422 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4423 if (!mcmd) {
4424 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4425 "qla_target(%d): Allocation of management "
4426 "command failed, some commands and their data could "
4427 "leak\n", vha->vp_idx);
4428 return -ENOMEM;
4429 }
4430 memset(mcmd, 0, sizeof(*mcmd));
4431 mcmd->sess = sess;
4432
4433 if (iocb) {
4434 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4435 sizeof(mcmd->orig_iocb.imm_ntfy));
4436 }
4437 mcmd->tmr_func = fn;
4438 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004439 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran84905df2018-05-01 09:01:53 -07004440 mcmd->qpair = h->qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004441 mcmd->vha = vha;
Quinn Tran84905df2018-05-01 09:01:53 -07004442 mcmd->se_cmd.cpuid = h->cpuid;
4443 mcmd->unpacked_lun = lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004444
4445 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004446 case QLA_TGT_LUN_RESET:
Quinn Tran84905df2018-05-01 09:01:53 -07004447 case QLA_TGT_CLEAR_TS:
4448 case QLA_TGT_ABORT_TS:
4449 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
Bart Van Assche50435d42018-10-18 15:45:40 -07004450 /* fall through */
Quinn Tran84905df2018-05-01 09:01:53 -07004451 case QLA_TGT_CLEAR_ACA:
4452 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4453 mcmd->qpair = h->qpair;
4454 mcmd->se_cmd.cpuid = h->cpuid;
4455 break;
4456
4457 case QLA_TGT_TARGET_RESET:
4458 case QLA_TGT_NEXUS_LOSS_SESS:
4459 case QLA_TGT_NEXUS_LOSS:
4460 case QLA_TGT_ABORT_ALL:
4461 default:
4462 /* no-op */
4463 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004464 }
4465
Quinn Tran84905df2018-05-01 09:01:53 -07004466 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4467 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4468 &mcmd->work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004469
4470 return 0;
4471}
4472
4473/* ha->hardware_lock supposed to be held on entry */
4474static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4475{
4476 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4477 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004478 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004479 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004480 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004481 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004482
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004483 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004484
4485 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004486 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4487 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004488 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4489
Quinn Tranf775bd12017-06-02 09:11:59 -07004490 unpacked_lun =
4491 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004492
Quinn Tranfb352652017-12-28 12:33:32 -08004493 if (sess == NULL || sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004494 return -EFAULT;
4495
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004496 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4497}
4498
4499/* ha->hardware_lock supposed to be held on entry */
4500static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004501 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004502{
4503 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4504 struct qla_hw_data *ha = vha->hw;
4505 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004506 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004507 int rc;
4508
4509 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4510 if (mcmd == NULL) {
4511 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4512 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4513 vha->vp_idx, __func__);
4514 return -ENOMEM;
4515 }
4516 memset(mcmd, 0, sizeof(*mcmd));
4517
4518 mcmd->sess = sess;
4519 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4520 sizeof(mcmd->orig_iocb.imm_ntfy));
4521
Quinn Tranf775bd12017-06-02 09:11:59 -07004522 unpacked_lun =
4523 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004524 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004525 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004526 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004527
Quinn Tranbe92fc32017-01-19 22:27:54 -08004528 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004529 le16_to_cpu(iocb->u.isp2x.seq_id));
4530 if (rc != 0) {
4531 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4532 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4533 vha->vp_idx, rc);
4534 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4535 return -EFAULT;
4536 }
4537
4538 return 0;
4539}
4540
4541/* ha->hardware_lock supposed to be held on entry */
4542static int qlt_abort_task(struct scsi_qla_host *vha,
4543 struct imm_ntfy_from_isp *iocb)
4544{
4545 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004546 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004547 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004548 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004549
4550 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4551
Quinn Tran75601512015-12-17 14:57:04 -05004552 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004553 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004554 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4555
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004556 if (sess == NULL) {
4557 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4558 "qla_target(%d): task abort for unexisting "
4559 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004560 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004561 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4562 }
4563
4564 return __qlt_abort_task(vha, iocb, sess);
4565}
4566
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004567void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4568{
Quinn Tran726b8542017-01-19 22:28:00 -08004569 if (rc != MBS_COMMAND_COMPLETE) {
4570 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4571 "%s: se_sess %p / sess %p from"
4572 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4573 " LOGO failed: %#x\n",
4574 __func__,
4575 fcport->se_sess,
4576 fcport,
4577 fcport->port_name, fcport->loop_id,
4578 fcport->d_id.b.domain, fcport->d_id.b.area,
4579 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004580 }
Quinn Tran726b8542017-01-19 22:28:00 -08004581
4582 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004583}
4584
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004585/*
4586* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4587*
4588* Schedules sessions with matching port_id/loop_id but different wwn for
4589* deletion. Returns existing session with matching wwn if present.
4590* Null otherwise.
4591*/
Quinn Tran726b8542017-01-19 22:28:00 -08004592struct fc_port *
4593qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004594 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004595{
Quinn Tran5d964832017-01-19 22:27:59 -08004596 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004597 uint64_t other_wwn;
4598
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004599 *conflict_sess = NULL;
4600
Quinn Tran5d964832017-01-19 22:27:59 -08004601 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004602
4603 other_wwn = wwn_to_u64(other_sess->port_name);
4604
4605 if (wwn == other_wwn) {
4606 WARN_ON(sess);
4607 sess = other_sess;
4608 continue;
4609 }
4610
4611 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004612 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004613 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004614 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004615 "Invalidating sess %p loop_id %d wwn %llx.\n",
4616 other_sess, other_sess->loop_id, other_wwn);
4617
4618 /*
4619 * logout_on_delete is set by default, but another
4620 * session that has the same s_id/loop_id combo
4621 * might have cleared it when requested this session
4622 * deletion, so don't touch it
4623 */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004624 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004625 } else {
4626 /*
4627 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004628 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004629 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004630 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004631 "Invalidating sess %p loop_id %d wwn %llx.\n",
4632 other_sess, other_sess->loop_id, other_wwn);
4633
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004634 other_sess->keep_nport_handle = 1;
Quinn Tranba743f92017-12-04 14:45:12 -08004635 if (other_sess->disc_state != DSC_DELETED)
4636 *conflict_sess = other_sess;
Quinn Tran94cff6e2017-12-28 12:33:42 -08004637 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004638 }
4639 continue;
4640 }
4641
4642 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004643 if ((loop_id == other_sess->loop_id) &&
4644 (loop_id != FC_NO_LOOP_ID)) {
4645 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004646 "Invalidating sess %p loop_id %d wwn %llx.\n",
4647 other_sess, other_sess->loop_id, other_wwn);
4648
4649 /* Same loop_id but different s_id
4650 * Ok to kill and logout */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004651 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004652 }
4653 }
4654
4655 return sess;
4656}
4657
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004658/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4659static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4660{
4661 struct qla_tgt_sess_op *op;
4662 struct qla_tgt_cmd *cmd;
4663 uint32_t key;
4664 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004665 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004666
4667 key = (((u32)s_id->b.domain << 16) |
4668 ((u32)s_id->b.area << 8) |
4669 ((u32)s_id->b.al_pa));
4670
Quinn Tran8b631d82017-06-02 09:11:54 -07004671 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004672 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4673 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004674
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004675 if (op_key == key) {
4676 op->aborted = true;
4677 count++;
4678 }
4679 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004680
4681 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4682 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004683
Quinn Tran41dc5292017-01-19 22:28:03 -08004684 if (op_key == key) {
4685 op->aborted = true;
4686 count++;
4687 }
4688 }
4689
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004690 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4691 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004692
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004693 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004694 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004695 count++;
4696 }
4697 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004698 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004699
4700 return count;
4701}
4702
Quinn Tran9cd883f2017-12-28 12:33:24 -08004703static int qlt_handle_login(struct scsi_qla_host *vha,
4704 struct imm_ntfy_from_isp *iocb)
4705{
4706 struct fc_port *sess = NULL, *conflict_sess = NULL;
4707 uint64_t wwn;
4708 port_id_t port_id;
4709 uint16_t loop_id, wd3_lo;
4710 int res = 0;
4711 struct qlt_plogi_ack_t *pla;
4712 unsigned long flags;
4713
Bart Van Assche57bf5952019-08-08 20:01:34 -07004714 lockdep_assert_held(&vha->hw->hardware_lock);
4715
Quinn Tran9cd883f2017-12-28 12:33:24 -08004716 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4717
4718 port_id.b.domain = iocb->u.isp24.port_id[2];
4719 port_id.b.area = iocb->u.isp24.port_id[1];
4720 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4721 port_id.b.rsvd_1 = 0;
4722
4723 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4724
4725 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4726 abort_cmds_for_s_id(vha, &port_id);
4727
4728 if (wwn) {
4729 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4730 sess = qlt_find_sess_invalidate_other(vha, wwn,
4731 port_id, loop_id, &conflict_sess);
4732 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Tranaa9e6d72018-08-31 11:24:38 -07004733 } else {
4734 ql_dbg(ql_dbg_disc, vha, 0xffff,
4735 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4736 __func__, __LINE__, loop_id, port_id.b24);
4737 qlt_send_term_imm_notif(vha, iocb, 1);
4738 goto out;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004739 }
4740
4741 if (IS_SW_RESV_ADDR(port_id)) {
4742 res = 1;
4743 goto out;
4744 }
4745
4746 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4747 if (!pla) {
Quinn Tran48acad02018-08-02 13:16:44 -07004748 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4749 "%s %d %8phC Term INOT due to mem alloc fail",
4750 __func__, __LINE__,
4751 iocb->u.isp24.port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004752 qlt_send_term_imm_notif(vha, iocb, 1);
4753 goto out;
4754 }
4755
4756 if (conflict_sess) {
4757 conflict_sess->login_gen++;
4758 qlt_plogi_ack_link(vha, pla, conflict_sess,
4759 QLT_PLOGI_LINK_CONFLICT);
4760 }
4761
4762 if (!sess) {
4763 pla->ref_count++;
4764 ql_dbg(ql_dbg_disc, vha, 0xffff,
4765 "%s %d %8phC post new sess\n",
4766 __func__, __LINE__, iocb->u.isp24.port_name);
Quinn Trana4239942017-12-28 12:33:26 -08004767 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4768 qla24xx_post_newsess_work(vha, &port_id,
4769 iocb->u.isp24.port_name,
4770 iocb->u.isp24.u.plogi.node_name,
4771 pla, FC4_TYPE_UNKNOWN);
4772 else
4773 qla24xx_post_newsess_work(vha, &port_id,
4774 iocb->u.isp24.port_name, NULL,
4775 pla, FC4_TYPE_UNKNOWN);
4776
Quinn Tran9cd883f2017-12-28 12:33:24 -08004777 goto out;
4778 }
4779
Quinn Trancd4ed6b2018-08-31 11:24:31 -07004780 if (sess->disc_state == DSC_UPD_FCPORT) {
4781 u16 sec;
4782
4783 /*
4784 * Remote port registration is still going on from
4785 * previous login. Allow it to finish before we
4786 * accept the new login.
4787 */
4788 sess->next_disc_state = DSC_DELETE_PEND;
4789 sec = jiffies_to_msecs(jiffies -
4790 sess->jiffies_at_registration) / 1000;
4791 if (sess->sec_since_registration < sec && sec &&
4792 !(sec % 5)) {
4793 sess->sec_since_registration = sec;
4794 ql_dbg(ql_dbg_disc, vha, 0xffff,
4795 "%s %8phC - Slow Rport registration (%d Sec)\n",
4796 __func__, sess->port_name, sec);
4797 }
4798
4799 if (!conflict_sess)
4800 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4801
4802 qlt_send_term_imm_notif(vha, iocb, 1);
4803 goto out;
4804 }
4805
Quinn Tran9cd883f2017-12-28 12:33:24 -08004806 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4807 sess->d_id = port_id;
4808 sess->login_gen++;
4809
4810 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4811 sess->fw_login_state = DSC_LS_PRLI_PEND;
4812 sess->local = 0;
4813 sess->loop_id = loop_id;
4814 sess->d_id = port_id;
4815 sess->fw_login_state = DSC_LS_PRLI_PEND;
4816 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4817
4818 if (wd3_lo & BIT_7)
4819 sess->conf_compl_supported = 1;
4820
4821 if ((wd3_lo & BIT_4) == 0)
4822 sess->port_type = FCT_INITIATOR;
4823 else
4824 sess->port_type = FCT_TARGET;
4825
4826 } else
4827 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4828
4829
4830 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4831 "%s %d %8phC DS %d\n",
4832 __func__, __LINE__, sess->port_name, sess->disc_state);
4833
4834 switch (sess->disc_state) {
4835 case DSC_DELETED:
4836 qlt_plogi_ack_unref(vha, pla);
4837 break;
4838
4839 default:
4840 /*
4841 * Under normal circumstances we want to release nport handle
4842 * during LOGO process to avoid nport handle leaks inside FW.
4843 * The exception is when LOGO is done while another PLOGI with
4844 * the same nport handle is waiting as might be the case here.
4845 * Note: there is always a possibily of a race where session
4846 * deletion has already started for other reasons (e.g. ACL
4847 * removal) and now PLOGI arrives:
4848 * 1. if PLOGI arrived in FW after nport handle has been freed,
4849 * FW must have assigned this PLOGI a new/same handle and we
4850 * can proceed ACK'ing it as usual when session deletion
4851 * completes.
4852 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4853 * bit reached it, the handle has now been released. We'll
4854 * get an error when we ACK this PLOGI. Nothing will be sent
4855 * back to initiator. Initiator should eventually retry
4856 * PLOGI and situation will correct itself.
4857 */
4858 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4859 (sess->d_id.b24 == port_id.b24));
4860
4861 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4862 "%s %d %8phC post del sess\n",
4863 __func__, __LINE__, sess->port_name);
4864
4865
Quinn Trand8630bb2017-12-28 12:33:43 -08004866 qlt_schedule_sess_for_deletion(sess);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004867 break;
4868 }
4869out:
4870 return res;
4871}
4872
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004873/*
4874 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4875 */
4876static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4877 struct imm_ntfy_from_isp *iocb)
4878{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004879 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004880 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004881 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004882 uint64_t wwn;
4883 port_id_t port_id;
4884 uint16_t loop_id;
4885 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004886 int res = 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004887 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004888
Bart Van Assche57bf5952019-08-08 20:01:34 -07004889 lockdep_assert_held(&ha->hardware_lock);
4890
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004891 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4892
4893 port_id.b.domain = iocb->u.isp24.port_id[2];
4894 port_id.b.area = iocb->u.isp24.port_id[1];
4895 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4896 port_id.b.rsvd_1 = 0;
4897
4898 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4899
Quinn Tran726b8542017-01-19 22:28:00 -08004900 ql_dbg(ql_dbg_disc, vha, 0xf026,
4901 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4902 vha->vp_idx, iocb->u.isp24.port_id[2],
4903 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4904 iocb->u.isp24.status_subcode, loop_id,
4905 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004906
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004907 /* res = 1 means ack at the end of thread
4908 * res = 0 means ack async/later.
4909 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004910 switch (iocb->u.isp24.status_subcode) {
4911 case ELS_PLOGI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004912 res = qlt_handle_login(vha, iocb);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004913 break;
4914
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004915 case ELS_PRLI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004916 if (N2N_TOPO(ha)) {
4917 sess = qla2x00_find_fcport_by_wwpn(vha,
4918 iocb->u.isp24.port_name, 1);
4919
4920 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4921 ql_dbg(ql_dbg_disc, vha, 0xffff,
4922 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4923 __func__, __LINE__,
4924 iocb->u.isp24.port_name);
4925 qlt_send_term_imm_notif(vha, iocb, 1);
4926 break;
4927 }
4928
4929 res = qlt_handle_login(vha, iocb);
4930 break;
4931 }
4932
Quinn Trana4239942017-12-28 12:33:26 -08004933 if (IS_SW_RESV_ADDR(port_id)) {
4934 res = 1;
4935 break;
4936 }
4937
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004938 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4939
Quinn Tran75601512015-12-17 14:57:04 -05004940 if (wwn) {
4941 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004942 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4943 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004944 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4945 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004946
4947 if (conflict_sess) {
Quinn Tran5ef696a2017-12-04 14:45:05 -08004948 switch (conflict_sess->disc_state) {
4949 case DSC_DELETED:
4950 case DSC_DELETE_PEND:
4951 break;
4952 default:
4953 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4954 "PRLI with conflicting sess %p port %8phC\n",
4955 conflict_sess, conflict_sess->port_name);
4956 conflict_sess->fw_login_state =
4957 DSC_LS_PORT_UNAVAIL;
4958 qlt_send_term_imm_notif(vha, iocb, 1);
4959 res = 0;
4960 break;
4961 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004962 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004963
4964 if (sess != NULL) {
Quinn Trana4239942017-12-28 12:33:26 -08004965 bool delete = false;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07004966 int sec;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004967
Quinn Tran82abdca2017-12-28 12:33:22 -08004968 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4969 switch (sess->fw_login_state) {
Quinn Trana4239942017-12-28 12:33:26 -08004970 case DSC_LS_PLOGI_PEND:
Quinn Tran82abdca2017-12-28 12:33:22 -08004971 case DSC_LS_PLOGI_COMP:
4972 case DSC_LS_PRLI_COMP:
4973 break;
4974 default:
Quinn Trana4239942017-12-28 12:33:26 -08004975 delete = true;
4976 break;
4977 }
4978
4979 switch (sess->disc_state) {
Quinn Trancd4ed6b2018-08-31 11:24:31 -07004980 case DSC_UPD_FCPORT:
4981 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4982 flags);
4983
4984 sec = jiffies_to_msecs(jiffies -
4985 sess->jiffies_at_registration)/1000;
4986 if (sess->sec_since_registration < sec && sec &&
4987 !(sec % 5)) {
4988 sess->sec_since_registration = sec;
4989 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
4990 "%s %8phC : Slow Rport registration(%d Sec)\n",
4991 __func__, sess->port_name, sec);
4992 }
4993 qlt_send_term_imm_notif(vha, iocb, 1);
4994 return 0;
4995
Quinn Trana4239942017-12-28 12:33:26 -08004996 case DSC_LOGIN_PEND:
4997 case DSC_GPDB:
Quinn Trana4239942017-12-28 12:33:26 -08004998 case DSC_LOGIN_COMPLETE:
4999 case DSC_ADISC:
5000 delete = false;
5001 break;
5002 default:
5003 break;
5004 }
5005
5006 if (delete) {
Quinn Tran82abdca2017-12-28 12:33:22 -08005007 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5008 flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005009 /*
5010 * Impatient initiator sent PRLI before last
5011 * PLOGI could finish. Will force him to re-try,
5012 * while last one finishes.
5013 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04005014 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005015 "sess %p PRLI received, before plogi ack.\n",
5016 sess);
5017 qlt_send_term_imm_notif(vha, iocb, 1);
5018 res = 0;
5019 break;
5020 }
5021
5022 /*
5023 * This shouldn't happen under normal circumstances,
5024 * since we have deleted the old session during PLOGI
5025 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04005026 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005027 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5028 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5029
5030 sess->local = 0;
5031 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08005032 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005033 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005034
5035 if (wd3_lo & BIT_7)
5036 sess->conf_compl_supported = 1;
5037
Quinn Tran726b8542017-01-19 22:28:00 -08005038 if ((wd3_lo & BIT_4) == 0)
5039 sess->port_type = FCT_INITIATOR;
5040 else
5041 sess->port_type = FCT_TARGET;
Quinn Tran82abdca2017-12-28 12:33:22 -08005042
5043 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005044 }
5045 res = 1; /* send notify ack */
5046
5047 /* Make session global (not used in fabric mode) */
5048 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005049 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005050 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07005051 "%s %d %8phC post nack\n",
5052 __func__, __LINE__, sess->port_name);
5053 qla24xx_post_nack_work(vha, sess, iocb,
5054 SRB_NACK_PRLI);
5055 res = 0;
5056 } else {
5057 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5058 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5059 qla2xxx_wake_dpc(vha);
5060 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005061 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005062 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005063 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07005064 "%s %d %8phC post nack\n",
5065 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08005066 qla24xx_post_nack_work(vha, sess, iocb,
5067 SRB_NACK_PRLI);
5068 res = 0;
5069 }
5070 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005071 break;
5072
Quinn Tran41dc5292017-01-19 22:28:03 -08005073 case ELS_TPRLO:
5074 if (le16_to_cpu(iocb->u.isp24.flags) &
5075 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5076 loop_id = 0xFFFF;
5077 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5078 res = 1;
5079 break;
5080 }
Bart Van Assche81881862017-12-07 16:02:46 -08005081 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005082 case ELS_LOGO:
5083 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08005084 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5085 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5086 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5087
5088 if (sess) {
5089 sess->login_gen++;
5090 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08005091 sess->logo_ack_needed = 1;
5092 memcpy(sess->iocb, iocb, IOCB_SIZE);
5093 }
5094
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005095 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08005096
Quinn Tran83548fe2017-06-02 09:12:01 -07005097 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08005098 "%s: logo %llx res %d sess %p ",
5099 __func__, wwn, res, sess);
5100 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08005101 /*
5102 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5103 * for LOGO_ACK & sess delete
5104 */
Quinn Tran726b8542017-01-19 22:28:00 -08005105 BUG_ON(!sess);
5106 res = 0;
5107 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08005108 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08005109 if (sess) {
Quinn Trand8630bb2017-12-28 12:33:43 -08005110 qlt_schedule_sess_for_deletion(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08005111 res = 0;
5112 }
5113 /* else logo will be ack */
5114 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005115 break;
5116 case ELS_PDISC:
5117 case ELS_ADISC:
5118 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005119 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005120
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005121 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005122 qlt_send_notify_ack(ha->base_qpair,
5123 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005124 tgt->link_reinit_iocb_pending = 0;
5125 }
Quinn Tran726b8542017-01-19 22:28:00 -08005126
5127 sess = qla2x00_find_fcport_by_wwpn(vha,
5128 iocb->u.isp24.port_name, 1);
5129 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005130 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08005131 "sess %p lid %d|%d DS %d LS %d\n",
5132 sess, sess->loop_id, loop_id,
5133 sess->disc_state, sess->fw_login_state);
5134 }
5135
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005136 res = 1; /* send notify ack */
5137 break;
5138 }
5139
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005140 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005141 default:
5142 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5143 "qla_target(%d): Unsupported ELS command %x "
5144 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5145 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5146 break;
5147 }
5148
Quinn Tran9cd883f2017-12-28 12:33:24 -08005149 ql_dbg(ql_dbg_disc, vha, 0xf026,
5150 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5151 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5152
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005153 return res;
5154}
5155
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005156/*
5157 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5158 */
5159static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5160 struct imm_ntfy_from_isp *iocb)
5161{
5162 struct qla_hw_data *ha = vha->hw;
5163 uint32_t add_flags = 0;
5164 int send_notify_ack = 1;
5165 uint16_t status;
5166
Bart Van Assche57bf5952019-08-08 20:01:34 -07005167 lockdep_assert_held(&ha->hardware_lock);
5168
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005169 status = le16_to_cpu(iocb->u.isp2x.status);
5170 switch (status) {
5171 case IMM_NTFY_LIP_RESET:
5172 {
5173 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5174 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5175 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5176 iocb->u.isp24.status_subcode);
5177
5178 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5179 send_notify_ack = 0;
5180 break;
5181 }
5182
5183 case IMM_NTFY_LIP_LINK_REINIT:
5184 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005185 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005186
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5188 "qla_target(%d): LINK REINIT (loop %#x, "
5189 "subcode %x)\n", vha->vp_idx,
5190 le16_to_cpu(iocb->u.isp24.nport_handle),
5191 iocb->u.isp24.status_subcode);
5192 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005193 qlt_send_notify_ack(ha->base_qpair,
5194 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005195 }
5196 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5197 tgt->link_reinit_iocb_pending = 1;
5198 /*
5199 * QLogic requires to wait after LINK REINIT for possible
5200 * PDISC or ADISC ELS commands
5201 */
5202 send_notify_ack = 0;
5203 break;
5204 }
5205
5206 case IMM_NTFY_PORT_LOGOUT:
5207 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5208 "qla_target(%d): Port logout (loop "
5209 "%#x, subcode %x)\n", vha->vp_idx,
5210 le16_to_cpu(iocb->u.isp24.nport_handle),
5211 iocb->u.isp24.status_subcode);
5212
5213 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5214 send_notify_ack = 0;
5215 /* The sessions will be cleared in the callback, if needed */
5216 break;
5217
5218 case IMM_NTFY_GLBL_TPRLO:
5219 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5220 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5221 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5222 send_notify_ack = 0;
5223 /* The sessions will be cleared in the callback, if needed */
5224 break;
5225
5226 case IMM_NTFY_PORT_CONFIG:
5227 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5228 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5229 status);
5230 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5231 send_notify_ack = 0;
5232 /* The sessions will be cleared in the callback, if needed */
5233 break;
5234
5235 case IMM_NTFY_GLBL_LOGO:
5236 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5237 "qla_target(%d): Link failure detected\n",
5238 vha->vp_idx);
5239 /* I_T nexus loss */
5240 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5241 send_notify_ack = 0;
5242 break;
5243
5244 case IMM_NTFY_IOCB_OVERFLOW:
5245 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5246 "qla_target(%d): Cannot provide requested "
5247 "capability (IOCB overflowed the immediate notify "
5248 "resource count)\n", vha->vp_idx);
5249 break;
5250
5251 case IMM_NTFY_ABORT_TASK:
5252 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5253 "qla_target(%d): Abort Task (S %08x I %#x -> "
5254 "L %#x)\n", vha->vp_idx,
5255 le16_to_cpu(iocb->u.isp2x.seq_id),
5256 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5257 le16_to_cpu(iocb->u.isp2x.lun));
5258 if (qlt_abort_task(vha, iocb) == 0)
5259 send_notify_ack = 0;
5260 break;
5261
5262 case IMM_NTFY_RESOURCE:
5263 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5264 "qla_target(%d): Out of resources, host %ld\n",
5265 vha->vp_idx, vha->host_no);
5266 break;
5267
5268 case IMM_NTFY_MSG_RX:
5269 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5270 "qla_target(%d): Immediate notify task %x\n",
5271 vha->vp_idx, iocb->u.isp2x.task_flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005272 break;
5273
5274 case IMM_NTFY_ELS:
5275 if (qlt_24xx_handle_els(vha, iocb) == 0)
5276 send_notify_ack = 0;
5277 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005278 default:
5279 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5280 "qla_target(%d): Received unknown immediate "
5281 "notify status %x\n", vha->vp_idx, status);
5282 break;
5283 }
5284
5285 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005286 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5287 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005288}
5289
5290/*
5291 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5292 * This function sends busy to ISP 2xxx or 24xx.
5293 */
Quinn Tran82de8022017-06-13 20:47:17 -07005294static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005295 struct atio_from_isp *atio, uint16_t status)
5296{
Quinn Tran82de8022017-06-13 20:47:17 -07005297 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005298 struct ctio7_to_24xx *ctio24;
5299 struct qla_hw_data *ha = vha->hw;
5300 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005301 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005302 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005303 u16 temp;
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005304 port_id_t id;
5305
5306 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5307 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5308 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5309 id.b.rsvd_1 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005310
Quinn Tran75601512015-12-17 14:57:04 -05005311 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005312 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
Quinn Tran75601512015-12-17 14:57:04 -05005313 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005314 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005315 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005316 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005317 }
5318 /* Sending marker isn't necessary, since we called from ISR */
5319
Quinn Tran82de8022017-06-13 20:47:17 -07005320 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005321 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005322 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005323 "qla_target(%d): %s failed: unable to allocate "
5324 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005325 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005326 }
5327
Quinn Tran60a9ead2017-06-13 20:47:28 -07005328 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005329 pkt->entry_count = 1;
5330 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5331
5332 ctio24 = (struct ctio7_to_24xx *)pkt;
5333 ctio24->entry_type = CTIO_TYPE7;
5334 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005335 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005336 ctio24->vp_index = vha->vp_idx;
5337 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5338 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5339 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5340 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005341 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005342 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005343 CTIO7_FLAGS_DONT_RET_CTIO;
5344 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005345 /*
5346 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5347 * if the explicit conformation is used.
5348 */
5349 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5350 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Quinn Trane25f7652018-05-01 09:01:50 -07005351
5352 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5353
5354 if (ctio24->u.status1.residual != 0)
5355 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5356
Himanshu Madhani63163e02014-09-25 06:14:59 -04005357 /* Memory Barrier */
5358 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005359 if (qpair->reqq_start_iocbs)
5360 qpair->reqq_start_iocbs(qpair);
5361 else
5362 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005363 return 0;
5364}
5365
5366/*
5367 * This routine is used to allocate a command for either a QFull condition
5368 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5369 * out previously.
5370 */
5371static void
5372qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5373 struct atio_from_isp *atio, uint16_t status, int qfull)
5374{
5375 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5376 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005377 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005378 struct se_session *se_sess;
5379 struct qla_tgt_cmd *cmd;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005380 int tag, cpu;
Quinn Tran82de8022017-06-13 20:47:17 -07005381 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005382
5383 if (unlikely(tgt->tgt_stop)) {
5384 ql_dbg(ql_dbg_io, vha, 0x300a,
5385 "New command while device %p is shutting down\n", tgt);
5386 return;
5387 }
5388
5389 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5390 vha->hw->tgt.num_qfull_cmds_dropped++;
5391 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005392 vha->qla_stats.stat_max_qfull_cmds_dropped)
5393 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005394 vha->hw->tgt.num_qfull_cmds_dropped;
5395
5396 ql_dbg(ql_dbg_io, vha, 0x3068,
5397 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5398 vha->vp_idx, __func__,
5399 vha->hw->tgt.num_qfull_cmds_dropped);
5400
5401 qlt_chk_exch_leak_thresh_hold(vha);
5402 return;
5403 }
5404
5405 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5406 (vha, atio->u.isp24.fcp_hdr.s_id);
5407 if (!sess)
5408 return;
5409
5410 se_sess = sess->se_sess;
5411
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005412 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Bart Van Asschec04466c2019-04-17 14:44:27 -07005413 if (tag < 0) {
Quinn Tran33e79972014-09-25 06:14:55 -04005414 ql_dbg(ql_dbg_io, vha, 0x3009,
5415 "qla_target(%d): %s: Allocation of cmd failed\n",
5416 vha->vp_idx, __func__);
5417
5418 vha->hw->tgt.num_qfull_cmds_dropped++;
5419 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005420 vha->qla_stats.stat_max_qfull_cmds_dropped)
5421 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005422 vha->hw->tgt.num_qfull_cmds_dropped;
5423
5424 qlt_chk_exch_leak_thresh_hold(vha);
5425 return;
5426 }
5427
Bart Van Asschec04466c2019-04-17 14:44:27 -07005428 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
Quinn Tran33e79972014-09-25 06:14:55 -04005429 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5430
5431 qlt_incr_num_pend_cmds(vha);
5432 INIT_LIST_HEAD(&cmd->cmd_list);
5433 memcpy(&cmd->atio, atio, sizeof(*atio));
5434
5435 cmd->tgt = vha->vha_tgt.qla_tgt;
5436 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005437 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005438 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005439 cmd->qpair = ha->base_qpair;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005440 cmd->se_cmd.map_cpu = cpu;
Quinn Tran33e79972014-09-25 06:14:55 -04005441
5442 if (qfull) {
5443 cmd->q_full = 1;
5444 /* NOTE: borrowing the state field to carry the status */
5445 cmd->state = status;
5446 } else
5447 cmd->term_exchg = 1;
5448
Quinn Tran82de8022017-06-13 20:47:17 -07005449 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005450 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5451
5452 vha->hw->tgt.num_qfull_cmds_alloc++;
5453 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005454 vha->qla_stats.stat_max_qfull_cmds_alloc)
5455 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005456 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005457 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005458}
5459
5460int
Quinn Tran82de8022017-06-13 20:47:17 -07005461qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005462{
Quinn Tran82de8022017-06-13 20:47:17 -07005463 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005464 struct qla_hw_data *ha = vha->hw;
5465 unsigned long flags;
5466 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005467 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005468 int rc = 0;
5469
5470 if (list_empty(&ha->tgt.q_full_list))
5471 return 0;
5472
5473 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005474 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005475
Quinn Tran82de8022017-06-13 20:47:17 -07005476 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005477 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005478 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005479 return 0;
5480 }
5481
Quinn Tran82de8022017-06-13 20:47:17 -07005482 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5483 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5484
5485 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5486 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005487 if (cmd->q_full)
5488 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005489 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005490 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005491 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005492
5493 if (rc == -ENOMEM)
5494 break;
5495
5496 if (cmd->q_full)
5497 ql_dbg(ql_dbg_io, vha, 0x3006,
5498 "%s: busy sent for ox_id[%04x]\n", __func__,
5499 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5500 else if (cmd->term_exchg)
5501 ql_dbg(ql_dbg_io, vha, 0x3007,
5502 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5503 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5504 else
5505 ql_dbg(ql_dbg_io, vha, 0x3008,
5506 "%s: Unexpected cmd in QFull list %p\n", __func__,
5507 cmd);
5508
5509 list_del(&cmd->cmd_list);
5510 list_add_tail(&cmd->cmd_list, &free_list);
5511
5512 /* piggy back on hardware_lock for protection */
5513 vha->hw->tgt.num_qfull_cmds_alloc--;
5514 }
Quinn Tran82de8022017-06-13 20:47:17 -07005515 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005516
5517 cmd = NULL;
5518
5519 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5520 list_del(&cmd->cmd_list);
5521 /* This cmd was never sent to TCM. There is no need
5522 * to schedule free or call free_cmd
5523 */
5524 qlt_free_cmd(cmd);
5525 }
Quinn Tran82de8022017-06-13 20:47:17 -07005526
5527 if (!list_empty(&q_full_list)) {
5528 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5529 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5530 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5531 }
5532
Quinn Tran33e79972014-09-25 06:14:55 -04005533 return rc;
5534}
5535
5536static void
Quinn Tran82de8022017-06-13 20:47:17 -07005537qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5538 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005539{
5540 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005541 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005542
Quinn Tran82de8022017-06-13 20:47:17 -07005543 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005544 if (rc == -ENOMEM)
5545 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5546}
5547
5548static int
Quinn Tran82de8022017-06-13 20:47:17 -07005549qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5550 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005551{
5552 struct qla_hw_data *ha = vha->hw;
Quinn Tran8b666802017-03-15 09:48:45 -07005553 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005554
5555 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5556 return 0;
5557
Quinn Tran8b666802017-03-15 09:48:45 -07005558 if (!ha_locked)
5559 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005560 qlt_send_busy(qpair, atio, qla_sam_status);
Quinn Tran8b666802017-03-15 09:48:45 -07005561 if (!ha_locked)
5562 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5563
Quinn Tran33e79972014-09-25 06:14:55 -04005564 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005565}
5566
5567/* ha->hardware_lock supposed to be held on entry */
5568/* called via callback from qla2xxx */
5569static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005570 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005571{
5572 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005573 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005574 int rc;
Quinn Tran7cf95f72017-12-28 12:33:28 -08005575 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005576
5577 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005578 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005579 "ATIO pkt, but no tgt (ha %p)", ha);
5580 return;
5581 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005582 /*
5583 * In tgt_stop mode we also should allow all requests to pass.
5584 * Otherwise, some commands can stuck.
5585 */
5586
Quinn Tran2f424b92015-12-17 14:57:07 -05005587 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005588
5589 switch (atio->u.raw.entry_type) {
5590 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005591 if (unlikely(atio->u.isp24.exchange_addr ==
5592 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005593 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005594 "qla_target(%d): ATIO_TYPE7 "
5595 "received with UNKNOWN exchange address, "
5596 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005597 if (!ha_locked)
5598 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005599 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
Quinn Tran2f424b92015-12-17 14:57:07 -05005600 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005601 spin_unlock_irqrestore(&ha->hardware_lock,
5602 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005603 break;
5604 }
Quinn Tran33e79972014-09-25 06:14:55 -04005605
Quinn Tran33e79972014-09-25 06:14:55 -04005606 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005607 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5608 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005609 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005610 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005611 return;
5612 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005613 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005614 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005615 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005616 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005617 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005618 if (!ha_locked)
5619 spin_lock_irqsave(&ha->hardware_lock, flags);
5620 switch (rc) {
5621 case -ENODEV:
5622 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5623 "qla_target: Unable to send command to target\n");
5624 break;
5625 case -EBADF:
5626 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5627 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005628 qlt_send_term_exchange(ha->base_qpair, NULL,
5629 atio, 1, 0);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005630 break;
5631 case -EBUSY:
5632 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5633 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5634 vha->vp_idx);
5635 qlt_send_busy(ha->base_qpair, atio,
5636 tc_sam_status);
5637 break;
5638 default:
5639 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5640 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5641 vha->vp_idx);
5642 qlt_send_busy(ha->base_qpair, atio,
5643 qla_sam_status);
5644 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005645 }
Quinn Tran7cf95f72017-12-28 12:33:28 -08005646 if (!ha_locked)
5647 spin_unlock_irqrestore(&ha->hardware_lock,
5648 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005649 }
5650 break;
5651
5652 case IMMED_NOTIFY_TYPE:
5653 {
5654 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5655 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5656 "qla_target(%d): Received ATIO packet %x "
5657 "with error status %x\n", vha->vp_idx,
5658 atio->u.raw.entry_type,
5659 atio->u.isp2x.entry_status);
5660 break;
5661 }
5662 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005663
5664 if (!ha_locked)
5665 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005666 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005667 if (!ha_locked)
5668 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005669 break;
5670 }
5671
5672 default:
5673 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5674 "qla_target(%d): Received unknown ATIO atio "
5675 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5676 break;
5677 }
5678
Quinn Tran2f424b92015-12-17 14:57:07 -05005679 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005680}
5681
Quinn Tran06910942018-09-04 14:19:12 -07005682/*
5683 * qpair lock is assume to be held
5684 * rc = 0 : send terminate & abts respond
5685 * rc != 0: do not send term & abts respond
5686 */
5687static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5688 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5689{
5690 struct qla_hw_data *ha = vha->hw;
5691 int rc = 0;
5692
5693 /*
5694 * Detect unresolved exchange. If the same ABTS is unable
5695 * to terminate an existing command and the same ABTS loops
5696 * between FW & Driver, then force FW dump. Under 1 jiff,
5697 * we should see multiple loops.
5698 */
5699 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5700 qpair->retry_term_jiff == jiffies) {
5701 /* found existing exchange */
5702 qpair->retry_term_cnt++;
5703 if (qpair->retry_term_cnt >= 5) {
5704 rc = EIO;
5705 qpair->retry_term_cnt = 0;
5706 ql_log(ql_log_warn, vha, 0xffff,
5707 "Unable to send ABTS Respond. Dumping firmware.\n");
5708 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5709 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5710
5711 if (qpair == ha->base_qpair)
5712 ha->isp_ops->fw_dump(vha, 1);
5713 else
5714 ha->isp_ops->fw_dump(vha, 0);
5715
5716 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5717 qla2xxx_wake_dpc(vha);
5718 }
5719 } else if (qpair->retry_term_jiff != jiffies) {
5720 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5721 qpair->retry_term_cnt = 0;
5722 qpair->retry_term_jiff = jiffies;
5723 }
5724
5725 return rc;
5726}
5727
Quinn Tran6b0431d2018-09-04 14:19:13 -07005728
5729static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5730 struct rsp_que *rsp, response_t *pkt)
5731{
5732 struct abts_resp_from_24xx_fw *entry =
5733 (struct abts_resp_from_24xx_fw *)pkt;
5734 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5735 struct qla_tgt_mgmt_cmd *mcmd;
5736 struct qla_hw_data *ha = vha->hw;
5737
Bart Van Assche81bcf1c2019-04-11 14:53:24 -07005738 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
Quinn Tran6b0431d2018-09-04 14:19:13 -07005739 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5740 ql_dbg(ql_dbg_async, vha, 0xe064,
5741 "qla_target(%d): ABTS Comp without mcmd\n",
5742 vha->vp_idx);
5743 return;
5744 }
5745
5746 if (mcmd)
5747 vha = mcmd->vha;
5748 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5749
5750 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5751 "ABTS_RESP_24XX: compl_status %x\n",
5752 entry->compl_status);
5753
5754 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5755 if ((entry->error_subcode1 == 0x1E) &&
5756 (entry->error_subcode2 == 0)) {
5757 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5758 ha->tgt.tgt_ops->free_mcmd(mcmd);
5759 return;
5760 }
5761 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5762 pkt, mcmd);
5763 } else {
5764 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5765 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5766 vha->vp_idx, entry->compl_status,
5767 entry->error_subcode1,
5768 entry->error_subcode2);
5769 ha->tgt.tgt_ops->free_mcmd(mcmd);
5770 }
5771 } else {
5772 ha->tgt.tgt_ops->free_mcmd(mcmd);
5773 }
5774}
5775
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005776/* ha->hardware_lock supposed to be held on entry */
5777/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005778static void qlt_response_pkt(struct scsi_qla_host *vha,
5779 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005780{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005781 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005782
5783 if (unlikely(tgt == NULL)) {
5784 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005785 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5786 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005787 return;
5788 }
5789
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005790 /*
5791 * In tgt_stop mode we also should allow all requests to pass.
5792 * Otherwise, some commands can stuck.
5793 */
5794
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005795 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005796 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005797 case CTIO_TYPE7:
5798 {
5799 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005800
Quinn Tran82de8022017-06-13 20:47:17 -07005801 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005802 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5803 entry);
5804 break;
5805 }
5806
5807 case ACCEPT_TGT_IO_TYPE:
5808 {
5809 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5810 int rc;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005811
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005812 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005813 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005814 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5815 "qla_target(%d): ATIO with error "
5816 "status %x received\n", vha->vp_idx,
5817 le16_to_cpu(atio->u.isp2x.status));
5818 break;
5819 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005820
Quinn Tran82de8022017-06-13 20:47:17 -07005821 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005822 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005823 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005824
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005825 rc = qlt_handle_cmd_for_atio(vha, atio);
5826 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005827 switch (rc) {
5828 case -ENODEV:
5829 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5830 "qla_target: Unable to send command to target\n");
5831 break;
5832 case -EBADF:
5833 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5834 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5835 qlt_send_term_exchange(rsp->qpair, NULL,
5836 atio, 1, 0);
5837 break;
5838 case -EBUSY:
5839 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5840 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5841 vha->vp_idx);
5842 qlt_send_busy(rsp->qpair, atio,
5843 tc_sam_status);
5844 break;
5845 default:
5846 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5847 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5848 vha->vp_idx);
5849 qlt_send_busy(rsp->qpair, atio,
5850 qla_sam_status);
5851 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005852 }
5853 }
5854 }
5855 break;
5856
5857 case CONTINUE_TGT_IO_TYPE:
5858 {
5859 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005860
Quinn Tran82de8022017-06-13 20:47:17 -07005861 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005862 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5863 entry);
5864 break;
5865 }
5866
5867 case CTIO_A64_TYPE:
5868 {
5869 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005870
Quinn Tran82de8022017-06-13 20:47:17 -07005871 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005872 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5873 entry);
5874 break;
5875 }
5876
5877 case IMMED_NOTIFY_TYPE:
5878 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5879 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5880 break;
5881
5882 case NOTIFY_ACK_TYPE:
5883 if (tgt->notify_ack_expected > 0) {
5884 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005885
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005886 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5887 "NOTIFY_ACK seq %08x status %x\n",
5888 le16_to_cpu(entry->u.isp2x.seq_id),
5889 le16_to_cpu(entry->u.isp2x.status));
5890 tgt->notify_ack_expected--;
5891 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005892 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005893 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5894 "qla_target(%d): NOTIFY_ACK "
5895 "failed %x\n", vha->vp_idx,
5896 le16_to_cpu(entry->u.isp2x.status));
5897 }
5898 } else {
5899 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5900 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5901 vha->vp_idx);
5902 }
5903 break;
5904
5905 case ABTS_RECV_24XX:
5906 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5907 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5908 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5909 break;
5910
5911 case ABTS_RESP_24XX:
5912 if (tgt->abts_resp_expected > 0) {
Quinn Tran6b0431d2018-09-04 14:19:13 -07005913 qlt_handle_abts_completion(vha, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005914 } else {
5915 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5916 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5917 "received\n", vha->vp_idx);
5918 }
5919 break;
5920
5921 default:
5922 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5923 "qla_target(%d): Received unknown response pkt "
5924 "type %x\n", vha->vp_idx, pkt->entry_type);
5925 break;
5926 }
5927
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005928}
5929
5930/*
5931 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5932 */
5933void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5934 uint16_t *mailbox)
5935{
5936 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005937 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005938 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005939
Quinn Tran3a33dc92017-06-02 09:12:04 -07005940 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005941 return;
5942
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005943 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5944 IS_QLA2100(ha))
5945 return;
5946 /*
5947 * In tgt_stop mode we also should allow all requests to pass.
5948 * Otherwise, some commands can stuck.
5949 */
5950
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005951
5952 switch (code) {
5953 case MBA_RESET: /* Reset */
5954 case MBA_SYSTEM_ERR: /* System Error */
5955 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5956 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5957 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5958 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005959 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005960 break;
5961 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5962 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5963 break;
5964
5965 case MBA_LOOP_UP:
5966 {
5967 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005968 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005969 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5970 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5971 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005972 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005973 qlt_send_notify_ack(ha->base_qpair,
5974 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005975 0, 0, 0, 0, 0, 0);
5976 tgt->link_reinit_iocb_pending = 0;
5977 }
5978 break;
5979 }
5980
5981 case MBA_LIP_OCCURRED:
5982 case MBA_LOOP_DOWN:
5983 case MBA_LIP_RESET:
5984 case MBA_RSCN_UPDATE:
5985 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005986 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005987 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5988 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5989 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005990 break;
5991
Quinn Tranead03852017-01-19 22:28:01 -08005992 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005993 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5994 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5995 vha->vp_idx,
5996 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5997 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005998
5999 if (le16_to_cpu(mailbox[3]) == 1) {
6000 /* exchange starvation. */
6001 vha->hw->exch_starvation++;
6002 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006003 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08006004 "Exchange starvation-. Resetting RISC\n");
6005
6006 vha->hw->exch_starvation = 0;
6007 if (IS_P3P_TYPE(vha->hw))
6008 set_bit(FCOE_CTX_RESET_NEEDED,
6009 &vha->dpc_flags);
6010 else
6011 set_bit(ISP_ABORT_NEEDED,
6012 &vha->dpc_flags);
6013 qla2xxx_wake_dpc(vha);
6014 }
6015 }
6016 break;
6017
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006018 case MBA_PORT_UPDATE:
6019 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6020 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006021 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01006022 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6023 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6024 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6025
6026 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08006027 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006028 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6029 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08006030 vha->hw->exch_starvation = 0;
6031 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006032 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6033 "Async MB 2: Port Logged Out\n");
6034 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006035 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006036 break;
6037 }
6038
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006039}
6040
6041static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6042 uint16_t loop_id)
6043{
Quinn Tran726b8542017-01-19 22:28:00 -08006044 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006045 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08006046 unsigned long flags;
6047 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006048
Quinn Tran063b36d2017-12-04 14:45:10 -08006049 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006050 if (!fcport) {
6051 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6052 "qla_target(%d): Allocation of tmp FC port failed",
6053 vha->vp_idx);
6054 return NULL;
6055 }
6056
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006057 fcport->loop_id = loop_id;
6058
Quinn Tran15f30a52017-03-15 09:48:52 -07006059 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006060 if (rc != QLA_SUCCESS) {
6061 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6062 "qla_target(%d): Failed to retrieve fcport "
6063 "information -- get_port_database() returned %x "
6064 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6065 kfree(fcport);
6066 return NULL;
6067 }
6068
Quinn Tran726b8542017-01-19 22:28:00 -08006069 del = NULL;
6070 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6071 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6072
6073 if (tfcp) {
6074 tfcp->d_id = fcport->d_id;
6075 tfcp->port_type = fcport->port_type;
6076 tfcp->supported_classes = fcport->supported_classes;
6077 tfcp->flags |= fcport->flags;
Quinn Tran76f9a2d2017-12-04 14:45:11 -08006078 tfcp->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08006079
6080 del = fcport;
6081 fcport = tfcp;
6082 } else {
6083 if (vha->hw->current_topology == ISP_CFG_F)
6084 fcport->flags |= FCF_FABRIC_DEVICE;
6085
6086 list_add_tail(&fcport->list, &vha->vp_fcports);
6087 if (!IS_SW_RESV_ADDR(fcport->d_id))
6088 vha->fcport_count++;
6089 fcport->login_gen++;
6090 fcport->disc_state = DSC_LOGIN_COMPLETE;
6091 fcport->login_succ = 1;
6092 newfcport = 1;
6093 }
6094
6095 fcport->deleted = 0;
6096 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6097
6098 switch (vha->host->active_mode) {
6099 case MODE_INITIATOR:
6100 case MODE_DUAL:
6101 if (newfcport) {
6102 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Trancd4ed6b2018-08-31 11:24:31 -07006103 qla24xx_sched_upd_fcport(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08006104 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07006105 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08006106 "%s %d %8phC post gpsc fcp_cnt %d\n",
6107 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6108 qla24xx_post_gpsc_work(vha, fcport);
6109 }
6110 }
6111 break;
6112
6113 case MODE_TARGET:
6114 default:
6115 break;
6116 }
6117 if (del)
6118 qla2x00_free_fcport(del);
6119
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006120 return fcport;
6121}
6122
6123/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08006124static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006125 uint8_t *s_id)
6126{
Quinn Tran5d964832017-01-19 22:27:59 -08006127 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006128 fc_port_t *fcport = NULL;
6129 int rc, global_resets;
6130 uint16_t loop_id = 0;
6131
Quinn Tran726b8542017-01-19 22:28:00 -08006132 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
6133 /*
6134 * This is Domain Controller, so it should be
6135 * OK to drop SCSI commands from it.
6136 */
6137 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6138 "Unable to find initiator with S_ID %x:%x:%x",
6139 s_id[0], s_id[1], s_id[2]);
6140 return NULL;
6141 }
6142
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006143 mutex_lock(&vha->vha_tgt.tgt_mutex);
6144
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006145retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006146 global_resets =
6147 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006148
6149 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6150 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006151 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6152
Quinn Tran726b8542017-01-19 22:28:00 -08006153 ql_log(ql_log_info, vha, 0xf071,
6154 "qla_target(%d): Unable to find "
6155 "initiator with S_ID %x:%x:%x",
6156 vha->vp_idx, s_id[0], s_id[1],
6157 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006158
6159 if (rc == -ENOENT) {
6160 qlt_port_logo_t logo;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07006161
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006162 sid_to_portid(s_id, &logo.id);
6163 logo.cmd_count = 1;
6164 qlt_send_first_logo(vha, &logo);
6165 }
6166
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006167 return NULL;
6168 }
6169
6170 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006171 if (!fcport) {
6172 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006173 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006174 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006175
6176 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006177 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006178 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6179 "qla_target(%d): global reset during session discovery "
6180 "(counter was %d, new %d), retrying", vha->vp_idx,
6181 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006182 atomic_read(&vha->vha_tgt.
6183 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006184 goto retry;
6185 }
6186
6187 sess = qlt_create_sess(vha, fcport, true);
6188
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006189 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6190
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006191 return sess;
6192}
6193
6194static void qlt_abort_work(struct qla_tgt *tgt,
6195 struct qla_tgt_sess_work_param *prm)
6196{
6197 struct scsi_qla_host *vha = tgt->vha;
6198 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08006199 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006200 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006201 uint8_t s_id[3];
6202 int rc;
6203
Quinn Tran75601512015-12-17 14:57:04 -05006204 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006205
6206 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05006207 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006208
6209 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6210 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6211 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6212
Bart Van Asscheac452b82019-08-08 20:01:40 -07006213 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006214 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006215 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006216
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006217 sess = qlt_make_local_sess(vha, s_id);
6218 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006219
Quinn Tran75601512015-12-17 14:57:04 -05006220 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006221 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05006222 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006223 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006224 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006225 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006226 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006227 }
6228
Quinn Tran726b8542017-01-19 22:28:00 -08006229 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006230 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08006231 "%s: kref_get fail %8phC \n",
6232 __func__, sess->port_name);
6233 sess = NULL;
6234 goto out_term2;
6235 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006236 }
6237
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006238 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006239 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6240
Bart Van Assche300ec742019-04-17 14:44:41 -07006241 ha->tgt.tgt_ops->put_sess(sess);
6242
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006243 if (rc != 0)
6244 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006245 return;
6246
Quinn Tran75601512015-12-17 14:57:04 -05006247out_term2:
Bart Van Assche300ec742019-04-17 14:44:41 -07006248 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6249
Quinn Tran726b8542017-01-19 22:28:00 -08006250 if (sess)
6251 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006252
6253out_term:
6254 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006255 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6256 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006257 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006258}
6259
6260static void qlt_tmr_work(struct qla_tgt *tgt,
6261 struct qla_tgt_sess_work_param *prm)
6262{
6263 struct atio_from_isp *a = &prm->tm_iocb2;
6264 struct scsi_qla_host *vha = tgt->vha;
6265 struct qla_hw_data *ha = vha->hw;
Colin Ian Kingbb6abdd2019-02-15 09:52:32 +00006266 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006267 unsigned long flags;
6268 uint8_t *s_id = NULL; /* to hide compiler warnings */
6269 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07006270 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07006271 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006272 void *iocb;
6273
Quinn Tran75601512015-12-17 14:57:04 -05006274 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006275
6276 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006277 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006278
6279 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6280 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6281 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006282 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006283
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006284 sess = qlt_make_local_sess(vha, s_id);
6285 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006286
Quinn Tran75601512015-12-17 14:57:04 -05006287 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006288 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006289 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006290 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006291 if (sess->deleted) {
Quinn Tranf159b3c2017-03-15 09:48:47 -07006292 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006293 }
6294
Quinn Tran726b8542017-01-19 22:28:00 -08006295 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006296 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08006297 "%s: kref_get fail %8phC\n",
6298 __func__, sess->port_name);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006299 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006300 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006301 }
6302
6303 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006304 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006305 unpacked_lun =
6306 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006307
6308 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006309 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6310
Bart Van Assche300ec742019-04-17 14:44:41 -07006311 ha->tgt.tgt_ops->put_sess(sess);
6312
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006313 if (rc != 0)
6314 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006315 return;
6316
Quinn Tranf159b3c2017-03-15 09:48:47 -07006317out_term2:
Quinn Tranf159b3c2017-03-15 09:48:47 -07006318 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006319out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006320 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006321}
6322
6323static void qlt_sess_work_fn(struct work_struct *work)
6324{
6325 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6326 struct scsi_qla_host *vha = tgt->vha;
6327 unsigned long flags;
6328
6329 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6330
6331 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6332 while (!list_empty(&tgt->sess_works_list)) {
6333 struct qla_tgt_sess_work_param *prm = list_entry(
6334 tgt->sess_works_list.next, typeof(*prm),
6335 sess_works_list_entry);
6336
6337 /*
6338 * This work can be scheduled on several CPUs at time, so we
6339 * must delete the entry to eliminate double processing
6340 */
6341 list_del(&prm->sess_works_list_entry);
6342
6343 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6344
6345 switch (prm->type) {
6346 case QLA_TGT_SESS_WORK_ABORT:
6347 qlt_abort_work(tgt, prm);
6348 break;
6349 case QLA_TGT_SESS_WORK_TM:
6350 qlt_tmr_work(tgt, prm);
6351 break;
6352 default:
6353 BUG_ON(1);
6354 break;
6355 }
6356
6357 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6358
6359 kfree(prm);
6360 }
6361 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6362}
6363
6364/* Must be called under tgt_host_action_mutex */
6365int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6366{
6367 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006368 int rc, i;
6369 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006370
6371 if (!QLA_TGT_MODE_ENABLED())
6372 return 0;
6373
Arun Easi33c36c02013-01-30 03:34:41 -05006374 if (!IS_TGT_MODE_CAPABLE(ha)) {
6375 ql_log(ql_log_warn, base_vha, 0xe070,
6376 "This adapter does not support target mode.\n");
6377 return 0;
6378 }
6379
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006380 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006381 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006382
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006383 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006384
6385 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6386 if (!tgt) {
6387 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6388 "Unable to allocate struct qla_tgt\n");
6389 return -ENOMEM;
6390 }
6391
Kees Cook6396bb22018-06-12 14:03:40 -07006392 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6393 sizeof(struct qla_qpair_hint),
6394 GFP_KERNEL);
Quinn Trane326d222017-06-13 20:47:18 -07006395 if (!tgt->qphints) {
6396 kfree(tgt);
6397 ql_log(ql_log_warn, base_vha, 0x0197,
6398 "Unable to allocate qpair hints.\n");
6399 return -ENOMEM;
6400 }
6401
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006402 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6403 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6404
Quinn Trane326d222017-06-13 20:47:18 -07006405 rc = btree_init64(&tgt->lun_qpair_map);
6406 if (rc) {
6407 kfree(tgt->qphints);
6408 kfree(tgt);
6409 ql_log(ql_log_info, base_vha, 0x0198,
6410 "Unable to initialize lun_qpair_map btree\n");
6411 return -EIO;
6412 }
6413 h = &tgt->qphints[0];
6414 h->qpair = ha->base_qpair;
6415 INIT_LIST_HEAD(&h->hint_elem);
6416 h->cpuid = ha->base_qpair->cpuid;
6417 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6418
6419 for (i = 0; i < ha->max_qpairs; i++) {
6420 unsigned long flags;
6421
6422 struct qla_qpair *qpair = ha->queue_pair_map[i];
Bart Van Asschebd432bb2019-04-11 14:53:17 -07006423
Quinn Trane326d222017-06-13 20:47:18 -07006424 h = &tgt->qphints[i + 1];
6425 INIT_LIST_HEAD(&h->hint_elem);
6426 if (qpair) {
6427 h->qpair = qpair;
6428 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6429 list_add_tail(&h->hint_elem, &qpair->hints_list);
6430 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6431 h->cpuid = qpair->cpuid;
6432 }
6433 }
6434
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006435 tgt->ha = ha;
6436 tgt->vha = base_vha;
6437 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006438 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006439 spin_lock_init(&tgt->sess_work_lock);
6440 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6441 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006442 atomic_set(&tgt->tgt_global_resets_count, 0);
6443
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006444 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006445
6446 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6447 "qla_target(%d): using 64 Bit PCI addressing",
6448 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006449 /* 3 is reserved */
6450 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006451
6452 mutex_lock(&qla_tgt_mutex);
6453 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6454 mutex_unlock(&qla_tgt_mutex);
6455
Quinn Tranf1443ee2017-03-15 09:48:51 -07006456 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6457 ha->tgt.tgt_ops->add_target(base_vha);
6458
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006459 return 0;
6460}
6461
6462/* Must be called under tgt_host_action_mutex */
6463int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6464{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006465 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006466 return 0;
6467
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006468 if (vha->fc_vport) {
6469 qlt_release(vha->vha_tgt.qla_tgt);
6470 return 0;
6471 }
Quinn Tran33e79972014-09-25 06:14:55 -04006472
6473 /* free left over qfull cmds */
6474 qlt_init_term_exchange(vha);
6475
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006476 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6477 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006478 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006479
6480 return 0;
6481}
6482
Quinn Tran482c9dc2017-03-15 09:48:54 -07006483void qlt_remove_target_resources(struct qla_hw_data *ha)
6484{
6485 struct scsi_qla_host *node;
6486 u32 key = 0;
6487
6488 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6489 btree_remove32(&ha->tgt.host_map, key);
6490
6491 btree_destroy32(&ha->tgt.host_map);
6492}
6493
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006494static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6495 unsigned char *b)
6496{
Bart Van Assche09434912019-08-08 20:01:36 -07006497 pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6498 pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006499 put_unaligned_be64(wwpn, b);
Bart Van Assche09434912019-08-08 20:01:36 -07006500 pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006501}
6502
6503/**
6504 * qla_tgt_lport_register - register lport with external module
6505 *
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006506 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
Bart Van Assche807eb902018-10-18 15:45:41 -07006507 * @phys_wwpn: physical port WWPN
6508 * @npiv_wwpn: NPIV WWPN
6509 * @npiv_wwnn: NPIV WWNN
Bart Van Assche2db62282018-01-23 16:33:51 -08006510 * @callback: lport initialization callback for tcm_qla2xxx code
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006511 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006512int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6513 u64 npiv_wwpn, u64 npiv_wwnn,
6514 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006515{
6516 struct qla_tgt *tgt;
6517 struct scsi_qla_host *vha;
6518 struct qla_hw_data *ha;
6519 struct Scsi_Host *host;
6520 unsigned long flags;
6521 int rc;
6522 u8 b[WWN_SIZE];
6523
6524 mutex_lock(&qla_tgt_mutex);
6525 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6526 vha = tgt->vha;
6527 ha = vha->hw;
6528
6529 host = vha->host;
6530 if (!host)
6531 continue;
6532
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006533 if (!(host->hostt->supported_mode & MODE_TARGET))
6534 continue;
6535
Quinn Tran0645cb82018-09-11 10:18:18 -07006536 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6537 continue;
6538
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006539 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006540 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006541 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6542 host->host_no);
6543 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6544 continue;
6545 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006546 if (tgt->tgt_stop) {
6547 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6548 host->host_no);
6549 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6550 continue;
6551 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006552 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6553
6554 if (!scsi_host_get(host)) {
6555 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6556 "Unable to scsi_host_get() for"
6557 " qla2xxx scsi_host\n");
6558 continue;
6559 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006560 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006561
6562 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6563 scsi_host_put(host);
6564 continue;
6565 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006566 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6567 if (rc != 0)
6568 scsi_host_put(host);
6569
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006570 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006571 return rc;
6572 }
6573 mutex_unlock(&qla_tgt_mutex);
6574
6575 return -ENODEV;
6576}
6577EXPORT_SYMBOL(qlt_lport_register);
6578
6579/**
6580 * qla_tgt_lport_deregister - Degister lport
6581 *
6582 * @vha: Registered scsi_qla_host pointer
6583 */
6584void qlt_lport_deregister(struct scsi_qla_host *vha)
6585{
6586 struct qla_hw_data *ha = vha->hw;
6587 struct Scsi_Host *sh = vha->host;
6588 /*
6589 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6590 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006591 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006592 ha->tgt.tgt_ops = NULL;
6593 /*
6594 * Release the Scsi_Host reference for the underlying qla2xxx host
6595 */
6596 scsi_host_put(sh);
6597}
6598EXPORT_SYMBOL(qlt_lport_deregister);
6599
6600/* Must be called under HW lock */
Quinn Tran0645cb82018-09-11 10:18:18 -07006601void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006602{
Quinn Tran0645cb82018-09-11 10:18:18 -07006603 switch (vha->qlini_mode) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006604 case QLA2XXX_INI_MODE_DISABLED:
6605 case QLA2XXX_INI_MODE_EXCLUSIVE:
6606 vha->host->active_mode = MODE_TARGET;
6607 break;
6608 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tran0645cb82018-09-11 10:18:18 -07006609 vha->host->active_mode = MODE_INITIATOR;
Quinn Tranead03852017-01-19 22:28:01 -08006610 break;
6611 case QLA2XXX_INI_MODE_DUAL:
6612 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006613 break;
6614 default:
6615 break;
6616 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006617}
6618
6619/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006620static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006621{
Quinn Tran0645cb82018-09-11 10:18:18 -07006622 switch (vha->qlini_mode) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006623 case QLA2XXX_INI_MODE_DISABLED:
6624 vha->host->active_mode = MODE_UNKNOWN;
6625 break;
6626 case QLA2XXX_INI_MODE_EXCLUSIVE:
6627 vha->host->active_mode = MODE_INITIATOR;
6628 break;
6629 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006630 case QLA2XXX_INI_MODE_DUAL:
6631 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006632 break;
6633 default:
6634 break;
6635 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006636}
6637
6638/*
6639 * qla_tgt_enable_vha - NO LOCK HELD
6640 *
6641 * host_reset, bring up w/ Target Mode Enabled
6642 */
6643void
6644qlt_enable_vha(struct scsi_qla_host *vha)
6645{
6646 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006647 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006648 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006649 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006650
6651 if (!tgt) {
6652 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6653 "Unable to locate qla_tgt pointer from"
6654 " struct qla_hw_data\n");
6655 dump_stack();
6656 return;
6657 }
Quinn Tran0645cb82018-09-11 10:18:18 -07006658 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6659 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006660
Quinn Tran178235f2019-07-26 09:07:29 -07006661 if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6662 ha->tgt.num_act_qpairs = ha->max_qpairs;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006663 spin_lock_irqsave(&ha->hardware_lock, flags);
6664 tgt->tgt_stopped = 0;
6665 qlt_set_mode(vha);
6666 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6667
Quinn Tranb6faaaf2018-09-04 14:19:09 -07006668 mutex_lock(&ha->optrom_mutex);
6669 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6670 "%s.\n", __func__);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006671 if (vha->vp_idx) {
6672 qla24xx_disable_vp(vha);
6673 qla24xx_enable_vp(vha);
6674 } else {
6675 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6676 qla2xxx_wake_dpc(base_vha);
6677 qla2x00_wait_for_hba_online(base_vha);
6678 }
Quinn Tranb6faaaf2018-09-04 14:19:09 -07006679 mutex_unlock(&ha->optrom_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006680}
6681EXPORT_SYMBOL(qlt_enable_vha);
6682
6683/*
6684 * qla_tgt_disable_vha - NO LOCK HELD
6685 *
6686 * Disable Target Mode and reset the adapter
6687 */
Joern Engel55a90662014-09-16 16:23:15 -04006688static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006689{
6690 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006691 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006692 unsigned long flags;
6693
6694 if (!tgt) {
6695 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6696 "Unable to locate qla_tgt pointer from"
6697 " struct qla_hw_data\n");
6698 dump_stack();
6699 return;
6700 }
6701
6702 spin_lock_irqsave(&ha->hardware_lock, flags);
6703 qlt_clear_mode(vha);
6704 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6705
6706 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6707 qla2xxx_wake_dpc(vha);
6708 qla2x00_wait_for_hba_online(vha);
6709}
6710
6711/*
6712 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6713 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6714 * members.
6715 */
6716void
6717qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6718{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006719 vha->vha_tgt.qla_tgt = NULL;
6720
6721 mutex_init(&vha->vha_tgt.tgt_mutex);
6722 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006723
6724 qlt_clear_mode(vha);
6725
6726 /*
6727 * NOTE: Currently the value is kept the same for <24xx and
6728 * >=24xx ISPs. If it is necessary to change it,
6729 * the check should be added for specific ISPs,
6730 * assigning the value appropriately.
6731 */
6732 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006733
6734 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006735}
6736
Quinn Trane374f9f2017-12-28 12:33:31 -08006737u8
6738qlt_rff_id(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006739{
Quinn Trane374f9f2017-12-28 12:33:31 -08006740 u8 fc4_feature = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006741 /*
6742 * FC-4 Feature bit 0 indicates target functionality to the name server.
6743 */
6744 if (qla_tgt_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006745 fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006746 } else if (qla_ini_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006747 fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006748 } else if (qla_dual_mode_enabled(vha))
Quinn Trane374f9f2017-12-28 12:33:31 -08006749 fc4_feature = BIT_0 | BIT_1;
6750
6751 return fc4_feature;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006752}
6753
6754/*
6755 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6756 * @ha: HA context
6757 *
6758 * Beginning of ATIO ring has initialization control block already built
6759 * by nvram config routine.
6760 *
6761 * Returns 0 on success.
6762 */
6763void
6764qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6765{
6766 struct qla_hw_data *ha = vha->hw;
6767 uint16_t cnt;
6768 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6769
Quinn Tranead03852017-01-19 22:28:01 -08006770 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006771 return;
6772
6773 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6774 pkt->u.raw.signature = ATIO_PROCESSED;
6775 pkt++;
6776 }
6777
6778}
6779
6780/*
6781 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6782 * @ha: SCSI driver HA context
6783 */
6784void
Quinn Tran2f424b92015-12-17 14:57:07 -05006785qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006786{
6787 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006788 struct atio_from_isp *pkt;
6789 int cnt, i;
6790
Quinn Tranec7193e2017-03-15 09:48:55 -07006791 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006792 return;
6793
Quinn Tran5f355092016-12-23 18:06:11 -08006794 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6795 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006796 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6797 cnt = pkt->u.raw.entry_count;
6798
Quinn Tran5f355092016-12-23 18:06:11 -08006799 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6800 /*
6801 * This packet is corrupted. The header + payload
6802 * can not be trusted. There is no point in passing
6803 * it further up.
6804 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006805 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006806 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6807 pkt->u.isp24.fcp_hdr.s_id,
6808 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6809 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6810
6811 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006812 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6813 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006814 } else {
6815 qlt_24xx_atio_pkt_all_vps(vha,
6816 (struct atio_from_isp *)pkt, ha_locked);
6817 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006818
6819 for (i = 0; i < cnt; i++) {
6820 ha->tgt.atio_ring_index++;
6821 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6822 ha->tgt.atio_ring_index = 0;
6823 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6824 } else
6825 ha->tgt.atio_ring_ptr++;
6826
6827 pkt->u.raw.signature = ATIO_PROCESSED;
6828 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6829 }
6830 wmb();
6831 }
6832
6833 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006834 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006835}
6836
6837void
Arun Easiaa230bc2013-01-30 03:34:39 -05006838qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006839{
6840 struct qla_hw_data *ha = vha->hw;
Quinn Trand2b292c2017-12-28 12:33:17 -08006841 struct qla_msix_entry *msix = &ha->msix_entries[2];
6842 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6843
Arun Easiaa230bc2013-01-30 03:34:39 -05006844 if (!QLA_TGT_MODE_ENABLED())
6845 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006846
Arun Easiaa230bc2013-01-30 03:34:39 -05006847 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6848 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6849 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6850
Quinn Trand2b292c2017-12-28 12:33:17 -08006851 if (ha->flags.msix_enabled) {
Joe Carnuccioecc89f22019-03-12 11:08:13 -07006852 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
Quinn Trand2b292c2017-12-28 12:33:17 -08006853 if (IS_QLA2071(ha)) {
6854 /* 4 ports Baker: Enable Interrupt Handshake */
6855 icb->msix_atio = 0;
6856 icb->firmware_options_2 |= BIT_26;
6857 } else {
6858 icb->msix_atio = cpu_to_le16(msix->entry);
6859 icb->firmware_options_2 &= ~BIT_26;
6860 }
6861 ql_dbg(ql_dbg_init, vha, 0xf072,
6862 "Registering ICB vector 0x%x for atio que.\n",
6863 msix->entry);
6864 }
6865 } else {
6866 /* INTx|MSI */
Joe Carnuccioecc89f22019-03-12 11:08:13 -07006867 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
Quinn Trand2b292c2017-12-28 12:33:17 -08006868 icb->msix_atio = 0;
6869 icb->firmware_options_2 |= BIT_26;
6870 ql_dbg(ql_dbg_init, vha, 0xf072,
6871 "%s: Use INTx for ATIOQ.\n", __func__);
6872 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006873 }
6874}
6875
6876void
6877qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6878{
6879 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006880 u32 tmp;
Quinn Tran99e1b682017-06-02 09:12:03 -07006881
6882 if (!QLA_TGT_MODE_ENABLED())
6883 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006884
Quinn Tranead03852017-01-19 22:28:01 -08006885 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006886 if (!ha->tgt.saved_set) {
6887 /* We save only once */
6888 ha->tgt.saved_exchange_count = nv->exchange_count;
6889 ha->tgt.saved_firmware_options_1 =
6890 nv->firmware_options_1;
6891 ha->tgt.saved_firmware_options_2 =
6892 nv->firmware_options_2;
6893 ha->tgt.saved_firmware_options_3 =
6894 nv->firmware_options_3;
6895 ha->tgt.saved_set = 1;
6896 }
6897
Quinn Tran99e1b682017-06-02 09:12:03 -07006898 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006899 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006900 else /* dual */
Quinn Tran0645cb82018-09-11 10:18:18 -07006901 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006902
6903 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006904 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006905
6906 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006907 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006908 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006909
6910 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006911 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006912 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006913 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006914 if (ql2xtgt_tape_enable)
6915 /* Enable FC Tape support */
6916 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6917 else
6918 /* Disable FC Tape support */
6919 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6920
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006921 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006922 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006923
6924 /*
6925 * clear BIT 15 explicitly as we have seen at least
6926 * a couple of instances where this was set and this
6927 * was causing the firmware to not be initialized.
6928 */
6929 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006930 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006931 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08006932
6933 if (IS_QLA25XX(ha)) {
6934 /* Change Loop-prefer to Pt-Pt */
6935 tmp = ~(BIT_4|BIT_5|BIT_6);
6936 nv->firmware_options_2 &= cpu_to_le32(tmp);
6937 tmp = P2P << 4;
6938 nv->firmware_options_2 |= cpu_to_le32(tmp);
6939 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006940 } else {
6941 if (ha->tgt.saved_set) {
6942 nv->exchange_count = ha->tgt.saved_exchange_count;
6943 nv->firmware_options_1 =
6944 ha->tgt.saved_firmware_options_1;
6945 nv->firmware_options_2 =
6946 ha->tgt.saved_firmware_options_2;
6947 nv->firmware_options_3 =
6948 ha->tgt.saved_firmware_options_3;
6949 }
6950 return;
6951 }
6952
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006953 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006954 if (vha->flags.init_done)
6955 fc_host_supported_classes(vha->host) =
6956 FC_COS_CLASS2 | FC_COS_CLASS3;
6957
Bart Van Asschead950362015-07-09 07:24:08 -07006958 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006959 } else {
6960 if (vha->flags.init_done)
6961 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6962
Bart Van Asschead950362015-07-09 07:24:08 -07006963 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006964 }
6965}
6966
6967void
6968qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6969 struct init_cb_24xx *icb)
6970{
6971 struct qla_hw_data *ha = vha->hw;
6972
Quinn Tran481ce732015-12-17 14:57:08 -05006973 if (!QLA_TGT_MODE_ENABLED())
6974 return;
6975
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006976 if (ha->tgt.node_name_set) {
6977 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006978 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006979 }
6980}
6981
Arun Easiaa230bc2013-01-30 03:34:39 -05006982void
6983qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6984{
6985 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006986 u32 tmp;
Arun Easiaa230bc2013-01-30 03:34:39 -05006987
6988 if (!QLA_TGT_MODE_ENABLED())
6989 return;
6990
Quinn Tranead03852017-01-19 22:28:01 -08006991 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006992 if (!ha->tgt.saved_set) {
6993 /* We save only once */
6994 ha->tgt.saved_exchange_count = nv->exchange_count;
6995 ha->tgt.saved_firmware_options_1 =
6996 nv->firmware_options_1;
6997 ha->tgt.saved_firmware_options_2 =
6998 nv->firmware_options_2;
6999 ha->tgt.saved_firmware_options_3 =
7000 nv->firmware_options_3;
7001 ha->tgt.saved_set = 1;
7002 }
7003
Quinn Tran99e1b682017-06-02 09:12:03 -07007004 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08007005 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07007006 else /* dual */
Quinn Tran0645cb82018-09-11 10:18:18 -07007007 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05007008
7009 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07007010 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05007011
7012 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08007013 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07007014 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05007015 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007016 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05007017 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007018 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08007019 /*
7020 * clear BIT 15 explicitly as we have seen at
7021 * least a couple of instances where this was set
7022 * and this was causing the firmware to not be
7023 * initialized.
7024 */
7025 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04007026 if (ql2xtgt_tape_enable)
7027 /* Enable FC tape support */
7028 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7029 else
7030 /* Disable FC tape support */
7031 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7032
Arun Easiaa230bc2013-01-30 03:34:39 -05007033 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007034 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05007035 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07007036 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08007037
7038 /* Change Loop-prefer to Pt-Pt */
7039 tmp = ~(BIT_4|BIT_5|BIT_6);
7040 nv->firmware_options_2 &= cpu_to_le32(tmp);
7041 tmp = P2P << 4;
7042 nv->firmware_options_2 |= cpu_to_le32(tmp);
Arun Easiaa230bc2013-01-30 03:34:39 -05007043 } else {
7044 if (ha->tgt.saved_set) {
7045 nv->exchange_count = ha->tgt.saved_exchange_count;
7046 nv->firmware_options_1 =
7047 ha->tgt.saved_firmware_options_1;
7048 nv->firmware_options_2 =
7049 ha->tgt.saved_firmware_options_2;
7050 nv->firmware_options_3 =
7051 ha->tgt.saved_firmware_options_3;
7052 }
7053 return;
7054 }
7055
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007056 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007057 if (vha->flags.init_done)
7058 fc_host_supported_classes(vha->host) =
7059 FC_COS_CLASS2 | FC_COS_CLASS3;
7060
Bart Van Asschead950362015-07-09 07:24:08 -07007061 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05007062 } else {
7063 if (vha->flags.init_done)
7064 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7065
Bart Van Asschead950362015-07-09 07:24:08 -07007066 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05007067 }
7068}
7069
7070void
7071qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7072 struct init_cb_81xx *icb)
7073{
7074 struct qla_hw_data *ha = vha->hw;
7075
7076 if (!QLA_TGT_MODE_ENABLED())
7077 return;
7078
7079 if (ha->tgt.node_name_set) {
7080 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07007081 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05007082 }
7083}
7084
7085void
7086qlt_83xx_iospace_config(struct qla_hw_data *ha)
7087{
7088 if (!QLA_TGT_MODE_ENABLED())
7089 return;
7090
7091 ha->msix_count += 1; /* For ATIO Q */
7092}
7093
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007094
7095void
7096qlt_modify_vp_config(struct scsi_qla_host *vha,
7097 struct vp_config_entry_24xx *vpmod)
7098{
Quinn Tranead03852017-01-19 22:28:01 -08007099 /* enable target mode. Bit5 = 1 => disable */
7100 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007101 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08007102
Quinn Tranead03852017-01-19 22:28:01 -08007103 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08007104 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007105 vpmod->options_idx1 &= ~BIT_4;
7106}
7107
7108void
7109qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7110{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007111 int rc;
7112
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007113 if (!QLA_TGT_MODE_ENABLED())
7114 return;
7115
Joe Carnuccioecc89f22019-03-12 11:08:13 -07007116 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
7117 IS_QLA28XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007118 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7119 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7120 } else {
7121 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7122 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7123 }
7124
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08007125 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7126 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08007127
7128 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7129 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7130 qlt_unknown_atio_work_fn);
7131
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007132 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007133
7134 rc = btree_init32(&ha->tgt.host_map);
7135 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007136 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007137 "Unable to initialize ha->host_map btree\n");
7138
7139 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007140}
7141
Arun Easiaa230bc2013-01-30 03:34:39 -05007142irqreturn_t
7143qla83xx_msix_atio_q(int irq, void *dev_id)
7144{
7145 struct rsp_que *rsp;
7146 scsi_qla_host_t *vha;
7147 struct qla_hw_data *ha;
7148 unsigned long flags;
7149
7150 rsp = (struct rsp_que *) dev_id;
7151 ha = rsp->hw;
7152 vha = pci_get_drvdata(ha->pdev);
7153
Quinn Tran2f424b92015-12-17 14:57:07 -05007154 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007155
Quinn Tran2f424b92015-12-17 14:57:07 -05007156 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05007157
Quinn Tran2f424b92015-12-17 14:57:07 -05007158 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007159
7160 return IRQ_HANDLED;
7161}
7162
Quinn Tran2f424b92015-12-17 14:57:07 -05007163static void
7164qlt_handle_abts_recv_work(struct work_struct *work)
7165{
7166 struct qla_tgt_sess_op *op = container_of(work,
7167 struct qla_tgt_sess_op, work);
7168 scsi_qla_host_t *vha = op->vha;
7169 struct qla_hw_data *ha = vha->hw;
7170 unsigned long flags;
7171
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007172 if (qla2x00_reset_active(vha) ||
7173 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05007174 return;
7175
7176 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7177 qlt_24xx_process_atio_queue(vha, 0);
7178 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7179
7180 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07007181 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05007182 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07007183
7184 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05007185}
7186
7187void
Quinn Tran82de8022017-06-13 20:47:17 -07007188qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7189 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05007190{
7191 struct qla_tgt_sess_op *op;
7192
7193 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7194
7195 if (!op) {
7196 /* do not reach for ATIO queue here. This is best effort err
7197 * recovery at this point.
7198 */
Quinn Tran82de8022017-06-13 20:47:17 -07007199 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05007200 return;
7201 }
7202
7203 memcpy(&op->atio, pkt, sizeof(*pkt));
7204 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007205 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07007206 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05007207 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7208 queue_work(qla_tgt_wq, &op->work);
7209 return;
7210}
7211
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007212int
7213qlt_mem_alloc(struct qla_hw_data *ha)
7214{
7215 if (!QLA_TGT_MODE_ENABLED())
7216 return 0;
7217
Kees Cook6396bb22018-06-12 14:03:40 -07007218 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7219 sizeof(struct qla_tgt_vp_map),
7220 GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007221 if (!ha->tgt.tgt_vp_map)
7222 return -ENOMEM;
7223
7224 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7225 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7226 &ha->tgt.atio_dma, GFP_KERNEL);
7227 if (!ha->tgt.atio_ring) {
7228 kfree(ha->tgt.tgt_vp_map);
7229 return -ENOMEM;
7230 }
7231 return 0;
7232}
7233
7234void
7235qlt_mem_free(struct qla_hw_data *ha)
7236{
7237 if (!QLA_TGT_MODE_ENABLED())
7238 return;
7239
7240 if (ha->tgt.atio_ring) {
7241 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7242 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7243 ha->tgt.atio_dma);
7244 }
Bart Van Assche5365bf92019-04-17 14:44:22 -07007245 ha->tgt.atio_ring = NULL;
7246 ha->tgt.atio_dma = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007247 kfree(ha->tgt.tgt_vp_map);
Bart Van Assche5365bf92019-04-17 14:44:22 -07007248 ha->tgt.tgt_vp_map = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007249}
7250
7251/* vport_slock to be held by the caller */
7252void
7253qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7254{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007255 void *slot;
7256 u32 key;
7257 int rc;
7258
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007259 if (!QLA_TGT_MODE_ENABLED())
7260 return;
7261
Quinn Tran482c9dc2017-03-15 09:48:54 -07007262 key = vha->d_id.b24;
7263
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007264 switch (cmd) {
7265 case SET_VP_IDX:
7266 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7267 break;
7268 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07007269 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7270 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007271 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007272 "Save vha in host_map %p %06x\n", vha, key);
7273 rc = btree_insert32(&vha->hw->tgt.host_map,
7274 key, vha, GFP_ATOMIC);
7275 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007276 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007277 "Unable to insert s_id into host_map: %06x\n",
7278 key);
7279 return;
7280 }
Quinn Tran83548fe2017-06-02 09:12:01 -07007281 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7282 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007283 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007284 break;
7285 case RESET_VP_IDX:
7286 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7287 break;
7288 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07007289 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007290 "clear vha in host_map %p %06x\n", vha, key);
7291 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7292 if (slot)
7293 btree_remove32(&vha->hw->tgt.host_map, key);
7294 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007295 break;
7296 }
7297}
7298
Quinn Tran482c9dc2017-03-15 09:48:54 -07007299void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7300{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007301
7302 if (!vha->d_id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007303 vha->d_id = id;
7304 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007305 } else if (vha->d_id.b24 != id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007306 qlt_update_vp_map(vha, RESET_AL_PA);
7307 vha->d_id = id;
7308 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007309 }
7310}
7311
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007312static int __init qlt_parse_ini_mode(void)
7313{
7314 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7315 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7316 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7317 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7318 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7319 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007320 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7321 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007322 else
7323 return false;
7324
7325 return true;
7326}
7327
7328int __init qlt_init(void)
7329{
7330 int ret;
7331
Bart Van Asschebc044592019-04-17 14:44:37 -07007332 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7333 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7334
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007335 if (!qlt_parse_ini_mode()) {
7336 ql_log(ql_log_fatal, NULL, 0xe06b,
7337 "qlt_parse_ini_mode() failed\n");
7338 return -EINVAL;
7339 }
7340
7341 if (!QLA_TGT_MODE_ENABLED())
7342 return 0;
7343
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007344 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7345 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7346 qla_tgt_mgmt_cmd), 0, NULL);
7347 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007348 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007349 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007350 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007351 }
7352
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007353 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007354 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7355 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007356
7357 if (!qla_tgt_plogi_cachep) {
7358 ql_log(ql_log_fatal, NULL, 0xe06d,
7359 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7360 ret = -ENOMEM;
7361 goto out_mgmt_cmd_cachep;
7362 }
7363
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007364 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7365 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7366 if (!qla_tgt_mgmt_cmd_mempool) {
7367 ql_log(ql_log_fatal, NULL, 0xe06e,
7368 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7369 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007370 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007371 }
7372
7373 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7374 if (!qla_tgt_wq) {
7375 ql_log(ql_log_fatal, NULL, 0xe06f,
7376 "alloc_workqueue for qla_tgt_wq failed\n");
7377 ret = -ENOMEM;
7378 goto out_cmd_mempool;
7379 }
7380 /*
7381 * Return 1 to signal that initiator-mode is being disabled
7382 */
7383 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7384
7385out_cmd_mempool:
7386 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007387out_plogi_cachep:
7388 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007389out_mgmt_cmd_cachep:
7390 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007391 return ret;
7392}
7393
7394void qlt_exit(void)
7395{
7396 if (!QLA_TGT_MODE_ENABLED())
7397 return;
7398
7399 destroy_workqueue(qla_tgt_wq);
7400 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007401 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007402 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007403}