blob: 0fea2e2326becbf4993dd7cc216e36dad529d678 [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 Tran09620eeb2017-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 Tran8f6fc8d2017-03-15 09:48:46 -0700144
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400145/*
146 * Global Variables
147 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400148static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
Quinn Tranb5d15312017-08-30 10:16:49 -0700149struct kmem_cache *qla_tgt_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400150static mempool_t *qla_tgt_mgmt_cmd_mempool;
151static struct workqueue_struct *qla_tgt_wq;
152static DEFINE_MUTEX(qla_tgt_mutex);
153static LIST_HEAD(qla_tgt_glist);
154
Quinn Tranbe251522017-03-15 09:48:49 -0700155static const char *prot_op_str(u32 prot_op)
156{
157 switch (prot_op) {
158 case TARGET_PROT_NORMAL: return "NORMAL";
159 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
160 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
161 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
162 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
163 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
164 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
165 default: return "UNKNOWN";
166 }
167}
168
Alexei Potashnikdf673272015-07-14 16:00:46 -0400169/* This API intentionally takes dest as a parameter, rather than returning
170 * int value to avoid caller forgetting to issue wmb() after the store */
171void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
172{
173 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
174 *dest = atomic_inc_return(&base_vha->generation_tick);
175 /* memory barrier */
176 wmb();
177}
178
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400179/* Might release hw lock, then reaquire!! */
180static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
181{
182 /* Send marker if required */
183 if (unlikely(vha->marker_needed != 0)) {
184 int rc = qla2x00_issue_marker(vha, vha_locked);
185 if (rc != QLA_SUCCESS) {
186 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
187 "qla_target(%d): issue_marker() failed\n",
188 vha->vp_idx);
189 }
190 return rc;
191 }
192 return QLA_SUCCESS;
193}
194
195static inline
196struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
197 uint8_t *d_id)
198{
Quinn Tran482c9dc2017-03-15 09:48:54 -0700199 struct scsi_qla_host *host;
200 uint32_t key = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400201
Quinn Tran482c9dc2017-03-15 09:48:54 -0700202 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
203 (vha->d_id.b.al_pa == d_id[2]))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400204 return vha;
205
Quinn Tran482c9dc2017-03-15 09:48:54 -0700206 key = (uint32_t)d_id[0] << 16;
207 key |= (uint32_t)d_id[1] << 8;
208 key |= (uint32_t)d_id[2];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400209
Quinn Tran482c9dc2017-03-15 09:48:54 -0700210 host = btree_lookup32(&vha->hw->tgt.host_map, key);
211 if (!host)
Quinn Tran604e2e52017-12-28 12:33:37 -0800212 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
Quinn Tran83548fe2017-06-02 09:12:01 -0700213 "Unable to find host %06x\n", key);
Quinn Tran482c9dc2017-03-15 09:48:54 -0700214
215 return host;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400216}
217
218static inline
219struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
220 uint16_t vp_idx)
221{
222 struct qla_hw_data *ha = vha->hw;
223
224 if (vha->vp_idx == vp_idx)
225 return vha;
226
227 BUG_ON(ha->tgt.tgt_vp_map == NULL);
228 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
229 return ha->tgt.tgt_vp_map[vp_idx].vha;
230
231 return NULL;
232}
233
Quinn Tran33e79972014-09-25 06:14:55 -0400234static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
235{
236 unsigned long flags;
237
238 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
239
240 vha->hw->tgt.num_pend_cmds++;
Joe Carnucciofc90ada2016-07-06 11:14:23 -0400241 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
242 vha->qla_stats.stat_max_pend_cmds =
Quinn Tran33e79972014-09-25 06:14:55 -0400243 vha->hw->tgt.num_pend_cmds;
244 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
245}
246static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
247{
248 unsigned long flags;
249
250 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
251 vha->hw->tgt.num_pend_cmds--;
252 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
253}
254
Quinn Tran41dc5292017-01-19 22:28:03 -0800255
256static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
Quinn Tran82de8022017-06-13 20:47:17 -0700257 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran41dc5292017-01-19 22:28:03 -0800258{
259 struct qla_tgt_sess_op *u;
260 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
261 unsigned long flags;
262
263 if (tgt->tgt_stop) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700264 ql_dbg(ql_dbg_async, vha, 0x502c,
265 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
266 vha->vp_idx);
Quinn Tran41dc5292017-01-19 22:28:03 -0800267 goto out_term;
268 }
269
270 u = kzalloc(sizeof(*u), GFP_ATOMIC);
Quinn Tran83548fe2017-06-02 09:12:01 -0700271 if (u == NULL)
Quinn Tran41dc5292017-01-19 22:28:03 -0800272 goto out_term;
Quinn Tran41dc5292017-01-19 22:28:03 -0800273
274 u->vha = vha;
275 memcpy(&u->atio, atio, sizeof(*atio));
276 INIT_LIST_HEAD(&u->cmd_list);
277
278 spin_lock_irqsave(&vha->cmd_list_lock, flags);
279 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
280 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
281
282 schedule_delayed_work(&vha->unknown_atio_work, 1);
283
284out:
285 return;
286
287out_term:
Quinn Tran82de8022017-06-13 20:47:17 -0700288 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800289 goto out;
290}
291
292static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
293 uint8_t ha_locked)
294{
295 struct qla_tgt_sess_op *u, *t;
296 scsi_qla_host_t *host;
297 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
298 unsigned long flags;
299 uint8_t queued = 0;
300
301 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
302 if (u->aborted) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700303 ql_dbg(ql_dbg_async, vha, 0x502e,
304 "Freeing unknown %s %p, because of Abort\n",
Quinn Tran41dc5292017-01-19 22:28:03 -0800305 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700306 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
307 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800308 goto abort;
309 }
310
311 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
312 if (host != NULL) {
Quinn Tran604e2e52017-12-28 12:33:37 -0800313 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
Quinn Tran83548fe2017-06-02 09:12:01 -0700314 "Requeuing unknown ATIO_TYPE7 %p\n", u);
Quinn Tran41dc5292017-01-19 22:28:03 -0800315 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
316 } else if (tgt->tgt_stop) {
Quinn Tran604e2e52017-12-28 12:33:37 -0800317 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
Quinn Tran83548fe2017-06-02 09:12:01 -0700318 "Freeing unknown %s %p, because tgt is being stopped\n",
319 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700320 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
321 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800322 } else {
Quinn Tran604e2e52017-12-28 12:33:37 -0800323 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
Quinn Tran83548fe2017-06-02 09:12:01 -0700324 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
Quinn Tran41dc5292017-01-19 22:28:03 -0800325 if (!queued) {
326 queued = 1;
327 schedule_delayed_work(&vha->unknown_atio_work,
328 1);
329 }
330 continue;
331 }
332
333abort:
334 spin_lock_irqsave(&vha->cmd_list_lock, flags);
335 list_del(&u->cmd_list);
336 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
337 kfree(u);
338 }
339}
340
341void qlt_unknown_atio_work_fn(struct work_struct *work)
342{
343 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
344 struct scsi_qla_host, unknown_atio_work);
345
346 qlt_try_to_dequeue_unknown_atios(vha, 0);
347}
348
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500349static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500350 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400351{
Quinn Tranf83adb62014-04-11 16:54:43 -0400352 ql_dbg(ql_dbg_tgt, vha, 0xe072,
353 "%s: qla_target(%d): type %x ox_id %04x\n",
354 __func__, vha->vp_idx, atio->u.raw.entry_type,
355 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
356
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400357 switch (atio->u.raw.entry_type) {
358 case ATIO_TYPE7:
359 {
360 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
361 atio->u.isp24.fcp_hdr.d_id);
362 if (unlikely(NULL == host)) {
363 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
364 "qla_target(%d): Received ATIO_TYPE7 "
365 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
366 atio->u.isp24.fcp_hdr.d_id[0],
367 atio->u.isp24.fcp_hdr.d_id[1],
368 atio->u.isp24.fcp_hdr.d_id[2]);
Quinn Tran41dc5292017-01-19 22:28:03 -0800369
370
371 qlt_queue_unknown_atio(vha, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400372 break;
373 }
Quinn Tran41dc5292017-01-19 22:28:03 -0800374 if (unlikely(!list_empty(&vha->unknown_atio_list)))
375 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
376
Quinn Tran2f424b92015-12-17 14:57:07 -0500377 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400378 break;
379 }
380
381 case IMMED_NOTIFY_TYPE:
382 {
383 struct scsi_qla_host *host = vha;
384 struct imm_ntfy_from_isp *entry =
385 (struct imm_ntfy_from_isp *)atio;
386
Quinn Tran82de8022017-06-13 20:47:17 -0700387 qlt_issue_marker(vha, ha_locked);
388
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400389 if ((entry->u.isp24.vp_index != 0xFF) &&
390 (entry->u.isp24.nport_handle != 0xFFFF)) {
391 host = qlt_find_host_by_vp_idx(vha,
392 entry->u.isp24.vp_index);
393 if (unlikely(!host)) {
394 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
395 "qla_target(%d): Received "
396 "ATIO (IMMED_NOTIFY_TYPE) "
397 "with unknown vp_index %d\n",
398 vha->vp_idx, entry->u.isp24.vp_index);
399 break;
400 }
401 }
Quinn Tran2f424b92015-12-17 14:57:07 -0500402 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400403 break;
404 }
405
Quinn Tran41dc5292017-01-19 22:28:03 -0800406 case VP_RPT_ID_IOCB_TYPE:
407 qla24xx_report_id_acquisition(vha,
408 (struct vp_rpt_id_entry_24xx *)atio);
409 break;
410
411 case ABTS_RECV_24XX:
412 {
413 struct abts_recv_from_24xx *entry =
414 (struct abts_recv_from_24xx *)atio;
415 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
416 entry->vp_index);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700417 unsigned long flags;
418
Quinn Tran41dc5292017-01-19 22:28:03 -0800419 if (unlikely(!host)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700420 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
Quinn Tran41dc5292017-01-19 22:28:03 -0800421 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
422 "received, with unknown vp_index %d\n",
423 vha->vp_idx, entry->vp_index);
424 break;
425 }
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700426 if (!ha_locked)
427 spin_lock_irqsave(&host->hw->hardware_lock, flags);
428 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
429 if (!ha_locked)
430 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -0800431 break;
Quinn Tran41dc5292017-01-19 22:28:03 -0800432 }
433
434 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
435
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400436 default:
437 ql_dbg(ql_dbg_tgt, vha, 0xe040,
438 "qla_target(%d): Received unknown ATIO atio "
439 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
440 break;
441 }
442
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500443 return false;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400444}
445
Quinn Tran82de8022017-06-13 20:47:17 -0700446void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
447 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400448{
449 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -0400450 case CTIO_CRC2:
451 ql_dbg(ql_dbg_tgt, vha, 0xe073,
452 "qla_target(%d):%s: CRC2 Response pkt\n",
453 vha->vp_idx, __func__);
Bart Van Assche81881862017-12-07 16:02:46 -0800454 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400455 case CTIO_TYPE7:
456 {
457 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
458 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
459 entry->vp_index);
460 if (unlikely(!host)) {
461 ql_dbg(ql_dbg_tgt, vha, 0xe041,
462 "qla_target(%d): Response pkt (CTIO_TYPE7) "
463 "received, with unknown vp_index %d\n",
464 vha->vp_idx, entry->vp_index);
465 break;
466 }
Quinn Tran82de8022017-06-13 20:47:17 -0700467 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400468 break;
469 }
470
471 case IMMED_NOTIFY_TYPE:
472 {
473 struct scsi_qla_host *host = vha;
474 struct imm_ntfy_from_isp *entry =
475 (struct imm_ntfy_from_isp *)pkt;
476
477 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
478 if (unlikely(!host)) {
479 ql_dbg(ql_dbg_tgt, vha, 0xe042,
480 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
481 "received, with unknown vp_index %d\n",
482 vha->vp_idx, entry->u.isp24.vp_index);
483 break;
484 }
Quinn Tran82de8022017-06-13 20:47:17 -0700485 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400486 break;
487 }
488
489 case NOTIFY_ACK_TYPE:
490 {
491 struct scsi_qla_host *host = vha;
492 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
493
494 if (0xFF != entry->u.isp24.vp_index) {
495 host = qlt_find_host_by_vp_idx(vha,
496 entry->u.isp24.vp_index);
497 if (unlikely(!host)) {
498 ql_dbg(ql_dbg_tgt, vha, 0xe043,
499 "qla_target(%d): Response "
500 "pkt (NOTIFY_ACK_TYPE) "
501 "received, with unknown "
502 "vp_index %d\n", vha->vp_idx,
503 entry->u.isp24.vp_index);
504 break;
505 }
506 }
Quinn Tran82de8022017-06-13 20:47:17 -0700507 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400508 break;
509 }
510
511 case ABTS_RECV_24XX:
512 {
513 struct abts_recv_from_24xx *entry =
514 (struct abts_recv_from_24xx *)pkt;
515 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
516 entry->vp_index);
517 if (unlikely(!host)) {
518 ql_dbg(ql_dbg_tgt, vha, 0xe044,
519 "qla_target(%d): Response pkt "
520 "(ABTS_RECV_24XX) received, with unknown "
521 "vp_index %d\n", vha->vp_idx, entry->vp_index);
522 break;
523 }
Quinn Tran82de8022017-06-13 20:47:17 -0700524 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400525 break;
526 }
527
528 case ABTS_RESP_24XX:
529 {
530 struct abts_resp_to_24xx *entry =
531 (struct abts_resp_to_24xx *)pkt;
532 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
533 entry->vp_index);
534 if (unlikely(!host)) {
535 ql_dbg(ql_dbg_tgt, vha, 0xe045,
536 "qla_target(%d): Response pkt "
537 "(ABTS_RECV_24XX) received, with unknown "
538 "vp_index %d\n", vha->vp_idx, entry->vp_index);
539 break;
540 }
Quinn Tran82de8022017-06-13 20:47:17 -0700541 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400542 break;
543 }
544
545 default:
Quinn Tran82de8022017-06-13 20:47:17 -0700546 qlt_response_pkt(vha, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400547 break;
548 }
549
550}
551
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500552/*
553 * All qlt_plogi_ack_t operations are protected by hardware_lock
554 */
Quinn Tran726b8542017-01-19 22:28:00 -0800555static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
556 struct imm_ntfy_from_isp *ntfy, int type)
557{
558 struct qla_work_evt *e;
559 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
560 if (!e)
561 return QLA_FUNCTION_FAILED;
562
563 e->u.nack.fcport = fcport;
564 e->u.nack.type = type;
565 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
566 return qla2x00_post_work(vha, e);
567}
568
569static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800570void qla2x00_async_nack_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800571{
Quinn Tran726b8542017-01-19 22:28:00 -0800572 struct srb *sp = (struct srb *)s;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800573 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800574 unsigned long flags;
575
Quinn Tran83548fe2017-06-02 09:12:01 -0700576 ql_dbg(ql_dbg_disc, vha, 0x20f2,
577 "Async done-%s res %x %8phC type %d\n",
578 sp->name, res, sp->fcport->port_name, sp->type);
Quinn Tran726b8542017-01-19 22:28:00 -0800579
580 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
581 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -0700582 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran726b8542017-01-19 22:28:00 -0800583
584 switch (sp->type) {
585 case SRB_NACK_PLOGI:
586 sp->fcport->login_gen++;
587 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
588 sp->fcport->logout_on_delete = 1;
Quinn Tran5b334692017-03-15 09:48:48 -0700589 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
Quinn Tran35158322017-08-30 10:16:50 -0700590 sp->fcport->send_els_logo = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800591 break;
592
593 case SRB_NACK_PRLI:
594 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
595 sp->fcport->deleted = 0;
Quinn Tran35158322017-08-30 10:16:50 -0700596 sp->fcport->send_els_logo = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800597
598 if (!sp->fcport->login_succ &&
599 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
600 sp->fcport->login_succ = 1;
601
602 vha->fcport_count++;
603
Quinn Tran29528492018-05-01 09:01:45 -0700604 ql_dbg(ql_dbg_disc, vha, 0x20f3,
605 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
606 __func__, __LINE__,
607 sp->fcport->port_name,
608 vha->fcport_count);
609 sp->fcport->disc_state = DSC_UPD_FCPORT;
610 qla24xx_post_upd_fcport_work(vha, sp->fcport);
611 } else {
612 sp->fcport->login_retry = 0;
613 sp->fcport->disc_state = DSC_LOGIN_COMPLETE;
614 sp->fcport->deleted = 0;
615 sp->fcport->logout_on_delete = 1;
Quinn Tran726b8542017-01-19 22:28:00 -0800616 }
617 break;
618
619 case SRB_NACK_LOGO:
620 sp->fcport->login_gen++;
621 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
622 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
623 break;
624 }
625 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
626
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800627 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800628}
629
630int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
631 struct imm_ntfy_from_isp *ntfy, int type)
632{
633 int rval = QLA_FUNCTION_FAILED;
634 srb_t *sp;
635 char *c = NULL;
636
637 fcport->flags |= FCF_ASYNC_SENT;
638 switch (type) {
639 case SRB_NACK_PLOGI:
640 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
641 c = "PLOGI";
642 break;
643 case SRB_NACK_PRLI:
644 fcport->fw_login_state = DSC_LS_PRLI_PEND;
Quinn Tranec7193e2017-03-15 09:48:55 -0700645 fcport->deleted = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800646 c = "PRLI";
647 break;
648 case SRB_NACK_LOGO:
649 fcport->fw_login_state = DSC_LS_LOGO_PEND;
650 c = "LOGO";
651 break;
652 }
653
654 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
655 if (!sp)
656 goto done;
657
658 sp->type = type;
659 sp->name = "nack";
660
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000661 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
Quinn Tran726b8542017-01-19 22:28:00 -0800662 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
663
664 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
Quinn Tran726b8542017-01-19 22:28:00 -0800665 sp->done = qla2x00_async_nack_sp_done;
666
667 rval = qla2x00_start_sp(sp);
668 if (rval != QLA_SUCCESS)
669 goto done_free_sp;
670
Quinn Tran83548fe2017-06-02 09:12:01 -0700671 ql_dbg(ql_dbg_disc, vha, 0x20f4,
672 "Async-%s %8phC hndl %x %s\n",
673 sp->name, fcport->port_name, sp->handle, c);
Quinn Tran726b8542017-01-19 22:28:00 -0800674
675 return rval;
676
677done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800678 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800679done:
680 fcport->flags &= ~FCF_ASYNC_SENT;
681 return rval;
682}
683
684void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
685{
686 fc_port_t *t;
687 unsigned long flags;
688
689 switch (e->u.nack.type) {
690 case SRB_NACK_PRLI:
691 mutex_lock(&vha->vha_tgt.tgt_mutex);
692 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
693 mutex_unlock(&vha->vha_tgt.tgt_mutex);
694 if (t) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700695 ql_log(ql_log_info, vha, 0xd034,
Quinn Tran726b8542017-01-19 22:28:00 -0800696 "%s create sess success %p", __func__, t);
697 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
698 /* create sess has an extra kref */
699 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
700 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
701 }
702 break;
703 }
704 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
705 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
706}
707
708void qla24xx_delete_sess_fn(struct work_struct *work)
709{
710 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
711 struct qla_hw_data *ha = fcport->vha->hw;
712 unsigned long flags;
713
714 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
715
716 if (fcport->se_sess) {
717 ha->tgt.tgt_ops->shutdown_sess(fcport);
718 ha->tgt.tgt_ops->put_sess(fcport);
719 } else {
720 qlt_unreg_sess(fcport);
721 }
722 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
723}
724
725/*
726 * Called from qla2x00_reg_remote_port()
727 */
728void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
729{
730 struct qla_hw_data *ha = vha->hw;
731 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
732 struct fc_port *sess = fcport;
733 unsigned long flags;
734
735 if (!vha->hw->tgt.tgt_ops)
736 return;
737
738 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
739 if (tgt->tgt_stop) {
740 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
741 return;
742 }
743
744 if (fcport->disc_state == DSC_DELETE_PEND) {
745 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
746 return;
747 }
748
749 if (!sess->se_sess) {
750 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
751
752 mutex_lock(&vha->vha_tgt.tgt_mutex);
753 sess = qlt_create_sess(vha, fcport, false);
754 mutex_unlock(&vha->vha_tgt.tgt_mutex);
755
756 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
757 } else {
758 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
759 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
760 return;
761 }
762
763 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700764 ql_dbg(ql_dbg_disc, vha, 0x2107,
Quinn Tran726b8542017-01-19 22:28:00 -0800765 "%s: kref_get fail sess %8phC \n",
766 __func__, sess->port_name);
767 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
768 return;
769 }
770
771 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
772 "qla_target(%u): %ssession for port %8phC "
773 "(loop ID %d) reappeared\n", vha->vp_idx,
774 sess->local ? "local " : "", sess->port_name, sess->loop_id);
775
776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
777 "Reappeared sess %p\n", sess);
778
779 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
780 fcport->loop_id,
781 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
782 }
783
784 if (sess && sess->local) {
785 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
786 "qla_target(%u): local session for "
787 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
788 fcport->port_name, sess->loop_id);
789 sess->local = 0;
790 }
791 ha->tgt.tgt_ops->put_sess(sess);
792 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
793}
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500794
795/*
796 * This is a zero-base ref-counting solution, since hardware_lock
797 * guarantees that ref_count is not modified concurrently.
798 * Upon successful return content of iocb is undefined
799 */
Quinn Tran5d964832017-01-19 22:27:59 -0800800static struct qlt_plogi_ack_t *
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500801qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
802 struct imm_ntfy_from_isp *iocb)
803{
Quinn Tran5d964832017-01-19 22:27:59 -0800804 struct qlt_plogi_ack_t *pla;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500805
806 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
807 if (pla->id.b24 == id->b24) {
808 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
Quinn Tran5d964832017-01-19 22:27:59 -0800809 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500810 return pla;
811 }
812 }
813
814 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
815 if (!pla) {
816 ql_dbg(ql_dbg_async, vha, 0x5088,
817 "qla_target(%d): Allocation of plogi_ack failed\n",
818 vha->vp_idx);
819 return NULL;
820 }
821
Quinn Tran5d964832017-01-19 22:27:59 -0800822 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500823 pla->id = *id;
824 list_add_tail(&pla->list, &vha->plogi_ack_list);
825
826 return pla;
827}
828
Quinn Tran726b8542017-01-19 22:28:00 -0800829void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -0800830 struct qlt_plogi_ack_t *pla)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500831{
Quinn Tran5d964832017-01-19 22:27:59 -0800832 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Quinn Tran726b8542017-01-19 22:28:00 -0800833 port_id_t port_id;
834 uint16_t loop_id;
835 fc_port_t *fcport = pla->fcport;
836
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500837 BUG_ON(!pla->ref_count);
838 pla->ref_count--;
839
840 if (pla->ref_count)
841 return;
842
Quinn Tran726b8542017-01-19 22:28:00 -0800843 ql_dbg(ql_dbg_disc, vha, 0x5089,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500844 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
Quinn Tran5d964832017-01-19 22:27:59 -0800845 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
846 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
847 iocb->u.isp24.port_id[0],
848 le16_to_cpu(iocb->u.isp24.nport_handle),
849 iocb->u.isp24.exchange_address, iocb->ox_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800850
851 port_id.b.domain = iocb->u.isp24.port_id[2];
852 port_id.b.area = iocb->u.isp24.port_id[1];
853 port_id.b.al_pa = iocb->u.isp24.port_id[0];
854 port_id.b.rsvd_1 = 0;
855
856 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
857
858 fcport->loop_id = loop_id;
859 fcport->d_id = port_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800860 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
861 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
862 else
863 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
Quinn Tran726b8542017-01-19 22:28:00 -0800864
865 list_for_each_entry(fcport, &vha->vp_fcports, list) {
866 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
867 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
868 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
869 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
870 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500871
872 list_del(&pla->list);
873 kmem_cache_free(qla_tgt_plogi_cachep, pla);
874}
875
Quinn Tran726b8542017-01-19 22:28:00 -0800876void
Quinn Tran5d964832017-01-19 22:27:59 -0800877qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
878 struct fc_port *sess, enum qlt_plogi_link_t link)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500879{
Quinn Tran5d964832017-01-19 22:27:59 -0800880 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500881 /* Inc ref_count first because link might already be pointing at pla */
882 pla->ref_count++;
883
Quinn Tran726b8542017-01-19 22:28:00 -0800884 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
885 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
886 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
887 sess, link, sess->port_name,
888 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
889 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
890 pla->ref_count, pla, link);
891
Quinn Tran5ef696a2017-12-04 14:45:05 -0800892 if (link == QLT_PLOGI_LINK_CONFLICT) {
893 switch (sess->disc_state) {
894 case DSC_DELETED:
895 case DSC_DELETE_PEND:
896 pla->ref_count--;
897 return;
898 default:
899 break;
900 }
901 }
902
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500903 if (sess->plogi_link[link])
904 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
905
Quinn Tran726b8542017-01-19 22:28:00 -0800906 if (link == QLT_PLOGI_LINK_SAME_WWN)
907 pla->fcport = sess;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500908
909 sess->plogi_link[link] = pla;
910}
911
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500912typedef struct {
913 /* These fields must be initialized by the caller */
914 port_id_t id;
915 /*
916 * number of cmds dropped while we were waiting for
917 * initiator to ack LOGO initialize to 1 if LOGO is
918 * triggered by a command, otherwise, to 0
919 */
920 int cmd_count;
921
922 /* These fields are used by callee */
923 struct list_head list;
924} qlt_port_logo_t;
925
926static void
927qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
928{
929 qlt_port_logo_t *tmp;
930 int res;
931
932 mutex_lock(&vha->vha_tgt.tgt_mutex);
933
934 list_for_each_entry(tmp, &vha->logo_list, list) {
935 if (tmp->id.b24 == logo->id.b24) {
936 tmp->cmd_count += logo->cmd_count;
937 mutex_unlock(&vha->vha_tgt.tgt_mutex);
938 return;
939 }
940 }
941
942 list_add_tail(&logo->list, &vha->logo_list);
943
944 mutex_unlock(&vha->vha_tgt.tgt_mutex);
945
946 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
947
948 mutex_lock(&vha->vha_tgt.tgt_mutex);
949 list_del(&logo->list);
950 mutex_unlock(&vha->vha_tgt.tgt_mutex);
951
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500952 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
953 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
954 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
955 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500956}
957
Darren Trapp9dd96862018-03-20 23:09:32 -0700958void qlt_free_session_done(struct work_struct *work)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400959{
Quinn Tran5d964832017-01-19 22:27:59 -0800960 struct fc_port *sess = container_of(work, struct fc_port,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400961 free_work);
962 struct qla_tgt *tgt = sess->tgt;
963 struct scsi_qla_host *vha = sess->vha;
964 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400965 unsigned long flags;
966 bool logout_started = false;
Quinn Tran41dc5292017-01-19 22:28:03 -0800967 scsi_qla_host_t *base_vha;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800968 struct qlt_plogi_ack_t *own =
969 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400970
971 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
972 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500973 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400974 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
Quinn Tran37cacc02017-01-19 22:27:58 -0800975 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400976 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500977 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400978
Quinn Tran726b8542017-01-19 22:28:00 -0800979 if (!IS_SW_RESV_ADDR(sess->d_id)) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800980 if (sess->send_els_logo) {
981 qlt_port_logo_t logo;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500982
Quinn Tran41dc5292017-01-19 22:28:03 -0800983 logo.id = sess->d_id;
984 logo.cmd_count = 0;
Himanshu Madhania2390342018-01-22 12:04:20 -0800985 sess->send_els_logo = 0;
Quinn Tran41dc5292017-01-19 22:28:03 -0800986 qlt_send_first_logo(vha, &logo);
987 }
988
Quinn Tranba743f92017-12-04 14:45:12 -0800989 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800990 int rc;
991
Quinn Tran9cd883f2017-12-28 12:33:24 -0800992 if (!own ||
993 (own &&
994 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
995 rc = qla2x00_post_async_logout_work(vha, sess,
996 NULL);
997 if (rc != QLA_SUCCESS)
998 ql_log(ql_log_warn, vha, 0xf085,
999 "Schedule logo failed sess %p rc %d\n",
1000 sess, rc);
1001 else
1002 logout_started = true;
1003 } else if (own && (own->iocb.u.isp24.status_subcode ==
1004 ELS_PRLI) && ha->flags.rida_fmt2) {
1005 rc = qla2x00_post_async_prlo_work(vha, sess,
1006 NULL);
1007 if (rc != QLA_SUCCESS)
1008 ql_log(ql_log_warn, vha, 0xf085,
1009 "Schedule PRLO failed sess %p rc %d\n",
1010 sess, rc);
1011 else
1012 logout_started = true;
1013 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001014 }
Quinn Tran726b8542017-01-19 22:28:00 -08001015 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001016
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001017 /*
1018 * Release the target session for FC Nexus from fabric module code.
1019 */
1020 if (sess->se_sess != NULL)
1021 ha->tgt.tgt_ops->free_session(sess);
1022
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001023 if (logout_started) {
1024 bool traced = false;
1025
Mark Rutland6aa7de02017-10-23 14:07:29 -07001026 while (!READ_ONCE(sess->logout_completed)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001027 if (!traced) {
1028 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1029 "%s: waiting for sess %p logout\n",
1030 __func__, sess);
1031 traced = true;
1032 }
1033 msleep(100);
1034 }
1035
Quinn Tran726b8542017-01-19 22:28:00 -08001036 ql_dbg(ql_dbg_disc, vha, 0xf087,
Quinn Tran9cd883f2017-12-28 12:33:24 -08001037 "%s: sess %p logout completed\n", __func__, sess);
Quinn Tran41dc5292017-01-19 22:28:03 -08001038 }
1039
1040 if (sess->logo_ack_needed) {
1041 sess->logo_ack_needed = 0;
1042 qla24xx_async_notify_ack(vha, sess,
1043 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001044 }
1045
Quinn Tran726b8542017-01-19 22:28:00 -08001046 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1047 if (sess->se_sess) {
1048 sess->se_sess = NULL;
1049 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1050 tgt->sess_count--;
1051 }
1052
1053 sess->disc_state = DSC_DELETED;
1054 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1055 sess->deleted = QLA_SESS_DELETED;
1056 sess->login_retry = vha->hw->login_retry_count;
1057
1058 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1059 vha->fcport_count--;
1060 sess->login_succ = 0;
1061 }
1062
Quinn Tranba743f92017-12-04 14:45:12 -08001063 qla2x00_clear_loop_id(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08001064
1065 if (sess->conflict) {
1066 sess->conflict->login_pause = 0;
1067 sess->conflict = NULL;
1068 if (!test_bit(UNLOADING, &vha->dpc_flags))
1069 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1070 }
1071
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001072 {
Quinn Tran5d964832017-01-19 22:27:59 -08001073 struct qlt_plogi_ack_t *con =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001074 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
Quinn Tran5d964832017-01-19 22:27:59 -08001075 struct imm_ntfy_from_isp *iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001076
1077 if (con) {
Quinn Tran5d964832017-01-19 22:27:59 -08001078 iocb = &con->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001079 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
Quinn Tran5d964832017-01-19 22:27:59 -08001080 "se_sess %p / sess %p port %8phC is gone,"
1081 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1082 sess->se_sess, sess, sess->port_name,
1083 own ? "releasing own PLOGI" : "no own PLOGI pending",
1084 own ? own->ref_count : -1,
1085 iocb->u.isp24.port_name, con->ref_count);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001086 qlt_plogi_ack_unref(vha, con);
Quinn Tran726b8542017-01-19 22:28:00 -08001087 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001088 } else {
1089 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1090 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1091 sess->se_sess, sess, sess->port_name,
1092 own ? "releasing own PLOGI" :
1093 "no own PLOGI pending",
1094 own ? own->ref_count : -1);
1095 }
1096
Quinn Tran726b8542017-01-19 22:28:00 -08001097 if (own) {
1098 sess->fw_login_state = DSC_LS_PLOGI_PEND;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001099 qlt_plogi_ack_unref(vha, own);
Quinn Tran726b8542017-01-19 22:28:00 -08001100 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1101 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001102 }
Quinn Tran1ae634e2017-12-28 12:33:44 -08001103
Quinn Tran5d964832017-01-19 22:27:59 -08001104 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1105
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
Quinn Tran726b8542017-01-19 22:28:00 -08001107 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1108 sess, sess->port_name, vha->fcport_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001109
Quinn Tran726b8542017-01-19 22:28:00 -08001110 if (tgt && (tgt->sess_count == 0))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001111 wake_up_all(&tgt->waitQ);
Quinn Tran726b8542017-01-19 22:28:00 -08001112
1113 if (vha->fcport_count == 0)
1114 wake_up_all(&vha->fcport_waitQ);
1115
Quinn Tran41dc5292017-01-19 22:28:03 -08001116 base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran1ae634e2017-12-28 12:33:44 -08001117
1118 sess->free_pending = 0;
1119
Quinn Tran41dc5292017-01-19 22:28:03 -08001120 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1121 return;
1122
Quinn Trana4239942017-12-28 12:33:26 -08001123 if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1124 switch (vha->host->active_mode) {
1125 case MODE_INITIATOR:
1126 case MODE_DUAL:
1127 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1128 qla2xxx_wake_dpc(vha);
1129 break;
1130 case MODE_TARGET:
1131 default:
1132 /* no-op */
1133 break;
1134 }
Quinn Tran726b8542017-01-19 22:28:00 -08001135 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001136}
1137
Quinn Tran75601512015-12-17 14:57:04 -05001138/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08001139void qlt_unreg_sess(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001140{
1141 struct scsi_qla_host *vha = sess->vha;
Quinn Tran1ae634e2017-12-28 12:33:44 -08001142 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001143
Quinn Tran83548fe2017-06-02 09:12:01 -07001144 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
Quinn Tran5d964832017-01-19 22:27:59 -08001145 "%s sess %p for deletion %8phC\n",
1146 __func__, sess, sess->port_name);
1147
Quinn Tran1ae634e2017-12-28 12:33:44 -08001148 spin_lock_irqsave(&sess->vha->work_lock, flags);
1149 if (sess->free_pending) {
1150 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1151 return;
1152 }
1153 sess->free_pending = 1;
1154 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1155
Quinn Tran36c78452016-02-04 11:45:18 -05001156 if (sess->se_sess)
1157 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001158
Quinn Tran5d964832017-01-19 22:27:59 -08001159 qla2x00_mark_device_lost(vha, sess, 1, 1);
1160
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001161 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Tran726b8542017-01-19 22:28:00 -08001162 sess->disc_state = DSC_DELETE_PEND;
1163 sess->last_rscn_gen = sess->rscn_gen;
1164 sess->last_login_gen = sess->login_gen;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001165
Darren Trapp9dd96862018-03-20 23:09:32 -07001166 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1167 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1168 sess->nvme_flag |= NVME_FLAG_DELETING;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001169 schedule_work(&sess->nvme_del_work);
Darren Trapp9dd96862018-03-20 23:09:32 -07001170 } else {
1171 INIT_WORK(&sess->free_work, qlt_free_session_done);
1172 schedule_work(&sess->free_work);
1173 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001174}
Quinn Tran5d964832017-01-19 22:27:59 -08001175EXPORT_SYMBOL(qlt_unreg_sess);
Quinn Tran75601512015-12-17 14:57:04 -05001176
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001177static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1178{
1179 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001180 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001181 uint16_t loop_id;
1182 int res = 0;
1183 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -05001184 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001185
1186 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1187 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001188 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -04001189 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -05001190 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -04001191 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001192 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001193 } else {
Quinn Tran75601512015-12-17 14:57:04 -05001194 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001195 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05001196 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001197 }
1198
1199 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1200 "Using sess for qla_tgt_reset: %p\n", sess);
1201 if (!sess) {
1202 res = -ESRCH;
1203 return res;
1204 }
1205
1206 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001207 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1208 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001209 mcmd, loop_id);
1210
Quinn Tranbb1181c2016-12-23 18:06:05 -08001211 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001212}
1213
Quinn Tran726b8542017-01-19 22:28:00 -08001214static void qla24xx_chk_fcp_state(struct fc_port *sess)
1215{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001216 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
Quinn Tran726b8542017-01-19 22:28:00 -08001217 sess->logout_on_delete = 0;
1218 sess->logo_ack_needed = 0;
1219 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1220 sess->scan_state = 0;
1221 }
1222}
1223
Quinn Tran94cff6e2017-12-28 12:33:42 -08001224void qlt_schedule_sess_for_deletion(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001225{
1226 struct qla_tgt *tgt = sess->tgt;
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001227 struct qla_hw_data *ha = sess->vha->hw;
Quinn Trand8630bb2017-12-28 12:33:43 -08001228 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001229
Quinn Tran726b8542017-01-19 22:28:00 -08001230 if (sess->disc_state == DSC_DELETE_PEND)
1231 return;
1232
1233 if (sess->disc_state == DSC_DELETED) {
1234 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1235 wake_up_all(&tgt->waitQ);
1236 if (sess->vha->fcport_count == 0)
1237 wake_up_all(&sess->vha->fcport_waitQ);
1238
1239 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1240 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001241 return;
1242 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001243
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001244 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001245 if (sess->deleted == QLA_SESS_DELETED)
1246 sess->logout_on_delete = 0;
1247
Quinn Trand8630bb2017-12-28 12:33:43 -08001248 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001249 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001250 return;
1251 }
Quinn Tran726b8542017-01-19 22:28:00 -08001252 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001253 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08001254
1255 sess->disc_state = DSC_DELETE_PEND;
1256
Quinn Tran726b8542017-01-19 22:28:00 -08001257 qla24xx_chk_fcp_state(sess);
1258
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001259 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1260 "Scheduling sess %p for deletion\n", sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001261
Quinn Trana01c77d2017-12-04 14:44:58 -08001262 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001263 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
Quinn Tran726b8542017-01-19 22:28:00 -08001264}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001265
Joern Engelc5701042014-09-16 16:23:14 -04001266static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001267{
Quinn Tran5d964832017-01-19 22:27:59 -08001268 struct fc_port *sess;
1269 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001270
Quinn Tran5d964832017-01-19 22:27:59 -08001271 list_for_each_entry(sess, &vha->vp_fcports, list) {
1272 if (sess->se_sess)
Quinn Tran94cff6e2017-12-28 12:33:42 -08001273 qlt_schedule_sess_for_deletion(sess);
Quinn Tran5d964832017-01-19 22:27:59 -08001274 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001275
1276 /* At this point tgt could be already dead */
1277}
1278
1279static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1280 uint16_t *loop_id)
1281{
1282 struct qla_hw_data *ha = vha->hw;
1283 dma_addr_t gid_list_dma;
1284 struct gid_list_info *gid_list;
1285 char *id_iter;
1286 int res, rc, i;
1287 uint16_t entries;
1288
1289 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1290 &gid_list_dma, GFP_KERNEL);
1291 if (!gid_list) {
1292 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1293 "qla_target(%d): DMA Alloc failed of %u\n",
1294 vha->vp_idx, qla2x00_gid_list_size(ha));
1295 return -ENOMEM;
1296 }
1297
1298 /* Get list of logged in devices */
Quinn Tran15f30a52017-03-15 09:48:52 -07001299 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001300 if (rc != QLA_SUCCESS) {
1301 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1302 "qla_target(%d): get_id_list() failed: %x\n",
1303 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001304 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001305 goto out_free_id_list;
1306 }
1307
1308 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001309 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001310 for (i = 0; i < entries; i++) {
1311 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1312 if ((gid->al_pa == s_id[2]) &&
1313 (gid->area == s_id[1]) &&
1314 (gid->domain == s_id[0])) {
1315 *loop_id = le16_to_cpu(gid->loop_id);
1316 res = 0;
1317 break;
1318 }
1319 id_iter += ha->gid_list_info_size;
1320 }
1321
1322out_free_id_list:
1323 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1324 gid_list, gid_list_dma);
1325 return res;
1326}
1327
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001328/*
1329 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1330 * Caller must put it.
1331 */
Quinn Tran5d964832017-01-19 22:27:59 -08001332static struct fc_port *qlt_create_sess(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001333 struct scsi_qla_host *vha,
1334 fc_port_t *fcport,
1335 bool local)
1336{
1337 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -08001338 struct fc_port *sess = fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001339 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001340
Quinn Tran726b8542017-01-19 22:28:00 -08001341 if (vha->vha_tgt.qla_tgt->tgt_stop)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001342 return NULL;
Quinn Tran726b8542017-01-19 22:28:00 -08001343
1344 if (fcport->se_sess) {
1345 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001346 ql_dbg(ql_dbg_disc, vha, 0x20f6,
Quinn Tran726b8542017-01-19 22:28:00 -08001347 "%s: kref_get_unless_zero failed for %8phC\n",
1348 __func__, sess->port_name);
1349 return NULL;
1350 }
1351 return fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001352 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001353 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001354 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001355
Quinn Tran726b8542017-01-19 22:28:00 -08001356 /*
1357 * Under normal circumstances we want to logout from firmware when
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001358 * session eventually ends and release corresponding nport handle.
1359 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
Quinn Tran726b8542017-01-19 22:28:00 -08001360 * code will adjust these flags as necessary.
1361 */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001362 sess->logout_on_delete = 1;
1363 sess->keep_nport_handle = 0;
Quinn Tran726b8542017-01-19 22:28:00 -08001364 sess->logout_completed = 0;
1365
1366 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1367 &fcport->port_name[0], sess) < 0) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001368 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
Quinn Tran726b8542017-01-19 22:28:00 -08001369 "(%d) %8phC check_initiator_node_acl failed\n",
1370 vha->vp_idx, fcport->port_name);
1371 return NULL;
1372 } else {
1373 kref_init(&fcport->sess_kref);
1374 /*
1375 * Take an extra reference to ->sess_kref here to handle
1376 * fc_port access across ->tgt.sess_lock reaquire.
1377 */
1378 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001379 ql_dbg(ql_dbg_disc, vha, 0x20f7,
Quinn Tran726b8542017-01-19 22:28:00 -08001380 "%s: kref_get_unless_zero failed for %8phC\n",
1381 __func__, sess->port_name);
1382 return NULL;
1383 }
1384
1385 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1386 if (!IS_SW_RESV_ADDR(sess->d_id))
1387 vha->vha_tgt.qla_tgt->sess_count++;
1388
1389 qlt_do_generation_tick(vha, &sess->generation);
1390 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1391 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001392
1393 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
Quinn Tran726b8542017-01-19 22:28:00 -08001394 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1395 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1396 vha->vha_tgt.qla_tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001397
1398 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001399 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1400 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1401 vha->vp_idx, local ? "local " : "", fcport->port_name,
Quinn Tran37cacc02017-01-19 22:27:58 -08001402 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1403 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001404
1405 return sess;
1406}
1407
1408/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001409 * max_gen - specifies maximum session generation
1410 * at which this deletion requestion is still valid
1411 */
1412void
1413qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001414{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001415 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08001416 struct fc_port *sess = fcport;
Quinn Tran75601512015-12-17 14:57:04 -05001417 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001418
1419 if (!vha->hw->tgt.tgt_ops)
1420 return;
1421
Roland Dreierb2032fd2015-07-14 16:00:42 -04001422 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001423 return;
1424
Quinn Tran75601512015-12-17 14:57:04 -05001425 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001426 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001427 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001428 return;
1429 }
Quinn Tran5d964832017-01-19 22:27:59 -08001430 if (!sess->se_sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001431 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001432 return;
1433 }
1434
Alexei Potashnikdf673272015-07-14 16:00:46 -04001435 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001436 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001437 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1438 "Ignoring stale deletion request for se_sess %p / sess %p"
1439 " for port %8phC, req_gen %d, sess_gen %d\n",
1440 sess->se_sess, sess, sess->port_name, max_gen,
1441 sess->generation);
1442 return;
1443 }
1444
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001445 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1446
1447 sess->local = 1;
Quinn Tran75601512015-12-17 14:57:04 -05001448 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +01001449 qlt_schedule_sess_for_deletion(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001450}
1451
1452static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1453{
1454 struct qla_hw_data *ha = tgt->ha;
1455 unsigned long flags;
1456 int res;
1457 /*
1458 * We need to protect against race, when tgt is freed before or
1459 * inside wake_up()
1460 */
Quinn Tran726b8542017-01-19 22:28:00 -08001461 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001462 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
Quinn Tran5d964832017-01-19 22:27:59 -08001463 "tgt %p, sess_count=%d\n",
1464 tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001465 res = (tgt->sess_count == 0);
Quinn Tran726b8542017-01-19 22:28:00 -08001466 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001467
1468 return res;
1469}
1470
1471/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001472int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001473{
1474 struct scsi_qla_host *vha = tgt->vha;
1475 struct qla_hw_data *ha = tgt->ha;
1476 unsigned long flags;
1477
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001478 mutex_lock(&qla_tgt_mutex);
1479 if (!vha->fc_vport) {
1480 struct Scsi_Host *sh = vha->host;
1481 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1482 bool npiv_vports;
1483
1484 spin_lock_irqsave(sh->host_lock, flags);
1485 npiv_vports = (fc_host->npiv_vports_inuse);
1486 spin_unlock_irqrestore(sh->host_lock, flags);
1487
1488 if (npiv_vports) {
1489 mutex_unlock(&qla_tgt_mutex);
Quinn Tran3a33dc92017-06-02 09:12:04 -07001490 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1491 "NPIV is in use. Can not stop target\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001492 return -EPERM;
1493 }
1494 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001495 if (tgt->tgt_stop || tgt->tgt_stopped) {
1496 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1497 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001498 mutex_unlock(&qla_tgt_mutex);
1499 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001500 }
1501
Quinn Tran3a33dc92017-06-02 09:12:04 -07001502 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001503 vha->host_no, vha);
1504 /*
1505 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1506 * Lock is needed, because we still can get an incoming packet.
1507 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001508 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001509 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001510 qlt_clear_tgt_db(tgt);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001511 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001512 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001513
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001514 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1515 "Waiting for sess works (tgt %p)", tgt);
1516 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1517 while (!list_empty(&tgt->sess_works_list)) {
1518 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1519 flush_scheduled_work();
1520 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1521 }
1522 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1523
1524 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001525 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001526
Joe Carnucciob85e0952017-08-23 15:05:11 -07001527 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001528
1529 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001530 if (!ha->flags.host_shutting_down &&
1531 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001532 qlt_disable_vha(vha);
1533
1534 /* Wait for sessions to clear out (just in case) */
Joe Carnucciob85e0952017-08-23 15:05:11 -07001535 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001536 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001537}
1538EXPORT_SYMBOL(qlt_stop_phase1);
1539
1540/* Called by tcm_qla2xxx configfs code */
1541void qlt_stop_phase2(struct qla_tgt *tgt)
1542{
Quinn Tran3a33dc92017-06-02 09:12:04 -07001543 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001544
1545 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001546 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001547 "Already in tgt->tgt_stopped state\n");
1548 dump_stack();
1549 return;
1550 }
Quinn Tran3a33dc92017-06-02 09:12:04 -07001551 if (!tgt->tgt_stop) {
1552 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1553 "%s: phase1 stop is not completed\n", __func__);
1554 dump_stack();
1555 return;
1556 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001557
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001558 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001559 tgt->tgt_stop = 0;
1560 tgt->tgt_stopped = 1;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001561 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001562
Quinn Tran3a33dc92017-06-02 09:12:04 -07001563 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001564 tgt);
1565}
1566EXPORT_SYMBOL(qlt_stop_phase2);
1567
1568/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001569static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001570{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001571 scsi_qla_host_t *vha = tgt->vha;
Quinn Trane326d222017-06-13 20:47:18 -07001572 void *node;
1573 u64 key = 0;
1574 u16 i;
1575 struct qla_qpair_hint *h;
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001576 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001577
Quinn Tran3a33dc92017-06-02 09:12:04 -07001578 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1579 !tgt->tgt_stopped)
1580 qlt_stop_phase1(tgt);
1581
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001582 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001583 qlt_stop_phase2(tgt);
1584
Quinn Trane326d222017-06-13 20:47:18 -07001585 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1586 unsigned long flags;
1587
1588 h = &tgt->qphints[i];
1589 if (h->qpair) {
1590 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1591 list_del(&h->hint_elem);
1592 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1593 h->qpair = NULL;
1594 }
1595 }
1596 kfree(tgt->qphints);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001597 mutex_lock(&qla_tgt_mutex);
1598 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1599 mutex_unlock(&qla_tgt_mutex);
Quinn Trane326d222017-06-13 20:47:18 -07001600
1601 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1602 btree_remove64(&tgt->lun_qpair_map, key);
1603
1604 btree_destroy64(&tgt->lun_qpair_map);
1605
Sawan Chandak3be63b1e2017-12-04 14:45:04 -08001606 if (vha->vp_idx)
1607 if (ha->tgt.tgt_ops &&
1608 ha->tgt.tgt_ops->remove_target &&
1609 vha->vha_tgt.target_lport_ptr)
1610 ha->tgt.tgt_ops->remove_target(vha);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001611
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001612 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001613
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001614 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001615 "Release of tgt %p finished\n", tgt);
1616
1617 kfree(tgt);
1618}
1619
1620/* ha->hardware_lock supposed to be held on entry */
1621static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1622 const void *param, unsigned int param_size)
1623{
1624 struct qla_tgt_sess_work_param *prm;
1625 unsigned long flags;
1626
1627 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1628 if (!prm) {
1629 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1630 "qla_target(%d): Unable to create session "
1631 "work, command will be refused", 0);
1632 return -ENOMEM;
1633 }
1634
1635 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1636 "Scheduling work (type %d, prm %p)"
1637 " to find session for param %p (size %d, tgt %p)\n",
1638 type, prm, param, param_size, tgt);
1639
1640 prm->type = type;
1641 memcpy(&prm->tm_iocb, param, param_size);
1642
1643 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1644 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1645 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1646
1647 schedule_work(&tgt->sess_work);
1648
1649 return 0;
1650}
1651
1652/*
1653 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1654 */
Quinn Tran82de8022017-06-13 20:47:17 -07001655static void qlt_send_notify_ack(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001656 struct imm_ntfy_from_isp *ntfy,
1657 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1658 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1659{
Quinn Tran82de8022017-06-13 20:47:17 -07001660 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001661 struct qla_hw_data *ha = vha->hw;
1662 request_t *pkt;
1663 struct nack_to_isp *nack;
1664
Quinn Tranec7193e2017-03-15 09:48:55 -07001665 if (!ha->flags.fw_started)
1666 return;
1667
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001668 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1669
Quinn Tran82de8022017-06-13 20:47:17 -07001670 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001671 if (!pkt) {
1672 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1673 "qla_target(%d): %s failed: unable to allocate "
1674 "request packet\n", vha->vp_idx, __func__);
1675 return;
1676 }
1677
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001678 if (vha->vha_tgt.qla_tgt != NULL)
1679 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001680
1681 pkt->entry_type = NOTIFY_ACK_TYPE;
1682 pkt->entry_count = 1;
1683
1684 nack = (struct nack_to_isp *)pkt;
1685 nack->ox_id = ntfy->ox_id;
1686
Quinn Tran726b8542017-01-19 22:28:00 -08001687 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001688 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1689 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1690 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001691 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001692 }
1693 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1694 nack->u.isp24.status = ntfy->u.isp24.status;
1695 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001696 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001697 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1698 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1699 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1700 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1701 nack->u.isp24.srr_reject_code = srr_reject_code;
1702 nack->u.isp24.srr_reject_code_expl = srr_explan;
1703 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1704
1705 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1706 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1707 vha->vp_idx, nack->u.isp24.status);
1708
Himanshu Madhani63163e02014-09-25 06:14:59 -04001709 /* Memory Barrier */
1710 wmb();
Quinn Tran82de8022017-06-13 20:47:17 -07001711 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001712}
1713
1714/*
1715 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1716 */
Quinn Tran82de8022017-06-13 20:47:17 -07001717static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001718 struct abts_recv_from_24xx *abts, uint32_t status,
1719 bool ids_reversed)
1720{
Quinn Tran82de8022017-06-13 20:47:17 -07001721 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001722 struct qla_hw_data *ha = vha->hw;
1723 struct abts_resp_to_24xx *resp;
1724 uint32_t f_ctl;
1725 uint8_t *p;
1726
1727 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1728 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1729 ha, abts, status);
1730
Quinn Tran82de8022017-06-13 20:47:17 -07001731 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1732 NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001733 if (!resp) {
1734 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1735 "qla_target(%d): %s failed: unable to allocate "
1736 "request packet", vha->vp_idx, __func__);
1737 return;
1738 }
1739
1740 resp->entry_type = ABTS_RESP_24XX;
1741 resp->entry_count = 1;
1742 resp->nport_handle = abts->nport_handle;
1743 resp->vp_index = vha->vp_idx;
1744 resp->sof_type = abts->sof_type;
1745 resp->exchange_address = abts->exchange_address;
1746 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001747 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001748 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1749 F_CTL_SEQ_INITIATIVE);
1750 p = (uint8_t *)&f_ctl;
1751 resp->fcp_hdr_le.f_ctl[0] = *p++;
1752 resp->fcp_hdr_le.f_ctl[1] = *p++;
1753 resp->fcp_hdr_le.f_ctl[2] = *p;
1754 if (ids_reversed) {
1755 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1756 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1757 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1758 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1759 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1760 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1761 } else {
1762 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1763 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1764 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1765 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1766 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1767 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1768 }
1769 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1770 if (status == FCP_TMF_CMPL) {
1771 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1772 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1773 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1774 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1775 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1776 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1777 } else {
1778 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1779 resp->payload.ba_rjt.reason_code =
1780 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1781 /* Other bytes are zero */
1782 }
1783
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001784 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001785
Himanshu Madhani63163e02014-09-25 06:14:59 -04001786 /* Memory Barrier */
1787 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07001788 if (qpair->reqq_start_iocbs)
1789 qpair->reqq_start_iocbs(qpair);
1790 else
1791 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001792}
1793
1794/*
1795 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1796 */
1797static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1798 struct abts_resp_from_24xx_fw *entry)
1799{
1800 struct ctio7_to_24xx *ctio;
1801
1802 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1803 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001804
Quinn Tran82de8022017-06-13 20:47:17 -07001805 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1806 vha->hw->base_qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001807 if (ctio == NULL) {
1808 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1809 "qla_target(%d): %s failed: unable to allocate "
1810 "request packet\n", vha->vp_idx, __func__);
1811 return;
1812 }
1813
1814 /*
1815 * We've got on entrance firmware's response on by us generated
1816 * ABTS response. So, in it ID fields are reversed.
1817 */
1818
1819 ctio->entry_type = CTIO_TYPE7;
1820 ctio->entry_count = 1;
1821 ctio->nport_handle = entry->nport_handle;
1822 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001823 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001824 ctio->vp_index = vha->vp_idx;
1825 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1826 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1827 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1828 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001829 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1830 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001831 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001832
Himanshu Madhani63163e02014-09-25 06:14:59 -04001833 /* Memory Barrier */
1834 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001835 qla2x00_start_iocbs(vha, vha->req);
1836
Quinn Tran82de8022017-06-13 20:47:17 -07001837 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1838 (struct abts_recv_from_24xx *)entry,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001839 FCP_TMF_CMPL, true);
1840}
1841
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001842static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1843{
1844 struct qla_tgt_sess_op *op;
1845 struct qla_tgt_cmd *cmd;
Quinn Tran8b631d82017-06-02 09:11:54 -07001846 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001847
Quinn Tran8b631d82017-06-02 09:11:54 -07001848 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001849 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1850 if (tag == op->atio.u.isp24.exchange_addr) {
1851 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001852 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001853 return 1;
1854 }
1855 }
1856
Quinn Tran41dc5292017-01-19 22:28:03 -08001857 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1858 if (tag == op->atio.u.isp24.exchange_addr) {
1859 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001860 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08001861 return 1;
1862 }
1863 }
1864
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001865 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1866 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001867 cmd->aborted = 1;
Quinn Tran8b631d82017-06-02 09:11:54 -07001868 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001869 return 1;
1870 }
1871 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001872 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001873
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001874 return 0;
1875}
1876
1877/* drop cmds for the given lun
1878 * XXX only looks for cmds on the port through which lun reset was recieved
1879 * XXX does not go through the list of other port (which may have cmds
1880 * for the same lun)
1881 */
1882static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001883 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001884{
1885 struct qla_tgt_sess_op *op;
1886 struct qla_tgt_cmd *cmd;
1887 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001888 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001889
1890 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001891 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001892 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1893 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001894 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001895
1896 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1897 op_lun = scsilun_to_int(
1898 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1899 if (op_key == key && op_lun == lun)
1900 op->aborted = true;
1901 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001902
1903 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1904 uint32_t op_key;
1905 u64 op_lun;
1906
1907 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1908 op_lun = scsilun_to_int(
1909 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1910 if (op_key == key && op_lun == lun)
1911 op->aborted = true;
1912 }
1913
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001914 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1915 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001916 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001917
1918 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1919 cmd_lun = scsilun_to_int(
1920 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1921 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001922 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001923 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001924 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001925}
1926
Quinn Tran84905df2018-05-01 09:01:53 -07001927static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
1928 uint64_t unpacked_lun)
1929{
1930 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1931 struct qla_qpair_hint *h = NULL;
1932
1933 if (vha->flags.qpairs_available) {
1934 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
1935 if (!h)
1936 h = &tgt->qphints[0];
1937 } else {
1938 h = &tgt->qphints[0];
1939 }
1940
1941 return h;
1942}
1943
1944static void qlt_do_tmr_work(struct work_struct *work)
1945{
1946 struct qla_tgt_mgmt_cmd *mcmd =
1947 container_of(work, struct qla_tgt_mgmt_cmd, work);
1948 struct qla_hw_data *ha = mcmd->vha->hw;
1949 int rc = EIO;
1950 uint32_t tag;
1951 unsigned long flags;
1952
1953 switch (mcmd->tmr_func) {
1954 case QLA_TGT_ABTS:
1955 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
1956 break;
1957 default:
1958 tag = 0;
1959 break;
1960 }
1961
1962 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
1963 mcmd->tmr_func, tag);
1964
1965 if (rc != 0) {
1966 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
1967 switch (mcmd->tmr_func) {
1968 case QLA_TGT_ABTS:
1969 qlt_24xx_send_abts_resp(mcmd->qpair,
1970 &mcmd->orig_iocb.abts,
1971 FCP_TMF_REJECTED, false);
1972 break;
1973 case QLA_TGT_LUN_RESET:
1974 case QLA_TGT_CLEAR_TS:
1975 case QLA_TGT_ABORT_TS:
1976 case QLA_TGT_CLEAR_ACA:
1977 case QLA_TGT_TARGET_RESET:
1978 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
1979 qla_sam_status);
1980 break;
1981
1982 case QLA_TGT_ABORT_ALL:
1983 case QLA_TGT_NEXUS_LOSS_SESS:
1984 case QLA_TGT_NEXUS_LOSS:
1985 qlt_send_notify_ack(mcmd->qpair,
1986 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
1987 break;
1988 }
1989 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
1990
1991 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
1992 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
1993 mcmd->vha->vp_idx, rc);
1994 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1995 }
1996}
1997
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001998/* ha->hardware_lock supposed to be held on entry */
1999static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08002000 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002001{
2002 struct qla_hw_data *ha = vha->hw;
2003 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tran84905df2018-05-01 09:01:53 -07002004 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
Steve Hodgson06e97b42012-11-16 08:06:17 -08002005
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07002006 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
2007 /* send TASK_ABORT response immediately */
Linus Torvalds48ea2ce2017-07-13 14:27:32 -07002008 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07002009 return 0;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04002010 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002011
2012 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2013 "qla_target(%d): task abort (tag=%d)\n",
2014 vha->vp_idx, abts->exchange_addr_to_abort);
2015
2016 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2017 if (mcmd == NULL) {
2018 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2019 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2020 vha->vp_idx, __func__);
2021 return -ENOMEM;
2022 }
2023 memset(mcmd, 0, sizeof(*mcmd));
2024
2025 mcmd->sess = sess;
2026 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002027 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08002028 mcmd->tmr_func = QLA_TGT_ABTS;
Quinn Tran84905df2018-05-01 09:01:53 -07002029 mcmd->qpair = h->qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002030 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002031
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07002032 /*
2033 * LUN is looked up by target-core internally based on the passed
2034 * abts->exchange_addr_to_abort tag.
2035 */
Quinn Tran84905df2018-05-01 09:01:53 -07002036 mcmd->se_cmd.cpuid = h->cpuid;
2037
2038 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2039 struct qla_tgt_cmd *abort_cmd;
2040
2041 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2042 abts->exchange_addr_to_abort);
2043 if (abort_cmd && abort_cmd->qpair) {
2044 mcmd->qpair = abort_cmd->qpair;
2045 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2046 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002047 }
2048
Quinn Tran84905df2018-05-01 09:01:53 -07002049 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2050 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2051
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002052 return 0;
2053}
2054
2055/*
2056 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2057 */
2058static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2059 struct abts_recv_from_24xx *abts)
2060{
2061 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08002062 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002063 uint32_t tag = abts->exchange_addr_to_abort;
2064 uint8_t s_id[3];
2065 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05002066 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002067
2068 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2069 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2070 "qla_target(%d): ABTS: Abort Sequence not "
2071 "supported\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002072 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2073 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002074 return;
2075 }
2076
2077 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2078 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2079 "qla_target(%d): ABTS: Unknown Exchange "
2080 "Address received\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002081 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2082 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002083 return;
2084 }
2085
2086 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2087 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2088 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2089 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2090 le32_to_cpu(abts->fcp_hdr_le.parameter));
2091
2092 s_id[0] = abts->fcp_hdr_le.s_id[2];
2093 s_id[1] = abts->fcp_hdr_le.s_id[1];
2094 s_id[2] = abts->fcp_hdr_le.s_id[0];
2095
Quinn Tran75601512015-12-17 14:57:04 -05002096 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002097 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2098 if (!sess) {
2099 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
Colin Ian King03fea732018-03-19 10:53:41 +00002100 "qla_target(%d): task abort for non-existent session\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002101 vha->vp_idx);
Quinn Tran75601512015-12-17 14:57:04 -05002102 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2103
Quinn Tranfb352652017-12-28 12:33:32 -08002104 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2105 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002106 return;
2107 }
Quinn Tran75601512015-12-17 14:57:04 -05002108 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2109
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002110
Quinn Tran726b8542017-01-19 22:28:00 -08002111 if (sess->deleted) {
Quinn Tran82de8022017-06-13 20:47:17 -07002112 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2113 false);
Alexei Potashnike52a8b42015-07-14 16:00:48 -04002114 return;
2115 }
2116
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002117 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2118 if (rc != 0) {
2119 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2120 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2121 vha->vp_idx, rc);
Quinn Tran82de8022017-06-13 20:47:17 -07002122 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2123 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002124 return;
2125 }
2126}
2127
2128/*
2129 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2130 */
Quinn Tran82de8022017-06-13 20:47:17 -07002131static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002132 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2133{
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002134 struct scsi_qla_host *ha = mcmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002135 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2136 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002137 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002138
2139 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2140 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2141 ha, atio, resp_code);
2142
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002143
Quinn Tran82de8022017-06-13 20:47:17 -07002144 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002145 if (ctio == NULL) {
2146 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2147 "qla_target(%d): %s failed: unable to allocate "
2148 "request packet\n", ha->vp_idx, __func__);
2149 return;
2150 }
2151
2152 ctio->entry_type = CTIO_TYPE7;
2153 ctio->entry_count = 1;
2154 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2155 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002156 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002157 ctio->vp_index = ha->vp_idx;
2158 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2159 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2160 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2161 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002162 temp = (atio->u.isp24.attr << 9)|
2163 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2164 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002165 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2166 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002167 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002168 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2169 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002170 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002171
Himanshu Madhani63163e02014-09-25 06:14:59 -04002172 /* Memory Barrier */
2173 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002174 if (qpair->reqq_start_iocbs)
2175 qpair->reqq_start_iocbs(qpair);
2176 else
2177 qla2x00_start_iocbs(ha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002178}
2179
2180void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2181{
2182 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2183}
2184EXPORT_SYMBOL(qlt_free_mcmd);
2185
Quinn Tranbe251522017-03-15 09:48:49 -07002186/*
2187 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2188 * reacquire
2189 */
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002190void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07002191 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2192{
2193 struct atio_from_isp *atio = &cmd->atio;
2194 struct ctio7_to_24xx *ctio;
2195 uint16_t temp;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002196 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranbe251522017-03-15 09:48:49 -07002197
2198 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2199 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2200 "sense_key=%02x, asc=%02x, ascq=%02x",
2201 vha, atio, scsi_status, sense_key, asc, ascq);
2202
2203 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2204 if (!ctio) {
2205 ql_dbg(ql_dbg_async, vha, 0x3067,
2206 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2207 vha->host_no, __func__);
2208 goto out;
2209 }
2210
2211 ctio->entry_type = CTIO_TYPE7;
2212 ctio->entry_count = 1;
2213 ctio->handle = QLA_TGT_SKIP_HANDLE;
2214 ctio->nport_handle = cmd->sess->loop_id;
2215 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2216 ctio->vp_index = vha->vp_idx;
2217 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2218 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2219 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2220 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002221 temp = (atio->u.isp24.attr << 9) |
2222 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2223 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002224 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2225 ctio->u.status1.ox_id = cpu_to_le16(temp);
2226 ctio->u.status1.scsi_status =
2227 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2228 ctio->u.status1.response_len = cpu_to_le16(18);
2229 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2230
2231 if (ctio->u.status1.residual != 0)
2232 ctio->u.status1.scsi_status |=
2233 cpu_to_le16(SS_RESIDUAL_UNDER);
2234
2235 /* Response code and sense key */
2236 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2237 (&ctio->u.status1.sense_data)[0]);
2238 /* Additional sense length */
2239 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2240 /* ASC and ASCQ */
2241 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2242 (&ctio->u.status1.sense_data)[3]);
2243
2244 /* Memory Barrier */
2245 wmb();
2246
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002247 if (qpair->reqq_start_iocbs)
2248 qpair->reqq_start_iocbs(qpair);
2249 else
2250 qla2x00_start_iocbs(vha, qpair->req);
2251
Quinn Tranbe251522017-03-15 09:48:49 -07002252out:
2253 return;
2254}
2255
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002256/* callback from target fabric module code */
2257void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2258{
2259 struct scsi_qla_host *vha = mcmd->sess->vha;
2260 struct qla_hw_data *ha = vha->hw;
2261 unsigned long flags;
Quinn Tran82de8022017-06-13 20:47:17 -07002262 struct qla_qpair *qpair = mcmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002263
2264 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2265 "TM response mcmd (%p) status %#x state %#x",
2266 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2267
Quinn Tran82de8022017-06-13 20:47:17 -07002268 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002269
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002270 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002271 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002272 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002273 * previous life, just abort the processing.
2274 */
2275 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002276 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2277 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002278 mcmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002279 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002280 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002281 return;
2282 }
2283
Quinn Tran726b8542017-01-19 22:28:00 -08002284 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2285 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
Quinn Tran41dc5292017-01-19 22:28:03 -08002286 ELS_LOGO ||
2287 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2288 ELS_PRLO ||
2289 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2290 ELS_TPRLO) {
Quinn Tran83548fe2017-06-02 09:12:01 -07002291 ql_dbg(ql_dbg_disc, vha, 0x2106,
Quinn Tran726b8542017-01-19 22:28:00 -08002292 "TM response logo %phC status %#x state %#x",
2293 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2294 mcmd->flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08002295 qlt_schedule_sess_for_deletion(mcmd->sess);
Quinn Tran726b8542017-01-19 22:28:00 -08002296 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002297 qlt_send_notify_ack(vha->hw->base_qpair,
2298 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
Quinn Tran726b8542017-01-19 22:28:00 -08002299 }
2300 } else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05002301 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Quinn Tran82de8022017-06-13 20:47:17 -07002302 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002303 mcmd->fc_tm_rsp, false);
2304 else
Quinn Tran82de8022017-06-13 20:47:17 -07002305 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002306 mcmd->fc_tm_rsp);
2307 }
2308 /*
2309 * Make the callback for ->free_mcmd() to queue_work() and invoke
2310 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2311 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2312 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2313 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2314 * qlt_xmit_tm_rsp() returns here..
2315 */
2316 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002317 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002318}
2319EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2320
2321/* No locks */
2322static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2323{
2324 struct qla_tgt_cmd *cmd = prm->cmd;
2325
2326 BUG_ON(cmd->sg_cnt == 0);
2327
2328 prm->sg = (struct scatterlist *)cmd->sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002329 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002330 cmd->sg_cnt, cmd->dma_data_direction);
2331 if (unlikely(prm->seg_cnt == 0))
2332 goto out_err;
2333
2334 prm->cmd->sg_mapped = 1;
2335
Quinn Tranf83adb62014-04-11 16:54:43 -04002336 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2337 /*
2338 * If greater than four sg entries then we need to allocate
2339 * the continuation entries
2340 */
Quinn Tranb5399f72017-06-13 20:47:27 -07002341 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
Quinn Tranf83adb62014-04-11 16:54:43 -04002342 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
Quinn Tranb5399f72017-06-13 20:47:27 -07002343 QLA_TGT_DATASEGS_PER_CMD_24XX,
2344 QLA_TGT_DATASEGS_PER_CONT_24XX);
Quinn Tranf83adb62014-04-11 16:54:43 -04002345 } else {
2346 /* DIF */
2347 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2348 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2349 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2350 prm->tot_dsds = prm->seg_cnt;
2351 } else
2352 prm->tot_dsds = prm->seg_cnt;
2353
2354 if (cmd->prot_sg_cnt) {
2355 prm->prot_sg = cmd->prot_sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002356 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
Quinn Tranf83adb62014-04-11 16:54:43 -04002357 cmd->prot_sg, cmd->prot_sg_cnt,
2358 cmd->dma_data_direction);
2359 if (unlikely(prm->prot_seg_cnt == 0))
2360 goto out_err;
2361
2362 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2363 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2364 /* Dif Bundling not support here */
2365 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2366 cmd->blk_sz);
2367 prm->tot_dsds += prm->prot_seg_cnt;
2368 } else
2369 prm->tot_dsds += prm->prot_seg_cnt;
2370 }
2371 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002372
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002373 return 0;
2374
2375out_err:
Quinn Tran22d84722017-06-13 20:47:25 -07002376 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002377 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2378 0, prm->cmd->sg_cnt);
2379 return -1;
2380}
2381
Joern Engelf9b67212014-09-16 16:23:18 -04002382static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002383{
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002384 struct qla_hw_data *ha;
2385 struct qla_qpair *qpair;
Joern Engelf9b67212014-09-16 16:23:18 -04002386 if (!cmd->sg_mapped)
2387 return;
2388
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002389 qpair = cmd->qpair;
2390
2391 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2392 cmd->dma_data_direction);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002393 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002394
2395 if (cmd->prot_sg_cnt)
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002396 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
Quinn Tranf83adb62014-04-11 16:54:43 -04002397 cmd->dma_data_direction);
2398
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002399 if (!cmd->ctx)
2400 return;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002401 ha = vha->hw;
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002402 if (cmd->ctx_dsd_alloced)
2403 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2404
2405 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002406}
2407
Quinn Tran82de8022017-06-13 20:47:17 -07002408static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002409 uint32_t req_cnt)
2410{
Quinn Trand63b3282017-06-02 09:12:07 -07002411 uint32_t cnt;
Quinn Tran82de8022017-06-13 20:47:17 -07002412 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002413
Quinn Tran82de8022017-06-13 20:47:17 -07002414 if (req->cnt < (req_cnt + 2)) {
Quinn Tranaf7bb382017-06-13 20:47:23 -07002415 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2416 RD_REG_DWORD_RELAXED(req->req_q_out));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002417
Quinn Tran82de8022017-06-13 20:47:17 -07002418 if (req->ring_index < cnt)
2419 req->cnt = cnt - req->ring_index;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002420 else
Quinn Tran82de8022017-06-13 20:47:17 -07002421 req->cnt = req->length - (req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002422
Quinn Tran82de8022017-06-13 20:47:17 -07002423 if (unlikely(req->cnt < (req_cnt + 2)))
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002424 return -EAGAIN;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002425 }
2426
Quinn Tran82de8022017-06-13 20:47:17 -07002427 req->cnt -= req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002428
2429 return 0;
2430}
2431
2432/*
2433 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2434 */
Quinn Tran82de8022017-06-13 20:47:17 -07002435static inline void *qlt_get_req_pkt(struct req_que *req)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002436{
2437 /* Adjust ring index. */
Quinn Tran82de8022017-06-13 20:47:17 -07002438 req->ring_index++;
2439 if (req->ring_index == req->length) {
2440 req->ring_index = 0;
2441 req->ring_ptr = req->ring;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002442 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002443 req->ring_ptr++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002444 }
Quinn Tran82de8022017-06-13 20:47:17 -07002445 return (cont_entry_t *)req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002446}
2447
2448/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002449static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002450{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002451 uint32_t h;
Quinn Tranc5419e22017-06-13 20:47:16 -07002452 int index;
2453 uint8_t found = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07002454 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002455
Quinn Tranc5419e22017-06-13 20:47:16 -07002456 h = req->current_outstanding_cmd;
2457
2458 for (index = 1; index < req->num_outstanding_cmds; index++) {
2459 h++;
2460 if (h == req->num_outstanding_cmds)
2461 h = 1;
2462
2463 if (h == QLA_TGT_SKIP_HANDLE)
2464 continue;
2465
2466 if (!req->outstanding_cmds[h]) {
2467 found = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002468 break;
2469 }
Quinn Tranc5419e22017-06-13 20:47:16 -07002470 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002471
Quinn Tranc5419e22017-06-13 20:47:16 -07002472 if (found) {
2473 req->current_outstanding_cmd = h;
2474 } else {
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002475 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2476 "qla_target(%d): Ran out of empty cmd slots\n",
2477 qpair->vha->vp_idx);
Quinn Tranc5419e22017-06-13 20:47:16 -07002478 h = QLA_TGT_NULL_HANDLE;
2479 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002480
2481 return h;
2482}
2483
2484/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002485static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2486 struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002487{
2488 uint32_t h;
2489 struct ctio7_to_24xx *pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002490 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002491 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002492
Quinn Tran82de8022017-06-13 20:47:17 -07002493 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002494 prm->pkt = pkt;
2495 memset(pkt, 0, sizeof(*pkt));
2496
2497 pkt->entry_type = CTIO_TYPE7;
2498 pkt->entry_count = (uint8_t)prm->req_cnt;
Quinn Tran22d84722017-06-13 20:47:25 -07002499 pkt->vp_index = prm->cmd->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002500
Quinn Tran82de8022017-06-13 20:47:17 -07002501 h = qlt_make_handle(qpair);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002502 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2503 /*
2504 * CTIO type 7 from the firmware doesn't provide a way to
2505 * know the initiator's LOOP ID, hence we can't find
2506 * the session and, so, the command.
2507 */
2508 return -EAGAIN;
Quinn Trane326d222017-06-13 20:47:18 -07002509 } else
2510 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002511
Quinn Tran82de8022017-06-13 20:47:17 -07002512 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2513 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2514 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002515 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002516 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2517 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2518 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2519 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002520 temp = atio->u.isp24.attr << 9;
2521 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002522 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2523 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002524 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2525
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002526 return 0;
2527}
2528
2529/*
2530 * ha->hardware_lock supposed to be held on entry. We have already made sure
2531 * that there is sufficient amount of request entries to not drop it.
2532 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002533static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002534{
2535 int cnt;
2536 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002537
2538 /* Build continuation packets */
2539 while (prm->seg_cnt > 0) {
2540 cont_a64_entry_t *cont_pkt64 =
Quinn Tran82de8022017-06-13 20:47:17 -07002541 (cont_a64_entry_t *)qlt_get_req_pkt(
2542 prm->cmd->qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002543
2544 /*
2545 * Make sure that from cont_pkt64 none of
2546 * 64-bit specific fields used for 32-bit
2547 * addressing. Cast to (cont_entry_t *) for
2548 * that.
2549 */
2550
2551 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2552
2553 cont_pkt64->entry_count = 1;
2554 cont_pkt64->sys_define = 0;
2555
Quinn Tran32d29b42017-06-13 20:47:26 -07002556 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2557 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002558
2559 /* Load continuation entry data segments */
2560 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002561 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002562 cnt++, prm->seg_cnt--) {
2563 *dword_ptr++ =
2564 cpu_to_le32(pci_dma_lo32
2565 (sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002566 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2567 (sg_dma_address(prm->sg)));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002568 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2569
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002570 prm->sg = sg_next(prm->sg);
2571 }
2572 }
2573}
2574
2575/*
2576 * ha->hardware_lock supposed to be held on entry. We have already made sure
2577 * that there is sufficient amount of request entries to not drop it.
2578 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002579static void qlt_load_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002580{
2581 int cnt;
2582 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002583 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2584
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002585 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2586
2587 /* Setup packet address segment pointer */
2588 dword_ptr = pkt24->u.status0.dseg_0_address;
2589
2590 /* Set total data segment count */
2591 if (prm->seg_cnt)
2592 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2593
2594 if (prm->seg_cnt == 0) {
2595 /* No data transfer */
2596 *dword_ptr++ = 0;
2597 *dword_ptr = 0;
2598 return;
2599 }
2600
2601 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002602
2603 /* Load command entry data segments */
2604 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002605 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002606 cnt++, prm->seg_cnt--) {
2607 *dword_ptr++ =
2608 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002609
2610 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2611 sg_dma_address(prm->sg)));
2612
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002613 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2614
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002615 prm->sg = sg_next(prm->sg);
2616 }
2617
Quinn Tran32d29b42017-06-13 20:47:26 -07002618 qlt_load_cont_data_segments(prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002619}
2620
2621static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2622{
2623 return cmd->bufflen > 0;
2624}
2625
Quinn Tranbe251522017-03-15 09:48:49 -07002626static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2627{
2628 struct qla_tgt_cmd *cmd;
2629 struct scsi_qla_host *vha;
2630
2631 /* asc 0x10=dif error */
2632 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2633 cmd = prm->cmd;
2634 vha = cmd->vha;
2635 /* ASCQ */
2636 switch (prm->sense_buffer[13]) {
2637 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002638 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002639 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2640 "se_cmd=%p tag[%x]",
2641 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2642 cmd->atio.u.isp24.exchange_addr);
2643 break;
2644 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002645 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002646 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2647 "se_cmd=%p tag[%x]",
2648 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2649 cmd->atio.u.isp24.exchange_addr);
2650 break;
2651 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002652 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002653 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2654 "se_cmd=%p tag[%x]",
2655 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2656 cmd->atio.u.isp24.exchange_addr);
2657 break;
2658 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002659 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002660 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2661 "se_cmd=%p tag[%x]",
2662 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2663 cmd->atio.u.isp24.exchange_addr);
2664 break;
2665 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002666 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002667 }
2668}
2669
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002670/*
2671 * Called without ha->hardware_lock held
2672 */
2673static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2674 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2675 uint32_t *full_req_cnt)
2676{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002677 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Tran22d84722017-06-13 20:47:25 -07002678 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002679
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002680 prm->cmd = cmd;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002681 prm->tgt = cmd->tgt;
2682 prm->pkt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002683 prm->rq_result = scsi_status;
2684 prm->sense_buffer = &cmd->sense_buffer[0];
2685 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2686 prm->sg = NULL;
2687 prm->seg_cnt = -1;
2688 prm->req_cnt = 1;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002689 prm->residual = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002690 prm->add_status_pkt = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002691 prm->prot_sg = NULL;
2692 prm->prot_seg_cnt = 0;
2693 prm->tot_dsds = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002694
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002695 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2696 if (qlt_pci_map_calc_cnt(prm) != 0)
2697 return -EAGAIN;
2698 }
2699
2700 *full_req_cnt = prm->req_cnt;
2701
2702 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2703 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002704 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002705 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2706 prm->residual, se_cmd->tag,
2707 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2708 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002709 prm->rq_result |= SS_RESIDUAL_UNDER;
2710 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2711 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002712 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002713 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2714 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2715 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002716 prm->rq_result |= SS_RESIDUAL_OVER;
2717 }
2718
2719 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2720 /*
2721 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2722 * ignored in *xmit_response() below
2723 */
2724 if (qlt_has_data(cmd)) {
2725 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002726 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002727 (prm->rq_result != 0))) {
2728 prm->add_status_pkt = 1;
2729 (*full_req_cnt)++;
2730 }
2731 }
2732 }
2733
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002734 return 0;
2735}
2736
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002737static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2738 int sending_sense)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002739{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002740 if (cmd->qpair->enable_class_2)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002741 return 0;
2742
2743 if (sending_sense)
2744 return cmd->conf_compl_supported;
2745 else
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002746 return cmd->qpair->enable_explicit_conf &&
2747 cmd->conf_compl_supported;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002748}
2749
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002750static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2751 struct qla_tgt_prm *prm)
2752{
2753 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2754 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002755 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002756 if (qlt_need_explicit_conf(prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002757 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002758 CTIO7_FLAGS_EXPLICIT_CONFORM |
2759 CTIO7_FLAGS_CONFORM_REQ);
2760 }
2761 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2762 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2763 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2764 int i;
2765
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002766 if (qlt_need_explicit_conf(prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002767 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Quinn Tran22d84722017-06-13 20:47:25 -07002768 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002769 "Skipping EXPLICIT_CONFORM and "
2770 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2771 "non GOOD status\n");
2772 goto skip_explict_conf;
2773 }
Bart Van Asschead950362015-07-09 07:24:08 -07002774 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002775 CTIO7_FLAGS_EXPLICIT_CONFORM |
2776 CTIO7_FLAGS_CONFORM_REQ);
2777 }
2778skip_explict_conf:
2779 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002780 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002781 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002782 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002783 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002784 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002785 ctio->u.status1.sense_length =
2786 cpu_to_le16(prm->sense_buffer_len);
2787 for (i = 0; i < prm->sense_buffer_len/4; i++)
2788 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2789 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002790
2791 qlt_print_dif_err(prm);
2792
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002793 } else {
2794 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002795 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002796 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002797 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002798 ctio->u.status1.sense_length = 0;
2799 memset(ctio->u.status1.sense_data, 0,
2800 sizeof(ctio->u.status1.sense_data));
2801 }
2802
2803 /* Sense with len > 24, is it possible ??? */
2804}
2805
Quinn Tranf83adb62014-04-11 16:54:43 -04002806static inline int
2807qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2808{
Quinn Tranf83adb62014-04-11 16:54:43 -04002809 switch (se_cmd->prot_op) {
2810 case TARGET_PROT_DOUT_INSERT:
2811 case TARGET_PROT_DIN_STRIP:
2812 if (ql2xenablehba_err_chk >= 1)
2813 return 1;
2814 break;
2815 case TARGET_PROT_DOUT_PASS:
2816 case TARGET_PROT_DIN_PASS:
2817 if (ql2xenablehba_err_chk >= 2)
2818 return 1;
2819 break;
2820 case TARGET_PROT_DIN_INSERT:
2821 case TARGET_PROT_DOUT_STRIP:
2822 return 1;
2823 default:
2824 break;
2825 }
2826 return 0;
2827}
2828
Quinn Tranbe251522017-03-15 09:48:49 -07002829static inline int
2830qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002831{
Quinn Tranbe251522017-03-15 09:48:49 -07002832 switch (se_cmd->prot_op) {
2833 case TARGET_PROT_DIN_INSERT:
2834 case TARGET_PROT_DOUT_INSERT:
2835 case TARGET_PROT_DIN_STRIP:
2836 case TARGET_PROT_DOUT_STRIP:
2837 case TARGET_PROT_DIN_PASS:
2838 case TARGET_PROT_DOUT_PASS:
2839 return 1;
2840 default:
2841 return 0;
2842 }
2843 return 0;
2844}
Quinn Tranf83adb62014-04-11 16:54:43 -04002845
Quinn Tranbe251522017-03-15 09:48:49 -07002846/*
2847 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2848 */
2849static void
2850qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2851 uint16_t *pfw_prot_opts)
2852{
2853 struct se_cmd *se_cmd = &cmd->se_cmd;
2854 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2855 scsi_qla_host_t *vha = cmd->tgt->vha;
2856 struct qla_hw_data *ha = vha->hw;
2857 uint32_t t32 = 0;
2858
2859 /*
2860 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002861 * have been immplemented by TCM, before AppTag is avail.
2862 * Look for modesense_handlers[]
2863 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002864 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002865 ctx->app_tag_mask[0] = 0x0;
2866 ctx->app_tag_mask[1] = 0x0;
2867
Quinn Tranbe251522017-03-15 09:48:49 -07002868 if (IS_PI_UNINIT_CAPABLE(ha)) {
2869 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2870 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2871 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2872 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2873 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2874 }
2875
2876 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2877
Quinn Tranf83adb62014-04-11 16:54:43 -04002878 switch (se_cmd->prot_type) {
2879 case TARGET_DIF_TYPE0_PROT:
2880 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002881 * No check for ql2xenablehba_err_chk, as it
2882 * would be an I/O error if hba tag generation
2883 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002884 */
2885 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002886 /* enable ALL bytes of the ref tag */
2887 ctx->ref_tag_mask[0] = 0xff;
2888 ctx->ref_tag_mask[1] = 0xff;
2889 ctx->ref_tag_mask[2] = 0xff;
2890 ctx->ref_tag_mask[3] = 0xff;
2891 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002892 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002893 /*
2894 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2895 * REF tag, and 16 bit app tag.
2896 */
2897 ctx->ref_tag = cpu_to_le32(lba);
2898 if (!qla_tgt_ref_mask_check(se_cmd) ||
2899 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2900 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2901 break;
2902 }
2903 /* enable ALL bytes of the ref tag */
2904 ctx->ref_tag_mask[0] = 0xff;
2905 ctx->ref_tag_mask[1] = 0xff;
2906 ctx->ref_tag_mask[2] = 0xff;
2907 ctx->ref_tag_mask[3] = 0xff;
2908 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002909 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002910 /*
2911 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2912 * tag has to match LBA in CDB + N
2913 */
2914 ctx->ref_tag = cpu_to_le32(lba);
2915 if (!qla_tgt_ref_mask_check(se_cmd) ||
2916 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2917 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2918 break;
2919 }
2920 /* enable ALL bytes of the ref tag */
2921 ctx->ref_tag_mask[0] = 0xff;
2922 ctx->ref_tag_mask[1] = 0xff;
2923 ctx->ref_tag_mask[2] = 0xff;
2924 ctx->ref_tag_mask[3] = 0xff;
2925 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002926 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002927 /* For TYPE 3 protection: 16 bit GUARD only */
2928 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2929 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2930 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2931 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002932 }
2933}
2934
Quinn Tranf83adb62014-04-11 16:54:43 -04002935static inline int
Quinn Tran82de8022017-06-13 20:47:17 -07002936qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
Quinn Tranf83adb62014-04-11 16:54:43 -04002937{
2938 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002939 uint32_t transfer_length = 0;
2940 uint32_t data_bytes;
2941 uint32_t dif_bytes;
2942 uint8_t bundling = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04002943 struct crc_context *crc_ctx_pkt = NULL;
2944 struct qla_hw_data *ha;
2945 struct ctio_crc2_to_fw *pkt;
2946 dma_addr_t crc_ctx_dma;
2947 uint16_t fw_prot_opts = 0;
2948 struct qla_tgt_cmd *cmd = prm->cmd;
2949 struct se_cmd *se_cmd = &cmd->se_cmd;
2950 uint32_t h;
2951 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07002952 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002953 uint16_t t16;
Quinn Tran82de8022017-06-13 20:47:17 -07002954 scsi_qla_host_t *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04002955
Quinn Tranf83adb62014-04-11 16:54:43 -04002956 ha = vha->hw;
2957
Quinn Tran82de8022017-06-13 20:47:17 -07002958 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
Quinn Tranf83adb62014-04-11 16:54:43 -04002959 prm->pkt = pkt;
2960 memset(pkt, 0, sizeof(*pkt));
2961
Quinn Tran22d84722017-06-13 20:47:25 -07002962 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
Quinn Tranf83adb62014-04-11 16:54:43 -04002963 "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 -07002964 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
Quinn Tranf83adb62014-04-11 16:54:43 -04002965 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2966
2967 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2968 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2969 bundling = 0;
2970
2971 /* Compute dif len and adjust data len to incude protection */
2972 data_bytes = cmd->bufflen;
2973 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2974
2975 switch (se_cmd->prot_op) {
2976 case TARGET_PROT_DIN_INSERT:
2977 case TARGET_PROT_DOUT_STRIP:
2978 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07002979 if (cmd->prot_sg_cnt)
2980 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04002981 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002982 case TARGET_PROT_DIN_STRIP:
2983 case TARGET_PROT_DOUT_INSERT:
2984 case TARGET_PROT_DIN_PASS:
2985 case TARGET_PROT_DOUT_PASS:
2986 transfer_length = data_bytes + dif_bytes;
2987 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002988 default:
2989 BUG();
2990 break;
2991 }
2992
2993 if (!qlt_hba_err_chk_enabled(se_cmd))
2994 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2995 /* HBA error checking enabled */
2996 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2997 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2998 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2999 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3000 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3001 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3002 }
3003
3004 switch (se_cmd->prot_op) {
3005 case TARGET_PROT_DIN_INSERT:
3006 case TARGET_PROT_DOUT_INSERT:
3007 fw_prot_opts |= PO_MODE_DIF_INSERT;
3008 break;
3009 case TARGET_PROT_DIN_STRIP:
3010 case TARGET_PROT_DOUT_STRIP:
3011 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3012 break;
3013 case TARGET_PROT_DIN_PASS:
3014 case TARGET_PROT_DOUT_PASS:
3015 fw_prot_opts |= PO_MODE_DIF_PASS;
3016 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3017 break;
3018 default:/* Normal Request */
3019 fw_prot_opts |= PO_MODE_DIF_PASS;
3020 break;
3021 }
3022
Quinn Tranf83adb62014-04-11 16:54:43 -04003023 /* ---- PKT ---- */
3024 /* Update entry type to indicate Command Type CRC_2 IOCB */
3025 pkt->entry_type = CTIO_CRC2;
3026 pkt->entry_count = 1;
Quinn Tran22d84722017-06-13 20:47:25 -07003027 pkt->vp_index = cmd->vp_idx;
Quinn Tranf83adb62014-04-11 16:54:43 -04003028
Quinn Tran82de8022017-06-13 20:47:17 -07003029 h = qlt_make_handle(qpair);
Quinn Tranf83adb62014-04-11 16:54:43 -04003030 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3031 /*
3032 * CTIO type 7 from the firmware doesn't provide a way to
3033 * know the initiator's LOOP ID, hence we can't find
3034 * the session and, so, the command.
3035 */
3036 return -EAGAIN;
3037 } else
Quinn Tran82de8022017-06-13 20:47:17 -07003038 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Quinn Tranf83adb62014-04-11 16:54:43 -04003039
Quinn Tran82de8022017-06-13 20:47:17 -07003040 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3041 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07003042 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07003043 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04003044 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3045 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3046 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3047 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003048
3049 /* silence compile warning */
3050 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3051 pkt->ox_id = cpu_to_le16(t16);
3052
3053 t16 = (atio->u.isp24.attr << 9);
3054 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04003055 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3056
3057 /* Set transfer direction */
3058 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07003059 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04003060 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07003061 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04003062
Quinn Tranf83adb62014-04-11 16:54:43 -04003063 pkt->dseg_count = prm->tot_dsds;
3064 /* Fibre channel byte count */
3065 pkt->transfer_length = cpu_to_le32(transfer_length);
3066
Quinn Tranf83adb62014-04-11 16:54:43 -04003067 /* ----- CRC context -------- */
3068
3069 /* Allocate CRC context from global pool */
3070 crc_ctx_pkt = cmd->ctx =
Souptick Joarder501017f2018-02-15 01:40:38 +05303071 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
Quinn Tranf83adb62014-04-11 16:54:43 -04003072
3073 if (!crc_ctx_pkt)
3074 goto crc_queuing_error;
3075
Quinn Tranf83adb62014-04-11 16:54:43 -04003076 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3077 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3078
3079 /* Set handle */
3080 crc_ctx_pkt->handle = pkt->handle;
3081
Quinn Tranbe251522017-03-15 09:48:49 -07003082 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04003083
3084 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
3085 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
3086 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3087
Quinn Tranf83adb62014-04-11 16:54:43 -04003088 if (!bundling) {
3089 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
3090 } else {
3091 /*
3092 * Configure Bundling if we need to fetch interlaving
3093 * protection PCI accesses
3094 */
3095 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3096 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3097 crc_ctx_pkt->u.bundling.dseg_count =
3098 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3099 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
3100 }
3101
3102 /* Finish the common fields of CRC pkt */
3103 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3104 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3105 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07003106 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003107
Quinn Tranbe251522017-03-15 09:48:49 -07003108 memset((uint8_t *)&tc, 0 , sizeof(tc));
3109 tc.vha = vha;
3110 tc.blk_sz = cmd->blk_sz;
3111 tc.bufflen = cmd->bufflen;
3112 tc.sg = cmd->sg;
3113 tc.prot_sg = cmd->prot_sg;
3114 tc.ctx = crc_ctx_pkt;
3115 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04003116
3117 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07003118 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04003119
3120 if (!bundling && prm->prot_seg_cnt) {
3121 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003122 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003123 goto crc_queuing_error;
3124 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003125 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003126 goto crc_queuing_error;
3127
3128 if (bundling && prm->prot_seg_cnt) {
3129 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003130 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04003131
3132 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3133 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003134 prm->prot_seg_cnt, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003135 goto crc_queuing_error;
3136 }
3137 return QLA_SUCCESS;
3138
3139crc_queuing_error:
3140 /* Cleanup will be performed by the caller */
Quinn Tran82de8022017-06-13 20:47:17 -07003141 qpair->req->outstanding_cmds[h] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003142
3143 return QLA_FUNCTION_FAILED;
3144}
3145
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003146/*
3147 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3148 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3149 */
3150int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3151 uint8_t scsi_status)
3152{
3153 struct scsi_qla_host *vha = cmd->vha;
Quinn Tran82de8022017-06-13 20:47:17 -07003154 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003155 struct ctio7_to_24xx *pkt;
3156 struct qla_tgt_prm prm;
3157 uint32_t full_req_cnt = 0;
3158 unsigned long flags = 0;
3159 int res;
3160
Quinn Tran726b8542017-01-19 22:28:00 -08003161 if (cmd->sess && cmd->sess->deleted) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003162 cmd->state = QLA_TGT_STATE_PROCESSED;
3163 if (cmd->sess->logout_completed)
3164 /* no need to terminate. FW already freed exchange. */
3165 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3166 else
Quinn Tran82de8022017-06-13 20:47:17 -07003167 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003168 return 0;
3169 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003170
Quinn Tran22d84722017-06-13 20:47:25 -07003171 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
Quinn Tran82de8022017-06-13 20:47:17 -07003172 "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 -04003173 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3174 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
Quinn Tran82de8022017-06-13 20:47:17 -07003175 &cmd->se_cmd, qpair->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003176
3177 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3178 &full_req_cnt);
3179 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003180 return res;
3181 }
3182
Quinn Tran82de8022017-06-13 20:47:17 -07003183 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003184
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003185 if (xmit_type == QLA_TGT_XMIT_STATUS)
Quinn Tran60a9ead2017-06-13 20:47:28 -07003186 qpair->tgt_counters.core_qla_snd_status++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003187 else
Quinn Tran60a9ead2017-06-13 20:47:28 -07003188 qpair->tgt_counters.core_qla_que_buf++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003189
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003190 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003191 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003192 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003193 * previous life, just abort the processing.
3194 */
3195 cmd->state = QLA_TGT_STATE_PROCESSED;
3196 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003197 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003198 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3199 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003200 cmd->reset_count, qpair->chip_reset);
Quinn Tran82de8022017-06-13 20:47:17 -07003201 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04003202 return 0;
3203 }
3204
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003205 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003206 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003207 if (unlikely(res))
3208 goto out_unmap_unlock;
3209
Quinn Tranf83adb62014-04-11 16:54:43 -04003210 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
Quinn Tran82de8022017-06-13 20:47:17 -07003211 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003212 else
Quinn Tran82de8022017-06-13 20:47:17 -07003213 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Quinn Tran810e30b2015-06-10 11:05:20 -04003214 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003215 qpair->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003216 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003217 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003218
3219 pkt = (struct ctio7_to_24xx *)prm.pkt;
3220
3221 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3222 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003223 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003224 CTIO7_FLAGS_STATUS_MODE_0);
3225
Quinn Tranf83adb62014-04-11 16:54:43 -04003226 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003227 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003228
3229 if (prm.add_status_pkt == 0) {
3230 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3231 pkt->u.status0.scsi_status =
3232 cpu_to_le16(prm.rq_result);
3233 pkt->u.status0.residual =
3234 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003235 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003236 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003237 if (qlt_need_explicit_conf(cmd, 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003238 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003239 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003240 CTIO7_FLAGS_EXPLICIT_CONFORM |
3241 CTIO7_FLAGS_CONFORM_REQ);
3242 }
3243 }
3244
3245 } else {
3246 /*
3247 * We have already made sure that there is sufficient
3248 * amount of request entries to not drop HW lock in
3249 * req_pkt().
3250 */
3251 struct ctio7_to_24xx *ctio =
Quinn Tran82de8022017-06-13 20:47:17 -07003252 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3253 qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003254
Quinn Tran22d84722017-06-13 20:47:25 -07003255 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
Arun Easi667024a2014-09-25 06:14:47 -04003256 "Building additional status packet 0x%p.\n",
3257 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003258
Quinn Tranf83adb62014-04-11 16:54:43 -04003259 /*
3260 * T10Dif: ctio_crc2_to_fw overlay ontop of
3261 * ctio7_to_24xx
3262 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003263 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003264 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003265 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003266 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003267 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003268 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003269 CTIO7_FLAGS_DATA_IN);
3270
3271 /* Real finish is ctio_m1's finish */
3272 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003273 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003274 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003275
3276 /* qlt_24xx_init_ctio_to_isp will correct
3277 * all neccessary fields that's part of CTIO7.
3278 * There should be no residual of CTIO-CRC2 data.
3279 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003280 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3281 &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003282 }
3283 } else
3284 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3285
3286
3287 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003288 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003289
Himanshu Madhani63163e02014-09-25 06:14:59 -04003290 /* Memory Barrier */
3291 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003292 if (qpair->reqq_start_iocbs)
3293 qpair->reqq_start_iocbs(qpair);
3294 else
3295 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003296 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003297
3298 return 0;
3299
3300out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003301 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003302 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003303
3304 return res;
3305}
3306EXPORT_SYMBOL(qlt_xmit_response);
3307
3308int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3309{
3310 struct ctio7_to_24xx *pkt;
3311 struct scsi_qla_host *vha = cmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003312 struct qla_tgt *tgt = cmd->tgt;
3313 struct qla_tgt_prm prm;
Quinn Tran82de8022017-06-13 20:47:17 -07003314 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003315 int res = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003316 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003317
3318 memset(&prm, 0, sizeof(prm));
3319 prm.cmd = cmd;
3320 prm.tgt = tgt;
3321 prm.sg = NULL;
3322 prm.req_cnt = 1;
3323
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003324 /* Calculate number of entries and segments required */
3325 if (qlt_pci_map_calc_cnt(&prm) != 0)
3326 return -EAGAIN;
3327
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003328 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003329 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003330 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003331 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003332 * previous life, just abort the processing.
3333 */
3334 cmd->state = QLA_TGT_STATE_NEED_DATA;
3335 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003336 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003337 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3338 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003339 cmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003340 return 0;
3341 }
3342
Quinn Tran82de8022017-06-13 20:47:17 -07003343 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003344 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003345 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003346 if (res != 0)
3347 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003348 if (cmd->se_cmd.prot_op)
Quinn Tran82de8022017-06-13 20:47:17 -07003349 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003350 else
Quinn Tran82de8022017-06-13 20:47:17 -07003351 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003352
Quinn Tran810e30b2015-06-10 11:05:20 -04003353 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003354 qpair->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003355 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003356 }
3357
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003358 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003359 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003360 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003361
3362 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003363 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003364
3365 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003366 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003367
Himanshu Madhani63163e02014-09-25 06:14:59 -04003368 /* Memory Barrier */
3369 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003370 if (qpair->reqq_start_iocbs)
3371 qpair->reqq_start_iocbs(qpair);
3372 else
3373 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003374 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003375
3376 return res;
3377
3378out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003379 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003380 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003381
3382 return res;
3383}
3384EXPORT_SYMBOL(qlt_rdy_to_xfer);
3385
Quinn Tranf83adb62014-04-11 16:54:43 -04003386
3387/*
Quinn Tranbe251522017-03-15 09:48:49 -07003388 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003389 */
Quinn Tranbe251522017-03-15 09:48:49 -07003390static void
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003391qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003392 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003393{
3394 uint8_t *ap = &sts->actual_dif[0];
3395 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003396 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003397 uint8_t scsi_status, sense_key, asc, ascq;
3398 unsigned long flags;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003399 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003400
Quinn Tranbe251522017-03-15 09:48:49 -07003401 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003402
Quinn Tranbe251522017-03-15 09:48:49 -07003403 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3404 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3405 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003406
Quinn Tranbe251522017-03-15 09:48:49 -07003407 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3408 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3409 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003410
Quinn Tranbe251522017-03-15 09:48:49 -07003411 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3412 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003413
Quinn Tranbe251522017-03-15 09:48:49 -07003414 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003415
Quinn Tranbe251522017-03-15 09:48:49 -07003416 /* check appl tag */
3417 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003418 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3419 "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]",
3420 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3421 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3422 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3423 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003424
Quinn Tranbe251522017-03-15 09:48:49 -07003425 cmd->dif_err_code = DIF_ERR_APP;
3426 scsi_status = SAM_STAT_CHECK_CONDITION;
3427 sense_key = ABORTED_COMMAND;
3428 asc = 0x10;
3429 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003430 }
3431
3432 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003433 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003434 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3435 "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] ",
3436 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3437 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3438 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3439 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003440
Quinn Tranbe251522017-03-15 09:48:49 -07003441 cmd->dif_err_code = DIF_ERR_REF;
3442 scsi_status = SAM_STAT_CHECK_CONDITION;
3443 sense_key = ABORTED_COMMAND;
3444 asc = 0x10;
3445 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003446 goto out;
3447 }
3448
Quinn Tranbe251522017-03-15 09:48:49 -07003449 /* check guard */
3450 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003451 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3452 "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]",
3453 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3454 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3455 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3456 cmd->atio.u.isp24.fcp_hdr.ox_id);
3457
Quinn Tranbe251522017-03-15 09:48:49 -07003458 cmd->dif_err_code = DIF_ERR_GRD;
3459 scsi_status = SAM_STAT_CHECK_CONDITION;
3460 sense_key = ABORTED_COMMAND;
3461 asc = 0x10;
3462 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003463 }
3464out:
Quinn Tranbe251522017-03-15 09:48:49 -07003465 switch (cmd->state) {
3466 case QLA_TGT_STATE_NEED_DATA:
3467 /* handle_data will load DIF error code */
3468 cmd->state = QLA_TGT_STATE_DATA_IN;
3469 vha->hw->tgt.tgt_ops->handle_data(cmd);
3470 break;
3471 default:
3472 spin_lock_irqsave(&cmd->cmd_lock, flags);
3473 if (cmd->aborted) {
3474 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3475 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3476 break;
3477 }
3478 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003479
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003480 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3481 ascq);
Quinn Tranbe251522017-03-15 09:48:49 -07003482 /* assume scsi status gets out on the wire.
3483 * Will not wait for completion.
3484 */
3485 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3486 break;
3487 }
3488}
Quinn Tranf83adb62014-04-11 16:54:43 -04003489
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003490/* If hardware_lock held on entry, might drop it, then reaquire */
3491/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003492static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3493 struct imm_ntfy_from_isp *ntfy)
3494{
3495 struct nack_to_isp *nack;
3496 struct qla_hw_data *ha = vha->hw;
3497 request_t *pkt;
3498 int ret = 0;
3499
3500 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3501 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3502
Quinn Tranec7193e2017-03-15 09:48:55 -07003503 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003504 if (pkt == NULL) {
3505 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3506 "qla_target(%d): %s failed: unable to allocate "
3507 "request packet\n", vha->vp_idx, __func__);
3508 return -ENOMEM;
3509 }
3510
3511 pkt->entry_type = NOTIFY_ACK_TYPE;
3512 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003513 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003514
3515 nack = (struct nack_to_isp *)pkt;
3516 nack->ox_id = ntfy->ox_id;
3517
3518 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3519 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3520 nack->u.isp24.flags = ntfy->u.isp24.flags &
3521 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3522 }
3523
3524 /* terminate */
3525 nack->u.isp24.flags |=
3526 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3527
3528 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3529 nack->u.isp24.status = ntfy->u.isp24.status;
3530 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3531 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3532 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3533 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3534 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3535 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3536
3537 qla2x00_start_iocbs(vha, vha->req);
3538 return ret;
3539}
3540
3541static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3542 struct imm_ntfy_from_isp *imm, int ha_locked)
3543{
3544 unsigned long flags = 0;
3545 int rc;
3546
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003547 if (ha_locked) {
3548 rc = __qlt_send_term_imm_notif(vha, imm);
3549
3550#if 0 /* Todo */
3551 if (rc == -ENOMEM)
3552 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003553#else
3554 if (rc) {
3555 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003556#endif
3557 goto done;
3558 }
3559
3560 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3561 rc = __qlt_send_term_imm_notif(vha, imm);
3562
3563#if 0 /* Todo */
3564 if (rc == -ENOMEM)
3565 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3566#endif
3567
3568done:
3569 if (!ha_locked)
3570 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3571}
3572
Quinn Tran83548fe2017-06-02 09:12:01 -07003573/*
3574 * If hardware_lock held on entry, might drop it, then reaquire
3575 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3576 */
Quinn Tran82de8022017-06-13 20:47:17 -07003577static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003578 struct qla_tgt_cmd *cmd,
3579 struct atio_from_isp *atio)
3580{
Quinn Tran82de8022017-06-13 20:47:17 -07003581 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003582 struct ctio7_to_24xx *ctio24;
3583 struct qla_hw_data *ha = vha->hw;
3584 request_t *pkt;
3585 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003586 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003587
Quinn Tran83548fe2017-06-02 09:12:01 -07003588 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003589
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003590 if (cmd)
3591 vha = cmd->vha;
3592
Quinn Tran82de8022017-06-13 20:47:17 -07003593 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003594 if (pkt == NULL) {
3595 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3596 "qla_target(%d): %s failed: unable to allocate "
3597 "request packet\n", vha->vp_idx, __func__);
3598 return -ENOMEM;
3599 }
3600
3601 if (cmd != NULL) {
3602 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3603 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3604 "qla_target(%d): Terminating cmd %p with "
3605 "incorrect state %d\n", vha->vp_idx, cmd,
3606 cmd->state);
3607 } else
3608 ret = 1;
3609 }
3610
Quinn Tran60a9ead2017-06-13 20:47:28 -07003611 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003612 pkt->entry_count = 1;
3613 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3614
3615 ctio24 = (struct ctio7_to_24xx *)pkt;
3616 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003617 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003618 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003619 ctio24->vp_index = vha->vp_idx;
3620 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3621 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3622 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3623 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003624 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3625 CTIO7_FLAGS_TERMINATE;
3626 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003627 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3628 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003629
Himanshu Madhani63163e02014-09-25 06:14:59 -04003630 /* Memory Barrier */
3631 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003632 if (qpair->reqq_start_iocbs)
3633 qpair->reqq_start_iocbs(qpair);
3634 else
3635 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003636 return ret;
3637}
3638
Quinn Tran82de8022017-06-13 20:47:17 -07003639static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003640 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3641 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003642{
Quinn Tran82de8022017-06-13 20:47:17 -07003643 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003644 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003645 int rc;
3646
Quinn Tran82de8022017-06-13 20:47:17 -07003647 /* why use different vha? NPIV */
3648 if (cmd)
3649 vha = cmd->vha;
3650 else
3651 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003652
3653 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003654 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003655 if (rc == -ENOMEM)
3656 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003657 goto done;
3658 }
Quinn Tran82de8022017-06-13 20:47:17 -07003659 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3660 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003661 if (rc == -ENOMEM)
3662 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003663
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003664done:
Quinn Trana07100e2015-12-07 19:48:57 -05003665 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003666 if (cmd->sg_mapped)
3667 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003668 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3669 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003670
3671 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003672 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003673
Quinn Tran7b898542014-04-11 16:54:44 -04003674 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003675}
3676
Quinn Tran33e79972014-09-25 06:14:55 -04003677static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3678{
3679 struct list_head free_list;
3680 struct qla_tgt_cmd *cmd, *tcmd;
3681
3682 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003683 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003684
3685 cmd = tcmd = NULL;
3686 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3687 INIT_LIST_HEAD(&free_list);
3688 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3689
3690 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3691 list_del(&cmd->cmd_list);
3692 /* This cmd was never sent to TCM. There is no need
3693 * to schedule free or call free_cmd
3694 */
3695 qlt_free_cmd(cmd);
3696 vha->hw->tgt.num_qfull_cmds_alloc--;
3697 }
3698 }
3699 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3700}
3701
3702static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3703{
3704 uint32_t total_leaked;
3705
3706 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3707
3708 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3709 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3710
3711 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3712 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003713 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003714
3715 if (IS_P3P_TYPE(vha->hw))
3716 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3717 else
3718 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3719 qla2xxx_wake_dpc(vha);
3720 }
3721
3722}
3723
Quinn Trana07100e2015-12-07 19:48:57 -05003724int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003725{
3726 struct qla_tgt *tgt = cmd->tgt;
3727 struct scsi_qla_host *vha = tgt->vha;
3728 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003729 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003730
3731 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3732 "qla_target(%d): terminating exchange for aborted cmd=%p "
3733 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3734 se_cmd->tag);
3735
Quinn Trana07100e2015-12-07 19:48:57 -05003736 spin_lock_irqsave(&cmd->cmd_lock, flags);
3737 if (cmd->aborted) {
3738 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3739 /*
3740 * It's normal to see 2 calls in this path:
3741 * 1) XFER Rdy completion + CMD_T_ABORT
3742 * 2) TCM TMR - drain_state_list
3743 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003744 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3745 "multiple abort. %p transport_state %x, t_state %x, "
3746 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3747 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003748 return EIO;
3749 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003750 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003751 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003752 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003753
Quinn Tran82de8022017-06-13 20:47:17 -07003754 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003755 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003756}
3757EXPORT_SYMBOL(qlt_abort_cmd);
3758
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003759void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3760{
Quinn Tran5d964832017-01-19 22:27:59 -08003761 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003762
Quinn Tranf83adb62014-04-11 16:54:43 -04003763 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3764 "%s: se_cmd[%p] ox_id %04x\n",
3765 __func__, &cmd->se_cmd,
3766 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003767
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003768 BUG_ON(cmd->cmd_in_wq);
3769
Quinn Trana07100e2015-12-07 19:48:57 -05003770 if (cmd->sg_mapped)
3771 qlt_unmap_sg(cmd->vha, cmd);
3772
Quinn Tran33e79972014-09-25 06:14:55 -04003773 if (!cmd->q_full)
3774 qlt_decr_num_pend_cmds(cmd->vha);
3775
Quinn Tranf83adb62014-04-11 16:54:43 -04003776 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003777 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003778 if (unlikely(cmd->free_sg))
3779 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003780
3781 if (!sess || !sess->se_sess) {
3782 WARN_ON(1);
3783 return;
3784 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003785 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003786 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003787}
3788EXPORT_SYMBOL(qlt_free_cmd);
3789
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003790/*
3791 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3792 */
Quinn Tran82de8022017-06-13 20:47:17 -07003793static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003794 struct qla_tgt_cmd *cmd, uint32_t status)
3795{
3796 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003797 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003798
Quinn Tranbe251522017-03-15 09:48:49 -07003799 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003800 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003801 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3802 "se_cmd=%p tag[%x] op %#x/%s",
3803 cmd->lba, cmd->lba,
3804 cmd->num_blks, &cmd->se_cmd,
3805 cmd->atio.u.isp24.exchange_addr,
3806 cmd->se_cmd.prot_op,
3807 prot_op_str(cmd->se_cmd.prot_op));
3808
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003809 if (ctio != NULL) {
3810 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3811 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003812 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003813 } else
3814 term = 1;
3815
3816 if (term)
himanshu.madhani@cavium.com3efc31f2018-01-15 20:46:47 -08003817 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003818
3819 return term;
3820}
3821
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003822
3823/* ha->hardware_lock supposed to be held on entry */
3824static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003825 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003826{
3827 struct qla_tgt_cmd *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003828 struct req_que *req;
3829 int qid = GET_QID(handle);
3830 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003831
Quinn Tran82de8022017-06-13 20:47:17 -07003832 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3833 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003834
Quinn Tran82de8022017-06-13 20:47:17 -07003835 if (qid == rsp->req->id) {
3836 req = rsp->req;
3837 } else if (vha->hw->req_q_map[qid]) {
3838 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3839 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3840 vha->vp_idx, rsp->id, handle);
3841 req = vha->hw->req_q_map[qid];
3842 } else {
3843 return NULL;
3844 }
Arun Easi667024a2014-09-25 06:14:47 -04003845
Quinn Tran82de8022017-06-13 20:47:17 -07003846 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003847
Quinn Tran82de8022017-06-13 20:47:17 -07003848 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003849 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003850 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3851 "qla_target(%d): Wrong handle %x received\n",
3852 vha->vp_idx, handle);
3853 return NULL;
3854 }
Quinn Tran82de8022017-06-13 20:47:17 -07003855
3856 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3857 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003858 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003859 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3860 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003861 return NULL;
3862 }
Quinn Tran82de8022017-06-13 20:47:17 -07003863 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003864 } else if (ctio != NULL) {
3865 /* We can't get loop ID from CTIO7 */
3866 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3867 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3868 "support NULL handles\n", vha->vp_idx);
3869 return NULL;
3870 }
3871
3872 return cmd;
3873}
3874
Arun Easic0cb4492014-09-25 06:14:51 -04003875/* hardware_lock should be held by caller. */
Quinn Tranc5419e22017-06-13 20:47:16 -07003876void
Arun Easic0cb4492014-09-25 06:14:51 -04003877qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3878{
3879 struct qla_hw_data *ha = vha->hw;
Arun Easic0cb4492014-09-25 06:14:51 -04003880
3881 if (cmd->sg_mapped)
3882 qlt_unmap_sg(vha, cmd);
3883
Arun Easic0cb4492014-09-25 06:14:51 -04003884 /* TODO: fix debug message type and ids. */
3885 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3886 ql_dbg(ql_dbg_io, vha, 0xff00,
Quinn Tran82de8022017-06-13 20:47:17 -07003887 "HOST-ABORT: state=PROCESSED.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003888 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3889 cmd->write_data_transferred = 0;
3890 cmd->state = QLA_TGT_STATE_DATA_IN;
3891
3892 ql_dbg(ql_dbg_io, vha, 0xff01,
Quinn Tran82de8022017-06-13 20:47:17 -07003893 "HOST-ABORT: state=DATA_IN.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003894
3895 ha->tgt.tgt_ops->handle_data(cmd);
3896 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003897 } else {
3898 ql_dbg(ql_dbg_io, vha, 0xff03,
Quinn Tran82de8022017-06-13 20:47:17 -07003899 "HOST-ABORT: state=BAD(%d).\n",
Arun Easic0cb4492014-09-25 06:14:51 -04003900 cmd->state);
3901 dump_stack();
3902 }
3903
Quinn Tran1eb42f92017-01-19 22:27:55 -08003904 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003905 ha->tgt.tgt_ops->free_cmd(cmd);
3906}
3907
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003908/*
3909 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3910 */
Quinn Tran82de8022017-06-13 20:47:17 -07003911static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3912 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003913{
3914 struct qla_hw_data *ha = vha->hw;
3915 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003916 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07003917 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003918
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003919 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3920 /* That could happen only in case of an error/reset/abort */
3921 if (status != CTIO_SUCCESS) {
3922 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3923 "Intermediate CTIO received"
3924 " (status %x)\n", status);
3925 }
3926 return;
3927 }
3928
Quinn Tran82de8022017-06-13 20:47:17 -07003929 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003930 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003931 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003932
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003933 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003934 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003935
Joern Engelf9b67212014-09-16 16:23:18 -04003936 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003937
3938 if (unlikely(status != CTIO_SUCCESS)) {
3939 switch (status & 0xFFFF) {
3940 case CTIO_LIP_RESET:
3941 case CTIO_TARGET_RESET:
3942 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003943 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003944 case CTIO_TIMEOUT:
3945 case CTIO_INVALID_RX_ID:
3946 /* They are OK */
3947 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3948 "qla_target(%d): CTIO with "
3949 "status %#x received, state %x, se_cmd %p, "
3950 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3951 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3952 status, cmd->state, se_cmd);
3953 break;
3954
3955 case CTIO_PORT_LOGGED_OUT:
3956 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003957 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003958 int logged_out =
3959 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3960
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003962 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003963 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003964 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003965 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003966
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003967 if (logged_out && cmd->sess) {
3968 /*
3969 * Session is already logged out, but we need
3970 * to notify initiator, who's not aware of this
3971 */
3972 cmd->sess->logout_on_delete = 0;
3973 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07003974 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08003975 "%s %d %8phC post del sess\n",
3976 __func__, __LINE__, cmd->sess->port_name);
3977
Quinn Trand8630bb2017-12-28 12:33:43 -08003978 qlt_schedule_sess_for_deletion(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003979 }
3980 break;
3981 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003982 case CTIO_DIF_ERROR: {
3983 struct ctio_crc_from_fw *crc =
3984 (struct ctio_crc_from_fw *)ctio;
3985 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07003986 "qla_target(%d): CTIO with DIF_ERROR status %x "
3987 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3988 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003989 vha->vp_idx, status, cmd->state, se_cmd,
3990 *((u64 *)&crc->actual_dif[0]),
3991 *((u64 *)&crc->expected_dif[0]));
3992
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003993 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07003994 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04003995 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003996 default:
3997 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003998 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003999 vha->vp_idx, status, cmd->state, se_cmd);
4000 break;
4001 }
4002
Quinn Tran7b898542014-04-11 16:54:44 -04004003
Quinn Tran193b50b2015-12-17 14:57:03 -05004004 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04004005 * cmd is already aborted/terminated, we don't
4006 * need to terminate again. The exchange is already
4007 * cleaned up/freed at FW level. Just cleanup at driver
4008 * level.
4009 */
4010 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004011 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004012 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004013 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004014 return;
Quinn Tran7b898542014-04-11 16:54:44 -04004015 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004016 }
4017
4018 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004019 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004020 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004021 cmd->state = QLA_TGT_STATE_DATA_IN;
4022
Bart Van Assche52c82822015-07-09 07:23:26 -07004023 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004024 cmd->write_data_transferred = 1;
4025
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004026 ha->tgt.tgt_ops->handle_data(cmd);
4027 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05004028 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004029 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004030 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02004031 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004032 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08004033 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004034 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4035 "qla_target(%d): A command in state (%d) should "
4036 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4037 }
4038
Quinn Tran7b898542014-04-11 16:54:44 -04004039 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05004040 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004041 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4042 dump_stack();
4043 }
4044
4045 ha->tgt.tgt_ops->free_cmd(cmd);
4046}
4047
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004048static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4049 uint8_t task_codes)
4050{
4051 int fcp_task_attr;
4052
4053 switch (task_codes) {
4054 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004055 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004056 break;
4057 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004058 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004059 break;
4060 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004061 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004062 break;
4063 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004064 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004065 break;
4066 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004067 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004068 break;
4069 default:
4070 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4071 "qla_target: unknown task code %x, use ORDERED instead\n",
4072 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004073 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004074 break;
4075 }
4076
4077 return fcp_task_attr;
4078}
4079
Quinn Tran5d964832017-01-19 22:27:59 -08004080static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004081 uint8_t *);
4082/*
4083 * Process context for I/O path into tcm_qla2xxx code
4084 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004085static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004086{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004087 scsi_qla_host_t *vha = cmd->vha;
4088 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004089 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004090 struct atio_from_isp *atio = &cmd->atio;
4091 unsigned char *cdb;
4092 unsigned long flags;
4093 uint32_t data_length;
4094 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07004095 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004096
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004097 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004098 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004099
Quinn Tran193b50b2015-12-17 14:57:03 -05004100 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004101 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4102 "cmd with tag %u is aborted\n",
4103 cmd->atio.u.isp24.exchange_addr);
4104 goto out_term;
4105 }
4106
Quinn Trana07100e2015-12-07 19:48:57 -05004107 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004108 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004109 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004110
4111 if (atio->u.isp24.fcp_cmnd.rddata &&
4112 atio->u.isp24.fcp_cmnd.wrdata) {
4113 bidi = 1;
4114 data_dir = DMA_TO_DEVICE;
4115 } else if (atio->u.isp24.fcp_cmnd.rddata)
4116 data_dir = DMA_FROM_DEVICE;
4117 else if (atio->u.isp24.fcp_cmnd.wrdata)
4118 data_dir = DMA_TO_DEVICE;
4119 else
4120 data_dir = DMA_NONE;
4121
4122 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4123 atio->u.isp24.fcp_cmnd.task_attr);
Quinn Trane25f7652018-05-01 09:01:50 -07004124 data_length = get_datalen_for_atio(atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004125
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004126 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4127 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004128 if (ret != 0)
4129 goto out_term;
4130 /*
4131 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4132 */
Quinn Tran75601512015-12-17 14:57:04 -05004133 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004134 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004135 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004136 return;
4137
4138out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004139 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004140 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004141 * cmd has not sent to target yet, so pass NULL as the second
4142 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004143 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004144 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004145 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4146 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004147
4148 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004149 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Quinn Tran82de8022017-06-13 20:47:17 -07004150 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004151
4152 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004153 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004154 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004155}
4156
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004157static void qlt_do_work(struct work_struct *work)
4158{
4159 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004160 scsi_qla_host_t *vha = cmd->vha;
4161 unsigned long flags;
4162
4163 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4164 list_del(&cmd->cmd_list);
4165 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004166
4167 __qlt_do_work(cmd);
4168}
4169
Quinn Tran09620eeb2017-06-13 20:47:20 -07004170void qlt_clr_qp_table(struct scsi_qla_host *vha)
4171{
4172 unsigned long flags;
4173 struct qla_hw_data *ha = vha->hw;
4174 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4175 void *node;
4176 u64 key = 0;
4177
4178 ql_log(ql_log_info, vha, 0x706c,
4179 "User update Number of Active Qpairs %d\n",
4180 ha->tgt.num_act_qpairs);
4181
4182 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4183
4184 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4185 btree_remove64(&tgt->lun_qpair_map, key);
4186
4187 ha->base_qpair->lun_cnt = 0;
4188 for (key = 0; key < ha->max_qpairs; key++)
4189 if (ha->queue_pair_map[key])
4190 ha->queue_pair_map[key]->lun_cnt = 0;
4191
4192 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4193}
4194
Quinn Trane326d222017-06-13 20:47:18 -07004195static void qlt_assign_qpair(struct scsi_qla_host *vha,
4196 struct qla_tgt_cmd *cmd)
4197{
4198 struct qla_qpair *qpair, *qp;
4199 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4200 struct qla_qpair_hint *h;
4201
4202 if (vha->flags.qpairs_available) {
4203 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4204 if (unlikely(!h)) {
4205 /* spread lun to qpair ratio evently */
4206 int lcnt = 0, rc;
4207 struct scsi_qla_host *base_vha =
4208 pci_get_drvdata(vha->hw->pdev);
4209
4210 qpair = vha->hw->base_qpair;
4211 if (qpair->lun_cnt == 0) {
4212 qpair->lun_cnt++;
4213 h = qla_qpair_to_hint(tgt, qpair);
4214 BUG_ON(!h);
4215 rc = btree_insert64(&tgt->lun_qpair_map,
4216 cmd->unpacked_lun, h, GFP_ATOMIC);
4217 if (rc) {
4218 qpair->lun_cnt--;
4219 ql_log(ql_log_info, vha, 0xd037,
4220 "Unable to insert lun %llx into lun_qpair_map\n",
4221 cmd->unpacked_lun);
4222 }
4223 goto out;
4224 } else {
4225 lcnt = qpair->lun_cnt;
4226 }
4227
4228 h = NULL;
4229 list_for_each_entry(qp, &base_vha->qp_list,
4230 qp_list_elem) {
4231 if (qp->lun_cnt == 0) {
4232 qp->lun_cnt++;
4233 h = qla_qpair_to_hint(tgt, qp);
4234 BUG_ON(!h);
4235 rc = btree_insert64(&tgt->lun_qpair_map,
4236 cmd->unpacked_lun, h, GFP_ATOMIC);
4237 if (rc) {
4238 qp->lun_cnt--;
4239 ql_log(ql_log_info, vha, 0xd038,
4240 "Unable to insert lun %llx into lun_qpair_map\n",
4241 cmd->unpacked_lun);
4242 }
4243 qpair = qp;
4244 goto out;
4245 } else {
4246 if (qp->lun_cnt < lcnt) {
4247 lcnt = qp->lun_cnt;
4248 qpair = qp;
4249 continue;
4250 }
4251 }
4252 }
4253 BUG_ON(!qpair);
4254 qpair->lun_cnt++;
4255 h = qla_qpair_to_hint(tgt, qpair);
4256 BUG_ON(!h);
4257 rc = btree_insert64(&tgt->lun_qpair_map,
4258 cmd->unpacked_lun, h, GFP_ATOMIC);
4259 if (rc) {
4260 qpair->lun_cnt--;
4261 ql_log(ql_log_info, vha, 0xd039,
4262 "Unable to insert lun %llx into lun_qpair_map\n",
4263 cmd->unpacked_lun);
4264 }
4265 }
4266 } else {
4267 h = &tgt->qphints[0];
4268 }
4269out:
4270 cmd->qpair = h->qpair;
4271 cmd->se_cmd.cpuid = h->cpuid;
4272}
4273
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004274static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004275 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004276 struct atio_from_isp *atio)
4277{
4278 struct se_session *se_sess = sess->se_sess;
4279 struct qla_tgt_cmd *cmd;
4280 int tag;
4281
4282 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4283 if (tag < 0)
4284 return NULL;
4285
4286 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4287 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004288 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004289 memcpy(&cmd->atio, atio, sizeof(*atio));
4290 cmd->state = QLA_TGT_STATE_NEW;
4291 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004292 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004293 cmd->vha = vha;
4294 cmd->se_cmd.map_tag = tag;
4295 cmd->sess = sess;
4296 cmd->loop_id = sess->loop_id;
4297 cmd->conf_compl_supported = sess->conf_compl_supported;
4298
Quinn Tran1eb42f92017-01-19 22:27:55 -08004299 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004300 cmd->jiffies_at_alloc = get_jiffies_64();
4301
Quinn Trane326d222017-06-13 20:47:18 -07004302 cmd->unpacked_lun = scsilun_to_int(
4303 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4304 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004305 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004306 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004307
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004308 return cmd;
4309}
4310
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004311/* ha->hardware_lock supposed to be held on entry */
4312static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4313 struct atio_from_isp *atio)
4314{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004315 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004316 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004317 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004318 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004319 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08004320 port_id_t id;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004321
4322 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004323 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004324 "New command while device %p is shutting down\n", tgt);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004325 return -ENODEV;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004326 }
4327
Quinn Trana4239942017-12-28 12:33:26 -08004328 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4329 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4330 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4331 if (IS_SW_RESV_ADDR(id))
4332 return -EBUSY;
4333
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004334 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 -08004335 if (unlikely(!sess))
4336 return -EFAULT;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004337
4338 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4339 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004340 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004341 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004342 "New command while old session %p is being deleted\n",
4343 sess);
4344 return -EFAULT;
4345 }
4346
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004347 /*
4348 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4349 */
Quinn Tran726b8542017-01-19 22:28:00 -08004350 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004351 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004352 "%s: kref_get fail, %8phC oxid %x \n",
4353 __func__, sess->port_name,
4354 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4355 return -EFAULT;
4356 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004357
4358 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004359 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004360 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004361 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004362 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4363 ha->tgt.tgt_ops->put_sess(sess);
4364 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004365 return -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004366 }
4367
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004368 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004369 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004370
Quinn Tran726b8542017-01-19 22:28:00 -08004371 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004372 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004373 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004374
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004375 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004376 if (vha->flags.qpairs_available) {
4377 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4378 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004379 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4380 queue_work_on(smp_processor_id(), qla_tgt_wq,
4381 &cmd->work);
4382 else
4383 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4384 &cmd->work);
4385 } else {
4386 queue_work(qla_tgt_wq, &cmd->work);
4387 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004388
Quinn Tran82de8022017-06-13 20:47:17 -07004389 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004390}
4391
4392/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004393static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004394 int fn, void *iocb, int flags)
4395{
4396 struct scsi_qla_host *vha = sess->vha;
4397 struct qla_hw_data *ha = vha->hw;
4398 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004399 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Quinn Tran84905df2018-05-01 09:01:53 -07004400 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004401
4402 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4403 if (!mcmd) {
4404 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4405 "qla_target(%d): Allocation of management "
4406 "command failed, some commands and their data could "
4407 "leak\n", vha->vp_idx);
4408 return -ENOMEM;
4409 }
4410 memset(mcmd, 0, sizeof(*mcmd));
4411 mcmd->sess = sess;
4412
4413 if (iocb) {
4414 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4415 sizeof(mcmd->orig_iocb.imm_ntfy));
4416 }
4417 mcmd->tmr_func = fn;
4418 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004419 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran84905df2018-05-01 09:01:53 -07004420 mcmd->qpair = h->qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004421 mcmd->vha = vha;
Quinn Tran84905df2018-05-01 09:01:53 -07004422 mcmd->se_cmd.cpuid = h->cpuid;
4423 mcmd->unpacked_lun = lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004424
4425 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004426 case QLA_TGT_LUN_RESET:
Quinn Tran84905df2018-05-01 09:01:53 -07004427 case QLA_TGT_CLEAR_TS:
4428 case QLA_TGT_ABORT_TS:
4429 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4430 /* drop through */
4431 case QLA_TGT_CLEAR_ACA:
4432 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4433 mcmd->qpair = h->qpair;
4434 mcmd->se_cmd.cpuid = h->cpuid;
4435 break;
4436
4437 case QLA_TGT_TARGET_RESET:
4438 case QLA_TGT_NEXUS_LOSS_SESS:
4439 case QLA_TGT_NEXUS_LOSS:
4440 case QLA_TGT_ABORT_ALL:
4441 default:
4442 /* no-op */
4443 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004444 }
4445
Quinn Tran84905df2018-05-01 09:01:53 -07004446 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4447 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4448 &mcmd->work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004449
4450 return 0;
4451}
4452
4453/* ha->hardware_lock supposed to be held on entry */
4454static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4455{
4456 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4457 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004458 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004459 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004460 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004461 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004462
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004463 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004464
4465 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004466 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4467 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004468 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4469
Quinn Tranf775bd12017-06-02 09:11:59 -07004470 unpacked_lun =
4471 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004472
Quinn Tranfb352652017-12-28 12:33:32 -08004473 if (sess == NULL || sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004474 return -EFAULT;
4475
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004476 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4477}
4478
4479/* ha->hardware_lock supposed to be held on entry */
4480static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004481 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004482{
4483 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4484 struct qla_hw_data *ha = vha->hw;
4485 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004486 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004487 int rc;
4488
4489 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4490 if (mcmd == NULL) {
4491 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4492 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4493 vha->vp_idx, __func__);
4494 return -ENOMEM;
4495 }
4496 memset(mcmd, 0, sizeof(*mcmd));
4497
4498 mcmd->sess = sess;
4499 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4500 sizeof(mcmd->orig_iocb.imm_ntfy));
4501
Quinn Tranf775bd12017-06-02 09:11:59 -07004502 unpacked_lun =
4503 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004504 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004505 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004506 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004507
Quinn Tranbe92fc32017-01-19 22:27:54 -08004508 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004509 le16_to_cpu(iocb->u.isp2x.seq_id));
4510 if (rc != 0) {
4511 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4512 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4513 vha->vp_idx, rc);
4514 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4515 return -EFAULT;
4516 }
4517
4518 return 0;
4519}
4520
4521/* ha->hardware_lock supposed to be held on entry */
4522static int qlt_abort_task(struct scsi_qla_host *vha,
4523 struct imm_ntfy_from_isp *iocb)
4524{
4525 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004526 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004527 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004528 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004529
4530 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4531
Quinn Tran75601512015-12-17 14:57:04 -05004532 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004533 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004534 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4535
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004536 if (sess == NULL) {
4537 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4538 "qla_target(%d): task abort for unexisting "
4539 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004540 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004541 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4542 }
4543
4544 return __qlt_abort_task(vha, iocb, sess);
4545}
4546
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004547void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4548{
Quinn Tran726b8542017-01-19 22:28:00 -08004549 if (rc != MBS_COMMAND_COMPLETE) {
4550 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4551 "%s: se_sess %p / sess %p from"
4552 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4553 " LOGO failed: %#x\n",
4554 __func__,
4555 fcport->se_sess,
4556 fcport,
4557 fcport->port_name, fcport->loop_id,
4558 fcport->d_id.b.domain, fcport->d_id.b.area,
4559 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004560 }
Quinn Tran726b8542017-01-19 22:28:00 -08004561
4562 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004563}
4564
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004565/*
4566* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4567*
4568* Schedules sessions with matching port_id/loop_id but different wwn for
4569* deletion. Returns existing session with matching wwn if present.
4570* Null otherwise.
4571*/
Quinn Tran726b8542017-01-19 22:28:00 -08004572struct fc_port *
4573qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004574 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004575{
Quinn Tran5d964832017-01-19 22:27:59 -08004576 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004577 uint64_t other_wwn;
4578
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004579 *conflict_sess = NULL;
4580
Quinn Tran5d964832017-01-19 22:27:59 -08004581 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004582
4583 other_wwn = wwn_to_u64(other_sess->port_name);
4584
4585 if (wwn == other_wwn) {
4586 WARN_ON(sess);
4587 sess = other_sess;
4588 continue;
4589 }
4590
4591 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004592 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004593 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004594 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004595 "Invalidating sess %p loop_id %d wwn %llx.\n",
4596 other_sess, other_sess->loop_id, other_wwn);
4597
4598 /*
4599 * logout_on_delete is set by default, but another
4600 * session that has the same s_id/loop_id combo
4601 * might have cleared it when requested this session
4602 * deletion, so don't touch it
4603 */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004604 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004605 } else {
4606 /*
4607 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004608 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004609 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004610 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004611 "Invalidating sess %p loop_id %d wwn %llx.\n",
4612 other_sess, other_sess->loop_id, other_wwn);
4613
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004614 other_sess->keep_nport_handle = 1;
Quinn Tranba743f92017-12-04 14:45:12 -08004615 if (other_sess->disc_state != DSC_DELETED)
4616 *conflict_sess = other_sess;
Quinn Tran94cff6e2017-12-28 12:33:42 -08004617 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004618 }
4619 continue;
4620 }
4621
4622 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004623 if ((loop_id == other_sess->loop_id) &&
4624 (loop_id != FC_NO_LOOP_ID)) {
4625 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004626 "Invalidating sess %p loop_id %d wwn %llx.\n",
4627 other_sess, other_sess->loop_id, other_wwn);
4628
4629 /* Same loop_id but different s_id
4630 * Ok to kill and logout */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004631 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004632 }
4633 }
4634
4635 return sess;
4636}
4637
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004638/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4639static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4640{
4641 struct qla_tgt_sess_op *op;
4642 struct qla_tgt_cmd *cmd;
4643 uint32_t key;
4644 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004645 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004646
4647 key = (((u32)s_id->b.domain << 16) |
4648 ((u32)s_id->b.area << 8) |
4649 ((u32)s_id->b.al_pa));
4650
Quinn Tran8b631d82017-06-02 09:11:54 -07004651 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004652 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4653 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004654
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004655 if (op_key == key) {
4656 op->aborted = true;
4657 count++;
4658 }
4659 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004660
4661 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4662 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4663 if (op_key == key) {
4664 op->aborted = true;
4665 count++;
4666 }
4667 }
4668
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004669 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4670 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4671 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004672 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004673 count++;
4674 }
4675 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004676 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004677
4678 return count;
4679}
4680
Quinn Tran9cd883f2017-12-28 12:33:24 -08004681static int qlt_handle_login(struct scsi_qla_host *vha,
4682 struct imm_ntfy_from_isp *iocb)
4683{
4684 struct fc_port *sess = NULL, *conflict_sess = NULL;
4685 uint64_t wwn;
4686 port_id_t port_id;
4687 uint16_t loop_id, wd3_lo;
4688 int res = 0;
4689 struct qlt_plogi_ack_t *pla;
4690 unsigned long flags;
4691
4692 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4693
4694 port_id.b.domain = iocb->u.isp24.port_id[2];
4695 port_id.b.area = iocb->u.isp24.port_id[1];
4696 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4697 port_id.b.rsvd_1 = 0;
4698
4699 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4700
4701 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4702 abort_cmds_for_s_id(vha, &port_id);
4703
4704 if (wwn) {
4705 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4706 sess = qlt_find_sess_invalidate_other(vha, wwn,
4707 port_id, loop_id, &conflict_sess);
4708 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4709 }
4710
4711 if (IS_SW_RESV_ADDR(port_id)) {
4712 res = 1;
4713 goto out;
4714 }
4715
4716 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4717 if (!pla) {
4718 qlt_send_term_imm_notif(vha, iocb, 1);
4719 goto out;
4720 }
4721
4722 if (conflict_sess) {
4723 conflict_sess->login_gen++;
4724 qlt_plogi_ack_link(vha, pla, conflict_sess,
4725 QLT_PLOGI_LINK_CONFLICT);
4726 }
4727
4728 if (!sess) {
4729 pla->ref_count++;
4730 ql_dbg(ql_dbg_disc, vha, 0xffff,
4731 "%s %d %8phC post new sess\n",
4732 __func__, __LINE__, iocb->u.isp24.port_name);
Quinn Trana4239942017-12-28 12:33:26 -08004733 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4734 qla24xx_post_newsess_work(vha, &port_id,
4735 iocb->u.isp24.port_name,
4736 iocb->u.isp24.u.plogi.node_name,
4737 pla, FC4_TYPE_UNKNOWN);
4738 else
4739 qla24xx_post_newsess_work(vha, &port_id,
4740 iocb->u.isp24.port_name, NULL,
4741 pla, FC4_TYPE_UNKNOWN);
4742
Quinn Tran9cd883f2017-12-28 12:33:24 -08004743 goto out;
4744 }
4745
4746 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4747 sess->d_id = port_id;
4748 sess->login_gen++;
4749
4750 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4751 sess->fw_login_state = DSC_LS_PRLI_PEND;
4752 sess->local = 0;
4753 sess->loop_id = loop_id;
4754 sess->d_id = port_id;
4755 sess->fw_login_state = DSC_LS_PRLI_PEND;
4756 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4757
4758 if (wd3_lo & BIT_7)
4759 sess->conf_compl_supported = 1;
4760
4761 if ((wd3_lo & BIT_4) == 0)
4762 sess->port_type = FCT_INITIATOR;
4763 else
4764 sess->port_type = FCT_TARGET;
4765
4766 } else
4767 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4768
4769
4770 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4771 "%s %d %8phC DS %d\n",
4772 __func__, __LINE__, sess->port_name, sess->disc_state);
4773
4774 switch (sess->disc_state) {
4775 case DSC_DELETED:
4776 qlt_plogi_ack_unref(vha, pla);
4777 break;
4778
4779 default:
4780 /*
4781 * Under normal circumstances we want to release nport handle
4782 * during LOGO process to avoid nport handle leaks inside FW.
4783 * The exception is when LOGO is done while another PLOGI with
4784 * the same nport handle is waiting as might be the case here.
4785 * Note: there is always a possibily of a race where session
4786 * deletion has already started for other reasons (e.g. ACL
4787 * removal) and now PLOGI arrives:
4788 * 1. if PLOGI arrived in FW after nport handle has been freed,
4789 * FW must have assigned this PLOGI a new/same handle and we
4790 * can proceed ACK'ing it as usual when session deletion
4791 * completes.
4792 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4793 * bit reached it, the handle has now been released. We'll
4794 * get an error when we ACK this PLOGI. Nothing will be sent
4795 * back to initiator. Initiator should eventually retry
4796 * PLOGI and situation will correct itself.
4797 */
4798 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4799 (sess->d_id.b24 == port_id.b24));
4800
4801 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4802 "%s %d %8phC post del sess\n",
4803 __func__, __LINE__, sess->port_name);
4804
4805
Quinn Trand8630bb2017-12-28 12:33:43 -08004806 qlt_schedule_sess_for_deletion(sess);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004807 break;
4808 }
4809out:
4810 return res;
4811}
4812
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004813/*
4814 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4815 */
4816static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4817 struct imm_ntfy_from_isp *iocb)
4818{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004819 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004820 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004821 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004822 uint64_t wwn;
4823 port_id_t port_id;
4824 uint16_t loop_id;
4825 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004826 int res = 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004827 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004828
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004829 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4830
4831 port_id.b.domain = iocb->u.isp24.port_id[2];
4832 port_id.b.area = iocb->u.isp24.port_id[1];
4833 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4834 port_id.b.rsvd_1 = 0;
4835
4836 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4837
Quinn Tran726b8542017-01-19 22:28:00 -08004838 ql_dbg(ql_dbg_disc, vha, 0xf026,
4839 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4840 vha->vp_idx, iocb->u.isp24.port_id[2],
4841 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4842 iocb->u.isp24.status_subcode, loop_id,
4843 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004844
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004845 /* res = 1 means ack at the end of thread
4846 * res = 0 means ack async/later.
4847 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004848 switch (iocb->u.isp24.status_subcode) {
4849 case ELS_PLOGI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004850 res = qlt_handle_login(vha, iocb);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004851 break;
4852
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004853 case ELS_PRLI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004854 if (N2N_TOPO(ha)) {
4855 sess = qla2x00_find_fcport_by_wwpn(vha,
4856 iocb->u.isp24.port_name, 1);
4857
4858 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4859 ql_dbg(ql_dbg_disc, vha, 0xffff,
4860 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4861 __func__, __LINE__,
4862 iocb->u.isp24.port_name);
4863 qlt_send_term_imm_notif(vha, iocb, 1);
4864 break;
4865 }
4866
4867 res = qlt_handle_login(vha, iocb);
4868 break;
4869 }
4870
Quinn Trana4239942017-12-28 12:33:26 -08004871 if (IS_SW_RESV_ADDR(port_id)) {
4872 res = 1;
4873 break;
4874 }
4875
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004876 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4877
Quinn Tran75601512015-12-17 14:57:04 -05004878 if (wwn) {
4879 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004880 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4881 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004882 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4883 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004884
4885 if (conflict_sess) {
Quinn Tran5ef696a2017-12-04 14:45:05 -08004886 switch (conflict_sess->disc_state) {
4887 case DSC_DELETED:
4888 case DSC_DELETE_PEND:
4889 break;
4890 default:
4891 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4892 "PRLI with conflicting sess %p port %8phC\n",
4893 conflict_sess, conflict_sess->port_name);
4894 conflict_sess->fw_login_state =
4895 DSC_LS_PORT_UNAVAIL;
4896 qlt_send_term_imm_notif(vha, iocb, 1);
4897 res = 0;
4898 break;
4899 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004900 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004901
4902 if (sess != NULL) {
Quinn Trana4239942017-12-28 12:33:26 -08004903 bool delete = false;
Quinn Tran82abdca2017-12-28 12:33:22 -08004904 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4905 switch (sess->fw_login_state) {
Quinn Trana4239942017-12-28 12:33:26 -08004906 case DSC_LS_PLOGI_PEND:
Quinn Tran82abdca2017-12-28 12:33:22 -08004907 case DSC_LS_PLOGI_COMP:
4908 case DSC_LS_PRLI_COMP:
4909 break;
4910 default:
Quinn Trana4239942017-12-28 12:33:26 -08004911 delete = true;
4912 break;
4913 }
4914
4915 switch (sess->disc_state) {
4916 case DSC_LOGIN_PEND:
4917 case DSC_GPDB:
Quinn Trana4239942017-12-28 12:33:26 -08004918 case DSC_UPD_FCPORT:
4919 case DSC_LOGIN_COMPLETE:
4920 case DSC_ADISC:
4921 delete = false;
4922 break;
4923 default:
4924 break;
4925 }
4926
4927 if (delete) {
Quinn Tran82abdca2017-12-28 12:33:22 -08004928 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4929 flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004930 /*
4931 * Impatient initiator sent PRLI before last
4932 * PLOGI could finish. Will force him to re-try,
4933 * while last one finishes.
4934 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004935 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004936 "sess %p PRLI received, before plogi ack.\n",
4937 sess);
4938 qlt_send_term_imm_notif(vha, iocb, 1);
4939 res = 0;
4940 break;
4941 }
4942
4943 /*
4944 * This shouldn't happen under normal circumstances,
4945 * since we have deleted the old session during PLOGI
4946 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004947 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004948 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4949 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4950
4951 sess->local = 0;
4952 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08004953 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004954 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004955
4956 if (wd3_lo & BIT_7)
4957 sess->conf_compl_supported = 1;
4958
Quinn Tran726b8542017-01-19 22:28:00 -08004959 if ((wd3_lo & BIT_4) == 0)
4960 sess->port_type = FCT_INITIATOR;
4961 else
4962 sess->port_type = FCT_TARGET;
Quinn Tran82abdca2017-12-28 12:33:22 -08004963
4964 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04004965 }
4966 res = 1; /* send notify ack */
4967
4968 /* Make session global (not used in fabric mode) */
4969 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004970 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004971 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07004972 "%s %d %8phC post nack\n",
4973 __func__, __LINE__, sess->port_name);
4974 qla24xx_post_nack_work(vha, sess, iocb,
4975 SRB_NACK_PRLI);
4976 res = 0;
4977 } else {
4978 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4979 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4980 qla2xxx_wake_dpc(vha);
4981 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004982 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08004983 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004984 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07004985 "%s %d %8phC post nack\n",
4986 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004987 qla24xx_post_nack_work(vha, sess, iocb,
4988 SRB_NACK_PRLI);
4989 res = 0;
4990 }
4991 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004992 break;
4993
Quinn Tran41dc5292017-01-19 22:28:03 -08004994 case ELS_TPRLO:
4995 if (le16_to_cpu(iocb->u.isp24.flags) &
4996 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4997 loop_id = 0xFFFF;
4998 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4999 res = 1;
5000 break;
5001 }
Bart Van Assche81881862017-12-07 16:02:46 -08005002 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005003 case ELS_LOGO:
5004 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08005005 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5006 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5007 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5008
5009 if (sess) {
5010 sess->login_gen++;
5011 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08005012 sess->logo_ack_needed = 1;
5013 memcpy(sess->iocb, iocb, IOCB_SIZE);
5014 }
5015
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005016 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08005017
Quinn Tran83548fe2017-06-02 09:12:01 -07005018 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08005019 "%s: logo %llx res %d sess %p ",
5020 __func__, wwn, res, sess);
5021 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08005022 /*
5023 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5024 * for LOGO_ACK & sess delete
5025 */
Quinn Tran726b8542017-01-19 22:28:00 -08005026 BUG_ON(!sess);
5027 res = 0;
5028 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08005029 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08005030 if (sess) {
Quinn Trand8630bb2017-12-28 12:33:43 -08005031 qlt_schedule_sess_for_deletion(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08005032 res = 0;
5033 }
5034 /* else logo will be ack */
5035 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005036 break;
5037 case ELS_PDISC:
5038 case ELS_ADISC:
5039 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005040 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005041 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005042 qlt_send_notify_ack(ha->base_qpair,
5043 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005044 tgt->link_reinit_iocb_pending = 0;
5045 }
Quinn Tran726b8542017-01-19 22:28:00 -08005046
5047 sess = qla2x00_find_fcport_by_wwpn(vha,
5048 iocb->u.isp24.port_name, 1);
5049 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005050 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08005051 "sess %p lid %d|%d DS %d LS %d\n",
5052 sess, sess->loop_id, loop_id,
5053 sess->disc_state, sess->fw_login_state);
5054 }
5055
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005056 res = 1; /* send notify ack */
5057 break;
5058 }
5059
Alexei Potashnika6ca8872015-07-14 16:00:44 -04005060 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005061 default:
5062 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5063 "qla_target(%d): Unsupported ELS command %x "
5064 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5065 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5066 break;
5067 }
5068
Quinn Tran9cd883f2017-12-28 12:33:24 -08005069 ql_dbg(ql_dbg_disc, vha, 0xf026,
5070 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5071 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5072
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005073 return res;
5074}
5075
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005076/*
5077 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5078 */
5079static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5080 struct imm_ntfy_from_isp *iocb)
5081{
5082 struct qla_hw_data *ha = vha->hw;
5083 uint32_t add_flags = 0;
5084 int send_notify_ack = 1;
5085 uint16_t status;
5086
5087 status = le16_to_cpu(iocb->u.isp2x.status);
5088 switch (status) {
5089 case IMM_NTFY_LIP_RESET:
5090 {
5091 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5092 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5093 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5094 iocb->u.isp24.status_subcode);
5095
5096 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5097 send_notify_ack = 0;
5098 break;
5099 }
5100
5101 case IMM_NTFY_LIP_LINK_REINIT:
5102 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005103 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005104 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5105 "qla_target(%d): LINK REINIT (loop %#x, "
5106 "subcode %x)\n", vha->vp_idx,
5107 le16_to_cpu(iocb->u.isp24.nport_handle),
5108 iocb->u.isp24.status_subcode);
5109 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005110 qlt_send_notify_ack(ha->base_qpair,
5111 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005112 }
5113 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5114 tgt->link_reinit_iocb_pending = 1;
5115 /*
5116 * QLogic requires to wait after LINK REINIT for possible
5117 * PDISC or ADISC ELS commands
5118 */
5119 send_notify_ack = 0;
5120 break;
5121 }
5122
5123 case IMM_NTFY_PORT_LOGOUT:
5124 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5125 "qla_target(%d): Port logout (loop "
5126 "%#x, subcode %x)\n", vha->vp_idx,
5127 le16_to_cpu(iocb->u.isp24.nport_handle),
5128 iocb->u.isp24.status_subcode);
5129
5130 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5131 send_notify_ack = 0;
5132 /* The sessions will be cleared in the callback, if needed */
5133 break;
5134
5135 case IMM_NTFY_GLBL_TPRLO:
5136 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5137 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5138 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5139 send_notify_ack = 0;
5140 /* The sessions will be cleared in the callback, if needed */
5141 break;
5142
5143 case IMM_NTFY_PORT_CONFIG:
5144 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5145 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5146 status);
5147 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5148 send_notify_ack = 0;
5149 /* The sessions will be cleared in the callback, if needed */
5150 break;
5151
5152 case IMM_NTFY_GLBL_LOGO:
5153 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5154 "qla_target(%d): Link failure detected\n",
5155 vha->vp_idx);
5156 /* I_T nexus loss */
5157 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5158 send_notify_ack = 0;
5159 break;
5160
5161 case IMM_NTFY_IOCB_OVERFLOW:
5162 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5163 "qla_target(%d): Cannot provide requested "
5164 "capability (IOCB overflowed the immediate notify "
5165 "resource count)\n", vha->vp_idx);
5166 break;
5167
5168 case IMM_NTFY_ABORT_TASK:
5169 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5170 "qla_target(%d): Abort Task (S %08x I %#x -> "
5171 "L %#x)\n", vha->vp_idx,
5172 le16_to_cpu(iocb->u.isp2x.seq_id),
5173 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5174 le16_to_cpu(iocb->u.isp2x.lun));
5175 if (qlt_abort_task(vha, iocb) == 0)
5176 send_notify_ack = 0;
5177 break;
5178
5179 case IMM_NTFY_RESOURCE:
5180 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5181 "qla_target(%d): Out of resources, host %ld\n",
5182 vha->vp_idx, vha->host_no);
5183 break;
5184
5185 case IMM_NTFY_MSG_RX:
5186 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5187 "qla_target(%d): Immediate notify task %x\n",
5188 vha->vp_idx, iocb->u.isp2x.task_flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005189 break;
5190
5191 case IMM_NTFY_ELS:
5192 if (qlt_24xx_handle_els(vha, iocb) == 0)
5193 send_notify_ack = 0;
5194 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005195 default:
5196 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5197 "qla_target(%d): Received unknown immediate "
5198 "notify status %x\n", vha->vp_idx, status);
5199 break;
5200 }
5201
5202 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005203 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5204 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005205}
5206
5207/*
5208 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5209 * This function sends busy to ISP 2xxx or 24xx.
5210 */
Quinn Tran82de8022017-06-13 20:47:17 -07005211static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005212 struct atio_from_isp *atio, uint16_t status)
5213{
Quinn Tran82de8022017-06-13 20:47:17 -07005214 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005215 struct ctio7_to_24xx *ctio24;
5216 struct qla_hw_data *ha = vha->hw;
5217 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005218 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005219 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005220 u16 temp;
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005221 port_id_t id;
5222
5223 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5224 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5225 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5226 id.b.rsvd_1 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005227
Quinn Tran75601512015-12-17 14:57:04 -05005228 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005229 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
Quinn Tran75601512015-12-17 14:57:04 -05005230 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005231 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005232 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005233 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005234 }
5235 /* Sending marker isn't necessary, since we called from ISR */
5236
Quinn Tran82de8022017-06-13 20:47:17 -07005237 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005238 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005239 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005240 "qla_target(%d): %s failed: unable to allocate "
5241 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005242 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005243 }
5244
Quinn Tran60a9ead2017-06-13 20:47:28 -07005245 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005246 pkt->entry_count = 1;
5247 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5248
5249 ctio24 = (struct ctio7_to_24xx *)pkt;
5250 ctio24->entry_type = CTIO_TYPE7;
5251 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005252 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005253 ctio24->vp_index = vha->vp_idx;
5254 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5255 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5256 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5257 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005258 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005259 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005260 CTIO7_FLAGS_DONT_RET_CTIO;
5261 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005262 /*
5263 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5264 * if the explicit conformation is used.
5265 */
5266 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5267 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Quinn Trane25f7652018-05-01 09:01:50 -07005268
5269 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5270
5271 if (ctio24->u.status1.residual != 0)
5272 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5273
Himanshu Madhani63163e02014-09-25 06:14:59 -04005274 /* Memory Barrier */
5275 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005276 if (qpair->reqq_start_iocbs)
5277 qpair->reqq_start_iocbs(qpair);
5278 else
5279 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005280 return 0;
5281}
5282
5283/*
5284 * This routine is used to allocate a command for either a QFull condition
5285 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5286 * out previously.
5287 */
5288static void
5289qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5290 struct atio_from_isp *atio, uint16_t status, int qfull)
5291{
5292 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5293 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005294 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005295 struct se_session *se_sess;
5296 struct qla_tgt_cmd *cmd;
5297 int tag;
Quinn Tran82de8022017-06-13 20:47:17 -07005298 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005299
5300 if (unlikely(tgt->tgt_stop)) {
5301 ql_dbg(ql_dbg_io, vha, 0x300a,
5302 "New command while device %p is shutting down\n", tgt);
5303 return;
5304 }
5305
5306 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5307 vha->hw->tgt.num_qfull_cmds_dropped++;
5308 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005309 vha->qla_stats.stat_max_qfull_cmds_dropped)
5310 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005311 vha->hw->tgt.num_qfull_cmds_dropped;
5312
5313 ql_dbg(ql_dbg_io, vha, 0x3068,
5314 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5315 vha->vp_idx, __func__,
5316 vha->hw->tgt.num_qfull_cmds_dropped);
5317
5318 qlt_chk_exch_leak_thresh_hold(vha);
5319 return;
5320 }
5321
5322 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5323 (vha, atio->u.isp24.fcp_hdr.s_id);
5324 if (!sess)
5325 return;
5326
5327 se_sess = sess->se_sess;
5328
5329 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5330 if (tag < 0)
5331 return;
5332
5333 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5334 if (!cmd) {
5335 ql_dbg(ql_dbg_io, vha, 0x3009,
5336 "qla_target(%d): %s: Allocation of cmd failed\n",
5337 vha->vp_idx, __func__);
5338
5339 vha->hw->tgt.num_qfull_cmds_dropped++;
5340 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005341 vha->qla_stats.stat_max_qfull_cmds_dropped)
5342 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005343 vha->hw->tgt.num_qfull_cmds_dropped;
5344
5345 qlt_chk_exch_leak_thresh_hold(vha);
5346 return;
5347 }
5348
5349 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5350
5351 qlt_incr_num_pend_cmds(vha);
5352 INIT_LIST_HEAD(&cmd->cmd_list);
5353 memcpy(&cmd->atio, atio, sizeof(*atio));
5354
5355 cmd->tgt = vha->vha_tgt.qla_tgt;
5356 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005357 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005358 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005359 cmd->qpair = ha->base_qpair;
Quinn Tran33e79972014-09-25 06:14:55 -04005360
5361 if (qfull) {
5362 cmd->q_full = 1;
5363 /* NOTE: borrowing the state field to carry the status */
5364 cmd->state = status;
5365 } else
5366 cmd->term_exchg = 1;
5367
Quinn Tran82de8022017-06-13 20:47:17 -07005368 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005369 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5370
5371 vha->hw->tgt.num_qfull_cmds_alloc++;
5372 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005373 vha->qla_stats.stat_max_qfull_cmds_alloc)
5374 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005375 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005376 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005377}
5378
5379int
Quinn Tran82de8022017-06-13 20:47:17 -07005380qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005381{
Quinn Tran82de8022017-06-13 20:47:17 -07005382 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005383 struct qla_hw_data *ha = vha->hw;
5384 unsigned long flags;
5385 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005386 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005387 int rc = 0;
5388
5389 if (list_empty(&ha->tgt.q_full_list))
5390 return 0;
5391
5392 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005393 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005394
Quinn Tran82de8022017-06-13 20:47:17 -07005395 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005396 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005397 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005398 return 0;
5399 }
5400
Quinn Tran82de8022017-06-13 20:47:17 -07005401 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5402 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5403
5404 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5405 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005406 if (cmd->q_full)
5407 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005408 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005409 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005410 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005411
5412 if (rc == -ENOMEM)
5413 break;
5414
5415 if (cmd->q_full)
5416 ql_dbg(ql_dbg_io, vha, 0x3006,
5417 "%s: busy sent for ox_id[%04x]\n", __func__,
5418 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5419 else if (cmd->term_exchg)
5420 ql_dbg(ql_dbg_io, vha, 0x3007,
5421 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5422 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5423 else
5424 ql_dbg(ql_dbg_io, vha, 0x3008,
5425 "%s: Unexpected cmd in QFull list %p\n", __func__,
5426 cmd);
5427
5428 list_del(&cmd->cmd_list);
5429 list_add_tail(&cmd->cmd_list, &free_list);
5430
5431 /* piggy back on hardware_lock for protection */
5432 vha->hw->tgt.num_qfull_cmds_alloc--;
5433 }
Quinn Tran82de8022017-06-13 20:47:17 -07005434 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005435
5436 cmd = NULL;
5437
5438 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5439 list_del(&cmd->cmd_list);
5440 /* This cmd was never sent to TCM. There is no need
5441 * to schedule free or call free_cmd
5442 */
5443 qlt_free_cmd(cmd);
5444 }
Quinn Tran82de8022017-06-13 20:47:17 -07005445
5446 if (!list_empty(&q_full_list)) {
5447 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5448 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5449 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5450 }
5451
Quinn Tran33e79972014-09-25 06:14:55 -04005452 return rc;
5453}
5454
5455static void
Quinn Tran82de8022017-06-13 20:47:17 -07005456qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5457 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005458{
5459 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005460 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005461
Quinn Tran82de8022017-06-13 20:47:17 -07005462 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005463 if (rc == -ENOMEM)
5464 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5465}
5466
5467static int
Quinn Tran82de8022017-06-13 20:47:17 -07005468qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5469 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005470{
5471 struct qla_hw_data *ha = vha->hw;
Quinn Tran8b666802017-03-15 09:48:45 -07005472 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005473
5474 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5475 return 0;
5476
Quinn Tran8b666802017-03-15 09:48:45 -07005477 if (!ha_locked)
5478 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005479 qlt_send_busy(qpair, atio, qla_sam_status);
Quinn Tran8b666802017-03-15 09:48:45 -07005480 if (!ha_locked)
5481 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5482
Quinn Tran33e79972014-09-25 06:14:55 -04005483 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005484}
5485
5486/* ha->hardware_lock supposed to be held on entry */
5487/* called via callback from qla2xxx */
5488static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005489 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005490{
5491 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005492 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005493 int rc;
Quinn Tran7cf95f72017-12-28 12:33:28 -08005494 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005495
5496 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005497 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005498 "ATIO pkt, but no tgt (ha %p)", ha);
5499 return;
5500 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005501 /*
5502 * In tgt_stop mode we also should allow all requests to pass.
5503 * Otherwise, some commands can stuck.
5504 */
5505
Quinn Tran2f424b92015-12-17 14:57:07 -05005506 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005507
5508 switch (atio->u.raw.entry_type) {
5509 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005510 if (unlikely(atio->u.isp24.exchange_addr ==
5511 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005512 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005513 "qla_target(%d): ATIO_TYPE7 "
5514 "received with UNKNOWN exchange address, "
5515 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005516 if (!ha_locked)
5517 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005518 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
Quinn Tran2f424b92015-12-17 14:57:07 -05005519 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005520 spin_unlock_irqrestore(&ha->hardware_lock,
5521 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005522 break;
5523 }
Quinn Tran33e79972014-09-25 06:14:55 -04005524
Quinn Tran33e79972014-09-25 06:14:55 -04005525 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005526 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5527 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005528 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005529 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005530 return;
5531 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005532 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005533 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005534 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005535 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005536 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005537 if (!ha_locked)
5538 spin_lock_irqsave(&ha->hardware_lock, flags);
5539 switch (rc) {
5540 case -ENODEV:
5541 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5542 "qla_target: Unable to send command to target\n");
5543 break;
5544 case -EBADF:
5545 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5546 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005547 qlt_send_term_exchange(ha->base_qpair, NULL,
5548 atio, 1, 0);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005549 break;
5550 case -EBUSY:
5551 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5552 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5553 vha->vp_idx);
5554 qlt_send_busy(ha->base_qpair, atio,
5555 tc_sam_status);
5556 break;
5557 default:
5558 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5559 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5560 vha->vp_idx);
5561 qlt_send_busy(ha->base_qpair, atio,
5562 qla_sam_status);
5563 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005564 }
Quinn Tran7cf95f72017-12-28 12:33:28 -08005565 if (!ha_locked)
5566 spin_unlock_irqrestore(&ha->hardware_lock,
5567 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005568 }
5569 break;
5570
5571 case IMMED_NOTIFY_TYPE:
5572 {
5573 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5574 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5575 "qla_target(%d): Received ATIO packet %x "
5576 "with error status %x\n", vha->vp_idx,
5577 atio->u.raw.entry_type,
5578 atio->u.isp2x.entry_status);
5579 break;
5580 }
5581 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005582
5583 if (!ha_locked)
5584 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005585 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005586 if (!ha_locked)
5587 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005588 break;
5589 }
5590
5591 default:
5592 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5593 "qla_target(%d): Received unknown ATIO atio "
5594 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5595 break;
5596 }
5597
Quinn Tran2f424b92015-12-17 14:57:07 -05005598 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005599}
5600
5601/* ha->hardware_lock supposed to be held on entry */
5602/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005603static void qlt_response_pkt(struct scsi_qla_host *vha,
5604 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005605{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005606 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005607
5608 if (unlikely(tgt == NULL)) {
5609 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005610 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5611 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005612 return;
5613 }
5614
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005615 /*
5616 * In tgt_stop mode we also should allow all requests to pass.
5617 * Otherwise, some commands can stuck.
5618 */
5619
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005620 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005621 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005622 case CTIO_TYPE7:
5623 {
5624 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005625 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005626 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5627 entry);
5628 break;
5629 }
5630
5631 case ACCEPT_TGT_IO_TYPE:
5632 {
5633 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5634 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005635 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005636 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005637 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5638 "qla_target(%d): ATIO with error "
5639 "status %x received\n", vha->vp_idx,
5640 le16_to_cpu(atio->u.isp2x.status));
5641 break;
5642 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005643
Quinn Tran82de8022017-06-13 20:47:17 -07005644 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005645 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005646 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005647
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005648 rc = qlt_handle_cmd_for_atio(vha, atio);
5649 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005650 switch (rc) {
5651 case -ENODEV:
5652 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5653 "qla_target: Unable to send command to target\n");
5654 break;
5655 case -EBADF:
5656 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5657 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5658 qlt_send_term_exchange(rsp->qpair, NULL,
5659 atio, 1, 0);
5660 break;
5661 case -EBUSY:
5662 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5663 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5664 vha->vp_idx);
5665 qlt_send_busy(rsp->qpair, atio,
5666 tc_sam_status);
5667 break;
5668 default:
5669 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5670 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5671 vha->vp_idx);
5672 qlt_send_busy(rsp->qpair, atio,
5673 qla_sam_status);
5674 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005675 }
5676 }
5677 }
5678 break;
5679
5680 case CONTINUE_TGT_IO_TYPE:
5681 {
5682 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005683 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005684 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5685 entry);
5686 break;
5687 }
5688
5689 case CTIO_A64_TYPE:
5690 {
5691 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005692 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005693 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5694 entry);
5695 break;
5696 }
5697
5698 case IMMED_NOTIFY_TYPE:
5699 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5700 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5701 break;
5702
5703 case NOTIFY_ACK_TYPE:
5704 if (tgt->notify_ack_expected > 0) {
5705 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5706 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5707 "NOTIFY_ACK seq %08x status %x\n",
5708 le16_to_cpu(entry->u.isp2x.seq_id),
5709 le16_to_cpu(entry->u.isp2x.status));
5710 tgt->notify_ack_expected--;
5711 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005712 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005713 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5714 "qla_target(%d): NOTIFY_ACK "
5715 "failed %x\n", vha->vp_idx,
5716 le16_to_cpu(entry->u.isp2x.status));
5717 }
5718 } else {
5719 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5720 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5721 vha->vp_idx);
5722 }
5723 break;
5724
5725 case ABTS_RECV_24XX:
5726 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5727 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5728 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5729 break;
5730
5731 case ABTS_RESP_24XX:
5732 if (tgt->abts_resp_expected > 0) {
5733 struct abts_resp_from_24xx_fw *entry =
5734 (struct abts_resp_from_24xx_fw *)pkt;
5735 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5736 "ABTS_RESP_24XX: compl_status %x\n",
5737 entry->compl_status);
5738 tgt->abts_resp_expected--;
5739 if (le16_to_cpu(entry->compl_status) !=
5740 ABTS_RESP_COMPL_SUCCESS) {
5741 if ((entry->error_subcode1 == 0x1E) &&
5742 (entry->error_subcode2 == 0)) {
5743 /*
5744 * We've got a race here: aborted
5745 * exchange not terminated, i.e.
5746 * response for the aborted command was
5747 * sent between the abort request was
5748 * received and processed.
5749 * Unfortunately, the firmware has a
5750 * silly requirement that all aborted
5751 * exchanges must be explicitely
5752 * terminated, otherwise it refuses to
5753 * send responses for the abort
5754 * requests. So, we have to
5755 * (re)terminate the exchange and retry
5756 * the abort response.
5757 */
5758 qlt_24xx_retry_term_exchange(vha,
5759 entry);
5760 } else
5761 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5762 "qla_target(%d): ABTS_RESP_24XX "
5763 "failed %x (subcode %x:%x)",
5764 vha->vp_idx, entry->compl_status,
5765 entry->error_subcode1,
5766 entry->error_subcode2);
5767 }
5768 } else {
5769 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5770 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5771 "received\n", vha->vp_idx);
5772 }
5773 break;
5774
5775 default:
5776 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5777 "qla_target(%d): Received unknown response pkt "
5778 "type %x\n", vha->vp_idx, pkt->entry_type);
5779 break;
5780 }
5781
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005782}
5783
5784/*
5785 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5786 */
5787void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5788 uint16_t *mailbox)
5789{
5790 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005791 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005792 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005793
Quinn Tran3a33dc92017-06-02 09:12:04 -07005794 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005795 return;
5796
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005797 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5798 IS_QLA2100(ha))
5799 return;
5800 /*
5801 * In tgt_stop mode we also should allow all requests to pass.
5802 * Otherwise, some commands can stuck.
5803 */
5804
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005805
5806 switch (code) {
5807 case MBA_RESET: /* Reset */
5808 case MBA_SYSTEM_ERR: /* System Error */
5809 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5810 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5811 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5812 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005813 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005814 break;
5815 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5816 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5817 break;
5818
5819 case MBA_LOOP_UP:
5820 {
5821 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005822 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005823 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5824 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5825 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005826 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005827 qlt_send_notify_ack(ha->base_qpair,
5828 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005829 0, 0, 0, 0, 0, 0);
5830 tgt->link_reinit_iocb_pending = 0;
5831 }
5832 break;
5833 }
5834
5835 case MBA_LIP_OCCURRED:
5836 case MBA_LOOP_DOWN:
5837 case MBA_LIP_RESET:
5838 case MBA_RSCN_UPDATE:
5839 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005840 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005841 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5842 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5843 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005844 break;
5845
Quinn Tranead03852017-01-19 22:28:01 -08005846 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005847 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5848 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5849 vha->vp_idx,
5850 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5851 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005852
5853 if (le16_to_cpu(mailbox[3]) == 1) {
5854 /* exchange starvation. */
5855 vha->hw->exch_starvation++;
5856 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005857 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08005858 "Exchange starvation-. Resetting RISC\n");
5859
5860 vha->hw->exch_starvation = 0;
5861 if (IS_P3P_TYPE(vha->hw))
5862 set_bit(FCOE_CTX_RESET_NEEDED,
5863 &vha->dpc_flags);
5864 else
5865 set_bit(ISP_ABORT_NEEDED,
5866 &vha->dpc_flags);
5867 qla2xxx_wake_dpc(vha);
5868 }
5869 }
5870 break;
5871
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005872 case MBA_PORT_UPDATE:
5873 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5874 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005875 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005876 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5877 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5878 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5879
5880 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08005881 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005882 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5883 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08005884 vha->hw->exch_starvation = 0;
5885 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005886 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5887 "Async MB 2: Port Logged Out\n");
5888 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005889 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005890 break;
5891 }
5892
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005893}
5894
5895static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5896 uint16_t loop_id)
5897{
Quinn Tran726b8542017-01-19 22:28:00 -08005898 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005899 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08005900 unsigned long flags;
5901 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005902
Quinn Tran063b36d2017-12-04 14:45:10 -08005903 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005904 if (!fcport) {
5905 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5906 "qla_target(%d): Allocation of tmp FC port failed",
5907 vha->vp_idx);
5908 return NULL;
5909 }
5910
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005911 fcport->loop_id = loop_id;
5912
Quinn Tran15f30a52017-03-15 09:48:52 -07005913 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005914 if (rc != QLA_SUCCESS) {
5915 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5916 "qla_target(%d): Failed to retrieve fcport "
5917 "information -- get_port_database() returned %x "
5918 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5919 kfree(fcport);
5920 return NULL;
5921 }
5922
Quinn Tran726b8542017-01-19 22:28:00 -08005923 del = NULL;
5924 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5925 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5926
5927 if (tfcp) {
5928 tfcp->d_id = fcport->d_id;
5929 tfcp->port_type = fcport->port_type;
5930 tfcp->supported_classes = fcport->supported_classes;
5931 tfcp->flags |= fcport->flags;
Quinn Tran76f9a2d2017-12-04 14:45:11 -08005932 tfcp->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005933
5934 del = fcport;
5935 fcport = tfcp;
5936 } else {
5937 if (vha->hw->current_topology == ISP_CFG_F)
5938 fcport->flags |= FCF_FABRIC_DEVICE;
5939
5940 list_add_tail(&fcport->list, &vha->vp_fcports);
5941 if (!IS_SW_RESV_ADDR(fcport->d_id))
5942 vha->fcport_count++;
5943 fcport->login_gen++;
5944 fcport->disc_state = DSC_LOGIN_COMPLETE;
5945 fcport->login_succ = 1;
5946 newfcport = 1;
5947 }
5948
5949 fcport->deleted = 0;
5950 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5951
5952 switch (vha->host->active_mode) {
5953 case MODE_INITIATOR:
5954 case MODE_DUAL:
5955 if (newfcport) {
5956 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005957 ql_dbg(ql_dbg_disc, vha, 0x20fe,
Quinn Tran726b8542017-01-19 22:28:00 -08005958 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5959 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5960 qla24xx_post_upd_fcport_work(vha, fcport);
5961 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07005962 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08005963 "%s %d %8phC post gpsc fcp_cnt %d\n",
5964 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5965 qla24xx_post_gpsc_work(vha, fcport);
5966 }
5967 }
5968 break;
5969
5970 case MODE_TARGET:
5971 default:
5972 break;
5973 }
5974 if (del)
5975 qla2x00_free_fcport(del);
5976
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005977 return fcport;
5978}
5979
5980/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08005981static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005982 uint8_t *s_id)
5983{
Quinn Tran5d964832017-01-19 22:27:59 -08005984 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005985 fc_port_t *fcport = NULL;
5986 int rc, global_resets;
5987 uint16_t loop_id = 0;
5988
Quinn Tran726b8542017-01-19 22:28:00 -08005989 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5990 /*
5991 * This is Domain Controller, so it should be
5992 * OK to drop SCSI commands from it.
5993 */
5994 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5995 "Unable to find initiator with S_ID %x:%x:%x",
5996 s_id[0], s_id[1], s_id[2]);
5997 return NULL;
5998 }
5999
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006000 mutex_lock(&vha->vha_tgt.tgt_mutex);
6001
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006002retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006003 global_resets =
6004 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006005
6006 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6007 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006008 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6009
Quinn Tran726b8542017-01-19 22:28:00 -08006010 ql_log(ql_log_info, vha, 0xf071,
6011 "qla_target(%d): Unable to find "
6012 "initiator with S_ID %x:%x:%x",
6013 vha->vp_idx, s_id[0], s_id[1],
6014 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006015
6016 if (rc == -ENOENT) {
6017 qlt_port_logo_t logo;
6018 sid_to_portid(s_id, &logo.id);
6019 logo.cmd_count = 1;
6020 qlt_send_first_logo(vha, &logo);
6021 }
6022
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006023 return NULL;
6024 }
6025
6026 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006027 if (!fcport) {
6028 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006029 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006030 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006031
6032 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006033 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006034 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6035 "qla_target(%d): global reset during session discovery "
6036 "(counter was %d, new %d), retrying", vha->vp_idx,
6037 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006038 atomic_read(&vha->vha_tgt.
6039 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006040 goto retry;
6041 }
6042
6043 sess = qlt_create_sess(vha, fcport, true);
6044
Alexei Potashnik71cdc072015-12-17 14:57:01 -05006045 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6046
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006047 return sess;
6048}
6049
6050static void qlt_abort_work(struct qla_tgt *tgt,
6051 struct qla_tgt_sess_work_param *prm)
6052{
6053 struct scsi_qla_host *vha = tgt->vha;
6054 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08006055 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006056 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006057 uint32_t be_s_id;
6058 uint8_t s_id[3];
6059 int rc;
6060
Quinn Tran75601512015-12-17 14:57:04 -05006061 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006062
6063 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05006064 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006065
6066 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6067 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6068 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6069
6070 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6071 (unsigned char *)&be_s_id);
6072 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006073 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006074
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006075 sess = qlt_make_local_sess(vha, s_id);
6076 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006077
Quinn Tran75601512015-12-17 14:57:04 -05006078 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006079 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05006080 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006081 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006082 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006083 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006084 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006085 }
6086
Quinn Tran726b8542017-01-19 22:28:00 -08006087 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006088 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08006089 "%s: kref_get fail %8phC \n",
6090 __func__, sess->port_name);
6091 sess = NULL;
6092 goto out_term2;
6093 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006094 }
6095
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006096 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006097 ha->tgt.tgt_ops->put_sess(sess);
6098 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6099
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006100 if (rc != 0)
6101 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006102 return;
6103
Quinn Tran75601512015-12-17 14:57:04 -05006104out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08006105 if (sess)
6106 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05006107 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006108
6109out_term:
6110 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006111 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6112 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006113 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006114}
6115
6116static void qlt_tmr_work(struct qla_tgt *tgt,
6117 struct qla_tgt_sess_work_param *prm)
6118{
6119 struct atio_from_isp *a = &prm->tm_iocb2;
6120 struct scsi_qla_host *vha = tgt->vha;
6121 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08006122 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006123 unsigned long flags;
6124 uint8_t *s_id = NULL; /* to hide compiler warnings */
6125 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07006126 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07006127 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006128 void *iocb;
6129
Quinn Tran75601512015-12-17 14:57:04 -05006130 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006131
6132 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006133 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006134
6135 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6136 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6137 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006138 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006139
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006140 sess = qlt_make_local_sess(vha, s_id);
6141 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006142
Quinn Tran75601512015-12-17 14:57:04 -05006143 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006144 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006145 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006146 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006147 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006148 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006149 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006150 }
6151
Quinn Tran726b8542017-01-19 22:28:00 -08006152 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006153 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08006154 "%s: kref_get fail %8phC\n",
6155 __func__, sess->port_name);
6156 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006157 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006158 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006159 }
6160
6161 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006162 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006163 unpacked_lun =
6164 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006165
6166 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006167 ha->tgt.tgt_ops->put_sess(sess);
6168 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6169
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006170 if (rc != 0)
6171 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006172 return;
6173
Quinn Tranf159b3c2017-03-15 09:48:47 -07006174out_term2:
6175 if (sess)
6176 ha->tgt.tgt_ops->put_sess(sess);
6177 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006178out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006179 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006180}
6181
6182static void qlt_sess_work_fn(struct work_struct *work)
6183{
6184 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6185 struct scsi_qla_host *vha = tgt->vha;
6186 unsigned long flags;
6187
6188 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6189
6190 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6191 while (!list_empty(&tgt->sess_works_list)) {
6192 struct qla_tgt_sess_work_param *prm = list_entry(
6193 tgt->sess_works_list.next, typeof(*prm),
6194 sess_works_list_entry);
6195
6196 /*
6197 * This work can be scheduled on several CPUs at time, so we
6198 * must delete the entry to eliminate double processing
6199 */
6200 list_del(&prm->sess_works_list_entry);
6201
6202 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6203
6204 switch (prm->type) {
6205 case QLA_TGT_SESS_WORK_ABORT:
6206 qlt_abort_work(tgt, prm);
6207 break;
6208 case QLA_TGT_SESS_WORK_TM:
6209 qlt_tmr_work(tgt, prm);
6210 break;
6211 default:
6212 BUG_ON(1);
6213 break;
6214 }
6215
6216 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6217
6218 kfree(prm);
6219 }
6220 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6221}
6222
6223/* Must be called under tgt_host_action_mutex */
6224int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6225{
6226 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006227 int rc, i;
6228 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006229
6230 if (!QLA_TGT_MODE_ENABLED())
6231 return 0;
6232
Arun Easi33c36c02013-01-30 03:34:41 -05006233 if (!IS_TGT_MODE_CAPABLE(ha)) {
6234 ql_log(ql_log_warn, base_vha, 0xe070,
6235 "This adapter does not support target mode.\n");
6236 return 0;
6237 }
6238
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006239 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006240 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006241
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006242 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006243
6244 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6245 if (!tgt) {
6246 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6247 "Unable to allocate struct qla_tgt\n");
6248 return -ENOMEM;
6249 }
6250
Kees Cook6396bb22018-06-12 14:03:40 -07006251 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6252 sizeof(struct qla_qpair_hint),
6253 GFP_KERNEL);
Quinn Trane326d222017-06-13 20:47:18 -07006254 if (!tgt->qphints) {
6255 kfree(tgt);
6256 ql_log(ql_log_warn, base_vha, 0x0197,
6257 "Unable to allocate qpair hints.\n");
6258 return -ENOMEM;
6259 }
6260
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006261 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6262 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6263
Quinn Trane326d222017-06-13 20:47:18 -07006264 rc = btree_init64(&tgt->lun_qpair_map);
6265 if (rc) {
6266 kfree(tgt->qphints);
6267 kfree(tgt);
6268 ql_log(ql_log_info, base_vha, 0x0198,
6269 "Unable to initialize lun_qpair_map btree\n");
6270 return -EIO;
6271 }
6272 h = &tgt->qphints[0];
6273 h->qpair = ha->base_qpair;
6274 INIT_LIST_HEAD(&h->hint_elem);
6275 h->cpuid = ha->base_qpair->cpuid;
6276 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6277
6278 for (i = 0; i < ha->max_qpairs; i++) {
6279 unsigned long flags;
6280
6281 struct qla_qpair *qpair = ha->queue_pair_map[i];
6282 h = &tgt->qphints[i + 1];
6283 INIT_LIST_HEAD(&h->hint_elem);
6284 if (qpair) {
6285 h->qpair = qpair;
6286 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6287 list_add_tail(&h->hint_elem, &qpair->hints_list);
6288 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6289 h->cpuid = qpair->cpuid;
6290 }
6291 }
6292
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006293 tgt->ha = ha;
6294 tgt->vha = base_vha;
6295 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006296 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006297 spin_lock_init(&tgt->sess_work_lock);
6298 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6299 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006300 atomic_set(&tgt->tgt_global_resets_count, 0);
6301
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006302 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006303
6304 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6305 "qla_target(%d): using 64 Bit PCI addressing",
6306 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006307 /* 3 is reserved */
6308 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006309
6310 mutex_lock(&qla_tgt_mutex);
6311 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6312 mutex_unlock(&qla_tgt_mutex);
6313
Quinn Tranf1443ee2017-03-15 09:48:51 -07006314 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6315 ha->tgt.tgt_ops->add_target(base_vha);
6316
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006317 return 0;
6318}
6319
6320/* Must be called under tgt_host_action_mutex */
6321int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6322{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006323 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006324 return 0;
6325
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006326 if (vha->fc_vport) {
6327 qlt_release(vha->vha_tgt.qla_tgt);
6328 return 0;
6329 }
Quinn Tran33e79972014-09-25 06:14:55 -04006330
6331 /* free left over qfull cmds */
6332 qlt_init_term_exchange(vha);
6333
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006334 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6335 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006336 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006337
6338 return 0;
6339}
6340
Quinn Tran482c9dc2017-03-15 09:48:54 -07006341void qlt_remove_target_resources(struct qla_hw_data *ha)
6342{
6343 struct scsi_qla_host *node;
6344 u32 key = 0;
6345
6346 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6347 btree_remove32(&ha->tgt.host_map, key);
6348
6349 btree_destroy32(&ha->tgt.host_map);
6350}
6351
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006352static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6353 unsigned char *b)
6354{
6355 int i;
6356
6357 pr_debug("qla2xxx HW vha->node_name: ");
6358 for (i = 0; i < WWN_SIZE; i++)
6359 pr_debug("%02x ", vha->node_name[i]);
6360 pr_debug("\n");
6361 pr_debug("qla2xxx HW vha->port_name: ");
6362 for (i = 0; i < WWN_SIZE; i++)
6363 pr_debug("%02x ", vha->port_name[i]);
6364 pr_debug("\n");
6365
6366 pr_debug("qla2xxx passed configfs WWPN: ");
6367 put_unaligned_be64(wwpn, b);
6368 for (i = 0; i < WWN_SIZE; i++)
6369 pr_debug("%02x ", b[i]);
6370 pr_debug("\n");
6371}
6372
6373/**
6374 * qla_tgt_lport_register - register lport with external module
6375 *
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006376 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
Bart Van Assche2db62282018-01-23 16:33:51 -08006377 * @phys_wwpn:
6378 * @npiv_wwpn:
6379 * @npiv_wwnn:
6380 * @callback: lport initialization callback for tcm_qla2xxx code
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006381 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006382int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6383 u64 npiv_wwpn, u64 npiv_wwnn,
6384 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006385{
6386 struct qla_tgt *tgt;
6387 struct scsi_qla_host *vha;
6388 struct qla_hw_data *ha;
6389 struct Scsi_Host *host;
6390 unsigned long flags;
6391 int rc;
6392 u8 b[WWN_SIZE];
6393
6394 mutex_lock(&qla_tgt_mutex);
6395 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6396 vha = tgt->vha;
6397 ha = vha->hw;
6398
6399 host = vha->host;
6400 if (!host)
6401 continue;
6402
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006403 if (!(host->hostt->supported_mode & MODE_TARGET))
6404 continue;
6405
6406 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006407 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006408 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6409 host->host_no);
6410 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6411 continue;
6412 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006413 if (tgt->tgt_stop) {
6414 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6415 host->host_no);
6416 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6417 continue;
6418 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006419 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6420
6421 if (!scsi_host_get(host)) {
6422 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6423 "Unable to scsi_host_get() for"
6424 " qla2xxx scsi_host\n");
6425 continue;
6426 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006427 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006428
6429 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6430 scsi_host_put(host);
6431 continue;
6432 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006433 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6434 if (rc != 0)
6435 scsi_host_put(host);
6436
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006437 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006438 return rc;
6439 }
6440 mutex_unlock(&qla_tgt_mutex);
6441
6442 return -ENODEV;
6443}
6444EXPORT_SYMBOL(qlt_lport_register);
6445
6446/**
6447 * qla_tgt_lport_deregister - Degister lport
6448 *
6449 * @vha: Registered scsi_qla_host pointer
6450 */
6451void qlt_lport_deregister(struct scsi_qla_host *vha)
6452{
6453 struct qla_hw_data *ha = vha->hw;
6454 struct Scsi_Host *sh = vha->host;
6455 /*
6456 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6457 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006458 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006459 ha->tgt.tgt_ops = NULL;
6460 /*
6461 * Release the Scsi_Host reference for the underlying qla2xxx host
6462 */
6463 scsi_host_put(sh);
6464}
6465EXPORT_SYMBOL(qlt_lport_deregister);
6466
6467/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006468static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006469{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006470 switch (ql2x_ini_mode) {
6471 case QLA2XXX_INI_MODE_DISABLED:
6472 case QLA2XXX_INI_MODE_EXCLUSIVE:
6473 vha->host->active_mode = MODE_TARGET;
6474 break;
6475 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006476 vha->host->active_mode = MODE_UNKNOWN;
6477 break;
6478 case QLA2XXX_INI_MODE_DUAL:
6479 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006480 break;
6481 default:
6482 break;
6483 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006484}
6485
6486/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006487static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006488{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006489 switch (ql2x_ini_mode) {
6490 case QLA2XXX_INI_MODE_DISABLED:
6491 vha->host->active_mode = MODE_UNKNOWN;
6492 break;
6493 case QLA2XXX_INI_MODE_EXCLUSIVE:
6494 vha->host->active_mode = MODE_INITIATOR;
6495 break;
6496 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006497 case QLA2XXX_INI_MODE_DUAL:
6498 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006499 break;
6500 default:
6501 break;
6502 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006503}
6504
6505/*
6506 * qla_tgt_enable_vha - NO LOCK HELD
6507 *
6508 * host_reset, bring up w/ Target Mode Enabled
6509 */
6510void
6511qlt_enable_vha(struct scsi_qla_host *vha)
6512{
6513 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006514 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006515 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006516 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006517
6518 if (!tgt) {
6519 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6520 "Unable to locate qla_tgt pointer from"
6521 " struct qla_hw_data\n");
6522 dump_stack();
6523 return;
6524 }
6525
6526 spin_lock_irqsave(&ha->hardware_lock, flags);
6527 tgt->tgt_stopped = 0;
6528 qlt_set_mode(vha);
6529 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6530
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006531 if (vha->vp_idx) {
6532 qla24xx_disable_vp(vha);
6533 qla24xx_enable_vp(vha);
6534 } else {
6535 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6536 qla2xxx_wake_dpc(base_vha);
6537 qla2x00_wait_for_hba_online(base_vha);
6538 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006539}
6540EXPORT_SYMBOL(qlt_enable_vha);
6541
6542/*
6543 * qla_tgt_disable_vha - NO LOCK HELD
6544 *
6545 * Disable Target Mode and reset the adapter
6546 */
Joern Engel55a90662014-09-16 16:23:15 -04006547static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006548{
6549 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006550 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006551 unsigned long flags;
6552
6553 if (!tgt) {
6554 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6555 "Unable to locate qla_tgt pointer from"
6556 " struct qla_hw_data\n");
6557 dump_stack();
6558 return;
6559 }
6560
6561 spin_lock_irqsave(&ha->hardware_lock, flags);
6562 qlt_clear_mode(vha);
6563 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6564
6565 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6566 qla2xxx_wake_dpc(vha);
6567 qla2x00_wait_for_hba_online(vha);
6568}
6569
6570/*
6571 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6572 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6573 * members.
6574 */
6575void
6576qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6577{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006578 vha->vha_tgt.qla_tgt = NULL;
6579
6580 mutex_init(&vha->vha_tgt.tgt_mutex);
6581 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006582
6583 qlt_clear_mode(vha);
6584
6585 /*
6586 * NOTE: Currently the value is kept the same for <24xx and
6587 * >=24xx ISPs. If it is necessary to change it,
6588 * the check should be added for specific ISPs,
6589 * assigning the value appropriately.
6590 */
6591 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006592
6593 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006594}
6595
Quinn Trane374f9f2017-12-28 12:33:31 -08006596u8
6597qlt_rff_id(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006598{
Quinn Trane374f9f2017-12-28 12:33:31 -08006599 u8 fc4_feature = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006600 /*
6601 * FC-4 Feature bit 0 indicates target functionality to the name server.
6602 */
6603 if (qla_tgt_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006604 fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006605 } else if (qla_ini_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006606 fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006607 } else if (qla_dual_mode_enabled(vha))
Quinn Trane374f9f2017-12-28 12:33:31 -08006608 fc4_feature = BIT_0 | BIT_1;
6609
6610 return fc4_feature;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006611}
6612
6613/*
6614 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6615 * @ha: HA context
6616 *
6617 * Beginning of ATIO ring has initialization control block already built
6618 * by nvram config routine.
6619 *
6620 * Returns 0 on success.
6621 */
6622void
6623qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6624{
6625 struct qla_hw_data *ha = vha->hw;
6626 uint16_t cnt;
6627 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6628
Quinn Tranead03852017-01-19 22:28:01 -08006629 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006630 return;
6631
6632 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6633 pkt->u.raw.signature = ATIO_PROCESSED;
6634 pkt++;
6635 }
6636
6637}
6638
6639/*
6640 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6641 * @ha: SCSI driver HA context
6642 */
6643void
Quinn Tran2f424b92015-12-17 14:57:07 -05006644qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006645{
6646 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006647 struct atio_from_isp *pkt;
6648 int cnt, i;
6649
Quinn Tranec7193e2017-03-15 09:48:55 -07006650 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006651 return;
6652
Quinn Tran5f355092016-12-23 18:06:11 -08006653 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6654 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006655 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6656 cnt = pkt->u.raw.entry_count;
6657
Quinn Tran5f355092016-12-23 18:06:11 -08006658 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6659 /*
6660 * This packet is corrupted. The header + payload
6661 * can not be trusted. There is no point in passing
6662 * it further up.
6663 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006664 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006665 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6666 pkt->u.isp24.fcp_hdr.s_id,
6667 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6668 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6669
6670 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006671 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6672 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006673 } else {
6674 qlt_24xx_atio_pkt_all_vps(vha,
6675 (struct atio_from_isp *)pkt, ha_locked);
6676 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006677
6678 for (i = 0; i < cnt; i++) {
6679 ha->tgt.atio_ring_index++;
6680 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6681 ha->tgt.atio_ring_index = 0;
6682 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6683 } else
6684 ha->tgt.atio_ring_ptr++;
6685
6686 pkt->u.raw.signature = ATIO_PROCESSED;
6687 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6688 }
6689 wmb();
6690 }
6691
6692 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006693 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006694}
6695
6696void
Arun Easiaa230bc2013-01-30 03:34:39 -05006697qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006698{
6699 struct qla_hw_data *ha = vha->hw;
Quinn Trand2b292c2017-12-28 12:33:17 -08006700 struct qla_msix_entry *msix = &ha->msix_entries[2];
6701 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6702
Arun Easiaa230bc2013-01-30 03:34:39 -05006703 if (!QLA_TGT_MODE_ENABLED())
6704 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006705
Arun Easiaa230bc2013-01-30 03:34:39 -05006706 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6707 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6708 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6709
Quinn Trand2b292c2017-12-28 12:33:17 -08006710 if (ha->flags.msix_enabled) {
6711 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6712 if (IS_QLA2071(ha)) {
6713 /* 4 ports Baker: Enable Interrupt Handshake */
6714 icb->msix_atio = 0;
6715 icb->firmware_options_2 |= BIT_26;
6716 } else {
6717 icb->msix_atio = cpu_to_le16(msix->entry);
6718 icb->firmware_options_2 &= ~BIT_26;
6719 }
6720 ql_dbg(ql_dbg_init, vha, 0xf072,
6721 "Registering ICB vector 0x%x for atio que.\n",
6722 msix->entry);
6723 }
6724 } else {
6725 /* INTx|MSI */
6726 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6727 icb->msix_atio = 0;
6728 icb->firmware_options_2 |= BIT_26;
6729 ql_dbg(ql_dbg_init, vha, 0xf072,
6730 "%s: Use INTx for ATIOQ.\n", __func__);
6731 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006732 }
6733}
6734
6735void
6736qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6737{
6738 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006739 u32 tmp;
Quinn Tran99e1b682017-06-02 09:12:03 -07006740
6741 if (!QLA_TGT_MODE_ENABLED())
6742 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006743
Quinn Tranead03852017-01-19 22:28:01 -08006744 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006745 if (!ha->tgt.saved_set) {
6746 /* We save only once */
6747 ha->tgt.saved_exchange_count = nv->exchange_count;
6748 ha->tgt.saved_firmware_options_1 =
6749 nv->firmware_options_1;
6750 ha->tgt.saved_firmware_options_2 =
6751 nv->firmware_options_2;
6752 ha->tgt.saved_firmware_options_3 =
6753 nv->firmware_options_3;
6754 ha->tgt.saved_set = 1;
6755 }
6756
Quinn Tran99e1b682017-06-02 09:12:03 -07006757 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006758 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006759 else /* dual */
6760 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006761
6762 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006763 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006764
6765 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006766 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006767 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006768
6769 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006770 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006771 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006772 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006773 if (ql2xtgt_tape_enable)
6774 /* Enable FC Tape support */
6775 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6776 else
6777 /* Disable FC Tape support */
6778 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6779
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006780 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006781 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006782
6783 /*
6784 * clear BIT 15 explicitly as we have seen at least
6785 * a couple of instances where this was set and this
6786 * was causing the firmware to not be initialized.
6787 */
6788 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006789 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006790 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08006791
6792 if (IS_QLA25XX(ha)) {
6793 /* Change Loop-prefer to Pt-Pt */
6794 tmp = ~(BIT_4|BIT_5|BIT_6);
6795 nv->firmware_options_2 &= cpu_to_le32(tmp);
6796 tmp = P2P << 4;
6797 nv->firmware_options_2 |= cpu_to_le32(tmp);
6798 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006799 } else {
6800 if (ha->tgt.saved_set) {
6801 nv->exchange_count = ha->tgt.saved_exchange_count;
6802 nv->firmware_options_1 =
6803 ha->tgt.saved_firmware_options_1;
6804 nv->firmware_options_2 =
6805 ha->tgt.saved_firmware_options_2;
6806 nv->firmware_options_3 =
6807 ha->tgt.saved_firmware_options_3;
6808 }
6809 return;
6810 }
6811
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006812 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006813 if (vha->flags.init_done)
6814 fc_host_supported_classes(vha->host) =
6815 FC_COS_CLASS2 | FC_COS_CLASS3;
6816
Bart Van Asschead950362015-07-09 07:24:08 -07006817 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006818 } else {
6819 if (vha->flags.init_done)
6820 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6821
Bart Van Asschead950362015-07-09 07:24:08 -07006822 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006823 }
6824}
6825
6826void
6827qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6828 struct init_cb_24xx *icb)
6829{
6830 struct qla_hw_data *ha = vha->hw;
6831
Quinn Tran481ce732015-12-17 14:57:08 -05006832 if (!QLA_TGT_MODE_ENABLED())
6833 return;
6834
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006835 if (ha->tgt.node_name_set) {
6836 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006837 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006838 }
Quinn Tran481ce732015-12-17 14:57:08 -05006839
6840 /* disable ZIO at start time. */
6841 if (!vha->flags.init_done) {
6842 uint32_t tmp;
6843 tmp = le32_to_cpu(icb->firmware_options_2);
6844 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6845 icb->firmware_options_2 = cpu_to_le32(tmp);
6846 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006847}
6848
Arun Easiaa230bc2013-01-30 03:34:39 -05006849void
6850qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6851{
6852 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006853 u32 tmp;
Arun Easiaa230bc2013-01-30 03:34:39 -05006854
6855 if (!QLA_TGT_MODE_ENABLED())
6856 return;
6857
Quinn Tranead03852017-01-19 22:28:01 -08006858 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006859 if (!ha->tgt.saved_set) {
6860 /* We save only once */
6861 ha->tgt.saved_exchange_count = nv->exchange_count;
6862 ha->tgt.saved_firmware_options_1 =
6863 nv->firmware_options_1;
6864 ha->tgt.saved_firmware_options_2 =
6865 nv->firmware_options_2;
6866 ha->tgt.saved_firmware_options_3 =
6867 nv->firmware_options_3;
6868 ha->tgt.saved_set = 1;
6869 }
6870
Quinn Tran99e1b682017-06-02 09:12:03 -07006871 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006872 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006873 else /* dual */
6874 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05006875
6876 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006877 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006878
6879 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006880 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006881 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006882 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006883 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006884 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006885 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006886 /*
6887 * clear BIT 15 explicitly as we have seen at
6888 * least a couple of instances where this was set
6889 * and this was causing the firmware to not be
6890 * initialized.
6891 */
6892 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006893 if (ql2xtgt_tape_enable)
6894 /* Enable FC tape support */
6895 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6896 else
6897 /* Disable FC tape support */
6898 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6899
Arun Easiaa230bc2013-01-30 03:34:39 -05006900 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006901 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006902 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006903 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08006904
6905 /* Change Loop-prefer to Pt-Pt */
6906 tmp = ~(BIT_4|BIT_5|BIT_6);
6907 nv->firmware_options_2 &= cpu_to_le32(tmp);
6908 tmp = P2P << 4;
6909 nv->firmware_options_2 |= cpu_to_le32(tmp);
Arun Easiaa230bc2013-01-30 03:34:39 -05006910 } else {
6911 if (ha->tgt.saved_set) {
6912 nv->exchange_count = ha->tgt.saved_exchange_count;
6913 nv->firmware_options_1 =
6914 ha->tgt.saved_firmware_options_1;
6915 nv->firmware_options_2 =
6916 ha->tgt.saved_firmware_options_2;
6917 nv->firmware_options_3 =
6918 ha->tgt.saved_firmware_options_3;
6919 }
6920 return;
6921 }
6922
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006923 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006924 if (vha->flags.init_done)
6925 fc_host_supported_classes(vha->host) =
6926 FC_COS_CLASS2 | FC_COS_CLASS3;
6927
Bart Van Asschead950362015-07-09 07:24:08 -07006928 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006929 } else {
6930 if (vha->flags.init_done)
6931 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6932
Bart Van Asschead950362015-07-09 07:24:08 -07006933 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006934 }
6935}
6936
6937void
6938qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6939 struct init_cb_81xx *icb)
6940{
6941 struct qla_hw_data *ha = vha->hw;
6942
6943 if (!QLA_TGT_MODE_ENABLED())
6944 return;
6945
6946 if (ha->tgt.node_name_set) {
6947 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006948 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006949 }
Quinn Tran481ce732015-12-17 14:57:08 -05006950
6951 /* disable ZIO at start time. */
6952 if (!vha->flags.init_done) {
6953 uint32_t tmp;
6954 tmp = le32_to_cpu(icb->firmware_options_2);
6955 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6956 icb->firmware_options_2 = cpu_to_le32(tmp);
6957 }
6958
Arun Easiaa230bc2013-01-30 03:34:39 -05006959}
6960
6961void
6962qlt_83xx_iospace_config(struct qla_hw_data *ha)
6963{
6964 if (!QLA_TGT_MODE_ENABLED())
6965 return;
6966
6967 ha->msix_count += 1; /* For ATIO Q */
6968}
6969
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006970
6971void
6972qlt_modify_vp_config(struct scsi_qla_host *vha,
6973 struct vp_config_entry_24xx *vpmod)
6974{
Quinn Tranead03852017-01-19 22:28:01 -08006975 /* enable target mode. Bit5 = 1 => disable */
6976 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006977 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08006978
Quinn Tranead03852017-01-19 22:28:01 -08006979 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08006980 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006981 vpmod->options_idx1 &= ~BIT_4;
6982}
6983
6984void
6985qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6986{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006987 int rc;
6988
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006989 if (!QLA_TGT_MODE_ENABLED())
6990 return;
6991
Himanshu Madhanic9558862017-10-13 09:34:04 -07006992 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006993 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6994 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6995 } else {
6996 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6997 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6998 }
6999
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08007000 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7001 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08007002
7003 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7004 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7005 qlt_unknown_atio_work_fn);
7006
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007007 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007008
7009 rc = btree_init32(&ha->tgt.host_map);
7010 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007011 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007012 "Unable to initialize ha->host_map btree\n");
7013
7014 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007015}
7016
Arun Easiaa230bc2013-01-30 03:34:39 -05007017irqreturn_t
7018qla83xx_msix_atio_q(int irq, void *dev_id)
7019{
7020 struct rsp_que *rsp;
7021 scsi_qla_host_t *vha;
7022 struct qla_hw_data *ha;
7023 unsigned long flags;
7024
7025 rsp = (struct rsp_que *) dev_id;
7026 ha = rsp->hw;
7027 vha = pci_get_drvdata(ha->pdev);
7028
Quinn Tran2f424b92015-12-17 14:57:07 -05007029 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007030
Quinn Tran2f424b92015-12-17 14:57:07 -05007031 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05007032
Quinn Tran2f424b92015-12-17 14:57:07 -05007033 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05007034
7035 return IRQ_HANDLED;
7036}
7037
Quinn Tran2f424b92015-12-17 14:57:07 -05007038static void
7039qlt_handle_abts_recv_work(struct work_struct *work)
7040{
7041 struct qla_tgt_sess_op *op = container_of(work,
7042 struct qla_tgt_sess_op, work);
7043 scsi_qla_host_t *vha = op->vha;
7044 struct qla_hw_data *ha = vha->hw;
7045 unsigned long flags;
7046
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007047 if (qla2x00_reset_active(vha) ||
7048 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05007049 return;
7050
7051 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7052 qlt_24xx_process_atio_queue(vha, 0);
7053 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7054
7055 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07007056 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05007057 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07007058
7059 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05007060}
7061
7062void
Quinn Tran82de8022017-06-13 20:47:17 -07007063qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7064 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05007065{
7066 struct qla_tgt_sess_op *op;
7067
7068 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7069
7070 if (!op) {
7071 /* do not reach for ATIO queue here. This is best effort err
7072 * recovery at this point.
7073 */
Quinn Tran82de8022017-06-13 20:47:17 -07007074 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05007075 return;
7076 }
7077
7078 memcpy(&op->atio, pkt, sizeof(*pkt));
7079 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007080 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07007081 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05007082 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7083 queue_work(qla_tgt_wq, &op->work);
7084 return;
7085}
7086
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007087int
7088qlt_mem_alloc(struct qla_hw_data *ha)
7089{
7090 if (!QLA_TGT_MODE_ENABLED())
7091 return 0;
7092
Kees Cook6396bb22018-06-12 14:03:40 -07007093 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7094 sizeof(struct qla_tgt_vp_map),
7095 GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007096 if (!ha->tgt.tgt_vp_map)
7097 return -ENOMEM;
7098
7099 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7100 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7101 &ha->tgt.atio_dma, GFP_KERNEL);
7102 if (!ha->tgt.atio_ring) {
7103 kfree(ha->tgt.tgt_vp_map);
7104 return -ENOMEM;
7105 }
7106 return 0;
7107}
7108
7109void
7110qlt_mem_free(struct qla_hw_data *ha)
7111{
7112 if (!QLA_TGT_MODE_ENABLED())
7113 return;
7114
7115 if (ha->tgt.atio_ring) {
7116 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7117 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7118 ha->tgt.atio_dma);
7119 }
7120 kfree(ha->tgt.tgt_vp_map);
7121}
7122
7123/* vport_slock to be held by the caller */
7124void
7125qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7126{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007127 void *slot;
7128 u32 key;
7129 int rc;
7130
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007131 if (!QLA_TGT_MODE_ENABLED())
7132 return;
7133
Quinn Tran482c9dc2017-03-15 09:48:54 -07007134 key = vha->d_id.b24;
7135
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007136 switch (cmd) {
7137 case SET_VP_IDX:
7138 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7139 break;
7140 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07007141 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7142 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007143 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007144 "Save vha in host_map %p %06x\n", vha, key);
7145 rc = btree_insert32(&vha->hw->tgt.host_map,
7146 key, vha, GFP_ATOMIC);
7147 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007148 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007149 "Unable to insert s_id into host_map: %06x\n",
7150 key);
7151 return;
7152 }
Quinn Tran83548fe2017-06-02 09:12:01 -07007153 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7154 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007155 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007156 break;
7157 case RESET_VP_IDX:
7158 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7159 break;
7160 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07007161 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007162 "clear vha in host_map %p %06x\n", vha, key);
7163 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7164 if (slot)
7165 btree_remove32(&vha->hw->tgt.host_map, key);
7166 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007167 break;
7168 }
7169}
7170
Quinn Tran482c9dc2017-03-15 09:48:54 -07007171void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7172{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007173
7174 if (!vha->d_id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007175 vha->d_id = id;
7176 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007177 } else if (vha->d_id.b24 != id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007178 qlt_update_vp_map(vha, RESET_AL_PA);
7179 vha->d_id = id;
7180 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007181 }
7182}
7183
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007184static int __init qlt_parse_ini_mode(void)
7185{
7186 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7187 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7188 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7189 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7190 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7191 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007192 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7193 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007194 else
7195 return false;
7196
7197 return true;
7198}
7199
7200int __init qlt_init(void)
7201{
7202 int ret;
7203
7204 if (!qlt_parse_ini_mode()) {
7205 ql_log(ql_log_fatal, NULL, 0xe06b,
7206 "qlt_parse_ini_mode() failed\n");
7207 return -EINVAL;
7208 }
7209
7210 if (!QLA_TGT_MODE_ENABLED())
7211 return 0;
7212
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007213 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7214 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7215 qla_tgt_mgmt_cmd), 0, NULL);
7216 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007217 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007218 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007219 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007220 }
7221
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007222 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007223 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7224 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007225
7226 if (!qla_tgt_plogi_cachep) {
7227 ql_log(ql_log_fatal, NULL, 0xe06d,
7228 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7229 ret = -ENOMEM;
7230 goto out_mgmt_cmd_cachep;
7231 }
7232
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007233 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7234 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7235 if (!qla_tgt_mgmt_cmd_mempool) {
7236 ql_log(ql_log_fatal, NULL, 0xe06e,
7237 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7238 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007239 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007240 }
7241
7242 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7243 if (!qla_tgt_wq) {
7244 ql_log(ql_log_fatal, NULL, 0xe06f,
7245 "alloc_workqueue for qla_tgt_wq failed\n");
7246 ret = -ENOMEM;
7247 goto out_cmd_mempool;
7248 }
7249 /*
7250 * Return 1 to signal that initiator-mode is being disabled
7251 */
7252 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7253
7254out_cmd_mempool:
7255 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007256out_plogi_cachep:
7257 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007258out_mgmt_cmd_cachep:
7259 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007260 return ret;
7261}
7262
7263void qlt_exit(void)
7264{
7265 if (!QLA_TGT_MODE_ENABLED())
7266 return;
7267
7268 destroy_workqueue(qla_tgt_wq);
7269 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007270 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007271 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007272}