blob: 2fd5c09b42d46af984ee30688cb676afcb6c5c24 [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
792 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
793 if (pla->id.b24 == id->b24) {
Quinn Tran48acad02018-08-02 13:16:44 -0700794 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
795 "%s %d %8phC Term INOT due to new INOT",
796 __func__, __LINE__,
797 pla->iocb.u.isp24.port_name);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500798 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
Quinn Tran5d964832017-01-19 22:27:59 -0800799 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500800 return pla;
801 }
802 }
803
804 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
805 if (!pla) {
806 ql_dbg(ql_dbg_async, vha, 0x5088,
807 "qla_target(%d): Allocation of plogi_ack failed\n",
808 vha->vp_idx);
809 return NULL;
810 }
811
Quinn Tran5d964832017-01-19 22:27:59 -0800812 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500813 pla->id = *id;
814 list_add_tail(&pla->list, &vha->plogi_ack_list);
815
816 return pla;
817}
818
Quinn Tran726b8542017-01-19 22:28:00 -0800819void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -0800820 struct qlt_plogi_ack_t *pla)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500821{
Quinn Tran5d964832017-01-19 22:27:59 -0800822 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Quinn Tran726b8542017-01-19 22:28:00 -0800823 port_id_t port_id;
824 uint16_t loop_id;
825 fc_port_t *fcport = pla->fcport;
826
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500827 BUG_ON(!pla->ref_count);
828 pla->ref_count--;
829
830 if (pla->ref_count)
831 return;
832
Quinn Tran726b8542017-01-19 22:28:00 -0800833 ql_dbg(ql_dbg_disc, vha, 0x5089,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500834 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
Quinn Tran5d964832017-01-19 22:27:59 -0800835 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
836 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
837 iocb->u.isp24.port_id[0],
838 le16_to_cpu(iocb->u.isp24.nport_handle),
839 iocb->u.isp24.exchange_address, iocb->ox_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800840
841 port_id.b.domain = iocb->u.isp24.port_id[2];
842 port_id.b.area = iocb->u.isp24.port_id[1];
843 port_id.b.al_pa = iocb->u.isp24.port_id[0];
844 port_id.b.rsvd_1 = 0;
845
846 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
847
848 fcport->loop_id = loop_id;
849 fcport->d_id = port_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800850 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
851 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
852 else
853 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
Quinn Tran726b8542017-01-19 22:28:00 -0800854
855 list_for_each_entry(fcport, &vha->vp_fcports, list) {
856 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
857 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
858 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
859 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
860 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500861
862 list_del(&pla->list);
863 kmem_cache_free(qla_tgt_plogi_cachep, pla);
864}
865
Quinn Tran726b8542017-01-19 22:28:00 -0800866void
Quinn Tran5d964832017-01-19 22:27:59 -0800867qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
868 struct fc_port *sess, enum qlt_plogi_link_t link)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500869{
Quinn Tran5d964832017-01-19 22:27:59 -0800870 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500871 /* Inc ref_count first because link might already be pointing at pla */
872 pla->ref_count++;
873
Quinn Tran726b8542017-01-19 22:28:00 -0800874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
875 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
876 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
877 sess, link, sess->port_name,
878 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
879 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
880 pla->ref_count, pla, link);
881
Quinn Tran5ef696a2017-12-04 14:45:05 -0800882 if (link == QLT_PLOGI_LINK_CONFLICT) {
883 switch (sess->disc_state) {
884 case DSC_DELETED:
885 case DSC_DELETE_PEND:
886 pla->ref_count--;
887 return;
888 default:
889 break;
890 }
891 }
892
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500893 if (sess->plogi_link[link])
894 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
895
Quinn Tran726b8542017-01-19 22:28:00 -0800896 if (link == QLT_PLOGI_LINK_SAME_WWN)
897 pla->fcport = sess;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500898
899 sess->plogi_link[link] = pla;
900}
901
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500902typedef struct {
903 /* These fields must be initialized by the caller */
904 port_id_t id;
905 /*
906 * number of cmds dropped while we were waiting for
907 * initiator to ack LOGO initialize to 1 if LOGO is
908 * triggered by a command, otherwise, to 0
909 */
910 int cmd_count;
911
912 /* These fields are used by callee */
913 struct list_head list;
914} qlt_port_logo_t;
915
916static void
917qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
918{
919 qlt_port_logo_t *tmp;
920 int res;
921
922 mutex_lock(&vha->vha_tgt.tgt_mutex);
923
924 list_for_each_entry(tmp, &vha->logo_list, list) {
925 if (tmp->id.b24 == logo->id.b24) {
926 tmp->cmd_count += logo->cmd_count;
927 mutex_unlock(&vha->vha_tgt.tgt_mutex);
928 return;
929 }
930 }
931
932 list_add_tail(&logo->list, &vha->logo_list);
933
934 mutex_unlock(&vha->vha_tgt.tgt_mutex);
935
936 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
937
938 mutex_lock(&vha->vha_tgt.tgt_mutex);
939 list_del(&logo->list);
940 mutex_unlock(&vha->vha_tgt.tgt_mutex);
941
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500942 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
943 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
944 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
945 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500946}
947
Darren Trapp9dd96862018-03-20 23:09:32 -0700948void qlt_free_session_done(struct work_struct *work)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400949{
Quinn Tran5d964832017-01-19 22:27:59 -0800950 struct fc_port *sess = container_of(work, struct fc_port,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400951 free_work);
952 struct qla_tgt *tgt = sess->tgt;
953 struct scsi_qla_host *vha = sess->vha;
954 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400955 unsigned long flags;
956 bool logout_started = false;
Quinn Tran41dc5292017-01-19 22:28:03 -0800957 scsi_qla_host_t *base_vha;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800958 struct qlt_plogi_ack_t *own =
959 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400960
961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
962 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500963 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400964 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
Quinn Tran37cacc02017-01-19 22:27:58 -0800965 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400966 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500967 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400968
Quinn Tran726b8542017-01-19 22:28:00 -0800969 if (!IS_SW_RESV_ADDR(sess->d_id)) {
Quinn Tran21374902019-04-23 14:52:35 -0700970 qla2x00_mark_device_lost(vha, sess, 0, 0);
971
Quinn Tran41dc5292017-01-19 22:28:03 -0800972 if (sess->send_els_logo) {
973 qlt_port_logo_t logo;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500974
Quinn Tran41dc5292017-01-19 22:28:03 -0800975 logo.id = sess->d_id;
976 logo.cmd_count = 0;
Quinn Trancb97f2c2018-08-02 13:16:51 -0700977 if (!own)
978 qlt_send_first_logo(vha, &logo);
Himanshu Madhania2390342018-01-22 12:04:20 -0800979 sess->send_els_logo = 0;
Quinn Tran41dc5292017-01-19 22:28:03 -0800980 }
981
Quinn Tranba743f92017-12-04 14:45:12 -0800982 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800983 int rc;
984
Quinn Tran9cd883f2017-12-28 12:33:24 -0800985 if (!own ||
986 (own &&
987 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
988 rc = qla2x00_post_async_logout_work(vha, sess,
989 NULL);
990 if (rc != QLA_SUCCESS)
991 ql_log(ql_log_warn, vha, 0xf085,
992 "Schedule logo failed sess %p rc %d\n",
993 sess, rc);
994 else
995 logout_started = true;
996 } else if (own && (own->iocb.u.isp24.status_subcode ==
997 ELS_PRLI) && ha->flags.rida_fmt2) {
998 rc = qla2x00_post_async_prlo_work(vha, sess,
999 NULL);
1000 if (rc != QLA_SUCCESS)
1001 ql_log(ql_log_warn, vha, 0xf085,
1002 "Schedule PRLO failed sess %p rc %d\n",
1003 sess, rc);
1004 else
1005 logout_started = true;
1006 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001007 }
Quinn Tran726b8542017-01-19 22:28:00 -08001008 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001009
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001010 /*
1011 * Release the target session for FC Nexus from fabric module code.
1012 */
1013 if (sess->se_sess != NULL)
1014 ha->tgt.tgt_ops->free_session(sess);
1015
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001016 if (logout_started) {
1017 bool traced = false;
1018
Mark Rutland6aa7de02017-10-23 14:07:29 -07001019 while (!READ_ONCE(sess->logout_completed)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001020 if (!traced) {
1021 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1022 "%s: waiting for sess %p logout\n",
1023 __func__, sess);
1024 traced = true;
1025 }
1026 msleep(100);
1027 }
1028
Quinn Tran726b8542017-01-19 22:28:00 -08001029 ql_dbg(ql_dbg_disc, vha, 0xf087,
Quinn Tran9cd883f2017-12-28 12:33:24 -08001030 "%s: sess %p logout completed\n", __func__, sess);
Quinn Tran41dc5292017-01-19 22:28:03 -08001031 }
1032
1033 if (sess->logo_ack_needed) {
1034 sess->logo_ack_needed = 0;
1035 qla24xx_async_notify_ack(vha, sess,
1036 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001037 }
1038
Quinn Tran726b8542017-01-19 22:28:00 -08001039 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1040 if (sess->se_sess) {
1041 sess->se_sess = NULL;
1042 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1043 tgt->sess_count--;
1044 }
1045
1046 sess->disc_state = DSC_DELETED;
1047 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1048 sess->deleted = QLA_SESS_DELETED;
Quinn Tran726b8542017-01-19 22:28:00 -08001049
1050 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1051 vha->fcport_count--;
1052 sess->login_succ = 0;
1053 }
1054
Quinn Tranba743f92017-12-04 14:45:12 -08001055 qla2x00_clear_loop_id(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08001056
1057 if (sess->conflict) {
1058 sess->conflict->login_pause = 0;
1059 sess->conflict = NULL;
1060 if (!test_bit(UNLOADING, &vha->dpc_flags))
1061 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1062 }
1063
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001064 {
Quinn Tran5d964832017-01-19 22:27:59 -08001065 struct qlt_plogi_ack_t *con =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001066 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
Quinn Tran5d964832017-01-19 22:27:59 -08001067 struct imm_ntfy_from_isp *iocb;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07001068
Quinn Tran48acad02018-08-02 13:16:44 -07001069 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001070
1071 if (con) {
Quinn Tran5d964832017-01-19 22:27:59 -08001072 iocb = &con->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001073 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
Quinn Tran5d964832017-01-19 22:27:59 -08001074 "se_sess %p / sess %p port %8phC is gone,"
1075 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1076 sess->se_sess, sess, sess->port_name,
1077 own ? "releasing own PLOGI" : "no own PLOGI pending",
1078 own ? own->ref_count : -1,
1079 iocb->u.isp24.port_name, con->ref_count);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001080 qlt_plogi_ack_unref(vha, con);
Quinn Tran726b8542017-01-19 22:28:00 -08001081 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001082 } else {
1083 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1084 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1085 sess->se_sess, sess, sess->port_name,
1086 own ? "releasing own PLOGI" :
1087 "no own PLOGI pending",
1088 own ? own->ref_count : -1);
1089 }
1090
Quinn Tran726b8542017-01-19 22:28:00 -08001091 if (own) {
1092 sess->fw_login_state = DSC_LS_PLOGI_PEND;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001093 qlt_plogi_ack_unref(vha, own);
Quinn Tran726b8542017-01-19 22:28:00 -08001094 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1095 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001096 }
Quinn Tran1ae634e2017-12-28 12:33:44 -08001097
Quinn Tran5d964832017-01-19 22:27:59 -08001098 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1099
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001100 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
Quinn Tran726b8542017-01-19 22:28:00 -08001101 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1102 sess, sess->port_name, vha->fcport_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001103
Quinn Tran726b8542017-01-19 22:28:00 -08001104 if (tgt && (tgt->sess_count == 0))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001105 wake_up_all(&tgt->waitQ);
Quinn Tran726b8542017-01-19 22:28:00 -08001106
1107 if (vha->fcport_count == 0)
1108 wake_up_all(&vha->fcport_waitQ);
1109
Quinn Tran41dc5292017-01-19 22:28:03 -08001110 base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran1ae634e2017-12-28 12:33:44 -08001111
1112 sess->free_pending = 0;
1113
Quinn Tran41dc5292017-01-19 22:28:03 -08001114 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1115 return;
1116
Quinn Trana4239942017-12-28 12:33:26 -08001117 if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1118 switch (vha->host->active_mode) {
1119 case MODE_INITIATOR:
1120 case MODE_DUAL:
1121 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1122 qla2xxx_wake_dpc(vha);
1123 break;
1124 case MODE_TARGET:
1125 default:
1126 /* no-op */
1127 break;
1128 }
Quinn Tran726b8542017-01-19 22:28:00 -08001129 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001130}
1131
Quinn Tran75601512015-12-17 14:57:04 -05001132/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08001133void qlt_unreg_sess(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001134{
1135 struct scsi_qla_host *vha = sess->vha;
Quinn Tran1ae634e2017-12-28 12:33:44 -08001136 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001137
Quinn Tran83548fe2017-06-02 09:12:01 -07001138 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
Quinn Tran5d964832017-01-19 22:27:59 -08001139 "%s sess %p for deletion %8phC\n",
1140 __func__, sess, sess->port_name);
1141
Quinn Tran1ae634e2017-12-28 12:33:44 -08001142 spin_lock_irqsave(&sess->vha->work_lock, flags);
1143 if (sess->free_pending) {
1144 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1145 return;
1146 }
1147 sess->free_pending = 1;
1148 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1149
Quinn Tran36c78452016-02-04 11:45:18 -05001150 if (sess->se_sess)
1151 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001152
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001153 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Tran726b8542017-01-19 22:28:00 -08001154 sess->disc_state = DSC_DELETE_PEND;
1155 sess->last_rscn_gen = sess->rscn_gen;
1156 sess->last_login_gen = sess->login_gen;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001157
Darren Trapp9dd96862018-03-20 23:09:32 -07001158 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1159 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1160 sess->nvme_flag |= NVME_FLAG_DELETING;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001161 schedule_work(&sess->nvme_del_work);
Darren Trapp9dd96862018-03-20 23:09:32 -07001162 } else {
1163 INIT_WORK(&sess->free_work, qlt_free_session_done);
1164 schedule_work(&sess->free_work);
1165 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001166}
Quinn Tran5d964832017-01-19 22:27:59 -08001167EXPORT_SYMBOL(qlt_unreg_sess);
Quinn Tran75601512015-12-17 14:57:04 -05001168
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001169static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1170{
1171 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001172 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001173 uint16_t loop_id;
1174 int res = 0;
1175 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -05001176 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001177
1178 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1179 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001180 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -04001181 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -05001182 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -04001183 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001184 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001185 } else {
Quinn Tran75601512015-12-17 14:57:04 -05001186 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001187 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05001188 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001189 }
1190
1191 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1192 "Using sess for qla_tgt_reset: %p\n", sess);
1193 if (!sess) {
1194 res = -ESRCH;
1195 return res;
1196 }
1197
1198 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001199 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1200 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001201 mcmd, loop_id);
1202
Quinn Tranbb1181c2016-12-23 18:06:05 -08001203 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001204}
1205
Quinn Tran726b8542017-01-19 22:28:00 -08001206static void qla24xx_chk_fcp_state(struct fc_port *sess)
1207{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001208 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
Quinn Tran726b8542017-01-19 22:28:00 -08001209 sess->logout_on_delete = 0;
1210 sess->logo_ack_needed = 0;
1211 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1212 sess->scan_state = 0;
1213 }
1214}
1215
Quinn Tran94cff6e2017-12-28 12:33:42 -08001216void qlt_schedule_sess_for_deletion(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001217{
1218 struct qla_tgt *tgt = sess->tgt;
Quinn Trand8630bb2017-12-28 12:33:43 -08001219 unsigned long flags;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001220 u16 sec;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001221
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001222 switch (sess->disc_state) {
1223 case DSC_DELETE_PEND:
Quinn Tran726b8542017-01-19 22:28:00 -08001224 return;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001225 case DSC_DELETED:
Quinn Tran726b8542017-01-19 22:28:00 -08001226 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1227 wake_up_all(&tgt->waitQ);
1228 if (sess->vha->fcport_count == 0)
1229 wake_up_all(&sess->vha->fcport_waitQ);
1230
1231 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1232 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001233 return;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001234 break;
1235 case DSC_UPD_FCPORT:
1236 /*
1237 * This port is not done reporting to upper layer.
1238 * let it finish
1239 */
1240 sess->next_disc_state = DSC_DELETE_PEND;
1241 sec = jiffies_to_msecs(jiffies -
1242 sess->jiffies_at_registration)/1000;
1243 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1244 sess->sec_since_registration = sec;
1245 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1246 "%s %8phC : Slow Rport registration(%d Sec)\n",
1247 __func__, sess->port_name, sec);
1248 }
1249 return;
1250 default:
1251 break;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001252 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001253
Mikhail Malygin49d7bd32018-06-13 13:05:57 +00001254 spin_lock_irqsave(&sess->vha->work_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001255 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
Mikhail Malygin49d7bd32018-06-13 13:05:57 +00001256 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001257 return;
1258 }
Quinn Tran726b8542017-01-19 22:28:00 -08001259 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Mikhail Malygin49d7bd32018-06-13 13:05:57 +00001260 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001261
1262 sess->disc_state = DSC_DELETE_PEND;
1263
Quinn Tran726b8542017-01-19 22:28:00 -08001264 qla24xx_chk_fcp_state(sess);
1265
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001266 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
Quinn Tran5c640052018-09-26 22:05:14 -07001267 "Scheduling sess %p for deletion %8phC\n",
1268 sess, sess->port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001269
Quinn Trana01c77d2017-12-04 14:44:58 -08001270 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001271 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
Quinn Tran726b8542017-01-19 22:28:00 -08001272}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001273
Joern Engelc5701042014-09-16 16:23:14 -04001274static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001275{
Quinn Tran5d964832017-01-19 22:27:59 -08001276 struct fc_port *sess;
1277 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001278
Quinn Tran5d964832017-01-19 22:27:59 -08001279 list_for_each_entry(sess, &vha->vp_fcports, list) {
1280 if (sess->se_sess)
Quinn Tran94cff6e2017-12-28 12:33:42 -08001281 qlt_schedule_sess_for_deletion(sess);
Quinn Tran5d964832017-01-19 22:27:59 -08001282 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001283
1284 /* At this point tgt could be already dead */
1285}
1286
1287static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1288 uint16_t *loop_id)
1289{
1290 struct qla_hw_data *ha = vha->hw;
1291 dma_addr_t gid_list_dma;
1292 struct gid_list_info *gid_list;
1293 char *id_iter;
1294 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
1316 id_iter = (char *)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++) {
1319 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07001320
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001321 if ((gid->al_pa == s_id[2]) &&
1322 (gid->area == s_id[1]) &&
1323 (gid->domain == s_id[0])) {
1324 *loop_id = le16_to_cpu(gid->loop_id);
1325 res = 0;
1326 break;
1327 }
1328 id_iter += ha->gid_list_info_size;
1329 }
1330
1331out_free_id_list:
1332 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1333 gid_list, gid_list_dma);
1334 return res;
1335}
1336
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001337/*
1338 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1339 * Caller must put it.
1340 */
Quinn Tran5d964832017-01-19 22:27:59 -08001341static struct fc_port *qlt_create_sess(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001342 struct scsi_qla_host *vha,
1343 fc_port_t *fcport,
1344 bool local)
1345{
1346 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -08001347 struct fc_port *sess = fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001348 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001349
Quinn Tran726b8542017-01-19 22:28:00 -08001350 if (vha->vha_tgt.qla_tgt->tgt_stop)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001351 return NULL;
Quinn Tran726b8542017-01-19 22:28:00 -08001352
1353 if (fcport->se_sess) {
1354 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001355 ql_dbg(ql_dbg_disc, vha, 0x20f6,
Quinn Tran726b8542017-01-19 22:28:00 -08001356 "%s: kref_get_unless_zero failed for %8phC\n",
1357 __func__, sess->port_name);
1358 return NULL;
1359 }
1360 return fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001361 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001362 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001363 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001364
Quinn Tran726b8542017-01-19 22:28:00 -08001365 /*
1366 * Under normal circumstances we want to logout from firmware when
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001367 * session eventually ends and release corresponding nport handle.
1368 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
Quinn Tran726b8542017-01-19 22:28:00 -08001369 * code will adjust these flags as necessary.
1370 */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001371 sess->logout_on_delete = 1;
1372 sess->keep_nport_handle = 0;
Quinn Tran726b8542017-01-19 22:28:00 -08001373 sess->logout_completed = 0;
1374
1375 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1376 &fcport->port_name[0], sess) < 0) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001377 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
Quinn Tran726b8542017-01-19 22:28:00 -08001378 "(%d) %8phC check_initiator_node_acl failed\n",
1379 vha->vp_idx, fcport->port_name);
1380 return NULL;
1381 } else {
1382 kref_init(&fcport->sess_kref);
1383 /*
1384 * Take an extra reference to ->sess_kref here to handle
1385 * fc_port access across ->tgt.sess_lock reaquire.
1386 */
1387 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001388 ql_dbg(ql_dbg_disc, vha, 0x20f7,
Quinn Tran726b8542017-01-19 22:28:00 -08001389 "%s: kref_get_unless_zero failed for %8phC\n",
1390 __func__, sess->port_name);
1391 return NULL;
1392 }
1393
1394 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1395 if (!IS_SW_RESV_ADDR(sess->d_id))
1396 vha->vha_tgt.qla_tgt->sess_count++;
1397
1398 qlt_do_generation_tick(vha, &sess->generation);
1399 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1400 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001401
1402 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
Quinn Tran726b8542017-01-19 22:28:00 -08001403 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1404 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1405 vha->vha_tgt.qla_tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001406
1407 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001408 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1409 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1410 vha->vp_idx, local ? "local " : "", fcport->port_name,
Quinn Tran37cacc02017-01-19 22:27:58 -08001411 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1412 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001413
1414 return sess;
1415}
1416
1417/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001418 * max_gen - specifies maximum session generation
1419 * at which this deletion requestion is still valid
1420 */
1421void
1422qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001423{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001424 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08001425 struct fc_port *sess = fcport;
Quinn Tran75601512015-12-17 14:57:04 -05001426 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001427
1428 if (!vha->hw->tgt.tgt_ops)
1429 return;
1430
Roland Dreierb2032fd2015-07-14 16:00:42 -04001431 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001432 return;
1433
Quinn Tran75601512015-12-17 14:57:04 -05001434 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001435 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001436 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001437 return;
1438 }
Quinn Tran5d964832017-01-19 22:27:59 -08001439 if (!sess->se_sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001440 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001441 return;
1442 }
1443
Alexei Potashnikdf673272015-07-14 16:00:46 -04001444 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001445 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001446 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1447 "Ignoring stale deletion request for se_sess %p / sess %p"
1448 " for port %8phC, req_gen %d, sess_gen %d\n",
1449 sess->se_sess, sess, sess->port_name, max_gen,
1450 sess->generation);
1451 return;
1452 }
1453
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001454 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1455
1456 sess->local = 1;
Quinn Tran75601512015-12-17 14:57:04 -05001457 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001458 qlt_schedule_sess_for_deletion(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001459}
1460
1461static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1462{
1463 struct qla_hw_data *ha = tgt->ha;
1464 unsigned long flags;
1465 int res;
1466 /*
1467 * We need to protect against race, when tgt is freed before or
1468 * inside wake_up()
1469 */
Quinn Tran726b8542017-01-19 22:28:00 -08001470 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001471 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
Quinn Tran5d964832017-01-19 22:27:59 -08001472 "tgt %p, sess_count=%d\n",
1473 tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001474 res = (tgt->sess_count == 0);
Quinn Tran726b8542017-01-19 22:28:00 -08001475 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001476
1477 return res;
1478}
1479
1480/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001481int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001482{
1483 struct scsi_qla_host *vha = tgt->vha;
1484 struct qla_hw_data *ha = tgt->ha;
1485 unsigned long flags;
1486
Quinn Tranb6faaaf2018-09-04 14:19:09 -07001487 mutex_lock(&ha->optrom_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001488 mutex_lock(&qla_tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001489
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001490 if (tgt->tgt_stop || tgt->tgt_stopped) {
1491 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1492 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001493 mutex_unlock(&qla_tgt_mutex);
Quinn Tranb6faaaf2018-09-04 14:19:09 -07001494 mutex_unlock(&ha->optrom_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001495 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001496 }
1497
Quinn Tran3a33dc92017-06-02 09:12:04 -07001498 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001499 vha->host_no, vha);
1500 /*
1501 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1502 * Lock is needed, because we still can get an incoming packet.
1503 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001504 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001505 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001506 qlt_clear_tgt_db(tgt);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001507 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001508 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001509
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001510 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1511 "Waiting for sess works (tgt %p)", tgt);
1512 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1513 while (!list_empty(&tgt->sess_works_list)) {
1514 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1515 flush_scheduled_work();
1516 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1517 }
1518 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1519
1520 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001521 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001522
Joe Carnucciob85e0952017-08-23 15:05:11 -07001523 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001524
1525 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001526 if (!ha->flags.host_shutting_down &&
1527 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001528 qlt_disable_vha(vha);
1529
1530 /* Wait for sessions to clear out (just in case) */
Joe Carnucciob85e0952017-08-23 15:05:11 -07001531 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Quinn Tranb6faaaf2018-09-04 14:19:09 -07001532 mutex_unlock(&ha->optrom_mutex);
1533
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001534 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001535}
1536EXPORT_SYMBOL(qlt_stop_phase1);
1537
1538/* Called by tcm_qla2xxx configfs code */
1539void qlt_stop_phase2(struct qla_tgt *tgt)
1540{
Quinn Tran3a33dc92017-06-02 09:12:04 -07001541 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001542
1543 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001544 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001545 "Already in tgt->tgt_stopped state\n");
1546 dump_stack();
1547 return;
1548 }
Quinn Tran3a33dc92017-06-02 09:12:04 -07001549 if (!tgt->tgt_stop) {
1550 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1551 "%s: phase1 stop is not completed\n", __func__);
1552 dump_stack();
1553 return;
1554 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001555
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001556 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001557 tgt->tgt_stop = 0;
1558 tgt->tgt_stopped = 1;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001559 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001560
Quinn Tran3a33dc92017-06-02 09:12:04 -07001561 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001562 tgt);
Quinn Tran0645cb82018-09-11 10:18:18 -07001563
1564 switch (vha->qlini_mode) {
1565 case QLA2XXX_INI_MODE_EXCLUSIVE:
1566 vha->flags.online = 1;
1567 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1568 break;
1569 default:
1570 break;
1571 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001572}
1573EXPORT_SYMBOL(qlt_stop_phase2);
1574
1575/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001576static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001577{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001578 scsi_qla_host_t *vha = tgt->vha;
Quinn Trane326d222017-06-13 20:47:18 -07001579 void *node;
1580 u64 key = 0;
1581 u16 i;
1582 struct qla_qpair_hint *h;
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001583 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001584
Quinn Tran3a33dc92017-06-02 09:12:04 -07001585 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1586 !tgt->tgt_stopped)
1587 qlt_stop_phase1(tgt);
1588
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001589 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001590 qlt_stop_phase2(tgt);
1591
Quinn Trane326d222017-06-13 20:47:18 -07001592 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1593 unsigned long flags;
1594
1595 h = &tgt->qphints[i];
1596 if (h->qpair) {
1597 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1598 list_del(&h->hint_elem);
1599 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1600 h->qpair = NULL;
1601 }
1602 }
1603 kfree(tgt->qphints);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001604 mutex_lock(&qla_tgt_mutex);
1605 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1606 mutex_unlock(&qla_tgt_mutex);
Quinn Trane326d222017-06-13 20:47:18 -07001607
1608 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1609 btree_remove64(&tgt->lun_qpair_map, key);
1610
1611 btree_destroy64(&tgt->lun_qpair_map);
1612
Sawan Chandak3be63b1e2017-12-04 14:45:04 -08001613 if (vha->vp_idx)
1614 if (ha->tgt.tgt_ops &&
1615 ha->tgt.tgt_ops->remove_target &&
1616 vha->vha_tgt.target_lport_ptr)
1617 ha->tgt.tgt_ops->remove_target(vha);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001618
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001619 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001620
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001621 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001622 "Release of tgt %p finished\n", tgt);
1623
1624 kfree(tgt);
1625}
1626
1627/* ha->hardware_lock supposed to be held on entry */
1628static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1629 const void *param, unsigned int param_size)
1630{
1631 struct qla_tgt_sess_work_param *prm;
1632 unsigned long flags;
1633
1634 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1635 if (!prm) {
1636 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1637 "qla_target(%d): Unable to create session "
1638 "work, command will be refused", 0);
1639 return -ENOMEM;
1640 }
1641
1642 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1643 "Scheduling work (type %d, prm %p)"
1644 " to find session for param %p (size %d, tgt %p)\n",
1645 type, prm, param, param_size, tgt);
1646
1647 prm->type = type;
1648 memcpy(&prm->tm_iocb, param, param_size);
1649
1650 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1651 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1652 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1653
1654 schedule_work(&tgt->sess_work);
1655
1656 return 0;
1657}
1658
1659/*
1660 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1661 */
Quinn Tran82de8022017-06-13 20:47:17 -07001662static void qlt_send_notify_ack(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001663 struct imm_ntfy_from_isp *ntfy,
1664 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1665 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1666{
Quinn Tran82de8022017-06-13 20:47:17 -07001667 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001668 struct qla_hw_data *ha = vha->hw;
1669 request_t *pkt;
1670 struct nack_to_isp *nack;
1671
Quinn Tranec7193e2017-03-15 09:48:55 -07001672 if (!ha->flags.fw_started)
1673 return;
1674
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001675 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1676
Quinn Tran82de8022017-06-13 20:47:17 -07001677 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001678 if (!pkt) {
1679 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1680 "qla_target(%d): %s failed: unable to allocate "
1681 "request packet\n", vha->vp_idx, __func__);
1682 return;
1683 }
1684
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001685 if (vha->vha_tgt.qla_tgt != NULL)
1686 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001687
1688 pkt->entry_type = NOTIFY_ACK_TYPE;
1689 pkt->entry_count = 1;
1690
1691 nack = (struct nack_to_isp *)pkt;
1692 nack->ox_id = ntfy->ox_id;
1693
Quinn Tran726b8542017-01-19 22:28:00 -08001694 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001695 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1696 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1697 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001698 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001699 }
1700 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1701 nack->u.isp24.status = ntfy->u.isp24.status;
1702 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001703 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001704 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1705 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1706 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1707 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1708 nack->u.isp24.srr_reject_code = srr_reject_code;
1709 nack->u.isp24.srr_reject_code_expl = srr_explan;
1710 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1711
1712 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1713 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1714 vha->vp_idx, nack->u.isp24.status);
1715
Himanshu Madhani63163e02014-09-25 06:14:59 -04001716 /* Memory Barrier */
1717 wmb();
Quinn Tran82de8022017-06-13 20:47:17 -07001718 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001719}
1720
Quinn Tran6b0431d2018-09-04 14:19:13 -07001721static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1722{
1723 struct scsi_qla_host *vha = mcmd->vha;
1724 struct qla_hw_data *ha = vha->hw;
1725 struct abts_resp_to_24xx *resp;
1726 uint32_t f_ctl, h;
1727 uint8_t *p;
1728 int rc;
1729 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1730 struct qla_qpair *qpair = mcmd->qpair;
1731
1732 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1733 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1734 ha, mcmd->fc_tm_rsp);
1735
1736 rc = qlt_check_reserve_free_req(qpair, 1);
1737 if (rc) {
1738 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1739 "qla_target(%d): %s failed: unable to allocate request packet\n",
1740 vha->vp_idx, __func__);
1741 return -EAGAIN;
1742 }
1743
1744 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1745 memset(resp, 0, sizeof(*resp));
1746
1747 h = qlt_make_handle(qpair);
1748 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1749 /*
1750 * CTIO type 7 from the firmware doesn't provide a way to
1751 * know the initiator's LOOP ID, hence we can't find
1752 * the session and, so, the command.
1753 */
1754 return -EAGAIN;
1755 } else {
1756 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1757 }
1758
1759 resp->handle = MAKE_HANDLE(qpair->req->id, h);
1760 resp->entry_type = ABTS_RESP_24XX;
1761 resp->entry_count = 1;
1762 resp->nport_handle = abts->nport_handle;
1763 resp->vp_index = vha->vp_idx;
1764 resp->sof_type = abts->sof_type;
1765 resp->exchange_address = abts->exchange_address;
1766 resp->fcp_hdr_le = abts->fcp_hdr_le;
1767 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1768 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1769 F_CTL_SEQ_INITIATIVE);
1770 p = (uint8_t *)&f_ctl;
1771 resp->fcp_hdr_le.f_ctl[0] = *p++;
1772 resp->fcp_hdr_le.f_ctl[1] = *p++;
1773 resp->fcp_hdr_le.f_ctl[2] = *p;
1774
1775 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1776 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1777 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1778 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1779 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1780 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1781
1782 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1783 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1784 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1785 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1786 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1787 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1788 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1789 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1790 } else {
1791 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1792 resp->payload.ba_rjt.reason_code =
1793 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1794 /* Other bytes are zero */
1795 }
1796
1797 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1798
1799 /* Memory Barrier */
1800 wmb();
1801 if (qpair->reqq_start_iocbs)
1802 qpair->reqq_start_iocbs(qpair);
1803 else
1804 qla2x00_start_iocbs(vha, qpair->req);
1805
1806 return rc;
1807}
1808
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001809/*
1810 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1811 */
Quinn Tran82de8022017-06-13 20:47:17 -07001812static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001813 struct abts_recv_from_24xx *abts, uint32_t status,
1814 bool ids_reversed)
1815{
Quinn Tran82de8022017-06-13 20:47:17 -07001816 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001817 struct qla_hw_data *ha = vha->hw;
1818 struct abts_resp_to_24xx *resp;
1819 uint32_t f_ctl;
1820 uint8_t *p;
1821
1822 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1823 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1824 ha, abts, status);
1825
Quinn Tran82de8022017-06-13 20:47:17 -07001826 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1827 NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001828 if (!resp) {
1829 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1830 "qla_target(%d): %s failed: unable to allocate "
1831 "request packet", vha->vp_idx, __func__);
1832 return;
1833 }
1834
1835 resp->entry_type = ABTS_RESP_24XX;
Quinn Tran6b0431d2018-09-04 14:19:13 -07001836 resp->handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001837 resp->entry_count = 1;
1838 resp->nport_handle = abts->nport_handle;
1839 resp->vp_index = vha->vp_idx;
1840 resp->sof_type = abts->sof_type;
1841 resp->exchange_address = abts->exchange_address;
1842 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001843 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001844 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1845 F_CTL_SEQ_INITIATIVE);
1846 p = (uint8_t *)&f_ctl;
1847 resp->fcp_hdr_le.f_ctl[0] = *p++;
1848 resp->fcp_hdr_le.f_ctl[1] = *p++;
1849 resp->fcp_hdr_le.f_ctl[2] = *p;
1850 if (ids_reversed) {
1851 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1852 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1853 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1854 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1855 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1856 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1857 } else {
1858 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1859 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1860 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1861 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1862 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1863 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1864 }
1865 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1866 if (status == FCP_TMF_CMPL) {
1867 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1868 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1869 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1870 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1871 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1872 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1873 } else {
1874 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1875 resp->payload.ba_rjt.reason_code =
1876 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1877 /* Other bytes are zero */
1878 }
1879
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001880 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001881
Himanshu Madhani63163e02014-09-25 06:14:59 -04001882 /* Memory Barrier */
1883 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07001884 if (qpair->reqq_start_iocbs)
1885 qpair->reqq_start_iocbs(qpair);
1886 else
1887 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001888}
1889
1890/*
1891 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1892 */
1893static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
Quinn Tran6b0431d2018-09-04 14:19:13 -07001894 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001895{
1896 struct ctio7_to_24xx *ctio;
Quinn Tran06910942018-09-04 14:19:12 -07001897 u16 tmp;
Quinn Tran6b0431d2018-09-04 14:19:13 -07001898 struct abts_recv_from_24xx *entry;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001899
Quinn Tran06910942018-09-04 14:19:12 -07001900 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001901 if (ctio == NULL) {
1902 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1903 "qla_target(%d): %s failed: unable to allocate "
1904 "request packet\n", vha->vp_idx, __func__);
1905 return;
1906 }
1907
Quinn Tran6b0431d2018-09-04 14:19:13 -07001908 if (mcmd)
1909 /* abts from remote port */
1910 entry = &mcmd->orig_iocb.abts;
1911 else
1912 /* abts from this driver. */
1913 entry = (struct abts_recv_from_24xx *)pkt;
1914
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001915 /*
1916 * We've got on entrance firmware's response on by us generated
1917 * ABTS response. So, in it ID fields are reversed.
1918 */
1919
1920 ctio->entry_type = CTIO_TYPE7;
1921 ctio->entry_count = 1;
1922 ctio->nport_handle = entry->nport_handle;
1923 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001924 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001925 ctio->vp_index = vha->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001926 ctio->exchange_addr = entry->exchange_addr_to_abort;
Quinn Tran06910942018-09-04 14:19:12 -07001927 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
Quinn Tran06910942018-09-04 14:19:12 -07001928
Quinn Tran6b0431d2018-09-04 14:19:13 -07001929 if (mcmd) {
1930 ctio->initiator_id[0] = entry->fcp_hdr_le.s_id[0];
1931 ctio->initiator_id[1] = entry->fcp_hdr_le.s_id[1];
1932 ctio->initiator_id[2] = entry->fcp_hdr_le.s_id[2];
1933
1934 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1935 tmp |= (mcmd->abort_io_attr << 9);
1936 else if (qpair->retry_term_cnt & 1)
1937 tmp |= (0x4 << 9);
1938 } else {
1939 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1940 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1941 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1942
1943 if (qpair->retry_term_cnt & 1)
1944 tmp |= (0x4 << 9);
1945 }
1946 ctio->u.status1.flags = cpu_to_le16(tmp);
1947 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1948
1949 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1950 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1951 le16_to_cpu(ctio->u.status1.flags),
1952 le16_to_cpu(ctio->u.status1.ox_id),
1953 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001954
Himanshu Madhani63163e02014-09-25 06:14:59 -04001955 /* Memory Barrier */
1956 wmb();
Quinn Tran06910942018-09-04 14:19:12 -07001957 if (qpair->reqq_start_iocbs)
1958 qpair->reqq_start_iocbs(qpair);
1959 else
1960 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001961
Quinn Tran6b0431d2018-09-04 14:19:13 -07001962 if (mcmd)
1963 qlt_build_abts_resp_iocb(mcmd);
1964 else
1965 qlt_24xx_send_abts_resp(qpair,
1966 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001967
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001968}
1969
1970/* drop cmds for the given lun
1971 * XXX only looks for cmds on the port through which lun reset was recieved
1972 * XXX does not go through the list of other port (which may have cmds
1973 * for the same lun)
1974 */
1975static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001976 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001977{
1978 struct qla_tgt_sess_op *op;
1979 struct qla_tgt_cmd *cmd;
1980 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001981 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001982
1983 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001984 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001985 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1986 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001987 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001988
1989 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1990 op_lun = scsilun_to_int(
1991 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1992 if (op_key == key && op_lun == lun)
1993 op->aborted = true;
1994 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001995
1996 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1997 uint32_t op_key;
1998 u64 op_lun;
1999
2000 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2001 op_lun = scsilun_to_int(
2002 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2003 if (op_key == key && op_lun == lun)
2004 op->aborted = true;
2005 }
2006
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002007 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2008 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07002009 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002010
2011 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2012 cmd_lun = scsilun_to_int(
2013 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2014 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05002015 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002016 }
Quinn Tran8b631d82017-06-02 09:11:54 -07002017 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002018}
2019
Quinn Tran84905df2018-05-01 09:01:53 -07002020static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2021 uint64_t unpacked_lun)
2022{
2023 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2024 struct qla_qpair_hint *h = NULL;
2025
2026 if (vha->flags.qpairs_available) {
2027 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2028 if (!h)
2029 h = &tgt->qphints[0];
2030 } else {
2031 h = &tgt->qphints[0];
2032 }
2033
2034 return h;
2035}
2036
2037static void qlt_do_tmr_work(struct work_struct *work)
2038{
2039 struct qla_tgt_mgmt_cmd *mcmd =
2040 container_of(work, struct qla_tgt_mgmt_cmd, work);
2041 struct qla_hw_data *ha = mcmd->vha->hw;
2042 int rc = EIO;
2043 uint32_t tag;
2044 unsigned long flags;
2045
2046 switch (mcmd->tmr_func) {
2047 case QLA_TGT_ABTS:
2048 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
2049 break;
2050 default:
2051 tag = 0;
2052 break;
2053 }
2054
2055 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2056 mcmd->tmr_func, tag);
2057
2058 if (rc != 0) {
2059 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2060 switch (mcmd->tmr_func) {
2061 case QLA_TGT_ABTS:
Quinn Tran6b0431d2018-09-04 14:19:13 -07002062 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2063 qlt_build_abts_resp_iocb(mcmd);
Quinn Tran84905df2018-05-01 09:01:53 -07002064 break;
2065 case QLA_TGT_LUN_RESET:
2066 case QLA_TGT_CLEAR_TS:
2067 case QLA_TGT_ABORT_TS:
2068 case QLA_TGT_CLEAR_ACA:
2069 case QLA_TGT_TARGET_RESET:
2070 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2071 qla_sam_status);
2072 break;
2073
2074 case QLA_TGT_ABORT_ALL:
2075 case QLA_TGT_NEXUS_LOSS_SESS:
2076 case QLA_TGT_NEXUS_LOSS:
2077 qlt_send_notify_ack(mcmd->qpair,
2078 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2079 break;
2080 }
2081 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2082
2083 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2084 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2085 mcmd->vha->vp_idx, rc);
2086 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2087 }
2088}
2089
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002090/* ha->hardware_lock supposed to be held on entry */
2091static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08002092 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002093{
2094 struct qla_hw_data *ha = vha->hw;
2095 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tran84905df2018-05-01 09:01:53 -07002096 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
Steve Hodgson06e97b42012-11-16 08:06:17 -08002097
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002098 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2099 "qla_target(%d): task abort (tag=%d)\n",
2100 vha->vp_idx, abts->exchange_addr_to_abort);
2101
2102 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2103 if (mcmd == NULL) {
2104 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2105 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2106 vha->vp_idx, __func__);
2107 return -ENOMEM;
2108 }
2109 memset(mcmd, 0, sizeof(*mcmd));
Quinn Tran6b0431d2018-09-04 14:19:13 -07002110 mcmd->cmd_type = TYPE_TGT_TMCMD;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002111 mcmd->sess = sess;
2112 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002113 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08002114 mcmd->tmr_func = QLA_TGT_ABTS;
Quinn Tran84905df2018-05-01 09:01:53 -07002115 mcmd->qpair = h->qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002116 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002117
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07002118 /*
2119 * LUN is looked up by target-core internally based on the passed
2120 * abts->exchange_addr_to_abort tag.
2121 */
Quinn Tran84905df2018-05-01 09:01:53 -07002122 mcmd->se_cmd.cpuid = h->cpuid;
2123
2124 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2125 struct qla_tgt_cmd *abort_cmd;
2126
2127 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2128 abts->exchange_addr_to_abort);
2129 if (abort_cmd && abort_cmd->qpair) {
2130 mcmd->qpair = abort_cmd->qpair;
2131 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
Quinn Tran6b0431d2018-09-04 14:19:13 -07002132 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2133 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
Quinn Tran84905df2018-05-01 09:01:53 -07002134 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002135 }
2136
Quinn Tran84905df2018-05-01 09:01:53 -07002137 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2138 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2139
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002140 return 0;
2141}
2142
2143/*
2144 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2145 */
2146static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2147 struct abts_recv_from_24xx *abts)
2148{
2149 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08002150 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002151 uint32_t tag = abts->exchange_addr_to_abort;
2152 uint8_t s_id[3];
2153 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05002154 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002155
2156 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2157 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2158 "qla_target(%d): ABTS: Abort Sequence not "
2159 "supported\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002160 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2161 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002162 return;
2163 }
2164
2165 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2166 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2167 "qla_target(%d): ABTS: Unknown Exchange "
2168 "Address received\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002169 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2170 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002171 return;
2172 }
2173
2174 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2175 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2176 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2177 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2178 le32_to_cpu(abts->fcp_hdr_le.parameter));
2179
2180 s_id[0] = abts->fcp_hdr_le.s_id[2];
2181 s_id[1] = abts->fcp_hdr_le.s_id[1];
2182 s_id[2] = abts->fcp_hdr_le.s_id[0];
2183
Quinn Tran75601512015-12-17 14:57:04 -05002184 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002185 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2186 if (!sess) {
2187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
Colin Ian King03fea732018-03-19 10:53:41 +00002188 "qla_target(%d): task abort for non-existent session\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002189 vha->vp_idx);
Quinn Tran75601512015-12-17 14:57:04 -05002190 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2191
Quinn Tranfb352652017-12-28 12:33:32 -08002192 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2193 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002194 return;
2195 }
Quinn Tran75601512015-12-17 14:57:04 -05002196 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2197
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002198
Quinn Tran726b8542017-01-19 22:28:00 -08002199 if (sess->deleted) {
Quinn Tran82de8022017-06-13 20:47:17 -07002200 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2201 false);
Alexei Potashnike52a8b42015-07-14 16:00:48 -04002202 return;
2203 }
2204
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002205 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2206 if (rc != 0) {
2207 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2208 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2209 vha->vp_idx, rc);
Quinn Tran82de8022017-06-13 20:47:17 -07002210 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2211 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002212 return;
2213 }
2214}
2215
2216/*
2217 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2218 */
Quinn Tran82de8022017-06-13 20:47:17 -07002219static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002220 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2221{
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002222 struct scsi_qla_host *ha = mcmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002223 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2224 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002225 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002226
2227 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2228 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2229 ha, atio, resp_code);
2230
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002231
Quinn Tran82de8022017-06-13 20:47:17 -07002232 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002233 if (ctio == NULL) {
2234 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2235 "qla_target(%d): %s failed: unable to allocate "
2236 "request packet\n", ha->vp_idx, __func__);
2237 return;
2238 }
2239
2240 ctio->entry_type = CTIO_TYPE7;
2241 ctio->entry_count = 1;
2242 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2243 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002244 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002245 ctio->vp_index = ha->vp_idx;
2246 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2247 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2248 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2249 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002250 temp = (atio->u.isp24.attr << 9)|
2251 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2252 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002253 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2254 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002255 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002256 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2257 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002258 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002259
Himanshu Madhani63163e02014-09-25 06:14:59 -04002260 /* Memory Barrier */
2261 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002262 if (qpair->reqq_start_iocbs)
2263 qpair->reqq_start_iocbs(qpair);
2264 else
2265 qla2x00_start_iocbs(ha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002266}
2267
2268void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2269{
2270 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2271}
2272EXPORT_SYMBOL(qlt_free_mcmd);
2273
Quinn Tranbe251522017-03-15 09:48:49 -07002274/*
2275 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2276 * reacquire
2277 */
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002278void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07002279 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2280{
2281 struct atio_from_isp *atio = &cmd->atio;
2282 struct ctio7_to_24xx *ctio;
2283 uint16_t temp;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002284 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranbe251522017-03-15 09:48:49 -07002285
2286 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2287 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2288 "sense_key=%02x, asc=%02x, ascq=%02x",
2289 vha, atio, scsi_status, sense_key, asc, ascq);
2290
2291 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2292 if (!ctio) {
2293 ql_dbg(ql_dbg_async, vha, 0x3067,
2294 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2295 vha->host_no, __func__);
2296 goto out;
2297 }
2298
2299 ctio->entry_type = CTIO_TYPE7;
2300 ctio->entry_count = 1;
2301 ctio->handle = QLA_TGT_SKIP_HANDLE;
2302 ctio->nport_handle = cmd->sess->loop_id;
2303 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2304 ctio->vp_index = vha->vp_idx;
2305 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2306 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2307 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2308 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002309 temp = (atio->u.isp24.attr << 9) |
2310 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2311 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002312 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2313 ctio->u.status1.ox_id = cpu_to_le16(temp);
2314 ctio->u.status1.scsi_status =
2315 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2316 ctio->u.status1.response_len = cpu_to_le16(18);
2317 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2318
2319 if (ctio->u.status1.residual != 0)
2320 ctio->u.status1.scsi_status |=
2321 cpu_to_le16(SS_RESIDUAL_UNDER);
2322
Bart Van Asschea861b492019-04-17 14:44:42 -07002323 /* Fixed format sense data. */
2324 ctio->u.status1.sense_data[0] = 0x70;
2325 ctio->u.status1.sense_data[2] = sense_key;
Quinn Tranbe251522017-03-15 09:48:49 -07002326 /* Additional sense length */
Bart Van Asschea861b492019-04-17 14:44:42 -07002327 ctio->u.status1.sense_data[7] = 0xa;
Quinn Tranbe251522017-03-15 09:48:49 -07002328 /* ASC and ASCQ */
Bart Van Asschea861b492019-04-17 14:44:42 -07002329 ctio->u.status1.sense_data[12] = asc;
2330 ctio->u.status1.sense_data[13] = ascq;
Quinn Tranbe251522017-03-15 09:48:49 -07002331
2332 /* Memory Barrier */
2333 wmb();
2334
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002335 if (qpair->reqq_start_iocbs)
2336 qpair->reqq_start_iocbs(qpair);
2337 else
2338 qla2x00_start_iocbs(vha, qpair->req);
2339
Quinn Tranbe251522017-03-15 09:48:49 -07002340out:
2341 return;
2342}
2343
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002344/* callback from target fabric module code */
2345void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2346{
2347 struct scsi_qla_host *vha = mcmd->sess->vha;
2348 struct qla_hw_data *ha = vha->hw;
2349 unsigned long flags;
Quinn Tran82de8022017-06-13 20:47:17 -07002350 struct qla_qpair *qpair = mcmd->qpair;
Quinn Tran6b0431d2018-09-04 14:19:13 -07002351 bool free_mcmd = true;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002352
2353 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2354 "TM response mcmd (%p) status %#x state %#x",
2355 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2356
Quinn Tran82de8022017-06-13 20:47:17 -07002357 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002358
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002359 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002360 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002361 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002362 * previous life, just abort the processing.
2363 */
2364 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002365 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2366 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002367 mcmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002368 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002369 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002370 return;
2371 }
2372
Quinn Tran726b8542017-01-19 22:28:00 -08002373 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
Bart Van Assche8837aa82018-11-27 15:04:54 -08002374 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2375 case ELS_LOGO:
2376 case ELS_PRLO:
2377 case ELS_TPRLO:
Quinn Tran83548fe2017-06-02 09:12:01 -07002378 ql_dbg(ql_dbg_disc, vha, 0x2106,
Bart Van Assche19ce1922019-04-17 14:44:11 -07002379 "TM response logo %8phC status %#x state %#x",
Quinn Tran726b8542017-01-19 22:28:00 -08002380 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2381 mcmd->flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08002382 qlt_schedule_sess_for_deletion(mcmd->sess);
Bart Van Assche8837aa82018-11-27 15:04:54 -08002383 break;
2384 default:
Quinn Tran82de8022017-06-13 20:47:17 -07002385 qlt_send_notify_ack(vha->hw->base_qpair,
2386 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
Bart Van Assche8837aa82018-11-27 15:04:54 -08002387 break;
Quinn Tran726b8542017-01-19 22:28:00 -08002388 }
2389 } else {
Quinn Tran6b0431d2018-09-04 14:19:13 -07002390 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2391 qlt_build_abts_resp_iocb(mcmd);
2392 free_mcmd = false;
2393 } else
Quinn Tran82de8022017-06-13 20:47:17 -07002394 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002395 mcmd->fc_tm_rsp);
2396 }
2397 /*
2398 * Make the callback for ->free_mcmd() to queue_work() and invoke
2399 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2400 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2401 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2402 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2403 * qlt_xmit_tm_rsp() returns here..
2404 */
Quinn Tran6b0431d2018-09-04 14:19:13 -07002405 if (free_mcmd)
2406 ha->tgt.tgt_ops->free_mcmd(mcmd);
2407
Quinn Tran82de8022017-06-13 20:47:17 -07002408 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002409}
2410EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2411
2412/* No locks */
2413static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2414{
2415 struct qla_tgt_cmd *cmd = prm->cmd;
2416
2417 BUG_ON(cmd->sg_cnt == 0);
2418
2419 prm->sg = (struct scatterlist *)cmd->sg;
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002420 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002421 cmd->sg_cnt, cmd->dma_data_direction);
2422 if (unlikely(prm->seg_cnt == 0))
2423 goto out_err;
2424
2425 prm->cmd->sg_mapped = 1;
2426
Quinn Tranf83adb62014-04-11 16:54:43 -04002427 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2428 /*
2429 * If greater than four sg entries then we need to allocate
2430 * the continuation entries
2431 */
Quinn Tranb5399f72017-06-13 20:47:27 -07002432 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
Quinn Tranf83adb62014-04-11 16:54:43 -04002433 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
Quinn Tranb5399f72017-06-13 20:47:27 -07002434 QLA_TGT_DATASEGS_PER_CMD_24XX,
2435 QLA_TGT_DATASEGS_PER_CONT_24XX);
Quinn Tranf83adb62014-04-11 16:54:43 -04002436 } else {
2437 /* DIF */
2438 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2439 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2440 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2441 prm->tot_dsds = prm->seg_cnt;
2442 } else
2443 prm->tot_dsds = prm->seg_cnt;
2444
2445 if (cmd->prot_sg_cnt) {
2446 prm->prot_sg = cmd->prot_sg;
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002447 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
Quinn Tranf83adb62014-04-11 16:54:43 -04002448 cmd->prot_sg, cmd->prot_sg_cnt,
2449 cmd->dma_data_direction);
2450 if (unlikely(prm->prot_seg_cnt == 0))
2451 goto out_err;
2452
2453 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2454 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2455 /* Dif Bundling not support here */
2456 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2457 cmd->blk_sz);
2458 prm->tot_dsds += prm->prot_seg_cnt;
2459 } else
2460 prm->tot_dsds += prm->prot_seg_cnt;
2461 }
2462 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002463
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002464 return 0;
2465
2466out_err:
Quinn Tran22d84722017-06-13 20:47:25 -07002467 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002468 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2469 0, prm->cmd->sg_cnt);
2470 return -1;
2471}
2472
Joern Engelf9b67212014-09-16 16:23:18 -04002473static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002474{
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002475 struct qla_hw_data *ha;
2476 struct qla_qpair *qpair;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07002477
Joern Engelf9b67212014-09-16 16:23:18 -04002478 if (!cmd->sg_mapped)
2479 return;
2480
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002481 qpair = cmd->qpair;
2482
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002483 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002484 cmd->dma_data_direction);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002485 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002486
2487 if (cmd->prot_sg_cnt)
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002488 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
Quinn Tranf83adb62014-04-11 16:54:43 -04002489 cmd->dma_data_direction);
2490
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002491 if (!cmd->ctx)
2492 return;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002493 ha = vha->hw;
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002494 if (cmd->ctx_dsd_alloced)
2495 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2496
2497 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002498}
2499
Quinn Tran82de8022017-06-13 20:47:17 -07002500static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002501 uint32_t req_cnt)
2502{
Quinn Trand63b3282017-06-02 09:12:07 -07002503 uint32_t cnt;
Quinn Tran82de8022017-06-13 20:47:17 -07002504 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002505
Quinn Tran82de8022017-06-13 20:47:17 -07002506 if (req->cnt < (req_cnt + 2)) {
Quinn Tranaf7bb382017-06-13 20:47:23 -07002507 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2508 RD_REG_DWORD_RELAXED(req->req_q_out));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002509
Quinn Tran82de8022017-06-13 20:47:17 -07002510 if (req->ring_index < cnt)
2511 req->cnt = cnt - req->ring_index;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002512 else
Quinn Tran82de8022017-06-13 20:47:17 -07002513 req->cnt = req->length - (req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002514
Quinn Tran82de8022017-06-13 20:47:17 -07002515 if (unlikely(req->cnt < (req_cnt + 2)))
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002516 return -EAGAIN;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002517 }
2518
Quinn Tran82de8022017-06-13 20:47:17 -07002519 req->cnt -= req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002520
2521 return 0;
2522}
2523
2524/*
2525 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2526 */
Quinn Tran82de8022017-06-13 20:47:17 -07002527static inline void *qlt_get_req_pkt(struct req_que *req)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002528{
2529 /* Adjust ring index. */
Quinn Tran82de8022017-06-13 20:47:17 -07002530 req->ring_index++;
2531 if (req->ring_index == req->length) {
2532 req->ring_index = 0;
2533 req->ring_ptr = req->ring;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002534 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002535 req->ring_ptr++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002536 }
Quinn Tran82de8022017-06-13 20:47:17 -07002537 return (cont_entry_t *)req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002538}
2539
2540/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002541static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002542{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002543 uint32_t h;
Quinn Tranc5419e22017-06-13 20:47:16 -07002544 int index;
2545 uint8_t found = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07002546 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002547
Quinn Tranc5419e22017-06-13 20:47:16 -07002548 h = req->current_outstanding_cmd;
2549
2550 for (index = 1; index < req->num_outstanding_cmds; index++) {
2551 h++;
2552 if (h == req->num_outstanding_cmds)
2553 h = 1;
2554
2555 if (h == QLA_TGT_SKIP_HANDLE)
2556 continue;
2557
2558 if (!req->outstanding_cmds[h]) {
2559 found = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002560 break;
2561 }
Quinn Tranc5419e22017-06-13 20:47:16 -07002562 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002563
Quinn Tranc5419e22017-06-13 20:47:16 -07002564 if (found) {
2565 req->current_outstanding_cmd = h;
2566 } else {
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002567 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2568 "qla_target(%d): Ran out of empty cmd slots\n",
2569 qpair->vha->vp_idx);
Quinn Tranc5419e22017-06-13 20:47:16 -07002570 h = QLA_TGT_NULL_HANDLE;
2571 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002572
2573 return h;
2574}
2575
2576/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002577static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2578 struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002579{
2580 uint32_t h;
2581 struct ctio7_to_24xx *pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002582 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002583 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002584
Quinn Tran82de8022017-06-13 20:47:17 -07002585 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002586 prm->pkt = pkt;
2587 memset(pkt, 0, sizeof(*pkt));
2588
2589 pkt->entry_type = CTIO_TYPE7;
2590 pkt->entry_count = (uint8_t)prm->req_cnt;
Quinn Tran22d84722017-06-13 20:47:25 -07002591 pkt->vp_index = prm->cmd->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002592
Quinn Tran82de8022017-06-13 20:47:17 -07002593 h = qlt_make_handle(qpair);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002594 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2595 /*
2596 * CTIO type 7 from the firmware doesn't provide a way to
2597 * know the initiator's LOOP ID, hence we can't find
2598 * the session and, so, the command.
2599 */
2600 return -EAGAIN;
Quinn Trane326d222017-06-13 20:47:18 -07002601 } else
2602 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002603
Quinn Tran82de8022017-06-13 20:47:17 -07002604 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2605 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2606 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002607 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002608 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2609 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2610 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2611 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002612 temp = atio->u.isp24.attr << 9;
2613 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002614 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2615 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002616 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2617
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002618 return 0;
2619}
2620
2621/*
2622 * ha->hardware_lock supposed to be held on entry. We have already made sure
2623 * that there is sufficient amount of request entries to not drop it.
2624 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002625static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002626{
2627 int cnt;
Bart Van Assche15b7a682019-04-17 14:44:38 -07002628 struct dsd64 *cur_dsd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002629
2630 /* Build continuation packets */
2631 while (prm->seg_cnt > 0) {
2632 cont_a64_entry_t *cont_pkt64 =
Quinn Tran82de8022017-06-13 20:47:17 -07002633 (cont_a64_entry_t *)qlt_get_req_pkt(
2634 prm->cmd->qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002635
2636 /*
2637 * Make sure that from cont_pkt64 none of
2638 * 64-bit specific fields used for 32-bit
2639 * addressing. Cast to (cont_entry_t *) for
2640 * that.
2641 */
2642
2643 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2644
2645 cont_pkt64->entry_count = 1;
2646 cont_pkt64->sys_define = 0;
2647
Quinn Tran32d29b42017-06-13 20:47:26 -07002648 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
Bart Van Assche15b7a682019-04-17 14:44:38 -07002649 cur_dsd = cont_pkt64->dsd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002650
2651 /* Load continuation entry data segments */
2652 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002653 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002654 cnt++, prm->seg_cnt--) {
Bart Van Assche15b7a682019-04-17 14:44:38 -07002655 append_dsd64(&cur_dsd, prm->sg);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002656 prm->sg = sg_next(prm->sg);
2657 }
2658 }
2659}
2660
2661/*
2662 * ha->hardware_lock supposed to be held on entry. We have already made sure
2663 * that there is sufficient amount of request entries to not drop it.
2664 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002665static void qlt_load_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002666{
2667 int cnt;
Bart Van Assche15b7a682019-04-17 14:44:38 -07002668 struct dsd64 *cur_dsd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002669 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2670
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002671 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2672
2673 /* Setup packet address segment pointer */
Bart Van Assche15b7a682019-04-17 14:44:38 -07002674 cur_dsd = &pkt24->u.status0.dsd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002675
2676 /* Set total data segment count */
2677 if (prm->seg_cnt)
2678 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2679
2680 if (prm->seg_cnt == 0) {
2681 /* No data transfer */
Bart Van Assche15b7a682019-04-17 14:44:38 -07002682 cur_dsd->address = 0;
2683 cur_dsd->length = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002684 return;
2685 }
2686
2687 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002688
2689 /* Load command entry data segments */
2690 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002691 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002692 cnt++, prm->seg_cnt--) {
Bart Van Assche15b7a682019-04-17 14:44:38 -07002693 append_dsd64(&cur_dsd, prm->sg);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002694 prm->sg = sg_next(prm->sg);
2695 }
2696
Quinn Tran32d29b42017-06-13 20:47:26 -07002697 qlt_load_cont_data_segments(prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002698}
2699
2700static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2701{
2702 return cmd->bufflen > 0;
2703}
2704
Quinn Tranbe251522017-03-15 09:48:49 -07002705static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2706{
2707 struct qla_tgt_cmd *cmd;
2708 struct scsi_qla_host *vha;
2709
2710 /* asc 0x10=dif error */
2711 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2712 cmd = prm->cmd;
2713 vha = cmd->vha;
2714 /* ASCQ */
2715 switch (prm->sense_buffer[13]) {
2716 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002717 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002718 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2719 "se_cmd=%p tag[%x]",
2720 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2721 cmd->atio.u.isp24.exchange_addr);
2722 break;
2723 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002724 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002725 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2726 "se_cmd=%p tag[%x]",
2727 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2728 cmd->atio.u.isp24.exchange_addr);
2729 break;
2730 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002731 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002732 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2733 "se_cmd=%p tag[%x]",
2734 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2735 cmd->atio.u.isp24.exchange_addr);
2736 break;
2737 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002738 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002739 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2740 "se_cmd=%p tag[%x]",
2741 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2742 cmd->atio.u.isp24.exchange_addr);
2743 break;
2744 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002745 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002746 }
2747}
2748
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002749/*
2750 * Called without ha->hardware_lock held
2751 */
2752static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2753 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2754 uint32_t *full_req_cnt)
2755{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002756 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Tran22d84722017-06-13 20:47:25 -07002757 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002758
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002759 prm->cmd = cmd;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002760 prm->tgt = cmd->tgt;
2761 prm->pkt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002762 prm->rq_result = scsi_status;
2763 prm->sense_buffer = &cmd->sense_buffer[0];
2764 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2765 prm->sg = NULL;
2766 prm->seg_cnt = -1;
2767 prm->req_cnt = 1;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002768 prm->residual = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002769 prm->add_status_pkt = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002770 prm->prot_sg = NULL;
2771 prm->prot_seg_cnt = 0;
2772 prm->tot_dsds = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002773
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002774 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2775 if (qlt_pci_map_calc_cnt(prm) != 0)
2776 return -EAGAIN;
2777 }
2778
2779 *full_req_cnt = prm->req_cnt;
2780
2781 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2782 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002783 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002784 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2785 prm->residual, se_cmd->tag,
2786 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2787 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002788 prm->rq_result |= SS_RESIDUAL_UNDER;
2789 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2790 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002791 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002792 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2793 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2794 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002795 prm->rq_result |= SS_RESIDUAL_OVER;
2796 }
2797
2798 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2799 /*
2800 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2801 * ignored in *xmit_response() below
2802 */
2803 if (qlt_has_data(cmd)) {
2804 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002805 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002806 (prm->rq_result != 0))) {
2807 prm->add_status_pkt = 1;
2808 (*full_req_cnt)++;
2809 }
2810 }
2811 }
2812
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002813 return 0;
2814}
2815
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002816static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2817 int sending_sense)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002818{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002819 if (cmd->qpair->enable_class_2)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002820 return 0;
2821
2822 if (sending_sense)
2823 return cmd->conf_compl_supported;
2824 else
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002825 return cmd->qpair->enable_explicit_conf &&
2826 cmd->conf_compl_supported;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002827}
2828
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002829static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2830 struct qla_tgt_prm *prm)
2831{
2832 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2833 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002834 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002835 if (qlt_need_explicit_conf(prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002836 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002837 CTIO7_FLAGS_EXPLICIT_CONFORM |
2838 CTIO7_FLAGS_CONFORM_REQ);
2839 }
2840 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2841 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2842 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2843 int i;
2844
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002845 if (qlt_need_explicit_conf(prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002846 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Quinn Tran22d84722017-06-13 20:47:25 -07002847 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002848 "Skipping EXPLICIT_CONFORM and "
2849 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2850 "non GOOD status\n");
2851 goto skip_explict_conf;
2852 }
Bart Van Asschead950362015-07-09 07:24:08 -07002853 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002854 CTIO7_FLAGS_EXPLICIT_CONFORM |
2855 CTIO7_FLAGS_CONFORM_REQ);
2856 }
2857skip_explict_conf:
2858 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002859 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002860 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002861 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002862 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002863 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002864 ctio->u.status1.sense_length =
2865 cpu_to_le16(prm->sense_buffer_len);
2866 for (i = 0; i < prm->sense_buffer_len/4; i++)
2867 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2868 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002869
2870 qlt_print_dif_err(prm);
2871
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002872 } else {
2873 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002874 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002875 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002876 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002877 ctio->u.status1.sense_length = 0;
2878 memset(ctio->u.status1.sense_data, 0,
2879 sizeof(ctio->u.status1.sense_data));
2880 }
2881
2882 /* Sense with len > 24, is it possible ??? */
2883}
2884
Quinn Tranf83adb62014-04-11 16:54:43 -04002885static inline int
2886qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2887{
Quinn Tranf83adb62014-04-11 16:54:43 -04002888 switch (se_cmd->prot_op) {
2889 case TARGET_PROT_DOUT_INSERT:
2890 case TARGET_PROT_DIN_STRIP:
2891 if (ql2xenablehba_err_chk >= 1)
2892 return 1;
2893 break;
2894 case TARGET_PROT_DOUT_PASS:
2895 case TARGET_PROT_DIN_PASS:
2896 if (ql2xenablehba_err_chk >= 2)
2897 return 1;
2898 break;
2899 case TARGET_PROT_DIN_INSERT:
2900 case TARGET_PROT_DOUT_STRIP:
2901 return 1;
2902 default:
2903 break;
2904 }
2905 return 0;
2906}
2907
Quinn Tranbe251522017-03-15 09:48:49 -07002908static inline int
2909qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002910{
Quinn Tranbe251522017-03-15 09:48:49 -07002911 switch (se_cmd->prot_op) {
2912 case TARGET_PROT_DIN_INSERT:
2913 case TARGET_PROT_DOUT_INSERT:
2914 case TARGET_PROT_DIN_STRIP:
2915 case TARGET_PROT_DOUT_STRIP:
2916 case TARGET_PROT_DIN_PASS:
2917 case TARGET_PROT_DOUT_PASS:
2918 return 1;
2919 default:
2920 return 0;
2921 }
2922 return 0;
2923}
Quinn Tranf83adb62014-04-11 16:54:43 -04002924
Quinn Tranbe251522017-03-15 09:48:49 -07002925/*
2926 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2927 */
2928static void
2929qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2930 uint16_t *pfw_prot_opts)
2931{
2932 struct se_cmd *se_cmd = &cmd->se_cmd;
2933 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2934 scsi_qla_host_t *vha = cmd->tgt->vha;
2935 struct qla_hw_data *ha = vha->hw;
2936 uint32_t t32 = 0;
2937
2938 /*
2939 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002940 * have been immplemented by TCM, before AppTag is avail.
2941 * Look for modesense_handlers[]
2942 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002943 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002944 ctx->app_tag_mask[0] = 0x0;
2945 ctx->app_tag_mask[1] = 0x0;
2946
Quinn Tranbe251522017-03-15 09:48:49 -07002947 if (IS_PI_UNINIT_CAPABLE(ha)) {
2948 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2949 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2950 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2951 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2952 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2953 }
2954
2955 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2956
Quinn Tranf83adb62014-04-11 16:54:43 -04002957 switch (se_cmd->prot_type) {
2958 case TARGET_DIF_TYPE0_PROT:
2959 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002960 * No check for ql2xenablehba_err_chk, as it
2961 * would be an I/O error if hba tag generation
2962 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002963 */
2964 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002965 /* enable ALL bytes of the ref tag */
2966 ctx->ref_tag_mask[0] = 0xff;
2967 ctx->ref_tag_mask[1] = 0xff;
2968 ctx->ref_tag_mask[2] = 0xff;
2969 ctx->ref_tag_mask[3] = 0xff;
2970 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002971 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002972 /*
2973 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2974 * REF tag, and 16 bit app tag.
2975 */
2976 ctx->ref_tag = cpu_to_le32(lba);
2977 if (!qla_tgt_ref_mask_check(se_cmd) ||
2978 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2979 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2980 break;
2981 }
2982 /* enable ALL bytes of the ref tag */
2983 ctx->ref_tag_mask[0] = 0xff;
2984 ctx->ref_tag_mask[1] = 0xff;
2985 ctx->ref_tag_mask[2] = 0xff;
2986 ctx->ref_tag_mask[3] = 0xff;
2987 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002988 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002989 /*
2990 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2991 * tag has to match LBA in CDB + N
2992 */
2993 ctx->ref_tag = cpu_to_le32(lba);
2994 if (!qla_tgt_ref_mask_check(se_cmd) ||
2995 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2996 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2997 break;
2998 }
2999 /* enable ALL bytes of the ref tag */
3000 ctx->ref_tag_mask[0] = 0xff;
3001 ctx->ref_tag_mask[1] = 0xff;
3002 ctx->ref_tag_mask[2] = 0xff;
3003 ctx->ref_tag_mask[3] = 0xff;
3004 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003005 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07003006 /* For TYPE 3 protection: 16 bit GUARD only */
3007 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3008 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3009 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3010 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003011 }
3012}
3013
Quinn Tranf83adb62014-04-11 16:54:43 -04003014static inline int
Quinn Tran82de8022017-06-13 20:47:17 -07003015qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
Quinn Tranf83adb62014-04-11 16:54:43 -04003016{
Bart Van Assche15b7a682019-04-17 14:44:38 -07003017 struct dsd64 *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003018 uint32_t transfer_length = 0;
3019 uint32_t data_bytes;
3020 uint32_t dif_bytes;
3021 uint8_t bundling = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003022 struct crc_context *crc_ctx_pkt = NULL;
3023 struct qla_hw_data *ha;
3024 struct ctio_crc2_to_fw *pkt;
3025 dma_addr_t crc_ctx_dma;
3026 uint16_t fw_prot_opts = 0;
3027 struct qla_tgt_cmd *cmd = prm->cmd;
3028 struct se_cmd *se_cmd = &cmd->se_cmd;
3029 uint32_t h;
3030 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07003031 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003032 uint16_t t16;
Quinn Tran82de8022017-06-13 20:47:17 -07003033 scsi_qla_host_t *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003034
Quinn Tranf83adb62014-04-11 16:54:43 -04003035 ha = vha->hw;
3036
Quinn Tran82de8022017-06-13 20:47:17 -07003037 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
Quinn Tranf83adb62014-04-11 16:54:43 -04003038 prm->pkt = pkt;
3039 memset(pkt, 0, sizeof(*pkt));
3040
Quinn Tran22d84722017-06-13 20:47:25 -07003041 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
Quinn Tranf83adb62014-04-11 16:54:43 -04003042 "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 -07003043 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
Quinn Tranf83adb62014-04-11 16:54:43 -04003044 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3045
3046 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3047 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3048 bundling = 0;
3049
3050 /* Compute dif len and adjust data len to incude protection */
3051 data_bytes = cmd->bufflen;
3052 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3053
3054 switch (se_cmd->prot_op) {
3055 case TARGET_PROT_DIN_INSERT:
3056 case TARGET_PROT_DOUT_STRIP:
3057 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07003058 if (cmd->prot_sg_cnt)
3059 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04003060 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003061 case TARGET_PROT_DIN_STRIP:
3062 case TARGET_PROT_DOUT_INSERT:
3063 case TARGET_PROT_DIN_PASS:
3064 case TARGET_PROT_DOUT_PASS:
3065 transfer_length = data_bytes + dif_bytes;
3066 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003067 default:
3068 BUG();
3069 break;
3070 }
3071
3072 if (!qlt_hba_err_chk_enabled(se_cmd))
3073 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3074 /* HBA error checking enabled */
3075 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3076 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3077 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3078 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3079 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3080 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3081 }
3082
3083 switch (se_cmd->prot_op) {
3084 case TARGET_PROT_DIN_INSERT:
3085 case TARGET_PROT_DOUT_INSERT:
3086 fw_prot_opts |= PO_MODE_DIF_INSERT;
3087 break;
3088 case TARGET_PROT_DIN_STRIP:
3089 case TARGET_PROT_DOUT_STRIP:
3090 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3091 break;
3092 case TARGET_PROT_DIN_PASS:
3093 case TARGET_PROT_DOUT_PASS:
3094 fw_prot_opts |= PO_MODE_DIF_PASS;
3095 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3096 break;
3097 default:/* Normal Request */
3098 fw_prot_opts |= PO_MODE_DIF_PASS;
3099 break;
3100 }
3101
Quinn Tranf83adb62014-04-11 16:54:43 -04003102 /* ---- PKT ---- */
3103 /* Update entry type to indicate Command Type CRC_2 IOCB */
3104 pkt->entry_type = CTIO_CRC2;
3105 pkt->entry_count = 1;
Quinn Tran22d84722017-06-13 20:47:25 -07003106 pkt->vp_index = cmd->vp_idx;
Quinn Tranf83adb62014-04-11 16:54:43 -04003107
Quinn Tran82de8022017-06-13 20:47:17 -07003108 h = qlt_make_handle(qpair);
Quinn Tranf83adb62014-04-11 16:54:43 -04003109 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3110 /*
3111 * CTIO type 7 from the firmware doesn't provide a way to
3112 * know the initiator's LOOP ID, hence we can't find
3113 * the session and, so, the command.
3114 */
3115 return -EAGAIN;
3116 } else
Quinn Tran82de8022017-06-13 20:47:17 -07003117 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003118
Quinn Tran82de8022017-06-13 20:47:17 -07003119 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3120 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07003121 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07003122 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04003123 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3124 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3125 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3126 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003127
3128 /* silence compile warning */
3129 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3130 pkt->ox_id = cpu_to_le16(t16);
3131
3132 t16 = (atio->u.isp24.attr << 9);
3133 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04003134 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3135
3136 /* Set transfer direction */
3137 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07003138 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04003139 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07003140 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04003141
Quinn Tranf83adb62014-04-11 16:54:43 -04003142 pkt->dseg_count = prm->tot_dsds;
3143 /* Fibre channel byte count */
3144 pkt->transfer_length = cpu_to_le32(transfer_length);
3145
Quinn Tranf83adb62014-04-11 16:54:43 -04003146 /* ----- CRC context -------- */
3147
3148 /* Allocate CRC context from global pool */
3149 crc_ctx_pkt = cmd->ctx =
Souptick Joarder501017f2018-02-15 01:40:38 +05303150 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
Quinn Tranf83adb62014-04-11 16:54:43 -04003151
3152 if (!crc_ctx_pkt)
3153 goto crc_queuing_error;
3154
Quinn Tranf83adb62014-04-11 16:54:43 -04003155 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3156 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3157
3158 /* Set handle */
3159 crc_ctx_pkt->handle = pkt->handle;
3160
Quinn Tranbe251522017-03-15 09:48:49 -07003161 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04003162
Bart Van Assched4556a42019-04-17 14:44:39 -07003163 put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
Quinn Tranf83adb62014-04-11 16:54:43 -04003164 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3165
Quinn Tranf83adb62014-04-11 16:54:43 -04003166 if (!bundling) {
Bart Van Assche15b7a682019-04-17 14:44:38 -07003167 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003168 } else {
3169 /*
3170 * Configure Bundling if we need to fetch interlaving
3171 * protection PCI accesses
3172 */
3173 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3174 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3175 crc_ctx_pkt->u.bundling.dseg_count =
3176 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
Bart Van Assche15b7a682019-04-17 14:44:38 -07003177 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003178 }
3179
3180 /* Finish the common fields of CRC pkt */
3181 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3182 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3183 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07003184 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003185
Quinn Tranbe251522017-03-15 09:48:49 -07003186 memset((uint8_t *)&tc, 0 , sizeof(tc));
3187 tc.vha = vha;
3188 tc.blk_sz = cmd->blk_sz;
3189 tc.bufflen = cmd->bufflen;
3190 tc.sg = cmd->sg;
3191 tc.prot_sg = cmd->prot_sg;
3192 tc.ctx = crc_ctx_pkt;
3193 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04003194
3195 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07003196 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04003197
3198 if (!bundling && prm->prot_seg_cnt) {
3199 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003200 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003201 goto crc_queuing_error;
3202 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003203 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003204 goto crc_queuing_error;
3205
3206 if (bundling && prm->prot_seg_cnt) {
3207 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003208 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04003209
Bart Van Assche15b7a682019-04-17 14:44:38 -07003210 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003211 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Giridhar Malavali50b81272018-12-21 09:33:45 -08003212 prm->prot_seg_cnt, cmd))
Quinn Tranf83adb62014-04-11 16:54:43 -04003213 goto crc_queuing_error;
3214 }
3215 return QLA_SUCCESS;
3216
3217crc_queuing_error:
3218 /* Cleanup will be performed by the caller */
Quinn Tran82de8022017-06-13 20:47:17 -07003219 qpair->req->outstanding_cmds[h] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003220
3221 return QLA_FUNCTION_FAILED;
3222}
3223
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003224/*
3225 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3226 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3227 */
3228int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3229 uint8_t scsi_status)
3230{
3231 struct scsi_qla_host *vha = cmd->vha;
Quinn Tran82de8022017-06-13 20:47:17 -07003232 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003233 struct ctio7_to_24xx *pkt;
3234 struct qla_tgt_prm prm;
3235 uint32_t full_req_cnt = 0;
3236 unsigned long flags = 0;
3237 int res;
3238
Quinn Tran51fd6e62019-01-24 23:23:43 -08003239 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3240 (cmd->sess && cmd->sess->deleted)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003241 cmd->state = QLA_TGT_STATE_PROCESSED;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003242 return 0;
3243 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003244
Quinn Tran22d84722017-06-13 20:47:25 -07003245 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
Quinn Tran82de8022017-06-13 20:47:17 -07003246 "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 -04003247 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3248 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
Quinn Tran82de8022017-06-13 20:47:17 -07003249 &cmd->se_cmd, qpair->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003250
3251 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3252 &full_req_cnt);
3253 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003254 return res;
3255 }
3256
Quinn Tran82de8022017-06-13 20:47:17 -07003257 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003258
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003259 if (xmit_type == QLA_TGT_XMIT_STATUS)
Quinn Tran60a9ead2017-06-13 20:47:28 -07003260 qpair->tgt_counters.core_qla_snd_status++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003261 else
Quinn Tran60a9ead2017-06-13 20:47:28 -07003262 qpair->tgt_counters.core_qla_que_buf++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003263
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003264 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003265 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003266 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003267 * previous life, just abort the processing.
3268 */
3269 cmd->state = QLA_TGT_STATE_PROCESSED;
Quinn Tran22d84722017-06-13 20:47:25 -07003270 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003271 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3272 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003273 cmd->reset_count, qpair->chip_reset);
Quinn Tran82de8022017-06-13 20:47:17 -07003274 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04003275 return 0;
3276 }
3277
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003278 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003279 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003280 if (unlikely(res))
3281 goto out_unmap_unlock;
3282
Quinn Tranf83adb62014-04-11 16:54:43 -04003283 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
Quinn Tran82de8022017-06-13 20:47:17 -07003284 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003285 else
Quinn Tran82de8022017-06-13 20:47:17 -07003286 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Quinn Tran810e30b2015-06-10 11:05:20 -04003287 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003288 qpair->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003289 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003290 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003291
3292 pkt = (struct ctio7_to_24xx *)prm.pkt;
3293
3294 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3295 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003296 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003297 CTIO7_FLAGS_STATUS_MODE_0);
3298
Quinn Tranf83adb62014-04-11 16:54:43 -04003299 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003300 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003301
3302 if (prm.add_status_pkt == 0) {
3303 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3304 pkt->u.status0.scsi_status =
3305 cpu_to_le16(prm.rq_result);
3306 pkt->u.status0.residual =
3307 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003308 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003309 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003310 if (qlt_need_explicit_conf(cmd, 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003311 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003312 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003313 CTIO7_FLAGS_EXPLICIT_CONFORM |
3314 CTIO7_FLAGS_CONFORM_REQ);
3315 }
3316 }
3317
3318 } else {
3319 /*
3320 * We have already made sure that there is sufficient
3321 * amount of request entries to not drop HW lock in
3322 * req_pkt().
3323 */
3324 struct ctio7_to_24xx *ctio =
Quinn Tran82de8022017-06-13 20:47:17 -07003325 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3326 qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003327
Quinn Tran22d84722017-06-13 20:47:25 -07003328 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
Arun Easi667024a2014-09-25 06:14:47 -04003329 "Building additional status packet 0x%p.\n",
3330 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003331
Quinn Tranf83adb62014-04-11 16:54:43 -04003332 /*
3333 * T10Dif: ctio_crc2_to_fw overlay ontop of
3334 * ctio7_to_24xx
3335 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003336 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003337 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003338 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003339 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003340 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003341 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003342 CTIO7_FLAGS_DATA_IN);
3343
3344 /* Real finish is ctio_m1's finish */
3345 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003346 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003347 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003348
3349 /* qlt_24xx_init_ctio_to_isp will correct
3350 * all neccessary fields that's part of CTIO7.
3351 * There should be no residual of CTIO-CRC2 data.
3352 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003353 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3354 &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003355 }
3356 } else
3357 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3358
3359
3360 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003361 cmd->cmd_sent_to_fw = 1;
Quinn Tran079a3a32018-09-04 14:19:11 -07003362 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003363
Himanshu Madhani63163e02014-09-25 06:14:59 -04003364 /* Memory Barrier */
3365 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003366 if (qpair->reqq_start_iocbs)
3367 qpair->reqq_start_iocbs(qpair);
3368 else
3369 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003370 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003371
3372 return 0;
3373
3374out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003375 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003376 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003377
3378 return res;
3379}
3380EXPORT_SYMBOL(qlt_xmit_response);
3381
3382int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3383{
3384 struct ctio7_to_24xx *pkt;
3385 struct scsi_qla_host *vha = cmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003386 struct qla_tgt *tgt = cmd->tgt;
3387 struct qla_tgt_prm prm;
Quinn Tran82de8022017-06-13 20:47:17 -07003388 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003389 int res = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003390 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003391
3392 memset(&prm, 0, sizeof(prm));
3393 prm.cmd = cmd;
3394 prm.tgt = tgt;
3395 prm.sg = NULL;
3396 prm.req_cnt = 1;
3397
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003398 /* Calculate number of entries and segments required */
3399 if (qlt_pci_map_calc_cnt(&prm) != 0)
3400 return -EAGAIN;
3401
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003402 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003403 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003404 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003405 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003406 * previous life, just abort the processing.
3407 */
Bart Van Asscheaefed3e2019-04-17 14:44:29 -07003408 cmd->aborted = 1;
3409 cmd->write_data_transferred = 0;
3410 cmd->state = QLA_TGT_STATE_DATA_IN;
3411 vha->hw->tgt.tgt_ops->handle_data(cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003412 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003413 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3414 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003415 cmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003416 return 0;
3417 }
3418
Quinn Tran82de8022017-06-13 20:47:17 -07003419 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003420 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003421 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003422 if (res != 0)
3423 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003424 if (cmd->se_cmd.prot_op)
Quinn Tran82de8022017-06-13 20:47:17 -07003425 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003426 else
Quinn Tran82de8022017-06-13 20:47:17 -07003427 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003428
Quinn Tran810e30b2015-06-10 11:05:20 -04003429 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003430 qpair->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003431 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003432 }
3433
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003434 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003435 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003436 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003437
3438 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003439 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003440
3441 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003442 cmd->cmd_sent_to_fw = 1;
Quinn Tran079a3a32018-09-04 14:19:11 -07003443 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003444
Himanshu Madhani63163e02014-09-25 06:14:59 -04003445 /* Memory Barrier */
3446 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003447 if (qpair->reqq_start_iocbs)
3448 qpair->reqq_start_iocbs(qpair);
3449 else
3450 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003451 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003452
3453 return res;
3454
3455out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003456 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003457 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003458
3459 return res;
3460}
3461EXPORT_SYMBOL(qlt_rdy_to_xfer);
3462
Quinn Tranf83adb62014-04-11 16:54:43 -04003463
3464/*
Quinn Tranbe251522017-03-15 09:48:49 -07003465 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003466 */
Quinn Tranbe251522017-03-15 09:48:49 -07003467static void
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003468qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003469 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003470{
3471 uint8_t *ap = &sts->actual_dif[0];
3472 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003473 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003474 uint8_t scsi_status, sense_key, asc, ascq;
3475 unsigned long flags;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003476 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003477
Quinn Tranbe251522017-03-15 09:48:49 -07003478 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003479
Quinn Tranbe251522017-03-15 09:48:49 -07003480 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3481 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3482 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003483
Quinn Tranbe251522017-03-15 09:48:49 -07003484 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3485 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3486 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003487
Quinn Tranbe251522017-03-15 09:48:49 -07003488 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3489 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003490
Quinn Tranbe251522017-03-15 09:48:49 -07003491 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003492
Quinn Tranbe251522017-03-15 09:48:49 -07003493 /* check appl tag */
3494 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003495 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3496 "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]",
3497 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3498 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3499 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3500 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003501
Quinn Tranbe251522017-03-15 09:48:49 -07003502 cmd->dif_err_code = DIF_ERR_APP;
3503 scsi_status = SAM_STAT_CHECK_CONDITION;
3504 sense_key = ABORTED_COMMAND;
3505 asc = 0x10;
3506 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003507 }
3508
3509 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003510 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003511 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3512 "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] ",
3513 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3514 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3515 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3516 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003517
Quinn Tranbe251522017-03-15 09:48:49 -07003518 cmd->dif_err_code = DIF_ERR_REF;
3519 scsi_status = SAM_STAT_CHECK_CONDITION;
3520 sense_key = ABORTED_COMMAND;
3521 asc = 0x10;
3522 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003523 goto out;
3524 }
3525
Quinn Tranbe251522017-03-15 09:48:49 -07003526 /* check guard */
3527 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003528 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3529 "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]",
3530 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3531 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3532 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3533 cmd->atio.u.isp24.fcp_hdr.ox_id);
3534
Quinn Tranbe251522017-03-15 09:48:49 -07003535 cmd->dif_err_code = DIF_ERR_GRD;
3536 scsi_status = SAM_STAT_CHECK_CONDITION;
3537 sense_key = ABORTED_COMMAND;
3538 asc = 0x10;
3539 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003540 }
3541out:
Quinn Tranbe251522017-03-15 09:48:49 -07003542 switch (cmd->state) {
3543 case QLA_TGT_STATE_NEED_DATA:
3544 /* handle_data will load DIF error code */
3545 cmd->state = QLA_TGT_STATE_DATA_IN;
3546 vha->hw->tgt.tgt_ops->handle_data(cmd);
3547 break;
3548 default:
3549 spin_lock_irqsave(&cmd->cmd_lock, flags);
3550 if (cmd->aborted) {
3551 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3552 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3553 break;
3554 }
3555 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003556
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003557 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3558 ascq);
Quinn Tranbe251522017-03-15 09:48:49 -07003559 /* assume scsi status gets out on the wire.
3560 * Will not wait for completion.
3561 */
3562 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3563 break;
3564 }
3565}
Quinn Tranf83adb62014-04-11 16:54:43 -04003566
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003567/* If hardware_lock held on entry, might drop it, then reaquire */
3568/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003569static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3570 struct imm_ntfy_from_isp *ntfy)
3571{
3572 struct nack_to_isp *nack;
3573 struct qla_hw_data *ha = vha->hw;
3574 request_t *pkt;
3575 int ret = 0;
3576
3577 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3578 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3579
Quinn Tranec7193e2017-03-15 09:48:55 -07003580 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003581 if (pkt == NULL) {
3582 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3583 "qla_target(%d): %s failed: unable to allocate "
3584 "request packet\n", vha->vp_idx, __func__);
3585 return -ENOMEM;
3586 }
3587
3588 pkt->entry_type = NOTIFY_ACK_TYPE;
3589 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003590 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003591
3592 nack = (struct nack_to_isp *)pkt;
3593 nack->ox_id = ntfy->ox_id;
3594
3595 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3596 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3597 nack->u.isp24.flags = ntfy->u.isp24.flags &
3598 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3599 }
3600
3601 /* terminate */
3602 nack->u.isp24.flags |=
3603 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3604
3605 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3606 nack->u.isp24.status = ntfy->u.isp24.status;
3607 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3608 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3609 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3610 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3611 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3612 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3613
3614 qla2x00_start_iocbs(vha, vha->req);
3615 return ret;
3616}
3617
3618static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3619 struct imm_ntfy_from_isp *imm, int ha_locked)
3620{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003621 int rc;
3622
Bart Van Asschee65449a2019-04-17 14:44:26 -07003623 WARN_ON_ONCE(!ha_locked);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003624 rc = __qlt_send_term_imm_notif(vha, imm);
Bart Van Asschee65449a2019-04-17 14:44:26 -07003625 pr_debug("rc = %d\n", rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003626}
3627
Quinn Tran83548fe2017-06-02 09:12:01 -07003628/*
3629 * If hardware_lock held on entry, might drop it, then reaquire
3630 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3631 */
Quinn Tran82de8022017-06-13 20:47:17 -07003632static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003633 struct qla_tgt_cmd *cmd,
3634 struct atio_from_isp *atio)
3635{
Quinn Tran82de8022017-06-13 20:47:17 -07003636 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003637 struct ctio7_to_24xx *ctio24;
3638 struct qla_hw_data *ha = vha->hw;
3639 request_t *pkt;
3640 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003641 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003642
Quinn Tran83548fe2017-06-02 09:12:01 -07003643 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003644
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003645 if (cmd)
3646 vha = cmd->vha;
3647
Quinn Tran82de8022017-06-13 20:47:17 -07003648 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003649 if (pkt == NULL) {
3650 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3651 "qla_target(%d): %s failed: unable to allocate "
3652 "request packet\n", vha->vp_idx, __func__);
3653 return -ENOMEM;
3654 }
3655
3656 if (cmd != NULL) {
3657 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3658 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3659 "qla_target(%d): Terminating cmd %p with "
3660 "incorrect state %d\n", vha->vp_idx, cmd,
3661 cmd->state);
3662 } else
3663 ret = 1;
3664 }
3665
Quinn Tran60a9ead2017-06-13 20:47:28 -07003666 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003667 pkt->entry_count = 1;
3668 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3669
3670 ctio24 = (struct ctio7_to_24xx *)pkt;
3671 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003672 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003673 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003674 ctio24->vp_index = vha->vp_idx;
3675 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3676 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3677 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3678 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003679 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3680 CTIO7_FLAGS_TERMINATE;
3681 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003682 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3683 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003684
Himanshu Madhani63163e02014-09-25 06:14:59 -04003685 /* Memory Barrier */
3686 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003687 if (qpair->reqq_start_iocbs)
3688 qpair->reqq_start_iocbs(qpair);
3689 else
3690 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003691 return ret;
3692}
3693
Quinn Tran82de8022017-06-13 20:47:17 -07003694static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003695 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3696 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003697{
Quinn Tran82de8022017-06-13 20:47:17 -07003698 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003699 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003700 int rc;
3701
Quinn Tran82de8022017-06-13 20:47:17 -07003702 /* why use different vha? NPIV */
3703 if (cmd)
3704 vha = cmd->vha;
3705 else
3706 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003707
3708 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003709 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003710 if (rc == -ENOMEM)
3711 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003712 goto done;
3713 }
Quinn Tran82de8022017-06-13 20:47:17 -07003714 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3715 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003716 if (rc == -ENOMEM)
3717 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003718
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003719done:
Quinn Trana07100e2015-12-07 19:48:57 -05003720 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003721 if (cmd->sg_mapped)
3722 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003723 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3724 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003725
3726 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003727 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003728
Quinn Tran7b898542014-04-11 16:54:44 -04003729 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003730}
3731
Quinn Tran33e79972014-09-25 06:14:55 -04003732static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3733{
3734 struct list_head free_list;
3735 struct qla_tgt_cmd *cmd, *tcmd;
3736
3737 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003738 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003739
3740 cmd = tcmd = NULL;
3741 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3742 INIT_LIST_HEAD(&free_list);
3743 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3744
3745 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3746 list_del(&cmd->cmd_list);
3747 /* This cmd was never sent to TCM. There is no need
3748 * to schedule free or call free_cmd
3749 */
3750 qlt_free_cmd(cmd);
3751 vha->hw->tgt.num_qfull_cmds_alloc--;
3752 }
3753 }
3754 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3755}
3756
3757static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3758{
3759 uint32_t total_leaked;
3760
3761 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3762
3763 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3764 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3765
3766 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3767 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003768 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003769
3770 if (IS_P3P_TYPE(vha->hw))
3771 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3772 else
3773 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3774 qla2xxx_wake_dpc(vha);
3775 }
3776
3777}
3778
Quinn Trana07100e2015-12-07 19:48:57 -05003779int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003780{
3781 struct qla_tgt *tgt = cmd->tgt;
3782 struct scsi_qla_host *vha = tgt->vha;
3783 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003784 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003785
3786 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3787 "qla_target(%d): terminating exchange for aborted cmd=%p "
3788 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3789 se_cmd->tag);
3790
Quinn Trana07100e2015-12-07 19:48:57 -05003791 spin_lock_irqsave(&cmd->cmd_lock, flags);
3792 if (cmd->aborted) {
3793 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3794 /*
3795 * It's normal to see 2 calls in this path:
3796 * 1) XFER Rdy completion + CMD_T_ABORT
3797 * 2) TCM TMR - drain_state_list
3798 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003799 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3800 "multiple abort. %p transport_state %x, t_state %x, "
3801 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3802 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003803 return EIO;
3804 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003805 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003806 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003807 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003808
Quinn Tran82de8022017-06-13 20:47:17 -07003809 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003810 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003811}
3812EXPORT_SYMBOL(qlt_abort_cmd);
3813
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003814void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3815{
Quinn Tran5d964832017-01-19 22:27:59 -08003816 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003817
Quinn Tranf83adb62014-04-11 16:54:43 -04003818 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3819 "%s: se_cmd[%p] ox_id %04x\n",
3820 __func__, &cmd->se_cmd,
3821 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003822
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003823 BUG_ON(cmd->cmd_in_wq);
3824
Quinn Trana07100e2015-12-07 19:48:57 -05003825 if (cmd->sg_mapped)
3826 qlt_unmap_sg(cmd->vha, cmd);
3827
Quinn Tran33e79972014-09-25 06:14:55 -04003828 if (!cmd->q_full)
3829 qlt_decr_num_pend_cmds(cmd->vha);
3830
Quinn Tranf83adb62014-04-11 16:54:43 -04003831 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003832 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003833 if (unlikely(cmd->free_sg))
3834 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003835
3836 if (!sess || !sess->se_sess) {
3837 WARN_ON(1);
3838 return;
3839 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003840 cmd->jiffies_at_free = get_jiffies_64();
Matthew Wilcox83c2b542018-06-12 12:05:43 -07003841 target_free_tag(sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003842}
3843EXPORT_SYMBOL(qlt_free_cmd);
3844
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003845/*
3846 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3847 */
Quinn Tran82de8022017-06-13 20:47:17 -07003848static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003849 struct qla_tgt_cmd *cmd, uint32_t status)
3850{
3851 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003852 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003853
Quinn Tranbe251522017-03-15 09:48:49 -07003854 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003855 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003856 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3857 "se_cmd=%p tag[%x] op %#x/%s",
3858 cmd->lba, cmd->lba,
3859 cmd->num_blks, &cmd->se_cmd,
3860 cmd->atio.u.isp24.exchange_addr,
3861 cmd->se_cmd.prot_op,
3862 prot_op_str(cmd->se_cmd.prot_op));
3863
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003864 if (ctio != NULL) {
3865 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07003866
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003867 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003868 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003869 } else
3870 term = 1;
3871
3872 if (term)
himanshu.madhani@cavium.com3efc31f2018-01-15 20:46:47 -08003873 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003874
3875 return term;
3876}
3877
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003878
3879/* ha->hardware_lock supposed to be held on entry */
Quinn Tran6b0431d2018-09-04 14:19:13 -07003880static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003881 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003882{
Quinn Tran6b0431d2018-09-04 14:19:13 -07003883 void *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003884 struct req_que *req;
3885 int qid = GET_QID(handle);
3886 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003887
Quinn Tran82de8022017-06-13 20:47:17 -07003888 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3889 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003890
Quinn Tran82de8022017-06-13 20:47:17 -07003891 if (qid == rsp->req->id) {
3892 req = rsp->req;
3893 } else if (vha->hw->req_q_map[qid]) {
3894 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3895 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3896 vha->vp_idx, rsp->id, handle);
3897 req = vha->hw->req_q_map[qid];
3898 } else {
3899 return NULL;
3900 }
Arun Easi667024a2014-09-25 06:14:47 -04003901
Quinn Tran82de8022017-06-13 20:47:17 -07003902 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003903
Quinn Tran82de8022017-06-13 20:47:17 -07003904 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003905 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003906 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3907 "qla_target(%d): Wrong handle %x received\n",
3908 vha->vp_idx, handle);
3909 return NULL;
3910 }
Quinn Tran82de8022017-06-13 20:47:17 -07003911
Quinn Tran6b0431d2018-09-04 14:19:13 -07003912 cmd = (void *) req->outstanding_cmds[h];
Quinn Tran82de8022017-06-13 20:47:17 -07003913 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003914 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003915 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3916 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003917 return NULL;
3918 }
Quinn Tran82de8022017-06-13 20:47:17 -07003919 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003920 } else if (ctio != NULL) {
3921 /* We can't get loop ID from CTIO7 */
3922 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3923 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3924 "support NULL handles\n", vha->vp_idx);
3925 return NULL;
3926 }
3927
3928 return cmd;
3929}
3930
3931/*
3932 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3933 */
Quinn Tran82de8022017-06-13 20:47:17 -07003934static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3935 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003936{
3937 struct qla_hw_data *ha = vha->hw;
3938 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003939 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07003940 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003941
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003942 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3943 /* That could happen only in case of an error/reset/abort */
3944 if (status != CTIO_SUCCESS) {
3945 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3946 "Intermediate CTIO received"
3947 " (status %x)\n", status);
3948 }
3949 return;
3950 }
3951
Bart Van Assche81bcf1c2019-04-11 14:53:24 -07003952 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003953 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003954 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003955
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003956 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003957 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003958
Joern Engelf9b67212014-09-16 16:23:18 -04003959 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003960
3961 if (unlikely(status != CTIO_SUCCESS)) {
3962 switch (status & 0xFFFF) {
Quinn Tran079a3a32018-09-04 14:19:11 -07003963 case CTIO_INVALID_RX_ID:
3964 if (printk_ratelimit())
3965 dev_info(&vha->hw->pdev->dev,
3966 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
3967 vha->vp_idx, cmd->atio.u.isp24.attr,
3968 ((cmd->ctio_flags >> 9) & 0xf),
3969 cmd->ctio_flags);
3970
3971 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003972 case CTIO_LIP_RESET:
3973 case CTIO_TARGET_RESET:
3974 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003975 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003976 case CTIO_TIMEOUT:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003977 /* They are OK */
3978 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3979 "qla_target(%d): CTIO with "
3980 "status %#x received, state %x, se_cmd %p, "
3981 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3982 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3983 status, cmd->state, se_cmd);
3984 break;
3985
3986 case CTIO_PORT_LOGGED_OUT:
3987 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003988 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003989 int logged_out =
3990 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3991
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003992 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003993 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003994 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003995 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003996 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003997
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003998 if (logged_out && cmd->sess) {
3999 /*
4000 * Session is already logged out, but we need
4001 * to notify initiator, who's not aware of this
4002 */
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004003 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07004004 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08004005 "%s %d %8phC post del sess\n",
4006 __func__, __LINE__, cmd->sess->port_name);
4007
Quinn Trand8630bb2017-12-28 12:33:43 -08004008 qlt_schedule_sess_for_deletion(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004009 }
4010 break;
4011 }
Quinn Tranf83adb62014-04-11 16:54:43 -04004012 case CTIO_DIF_ERROR: {
4013 struct ctio_crc_from_fw *crc =
4014 (struct ctio_crc_from_fw *)ctio;
4015 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07004016 "qla_target(%d): CTIO with DIF_ERROR status %x "
4017 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4018 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04004019 vha->vp_idx, status, cmd->state, se_cmd,
4020 *((u64 *)&crc->actual_dif[0]),
4021 *((u64 *)&crc->expected_dif[0]));
4022
Quinn Tran8abfa9e2017-06-13 20:47:24 -07004023 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07004024 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04004025 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004026 default:
4027 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04004028 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004029 vha->vp_idx, status, cmd->state, se_cmd);
4030 break;
4031 }
4032
Quinn Tran7b898542014-04-11 16:54:44 -04004033
Quinn Tran193b50b2015-12-17 14:57:03 -05004034 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04004035 * cmd is already aborted/terminated, we don't
4036 * need to terminate again. The exchange is already
4037 * cleaned up/freed at FW level. Just cleanup at driver
4038 * level.
4039 */
4040 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004041 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004042 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004043 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004044 return;
Quinn Tran7b898542014-04-11 16:54:44 -04004045 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004046 }
4047
4048 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004049 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004050 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004051 cmd->state = QLA_TGT_STATE_DATA_IN;
4052
Bart Van Assche52c82822015-07-09 07:23:26 -07004053 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004054 cmd->write_data_transferred = 1;
4055
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004056 ha->tgt.tgt_ops->handle_data(cmd);
4057 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05004058 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004059 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004060 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02004061 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004062 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004063 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004064 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4065 "qla_target(%d): A command in state (%d) should "
4066 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4067 }
4068
Quinn Tran7b898542014-04-11 16:54:44 -04004069 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004070 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004071 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4072 dump_stack();
4073 }
4074
4075 ha->tgt.tgt_ops->free_cmd(cmd);
4076}
4077
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004078static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4079 uint8_t task_codes)
4080{
4081 int fcp_task_attr;
4082
4083 switch (task_codes) {
4084 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004085 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004086 break;
4087 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004088 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004089 break;
4090 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004091 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004092 break;
4093 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004094 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004095 break;
4096 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004097 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004098 break;
4099 default:
4100 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4101 "qla_target: unknown task code %x, use ORDERED instead\n",
4102 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004103 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004104 break;
4105 }
4106
4107 return fcp_task_attr;
4108}
4109
Quinn Tran5d964832017-01-19 22:27:59 -08004110static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004111 uint8_t *);
4112/*
4113 * Process context for I/O path into tcm_qla2xxx code
4114 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004115static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004116{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004117 scsi_qla_host_t *vha = cmd->vha;
4118 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004119 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004120 struct atio_from_isp *atio = &cmd->atio;
4121 unsigned char *cdb;
4122 unsigned long flags;
4123 uint32_t data_length;
4124 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07004125 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004126
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004127 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004128 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004129
Quinn Tran193b50b2015-12-17 14:57:03 -05004130 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004131 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4132 "cmd with tag %u is aborted\n",
4133 cmd->atio.u.isp24.exchange_addr);
4134 goto out_term;
4135 }
4136
Quinn Trana07100e2015-12-07 19:48:57 -05004137 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004138 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004139 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004140
4141 if (atio->u.isp24.fcp_cmnd.rddata &&
4142 atio->u.isp24.fcp_cmnd.wrdata) {
4143 bidi = 1;
4144 data_dir = DMA_TO_DEVICE;
4145 } else if (atio->u.isp24.fcp_cmnd.rddata)
4146 data_dir = DMA_FROM_DEVICE;
4147 else if (atio->u.isp24.fcp_cmnd.wrdata)
4148 data_dir = DMA_TO_DEVICE;
4149 else
4150 data_dir = DMA_NONE;
4151
4152 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4153 atio->u.isp24.fcp_cmnd.task_attr);
Quinn Trane25f7652018-05-01 09:01:50 -07004154 data_length = get_datalen_for_atio(atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004155
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004156 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4157 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004158 if (ret != 0)
4159 goto out_term;
4160 /*
Bart Van Assche67eb4a602019-04-17 14:44:10 -07004161 * Drop extra session reference from qlt_handle_cmd_for_atio().
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004162 */
Quinn Tran5d964832017-01-19 22:27:59 -08004163 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004164 return;
4165
4166out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004167 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004168 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004169 * cmd has not sent to target yet, so pass NULL as the second
4170 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004171 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004172 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004173 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4174 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004175
4176 qlt_decr_num_pend_cmds(vha);
Matthew Wilcox83c2b542018-06-12 12:05:43 -07004177 target_free_tag(sess->se_sess, &cmd->se_cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07004178 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004179
Quinn Tran5d964832017-01-19 22:27:59 -08004180 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004181}
4182
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004183static void qlt_do_work(struct work_struct *work)
4184{
4185 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004186 scsi_qla_host_t *vha = cmd->vha;
4187 unsigned long flags;
4188
4189 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4190 list_del(&cmd->cmd_list);
4191 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004192
4193 __qlt_do_work(cmd);
4194}
4195
Quinn Tran09620ee2017-06-13 20:47:20 -07004196void qlt_clr_qp_table(struct scsi_qla_host *vha)
4197{
4198 unsigned long flags;
4199 struct qla_hw_data *ha = vha->hw;
4200 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4201 void *node;
4202 u64 key = 0;
4203
4204 ql_log(ql_log_info, vha, 0x706c,
4205 "User update Number of Active Qpairs %d\n",
4206 ha->tgt.num_act_qpairs);
4207
4208 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4209
4210 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4211 btree_remove64(&tgt->lun_qpair_map, key);
4212
4213 ha->base_qpair->lun_cnt = 0;
4214 for (key = 0; key < ha->max_qpairs; key++)
4215 if (ha->queue_pair_map[key])
4216 ha->queue_pair_map[key]->lun_cnt = 0;
4217
4218 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4219}
4220
Quinn Trane326d222017-06-13 20:47:18 -07004221static void qlt_assign_qpair(struct scsi_qla_host *vha,
4222 struct qla_tgt_cmd *cmd)
4223{
4224 struct qla_qpair *qpair, *qp;
4225 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4226 struct qla_qpair_hint *h;
4227
4228 if (vha->flags.qpairs_available) {
4229 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4230 if (unlikely(!h)) {
4231 /* spread lun to qpair ratio evently */
4232 int lcnt = 0, rc;
4233 struct scsi_qla_host *base_vha =
4234 pci_get_drvdata(vha->hw->pdev);
4235
4236 qpair = vha->hw->base_qpair;
4237 if (qpair->lun_cnt == 0) {
4238 qpair->lun_cnt++;
4239 h = qla_qpair_to_hint(tgt, qpair);
4240 BUG_ON(!h);
4241 rc = btree_insert64(&tgt->lun_qpair_map,
4242 cmd->unpacked_lun, h, GFP_ATOMIC);
4243 if (rc) {
4244 qpair->lun_cnt--;
4245 ql_log(ql_log_info, vha, 0xd037,
4246 "Unable to insert lun %llx into lun_qpair_map\n",
4247 cmd->unpacked_lun);
4248 }
4249 goto out;
4250 } else {
4251 lcnt = qpair->lun_cnt;
4252 }
4253
4254 h = NULL;
4255 list_for_each_entry(qp, &base_vha->qp_list,
4256 qp_list_elem) {
4257 if (qp->lun_cnt == 0) {
4258 qp->lun_cnt++;
4259 h = qla_qpair_to_hint(tgt, qp);
4260 BUG_ON(!h);
4261 rc = btree_insert64(&tgt->lun_qpair_map,
4262 cmd->unpacked_lun, h, GFP_ATOMIC);
4263 if (rc) {
4264 qp->lun_cnt--;
4265 ql_log(ql_log_info, vha, 0xd038,
4266 "Unable to insert lun %llx into lun_qpair_map\n",
4267 cmd->unpacked_lun);
4268 }
4269 qpair = qp;
4270 goto out;
4271 } else {
4272 if (qp->lun_cnt < lcnt) {
4273 lcnt = qp->lun_cnt;
4274 qpair = qp;
4275 continue;
4276 }
4277 }
4278 }
4279 BUG_ON(!qpair);
4280 qpair->lun_cnt++;
4281 h = qla_qpair_to_hint(tgt, qpair);
4282 BUG_ON(!h);
4283 rc = btree_insert64(&tgt->lun_qpair_map,
4284 cmd->unpacked_lun, h, GFP_ATOMIC);
4285 if (rc) {
4286 qpair->lun_cnt--;
4287 ql_log(ql_log_info, vha, 0xd039,
4288 "Unable to insert lun %llx into lun_qpair_map\n",
4289 cmd->unpacked_lun);
4290 }
4291 }
4292 } else {
4293 h = &tgt->qphints[0];
4294 }
4295out:
4296 cmd->qpair = h->qpair;
4297 cmd->se_cmd.cpuid = h->cpuid;
4298}
4299
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004300static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004301 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004302 struct atio_from_isp *atio)
4303{
4304 struct se_session *se_sess = sess->se_sess;
4305 struct qla_tgt_cmd *cmd;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004306 int tag, cpu;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004307
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004308 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004309 if (tag < 0)
4310 return NULL;
4311
4312 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4313 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004314 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004315 memcpy(&cmd->atio, atio, sizeof(*atio));
4316 cmd->state = QLA_TGT_STATE_NEW;
4317 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004318 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004319 cmd->vha = vha;
4320 cmd->se_cmd.map_tag = tag;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004321 cmd->se_cmd.map_cpu = cpu;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004322 cmd->sess = sess;
4323 cmd->loop_id = sess->loop_id;
4324 cmd->conf_compl_supported = sess->conf_compl_supported;
4325
Quinn Tran1eb42f92017-01-19 22:27:55 -08004326 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004327 cmd->jiffies_at_alloc = get_jiffies_64();
4328
Quinn Trane326d222017-06-13 20:47:18 -07004329 cmd->unpacked_lun = scsilun_to_int(
4330 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4331 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004332 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004333 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004334
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004335 return cmd;
4336}
4337
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004338/* ha->hardware_lock supposed to be held on entry */
4339static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4340 struct atio_from_isp *atio)
4341{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004342 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004343 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004344 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004345 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004346 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08004347 port_id_t id;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004348
4349 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004350 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004351 "New command while device %p is shutting down\n", tgt);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004352 return -ENODEV;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004353 }
4354
Quinn Trana4239942017-12-28 12:33:26 -08004355 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4356 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4357 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4358 if (IS_SW_RESV_ADDR(id))
4359 return -EBUSY;
4360
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004361 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 -08004362 if (unlikely(!sess))
4363 return -EFAULT;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004364
4365 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4366 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004367 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004368 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004369 "New command while old session %p is being deleted\n",
4370 sess);
4371 return -EFAULT;
4372 }
4373
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004374 /*
4375 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4376 */
Quinn Tran726b8542017-01-19 22:28:00 -08004377 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004378 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004379 "%s: kref_get fail, %8phC oxid %x \n",
4380 __func__, sess->port_name,
4381 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4382 return -EFAULT;
4383 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004384
4385 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004386 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004387 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004388 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004389 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004390 return -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004391 }
4392
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004393 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004394 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004395
Quinn Tran726b8542017-01-19 22:28:00 -08004396 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004397 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004398 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004399
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004400 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004401 if (vha->flags.qpairs_available) {
4402 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4403 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004404 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4405 queue_work_on(smp_processor_id(), qla_tgt_wq,
4406 &cmd->work);
4407 else
4408 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4409 &cmd->work);
4410 } else {
4411 queue_work(qla_tgt_wq, &cmd->work);
4412 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004413
Quinn Tran82de8022017-06-13 20:47:17 -07004414 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004415}
4416
4417/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004418static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004419 int fn, void *iocb, int flags)
4420{
4421 struct scsi_qla_host *vha = sess->vha;
4422 struct qla_hw_data *ha = vha->hw;
4423 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004424 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Quinn Tran84905df2018-05-01 09:01:53 -07004425 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004426
4427 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4428 if (!mcmd) {
4429 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4430 "qla_target(%d): Allocation of management "
4431 "command failed, some commands and their data could "
4432 "leak\n", vha->vp_idx);
4433 return -ENOMEM;
4434 }
4435 memset(mcmd, 0, sizeof(*mcmd));
4436 mcmd->sess = sess;
4437
4438 if (iocb) {
4439 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4440 sizeof(mcmd->orig_iocb.imm_ntfy));
4441 }
4442 mcmd->tmr_func = fn;
4443 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004444 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran84905df2018-05-01 09:01:53 -07004445 mcmd->qpair = h->qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004446 mcmd->vha = vha;
Quinn Tran84905df2018-05-01 09:01:53 -07004447 mcmd->se_cmd.cpuid = h->cpuid;
4448 mcmd->unpacked_lun = lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004449
4450 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004451 case QLA_TGT_LUN_RESET:
Quinn Tran84905df2018-05-01 09:01:53 -07004452 case QLA_TGT_CLEAR_TS:
4453 case QLA_TGT_ABORT_TS:
4454 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
Bart Van Assche50435d42018-10-18 15:45:40 -07004455 /* fall through */
Quinn Tran84905df2018-05-01 09:01:53 -07004456 case QLA_TGT_CLEAR_ACA:
4457 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4458 mcmd->qpair = h->qpair;
4459 mcmd->se_cmd.cpuid = h->cpuid;
4460 break;
4461
4462 case QLA_TGT_TARGET_RESET:
4463 case QLA_TGT_NEXUS_LOSS_SESS:
4464 case QLA_TGT_NEXUS_LOSS:
4465 case QLA_TGT_ABORT_ALL:
4466 default:
4467 /* no-op */
4468 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004469 }
4470
Quinn Tran84905df2018-05-01 09:01:53 -07004471 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4472 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4473 &mcmd->work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004474
4475 return 0;
4476}
4477
4478/* ha->hardware_lock supposed to be held on entry */
4479static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4480{
4481 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4482 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004483 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004484 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004485 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004486 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004487
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004488 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004489
4490 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004491 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4492 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004493 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4494
Quinn Tranf775bd12017-06-02 09:11:59 -07004495 unpacked_lun =
4496 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004497
Quinn Tranfb352652017-12-28 12:33:32 -08004498 if (sess == NULL || sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004499 return -EFAULT;
4500
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004501 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4502}
4503
4504/* ha->hardware_lock supposed to be held on entry */
4505static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004506 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004507{
4508 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4509 struct qla_hw_data *ha = vha->hw;
4510 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004511 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004512 int rc;
4513
4514 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4515 if (mcmd == NULL) {
4516 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4517 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4518 vha->vp_idx, __func__);
4519 return -ENOMEM;
4520 }
4521 memset(mcmd, 0, sizeof(*mcmd));
4522
4523 mcmd->sess = sess;
4524 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4525 sizeof(mcmd->orig_iocb.imm_ntfy));
4526
Quinn Tranf775bd12017-06-02 09:11:59 -07004527 unpacked_lun =
4528 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004529 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004530 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004531 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004532
Quinn Tranbe92fc32017-01-19 22:27:54 -08004533 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004534 le16_to_cpu(iocb->u.isp2x.seq_id));
4535 if (rc != 0) {
4536 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4537 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4538 vha->vp_idx, rc);
4539 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4540 return -EFAULT;
4541 }
4542
4543 return 0;
4544}
4545
4546/* ha->hardware_lock supposed to be held on entry */
4547static int qlt_abort_task(struct scsi_qla_host *vha,
4548 struct imm_ntfy_from_isp *iocb)
4549{
4550 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004551 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004552 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004553 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004554
4555 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4556
Quinn Tran75601512015-12-17 14:57:04 -05004557 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004558 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004559 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4560
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004561 if (sess == NULL) {
4562 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4563 "qla_target(%d): task abort for unexisting "
4564 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004565 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004566 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4567 }
4568
4569 return __qlt_abort_task(vha, iocb, sess);
4570}
4571
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004572void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4573{
Quinn Tran726b8542017-01-19 22:28:00 -08004574 if (rc != MBS_COMMAND_COMPLETE) {
4575 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4576 "%s: se_sess %p / sess %p from"
4577 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4578 " LOGO failed: %#x\n",
4579 __func__,
4580 fcport->se_sess,
4581 fcport,
4582 fcport->port_name, fcport->loop_id,
4583 fcport->d_id.b.domain, fcport->d_id.b.area,
4584 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004585 }
Quinn Tran726b8542017-01-19 22:28:00 -08004586
4587 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004588}
4589
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004590/*
4591* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4592*
4593* Schedules sessions with matching port_id/loop_id but different wwn for
4594* deletion. Returns existing session with matching wwn if present.
4595* Null otherwise.
4596*/
Quinn Tran726b8542017-01-19 22:28:00 -08004597struct fc_port *
4598qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004599 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004600{
Quinn Tran5d964832017-01-19 22:27:59 -08004601 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004602 uint64_t other_wwn;
4603
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004604 *conflict_sess = NULL;
4605
Quinn Tran5d964832017-01-19 22:27:59 -08004606 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004607
4608 other_wwn = wwn_to_u64(other_sess->port_name);
4609
4610 if (wwn == other_wwn) {
4611 WARN_ON(sess);
4612 sess = other_sess;
4613 continue;
4614 }
4615
4616 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004617 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004618 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004619 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004620 "Invalidating sess %p loop_id %d wwn %llx.\n",
4621 other_sess, other_sess->loop_id, other_wwn);
4622
4623 /*
4624 * logout_on_delete is set by default, but another
4625 * session that has the same s_id/loop_id combo
4626 * might have cleared it when requested this session
4627 * deletion, so don't touch it
4628 */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004629 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004630 } else {
4631 /*
4632 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004633 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004634 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004635 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004636 "Invalidating sess %p loop_id %d wwn %llx.\n",
4637 other_sess, other_sess->loop_id, other_wwn);
4638
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004639 other_sess->keep_nport_handle = 1;
Quinn Tranba743f92017-12-04 14:45:12 -08004640 if (other_sess->disc_state != DSC_DELETED)
4641 *conflict_sess = other_sess;
Quinn Tran94cff6e2017-12-28 12:33:42 -08004642 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004643 }
4644 continue;
4645 }
4646
4647 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004648 if ((loop_id == other_sess->loop_id) &&
4649 (loop_id != FC_NO_LOOP_ID)) {
4650 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004651 "Invalidating sess %p loop_id %d wwn %llx.\n",
4652 other_sess, other_sess->loop_id, other_wwn);
4653
4654 /* Same loop_id but different s_id
4655 * Ok to kill and logout */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004656 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004657 }
4658 }
4659
4660 return sess;
4661}
4662
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004663/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4664static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4665{
4666 struct qla_tgt_sess_op *op;
4667 struct qla_tgt_cmd *cmd;
4668 uint32_t key;
4669 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004670 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004671
4672 key = (((u32)s_id->b.domain << 16) |
4673 ((u32)s_id->b.area << 8) |
4674 ((u32)s_id->b.al_pa));
4675
Quinn Tran8b631d82017-06-02 09:11:54 -07004676 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004677 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4678 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004679
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004680 if (op_key == key) {
4681 op->aborted = true;
4682 count++;
4683 }
4684 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004685
4686 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4687 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004688
Quinn Tran41dc5292017-01-19 22:28:03 -08004689 if (op_key == key) {
4690 op->aborted = true;
4691 count++;
4692 }
4693 }
4694
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004695 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4696 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004697
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004698 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004699 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004700 count++;
4701 }
4702 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004703 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004704
4705 return count;
4706}
4707
Quinn Tran9cd883f2017-12-28 12:33:24 -08004708static int qlt_handle_login(struct scsi_qla_host *vha,
4709 struct imm_ntfy_from_isp *iocb)
4710{
4711 struct fc_port *sess = NULL, *conflict_sess = NULL;
4712 uint64_t wwn;
4713 port_id_t port_id;
4714 uint16_t loop_id, wd3_lo;
4715 int res = 0;
4716 struct qlt_plogi_ack_t *pla;
4717 unsigned long flags;
4718
4719 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4720
4721 port_id.b.domain = iocb->u.isp24.port_id[2];
4722 port_id.b.area = iocb->u.isp24.port_id[1];
4723 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4724 port_id.b.rsvd_1 = 0;
4725
4726 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4727
4728 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4729 abort_cmds_for_s_id(vha, &port_id);
4730
4731 if (wwn) {
4732 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4733 sess = qlt_find_sess_invalidate_other(vha, wwn,
4734 port_id, loop_id, &conflict_sess);
4735 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Tranaa9e6d72018-08-31 11:24:38 -07004736 } else {
4737 ql_dbg(ql_dbg_disc, vha, 0xffff,
4738 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4739 __func__, __LINE__, loop_id, port_id.b24);
4740 qlt_send_term_imm_notif(vha, iocb, 1);
4741 goto out;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004742 }
4743
4744 if (IS_SW_RESV_ADDR(port_id)) {
4745 res = 1;
4746 goto out;
4747 }
4748
4749 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4750 if (!pla) {
Quinn Tran48acad02018-08-02 13:16:44 -07004751 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4752 "%s %d %8phC Term INOT due to mem alloc fail",
4753 __func__, __LINE__,
4754 iocb->u.isp24.port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004755 qlt_send_term_imm_notif(vha, iocb, 1);
4756 goto out;
4757 }
4758
4759 if (conflict_sess) {
4760 conflict_sess->login_gen++;
4761 qlt_plogi_ack_link(vha, pla, conflict_sess,
4762 QLT_PLOGI_LINK_CONFLICT);
4763 }
4764
4765 if (!sess) {
4766 pla->ref_count++;
4767 ql_dbg(ql_dbg_disc, vha, 0xffff,
4768 "%s %d %8phC post new sess\n",
4769 __func__, __LINE__, iocb->u.isp24.port_name);
Quinn Trana4239942017-12-28 12:33:26 -08004770 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4771 qla24xx_post_newsess_work(vha, &port_id,
4772 iocb->u.isp24.port_name,
4773 iocb->u.isp24.u.plogi.node_name,
4774 pla, FC4_TYPE_UNKNOWN);
4775 else
4776 qla24xx_post_newsess_work(vha, &port_id,
4777 iocb->u.isp24.port_name, NULL,
4778 pla, FC4_TYPE_UNKNOWN);
4779
Quinn Tran9cd883f2017-12-28 12:33:24 -08004780 goto out;
4781 }
4782
Quinn Trancd4ed6b2018-08-31 11:24:31 -07004783 if (sess->disc_state == DSC_UPD_FCPORT) {
4784 u16 sec;
4785
4786 /*
4787 * Remote port registration is still going on from
4788 * previous login. Allow it to finish before we
4789 * accept the new login.
4790 */
4791 sess->next_disc_state = DSC_DELETE_PEND;
4792 sec = jiffies_to_msecs(jiffies -
4793 sess->jiffies_at_registration) / 1000;
4794 if (sess->sec_since_registration < sec && sec &&
4795 !(sec % 5)) {
4796 sess->sec_since_registration = sec;
4797 ql_dbg(ql_dbg_disc, vha, 0xffff,
4798 "%s %8phC - Slow Rport registration (%d Sec)\n",
4799 __func__, sess->port_name, sec);
4800 }
4801
4802 if (!conflict_sess)
4803 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4804
4805 qlt_send_term_imm_notif(vha, iocb, 1);
4806 goto out;
4807 }
4808
Quinn Tran9cd883f2017-12-28 12:33:24 -08004809 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4810 sess->d_id = port_id;
4811 sess->login_gen++;
4812
4813 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4814 sess->fw_login_state = DSC_LS_PRLI_PEND;
4815 sess->local = 0;
4816 sess->loop_id = loop_id;
4817 sess->d_id = port_id;
4818 sess->fw_login_state = DSC_LS_PRLI_PEND;
4819 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4820
4821 if (wd3_lo & BIT_7)
4822 sess->conf_compl_supported = 1;
4823
4824 if ((wd3_lo & BIT_4) == 0)
4825 sess->port_type = FCT_INITIATOR;
4826 else
4827 sess->port_type = FCT_TARGET;
4828
4829 } else
4830 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4831
4832
4833 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4834 "%s %d %8phC DS %d\n",
4835 __func__, __LINE__, sess->port_name, sess->disc_state);
4836
4837 switch (sess->disc_state) {
4838 case DSC_DELETED:
4839 qlt_plogi_ack_unref(vha, pla);
4840 break;
4841
4842 default:
4843 /*
4844 * Under normal circumstances we want to release nport handle
4845 * during LOGO process to avoid nport handle leaks inside FW.
4846 * The exception is when LOGO is done while another PLOGI with
4847 * the same nport handle is waiting as might be the case here.
4848 * Note: there is always a possibily of a race where session
4849 * deletion has already started for other reasons (e.g. ACL
4850 * removal) and now PLOGI arrives:
4851 * 1. if PLOGI arrived in FW after nport handle has been freed,
4852 * FW must have assigned this PLOGI a new/same handle and we
4853 * can proceed ACK'ing it as usual when session deletion
4854 * completes.
4855 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4856 * bit reached it, the handle has now been released. We'll
4857 * get an error when we ACK this PLOGI. Nothing will be sent
4858 * back to initiator. Initiator should eventually retry
4859 * PLOGI and situation will correct itself.
4860 */
4861 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4862 (sess->d_id.b24 == port_id.b24));
4863
4864 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4865 "%s %d %8phC post del sess\n",
4866 __func__, __LINE__, sess->port_name);
4867
4868
Quinn Trand8630bb2017-12-28 12:33:43 -08004869 qlt_schedule_sess_for_deletion(sess);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004870 break;
4871 }
4872out:
4873 return res;
4874}
4875
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004876/*
4877 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4878 */
4879static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4880 struct imm_ntfy_from_isp *iocb)
4881{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004882 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004883 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004884 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004885 uint64_t wwn;
4886 port_id_t port_id;
4887 uint16_t loop_id;
4888 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004889 int res = 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004890 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004891
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004892 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4893
4894 port_id.b.domain = iocb->u.isp24.port_id[2];
4895 port_id.b.area = iocb->u.isp24.port_id[1];
4896 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4897 port_id.b.rsvd_1 = 0;
4898
4899 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4900
Quinn Tran726b8542017-01-19 22:28:00 -08004901 ql_dbg(ql_dbg_disc, vha, 0xf026,
4902 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4903 vha->vp_idx, iocb->u.isp24.port_id[2],
4904 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4905 iocb->u.isp24.status_subcode, loop_id,
4906 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004907
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004908 /* res = 1 means ack at the end of thread
4909 * res = 0 means ack async/later.
4910 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004911 switch (iocb->u.isp24.status_subcode) {
4912 case ELS_PLOGI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004913 res = qlt_handle_login(vha, iocb);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004914 break;
4915
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004916 case ELS_PRLI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004917 if (N2N_TOPO(ha)) {
4918 sess = qla2x00_find_fcport_by_wwpn(vha,
4919 iocb->u.isp24.port_name, 1);
4920
4921 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4922 ql_dbg(ql_dbg_disc, vha, 0xffff,
4923 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4924 __func__, __LINE__,
4925 iocb->u.isp24.port_name);
4926 qlt_send_term_imm_notif(vha, iocb, 1);
4927 break;
4928 }
4929
4930 res = qlt_handle_login(vha, iocb);
4931 break;
4932 }
4933
Quinn Trana4239942017-12-28 12:33:26 -08004934 if (IS_SW_RESV_ADDR(port_id)) {
4935 res = 1;
4936 break;
4937 }
4938
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004939 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4940
Quinn Tran75601512015-12-17 14:57:04 -05004941 if (wwn) {
4942 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004943 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4944 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004945 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4946 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004947
4948 if (conflict_sess) {
Quinn Tran5ef696a2017-12-04 14:45:05 -08004949 switch (conflict_sess->disc_state) {
4950 case DSC_DELETED:
4951 case DSC_DELETE_PEND:
4952 break;
4953 default:
4954 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4955 "PRLI with conflicting sess %p port %8phC\n",
4956 conflict_sess, conflict_sess->port_name);
4957 conflict_sess->fw_login_state =
4958 DSC_LS_PORT_UNAVAIL;
4959 qlt_send_term_imm_notif(vha, iocb, 1);
4960 res = 0;
4961 break;
4962 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004963 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004964
4965 if (sess != NULL) {
Quinn Trana4239942017-12-28 12:33:26 -08004966 bool delete = false;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07004967 int sec;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004968
Quinn Tran82abdca2017-12-28 12:33:22 -08004969 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4970 switch (sess->fw_login_state) {
Quinn Trana4239942017-12-28 12:33:26 -08004971 case DSC_LS_PLOGI_PEND:
Quinn Tran82abdca2017-12-28 12:33:22 -08004972 case DSC_LS_PLOGI_COMP:
4973 case DSC_LS_PRLI_COMP:
4974 break;
4975 default:
Quinn Trana4239942017-12-28 12:33:26 -08004976 delete = true;
4977 break;
4978 }
4979
4980 switch (sess->disc_state) {
Quinn Trancd4ed6b2018-08-31 11:24:31 -07004981 case DSC_UPD_FCPORT:
4982 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4983 flags);
4984
4985 sec = jiffies_to_msecs(jiffies -
4986 sess->jiffies_at_registration)/1000;
4987 if (sess->sec_since_registration < sec && sec &&
4988 !(sec % 5)) {
4989 sess->sec_since_registration = sec;
4990 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
4991 "%s %8phC : Slow Rport registration(%d Sec)\n",
4992 __func__, sess->port_name, sec);
4993 }
4994 qlt_send_term_imm_notif(vha, iocb, 1);
4995 return 0;
4996
Quinn Trana4239942017-12-28 12:33:26 -08004997 case DSC_LOGIN_PEND:
4998 case DSC_GPDB:
Quinn Trana4239942017-12-28 12:33:26 -08004999 case DSC_LOGIN_COMPLETE:
5000 case DSC_ADISC:
5001 delete = false;
5002 break;
5003 default:
5004 break;
5005 }
5006
5007 if (delete) {
Quinn Tran82abdca2017-12-28 12:33:22 -08005008 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5009 flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005010 /*
5011 * Impatient initiator sent PRLI before last
5012 * PLOGI could finish. Will force him to re-try,
5013 * while last one finishes.
5014 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04005015 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005016 "sess %p PRLI received, before plogi ack.\n",
5017 sess);
5018 qlt_send_term_imm_notif(vha, iocb, 1);
5019 res = 0;
5020 break;
5021 }
5022
5023 /*
5024 * This shouldn't happen under normal circumstances,
5025 * since we have deleted the old session during PLOGI
5026 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04005027 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005028 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5029 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5030
5031 sess->local = 0;
5032 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08005033 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005034 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005035
5036 if (wd3_lo & BIT_7)
5037 sess->conf_compl_supported = 1;
5038
Quinn Tran726b8542017-01-19 22:28:00 -08005039 if ((wd3_lo & BIT_4) == 0)
5040 sess->port_type = FCT_INITIATOR;
5041 else
5042 sess->port_type = FCT_TARGET;
Quinn Tran82abdca2017-12-28 12:33:22 -08005043
5044 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005045 }
5046 res = 1; /* send notify ack */
5047
5048 /* Make session global (not used in fabric mode) */
5049 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005050 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005051 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07005052 "%s %d %8phC post nack\n",
5053 __func__, __LINE__, sess->port_name);
5054 qla24xx_post_nack_work(vha, sess, iocb,
5055 SRB_NACK_PRLI);
5056 res = 0;
5057 } else {
5058 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5059 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5060 qla2xxx_wake_dpc(vha);
5061 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005062 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005063 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005064 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07005065 "%s %d %8phC post nack\n",
5066 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08005067 qla24xx_post_nack_work(vha, sess, iocb,
5068 SRB_NACK_PRLI);
5069 res = 0;
5070 }
5071 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005072 break;
5073
Quinn Tran41dc5292017-01-19 22:28:03 -08005074 case ELS_TPRLO:
5075 if (le16_to_cpu(iocb->u.isp24.flags) &
5076 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5077 loop_id = 0xFFFF;
5078 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5079 res = 1;
5080 break;
5081 }
Bart Van Assche81881862017-12-07 16:02:46 -08005082 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005083 case ELS_LOGO:
5084 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08005085 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5086 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5087 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5088
5089 if (sess) {
5090 sess->login_gen++;
5091 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08005092 sess->logo_ack_needed = 1;
5093 memcpy(sess->iocb, iocb, IOCB_SIZE);
5094 }
5095
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005096 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08005097
Quinn Tran83548fe2017-06-02 09:12:01 -07005098 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08005099 "%s: logo %llx res %d sess %p ",
5100 __func__, wwn, res, sess);
5101 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08005102 /*
5103 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5104 * for LOGO_ACK & sess delete
5105 */
Quinn Tran726b8542017-01-19 22:28:00 -08005106 BUG_ON(!sess);
5107 res = 0;
5108 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08005109 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08005110 if (sess) {
Quinn Trand8630bb2017-12-28 12:33:43 -08005111 qlt_schedule_sess_for_deletion(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08005112 res = 0;
5113 }
5114 /* else logo will be ack */
5115 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005116 break;
5117 case ELS_PDISC:
5118 case ELS_ADISC:
5119 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005120 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005121
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005122 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005123 qlt_send_notify_ack(ha->base_qpair,
5124 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005125 tgt->link_reinit_iocb_pending = 0;
5126 }
Quinn Tran726b8542017-01-19 22:28:00 -08005127
5128 sess = qla2x00_find_fcport_by_wwpn(vha,
5129 iocb->u.isp24.port_name, 1);
5130 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005131 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08005132 "sess %p lid %d|%d DS %d LS %d\n",
5133 sess, sess->loop_id, loop_id,
5134 sess->disc_state, sess->fw_login_state);
5135 }
5136
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005137 res = 1; /* send notify ack */
5138 break;
5139 }
5140
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005141 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005142 default:
5143 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5144 "qla_target(%d): Unsupported ELS command %x "
5145 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5146 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5147 break;
5148 }
5149
Quinn Tran9cd883f2017-12-28 12:33:24 -08005150 ql_dbg(ql_dbg_disc, vha, 0xf026,
5151 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5152 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5153
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005154 return res;
5155}
5156
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005157/*
5158 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5159 */
5160static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5161 struct imm_ntfy_from_isp *iocb)
5162{
5163 struct qla_hw_data *ha = vha->hw;
5164 uint32_t add_flags = 0;
5165 int send_notify_ack = 1;
5166 uint16_t status;
5167
5168 status = le16_to_cpu(iocb->u.isp2x.status);
5169 switch (status) {
5170 case IMM_NTFY_LIP_RESET:
5171 {
5172 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5173 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5174 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5175 iocb->u.isp24.status_subcode);
5176
5177 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5178 send_notify_ack = 0;
5179 break;
5180 }
5181
5182 case IMM_NTFY_LIP_LINK_REINIT:
5183 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005184 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005185
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005186 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5187 "qla_target(%d): LINK REINIT (loop %#x, "
5188 "subcode %x)\n", vha->vp_idx,
5189 le16_to_cpu(iocb->u.isp24.nport_handle),
5190 iocb->u.isp24.status_subcode);
5191 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005192 qlt_send_notify_ack(ha->base_qpair,
5193 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005194 }
5195 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5196 tgt->link_reinit_iocb_pending = 1;
5197 /*
5198 * QLogic requires to wait after LINK REINIT for possible
5199 * PDISC or ADISC ELS commands
5200 */
5201 send_notify_ack = 0;
5202 break;
5203 }
5204
5205 case IMM_NTFY_PORT_LOGOUT:
5206 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5207 "qla_target(%d): Port logout (loop "
5208 "%#x, subcode %x)\n", vha->vp_idx,
5209 le16_to_cpu(iocb->u.isp24.nport_handle),
5210 iocb->u.isp24.status_subcode);
5211
5212 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5213 send_notify_ack = 0;
5214 /* The sessions will be cleared in the callback, if needed */
5215 break;
5216
5217 case IMM_NTFY_GLBL_TPRLO:
5218 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5219 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5220 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5221 send_notify_ack = 0;
5222 /* The sessions will be cleared in the callback, if needed */
5223 break;
5224
5225 case IMM_NTFY_PORT_CONFIG:
5226 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5227 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5228 status);
5229 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5230 send_notify_ack = 0;
5231 /* The sessions will be cleared in the callback, if needed */
5232 break;
5233
5234 case IMM_NTFY_GLBL_LOGO:
5235 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5236 "qla_target(%d): Link failure detected\n",
5237 vha->vp_idx);
5238 /* I_T nexus loss */
5239 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5240 send_notify_ack = 0;
5241 break;
5242
5243 case IMM_NTFY_IOCB_OVERFLOW:
5244 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5245 "qla_target(%d): Cannot provide requested "
5246 "capability (IOCB overflowed the immediate notify "
5247 "resource count)\n", vha->vp_idx);
5248 break;
5249
5250 case IMM_NTFY_ABORT_TASK:
5251 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5252 "qla_target(%d): Abort Task (S %08x I %#x -> "
5253 "L %#x)\n", vha->vp_idx,
5254 le16_to_cpu(iocb->u.isp2x.seq_id),
5255 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5256 le16_to_cpu(iocb->u.isp2x.lun));
5257 if (qlt_abort_task(vha, iocb) == 0)
5258 send_notify_ack = 0;
5259 break;
5260
5261 case IMM_NTFY_RESOURCE:
5262 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5263 "qla_target(%d): Out of resources, host %ld\n",
5264 vha->vp_idx, vha->host_no);
5265 break;
5266
5267 case IMM_NTFY_MSG_RX:
5268 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5269 "qla_target(%d): Immediate notify task %x\n",
5270 vha->vp_idx, iocb->u.isp2x.task_flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005271 break;
5272
5273 case IMM_NTFY_ELS:
5274 if (qlt_24xx_handle_els(vha, iocb) == 0)
5275 send_notify_ack = 0;
5276 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005277 default:
5278 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5279 "qla_target(%d): Received unknown immediate "
5280 "notify status %x\n", vha->vp_idx, status);
5281 break;
5282 }
5283
5284 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005285 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5286 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005287}
5288
5289/*
5290 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5291 * This function sends busy to ISP 2xxx or 24xx.
5292 */
Quinn Tran82de8022017-06-13 20:47:17 -07005293static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005294 struct atio_from_isp *atio, uint16_t status)
5295{
Quinn Tran82de8022017-06-13 20:47:17 -07005296 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005297 struct ctio7_to_24xx *ctio24;
5298 struct qla_hw_data *ha = vha->hw;
5299 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005300 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005301 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005302 u16 temp;
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005303 port_id_t id;
5304
5305 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5306 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5307 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5308 id.b.rsvd_1 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005309
Quinn Tran75601512015-12-17 14:57:04 -05005310 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005311 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
Quinn Tran75601512015-12-17 14:57:04 -05005312 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005313 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005314 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005315 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005316 }
5317 /* Sending marker isn't necessary, since we called from ISR */
5318
Quinn Tran82de8022017-06-13 20:47:17 -07005319 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005320 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005321 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005322 "qla_target(%d): %s failed: unable to allocate "
5323 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005324 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005325 }
5326
Quinn Tran60a9ead2017-06-13 20:47:28 -07005327 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005328 pkt->entry_count = 1;
5329 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5330
5331 ctio24 = (struct ctio7_to_24xx *)pkt;
5332 ctio24->entry_type = CTIO_TYPE7;
5333 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005334 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005335 ctio24->vp_index = vha->vp_idx;
5336 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5337 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5338 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5339 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005340 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005341 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005342 CTIO7_FLAGS_DONT_RET_CTIO;
5343 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005344 /*
5345 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5346 * if the explicit conformation is used.
5347 */
5348 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5349 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Quinn Trane25f7652018-05-01 09:01:50 -07005350
5351 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5352
5353 if (ctio24->u.status1.residual != 0)
5354 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5355
Himanshu Madhani63163e02014-09-25 06:14:59 -04005356 /* Memory Barrier */
5357 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005358 if (qpair->reqq_start_iocbs)
5359 qpair->reqq_start_iocbs(qpair);
5360 else
5361 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005362 return 0;
5363}
5364
5365/*
5366 * This routine is used to allocate a command for either a QFull condition
5367 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5368 * out previously.
5369 */
5370static void
5371qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5372 struct atio_from_isp *atio, uint16_t status, int qfull)
5373{
5374 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5375 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005376 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005377 struct se_session *se_sess;
5378 struct qla_tgt_cmd *cmd;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005379 int tag, cpu;
Quinn Tran82de8022017-06-13 20:47:17 -07005380 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005381
5382 if (unlikely(tgt->tgt_stop)) {
5383 ql_dbg(ql_dbg_io, vha, 0x300a,
5384 "New command while device %p is shutting down\n", tgt);
5385 return;
5386 }
5387
5388 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5389 vha->hw->tgt.num_qfull_cmds_dropped++;
5390 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005391 vha->qla_stats.stat_max_qfull_cmds_dropped)
5392 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005393 vha->hw->tgt.num_qfull_cmds_dropped;
5394
5395 ql_dbg(ql_dbg_io, vha, 0x3068,
5396 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5397 vha->vp_idx, __func__,
5398 vha->hw->tgt.num_qfull_cmds_dropped);
5399
5400 qlt_chk_exch_leak_thresh_hold(vha);
5401 return;
5402 }
5403
5404 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5405 (vha, atio->u.isp24.fcp_hdr.s_id);
5406 if (!sess)
5407 return;
5408
5409 se_sess = sess->se_sess;
5410
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005411 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Bart Van Asschec04466c2019-04-17 14:44:27 -07005412 if (tag < 0) {
Quinn Tran33e79972014-09-25 06:14:55 -04005413 ql_dbg(ql_dbg_io, vha, 0x3009,
5414 "qla_target(%d): %s: Allocation of cmd failed\n",
5415 vha->vp_idx, __func__);
5416
5417 vha->hw->tgt.num_qfull_cmds_dropped++;
5418 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005419 vha->qla_stats.stat_max_qfull_cmds_dropped)
5420 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005421 vha->hw->tgt.num_qfull_cmds_dropped;
5422
5423 qlt_chk_exch_leak_thresh_hold(vha);
5424 return;
5425 }
5426
Bart Van Asschec04466c2019-04-17 14:44:27 -07005427 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
Quinn Tran33e79972014-09-25 06:14:55 -04005428 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5429
5430 qlt_incr_num_pend_cmds(vha);
5431 INIT_LIST_HEAD(&cmd->cmd_list);
5432 memcpy(&cmd->atio, atio, sizeof(*atio));
5433
5434 cmd->tgt = vha->vha_tgt.qla_tgt;
5435 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005436 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005437 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005438 cmd->qpair = ha->base_qpair;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005439 cmd->se_cmd.map_cpu = cpu;
Quinn Tran33e79972014-09-25 06:14:55 -04005440
5441 if (qfull) {
5442 cmd->q_full = 1;
5443 /* NOTE: borrowing the state field to carry the status */
5444 cmd->state = status;
5445 } else
5446 cmd->term_exchg = 1;
5447
Quinn Tran82de8022017-06-13 20:47:17 -07005448 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005449 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5450
5451 vha->hw->tgt.num_qfull_cmds_alloc++;
5452 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005453 vha->qla_stats.stat_max_qfull_cmds_alloc)
5454 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005455 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005456 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005457}
5458
5459int
Quinn Tran82de8022017-06-13 20:47:17 -07005460qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005461{
Quinn Tran82de8022017-06-13 20:47:17 -07005462 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005463 struct qla_hw_data *ha = vha->hw;
5464 unsigned long flags;
5465 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005466 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005467 int rc = 0;
5468
5469 if (list_empty(&ha->tgt.q_full_list))
5470 return 0;
5471
5472 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005473 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005474
Quinn Tran82de8022017-06-13 20:47:17 -07005475 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005476 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005477 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005478 return 0;
5479 }
5480
Quinn Tran82de8022017-06-13 20:47:17 -07005481 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5482 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5483
5484 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5485 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005486 if (cmd->q_full)
5487 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005488 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005489 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005490 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005491
5492 if (rc == -ENOMEM)
5493 break;
5494
5495 if (cmd->q_full)
5496 ql_dbg(ql_dbg_io, vha, 0x3006,
5497 "%s: busy sent for ox_id[%04x]\n", __func__,
5498 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5499 else if (cmd->term_exchg)
5500 ql_dbg(ql_dbg_io, vha, 0x3007,
5501 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5502 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5503 else
5504 ql_dbg(ql_dbg_io, vha, 0x3008,
5505 "%s: Unexpected cmd in QFull list %p\n", __func__,
5506 cmd);
5507
5508 list_del(&cmd->cmd_list);
5509 list_add_tail(&cmd->cmd_list, &free_list);
5510
5511 /* piggy back on hardware_lock for protection */
5512 vha->hw->tgt.num_qfull_cmds_alloc--;
5513 }
Quinn Tran82de8022017-06-13 20:47:17 -07005514 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005515
5516 cmd = NULL;
5517
5518 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5519 list_del(&cmd->cmd_list);
5520 /* This cmd was never sent to TCM. There is no need
5521 * to schedule free or call free_cmd
5522 */
5523 qlt_free_cmd(cmd);
5524 }
Quinn Tran82de8022017-06-13 20:47:17 -07005525
5526 if (!list_empty(&q_full_list)) {
5527 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5528 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5529 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5530 }
5531
Quinn Tran33e79972014-09-25 06:14:55 -04005532 return rc;
5533}
5534
5535static void
Quinn Tran82de8022017-06-13 20:47:17 -07005536qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5537 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005538{
5539 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005540 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005541
Quinn Tran82de8022017-06-13 20:47:17 -07005542 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005543 if (rc == -ENOMEM)
5544 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5545}
5546
5547static int
Quinn Tran82de8022017-06-13 20:47:17 -07005548qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5549 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005550{
5551 struct qla_hw_data *ha = vha->hw;
Quinn Tran8b666802017-03-15 09:48:45 -07005552 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005553
5554 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5555 return 0;
5556
Quinn Tran8b666802017-03-15 09:48:45 -07005557 if (!ha_locked)
5558 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005559 qlt_send_busy(qpair, atio, qla_sam_status);
Quinn Tran8b666802017-03-15 09:48:45 -07005560 if (!ha_locked)
5561 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5562
Quinn Tran33e79972014-09-25 06:14:55 -04005563 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005564}
5565
5566/* ha->hardware_lock supposed to be held on entry */
5567/* called via callback from qla2xxx */
5568static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005569 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005570{
5571 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005572 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005573 int rc;
Quinn Tran7cf95f72017-12-28 12:33:28 -08005574 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005575
5576 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005577 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005578 "ATIO pkt, but no tgt (ha %p)", ha);
5579 return;
5580 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005581 /*
5582 * In tgt_stop mode we also should allow all requests to pass.
5583 * Otherwise, some commands can stuck.
5584 */
5585
Quinn Tran2f424b92015-12-17 14:57:07 -05005586 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005587
5588 switch (atio->u.raw.entry_type) {
5589 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005590 if (unlikely(atio->u.isp24.exchange_addr ==
5591 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005592 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005593 "qla_target(%d): ATIO_TYPE7 "
5594 "received with UNKNOWN exchange address, "
5595 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005596 if (!ha_locked)
5597 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005598 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
Quinn Tran2f424b92015-12-17 14:57:07 -05005599 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005600 spin_unlock_irqrestore(&ha->hardware_lock,
5601 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005602 break;
5603 }
Quinn Tran33e79972014-09-25 06:14:55 -04005604
Quinn Tran33e79972014-09-25 06:14:55 -04005605 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005606 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5607 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005608 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005609 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005610 return;
5611 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005612 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005613 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005614 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005615 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005616 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005617 if (!ha_locked)
5618 spin_lock_irqsave(&ha->hardware_lock, flags);
5619 switch (rc) {
5620 case -ENODEV:
5621 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5622 "qla_target: Unable to send command to target\n");
5623 break;
5624 case -EBADF:
5625 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5626 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005627 qlt_send_term_exchange(ha->base_qpair, NULL,
5628 atio, 1, 0);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005629 break;
5630 case -EBUSY:
5631 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5632 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5633 vha->vp_idx);
5634 qlt_send_busy(ha->base_qpair, atio,
5635 tc_sam_status);
5636 break;
5637 default:
5638 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5639 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5640 vha->vp_idx);
5641 qlt_send_busy(ha->base_qpair, atio,
5642 qla_sam_status);
5643 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005644 }
Quinn Tran7cf95f72017-12-28 12:33:28 -08005645 if (!ha_locked)
5646 spin_unlock_irqrestore(&ha->hardware_lock,
5647 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005648 }
5649 break;
5650
5651 case IMMED_NOTIFY_TYPE:
5652 {
5653 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5654 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5655 "qla_target(%d): Received ATIO packet %x "
5656 "with error status %x\n", vha->vp_idx,
5657 atio->u.raw.entry_type,
5658 atio->u.isp2x.entry_status);
5659 break;
5660 }
5661 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005662
5663 if (!ha_locked)
5664 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005665 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005666 if (!ha_locked)
5667 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005668 break;
5669 }
5670
5671 default:
5672 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5673 "qla_target(%d): Received unknown ATIO atio "
5674 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5675 break;
5676 }
5677
Quinn Tran2f424b92015-12-17 14:57:07 -05005678 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005679}
5680
Quinn Tran06910942018-09-04 14:19:12 -07005681/*
5682 * qpair lock is assume to be held
5683 * rc = 0 : send terminate & abts respond
5684 * rc != 0: do not send term & abts respond
5685 */
5686static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5687 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5688{
5689 struct qla_hw_data *ha = vha->hw;
5690 int rc = 0;
5691
5692 /*
5693 * Detect unresolved exchange. If the same ABTS is unable
5694 * to terminate an existing command and the same ABTS loops
5695 * between FW & Driver, then force FW dump. Under 1 jiff,
5696 * we should see multiple loops.
5697 */
5698 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5699 qpair->retry_term_jiff == jiffies) {
5700 /* found existing exchange */
5701 qpair->retry_term_cnt++;
5702 if (qpair->retry_term_cnt >= 5) {
5703 rc = EIO;
5704 qpair->retry_term_cnt = 0;
5705 ql_log(ql_log_warn, vha, 0xffff,
5706 "Unable to send ABTS Respond. Dumping firmware.\n");
5707 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5708 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5709
5710 if (qpair == ha->base_qpair)
5711 ha->isp_ops->fw_dump(vha, 1);
5712 else
5713 ha->isp_ops->fw_dump(vha, 0);
5714
5715 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5716 qla2xxx_wake_dpc(vha);
5717 }
5718 } else if (qpair->retry_term_jiff != jiffies) {
5719 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5720 qpair->retry_term_cnt = 0;
5721 qpair->retry_term_jiff = jiffies;
5722 }
5723
5724 return rc;
5725}
5726
Quinn Tran6b0431d2018-09-04 14:19:13 -07005727
5728static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5729 struct rsp_que *rsp, response_t *pkt)
5730{
5731 struct abts_resp_from_24xx_fw *entry =
5732 (struct abts_resp_from_24xx_fw *)pkt;
5733 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5734 struct qla_tgt_mgmt_cmd *mcmd;
5735 struct qla_hw_data *ha = vha->hw;
5736
Bart Van Assche81bcf1c2019-04-11 14:53:24 -07005737 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
Quinn Tran6b0431d2018-09-04 14:19:13 -07005738 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5739 ql_dbg(ql_dbg_async, vha, 0xe064,
5740 "qla_target(%d): ABTS Comp without mcmd\n",
5741 vha->vp_idx);
5742 return;
5743 }
5744
5745 if (mcmd)
5746 vha = mcmd->vha;
5747 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5748
5749 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5750 "ABTS_RESP_24XX: compl_status %x\n",
5751 entry->compl_status);
5752
5753 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5754 if ((entry->error_subcode1 == 0x1E) &&
5755 (entry->error_subcode2 == 0)) {
5756 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5757 ha->tgt.tgt_ops->free_mcmd(mcmd);
5758 return;
5759 }
5760 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5761 pkt, mcmd);
5762 } else {
5763 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5764 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5765 vha->vp_idx, entry->compl_status,
5766 entry->error_subcode1,
5767 entry->error_subcode2);
5768 ha->tgt.tgt_ops->free_mcmd(mcmd);
5769 }
5770 } else {
5771 ha->tgt.tgt_ops->free_mcmd(mcmd);
5772 }
5773}
5774
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005775/* ha->hardware_lock supposed to be held on entry */
5776/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005777static void qlt_response_pkt(struct scsi_qla_host *vha,
5778 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005779{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005780 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005781
5782 if (unlikely(tgt == NULL)) {
5783 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005784 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5785 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005786 return;
5787 }
5788
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005789 /*
5790 * In tgt_stop mode we also should allow all requests to pass.
5791 * Otherwise, some commands can stuck.
5792 */
5793
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005794 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005795 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005796 case CTIO_TYPE7:
5797 {
5798 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005799
Quinn Tran82de8022017-06-13 20:47:17 -07005800 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005801 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5802 entry);
5803 break;
5804 }
5805
5806 case ACCEPT_TGT_IO_TYPE:
5807 {
5808 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5809 int rc;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005810
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005811 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005812 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005813 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5814 "qla_target(%d): ATIO with error "
5815 "status %x received\n", vha->vp_idx,
5816 le16_to_cpu(atio->u.isp2x.status));
5817 break;
5818 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005819
Quinn Tran82de8022017-06-13 20:47:17 -07005820 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005821 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005822 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005823
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005824 rc = qlt_handle_cmd_for_atio(vha, atio);
5825 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005826 switch (rc) {
5827 case -ENODEV:
5828 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5829 "qla_target: Unable to send command to target\n");
5830 break;
5831 case -EBADF:
5832 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5833 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5834 qlt_send_term_exchange(rsp->qpair, NULL,
5835 atio, 1, 0);
5836 break;
5837 case -EBUSY:
5838 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5839 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5840 vha->vp_idx);
5841 qlt_send_busy(rsp->qpair, atio,
5842 tc_sam_status);
5843 break;
5844 default:
5845 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5846 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5847 vha->vp_idx);
5848 qlt_send_busy(rsp->qpair, atio,
5849 qla_sam_status);
5850 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005851 }
5852 }
5853 }
5854 break;
5855
5856 case CONTINUE_TGT_IO_TYPE:
5857 {
5858 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005859
Quinn Tran82de8022017-06-13 20:47:17 -07005860 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005861 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5862 entry);
5863 break;
5864 }
5865
5866 case CTIO_A64_TYPE:
5867 {
5868 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005869
Quinn Tran82de8022017-06-13 20:47:17 -07005870 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005871 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5872 entry);
5873 break;
5874 }
5875
5876 case IMMED_NOTIFY_TYPE:
5877 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5878 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5879 break;
5880
5881 case NOTIFY_ACK_TYPE:
5882 if (tgt->notify_ack_expected > 0) {
5883 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005884
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005885 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5886 "NOTIFY_ACK seq %08x status %x\n",
5887 le16_to_cpu(entry->u.isp2x.seq_id),
5888 le16_to_cpu(entry->u.isp2x.status));
5889 tgt->notify_ack_expected--;
5890 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005891 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005892 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5893 "qla_target(%d): NOTIFY_ACK "
5894 "failed %x\n", vha->vp_idx,
5895 le16_to_cpu(entry->u.isp2x.status));
5896 }
5897 } else {
5898 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5899 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5900 vha->vp_idx);
5901 }
5902 break;
5903
5904 case ABTS_RECV_24XX:
5905 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5906 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5907 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5908 break;
5909
5910 case ABTS_RESP_24XX:
5911 if (tgt->abts_resp_expected > 0) {
Quinn Tran6b0431d2018-09-04 14:19:13 -07005912 qlt_handle_abts_completion(vha, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005913 } else {
5914 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5915 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5916 "received\n", vha->vp_idx);
5917 }
5918 break;
5919
5920 default:
5921 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5922 "qla_target(%d): Received unknown response pkt "
5923 "type %x\n", vha->vp_idx, pkt->entry_type);
5924 break;
5925 }
5926
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005927}
5928
5929/*
5930 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5931 */
5932void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5933 uint16_t *mailbox)
5934{
5935 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005936 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005937 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005938
Quinn Tran3a33dc92017-06-02 09:12:04 -07005939 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005940 return;
5941
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005942 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5943 IS_QLA2100(ha))
5944 return;
5945 /*
5946 * In tgt_stop mode we also should allow all requests to pass.
5947 * Otherwise, some commands can stuck.
5948 */
5949
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005950
5951 switch (code) {
5952 case MBA_RESET: /* Reset */
5953 case MBA_SYSTEM_ERR: /* System Error */
5954 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5955 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5956 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5957 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005958 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005959 break;
5960 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5961 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5962 break;
5963
5964 case MBA_LOOP_UP:
5965 {
5966 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005967 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005968 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5969 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5970 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005971 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005972 qlt_send_notify_ack(ha->base_qpair,
5973 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005974 0, 0, 0, 0, 0, 0);
5975 tgt->link_reinit_iocb_pending = 0;
5976 }
5977 break;
5978 }
5979
5980 case MBA_LIP_OCCURRED:
5981 case MBA_LOOP_DOWN:
5982 case MBA_LIP_RESET:
5983 case MBA_RSCN_UPDATE:
5984 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005985 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005986 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5987 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5988 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005989 break;
5990
Quinn Tranead03852017-01-19 22:28:01 -08005991 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005992 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5993 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5994 vha->vp_idx,
5995 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5996 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005997
5998 if (le16_to_cpu(mailbox[3]) == 1) {
5999 /* exchange starvation. */
6000 vha->hw->exch_starvation++;
6001 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006002 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08006003 "Exchange starvation-. Resetting RISC\n");
6004
6005 vha->hw->exch_starvation = 0;
6006 if (IS_P3P_TYPE(vha->hw))
6007 set_bit(FCOE_CTX_RESET_NEEDED,
6008 &vha->dpc_flags);
6009 else
6010 set_bit(ISP_ABORT_NEEDED,
6011 &vha->dpc_flags);
6012 qla2xxx_wake_dpc(vha);
6013 }
6014 }
6015 break;
6016
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006017 case MBA_PORT_UPDATE:
6018 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6019 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006020 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01006021 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6022 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6023 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6024
6025 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08006026 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006027 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6028 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08006029 vha->hw->exch_starvation = 0;
6030 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006031 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6032 "Async MB 2: Port Logged Out\n");
6033 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006034 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006035 break;
6036 }
6037
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006038}
6039
6040static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6041 uint16_t loop_id)
6042{
Quinn Tran726b8542017-01-19 22:28:00 -08006043 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006044 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08006045 unsigned long flags;
6046 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006047
Quinn Tran063b36d2017-12-04 14:45:10 -08006048 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006049 if (!fcport) {
6050 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6051 "qla_target(%d): Allocation of tmp FC port failed",
6052 vha->vp_idx);
6053 return NULL;
6054 }
6055
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006056 fcport->loop_id = loop_id;
6057
Quinn Tran15f30a52017-03-15 09:48:52 -07006058 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006059 if (rc != QLA_SUCCESS) {
6060 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6061 "qla_target(%d): Failed to retrieve fcport "
6062 "information -- get_port_database() returned %x "
6063 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6064 kfree(fcport);
6065 return NULL;
6066 }
6067
Quinn Tran726b8542017-01-19 22:28:00 -08006068 del = NULL;
6069 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6070 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6071
6072 if (tfcp) {
6073 tfcp->d_id = fcport->d_id;
6074 tfcp->port_type = fcport->port_type;
6075 tfcp->supported_classes = fcport->supported_classes;
6076 tfcp->flags |= fcport->flags;
Quinn Tran76f9a2d2017-12-04 14:45:11 -08006077 tfcp->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08006078
6079 del = fcport;
6080 fcport = tfcp;
6081 } else {
6082 if (vha->hw->current_topology == ISP_CFG_F)
6083 fcport->flags |= FCF_FABRIC_DEVICE;
6084
6085 list_add_tail(&fcport->list, &vha->vp_fcports);
6086 if (!IS_SW_RESV_ADDR(fcport->d_id))
6087 vha->fcport_count++;
6088 fcport->login_gen++;
6089 fcport->disc_state = DSC_LOGIN_COMPLETE;
6090 fcport->login_succ = 1;
6091 newfcport = 1;
6092 }
6093
6094 fcport->deleted = 0;
6095 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6096
6097 switch (vha->host->active_mode) {
6098 case MODE_INITIATOR:
6099 case MODE_DUAL:
6100 if (newfcport) {
6101 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Trancd4ed6b2018-08-31 11:24:31 -07006102 qla24xx_sched_upd_fcport(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08006103 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07006104 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08006105 "%s %d %8phC post gpsc fcp_cnt %d\n",
6106 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6107 qla24xx_post_gpsc_work(vha, fcport);
6108 }
6109 }
6110 break;
6111
6112 case MODE_TARGET:
6113 default:
6114 break;
6115 }
6116 if (del)
6117 qla2x00_free_fcport(del);
6118
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006119 return fcport;
6120}
6121
6122/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08006123static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006124 uint8_t *s_id)
6125{
Quinn Tran5d964832017-01-19 22:27:59 -08006126 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006127 fc_port_t *fcport = NULL;
6128 int rc, global_resets;
6129 uint16_t loop_id = 0;
6130
Quinn Tran726b8542017-01-19 22:28:00 -08006131 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
6132 /*
6133 * This is Domain Controller, so it should be
6134 * OK to drop SCSI commands from it.
6135 */
6136 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6137 "Unable to find initiator with S_ID %x:%x:%x",
6138 s_id[0], s_id[1], s_id[2]);
6139 return NULL;
6140 }
6141
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006142 mutex_lock(&vha->vha_tgt.tgt_mutex);
6143
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006144retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006145 global_resets =
6146 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006147
6148 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6149 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006150 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6151
Quinn Tran726b8542017-01-19 22:28:00 -08006152 ql_log(ql_log_info, vha, 0xf071,
6153 "qla_target(%d): Unable to find "
6154 "initiator with S_ID %x:%x:%x",
6155 vha->vp_idx, s_id[0], s_id[1],
6156 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006157
6158 if (rc == -ENOENT) {
6159 qlt_port_logo_t logo;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07006160
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006161 sid_to_portid(s_id, &logo.id);
6162 logo.cmd_count = 1;
6163 qlt_send_first_logo(vha, &logo);
6164 }
6165
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006166 return NULL;
6167 }
6168
6169 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006170 if (!fcport) {
6171 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006172 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006173 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006174
6175 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006176 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006177 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6178 "qla_target(%d): global reset during session discovery "
6179 "(counter was %d, new %d), retrying", vha->vp_idx,
6180 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006181 atomic_read(&vha->vha_tgt.
6182 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006183 goto retry;
6184 }
6185
6186 sess = qlt_create_sess(vha, fcport, true);
6187
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006188 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6189
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006190 return sess;
6191}
6192
6193static void qlt_abort_work(struct qla_tgt *tgt,
6194 struct qla_tgt_sess_work_param *prm)
6195{
6196 struct scsi_qla_host *vha = tgt->vha;
6197 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08006198 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006199 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006200 uint32_t be_s_id;
6201 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
6213 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6214 (unsigned char *)&be_s_id);
6215 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006216 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006217
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006218 sess = qlt_make_local_sess(vha, s_id);
6219 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006220
Quinn Tran75601512015-12-17 14:57:04 -05006221 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006222 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05006223 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006224 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006225 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006226 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006227 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006228 }
6229
Quinn Tran726b8542017-01-19 22:28:00 -08006230 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006231 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08006232 "%s: kref_get fail %8phC \n",
6233 __func__, sess->port_name);
6234 sess = NULL;
6235 goto out_term2;
6236 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006237 }
6238
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006239 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006240 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6241
Bart Van Assche300ec742019-04-17 14:44:41 -07006242 ha->tgt.tgt_ops->put_sess(sess);
6243
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006244 if (rc != 0)
6245 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006246 return;
6247
Quinn Tran75601512015-12-17 14:57:04 -05006248out_term2:
Bart Van Assche300ec742019-04-17 14:44:41 -07006249 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6250
Quinn Tran726b8542017-01-19 22:28:00 -08006251 if (sess)
6252 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006253
6254out_term:
6255 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006256 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6257 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006258 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006259}
6260
6261static void qlt_tmr_work(struct qla_tgt *tgt,
6262 struct qla_tgt_sess_work_param *prm)
6263{
6264 struct atio_from_isp *a = &prm->tm_iocb2;
6265 struct scsi_qla_host *vha = tgt->vha;
6266 struct qla_hw_data *ha = vha->hw;
Colin Ian Kingbb6abdd2019-02-15 09:52:32 +00006267 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006268 unsigned long flags;
6269 uint8_t *s_id = NULL; /* to hide compiler warnings */
6270 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07006271 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07006272 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006273 void *iocb;
6274
Quinn Tran75601512015-12-17 14:57:04 -05006275 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006276
6277 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006278 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006279
6280 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6281 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6282 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006283 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006284
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006285 sess = qlt_make_local_sess(vha, s_id);
6286 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006287
Quinn Tran75601512015-12-17 14:57:04 -05006288 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006289 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006290 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006291 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006292 if (sess->deleted) {
Quinn Tranf159b3c2017-03-15 09:48:47 -07006293 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006294 }
6295
Quinn Tran726b8542017-01-19 22:28:00 -08006296 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006297 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08006298 "%s: kref_get fail %8phC\n",
6299 __func__, sess->port_name);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006300 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006301 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006302 }
6303
6304 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006305 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006306 unpacked_lun =
6307 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006308
6309 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006310 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6311
Bart Van Assche300ec742019-04-17 14:44:41 -07006312 ha->tgt.tgt_ops->put_sess(sess);
6313
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006314 if (rc != 0)
6315 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006316 return;
6317
Quinn Tranf159b3c2017-03-15 09:48:47 -07006318out_term2:
Quinn Tranf159b3c2017-03-15 09:48:47 -07006319 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006320out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006321 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006322}
6323
6324static void qlt_sess_work_fn(struct work_struct *work)
6325{
6326 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6327 struct scsi_qla_host *vha = tgt->vha;
6328 unsigned long flags;
6329
6330 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6331
6332 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6333 while (!list_empty(&tgt->sess_works_list)) {
6334 struct qla_tgt_sess_work_param *prm = list_entry(
6335 tgt->sess_works_list.next, typeof(*prm),
6336 sess_works_list_entry);
6337
6338 /*
6339 * This work can be scheduled on several CPUs at time, so we
6340 * must delete the entry to eliminate double processing
6341 */
6342 list_del(&prm->sess_works_list_entry);
6343
6344 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6345
6346 switch (prm->type) {
6347 case QLA_TGT_SESS_WORK_ABORT:
6348 qlt_abort_work(tgt, prm);
6349 break;
6350 case QLA_TGT_SESS_WORK_TM:
6351 qlt_tmr_work(tgt, prm);
6352 break;
6353 default:
6354 BUG_ON(1);
6355 break;
6356 }
6357
6358 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6359
6360 kfree(prm);
6361 }
6362 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6363}
6364
6365/* Must be called under tgt_host_action_mutex */
6366int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6367{
6368 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006369 int rc, i;
6370 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006371
6372 if (!QLA_TGT_MODE_ENABLED())
6373 return 0;
6374
Arun Easi33c36c02013-01-30 03:34:41 -05006375 if (!IS_TGT_MODE_CAPABLE(ha)) {
6376 ql_log(ql_log_warn, base_vha, 0xe070,
6377 "This adapter does not support target mode.\n");
6378 return 0;
6379 }
6380
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006381 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006382 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006383
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006384 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006385
6386 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6387 if (!tgt) {
6388 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6389 "Unable to allocate struct qla_tgt\n");
6390 return -ENOMEM;
6391 }
6392
Kees Cook6396bb22018-06-12 14:03:40 -07006393 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6394 sizeof(struct qla_qpair_hint),
6395 GFP_KERNEL);
Quinn Trane326d222017-06-13 20:47:18 -07006396 if (!tgt->qphints) {
6397 kfree(tgt);
6398 ql_log(ql_log_warn, base_vha, 0x0197,
6399 "Unable to allocate qpair hints.\n");
6400 return -ENOMEM;
6401 }
6402
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006403 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6404 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6405
Quinn Trane326d222017-06-13 20:47:18 -07006406 rc = btree_init64(&tgt->lun_qpair_map);
6407 if (rc) {
6408 kfree(tgt->qphints);
6409 kfree(tgt);
6410 ql_log(ql_log_info, base_vha, 0x0198,
6411 "Unable to initialize lun_qpair_map btree\n");
6412 return -EIO;
6413 }
6414 h = &tgt->qphints[0];
6415 h->qpair = ha->base_qpair;
6416 INIT_LIST_HEAD(&h->hint_elem);
6417 h->cpuid = ha->base_qpair->cpuid;
6418 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6419
6420 for (i = 0; i < ha->max_qpairs; i++) {
6421 unsigned long flags;
6422
6423 struct qla_qpair *qpair = ha->queue_pair_map[i];
Bart Van Asschebd432bb2019-04-11 14:53:17 -07006424
Quinn Trane326d222017-06-13 20:47:18 -07006425 h = &tgt->qphints[i + 1];
6426 INIT_LIST_HEAD(&h->hint_elem);
6427 if (qpair) {
6428 h->qpair = qpair;
6429 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6430 list_add_tail(&h->hint_elem, &qpair->hints_list);
6431 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6432 h->cpuid = qpair->cpuid;
6433 }
6434 }
6435
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006436 tgt->ha = ha;
6437 tgt->vha = base_vha;
6438 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006439 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006440 spin_lock_init(&tgt->sess_work_lock);
6441 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6442 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006443 atomic_set(&tgt->tgt_global_resets_count, 0);
6444
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006445 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006446
6447 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6448 "qla_target(%d): using 64 Bit PCI addressing",
6449 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006450 /* 3 is reserved */
6451 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006452
6453 mutex_lock(&qla_tgt_mutex);
6454 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6455 mutex_unlock(&qla_tgt_mutex);
6456
Quinn Tranf1443ee2017-03-15 09:48:51 -07006457 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6458 ha->tgt.tgt_ops->add_target(base_vha);
6459
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006460 return 0;
6461}
6462
6463/* Must be called under tgt_host_action_mutex */
6464int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6465{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006466 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006467 return 0;
6468
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006469 if (vha->fc_vport) {
6470 qlt_release(vha->vha_tgt.qla_tgt);
6471 return 0;
6472 }
Quinn Tran33e79972014-09-25 06:14:55 -04006473
6474 /* free left over qfull cmds */
6475 qlt_init_term_exchange(vha);
6476
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006477 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6478 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006479 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006480
6481 return 0;
6482}
6483
Quinn Tran482c9dc2017-03-15 09:48:54 -07006484void qlt_remove_target_resources(struct qla_hw_data *ha)
6485{
6486 struct scsi_qla_host *node;
6487 u32 key = 0;
6488
6489 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6490 btree_remove32(&ha->tgt.host_map, key);
6491
6492 btree_destroy32(&ha->tgt.host_map);
6493}
6494
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006495static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6496 unsigned char *b)
6497{
6498 int i;
6499
6500 pr_debug("qla2xxx HW vha->node_name: ");
6501 for (i = 0; i < WWN_SIZE; i++)
6502 pr_debug("%02x ", vha->node_name[i]);
6503 pr_debug("\n");
6504 pr_debug("qla2xxx HW vha->port_name: ");
6505 for (i = 0; i < WWN_SIZE; i++)
6506 pr_debug("%02x ", vha->port_name[i]);
6507 pr_debug("\n");
6508
6509 pr_debug("qla2xxx passed configfs WWPN: ");
6510 put_unaligned_be64(wwpn, b);
6511 for (i = 0; i < WWN_SIZE; i++)
6512 pr_debug("%02x ", b[i]);
6513 pr_debug("\n");
6514}
6515
6516/**
6517 * qla_tgt_lport_register - register lport with external module
6518 *
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006519 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
Bart Van Assche807eb902018-10-18 15:45:41 -07006520 * @phys_wwpn: physical port WWPN
6521 * @npiv_wwpn: NPIV WWPN
6522 * @npiv_wwnn: NPIV WWNN
Bart Van Assche2db62282018-01-23 16:33:51 -08006523 * @callback: lport initialization callback for tcm_qla2xxx code
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006524 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006525int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6526 u64 npiv_wwpn, u64 npiv_wwnn,
6527 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006528{
6529 struct qla_tgt *tgt;
6530 struct scsi_qla_host *vha;
6531 struct qla_hw_data *ha;
6532 struct Scsi_Host *host;
6533 unsigned long flags;
6534 int rc;
6535 u8 b[WWN_SIZE];
6536
6537 mutex_lock(&qla_tgt_mutex);
6538 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6539 vha = tgt->vha;
6540 ha = vha->hw;
6541
6542 host = vha->host;
6543 if (!host)
6544 continue;
6545
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006546 if (!(host->hostt->supported_mode & MODE_TARGET))
6547 continue;
6548
Quinn Tran0645cb82018-09-11 10:18:18 -07006549 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6550 continue;
6551
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006552 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006553 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006554 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6555 host->host_no);
6556 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6557 continue;
6558 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006559 if (tgt->tgt_stop) {
6560 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6561 host->host_no);
6562 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6563 continue;
6564 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006565 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6566
6567 if (!scsi_host_get(host)) {
6568 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6569 "Unable to scsi_host_get() for"
6570 " qla2xxx scsi_host\n");
6571 continue;
6572 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006573 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006574
6575 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6576 scsi_host_put(host);
6577 continue;
6578 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006579 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6580 if (rc != 0)
6581 scsi_host_put(host);
6582
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006583 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006584 return rc;
6585 }
6586 mutex_unlock(&qla_tgt_mutex);
6587
6588 return -ENODEV;
6589}
6590EXPORT_SYMBOL(qlt_lport_register);
6591
6592/**
6593 * qla_tgt_lport_deregister - Degister lport
6594 *
6595 * @vha: Registered scsi_qla_host pointer
6596 */
6597void qlt_lport_deregister(struct scsi_qla_host *vha)
6598{
6599 struct qla_hw_data *ha = vha->hw;
6600 struct Scsi_Host *sh = vha->host;
6601 /*
6602 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6603 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006604 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006605 ha->tgt.tgt_ops = NULL;
6606 /*
6607 * Release the Scsi_Host reference for the underlying qla2xxx host
6608 */
6609 scsi_host_put(sh);
6610}
6611EXPORT_SYMBOL(qlt_lport_deregister);
6612
6613/* Must be called under HW lock */
Quinn Tran0645cb82018-09-11 10:18:18 -07006614void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006615{
Quinn Tran0645cb82018-09-11 10:18:18 -07006616 switch (vha->qlini_mode) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006617 case QLA2XXX_INI_MODE_DISABLED:
6618 case QLA2XXX_INI_MODE_EXCLUSIVE:
6619 vha->host->active_mode = MODE_TARGET;
6620 break;
6621 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tran0645cb82018-09-11 10:18:18 -07006622 vha->host->active_mode = MODE_INITIATOR;
Quinn Tranead03852017-01-19 22:28:01 -08006623 break;
6624 case QLA2XXX_INI_MODE_DUAL:
6625 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006626 break;
6627 default:
6628 break;
6629 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006630}
6631
6632/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006633static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006634{
Quinn Tran0645cb82018-09-11 10:18:18 -07006635 switch (vha->qlini_mode) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006636 case QLA2XXX_INI_MODE_DISABLED:
6637 vha->host->active_mode = MODE_UNKNOWN;
6638 break;
6639 case QLA2XXX_INI_MODE_EXCLUSIVE:
6640 vha->host->active_mode = MODE_INITIATOR;
6641 break;
6642 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006643 case QLA2XXX_INI_MODE_DUAL:
6644 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006645 break;
6646 default:
6647 break;
6648 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006649}
6650
6651/*
6652 * qla_tgt_enable_vha - NO LOCK HELD
6653 *
6654 * host_reset, bring up w/ Target Mode Enabled
6655 */
6656void
6657qlt_enable_vha(struct scsi_qla_host *vha)
6658{
6659 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006660 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006661 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006662 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006663
6664 if (!tgt) {
6665 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6666 "Unable to locate qla_tgt pointer from"
6667 " struct qla_hw_data\n");
6668 dump_stack();
6669 return;
6670 }
Quinn Tran0645cb82018-09-11 10:18:18 -07006671 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6672 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006673
6674 spin_lock_irqsave(&ha->hardware_lock, flags);
6675 tgt->tgt_stopped = 0;
6676 qlt_set_mode(vha);
6677 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6678
Quinn Tranb6faaaf2018-09-04 14:19:09 -07006679 mutex_lock(&ha->optrom_mutex);
6680 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6681 "%s.\n", __func__);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006682 if (vha->vp_idx) {
6683 qla24xx_disable_vp(vha);
6684 qla24xx_enable_vp(vha);
6685 } else {
6686 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6687 qla2xxx_wake_dpc(base_vha);
6688 qla2x00_wait_for_hba_online(base_vha);
6689 }
Quinn Tranb6faaaf2018-09-04 14:19:09 -07006690 mutex_unlock(&ha->optrom_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006691}
6692EXPORT_SYMBOL(qlt_enable_vha);
6693
6694/*
6695 * qla_tgt_disable_vha - NO LOCK HELD
6696 *
6697 * Disable Target Mode and reset the adapter
6698 */
Joern Engel55a90662014-09-16 16:23:15 -04006699static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006700{
6701 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006702 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006703 unsigned long flags;
6704
6705 if (!tgt) {
6706 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6707 "Unable to locate qla_tgt pointer from"
6708 " struct qla_hw_data\n");
6709 dump_stack();
6710 return;
6711 }
6712
6713 spin_lock_irqsave(&ha->hardware_lock, flags);
6714 qlt_clear_mode(vha);
6715 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6716
6717 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6718 qla2xxx_wake_dpc(vha);
6719 qla2x00_wait_for_hba_online(vha);
6720}
6721
6722/*
6723 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6724 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6725 * members.
6726 */
6727void
6728qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6729{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006730 vha->vha_tgt.qla_tgt = NULL;
6731
6732 mutex_init(&vha->vha_tgt.tgt_mutex);
6733 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006734
6735 qlt_clear_mode(vha);
6736
6737 /*
6738 * NOTE: Currently the value is kept the same for <24xx and
6739 * >=24xx ISPs. If it is necessary to change it,
6740 * the check should be added for specific ISPs,
6741 * assigning the value appropriately.
6742 */
6743 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006744
6745 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006746}
6747
Quinn Trane374f9f2017-12-28 12:33:31 -08006748u8
6749qlt_rff_id(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006750{
Quinn Trane374f9f2017-12-28 12:33:31 -08006751 u8 fc4_feature = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006752 /*
6753 * FC-4 Feature bit 0 indicates target functionality to the name server.
6754 */
6755 if (qla_tgt_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006756 fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006757 } else if (qla_ini_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006758 fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006759 } else if (qla_dual_mode_enabled(vha))
Quinn Trane374f9f2017-12-28 12:33:31 -08006760 fc4_feature = BIT_0 | BIT_1;
6761
6762 return fc4_feature;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006763}
6764
6765/*
6766 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6767 * @ha: HA context
6768 *
6769 * Beginning of ATIO ring has initialization control block already built
6770 * by nvram config routine.
6771 *
6772 * Returns 0 on success.
6773 */
6774void
6775qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6776{
6777 struct qla_hw_data *ha = vha->hw;
6778 uint16_t cnt;
6779 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6780
Quinn Tranead03852017-01-19 22:28:01 -08006781 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006782 return;
6783
6784 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6785 pkt->u.raw.signature = ATIO_PROCESSED;
6786 pkt++;
6787 }
6788
6789}
6790
6791/*
6792 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6793 * @ha: SCSI driver HA context
6794 */
6795void
Quinn Tran2f424b92015-12-17 14:57:07 -05006796qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006797{
6798 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006799 struct atio_from_isp *pkt;
6800 int cnt, i;
6801
Quinn Tranec7193e2017-03-15 09:48:55 -07006802 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006803 return;
6804
Quinn Tran5f355092016-12-23 18:06:11 -08006805 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6806 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006807 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6808 cnt = pkt->u.raw.entry_count;
6809
Quinn Tran5f355092016-12-23 18:06:11 -08006810 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6811 /*
6812 * This packet is corrupted. The header + payload
6813 * can not be trusted. There is no point in passing
6814 * it further up.
6815 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006816 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006817 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6818 pkt->u.isp24.fcp_hdr.s_id,
6819 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6820 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6821
6822 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006823 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6824 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006825 } else {
6826 qlt_24xx_atio_pkt_all_vps(vha,
6827 (struct atio_from_isp *)pkt, ha_locked);
6828 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006829
6830 for (i = 0; i < cnt; i++) {
6831 ha->tgt.atio_ring_index++;
6832 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6833 ha->tgt.atio_ring_index = 0;
6834 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6835 } else
6836 ha->tgt.atio_ring_ptr++;
6837
6838 pkt->u.raw.signature = ATIO_PROCESSED;
6839 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6840 }
6841 wmb();
6842 }
6843
6844 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006845 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006846}
6847
6848void
Arun Easiaa230bc2013-01-30 03:34:39 -05006849qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006850{
6851 struct qla_hw_data *ha = vha->hw;
Quinn Trand2b292c2017-12-28 12:33:17 -08006852 struct qla_msix_entry *msix = &ha->msix_entries[2];
6853 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6854
Arun Easiaa230bc2013-01-30 03:34:39 -05006855 if (!QLA_TGT_MODE_ENABLED())
6856 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006857
Arun Easiaa230bc2013-01-30 03:34:39 -05006858 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6859 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6860 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6861
Quinn Trand2b292c2017-12-28 12:33:17 -08006862 if (ha->flags.msix_enabled) {
Joe Carnuccioecc89f22019-03-12 11:08:13 -07006863 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
Quinn Trand2b292c2017-12-28 12:33:17 -08006864 if (IS_QLA2071(ha)) {
6865 /* 4 ports Baker: Enable Interrupt Handshake */
6866 icb->msix_atio = 0;
6867 icb->firmware_options_2 |= BIT_26;
6868 } else {
6869 icb->msix_atio = cpu_to_le16(msix->entry);
6870 icb->firmware_options_2 &= ~BIT_26;
6871 }
6872 ql_dbg(ql_dbg_init, vha, 0xf072,
6873 "Registering ICB vector 0x%x for atio que.\n",
6874 msix->entry);
6875 }
6876 } else {
6877 /* INTx|MSI */
Joe Carnuccioecc89f22019-03-12 11:08:13 -07006878 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
Quinn Trand2b292c2017-12-28 12:33:17 -08006879 icb->msix_atio = 0;
6880 icb->firmware_options_2 |= BIT_26;
6881 ql_dbg(ql_dbg_init, vha, 0xf072,
6882 "%s: Use INTx for ATIOQ.\n", __func__);
6883 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006884 }
6885}
6886
6887void
6888qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6889{
6890 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006891 u32 tmp;
Quinn Tran99e1b682017-06-02 09:12:03 -07006892
6893 if (!QLA_TGT_MODE_ENABLED())
6894 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006895
Quinn Tranead03852017-01-19 22:28:01 -08006896 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006897 if (!ha->tgt.saved_set) {
6898 /* We save only once */
6899 ha->tgt.saved_exchange_count = nv->exchange_count;
6900 ha->tgt.saved_firmware_options_1 =
6901 nv->firmware_options_1;
6902 ha->tgt.saved_firmware_options_2 =
6903 nv->firmware_options_2;
6904 ha->tgt.saved_firmware_options_3 =
6905 nv->firmware_options_3;
6906 ha->tgt.saved_set = 1;
6907 }
6908
Quinn Tran99e1b682017-06-02 09:12:03 -07006909 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006910 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006911 else /* dual */
Quinn Tran0645cb82018-09-11 10:18:18 -07006912 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006913
6914 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006915 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006916
6917 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006918 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006919 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006920
6921 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006922 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006923 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006924 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006925 if (ql2xtgt_tape_enable)
6926 /* Enable FC Tape support */
6927 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6928 else
6929 /* Disable FC Tape support */
6930 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6931
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006932 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006933 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006934
6935 /*
6936 * clear BIT 15 explicitly as we have seen at least
6937 * a couple of instances where this was set and this
6938 * was causing the firmware to not be initialized.
6939 */
6940 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006941 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006942 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08006943
6944 if (IS_QLA25XX(ha)) {
6945 /* Change Loop-prefer to Pt-Pt */
6946 tmp = ~(BIT_4|BIT_5|BIT_6);
6947 nv->firmware_options_2 &= cpu_to_le32(tmp);
6948 tmp = P2P << 4;
6949 nv->firmware_options_2 |= cpu_to_le32(tmp);
6950 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006951 } else {
6952 if (ha->tgt.saved_set) {
6953 nv->exchange_count = ha->tgt.saved_exchange_count;
6954 nv->firmware_options_1 =
6955 ha->tgt.saved_firmware_options_1;
6956 nv->firmware_options_2 =
6957 ha->tgt.saved_firmware_options_2;
6958 nv->firmware_options_3 =
6959 ha->tgt.saved_firmware_options_3;
6960 }
6961 return;
6962 }
6963
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006964 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006965 if (vha->flags.init_done)
6966 fc_host_supported_classes(vha->host) =
6967 FC_COS_CLASS2 | FC_COS_CLASS3;
6968
Bart Van Asschead950362015-07-09 07:24:08 -07006969 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006970 } else {
6971 if (vha->flags.init_done)
6972 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6973
Bart Van Asschead950362015-07-09 07:24:08 -07006974 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006975 }
6976}
6977
6978void
6979qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6980 struct init_cb_24xx *icb)
6981{
6982 struct qla_hw_data *ha = vha->hw;
6983
Quinn Tran481ce732015-12-17 14:57:08 -05006984 if (!QLA_TGT_MODE_ENABLED())
6985 return;
6986
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006987 if (ha->tgt.node_name_set) {
6988 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006989 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006990 }
6991}
6992
Arun Easiaa230bc2013-01-30 03:34:39 -05006993void
6994qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6995{
6996 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006997 u32 tmp;
Arun Easiaa230bc2013-01-30 03:34:39 -05006998
6999 if (!QLA_TGT_MODE_ENABLED())
7000 return;
7001
Quinn Tranead03852017-01-19 22:28:01 -08007002 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007003 if (!ha->tgt.saved_set) {
7004 /* We save only once */
7005 ha->tgt.saved_exchange_count = nv->exchange_count;
7006 ha->tgt.saved_firmware_options_1 =
7007 nv->firmware_options_1;
7008 ha->tgt.saved_firmware_options_2 =
7009 nv->firmware_options_2;
7010 ha->tgt.saved_firmware_options_3 =
7011 nv->firmware_options_3;
7012 ha->tgt.saved_set = 1;
7013 }
7014
Quinn Tran99e1b682017-06-02 09:12:03 -07007015 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08007016 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07007017 else /* dual */
Quinn Tran0645cb82018-09-11 10:18:18 -07007018 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05007019
7020 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07007021 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05007022
7023 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08007024 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07007025 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05007026 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007027 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05007028 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007029 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08007030 /*
7031 * clear BIT 15 explicitly as we have seen at
7032 * least a couple of instances where this was set
7033 * and this was causing the firmware to not be
7034 * initialized.
7035 */
7036 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04007037 if (ql2xtgt_tape_enable)
7038 /* Enable FC tape support */
7039 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7040 else
7041 /* Disable FC tape support */
7042 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7043
Arun Easiaa230bc2013-01-30 03:34:39 -05007044 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007045 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05007046 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07007047 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08007048
7049 /* Change Loop-prefer to Pt-Pt */
7050 tmp = ~(BIT_4|BIT_5|BIT_6);
7051 nv->firmware_options_2 &= cpu_to_le32(tmp);
7052 tmp = P2P << 4;
7053 nv->firmware_options_2 |= cpu_to_le32(tmp);
Arun Easiaa230bc2013-01-30 03:34:39 -05007054 } else {
7055 if (ha->tgt.saved_set) {
7056 nv->exchange_count = ha->tgt.saved_exchange_count;
7057 nv->firmware_options_1 =
7058 ha->tgt.saved_firmware_options_1;
7059 nv->firmware_options_2 =
7060 ha->tgt.saved_firmware_options_2;
7061 nv->firmware_options_3 =
7062 ha->tgt.saved_firmware_options_3;
7063 }
7064 return;
7065 }
7066
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007067 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007068 if (vha->flags.init_done)
7069 fc_host_supported_classes(vha->host) =
7070 FC_COS_CLASS2 | FC_COS_CLASS3;
7071
Bart Van Asschead950362015-07-09 07:24:08 -07007072 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05007073 } else {
7074 if (vha->flags.init_done)
7075 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7076
Bart Van Asschead950362015-07-09 07:24:08 -07007077 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05007078 }
7079}
7080
7081void
7082qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7083 struct init_cb_81xx *icb)
7084{
7085 struct qla_hw_data *ha = vha->hw;
7086
7087 if (!QLA_TGT_MODE_ENABLED())
7088 return;
7089
7090 if (ha->tgt.node_name_set) {
7091 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07007092 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05007093 }
7094}
7095
7096void
7097qlt_83xx_iospace_config(struct qla_hw_data *ha)
7098{
7099 if (!QLA_TGT_MODE_ENABLED())
7100 return;
7101
7102 ha->msix_count += 1; /* For ATIO Q */
7103}
7104
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007105
7106void
7107qlt_modify_vp_config(struct scsi_qla_host *vha,
7108 struct vp_config_entry_24xx *vpmod)
7109{
Quinn Tranead03852017-01-19 22:28:01 -08007110 /* enable target mode. Bit5 = 1 => disable */
7111 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007112 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08007113
Quinn Tranead03852017-01-19 22:28:01 -08007114 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08007115 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007116 vpmod->options_idx1 &= ~BIT_4;
7117}
7118
7119void
7120qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7121{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007122 int rc;
7123
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007124 if (!QLA_TGT_MODE_ENABLED())
7125 return;
7126
Joe Carnuccioecc89f22019-03-12 11:08:13 -07007127 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
7128 IS_QLA28XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007129 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7130 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7131 } else {
7132 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7133 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7134 }
7135
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08007136 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7137 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08007138
7139 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7140 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7141 qlt_unknown_atio_work_fn);
7142
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007143 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007144
7145 rc = btree_init32(&ha->tgt.host_map);
7146 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007147 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007148 "Unable to initialize ha->host_map btree\n");
7149
7150 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007151}
7152
Arun Easiaa230bc2013-01-30 03:34:39 -05007153irqreturn_t
7154qla83xx_msix_atio_q(int irq, void *dev_id)
7155{
7156 struct rsp_que *rsp;
7157 scsi_qla_host_t *vha;
7158 struct qla_hw_data *ha;
7159 unsigned long flags;
7160
7161 rsp = (struct rsp_que *) dev_id;
7162 ha = rsp->hw;
7163 vha = pci_get_drvdata(ha->pdev);
7164
Quinn Tran2f424b92015-12-17 14:57:07 -05007165 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007166
Quinn Tran2f424b92015-12-17 14:57:07 -05007167 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05007168
Quinn Tran2f424b92015-12-17 14:57:07 -05007169 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007170
7171 return IRQ_HANDLED;
7172}
7173
Quinn Tran2f424b92015-12-17 14:57:07 -05007174static void
7175qlt_handle_abts_recv_work(struct work_struct *work)
7176{
7177 struct qla_tgt_sess_op *op = container_of(work,
7178 struct qla_tgt_sess_op, work);
7179 scsi_qla_host_t *vha = op->vha;
7180 struct qla_hw_data *ha = vha->hw;
7181 unsigned long flags;
7182
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007183 if (qla2x00_reset_active(vha) ||
7184 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05007185 return;
7186
7187 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7188 qlt_24xx_process_atio_queue(vha, 0);
7189 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7190
7191 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07007192 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05007193 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07007194
7195 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05007196}
7197
7198void
Quinn Tran82de8022017-06-13 20:47:17 -07007199qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7200 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05007201{
7202 struct qla_tgt_sess_op *op;
7203
7204 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7205
7206 if (!op) {
7207 /* do not reach for ATIO queue here. This is best effort err
7208 * recovery at this point.
7209 */
Quinn Tran82de8022017-06-13 20:47:17 -07007210 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05007211 return;
7212 }
7213
7214 memcpy(&op->atio, pkt, sizeof(*pkt));
7215 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007216 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07007217 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05007218 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7219 queue_work(qla_tgt_wq, &op->work);
7220 return;
7221}
7222
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007223int
7224qlt_mem_alloc(struct qla_hw_data *ha)
7225{
7226 if (!QLA_TGT_MODE_ENABLED())
7227 return 0;
7228
Kees Cook6396bb22018-06-12 14:03:40 -07007229 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7230 sizeof(struct qla_tgt_vp_map),
7231 GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007232 if (!ha->tgt.tgt_vp_map)
7233 return -ENOMEM;
7234
7235 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7236 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7237 &ha->tgt.atio_dma, GFP_KERNEL);
7238 if (!ha->tgt.atio_ring) {
7239 kfree(ha->tgt.tgt_vp_map);
7240 return -ENOMEM;
7241 }
7242 return 0;
7243}
7244
7245void
7246qlt_mem_free(struct qla_hw_data *ha)
7247{
7248 if (!QLA_TGT_MODE_ENABLED())
7249 return;
7250
7251 if (ha->tgt.atio_ring) {
7252 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7253 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7254 ha->tgt.atio_dma);
7255 }
Bart Van Assche5365bf92019-04-17 14:44:22 -07007256 ha->tgt.atio_ring = NULL;
7257 ha->tgt.atio_dma = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007258 kfree(ha->tgt.tgt_vp_map);
Bart Van Assche5365bf92019-04-17 14:44:22 -07007259 ha->tgt.tgt_vp_map = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007260}
7261
7262/* vport_slock to be held by the caller */
7263void
7264qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7265{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007266 void *slot;
7267 u32 key;
7268 int rc;
7269
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007270 if (!QLA_TGT_MODE_ENABLED())
7271 return;
7272
Quinn Tran482c9dc2017-03-15 09:48:54 -07007273 key = vha->d_id.b24;
7274
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007275 switch (cmd) {
7276 case SET_VP_IDX:
7277 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7278 break;
7279 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07007280 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7281 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007282 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007283 "Save vha in host_map %p %06x\n", vha, key);
7284 rc = btree_insert32(&vha->hw->tgt.host_map,
7285 key, vha, GFP_ATOMIC);
7286 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007287 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007288 "Unable to insert s_id into host_map: %06x\n",
7289 key);
7290 return;
7291 }
Quinn Tran83548fe2017-06-02 09:12:01 -07007292 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7293 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007294 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007295 break;
7296 case RESET_VP_IDX:
7297 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7298 break;
7299 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07007300 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007301 "clear vha in host_map %p %06x\n", vha, key);
7302 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7303 if (slot)
7304 btree_remove32(&vha->hw->tgt.host_map, key);
7305 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007306 break;
7307 }
7308}
7309
Quinn Tran482c9dc2017-03-15 09:48:54 -07007310void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7311{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007312
7313 if (!vha->d_id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007314 vha->d_id = id;
7315 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007316 } else if (vha->d_id.b24 != id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007317 qlt_update_vp_map(vha, RESET_AL_PA);
7318 vha->d_id = id;
7319 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007320 }
7321}
7322
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007323static int __init qlt_parse_ini_mode(void)
7324{
7325 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7326 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7327 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7328 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7329 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7330 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007331 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7332 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007333 else
7334 return false;
7335
7336 return true;
7337}
7338
7339int __init qlt_init(void)
7340{
7341 int ret;
7342
Bart Van Asschebc044592019-04-17 14:44:37 -07007343 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7344 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7345
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007346 if (!qlt_parse_ini_mode()) {
7347 ql_log(ql_log_fatal, NULL, 0xe06b,
7348 "qlt_parse_ini_mode() failed\n");
7349 return -EINVAL;
7350 }
7351
7352 if (!QLA_TGT_MODE_ENABLED())
7353 return 0;
7354
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007355 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7356 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7357 qla_tgt_mgmt_cmd), 0, NULL);
7358 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007359 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007360 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007361 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007362 }
7363
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007364 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007365 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7366 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007367
7368 if (!qla_tgt_plogi_cachep) {
7369 ql_log(ql_log_fatal, NULL, 0xe06d,
7370 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7371 ret = -ENOMEM;
7372 goto out_mgmt_cmd_cachep;
7373 }
7374
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007375 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7376 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7377 if (!qla_tgt_mgmt_cmd_mempool) {
7378 ql_log(ql_log_fatal, NULL, 0xe06e,
7379 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7380 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007381 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007382 }
7383
7384 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7385 if (!qla_tgt_wq) {
7386 ql_log(ql_log_fatal, NULL, 0xe06f,
7387 "alloc_workqueue for qla_tgt_wq failed\n");
7388 ret = -ENOMEM;
7389 goto out_cmd_mempool;
7390 }
7391 /*
7392 * Return 1 to signal that initiator-mode is being disabled
7393 */
7394 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7395
7396out_cmd_mempool:
7397 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007398out_plogi_cachep:
7399 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007400out_mgmt_cmd_cachep:
7401 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007402 return ret;
7403}
7404
7405void qlt_exit(void)
7406{
7407 if (!QLA_TGT_MODE_ENABLED())
7408 return;
7409
7410 destroy_workqueue(qla_tgt_wq);
7411 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007412 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007413 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007414}