blob: bb16280a8d72c56c54f5c602775c91e03e77d878 [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{
3654 unsigned long flags = 0;
3655 int rc;
3656
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003657 if (ha_locked) {
3658 rc = __qlt_send_term_imm_notif(vha, imm);
3659
3660#if 0 /* Todo */
3661 if (rc == -ENOMEM)
3662 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003663#else
3664 if (rc) {
3665 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003666#endif
3667 goto done;
3668 }
3669
3670 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3671 rc = __qlt_send_term_imm_notif(vha, imm);
3672
3673#if 0 /* Todo */
3674 if (rc == -ENOMEM)
3675 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3676#endif
3677
3678done:
3679 if (!ha_locked)
3680 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3681}
3682
Quinn Tran83548fe2017-06-02 09:12:01 -07003683/*
3684 * If hardware_lock held on entry, might drop it, then reaquire
3685 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3686 */
Quinn Tran82de8022017-06-13 20:47:17 -07003687static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003688 struct qla_tgt_cmd *cmd,
3689 struct atio_from_isp *atio)
3690{
Quinn Tran82de8022017-06-13 20:47:17 -07003691 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003692 struct ctio7_to_24xx *ctio24;
3693 struct qla_hw_data *ha = vha->hw;
3694 request_t *pkt;
3695 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003696 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003697
Quinn Tran83548fe2017-06-02 09:12:01 -07003698 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003699
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003700 if (cmd)
3701 vha = cmd->vha;
3702
Quinn Tran82de8022017-06-13 20:47:17 -07003703 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003704 if (pkt == NULL) {
3705 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3706 "qla_target(%d): %s failed: unable to allocate "
3707 "request packet\n", vha->vp_idx, __func__);
3708 return -ENOMEM;
3709 }
3710
3711 if (cmd != NULL) {
3712 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3713 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3714 "qla_target(%d): Terminating cmd %p with "
3715 "incorrect state %d\n", vha->vp_idx, cmd,
3716 cmd->state);
3717 } else
3718 ret = 1;
3719 }
3720
Quinn Tran60a9ead2017-06-13 20:47:28 -07003721 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003722 pkt->entry_count = 1;
3723 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3724
3725 ctio24 = (struct ctio7_to_24xx *)pkt;
3726 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003727 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003728 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003729 ctio24->vp_index = vha->vp_idx;
3730 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3731 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3732 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3733 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003734 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3735 CTIO7_FLAGS_TERMINATE;
3736 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003737 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3738 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003739
Himanshu Madhani63163e02014-09-25 06:14:59 -04003740 /* Memory Barrier */
3741 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003742 if (qpair->reqq_start_iocbs)
3743 qpair->reqq_start_iocbs(qpair);
3744 else
3745 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003746 return ret;
3747}
3748
Quinn Tran82de8022017-06-13 20:47:17 -07003749static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003750 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3751 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003752{
Quinn Tran82de8022017-06-13 20:47:17 -07003753 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003754 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003755 int rc;
3756
Quinn Tran82de8022017-06-13 20:47:17 -07003757 /* why use different vha? NPIV */
3758 if (cmd)
3759 vha = cmd->vha;
3760 else
3761 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003762
3763 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003764 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003765 if (rc == -ENOMEM)
3766 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003767 goto done;
3768 }
Quinn Tran82de8022017-06-13 20:47:17 -07003769 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3770 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003771 if (rc == -ENOMEM)
3772 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003773
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003774done:
Quinn Trana07100e2015-12-07 19:48:57 -05003775 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003776 if (cmd->sg_mapped)
3777 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003778 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3779 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003780
3781 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003782 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003783
Quinn Tran7b898542014-04-11 16:54:44 -04003784 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003785}
3786
Quinn Tran33e79972014-09-25 06:14:55 -04003787static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3788{
3789 struct list_head free_list;
3790 struct qla_tgt_cmd *cmd, *tcmd;
3791
3792 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003793 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003794
3795 cmd = tcmd = NULL;
3796 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3797 INIT_LIST_HEAD(&free_list);
3798 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3799
3800 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3801 list_del(&cmd->cmd_list);
3802 /* This cmd was never sent to TCM. There is no need
3803 * to schedule free or call free_cmd
3804 */
3805 qlt_free_cmd(cmd);
3806 vha->hw->tgt.num_qfull_cmds_alloc--;
3807 }
3808 }
3809 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3810}
3811
3812static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3813{
3814 uint32_t total_leaked;
3815
3816 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3817
3818 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3819 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3820
3821 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3822 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003823 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003824
3825 if (IS_P3P_TYPE(vha->hw))
3826 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3827 else
3828 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3829 qla2xxx_wake_dpc(vha);
3830 }
3831
3832}
3833
Quinn Trana07100e2015-12-07 19:48:57 -05003834int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003835{
3836 struct qla_tgt *tgt = cmd->tgt;
3837 struct scsi_qla_host *vha = tgt->vha;
3838 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003839 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003840
3841 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3842 "qla_target(%d): terminating exchange for aborted cmd=%p "
3843 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3844 se_cmd->tag);
3845
Quinn Trana07100e2015-12-07 19:48:57 -05003846 spin_lock_irqsave(&cmd->cmd_lock, flags);
3847 if (cmd->aborted) {
3848 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3849 /*
3850 * It's normal to see 2 calls in this path:
3851 * 1) XFER Rdy completion + CMD_T_ABORT
3852 * 2) TCM TMR - drain_state_list
3853 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003854 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3855 "multiple abort. %p transport_state %x, t_state %x, "
3856 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3857 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003858 return EIO;
3859 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003860 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003861 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003862 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003863
Quinn Tran82de8022017-06-13 20:47:17 -07003864 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003865 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003866}
3867EXPORT_SYMBOL(qlt_abort_cmd);
3868
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003869void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3870{
Quinn Tran5d964832017-01-19 22:27:59 -08003871 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003872
Quinn Tranf83adb62014-04-11 16:54:43 -04003873 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3874 "%s: se_cmd[%p] ox_id %04x\n",
3875 __func__, &cmd->se_cmd,
3876 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003877
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003878 BUG_ON(cmd->cmd_in_wq);
3879
Quinn Trana07100e2015-12-07 19:48:57 -05003880 if (cmd->sg_mapped)
3881 qlt_unmap_sg(cmd->vha, cmd);
3882
Quinn Tran33e79972014-09-25 06:14:55 -04003883 if (!cmd->q_full)
3884 qlt_decr_num_pend_cmds(cmd->vha);
3885
Quinn Tranf83adb62014-04-11 16:54:43 -04003886 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003887 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003888 if (unlikely(cmd->free_sg))
3889 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003890
3891 if (!sess || !sess->se_sess) {
3892 WARN_ON(1);
3893 return;
3894 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003895 cmd->jiffies_at_free = get_jiffies_64();
Matthew Wilcox83c2b542018-06-12 12:05:43 -07003896 target_free_tag(sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003897}
3898EXPORT_SYMBOL(qlt_free_cmd);
3899
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003900/*
3901 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3902 */
Quinn Tran82de8022017-06-13 20:47:17 -07003903static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003904 struct qla_tgt_cmd *cmd, uint32_t status)
3905{
3906 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003907 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003908
Quinn Tranbe251522017-03-15 09:48:49 -07003909 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003910 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003911 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3912 "se_cmd=%p tag[%x] op %#x/%s",
3913 cmd->lba, cmd->lba,
3914 cmd->num_blks, &cmd->se_cmd,
3915 cmd->atio.u.isp24.exchange_addr,
3916 cmd->se_cmd.prot_op,
3917 prot_op_str(cmd->se_cmd.prot_op));
3918
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003919 if (ctio != NULL) {
3920 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07003921
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003922 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003923 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003924 } else
3925 term = 1;
3926
3927 if (term)
himanshu.madhani@cavium.com3efc31f2018-01-15 20:46:47 -08003928 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003929
3930 return term;
3931}
3932
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003933
3934/* ha->hardware_lock supposed to be held on entry */
Quinn Tran6b0431d2018-09-04 14:19:13 -07003935static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003936 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003937{
Quinn Tran6b0431d2018-09-04 14:19:13 -07003938 void *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003939 struct req_que *req;
3940 int qid = GET_QID(handle);
3941 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003942
Quinn Tran82de8022017-06-13 20:47:17 -07003943 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3944 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003945
Quinn Tran82de8022017-06-13 20:47:17 -07003946 if (qid == rsp->req->id) {
3947 req = rsp->req;
3948 } else if (vha->hw->req_q_map[qid]) {
3949 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3950 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3951 vha->vp_idx, rsp->id, handle);
3952 req = vha->hw->req_q_map[qid];
3953 } else {
3954 return NULL;
3955 }
Arun Easi667024a2014-09-25 06:14:47 -04003956
Quinn Tran82de8022017-06-13 20:47:17 -07003957 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003958
Quinn Tran82de8022017-06-13 20:47:17 -07003959 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003960 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003961 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3962 "qla_target(%d): Wrong handle %x received\n",
3963 vha->vp_idx, handle);
3964 return NULL;
3965 }
Quinn Tran82de8022017-06-13 20:47:17 -07003966
Quinn Tran6b0431d2018-09-04 14:19:13 -07003967 cmd = (void *) req->outstanding_cmds[h];
Quinn Tran82de8022017-06-13 20:47:17 -07003968 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003969 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003970 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3971 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003972 return NULL;
3973 }
Quinn Tran82de8022017-06-13 20:47:17 -07003974 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003975 } else if (ctio != NULL) {
3976 /* We can't get loop ID from CTIO7 */
3977 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3978 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3979 "support NULL handles\n", vha->vp_idx);
3980 return NULL;
3981 }
3982
3983 return cmd;
3984}
3985
Arun Easic0cb4492014-09-25 06:14:51 -04003986/* hardware_lock should be held by caller. */
Quinn Tranc5419e22017-06-13 20:47:16 -07003987void
Arun Easic0cb4492014-09-25 06:14:51 -04003988qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3989{
3990 struct qla_hw_data *ha = vha->hw;
Arun Easic0cb4492014-09-25 06:14:51 -04003991
3992 if (cmd->sg_mapped)
3993 qlt_unmap_sg(vha, cmd);
3994
Arun Easic0cb4492014-09-25 06:14:51 -04003995 /* TODO: fix debug message type and ids. */
3996 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3997 ql_dbg(ql_dbg_io, vha, 0xff00,
Quinn Tran82de8022017-06-13 20:47:17 -07003998 "HOST-ABORT: state=PROCESSED.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003999 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4000 cmd->write_data_transferred = 0;
4001 cmd->state = QLA_TGT_STATE_DATA_IN;
4002
4003 ql_dbg(ql_dbg_io, vha, 0xff01,
Quinn Tran82de8022017-06-13 20:47:17 -07004004 "HOST-ABORT: state=DATA_IN.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04004005
4006 ha->tgt.tgt_ops->handle_data(cmd);
4007 return;
Arun Easic0cb4492014-09-25 06:14:51 -04004008 } else {
4009 ql_dbg(ql_dbg_io, vha, 0xff03,
Quinn Tran82de8022017-06-13 20:47:17 -07004010 "HOST-ABORT: state=BAD(%d).\n",
Arun Easic0cb4492014-09-25 06:14:51 -04004011 cmd->state);
4012 dump_stack();
4013 }
4014
Quinn Tran1eb42f92017-01-19 22:27:55 -08004015 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04004016 ha->tgt.tgt_ops->free_cmd(cmd);
4017}
4018
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004019/*
4020 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4021 */
Quinn Tran82de8022017-06-13 20:47:17 -07004022static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
4023 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004024{
4025 struct qla_hw_data *ha = vha->hw;
4026 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004027 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07004028 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004029
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004030 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
4031 /* That could happen only in case of an error/reset/abort */
4032 if (status != CTIO_SUCCESS) {
4033 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
4034 "Intermediate CTIO received"
4035 " (status %x)\n", status);
4036 }
4037 return;
4038 }
4039
Bart Van Assche81bcf1c2019-04-11 14:53:24 -07004040 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07004041 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004042 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07004043
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004044 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04004045 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004046
Joern Engelf9b67212014-09-16 16:23:18 -04004047 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004048
4049 if (unlikely(status != CTIO_SUCCESS)) {
4050 switch (status & 0xFFFF) {
Quinn Tran079a3a32018-09-04 14:19:11 -07004051 case CTIO_INVALID_RX_ID:
4052 if (printk_ratelimit())
4053 dev_info(&vha->hw->pdev->dev,
4054 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4055 vha->vp_idx, cmd->atio.u.isp24.attr,
4056 ((cmd->ctio_flags >> 9) & 0xf),
4057 cmd->ctio_flags);
4058
4059 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004060 case CTIO_LIP_RESET:
4061 case CTIO_TARGET_RESET:
4062 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04004063 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004064 case CTIO_TIMEOUT:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004065 /* They are OK */
4066 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4067 "qla_target(%d): CTIO with "
4068 "status %#x received, state %x, se_cmd %p, "
4069 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4070 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4071 status, cmd->state, se_cmd);
4072 break;
4073
4074 case CTIO_PORT_LOGGED_OUT:
4075 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004076 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08004077 int logged_out =
4078 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4079
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004080 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004081 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004082 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08004083 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004084 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004085
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004086 if (logged_out && cmd->sess) {
4087 /*
4088 * Session is already logged out, but we need
4089 * to notify initiator, who's not aware of this
4090 */
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004091 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07004092 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08004093 "%s %d %8phC post del sess\n",
4094 __func__, __LINE__, cmd->sess->port_name);
4095
Quinn Trand8630bb2017-12-28 12:33:43 -08004096 qlt_schedule_sess_for_deletion(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05004097 }
4098 break;
4099 }
Quinn Tranf83adb62014-04-11 16:54:43 -04004100 case CTIO_DIF_ERROR: {
4101 struct ctio_crc_from_fw *crc =
4102 (struct ctio_crc_from_fw *)ctio;
4103 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07004104 "qla_target(%d): CTIO with DIF_ERROR status %x "
4105 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4106 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04004107 vha->vp_idx, status, cmd->state, se_cmd,
4108 *((u64 *)&crc->actual_dif[0]),
4109 *((u64 *)&crc->expected_dif[0]));
4110
Quinn Tran8abfa9e2017-06-13 20:47:24 -07004111 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07004112 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04004113 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004114 default:
4115 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04004116 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004117 vha->vp_idx, status, cmd->state, se_cmd);
4118 break;
4119 }
4120
Quinn Tran7b898542014-04-11 16:54:44 -04004121
Quinn Tran193b50b2015-12-17 14:57:03 -05004122 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04004123 * cmd is already aborted/terminated, we don't
4124 * need to terminate again. The exchange is already
4125 * cleaned up/freed at FW level. Just cleanup at driver
4126 * level.
4127 */
4128 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004129 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004130 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004131 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004132 return;
Quinn Tran7b898542014-04-11 16:54:44 -04004133 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004134 }
4135
4136 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004137 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004138 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004139 cmd->state = QLA_TGT_STATE_DATA_IN;
4140
Bart Van Assche52c82822015-07-09 07:23:26 -07004141 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004142 cmd->write_data_transferred = 1;
4143
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004144 ha->tgt.tgt_ops->handle_data(cmd);
4145 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05004146 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004147 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004148 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02004149 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004150 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004151 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004152 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4153 "qla_target(%d): A command in state (%d) should "
4154 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4155 }
4156
Quinn Tran7b898542014-04-11 16:54:44 -04004157 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004158 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004159 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4160 dump_stack();
4161 }
4162
4163 ha->tgt.tgt_ops->free_cmd(cmd);
4164}
4165
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004166static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4167 uint8_t task_codes)
4168{
4169 int fcp_task_attr;
4170
4171 switch (task_codes) {
4172 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004173 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004174 break;
4175 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004176 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004177 break;
4178 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004179 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004180 break;
4181 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004182 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004183 break;
4184 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004185 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004186 break;
4187 default:
4188 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4189 "qla_target: unknown task code %x, use ORDERED instead\n",
4190 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004191 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004192 break;
4193 }
4194
4195 return fcp_task_attr;
4196}
4197
Quinn Tran5d964832017-01-19 22:27:59 -08004198static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004199 uint8_t *);
4200/*
4201 * Process context for I/O path into tcm_qla2xxx code
4202 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004203static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004204{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004205 scsi_qla_host_t *vha = cmd->vha;
4206 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004207 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004208 struct atio_from_isp *atio = &cmd->atio;
4209 unsigned char *cdb;
4210 unsigned long flags;
4211 uint32_t data_length;
4212 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07004213 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004214
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004215 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004216 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004217
Quinn Tran193b50b2015-12-17 14:57:03 -05004218 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004219 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4220 "cmd with tag %u is aborted\n",
4221 cmd->atio.u.isp24.exchange_addr);
4222 goto out_term;
4223 }
4224
Quinn Trana07100e2015-12-07 19:48:57 -05004225 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004226 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004227 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004228
4229 if (atio->u.isp24.fcp_cmnd.rddata &&
4230 atio->u.isp24.fcp_cmnd.wrdata) {
4231 bidi = 1;
4232 data_dir = DMA_TO_DEVICE;
4233 } else if (atio->u.isp24.fcp_cmnd.rddata)
4234 data_dir = DMA_FROM_DEVICE;
4235 else if (atio->u.isp24.fcp_cmnd.wrdata)
4236 data_dir = DMA_TO_DEVICE;
4237 else
4238 data_dir = DMA_NONE;
4239
4240 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4241 atio->u.isp24.fcp_cmnd.task_attr);
Quinn Trane25f7652018-05-01 09:01:50 -07004242 data_length = get_datalen_for_atio(atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004243
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004244 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4245 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004246 if (ret != 0)
4247 goto out_term;
4248 /*
Bart Van Assche67eb4a602019-04-17 14:44:10 -07004249 * Drop extra session reference from qlt_handle_cmd_for_atio().
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004250 */
Quinn Tran75601512015-12-17 14:57:04 -05004251 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004252 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004253 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004254 return;
4255
4256out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004257 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004258 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004259 * cmd has not sent to target yet, so pass NULL as the second
4260 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004261 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004262 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004263 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4264 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004265
4266 qlt_decr_num_pend_cmds(vha);
Matthew Wilcox83c2b542018-06-12 12:05:43 -07004267 target_free_tag(sess->se_sess, &cmd->se_cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07004268 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004269
4270 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004271 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004272 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004273}
4274
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004275static void qlt_do_work(struct work_struct *work)
4276{
4277 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004278 scsi_qla_host_t *vha = cmd->vha;
4279 unsigned long flags;
4280
4281 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4282 list_del(&cmd->cmd_list);
4283 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004284
4285 __qlt_do_work(cmd);
4286}
4287
Quinn Tran09620ee2017-06-13 20:47:20 -07004288void qlt_clr_qp_table(struct scsi_qla_host *vha)
4289{
4290 unsigned long flags;
4291 struct qla_hw_data *ha = vha->hw;
4292 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4293 void *node;
4294 u64 key = 0;
4295
4296 ql_log(ql_log_info, vha, 0x706c,
4297 "User update Number of Active Qpairs %d\n",
4298 ha->tgt.num_act_qpairs);
4299
4300 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4301
4302 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4303 btree_remove64(&tgt->lun_qpair_map, key);
4304
4305 ha->base_qpair->lun_cnt = 0;
4306 for (key = 0; key < ha->max_qpairs; key++)
4307 if (ha->queue_pair_map[key])
4308 ha->queue_pair_map[key]->lun_cnt = 0;
4309
4310 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4311}
4312
Quinn Trane326d222017-06-13 20:47:18 -07004313static void qlt_assign_qpair(struct scsi_qla_host *vha,
4314 struct qla_tgt_cmd *cmd)
4315{
4316 struct qla_qpair *qpair, *qp;
4317 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4318 struct qla_qpair_hint *h;
4319
4320 if (vha->flags.qpairs_available) {
4321 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4322 if (unlikely(!h)) {
4323 /* spread lun to qpair ratio evently */
4324 int lcnt = 0, rc;
4325 struct scsi_qla_host *base_vha =
4326 pci_get_drvdata(vha->hw->pdev);
4327
4328 qpair = vha->hw->base_qpair;
4329 if (qpair->lun_cnt == 0) {
4330 qpair->lun_cnt++;
4331 h = qla_qpair_to_hint(tgt, qpair);
4332 BUG_ON(!h);
4333 rc = btree_insert64(&tgt->lun_qpair_map,
4334 cmd->unpacked_lun, h, GFP_ATOMIC);
4335 if (rc) {
4336 qpair->lun_cnt--;
4337 ql_log(ql_log_info, vha, 0xd037,
4338 "Unable to insert lun %llx into lun_qpair_map\n",
4339 cmd->unpacked_lun);
4340 }
4341 goto out;
4342 } else {
4343 lcnt = qpair->lun_cnt;
4344 }
4345
4346 h = NULL;
4347 list_for_each_entry(qp, &base_vha->qp_list,
4348 qp_list_elem) {
4349 if (qp->lun_cnt == 0) {
4350 qp->lun_cnt++;
4351 h = qla_qpair_to_hint(tgt, qp);
4352 BUG_ON(!h);
4353 rc = btree_insert64(&tgt->lun_qpair_map,
4354 cmd->unpacked_lun, h, GFP_ATOMIC);
4355 if (rc) {
4356 qp->lun_cnt--;
4357 ql_log(ql_log_info, vha, 0xd038,
4358 "Unable to insert lun %llx into lun_qpair_map\n",
4359 cmd->unpacked_lun);
4360 }
4361 qpair = qp;
4362 goto out;
4363 } else {
4364 if (qp->lun_cnt < lcnt) {
4365 lcnt = qp->lun_cnt;
4366 qpair = qp;
4367 continue;
4368 }
4369 }
4370 }
4371 BUG_ON(!qpair);
4372 qpair->lun_cnt++;
4373 h = qla_qpair_to_hint(tgt, qpair);
4374 BUG_ON(!h);
4375 rc = btree_insert64(&tgt->lun_qpair_map,
4376 cmd->unpacked_lun, h, GFP_ATOMIC);
4377 if (rc) {
4378 qpair->lun_cnt--;
4379 ql_log(ql_log_info, vha, 0xd039,
4380 "Unable to insert lun %llx into lun_qpair_map\n",
4381 cmd->unpacked_lun);
4382 }
4383 }
4384 } else {
4385 h = &tgt->qphints[0];
4386 }
4387out:
4388 cmd->qpair = h->qpair;
4389 cmd->se_cmd.cpuid = h->cpuid;
4390}
4391
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004392static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004393 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004394 struct atio_from_isp *atio)
4395{
4396 struct se_session *se_sess = sess->se_sess;
4397 struct qla_tgt_cmd *cmd;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004398 int tag, cpu;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004399
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004400 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004401 if (tag < 0)
4402 return NULL;
4403
4404 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4405 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004406 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004407 memcpy(&cmd->atio, atio, sizeof(*atio));
4408 cmd->state = QLA_TGT_STATE_NEW;
4409 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004410 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004411 cmd->vha = vha;
4412 cmd->se_cmd.map_tag = tag;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07004413 cmd->se_cmd.map_cpu = cpu;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004414 cmd->sess = sess;
4415 cmd->loop_id = sess->loop_id;
4416 cmd->conf_compl_supported = sess->conf_compl_supported;
4417
Quinn Tran1eb42f92017-01-19 22:27:55 -08004418 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004419 cmd->jiffies_at_alloc = get_jiffies_64();
4420
Quinn Trane326d222017-06-13 20:47:18 -07004421 cmd->unpacked_lun = scsilun_to_int(
4422 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4423 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004424 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004425 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004426
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004427 return cmd;
4428}
4429
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004430/* ha->hardware_lock supposed to be held on entry */
4431static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4432 struct atio_from_isp *atio)
4433{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004434 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004435 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004436 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004437 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004438 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08004439 port_id_t id;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004440
4441 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004442 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004443 "New command while device %p is shutting down\n", tgt);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004444 return -ENODEV;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004445 }
4446
Quinn Trana4239942017-12-28 12:33:26 -08004447 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4448 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4449 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4450 if (IS_SW_RESV_ADDR(id))
4451 return -EBUSY;
4452
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004453 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 -08004454 if (unlikely(!sess))
4455 return -EFAULT;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004456
4457 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4458 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004459 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004460 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004461 "New command while old session %p is being deleted\n",
4462 sess);
4463 return -EFAULT;
4464 }
4465
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004466 /*
4467 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4468 */
Quinn Tran726b8542017-01-19 22:28:00 -08004469 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004470 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004471 "%s: kref_get fail, %8phC oxid %x \n",
4472 __func__, sess->port_name,
4473 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4474 return -EFAULT;
4475 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004476
4477 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004478 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004479 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004480 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004481 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4482 ha->tgt.tgt_ops->put_sess(sess);
4483 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004484 return -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004485 }
4486
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004487 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004488 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004489
Quinn Tran726b8542017-01-19 22:28:00 -08004490 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004491 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004492 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004493
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004494 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004495 if (vha->flags.qpairs_available) {
4496 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4497 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004498 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4499 queue_work_on(smp_processor_id(), qla_tgt_wq,
4500 &cmd->work);
4501 else
4502 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4503 &cmd->work);
4504 } else {
4505 queue_work(qla_tgt_wq, &cmd->work);
4506 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004507
Quinn Tran82de8022017-06-13 20:47:17 -07004508 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004509}
4510
4511/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004512static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004513 int fn, void *iocb, int flags)
4514{
4515 struct scsi_qla_host *vha = sess->vha;
4516 struct qla_hw_data *ha = vha->hw;
4517 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004518 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Quinn Tran84905df2018-05-01 09:01:53 -07004519 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004520
4521 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4522 if (!mcmd) {
4523 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4524 "qla_target(%d): Allocation of management "
4525 "command failed, some commands and their data could "
4526 "leak\n", vha->vp_idx);
4527 return -ENOMEM;
4528 }
4529 memset(mcmd, 0, sizeof(*mcmd));
4530 mcmd->sess = sess;
4531
4532 if (iocb) {
4533 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4534 sizeof(mcmd->orig_iocb.imm_ntfy));
4535 }
4536 mcmd->tmr_func = fn;
4537 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004538 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran84905df2018-05-01 09:01:53 -07004539 mcmd->qpair = h->qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004540 mcmd->vha = vha;
Quinn Tran84905df2018-05-01 09:01:53 -07004541 mcmd->se_cmd.cpuid = h->cpuid;
4542 mcmd->unpacked_lun = lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004543
4544 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004545 case QLA_TGT_LUN_RESET:
Quinn Tran84905df2018-05-01 09:01:53 -07004546 case QLA_TGT_CLEAR_TS:
4547 case QLA_TGT_ABORT_TS:
4548 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
Bart Van Assche50435d42018-10-18 15:45:40 -07004549 /* fall through */
Quinn Tran84905df2018-05-01 09:01:53 -07004550 case QLA_TGT_CLEAR_ACA:
4551 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4552 mcmd->qpair = h->qpair;
4553 mcmd->se_cmd.cpuid = h->cpuid;
4554 break;
4555
4556 case QLA_TGT_TARGET_RESET:
4557 case QLA_TGT_NEXUS_LOSS_SESS:
4558 case QLA_TGT_NEXUS_LOSS:
4559 case QLA_TGT_ABORT_ALL:
4560 default:
4561 /* no-op */
4562 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004563 }
4564
Quinn Tran84905df2018-05-01 09:01:53 -07004565 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4566 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4567 &mcmd->work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004568
4569 return 0;
4570}
4571
4572/* ha->hardware_lock supposed to be held on entry */
4573static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4574{
4575 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4576 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004577 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004578 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004579 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004580 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004581
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004582 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004583
4584 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004585 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4586 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004587 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4588
Quinn Tranf775bd12017-06-02 09:11:59 -07004589 unpacked_lun =
4590 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004591
Quinn Tranfb352652017-12-28 12:33:32 -08004592 if (sess == NULL || sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004593 return -EFAULT;
4594
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004595 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4596}
4597
4598/* ha->hardware_lock supposed to be held on entry */
4599static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004600 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004601{
4602 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4603 struct qla_hw_data *ha = vha->hw;
4604 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004605 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004606 int rc;
4607
4608 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4609 if (mcmd == NULL) {
4610 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4611 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4612 vha->vp_idx, __func__);
4613 return -ENOMEM;
4614 }
4615 memset(mcmd, 0, sizeof(*mcmd));
4616
4617 mcmd->sess = sess;
4618 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4619 sizeof(mcmd->orig_iocb.imm_ntfy));
4620
Quinn Tranf775bd12017-06-02 09:11:59 -07004621 unpacked_lun =
4622 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004623 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004624 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004625 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004626
Quinn Tranbe92fc32017-01-19 22:27:54 -08004627 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004628 le16_to_cpu(iocb->u.isp2x.seq_id));
4629 if (rc != 0) {
4630 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4631 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4632 vha->vp_idx, rc);
4633 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4634 return -EFAULT;
4635 }
4636
4637 return 0;
4638}
4639
4640/* ha->hardware_lock supposed to be held on entry */
4641static int qlt_abort_task(struct scsi_qla_host *vha,
4642 struct imm_ntfy_from_isp *iocb)
4643{
4644 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004645 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004646 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004647 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004648
4649 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4650
Quinn Tran75601512015-12-17 14:57:04 -05004651 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004652 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004653 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4654
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004655 if (sess == NULL) {
4656 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4657 "qla_target(%d): task abort for unexisting "
4658 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004659 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004660 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4661 }
4662
4663 return __qlt_abort_task(vha, iocb, sess);
4664}
4665
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004666void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4667{
Quinn Tran726b8542017-01-19 22:28:00 -08004668 if (rc != MBS_COMMAND_COMPLETE) {
4669 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4670 "%s: se_sess %p / sess %p from"
4671 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4672 " LOGO failed: %#x\n",
4673 __func__,
4674 fcport->se_sess,
4675 fcport,
4676 fcport->port_name, fcport->loop_id,
4677 fcport->d_id.b.domain, fcport->d_id.b.area,
4678 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004679 }
Quinn Tran726b8542017-01-19 22:28:00 -08004680
4681 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004682}
4683
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004684/*
4685* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4686*
4687* Schedules sessions with matching port_id/loop_id but different wwn for
4688* deletion. Returns existing session with matching wwn if present.
4689* Null otherwise.
4690*/
Quinn Tran726b8542017-01-19 22:28:00 -08004691struct fc_port *
4692qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004693 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004694{
Quinn Tran5d964832017-01-19 22:27:59 -08004695 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004696 uint64_t other_wwn;
4697
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004698 *conflict_sess = NULL;
4699
Quinn Tran5d964832017-01-19 22:27:59 -08004700 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004701
4702 other_wwn = wwn_to_u64(other_sess->port_name);
4703
4704 if (wwn == other_wwn) {
4705 WARN_ON(sess);
4706 sess = other_sess;
4707 continue;
4708 }
4709
4710 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004711 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004712 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004713 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004714 "Invalidating sess %p loop_id %d wwn %llx.\n",
4715 other_sess, other_sess->loop_id, other_wwn);
4716
4717 /*
4718 * logout_on_delete is set by default, but another
4719 * session that has the same s_id/loop_id combo
4720 * might have cleared it when requested this session
4721 * deletion, so don't touch it
4722 */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004723 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004724 } else {
4725 /*
4726 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004727 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004728 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004729 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004730 "Invalidating sess %p loop_id %d wwn %llx.\n",
4731 other_sess, other_sess->loop_id, other_wwn);
4732
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004733 other_sess->keep_nport_handle = 1;
Quinn Tranba743f92017-12-04 14:45:12 -08004734 if (other_sess->disc_state != DSC_DELETED)
4735 *conflict_sess = other_sess;
Quinn Tran94cff6e2017-12-28 12:33:42 -08004736 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004737 }
4738 continue;
4739 }
4740
4741 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004742 if ((loop_id == other_sess->loop_id) &&
4743 (loop_id != FC_NO_LOOP_ID)) {
4744 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004745 "Invalidating sess %p loop_id %d wwn %llx.\n",
4746 other_sess, other_sess->loop_id, other_wwn);
4747
4748 /* Same loop_id but different s_id
4749 * Ok to kill and logout */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004750 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004751 }
4752 }
4753
4754 return sess;
4755}
4756
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004757/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4758static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4759{
4760 struct qla_tgt_sess_op *op;
4761 struct qla_tgt_cmd *cmd;
4762 uint32_t key;
4763 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004764 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004765
4766 key = (((u32)s_id->b.domain << 16) |
4767 ((u32)s_id->b.area << 8) |
4768 ((u32)s_id->b.al_pa));
4769
Quinn Tran8b631d82017-06-02 09:11:54 -07004770 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004771 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4772 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004773
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004774 if (op_key == key) {
4775 op->aborted = true;
4776 count++;
4777 }
4778 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004779
4780 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4781 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004782
Quinn Tran41dc5292017-01-19 22:28:03 -08004783 if (op_key == key) {
4784 op->aborted = true;
4785 count++;
4786 }
4787 }
4788
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004789 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4790 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
Bart Van Asschebd432bb2019-04-11 14:53:17 -07004791
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004792 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004793 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004794 count++;
4795 }
4796 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004797 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004798
4799 return count;
4800}
4801
Quinn Tran9cd883f2017-12-28 12:33:24 -08004802static int qlt_handle_login(struct scsi_qla_host *vha,
4803 struct imm_ntfy_from_isp *iocb)
4804{
4805 struct fc_port *sess = NULL, *conflict_sess = NULL;
4806 uint64_t wwn;
4807 port_id_t port_id;
4808 uint16_t loop_id, wd3_lo;
4809 int res = 0;
4810 struct qlt_plogi_ack_t *pla;
4811 unsigned long flags;
4812
4813 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4814
4815 port_id.b.domain = iocb->u.isp24.port_id[2];
4816 port_id.b.area = iocb->u.isp24.port_id[1];
4817 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4818 port_id.b.rsvd_1 = 0;
4819
4820 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4821
4822 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4823 abort_cmds_for_s_id(vha, &port_id);
4824
4825 if (wwn) {
4826 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4827 sess = qlt_find_sess_invalidate_other(vha, wwn,
4828 port_id, loop_id, &conflict_sess);
4829 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Tranaa9e6d72018-08-31 11:24:38 -07004830 } else {
4831 ql_dbg(ql_dbg_disc, vha, 0xffff,
4832 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4833 __func__, __LINE__, loop_id, port_id.b24);
4834 qlt_send_term_imm_notif(vha, iocb, 1);
4835 goto out;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004836 }
4837
4838 if (IS_SW_RESV_ADDR(port_id)) {
4839 res = 1;
4840 goto out;
4841 }
4842
4843 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4844 if (!pla) {
Quinn Tran48acad02018-08-02 13:16:44 -07004845 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4846 "%s %d %8phC Term INOT due to mem alloc fail",
4847 __func__, __LINE__,
4848 iocb->u.isp24.port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004849 qlt_send_term_imm_notif(vha, iocb, 1);
4850 goto out;
4851 }
4852
4853 if (conflict_sess) {
4854 conflict_sess->login_gen++;
4855 qlt_plogi_ack_link(vha, pla, conflict_sess,
4856 QLT_PLOGI_LINK_CONFLICT);
4857 }
4858
4859 if (!sess) {
4860 pla->ref_count++;
4861 ql_dbg(ql_dbg_disc, vha, 0xffff,
4862 "%s %d %8phC post new sess\n",
4863 __func__, __LINE__, iocb->u.isp24.port_name);
Quinn Trana4239942017-12-28 12:33:26 -08004864 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4865 qla24xx_post_newsess_work(vha, &port_id,
4866 iocb->u.isp24.port_name,
4867 iocb->u.isp24.u.plogi.node_name,
4868 pla, FC4_TYPE_UNKNOWN);
4869 else
4870 qla24xx_post_newsess_work(vha, &port_id,
4871 iocb->u.isp24.port_name, NULL,
4872 pla, FC4_TYPE_UNKNOWN);
4873
Quinn Tran9cd883f2017-12-28 12:33:24 -08004874 goto out;
4875 }
4876
Quinn Trancd4ed6b2018-08-31 11:24:31 -07004877 if (sess->disc_state == DSC_UPD_FCPORT) {
4878 u16 sec;
4879
4880 /*
4881 * Remote port registration is still going on from
4882 * previous login. Allow it to finish before we
4883 * accept the new login.
4884 */
4885 sess->next_disc_state = DSC_DELETE_PEND;
4886 sec = jiffies_to_msecs(jiffies -
4887 sess->jiffies_at_registration) / 1000;
4888 if (sess->sec_since_registration < sec && sec &&
4889 !(sec % 5)) {
4890 sess->sec_since_registration = sec;
4891 ql_dbg(ql_dbg_disc, vha, 0xffff,
4892 "%s %8phC - Slow Rport registration (%d Sec)\n",
4893 __func__, sess->port_name, sec);
4894 }
4895
4896 if (!conflict_sess)
4897 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4898
4899 qlt_send_term_imm_notif(vha, iocb, 1);
4900 goto out;
4901 }
4902
Quinn Tran9cd883f2017-12-28 12:33:24 -08004903 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4904 sess->d_id = port_id;
4905 sess->login_gen++;
4906
4907 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4908 sess->fw_login_state = DSC_LS_PRLI_PEND;
4909 sess->local = 0;
4910 sess->loop_id = loop_id;
4911 sess->d_id = port_id;
4912 sess->fw_login_state = DSC_LS_PRLI_PEND;
4913 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4914
4915 if (wd3_lo & BIT_7)
4916 sess->conf_compl_supported = 1;
4917
4918 if ((wd3_lo & BIT_4) == 0)
4919 sess->port_type = FCT_INITIATOR;
4920 else
4921 sess->port_type = FCT_TARGET;
4922
4923 } else
4924 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4925
4926
4927 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4928 "%s %d %8phC DS %d\n",
4929 __func__, __LINE__, sess->port_name, sess->disc_state);
4930
4931 switch (sess->disc_state) {
4932 case DSC_DELETED:
4933 qlt_plogi_ack_unref(vha, pla);
4934 break;
4935
4936 default:
4937 /*
4938 * Under normal circumstances we want to release nport handle
4939 * during LOGO process to avoid nport handle leaks inside FW.
4940 * The exception is when LOGO is done while another PLOGI with
4941 * the same nport handle is waiting as might be the case here.
4942 * Note: there is always a possibily of a race where session
4943 * deletion has already started for other reasons (e.g. ACL
4944 * removal) and now PLOGI arrives:
4945 * 1. if PLOGI arrived in FW after nport handle has been freed,
4946 * FW must have assigned this PLOGI a new/same handle and we
4947 * can proceed ACK'ing it as usual when session deletion
4948 * completes.
4949 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4950 * bit reached it, the handle has now been released. We'll
4951 * get an error when we ACK this PLOGI. Nothing will be sent
4952 * back to initiator. Initiator should eventually retry
4953 * PLOGI and situation will correct itself.
4954 */
4955 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4956 (sess->d_id.b24 == port_id.b24));
4957
4958 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4959 "%s %d %8phC post del sess\n",
4960 __func__, __LINE__, sess->port_name);
4961
4962
Quinn Trand8630bb2017-12-28 12:33:43 -08004963 qlt_schedule_sess_for_deletion(sess);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004964 break;
4965 }
4966out:
4967 return res;
4968}
4969
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004970/*
4971 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4972 */
4973static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4974 struct imm_ntfy_from_isp *iocb)
4975{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004976 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004977 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004978 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004979 uint64_t wwn;
4980 port_id_t port_id;
4981 uint16_t loop_id;
4982 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004983 int res = 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004984 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004985
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004986 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4987
4988 port_id.b.domain = iocb->u.isp24.port_id[2];
4989 port_id.b.area = iocb->u.isp24.port_id[1];
4990 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4991 port_id.b.rsvd_1 = 0;
4992
4993 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4994
Quinn Tran726b8542017-01-19 22:28:00 -08004995 ql_dbg(ql_dbg_disc, vha, 0xf026,
4996 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4997 vha->vp_idx, iocb->u.isp24.port_id[2],
4998 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4999 iocb->u.isp24.status_subcode, loop_id,
5000 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005001
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005002 /* res = 1 means ack at the end of thread
5003 * res = 0 means ack async/later.
5004 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005005 switch (iocb->u.isp24.status_subcode) {
5006 case ELS_PLOGI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08005007 res = qlt_handle_login(vha, iocb);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005008 break;
5009
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005010 case ELS_PRLI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08005011 if (N2N_TOPO(ha)) {
5012 sess = qla2x00_find_fcport_by_wwpn(vha,
5013 iocb->u.isp24.port_name, 1);
5014
5015 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5016 ql_dbg(ql_dbg_disc, vha, 0xffff,
5017 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5018 __func__, __LINE__,
5019 iocb->u.isp24.port_name);
5020 qlt_send_term_imm_notif(vha, iocb, 1);
5021 break;
5022 }
5023
5024 res = qlt_handle_login(vha, iocb);
5025 break;
5026 }
5027
Quinn Trana4239942017-12-28 12:33:26 -08005028 if (IS_SW_RESV_ADDR(port_id)) {
5029 res = 1;
5030 break;
5031 }
5032
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005033 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5034
Quinn Tran75601512015-12-17 14:57:04 -05005035 if (wwn) {
5036 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08005037 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5038 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05005039 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5040 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05005041
5042 if (conflict_sess) {
Quinn Tran5ef696a2017-12-04 14:45:05 -08005043 switch (conflict_sess->disc_state) {
5044 case DSC_DELETED:
5045 case DSC_DELETE_PEND:
5046 break;
5047 default:
5048 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5049 "PRLI with conflicting sess %p port %8phC\n",
5050 conflict_sess, conflict_sess->port_name);
5051 conflict_sess->fw_login_state =
5052 DSC_LS_PORT_UNAVAIL;
5053 qlt_send_term_imm_notif(vha, iocb, 1);
5054 res = 0;
5055 break;
5056 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05005057 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005058
5059 if (sess != NULL) {
Quinn Trana4239942017-12-28 12:33:26 -08005060 bool delete = false;
Quinn Trancd4ed6b2018-08-31 11:24:31 -07005061 int sec;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005062
Quinn Tran82abdca2017-12-28 12:33:22 -08005063 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5064 switch (sess->fw_login_state) {
Quinn Trana4239942017-12-28 12:33:26 -08005065 case DSC_LS_PLOGI_PEND:
Quinn Tran82abdca2017-12-28 12:33:22 -08005066 case DSC_LS_PLOGI_COMP:
5067 case DSC_LS_PRLI_COMP:
5068 break;
5069 default:
Quinn Trana4239942017-12-28 12:33:26 -08005070 delete = true;
5071 break;
5072 }
5073
5074 switch (sess->disc_state) {
Quinn Trancd4ed6b2018-08-31 11:24:31 -07005075 case DSC_UPD_FCPORT:
5076 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5077 flags);
5078
5079 sec = jiffies_to_msecs(jiffies -
5080 sess->jiffies_at_registration)/1000;
5081 if (sess->sec_since_registration < sec && sec &&
5082 !(sec % 5)) {
5083 sess->sec_since_registration = sec;
5084 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5085 "%s %8phC : Slow Rport registration(%d Sec)\n",
5086 __func__, sess->port_name, sec);
5087 }
5088 qlt_send_term_imm_notif(vha, iocb, 1);
5089 return 0;
5090
Quinn Trana4239942017-12-28 12:33:26 -08005091 case DSC_LOGIN_PEND:
5092 case DSC_GPDB:
Quinn Trana4239942017-12-28 12:33:26 -08005093 case DSC_LOGIN_COMPLETE:
5094 case DSC_ADISC:
5095 delete = false;
5096 break;
5097 default:
5098 break;
5099 }
5100
5101 if (delete) {
Quinn Tran82abdca2017-12-28 12:33:22 -08005102 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5103 flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005104 /*
5105 * Impatient initiator sent PRLI before last
5106 * PLOGI could finish. Will force him to re-try,
5107 * while last one finishes.
5108 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04005109 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005110 "sess %p PRLI received, before plogi ack.\n",
5111 sess);
5112 qlt_send_term_imm_notif(vha, iocb, 1);
5113 res = 0;
5114 break;
5115 }
5116
5117 /*
5118 * This shouldn't happen under normal circumstances,
5119 * since we have deleted the old session during PLOGI
5120 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04005121 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005122 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5123 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5124
5125 sess->local = 0;
5126 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08005127 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005128 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005129
5130 if (wd3_lo & BIT_7)
5131 sess->conf_compl_supported = 1;
5132
Quinn Tran726b8542017-01-19 22:28:00 -08005133 if ((wd3_lo & BIT_4) == 0)
5134 sess->port_type = FCT_INITIATOR;
5135 else
5136 sess->port_type = FCT_TARGET;
Quinn Tran82abdca2017-12-28 12:33:22 -08005137
5138 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005139 }
5140 res = 1; /* send notify ack */
5141
5142 /* Make session global (not used in fabric mode) */
5143 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005144 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005145 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07005146 "%s %d %8phC post nack\n",
5147 __func__, __LINE__, sess->port_name);
5148 qla24xx_post_nack_work(vha, sess, iocb,
5149 SRB_NACK_PRLI);
5150 res = 0;
5151 } else {
5152 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5153 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5154 qla2xxx_wake_dpc(vha);
5155 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005156 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005157 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005158 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07005159 "%s %d %8phC post nack\n",
5160 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08005161 qla24xx_post_nack_work(vha, sess, iocb,
5162 SRB_NACK_PRLI);
5163 res = 0;
5164 }
5165 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005166 break;
5167
Quinn Tran41dc5292017-01-19 22:28:03 -08005168 case ELS_TPRLO:
5169 if (le16_to_cpu(iocb->u.isp24.flags) &
5170 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5171 loop_id = 0xFFFF;
5172 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5173 res = 1;
5174 break;
5175 }
Bart Van Assche81881862017-12-07 16:02:46 -08005176 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005177 case ELS_LOGO:
5178 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08005179 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5180 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5181 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5182
5183 if (sess) {
5184 sess->login_gen++;
5185 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08005186 sess->logo_ack_needed = 1;
5187 memcpy(sess->iocb, iocb, IOCB_SIZE);
5188 }
5189
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005190 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08005191
Quinn Tran83548fe2017-06-02 09:12:01 -07005192 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08005193 "%s: logo %llx res %d sess %p ",
5194 __func__, wwn, res, sess);
5195 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08005196 /*
5197 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5198 * for LOGO_ACK & sess delete
5199 */
Quinn Tran726b8542017-01-19 22:28:00 -08005200 BUG_ON(!sess);
5201 res = 0;
5202 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08005203 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08005204 if (sess) {
Quinn Trand8630bb2017-12-28 12:33:43 -08005205 qlt_schedule_sess_for_deletion(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08005206 res = 0;
5207 }
5208 /* else logo will be ack */
5209 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005210 break;
5211 case ELS_PDISC:
5212 case ELS_ADISC:
5213 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005214 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005215
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005216 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005217 qlt_send_notify_ack(ha->base_qpair,
5218 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005219 tgt->link_reinit_iocb_pending = 0;
5220 }
Quinn Tran726b8542017-01-19 22:28:00 -08005221
5222 sess = qla2x00_find_fcport_by_wwpn(vha,
5223 iocb->u.isp24.port_name, 1);
5224 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005225 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08005226 "sess %p lid %d|%d DS %d LS %d\n",
5227 sess, sess->loop_id, loop_id,
5228 sess->disc_state, sess->fw_login_state);
5229 }
5230
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005231 res = 1; /* send notify ack */
5232 break;
5233 }
5234
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005235 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005236 default:
5237 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5238 "qla_target(%d): Unsupported ELS command %x "
5239 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5240 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5241 break;
5242 }
5243
Quinn Tran9cd883f2017-12-28 12:33:24 -08005244 ql_dbg(ql_dbg_disc, vha, 0xf026,
5245 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5246 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5247
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005248 return res;
5249}
5250
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005251/*
5252 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5253 */
5254static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5255 struct imm_ntfy_from_isp *iocb)
5256{
5257 struct qla_hw_data *ha = vha->hw;
5258 uint32_t add_flags = 0;
5259 int send_notify_ack = 1;
5260 uint16_t status;
5261
5262 status = le16_to_cpu(iocb->u.isp2x.status);
5263 switch (status) {
5264 case IMM_NTFY_LIP_RESET:
5265 {
5266 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5267 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5268 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5269 iocb->u.isp24.status_subcode);
5270
5271 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5272 send_notify_ack = 0;
5273 break;
5274 }
5275
5276 case IMM_NTFY_LIP_LINK_REINIT:
5277 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005278 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005279
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005280 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5281 "qla_target(%d): LINK REINIT (loop %#x, "
5282 "subcode %x)\n", vha->vp_idx,
5283 le16_to_cpu(iocb->u.isp24.nport_handle),
5284 iocb->u.isp24.status_subcode);
5285 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005286 qlt_send_notify_ack(ha->base_qpair,
5287 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005288 }
5289 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5290 tgt->link_reinit_iocb_pending = 1;
5291 /*
5292 * QLogic requires to wait after LINK REINIT for possible
5293 * PDISC or ADISC ELS commands
5294 */
5295 send_notify_ack = 0;
5296 break;
5297 }
5298
5299 case IMM_NTFY_PORT_LOGOUT:
5300 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5301 "qla_target(%d): Port logout (loop "
5302 "%#x, subcode %x)\n", vha->vp_idx,
5303 le16_to_cpu(iocb->u.isp24.nport_handle),
5304 iocb->u.isp24.status_subcode);
5305
5306 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5307 send_notify_ack = 0;
5308 /* The sessions will be cleared in the callback, if needed */
5309 break;
5310
5311 case IMM_NTFY_GLBL_TPRLO:
5312 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5313 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5314 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5315 send_notify_ack = 0;
5316 /* The sessions will be cleared in the callback, if needed */
5317 break;
5318
5319 case IMM_NTFY_PORT_CONFIG:
5320 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5321 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5322 status);
5323 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5324 send_notify_ack = 0;
5325 /* The sessions will be cleared in the callback, if needed */
5326 break;
5327
5328 case IMM_NTFY_GLBL_LOGO:
5329 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5330 "qla_target(%d): Link failure detected\n",
5331 vha->vp_idx);
5332 /* I_T nexus loss */
5333 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5334 send_notify_ack = 0;
5335 break;
5336
5337 case IMM_NTFY_IOCB_OVERFLOW:
5338 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5339 "qla_target(%d): Cannot provide requested "
5340 "capability (IOCB overflowed the immediate notify "
5341 "resource count)\n", vha->vp_idx);
5342 break;
5343
5344 case IMM_NTFY_ABORT_TASK:
5345 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5346 "qla_target(%d): Abort Task (S %08x I %#x -> "
5347 "L %#x)\n", vha->vp_idx,
5348 le16_to_cpu(iocb->u.isp2x.seq_id),
5349 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5350 le16_to_cpu(iocb->u.isp2x.lun));
5351 if (qlt_abort_task(vha, iocb) == 0)
5352 send_notify_ack = 0;
5353 break;
5354
5355 case IMM_NTFY_RESOURCE:
5356 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5357 "qla_target(%d): Out of resources, host %ld\n",
5358 vha->vp_idx, vha->host_no);
5359 break;
5360
5361 case IMM_NTFY_MSG_RX:
5362 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5363 "qla_target(%d): Immediate notify task %x\n",
5364 vha->vp_idx, iocb->u.isp2x.task_flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005365 break;
5366
5367 case IMM_NTFY_ELS:
5368 if (qlt_24xx_handle_els(vha, iocb) == 0)
5369 send_notify_ack = 0;
5370 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005371 default:
5372 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5373 "qla_target(%d): Received unknown immediate "
5374 "notify status %x\n", vha->vp_idx, status);
5375 break;
5376 }
5377
5378 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005379 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5380 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005381}
5382
5383/*
5384 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5385 * This function sends busy to ISP 2xxx or 24xx.
5386 */
Quinn Tran82de8022017-06-13 20:47:17 -07005387static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005388 struct atio_from_isp *atio, uint16_t status)
5389{
Quinn Tran82de8022017-06-13 20:47:17 -07005390 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005391 struct ctio7_to_24xx *ctio24;
5392 struct qla_hw_data *ha = vha->hw;
5393 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005394 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005395 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005396 u16 temp;
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005397 port_id_t id;
5398
5399 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5400 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5401 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5402 id.b.rsvd_1 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005403
Quinn Tran75601512015-12-17 14:57:04 -05005404 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005405 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
Quinn Tran75601512015-12-17 14:57:04 -05005406 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005407 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005408 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005409 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005410 }
5411 /* Sending marker isn't necessary, since we called from ISR */
5412
Quinn Tran82de8022017-06-13 20:47:17 -07005413 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005414 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005415 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005416 "qla_target(%d): %s failed: unable to allocate "
5417 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005418 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005419 }
5420
Quinn Tran60a9ead2017-06-13 20:47:28 -07005421 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005422 pkt->entry_count = 1;
5423 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5424
5425 ctio24 = (struct ctio7_to_24xx *)pkt;
5426 ctio24->entry_type = CTIO_TYPE7;
5427 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005428 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005429 ctio24->vp_index = vha->vp_idx;
5430 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5431 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5432 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5433 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005434 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005435 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005436 CTIO7_FLAGS_DONT_RET_CTIO;
5437 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005438 /*
5439 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5440 * if the explicit conformation is used.
5441 */
5442 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5443 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Quinn Trane25f7652018-05-01 09:01:50 -07005444
5445 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5446
5447 if (ctio24->u.status1.residual != 0)
5448 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5449
Himanshu Madhani63163e02014-09-25 06:14:59 -04005450 /* Memory Barrier */
5451 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005452 if (qpair->reqq_start_iocbs)
5453 qpair->reqq_start_iocbs(qpair);
5454 else
5455 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005456 return 0;
5457}
5458
5459/*
5460 * This routine is used to allocate a command for either a QFull condition
5461 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5462 * out previously.
5463 */
5464static void
5465qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5466 struct atio_from_isp *atio, uint16_t status, int qfull)
5467{
5468 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5469 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005470 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005471 struct se_session *se_sess;
5472 struct qla_tgt_cmd *cmd;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005473 int tag, cpu;
Quinn Tran82de8022017-06-13 20:47:17 -07005474 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005475
5476 if (unlikely(tgt->tgt_stop)) {
5477 ql_dbg(ql_dbg_io, vha, 0x300a,
5478 "New command while device %p is shutting down\n", tgt);
5479 return;
5480 }
5481
5482 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5483 vha->hw->tgt.num_qfull_cmds_dropped++;
5484 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005485 vha->qla_stats.stat_max_qfull_cmds_dropped)
5486 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005487 vha->hw->tgt.num_qfull_cmds_dropped;
5488
5489 ql_dbg(ql_dbg_io, vha, 0x3068,
5490 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5491 vha->vp_idx, __func__,
5492 vha->hw->tgt.num_qfull_cmds_dropped);
5493
5494 qlt_chk_exch_leak_thresh_hold(vha);
5495 return;
5496 }
5497
5498 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5499 (vha, atio->u.isp24.fcp_hdr.s_id);
5500 if (!sess)
5501 return;
5502
5503 se_sess = sess->se_sess;
5504
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005505 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Quinn Tran33e79972014-09-25 06:14:55 -04005506 if (tag < 0)
5507 return;
5508
5509 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5510 if (!cmd) {
5511 ql_dbg(ql_dbg_io, vha, 0x3009,
5512 "qla_target(%d): %s: Allocation of cmd failed\n",
5513 vha->vp_idx, __func__);
5514
5515 vha->hw->tgt.num_qfull_cmds_dropped++;
5516 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005517 vha->qla_stats.stat_max_qfull_cmds_dropped)
5518 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005519 vha->hw->tgt.num_qfull_cmds_dropped;
5520
5521 qlt_chk_exch_leak_thresh_hold(vha);
5522 return;
5523 }
5524
5525 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5526
5527 qlt_incr_num_pend_cmds(vha);
5528 INIT_LIST_HEAD(&cmd->cmd_list);
5529 memcpy(&cmd->atio, atio, sizeof(*atio));
5530
5531 cmd->tgt = vha->vha_tgt.qla_tgt;
5532 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005533 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005534 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005535 cmd->qpair = ha->base_qpair;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -07005536 cmd->se_cmd.map_cpu = cpu;
Quinn Tran33e79972014-09-25 06:14:55 -04005537
5538 if (qfull) {
5539 cmd->q_full = 1;
5540 /* NOTE: borrowing the state field to carry the status */
5541 cmd->state = status;
5542 } else
5543 cmd->term_exchg = 1;
5544
Quinn Tran82de8022017-06-13 20:47:17 -07005545 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005546 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5547
5548 vha->hw->tgt.num_qfull_cmds_alloc++;
5549 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005550 vha->qla_stats.stat_max_qfull_cmds_alloc)
5551 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005552 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005553 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005554}
5555
5556int
Quinn Tran82de8022017-06-13 20:47:17 -07005557qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005558{
Quinn Tran82de8022017-06-13 20:47:17 -07005559 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005560 struct qla_hw_data *ha = vha->hw;
5561 unsigned long flags;
5562 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005563 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005564 int rc = 0;
5565
5566 if (list_empty(&ha->tgt.q_full_list))
5567 return 0;
5568
5569 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005570 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005571
Quinn Tran82de8022017-06-13 20:47:17 -07005572 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005573 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005574 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005575 return 0;
5576 }
5577
Quinn Tran82de8022017-06-13 20:47:17 -07005578 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5579 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5580
5581 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5582 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005583 if (cmd->q_full)
5584 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005585 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005586 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005587 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005588
5589 if (rc == -ENOMEM)
5590 break;
5591
5592 if (cmd->q_full)
5593 ql_dbg(ql_dbg_io, vha, 0x3006,
5594 "%s: busy sent for ox_id[%04x]\n", __func__,
5595 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5596 else if (cmd->term_exchg)
5597 ql_dbg(ql_dbg_io, vha, 0x3007,
5598 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5599 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5600 else
5601 ql_dbg(ql_dbg_io, vha, 0x3008,
5602 "%s: Unexpected cmd in QFull list %p\n", __func__,
5603 cmd);
5604
5605 list_del(&cmd->cmd_list);
5606 list_add_tail(&cmd->cmd_list, &free_list);
5607
5608 /* piggy back on hardware_lock for protection */
5609 vha->hw->tgt.num_qfull_cmds_alloc--;
5610 }
Quinn Tran82de8022017-06-13 20:47:17 -07005611 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005612
5613 cmd = NULL;
5614
5615 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5616 list_del(&cmd->cmd_list);
5617 /* This cmd was never sent to TCM. There is no need
5618 * to schedule free or call free_cmd
5619 */
5620 qlt_free_cmd(cmd);
5621 }
Quinn Tran82de8022017-06-13 20:47:17 -07005622
5623 if (!list_empty(&q_full_list)) {
5624 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5625 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5626 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5627 }
5628
Quinn Tran33e79972014-09-25 06:14:55 -04005629 return rc;
5630}
5631
5632static void
Quinn Tran82de8022017-06-13 20:47:17 -07005633qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5634 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005635{
5636 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005637 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005638
Quinn Tran82de8022017-06-13 20:47:17 -07005639 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005640 if (rc == -ENOMEM)
5641 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5642}
5643
5644static int
Quinn Tran82de8022017-06-13 20:47:17 -07005645qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5646 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005647{
5648 struct qla_hw_data *ha = vha->hw;
Quinn Tran8b666802017-03-15 09:48:45 -07005649 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005650
5651 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5652 return 0;
5653
Quinn Tran8b666802017-03-15 09:48:45 -07005654 if (!ha_locked)
5655 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005656 qlt_send_busy(qpair, atio, qla_sam_status);
Quinn Tran8b666802017-03-15 09:48:45 -07005657 if (!ha_locked)
5658 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5659
Quinn Tran33e79972014-09-25 06:14:55 -04005660 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005661}
5662
5663/* ha->hardware_lock supposed to be held on entry */
5664/* called via callback from qla2xxx */
5665static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005666 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005667{
5668 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005669 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005670 int rc;
Quinn Tran7cf95f72017-12-28 12:33:28 -08005671 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005672
5673 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005674 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005675 "ATIO pkt, but no tgt (ha %p)", ha);
5676 return;
5677 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005678 /*
5679 * In tgt_stop mode we also should allow all requests to pass.
5680 * Otherwise, some commands can stuck.
5681 */
5682
Quinn Tran2f424b92015-12-17 14:57:07 -05005683 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005684
5685 switch (atio->u.raw.entry_type) {
5686 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005687 if (unlikely(atio->u.isp24.exchange_addr ==
5688 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005689 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005690 "qla_target(%d): ATIO_TYPE7 "
5691 "received with UNKNOWN exchange address, "
5692 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005693 if (!ha_locked)
5694 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005695 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
Quinn Tran2f424b92015-12-17 14:57:07 -05005696 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005697 spin_unlock_irqrestore(&ha->hardware_lock,
5698 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005699 break;
5700 }
Quinn Tran33e79972014-09-25 06:14:55 -04005701
Quinn Tran33e79972014-09-25 06:14:55 -04005702 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005703 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5704 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005705 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005706 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005707 return;
5708 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005709 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005710 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005711 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005712 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005713 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005714 if (!ha_locked)
5715 spin_lock_irqsave(&ha->hardware_lock, flags);
5716 switch (rc) {
5717 case -ENODEV:
5718 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5719 "qla_target: Unable to send command to target\n");
5720 break;
5721 case -EBADF:
5722 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5723 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005724 qlt_send_term_exchange(ha->base_qpair, NULL,
5725 atio, 1, 0);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005726 break;
5727 case -EBUSY:
5728 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5729 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5730 vha->vp_idx);
5731 qlt_send_busy(ha->base_qpair, atio,
5732 tc_sam_status);
5733 break;
5734 default:
5735 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5736 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5737 vha->vp_idx);
5738 qlt_send_busy(ha->base_qpair, atio,
5739 qla_sam_status);
5740 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005741 }
Quinn Tran7cf95f72017-12-28 12:33:28 -08005742 if (!ha_locked)
5743 spin_unlock_irqrestore(&ha->hardware_lock,
5744 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005745 }
5746 break;
5747
5748 case IMMED_NOTIFY_TYPE:
5749 {
5750 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5751 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5752 "qla_target(%d): Received ATIO packet %x "
5753 "with error status %x\n", vha->vp_idx,
5754 atio->u.raw.entry_type,
5755 atio->u.isp2x.entry_status);
5756 break;
5757 }
5758 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005759
5760 if (!ha_locked)
5761 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005762 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005763 if (!ha_locked)
5764 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005765 break;
5766 }
5767
5768 default:
5769 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5770 "qla_target(%d): Received unknown ATIO atio "
5771 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5772 break;
5773 }
5774
Quinn Tran2f424b92015-12-17 14:57:07 -05005775 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005776}
5777
Quinn Tran06910942018-09-04 14:19:12 -07005778/*
5779 * qpair lock is assume to be held
5780 * rc = 0 : send terminate & abts respond
5781 * rc != 0: do not send term & abts respond
5782 */
5783static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5784 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5785{
5786 struct qla_hw_data *ha = vha->hw;
5787 int rc = 0;
5788
5789 /*
5790 * Detect unresolved exchange. If the same ABTS is unable
5791 * to terminate an existing command and the same ABTS loops
5792 * between FW & Driver, then force FW dump. Under 1 jiff,
5793 * we should see multiple loops.
5794 */
5795 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5796 qpair->retry_term_jiff == jiffies) {
5797 /* found existing exchange */
5798 qpair->retry_term_cnt++;
5799 if (qpair->retry_term_cnt >= 5) {
5800 rc = EIO;
5801 qpair->retry_term_cnt = 0;
5802 ql_log(ql_log_warn, vha, 0xffff,
5803 "Unable to send ABTS Respond. Dumping firmware.\n");
5804 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5805 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5806
5807 if (qpair == ha->base_qpair)
5808 ha->isp_ops->fw_dump(vha, 1);
5809 else
5810 ha->isp_ops->fw_dump(vha, 0);
5811
5812 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5813 qla2xxx_wake_dpc(vha);
5814 }
5815 } else if (qpair->retry_term_jiff != jiffies) {
5816 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5817 qpair->retry_term_cnt = 0;
5818 qpair->retry_term_jiff = jiffies;
5819 }
5820
5821 return rc;
5822}
5823
Quinn Tran6b0431d2018-09-04 14:19:13 -07005824
5825static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5826 struct rsp_que *rsp, response_t *pkt)
5827{
5828 struct abts_resp_from_24xx_fw *entry =
5829 (struct abts_resp_from_24xx_fw *)pkt;
5830 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5831 struct qla_tgt_mgmt_cmd *mcmd;
5832 struct qla_hw_data *ha = vha->hw;
5833
Bart Van Assche81bcf1c2019-04-11 14:53:24 -07005834 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
Quinn Tran6b0431d2018-09-04 14:19:13 -07005835 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5836 ql_dbg(ql_dbg_async, vha, 0xe064,
5837 "qla_target(%d): ABTS Comp without mcmd\n",
5838 vha->vp_idx);
5839 return;
5840 }
5841
5842 if (mcmd)
5843 vha = mcmd->vha;
5844 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5845
5846 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5847 "ABTS_RESP_24XX: compl_status %x\n",
5848 entry->compl_status);
5849
5850 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5851 if ((entry->error_subcode1 == 0x1E) &&
5852 (entry->error_subcode2 == 0)) {
5853 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5854 ha->tgt.tgt_ops->free_mcmd(mcmd);
5855 return;
5856 }
5857 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5858 pkt, mcmd);
5859 } else {
5860 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5861 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5862 vha->vp_idx, entry->compl_status,
5863 entry->error_subcode1,
5864 entry->error_subcode2);
5865 ha->tgt.tgt_ops->free_mcmd(mcmd);
5866 }
5867 } else {
5868 ha->tgt.tgt_ops->free_mcmd(mcmd);
5869 }
5870}
5871
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005872/* ha->hardware_lock supposed to be held on entry */
5873/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005874static void qlt_response_pkt(struct scsi_qla_host *vha,
5875 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005876{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005877 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005878
5879 if (unlikely(tgt == NULL)) {
5880 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005881 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5882 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005883 return;
5884 }
5885
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005886 /*
5887 * In tgt_stop mode we also should allow all requests to pass.
5888 * Otherwise, some commands can stuck.
5889 */
5890
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005891 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005892 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005893 case CTIO_TYPE7:
5894 {
5895 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005896
Quinn Tran82de8022017-06-13 20:47:17 -07005897 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005898 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5899 entry);
5900 break;
5901 }
5902
5903 case ACCEPT_TGT_IO_TYPE:
5904 {
5905 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5906 int rc;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005907
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005908 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005909 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005910 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5911 "qla_target(%d): ATIO with error "
5912 "status %x received\n", vha->vp_idx,
5913 le16_to_cpu(atio->u.isp2x.status));
5914 break;
5915 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005916
Quinn Tran82de8022017-06-13 20:47:17 -07005917 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005918 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005919 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005920
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005921 rc = qlt_handle_cmd_for_atio(vha, atio);
5922 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005923 switch (rc) {
5924 case -ENODEV:
5925 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5926 "qla_target: Unable to send command to target\n");
5927 break;
5928 case -EBADF:
5929 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5930 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5931 qlt_send_term_exchange(rsp->qpair, NULL,
5932 atio, 1, 0);
5933 break;
5934 case -EBUSY:
5935 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5936 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5937 vha->vp_idx);
5938 qlt_send_busy(rsp->qpair, atio,
5939 tc_sam_status);
5940 break;
5941 default:
5942 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5943 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5944 vha->vp_idx);
5945 qlt_send_busy(rsp->qpair, atio,
5946 qla_sam_status);
5947 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005948 }
5949 }
5950 }
5951 break;
5952
5953 case CONTINUE_TGT_IO_TYPE:
5954 {
5955 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005956
Quinn Tran82de8022017-06-13 20:47:17 -07005957 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005958 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5959 entry);
5960 break;
5961 }
5962
5963 case CTIO_A64_TYPE:
5964 {
5965 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005966
Quinn Tran82de8022017-06-13 20:47:17 -07005967 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005968 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5969 entry);
5970 break;
5971 }
5972
5973 case IMMED_NOTIFY_TYPE:
5974 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5975 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5976 break;
5977
5978 case NOTIFY_ACK_TYPE:
5979 if (tgt->notify_ack_expected > 0) {
5980 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07005981
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005982 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5983 "NOTIFY_ACK seq %08x status %x\n",
5984 le16_to_cpu(entry->u.isp2x.seq_id),
5985 le16_to_cpu(entry->u.isp2x.status));
5986 tgt->notify_ack_expected--;
5987 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005988 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005989 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5990 "qla_target(%d): NOTIFY_ACK "
5991 "failed %x\n", vha->vp_idx,
5992 le16_to_cpu(entry->u.isp2x.status));
5993 }
5994 } else {
5995 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5996 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5997 vha->vp_idx);
5998 }
5999 break;
6000
6001 case ABTS_RECV_24XX:
6002 ql_dbg(ql_dbg_tgt, vha, 0xe037,
6003 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
6004 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
6005 break;
6006
6007 case ABTS_RESP_24XX:
6008 if (tgt->abts_resp_expected > 0) {
Quinn Tran6b0431d2018-09-04 14:19:13 -07006009 qlt_handle_abts_completion(vha, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006010 } else {
6011 ql_dbg(ql_dbg_tgt, vha, 0xe064,
6012 "qla_target(%d): Unexpected ABTS_RESP_24XX "
6013 "received\n", vha->vp_idx);
6014 }
6015 break;
6016
6017 default:
6018 ql_dbg(ql_dbg_tgt, vha, 0xe065,
6019 "qla_target(%d): Received unknown response pkt "
6020 "type %x\n", vha->vp_idx, pkt->entry_type);
6021 break;
6022 }
6023
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006024}
6025
6026/*
6027 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6028 */
6029void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6030 uint16_t *mailbox)
6031{
6032 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006033 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01006034 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006035
Quinn Tran3a33dc92017-06-02 09:12:04 -07006036 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006037 return;
6038
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006039 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6040 IS_QLA2100(ha))
6041 return;
6042 /*
6043 * In tgt_stop mode we also should allow all requests to pass.
6044 * Otherwise, some commands can stuck.
6045 */
6046
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006047
6048 switch (code) {
6049 case MBA_RESET: /* Reset */
6050 case MBA_SYSTEM_ERR: /* System Error */
6051 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
6052 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
6053 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6054 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006055 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006056 break;
6057 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
6058 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6059 break;
6060
6061 case MBA_LOOP_UP:
6062 {
6063 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006064 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01006065 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6066 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6067 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006068 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07006069 qlt_send_notify_ack(ha->base_qpair,
6070 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006071 0, 0, 0, 0, 0, 0);
6072 tgt->link_reinit_iocb_pending = 0;
6073 }
6074 break;
6075 }
6076
6077 case MBA_LIP_OCCURRED:
6078 case MBA_LOOP_DOWN:
6079 case MBA_LIP_RESET:
6080 case MBA_RSCN_UPDATE:
6081 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006082 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01006083 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6084 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6085 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006086 break;
6087
Quinn Tranead03852017-01-19 22:28:01 -08006088 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07006089 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6090 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6091 vha->vp_idx,
6092 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6093 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08006094
6095 if (le16_to_cpu(mailbox[3]) == 1) {
6096 /* exchange starvation. */
6097 vha->hw->exch_starvation++;
6098 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006099 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08006100 "Exchange starvation-. Resetting RISC\n");
6101
6102 vha->hw->exch_starvation = 0;
6103 if (IS_P3P_TYPE(vha->hw))
6104 set_bit(FCOE_CTX_RESET_NEEDED,
6105 &vha->dpc_flags);
6106 else
6107 set_bit(ISP_ABORT_NEEDED,
6108 &vha->dpc_flags);
6109 qla2xxx_wake_dpc(vha);
6110 }
6111 }
6112 break;
6113
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006114 case MBA_PORT_UPDATE:
6115 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6116 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09006117 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01006118 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6119 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6120 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6121
6122 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08006123 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006124 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6125 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08006126 vha->hw->exch_starvation = 0;
6127 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006128 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6129 "Async MB 2: Port Logged Out\n");
6130 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006131 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006132 break;
6133 }
6134
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006135}
6136
6137static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6138 uint16_t loop_id)
6139{
Quinn Tran726b8542017-01-19 22:28:00 -08006140 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006141 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08006142 unsigned long flags;
6143 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006144
Quinn Tran063b36d2017-12-04 14:45:10 -08006145 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006146 if (!fcport) {
6147 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6148 "qla_target(%d): Allocation of tmp FC port failed",
6149 vha->vp_idx);
6150 return NULL;
6151 }
6152
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006153 fcport->loop_id = loop_id;
6154
Quinn Tran15f30a52017-03-15 09:48:52 -07006155 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006156 if (rc != QLA_SUCCESS) {
6157 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6158 "qla_target(%d): Failed to retrieve fcport "
6159 "information -- get_port_database() returned %x "
6160 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6161 kfree(fcport);
6162 return NULL;
6163 }
6164
Quinn Tran726b8542017-01-19 22:28:00 -08006165 del = NULL;
6166 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6167 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6168
6169 if (tfcp) {
6170 tfcp->d_id = fcport->d_id;
6171 tfcp->port_type = fcport->port_type;
6172 tfcp->supported_classes = fcport->supported_classes;
6173 tfcp->flags |= fcport->flags;
Quinn Tran76f9a2d2017-12-04 14:45:11 -08006174 tfcp->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08006175
6176 del = fcport;
6177 fcport = tfcp;
6178 } else {
6179 if (vha->hw->current_topology == ISP_CFG_F)
6180 fcport->flags |= FCF_FABRIC_DEVICE;
6181
6182 list_add_tail(&fcport->list, &vha->vp_fcports);
6183 if (!IS_SW_RESV_ADDR(fcport->d_id))
6184 vha->fcport_count++;
6185 fcport->login_gen++;
6186 fcport->disc_state = DSC_LOGIN_COMPLETE;
6187 fcport->login_succ = 1;
6188 newfcport = 1;
6189 }
6190
6191 fcport->deleted = 0;
6192 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6193
6194 switch (vha->host->active_mode) {
6195 case MODE_INITIATOR:
6196 case MODE_DUAL:
6197 if (newfcport) {
6198 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Trancd4ed6b2018-08-31 11:24:31 -07006199 qla24xx_sched_upd_fcport(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08006200 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07006201 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08006202 "%s %d %8phC post gpsc fcp_cnt %d\n",
6203 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6204 qla24xx_post_gpsc_work(vha, fcport);
6205 }
6206 }
6207 break;
6208
6209 case MODE_TARGET:
6210 default:
6211 break;
6212 }
6213 if (del)
6214 qla2x00_free_fcport(del);
6215
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006216 return fcport;
6217}
6218
6219/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08006220static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006221 uint8_t *s_id)
6222{
Quinn Tran5d964832017-01-19 22:27:59 -08006223 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006224 fc_port_t *fcport = NULL;
6225 int rc, global_resets;
6226 uint16_t loop_id = 0;
6227
Quinn Tran726b8542017-01-19 22:28:00 -08006228 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
6229 /*
6230 * This is Domain Controller, so it should be
6231 * OK to drop SCSI commands from it.
6232 */
6233 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6234 "Unable to find initiator with S_ID %x:%x:%x",
6235 s_id[0], s_id[1], s_id[2]);
6236 return NULL;
6237 }
6238
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006239 mutex_lock(&vha->vha_tgt.tgt_mutex);
6240
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006241retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006242 global_resets =
6243 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006244
6245 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6246 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006247 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6248
Quinn Tran726b8542017-01-19 22:28:00 -08006249 ql_log(ql_log_info, vha, 0xf071,
6250 "qla_target(%d): Unable to find "
6251 "initiator with S_ID %x:%x:%x",
6252 vha->vp_idx, s_id[0], s_id[1],
6253 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006254
6255 if (rc == -ENOENT) {
6256 qlt_port_logo_t logo;
Bart Van Asschebd432bb2019-04-11 14:53:17 -07006257
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006258 sid_to_portid(s_id, &logo.id);
6259 logo.cmd_count = 1;
6260 qlt_send_first_logo(vha, &logo);
6261 }
6262
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006263 return NULL;
6264 }
6265
6266 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006267 if (!fcport) {
6268 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006269 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006270 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006271
6272 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006273 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006274 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6275 "qla_target(%d): global reset during session discovery "
6276 "(counter was %d, new %d), retrying", vha->vp_idx,
6277 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006278 atomic_read(&vha->vha_tgt.
6279 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006280 goto retry;
6281 }
6282
6283 sess = qlt_create_sess(vha, fcport, true);
6284
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006285 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6286
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006287 return sess;
6288}
6289
6290static void qlt_abort_work(struct qla_tgt *tgt,
6291 struct qla_tgt_sess_work_param *prm)
6292{
6293 struct scsi_qla_host *vha = tgt->vha;
6294 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08006295 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006296 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006297 uint32_t be_s_id;
6298 uint8_t s_id[3];
6299 int rc;
6300
Quinn Tran75601512015-12-17 14:57:04 -05006301 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006302
6303 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05006304 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006305
6306 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6307 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6308 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6309
6310 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6311 (unsigned char *)&be_s_id);
6312 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006313 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006314
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006315 sess = qlt_make_local_sess(vha, s_id);
6316 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006317
Quinn Tran75601512015-12-17 14:57:04 -05006318 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006319 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05006320 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006321 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006322 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006323 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006324 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006325 }
6326
Quinn Tran726b8542017-01-19 22:28:00 -08006327 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006328 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08006329 "%s: kref_get fail %8phC \n",
6330 __func__, sess->port_name);
6331 sess = NULL;
6332 goto out_term2;
6333 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006334 }
6335
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006336 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006337 ha->tgt.tgt_ops->put_sess(sess);
6338 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6339
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006340 if (rc != 0)
6341 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006342 return;
6343
Quinn Tran75601512015-12-17 14:57:04 -05006344out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08006345 if (sess)
6346 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05006347 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006348
6349out_term:
6350 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006351 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6352 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006353 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006354}
6355
6356static void qlt_tmr_work(struct qla_tgt *tgt,
6357 struct qla_tgt_sess_work_param *prm)
6358{
6359 struct atio_from_isp *a = &prm->tm_iocb2;
6360 struct scsi_qla_host *vha = tgt->vha;
6361 struct qla_hw_data *ha = vha->hw;
Colin Ian Kingbb6abdd2019-02-15 09:52:32 +00006362 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006363 unsigned long flags;
6364 uint8_t *s_id = NULL; /* to hide compiler warnings */
6365 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07006366 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07006367 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006368 void *iocb;
6369
Quinn Tran75601512015-12-17 14:57:04 -05006370 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006371
6372 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006373 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006374
6375 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6376 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6377 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006378 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006379
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006380 sess = qlt_make_local_sess(vha, s_id);
6381 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006382
Quinn Tran75601512015-12-17 14:57:04 -05006383 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006384 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006385 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006386 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006387 if (sess->deleted) {
Quinn Tranf159b3c2017-03-15 09:48:47 -07006388 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006389 }
6390
Quinn Tran726b8542017-01-19 22:28:00 -08006391 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006392 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08006393 "%s: kref_get fail %8phC\n",
6394 __func__, sess->port_name);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006395 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006396 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006397 }
6398
6399 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006400 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006401 unpacked_lun =
6402 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006403
6404 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006405 ha->tgt.tgt_ops->put_sess(sess);
6406 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6407
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006408 if (rc != 0)
6409 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006410 return;
6411
Quinn Tranf159b3c2017-03-15 09:48:47 -07006412out_term2:
Quinn Tranf159b3c2017-03-15 09:48:47 -07006413 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006414out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006415 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006416}
6417
6418static void qlt_sess_work_fn(struct work_struct *work)
6419{
6420 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6421 struct scsi_qla_host *vha = tgt->vha;
6422 unsigned long flags;
6423
6424 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6425
6426 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6427 while (!list_empty(&tgt->sess_works_list)) {
6428 struct qla_tgt_sess_work_param *prm = list_entry(
6429 tgt->sess_works_list.next, typeof(*prm),
6430 sess_works_list_entry);
6431
6432 /*
6433 * This work can be scheduled on several CPUs at time, so we
6434 * must delete the entry to eliminate double processing
6435 */
6436 list_del(&prm->sess_works_list_entry);
6437
6438 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6439
6440 switch (prm->type) {
6441 case QLA_TGT_SESS_WORK_ABORT:
6442 qlt_abort_work(tgt, prm);
6443 break;
6444 case QLA_TGT_SESS_WORK_TM:
6445 qlt_tmr_work(tgt, prm);
6446 break;
6447 default:
6448 BUG_ON(1);
6449 break;
6450 }
6451
6452 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6453
6454 kfree(prm);
6455 }
6456 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6457}
6458
6459/* Must be called under tgt_host_action_mutex */
6460int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6461{
6462 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006463 int rc, i;
6464 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006465
6466 if (!QLA_TGT_MODE_ENABLED())
6467 return 0;
6468
Arun Easi33c36c02013-01-30 03:34:41 -05006469 if (!IS_TGT_MODE_CAPABLE(ha)) {
6470 ql_log(ql_log_warn, base_vha, 0xe070,
6471 "This adapter does not support target mode.\n");
6472 return 0;
6473 }
6474
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006475 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006476 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006477
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006478 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006479
6480 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6481 if (!tgt) {
6482 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6483 "Unable to allocate struct qla_tgt\n");
6484 return -ENOMEM;
6485 }
6486
Kees Cook6396bb22018-06-12 14:03:40 -07006487 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6488 sizeof(struct qla_qpair_hint),
6489 GFP_KERNEL);
Quinn Trane326d222017-06-13 20:47:18 -07006490 if (!tgt->qphints) {
6491 kfree(tgt);
6492 ql_log(ql_log_warn, base_vha, 0x0197,
6493 "Unable to allocate qpair hints.\n");
6494 return -ENOMEM;
6495 }
6496
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006497 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6498 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6499
Quinn Trane326d222017-06-13 20:47:18 -07006500 rc = btree_init64(&tgt->lun_qpair_map);
6501 if (rc) {
6502 kfree(tgt->qphints);
6503 kfree(tgt);
6504 ql_log(ql_log_info, base_vha, 0x0198,
6505 "Unable to initialize lun_qpair_map btree\n");
6506 return -EIO;
6507 }
6508 h = &tgt->qphints[0];
6509 h->qpair = ha->base_qpair;
6510 INIT_LIST_HEAD(&h->hint_elem);
6511 h->cpuid = ha->base_qpair->cpuid;
6512 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6513
6514 for (i = 0; i < ha->max_qpairs; i++) {
6515 unsigned long flags;
6516
6517 struct qla_qpair *qpair = ha->queue_pair_map[i];
Bart Van Asschebd432bb2019-04-11 14:53:17 -07006518
Quinn Trane326d222017-06-13 20:47:18 -07006519 h = &tgt->qphints[i + 1];
6520 INIT_LIST_HEAD(&h->hint_elem);
6521 if (qpair) {
6522 h->qpair = qpair;
6523 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6524 list_add_tail(&h->hint_elem, &qpair->hints_list);
6525 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6526 h->cpuid = qpair->cpuid;
6527 }
6528 }
6529
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006530 tgt->ha = ha;
6531 tgt->vha = base_vha;
6532 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006533 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006534 spin_lock_init(&tgt->sess_work_lock);
6535 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6536 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006537 atomic_set(&tgt->tgt_global_resets_count, 0);
6538
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006539 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006540
6541 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6542 "qla_target(%d): using 64 Bit PCI addressing",
6543 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006544 /* 3 is reserved */
6545 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006546
6547 mutex_lock(&qla_tgt_mutex);
6548 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6549 mutex_unlock(&qla_tgt_mutex);
6550
Quinn Tranf1443ee2017-03-15 09:48:51 -07006551 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6552 ha->tgt.tgt_ops->add_target(base_vha);
6553
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006554 return 0;
6555}
6556
6557/* Must be called under tgt_host_action_mutex */
6558int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6559{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006560 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006561 return 0;
6562
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006563 if (vha->fc_vport) {
6564 qlt_release(vha->vha_tgt.qla_tgt);
6565 return 0;
6566 }
Quinn Tran33e79972014-09-25 06:14:55 -04006567
6568 /* free left over qfull cmds */
6569 qlt_init_term_exchange(vha);
6570
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006571 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6572 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006573 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006574
6575 return 0;
6576}
6577
Quinn Tran482c9dc2017-03-15 09:48:54 -07006578void qlt_remove_target_resources(struct qla_hw_data *ha)
6579{
6580 struct scsi_qla_host *node;
6581 u32 key = 0;
6582
6583 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6584 btree_remove32(&ha->tgt.host_map, key);
6585
6586 btree_destroy32(&ha->tgt.host_map);
6587}
6588
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006589static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6590 unsigned char *b)
6591{
6592 int i;
6593
6594 pr_debug("qla2xxx HW vha->node_name: ");
6595 for (i = 0; i < WWN_SIZE; i++)
6596 pr_debug("%02x ", vha->node_name[i]);
6597 pr_debug("\n");
6598 pr_debug("qla2xxx HW vha->port_name: ");
6599 for (i = 0; i < WWN_SIZE; i++)
6600 pr_debug("%02x ", vha->port_name[i]);
6601 pr_debug("\n");
6602
6603 pr_debug("qla2xxx passed configfs WWPN: ");
6604 put_unaligned_be64(wwpn, b);
6605 for (i = 0; i < WWN_SIZE; i++)
6606 pr_debug("%02x ", b[i]);
6607 pr_debug("\n");
6608}
6609
6610/**
6611 * qla_tgt_lport_register - register lport with external module
6612 *
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006613 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
Bart Van Assche807eb902018-10-18 15:45:41 -07006614 * @phys_wwpn: physical port WWPN
6615 * @npiv_wwpn: NPIV WWPN
6616 * @npiv_wwnn: NPIV WWNN
Bart Van Assche2db62282018-01-23 16:33:51 -08006617 * @callback: lport initialization callback for tcm_qla2xxx code
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006618 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006619int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6620 u64 npiv_wwpn, u64 npiv_wwnn,
6621 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006622{
6623 struct qla_tgt *tgt;
6624 struct scsi_qla_host *vha;
6625 struct qla_hw_data *ha;
6626 struct Scsi_Host *host;
6627 unsigned long flags;
6628 int rc;
6629 u8 b[WWN_SIZE];
6630
6631 mutex_lock(&qla_tgt_mutex);
6632 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6633 vha = tgt->vha;
6634 ha = vha->hw;
6635
6636 host = vha->host;
6637 if (!host)
6638 continue;
6639
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006640 if (!(host->hostt->supported_mode & MODE_TARGET))
6641 continue;
6642
Quinn Tran0645cb82018-09-11 10:18:18 -07006643 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6644 continue;
6645
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006646 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006647 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006648 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6649 host->host_no);
6650 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6651 continue;
6652 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006653 if (tgt->tgt_stop) {
6654 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6655 host->host_no);
6656 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6657 continue;
6658 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006659 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6660
6661 if (!scsi_host_get(host)) {
6662 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6663 "Unable to scsi_host_get() for"
6664 " qla2xxx scsi_host\n");
6665 continue;
6666 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006667 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006668
6669 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6670 scsi_host_put(host);
6671 continue;
6672 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006673 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6674 if (rc != 0)
6675 scsi_host_put(host);
6676
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006677 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006678 return rc;
6679 }
6680 mutex_unlock(&qla_tgt_mutex);
6681
6682 return -ENODEV;
6683}
6684EXPORT_SYMBOL(qlt_lport_register);
6685
6686/**
6687 * qla_tgt_lport_deregister - Degister lport
6688 *
6689 * @vha: Registered scsi_qla_host pointer
6690 */
6691void qlt_lport_deregister(struct scsi_qla_host *vha)
6692{
6693 struct qla_hw_data *ha = vha->hw;
6694 struct Scsi_Host *sh = vha->host;
6695 /*
6696 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6697 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006698 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006699 ha->tgt.tgt_ops = NULL;
6700 /*
6701 * Release the Scsi_Host reference for the underlying qla2xxx host
6702 */
6703 scsi_host_put(sh);
6704}
6705EXPORT_SYMBOL(qlt_lport_deregister);
6706
6707/* Must be called under HW lock */
Quinn Tran0645cb82018-09-11 10:18:18 -07006708void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006709{
Quinn Tran0645cb82018-09-11 10:18:18 -07006710 switch (vha->qlini_mode) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006711 case QLA2XXX_INI_MODE_DISABLED:
6712 case QLA2XXX_INI_MODE_EXCLUSIVE:
6713 vha->host->active_mode = MODE_TARGET;
6714 break;
6715 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tran0645cb82018-09-11 10:18:18 -07006716 vha->host->active_mode = MODE_INITIATOR;
Quinn Tranead03852017-01-19 22:28:01 -08006717 break;
6718 case QLA2XXX_INI_MODE_DUAL:
6719 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006720 break;
6721 default:
6722 break;
6723 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006724}
6725
6726/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006727static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006728{
Quinn Tran0645cb82018-09-11 10:18:18 -07006729 switch (vha->qlini_mode) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006730 case QLA2XXX_INI_MODE_DISABLED:
6731 vha->host->active_mode = MODE_UNKNOWN;
6732 break;
6733 case QLA2XXX_INI_MODE_EXCLUSIVE:
6734 vha->host->active_mode = MODE_INITIATOR;
6735 break;
6736 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006737 case QLA2XXX_INI_MODE_DUAL:
6738 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006739 break;
6740 default:
6741 break;
6742 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006743}
6744
6745/*
6746 * qla_tgt_enable_vha - NO LOCK HELD
6747 *
6748 * host_reset, bring up w/ Target Mode Enabled
6749 */
6750void
6751qlt_enable_vha(struct scsi_qla_host *vha)
6752{
6753 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006754 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006755 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006756 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006757
6758 if (!tgt) {
6759 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6760 "Unable to locate qla_tgt pointer from"
6761 " struct qla_hw_data\n");
6762 dump_stack();
6763 return;
6764 }
Quinn Tran0645cb82018-09-11 10:18:18 -07006765 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6766 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006767
6768 spin_lock_irqsave(&ha->hardware_lock, flags);
6769 tgt->tgt_stopped = 0;
6770 qlt_set_mode(vha);
6771 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6772
Quinn Tranb6faaaf2018-09-04 14:19:09 -07006773 mutex_lock(&ha->optrom_mutex);
6774 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6775 "%s.\n", __func__);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006776 if (vha->vp_idx) {
6777 qla24xx_disable_vp(vha);
6778 qla24xx_enable_vp(vha);
6779 } else {
6780 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6781 qla2xxx_wake_dpc(base_vha);
6782 qla2x00_wait_for_hba_online(base_vha);
6783 }
Quinn Tranb6faaaf2018-09-04 14:19:09 -07006784 mutex_unlock(&ha->optrom_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006785}
6786EXPORT_SYMBOL(qlt_enable_vha);
6787
6788/*
6789 * qla_tgt_disable_vha - NO LOCK HELD
6790 *
6791 * Disable Target Mode and reset the adapter
6792 */
Joern Engel55a90662014-09-16 16:23:15 -04006793static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006794{
6795 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006796 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006797 unsigned long flags;
6798
6799 if (!tgt) {
6800 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6801 "Unable to locate qla_tgt pointer from"
6802 " struct qla_hw_data\n");
6803 dump_stack();
6804 return;
6805 }
6806
6807 spin_lock_irqsave(&ha->hardware_lock, flags);
6808 qlt_clear_mode(vha);
6809 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6810
6811 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6812 qla2xxx_wake_dpc(vha);
6813 qla2x00_wait_for_hba_online(vha);
6814}
6815
6816/*
6817 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6818 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6819 * members.
6820 */
6821void
6822qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6823{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006824 vha->vha_tgt.qla_tgt = NULL;
6825
6826 mutex_init(&vha->vha_tgt.tgt_mutex);
6827 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006828
6829 qlt_clear_mode(vha);
6830
6831 /*
6832 * NOTE: Currently the value is kept the same for <24xx and
6833 * >=24xx ISPs. If it is necessary to change it,
6834 * the check should be added for specific ISPs,
6835 * assigning the value appropriately.
6836 */
6837 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006838
6839 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006840}
6841
Quinn Trane374f9f2017-12-28 12:33:31 -08006842u8
6843qlt_rff_id(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006844{
Quinn Trane374f9f2017-12-28 12:33:31 -08006845 u8 fc4_feature = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006846 /*
6847 * FC-4 Feature bit 0 indicates target functionality to the name server.
6848 */
6849 if (qla_tgt_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006850 fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006851 } else if (qla_ini_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006852 fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006853 } else if (qla_dual_mode_enabled(vha))
Quinn Trane374f9f2017-12-28 12:33:31 -08006854 fc4_feature = BIT_0 | BIT_1;
6855
6856 return fc4_feature;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006857}
6858
6859/*
6860 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6861 * @ha: HA context
6862 *
6863 * Beginning of ATIO ring has initialization control block already built
6864 * by nvram config routine.
6865 *
6866 * Returns 0 on success.
6867 */
6868void
6869qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6870{
6871 struct qla_hw_data *ha = vha->hw;
6872 uint16_t cnt;
6873 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6874
Quinn Tranead03852017-01-19 22:28:01 -08006875 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006876 return;
6877
6878 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6879 pkt->u.raw.signature = ATIO_PROCESSED;
6880 pkt++;
6881 }
6882
6883}
6884
6885/*
6886 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6887 * @ha: SCSI driver HA context
6888 */
6889void
Quinn Tran2f424b92015-12-17 14:57:07 -05006890qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006891{
6892 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006893 struct atio_from_isp *pkt;
6894 int cnt, i;
6895
Quinn Tranec7193e2017-03-15 09:48:55 -07006896 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006897 return;
6898
Quinn Tran5f355092016-12-23 18:06:11 -08006899 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6900 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006901 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6902 cnt = pkt->u.raw.entry_count;
6903
Quinn Tran5f355092016-12-23 18:06:11 -08006904 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6905 /*
6906 * This packet is corrupted. The header + payload
6907 * can not be trusted. There is no point in passing
6908 * it further up.
6909 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006910 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006911 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6912 pkt->u.isp24.fcp_hdr.s_id,
6913 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6914 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6915
6916 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006917 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6918 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006919 } else {
6920 qlt_24xx_atio_pkt_all_vps(vha,
6921 (struct atio_from_isp *)pkt, ha_locked);
6922 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006923
6924 for (i = 0; i < cnt; i++) {
6925 ha->tgt.atio_ring_index++;
6926 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6927 ha->tgt.atio_ring_index = 0;
6928 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6929 } else
6930 ha->tgt.atio_ring_ptr++;
6931
6932 pkt->u.raw.signature = ATIO_PROCESSED;
6933 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6934 }
6935 wmb();
6936 }
6937
6938 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006939 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006940}
6941
6942void
Arun Easiaa230bc2013-01-30 03:34:39 -05006943qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006944{
6945 struct qla_hw_data *ha = vha->hw;
Quinn Trand2b292c2017-12-28 12:33:17 -08006946 struct qla_msix_entry *msix = &ha->msix_entries[2];
6947 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6948
Arun Easiaa230bc2013-01-30 03:34:39 -05006949 if (!QLA_TGT_MODE_ENABLED())
6950 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006951
Arun Easiaa230bc2013-01-30 03:34:39 -05006952 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6953 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6954 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6955
Quinn Trand2b292c2017-12-28 12:33:17 -08006956 if (ha->flags.msix_enabled) {
Joe Carnuccioecc89f22019-03-12 11:08:13 -07006957 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
Quinn Trand2b292c2017-12-28 12:33:17 -08006958 if (IS_QLA2071(ha)) {
6959 /* 4 ports Baker: Enable Interrupt Handshake */
6960 icb->msix_atio = 0;
6961 icb->firmware_options_2 |= BIT_26;
6962 } else {
6963 icb->msix_atio = cpu_to_le16(msix->entry);
6964 icb->firmware_options_2 &= ~BIT_26;
6965 }
6966 ql_dbg(ql_dbg_init, vha, 0xf072,
6967 "Registering ICB vector 0x%x for atio que.\n",
6968 msix->entry);
6969 }
6970 } else {
6971 /* INTx|MSI */
Joe Carnuccioecc89f22019-03-12 11:08:13 -07006972 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
Quinn Trand2b292c2017-12-28 12:33:17 -08006973 icb->msix_atio = 0;
6974 icb->firmware_options_2 |= BIT_26;
6975 ql_dbg(ql_dbg_init, vha, 0xf072,
6976 "%s: Use INTx for ATIOQ.\n", __func__);
6977 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006978 }
6979}
6980
6981void
6982qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6983{
6984 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006985 u32 tmp;
Quinn Tran99e1b682017-06-02 09:12:03 -07006986
6987 if (!QLA_TGT_MODE_ENABLED())
6988 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006989
Quinn Tranead03852017-01-19 22:28:01 -08006990 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006991 if (!ha->tgt.saved_set) {
6992 /* We save only once */
6993 ha->tgt.saved_exchange_count = nv->exchange_count;
6994 ha->tgt.saved_firmware_options_1 =
6995 nv->firmware_options_1;
6996 ha->tgt.saved_firmware_options_2 =
6997 nv->firmware_options_2;
6998 ha->tgt.saved_firmware_options_3 =
6999 nv->firmware_options_3;
7000 ha->tgt.saved_set = 1;
7001 }
7002
Quinn Tran99e1b682017-06-02 09:12:03 -07007003 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08007004 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07007005 else /* dual */
Quinn Tran0645cb82018-09-11 10:18:18 -07007006 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007007
7008 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07007009 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007010
7011 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08007012 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07007013 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007014
7015 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007016 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007017 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007018 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04007019 if (ql2xtgt_tape_enable)
7020 /* Enable FC Tape support */
7021 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7022 else
7023 /* Disable FC Tape support */
7024 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7025
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007026 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007027 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08007028
7029 /*
7030 * clear BIT 15 explicitly as we have seen at least
7031 * a couple of instances where this was set and this
7032 * was causing the firmware to not be initialized.
7033 */
7034 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007035 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07007036 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08007037
7038 if (IS_QLA25XX(ha)) {
7039 /* Change Loop-prefer to Pt-Pt */
7040 tmp = ~(BIT_4|BIT_5|BIT_6);
7041 nv->firmware_options_2 &= cpu_to_le32(tmp);
7042 tmp = P2P << 4;
7043 nv->firmware_options_2 |= cpu_to_le32(tmp);
7044 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007045 } else {
7046 if (ha->tgt.saved_set) {
7047 nv->exchange_count = ha->tgt.saved_exchange_count;
7048 nv->firmware_options_1 =
7049 ha->tgt.saved_firmware_options_1;
7050 nv->firmware_options_2 =
7051 ha->tgt.saved_firmware_options_2;
7052 nv->firmware_options_3 =
7053 ha->tgt.saved_firmware_options_3;
7054 }
7055 return;
7056 }
7057
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007058 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007059 if (vha->flags.init_done)
7060 fc_host_supported_classes(vha->host) =
7061 FC_COS_CLASS2 | FC_COS_CLASS3;
7062
Bart Van Asschead950362015-07-09 07:24:08 -07007063 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007064 } else {
7065 if (vha->flags.init_done)
7066 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7067
Bart Van Asschead950362015-07-09 07:24:08 -07007068 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007069 }
7070}
7071
7072void
7073qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7074 struct init_cb_24xx *icb)
7075{
7076 struct qla_hw_data *ha = vha->hw;
7077
Quinn Tran481ce732015-12-17 14:57:08 -05007078 if (!QLA_TGT_MODE_ENABLED())
7079 return;
7080
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007081 if (ha->tgt.node_name_set) {
7082 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07007083 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007084 }
7085}
7086
Arun Easiaa230bc2013-01-30 03:34:39 -05007087void
7088qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7089{
7090 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08007091 u32 tmp;
Arun Easiaa230bc2013-01-30 03:34:39 -05007092
7093 if (!QLA_TGT_MODE_ENABLED())
7094 return;
7095
Quinn Tranead03852017-01-19 22:28:01 -08007096 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007097 if (!ha->tgt.saved_set) {
7098 /* We save only once */
7099 ha->tgt.saved_exchange_count = nv->exchange_count;
7100 ha->tgt.saved_firmware_options_1 =
7101 nv->firmware_options_1;
7102 ha->tgt.saved_firmware_options_2 =
7103 nv->firmware_options_2;
7104 ha->tgt.saved_firmware_options_3 =
7105 nv->firmware_options_3;
7106 ha->tgt.saved_set = 1;
7107 }
7108
Quinn Tran99e1b682017-06-02 09:12:03 -07007109 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08007110 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07007111 else /* dual */
Quinn Tran0645cb82018-09-11 10:18:18 -07007112 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05007113
7114 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07007115 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05007116
7117 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08007118 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07007119 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05007120 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007121 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05007122 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007123 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08007124 /*
7125 * clear BIT 15 explicitly as we have seen at
7126 * least a couple of instances where this was set
7127 * and this was causing the firmware to not be
7128 * initialized.
7129 */
7130 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04007131 if (ql2xtgt_tape_enable)
7132 /* Enable FC tape support */
7133 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7134 else
7135 /* Disable FC tape support */
7136 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7137
Arun Easiaa230bc2013-01-30 03:34:39 -05007138 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07007139 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05007140 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07007141 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08007142
7143 /* Change Loop-prefer to Pt-Pt */
7144 tmp = ~(BIT_4|BIT_5|BIT_6);
7145 nv->firmware_options_2 &= cpu_to_le32(tmp);
7146 tmp = P2P << 4;
7147 nv->firmware_options_2 |= cpu_to_le32(tmp);
Arun Easiaa230bc2013-01-30 03:34:39 -05007148 } else {
7149 if (ha->tgt.saved_set) {
7150 nv->exchange_count = ha->tgt.saved_exchange_count;
7151 nv->firmware_options_1 =
7152 ha->tgt.saved_firmware_options_1;
7153 nv->firmware_options_2 =
7154 ha->tgt.saved_firmware_options_2;
7155 nv->firmware_options_3 =
7156 ha->tgt.saved_firmware_options_3;
7157 }
7158 return;
7159 }
7160
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007161 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007162 if (vha->flags.init_done)
7163 fc_host_supported_classes(vha->host) =
7164 FC_COS_CLASS2 | FC_COS_CLASS3;
7165
Bart Van Asschead950362015-07-09 07:24:08 -07007166 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05007167 } else {
7168 if (vha->flags.init_done)
7169 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7170
Bart Van Asschead950362015-07-09 07:24:08 -07007171 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05007172 }
7173}
7174
7175void
7176qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7177 struct init_cb_81xx *icb)
7178{
7179 struct qla_hw_data *ha = vha->hw;
7180
7181 if (!QLA_TGT_MODE_ENABLED())
7182 return;
7183
7184 if (ha->tgt.node_name_set) {
7185 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07007186 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05007187 }
7188}
7189
7190void
7191qlt_83xx_iospace_config(struct qla_hw_data *ha)
7192{
7193 if (!QLA_TGT_MODE_ENABLED())
7194 return;
7195
7196 ha->msix_count += 1; /* For ATIO Q */
7197}
7198
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007199
7200void
7201qlt_modify_vp_config(struct scsi_qla_host *vha,
7202 struct vp_config_entry_24xx *vpmod)
7203{
Quinn Tranead03852017-01-19 22:28:01 -08007204 /* enable target mode. Bit5 = 1 => disable */
7205 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007206 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08007207
Quinn Tranead03852017-01-19 22:28:01 -08007208 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08007209 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007210 vpmod->options_idx1 &= ~BIT_4;
7211}
7212
7213void
7214qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7215{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007216 int rc;
7217
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007218 if (!QLA_TGT_MODE_ENABLED())
7219 return;
7220
Joe Carnuccioecc89f22019-03-12 11:08:13 -07007221 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
7222 IS_QLA28XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05007223 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7224 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7225 } else {
7226 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7227 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7228 }
7229
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08007230 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7231 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08007232
7233 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7234 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7235 qlt_unknown_atio_work_fn);
7236
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007237 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007238
7239 rc = btree_init32(&ha->tgt.host_map);
7240 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007241 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007242 "Unable to initialize ha->host_map btree\n");
7243
7244 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007245}
7246
Arun Easiaa230bc2013-01-30 03:34:39 -05007247irqreturn_t
7248qla83xx_msix_atio_q(int irq, void *dev_id)
7249{
7250 struct rsp_que *rsp;
7251 scsi_qla_host_t *vha;
7252 struct qla_hw_data *ha;
7253 unsigned long flags;
7254
7255 rsp = (struct rsp_que *) dev_id;
7256 ha = rsp->hw;
7257 vha = pci_get_drvdata(ha->pdev);
7258
Quinn Tran2f424b92015-12-17 14:57:07 -05007259 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007260
Quinn Tran2f424b92015-12-17 14:57:07 -05007261 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05007262
Quinn Tran2f424b92015-12-17 14:57:07 -05007263 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007264
7265 return IRQ_HANDLED;
7266}
7267
Quinn Tran2f424b92015-12-17 14:57:07 -05007268static void
7269qlt_handle_abts_recv_work(struct work_struct *work)
7270{
7271 struct qla_tgt_sess_op *op = container_of(work,
7272 struct qla_tgt_sess_op, work);
7273 scsi_qla_host_t *vha = op->vha;
7274 struct qla_hw_data *ha = vha->hw;
7275 unsigned long flags;
7276
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007277 if (qla2x00_reset_active(vha) ||
7278 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05007279 return;
7280
7281 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7282 qlt_24xx_process_atio_queue(vha, 0);
7283 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7284
7285 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07007286 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05007287 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07007288
7289 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05007290}
7291
7292void
Quinn Tran82de8022017-06-13 20:47:17 -07007293qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7294 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05007295{
7296 struct qla_tgt_sess_op *op;
7297
7298 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7299
7300 if (!op) {
7301 /* do not reach for ATIO queue here. This is best effort err
7302 * recovery at this point.
7303 */
Quinn Tran82de8022017-06-13 20:47:17 -07007304 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05007305 return;
7306 }
7307
7308 memcpy(&op->atio, pkt, sizeof(*pkt));
7309 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007310 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07007311 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05007312 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7313 queue_work(qla_tgt_wq, &op->work);
7314 return;
7315}
7316
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007317int
7318qlt_mem_alloc(struct qla_hw_data *ha)
7319{
7320 if (!QLA_TGT_MODE_ENABLED())
7321 return 0;
7322
Kees Cook6396bb22018-06-12 14:03:40 -07007323 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7324 sizeof(struct qla_tgt_vp_map),
7325 GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007326 if (!ha->tgt.tgt_vp_map)
7327 return -ENOMEM;
7328
7329 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7330 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7331 &ha->tgt.atio_dma, GFP_KERNEL);
7332 if (!ha->tgt.atio_ring) {
7333 kfree(ha->tgt.tgt_vp_map);
7334 return -ENOMEM;
7335 }
7336 return 0;
7337}
7338
7339void
7340qlt_mem_free(struct qla_hw_data *ha)
7341{
7342 if (!QLA_TGT_MODE_ENABLED())
7343 return;
7344
7345 if (ha->tgt.atio_ring) {
7346 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7347 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7348 ha->tgt.atio_dma);
7349 }
7350 kfree(ha->tgt.tgt_vp_map);
7351}
7352
7353/* vport_slock to be held by the caller */
7354void
7355qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7356{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007357 void *slot;
7358 u32 key;
7359 int rc;
7360
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007361 if (!QLA_TGT_MODE_ENABLED())
7362 return;
7363
Quinn Tran482c9dc2017-03-15 09:48:54 -07007364 key = vha->d_id.b24;
7365
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007366 switch (cmd) {
7367 case SET_VP_IDX:
7368 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7369 break;
7370 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07007371 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7372 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007373 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007374 "Save vha in host_map %p %06x\n", vha, key);
7375 rc = btree_insert32(&vha->hw->tgt.host_map,
7376 key, vha, GFP_ATOMIC);
7377 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007378 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007379 "Unable to insert s_id into host_map: %06x\n",
7380 key);
7381 return;
7382 }
Quinn Tran83548fe2017-06-02 09:12:01 -07007383 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7384 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007385 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007386 break;
7387 case RESET_VP_IDX:
7388 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7389 break;
7390 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07007391 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007392 "clear vha in host_map %p %06x\n", vha, key);
7393 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7394 if (slot)
7395 btree_remove32(&vha->hw->tgt.host_map, key);
7396 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007397 break;
7398 }
7399}
7400
Quinn Tran482c9dc2017-03-15 09:48:54 -07007401void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7402{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007403
7404 if (!vha->d_id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007405 vha->d_id = id;
7406 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007407 } else if (vha->d_id.b24 != id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007408 qlt_update_vp_map(vha, RESET_AL_PA);
7409 vha->d_id = id;
7410 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007411 }
7412}
7413
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007414static int __init qlt_parse_ini_mode(void)
7415{
7416 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7417 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7418 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7419 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7420 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7421 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007422 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7423 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007424 else
7425 return false;
7426
7427 return true;
7428}
7429
7430int __init qlt_init(void)
7431{
7432 int ret;
7433
7434 if (!qlt_parse_ini_mode()) {
7435 ql_log(ql_log_fatal, NULL, 0xe06b,
7436 "qlt_parse_ini_mode() failed\n");
7437 return -EINVAL;
7438 }
7439
7440 if (!QLA_TGT_MODE_ENABLED())
7441 return 0;
7442
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007443 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7444 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7445 qla_tgt_mgmt_cmd), 0, NULL);
7446 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007447 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007448 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007449 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007450 }
7451
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007452 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007453 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7454 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007455
7456 if (!qla_tgt_plogi_cachep) {
7457 ql_log(ql_log_fatal, NULL, 0xe06d,
7458 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7459 ret = -ENOMEM;
7460 goto out_mgmt_cmd_cachep;
7461 }
7462
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007463 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7464 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7465 if (!qla_tgt_mgmt_cmd_mempool) {
7466 ql_log(ql_log_fatal, NULL, 0xe06e,
7467 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7468 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007469 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007470 }
7471
7472 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7473 if (!qla_tgt_wq) {
7474 ql_log(ql_log_fatal, NULL, 0xe06f,
7475 "alloc_workqueue for qla_tgt_wq failed\n");
7476 ret = -ENOMEM;
7477 goto out_cmd_mempool;
7478 }
7479 /*
7480 * Return 1 to signal that initiator-mode is being disabled
7481 */
7482 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7483
7484out_cmd_mempool:
7485 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007486out_plogi_cachep:
7487 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007488out_mgmt_cmd_cachep:
7489 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007490 return ret;
7491}
7492
7493void qlt_exit(void)
7494{
7495 if (!QLA_TGT_MODE_ENABLED())
7496 return;
7497
7498 destroy_workqueue(qla_tgt_wq);
7499 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007500 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007501 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007502}