blob: 08f22ed0c71906357ee6ffaf21f8f7d6d58ef1d8 [file] [log] [blame]
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001/*
2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
3 *
4 * based on qla2x00t.c code:
5 *
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
10 *
11 * Forward port and refactoring to modern qla2xxx and target/configfs
12 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -070013 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
18 * of the License.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/types.h>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040029#include <linux/blkdev.h>
30#include <linux/interrupt.h>
31#include <linux/pci.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/workqueue.h>
35#include <asm/unaligned.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_tcq.h>
39#include <target/target_core_base.h>
40#include <target/target_core_fabric.h>
41
42#include "qla_def.h"
43#include "qla_target.h"
44
Arun Easid154f352014-09-25 06:14:48 -040045static int ql2xtgt_tape_enable;
46module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
49
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040050static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51module_param(qlini_mode, charp, S_IRUGO);
52MODULE_PARM_DESC(qlini_mode,
53 "Determines when initiator mode will be enabled. Possible values: "
54 "\"exclusive\" - initiator mode will be enabled on load, "
55 "disabled on enabling target mode and then on disabling target mode "
56 "enabled back; "
57 "\"disabled\" - initiator mode will never be enabled; "
Quinn Tranead03852017-01-19 22:28:01 -080058 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
59 "when ready "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040060 "\"enabled\" (default) - initiator mode will always stay enabled.");
61
Quinn Tran99e1b682017-06-02 09:12:03 -070062static int ql_dm_tgt_ex_pct = 0;
Quinn Tranead03852017-01-19 22:28:01 -080063module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
67 "for Target mode.");
68
Quinn Tran09620ee2017-06-13 20:47:20 -070069int ql2xuctrlirq = 1;
70module_param(ql2xuctrlirq, int, 0644);
71MODULE_PARM_DESC(ql2xuctrlirq,
72 "User to control IRQ placement via smp_affinity."
73 "Valid with qlini_mode=disabled."
74 "1(default): enable");
75
Arun Easiaa230bc2013-01-30 03:34:39 -050076int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040077
Quinn Tran7cf95f72017-12-28 12:33:28 -080078static int qla_sam_status = SAM_STAT_BUSY;
79static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
Quinn Tran33e79972014-09-25 06:14:55 -040080
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040081/*
82 * From scsi/fc/fc_fcp.h
83 */
84enum fcp_resp_rsp_codes {
85 FCP_TMF_CMPL = 0,
86 FCP_DATA_LEN_INVALID = 1,
87 FCP_CMND_FIELDS_INVALID = 2,
88 FCP_DATA_PARAM_MISMATCH = 3,
89 FCP_TMF_REJECTED = 4,
90 FCP_TMF_FAILED = 5,
91 FCP_TMF_INVALID_LUN = 9,
92};
93
94/*
95 * fc_pri_ta from scsi/fc/fc_fcp.h
96 */
97#define FCP_PTA_SIMPLE 0 /* simple task attribute */
98#define FCP_PTA_HEADQ 1 /* head of queue task attribute */
99#define FCP_PTA_ORDERED 2 /* ordered task attribute */
Masanari Iida6efb3c0a2012-10-26 22:10:54 +0900100#define FCP_PTA_ACA 4 /* auto. contingent allegiance */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400101#define FCP_PTA_MASK 7 /* mask for task attribute field */
102#define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
103#define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
104
105/*
106 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
107 * must be called under HW lock and could unlock/lock it inside.
108 * It isn't an issue, since in the current implementation on the time when
109 * those functions are called:
110 *
111 * - Either context is IRQ and only IRQ handler can modify HW data,
112 * including rings related fields,
113 *
114 * - Or access to target mode variables from struct qla_tgt doesn't
115 * cross those functions boundaries, except tgt_stop, which
116 * additionally protected by irq_cmd_count.
117 */
118/* Predefs for callbacks handed to qla2xxx LLD */
119static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500120 struct atio_from_isp *pkt, uint8_t);
Quinn Tran82de8022017-06-13 20:47:17 -0700121static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
122 response_t *pkt);
Quinn Tran5d964832017-01-19 22:27:59 -0800123static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400124 int fn, void *iocb, int flags);
Quinn Tran82de8022017-06-13 20:47:17 -0700125static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
Quinn Trana07100e2015-12-07 19:48:57 -0500126 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
Quinn Tran33e79972014-09-25 06:14:55 -0400127static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
128 struct atio_from_isp *atio, uint16_t status, int qfull);
Joern Engel55a90662014-09-16 16:23:15 -0400129static void qlt_disable_vha(struct scsi_qla_host *vha);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400130static void qlt_clear_tgt_db(struct qla_tgt *tgt);
Quinn Tran82de8022017-06-13 20:47:17 -0700131static void qlt_send_notify_ack(struct qla_qpair *qpair,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400132 struct imm_ntfy_from_isp *ntfy,
133 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
134 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500135static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
136 struct imm_ntfy_from_isp *imm, int ha_locked);
Quinn Tran726b8542017-01-19 22:28:00 -0800137static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
138 fc_port_t *fcport, bool local);
139void qlt_unreg_sess(struct fc_port *sess);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700140static void qlt_24xx_handle_abts(struct scsi_qla_host *,
141 struct abts_recv_from_24xx *);
Quinn Tran82de8022017-06-13 20:47:17 -0700142static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
143 uint16_t);
Quinn Tran6b0431d2018-09-04 14:19:13 -0700144static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
145static inline uint32_t qlt_make_handle(struct qla_qpair *);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700146
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400147/*
148 * Global Variables
149 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400150static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
Quinn Tranb5d15312017-08-30 10:16:49 -0700151struct kmem_cache *qla_tgt_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400152static mempool_t *qla_tgt_mgmt_cmd_mempool;
153static struct workqueue_struct *qla_tgt_wq;
154static DEFINE_MUTEX(qla_tgt_mutex);
155static LIST_HEAD(qla_tgt_glist);
156
Quinn Tranbe251522017-03-15 09:48:49 -0700157static const char *prot_op_str(u32 prot_op)
158{
159 switch (prot_op) {
160 case TARGET_PROT_NORMAL: return "NORMAL";
161 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
162 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
163 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
164 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
165 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
166 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
167 default: return "UNKNOWN";
168 }
169}
170
Alexei Potashnikdf673272015-07-14 16:00:46 -0400171/* This API intentionally takes dest as a parameter, rather than returning
172 * int value to avoid caller forgetting to issue wmb() after the store */
173void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
174{
175 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
176 *dest = atomic_inc_return(&base_vha->generation_tick);
177 /* memory barrier */
178 wmb();
179}
180
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400181/* Might release hw lock, then reaquire!! */
182static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
183{
184 /* Send marker if required */
185 if (unlikely(vha->marker_needed != 0)) {
186 int rc = qla2x00_issue_marker(vha, vha_locked);
Bart Van Asschebd432bb2019-04-11 14:53:17 -0700187
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400188 if (rc != QLA_SUCCESS) {
189 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
190 "qla_target(%d): issue_marker() failed\n",
191 vha->vp_idx);
192 }
193 return rc;
194 }
195 return QLA_SUCCESS;
196}
197
198static inline
199struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
200 uint8_t *d_id)
201{
Quinn Tran482c9dc2017-03-15 09:48:54 -0700202 struct scsi_qla_host *host;
203 uint32_t key = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400204
Quinn Tran482c9dc2017-03-15 09:48:54 -0700205 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
206 (vha->d_id.b.al_pa == d_id[2]))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400207 return vha;
208
Quinn Tran482c9dc2017-03-15 09:48:54 -0700209 key = (uint32_t)d_id[0] << 16;
210 key |= (uint32_t)d_id[1] << 8;
211 key |= (uint32_t)d_id[2];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400212
Quinn Tran482c9dc2017-03-15 09:48:54 -0700213 host = btree_lookup32(&vha->hw->tgt.host_map, key);
214 if (!host)
Quinn Tran604e2e52017-12-28 12:33:37 -0800215 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
Quinn Tran83548fe2017-06-02 09:12:01 -0700216 "Unable to find host %06x\n", key);
Quinn Tran482c9dc2017-03-15 09:48:54 -0700217
218 return host;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400219}
220
221static inline
222struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
223 uint16_t vp_idx)
224{
225 struct qla_hw_data *ha = vha->hw;
226
227 if (vha->vp_idx == vp_idx)
228 return vha;
229
230 BUG_ON(ha->tgt.tgt_vp_map == NULL);
231 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
232 return ha->tgt.tgt_vp_map[vp_idx].vha;
233
234 return NULL;
235}
236
Quinn Tran33e79972014-09-25 06:14:55 -0400237static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
238{
239 unsigned long flags;
240
241 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
242
243 vha->hw->tgt.num_pend_cmds++;
Joe Carnucciofc90ada2016-07-06 11:14:23 -0400244 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
245 vha->qla_stats.stat_max_pend_cmds =
Quinn Tran33e79972014-09-25 06:14:55 -0400246 vha->hw->tgt.num_pend_cmds;
247 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
248}
249static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
250{
251 unsigned long flags;
252
253 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
254 vha->hw->tgt.num_pend_cmds--;
255 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
256}
257
Quinn Tran41dc5292017-01-19 22:28:03 -0800258
259static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
Quinn Tran82de8022017-06-13 20:47:17 -0700260 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran41dc5292017-01-19 22:28:03 -0800261{
262 struct qla_tgt_sess_op *u;
263 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
264 unsigned long flags;
265
266 if (tgt->tgt_stop) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700267 ql_dbg(ql_dbg_async, vha, 0x502c,
268 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
269 vha->vp_idx);
Quinn Tran41dc5292017-01-19 22:28:03 -0800270 goto out_term;
271 }
272
273 u = kzalloc(sizeof(*u), GFP_ATOMIC);
Quinn Tran83548fe2017-06-02 09:12:01 -0700274 if (u == NULL)
Quinn Tran41dc5292017-01-19 22:28:03 -0800275 goto out_term;
Quinn Tran41dc5292017-01-19 22:28:03 -0800276
277 u->vha = vha;
278 memcpy(&u->atio, atio, sizeof(*atio));
279 INIT_LIST_HEAD(&u->cmd_list);
280
281 spin_lock_irqsave(&vha->cmd_list_lock, flags);
282 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
283 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
284
285 schedule_delayed_work(&vha->unknown_atio_work, 1);
286
287out:
288 return;
289
290out_term:
Quinn Tran82de8022017-06-13 20:47:17 -0700291 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800292 goto out;
293}
294
295static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
296 uint8_t ha_locked)
297{
298 struct qla_tgt_sess_op *u, *t;
299 scsi_qla_host_t *host;
300 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
301 unsigned long flags;
302 uint8_t queued = 0;
303
304 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
305 if (u->aborted) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700306 ql_dbg(ql_dbg_async, vha, 0x502e,
307 "Freeing unknown %s %p, because of Abort\n",
Quinn Tran41dc5292017-01-19 22:28:03 -0800308 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700309 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
310 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800311 goto abort;
312 }
313
314 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
315 if (host != NULL) {
Quinn Tran604e2e52017-12-28 12:33:37 -0800316 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
Quinn Tran83548fe2017-06-02 09:12:01 -0700317 "Requeuing unknown ATIO_TYPE7 %p\n", u);
Quinn Tran41dc5292017-01-19 22:28:03 -0800318 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
319 } else if (tgt->tgt_stop) {
Quinn Tran604e2e52017-12-28 12:33:37 -0800320 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
Quinn Tran83548fe2017-06-02 09:12:01 -0700321 "Freeing unknown %s %p, because tgt is being stopped\n",
322 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700323 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
324 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800325 } else {
Quinn Tran604e2e52017-12-28 12:33:37 -0800326 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
Quinn Tran83548fe2017-06-02 09:12:01 -0700327 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
Quinn Tran41dc5292017-01-19 22:28:03 -0800328 if (!queued) {
329 queued = 1;
330 schedule_delayed_work(&vha->unknown_atio_work,
331 1);
332 }
333 continue;
334 }
335
336abort:
337 spin_lock_irqsave(&vha->cmd_list_lock, flags);
338 list_del(&u->cmd_list);
339 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
340 kfree(u);
341 }
342}
343
344void qlt_unknown_atio_work_fn(struct work_struct *work)
345{
346 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
347 struct scsi_qla_host, unknown_atio_work);
348
349 qlt_try_to_dequeue_unknown_atios(vha, 0);
350}
351
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500352static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500353 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400354{
Quinn Tranf83adb62014-04-11 16:54:43 -0400355 ql_dbg(ql_dbg_tgt, vha, 0xe072,
356 "%s: qla_target(%d): type %x ox_id %04x\n",
357 __func__, vha->vp_idx, atio->u.raw.entry_type,
358 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
359
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400360 switch (atio->u.raw.entry_type) {
361 case ATIO_TYPE7:
362 {
363 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
364 atio->u.isp24.fcp_hdr.d_id);
365 if (unlikely(NULL == host)) {
366 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
367 "qla_target(%d): Received ATIO_TYPE7 "
368 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
369 atio->u.isp24.fcp_hdr.d_id[0],
370 atio->u.isp24.fcp_hdr.d_id[1],
371 atio->u.isp24.fcp_hdr.d_id[2]);
Quinn Tran41dc5292017-01-19 22:28:03 -0800372
373
374 qlt_queue_unknown_atio(vha, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400375 break;
376 }
Quinn Tran41dc5292017-01-19 22:28:03 -0800377 if (unlikely(!list_empty(&vha->unknown_atio_list)))
378 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
379
Quinn Tran2f424b92015-12-17 14:57:07 -0500380 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400381 break;
382 }
383
384 case IMMED_NOTIFY_TYPE:
385 {
386 struct scsi_qla_host *host = vha;
387 struct imm_ntfy_from_isp *entry =
388 (struct imm_ntfy_from_isp *)atio;
389
Quinn Tran82de8022017-06-13 20:47:17 -0700390 qlt_issue_marker(vha, ha_locked);
391
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400392 if ((entry->u.isp24.vp_index != 0xFF) &&
393 (entry->u.isp24.nport_handle != 0xFFFF)) {
394 host = qlt_find_host_by_vp_idx(vha,
395 entry->u.isp24.vp_index);
396 if (unlikely(!host)) {
397 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
398 "qla_target(%d): Received "
399 "ATIO (IMMED_NOTIFY_TYPE) "
400 "with unknown vp_index %d\n",
401 vha->vp_idx, entry->u.isp24.vp_index);
402 break;
403 }
404 }
Quinn Tran2f424b92015-12-17 14:57:07 -0500405 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400406 break;
407 }
408
Quinn Tran41dc5292017-01-19 22:28:03 -0800409 case VP_RPT_ID_IOCB_TYPE:
410 qla24xx_report_id_acquisition(vha,
411 (struct vp_rpt_id_entry_24xx *)atio);
412 break;
413
414 case ABTS_RECV_24XX:
415 {
416 struct abts_recv_from_24xx *entry =
417 (struct abts_recv_from_24xx *)atio;
418 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
419 entry->vp_index);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700420 unsigned long flags;
421
Quinn Tran41dc5292017-01-19 22:28:03 -0800422 if (unlikely(!host)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700423 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
Quinn Tran41dc5292017-01-19 22:28:03 -0800424 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
425 "received, with unknown vp_index %d\n",
426 vha->vp_idx, entry->vp_index);
427 break;
428 }
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700429 if (!ha_locked)
430 spin_lock_irqsave(&host->hw->hardware_lock, flags);
431 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
432 if (!ha_locked)
433 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -0800434 break;
Quinn Tran41dc5292017-01-19 22:28:03 -0800435 }
436
437 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
438
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400439 default:
440 ql_dbg(ql_dbg_tgt, vha, 0xe040,
441 "qla_target(%d): Received unknown ATIO atio "
442 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
443 break;
444 }
445
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500446 return false;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400447}
448
Quinn Tran82de8022017-06-13 20:47:17 -0700449void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
450 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400451{
452 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -0400453 case CTIO_CRC2:
454 ql_dbg(ql_dbg_tgt, vha, 0xe073,
455 "qla_target(%d):%s: CRC2 Response pkt\n",
456 vha->vp_idx, __func__);
Bart Van Assche81881862017-12-07 16:02:46 -0800457 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400458 case CTIO_TYPE7:
459 {
460 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
461 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
462 entry->vp_index);
463 if (unlikely(!host)) {
464 ql_dbg(ql_dbg_tgt, vha, 0xe041,
465 "qla_target(%d): Response pkt (CTIO_TYPE7) "
466 "received, with unknown vp_index %d\n",
467 vha->vp_idx, entry->vp_index);
468 break;
469 }
Quinn Tran82de8022017-06-13 20:47:17 -0700470 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400471 break;
472 }
473
474 case IMMED_NOTIFY_TYPE:
475 {
476 struct scsi_qla_host *host = vha;
477 struct imm_ntfy_from_isp *entry =
478 (struct imm_ntfy_from_isp *)pkt;
479
480 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
481 if (unlikely(!host)) {
482 ql_dbg(ql_dbg_tgt, vha, 0xe042,
483 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
484 "received, with unknown vp_index %d\n",
485 vha->vp_idx, entry->u.isp24.vp_index);
486 break;
487 }
Quinn Tran82de8022017-06-13 20:47:17 -0700488 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400489 break;
490 }
491
492 case NOTIFY_ACK_TYPE:
493 {
494 struct scsi_qla_host *host = vha;
495 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
496
497 if (0xFF != entry->u.isp24.vp_index) {
498 host = qlt_find_host_by_vp_idx(vha,
499 entry->u.isp24.vp_index);
500 if (unlikely(!host)) {
501 ql_dbg(ql_dbg_tgt, vha, 0xe043,
502 "qla_target(%d): Response "
503 "pkt (NOTIFY_ACK_TYPE) "
504 "received, with unknown "
505 "vp_index %d\n", vha->vp_idx,
506 entry->u.isp24.vp_index);
507 break;
508 }
509 }
Quinn Tran82de8022017-06-13 20:47:17 -0700510 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400511 break;
512 }
513
514 case ABTS_RECV_24XX:
515 {
516 struct abts_recv_from_24xx *entry =
517 (struct abts_recv_from_24xx *)pkt;
518 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
519 entry->vp_index);
520 if (unlikely(!host)) {
521 ql_dbg(ql_dbg_tgt, vha, 0xe044,
522 "qla_target(%d): Response pkt "
523 "(ABTS_RECV_24XX) received, with unknown "
524 "vp_index %d\n", vha->vp_idx, entry->vp_index);
525 break;
526 }
Quinn Tran82de8022017-06-13 20:47:17 -0700527 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400528 break;
529 }
530
531 case ABTS_RESP_24XX:
532 {
533 struct abts_resp_to_24xx *entry =
534 (struct abts_resp_to_24xx *)pkt;
535 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
536 entry->vp_index);
537 if (unlikely(!host)) {
538 ql_dbg(ql_dbg_tgt, vha, 0xe045,
539 "qla_target(%d): Response pkt "
540 "(ABTS_RECV_24XX) received, with unknown "
541 "vp_index %d\n", vha->vp_idx, entry->vp_index);
542 break;
543 }
Quinn Tran82de8022017-06-13 20:47:17 -0700544 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400545 break;
546 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400547 default:
Quinn Tran82de8022017-06-13 20:47:17 -0700548 qlt_response_pkt(vha, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400549 break;
550 }
551
552}
553
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500554/*
555 * All qlt_plogi_ack_t operations are protected by hardware_lock
556 */
Quinn Tran726b8542017-01-19 22:28:00 -0800557static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
558 struct imm_ntfy_from_isp *ntfy, int type)
559{
560 struct qla_work_evt *e;
Bart Van Asschebd432bb2019-04-11 14:53:17 -0700561
Quinn Tran726b8542017-01-19 22:28:00 -0800562 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
563 if (!e)
564 return QLA_FUNCTION_FAILED;
565
566 e->u.nack.fcport = fcport;
567 e->u.nack.type = type;
568 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
569 return qla2x00_post_work(vha, e);
570}
571
572static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800573void qla2x00_async_nack_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800574{
Quinn Tran726b8542017-01-19 22:28:00 -0800575 struct srb *sp = (struct srb *)s;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800576 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800577 unsigned long flags;
578
Quinn Tran83548fe2017-06-02 09:12:01 -0700579 ql_dbg(ql_dbg_disc, vha, 0x20f2,
580 "Async done-%s res %x %8phC type %d\n",
581 sp->name, res, sp->fcport->port_name, sp->type);
Quinn Tran726b8542017-01-19 22:28:00 -0800582
583 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
584 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -0700585 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran726b8542017-01-19 22:28:00 -0800586
587 switch (sp->type) {
588 case SRB_NACK_PLOGI:
589 sp->fcport->login_gen++;
590 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
591 sp->fcport->logout_on_delete = 1;
Quinn Tran5b334692017-03-15 09:48:48 -0700592 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
Quinn Tran35158322017-08-30 10:16:50 -0700593 sp->fcport->send_els_logo = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800594 break;
595
596 case SRB_NACK_PRLI:
597 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
598 sp->fcport->deleted = 0;
Quinn Tran35158322017-08-30 10:16:50 -0700599 sp->fcport->send_els_logo = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800600
601 if (!sp->fcport->login_succ &&
602 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
603 sp->fcport->login_succ = 1;
604
605 vha->fcport_count++;
Quinn Tran0aca7782018-09-04 14:19:16 -0700606 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Trancd4ed6b2018-08-31 11:24:31 -0700607 qla24xx_sched_upd_fcport(sp->fcport);
Quinn Tran0aca7782018-09-04 14:19:16 -0700608 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tran29528492018-05-01 09:01:45 -0700609 } else {
610 sp->fcport->login_retry = 0;
611 sp->fcport->disc_state = DSC_LOGIN_COMPLETE;
612 sp->fcport->deleted = 0;
613 sp->fcport->logout_on_delete = 1;
Quinn Tran726b8542017-01-19 22:28:00 -0800614 }
615 break;
616
617 case SRB_NACK_LOGO:
618 sp->fcport->login_gen++;
619 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
620 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
621 break;
622 }
623 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
624
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800625 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800626}
627
628int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
629 struct imm_ntfy_from_isp *ntfy, int type)
630{
631 int rval = QLA_FUNCTION_FAILED;
632 srb_t *sp;
633 char *c = NULL;
634
635 fcport->flags |= FCF_ASYNC_SENT;
636 switch (type) {
637 case SRB_NACK_PLOGI:
638 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
639 c = "PLOGI";
640 break;
641 case SRB_NACK_PRLI:
642 fcport->fw_login_state = DSC_LS_PRLI_PEND;
Quinn Tranec7193e2017-03-15 09:48:55 -0700643 fcport->deleted = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800644 c = "PRLI";
645 break;
646 case SRB_NACK_LOGO:
647 fcport->fw_login_state = DSC_LS_LOGO_PEND;
648 c = "LOGO";
649 break;
650 }
651
652 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
653 if (!sp)
654 goto done;
655
656 sp->type = type;
657 sp->name = "nack";
658
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000659 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
Quinn Tran726b8542017-01-19 22:28:00 -0800660 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
661
662 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
Quinn Tran726b8542017-01-19 22:28:00 -0800663 sp->done = qla2x00_async_nack_sp_done;
664
Quinn Tran83548fe2017-06-02 09:12:01 -0700665 ql_dbg(ql_dbg_disc, vha, 0x20f4,
666 "Async-%s %8phC hndl %x %s\n",
667 sp->name, fcport->port_name, sp->handle, c);
Quinn Tran726b8542017-01-19 22:28:00 -0800668
Bill Kuzejaf233e8c2019-02-14 10:52:29 -0500669 rval = qla2x00_start_sp(sp);
670 if (rval != QLA_SUCCESS)
671 goto done_free_sp;
672
Quinn Tran726b8542017-01-19 22:28:00 -0800673 return rval;
674
675done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800676 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800677done:
678 fcport->flags &= ~FCF_ASYNC_SENT;
679 return rval;
680}
681
682void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
683{
684 fc_port_t *t;
685 unsigned long flags;
686
687 switch (e->u.nack.type) {
688 case SRB_NACK_PRLI:
Quinn Tran1021f0b2019-01-24 23:23:45 -0800689 t = e->u.nack.fcport;
690 flush_work(&t->del_work);
691 flush_work(&t->free_work);
Quinn Tran726b8542017-01-19 22:28:00 -0800692 mutex_lock(&vha->vha_tgt.tgt_mutex);
693 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
694 mutex_unlock(&vha->vha_tgt.tgt_mutex);
695 if (t) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700696 ql_log(ql_log_info, vha, 0xd034,
Quinn Tran726b8542017-01-19 22:28:00 -0800697 "%s create sess success %p", __func__, t);
698 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
699 /* create sess has an extra kref */
700 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
701 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
702 }
703 break;
704 }
705 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
Bart Van Assche845bbb02019-04-11 14:53:18 -0700706 (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
Quinn Tran726b8542017-01-19 22:28:00 -0800707}
708
709void qla24xx_delete_sess_fn(struct work_struct *work)
710{
711 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
712 struct qla_hw_data *ha = fcport->vha->hw;
713 unsigned long flags;
714
715 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
716
717 if (fcport->se_sess) {
718 ha->tgt.tgt_ops->shutdown_sess(fcport);
719 ha->tgt.tgt_ops->put_sess(fcport);
720 } else {
721 qlt_unreg_sess(fcport);
722 }
723 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
724}
725
726/*
727 * Called from qla2x00_reg_remote_port()
728 */
729void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
730{
731 struct qla_hw_data *ha = vha->hw;
732 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
733 struct fc_port *sess = fcport;
734 unsigned long flags;
735
736 if (!vha->hw->tgt.tgt_ops)
737 return;
738
739 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
740 if (tgt->tgt_stop) {
741 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
742 return;
743 }
744
745 if (fcport->disc_state == DSC_DELETE_PEND) {
746 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
747 return;
748 }
749
750 if (!sess->se_sess) {
751 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
752
753 mutex_lock(&vha->vha_tgt.tgt_mutex);
754 sess = qlt_create_sess(vha, fcport, false);
755 mutex_unlock(&vha->vha_tgt.tgt_mutex);
756
757 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
758 } else {
759 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
760 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
761 return;
762 }
763
764 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700765 ql_dbg(ql_dbg_disc, vha, 0x2107,
Quinn Tran726b8542017-01-19 22:28:00 -0800766 "%s: kref_get fail sess %8phC \n",
767 __func__, sess->port_name);
768 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
769 return;
770 }
771
772 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
773 "qla_target(%u): %ssession for port %8phC "
774 "(loop ID %d) reappeared\n", vha->vp_idx,
775 sess->local ? "local " : "", sess->port_name, sess->loop_id);
776
777 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
778 "Reappeared sess %p\n", sess);
779
780 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
781 fcport->loop_id,
782 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
783 }
784
785 if (sess && sess->local) {
786 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
787 "qla_target(%u): local session for "
788 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
789 fcport->port_name, sess->loop_id);
790 sess->local = 0;
791 }
792 ha->tgt.tgt_ops->put_sess(sess);
793 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
794}
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500795
796/*
797 * This is a zero-base ref-counting solution, since hardware_lock
798 * guarantees that ref_count is not modified concurrently.
799 * Upon successful return content of iocb is undefined
800 */
Quinn Tran5d964832017-01-19 22:27:59 -0800801static struct qlt_plogi_ack_t *
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500802qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
803 struct imm_ntfy_from_isp *iocb)
804{
Quinn Tran5d964832017-01-19 22:27:59 -0800805 struct qlt_plogi_ack_t *pla;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500806
807 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
808 if (pla->id.b24 == id->b24) {
Quinn Tran48acad02018-08-02 13:16:44 -0700809 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
810 "%s %d %8phC Term INOT due to new INOT",
811 __func__, __LINE__,
812 pla->iocb.u.isp24.port_name);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500813 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
Quinn Tran5d964832017-01-19 22:27:59 -0800814 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500815 return pla;
816 }
817 }
818
819 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
820 if (!pla) {
821 ql_dbg(ql_dbg_async, vha, 0x5088,
822 "qla_target(%d): Allocation of plogi_ack failed\n",
823 vha->vp_idx);
824 return NULL;
825 }
826
Quinn Tran5d964832017-01-19 22:27:59 -0800827 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500828 pla->id = *id;
829 list_add_tail(&pla->list, &vha->plogi_ack_list);
830
831 return pla;
832}
833
Quinn Tran726b8542017-01-19 22:28:00 -0800834void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -0800835 struct qlt_plogi_ack_t *pla)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500836{
Quinn Tran5d964832017-01-19 22:27:59 -0800837 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Quinn Tran726b8542017-01-19 22:28:00 -0800838 port_id_t port_id;
839 uint16_t loop_id;
840 fc_port_t *fcport = pla->fcport;
841
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500842 BUG_ON(!pla->ref_count);
843 pla->ref_count--;
844
845 if (pla->ref_count)
846 return;
847
Quinn Tran726b8542017-01-19 22:28:00 -0800848 ql_dbg(ql_dbg_disc, vha, 0x5089,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500849 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
Quinn Tran5d964832017-01-19 22:27:59 -0800850 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
851 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
852 iocb->u.isp24.port_id[0],
853 le16_to_cpu(iocb->u.isp24.nport_handle),
854 iocb->u.isp24.exchange_address, iocb->ox_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800855
856 port_id.b.domain = iocb->u.isp24.port_id[2];
857 port_id.b.area = iocb->u.isp24.port_id[1];
858 port_id.b.al_pa = iocb->u.isp24.port_id[0];
859 port_id.b.rsvd_1 = 0;
860
861 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
862
863 fcport->loop_id = loop_id;
864 fcport->d_id = port_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800865 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
866 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
867 else
868 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
Quinn Tran726b8542017-01-19 22:28:00 -0800869
870 list_for_each_entry(fcport, &vha->vp_fcports, list) {
871 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
872 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
873 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
874 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
875 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500876
877 list_del(&pla->list);
878 kmem_cache_free(qla_tgt_plogi_cachep, pla);
879}
880
Quinn Tran726b8542017-01-19 22:28:00 -0800881void
Quinn Tran5d964832017-01-19 22:27:59 -0800882qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
883 struct fc_port *sess, enum qlt_plogi_link_t link)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500884{
Quinn Tran5d964832017-01-19 22:27:59 -0800885 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500886 /* Inc ref_count first because link might already be pointing at pla */
887 pla->ref_count++;
888
Quinn Tran726b8542017-01-19 22:28:00 -0800889 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
890 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
891 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
892 sess, link, sess->port_name,
893 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
894 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
895 pla->ref_count, pla, link);
896
Quinn Tran5ef696a2017-12-04 14:45:05 -0800897 if (link == QLT_PLOGI_LINK_CONFLICT) {
898 switch (sess->disc_state) {
899 case DSC_DELETED:
900 case DSC_DELETE_PEND:
901 pla->ref_count--;
902 return;
903 default:
904 break;
905 }
906 }
907
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500908 if (sess->plogi_link[link])
909 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
910
Quinn Tran726b8542017-01-19 22:28:00 -0800911 if (link == QLT_PLOGI_LINK_SAME_WWN)
912 pla->fcport = sess;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500913
914 sess->plogi_link[link] = pla;
915}
916
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500917typedef struct {
918 /* These fields must be initialized by the caller */
919 port_id_t id;
920 /*
921 * number of cmds dropped while we were waiting for
922 * initiator to ack LOGO initialize to 1 if LOGO is
923 * triggered by a command, otherwise, to 0
924 */
925 int cmd_count;
926
927 /* These fields are used by callee */
928 struct list_head list;
929} qlt_port_logo_t;
930
931static void
932qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
933{
934 qlt_port_logo_t *tmp;
935 int res;
936
937 mutex_lock(&vha->vha_tgt.tgt_mutex);
938
939 list_for_each_entry(tmp, &vha->logo_list, list) {
940 if (tmp->id.b24 == logo->id.b24) {
941 tmp->cmd_count += logo->cmd_count;
942 mutex_unlock(&vha->vha_tgt.tgt_mutex);
943 return;
944 }
945 }
946
947 list_add_tail(&logo->list, &vha->logo_list);
948
949 mutex_unlock(&vha->vha_tgt.tgt_mutex);
950
951 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
952
953 mutex_lock(&vha->vha_tgt.tgt_mutex);
954 list_del(&logo->list);
955 mutex_unlock(&vha->vha_tgt.tgt_mutex);
956
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500957 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
958 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
959 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
960 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500961}
962
Darren Trapp9dd96862018-03-20 23:09:32 -0700963void qlt_free_session_done(struct work_struct *work)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400964{
Quinn Tran5d964832017-01-19 22:27:59 -0800965 struct fc_port *sess = container_of(work, struct fc_port,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400966 free_work);
967 struct qla_tgt *tgt = sess->tgt;
968 struct scsi_qla_host *vha = sess->vha;
969 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400970 unsigned long flags;
971 bool logout_started = false;
Quinn Tran41dc5292017-01-19 22:28:03 -0800972 scsi_qla_host_t *base_vha;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800973 struct qlt_plogi_ack_t *own =
974 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400975
976 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
977 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500978 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400979 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
Quinn Tran37cacc02017-01-19 22:27:58 -0800980 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400981 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500982 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400983
Quinn Tran726b8542017-01-19 22:28:00 -0800984 if (!IS_SW_RESV_ADDR(sess->d_id)) {
Quinn Tran21374902019-04-23 14:52:35 -0700985 qla2x00_mark_device_lost(vha, sess, 0, 0);
986
Quinn Tran41dc5292017-01-19 22:28:03 -0800987 if (sess->send_els_logo) {
988 qlt_port_logo_t logo;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500989
Quinn Tran41dc5292017-01-19 22:28:03 -0800990 logo.id = sess->d_id;
991 logo.cmd_count = 0;
Quinn Trancb97f2c2018-08-02 13:16:51 -0700992 if (!own)
993 qlt_send_first_logo(vha, &logo);
Himanshu Madhania2390342018-01-22 12:04:20 -0800994 sess->send_els_logo = 0;
Quinn Tran41dc5292017-01-19 22:28:03 -0800995 }
996
Quinn Tranba743f92017-12-04 14:45:12 -0800997 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800998 int rc;
999
Quinn Tran9cd883f2017-12-28 12:33:24 -08001000 if (!own ||
1001 (own &&
1002 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
1003 rc = qla2x00_post_async_logout_work(vha, sess,
1004 NULL);
1005 if (rc != QLA_SUCCESS)
1006 ql_log(ql_log_warn, vha, 0xf085,
1007 "Schedule logo failed sess %p rc %d\n",
1008 sess, rc);
1009 else
1010 logout_started = true;
1011 } else if (own && (own->iocb.u.isp24.status_subcode ==
1012 ELS_PRLI) && ha->flags.rida_fmt2) {
1013 rc = qla2x00_post_async_prlo_work(vha, sess,
1014 NULL);
1015 if (rc != QLA_SUCCESS)
1016 ql_log(ql_log_warn, vha, 0xf085,
1017 "Schedule PRLO failed sess %p rc %d\n",
1018 sess, rc);
1019 else
1020 logout_started = true;
1021 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001022 }
Quinn Tran726b8542017-01-19 22:28:00 -08001023 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001024
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001025 /*
1026 * Release the target session for FC Nexus from fabric module code.
1027 */
1028 if (sess->se_sess != NULL)
1029 ha->tgt.tgt_ops->free_session(sess);
1030
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001031 if (logout_started) {
1032 bool traced = false;
1033
Mark Rutland6aa7de02017-10-23 14:07:29 -07001034 while (!READ_ONCE(sess->logout_completed)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001035 if (!traced) {
1036 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1037 "%s: waiting for sess %p logout\n",
1038 __func__, sess);
1039 traced = true;
1040 }
1041 msleep(100);
1042 }
1043
Quinn Tran726b8542017-01-19 22:28:00 -08001044 ql_dbg(ql_dbg_disc, vha, 0xf087,
Quinn Tran9cd883f2017-12-28 12:33:24 -08001045 "%s: sess %p logout completed\n", __func__, sess);
Quinn Tran41dc5292017-01-19 22:28:03 -08001046 }
1047
1048 if (sess->logo_ack_needed) {
1049 sess->logo_ack_needed = 0;
1050 qla24xx_async_notify_ack(vha, sess,
1051 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001052 }
1053
Quinn Tran726b8542017-01-19 22:28:00 -08001054 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1055 if (sess->se_sess) {
1056 sess->se_sess = NULL;
1057 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1058 tgt->sess_count--;
1059 }
1060
1061 sess->disc_state = DSC_DELETED;
1062 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1063 sess->deleted = QLA_SESS_DELETED;
Quinn Tran726b8542017-01-19 22:28:00 -08001064
1065 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1066 vha->fcport_count--;
1067 sess->login_succ = 0;
1068 }
1069
Quinn Tranba743f92017-12-04 14:45:12 -08001070 qla2x00_clear_loop_id(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08001071
1072 if (sess->conflict) {
1073 sess->conflict->login_pause = 0;
1074 sess->conflict = NULL;
1075 if (!test_bit(UNLOADING, &vha->dpc_flags))
1076 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1077 }
1078
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001079 {
Quinn Tran5d964832017-01-19 22:27:59 -08001080 struct qlt_plogi_ack_t *con =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001081 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
Quinn Tran5d964832017-01-19 22:27:59 -08001082 struct imm_ntfy_from_isp *iocb;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07001083
Quinn Tran48acad02018-08-02 13:16:44 -07001084 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001085
1086 if (con) {
Quinn Tran5d964832017-01-19 22:27:59 -08001087 iocb = &con->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001088 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
Quinn Tran5d964832017-01-19 22:27:59 -08001089 "se_sess %p / sess %p port %8phC is gone,"
1090 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1091 sess->se_sess, sess, sess->port_name,
1092 own ? "releasing own PLOGI" : "no own PLOGI pending",
1093 own ? own->ref_count : -1,
1094 iocb->u.isp24.port_name, con->ref_count);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001095 qlt_plogi_ack_unref(vha, con);
Quinn Tran726b8542017-01-19 22:28:00 -08001096 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001097 } else {
1098 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1099 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1100 sess->se_sess, sess, sess->port_name,
1101 own ? "releasing own PLOGI" :
1102 "no own PLOGI pending",
1103 own ? own->ref_count : -1);
1104 }
1105
Quinn Tran726b8542017-01-19 22:28:00 -08001106 if (own) {
1107 sess->fw_login_state = DSC_LS_PLOGI_PEND;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001108 qlt_plogi_ack_unref(vha, own);
Quinn Tran726b8542017-01-19 22:28:00 -08001109 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1110 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001111 }
Quinn Tran1ae634e2017-12-28 12:33:44 -08001112
Quinn Tran5d964832017-01-19 22:27:59 -08001113 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1114
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001115 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
Quinn Tran726b8542017-01-19 22:28:00 -08001116 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1117 sess, sess->port_name, vha->fcport_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001118
Quinn Tran726b8542017-01-19 22:28:00 -08001119 if (tgt && (tgt->sess_count == 0))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001120 wake_up_all(&tgt->waitQ);
Quinn Tran726b8542017-01-19 22:28:00 -08001121
1122 if (vha->fcport_count == 0)
1123 wake_up_all(&vha->fcport_waitQ);
1124
Quinn Tran41dc5292017-01-19 22:28:03 -08001125 base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran1ae634e2017-12-28 12:33:44 -08001126
1127 sess->free_pending = 0;
1128
Quinn Tran41dc5292017-01-19 22:28:03 -08001129 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1130 return;
1131
Quinn Trana4239942017-12-28 12:33:26 -08001132 if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1133 switch (vha->host->active_mode) {
1134 case MODE_INITIATOR:
1135 case MODE_DUAL:
1136 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1137 qla2xxx_wake_dpc(vha);
1138 break;
1139 case MODE_TARGET:
1140 default:
1141 /* no-op */
1142 break;
1143 }
Quinn Tran726b8542017-01-19 22:28:00 -08001144 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001145}
1146
Quinn Tran75601512015-12-17 14:57:04 -05001147/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08001148void qlt_unreg_sess(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001149{
1150 struct scsi_qla_host *vha = sess->vha;
Quinn Tran1ae634e2017-12-28 12:33:44 -08001151 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001152
Quinn Tran83548fe2017-06-02 09:12:01 -07001153 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
Quinn Tran5d964832017-01-19 22:27:59 -08001154 "%s sess %p for deletion %8phC\n",
1155 __func__, sess, sess->port_name);
1156
Quinn Tran1ae634e2017-12-28 12:33:44 -08001157 spin_lock_irqsave(&sess->vha->work_lock, flags);
1158 if (sess->free_pending) {
1159 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1160 return;
1161 }
1162 sess->free_pending = 1;
1163 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1164
Quinn Tran36c78452016-02-04 11:45:18 -05001165 if (sess->se_sess)
1166 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001167
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001168 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Tran726b8542017-01-19 22:28:00 -08001169 sess->disc_state = DSC_DELETE_PEND;
1170 sess->last_rscn_gen = sess->rscn_gen;
1171 sess->last_login_gen = sess->login_gen;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001172
Darren Trapp9dd96862018-03-20 23:09:32 -07001173 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1174 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1175 sess->nvme_flag |= NVME_FLAG_DELETING;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001176 schedule_work(&sess->nvme_del_work);
Darren Trapp9dd96862018-03-20 23:09:32 -07001177 } else {
1178 INIT_WORK(&sess->free_work, qlt_free_session_done);
1179 schedule_work(&sess->free_work);
1180 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001181}
Quinn Tran5d964832017-01-19 22:27:59 -08001182EXPORT_SYMBOL(qlt_unreg_sess);
Quinn Tran75601512015-12-17 14:57:04 -05001183
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001184static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1185{
1186 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001187 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001188 uint16_t loop_id;
1189 int res = 0;
1190 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -05001191 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001192
1193 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1194 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001195 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -04001196 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -05001197 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -04001198 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001199 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001200 } else {
Quinn Tran75601512015-12-17 14:57:04 -05001201 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001202 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05001203 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001204 }
1205
1206 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1207 "Using sess for qla_tgt_reset: %p\n", sess);
1208 if (!sess) {
1209 res = -ESRCH;
1210 return res;
1211 }
1212
1213 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001214 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1215 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001216 mcmd, loop_id);
1217
Quinn Tranbb1181c2016-12-23 18:06:05 -08001218 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001219}
1220
Quinn Tran726b8542017-01-19 22:28:00 -08001221static void qla24xx_chk_fcp_state(struct fc_port *sess)
1222{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001223 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
Quinn Tran726b8542017-01-19 22:28:00 -08001224 sess->logout_on_delete = 0;
1225 sess->logo_ack_needed = 0;
1226 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1227 sess->scan_state = 0;
1228 }
1229}
1230
Quinn Tran94cff6e2017-12-28 12:33:42 -08001231void qlt_schedule_sess_for_deletion(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001232{
1233 struct qla_tgt *tgt = sess->tgt;
Quinn Trand8630bb2017-12-28 12:33:43 -08001234 unsigned long flags;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001235 u16 sec;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001236
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001237 switch (sess->disc_state) {
1238 case DSC_DELETE_PEND:
Quinn Tran726b8542017-01-19 22:28:00 -08001239 return;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001240 case DSC_DELETED:
Quinn Tran726b8542017-01-19 22:28:00 -08001241 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1242 wake_up_all(&tgt->waitQ);
1243 if (sess->vha->fcport_count == 0)
1244 wake_up_all(&sess->vha->fcport_waitQ);
1245
1246 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1247 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001248 return;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07001249 break;
1250 case DSC_UPD_FCPORT:
1251 /*
1252 * This port is not done reporting to upper layer.
1253 * let it finish
1254 */
1255 sess->next_disc_state = DSC_DELETE_PEND;
1256 sec = jiffies_to_msecs(jiffies -
1257 sess->jiffies_at_registration)/1000;
1258 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1259 sess->sec_since_registration = sec;
1260 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1261 "%s %8phC : Slow Rport registration(%d Sec)\n",
1262 __func__, sess->port_name, sec);
1263 }
1264 return;
1265 default:
1266 break;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001267 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001268
Mikhail Malygin49d7bd32018-06-13 13:05:57 +00001269 spin_lock_irqsave(&sess->vha->work_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001270 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
Mikhail Malygin49d7bd32018-06-13 13:05:57 +00001271 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001272 return;
1273 }
Quinn Tran726b8542017-01-19 22:28:00 -08001274 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Mikhail Malygin49d7bd32018-06-13 13:05:57 +00001275 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001276
1277 sess->disc_state = DSC_DELETE_PEND;
1278
Quinn Tran726b8542017-01-19 22:28:00 -08001279 qla24xx_chk_fcp_state(sess);
1280
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001281 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
Quinn Tran5c640052018-09-26 22:05:14 -07001282 "Scheduling sess %p for deletion %8phC\n",
1283 sess, sess->port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001284
Quinn Trana01c77d2017-12-04 14:44:58 -08001285 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001286 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
Quinn Tran726b8542017-01-19 22:28:00 -08001287}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001288
Joern Engelc5701042014-09-16 16:23:14 -04001289static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001290{
Quinn Tran5d964832017-01-19 22:27:59 -08001291 struct fc_port *sess;
1292 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001293
Quinn Tran5d964832017-01-19 22:27:59 -08001294 list_for_each_entry(sess, &vha->vp_fcports, list) {
1295 if (sess->se_sess)
Quinn Tran94cff6e2017-12-28 12:33:42 -08001296 qlt_schedule_sess_for_deletion(sess);
Quinn Tran5d964832017-01-19 22:27:59 -08001297 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001298
1299 /* At this point tgt could be already dead */
1300}
1301
1302static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1303 uint16_t *loop_id)
1304{
1305 struct qla_hw_data *ha = vha->hw;
1306 dma_addr_t gid_list_dma;
1307 struct gid_list_info *gid_list;
1308 char *id_iter;
1309 int res, rc, i;
1310 uint16_t entries;
1311
1312 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1313 &gid_list_dma, GFP_KERNEL);
1314 if (!gid_list) {
1315 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1316 "qla_target(%d): DMA Alloc failed of %u\n",
1317 vha->vp_idx, qla2x00_gid_list_size(ha));
1318 return -ENOMEM;
1319 }
1320
1321 /* Get list of logged in devices */
Quinn Tran15f30a52017-03-15 09:48:52 -07001322 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001323 if (rc != QLA_SUCCESS) {
1324 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1325 "qla_target(%d): get_id_list() failed: %x\n",
1326 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001327 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001328 goto out_free_id_list;
1329 }
1330
1331 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001332 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001333 for (i = 0; i < entries; i++) {
1334 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07001335
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001336 if ((gid->al_pa == s_id[2]) &&
1337 (gid->area == s_id[1]) &&
1338 (gid->domain == s_id[0])) {
1339 *loop_id = le16_to_cpu(gid->loop_id);
1340 res = 0;
1341 break;
1342 }
1343 id_iter += ha->gid_list_info_size;
1344 }
1345
1346out_free_id_list:
1347 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1348 gid_list, gid_list_dma);
1349 return res;
1350}
1351
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001352/*
1353 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1354 * Caller must put it.
1355 */
Quinn Tran5d964832017-01-19 22:27:59 -08001356static struct fc_port *qlt_create_sess(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001357 struct scsi_qla_host *vha,
1358 fc_port_t *fcport,
1359 bool local)
1360{
1361 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -08001362 struct fc_port *sess = fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001363 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001364
Quinn Tran726b8542017-01-19 22:28:00 -08001365 if (vha->vha_tgt.qla_tgt->tgt_stop)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001366 return NULL;
Quinn Tran726b8542017-01-19 22:28:00 -08001367
1368 if (fcport->se_sess) {
1369 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001370 ql_dbg(ql_dbg_disc, vha, 0x20f6,
Quinn Tran726b8542017-01-19 22:28:00 -08001371 "%s: kref_get_unless_zero failed for %8phC\n",
1372 __func__, sess->port_name);
1373 return NULL;
1374 }
1375 return fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001376 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001377 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001378 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001379
Quinn Tran726b8542017-01-19 22:28:00 -08001380 /*
1381 * Under normal circumstances we want to logout from firmware when
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001382 * session eventually ends and release corresponding nport handle.
1383 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
Quinn Tran726b8542017-01-19 22:28:00 -08001384 * code will adjust these flags as necessary.
1385 */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001386 sess->logout_on_delete = 1;
1387 sess->keep_nport_handle = 0;
Quinn Tran726b8542017-01-19 22:28:00 -08001388 sess->logout_completed = 0;
1389
1390 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1391 &fcport->port_name[0], sess) < 0) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001392 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
Quinn Tran726b8542017-01-19 22:28:00 -08001393 "(%d) %8phC check_initiator_node_acl failed\n",
1394 vha->vp_idx, fcport->port_name);
1395 return NULL;
1396 } else {
1397 kref_init(&fcport->sess_kref);
1398 /*
1399 * Take an extra reference to ->sess_kref here to handle
1400 * fc_port access across ->tgt.sess_lock reaquire.
1401 */
1402 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001403 ql_dbg(ql_dbg_disc, vha, 0x20f7,
Quinn Tran726b8542017-01-19 22:28:00 -08001404 "%s: kref_get_unless_zero failed for %8phC\n",
1405 __func__, sess->port_name);
1406 return NULL;
1407 }
1408
1409 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1410 if (!IS_SW_RESV_ADDR(sess->d_id))
1411 vha->vha_tgt.qla_tgt->sess_count++;
1412
1413 qlt_do_generation_tick(vha, &sess->generation);
1414 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1415 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001416
1417 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
Quinn Tran726b8542017-01-19 22:28:00 -08001418 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1419 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1420 vha->vha_tgt.qla_tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001421
1422 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001423 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1424 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1425 vha->vp_idx, local ? "local " : "", fcport->port_name,
Quinn Tran37cacc02017-01-19 22:27:58 -08001426 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1427 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001428
1429 return sess;
1430}
1431
1432/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001433 * max_gen - specifies maximum session generation
1434 * at which this deletion requestion is still valid
1435 */
1436void
1437qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001438{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001439 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08001440 struct fc_port *sess = fcport;
Quinn Tran75601512015-12-17 14:57:04 -05001441 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001442
1443 if (!vha->hw->tgt.tgt_ops)
1444 return;
1445
Roland Dreierb2032fd2015-07-14 16:00:42 -04001446 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001447 return;
1448
Quinn Tran75601512015-12-17 14:57:04 -05001449 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001450 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001451 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001452 return;
1453 }
Quinn Tran5d964832017-01-19 22:27:59 -08001454 if (!sess->se_sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001455 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001456 return;
1457 }
1458
Alexei Potashnikdf673272015-07-14 16:00:46 -04001459 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001460 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001461 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1462 "Ignoring stale deletion request for se_sess %p / sess %p"
1463 " for port %8phC, req_gen %d, sess_gen %d\n",
1464 sess->se_sess, sess, sess->port_name, max_gen,
1465 sess->generation);
1466 return;
1467 }
1468
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001469 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1470
1471 sess->local = 1;
Quinn Tran75601512015-12-17 14:57:04 -05001472 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001473 qlt_schedule_sess_for_deletion(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001474}
1475
1476static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1477{
1478 struct qla_hw_data *ha = tgt->ha;
1479 unsigned long flags;
1480 int res;
1481 /*
1482 * We need to protect against race, when tgt is freed before or
1483 * inside wake_up()
1484 */
Quinn Tran726b8542017-01-19 22:28:00 -08001485 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001486 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
Quinn Tran5d964832017-01-19 22:27:59 -08001487 "tgt %p, sess_count=%d\n",
1488 tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001489 res = (tgt->sess_count == 0);
Quinn Tran726b8542017-01-19 22:28:00 -08001490 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001491
1492 return res;
1493}
1494
1495/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001496int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001497{
1498 struct scsi_qla_host *vha = tgt->vha;
1499 struct qla_hw_data *ha = tgt->ha;
1500 unsigned long flags;
1501
Quinn Tranb6faaaf2018-09-04 14:19:09 -07001502 mutex_lock(&ha->optrom_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001503 mutex_lock(&qla_tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001504
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001505 if (tgt->tgt_stop || tgt->tgt_stopped) {
1506 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1507 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001508 mutex_unlock(&qla_tgt_mutex);
Quinn Tranb6faaaf2018-09-04 14:19:09 -07001509 mutex_unlock(&ha->optrom_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001510 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001511 }
1512
Quinn Tran3a33dc92017-06-02 09:12:04 -07001513 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001514 vha->host_no, vha);
1515 /*
1516 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1517 * Lock is needed, because we still can get an incoming packet.
1518 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001519 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001520 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001521 qlt_clear_tgt_db(tgt);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001522 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001523 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001524
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001525 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1526 "Waiting for sess works (tgt %p)", tgt);
1527 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1528 while (!list_empty(&tgt->sess_works_list)) {
1529 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1530 flush_scheduled_work();
1531 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1532 }
1533 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1534
1535 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001536 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001537
Joe Carnucciob85e0952017-08-23 15:05:11 -07001538 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001539
1540 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001541 if (!ha->flags.host_shutting_down &&
1542 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001543 qlt_disable_vha(vha);
1544
1545 /* Wait for sessions to clear out (just in case) */
Joe Carnucciob85e0952017-08-23 15:05:11 -07001546 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Quinn Tranb6faaaf2018-09-04 14:19:09 -07001547 mutex_unlock(&ha->optrom_mutex);
1548
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001549 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001550}
1551EXPORT_SYMBOL(qlt_stop_phase1);
1552
1553/* Called by tcm_qla2xxx configfs code */
1554void qlt_stop_phase2(struct qla_tgt *tgt)
1555{
Quinn Tran3a33dc92017-06-02 09:12:04 -07001556 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001557
1558 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001559 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001560 "Already in tgt->tgt_stopped state\n");
1561 dump_stack();
1562 return;
1563 }
Quinn Tran3a33dc92017-06-02 09:12:04 -07001564 if (!tgt->tgt_stop) {
1565 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1566 "%s: phase1 stop is not completed\n", __func__);
1567 dump_stack();
1568 return;
1569 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001570
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001571 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001572 tgt->tgt_stop = 0;
1573 tgt->tgt_stopped = 1;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001574 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001575
Quinn Tran3a33dc92017-06-02 09:12:04 -07001576 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001577 tgt);
Quinn Tran0645cb82018-09-11 10:18:18 -07001578
1579 switch (vha->qlini_mode) {
1580 case QLA2XXX_INI_MODE_EXCLUSIVE:
1581 vha->flags.online = 1;
1582 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1583 break;
1584 default:
1585 break;
1586 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001587}
1588EXPORT_SYMBOL(qlt_stop_phase2);
1589
1590/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001591static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001592{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001593 scsi_qla_host_t *vha = tgt->vha;
Quinn Trane326d222017-06-13 20:47:18 -07001594 void *node;
1595 u64 key = 0;
1596 u16 i;
1597 struct qla_qpair_hint *h;
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001598 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001599
Quinn Tran3a33dc92017-06-02 09:12:04 -07001600 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1601 !tgt->tgt_stopped)
1602 qlt_stop_phase1(tgt);
1603
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001604 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001605 qlt_stop_phase2(tgt);
1606
Quinn Trane326d222017-06-13 20:47:18 -07001607 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1608 unsigned long flags;
1609
1610 h = &tgt->qphints[i];
1611 if (h->qpair) {
1612 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1613 list_del(&h->hint_elem);
1614 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1615 h->qpair = NULL;
1616 }
1617 }
1618 kfree(tgt->qphints);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001619 mutex_lock(&qla_tgt_mutex);
1620 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1621 mutex_unlock(&qla_tgt_mutex);
Quinn Trane326d222017-06-13 20:47:18 -07001622
1623 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1624 btree_remove64(&tgt->lun_qpair_map, key);
1625
1626 btree_destroy64(&tgt->lun_qpair_map);
1627
Sawan Chandak3be63b1e2017-12-04 14:45:04 -08001628 if (vha->vp_idx)
1629 if (ha->tgt.tgt_ops &&
1630 ha->tgt.tgt_ops->remove_target &&
1631 vha->vha_tgt.target_lport_ptr)
1632 ha->tgt.tgt_ops->remove_target(vha);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001633
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001634 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001635
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001636 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001637 "Release of tgt %p finished\n", tgt);
1638
1639 kfree(tgt);
1640}
1641
1642/* ha->hardware_lock supposed to be held on entry */
1643static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1644 const void *param, unsigned int param_size)
1645{
1646 struct qla_tgt_sess_work_param *prm;
1647 unsigned long flags;
1648
1649 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1650 if (!prm) {
1651 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1652 "qla_target(%d): Unable to create session "
1653 "work, command will be refused", 0);
1654 return -ENOMEM;
1655 }
1656
1657 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1658 "Scheduling work (type %d, prm %p)"
1659 " to find session for param %p (size %d, tgt %p)\n",
1660 type, prm, param, param_size, tgt);
1661
1662 prm->type = type;
1663 memcpy(&prm->tm_iocb, param, param_size);
1664
1665 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1666 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1667 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1668
1669 schedule_work(&tgt->sess_work);
1670
1671 return 0;
1672}
1673
1674/*
1675 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1676 */
Quinn Tran82de8022017-06-13 20:47:17 -07001677static void qlt_send_notify_ack(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001678 struct imm_ntfy_from_isp *ntfy,
1679 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1680 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1681{
Quinn Tran82de8022017-06-13 20:47:17 -07001682 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001683 struct qla_hw_data *ha = vha->hw;
1684 request_t *pkt;
1685 struct nack_to_isp *nack;
1686
Quinn Tranec7193e2017-03-15 09:48:55 -07001687 if (!ha->flags.fw_started)
1688 return;
1689
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001690 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1691
Quinn Tran82de8022017-06-13 20:47:17 -07001692 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001693 if (!pkt) {
1694 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1695 "qla_target(%d): %s failed: unable to allocate "
1696 "request packet\n", vha->vp_idx, __func__);
1697 return;
1698 }
1699
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001700 if (vha->vha_tgt.qla_tgt != NULL)
1701 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001702
1703 pkt->entry_type = NOTIFY_ACK_TYPE;
1704 pkt->entry_count = 1;
1705
1706 nack = (struct nack_to_isp *)pkt;
1707 nack->ox_id = ntfy->ox_id;
1708
Quinn Tran726b8542017-01-19 22:28:00 -08001709 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001710 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1711 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1712 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001713 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001714 }
1715 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1716 nack->u.isp24.status = ntfy->u.isp24.status;
1717 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001718 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001719 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1720 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1721 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1722 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1723 nack->u.isp24.srr_reject_code = srr_reject_code;
1724 nack->u.isp24.srr_reject_code_expl = srr_explan;
1725 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1726
1727 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1728 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1729 vha->vp_idx, nack->u.isp24.status);
1730
Himanshu Madhani63163e02014-09-25 06:14:59 -04001731 /* Memory Barrier */
1732 wmb();
Quinn Tran82de8022017-06-13 20:47:17 -07001733 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001734}
1735
Quinn Tran6b0431d2018-09-04 14:19:13 -07001736static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1737{
1738 struct scsi_qla_host *vha = mcmd->vha;
1739 struct qla_hw_data *ha = vha->hw;
1740 struct abts_resp_to_24xx *resp;
1741 uint32_t f_ctl, h;
1742 uint8_t *p;
1743 int rc;
1744 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1745 struct qla_qpair *qpair = mcmd->qpair;
1746
1747 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1748 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1749 ha, mcmd->fc_tm_rsp);
1750
1751 rc = qlt_check_reserve_free_req(qpair, 1);
1752 if (rc) {
1753 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1754 "qla_target(%d): %s failed: unable to allocate request packet\n",
1755 vha->vp_idx, __func__);
1756 return -EAGAIN;
1757 }
1758
1759 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1760 memset(resp, 0, sizeof(*resp));
1761
1762 h = qlt_make_handle(qpair);
1763 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1764 /*
1765 * CTIO type 7 from the firmware doesn't provide a way to
1766 * know the initiator's LOOP ID, hence we can't find
1767 * the session and, so, the command.
1768 */
1769 return -EAGAIN;
1770 } else {
1771 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1772 }
1773
1774 resp->handle = MAKE_HANDLE(qpair->req->id, h);
1775 resp->entry_type = ABTS_RESP_24XX;
1776 resp->entry_count = 1;
1777 resp->nport_handle = abts->nport_handle;
1778 resp->vp_index = vha->vp_idx;
1779 resp->sof_type = abts->sof_type;
1780 resp->exchange_address = abts->exchange_address;
1781 resp->fcp_hdr_le = abts->fcp_hdr_le;
1782 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1783 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1784 F_CTL_SEQ_INITIATIVE);
1785 p = (uint8_t *)&f_ctl;
1786 resp->fcp_hdr_le.f_ctl[0] = *p++;
1787 resp->fcp_hdr_le.f_ctl[1] = *p++;
1788 resp->fcp_hdr_le.f_ctl[2] = *p;
1789
1790 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1791 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1792 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1793 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1794 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1795 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1796
1797 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1798 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1799 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1800 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1801 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1802 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1803 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1804 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1805 } else {
1806 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1807 resp->payload.ba_rjt.reason_code =
1808 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1809 /* Other bytes are zero */
1810 }
1811
1812 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1813
1814 /* Memory Barrier */
1815 wmb();
1816 if (qpair->reqq_start_iocbs)
1817 qpair->reqq_start_iocbs(qpair);
1818 else
1819 qla2x00_start_iocbs(vha, qpair->req);
1820
1821 return rc;
1822}
1823
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001824/*
1825 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1826 */
Quinn Tran82de8022017-06-13 20:47:17 -07001827static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001828 struct abts_recv_from_24xx *abts, uint32_t status,
1829 bool ids_reversed)
1830{
Quinn Tran82de8022017-06-13 20:47:17 -07001831 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001832 struct qla_hw_data *ha = vha->hw;
1833 struct abts_resp_to_24xx *resp;
1834 uint32_t f_ctl;
1835 uint8_t *p;
1836
1837 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1838 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1839 ha, abts, status);
1840
Quinn Tran82de8022017-06-13 20:47:17 -07001841 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1842 NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001843 if (!resp) {
1844 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1845 "qla_target(%d): %s failed: unable to allocate "
1846 "request packet", vha->vp_idx, __func__);
1847 return;
1848 }
1849
1850 resp->entry_type = ABTS_RESP_24XX;
Quinn Tran6b0431d2018-09-04 14:19:13 -07001851 resp->handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001852 resp->entry_count = 1;
1853 resp->nport_handle = abts->nport_handle;
1854 resp->vp_index = vha->vp_idx;
1855 resp->sof_type = abts->sof_type;
1856 resp->exchange_address = abts->exchange_address;
1857 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001858 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001859 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1860 F_CTL_SEQ_INITIATIVE);
1861 p = (uint8_t *)&f_ctl;
1862 resp->fcp_hdr_le.f_ctl[0] = *p++;
1863 resp->fcp_hdr_le.f_ctl[1] = *p++;
1864 resp->fcp_hdr_le.f_ctl[2] = *p;
1865 if (ids_reversed) {
1866 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1867 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1868 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1869 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1870 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1871 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1872 } else {
1873 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1874 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1875 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1876 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1877 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1878 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1879 }
1880 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1881 if (status == FCP_TMF_CMPL) {
1882 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1883 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1884 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1885 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1886 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1887 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1888 } else {
1889 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1890 resp->payload.ba_rjt.reason_code =
1891 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1892 /* Other bytes are zero */
1893 }
1894
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001895 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001896
Himanshu Madhani63163e02014-09-25 06:14:59 -04001897 /* Memory Barrier */
1898 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07001899 if (qpair->reqq_start_iocbs)
1900 qpair->reqq_start_iocbs(qpair);
1901 else
1902 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001903}
1904
1905/*
1906 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1907 */
1908static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
Quinn Tran6b0431d2018-09-04 14:19:13 -07001909 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001910{
1911 struct ctio7_to_24xx *ctio;
Quinn Tran06910942018-09-04 14:19:12 -07001912 u16 tmp;
Quinn Tran6b0431d2018-09-04 14:19:13 -07001913 struct abts_recv_from_24xx *entry;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001914
Quinn Tran06910942018-09-04 14:19:12 -07001915 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001916 if (ctio == NULL) {
1917 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1918 "qla_target(%d): %s failed: unable to allocate "
1919 "request packet\n", vha->vp_idx, __func__);
1920 return;
1921 }
1922
Quinn Tran6b0431d2018-09-04 14:19:13 -07001923 if (mcmd)
1924 /* abts from remote port */
1925 entry = &mcmd->orig_iocb.abts;
1926 else
1927 /* abts from this driver. */
1928 entry = (struct abts_recv_from_24xx *)pkt;
1929
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001930 /*
1931 * We've got on entrance firmware's response on by us generated
1932 * ABTS response. So, in it ID fields are reversed.
1933 */
1934
1935 ctio->entry_type = CTIO_TYPE7;
1936 ctio->entry_count = 1;
1937 ctio->nport_handle = entry->nport_handle;
1938 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001939 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001940 ctio->vp_index = vha->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001941 ctio->exchange_addr = entry->exchange_addr_to_abort;
Quinn Tran06910942018-09-04 14:19:12 -07001942 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
Quinn Tran06910942018-09-04 14:19:12 -07001943
Quinn Tran6b0431d2018-09-04 14:19:13 -07001944 if (mcmd) {
1945 ctio->initiator_id[0] = entry->fcp_hdr_le.s_id[0];
1946 ctio->initiator_id[1] = entry->fcp_hdr_le.s_id[1];
1947 ctio->initiator_id[2] = entry->fcp_hdr_le.s_id[2];
1948
1949 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1950 tmp |= (mcmd->abort_io_attr << 9);
1951 else if (qpair->retry_term_cnt & 1)
1952 tmp |= (0x4 << 9);
1953 } else {
1954 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1955 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1956 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1957
1958 if (qpair->retry_term_cnt & 1)
1959 tmp |= (0x4 << 9);
1960 }
1961 ctio->u.status1.flags = cpu_to_le16(tmp);
1962 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1963
1964 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1965 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1966 le16_to_cpu(ctio->u.status1.flags),
1967 le16_to_cpu(ctio->u.status1.ox_id),
1968 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001969
Himanshu Madhani63163e02014-09-25 06:14:59 -04001970 /* Memory Barrier */
1971 wmb();
Quinn Tran06910942018-09-04 14:19:12 -07001972 if (qpair->reqq_start_iocbs)
1973 qpair->reqq_start_iocbs(qpair);
1974 else
1975 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001976
Quinn Tran6b0431d2018-09-04 14:19:13 -07001977 if (mcmd)
1978 qlt_build_abts_resp_iocb(mcmd);
1979 else
1980 qlt_24xx_send_abts_resp(qpair,
1981 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001982
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001983}
1984
1985/* drop cmds for the given lun
1986 * XXX only looks for cmds on the port through which lun reset was recieved
1987 * XXX does not go through the list of other port (which may have cmds
1988 * for the same lun)
1989 */
1990static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001991 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001992{
1993 struct qla_tgt_sess_op *op;
1994 struct qla_tgt_cmd *cmd;
1995 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001996 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001997
1998 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001999 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002000 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
2001 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07002002 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002003
2004 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2005 op_lun = scsilun_to_int(
2006 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2007 if (op_key == key && op_lun == lun)
2008 op->aborted = true;
2009 }
Quinn Tran41dc5292017-01-19 22:28:03 -08002010
2011 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2012 uint32_t op_key;
2013 u64 op_lun;
2014
2015 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2016 op_lun = scsilun_to_int(
2017 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2018 if (op_key == key && op_lun == lun)
2019 op->aborted = true;
2020 }
2021
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002022 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2023 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07002024 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002025
2026 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2027 cmd_lun = scsilun_to_int(
2028 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2029 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05002030 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002031 }
Quinn Tran8b631d82017-06-02 09:11:54 -07002032 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002033}
2034
Quinn Tran84905df2018-05-01 09:01:53 -07002035static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2036 uint64_t unpacked_lun)
2037{
2038 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2039 struct qla_qpair_hint *h = NULL;
2040
2041 if (vha->flags.qpairs_available) {
2042 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2043 if (!h)
2044 h = &tgt->qphints[0];
2045 } else {
2046 h = &tgt->qphints[0];
2047 }
2048
2049 return h;
2050}
2051
2052static void qlt_do_tmr_work(struct work_struct *work)
2053{
2054 struct qla_tgt_mgmt_cmd *mcmd =
2055 container_of(work, struct qla_tgt_mgmt_cmd, work);
2056 struct qla_hw_data *ha = mcmd->vha->hw;
2057 int rc = EIO;
2058 uint32_t tag;
2059 unsigned long flags;
2060
2061 switch (mcmd->tmr_func) {
2062 case QLA_TGT_ABTS:
2063 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
2064 break;
2065 default:
2066 tag = 0;
2067 break;
2068 }
2069
2070 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2071 mcmd->tmr_func, tag);
2072
2073 if (rc != 0) {
2074 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2075 switch (mcmd->tmr_func) {
2076 case QLA_TGT_ABTS:
Quinn Tran6b0431d2018-09-04 14:19:13 -07002077 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2078 qlt_build_abts_resp_iocb(mcmd);
Quinn Tran84905df2018-05-01 09:01:53 -07002079 break;
2080 case QLA_TGT_LUN_RESET:
2081 case QLA_TGT_CLEAR_TS:
2082 case QLA_TGT_ABORT_TS:
2083 case QLA_TGT_CLEAR_ACA:
2084 case QLA_TGT_TARGET_RESET:
2085 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2086 qla_sam_status);
2087 break;
2088
2089 case QLA_TGT_ABORT_ALL:
2090 case QLA_TGT_NEXUS_LOSS_SESS:
2091 case QLA_TGT_NEXUS_LOSS:
2092 qlt_send_notify_ack(mcmd->qpair,
2093 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2094 break;
2095 }
2096 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2097
2098 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2099 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2100 mcmd->vha->vp_idx, rc);
2101 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2102 }
2103}
2104
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002105/* ha->hardware_lock supposed to be held on entry */
2106static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08002107 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002108{
2109 struct qla_hw_data *ha = vha->hw;
2110 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tran84905df2018-05-01 09:01:53 -07002111 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
Steve Hodgson06e97b42012-11-16 08:06:17 -08002112
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002113 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2114 "qla_target(%d): task abort (tag=%d)\n",
2115 vha->vp_idx, abts->exchange_addr_to_abort);
2116
2117 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2118 if (mcmd == NULL) {
2119 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2120 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2121 vha->vp_idx, __func__);
2122 return -ENOMEM;
2123 }
2124 memset(mcmd, 0, sizeof(*mcmd));
Quinn Tran6b0431d2018-09-04 14:19:13 -07002125 mcmd->cmd_type = TYPE_TGT_TMCMD;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002126 mcmd->sess = sess;
2127 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002128 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08002129 mcmd->tmr_func = QLA_TGT_ABTS;
Quinn Tran84905df2018-05-01 09:01:53 -07002130 mcmd->qpair = h->qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002131 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002132
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07002133 /*
2134 * LUN is looked up by target-core internally based on the passed
2135 * abts->exchange_addr_to_abort tag.
2136 */
Quinn Tran84905df2018-05-01 09:01:53 -07002137 mcmd->se_cmd.cpuid = h->cpuid;
2138
2139 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2140 struct qla_tgt_cmd *abort_cmd;
2141
2142 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2143 abts->exchange_addr_to_abort);
2144 if (abort_cmd && abort_cmd->qpair) {
2145 mcmd->qpair = abort_cmd->qpair;
2146 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
Quinn Tran6b0431d2018-09-04 14:19:13 -07002147 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2148 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
Quinn Tran84905df2018-05-01 09:01:53 -07002149 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002150 }
2151
Quinn Tran84905df2018-05-01 09:01:53 -07002152 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2153 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2154
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002155 return 0;
2156}
2157
2158/*
2159 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2160 */
2161static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2162 struct abts_recv_from_24xx *abts)
2163{
2164 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08002165 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002166 uint32_t tag = abts->exchange_addr_to_abort;
2167 uint8_t s_id[3];
2168 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05002169 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002170
2171 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2172 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2173 "qla_target(%d): ABTS: Abort Sequence not "
2174 "supported\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002175 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2176 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002177 return;
2178 }
2179
2180 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2181 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2182 "qla_target(%d): ABTS: Unknown Exchange "
2183 "Address received\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002184 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2185 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002186 return;
2187 }
2188
2189 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2190 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2191 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2192 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2193 le32_to_cpu(abts->fcp_hdr_le.parameter));
2194
2195 s_id[0] = abts->fcp_hdr_le.s_id[2];
2196 s_id[1] = abts->fcp_hdr_le.s_id[1];
2197 s_id[2] = abts->fcp_hdr_le.s_id[0];
2198
Quinn Tran75601512015-12-17 14:57:04 -05002199 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002200 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2201 if (!sess) {
2202 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
Colin Ian King03fea732018-03-19 10:53:41 +00002203 "qla_target(%d): task abort for non-existent session\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002204 vha->vp_idx);
Quinn Tran75601512015-12-17 14:57:04 -05002205 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2206
Quinn Tranfb352652017-12-28 12:33:32 -08002207 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2208 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002209 return;
2210 }
Quinn Tran75601512015-12-17 14:57:04 -05002211 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2212
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002213
Quinn Tran726b8542017-01-19 22:28:00 -08002214 if (sess->deleted) {
Quinn Tran82de8022017-06-13 20:47:17 -07002215 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2216 false);
Alexei Potashnike52a8b42015-07-14 16:00:48 -04002217 return;
2218 }
2219
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002220 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2221 if (rc != 0) {
2222 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2223 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2224 vha->vp_idx, rc);
Quinn Tran82de8022017-06-13 20:47:17 -07002225 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2226 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002227 return;
2228 }
2229}
2230
2231/*
2232 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2233 */
Quinn Tran82de8022017-06-13 20:47:17 -07002234static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002235 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2236{
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002237 struct scsi_qla_host *ha = mcmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002238 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2239 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002240 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002241
2242 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2243 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2244 ha, atio, resp_code);
2245
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002246
Quinn Tran82de8022017-06-13 20:47:17 -07002247 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002248 if (ctio == NULL) {
2249 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2250 "qla_target(%d): %s failed: unable to allocate "
2251 "request packet\n", ha->vp_idx, __func__);
2252 return;
2253 }
2254
2255 ctio->entry_type = CTIO_TYPE7;
2256 ctio->entry_count = 1;
2257 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2258 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002259 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002260 ctio->vp_index = ha->vp_idx;
2261 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2262 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2263 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2264 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002265 temp = (atio->u.isp24.attr << 9)|
2266 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2267 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002268 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2269 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002270 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002271 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2272 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002273 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002274
Himanshu Madhani63163e02014-09-25 06:14:59 -04002275 /* Memory Barrier */
2276 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002277 if (qpair->reqq_start_iocbs)
2278 qpair->reqq_start_iocbs(qpair);
2279 else
2280 qla2x00_start_iocbs(ha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002281}
2282
2283void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2284{
2285 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2286}
2287EXPORT_SYMBOL(qlt_free_mcmd);
2288
Quinn Tranbe251522017-03-15 09:48:49 -07002289/*
2290 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2291 * reacquire
2292 */
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002293void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07002294 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2295{
2296 struct atio_from_isp *atio = &cmd->atio;
2297 struct ctio7_to_24xx *ctio;
2298 uint16_t temp;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002299 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranbe251522017-03-15 09:48:49 -07002300
2301 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2302 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2303 "sense_key=%02x, asc=%02x, ascq=%02x",
2304 vha, atio, scsi_status, sense_key, asc, ascq);
2305
2306 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2307 if (!ctio) {
2308 ql_dbg(ql_dbg_async, vha, 0x3067,
2309 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2310 vha->host_no, __func__);
2311 goto out;
2312 }
2313
2314 ctio->entry_type = CTIO_TYPE7;
2315 ctio->entry_count = 1;
2316 ctio->handle = QLA_TGT_SKIP_HANDLE;
2317 ctio->nport_handle = cmd->sess->loop_id;
2318 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2319 ctio->vp_index = vha->vp_idx;
2320 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2321 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2322 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2323 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002324 temp = (atio->u.isp24.attr << 9) |
2325 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2326 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002327 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2328 ctio->u.status1.ox_id = cpu_to_le16(temp);
2329 ctio->u.status1.scsi_status =
2330 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2331 ctio->u.status1.response_len = cpu_to_le16(18);
2332 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2333
2334 if (ctio->u.status1.residual != 0)
2335 ctio->u.status1.scsi_status |=
2336 cpu_to_le16(SS_RESIDUAL_UNDER);
2337
2338 /* Response code and sense key */
2339 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2340 (&ctio->u.status1.sense_data)[0]);
2341 /* Additional sense length */
2342 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2343 /* ASC and ASCQ */
2344 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2345 (&ctio->u.status1.sense_data)[3]);
2346
2347 /* Memory Barrier */
2348 wmb();
2349
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002350 if (qpair->reqq_start_iocbs)
2351 qpair->reqq_start_iocbs(qpair);
2352 else
2353 qla2x00_start_iocbs(vha, qpair->req);
2354
Quinn Tranbe251522017-03-15 09:48:49 -07002355out:
2356 return;
2357}
2358
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002359/* callback from target fabric module code */
2360void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2361{
2362 struct scsi_qla_host *vha = mcmd->sess->vha;
2363 struct qla_hw_data *ha = vha->hw;
2364 unsigned long flags;
Quinn Tran82de8022017-06-13 20:47:17 -07002365 struct qla_qpair *qpair = mcmd->qpair;
Quinn Tran6b0431d2018-09-04 14:19:13 -07002366 bool free_mcmd = true;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002367
2368 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2369 "TM response mcmd (%p) status %#x state %#x",
2370 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2371
Quinn Tran82de8022017-06-13 20:47:17 -07002372 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002373
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002374 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002375 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002376 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002377 * previous life, just abort the processing.
2378 */
2379 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002380 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2381 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002382 mcmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002383 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002384 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002385 return;
2386 }
2387
Quinn Tran726b8542017-01-19 22:28:00 -08002388 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
Bart Van Assche8837aa82018-11-27 15:04:54 -08002389 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2390 case ELS_LOGO:
2391 case ELS_PRLO:
2392 case ELS_TPRLO:
Quinn Tran83548fe2017-06-02 09:12:01 -07002393 ql_dbg(ql_dbg_disc, vha, 0x2106,
Bart Van Assche19ce1922019-04-17 14:44:11 -07002394 "TM response logo %8phC status %#x state %#x",
Quinn Tran726b8542017-01-19 22:28:00 -08002395 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2396 mcmd->flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08002397 qlt_schedule_sess_for_deletion(mcmd->sess);
Bart Van Assche8837aa82018-11-27 15:04:54 -08002398 break;
2399 default:
Quinn Tran82de8022017-06-13 20:47:17 -07002400 qlt_send_notify_ack(vha->hw->base_qpair,
2401 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
Bart Van Assche8837aa82018-11-27 15:04:54 -08002402 break;
Quinn Tran726b8542017-01-19 22:28:00 -08002403 }
2404 } else {
Quinn Tran6b0431d2018-09-04 14:19:13 -07002405 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2406 qlt_build_abts_resp_iocb(mcmd);
2407 free_mcmd = false;
2408 } else
Quinn Tran82de8022017-06-13 20:47:17 -07002409 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002410 mcmd->fc_tm_rsp);
2411 }
2412 /*
2413 * Make the callback for ->free_mcmd() to queue_work() and invoke
2414 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2415 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2416 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2417 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2418 * qlt_xmit_tm_rsp() returns here..
2419 */
Quinn Tran6b0431d2018-09-04 14:19:13 -07002420 if (free_mcmd)
2421 ha->tgt.tgt_ops->free_mcmd(mcmd);
2422
Quinn Tran82de8022017-06-13 20:47:17 -07002423 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002424}
2425EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2426
2427/* No locks */
2428static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2429{
2430 struct qla_tgt_cmd *cmd = prm->cmd;
2431
2432 BUG_ON(cmd->sg_cnt == 0);
2433
2434 prm->sg = (struct scatterlist *)cmd->sg;
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002435 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002436 cmd->sg_cnt, cmd->dma_data_direction);
2437 if (unlikely(prm->seg_cnt == 0))
2438 goto out_err;
2439
2440 prm->cmd->sg_mapped = 1;
2441
Quinn Tranf83adb62014-04-11 16:54:43 -04002442 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2443 /*
2444 * If greater than four sg entries then we need to allocate
2445 * the continuation entries
2446 */
Quinn Tranb5399f72017-06-13 20:47:27 -07002447 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
Quinn Tranf83adb62014-04-11 16:54:43 -04002448 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
Quinn Tranb5399f72017-06-13 20:47:27 -07002449 QLA_TGT_DATASEGS_PER_CMD_24XX,
2450 QLA_TGT_DATASEGS_PER_CONT_24XX);
Quinn Tranf83adb62014-04-11 16:54:43 -04002451 } else {
2452 /* DIF */
2453 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2454 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2455 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2456 prm->tot_dsds = prm->seg_cnt;
2457 } else
2458 prm->tot_dsds = prm->seg_cnt;
2459
2460 if (cmd->prot_sg_cnt) {
2461 prm->prot_sg = cmd->prot_sg;
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002462 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
Quinn Tranf83adb62014-04-11 16:54:43 -04002463 cmd->prot_sg, cmd->prot_sg_cnt,
2464 cmd->dma_data_direction);
2465 if (unlikely(prm->prot_seg_cnt == 0))
2466 goto out_err;
2467
2468 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2469 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2470 /* Dif Bundling not support here */
2471 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2472 cmd->blk_sz);
2473 prm->tot_dsds += prm->prot_seg_cnt;
2474 } else
2475 prm->tot_dsds += prm->prot_seg_cnt;
2476 }
2477 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002478
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002479 return 0;
2480
2481out_err:
Quinn Tran22d84722017-06-13 20:47:25 -07002482 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002483 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2484 0, prm->cmd->sg_cnt);
2485 return -1;
2486}
2487
Joern Engelf9b67212014-09-16 16:23:18 -04002488static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002489{
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002490 struct qla_hw_data *ha;
2491 struct qla_qpair *qpair;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07002492
Joern Engelf9b67212014-09-16 16:23:18 -04002493 if (!cmd->sg_mapped)
2494 return;
2495
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002496 qpair = cmd->qpair;
2497
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002498 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002499 cmd->dma_data_direction);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002500 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002501
2502 if (cmd->prot_sg_cnt)
Christoph Hellwige7d0bb72018-10-11 09:42:07 +02002503 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
Quinn Tranf83adb62014-04-11 16:54:43 -04002504 cmd->dma_data_direction);
2505
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002506 if (!cmd->ctx)
2507 return;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002508 ha = vha->hw;
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002509 if (cmd->ctx_dsd_alloced)
2510 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2511
2512 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002513}
2514
Quinn Tran82de8022017-06-13 20:47:17 -07002515static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002516 uint32_t req_cnt)
2517{
Quinn Trand63b3282017-06-02 09:12:07 -07002518 uint32_t cnt;
Quinn Tran82de8022017-06-13 20:47:17 -07002519 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002520
Quinn Tran82de8022017-06-13 20:47:17 -07002521 if (req->cnt < (req_cnt + 2)) {
Quinn Tranaf7bb382017-06-13 20:47:23 -07002522 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2523 RD_REG_DWORD_RELAXED(req->req_q_out));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002524
Quinn Tran82de8022017-06-13 20:47:17 -07002525 if (req->ring_index < cnt)
2526 req->cnt = cnt - req->ring_index;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002527 else
Quinn Tran82de8022017-06-13 20:47:17 -07002528 req->cnt = req->length - (req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002529
Quinn Tran82de8022017-06-13 20:47:17 -07002530 if (unlikely(req->cnt < (req_cnt + 2)))
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002531 return -EAGAIN;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002532 }
2533
Quinn Tran82de8022017-06-13 20:47:17 -07002534 req->cnt -= req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002535
2536 return 0;
2537}
2538
2539/*
2540 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2541 */
Quinn Tran82de8022017-06-13 20:47:17 -07002542static inline void *qlt_get_req_pkt(struct req_que *req)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002543{
2544 /* Adjust ring index. */
Quinn Tran82de8022017-06-13 20:47:17 -07002545 req->ring_index++;
2546 if (req->ring_index == req->length) {
2547 req->ring_index = 0;
2548 req->ring_ptr = req->ring;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002549 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002550 req->ring_ptr++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002551 }
Quinn Tran82de8022017-06-13 20:47:17 -07002552 return (cont_entry_t *)req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002553}
2554
2555/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002556static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002557{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002558 uint32_t h;
Quinn Tranc5419e22017-06-13 20:47:16 -07002559 int index;
2560 uint8_t found = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07002561 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002562
Quinn Tranc5419e22017-06-13 20:47:16 -07002563 h = req->current_outstanding_cmd;
2564
2565 for (index = 1; index < req->num_outstanding_cmds; index++) {
2566 h++;
2567 if (h == req->num_outstanding_cmds)
2568 h = 1;
2569
2570 if (h == QLA_TGT_SKIP_HANDLE)
2571 continue;
2572
2573 if (!req->outstanding_cmds[h]) {
2574 found = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002575 break;
2576 }
Quinn Tranc5419e22017-06-13 20:47:16 -07002577 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002578
Quinn Tranc5419e22017-06-13 20:47:16 -07002579 if (found) {
2580 req->current_outstanding_cmd = h;
2581 } else {
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002582 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2583 "qla_target(%d): Ran out of empty cmd slots\n",
2584 qpair->vha->vp_idx);
Quinn Tranc5419e22017-06-13 20:47:16 -07002585 h = QLA_TGT_NULL_HANDLE;
2586 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002587
2588 return h;
2589}
2590
2591/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002592static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2593 struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002594{
2595 uint32_t h;
2596 struct ctio7_to_24xx *pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002597 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002598 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002599
Quinn Tran82de8022017-06-13 20:47:17 -07002600 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002601 prm->pkt = pkt;
2602 memset(pkt, 0, sizeof(*pkt));
2603
2604 pkt->entry_type = CTIO_TYPE7;
2605 pkt->entry_count = (uint8_t)prm->req_cnt;
Quinn Tran22d84722017-06-13 20:47:25 -07002606 pkt->vp_index = prm->cmd->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002607
Quinn Tran82de8022017-06-13 20:47:17 -07002608 h = qlt_make_handle(qpair);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002609 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2610 /*
2611 * CTIO type 7 from the firmware doesn't provide a way to
2612 * know the initiator's LOOP ID, hence we can't find
2613 * the session and, so, the command.
2614 */
2615 return -EAGAIN;
Quinn Trane326d222017-06-13 20:47:18 -07002616 } else
2617 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002618
Quinn Tran82de8022017-06-13 20:47:17 -07002619 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2620 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2621 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002622 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002623 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2624 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2625 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2626 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002627 temp = atio->u.isp24.attr << 9;
2628 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002629 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2630 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002631 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2632
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002633 return 0;
2634}
2635
2636/*
2637 * ha->hardware_lock supposed to be held on entry. We have already made sure
2638 * that there is sufficient amount of request entries to not drop it.
2639 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002640static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002641{
2642 int cnt;
2643 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002644
2645 /* Build continuation packets */
2646 while (prm->seg_cnt > 0) {
2647 cont_a64_entry_t *cont_pkt64 =
Quinn Tran82de8022017-06-13 20:47:17 -07002648 (cont_a64_entry_t *)qlt_get_req_pkt(
2649 prm->cmd->qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002650
2651 /*
2652 * Make sure that from cont_pkt64 none of
2653 * 64-bit specific fields used for 32-bit
2654 * addressing. Cast to (cont_entry_t *) for
2655 * that.
2656 */
2657
2658 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2659
2660 cont_pkt64->entry_count = 1;
2661 cont_pkt64->sys_define = 0;
2662
Quinn Tran32d29b42017-06-13 20:47:26 -07002663 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2664 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002665
2666 /* Load continuation entry data segments */
2667 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002668 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002669 cnt++, prm->seg_cnt--) {
2670 *dword_ptr++ =
Christoph Hellwig3d5ca1e2018-10-18 15:03:37 +02002671 cpu_to_le32(lower_32_bits
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002672 (sg_dma_address(prm->sg)));
Christoph Hellwig3d5ca1e2018-10-18 15:03:37 +02002673 *dword_ptr++ = cpu_to_le32(upper_32_bits
Quinn Tran32d29b42017-06-13 20:47:26 -07002674 (sg_dma_address(prm->sg)));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002675 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2676
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002677 prm->sg = sg_next(prm->sg);
2678 }
2679 }
2680}
2681
2682/*
2683 * ha->hardware_lock supposed to be held on entry. We have already made sure
2684 * that there is sufficient amount of request entries to not drop it.
2685 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002686static void qlt_load_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002687{
2688 int cnt;
2689 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002690 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2691
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002692 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2693
2694 /* Setup packet address segment pointer */
2695 dword_ptr = pkt24->u.status0.dseg_0_address;
2696
2697 /* Set total data segment count */
2698 if (prm->seg_cnt)
2699 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2700
2701 if (prm->seg_cnt == 0) {
2702 /* No data transfer */
2703 *dword_ptr++ = 0;
2704 *dword_ptr = 0;
2705 return;
2706 }
2707
2708 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002709
2710 /* Load command entry data segments */
2711 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002712 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002713 cnt++, prm->seg_cnt--) {
2714 *dword_ptr++ =
Christoph Hellwig3d5ca1e2018-10-18 15:03:37 +02002715 cpu_to_le32(lower_32_bits(sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002716
Christoph Hellwig3d5ca1e2018-10-18 15:03:37 +02002717 *dword_ptr++ = cpu_to_le32(upper_32_bits(
Quinn Tran32d29b42017-06-13 20:47:26 -07002718 sg_dma_address(prm->sg)));
2719
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002720 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2721
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002722 prm->sg = sg_next(prm->sg);
2723 }
2724
Quinn Tran32d29b42017-06-13 20:47:26 -07002725 qlt_load_cont_data_segments(prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002726}
2727
2728static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2729{
2730 return cmd->bufflen > 0;
2731}
2732
Quinn Tranbe251522017-03-15 09:48:49 -07002733static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2734{
2735 struct qla_tgt_cmd *cmd;
2736 struct scsi_qla_host *vha;
2737
2738 /* asc 0x10=dif error */
2739 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2740 cmd = prm->cmd;
2741 vha = cmd->vha;
2742 /* ASCQ */
2743 switch (prm->sense_buffer[13]) {
2744 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002745 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002746 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2747 "se_cmd=%p tag[%x]",
2748 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2749 cmd->atio.u.isp24.exchange_addr);
2750 break;
2751 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002752 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002753 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2754 "se_cmd=%p tag[%x]",
2755 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2756 cmd->atio.u.isp24.exchange_addr);
2757 break;
2758 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002759 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002760 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2761 "se_cmd=%p tag[%x]",
2762 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2763 cmd->atio.u.isp24.exchange_addr);
2764 break;
2765 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002766 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002767 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2768 "se_cmd=%p tag[%x]",
2769 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2770 cmd->atio.u.isp24.exchange_addr);
2771 break;
2772 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002773 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002774 }
2775}
2776
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002777/*
2778 * Called without ha->hardware_lock held
2779 */
2780static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2781 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2782 uint32_t *full_req_cnt)
2783{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002784 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Tran22d84722017-06-13 20:47:25 -07002785 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002786
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002787 prm->cmd = cmd;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002788 prm->tgt = cmd->tgt;
2789 prm->pkt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002790 prm->rq_result = scsi_status;
2791 prm->sense_buffer = &cmd->sense_buffer[0];
2792 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2793 prm->sg = NULL;
2794 prm->seg_cnt = -1;
2795 prm->req_cnt = 1;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002796 prm->residual = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002797 prm->add_status_pkt = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002798 prm->prot_sg = NULL;
2799 prm->prot_seg_cnt = 0;
2800 prm->tot_dsds = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002801
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002802 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2803 if (qlt_pci_map_calc_cnt(prm) != 0)
2804 return -EAGAIN;
2805 }
2806
2807 *full_req_cnt = prm->req_cnt;
2808
2809 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2810 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002811 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002812 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2813 prm->residual, se_cmd->tag,
2814 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2815 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002816 prm->rq_result |= SS_RESIDUAL_UNDER;
2817 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2818 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002819 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002820 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2821 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2822 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002823 prm->rq_result |= SS_RESIDUAL_OVER;
2824 }
2825
2826 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2827 /*
2828 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2829 * ignored in *xmit_response() below
2830 */
2831 if (qlt_has_data(cmd)) {
2832 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002833 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002834 (prm->rq_result != 0))) {
2835 prm->add_status_pkt = 1;
2836 (*full_req_cnt)++;
2837 }
2838 }
2839 }
2840
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002841 return 0;
2842}
2843
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002844static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2845 int sending_sense)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002846{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002847 if (cmd->qpair->enable_class_2)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002848 return 0;
2849
2850 if (sending_sense)
2851 return cmd->conf_compl_supported;
2852 else
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002853 return cmd->qpair->enable_explicit_conf &&
2854 cmd->conf_compl_supported;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002855}
2856
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002857static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2858 struct qla_tgt_prm *prm)
2859{
2860 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2861 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002862 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002863 if (qlt_need_explicit_conf(prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002864 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002865 CTIO7_FLAGS_EXPLICIT_CONFORM |
2866 CTIO7_FLAGS_CONFORM_REQ);
2867 }
2868 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2869 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2870 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2871 int i;
2872
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002873 if (qlt_need_explicit_conf(prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002874 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Quinn Tran22d84722017-06-13 20:47:25 -07002875 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002876 "Skipping EXPLICIT_CONFORM and "
2877 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2878 "non GOOD status\n");
2879 goto skip_explict_conf;
2880 }
Bart Van Asschead950362015-07-09 07:24:08 -07002881 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002882 CTIO7_FLAGS_EXPLICIT_CONFORM |
2883 CTIO7_FLAGS_CONFORM_REQ);
2884 }
2885skip_explict_conf:
2886 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002887 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002888 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002889 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002890 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002891 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002892 ctio->u.status1.sense_length =
2893 cpu_to_le16(prm->sense_buffer_len);
2894 for (i = 0; i < prm->sense_buffer_len/4; i++)
2895 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2896 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002897
2898 qlt_print_dif_err(prm);
2899
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002900 } else {
2901 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002902 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002903 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002904 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002905 ctio->u.status1.sense_length = 0;
2906 memset(ctio->u.status1.sense_data, 0,
2907 sizeof(ctio->u.status1.sense_data));
2908 }
2909
2910 /* Sense with len > 24, is it possible ??? */
2911}
2912
Quinn Tranf83adb62014-04-11 16:54:43 -04002913static inline int
2914qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2915{
Quinn Tranf83adb62014-04-11 16:54:43 -04002916 switch (se_cmd->prot_op) {
2917 case TARGET_PROT_DOUT_INSERT:
2918 case TARGET_PROT_DIN_STRIP:
2919 if (ql2xenablehba_err_chk >= 1)
2920 return 1;
2921 break;
2922 case TARGET_PROT_DOUT_PASS:
2923 case TARGET_PROT_DIN_PASS:
2924 if (ql2xenablehba_err_chk >= 2)
2925 return 1;
2926 break;
2927 case TARGET_PROT_DIN_INSERT:
2928 case TARGET_PROT_DOUT_STRIP:
2929 return 1;
2930 default:
2931 break;
2932 }
2933 return 0;
2934}
2935
Quinn Tranbe251522017-03-15 09:48:49 -07002936static inline int
2937qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002938{
Quinn Tranbe251522017-03-15 09:48:49 -07002939 switch (se_cmd->prot_op) {
2940 case TARGET_PROT_DIN_INSERT:
2941 case TARGET_PROT_DOUT_INSERT:
2942 case TARGET_PROT_DIN_STRIP:
2943 case TARGET_PROT_DOUT_STRIP:
2944 case TARGET_PROT_DIN_PASS:
2945 case TARGET_PROT_DOUT_PASS:
2946 return 1;
2947 default:
2948 return 0;
2949 }
2950 return 0;
2951}
Quinn Tranf83adb62014-04-11 16:54:43 -04002952
Quinn Tranbe251522017-03-15 09:48:49 -07002953/*
2954 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2955 */
2956static void
2957qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2958 uint16_t *pfw_prot_opts)
2959{
2960 struct se_cmd *se_cmd = &cmd->se_cmd;
2961 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2962 scsi_qla_host_t *vha = cmd->tgt->vha;
2963 struct qla_hw_data *ha = vha->hw;
2964 uint32_t t32 = 0;
2965
2966 /*
2967 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002968 * have been immplemented by TCM, before AppTag is avail.
2969 * Look for modesense_handlers[]
2970 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002971 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002972 ctx->app_tag_mask[0] = 0x0;
2973 ctx->app_tag_mask[1] = 0x0;
2974
Quinn Tranbe251522017-03-15 09:48:49 -07002975 if (IS_PI_UNINIT_CAPABLE(ha)) {
2976 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2977 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2978 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2979 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2980 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2981 }
2982
2983 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2984
Quinn Tranf83adb62014-04-11 16:54:43 -04002985 switch (se_cmd->prot_type) {
2986 case TARGET_DIF_TYPE0_PROT:
2987 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002988 * No check for ql2xenablehba_err_chk, as it
2989 * would be an I/O error if hba tag generation
2990 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002991 */
2992 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002993 /* enable ALL bytes of the ref tag */
2994 ctx->ref_tag_mask[0] = 0xff;
2995 ctx->ref_tag_mask[1] = 0xff;
2996 ctx->ref_tag_mask[2] = 0xff;
2997 ctx->ref_tag_mask[3] = 0xff;
2998 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002999 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07003000 /*
3001 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
3002 * REF tag, and 16 bit app tag.
3003 */
3004 ctx->ref_tag = cpu_to_le32(lba);
3005 if (!qla_tgt_ref_mask_check(se_cmd) ||
3006 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3007 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3008 break;
3009 }
3010 /* enable ALL bytes of the ref tag */
3011 ctx->ref_tag_mask[0] = 0xff;
3012 ctx->ref_tag_mask[1] = 0xff;
3013 ctx->ref_tag_mask[2] = 0xff;
3014 ctx->ref_tag_mask[3] = 0xff;
3015 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003016 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07003017 /*
3018 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
3019 * tag has to match LBA in CDB + N
3020 */
3021 ctx->ref_tag = cpu_to_le32(lba);
3022 if (!qla_tgt_ref_mask_check(se_cmd) ||
3023 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3024 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3025 break;
3026 }
3027 /* enable ALL bytes of the ref tag */
3028 ctx->ref_tag_mask[0] = 0xff;
3029 ctx->ref_tag_mask[1] = 0xff;
3030 ctx->ref_tag_mask[2] = 0xff;
3031 ctx->ref_tag_mask[3] = 0xff;
3032 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003033 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07003034 /* For TYPE 3 protection: 16 bit GUARD only */
3035 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3036 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3037 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3038 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003039 }
3040}
3041
Quinn Tranf83adb62014-04-11 16:54:43 -04003042static inline int
Quinn Tran82de8022017-06-13 20:47:17 -07003043qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
Quinn Tranf83adb62014-04-11 16:54:43 -04003044{
3045 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003046 uint32_t transfer_length = 0;
3047 uint32_t data_bytes;
3048 uint32_t dif_bytes;
3049 uint8_t bundling = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003050 struct crc_context *crc_ctx_pkt = NULL;
3051 struct qla_hw_data *ha;
3052 struct ctio_crc2_to_fw *pkt;
3053 dma_addr_t crc_ctx_dma;
3054 uint16_t fw_prot_opts = 0;
3055 struct qla_tgt_cmd *cmd = prm->cmd;
3056 struct se_cmd *se_cmd = &cmd->se_cmd;
3057 uint32_t h;
3058 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07003059 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003060 uint16_t t16;
Quinn Tran82de8022017-06-13 20:47:17 -07003061 scsi_qla_host_t *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003062
Quinn Tranf83adb62014-04-11 16:54:43 -04003063 ha = vha->hw;
3064
Quinn Tran82de8022017-06-13 20:47:17 -07003065 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
Quinn Tranf83adb62014-04-11 16:54:43 -04003066 prm->pkt = pkt;
3067 memset(pkt, 0, sizeof(*pkt));
3068
Quinn Tran22d84722017-06-13 20:47:25 -07003069 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
Quinn Tranf83adb62014-04-11 16:54:43 -04003070 "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 -07003071 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
Quinn Tranf83adb62014-04-11 16:54:43 -04003072 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3073
3074 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3075 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3076 bundling = 0;
3077
3078 /* Compute dif len and adjust data len to incude protection */
3079 data_bytes = cmd->bufflen;
3080 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3081
3082 switch (se_cmd->prot_op) {
3083 case TARGET_PROT_DIN_INSERT:
3084 case TARGET_PROT_DOUT_STRIP:
3085 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07003086 if (cmd->prot_sg_cnt)
3087 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04003088 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003089 case TARGET_PROT_DIN_STRIP:
3090 case TARGET_PROT_DOUT_INSERT:
3091 case TARGET_PROT_DIN_PASS:
3092 case TARGET_PROT_DOUT_PASS:
3093 transfer_length = data_bytes + dif_bytes;
3094 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04003095 default:
3096 BUG();
3097 break;
3098 }
3099
3100 if (!qlt_hba_err_chk_enabled(se_cmd))
3101 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3102 /* HBA error checking enabled */
3103 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3104 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3105 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3106 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3107 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3108 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3109 }
3110
3111 switch (se_cmd->prot_op) {
3112 case TARGET_PROT_DIN_INSERT:
3113 case TARGET_PROT_DOUT_INSERT:
3114 fw_prot_opts |= PO_MODE_DIF_INSERT;
3115 break;
3116 case TARGET_PROT_DIN_STRIP:
3117 case TARGET_PROT_DOUT_STRIP:
3118 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3119 break;
3120 case TARGET_PROT_DIN_PASS:
3121 case TARGET_PROT_DOUT_PASS:
3122 fw_prot_opts |= PO_MODE_DIF_PASS;
3123 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3124 break;
3125 default:/* Normal Request */
3126 fw_prot_opts |= PO_MODE_DIF_PASS;
3127 break;
3128 }
3129
Quinn Tranf83adb62014-04-11 16:54:43 -04003130 /* ---- PKT ---- */
3131 /* Update entry type to indicate Command Type CRC_2 IOCB */
3132 pkt->entry_type = CTIO_CRC2;
3133 pkt->entry_count = 1;
Quinn Tran22d84722017-06-13 20:47:25 -07003134 pkt->vp_index = cmd->vp_idx;
Quinn Tranf83adb62014-04-11 16:54:43 -04003135
Quinn Tran82de8022017-06-13 20:47:17 -07003136 h = qlt_make_handle(qpair);
Quinn Tranf83adb62014-04-11 16:54:43 -04003137 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3138 /*
3139 * CTIO type 7 from the firmware doesn't provide a way to
3140 * know the initiator's LOOP ID, hence we can't find
3141 * the session and, so, the command.
3142 */
3143 return -EAGAIN;
3144 } else
Quinn Tran82de8022017-06-13 20:47:17 -07003145 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003146
Quinn Tran82de8022017-06-13 20:47:17 -07003147 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3148 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07003149 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07003150 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04003151 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3152 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3153 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3154 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003155
3156 /* silence compile warning */
3157 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3158 pkt->ox_id = cpu_to_le16(t16);
3159
3160 t16 = (atio->u.isp24.attr << 9);
3161 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04003162 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3163
3164 /* Set transfer direction */
3165 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07003166 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04003167 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07003168 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04003169
Quinn Tranf83adb62014-04-11 16:54:43 -04003170 pkt->dseg_count = prm->tot_dsds;
3171 /* Fibre channel byte count */
3172 pkt->transfer_length = cpu_to_le32(transfer_length);
3173
Quinn Tranf83adb62014-04-11 16:54:43 -04003174 /* ----- CRC context -------- */
3175
3176 /* Allocate CRC context from global pool */
3177 crc_ctx_pkt = cmd->ctx =
Souptick Joarder501017f2018-02-15 01:40:38 +05303178 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
Quinn Tranf83adb62014-04-11 16:54:43 -04003179
3180 if (!crc_ctx_pkt)
3181 goto crc_queuing_error;
3182
Quinn Tranf83adb62014-04-11 16:54:43 -04003183 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3184 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3185
3186 /* Set handle */
3187 crc_ctx_pkt->handle = pkt->handle;
3188
Quinn Tranbe251522017-03-15 09:48:49 -07003189 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04003190
3191 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
3192 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
3193 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3194
Quinn Tranf83adb62014-04-11 16:54:43 -04003195 if (!bundling) {
3196 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
3197 } else {
3198 /*
3199 * Configure Bundling if we need to fetch interlaving
3200 * protection PCI accesses
3201 */
3202 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3203 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3204 crc_ctx_pkt->u.bundling.dseg_count =
3205 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3206 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
3207 }
3208
3209 /* Finish the common fields of CRC pkt */
3210 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3211 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3212 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07003213 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003214
Quinn Tranbe251522017-03-15 09:48:49 -07003215 memset((uint8_t *)&tc, 0 , sizeof(tc));
3216 tc.vha = vha;
3217 tc.blk_sz = cmd->blk_sz;
3218 tc.bufflen = cmd->bufflen;
3219 tc.sg = cmd->sg;
3220 tc.prot_sg = cmd->prot_sg;
3221 tc.ctx = crc_ctx_pkt;
3222 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04003223
3224 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07003225 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04003226
3227 if (!bundling && prm->prot_seg_cnt) {
3228 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003229 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003230 goto crc_queuing_error;
3231 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003232 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003233 goto crc_queuing_error;
3234
3235 if (bundling && prm->prot_seg_cnt) {
3236 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003237 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04003238
3239 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3240 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Giridhar Malavali50b81272018-12-21 09:33:45 -08003241 prm->prot_seg_cnt, cmd))
Quinn Tranf83adb62014-04-11 16:54:43 -04003242 goto crc_queuing_error;
3243 }
3244 return QLA_SUCCESS;
3245
3246crc_queuing_error:
3247 /* Cleanup will be performed by the caller */
Quinn Tran82de8022017-06-13 20:47:17 -07003248 qpair->req->outstanding_cmds[h] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003249
3250 return QLA_FUNCTION_FAILED;
3251}
3252
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003253/*
3254 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3255 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3256 */
3257int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3258 uint8_t scsi_status)
3259{
3260 struct scsi_qla_host *vha = cmd->vha;
Quinn Tran82de8022017-06-13 20:47:17 -07003261 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003262 struct ctio7_to_24xx *pkt;
3263 struct qla_tgt_prm prm;
3264 uint32_t full_req_cnt = 0;
3265 unsigned long flags = 0;
3266 int res;
3267
Quinn Tran51fd6e62019-01-24 23:23:43 -08003268 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3269 (cmd->sess && cmd->sess->deleted)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003270 cmd->state = QLA_TGT_STATE_PROCESSED;
Quinn Tran51fd6e62019-01-24 23:23:43 -08003271 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003272 return 0;
3273 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003274
Quinn Tran22d84722017-06-13 20:47:25 -07003275 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
Quinn Tran82de8022017-06-13 20:47:17 -07003276 "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 -04003277 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3278 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
Quinn Tran82de8022017-06-13 20:47:17 -07003279 &cmd->se_cmd, qpair->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003280
3281 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3282 &full_req_cnt);
3283 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003284 return res;
3285 }
3286
Quinn Tran82de8022017-06-13 20:47:17 -07003287 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003288
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003289 if (xmit_type == QLA_TGT_XMIT_STATUS)
Quinn Tran60a9ead2017-06-13 20:47:28 -07003290 qpair->tgt_counters.core_qla_snd_status++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003291 else
Quinn Tran60a9ead2017-06-13 20:47:28 -07003292 qpair->tgt_counters.core_qla_que_buf++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003293
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003294 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003295 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003296 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003297 * previous life, just abort the processing.
3298 */
3299 cmd->state = QLA_TGT_STATE_PROCESSED;
3300 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003301 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003302 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3303 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003304 cmd->reset_count, qpair->chip_reset);
Quinn Tran82de8022017-06-13 20:47:17 -07003305 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04003306 return 0;
3307 }
3308
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003309 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003310 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003311 if (unlikely(res))
3312 goto out_unmap_unlock;
3313
Quinn Tranf83adb62014-04-11 16:54:43 -04003314 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
Quinn Tran82de8022017-06-13 20:47:17 -07003315 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003316 else
Quinn Tran82de8022017-06-13 20:47:17 -07003317 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Quinn Tran810e30b2015-06-10 11:05:20 -04003318 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003319 qpair->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003320 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003321 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003322
3323 pkt = (struct ctio7_to_24xx *)prm.pkt;
3324
3325 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3326 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003327 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003328 CTIO7_FLAGS_STATUS_MODE_0);
3329
Quinn Tranf83adb62014-04-11 16:54:43 -04003330 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003331 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003332
3333 if (prm.add_status_pkt == 0) {
3334 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3335 pkt->u.status0.scsi_status =
3336 cpu_to_le16(prm.rq_result);
3337 pkt->u.status0.residual =
3338 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003339 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003340 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003341 if (qlt_need_explicit_conf(cmd, 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003342 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003343 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003344 CTIO7_FLAGS_EXPLICIT_CONFORM |
3345 CTIO7_FLAGS_CONFORM_REQ);
3346 }
3347 }
3348
3349 } else {
3350 /*
3351 * We have already made sure that there is sufficient
3352 * amount of request entries to not drop HW lock in
3353 * req_pkt().
3354 */
3355 struct ctio7_to_24xx *ctio =
Quinn Tran82de8022017-06-13 20:47:17 -07003356 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3357 qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003358
Quinn Tran22d84722017-06-13 20:47:25 -07003359 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
Arun Easi667024a2014-09-25 06:14:47 -04003360 "Building additional status packet 0x%p.\n",
3361 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003362
Quinn Tranf83adb62014-04-11 16:54:43 -04003363 /*
3364 * T10Dif: ctio_crc2_to_fw overlay ontop of
3365 * ctio7_to_24xx
3366 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003367 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003368 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003369 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003370 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003371 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003372 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003373 CTIO7_FLAGS_DATA_IN);
3374
3375 /* Real finish is ctio_m1's finish */
3376 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003377 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003378 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003379
3380 /* qlt_24xx_init_ctio_to_isp will correct
3381 * all neccessary fields that's part of CTIO7.
3382 * There should be no residual of CTIO-CRC2 data.
3383 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003384 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3385 &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003386 }
3387 } else
3388 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3389
3390
3391 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand594db02018-09-11 10:18:23 -07003392 spin_lock(&cmd->cmd_lock);
Quinn Trand564a372014-09-25 06:14:57 -04003393 cmd->cmd_sent_to_fw = 1;
Quinn Trand594db02018-09-11 10:18:23 -07003394 spin_unlock(&cmd->cmd_lock);
Quinn Tran079a3a32018-09-04 14:19:11 -07003395 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003396
Himanshu Madhani63163e02014-09-25 06:14:59 -04003397 /* Memory Barrier */
3398 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003399 if (qpair->reqq_start_iocbs)
3400 qpair->reqq_start_iocbs(qpair);
3401 else
3402 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003403 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003404
3405 return 0;
3406
3407out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003408 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003409 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003410
3411 return res;
3412}
3413EXPORT_SYMBOL(qlt_xmit_response);
3414
3415int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3416{
3417 struct ctio7_to_24xx *pkt;
3418 struct scsi_qla_host *vha = cmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003419 struct qla_tgt *tgt = cmd->tgt;
3420 struct qla_tgt_prm prm;
Quinn Tran82de8022017-06-13 20:47:17 -07003421 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003422 int res = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003423 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003424
3425 memset(&prm, 0, sizeof(prm));
3426 prm.cmd = cmd;
3427 prm.tgt = tgt;
3428 prm.sg = NULL;
3429 prm.req_cnt = 1;
3430
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003431 /* Calculate number of entries and segments required */
3432 if (qlt_pci_map_calc_cnt(&prm) != 0)
3433 return -EAGAIN;
3434
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003435 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003436 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003437 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003438 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003439 * previous life, just abort the processing.
3440 */
3441 cmd->state = QLA_TGT_STATE_NEED_DATA;
3442 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003443 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003444 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3445 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003446 cmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003447 return 0;
3448 }
3449
Quinn Tran82de8022017-06-13 20:47:17 -07003450 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003451 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003452 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003453 if (res != 0)
3454 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003455 if (cmd->se_cmd.prot_op)
Quinn Tran82de8022017-06-13 20:47:17 -07003456 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003457 else
Quinn Tran82de8022017-06-13 20:47:17 -07003458 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003459
Quinn Tran810e30b2015-06-10 11:05:20 -04003460 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003461 qpair->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003462 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003463 }
3464
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003465 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003466 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003467 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003468
3469 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003470 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003471
3472 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand594db02018-09-11 10:18:23 -07003473 spin_lock(&cmd->cmd_lock);
Quinn Trand564a372014-09-25 06:14:57 -04003474 cmd->cmd_sent_to_fw = 1;
Quinn Trand594db02018-09-11 10:18:23 -07003475 spin_unlock(&cmd->cmd_lock);
Quinn Tran079a3a32018-09-04 14:19:11 -07003476 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003477
Himanshu Madhani63163e02014-09-25 06:14:59 -04003478 /* Memory Barrier */
3479 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003480 if (qpair->reqq_start_iocbs)
3481 qpair->reqq_start_iocbs(qpair);
3482 else
3483 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003484 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003485
3486 return res;
3487
3488out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003489 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003490 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003491
3492 return res;
3493}
3494EXPORT_SYMBOL(qlt_rdy_to_xfer);
3495
Quinn Tranf83adb62014-04-11 16:54:43 -04003496
3497/*
Quinn Tranbe251522017-03-15 09:48:49 -07003498 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003499 */
Quinn Tranbe251522017-03-15 09:48:49 -07003500static void
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003501qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003502 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003503{
3504 uint8_t *ap = &sts->actual_dif[0];
3505 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003506 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003507 uint8_t scsi_status, sense_key, asc, ascq;
3508 unsigned long flags;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003509 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003510
Quinn Tranbe251522017-03-15 09:48:49 -07003511 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003512
Quinn Tranbe251522017-03-15 09:48:49 -07003513 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3514 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3515 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003516
Quinn Tranbe251522017-03-15 09:48:49 -07003517 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3518 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3519 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003520
Quinn Tranbe251522017-03-15 09:48:49 -07003521 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3522 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003523
Quinn Tranbe251522017-03-15 09:48:49 -07003524 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003525
Quinn Tranbe251522017-03-15 09:48:49 -07003526 /* check appl tag */
3527 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003528 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3529 "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]",
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);
Quinn Tranf83adb62014-04-11 16:54:43 -04003534
Quinn Tranbe251522017-03-15 09:48:49 -07003535 cmd->dif_err_code = DIF_ERR_APP;
3536 scsi_status = SAM_STAT_CHECK_CONDITION;
3537 sense_key = ABORTED_COMMAND;
3538 asc = 0x10;
3539 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003540 }
3541
3542 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003543 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003544 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3545 "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] ",
3546 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3547 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3548 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3549 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003550
Quinn Tranbe251522017-03-15 09:48:49 -07003551 cmd->dif_err_code = DIF_ERR_REF;
3552 scsi_status = SAM_STAT_CHECK_CONDITION;
3553 sense_key = ABORTED_COMMAND;
3554 asc = 0x10;
3555 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003556 goto out;
3557 }
3558
Quinn Tranbe251522017-03-15 09:48:49 -07003559 /* check guard */
3560 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003561 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3562 "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]",
3563 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3564 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3565 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3566 cmd->atio.u.isp24.fcp_hdr.ox_id);
3567
Quinn Tranbe251522017-03-15 09:48:49 -07003568 cmd->dif_err_code = DIF_ERR_GRD;
3569 scsi_status = SAM_STAT_CHECK_CONDITION;
3570 sense_key = ABORTED_COMMAND;
3571 asc = 0x10;
3572 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003573 }
3574out:
Quinn Tranbe251522017-03-15 09:48:49 -07003575 switch (cmd->state) {
3576 case QLA_TGT_STATE_NEED_DATA:
3577 /* handle_data will load DIF error code */
3578 cmd->state = QLA_TGT_STATE_DATA_IN;
3579 vha->hw->tgt.tgt_ops->handle_data(cmd);
3580 break;
3581 default:
3582 spin_lock_irqsave(&cmd->cmd_lock, flags);
3583 if (cmd->aborted) {
3584 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3585 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3586 break;
3587 }
3588 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003589
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003590 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3591 ascq);
Quinn Tranbe251522017-03-15 09:48:49 -07003592 /* assume scsi status gets out on the wire.
3593 * Will not wait for completion.
3594 */
3595 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3596 break;
3597 }
3598}
Quinn Tranf83adb62014-04-11 16:54:43 -04003599
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003600/* If hardware_lock held on entry, might drop it, then reaquire */
3601/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003602static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3603 struct imm_ntfy_from_isp *ntfy)
3604{
3605 struct nack_to_isp *nack;
3606 struct qla_hw_data *ha = vha->hw;
3607 request_t *pkt;
3608 int ret = 0;
3609
3610 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3611 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3612
Quinn Tranec7193e2017-03-15 09:48:55 -07003613 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003614 if (pkt == NULL) {
3615 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3616 "qla_target(%d): %s failed: unable to allocate "
3617 "request packet\n", vha->vp_idx, __func__);
3618 return -ENOMEM;
3619 }
3620
3621 pkt->entry_type = NOTIFY_ACK_TYPE;
3622 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003623 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003624
3625 nack = (struct nack_to_isp *)pkt;
3626 nack->ox_id = ntfy->ox_id;
3627
3628 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3629 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3630 nack->u.isp24.flags = ntfy->u.isp24.flags &
3631 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3632 }
3633
3634 /* terminate */
3635 nack->u.isp24.flags |=
3636 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3637
3638 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3639 nack->u.isp24.status = ntfy->u.isp24.status;
3640 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3641 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3642 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3643 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3644 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3645 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3646
3647 qla2x00_start_iocbs(vha, vha->req);
3648 return ret;
3649}
3650
3651static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3652 struct imm_ntfy_from_isp *imm, int ha_locked)
3653{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003654 int rc;
3655
Bart Van Asschee65449a2019-04-17 14:44:26 -07003656 WARN_ON_ONCE(!ha_locked);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003657 rc = __qlt_send_term_imm_notif(vha, imm);
Bart Van Asschee65449a2019-04-17 14:44:26 -07003658 pr_debug("rc = %d\n", rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003659}
3660
Quinn Tran83548fe2017-06-02 09:12:01 -07003661/*
3662 * If hardware_lock held on entry, might drop it, then reaquire
3663 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3664 */
Quinn Tran82de8022017-06-13 20:47:17 -07003665static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003666 struct qla_tgt_cmd *cmd,
3667 struct atio_from_isp *atio)
3668{
Quinn Tran82de8022017-06-13 20:47:17 -07003669 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003670 struct ctio7_to_24xx *ctio24;
3671 struct qla_hw_data *ha = vha->hw;
3672 request_t *pkt;
3673 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003674 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003675
Quinn Tran83548fe2017-06-02 09:12:01 -07003676 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003677
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003678 if (cmd)
3679 vha = cmd->vha;
3680
Quinn Tran82de8022017-06-13 20:47:17 -07003681 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003682 if (pkt == NULL) {
3683 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3684 "qla_target(%d): %s failed: unable to allocate "
3685 "request packet\n", vha->vp_idx, __func__);
3686 return -ENOMEM;
3687 }
3688
3689 if (cmd != NULL) {
3690 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3691 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3692 "qla_target(%d): Terminating cmd %p with "
3693 "incorrect state %d\n", vha->vp_idx, cmd,
3694 cmd->state);
3695 } else
3696 ret = 1;
3697 }
3698
Quinn Tran60a9ead2017-06-13 20:47:28 -07003699 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003700 pkt->entry_count = 1;
3701 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3702
3703 ctio24 = (struct ctio7_to_24xx *)pkt;
3704 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003705 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003706 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003707 ctio24->vp_index = vha->vp_idx;
3708 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3709 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3710 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3711 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003712 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3713 CTIO7_FLAGS_TERMINATE;
3714 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003715 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3716 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003717
Himanshu Madhani63163e02014-09-25 06:14:59 -04003718 /* Memory Barrier */
3719 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003720 if (qpair->reqq_start_iocbs)
3721 qpair->reqq_start_iocbs(qpair);
3722 else
3723 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003724 return ret;
3725}
3726
Quinn Tran82de8022017-06-13 20:47:17 -07003727static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003728 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3729 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003730{
Quinn Tran82de8022017-06-13 20:47:17 -07003731 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003732 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003733 int rc;
3734
Quinn Tran82de8022017-06-13 20:47:17 -07003735 /* why use different vha? NPIV */
3736 if (cmd)
3737 vha = cmd->vha;
3738 else
3739 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003740
3741 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003742 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003743 if (rc == -ENOMEM)
3744 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003745 goto done;
3746 }
Quinn Tran82de8022017-06-13 20:47:17 -07003747 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3748 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003749 if (rc == -ENOMEM)
3750 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003751
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003752done:
Quinn Trana07100e2015-12-07 19:48:57 -05003753 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003754 if (cmd->sg_mapped)
3755 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003756 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3757 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003758
3759 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003760 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003761
Quinn Tran7b898542014-04-11 16:54:44 -04003762 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003763}
3764
Quinn Tran33e79972014-09-25 06:14:55 -04003765static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3766{
3767 struct list_head free_list;
3768 struct qla_tgt_cmd *cmd, *tcmd;
3769
3770 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003771 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003772
3773 cmd = tcmd = NULL;
3774 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3775 INIT_LIST_HEAD(&free_list);
3776 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3777
3778 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3779 list_del(&cmd->cmd_list);
3780 /* This cmd was never sent to TCM. There is no need
3781 * to schedule free or call free_cmd
3782 */
3783 qlt_free_cmd(cmd);
3784 vha->hw->tgt.num_qfull_cmds_alloc--;
3785 }
3786 }
3787 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3788}
3789
3790static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3791{
3792 uint32_t total_leaked;
3793
3794 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3795
3796 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3797 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3798
3799 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3800 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003801 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003802
3803 if (IS_P3P_TYPE(vha->hw))
3804 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3805 else
3806 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3807 qla2xxx_wake_dpc(vha);
3808 }
3809
3810}
3811
Quinn Trana07100e2015-12-07 19:48:57 -05003812int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003813{
3814 struct qla_tgt *tgt = cmd->tgt;
3815 struct scsi_qla_host *vha = tgt->vha;
3816 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003817 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003818
3819 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3820 "qla_target(%d): terminating exchange for aborted cmd=%p "
3821 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3822 se_cmd->tag);
3823
Quinn Trana07100e2015-12-07 19:48:57 -05003824 spin_lock_irqsave(&cmd->cmd_lock, flags);
3825 if (cmd->aborted) {
3826 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3827 /*
3828 * It's normal to see 2 calls in this path:
3829 * 1) XFER Rdy completion + CMD_T_ABORT
3830 * 2) TCM TMR - drain_state_list
3831 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003832 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3833 "multiple abort. %p transport_state %x, t_state %x, "
3834 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3835 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003836 return EIO;
3837 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003838 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003839 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003840 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003841
Quinn Tran82de8022017-06-13 20:47:17 -07003842 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003843 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003844}
3845EXPORT_SYMBOL(qlt_abort_cmd);
3846
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003847void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3848{
Quinn Tran5d964832017-01-19 22:27:59 -08003849 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003850
Quinn Tranf83adb62014-04-11 16:54:43 -04003851 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3852 "%s: se_cmd[%p] ox_id %04x\n",
3853 __func__, &cmd->se_cmd,
3854 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003855
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003856 BUG_ON(cmd->cmd_in_wq);
3857
Quinn Trana07100e2015-12-07 19:48:57 -05003858 if (cmd->sg_mapped)
3859 qlt_unmap_sg(cmd->vha, cmd);
3860
Quinn Tran33e79972014-09-25 06:14:55 -04003861 if (!cmd->q_full)
3862 qlt_decr_num_pend_cmds(cmd->vha);
3863
Quinn Tranf83adb62014-04-11 16:54:43 -04003864 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003865 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003866 if (unlikely(cmd->free_sg))
3867 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003868
3869 if (!sess || !sess->se_sess) {
3870 WARN_ON(1);
3871 return;
3872 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003873 cmd->jiffies_at_free = get_jiffies_64();
Matthew Wilcox83c2b542018-06-12 12:05:43 -07003874 target_free_tag(sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003875}
3876EXPORT_SYMBOL(qlt_free_cmd);
3877
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003878/*
3879 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3880 */
Quinn Tran82de8022017-06-13 20:47:17 -07003881static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003882 struct qla_tgt_cmd *cmd, uint32_t status)
3883{
3884 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003885 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003886
Quinn Tranbe251522017-03-15 09:48:49 -07003887 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003888 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003889 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3890 "se_cmd=%p tag[%x] op %#x/%s",
3891 cmd->lba, cmd->lba,
3892 cmd->num_blks, &cmd->se_cmd,
3893 cmd->atio.u.isp24.exchange_addr,
3894 cmd->se_cmd.prot_op,
3895 prot_op_str(cmd->se_cmd.prot_op));
3896
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003897 if (ctio != NULL) {
3898 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07003899
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003900 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003901 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003902 } else
3903 term = 1;
3904
3905 if (term)
himanshu.madhani@cavium.com3efc31f2018-01-15 20:46:47 -08003906 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003907
3908 return term;
3909}
3910
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003911
3912/* ha->hardware_lock supposed to be held on entry */
Quinn Tran6b0431d2018-09-04 14:19:13 -07003913static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003914 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003915{
Quinn Tran6b0431d2018-09-04 14:19:13 -07003916 void *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003917 struct req_que *req;
3918 int qid = GET_QID(handle);
3919 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003920
Quinn Tran82de8022017-06-13 20:47:17 -07003921 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3922 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003923
Quinn Tran82de8022017-06-13 20:47:17 -07003924 if (qid == rsp->req->id) {
3925 req = rsp->req;
3926 } else if (vha->hw->req_q_map[qid]) {
3927 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3928 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3929 vha->vp_idx, rsp->id, handle);
3930 req = vha->hw->req_q_map[qid];
3931 } else {
3932 return NULL;
3933 }
Arun Easi667024a2014-09-25 06:14:47 -04003934
Quinn Tran82de8022017-06-13 20:47:17 -07003935 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003936
Quinn Tran82de8022017-06-13 20:47:17 -07003937 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003938 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003939 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3940 "qla_target(%d): Wrong handle %x received\n",
3941 vha->vp_idx, handle);
3942 return NULL;
3943 }
Quinn Tran82de8022017-06-13 20:47:17 -07003944
Quinn Tran6b0431d2018-09-04 14:19:13 -07003945 cmd = (void *) req->outstanding_cmds[h];
Quinn Tran82de8022017-06-13 20:47:17 -07003946 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003947 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003948 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3949 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003950 return NULL;
3951 }
Quinn Tran82de8022017-06-13 20:47:17 -07003952 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003953 } else if (ctio != NULL) {
3954 /* We can't get loop ID from CTIO7 */
3955 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3956 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3957 "support NULL handles\n", vha->vp_idx);
3958 return NULL;
3959 }
3960
3961 return cmd;
3962}
3963
Arun Easic0cb4492014-09-25 06:14:51 -04003964/* hardware_lock should be held by caller. */
Quinn Tranc5419e22017-06-13 20:47:16 -07003965void
Arun Easic0cb4492014-09-25 06:14:51 -04003966qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3967{
3968 struct qla_hw_data *ha = vha->hw;
Arun Easic0cb4492014-09-25 06:14:51 -04003969
3970 if (cmd->sg_mapped)
3971 qlt_unmap_sg(vha, cmd);
3972
Arun Easic0cb4492014-09-25 06:14:51 -04003973 /* TODO: fix debug message type and ids. */
3974 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3975 ql_dbg(ql_dbg_io, vha, 0xff00,
Quinn Tran82de8022017-06-13 20:47:17 -07003976 "HOST-ABORT: state=PROCESSED.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003977 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3978 cmd->write_data_transferred = 0;
3979 cmd->state = QLA_TGT_STATE_DATA_IN;
3980
3981 ql_dbg(ql_dbg_io, vha, 0xff01,
Quinn Tran82de8022017-06-13 20:47:17 -07003982 "HOST-ABORT: state=DATA_IN.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003983
3984 ha->tgt.tgt_ops->handle_data(cmd);
3985 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003986 } else {
3987 ql_dbg(ql_dbg_io, vha, 0xff03,
Quinn Tran82de8022017-06-13 20:47:17 -07003988 "HOST-ABORT: state=BAD(%d).\n",
Arun Easic0cb4492014-09-25 06:14:51 -04003989 cmd->state);
3990 dump_stack();
3991 }
3992
Quinn Tran1eb42f92017-01-19 22:27:55 -08003993 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003994 ha->tgt.tgt_ops->free_cmd(cmd);
3995}
3996
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003997/*
3998 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3999 */
Quinn Tran82de8022017-06-13 20:47:17 -07004000static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
4001 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004002{
4003 struct qla_hw_data *ha = vha->hw;
4004 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004005 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07004006 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004007
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004008 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
4009 /* That could happen only in case of an error/reset/abort */
4010 if (status != CTIO_SUCCESS) {
4011 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
4012 "Intermediate CTIO received"
4013 " (status %x)\n", status);
4014 }
4015 return;
4016 }
4017
Bart Van Assche81bcf1c2019-04-11 14:53:24 -07004018 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07004019 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004020 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07004021
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004022 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04004023 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004024
Joern Engelf9b67212014-09-16 16:23:18 -04004025 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004026
4027 if (unlikely(status != CTIO_SUCCESS)) {
4028 switch (status & 0xFFFF) {
Quinn Tran079a3a32018-09-04 14:19:11 -07004029 case CTIO_INVALID_RX_ID:
4030 if (printk_ratelimit())
4031 dev_info(&vha->hw->pdev->dev,
4032 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4033 vha->vp_idx, cmd->atio.u.isp24.attr,
4034 ((cmd->ctio_flags >> 9) & 0xf),
4035 cmd->ctio_flags);
4036
4037 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004038 case CTIO_LIP_RESET:
4039 case CTIO_TARGET_RESET:
4040 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04004041 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004042 case CTIO_TIMEOUT:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004043 /* They are OK */
4044 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4045 "qla_target(%d): CTIO with "
4046 "status %#x received, state %x, se_cmd %p, "
4047 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4048 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4049 status, cmd->state, se_cmd);
4050 break;
4051
4052 case CTIO_PORT_LOGGED_OUT:
4053 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004054 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08004055 int logged_out =
4056 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4057
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004058 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004059 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004060 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08004061 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004062 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004063
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004064 if (logged_out && cmd->sess) {
4065 /*
4066 * Session is already logged out, but we need
4067 * to notify initiator, who's not aware of this
4068 */
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004069 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07004070 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08004071 "%s %d %8phC post del sess\n",
4072 __func__, __LINE__, cmd->sess->port_name);
4073
Quinn Trand8630bb2017-12-28 12:33:43 -08004074 qlt_schedule_sess_for_deletion(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004075 }
4076 break;
4077 }
Quinn Tranf83adb62014-04-11 16:54:43 -04004078 case CTIO_DIF_ERROR: {
4079 struct ctio_crc_from_fw *crc =
4080 (struct ctio_crc_from_fw *)ctio;
4081 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07004082 "qla_target(%d): CTIO with DIF_ERROR status %x "
4083 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4084 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04004085 vha->vp_idx, status, cmd->state, se_cmd,
4086 *((u64 *)&crc->actual_dif[0]),
4087 *((u64 *)&crc->expected_dif[0]));
4088
Quinn Tran8abfa9e2017-06-13 20:47:24 -07004089 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07004090 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04004091 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004092 default:
4093 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04004094 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004095 vha->vp_idx, status, cmd->state, se_cmd);
4096 break;
4097 }
4098
Quinn Tran7b898542014-04-11 16:54:44 -04004099
Quinn Tran193b50b2015-12-17 14:57:03 -05004100 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04004101 * cmd is already aborted/terminated, we don't
4102 * need to terminate again. The exchange is already
4103 * cleaned up/freed at FW level. Just cleanup at driver
4104 * level.
4105 */
4106 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004107 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004108 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004109 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004110 return;
Quinn Tran7b898542014-04-11 16:54:44 -04004111 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004112 }
4113
4114 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004115 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004116 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004117 cmd->state = QLA_TGT_STATE_DATA_IN;
4118
Bart Van Assche52c82822015-07-09 07:23:26 -07004119 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004120 cmd->write_data_transferred = 1;
4121
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004122 ha->tgt.tgt_ops->handle_data(cmd);
4123 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05004124 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004125 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004126 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02004127 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004128 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004129 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004130 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4131 "qla_target(%d): A command in state (%d) should "
4132 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4133 }
4134
Quinn Tran7b898542014-04-11 16:54:44 -04004135 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004136 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004137 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4138 dump_stack();
4139 }
4140
4141 ha->tgt.tgt_ops->free_cmd(cmd);
4142}
4143
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004144static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4145 uint8_t task_codes)
4146{
4147 int fcp_task_attr;
4148
4149 switch (task_codes) {
4150 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004151 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004152 break;
4153 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004154 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004155 break;
4156 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004157 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004158 break;
4159 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004160 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004161 break;
4162 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004163 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004164 break;
4165 default:
4166 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4167 "qla_target: unknown task code %x, use ORDERED instead\n",
4168 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004169 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004170 break;
4171 }
4172
4173 return fcp_task_attr;
4174}
4175
Quinn Tran5d964832017-01-19 22:27:59 -08004176static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004177 uint8_t *);
4178/*
4179 * Process context for I/O path into tcm_qla2xxx code
4180 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004181static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004182{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004183 scsi_qla_host_t *vha = cmd->vha;
4184 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004185 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004186 struct atio_from_isp *atio = &cmd->atio;
4187 unsigned char *cdb;
4188 unsigned long flags;
4189 uint32_t data_length;
4190 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07004191 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004192
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004193 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004194 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004195
Quinn Tran193b50b2015-12-17 14:57:03 -05004196 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004197 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4198 "cmd with tag %u is aborted\n",
4199 cmd->atio.u.isp24.exchange_addr);
4200 goto out_term;
4201 }
4202
Quinn Trana07100e2015-12-07 19:48:57 -05004203 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004204 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004205 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004206
4207 if (atio->u.isp24.fcp_cmnd.rddata &&
4208 atio->u.isp24.fcp_cmnd.wrdata) {
4209 bidi = 1;
4210 data_dir = DMA_TO_DEVICE;
4211 } else if (atio->u.isp24.fcp_cmnd.rddata)
4212 data_dir = DMA_FROM_DEVICE;
4213 else if (atio->u.isp24.fcp_cmnd.wrdata)
4214 data_dir = DMA_TO_DEVICE;
4215 else
4216 data_dir = DMA_NONE;
4217
4218 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4219 atio->u.isp24.fcp_cmnd.task_attr);
Quinn Trane25f7652018-05-01 09:01:50 -07004220 data_length = get_datalen_for_atio(atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004221
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004222 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4223 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004224 if (ret != 0)
4225 goto out_term;
4226 /*
Bart Van Assche67eb4a602019-04-17 14:44:10 -07004227 * Drop extra session reference from qlt_handle_cmd_for_atio().
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004228 */
Quinn Tran75601512015-12-17 14:57:04 -05004229 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004230 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004231 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004232 return;
4233
4234out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004235 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004236 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004237 * cmd has not sent to target yet, so pass NULL as the second
4238 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004239 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004240 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004241 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4242 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004243
4244 qlt_decr_num_pend_cmds(vha);
Matthew Wilcox83c2b542018-06-12 12:05:43 -07004245 target_free_tag(sess->se_sess, &cmd->se_cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07004246 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004247
4248 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004249 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004250 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004251}
4252
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004253static void qlt_do_work(struct work_struct *work)
4254{
4255 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004256 scsi_qla_host_t *vha = cmd->vha;
4257 unsigned long flags;
4258
4259 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4260 list_del(&cmd->cmd_list);
4261 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004262
4263 __qlt_do_work(cmd);
4264}
4265
Quinn Tran09620ee2017-06-13 20:47:20 -07004266void qlt_clr_qp_table(struct scsi_qla_host *vha)
4267{
4268 unsigned long flags;
4269 struct qla_hw_data *ha = vha->hw;
4270 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4271 void *node;
4272 u64 key = 0;
4273
4274 ql_log(ql_log_info, vha, 0x706c,
4275 "User update Number of Active Qpairs %d\n",
4276 ha->tgt.num_act_qpairs);
4277
4278 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4279
4280 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4281 btree_remove64(&tgt->lun_qpair_map, key);
4282
4283 ha->base_qpair->lun_cnt = 0;
4284 for (key = 0; key < ha->max_qpairs; key++)
4285 if (ha->queue_pair_map[key])
4286 ha->queue_pair_map[key]->lun_cnt = 0;
4287
4288 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4289}
4290
Quinn Trane326d222017-06-13 20:47:18 -07004291static void qlt_assign_qpair(struct scsi_qla_host *vha,
4292 struct qla_tgt_cmd *cmd)
4293{
4294 struct qla_qpair *qpair, *qp;
4295 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4296 struct qla_qpair_hint *h;
4297
4298 if (vha->flags.qpairs_available) {
4299 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4300 if (unlikely(!h)) {
4301 /* spread lun to qpair ratio evently */
4302 int lcnt = 0, rc;
4303 struct scsi_qla_host *base_vha =
4304 pci_get_drvdata(vha->hw->pdev);
4305
4306 qpair = vha->hw->base_qpair;
4307 if (qpair->lun_cnt == 0) {
4308 qpair->lun_cnt++;
4309 h = qla_qpair_to_hint(tgt, qpair);
4310 BUG_ON(!h);
4311 rc = btree_insert64(&tgt->lun_qpair_map,
4312 cmd->unpacked_lun, h, GFP_ATOMIC);
4313 if (rc) {
4314 qpair->lun_cnt--;
4315 ql_log(ql_log_info, vha, 0xd037,
4316 "Unable to insert lun %llx into lun_qpair_map\n",
4317 cmd->unpacked_lun);
4318 }
4319 goto out;
4320 } else {
4321 lcnt = qpair->lun_cnt;
4322 }
4323
4324 h = NULL;
4325 list_for_each_entry(qp, &base_vha->qp_list,
4326 qp_list_elem) {
4327 if (qp->lun_cnt == 0) {
4328 qp->lun_cnt++;
4329 h = qla_qpair_to_hint(tgt, qp);
4330 BUG_ON(!h);
4331 rc = btree_insert64(&tgt->lun_qpair_map,
4332 cmd->unpacked_lun, h, GFP_ATOMIC);
4333 if (rc) {
4334 qp->lun_cnt--;
4335 ql_log(ql_log_info, vha, 0xd038,
4336 "Unable to insert lun %llx into lun_qpair_map\n",
4337 cmd->unpacked_lun);
4338 }
4339 qpair = qp;
4340 goto out;
4341 } else {
4342 if (qp->lun_cnt < lcnt) {
4343 lcnt = qp->lun_cnt;
4344 qpair = qp;
4345 continue;
4346 }
4347 }
4348 }
4349 BUG_ON(!qpair);
4350 qpair->lun_cnt++;
4351 h = qla_qpair_to_hint(tgt, qpair);
4352 BUG_ON(!h);
4353 rc = btree_insert64(&tgt->lun_qpair_map,
4354 cmd->unpacked_lun, h, GFP_ATOMIC);
4355 if (rc) {
4356 qpair->lun_cnt--;
4357 ql_log(ql_log_info, vha, 0xd039,
4358 "Unable to insert lun %llx into lun_qpair_map\n",
4359 cmd->unpacked_lun);
4360 }
4361 }
4362 } else {
4363 h = &tgt->qphints[0];
4364 }
4365out:
4366 cmd->qpair = h->qpair;
4367 cmd->se_cmd.cpuid = h->cpuid;
4368}
4369
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004370static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004371 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004372 struct atio_from_isp *atio)
4373{
4374 struct se_session *se_sess = sess->se_sess;
4375 struct qla_tgt_cmd *cmd;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004376 int tag, cpu;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004377
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004378 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004379 if (tag < 0)
4380 return NULL;
4381
4382 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4383 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004384 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004385 memcpy(&cmd->atio, atio, sizeof(*atio));
4386 cmd->state = QLA_TGT_STATE_NEW;
4387 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004388 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004389 cmd->vha = vha;
4390 cmd->se_cmd.map_tag = tag;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004391 cmd->se_cmd.map_cpu = cpu;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004392 cmd->sess = sess;
4393 cmd->loop_id = sess->loop_id;
4394 cmd->conf_compl_supported = sess->conf_compl_supported;
4395
Quinn Tran1eb42f92017-01-19 22:27:55 -08004396 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004397 cmd->jiffies_at_alloc = get_jiffies_64();
4398
Quinn Trane326d222017-06-13 20:47:18 -07004399 cmd->unpacked_lun = scsilun_to_int(
4400 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4401 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004402 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004403 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004404
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004405 return cmd;
4406}
4407
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004408/* ha->hardware_lock supposed to be held on entry */
4409static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4410 struct atio_from_isp *atio)
4411{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004412 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004413 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004414 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004415 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004416 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08004417 port_id_t id;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004418
4419 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004420 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004421 "New command while device %p is shutting down\n", tgt);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004422 return -ENODEV;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004423 }
4424
Quinn Trana4239942017-12-28 12:33:26 -08004425 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4426 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4427 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4428 if (IS_SW_RESV_ADDR(id))
4429 return -EBUSY;
4430
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004431 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 -08004432 if (unlikely(!sess))
4433 return -EFAULT;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004434
4435 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4436 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004437 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004438 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004439 "New command while old session %p is being deleted\n",
4440 sess);
4441 return -EFAULT;
4442 }
4443
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004444 /*
4445 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4446 */
Quinn Tran726b8542017-01-19 22:28:00 -08004447 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004448 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004449 "%s: kref_get fail, %8phC oxid %x \n",
4450 __func__, sess->port_name,
4451 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4452 return -EFAULT;
4453 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004454
4455 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004456 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004457 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004458 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004459 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4460 ha->tgt.tgt_ops->put_sess(sess);
4461 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004462 return -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004463 }
4464
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004465 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004466 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004467
Quinn Tran726b8542017-01-19 22:28:00 -08004468 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004469 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004470 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004471
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004472 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004473 if (vha->flags.qpairs_available) {
4474 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4475 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004476 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4477 queue_work_on(smp_processor_id(), qla_tgt_wq,
4478 &cmd->work);
4479 else
4480 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4481 &cmd->work);
4482 } else {
4483 queue_work(qla_tgt_wq, &cmd->work);
4484 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004485
Quinn Tran82de8022017-06-13 20:47:17 -07004486 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004487}
4488
4489/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004490static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004491 int fn, void *iocb, int flags)
4492{
4493 struct scsi_qla_host *vha = sess->vha;
4494 struct qla_hw_data *ha = vha->hw;
4495 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004496 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Quinn Tran84905df2018-05-01 09:01:53 -07004497 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004498
4499 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4500 if (!mcmd) {
4501 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4502 "qla_target(%d): Allocation of management "
4503 "command failed, some commands and their data could "
4504 "leak\n", vha->vp_idx);
4505 return -ENOMEM;
4506 }
4507 memset(mcmd, 0, sizeof(*mcmd));
4508 mcmd->sess = sess;
4509
4510 if (iocb) {
4511 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4512 sizeof(mcmd->orig_iocb.imm_ntfy));
4513 }
4514 mcmd->tmr_func = fn;
4515 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004516 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran84905df2018-05-01 09:01:53 -07004517 mcmd->qpair = h->qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004518 mcmd->vha = vha;
Quinn Tran84905df2018-05-01 09:01:53 -07004519 mcmd->se_cmd.cpuid = h->cpuid;
4520 mcmd->unpacked_lun = lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004521
4522 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004523 case QLA_TGT_LUN_RESET:
Quinn Tran84905df2018-05-01 09:01:53 -07004524 case QLA_TGT_CLEAR_TS:
4525 case QLA_TGT_ABORT_TS:
4526 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
Bart Van Assche50435d42018-10-18 15:45:40 -07004527 /* fall through */
Quinn Tran84905df2018-05-01 09:01:53 -07004528 case QLA_TGT_CLEAR_ACA:
4529 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4530 mcmd->qpair = h->qpair;
4531 mcmd->se_cmd.cpuid = h->cpuid;
4532 break;
4533
4534 case QLA_TGT_TARGET_RESET:
4535 case QLA_TGT_NEXUS_LOSS_SESS:
4536 case QLA_TGT_NEXUS_LOSS:
4537 case QLA_TGT_ABORT_ALL:
4538 default:
4539 /* no-op */
4540 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004541 }
4542
Quinn Tran84905df2018-05-01 09:01:53 -07004543 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4544 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4545 &mcmd->work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004546
4547 return 0;
4548}
4549
4550/* ha->hardware_lock supposed to be held on entry */
4551static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4552{
4553 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4554 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004555 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004556 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004557 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004558 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004559
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004560 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004561
4562 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004563 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4564 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004565 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4566
Quinn Tranf775bd12017-06-02 09:11:59 -07004567 unpacked_lun =
4568 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004569
Quinn Tranfb352652017-12-28 12:33:32 -08004570 if (sess == NULL || sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004571 return -EFAULT;
4572
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004573 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4574}
4575
4576/* ha->hardware_lock supposed to be held on entry */
4577static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004578 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004579{
4580 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4581 struct qla_hw_data *ha = vha->hw;
4582 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004583 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004584 int rc;
4585
4586 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4587 if (mcmd == NULL) {
4588 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4589 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4590 vha->vp_idx, __func__);
4591 return -ENOMEM;
4592 }
4593 memset(mcmd, 0, sizeof(*mcmd));
4594
4595 mcmd->sess = sess;
4596 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4597 sizeof(mcmd->orig_iocb.imm_ntfy));
4598
Quinn Tranf775bd12017-06-02 09:11:59 -07004599 unpacked_lun =
4600 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004601 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004602 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004603 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004604
Quinn Tranbe92fc32017-01-19 22:27:54 -08004605 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004606 le16_to_cpu(iocb->u.isp2x.seq_id));
4607 if (rc != 0) {
4608 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4609 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4610 vha->vp_idx, rc);
4611 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4612 return -EFAULT;
4613 }
4614
4615 return 0;
4616}
4617
4618/* ha->hardware_lock supposed to be held on entry */
4619static int qlt_abort_task(struct scsi_qla_host *vha,
4620 struct imm_ntfy_from_isp *iocb)
4621{
4622 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004623 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004624 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004625 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004626
4627 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4628
Quinn Tran75601512015-12-17 14:57:04 -05004629 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004630 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004631 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4632
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004633 if (sess == NULL) {
4634 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4635 "qla_target(%d): task abort for unexisting "
4636 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004637 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004638 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4639 }
4640
4641 return __qlt_abort_task(vha, iocb, sess);
4642}
4643
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004644void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4645{
Quinn Tran726b8542017-01-19 22:28:00 -08004646 if (rc != MBS_COMMAND_COMPLETE) {
4647 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4648 "%s: se_sess %p / sess %p from"
4649 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4650 " LOGO failed: %#x\n",
4651 __func__,
4652 fcport->se_sess,
4653 fcport,
4654 fcport->port_name, fcport->loop_id,
4655 fcport->d_id.b.domain, fcport->d_id.b.area,
4656 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004657 }
Quinn Tran726b8542017-01-19 22:28:00 -08004658
4659 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004660}
4661
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004662/*
4663* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4664*
4665* Schedules sessions with matching port_id/loop_id but different wwn for
4666* deletion. Returns existing session with matching wwn if present.
4667* Null otherwise.
4668*/
Quinn Tran726b8542017-01-19 22:28:00 -08004669struct fc_port *
4670qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004671 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004672{
Quinn Tran5d964832017-01-19 22:27:59 -08004673 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004674 uint64_t other_wwn;
4675
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004676 *conflict_sess = NULL;
4677
Quinn Tran5d964832017-01-19 22:27:59 -08004678 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004679
4680 other_wwn = wwn_to_u64(other_sess->port_name);
4681
4682 if (wwn == other_wwn) {
4683 WARN_ON(sess);
4684 sess = other_sess;
4685 continue;
4686 }
4687
4688 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004689 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004690 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004691 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004692 "Invalidating sess %p loop_id %d wwn %llx.\n",
4693 other_sess, other_sess->loop_id, other_wwn);
4694
4695 /*
4696 * logout_on_delete is set by default, but another
4697 * session that has the same s_id/loop_id combo
4698 * might have cleared it when requested this session
4699 * deletion, so don't touch it
4700 */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004701 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004702 } else {
4703 /*
4704 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004705 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004706 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004707 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004708 "Invalidating sess %p loop_id %d wwn %llx.\n",
4709 other_sess, other_sess->loop_id, other_wwn);
4710
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004711 other_sess->keep_nport_handle = 1;
Quinn Tranba743f92017-12-04 14:45:12 -08004712 if (other_sess->disc_state != DSC_DELETED)
4713 *conflict_sess = other_sess;
Quinn Tran94cff6e2017-12-28 12:33:42 -08004714 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004715 }
4716 continue;
4717 }
4718
4719 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004720 if ((loop_id == other_sess->loop_id) &&
4721 (loop_id != FC_NO_LOOP_ID)) {
4722 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004723 "Invalidating sess %p loop_id %d wwn %llx.\n",
4724 other_sess, other_sess->loop_id, other_wwn);
4725
4726 /* Same loop_id but different s_id
4727 * Ok to kill and logout */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004728 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004729 }
4730 }
4731
4732 return sess;
4733}
4734
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004735/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4736static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4737{
4738 struct qla_tgt_sess_op *op;
4739 struct qla_tgt_cmd *cmd;
4740 uint32_t key;
4741 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004742 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004743
4744 key = (((u32)s_id->b.domain << 16) |
4745 ((u32)s_id->b.area << 8) |
4746 ((u32)s_id->b.al_pa));
4747
Quinn Tran8b631d82017-06-02 09:11:54 -07004748 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004749 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4750 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004751
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004752 if (op_key == key) {
4753 op->aborted = true;
4754 count++;
4755 }
4756 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004757
4758 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4759 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004760
Quinn Tran41dc5292017-01-19 22:28:03 -08004761 if (op_key == key) {
4762 op->aborted = true;
4763 count++;
4764 }
4765 }
4766
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004767 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4768 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004769
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004770 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004771 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004772 count++;
4773 }
4774 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004775 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004776
4777 return count;
4778}
4779
Quinn Tran9cd883f2017-12-28 12:33:24 -08004780static int qlt_handle_login(struct scsi_qla_host *vha,
4781 struct imm_ntfy_from_isp *iocb)
4782{
4783 struct fc_port *sess = NULL, *conflict_sess = NULL;
4784 uint64_t wwn;
4785 port_id_t port_id;
4786 uint16_t loop_id, wd3_lo;
4787 int res = 0;
4788 struct qlt_plogi_ack_t *pla;
4789 unsigned long flags;
4790
4791 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4792
4793 port_id.b.domain = iocb->u.isp24.port_id[2];
4794 port_id.b.area = iocb->u.isp24.port_id[1];
4795 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4796 port_id.b.rsvd_1 = 0;
4797
4798 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4799
4800 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4801 abort_cmds_for_s_id(vha, &port_id);
4802
4803 if (wwn) {
4804 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4805 sess = qlt_find_sess_invalidate_other(vha, wwn,
4806 port_id, loop_id, &conflict_sess);
4807 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Tranaa9e6d72018-08-31 11:24:38 -07004808 } else {
4809 ql_dbg(ql_dbg_disc, vha, 0xffff,
4810 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4811 __func__, __LINE__, loop_id, port_id.b24);
4812 qlt_send_term_imm_notif(vha, iocb, 1);
4813 goto out;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004814 }
4815
4816 if (IS_SW_RESV_ADDR(port_id)) {
4817 res = 1;
4818 goto out;
4819 }
4820
4821 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4822 if (!pla) {
Quinn Tran48acad02018-08-02 13:16:44 -07004823 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4824 "%s %d %8phC Term INOT due to mem alloc fail",
4825 __func__, __LINE__,
4826 iocb->u.isp24.port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004827 qlt_send_term_imm_notif(vha, iocb, 1);
4828 goto out;
4829 }
4830
4831 if (conflict_sess) {
4832 conflict_sess->login_gen++;
4833 qlt_plogi_ack_link(vha, pla, conflict_sess,
4834 QLT_PLOGI_LINK_CONFLICT);
4835 }
4836
4837 if (!sess) {
4838 pla->ref_count++;
4839 ql_dbg(ql_dbg_disc, vha, 0xffff,
4840 "%s %d %8phC post new sess\n",
4841 __func__, __LINE__, iocb->u.isp24.port_name);
Quinn Trana4239942017-12-28 12:33:26 -08004842 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4843 qla24xx_post_newsess_work(vha, &port_id,
4844 iocb->u.isp24.port_name,
4845 iocb->u.isp24.u.plogi.node_name,
4846 pla, FC4_TYPE_UNKNOWN);
4847 else
4848 qla24xx_post_newsess_work(vha, &port_id,
4849 iocb->u.isp24.port_name, NULL,
4850 pla, FC4_TYPE_UNKNOWN);
4851
Quinn Tran9cd883f2017-12-28 12:33:24 -08004852 goto out;
4853 }
4854
Quinn Trancd4ed6b2018-08-31 11:24:31 -07004855 if (sess->disc_state == DSC_UPD_FCPORT) {
4856 u16 sec;
4857
4858 /*
4859 * Remote port registration is still going on from
4860 * previous login. Allow it to finish before we
4861 * accept the new login.
4862 */
4863 sess->next_disc_state = DSC_DELETE_PEND;
4864 sec = jiffies_to_msecs(jiffies -
4865 sess->jiffies_at_registration) / 1000;
4866 if (sess->sec_since_registration < sec && sec &&
4867 !(sec % 5)) {
4868 sess->sec_since_registration = sec;
4869 ql_dbg(ql_dbg_disc, vha, 0xffff,
4870 "%s %8phC - Slow Rport registration (%d Sec)\n",
4871 __func__, sess->port_name, sec);
4872 }
4873
4874 if (!conflict_sess)
4875 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4876
4877 qlt_send_term_imm_notif(vha, iocb, 1);
4878 goto out;
4879 }
4880
Quinn Tran9cd883f2017-12-28 12:33:24 -08004881 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4882 sess->d_id = port_id;
4883 sess->login_gen++;
4884
4885 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4886 sess->fw_login_state = DSC_LS_PRLI_PEND;
4887 sess->local = 0;
4888 sess->loop_id = loop_id;
4889 sess->d_id = port_id;
4890 sess->fw_login_state = DSC_LS_PRLI_PEND;
4891 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4892
4893 if (wd3_lo & BIT_7)
4894 sess->conf_compl_supported = 1;
4895
4896 if ((wd3_lo & BIT_4) == 0)
4897 sess->port_type = FCT_INITIATOR;
4898 else
4899 sess->port_type = FCT_TARGET;
4900
4901 } else
4902 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4903
4904
4905 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4906 "%s %d %8phC DS %d\n",
4907 __func__, __LINE__, sess->port_name, sess->disc_state);
4908
4909 switch (sess->disc_state) {
4910 case DSC_DELETED:
4911 qlt_plogi_ack_unref(vha, pla);
4912 break;
4913
4914 default:
4915 /*
4916 * Under normal circumstances we want to release nport handle
4917 * during LOGO process to avoid nport handle leaks inside FW.
4918 * The exception is when LOGO is done while another PLOGI with
4919 * the same nport handle is waiting as might be the case here.
4920 * Note: there is always a possibily of a race where session
4921 * deletion has already started for other reasons (e.g. ACL
4922 * removal) and now PLOGI arrives:
4923 * 1. if PLOGI arrived in FW after nport handle has been freed,
4924 * FW must have assigned this PLOGI a new/same handle and we
4925 * can proceed ACK'ing it as usual when session deletion
4926 * completes.
4927 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4928 * bit reached it, the handle has now been released. We'll
4929 * get an error when we ACK this PLOGI. Nothing will be sent
4930 * back to initiator. Initiator should eventually retry
4931 * PLOGI and situation will correct itself.
4932 */
4933 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4934 (sess->d_id.b24 == port_id.b24));
4935
4936 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4937 "%s %d %8phC post del sess\n",
4938 __func__, __LINE__, sess->port_name);
4939
4940
Quinn Trand8630bb2017-12-28 12:33:43 -08004941 qlt_schedule_sess_for_deletion(sess);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004942 break;
4943 }
4944out:
4945 return res;
4946}
4947
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004948/*
4949 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4950 */
4951static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4952 struct imm_ntfy_from_isp *iocb)
4953{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004954 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004955 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004956 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004957 uint64_t wwn;
4958 port_id_t port_id;
4959 uint16_t loop_id;
4960 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004961 int res = 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004962 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004963
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004964 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4965
4966 port_id.b.domain = iocb->u.isp24.port_id[2];
4967 port_id.b.area = iocb->u.isp24.port_id[1];
4968 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4969 port_id.b.rsvd_1 = 0;
4970
4971 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4972
Quinn Tran726b8542017-01-19 22:28:00 -08004973 ql_dbg(ql_dbg_disc, vha, 0xf026,
4974 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4975 vha->vp_idx, iocb->u.isp24.port_id[2],
4976 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4977 iocb->u.isp24.status_subcode, loop_id,
4978 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004979
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004980 /* res = 1 means ack at the end of thread
4981 * res = 0 means ack async/later.
4982 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004983 switch (iocb->u.isp24.status_subcode) {
4984 case ELS_PLOGI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004985 res = qlt_handle_login(vha, iocb);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004986 break;
4987
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004988 case ELS_PRLI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004989 if (N2N_TOPO(ha)) {
4990 sess = qla2x00_find_fcport_by_wwpn(vha,
4991 iocb->u.isp24.port_name, 1);
4992
4993 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4994 ql_dbg(ql_dbg_disc, vha, 0xffff,
4995 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4996 __func__, __LINE__,
4997 iocb->u.isp24.port_name);
4998 qlt_send_term_imm_notif(vha, iocb, 1);
4999 break;
5000 }
5001
5002 res = qlt_handle_login(vha, iocb);
5003 break;
5004 }
5005
Quinn Trana4239942017-12-28 12:33:26 -08005006 if (IS_SW_RESV_ADDR(port_id)) {
5007 res = 1;
5008 break;
5009 }
5010
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005011 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5012
Quinn Tran75601512015-12-17 14:57:04 -05005013 if (wwn) {
5014 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08005015 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5016 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05005017 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5018 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05005019
5020 if (conflict_sess) {
Quinn Tran5ef696a2017-12-04 14:45:05 -08005021 switch (conflict_sess->disc_state) {
5022 case DSC_DELETED:
5023 case DSC_DELETE_PEND:
5024 break;
5025 default:
5026 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5027 "PRLI with conflicting sess %p port %8phC\n",
5028 conflict_sess, conflict_sess->port_name);
5029 conflict_sess->fw_login_state =
5030 DSC_LS_PORT_UNAVAIL;
5031 qlt_send_term_imm_notif(vha, iocb, 1);
5032 res = 0;
5033 break;
5034 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05005035 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005036
5037 if (sess != NULL) {
Quinn Trana4239942017-12-28 12:33:26 -08005038 bool delete = false;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07005039 int sec;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005040
Quinn Tran82abdca2017-12-28 12:33:22 -08005041 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5042 switch (sess->fw_login_state) {
Quinn Trana4239942017-12-28 12:33:26 -08005043 case DSC_LS_PLOGI_PEND:
Quinn Tran82abdca2017-12-28 12:33:22 -08005044 case DSC_LS_PLOGI_COMP:
5045 case DSC_LS_PRLI_COMP:
5046 break;
5047 default:
Quinn Trana4239942017-12-28 12:33:26 -08005048 delete = true;
5049 break;
5050 }
5051
5052 switch (sess->disc_state) {
Quinn Trancd4ed6b2018-08-31 11:24:31 -07005053 case DSC_UPD_FCPORT:
5054 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5055 flags);
5056
5057 sec = jiffies_to_msecs(jiffies -
5058 sess->jiffies_at_registration)/1000;
5059 if (sess->sec_since_registration < sec && sec &&
5060 !(sec % 5)) {
5061 sess->sec_since_registration = sec;
5062 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5063 "%s %8phC : Slow Rport registration(%d Sec)\n",
5064 __func__, sess->port_name, sec);
5065 }
5066 qlt_send_term_imm_notif(vha, iocb, 1);
5067 return 0;
5068
Quinn Trana4239942017-12-28 12:33:26 -08005069 case DSC_LOGIN_PEND:
5070 case DSC_GPDB:
Quinn Trana4239942017-12-28 12:33:26 -08005071 case DSC_LOGIN_COMPLETE:
5072 case DSC_ADISC:
5073 delete = false;
5074 break;
5075 default:
5076 break;
5077 }
5078
5079 if (delete) {
Quinn Tran82abdca2017-12-28 12:33:22 -08005080 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5081 flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005082 /*
5083 * Impatient initiator sent PRLI before last
5084 * PLOGI could finish. Will force him to re-try,
5085 * while last one finishes.
5086 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04005087 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005088 "sess %p PRLI received, before plogi ack.\n",
5089 sess);
5090 qlt_send_term_imm_notif(vha, iocb, 1);
5091 res = 0;
5092 break;
5093 }
5094
5095 /*
5096 * This shouldn't happen under normal circumstances,
5097 * since we have deleted the old session during PLOGI
5098 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04005099 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005100 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5101 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5102
5103 sess->local = 0;
5104 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08005105 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005106 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005107
5108 if (wd3_lo & BIT_7)
5109 sess->conf_compl_supported = 1;
5110
Quinn Tran726b8542017-01-19 22:28:00 -08005111 if ((wd3_lo & BIT_4) == 0)
5112 sess->port_type = FCT_INITIATOR;
5113 else
5114 sess->port_type = FCT_TARGET;
Quinn Tran82abdca2017-12-28 12:33:22 -08005115
5116 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005117 }
5118 res = 1; /* send notify ack */
5119
5120 /* Make session global (not used in fabric mode) */
5121 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005122 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005123 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07005124 "%s %d %8phC post nack\n",
5125 __func__, __LINE__, sess->port_name);
5126 qla24xx_post_nack_work(vha, sess, iocb,
5127 SRB_NACK_PRLI);
5128 res = 0;
5129 } else {
5130 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5131 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5132 qla2xxx_wake_dpc(vha);
5133 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005134 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005135 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005136 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07005137 "%s %d %8phC post nack\n",
5138 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08005139 qla24xx_post_nack_work(vha, sess, iocb,
5140 SRB_NACK_PRLI);
5141 res = 0;
5142 }
5143 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005144 break;
5145
Quinn Tran41dc5292017-01-19 22:28:03 -08005146 case ELS_TPRLO:
5147 if (le16_to_cpu(iocb->u.isp24.flags) &
5148 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5149 loop_id = 0xFFFF;
5150 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5151 res = 1;
5152 break;
5153 }
Bart Van Assche81881862017-12-07 16:02:46 -08005154 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005155 case ELS_LOGO:
5156 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08005157 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5158 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5159 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5160
5161 if (sess) {
5162 sess->login_gen++;
5163 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08005164 sess->logo_ack_needed = 1;
5165 memcpy(sess->iocb, iocb, IOCB_SIZE);
5166 }
5167
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005168 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08005169
Quinn Tran83548fe2017-06-02 09:12:01 -07005170 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08005171 "%s: logo %llx res %d sess %p ",
5172 __func__, wwn, res, sess);
5173 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08005174 /*
5175 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5176 * for LOGO_ACK & sess delete
5177 */
Quinn Tran726b8542017-01-19 22:28:00 -08005178 BUG_ON(!sess);
5179 res = 0;
5180 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08005181 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08005182 if (sess) {
Quinn Trand8630bb2017-12-28 12:33:43 -08005183 qlt_schedule_sess_for_deletion(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08005184 res = 0;
5185 }
5186 /* else logo will be ack */
5187 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005188 break;
5189 case ELS_PDISC:
5190 case ELS_ADISC:
5191 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005192 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005193
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005194 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005195 qlt_send_notify_ack(ha->base_qpair,
5196 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005197 tgt->link_reinit_iocb_pending = 0;
5198 }
Quinn Tran726b8542017-01-19 22:28:00 -08005199
5200 sess = qla2x00_find_fcport_by_wwpn(vha,
5201 iocb->u.isp24.port_name, 1);
5202 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005203 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08005204 "sess %p lid %d|%d DS %d LS %d\n",
5205 sess, sess->loop_id, loop_id,
5206 sess->disc_state, sess->fw_login_state);
5207 }
5208
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005209 res = 1; /* send notify ack */
5210 break;
5211 }
5212
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005213 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005214 default:
5215 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5216 "qla_target(%d): Unsupported ELS command %x "
5217 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5218 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5219 break;
5220 }
5221
Quinn Tran9cd883f2017-12-28 12:33:24 -08005222 ql_dbg(ql_dbg_disc, vha, 0xf026,
5223 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5224 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5225
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005226 return res;
5227}
5228
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005229/*
5230 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5231 */
5232static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5233 struct imm_ntfy_from_isp *iocb)
5234{
5235 struct qla_hw_data *ha = vha->hw;
5236 uint32_t add_flags = 0;
5237 int send_notify_ack = 1;
5238 uint16_t status;
5239
5240 status = le16_to_cpu(iocb->u.isp2x.status);
5241 switch (status) {
5242 case IMM_NTFY_LIP_RESET:
5243 {
5244 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5245 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5246 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5247 iocb->u.isp24.status_subcode);
5248
5249 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5250 send_notify_ack = 0;
5251 break;
5252 }
5253
5254 case IMM_NTFY_LIP_LINK_REINIT:
5255 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005256 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005257
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005258 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5259 "qla_target(%d): LINK REINIT (loop %#x, "
5260 "subcode %x)\n", vha->vp_idx,
5261 le16_to_cpu(iocb->u.isp24.nport_handle),
5262 iocb->u.isp24.status_subcode);
5263 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005264 qlt_send_notify_ack(ha->base_qpair,
5265 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005266 }
5267 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5268 tgt->link_reinit_iocb_pending = 1;
5269 /*
5270 * QLogic requires to wait after LINK REINIT for possible
5271 * PDISC or ADISC ELS commands
5272 */
5273 send_notify_ack = 0;
5274 break;
5275 }
5276
5277 case IMM_NTFY_PORT_LOGOUT:
5278 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5279 "qla_target(%d): Port logout (loop "
5280 "%#x, subcode %x)\n", vha->vp_idx,
5281 le16_to_cpu(iocb->u.isp24.nport_handle),
5282 iocb->u.isp24.status_subcode);
5283
5284 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5285 send_notify_ack = 0;
5286 /* The sessions will be cleared in the callback, if needed */
5287 break;
5288
5289 case IMM_NTFY_GLBL_TPRLO:
5290 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5291 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5292 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5293 send_notify_ack = 0;
5294 /* The sessions will be cleared in the callback, if needed */
5295 break;
5296
5297 case IMM_NTFY_PORT_CONFIG:
5298 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5299 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5300 status);
5301 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5302 send_notify_ack = 0;
5303 /* The sessions will be cleared in the callback, if needed */
5304 break;
5305
5306 case IMM_NTFY_GLBL_LOGO:
5307 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5308 "qla_target(%d): Link failure detected\n",
5309 vha->vp_idx);
5310 /* I_T nexus loss */
5311 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5312 send_notify_ack = 0;
5313 break;
5314
5315 case IMM_NTFY_IOCB_OVERFLOW:
5316 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5317 "qla_target(%d): Cannot provide requested "
5318 "capability (IOCB overflowed the immediate notify "
5319 "resource count)\n", vha->vp_idx);
5320 break;
5321
5322 case IMM_NTFY_ABORT_TASK:
5323 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5324 "qla_target(%d): Abort Task (S %08x I %#x -> "
5325 "L %#x)\n", vha->vp_idx,
5326 le16_to_cpu(iocb->u.isp2x.seq_id),
5327 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5328 le16_to_cpu(iocb->u.isp2x.lun));
5329 if (qlt_abort_task(vha, iocb) == 0)
5330 send_notify_ack = 0;
5331 break;
5332
5333 case IMM_NTFY_RESOURCE:
5334 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5335 "qla_target(%d): Out of resources, host %ld\n",
5336 vha->vp_idx, vha->host_no);
5337 break;
5338
5339 case IMM_NTFY_MSG_RX:
5340 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5341 "qla_target(%d): Immediate notify task %x\n",
5342 vha->vp_idx, iocb->u.isp2x.task_flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005343 break;
5344
5345 case IMM_NTFY_ELS:
5346 if (qlt_24xx_handle_els(vha, iocb) == 0)
5347 send_notify_ack = 0;
5348 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005349 default:
5350 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5351 "qla_target(%d): Received unknown immediate "
5352 "notify status %x\n", vha->vp_idx, status);
5353 break;
5354 }
5355
5356 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005357 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5358 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005359}
5360
5361/*
5362 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5363 * This function sends busy to ISP 2xxx or 24xx.
5364 */
Quinn Tran82de8022017-06-13 20:47:17 -07005365static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005366 struct atio_from_isp *atio, uint16_t status)
5367{
Quinn Tran82de8022017-06-13 20:47:17 -07005368 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005369 struct ctio7_to_24xx *ctio24;
5370 struct qla_hw_data *ha = vha->hw;
5371 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005372 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005373 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005374 u16 temp;
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005375 port_id_t id;
5376
5377 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5378 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5379 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5380 id.b.rsvd_1 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005381
Quinn Tran75601512015-12-17 14:57:04 -05005382 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005383 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
Quinn Tran75601512015-12-17 14:57:04 -05005384 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005385 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005386 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005387 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005388 }
5389 /* Sending marker isn't necessary, since we called from ISR */
5390
Quinn Tran82de8022017-06-13 20:47:17 -07005391 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005392 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005393 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005394 "qla_target(%d): %s failed: unable to allocate "
5395 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005396 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005397 }
5398
Quinn Tran60a9ead2017-06-13 20:47:28 -07005399 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005400 pkt->entry_count = 1;
5401 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5402
5403 ctio24 = (struct ctio7_to_24xx *)pkt;
5404 ctio24->entry_type = CTIO_TYPE7;
5405 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005406 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005407 ctio24->vp_index = vha->vp_idx;
5408 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5409 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5410 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5411 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005412 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005413 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005414 CTIO7_FLAGS_DONT_RET_CTIO;
5415 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005416 /*
5417 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5418 * if the explicit conformation is used.
5419 */
5420 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5421 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Quinn Trane25f7652018-05-01 09:01:50 -07005422
5423 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5424
5425 if (ctio24->u.status1.residual != 0)
5426 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5427
Himanshu Madhani63163e02014-09-25 06:14:59 -04005428 /* Memory Barrier */
5429 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005430 if (qpair->reqq_start_iocbs)
5431 qpair->reqq_start_iocbs(qpair);
5432 else
5433 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005434 return 0;
5435}
5436
5437/*
5438 * This routine is used to allocate a command for either a QFull condition
5439 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5440 * out previously.
5441 */
5442static void
5443qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5444 struct atio_from_isp *atio, uint16_t status, int qfull)
5445{
5446 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5447 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005448 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005449 struct se_session *se_sess;
5450 struct qla_tgt_cmd *cmd;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005451 int tag, cpu;
Quinn Tran82de8022017-06-13 20:47:17 -07005452 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005453
5454 if (unlikely(tgt->tgt_stop)) {
5455 ql_dbg(ql_dbg_io, vha, 0x300a,
5456 "New command while device %p is shutting down\n", tgt);
5457 return;
5458 }
5459
5460 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5461 vha->hw->tgt.num_qfull_cmds_dropped++;
5462 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005463 vha->qla_stats.stat_max_qfull_cmds_dropped)
5464 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005465 vha->hw->tgt.num_qfull_cmds_dropped;
5466
5467 ql_dbg(ql_dbg_io, vha, 0x3068,
5468 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5469 vha->vp_idx, __func__,
5470 vha->hw->tgt.num_qfull_cmds_dropped);
5471
5472 qlt_chk_exch_leak_thresh_hold(vha);
5473 return;
5474 }
5475
5476 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5477 (vha, atio->u.isp24.fcp_hdr.s_id);
5478 if (!sess)
5479 return;
5480
5481 se_sess = sess->se_sess;
5482
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005483 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Quinn Tran33e79972014-09-25 06:14:55 -04005484 if (tag < 0)
5485 return;
5486
5487 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5488 if (!cmd) {
5489 ql_dbg(ql_dbg_io, vha, 0x3009,
5490 "qla_target(%d): %s: Allocation of cmd failed\n",
5491 vha->vp_idx, __func__);
5492
5493 vha->hw->tgt.num_qfull_cmds_dropped++;
5494 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005495 vha->qla_stats.stat_max_qfull_cmds_dropped)
5496 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005497 vha->hw->tgt.num_qfull_cmds_dropped;
5498
5499 qlt_chk_exch_leak_thresh_hold(vha);
5500 return;
5501 }
5502
5503 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5504
5505 qlt_incr_num_pend_cmds(vha);
5506 INIT_LIST_HEAD(&cmd->cmd_list);
5507 memcpy(&cmd->atio, atio, sizeof(*atio));
5508
5509 cmd->tgt = vha->vha_tgt.qla_tgt;
5510 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005511 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005512 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005513 cmd->qpair = ha->base_qpair;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005514 cmd->se_cmd.map_cpu = cpu;
Quinn Tran33e79972014-09-25 06:14:55 -04005515
5516 if (qfull) {
5517 cmd->q_full = 1;
5518 /* NOTE: borrowing the state field to carry the status */
5519 cmd->state = status;
5520 } else
5521 cmd->term_exchg = 1;
5522
Quinn Tran82de8022017-06-13 20:47:17 -07005523 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005524 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5525
5526 vha->hw->tgt.num_qfull_cmds_alloc++;
5527 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005528 vha->qla_stats.stat_max_qfull_cmds_alloc)
5529 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005530 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005531 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005532}
5533
5534int
Quinn Tran82de8022017-06-13 20:47:17 -07005535qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005536{
Quinn Tran82de8022017-06-13 20:47:17 -07005537 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005538 struct qla_hw_data *ha = vha->hw;
5539 unsigned long flags;
5540 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005541 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005542 int rc = 0;
5543
5544 if (list_empty(&ha->tgt.q_full_list))
5545 return 0;
5546
5547 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005548 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005549
Quinn Tran82de8022017-06-13 20:47:17 -07005550 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005551 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005552 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005553 return 0;
5554 }
5555
Quinn Tran82de8022017-06-13 20:47:17 -07005556 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5557 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5558
5559 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5560 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005561 if (cmd->q_full)
5562 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005563 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005564 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005565 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005566
5567 if (rc == -ENOMEM)
5568 break;
5569
5570 if (cmd->q_full)
5571 ql_dbg(ql_dbg_io, vha, 0x3006,
5572 "%s: busy sent for ox_id[%04x]\n", __func__,
5573 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5574 else if (cmd->term_exchg)
5575 ql_dbg(ql_dbg_io, vha, 0x3007,
5576 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5577 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5578 else
5579 ql_dbg(ql_dbg_io, vha, 0x3008,
5580 "%s: Unexpected cmd in QFull list %p\n", __func__,
5581 cmd);
5582
5583 list_del(&cmd->cmd_list);
5584 list_add_tail(&cmd->cmd_list, &free_list);
5585
5586 /* piggy back on hardware_lock for protection */
5587 vha->hw->tgt.num_qfull_cmds_alloc--;
5588 }
Quinn Tran82de8022017-06-13 20:47:17 -07005589 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005590
5591 cmd = NULL;
5592
5593 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5594 list_del(&cmd->cmd_list);
5595 /* This cmd was never sent to TCM. There is no need
5596 * to schedule free or call free_cmd
5597 */
5598 qlt_free_cmd(cmd);
5599 }
Quinn Tran82de8022017-06-13 20:47:17 -07005600
5601 if (!list_empty(&q_full_list)) {
5602 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5603 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5604 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5605 }
5606
Quinn Tran33e79972014-09-25 06:14:55 -04005607 return rc;
5608}
5609
5610static void
Quinn Tran82de8022017-06-13 20:47:17 -07005611qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5612 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005613{
5614 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005615 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005616
Quinn Tran82de8022017-06-13 20:47:17 -07005617 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005618 if (rc == -ENOMEM)
5619 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5620}
5621
5622static int
Quinn Tran82de8022017-06-13 20:47:17 -07005623qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5624 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005625{
5626 struct qla_hw_data *ha = vha->hw;
Quinn Tran8b666802017-03-15 09:48:45 -07005627 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005628
5629 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5630 return 0;
5631
Quinn Tran8b666802017-03-15 09:48:45 -07005632 if (!ha_locked)
5633 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005634 qlt_send_busy(qpair, atio, qla_sam_status);
Quinn Tran8b666802017-03-15 09:48:45 -07005635 if (!ha_locked)
5636 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5637
Quinn Tran33e79972014-09-25 06:14:55 -04005638 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005639}
5640
5641/* ha->hardware_lock supposed to be held on entry */
5642/* called via callback from qla2xxx */
5643static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005644 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005645{
5646 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005647 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005648 int rc;
Quinn Tran7cf95f72017-12-28 12:33:28 -08005649 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005650
5651 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005652 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005653 "ATIO pkt, but no tgt (ha %p)", ha);
5654 return;
5655 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005656 /*
5657 * In tgt_stop mode we also should allow all requests to pass.
5658 * Otherwise, some commands can stuck.
5659 */
5660
Quinn Tran2f424b92015-12-17 14:57:07 -05005661 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005662
5663 switch (atio->u.raw.entry_type) {
5664 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005665 if (unlikely(atio->u.isp24.exchange_addr ==
5666 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005667 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005668 "qla_target(%d): ATIO_TYPE7 "
5669 "received with UNKNOWN exchange address, "
5670 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005671 if (!ha_locked)
5672 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005673 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
Quinn Tran2f424b92015-12-17 14:57:07 -05005674 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005675 spin_unlock_irqrestore(&ha->hardware_lock,
5676 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005677 break;
5678 }
Quinn Tran33e79972014-09-25 06:14:55 -04005679
Quinn Tran33e79972014-09-25 06:14:55 -04005680 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005681 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5682 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005683 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005684 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005685 return;
5686 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005687 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005688 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005689 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005690 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005691 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005692 if (!ha_locked)
5693 spin_lock_irqsave(&ha->hardware_lock, flags);
5694 switch (rc) {
5695 case -ENODEV:
5696 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5697 "qla_target: Unable to send command to target\n");
5698 break;
5699 case -EBADF:
5700 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5701 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005702 qlt_send_term_exchange(ha->base_qpair, NULL,
5703 atio, 1, 0);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005704 break;
5705 case -EBUSY:
5706 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5707 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5708 vha->vp_idx);
5709 qlt_send_busy(ha->base_qpair, atio,
5710 tc_sam_status);
5711 break;
5712 default:
5713 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5714 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5715 vha->vp_idx);
5716 qlt_send_busy(ha->base_qpair, atio,
5717 qla_sam_status);
5718 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005719 }
Quinn Tran7cf95f72017-12-28 12:33:28 -08005720 if (!ha_locked)
5721 spin_unlock_irqrestore(&ha->hardware_lock,
5722 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005723 }
5724 break;
5725
5726 case IMMED_NOTIFY_TYPE:
5727 {
5728 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5729 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5730 "qla_target(%d): Received ATIO packet %x "
5731 "with error status %x\n", vha->vp_idx,
5732 atio->u.raw.entry_type,
5733 atio->u.isp2x.entry_status);
5734 break;
5735 }
5736 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005737
5738 if (!ha_locked)
5739 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005740 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005741 if (!ha_locked)
5742 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005743 break;
5744 }
5745
5746 default:
5747 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5748 "qla_target(%d): Received unknown ATIO atio "
5749 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5750 break;
5751 }
5752
Quinn Tran2f424b92015-12-17 14:57:07 -05005753 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005754}
5755
Quinn Tran06910942018-09-04 14:19:12 -07005756/*
5757 * qpair lock is assume to be held
5758 * rc = 0 : send terminate & abts respond
5759 * rc != 0: do not send term & abts respond
5760 */
5761static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5762 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5763{
5764 struct qla_hw_data *ha = vha->hw;
5765 int rc = 0;
5766
5767 /*
5768 * Detect unresolved exchange. If the same ABTS is unable
5769 * to terminate an existing command and the same ABTS loops
5770 * between FW & Driver, then force FW dump. Under 1 jiff,
5771 * we should see multiple loops.
5772 */
5773 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5774 qpair->retry_term_jiff == jiffies) {
5775 /* found existing exchange */
5776 qpair->retry_term_cnt++;
5777 if (qpair->retry_term_cnt >= 5) {
5778 rc = EIO;
5779 qpair->retry_term_cnt = 0;
5780 ql_log(ql_log_warn, vha, 0xffff,
5781 "Unable to send ABTS Respond. Dumping firmware.\n");
5782 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5783 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5784
5785 if (qpair == ha->base_qpair)
5786 ha->isp_ops->fw_dump(vha, 1);
5787 else
5788 ha->isp_ops->fw_dump(vha, 0);
5789
5790 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5791 qla2xxx_wake_dpc(vha);
5792 }
5793 } else if (qpair->retry_term_jiff != jiffies) {
5794 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5795 qpair->retry_term_cnt = 0;
5796 qpair->retry_term_jiff = jiffies;
5797 }
5798
5799 return rc;
5800}
5801
Quinn Tran6b0431d2018-09-04 14:19:13 -07005802
5803static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5804 struct rsp_que *rsp, response_t *pkt)
5805{
5806 struct abts_resp_from_24xx_fw *entry =
5807 (struct abts_resp_from_24xx_fw *)pkt;
5808 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5809 struct qla_tgt_mgmt_cmd *mcmd;
5810 struct qla_hw_data *ha = vha->hw;
5811
Bart Van Assche81bcf1c2019-04-11 14:53:24 -07005812 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
Quinn Tran6b0431d2018-09-04 14:19:13 -07005813 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5814 ql_dbg(ql_dbg_async, vha, 0xe064,
5815 "qla_target(%d): ABTS Comp without mcmd\n",
5816 vha->vp_idx);
5817 return;
5818 }
5819
5820 if (mcmd)
5821 vha = mcmd->vha;
5822 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5823
5824 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5825 "ABTS_RESP_24XX: compl_status %x\n",
5826 entry->compl_status);
5827
5828 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5829 if ((entry->error_subcode1 == 0x1E) &&
5830 (entry->error_subcode2 == 0)) {
5831 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5832 ha->tgt.tgt_ops->free_mcmd(mcmd);
5833 return;
5834 }
5835 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5836 pkt, mcmd);
5837 } else {
5838 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5839 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5840 vha->vp_idx, entry->compl_status,
5841 entry->error_subcode1,
5842 entry->error_subcode2);
5843 ha->tgt.tgt_ops->free_mcmd(mcmd);
5844 }
5845 } else {
5846 ha->tgt.tgt_ops->free_mcmd(mcmd);
5847 }
5848}
5849
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005850/* ha->hardware_lock supposed to be held on entry */
5851/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005852static void qlt_response_pkt(struct scsi_qla_host *vha,
5853 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005854{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005855 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005856
5857 if (unlikely(tgt == NULL)) {
5858 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005859 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5860 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005861 return;
5862 }
5863
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005864 /*
5865 * In tgt_stop mode we also should allow all requests to pass.
5866 * Otherwise, some commands can stuck.
5867 */
5868
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005869 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005870 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005871 case CTIO_TYPE7:
5872 {
5873 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005874
Quinn Tran82de8022017-06-13 20:47:17 -07005875 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005876 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5877 entry);
5878 break;
5879 }
5880
5881 case ACCEPT_TGT_IO_TYPE:
5882 {
5883 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5884 int rc;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005885
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005886 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005887 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005888 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5889 "qla_target(%d): ATIO with error "
5890 "status %x received\n", vha->vp_idx,
5891 le16_to_cpu(atio->u.isp2x.status));
5892 break;
5893 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005894
Quinn Tran82de8022017-06-13 20:47:17 -07005895 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005896 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005897 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005898
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005899 rc = qlt_handle_cmd_for_atio(vha, atio);
5900 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005901 switch (rc) {
5902 case -ENODEV:
5903 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5904 "qla_target: Unable to send command to target\n");
5905 break;
5906 case -EBADF:
5907 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5908 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5909 qlt_send_term_exchange(rsp->qpair, NULL,
5910 atio, 1, 0);
5911 break;
5912 case -EBUSY:
5913 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5914 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5915 vha->vp_idx);
5916 qlt_send_busy(rsp->qpair, atio,
5917 tc_sam_status);
5918 break;
5919 default:
5920 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5921 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5922 vha->vp_idx);
5923 qlt_send_busy(rsp->qpair, atio,
5924 qla_sam_status);
5925 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005926 }
5927 }
5928 }
5929 break;
5930
5931 case CONTINUE_TGT_IO_TYPE:
5932 {
5933 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005934
Quinn Tran82de8022017-06-13 20:47:17 -07005935 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005936 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5937 entry);
5938 break;
5939 }
5940
5941 case CTIO_A64_TYPE:
5942 {
5943 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005944
Quinn Tran82de8022017-06-13 20:47:17 -07005945 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005946 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5947 entry);
5948 break;
5949 }
5950
5951 case IMMED_NOTIFY_TYPE:
5952 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5953 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5954 break;
5955
5956 case NOTIFY_ACK_TYPE:
5957 if (tgt->notify_ack_expected > 0) {
5958 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005959
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005960 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5961 "NOTIFY_ACK seq %08x status %x\n",
5962 le16_to_cpu(entry->u.isp2x.seq_id),
5963 le16_to_cpu(entry->u.isp2x.status));
5964 tgt->notify_ack_expected--;
5965 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005966 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005967 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5968 "qla_target(%d): NOTIFY_ACK "
5969 "failed %x\n", vha->vp_idx,
5970 le16_to_cpu(entry->u.isp2x.status));
5971 }
5972 } else {
5973 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5974 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5975 vha->vp_idx);
5976 }
5977 break;
5978
5979 case ABTS_RECV_24XX:
5980 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5981 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5982 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5983 break;
5984
5985 case ABTS_RESP_24XX:
5986 if (tgt->abts_resp_expected > 0) {
Quinn Tran6b0431d2018-09-04 14:19:13 -07005987 qlt_handle_abts_completion(vha, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005988 } else {
5989 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5990 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5991 "received\n", vha->vp_idx);
5992 }
5993 break;
5994
5995 default:
5996 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5997 "qla_target(%d): Received unknown response pkt "
5998 "type %x\n", vha->vp_idx, pkt->entry_type);
5999 break;
6000 }
6001
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006002}
6003
6004/*
6005 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6006 */
6007void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6008 uint16_t *mailbox)
6009{
6010 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006011 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01006012 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006013
Quinn Tran3a33dc92017-06-02 09:12:04 -07006014 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006015 return;
6016
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006017 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6018 IS_QLA2100(ha))
6019 return;
6020 /*
6021 * In tgt_stop mode we also should allow all requests to pass.
6022 * Otherwise, some commands can stuck.
6023 */
6024
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006025
6026 switch (code) {
6027 case MBA_RESET: /* Reset */
6028 case MBA_SYSTEM_ERR: /* System Error */
6029 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
6030 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
6031 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6032 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006033 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006034 break;
6035 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
6036 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6037 break;
6038
6039 case MBA_LOOP_UP:
6040 {
6041 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006042 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01006043 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6044 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6045 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006046 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07006047 qlt_send_notify_ack(ha->base_qpair,
6048 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006049 0, 0, 0, 0, 0, 0);
6050 tgt->link_reinit_iocb_pending = 0;
6051 }
6052 break;
6053 }
6054
6055 case MBA_LIP_OCCURRED:
6056 case MBA_LOOP_DOWN:
6057 case MBA_LIP_RESET:
6058 case MBA_RSCN_UPDATE:
6059 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006060 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01006061 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6062 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6063 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006064 break;
6065
Quinn Tranead03852017-01-19 22:28:01 -08006066 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07006067 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6068 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6069 vha->vp_idx,
6070 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6071 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08006072
6073 if (le16_to_cpu(mailbox[3]) == 1) {
6074 /* exchange starvation. */
6075 vha->hw->exch_starvation++;
6076 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006077 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08006078 "Exchange starvation-. Resetting RISC\n");
6079
6080 vha->hw->exch_starvation = 0;
6081 if (IS_P3P_TYPE(vha->hw))
6082 set_bit(FCOE_CTX_RESET_NEEDED,
6083 &vha->dpc_flags);
6084 else
6085 set_bit(ISP_ABORT_NEEDED,
6086 &vha->dpc_flags);
6087 qla2xxx_wake_dpc(vha);
6088 }
6089 }
6090 break;
6091
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006092 case MBA_PORT_UPDATE:
6093 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6094 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006095 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01006096 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6097 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6098 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6099
6100 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08006101 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006102 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6103 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08006104 vha->hw->exch_starvation = 0;
6105 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6107 "Async MB 2: Port Logged Out\n");
6108 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006109 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006110 break;
6111 }
6112
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006113}
6114
6115static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6116 uint16_t loop_id)
6117{
Quinn Tran726b8542017-01-19 22:28:00 -08006118 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006119 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08006120 unsigned long flags;
6121 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006122
Quinn Tran063b36d2017-12-04 14:45:10 -08006123 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006124 if (!fcport) {
6125 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6126 "qla_target(%d): Allocation of tmp FC port failed",
6127 vha->vp_idx);
6128 return NULL;
6129 }
6130
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006131 fcport->loop_id = loop_id;
6132
Quinn Tran15f30a52017-03-15 09:48:52 -07006133 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006134 if (rc != QLA_SUCCESS) {
6135 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6136 "qla_target(%d): Failed to retrieve fcport "
6137 "information -- get_port_database() returned %x "
6138 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6139 kfree(fcport);
6140 return NULL;
6141 }
6142
Quinn Tran726b8542017-01-19 22:28:00 -08006143 del = NULL;
6144 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6145 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6146
6147 if (tfcp) {
6148 tfcp->d_id = fcport->d_id;
6149 tfcp->port_type = fcport->port_type;
6150 tfcp->supported_classes = fcport->supported_classes;
6151 tfcp->flags |= fcport->flags;
Quinn Tran76f9a2d2017-12-04 14:45:11 -08006152 tfcp->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08006153
6154 del = fcport;
6155 fcport = tfcp;
6156 } else {
6157 if (vha->hw->current_topology == ISP_CFG_F)
6158 fcport->flags |= FCF_FABRIC_DEVICE;
6159
6160 list_add_tail(&fcport->list, &vha->vp_fcports);
6161 if (!IS_SW_RESV_ADDR(fcport->d_id))
6162 vha->fcport_count++;
6163 fcport->login_gen++;
6164 fcport->disc_state = DSC_LOGIN_COMPLETE;
6165 fcport->login_succ = 1;
6166 newfcport = 1;
6167 }
6168
6169 fcport->deleted = 0;
6170 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6171
6172 switch (vha->host->active_mode) {
6173 case MODE_INITIATOR:
6174 case MODE_DUAL:
6175 if (newfcport) {
6176 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Trancd4ed6b2018-08-31 11:24:31 -07006177 qla24xx_sched_upd_fcport(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08006178 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07006179 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08006180 "%s %d %8phC post gpsc fcp_cnt %d\n",
6181 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6182 qla24xx_post_gpsc_work(vha, fcport);
6183 }
6184 }
6185 break;
6186
6187 case MODE_TARGET:
6188 default:
6189 break;
6190 }
6191 if (del)
6192 qla2x00_free_fcport(del);
6193
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006194 return fcport;
6195}
6196
6197/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08006198static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006199 uint8_t *s_id)
6200{
Quinn Tran5d964832017-01-19 22:27:59 -08006201 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006202 fc_port_t *fcport = NULL;
6203 int rc, global_resets;
6204 uint16_t loop_id = 0;
6205
Quinn Tran726b8542017-01-19 22:28:00 -08006206 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
6207 /*
6208 * This is Domain Controller, so it should be
6209 * OK to drop SCSI commands from it.
6210 */
6211 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6212 "Unable to find initiator with S_ID %x:%x:%x",
6213 s_id[0], s_id[1], s_id[2]);
6214 return NULL;
6215 }
6216
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006217 mutex_lock(&vha->vha_tgt.tgt_mutex);
6218
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006219retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006220 global_resets =
6221 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006222
6223 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6224 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006225 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6226
Quinn Tran726b8542017-01-19 22:28:00 -08006227 ql_log(ql_log_info, vha, 0xf071,
6228 "qla_target(%d): Unable to find "
6229 "initiator with S_ID %x:%x:%x",
6230 vha->vp_idx, s_id[0], s_id[1],
6231 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006232
6233 if (rc == -ENOENT) {
6234 qlt_port_logo_t logo;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07006235
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006236 sid_to_portid(s_id, &logo.id);
6237 logo.cmd_count = 1;
6238 qlt_send_first_logo(vha, &logo);
6239 }
6240
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006241 return NULL;
6242 }
6243
6244 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006245 if (!fcport) {
6246 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006247 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006248 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006249
6250 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006251 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006252 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6253 "qla_target(%d): global reset during session discovery "
6254 "(counter was %d, new %d), retrying", vha->vp_idx,
6255 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006256 atomic_read(&vha->vha_tgt.
6257 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006258 goto retry;
6259 }
6260
6261 sess = qlt_create_sess(vha, fcport, true);
6262
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006263 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6264
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006265 return sess;
6266}
6267
6268static void qlt_abort_work(struct qla_tgt *tgt,
6269 struct qla_tgt_sess_work_param *prm)
6270{
6271 struct scsi_qla_host *vha = tgt->vha;
6272 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08006273 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006274 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006275 uint32_t be_s_id;
6276 uint8_t s_id[3];
6277 int rc;
6278
Quinn Tran75601512015-12-17 14:57:04 -05006279 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006280
6281 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05006282 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006283
6284 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6285 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6286 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6287
6288 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6289 (unsigned char *)&be_s_id);
6290 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006291 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006292
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006293 sess = qlt_make_local_sess(vha, s_id);
6294 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006295
Quinn Tran75601512015-12-17 14:57:04 -05006296 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006297 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05006298 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006299 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006300 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006301 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006302 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006303 }
6304
Quinn Tran726b8542017-01-19 22:28:00 -08006305 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006306 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08006307 "%s: kref_get fail %8phC \n",
6308 __func__, sess->port_name);
6309 sess = NULL;
6310 goto out_term2;
6311 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006312 }
6313
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006314 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006315 ha->tgt.tgt_ops->put_sess(sess);
6316 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6317
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006318 if (rc != 0)
6319 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006320 return;
6321
Quinn Tran75601512015-12-17 14:57:04 -05006322out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08006323 if (sess)
6324 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05006325 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006326
6327out_term:
6328 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006329 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6330 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006331 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006332}
6333
6334static void qlt_tmr_work(struct qla_tgt *tgt,
6335 struct qla_tgt_sess_work_param *prm)
6336{
6337 struct atio_from_isp *a = &prm->tm_iocb2;
6338 struct scsi_qla_host *vha = tgt->vha;
6339 struct qla_hw_data *ha = vha->hw;
Colin Ian Kingbb6abdd2019-02-15 09:52:32 +00006340 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006341 unsigned long flags;
6342 uint8_t *s_id = NULL; /* to hide compiler warnings */
6343 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07006344 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07006345 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006346 void *iocb;
6347
Quinn Tran75601512015-12-17 14:57:04 -05006348 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006349
6350 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006351 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006352
6353 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6354 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6355 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006356 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006357
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006358 sess = qlt_make_local_sess(vha, s_id);
6359 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006360
Quinn Tran75601512015-12-17 14:57:04 -05006361 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006362 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006363 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006364 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006365 if (sess->deleted) {
Quinn Tranf159b3c2017-03-15 09:48:47 -07006366 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006367 }
6368
Quinn Tran726b8542017-01-19 22:28:00 -08006369 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006370 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08006371 "%s: kref_get fail %8phC\n",
6372 __func__, sess->port_name);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006373 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006374 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006375 }
6376
6377 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006378 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006379 unpacked_lun =
6380 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006381
6382 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006383 ha->tgt.tgt_ops->put_sess(sess);
6384 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6385
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006386 if (rc != 0)
6387 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006388 return;
6389
Quinn Tranf159b3c2017-03-15 09:48:47 -07006390out_term2:
Quinn Tranf159b3c2017-03-15 09:48:47 -07006391 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006392out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006393 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006394}
6395
6396static void qlt_sess_work_fn(struct work_struct *work)
6397{
6398 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6399 struct scsi_qla_host *vha = tgt->vha;
6400 unsigned long flags;
6401
6402 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6403
6404 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6405 while (!list_empty(&tgt->sess_works_list)) {
6406 struct qla_tgt_sess_work_param *prm = list_entry(
6407 tgt->sess_works_list.next, typeof(*prm),
6408 sess_works_list_entry);
6409
6410 /*
6411 * This work can be scheduled on several CPUs at time, so we
6412 * must delete the entry to eliminate double processing
6413 */
6414 list_del(&prm->sess_works_list_entry);
6415
6416 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6417
6418 switch (prm->type) {
6419 case QLA_TGT_SESS_WORK_ABORT:
6420 qlt_abort_work(tgt, prm);
6421 break;
6422 case QLA_TGT_SESS_WORK_TM:
6423 qlt_tmr_work(tgt, prm);
6424 break;
6425 default:
6426 BUG_ON(1);
6427 break;
6428 }
6429
6430 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6431
6432 kfree(prm);
6433 }
6434 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6435}
6436
6437/* Must be called under tgt_host_action_mutex */
6438int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6439{
6440 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006441 int rc, i;
6442 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006443
6444 if (!QLA_TGT_MODE_ENABLED())
6445 return 0;
6446
Arun Easi33c36c02013-01-30 03:34:41 -05006447 if (!IS_TGT_MODE_CAPABLE(ha)) {
6448 ql_log(ql_log_warn, base_vha, 0xe070,
6449 "This adapter does not support target mode.\n");
6450 return 0;
6451 }
6452
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006453 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006454 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006455
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006456 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006457
6458 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6459 if (!tgt) {
6460 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6461 "Unable to allocate struct qla_tgt\n");
6462 return -ENOMEM;
6463 }
6464
Kees Cook6396bb22018-06-12 14:03:40 -07006465 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6466 sizeof(struct qla_qpair_hint),
6467 GFP_KERNEL);
Quinn Trane326d222017-06-13 20:47:18 -07006468 if (!tgt->qphints) {
6469 kfree(tgt);
6470 ql_log(ql_log_warn, base_vha, 0x0197,
6471 "Unable to allocate qpair hints.\n");
6472 return -ENOMEM;
6473 }
6474
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006475 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6476 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6477
Quinn Trane326d222017-06-13 20:47:18 -07006478 rc = btree_init64(&tgt->lun_qpair_map);
6479 if (rc) {
6480 kfree(tgt->qphints);
6481 kfree(tgt);
6482 ql_log(ql_log_info, base_vha, 0x0198,
6483 "Unable to initialize lun_qpair_map btree\n");
6484 return -EIO;
6485 }
6486 h = &tgt->qphints[0];
6487 h->qpair = ha->base_qpair;
6488 INIT_LIST_HEAD(&h->hint_elem);
6489 h->cpuid = ha->base_qpair->cpuid;
6490 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6491
6492 for (i = 0; i < ha->max_qpairs; i++) {
6493 unsigned long flags;
6494
6495 struct qla_qpair *qpair = ha->queue_pair_map[i];
Bart Van Asschebd432bb2019-04-11 14:53:17 -07006496
Quinn Trane326d222017-06-13 20:47:18 -07006497 h = &tgt->qphints[i + 1];
6498 INIT_LIST_HEAD(&h->hint_elem);
6499 if (qpair) {
6500 h->qpair = qpair;
6501 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6502 list_add_tail(&h->hint_elem, &qpair->hints_list);
6503 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6504 h->cpuid = qpair->cpuid;
6505 }
6506 }
6507
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006508 tgt->ha = ha;
6509 tgt->vha = base_vha;
6510 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006511 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006512 spin_lock_init(&tgt->sess_work_lock);
6513 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6514 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006515 atomic_set(&tgt->tgt_global_resets_count, 0);
6516
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006517 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006518
6519 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6520 "qla_target(%d): using 64 Bit PCI addressing",
6521 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006522 /* 3 is reserved */
6523 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006524
6525 mutex_lock(&qla_tgt_mutex);
6526 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6527 mutex_unlock(&qla_tgt_mutex);
6528
Quinn Tranf1443ee2017-03-15 09:48:51 -07006529 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6530 ha->tgt.tgt_ops->add_target(base_vha);
6531
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006532 return 0;
6533}
6534
6535/* Must be called under tgt_host_action_mutex */
6536int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6537{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006538 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006539 return 0;
6540
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006541 if (vha->fc_vport) {
6542 qlt_release(vha->vha_tgt.qla_tgt);
6543 return 0;
6544 }
Quinn Tran33e79972014-09-25 06:14:55 -04006545
6546 /* free left over qfull cmds */
6547 qlt_init_term_exchange(vha);
6548
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006549 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6550 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006551 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006552
6553 return 0;
6554}
6555
Quinn Tran482c9dc2017-03-15 09:48:54 -07006556void qlt_remove_target_resources(struct qla_hw_data *ha)
6557{
6558 struct scsi_qla_host *node;
6559 u32 key = 0;
6560
6561 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6562 btree_remove32(&ha->tgt.host_map, key);
6563
6564 btree_destroy32(&ha->tgt.host_map);
6565}
6566
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006567static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6568 unsigned char *b)
6569{
6570 int i;
6571
6572 pr_debug("qla2xxx HW vha->node_name: ");
6573 for (i = 0; i < WWN_SIZE; i++)
6574 pr_debug("%02x ", vha->node_name[i]);
6575 pr_debug("\n");
6576 pr_debug("qla2xxx HW vha->port_name: ");
6577 for (i = 0; i < WWN_SIZE; i++)
6578 pr_debug("%02x ", vha->port_name[i]);
6579 pr_debug("\n");
6580
6581 pr_debug("qla2xxx passed configfs WWPN: ");
6582 put_unaligned_be64(wwpn, b);
6583 for (i = 0; i < WWN_SIZE; i++)
6584 pr_debug("%02x ", b[i]);
6585 pr_debug("\n");
6586}
6587
6588/**
6589 * qla_tgt_lport_register - register lport with external module
6590 *
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006591 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
Bart Van Assche807eb902018-10-18 15:45:41 -07006592 * @phys_wwpn: physical port WWPN
6593 * @npiv_wwpn: NPIV WWPN
6594 * @npiv_wwnn: NPIV WWNN
Bart Van Assche2db62282018-01-23 16:33:51 -08006595 * @callback: lport initialization callback for tcm_qla2xxx code
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006596 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006597int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6598 u64 npiv_wwpn, u64 npiv_wwnn,
6599 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006600{
6601 struct qla_tgt *tgt;
6602 struct scsi_qla_host *vha;
6603 struct qla_hw_data *ha;
6604 struct Scsi_Host *host;
6605 unsigned long flags;
6606 int rc;
6607 u8 b[WWN_SIZE];
6608
6609 mutex_lock(&qla_tgt_mutex);
6610 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6611 vha = tgt->vha;
6612 ha = vha->hw;
6613
6614 host = vha->host;
6615 if (!host)
6616 continue;
6617
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006618 if (!(host->hostt->supported_mode & MODE_TARGET))
6619 continue;
6620
Quinn Tran0645cb82018-09-11 10:18:18 -07006621 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6622 continue;
6623
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006624 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006625 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006626 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6627 host->host_no);
6628 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6629 continue;
6630 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006631 if (tgt->tgt_stop) {
6632 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6633 host->host_no);
6634 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6635 continue;
6636 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006637 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6638
6639 if (!scsi_host_get(host)) {
6640 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6641 "Unable to scsi_host_get() for"
6642 " qla2xxx scsi_host\n");
6643 continue;
6644 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006645 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006646
6647 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6648 scsi_host_put(host);
6649 continue;
6650 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006651 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6652 if (rc != 0)
6653 scsi_host_put(host);
6654
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006655 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006656 return rc;
6657 }
6658 mutex_unlock(&qla_tgt_mutex);
6659
6660 return -ENODEV;
6661}
6662EXPORT_SYMBOL(qlt_lport_register);
6663
6664/**
6665 * qla_tgt_lport_deregister - Degister lport
6666 *
6667 * @vha: Registered scsi_qla_host pointer
6668 */
6669void qlt_lport_deregister(struct scsi_qla_host *vha)
6670{
6671 struct qla_hw_data *ha = vha->hw;
6672 struct Scsi_Host *sh = vha->host;
6673 /*
6674 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6675 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006676 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006677 ha->tgt.tgt_ops = NULL;
6678 /*
6679 * Release the Scsi_Host reference for the underlying qla2xxx host
6680 */
6681 scsi_host_put(sh);
6682}
6683EXPORT_SYMBOL(qlt_lport_deregister);
6684
6685/* Must be called under HW lock */
Quinn Tran0645cb82018-09-11 10:18:18 -07006686void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006687{
Quinn Tran0645cb82018-09-11 10:18:18 -07006688 switch (vha->qlini_mode) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006689 case QLA2XXX_INI_MODE_DISABLED:
6690 case QLA2XXX_INI_MODE_EXCLUSIVE:
6691 vha->host->active_mode = MODE_TARGET;
6692 break;
6693 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tran0645cb82018-09-11 10:18:18 -07006694 vha->host->active_mode = MODE_INITIATOR;
Quinn Tranead03852017-01-19 22:28:01 -08006695 break;
6696 case QLA2XXX_INI_MODE_DUAL:
6697 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006698 break;
6699 default:
6700 break;
6701 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006702}
6703
6704/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006705static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006706{
Quinn Tran0645cb82018-09-11 10:18:18 -07006707 switch (vha->qlini_mode) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006708 case QLA2XXX_INI_MODE_DISABLED:
6709 vha->host->active_mode = MODE_UNKNOWN;
6710 break;
6711 case QLA2XXX_INI_MODE_EXCLUSIVE:
6712 vha->host->active_mode = MODE_INITIATOR;
6713 break;
6714 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006715 case QLA2XXX_INI_MODE_DUAL:
6716 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006717 break;
6718 default:
6719 break;
6720 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006721}
6722
6723/*
6724 * qla_tgt_enable_vha - NO LOCK HELD
6725 *
6726 * host_reset, bring up w/ Target Mode Enabled
6727 */
6728void
6729qlt_enable_vha(struct scsi_qla_host *vha)
6730{
6731 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006732 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006733 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006734 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006735
6736 if (!tgt) {
6737 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6738 "Unable to locate qla_tgt pointer from"
6739 " struct qla_hw_data\n");
6740 dump_stack();
6741 return;
6742 }
Quinn Tran0645cb82018-09-11 10:18:18 -07006743 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6744 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006745
6746 spin_lock_irqsave(&ha->hardware_lock, flags);
6747 tgt->tgt_stopped = 0;
6748 qlt_set_mode(vha);
6749 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6750
Quinn Tranb6faaaf2018-09-04 14:19:09 -07006751 mutex_lock(&ha->optrom_mutex);
6752 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6753 "%s.\n", __func__);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006754 if (vha->vp_idx) {
6755 qla24xx_disable_vp(vha);
6756 qla24xx_enable_vp(vha);
6757 } else {
6758 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6759 qla2xxx_wake_dpc(base_vha);
6760 qla2x00_wait_for_hba_online(base_vha);
6761 }
Quinn Tranb6faaaf2018-09-04 14:19:09 -07006762 mutex_unlock(&ha->optrom_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006763}
6764EXPORT_SYMBOL(qlt_enable_vha);
6765
6766/*
6767 * qla_tgt_disable_vha - NO LOCK HELD
6768 *
6769 * Disable Target Mode and reset the adapter
6770 */
Joern Engel55a90662014-09-16 16:23:15 -04006771static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006772{
6773 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006774 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006775 unsigned long flags;
6776
6777 if (!tgt) {
6778 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6779 "Unable to locate qla_tgt pointer from"
6780 " struct qla_hw_data\n");
6781 dump_stack();
6782 return;
6783 }
6784
6785 spin_lock_irqsave(&ha->hardware_lock, flags);
6786 qlt_clear_mode(vha);
6787 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6788
6789 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6790 qla2xxx_wake_dpc(vha);
6791 qla2x00_wait_for_hba_online(vha);
6792}
6793
6794/*
6795 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6796 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6797 * members.
6798 */
6799void
6800qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6801{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006802 vha->vha_tgt.qla_tgt = NULL;
6803
6804 mutex_init(&vha->vha_tgt.tgt_mutex);
6805 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006806
6807 qlt_clear_mode(vha);
6808
6809 /*
6810 * NOTE: Currently the value is kept the same for <24xx and
6811 * >=24xx ISPs. If it is necessary to change it,
6812 * the check should be added for specific ISPs,
6813 * assigning the value appropriately.
6814 */
6815 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006816
6817 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006818}
6819
Quinn Trane374f9f2017-12-28 12:33:31 -08006820u8
6821qlt_rff_id(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006822{
Quinn Trane374f9f2017-12-28 12:33:31 -08006823 u8 fc4_feature = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006824 /*
6825 * FC-4 Feature bit 0 indicates target functionality to the name server.
6826 */
6827 if (qla_tgt_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006828 fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006829 } else if (qla_ini_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006830 fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006831 } else if (qla_dual_mode_enabled(vha))
Quinn Trane374f9f2017-12-28 12:33:31 -08006832 fc4_feature = BIT_0 | BIT_1;
6833
6834 return fc4_feature;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006835}
6836
6837/*
6838 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6839 * @ha: HA context
6840 *
6841 * Beginning of ATIO ring has initialization control block already built
6842 * by nvram config routine.
6843 *
6844 * Returns 0 on success.
6845 */
6846void
6847qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6848{
6849 struct qla_hw_data *ha = vha->hw;
6850 uint16_t cnt;
6851 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6852
Quinn Tranead03852017-01-19 22:28:01 -08006853 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006854 return;
6855
6856 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6857 pkt->u.raw.signature = ATIO_PROCESSED;
6858 pkt++;
6859 }
6860
6861}
6862
6863/*
6864 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6865 * @ha: SCSI driver HA context
6866 */
6867void
Quinn Tran2f424b92015-12-17 14:57:07 -05006868qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006869{
6870 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006871 struct atio_from_isp *pkt;
6872 int cnt, i;
6873
Quinn Tranec7193e2017-03-15 09:48:55 -07006874 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006875 return;
6876
Quinn Tran5f355092016-12-23 18:06:11 -08006877 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6878 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006879 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6880 cnt = pkt->u.raw.entry_count;
6881
Quinn Tran5f355092016-12-23 18:06:11 -08006882 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6883 /*
6884 * This packet is corrupted. The header + payload
6885 * can not be trusted. There is no point in passing
6886 * it further up.
6887 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006888 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006889 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6890 pkt->u.isp24.fcp_hdr.s_id,
6891 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6892 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6893
6894 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006895 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6896 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006897 } else {
6898 qlt_24xx_atio_pkt_all_vps(vha,
6899 (struct atio_from_isp *)pkt, ha_locked);
6900 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006901
6902 for (i = 0; i < cnt; i++) {
6903 ha->tgt.atio_ring_index++;
6904 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6905 ha->tgt.atio_ring_index = 0;
6906 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6907 } else
6908 ha->tgt.atio_ring_ptr++;
6909
6910 pkt->u.raw.signature = ATIO_PROCESSED;
6911 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6912 }
6913 wmb();
6914 }
6915
6916 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006917 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006918}
6919
6920void
Arun Easiaa230bc2013-01-30 03:34:39 -05006921qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006922{
6923 struct qla_hw_data *ha = vha->hw;
Quinn Trand2b292c2017-12-28 12:33:17 -08006924 struct qla_msix_entry *msix = &ha->msix_entries[2];
6925 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6926
Arun Easiaa230bc2013-01-30 03:34:39 -05006927 if (!QLA_TGT_MODE_ENABLED())
6928 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006929
Arun Easiaa230bc2013-01-30 03:34:39 -05006930 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6931 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6932 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6933
Quinn Trand2b292c2017-12-28 12:33:17 -08006934 if (ha->flags.msix_enabled) {
Joe Carnuccioecc89f22019-03-12 11:08:13 -07006935 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
Quinn Trand2b292c2017-12-28 12:33:17 -08006936 if (IS_QLA2071(ha)) {
6937 /* 4 ports Baker: Enable Interrupt Handshake */
6938 icb->msix_atio = 0;
6939 icb->firmware_options_2 |= BIT_26;
6940 } else {
6941 icb->msix_atio = cpu_to_le16(msix->entry);
6942 icb->firmware_options_2 &= ~BIT_26;
6943 }
6944 ql_dbg(ql_dbg_init, vha, 0xf072,
6945 "Registering ICB vector 0x%x for atio que.\n",
6946 msix->entry);
6947 }
6948 } else {
6949 /* INTx|MSI */
Joe Carnuccioecc89f22019-03-12 11:08:13 -07006950 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
Quinn Trand2b292c2017-12-28 12:33:17 -08006951 icb->msix_atio = 0;
6952 icb->firmware_options_2 |= BIT_26;
6953 ql_dbg(ql_dbg_init, vha, 0xf072,
6954 "%s: Use INTx for ATIOQ.\n", __func__);
6955 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006956 }
6957}
6958
6959void
6960qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6961{
6962 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006963 u32 tmp;
Quinn Tran99e1b682017-06-02 09:12:03 -07006964
6965 if (!QLA_TGT_MODE_ENABLED())
6966 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006967
Quinn Tranead03852017-01-19 22:28:01 -08006968 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006969 if (!ha->tgt.saved_set) {
6970 /* We save only once */
6971 ha->tgt.saved_exchange_count = nv->exchange_count;
6972 ha->tgt.saved_firmware_options_1 =
6973 nv->firmware_options_1;
6974 ha->tgt.saved_firmware_options_2 =
6975 nv->firmware_options_2;
6976 ha->tgt.saved_firmware_options_3 =
6977 nv->firmware_options_3;
6978 ha->tgt.saved_set = 1;
6979 }
6980
Quinn Tran99e1b682017-06-02 09:12:03 -07006981 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006982 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006983 else /* dual */
Quinn Tran0645cb82018-09-11 10:18:18 -07006984 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006985
6986 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006987 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006988
6989 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006990 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006991 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006992
6993 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006994 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006995 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006996 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006997 if (ql2xtgt_tape_enable)
6998 /* Enable FC Tape support */
6999 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7000 else
7001 /* Disable FC Tape support */
7002 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7003
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007004 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007005 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08007006
7007 /*
7008 * clear BIT 15 explicitly as we have seen at least
7009 * a couple of instances where this was set and this
7010 * was causing the firmware to not be initialized.
7011 */
7012 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007013 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07007014 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08007015
7016 if (IS_QLA25XX(ha)) {
7017 /* Change Loop-prefer to Pt-Pt */
7018 tmp = ~(BIT_4|BIT_5|BIT_6);
7019 nv->firmware_options_2 &= cpu_to_le32(tmp);
7020 tmp = P2P << 4;
7021 nv->firmware_options_2 |= cpu_to_le32(tmp);
7022 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007023 } else {
7024 if (ha->tgt.saved_set) {
7025 nv->exchange_count = ha->tgt.saved_exchange_count;
7026 nv->firmware_options_1 =
7027 ha->tgt.saved_firmware_options_1;
7028 nv->firmware_options_2 =
7029 ha->tgt.saved_firmware_options_2;
7030 nv->firmware_options_3 =
7031 ha->tgt.saved_firmware_options_3;
7032 }
7033 return;
7034 }
7035
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007036 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007037 if (vha->flags.init_done)
7038 fc_host_supported_classes(vha->host) =
7039 FC_COS_CLASS2 | FC_COS_CLASS3;
7040
Bart Van Asschead950362015-07-09 07:24:08 -07007041 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007042 } else {
7043 if (vha->flags.init_done)
7044 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7045
Bart Van Asschead950362015-07-09 07:24:08 -07007046 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007047 }
7048}
7049
7050void
7051qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7052 struct init_cb_24xx *icb)
7053{
7054 struct qla_hw_data *ha = vha->hw;
7055
Quinn Tran481ce732015-12-17 14:57:08 -05007056 if (!QLA_TGT_MODE_ENABLED())
7057 return;
7058
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007059 if (ha->tgt.node_name_set) {
7060 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07007061 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007062 }
7063}
7064
Arun Easiaa230bc2013-01-30 03:34:39 -05007065void
7066qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7067{
7068 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08007069 u32 tmp;
Arun Easiaa230bc2013-01-30 03:34:39 -05007070
7071 if (!QLA_TGT_MODE_ENABLED())
7072 return;
7073
Quinn Tranead03852017-01-19 22:28:01 -08007074 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007075 if (!ha->tgt.saved_set) {
7076 /* We save only once */
7077 ha->tgt.saved_exchange_count = nv->exchange_count;
7078 ha->tgt.saved_firmware_options_1 =
7079 nv->firmware_options_1;
7080 ha->tgt.saved_firmware_options_2 =
7081 nv->firmware_options_2;
7082 ha->tgt.saved_firmware_options_3 =
7083 nv->firmware_options_3;
7084 ha->tgt.saved_set = 1;
7085 }
7086
Quinn Tran99e1b682017-06-02 09:12:03 -07007087 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08007088 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07007089 else /* dual */
Quinn Tran0645cb82018-09-11 10:18:18 -07007090 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05007091
7092 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07007093 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05007094
7095 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08007096 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07007097 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05007098 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007099 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05007100 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007101 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08007102 /*
7103 * clear BIT 15 explicitly as we have seen at
7104 * least a couple of instances where this was set
7105 * and this was causing the firmware to not be
7106 * initialized.
7107 */
7108 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04007109 if (ql2xtgt_tape_enable)
7110 /* Enable FC tape support */
7111 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7112 else
7113 /* Disable FC tape support */
7114 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7115
Arun Easiaa230bc2013-01-30 03:34:39 -05007116 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007117 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05007118 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07007119 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08007120
7121 /* Change Loop-prefer to Pt-Pt */
7122 tmp = ~(BIT_4|BIT_5|BIT_6);
7123 nv->firmware_options_2 &= cpu_to_le32(tmp);
7124 tmp = P2P << 4;
7125 nv->firmware_options_2 |= cpu_to_le32(tmp);
Arun Easiaa230bc2013-01-30 03:34:39 -05007126 } else {
7127 if (ha->tgt.saved_set) {
7128 nv->exchange_count = ha->tgt.saved_exchange_count;
7129 nv->firmware_options_1 =
7130 ha->tgt.saved_firmware_options_1;
7131 nv->firmware_options_2 =
7132 ha->tgt.saved_firmware_options_2;
7133 nv->firmware_options_3 =
7134 ha->tgt.saved_firmware_options_3;
7135 }
7136 return;
7137 }
7138
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007139 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007140 if (vha->flags.init_done)
7141 fc_host_supported_classes(vha->host) =
7142 FC_COS_CLASS2 | FC_COS_CLASS3;
7143
Bart Van Asschead950362015-07-09 07:24:08 -07007144 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05007145 } else {
7146 if (vha->flags.init_done)
7147 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7148
Bart Van Asschead950362015-07-09 07:24:08 -07007149 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05007150 }
7151}
7152
7153void
7154qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7155 struct init_cb_81xx *icb)
7156{
7157 struct qla_hw_data *ha = vha->hw;
7158
7159 if (!QLA_TGT_MODE_ENABLED())
7160 return;
7161
7162 if (ha->tgt.node_name_set) {
7163 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07007164 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05007165 }
7166}
7167
7168void
7169qlt_83xx_iospace_config(struct qla_hw_data *ha)
7170{
7171 if (!QLA_TGT_MODE_ENABLED())
7172 return;
7173
7174 ha->msix_count += 1; /* For ATIO Q */
7175}
7176
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007177
7178void
7179qlt_modify_vp_config(struct scsi_qla_host *vha,
7180 struct vp_config_entry_24xx *vpmod)
7181{
Quinn Tranead03852017-01-19 22:28:01 -08007182 /* enable target mode. Bit5 = 1 => disable */
7183 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007184 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08007185
Quinn Tranead03852017-01-19 22:28:01 -08007186 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08007187 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007188 vpmod->options_idx1 &= ~BIT_4;
7189}
7190
7191void
7192qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7193{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007194 int rc;
7195
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007196 if (!QLA_TGT_MODE_ENABLED())
7197 return;
7198
Joe Carnuccioecc89f22019-03-12 11:08:13 -07007199 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
7200 IS_QLA28XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007201 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7202 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7203 } else {
7204 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7205 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7206 }
7207
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08007208 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7209 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08007210
7211 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7212 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7213 qlt_unknown_atio_work_fn);
7214
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007215 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007216
7217 rc = btree_init32(&ha->tgt.host_map);
7218 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007219 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007220 "Unable to initialize ha->host_map btree\n");
7221
7222 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007223}
7224
Arun Easiaa230bc2013-01-30 03:34:39 -05007225irqreturn_t
7226qla83xx_msix_atio_q(int irq, void *dev_id)
7227{
7228 struct rsp_que *rsp;
7229 scsi_qla_host_t *vha;
7230 struct qla_hw_data *ha;
7231 unsigned long flags;
7232
7233 rsp = (struct rsp_que *) dev_id;
7234 ha = rsp->hw;
7235 vha = pci_get_drvdata(ha->pdev);
7236
Quinn Tran2f424b92015-12-17 14:57:07 -05007237 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007238
Quinn Tran2f424b92015-12-17 14:57:07 -05007239 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05007240
Quinn Tran2f424b92015-12-17 14:57:07 -05007241 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007242
7243 return IRQ_HANDLED;
7244}
7245
Quinn Tran2f424b92015-12-17 14:57:07 -05007246static void
7247qlt_handle_abts_recv_work(struct work_struct *work)
7248{
7249 struct qla_tgt_sess_op *op = container_of(work,
7250 struct qla_tgt_sess_op, work);
7251 scsi_qla_host_t *vha = op->vha;
7252 struct qla_hw_data *ha = vha->hw;
7253 unsigned long flags;
7254
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007255 if (qla2x00_reset_active(vha) ||
7256 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05007257 return;
7258
7259 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7260 qlt_24xx_process_atio_queue(vha, 0);
7261 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7262
7263 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07007264 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05007265 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07007266
7267 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05007268}
7269
7270void
Quinn Tran82de8022017-06-13 20:47:17 -07007271qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7272 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05007273{
7274 struct qla_tgt_sess_op *op;
7275
7276 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7277
7278 if (!op) {
7279 /* do not reach for ATIO queue here. This is best effort err
7280 * recovery at this point.
7281 */
Quinn Tran82de8022017-06-13 20:47:17 -07007282 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05007283 return;
7284 }
7285
7286 memcpy(&op->atio, pkt, sizeof(*pkt));
7287 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007288 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07007289 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05007290 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7291 queue_work(qla_tgt_wq, &op->work);
7292 return;
7293}
7294
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007295int
7296qlt_mem_alloc(struct qla_hw_data *ha)
7297{
7298 if (!QLA_TGT_MODE_ENABLED())
7299 return 0;
7300
Kees Cook6396bb22018-06-12 14:03:40 -07007301 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7302 sizeof(struct qla_tgt_vp_map),
7303 GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007304 if (!ha->tgt.tgt_vp_map)
7305 return -ENOMEM;
7306
7307 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7308 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7309 &ha->tgt.atio_dma, GFP_KERNEL);
7310 if (!ha->tgt.atio_ring) {
7311 kfree(ha->tgt.tgt_vp_map);
7312 return -ENOMEM;
7313 }
7314 return 0;
7315}
7316
7317void
7318qlt_mem_free(struct qla_hw_data *ha)
7319{
7320 if (!QLA_TGT_MODE_ENABLED())
7321 return;
7322
7323 if (ha->tgt.atio_ring) {
7324 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7325 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7326 ha->tgt.atio_dma);
7327 }
Bart Van Assche5365bf92019-04-17 14:44:22 -07007328 ha->tgt.atio_ring = NULL;
7329 ha->tgt.atio_dma = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007330 kfree(ha->tgt.tgt_vp_map);
Bart Van Assche5365bf92019-04-17 14:44:22 -07007331 ha->tgt.tgt_vp_map = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007332}
7333
7334/* vport_slock to be held by the caller */
7335void
7336qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7337{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007338 void *slot;
7339 u32 key;
7340 int rc;
7341
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007342 if (!QLA_TGT_MODE_ENABLED())
7343 return;
7344
Quinn Tran482c9dc2017-03-15 09:48:54 -07007345 key = vha->d_id.b24;
7346
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007347 switch (cmd) {
7348 case SET_VP_IDX:
7349 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7350 break;
7351 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07007352 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7353 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007354 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007355 "Save vha in host_map %p %06x\n", vha, key);
7356 rc = btree_insert32(&vha->hw->tgt.host_map,
7357 key, vha, GFP_ATOMIC);
7358 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007359 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007360 "Unable to insert s_id into host_map: %06x\n",
7361 key);
7362 return;
7363 }
Quinn Tran83548fe2017-06-02 09:12:01 -07007364 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7365 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007366 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007367 break;
7368 case RESET_VP_IDX:
7369 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7370 break;
7371 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07007372 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007373 "clear vha in host_map %p %06x\n", vha, key);
7374 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7375 if (slot)
7376 btree_remove32(&vha->hw->tgt.host_map, key);
7377 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007378 break;
7379 }
7380}
7381
Quinn Tran482c9dc2017-03-15 09:48:54 -07007382void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7383{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007384
7385 if (!vha->d_id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007386 vha->d_id = id;
7387 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007388 } else if (vha->d_id.b24 != id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007389 qlt_update_vp_map(vha, RESET_AL_PA);
7390 vha->d_id = id;
7391 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007392 }
7393}
7394
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007395static int __init qlt_parse_ini_mode(void)
7396{
7397 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7398 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7399 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7400 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7401 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7402 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007403 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7404 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007405 else
7406 return false;
7407
7408 return true;
7409}
7410
7411int __init qlt_init(void)
7412{
7413 int ret;
7414
7415 if (!qlt_parse_ini_mode()) {
7416 ql_log(ql_log_fatal, NULL, 0xe06b,
7417 "qlt_parse_ini_mode() failed\n");
7418 return -EINVAL;
7419 }
7420
7421 if (!QLA_TGT_MODE_ENABLED())
7422 return 0;
7423
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007424 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7425 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7426 qla_tgt_mgmt_cmd), 0, NULL);
7427 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007428 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007429 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007430 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007431 }
7432
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007433 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007434 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7435 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007436
7437 if (!qla_tgt_plogi_cachep) {
7438 ql_log(ql_log_fatal, NULL, 0xe06d,
7439 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7440 ret = -ENOMEM;
7441 goto out_mgmt_cmd_cachep;
7442 }
7443
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007444 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7445 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7446 if (!qla_tgt_mgmt_cmd_mempool) {
7447 ql_log(ql_log_fatal, NULL, 0xe06e,
7448 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7449 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007450 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007451 }
7452
7453 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7454 if (!qla_tgt_wq) {
7455 ql_log(ql_log_fatal, NULL, 0xe06f,
7456 "alloc_workqueue for qla_tgt_wq failed\n");
7457 ret = -ENOMEM;
7458 goto out_cmd_mempool;
7459 }
7460 /*
7461 * Return 1 to signal that initiator-mode is being disabled
7462 */
7463 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7464
7465out_cmd_mempool:
7466 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007467out_plogi_cachep:
7468 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007469out_mgmt_cmd_cachep:
7470 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007471 return ret;
7472}
7473
7474void qlt_exit(void)
7475{
7476 if (!QLA_TGT_MODE_ENABLED())
7477 return;
7478
7479 destroy_workqueue(qla_tgt_wq);
7480 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007481 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007482 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007483}