blob: 5e81b64c8ef5c992d75c28c675f821aff61494d2 [file] [log] [blame]
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001/*
2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
3 *
4 * based on qla2x00t.c code:
5 *
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
10 *
11 * Forward port and refactoring to modern qla2xxx and target/configfs
12 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -070013 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
18 * of the License.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/types.h>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040029#include <linux/blkdev.h>
30#include <linux/interrupt.h>
31#include <linux/pci.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/workqueue.h>
35#include <asm/unaligned.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_tcq.h>
39#include <target/target_core_base.h>
40#include <target/target_core_fabric.h>
41
42#include "qla_def.h"
43#include "qla_target.h"
44
Arun Easid154f352014-09-25 06:14:48 -040045static int ql2xtgt_tape_enable;
46module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
49
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040050static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51module_param(qlini_mode, charp, S_IRUGO);
52MODULE_PARM_DESC(qlini_mode,
53 "Determines when initiator mode will be enabled. Possible values: "
54 "\"exclusive\" - initiator mode will be enabled on load, "
55 "disabled on enabling target mode and then on disabling target mode "
56 "enabled back; "
57 "\"disabled\" - initiator mode will never be enabled; "
Quinn Tranead03852017-01-19 22:28:01 -080058 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
59 "when ready "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040060 "\"enabled\" (default) - initiator mode will always stay enabled.");
61
Quinn Tran99e1b682017-06-02 09:12:03 -070062static int ql_dm_tgt_ex_pct = 0;
Quinn Tranead03852017-01-19 22:28:01 -080063module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
67 "for Target mode.");
68
Quinn Tran09620ee2017-06-13 20:47:20 -070069int ql2xuctrlirq = 1;
70module_param(ql2xuctrlirq, int, 0644);
71MODULE_PARM_DESC(ql2xuctrlirq,
72 "User to control IRQ placement via smp_affinity."
73 "Valid with qlini_mode=disabled."
74 "1(default): enable");
75
Arun Easiaa230bc2013-01-30 03:34:39 -050076int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040077
Quinn Tran7cf95f72017-12-28 12:33:28 -080078static int qla_sam_status = SAM_STAT_BUSY;
79static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
Quinn Tran33e79972014-09-25 06:14:55 -040080
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040081/*
82 * From scsi/fc/fc_fcp.h
83 */
84enum fcp_resp_rsp_codes {
85 FCP_TMF_CMPL = 0,
86 FCP_DATA_LEN_INVALID = 1,
87 FCP_CMND_FIELDS_INVALID = 2,
88 FCP_DATA_PARAM_MISMATCH = 3,
89 FCP_TMF_REJECTED = 4,
90 FCP_TMF_FAILED = 5,
91 FCP_TMF_INVALID_LUN = 9,
92};
93
94/*
95 * fc_pri_ta from scsi/fc/fc_fcp.h
96 */
97#define FCP_PTA_SIMPLE 0 /* simple task attribute */
98#define FCP_PTA_HEADQ 1 /* head of queue task attribute */
99#define FCP_PTA_ORDERED 2 /* ordered task attribute */
Masanari Iida6efb3c0a2012-10-26 22:10:54 +0900100#define FCP_PTA_ACA 4 /* auto. contingent allegiance */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400101#define FCP_PTA_MASK 7 /* mask for task attribute field */
102#define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
103#define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
104
105/*
106 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
107 * must be called under HW lock and could unlock/lock it inside.
108 * It isn't an issue, since in the current implementation on the time when
109 * those functions are called:
110 *
111 * - Either context is IRQ and only IRQ handler can modify HW data,
112 * including rings related fields,
113 *
114 * - Or access to target mode variables from struct qla_tgt doesn't
115 * cross those functions boundaries, except tgt_stop, which
116 * additionally protected by irq_cmd_count.
117 */
118/* Predefs for callbacks handed to qla2xxx LLD */
119static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500120 struct atio_from_isp *pkt, uint8_t);
Quinn Tran82de8022017-06-13 20:47:17 -0700121static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
122 response_t *pkt);
Quinn Tran5d964832017-01-19 22:27:59 -0800123static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400124 int fn, void *iocb, int flags);
Quinn Tran82de8022017-06-13 20:47:17 -0700125static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
Quinn Trana07100e2015-12-07 19:48:57 -0500126 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
Quinn Tran33e79972014-09-25 06:14:55 -0400127static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
128 struct atio_from_isp *atio, uint16_t status, int qfull);
Joern Engel55a90662014-09-16 16:23:15 -0400129static void qlt_disable_vha(struct scsi_qla_host *vha);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400130static void qlt_clear_tgt_db(struct qla_tgt *tgt);
Quinn Tran82de8022017-06-13 20:47:17 -0700131static void qlt_send_notify_ack(struct qla_qpair *qpair,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400132 struct imm_ntfy_from_isp *ntfy,
133 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
134 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500135static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
136 struct imm_ntfy_from_isp *imm, int ha_locked);
Quinn Tran726b8542017-01-19 22:28:00 -0800137static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
138 fc_port_t *fcport, bool local);
139void qlt_unreg_sess(struct fc_port *sess);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700140static void qlt_24xx_handle_abts(struct scsi_qla_host *,
141 struct abts_recv_from_24xx *);
Quinn Tran82de8022017-06-13 20:47:17 -0700142static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
143 uint16_t);
Quinn 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
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001927/* ha->hardware_lock supposed to be held on entry */
1928static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08001929 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001930{
1931 struct qla_hw_data *ha = vha->hw;
1932 struct qla_tgt_mgmt_cmd *mcmd;
1933 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001934
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001935 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1936 /* send TASK_ABORT response immediately */
Linus Torvalds48ea2ce2017-07-13 14:27:32 -07001937 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001938 return 0;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001939 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001940
1941 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1942 "qla_target(%d): task abort (tag=%d)\n",
1943 vha->vp_idx, abts->exchange_addr_to_abort);
1944
1945 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1946 if (mcmd == NULL) {
1947 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1948 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1949 vha->vp_idx, __func__);
1950 return -ENOMEM;
1951 }
1952 memset(mcmd, 0, sizeof(*mcmd));
1953
1954 mcmd->sess = sess;
1955 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001956 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08001957 mcmd->tmr_func = QLA_TGT_ABTS;
Quinn Tran82de8022017-06-13 20:47:17 -07001958 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07001959 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001960
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001961 /*
1962 * LUN is looked up by target-core internally based on the passed
1963 * abts->exchange_addr_to_abort tag.
1964 */
1965 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, 0, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001966 abts->exchange_addr_to_abort);
1967 if (rc != 0) {
1968 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1969 "qla_target(%d): tgt_ops->handle_tmr()"
1970 " failed: %d", vha->vp_idx, rc);
1971 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1972 return -EFAULT;
1973 }
1974
1975 return 0;
1976}
1977
1978/*
1979 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1980 */
1981static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1982 struct abts_recv_from_24xx *abts)
1983{
1984 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001985 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001986 uint32_t tag = abts->exchange_addr_to_abort;
1987 uint8_t s_id[3];
1988 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001989 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001990
1991 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1992 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1993 "qla_target(%d): ABTS: Abort Sequence not "
1994 "supported\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07001995 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1996 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001997 return;
1998 }
1999
2000 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2001 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2002 "qla_target(%d): ABTS: Unknown Exchange "
2003 "Address received\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002004 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2005 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002006 return;
2007 }
2008
2009 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2010 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2011 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2012 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2013 le32_to_cpu(abts->fcp_hdr_le.parameter));
2014
2015 s_id[0] = abts->fcp_hdr_le.s_id[2];
2016 s_id[1] = abts->fcp_hdr_le.s_id[1];
2017 s_id[2] = abts->fcp_hdr_le.s_id[0];
2018
Quinn Tran75601512015-12-17 14:57:04 -05002019 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002020 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2021 if (!sess) {
2022 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
Colin Ian King03fea732018-03-19 10:53:41 +00002023 "qla_target(%d): task abort for non-existent session\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002024 vha->vp_idx);
Quinn Tran75601512015-12-17 14:57:04 -05002025 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2026
Quinn Tranfb352652017-12-28 12:33:32 -08002027 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2028 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002029 return;
2030 }
Quinn Tran75601512015-12-17 14:57:04 -05002031 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2032
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002033
Quinn Tran726b8542017-01-19 22:28:00 -08002034 if (sess->deleted) {
Quinn Tran82de8022017-06-13 20:47:17 -07002035 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2036 false);
Alexei Potashnike52a8b42015-07-14 16:00:48 -04002037 return;
2038 }
2039
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002040 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2041 if (rc != 0) {
2042 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2043 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2044 vha->vp_idx, rc);
Quinn Tran82de8022017-06-13 20:47:17 -07002045 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2046 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002047 return;
2048 }
2049}
2050
2051/*
2052 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2053 */
Quinn Tran82de8022017-06-13 20:47:17 -07002054static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002055 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2056{
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002057 struct scsi_qla_host *ha = mcmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002058 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2059 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002060 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002061
2062 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2063 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2064 ha, atio, resp_code);
2065
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002066
Quinn Tran82de8022017-06-13 20:47:17 -07002067 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002068 if (ctio == NULL) {
2069 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2070 "qla_target(%d): %s failed: unable to allocate "
2071 "request packet\n", ha->vp_idx, __func__);
2072 return;
2073 }
2074
2075 ctio->entry_type = CTIO_TYPE7;
2076 ctio->entry_count = 1;
2077 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2078 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002079 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002080 ctio->vp_index = ha->vp_idx;
2081 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2082 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2083 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2084 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002085 temp = (atio->u.isp24.attr << 9)|
2086 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2087 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002088 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2089 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002090 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002091 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2092 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002093 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002094
Himanshu Madhani63163e02014-09-25 06:14:59 -04002095 /* Memory Barrier */
2096 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002097 if (qpair->reqq_start_iocbs)
2098 qpair->reqq_start_iocbs(qpair);
2099 else
2100 qla2x00_start_iocbs(ha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002101}
2102
2103void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2104{
2105 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2106}
2107EXPORT_SYMBOL(qlt_free_mcmd);
2108
Quinn Tranbe251522017-03-15 09:48:49 -07002109/*
2110 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2111 * reacquire
2112 */
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002113void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07002114 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2115{
2116 struct atio_from_isp *atio = &cmd->atio;
2117 struct ctio7_to_24xx *ctio;
2118 uint16_t temp;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002119 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranbe251522017-03-15 09:48:49 -07002120
2121 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2122 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2123 "sense_key=%02x, asc=%02x, ascq=%02x",
2124 vha, atio, scsi_status, sense_key, asc, ascq);
2125
2126 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2127 if (!ctio) {
2128 ql_dbg(ql_dbg_async, vha, 0x3067,
2129 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2130 vha->host_no, __func__);
2131 goto out;
2132 }
2133
2134 ctio->entry_type = CTIO_TYPE7;
2135 ctio->entry_count = 1;
2136 ctio->handle = QLA_TGT_SKIP_HANDLE;
2137 ctio->nport_handle = cmd->sess->loop_id;
2138 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2139 ctio->vp_index = vha->vp_idx;
2140 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2141 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2142 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2143 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002144 temp = (atio->u.isp24.attr << 9) |
2145 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2146 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002147 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2148 ctio->u.status1.ox_id = cpu_to_le16(temp);
2149 ctio->u.status1.scsi_status =
2150 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2151 ctio->u.status1.response_len = cpu_to_le16(18);
2152 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2153
2154 if (ctio->u.status1.residual != 0)
2155 ctio->u.status1.scsi_status |=
2156 cpu_to_le16(SS_RESIDUAL_UNDER);
2157
2158 /* Response code and sense key */
2159 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2160 (&ctio->u.status1.sense_data)[0]);
2161 /* Additional sense length */
2162 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2163 /* ASC and ASCQ */
2164 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2165 (&ctio->u.status1.sense_data)[3]);
2166
2167 /* Memory Barrier */
2168 wmb();
2169
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002170 if (qpair->reqq_start_iocbs)
2171 qpair->reqq_start_iocbs(qpair);
2172 else
2173 qla2x00_start_iocbs(vha, qpair->req);
2174
Quinn Tranbe251522017-03-15 09:48:49 -07002175out:
2176 return;
2177}
2178
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002179/* callback from target fabric module code */
2180void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2181{
2182 struct scsi_qla_host *vha = mcmd->sess->vha;
2183 struct qla_hw_data *ha = vha->hw;
2184 unsigned long flags;
Quinn Tran82de8022017-06-13 20:47:17 -07002185 struct qla_qpair *qpair = mcmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002186
2187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2188 "TM response mcmd (%p) status %#x state %#x",
2189 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2190
Quinn Tran82de8022017-06-13 20:47:17 -07002191 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002192
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002193 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002194 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002195 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002196 * previous life, just abort the processing.
2197 */
2198 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002199 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2200 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002201 mcmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002202 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002203 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002204 return;
2205 }
2206
Quinn Tran726b8542017-01-19 22:28:00 -08002207 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2208 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
Quinn Tran41dc5292017-01-19 22:28:03 -08002209 ELS_LOGO ||
2210 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2211 ELS_PRLO ||
2212 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2213 ELS_TPRLO) {
Quinn Tran83548fe2017-06-02 09:12:01 -07002214 ql_dbg(ql_dbg_disc, vha, 0x2106,
Quinn Tran726b8542017-01-19 22:28:00 -08002215 "TM response logo %phC status %#x state %#x",
2216 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2217 mcmd->flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08002218 qlt_schedule_sess_for_deletion(mcmd->sess);
Quinn Tran726b8542017-01-19 22:28:00 -08002219 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002220 qlt_send_notify_ack(vha->hw->base_qpair,
2221 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
Quinn Tran726b8542017-01-19 22:28:00 -08002222 }
2223 } else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05002224 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Quinn Tran82de8022017-06-13 20:47:17 -07002225 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002226 mcmd->fc_tm_rsp, false);
2227 else
Quinn Tran82de8022017-06-13 20:47:17 -07002228 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002229 mcmd->fc_tm_rsp);
2230 }
2231 /*
2232 * Make the callback for ->free_mcmd() to queue_work() and invoke
2233 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2234 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2235 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2236 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2237 * qlt_xmit_tm_rsp() returns here..
2238 */
2239 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002240 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002241}
2242EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2243
2244/* No locks */
2245static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2246{
2247 struct qla_tgt_cmd *cmd = prm->cmd;
2248
2249 BUG_ON(cmd->sg_cnt == 0);
2250
2251 prm->sg = (struct scatterlist *)cmd->sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002252 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002253 cmd->sg_cnt, cmd->dma_data_direction);
2254 if (unlikely(prm->seg_cnt == 0))
2255 goto out_err;
2256
2257 prm->cmd->sg_mapped = 1;
2258
Quinn Tranf83adb62014-04-11 16:54:43 -04002259 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2260 /*
2261 * If greater than four sg entries then we need to allocate
2262 * the continuation entries
2263 */
Quinn Tranb5399f72017-06-13 20:47:27 -07002264 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
Quinn Tranf83adb62014-04-11 16:54:43 -04002265 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
Quinn Tranb5399f72017-06-13 20:47:27 -07002266 QLA_TGT_DATASEGS_PER_CMD_24XX,
2267 QLA_TGT_DATASEGS_PER_CONT_24XX);
Quinn Tranf83adb62014-04-11 16:54:43 -04002268 } else {
2269 /* DIF */
2270 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2271 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2272 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2273 prm->tot_dsds = prm->seg_cnt;
2274 } else
2275 prm->tot_dsds = prm->seg_cnt;
2276
2277 if (cmd->prot_sg_cnt) {
2278 prm->prot_sg = cmd->prot_sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002279 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
Quinn Tranf83adb62014-04-11 16:54:43 -04002280 cmd->prot_sg, cmd->prot_sg_cnt,
2281 cmd->dma_data_direction);
2282 if (unlikely(prm->prot_seg_cnt == 0))
2283 goto out_err;
2284
2285 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2286 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2287 /* Dif Bundling not support here */
2288 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2289 cmd->blk_sz);
2290 prm->tot_dsds += prm->prot_seg_cnt;
2291 } else
2292 prm->tot_dsds += prm->prot_seg_cnt;
2293 }
2294 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002295
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002296 return 0;
2297
2298out_err:
Quinn Tran22d84722017-06-13 20:47:25 -07002299 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002300 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2301 0, prm->cmd->sg_cnt);
2302 return -1;
2303}
2304
Joern Engelf9b67212014-09-16 16:23:18 -04002305static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002306{
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002307 struct qla_hw_data *ha;
2308 struct qla_qpair *qpair;
Joern Engelf9b67212014-09-16 16:23:18 -04002309 if (!cmd->sg_mapped)
2310 return;
2311
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002312 qpair = cmd->qpair;
2313
2314 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2315 cmd->dma_data_direction);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002316 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002317
2318 if (cmd->prot_sg_cnt)
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002319 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
Quinn Tranf83adb62014-04-11 16:54:43 -04002320 cmd->dma_data_direction);
2321
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002322 if (!cmd->ctx)
2323 return;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002324 ha = vha->hw;
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002325 if (cmd->ctx_dsd_alloced)
2326 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2327
2328 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002329}
2330
Quinn Tran82de8022017-06-13 20:47:17 -07002331static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002332 uint32_t req_cnt)
2333{
Quinn Trand63b3282017-06-02 09:12:07 -07002334 uint32_t cnt;
Quinn Tran82de8022017-06-13 20:47:17 -07002335 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002336
Quinn Tran82de8022017-06-13 20:47:17 -07002337 if (req->cnt < (req_cnt + 2)) {
Quinn Tranaf7bb382017-06-13 20:47:23 -07002338 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2339 RD_REG_DWORD_RELAXED(req->req_q_out));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002340
Quinn Tran82de8022017-06-13 20:47:17 -07002341 if (req->ring_index < cnt)
2342 req->cnt = cnt - req->ring_index;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002343 else
Quinn Tran82de8022017-06-13 20:47:17 -07002344 req->cnt = req->length - (req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002345
Quinn Tran82de8022017-06-13 20:47:17 -07002346 if (unlikely(req->cnt < (req_cnt + 2)))
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002347 return -EAGAIN;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002348 }
2349
Quinn Tran82de8022017-06-13 20:47:17 -07002350 req->cnt -= req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002351
2352 return 0;
2353}
2354
2355/*
2356 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2357 */
Quinn Tran82de8022017-06-13 20:47:17 -07002358static inline void *qlt_get_req_pkt(struct req_que *req)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002359{
2360 /* Adjust ring index. */
Quinn Tran82de8022017-06-13 20:47:17 -07002361 req->ring_index++;
2362 if (req->ring_index == req->length) {
2363 req->ring_index = 0;
2364 req->ring_ptr = req->ring;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002365 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002366 req->ring_ptr++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002367 }
Quinn Tran82de8022017-06-13 20:47:17 -07002368 return (cont_entry_t *)req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002369}
2370
2371/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002372static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002373{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002374 uint32_t h;
Quinn Tranc5419e22017-06-13 20:47:16 -07002375 int index;
2376 uint8_t found = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07002377 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002378
Quinn Tranc5419e22017-06-13 20:47:16 -07002379 h = req->current_outstanding_cmd;
2380
2381 for (index = 1; index < req->num_outstanding_cmds; index++) {
2382 h++;
2383 if (h == req->num_outstanding_cmds)
2384 h = 1;
2385
2386 if (h == QLA_TGT_SKIP_HANDLE)
2387 continue;
2388
2389 if (!req->outstanding_cmds[h]) {
2390 found = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002391 break;
2392 }
Quinn Tranc5419e22017-06-13 20:47:16 -07002393 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002394
Quinn Tranc5419e22017-06-13 20:47:16 -07002395 if (found) {
2396 req->current_outstanding_cmd = h;
2397 } else {
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002398 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2399 "qla_target(%d): Ran out of empty cmd slots\n",
2400 qpair->vha->vp_idx);
Quinn Tranc5419e22017-06-13 20:47:16 -07002401 h = QLA_TGT_NULL_HANDLE;
2402 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002403
2404 return h;
2405}
2406
2407/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002408static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2409 struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002410{
2411 uint32_t h;
2412 struct ctio7_to_24xx *pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002413 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002414 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002415
Quinn Tran82de8022017-06-13 20:47:17 -07002416 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002417 prm->pkt = pkt;
2418 memset(pkt, 0, sizeof(*pkt));
2419
2420 pkt->entry_type = CTIO_TYPE7;
2421 pkt->entry_count = (uint8_t)prm->req_cnt;
Quinn Tran22d84722017-06-13 20:47:25 -07002422 pkt->vp_index = prm->cmd->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002423
Quinn Tran82de8022017-06-13 20:47:17 -07002424 h = qlt_make_handle(qpair);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002425 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2426 /*
2427 * CTIO type 7 from the firmware doesn't provide a way to
2428 * know the initiator's LOOP ID, hence we can't find
2429 * the session and, so, the command.
2430 */
2431 return -EAGAIN;
Quinn Trane326d222017-06-13 20:47:18 -07002432 } else
2433 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002434
Quinn Tran82de8022017-06-13 20:47:17 -07002435 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2436 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2437 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002438 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002439 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2440 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2441 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2442 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002443 temp = atio->u.isp24.attr << 9;
2444 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002445 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2446 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002447 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2448
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002449 return 0;
2450}
2451
2452/*
2453 * ha->hardware_lock supposed to be held on entry. We have already made sure
2454 * that there is sufficient amount of request entries to not drop it.
2455 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002456static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002457{
2458 int cnt;
2459 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002460
2461 /* Build continuation packets */
2462 while (prm->seg_cnt > 0) {
2463 cont_a64_entry_t *cont_pkt64 =
Quinn Tran82de8022017-06-13 20:47:17 -07002464 (cont_a64_entry_t *)qlt_get_req_pkt(
2465 prm->cmd->qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002466
2467 /*
2468 * Make sure that from cont_pkt64 none of
2469 * 64-bit specific fields used for 32-bit
2470 * addressing. Cast to (cont_entry_t *) for
2471 * that.
2472 */
2473
2474 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2475
2476 cont_pkt64->entry_count = 1;
2477 cont_pkt64->sys_define = 0;
2478
Quinn Tran32d29b42017-06-13 20:47:26 -07002479 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2480 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002481
2482 /* Load continuation entry data segments */
2483 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002484 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002485 cnt++, prm->seg_cnt--) {
2486 *dword_ptr++ =
2487 cpu_to_le32(pci_dma_lo32
2488 (sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002489 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2490 (sg_dma_address(prm->sg)));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002491 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2492
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002493 prm->sg = sg_next(prm->sg);
2494 }
2495 }
2496}
2497
2498/*
2499 * ha->hardware_lock supposed to be held on entry. We have already made sure
2500 * that there is sufficient amount of request entries to not drop it.
2501 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002502static void qlt_load_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002503{
2504 int cnt;
2505 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002506 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2507
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002508 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2509
2510 /* Setup packet address segment pointer */
2511 dword_ptr = pkt24->u.status0.dseg_0_address;
2512
2513 /* Set total data segment count */
2514 if (prm->seg_cnt)
2515 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2516
2517 if (prm->seg_cnt == 0) {
2518 /* No data transfer */
2519 *dword_ptr++ = 0;
2520 *dword_ptr = 0;
2521 return;
2522 }
2523
2524 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002525
2526 /* Load command entry data segments */
2527 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002528 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002529 cnt++, prm->seg_cnt--) {
2530 *dword_ptr++ =
2531 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002532
2533 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2534 sg_dma_address(prm->sg)));
2535
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002536 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2537
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002538 prm->sg = sg_next(prm->sg);
2539 }
2540
Quinn Tran32d29b42017-06-13 20:47:26 -07002541 qlt_load_cont_data_segments(prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002542}
2543
2544static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2545{
2546 return cmd->bufflen > 0;
2547}
2548
Quinn Tranbe251522017-03-15 09:48:49 -07002549static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2550{
2551 struct qla_tgt_cmd *cmd;
2552 struct scsi_qla_host *vha;
2553
2554 /* asc 0x10=dif error */
2555 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2556 cmd = prm->cmd;
2557 vha = cmd->vha;
2558 /* ASCQ */
2559 switch (prm->sense_buffer[13]) {
2560 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002561 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002562 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2563 "se_cmd=%p tag[%x]",
2564 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2565 cmd->atio.u.isp24.exchange_addr);
2566 break;
2567 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002568 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002569 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2570 "se_cmd=%p tag[%x]",
2571 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2572 cmd->atio.u.isp24.exchange_addr);
2573 break;
2574 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002575 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002576 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2577 "se_cmd=%p tag[%x]",
2578 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2579 cmd->atio.u.isp24.exchange_addr);
2580 break;
2581 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002582 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002583 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2584 "se_cmd=%p tag[%x]",
2585 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2586 cmd->atio.u.isp24.exchange_addr);
2587 break;
2588 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002589 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002590 }
2591}
2592
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002593/*
2594 * Called without ha->hardware_lock held
2595 */
2596static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2597 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2598 uint32_t *full_req_cnt)
2599{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002600 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Tran22d84722017-06-13 20:47:25 -07002601 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002602
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002603 prm->cmd = cmd;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002604 prm->tgt = cmd->tgt;
2605 prm->pkt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002606 prm->rq_result = scsi_status;
2607 prm->sense_buffer = &cmd->sense_buffer[0];
2608 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2609 prm->sg = NULL;
2610 prm->seg_cnt = -1;
2611 prm->req_cnt = 1;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002612 prm->residual = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002613 prm->add_status_pkt = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002614 prm->prot_sg = NULL;
2615 prm->prot_seg_cnt = 0;
2616 prm->tot_dsds = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002617
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002618 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2619 if (qlt_pci_map_calc_cnt(prm) != 0)
2620 return -EAGAIN;
2621 }
2622
2623 *full_req_cnt = prm->req_cnt;
2624
2625 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2626 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002627 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002628 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2629 prm->residual, se_cmd->tag,
2630 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2631 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002632 prm->rq_result |= SS_RESIDUAL_UNDER;
2633 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2634 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002635 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002636 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2637 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2638 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002639 prm->rq_result |= SS_RESIDUAL_OVER;
2640 }
2641
2642 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2643 /*
2644 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2645 * ignored in *xmit_response() below
2646 */
2647 if (qlt_has_data(cmd)) {
2648 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002649 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002650 (prm->rq_result != 0))) {
2651 prm->add_status_pkt = 1;
2652 (*full_req_cnt)++;
2653 }
2654 }
2655 }
2656
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002657 return 0;
2658}
2659
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002660static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2661 int sending_sense)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002662{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002663 if (cmd->qpair->enable_class_2)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002664 return 0;
2665
2666 if (sending_sense)
2667 return cmd->conf_compl_supported;
2668 else
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002669 return cmd->qpair->enable_explicit_conf &&
2670 cmd->conf_compl_supported;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002671}
2672
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002673static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2674 struct qla_tgt_prm *prm)
2675{
2676 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2677 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002678 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002679 if (qlt_need_explicit_conf(prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002680 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002681 CTIO7_FLAGS_EXPLICIT_CONFORM |
2682 CTIO7_FLAGS_CONFORM_REQ);
2683 }
2684 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2685 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2686 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2687 int i;
2688
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002689 if (qlt_need_explicit_conf(prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002690 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Quinn Tran22d84722017-06-13 20:47:25 -07002691 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002692 "Skipping EXPLICIT_CONFORM and "
2693 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2694 "non GOOD status\n");
2695 goto skip_explict_conf;
2696 }
Bart Van Asschead950362015-07-09 07:24:08 -07002697 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002698 CTIO7_FLAGS_EXPLICIT_CONFORM |
2699 CTIO7_FLAGS_CONFORM_REQ);
2700 }
2701skip_explict_conf:
2702 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002703 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002704 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002705 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002706 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002707 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002708 ctio->u.status1.sense_length =
2709 cpu_to_le16(prm->sense_buffer_len);
2710 for (i = 0; i < prm->sense_buffer_len/4; i++)
2711 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2712 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002713
2714 qlt_print_dif_err(prm);
2715
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002716 } else {
2717 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002718 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002719 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002720 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002721 ctio->u.status1.sense_length = 0;
2722 memset(ctio->u.status1.sense_data, 0,
2723 sizeof(ctio->u.status1.sense_data));
2724 }
2725
2726 /* Sense with len > 24, is it possible ??? */
2727}
2728
Quinn Tranf83adb62014-04-11 16:54:43 -04002729static inline int
2730qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2731{
Quinn Tranf83adb62014-04-11 16:54:43 -04002732 switch (se_cmd->prot_op) {
2733 case TARGET_PROT_DOUT_INSERT:
2734 case TARGET_PROT_DIN_STRIP:
2735 if (ql2xenablehba_err_chk >= 1)
2736 return 1;
2737 break;
2738 case TARGET_PROT_DOUT_PASS:
2739 case TARGET_PROT_DIN_PASS:
2740 if (ql2xenablehba_err_chk >= 2)
2741 return 1;
2742 break;
2743 case TARGET_PROT_DIN_INSERT:
2744 case TARGET_PROT_DOUT_STRIP:
2745 return 1;
2746 default:
2747 break;
2748 }
2749 return 0;
2750}
2751
Quinn Tranbe251522017-03-15 09:48:49 -07002752static inline int
2753qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002754{
Quinn Tranbe251522017-03-15 09:48:49 -07002755 switch (se_cmd->prot_op) {
2756 case TARGET_PROT_DIN_INSERT:
2757 case TARGET_PROT_DOUT_INSERT:
2758 case TARGET_PROT_DIN_STRIP:
2759 case TARGET_PROT_DOUT_STRIP:
2760 case TARGET_PROT_DIN_PASS:
2761 case TARGET_PROT_DOUT_PASS:
2762 return 1;
2763 default:
2764 return 0;
2765 }
2766 return 0;
2767}
Quinn Tranf83adb62014-04-11 16:54:43 -04002768
Quinn Tranbe251522017-03-15 09:48:49 -07002769/*
2770 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2771 */
2772static void
2773qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2774 uint16_t *pfw_prot_opts)
2775{
2776 struct se_cmd *se_cmd = &cmd->se_cmd;
2777 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2778 scsi_qla_host_t *vha = cmd->tgt->vha;
2779 struct qla_hw_data *ha = vha->hw;
2780 uint32_t t32 = 0;
2781
2782 /*
2783 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002784 * have been immplemented by TCM, before AppTag is avail.
2785 * Look for modesense_handlers[]
2786 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002787 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002788 ctx->app_tag_mask[0] = 0x0;
2789 ctx->app_tag_mask[1] = 0x0;
2790
Quinn Tranbe251522017-03-15 09:48:49 -07002791 if (IS_PI_UNINIT_CAPABLE(ha)) {
2792 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2793 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2794 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2795 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2796 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2797 }
2798
2799 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2800
Quinn Tranf83adb62014-04-11 16:54:43 -04002801 switch (se_cmd->prot_type) {
2802 case TARGET_DIF_TYPE0_PROT:
2803 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002804 * No check for ql2xenablehba_err_chk, as it
2805 * would be an I/O error if hba tag generation
2806 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002807 */
2808 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002809 /* enable ALL bytes of the ref tag */
2810 ctx->ref_tag_mask[0] = 0xff;
2811 ctx->ref_tag_mask[1] = 0xff;
2812 ctx->ref_tag_mask[2] = 0xff;
2813 ctx->ref_tag_mask[3] = 0xff;
2814 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002815 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002816 /*
2817 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2818 * REF tag, and 16 bit app tag.
2819 */
2820 ctx->ref_tag = cpu_to_le32(lba);
2821 if (!qla_tgt_ref_mask_check(se_cmd) ||
2822 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2823 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2824 break;
2825 }
2826 /* enable ALL bytes of the ref tag */
2827 ctx->ref_tag_mask[0] = 0xff;
2828 ctx->ref_tag_mask[1] = 0xff;
2829 ctx->ref_tag_mask[2] = 0xff;
2830 ctx->ref_tag_mask[3] = 0xff;
2831 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002832 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002833 /*
2834 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2835 * tag has to match LBA in CDB + N
2836 */
2837 ctx->ref_tag = cpu_to_le32(lba);
2838 if (!qla_tgt_ref_mask_check(se_cmd) ||
2839 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2840 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2841 break;
2842 }
2843 /* enable ALL bytes of the ref tag */
2844 ctx->ref_tag_mask[0] = 0xff;
2845 ctx->ref_tag_mask[1] = 0xff;
2846 ctx->ref_tag_mask[2] = 0xff;
2847 ctx->ref_tag_mask[3] = 0xff;
2848 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002849 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002850 /* For TYPE 3 protection: 16 bit GUARD only */
2851 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2852 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2853 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2854 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002855 }
2856}
2857
Quinn Tranf83adb62014-04-11 16:54:43 -04002858static inline int
Quinn Tran82de8022017-06-13 20:47:17 -07002859qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
Quinn Tranf83adb62014-04-11 16:54:43 -04002860{
2861 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002862 uint32_t transfer_length = 0;
2863 uint32_t data_bytes;
2864 uint32_t dif_bytes;
2865 uint8_t bundling = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04002866 struct crc_context *crc_ctx_pkt = NULL;
2867 struct qla_hw_data *ha;
2868 struct ctio_crc2_to_fw *pkt;
2869 dma_addr_t crc_ctx_dma;
2870 uint16_t fw_prot_opts = 0;
2871 struct qla_tgt_cmd *cmd = prm->cmd;
2872 struct se_cmd *se_cmd = &cmd->se_cmd;
2873 uint32_t h;
2874 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07002875 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002876 uint16_t t16;
Quinn Tran82de8022017-06-13 20:47:17 -07002877 scsi_qla_host_t *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04002878
Quinn Tranf83adb62014-04-11 16:54:43 -04002879 ha = vha->hw;
2880
Quinn Tran82de8022017-06-13 20:47:17 -07002881 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
Quinn Tranf83adb62014-04-11 16:54:43 -04002882 prm->pkt = pkt;
2883 memset(pkt, 0, sizeof(*pkt));
2884
Quinn Tran22d84722017-06-13 20:47:25 -07002885 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
Quinn Tranf83adb62014-04-11 16:54:43 -04002886 "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 -07002887 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
Quinn Tranf83adb62014-04-11 16:54:43 -04002888 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2889
2890 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2891 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2892 bundling = 0;
2893
2894 /* Compute dif len and adjust data len to incude protection */
2895 data_bytes = cmd->bufflen;
2896 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2897
2898 switch (se_cmd->prot_op) {
2899 case TARGET_PROT_DIN_INSERT:
2900 case TARGET_PROT_DOUT_STRIP:
2901 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07002902 if (cmd->prot_sg_cnt)
2903 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04002904 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002905 case TARGET_PROT_DIN_STRIP:
2906 case TARGET_PROT_DOUT_INSERT:
2907 case TARGET_PROT_DIN_PASS:
2908 case TARGET_PROT_DOUT_PASS:
2909 transfer_length = data_bytes + dif_bytes;
2910 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002911 default:
2912 BUG();
2913 break;
2914 }
2915
2916 if (!qlt_hba_err_chk_enabled(se_cmd))
2917 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2918 /* HBA error checking enabled */
2919 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2920 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2921 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2922 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2923 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2924 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2925 }
2926
2927 switch (se_cmd->prot_op) {
2928 case TARGET_PROT_DIN_INSERT:
2929 case TARGET_PROT_DOUT_INSERT:
2930 fw_prot_opts |= PO_MODE_DIF_INSERT;
2931 break;
2932 case TARGET_PROT_DIN_STRIP:
2933 case TARGET_PROT_DOUT_STRIP:
2934 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2935 break;
2936 case TARGET_PROT_DIN_PASS:
2937 case TARGET_PROT_DOUT_PASS:
2938 fw_prot_opts |= PO_MODE_DIF_PASS;
2939 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2940 break;
2941 default:/* Normal Request */
2942 fw_prot_opts |= PO_MODE_DIF_PASS;
2943 break;
2944 }
2945
Quinn Tranf83adb62014-04-11 16:54:43 -04002946 /* ---- PKT ---- */
2947 /* Update entry type to indicate Command Type CRC_2 IOCB */
2948 pkt->entry_type = CTIO_CRC2;
2949 pkt->entry_count = 1;
Quinn Tran22d84722017-06-13 20:47:25 -07002950 pkt->vp_index = cmd->vp_idx;
Quinn Tranf83adb62014-04-11 16:54:43 -04002951
Quinn Tran82de8022017-06-13 20:47:17 -07002952 h = qlt_make_handle(qpair);
Quinn Tranf83adb62014-04-11 16:54:43 -04002953 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2954 /*
2955 * CTIO type 7 from the firmware doesn't provide a way to
2956 * know the initiator's LOOP ID, hence we can't find
2957 * the session and, so, the command.
2958 */
2959 return -EAGAIN;
2960 } else
Quinn Tran82de8022017-06-13 20:47:17 -07002961 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002962
Quinn Tran82de8022017-06-13 20:47:17 -07002963 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2964 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07002965 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002966 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002967 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2968 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2969 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2970 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002971
2972 /* silence compile warning */
2973 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2974 pkt->ox_id = cpu_to_le16(t16);
2975
2976 t16 = (atio->u.isp24.attr << 9);
2977 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002978 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2979
2980 /* Set transfer direction */
2981 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002982 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002983 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002984 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002985
Quinn Tranf83adb62014-04-11 16:54:43 -04002986 pkt->dseg_count = prm->tot_dsds;
2987 /* Fibre channel byte count */
2988 pkt->transfer_length = cpu_to_le32(transfer_length);
2989
Quinn Tranf83adb62014-04-11 16:54:43 -04002990 /* ----- CRC context -------- */
2991
2992 /* Allocate CRC context from global pool */
2993 crc_ctx_pkt = cmd->ctx =
Souptick Joarder501017f2018-02-15 01:40:38 +05302994 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
Quinn Tranf83adb62014-04-11 16:54:43 -04002995
2996 if (!crc_ctx_pkt)
2997 goto crc_queuing_error;
2998
Quinn Tranf83adb62014-04-11 16:54:43 -04002999 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3000 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3001
3002 /* Set handle */
3003 crc_ctx_pkt->handle = pkt->handle;
3004
Quinn Tranbe251522017-03-15 09:48:49 -07003005 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04003006
3007 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
3008 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
3009 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3010
Quinn Tranf83adb62014-04-11 16:54:43 -04003011 if (!bundling) {
3012 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
3013 } else {
3014 /*
3015 * Configure Bundling if we need to fetch interlaving
3016 * protection PCI accesses
3017 */
3018 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3019 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3020 crc_ctx_pkt->u.bundling.dseg_count =
3021 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3022 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
3023 }
3024
3025 /* Finish the common fields of CRC pkt */
3026 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3027 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3028 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07003029 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003030
Quinn Tranbe251522017-03-15 09:48:49 -07003031 memset((uint8_t *)&tc, 0 , sizeof(tc));
3032 tc.vha = vha;
3033 tc.blk_sz = cmd->blk_sz;
3034 tc.bufflen = cmd->bufflen;
3035 tc.sg = cmd->sg;
3036 tc.prot_sg = cmd->prot_sg;
3037 tc.ctx = crc_ctx_pkt;
3038 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04003039
3040 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07003041 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04003042
3043 if (!bundling && prm->prot_seg_cnt) {
3044 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003045 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003046 goto crc_queuing_error;
3047 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003048 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003049 goto crc_queuing_error;
3050
3051 if (bundling && prm->prot_seg_cnt) {
3052 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003053 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04003054
3055 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3056 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003057 prm->prot_seg_cnt, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003058 goto crc_queuing_error;
3059 }
3060 return QLA_SUCCESS;
3061
3062crc_queuing_error:
3063 /* Cleanup will be performed by the caller */
Quinn Tran82de8022017-06-13 20:47:17 -07003064 qpair->req->outstanding_cmds[h] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003065
3066 return QLA_FUNCTION_FAILED;
3067}
3068
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003069/*
3070 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3071 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3072 */
3073int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3074 uint8_t scsi_status)
3075{
3076 struct scsi_qla_host *vha = cmd->vha;
Quinn Tran82de8022017-06-13 20:47:17 -07003077 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003078 struct ctio7_to_24xx *pkt;
3079 struct qla_tgt_prm prm;
3080 uint32_t full_req_cnt = 0;
3081 unsigned long flags = 0;
3082 int res;
3083
Quinn Tran726b8542017-01-19 22:28:00 -08003084 if (cmd->sess && cmd->sess->deleted) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003085 cmd->state = QLA_TGT_STATE_PROCESSED;
3086 if (cmd->sess->logout_completed)
3087 /* no need to terminate. FW already freed exchange. */
3088 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3089 else
Quinn Tran82de8022017-06-13 20:47:17 -07003090 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003091 return 0;
3092 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003093
Quinn Tran22d84722017-06-13 20:47:25 -07003094 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
Quinn Tran82de8022017-06-13 20:47:17 -07003095 "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 -04003096 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3097 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
Quinn Tran82de8022017-06-13 20:47:17 -07003098 &cmd->se_cmd, qpair->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003099
3100 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3101 &full_req_cnt);
3102 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003103 return res;
3104 }
3105
Quinn Tran82de8022017-06-13 20:47:17 -07003106 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003107
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003108 if (xmit_type == QLA_TGT_XMIT_STATUS)
Quinn Tran60a9ead2017-06-13 20:47:28 -07003109 qpair->tgt_counters.core_qla_snd_status++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003110 else
Quinn Tran60a9ead2017-06-13 20:47:28 -07003111 qpair->tgt_counters.core_qla_que_buf++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003112
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003113 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003114 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003115 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003116 * previous life, just abort the processing.
3117 */
3118 cmd->state = QLA_TGT_STATE_PROCESSED;
3119 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003120 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003121 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3122 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003123 cmd->reset_count, qpair->chip_reset);
Quinn Tran82de8022017-06-13 20:47:17 -07003124 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04003125 return 0;
3126 }
3127
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003128 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003129 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003130 if (unlikely(res))
3131 goto out_unmap_unlock;
3132
Quinn Tranf83adb62014-04-11 16:54:43 -04003133 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
Quinn Tran82de8022017-06-13 20:47:17 -07003134 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003135 else
Quinn Tran82de8022017-06-13 20:47:17 -07003136 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Quinn Tran810e30b2015-06-10 11:05:20 -04003137 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003138 qpair->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003139 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003140 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003141
3142 pkt = (struct ctio7_to_24xx *)prm.pkt;
3143
3144 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3145 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003146 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003147 CTIO7_FLAGS_STATUS_MODE_0);
3148
Quinn Tranf83adb62014-04-11 16:54:43 -04003149 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003150 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003151
3152 if (prm.add_status_pkt == 0) {
3153 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3154 pkt->u.status0.scsi_status =
3155 cpu_to_le16(prm.rq_result);
3156 pkt->u.status0.residual =
3157 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003158 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003159 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003160 if (qlt_need_explicit_conf(cmd, 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003161 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003162 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003163 CTIO7_FLAGS_EXPLICIT_CONFORM |
3164 CTIO7_FLAGS_CONFORM_REQ);
3165 }
3166 }
3167
3168 } else {
3169 /*
3170 * We have already made sure that there is sufficient
3171 * amount of request entries to not drop HW lock in
3172 * req_pkt().
3173 */
3174 struct ctio7_to_24xx *ctio =
Quinn Tran82de8022017-06-13 20:47:17 -07003175 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3176 qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003177
Quinn Tran22d84722017-06-13 20:47:25 -07003178 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
Arun Easi667024a2014-09-25 06:14:47 -04003179 "Building additional status packet 0x%p.\n",
3180 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003181
Quinn Tranf83adb62014-04-11 16:54:43 -04003182 /*
3183 * T10Dif: ctio_crc2_to_fw overlay ontop of
3184 * ctio7_to_24xx
3185 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003186 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003187 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003188 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003189 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003190 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003191 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003192 CTIO7_FLAGS_DATA_IN);
3193
3194 /* Real finish is ctio_m1's finish */
3195 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003196 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003197 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003198
3199 /* qlt_24xx_init_ctio_to_isp will correct
3200 * all neccessary fields that's part of CTIO7.
3201 * There should be no residual of CTIO-CRC2 data.
3202 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003203 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3204 &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003205 }
3206 } else
3207 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3208
3209
3210 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003211 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003212
Himanshu Madhani63163e02014-09-25 06:14:59 -04003213 /* Memory Barrier */
3214 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003215 if (qpair->reqq_start_iocbs)
3216 qpair->reqq_start_iocbs(qpair);
3217 else
3218 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003219 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003220
3221 return 0;
3222
3223out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003224 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003225 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003226
3227 return res;
3228}
3229EXPORT_SYMBOL(qlt_xmit_response);
3230
3231int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3232{
3233 struct ctio7_to_24xx *pkt;
3234 struct scsi_qla_host *vha = cmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003235 struct qla_tgt *tgt = cmd->tgt;
3236 struct qla_tgt_prm prm;
Quinn Tran82de8022017-06-13 20:47:17 -07003237 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003238 int res = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003239 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003240
3241 memset(&prm, 0, sizeof(prm));
3242 prm.cmd = cmd;
3243 prm.tgt = tgt;
3244 prm.sg = NULL;
3245 prm.req_cnt = 1;
3246
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003247 /* Calculate number of entries and segments required */
3248 if (qlt_pci_map_calc_cnt(&prm) != 0)
3249 return -EAGAIN;
3250
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003251 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003252 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003253 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003254 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003255 * previous life, just abort the processing.
3256 */
3257 cmd->state = QLA_TGT_STATE_NEED_DATA;
3258 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003259 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003260 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3261 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003262 cmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003263 return 0;
3264 }
3265
Quinn Tran82de8022017-06-13 20:47:17 -07003266 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003267 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003268 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003269 if (res != 0)
3270 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003271 if (cmd->se_cmd.prot_op)
Quinn Tran82de8022017-06-13 20:47:17 -07003272 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003273 else
Quinn Tran82de8022017-06-13 20:47:17 -07003274 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003275
Quinn Tran810e30b2015-06-10 11:05:20 -04003276 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003277 qpair->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003278 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003279 }
3280
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003281 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003282 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003283 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003284
3285 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003286 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003287
3288 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003289 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003290
Himanshu Madhani63163e02014-09-25 06:14:59 -04003291 /* Memory Barrier */
3292 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003293 if (qpair->reqq_start_iocbs)
3294 qpair->reqq_start_iocbs(qpair);
3295 else
3296 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003297 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003298
3299 return res;
3300
3301out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003302 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003303 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003304
3305 return res;
3306}
3307EXPORT_SYMBOL(qlt_rdy_to_xfer);
3308
Quinn Tranf83adb62014-04-11 16:54:43 -04003309
3310/*
Quinn Tranbe251522017-03-15 09:48:49 -07003311 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003312 */
Quinn Tranbe251522017-03-15 09:48:49 -07003313static void
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003314qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003315 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003316{
3317 uint8_t *ap = &sts->actual_dif[0];
3318 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003319 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003320 uint8_t scsi_status, sense_key, asc, ascq;
3321 unsigned long flags;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003322 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003323
Quinn Tranbe251522017-03-15 09:48:49 -07003324 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003325
Quinn Tranbe251522017-03-15 09:48:49 -07003326 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3327 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3328 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003329
Quinn Tranbe251522017-03-15 09:48:49 -07003330 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3331 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3332 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003333
Quinn Tranbe251522017-03-15 09:48:49 -07003334 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3335 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003336
Quinn Tranbe251522017-03-15 09:48:49 -07003337 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003338
Quinn Tranbe251522017-03-15 09:48:49 -07003339 /* check appl tag */
3340 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003341 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3342 "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]",
3343 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3344 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3345 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3346 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003347
Quinn Tranbe251522017-03-15 09:48:49 -07003348 cmd->dif_err_code = DIF_ERR_APP;
3349 scsi_status = SAM_STAT_CHECK_CONDITION;
3350 sense_key = ABORTED_COMMAND;
3351 asc = 0x10;
3352 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003353 }
3354
3355 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003356 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003357 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3358 "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] ",
3359 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3360 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3361 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3362 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003363
Quinn Tranbe251522017-03-15 09:48:49 -07003364 cmd->dif_err_code = DIF_ERR_REF;
3365 scsi_status = SAM_STAT_CHECK_CONDITION;
3366 sense_key = ABORTED_COMMAND;
3367 asc = 0x10;
3368 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003369 goto out;
3370 }
3371
Quinn Tranbe251522017-03-15 09:48:49 -07003372 /* check guard */
3373 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003374 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3375 "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]",
3376 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3377 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3378 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3379 cmd->atio.u.isp24.fcp_hdr.ox_id);
3380
Quinn Tranbe251522017-03-15 09:48:49 -07003381 cmd->dif_err_code = DIF_ERR_GRD;
3382 scsi_status = SAM_STAT_CHECK_CONDITION;
3383 sense_key = ABORTED_COMMAND;
3384 asc = 0x10;
3385 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003386 }
3387out:
Quinn Tranbe251522017-03-15 09:48:49 -07003388 switch (cmd->state) {
3389 case QLA_TGT_STATE_NEED_DATA:
3390 /* handle_data will load DIF error code */
3391 cmd->state = QLA_TGT_STATE_DATA_IN;
3392 vha->hw->tgt.tgt_ops->handle_data(cmd);
3393 break;
3394 default:
3395 spin_lock_irqsave(&cmd->cmd_lock, flags);
3396 if (cmd->aborted) {
3397 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3398 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3399 break;
3400 }
3401 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003402
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003403 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3404 ascq);
Quinn Tranbe251522017-03-15 09:48:49 -07003405 /* assume scsi status gets out on the wire.
3406 * Will not wait for completion.
3407 */
3408 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3409 break;
3410 }
3411}
Quinn Tranf83adb62014-04-11 16:54:43 -04003412
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003413/* If hardware_lock held on entry, might drop it, then reaquire */
3414/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003415static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3416 struct imm_ntfy_from_isp *ntfy)
3417{
3418 struct nack_to_isp *nack;
3419 struct qla_hw_data *ha = vha->hw;
3420 request_t *pkt;
3421 int ret = 0;
3422
3423 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3424 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3425
Quinn Tranec7193e2017-03-15 09:48:55 -07003426 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003427 if (pkt == NULL) {
3428 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3429 "qla_target(%d): %s failed: unable to allocate "
3430 "request packet\n", vha->vp_idx, __func__);
3431 return -ENOMEM;
3432 }
3433
3434 pkt->entry_type = NOTIFY_ACK_TYPE;
3435 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003436 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003437
3438 nack = (struct nack_to_isp *)pkt;
3439 nack->ox_id = ntfy->ox_id;
3440
3441 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3442 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3443 nack->u.isp24.flags = ntfy->u.isp24.flags &
3444 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3445 }
3446
3447 /* terminate */
3448 nack->u.isp24.flags |=
3449 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3450
3451 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3452 nack->u.isp24.status = ntfy->u.isp24.status;
3453 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3454 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3455 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3456 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3457 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3458 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3459
3460 qla2x00_start_iocbs(vha, vha->req);
3461 return ret;
3462}
3463
3464static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3465 struct imm_ntfy_from_isp *imm, int ha_locked)
3466{
3467 unsigned long flags = 0;
3468 int rc;
3469
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003470 if (ha_locked) {
3471 rc = __qlt_send_term_imm_notif(vha, imm);
3472
3473#if 0 /* Todo */
3474 if (rc == -ENOMEM)
3475 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003476#else
3477 if (rc) {
3478 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003479#endif
3480 goto done;
3481 }
3482
3483 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3484 rc = __qlt_send_term_imm_notif(vha, imm);
3485
3486#if 0 /* Todo */
3487 if (rc == -ENOMEM)
3488 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3489#endif
3490
3491done:
3492 if (!ha_locked)
3493 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3494}
3495
Quinn Tran83548fe2017-06-02 09:12:01 -07003496/*
3497 * If hardware_lock held on entry, might drop it, then reaquire
3498 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3499 */
Quinn Tran82de8022017-06-13 20:47:17 -07003500static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003501 struct qla_tgt_cmd *cmd,
3502 struct atio_from_isp *atio)
3503{
Quinn Tran82de8022017-06-13 20:47:17 -07003504 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003505 struct ctio7_to_24xx *ctio24;
3506 struct qla_hw_data *ha = vha->hw;
3507 request_t *pkt;
3508 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003509 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003510
Quinn Tran83548fe2017-06-02 09:12:01 -07003511 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003512
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003513 if (cmd)
3514 vha = cmd->vha;
3515
Quinn Tran82de8022017-06-13 20:47:17 -07003516 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003517 if (pkt == NULL) {
3518 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3519 "qla_target(%d): %s failed: unable to allocate "
3520 "request packet\n", vha->vp_idx, __func__);
3521 return -ENOMEM;
3522 }
3523
3524 if (cmd != NULL) {
3525 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3526 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3527 "qla_target(%d): Terminating cmd %p with "
3528 "incorrect state %d\n", vha->vp_idx, cmd,
3529 cmd->state);
3530 } else
3531 ret = 1;
3532 }
3533
Quinn Tran60a9ead2017-06-13 20:47:28 -07003534 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003535 pkt->entry_count = 1;
3536 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3537
3538 ctio24 = (struct ctio7_to_24xx *)pkt;
3539 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003540 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003541 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003542 ctio24->vp_index = vha->vp_idx;
3543 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3544 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3545 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3546 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003547 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3548 CTIO7_FLAGS_TERMINATE;
3549 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003550 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3551 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003552
3553 /* Most likely, it isn't needed */
3554 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3555 &atio->u.isp24.fcp_cmnd.add_cdb[
3556 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3557 if (ctio24->u.status1.residual != 0)
3558 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3559
Himanshu Madhani63163e02014-09-25 06:14:59 -04003560 /* Memory Barrier */
3561 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003562 if (qpair->reqq_start_iocbs)
3563 qpair->reqq_start_iocbs(qpair);
3564 else
3565 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003566 return ret;
3567}
3568
Quinn Tran82de8022017-06-13 20:47:17 -07003569static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003570 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3571 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003572{
Quinn Tran82de8022017-06-13 20:47:17 -07003573 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003574 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003575 int rc;
3576
Quinn Tran82de8022017-06-13 20:47:17 -07003577 /* why use different vha? NPIV */
3578 if (cmd)
3579 vha = cmd->vha;
3580 else
3581 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003582
3583 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003584 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003585 if (rc == -ENOMEM)
3586 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003587 goto done;
3588 }
Quinn Tran82de8022017-06-13 20:47:17 -07003589 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3590 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003591 if (rc == -ENOMEM)
3592 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003593
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003594done:
Quinn Trana07100e2015-12-07 19:48:57 -05003595 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003596 if (cmd->sg_mapped)
3597 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003598 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3599 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003600
3601 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003602 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003603
Quinn Tran7b898542014-04-11 16:54:44 -04003604 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003605}
3606
Quinn Tran33e79972014-09-25 06:14:55 -04003607static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3608{
3609 struct list_head free_list;
3610 struct qla_tgt_cmd *cmd, *tcmd;
3611
3612 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003613 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003614
3615 cmd = tcmd = NULL;
3616 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3617 INIT_LIST_HEAD(&free_list);
3618 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3619
3620 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3621 list_del(&cmd->cmd_list);
3622 /* This cmd was never sent to TCM. There is no need
3623 * to schedule free or call free_cmd
3624 */
3625 qlt_free_cmd(cmd);
3626 vha->hw->tgt.num_qfull_cmds_alloc--;
3627 }
3628 }
3629 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3630}
3631
3632static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3633{
3634 uint32_t total_leaked;
3635
3636 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3637
3638 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3639 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3640
3641 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3642 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003643 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003644
3645 if (IS_P3P_TYPE(vha->hw))
3646 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3647 else
3648 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3649 qla2xxx_wake_dpc(vha);
3650 }
3651
3652}
3653
Quinn Trana07100e2015-12-07 19:48:57 -05003654int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003655{
3656 struct qla_tgt *tgt = cmd->tgt;
3657 struct scsi_qla_host *vha = tgt->vha;
3658 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003659 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003660
3661 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3662 "qla_target(%d): terminating exchange for aborted cmd=%p "
3663 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3664 se_cmd->tag);
3665
Quinn Trana07100e2015-12-07 19:48:57 -05003666 spin_lock_irqsave(&cmd->cmd_lock, flags);
3667 if (cmd->aborted) {
3668 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3669 /*
3670 * It's normal to see 2 calls in this path:
3671 * 1) XFER Rdy completion + CMD_T_ABORT
3672 * 2) TCM TMR - drain_state_list
3673 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003674 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3675 "multiple abort. %p transport_state %x, t_state %x, "
3676 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3677 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003678 return EIO;
3679 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003680 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003681 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003682 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003683
Quinn Tran82de8022017-06-13 20:47:17 -07003684 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003685 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003686}
3687EXPORT_SYMBOL(qlt_abort_cmd);
3688
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003689void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3690{
Quinn Tran5d964832017-01-19 22:27:59 -08003691 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003692
Quinn Tranf83adb62014-04-11 16:54:43 -04003693 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3694 "%s: se_cmd[%p] ox_id %04x\n",
3695 __func__, &cmd->se_cmd,
3696 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003697
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003698 BUG_ON(cmd->cmd_in_wq);
3699
Quinn Trana07100e2015-12-07 19:48:57 -05003700 if (cmd->sg_mapped)
3701 qlt_unmap_sg(cmd->vha, cmd);
3702
Quinn Tran33e79972014-09-25 06:14:55 -04003703 if (!cmd->q_full)
3704 qlt_decr_num_pend_cmds(cmd->vha);
3705
Quinn Tranf83adb62014-04-11 16:54:43 -04003706 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003707 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003708 if (unlikely(cmd->free_sg))
3709 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003710
3711 if (!sess || !sess->se_sess) {
3712 WARN_ON(1);
3713 return;
3714 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003715 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003716 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003717}
3718EXPORT_SYMBOL(qlt_free_cmd);
3719
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003720/*
3721 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3722 */
Quinn Tran82de8022017-06-13 20:47:17 -07003723static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003724 struct qla_tgt_cmd *cmd, uint32_t status)
3725{
3726 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003727 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003728
Quinn Tranbe251522017-03-15 09:48:49 -07003729 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003730 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003731 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3732 "se_cmd=%p tag[%x] op %#x/%s",
3733 cmd->lba, cmd->lba,
3734 cmd->num_blks, &cmd->se_cmd,
3735 cmd->atio.u.isp24.exchange_addr,
3736 cmd->se_cmd.prot_op,
3737 prot_op_str(cmd->se_cmd.prot_op));
3738
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003739 if (ctio != NULL) {
3740 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3741 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003742 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003743 } else
3744 term = 1;
3745
3746 if (term)
himanshu.madhani@cavium.com3efc31f2018-01-15 20:46:47 -08003747 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003748
3749 return term;
3750}
3751
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003752
3753/* ha->hardware_lock supposed to be held on entry */
3754static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003755 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003756{
3757 struct qla_tgt_cmd *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003758 struct req_que *req;
3759 int qid = GET_QID(handle);
3760 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003761
Quinn Tran82de8022017-06-13 20:47:17 -07003762 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3763 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003764
Quinn Tran82de8022017-06-13 20:47:17 -07003765 if (qid == rsp->req->id) {
3766 req = rsp->req;
3767 } else if (vha->hw->req_q_map[qid]) {
3768 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3769 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3770 vha->vp_idx, rsp->id, handle);
3771 req = vha->hw->req_q_map[qid];
3772 } else {
3773 return NULL;
3774 }
Arun Easi667024a2014-09-25 06:14:47 -04003775
Quinn Tran82de8022017-06-13 20:47:17 -07003776 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003777
Quinn Tran82de8022017-06-13 20:47:17 -07003778 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003779 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003780 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3781 "qla_target(%d): Wrong handle %x received\n",
3782 vha->vp_idx, handle);
3783 return NULL;
3784 }
Quinn Tran82de8022017-06-13 20:47:17 -07003785
3786 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3787 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003788 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003789 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3790 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003791 return NULL;
3792 }
Quinn Tran82de8022017-06-13 20:47:17 -07003793 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003794 } else if (ctio != NULL) {
3795 /* We can't get loop ID from CTIO7 */
3796 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3797 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3798 "support NULL handles\n", vha->vp_idx);
3799 return NULL;
3800 }
3801
3802 return cmd;
3803}
3804
Arun Easic0cb4492014-09-25 06:14:51 -04003805/* hardware_lock should be held by caller. */
Quinn Tranc5419e22017-06-13 20:47:16 -07003806void
Arun Easic0cb4492014-09-25 06:14:51 -04003807qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3808{
3809 struct qla_hw_data *ha = vha->hw;
Arun Easic0cb4492014-09-25 06:14:51 -04003810
3811 if (cmd->sg_mapped)
3812 qlt_unmap_sg(vha, cmd);
3813
Arun Easic0cb4492014-09-25 06:14:51 -04003814 /* TODO: fix debug message type and ids. */
3815 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3816 ql_dbg(ql_dbg_io, vha, 0xff00,
Quinn Tran82de8022017-06-13 20:47:17 -07003817 "HOST-ABORT: state=PROCESSED.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003818 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3819 cmd->write_data_transferred = 0;
3820 cmd->state = QLA_TGT_STATE_DATA_IN;
3821
3822 ql_dbg(ql_dbg_io, vha, 0xff01,
Quinn Tran82de8022017-06-13 20:47:17 -07003823 "HOST-ABORT: state=DATA_IN.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003824
3825 ha->tgt.tgt_ops->handle_data(cmd);
3826 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003827 } else {
3828 ql_dbg(ql_dbg_io, vha, 0xff03,
Quinn Tran82de8022017-06-13 20:47:17 -07003829 "HOST-ABORT: state=BAD(%d).\n",
Arun Easic0cb4492014-09-25 06:14:51 -04003830 cmd->state);
3831 dump_stack();
3832 }
3833
Quinn Tran1eb42f92017-01-19 22:27:55 -08003834 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003835 ha->tgt.tgt_ops->free_cmd(cmd);
3836}
3837
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003838/*
3839 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3840 */
Quinn Tran82de8022017-06-13 20:47:17 -07003841static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3842 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003843{
3844 struct qla_hw_data *ha = vha->hw;
3845 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003846 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07003847 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003848
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003849 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3850 /* That could happen only in case of an error/reset/abort */
3851 if (status != CTIO_SUCCESS) {
3852 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3853 "Intermediate CTIO received"
3854 " (status %x)\n", status);
3855 }
3856 return;
3857 }
3858
Quinn Tran82de8022017-06-13 20:47:17 -07003859 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003860 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003861 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003862
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003863 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003864 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003865
Joern Engelf9b67212014-09-16 16:23:18 -04003866 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003867
3868 if (unlikely(status != CTIO_SUCCESS)) {
3869 switch (status & 0xFFFF) {
3870 case CTIO_LIP_RESET:
3871 case CTIO_TARGET_RESET:
3872 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003873 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003874 case CTIO_TIMEOUT:
3875 case CTIO_INVALID_RX_ID:
3876 /* They are OK */
3877 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3878 "qla_target(%d): CTIO with "
3879 "status %#x received, state %x, se_cmd %p, "
3880 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3881 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3882 status, cmd->state, se_cmd);
3883 break;
3884
3885 case CTIO_PORT_LOGGED_OUT:
3886 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003887 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003888 int logged_out =
3889 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3890
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003891 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003892 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003893 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003894 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003895 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003896
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003897 if (logged_out && cmd->sess) {
3898 /*
3899 * Session is already logged out, but we need
3900 * to notify initiator, who's not aware of this
3901 */
3902 cmd->sess->logout_on_delete = 0;
3903 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07003904 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08003905 "%s %d %8phC post del sess\n",
3906 __func__, __LINE__, cmd->sess->port_name);
3907
Quinn Trand8630bb2017-12-28 12:33:43 -08003908 qlt_schedule_sess_for_deletion(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003909 }
3910 break;
3911 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003912 case CTIO_DIF_ERROR: {
3913 struct ctio_crc_from_fw *crc =
3914 (struct ctio_crc_from_fw *)ctio;
3915 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07003916 "qla_target(%d): CTIO with DIF_ERROR status %x "
3917 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3918 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003919 vha->vp_idx, status, cmd->state, se_cmd,
3920 *((u64 *)&crc->actual_dif[0]),
3921 *((u64 *)&crc->expected_dif[0]));
3922
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003923 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07003924 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04003925 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003926 default:
3927 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003928 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003929 vha->vp_idx, status, cmd->state, se_cmd);
3930 break;
3931 }
3932
Quinn Tran7b898542014-04-11 16:54:44 -04003933
Quinn Tran193b50b2015-12-17 14:57:03 -05003934 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003935 * cmd is already aborted/terminated, we don't
3936 * need to terminate again. The exchange is already
3937 * cleaned up/freed at FW level. Just cleanup at driver
3938 * level.
3939 */
3940 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003941 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003942 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07003943 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003944 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003945 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003946 }
3947
3948 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003949 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003950 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003951 cmd->state = QLA_TGT_STATE_DATA_IN;
3952
Bart Van Assche52c82822015-07-09 07:23:26 -07003953 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003954 cmd->write_data_transferred = 1;
3955
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003956 ha->tgt.tgt_ops->handle_data(cmd);
3957 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003958 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003959 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003960 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003961 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003962 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003963 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003964 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3965 "qla_target(%d): A command in state (%d) should "
3966 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3967 }
3968
Quinn Tran7b898542014-04-11 16:54:44 -04003969 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003970 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003971 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3972 dump_stack();
3973 }
3974
3975 ha->tgt.tgt_ops->free_cmd(cmd);
3976}
3977
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003978static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3979 uint8_t task_codes)
3980{
3981 int fcp_task_attr;
3982
3983 switch (task_codes) {
3984 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003985 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003986 break;
3987 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003988 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003989 break;
3990 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003991 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003992 break;
3993 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003994 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003995 break;
3996 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003997 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003998 break;
3999 default:
4000 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4001 "qla_target: unknown task code %x, use ORDERED instead\n",
4002 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004003 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004004 break;
4005 }
4006
4007 return fcp_task_attr;
4008}
4009
Quinn Tran5d964832017-01-19 22:27:59 -08004010static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004011 uint8_t *);
4012/*
4013 * Process context for I/O path into tcm_qla2xxx code
4014 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004015static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004016{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004017 scsi_qla_host_t *vha = cmd->vha;
4018 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004019 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004020 struct atio_from_isp *atio = &cmd->atio;
4021 unsigned char *cdb;
4022 unsigned long flags;
4023 uint32_t data_length;
4024 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07004025 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004026
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004027 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004028 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004029
Quinn Tran193b50b2015-12-17 14:57:03 -05004030 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004031 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4032 "cmd with tag %u is aborted\n",
4033 cmd->atio.u.isp24.exchange_addr);
4034 goto out_term;
4035 }
4036
Quinn Trana07100e2015-12-07 19:48:57 -05004037 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004038 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004039 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004040
4041 if (atio->u.isp24.fcp_cmnd.rddata &&
4042 atio->u.isp24.fcp_cmnd.wrdata) {
4043 bidi = 1;
4044 data_dir = DMA_TO_DEVICE;
4045 } else if (atio->u.isp24.fcp_cmnd.rddata)
4046 data_dir = DMA_FROM_DEVICE;
4047 else if (atio->u.isp24.fcp_cmnd.wrdata)
4048 data_dir = DMA_TO_DEVICE;
4049 else
4050 data_dir = DMA_NONE;
4051
4052 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4053 atio->u.isp24.fcp_cmnd.task_attr);
4054 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4055 &atio->u.isp24.fcp_cmnd.add_cdb[
4056 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4057
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004058 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4059 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004060 if (ret != 0)
4061 goto out_term;
4062 /*
4063 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4064 */
Quinn Tran75601512015-12-17 14:57:04 -05004065 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004066 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004067 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004068 return;
4069
4070out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004071 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004072 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004073 * cmd has not sent to target yet, so pass NULL as the second
4074 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004075 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004076 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004077 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4078 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004079
4080 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004081 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Quinn Tran82de8022017-06-13 20:47:17 -07004082 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004083
4084 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004085 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004086 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004087}
4088
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004089static void qlt_do_work(struct work_struct *work)
4090{
4091 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004092 scsi_qla_host_t *vha = cmd->vha;
4093 unsigned long flags;
4094
4095 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4096 list_del(&cmd->cmd_list);
4097 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004098
4099 __qlt_do_work(cmd);
4100}
4101
Quinn Tran09620ee2017-06-13 20:47:20 -07004102void qlt_clr_qp_table(struct scsi_qla_host *vha)
4103{
4104 unsigned long flags;
4105 struct qla_hw_data *ha = vha->hw;
4106 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4107 void *node;
4108 u64 key = 0;
4109
4110 ql_log(ql_log_info, vha, 0x706c,
4111 "User update Number of Active Qpairs %d\n",
4112 ha->tgt.num_act_qpairs);
4113
4114 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4115
4116 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4117 btree_remove64(&tgt->lun_qpair_map, key);
4118
4119 ha->base_qpair->lun_cnt = 0;
4120 for (key = 0; key < ha->max_qpairs; key++)
4121 if (ha->queue_pair_map[key])
4122 ha->queue_pair_map[key]->lun_cnt = 0;
4123
4124 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4125}
4126
Quinn Trane326d222017-06-13 20:47:18 -07004127static void qlt_assign_qpair(struct scsi_qla_host *vha,
4128 struct qla_tgt_cmd *cmd)
4129{
4130 struct qla_qpair *qpair, *qp;
4131 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4132 struct qla_qpair_hint *h;
4133
4134 if (vha->flags.qpairs_available) {
4135 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4136 if (unlikely(!h)) {
4137 /* spread lun to qpair ratio evently */
4138 int lcnt = 0, rc;
4139 struct scsi_qla_host *base_vha =
4140 pci_get_drvdata(vha->hw->pdev);
4141
4142 qpair = vha->hw->base_qpair;
4143 if (qpair->lun_cnt == 0) {
4144 qpair->lun_cnt++;
4145 h = qla_qpair_to_hint(tgt, qpair);
4146 BUG_ON(!h);
4147 rc = btree_insert64(&tgt->lun_qpair_map,
4148 cmd->unpacked_lun, h, GFP_ATOMIC);
4149 if (rc) {
4150 qpair->lun_cnt--;
4151 ql_log(ql_log_info, vha, 0xd037,
4152 "Unable to insert lun %llx into lun_qpair_map\n",
4153 cmd->unpacked_lun);
4154 }
4155 goto out;
4156 } else {
4157 lcnt = qpair->lun_cnt;
4158 }
4159
4160 h = NULL;
4161 list_for_each_entry(qp, &base_vha->qp_list,
4162 qp_list_elem) {
4163 if (qp->lun_cnt == 0) {
4164 qp->lun_cnt++;
4165 h = qla_qpair_to_hint(tgt, qp);
4166 BUG_ON(!h);
4167 rc = btree_insert64(&tgt->lun_qpair_map,
4168 cmd->unpacked_lun, h, GFP_ATOMIC);
4169 if (rc) {
4170 qp->lun_cnt--;
4171 ql_log(ql_log_info, vha, 0xd038,
4172 "Unable to insert lun %llx into lun_qpair_map\n",
4173 cmd->unpacked_lun);
4174 }
4175 qpair = qp;
4176 goto out;
4177 } else {
4178 if (qp->lun_cnt < lcnt) {
4179 lcnt = qp->lun_cnt;
4180 qpair = qp;
4181 continue;
4182 }
4183 }
4184 }
4185 BUG_ON(!qpair);
4186 qpair->lun_cnt++;
4187 h = qla_qpair_to_hint(tgt, qpair);
4188 BUG_ON(!h);
4189 rc = btree_insert64(&tgt->lun_qpair_map,
4190 cmd->unpacked_lun, h, GFP_ATOMIC);
4191 if (rc) {
4192 qpair->lun_cnt--;
4193 ql_log(ql_log_info, vha, 0xd039,
4194 "Unable to insert lun %llx into lun_qpair_map\n",
4195 cmd->unpacked_lun);
4196 }
4197 }
4198 } else {
4199 h = &tgt->qphints[0];
4200 }
4201out:
4202 cmd->qpair = h->qpair;
4203 cmd->se_cmd.cpuid = h->cpuid;
4204}
4205
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004206static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004207 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004208 struct atio_from_isp *atio)
4209{
4210 struct se_session *se_sess = sess->se_sess;
4211 struct qla_tgt_cmd *cmd;
4212 int tag;
4213
4214 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4215 if (tag < 0)
4216 return NULL;
4217
4218 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4219 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004220 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004221 memcpy(&cmd->atio, atio, sizeof(*atio));
4222 cmd->state = QLA_TGT_STATE_NEW;
4223 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004224 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004225 cmd->vha = vha;
4226 cmd->se_cmd.map_tag = tag;
4227 cmd->sess = sess;
4228 cmd->loop_id = sess->loop_id;
4229 cmd->conf_compl_supported = sess->conf_compl_supported;
4230
Quinn Tran1eb42f92017-01-19 22:27:55 -08004231 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004232 cmd->jiffies_at_alloc = get_jiffies_64();
4233
Quinn Trane326d222017-06-13 20:47:18 -07004234 cmd->unpacked_lun = scsilun_to_int(
4235 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4236 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004237 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004238 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004239
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004240 return cmd;
4241}
4242
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004243/* ha->hardware_lock supposed to be held on entry */
4244static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4245 struct atio_from_isp *atio)
4246{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004247 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004248 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004249 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004250 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004251 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08004252 port_id_t id;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004253
4254 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004255 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004256 "New command while device %p is shutting down\n", tgt);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004257 return -ENODEV;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004258 }
4259
Quinn Trana4239942017-12-28 12:33:26 -08004260 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4261 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4262 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4263 if (IS_SW_RESV_ADDR(id))
4264 return -EBUSY;
4265
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004266 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 -08004267 if (unlikely(!sess))
4268 return -EFAULT;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004269
4270 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4271 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004272 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004273 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004274 "New command while old session %p is being deleted\n",
4275 sess);
4276 return -EFAULT;
4277 }
4278
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004279 /*
4280 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4281 */
Quinn Tran726b8542017-01-19 22:28:00 -08004282 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004283 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004284 "%s: kref_get fail, %8phC oxid %x \n",
4285 __func__, sess->port_name,
4286 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4287 return -EFAULT;
4288 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004289
4290 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004291 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004292 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004293 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004294 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4295 ha->tgt.tgt_ops->put_sess(sess);
4296 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004297 return -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004298 }
4299
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004300 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004301 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004302
Quinn Tran726b8542017-01-19 22:28:00 -08004303 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004304 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004305 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004306
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004307 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004308 if (vha->flags.qpairs_available) {
4309 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4310 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004311 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4312 queue_work_on(smp_processor_id(), qla_tgt_wq,
4313 &cmd->work);
4314 else
4315 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4316 &cmd->work);
4317 } else {
4318 queue_work(qla_tgt_wq, &cmd->work);
4319 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004320
Quinn Tran82de8022017-06-13 20:47:17 -07004321 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004322}
4323
4324/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004325static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004326 int fn, void *iocb, int flags)
4327{
4328 struct scsi_qla_host *vha = sess->vha;
4329 struct qla_hw_data *ha = vha->hw;
4330 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004331 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004332 int res;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004333
4334 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4335 if (!mcmd) {
4336 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4337 "qla_target(%d): Allocation of management "
4338 "command failed, some commands and their data could "
4339 "leak\n", vha->vp_idx);
4340 return -ENOMEM;
4341 }
4342 memset(mcmd, 0, sizeof(*mcmd));
4343 mcmd->sess = sess;
4344
4345 if (iocb) {
4346 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4347 sizeof(mcmd->orig_iocb.imm_ntfy));
4348 }
4349 mcmd->tmr_func = fn;
4350 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004351 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07004352 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004353 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004354
4355 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004356 case QLA_TGT_LUN_RESET:
Quinn Tranbe92fc32017-01-19 22:27:54 -08004357 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4358 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004359 }
4360
Quinn Tranbe92fc32017-01-19 22:27:54 -08004361 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004362 if (res != 0) {
4363 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4364 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4365 sess->vha->vp_idx, res);
4366 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4367 return -EFAULT;
4368 }
4369
4370 return 0;
4371}
4372
4373/* ha->hardware_lock supposed to be held on entry */
4374static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4375{
4376 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4377 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004378 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004379 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004380 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004381 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004382
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004383 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004384
4385 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004386 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4387 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004388 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4389
Quinn Tranf775bd12017-06-02 09:11:59 -07004390 unpacked_lun =
4391 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004392
Quinn Tranfb352652017-12-28 12:33:32 -08004393 if (sess == NULL || sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004394 return -EFAULT;
4395
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004396 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4397}
4398
4399/* ha->hardware_lock supposed to be held on entry */
4400static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004401 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004402{
4403 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4404 struct qla_hw_data *ha = vha->hw;
4405 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004406 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004407 int rc;
4408
4409 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4410 if (mcmd == NULL) {
4411 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4412 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4413 vha->vp_idx, __func__);
4414 return -ENOMEM;
4415 }
4416 memset(mcmd, 0, sizeof(*mcmd));
4417
4418 mcmd->sess = sess;
4419 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4420 sizeof(mcmd->orig_iocb.imm_ntfy));
4421
Quinn Tranf775bd12017-06-02 09:11:59 -07004422 unpacked_lun =
4423 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004424 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004425 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004426 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004427
Quinn Tranbe92fc32017-01-19 22:27:54 -08004428 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004429 le16_to_cpu(iocb->u.isp2x.seq_id));
4430 if (rc != 0) {
4431 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4432 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4433 vha->vp_idx, rc);
4434 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4435 return -EFAULT;
4436 }
4437
4438 return 0;
4439}
4440
4441/* ha->hardware_lock supposed to be held on entry */
4442static int qlt_abort_task(struct scsi_qla_host *vha,
4443 struct imm_ntfy_from_isp *iocb)
4444{
4445 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004446 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004447 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004448 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004449
4450 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4451
Quinn Tran75601512015-12-17 14:57:04 -05004452 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004453 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004454 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4455
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004456 if (sess == NULL) {
4457 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4458 "qla_target(%d): task abort for unexisting "
4459 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004460 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004461 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4462 }
4463
4464 return __qlt_abort_task(vha, iocb, sess);
4465}
4466
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004467void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4468{
Quinn Tran726b8542017-01-19 22:28:00 -08004469 if (rc != MBS_COMMAND_COMPLETE) {
4470 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4471 "%s: se_sess %p / sess %p from"
4472 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4473 " LOGO failed: %#x\n",
4474 __func__,
4475 fcport->se_sess,
4476 fcport,
4477 fcport->port_name, fcport->loop_id,
4478 fcport->d_id.b.domain, fcport->d_id.b.area,
4479 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004480 }
Quinn Tran726b8542017-01-19 22:28:00 -08004481
4482 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004483}
4484
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004485/*
4486* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4487*
4488* Schedules sessions with matching port_id/loop_id but different wwn for
4489* deletion. Returns existing session with matching wwn if present.
4490* Null otherwise.
4491*/
Quinn Tran726b8542017-01-19 22:28:00 -08004492struct fc_port *
4493qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004494 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004495{
Quinn Tran5d964832017-01-19 22:27:59 -08004496 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004497 uint64_t other_wwn;
4498
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004499 *conflict_sess = NULL;
4500
Quinn Tran5d964832017-01-19 22:27:59 -08004501 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004502
4503 other_wwn = wwn_to_u64(other_sess->port_name);
4504
4505 if (wwn == other_wwn) {
4506 WARN_ON(sess);
4507 sess = other_sess;
4508 continue;
4509 }
4510
4511 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004512 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004513 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004514 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004515 "Invalidating sess %p loop_id %d wwn %llx.\n",
4516 other_sess, other_sess->loop_id, other_wwn);
4517
4518 /*
4519 * logout_on_delete is set by default, but another
4520 * session that has the same s_id/loop_id combo
4521 * might have cleared it when requested this session
4522 * deletion, so don't touch it
4523 */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004524 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004525 } else {
4526 /*
4527 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004528 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004529 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004530 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004531 "Invalidating sess %p loop_id %d wwn %llx.\n",
4532 other_sess, other_sess->loop_id, other_wwn);
4533
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004534 other_sess->keep_nport_handle = 1;
Quinn Tranba743f92017-12-04 14:45:12 -08004535 if (other_sess->disc_state != DSC_DELETED)
4536 *conflict_sess = other_sess;
Quinn Tran94cff6e2017-12-28 12:33:42 -08004537 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004538 }
4539 continue;
4540 }
4541
4542 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004543 if ((loop_id == other_sess->loop_id) &&
4544 (loop_id != FC_NO_LOOP_ID)) {
4545 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004546 "Invalidating sess %p loop_id %d wwn %llx.\n",
4547 other_sess, other_sess->loop_id, other_wwn);
4548
4549 /* Same loop_id but different s_id
4550 * Ok to kill and logout */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004551 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004552 }
4553 }
4554
4555 return sess;
4556}
4557
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004558/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4559static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4560{
4561 struct qla_tgt_sess_op *op;
4562 struct qla_tgt_cmd *cmd;
4563 uint32_t key;
4564 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004565 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004566
4567 key = (((u32)s_id->b.domain << 16) |
4568 ((u32)s_id->b.area << 8) |
4569 ((u32)s_id->b.al_pa));
4570
Quinn Tran8b631d82017-06-02 09:11:54 -07004571 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004572 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4573 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004574
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004575 if (op_key == key) {
4576 op->aborted = true;
4577 count++;
4578 }
4579 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004580
4581 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4582 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4583 if (op_key == key) {
4584 op->aborted = true;
4585 count++;
4586 }
4587 }
4588
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004589 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4590 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4591 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004592 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004593 count++;
4594 }
4595 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004596 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004597
4598 return count;
4599}
4600
Quinn Tran9cd883f2017-12-28 12:33:24 -08004601static int qlt_handle_login(struct scsi_qla_host *vha,
4602 struct imm_ntfy_from_isp *iocb)
4603{
4604 struct fc_port *sess = NULL, *conflict_sess = NULL;
4605 uint64_t wwn;
4606 port_id_t port_id;
4607 uint16_t loop_id, wd3_lo;
4608 int res = 0;
4609 struct qlt_plogi_ack_t *pla;
4610 unsigned long flags;
4611
4612 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4613
4614 port_id.b.domain = iocb->u.isp24.port_id[2];
4615 port_id.b.area = iocb->u.isp24.port_id[1];
4616 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4617 port_id.b.rsvd_1 = 0;
4618
4619 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4620
4621 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4622 abort_cmds_for_s_id(vha, &port_id);
4623
4624 if (wwn) {
4625 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4626 sess = qlt_find_sess_invalidate_other(vha, wwn,
4627 port_id, loop_id, &conflict_sess);
4628 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4629 }
4630
4631 if (IS_SW_RESV_ADDR(port_id)) {
4632 res = 1;
4633 goto out;
4634 }
4635
4636 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4637 if (!pla) {
4638 qlt_send_term_imm_notif(vha, iocb, 1);
4639 goto out;
4640 }
4641
4642 if (conflict_sess) {
4643 conflict_sess->login_gen++;
4644 qlt_plogi_ack_link(vha, pla, conflict_sess,
4645 QLT_PLOGI_LINK_CONFLICT);
4646 }
4647
4648 if (!sess) {
4649 pla->ref_count++;
4650 ql_dbg(ql_dbg_disc, vha, 0xffff,
4651 "%s %d %8phC post new sess\n",
4652 __func__, __LINE__, iocb->u.isp24.port_name);
Quinn Trana4239942017-12-28 12:33:26 -08004653 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4654 qla24xx_post_newsess_work(vha, &port_id,
4655 iocb->u.isp24.port_name,
4656 iocb->u.isp24.u.plogi.node_name,
4657 pla, FC4_TYPE_UNKNOWN);
4658 else
4659 qla24xx_post_newsess_work(vha, &port_id,
4660 iocb->u.isp24.port_name, NULL,
4661 pla, FC4_TYPE_UNKNOWN);
4662
Quinn Tran9cd883f2017-12-28 12:33:24 -08004663 goto out;
4664 }
4665
4666 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4667 sess->d_id = port_id;
4668 sess->login_gen++;
4669
4670 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4671 sess->fw_login_state = DSC_LS_PRLI_PEND;
4672 sess->local = 0;
4673 sess->loop_id = loop_id;
4674 sess->d_id = port_id;
4675 sess->fw_login_state = DSC_LS_PRLI_PEND;
4676 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4677
4678 if (wd3_lo & BIT_7)
4679 sess->conf_compl_supported = 1;
4680
4681 if ((wd3_lo & BIT_4) == 0)
4682 sess->port_type = FCT_INITIATOR;
4683 else
4684 sess->port_type = FCT_TARGET;
4685
4686 } else
4687 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4688
4689
4690 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4691 "%s %d %8phC DS %d\n",
4692 __func__, __LINE__, sess->port_name, sess->disc_state);
4693
4694 switch (sess->disc_state) {
4695 case DSC_DELETED:
4696 qlt_plogi_ack_unref(vha, pla);
4697 break;
4698
4699 default:
4700 /*
4701 * Under normal circumstances we want to release nport handle
4702 * during LOGO process to avoid nport handle leaks inside FW.
4703 * The exception is when LOGO is done while another PLOGI with
4704 * the same nport handle is waiting as might be the case here.
4705 * Note: there is always a possibily of a race where session
4706 * deletion has already started for other reasons (e.g. ACL
4707 * removal) and now PLOGI arrives:
4708 * 1. if PLOGI arrived in FW after nport handle has been freed,
4709 * FW must have assigned this PLOGI a new/same handle and we
4710 * can proceed ACK'ing it as usual when session deletion
4711 * completes.
4712 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4713 * bit reached it, the handle has now been released. We'll
4714 * get an error when we ACK this PLOGI. Nothing will be sent
4715 * back to initiator. Initiator should eventually retry
4716 * PLOGI and situation will correct itself.
4717 */
4718 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4719 (sess->d_id.b24 == port_id.b24));
4720
4721 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4722 "%s %d %8phC post del sess\n",
4723 __func__, __LINE__, sess->port_name);
4724
4725
Quinn Trand8630bb2017-12-28 12:33:43 -08004726 qlt_schedule_sess_for_deletion(sess);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004727 break;
4728 }
4729out:
4730 return res;
4731}
4732
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004733/*
4734 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4735 */
4736static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4737 struct imm_ntfy_from_isp *iocb)
4738{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004739 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004740 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004741 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004742 uint64_t wwn;
4743 port_id_t port_id;
4744 uint16_t loop_id;
4745 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004746 int res = 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004747 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004748
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004749 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4750
4751 port_id.b.domain = iocb->u.isp24.port_id[2];
4752 port_id.b.area = iocb->u.isp24.port_id[1];
4753 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4754 port_id.b.rsvd_1 = 0;
4755
4756 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4757
Quinn Tran726b8542017-01-19 22:28:00 -08004758 ql_dbg(ql_dbg_disc, vha, 0xf026,
4759 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4760 vha->vp_idx, iocb->u.isp24.port_id[2],
4761 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4762 iocb->u.isp24.status_subcode, loop_id,
4763 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004764
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004765 /* res = 1 means ack at the end of thread
4766 * res = 0 means ack async/later.
4767 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004768 switch (iocb->u.isp24.status_subcode) {
4769 case ELS_PLOGI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004770 res = qlt_handle_login(vha, iocb);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004771 break;
4772
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004773 case ELS_PRLI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004774 if (N2N_TOPO(ha)) {
4775 sess = qla2x00_find_fcport_by_wwpn(vha,
4776 iocb->u.isp24.port_name, 1);
4777
4778 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4779 ql_dbg(ql_dbg_disc, vha, 0xffff,
4780 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4781 __func__, __LINE__,
4782 iocb->u.isp24.port_name);
4783 qlt_send_term_imm_notif(vha, iocb, 1);
4784 break;
4785 }
4786
4787 res = qlt_handle_login(vha, iocb);
4788 break;
4789 }
4790
Quinn Trana4239942017-12-28 12:33:26 -08004791 if (IS_SW_RESV_ADDR(port_id)) {
4792 res = 1;
4793 break;
4794 }
4795
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004796 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4797
Quinn Tran75601512015-12-17 14:57:04 -05004798 if (wwn) {
4799 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004800 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4801 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004802 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4803 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004804
4805 if (conflict_sess) {
Quinn Tran5ef696a2017-12-04 14:45:05 -08004806 switch (conflict_sess->disc_state) {
4807 case DSC_DELETED:
4808 case DSC_DELETE_PEND:
4809 break;
4810 default:
4811 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4812 "PRLI with conflicting sess %p port %8phC\n",
4813 conflict_sess, conflict_sess->port_name);
4814 conflict_sess->fw_login_state =
4815 DSC_LS_PORT_UNAVAIL;
4816 qlt_send_term_imm_notif(vha, iocb, 1);
4817 res = 0;
4818 break;
4819 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004820 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004821
4822 if (sess != NULL) {
Quinn Trana4239942017-12-28 12:33:26 -08004823 bool delete = false;
Quinn Tran82abdca2017-12-28 12:33:22 -08004824 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4825 switch (sess->fw_login_state) {
Quinn Trana4239942017-12-28 12:33:26 -08004826 case DSC_LS_PLOGI_PEND:
Quinn Tran82abdca2017-12-28 12:33:22 -08004827 case DSC_LS_PLOGI_COMP:
4828 case DSC_LS_PRLI_COMP:
4829 break;
4830 default:
Quinn Trana4239942017-12-28 12:33:26 -08004831 delete = true;
4832 break;
4833 }
4834
4835 switch (sess->disc_state) {
4836 case DSC_LOGIN_PEND:
4837 case DSC_GPDB:
Quinn Trana4239942017-12-28 12:33:26 -08004838 case DSC_UPD_FCPORT:
4839 case DSC_LOGIN_COMPLETE:
4840 case DSC_ADISC:
4841 delete = false;
4842 break;
4843 default:
4844 break;
4845 }
4846
4847 if (delete) {
Quinn Tran82abdca2017-12-28 12:33:22 -08004848 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4849 flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004850 /*
4851 * Impatient initiator sent PRLI before last
4852 * PLOGI could finish. Will force him to re-try,
4853 * while last one finishes.
4854 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004855 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004856 "sess %p PRLI received, before plogi ack.\n",
4857 sess);
4858 qlt_send_term_imm_notif(vha, iocb, 1);
4859 res = 0;
4860 break;
4861 }
4862
4863 /*
4864 * This shouldn't happen under normal circumstances,
4865 * since we have deleted the old session during PLOGI
4866 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004867 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004868 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4869 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4870
4871 sess->local = 0;
4872 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08004873 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004874 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004875
4876 if (wd3_lo & BIT_7)
4877 sess->conf_compl_supported = 1;
4878
Quinn Tran726b8542017-01-19 22:28:00 -08004879 if ((wd3_lo & BIT_4) == 0)
4880 sess->port_type = FCT_INITIATOR;
4881 else
4882 sess->port_type = FCT_TARGET;
Quinn Tran82abdca2017-12-28 12:33:22 -08004883
4884 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04004885 }
4886 res = 1; /* send notify ack */
4887
4888 /* Make session global (not used in fabric mode) */
4889 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004890 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004891 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07004892 "%s %d %8phC post nack\n",
4893 __func__, __LINE__, sess->port_name);
4894 qla24xx_post_nack_work(vha, sess, iocb,
4895 SRB_NACK_PRLI);
4896 res = 0;
4897 } else {
4898 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4899 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4900 qla2xxx_wake_dpc(vha);
4901 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004902 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08004903 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004904 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07004905 "%s %d %8phC post nack\n",
4906 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004907 qla24xx_post_nack_work(vha, sess, iocb,
4908 SRB_NACK_PRLI);
4909 res = 0;
4910 }
4911 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004912 break;
4913
Quinn Tran41dc5292017-01-19 22:28:03 -08004914 case ELS_TPRLO:
4915 if (le16_to_cpu(iocb->u.isp24.flags) &
4916 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4917 loop_id = 0xFFFF;
4918 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4919 res = 1;
4920 break;
4921 }
Bart Van Assche81881862017-12-07 16:02:46 -08004922 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004923 case ELS_LOGO:
4924 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08004925 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4926 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4927 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4928
4929 if (sess) {
4930 sess->login_gen++;
4931 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08004932 sess->logo_ack_needed = 1;
4933 memcpy(sess->iocb, iocb, IOCB_SIZE);
4934 }
4935
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004936 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08004937
Quinn Tran83548fe2017-06-02 09:12:01 -07004938 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08004939 "%s: logo %llx res %d sess %p ",
4940 __func__, wwn, res, sess);
4941 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08004942 /*
4943 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4944 * for LOGO_ACK & sess delete
4945 */
Quinn Tran726b8542017-01-19 22:28:00 -08004946 BUG_ON(!sess);
4947 res = 0;
4948 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08004949 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08004950 if (sess) {
Quinn Trand8630bb2017-12-28 12:33:43 -08004951 qlt_schedule_sess_for_deletion(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08004952 res = 0;
4953 }
4954 /* else logo will be ack */
4955 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004956 break;
4957 case ELS_PDISC:
4958 case ELS_ADISC:
4959 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004960 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004961 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07004962 qlt_send_notify_ack(ha->base_qpair,
4963 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004964 tgt->link_reinit_iocb_pending = 0;
4965 }
Quinn Tran726b8542017-01-19 22:28:00 -08004966
4967 sess = qla2x00_find_fcport_by_wwpn(vha,
4968 iocb->u.isp24.port_name, 1);
4969 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004970 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08004971 "sess %p lid %d|%d DS %d LS %d\n",
4972 sess, sess->loop_id, loop_id,
4973 sess->disc_state, sess->fw_login_state);
4974 }
4975
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004976 res = 1; /* send notify ack */
4977 break;
4978 }
4979
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004980 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004981 default:
4982 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4983 "qla_target(%d): Unsupported ELS command %x "
4984 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4985 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4986 break;
4987 }
4988
Quinn Tran9cd883f2017-12-28 12:33:24 -08004989 ql_dbg(ql_dbg_disc, vha, 0xf026,
4990 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
4991 vha->vp_idx, iocb->u.isp24.status_subcode, res);
4992
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004993 return res;
4994}
4995
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004996/*
4997 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4998 */
4999static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5000 struct imm_ntfy_from_isp *iocb)
5001{
5002 struct qla_hw_data *ha = vha->hw;
5003 uint32_t add_flags = 0;
5004 int send_notify_ack = 1;
5005 uint16_t status;
5006
5007 status = le16_to_cpu(iocb->u.isp2x.status);
5008 switch (status) {
5009 case IMM_NTFY_LIP_RESET:
5010 {
5011 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5012 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5013 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5014 iocb->u.isp24.status_subcode);
5015
5016 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5017 send_notify_ack = 0;
5018 break;
5019 }
5020
5021 case IMM_NTFY_LIP_LINK_REINIT:
5022 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005023 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005024 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5025 "qla_target(%d): LINK REINIT (loop %#x, "
5026 "subcode %x)\n", vha->vp_idx,
5027 le16_to_cpu(iocb->u.isp24.nport_handle),
5028 iocb->u.isp24.status_subcode);
5029 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005030 qlt_send_notify_ack(ha->base_qpair,
5031 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005032 }
5033 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5034 tgt->link_reinit_iocb_pending = 1;
5035 /*
5036 * QLogic requires to wait after LINK REINIT for possible
5037 * PDISC or ADISC ELS commands
5038 */
5039 send_notify_ack = 0;
5040 break;
5041 }
5042
5043 case IMM_NTFY_PORT_LOGOUT:
5044 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5045 "qla_target(%d): Port logout (loop "
5046 "%#x, subcode %x)\n", vha->vp_idx,
5047 le16_to_cpu(iocb->u.isp24.nport_handle),
5048 iocb->u.isp24.status_subcode);
5049
5050 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5051 send_notify_ack = 0;
5052 /* The sessions will be cleared in the callback, if needed */
5053 break;
5054
5055 case IMM_NTFY_GLBL_TPRLO:
5056 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5057 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5058 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5059 send_notify_ack = 0;
5060 /* The sessions will be cleared in the callback, if needed */
5061 break;
5062
5063 case IMM_NTFY_PORT_CONFIG:
5064 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5065 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5066 status);
5067 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5068 send_notify_ack = 0;
5069 /* The sessions will be cleared in the callback, if needed */
5070 break;
5071
5072 case IMM_NTFY_GLBL_LOGO:
5073 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5074 "qla_target(%d): Link failure detected\n",
5075 vha->vp_idx);
5076 /* I_T nexus loss */
5077 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5078 send_notify_ack = 0;
5079 break;
5080
5081 case IMM_NTFY_IOCB_OVERFLOW:
5082 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5083 "qla_target(%d): Cannot provide requested "
5084 "capability (IOCB overflowed the immediate notify "
5085 "resource count)\n", vha->vp_idx);
5086 break;
5087
5088 case IMM_NTFY_ABORT_TASK:
5089 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5090 "qla_target(%d): Abort Task (S %08x I %#x -> "
5091 "L %#x)\n", vha->vp_idx,
5092 le16_to_cpu(iocb->u.isp2x.seq_id),
5093 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5094 le16_to_cpu(iocb->u.isp2x.lun));
5095 if (qlt_abort_task(vha, iocb) == 0)
5096 send_notify_ack = 0;
5097 break;
5098
5099 case IMM_NTFY_RESOURCE:
5100 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5101 "qla_target(%d): Out of resources, host %ld\n",
5102 vha->vp_idx, vha->host_no);
5103 break;
5104
5105 case IMM_NTFY_MSG_RX:
5106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5107 "qla_target(%d): Immediate notify task %x\n",
5108 vha->vp_idx, iocb->u.isp2x.task_flags);
5109 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5110 send_notify_ack = 0;
5111 break;
5112
5113 case IMM_NTFY_ELS:
5114 if (qlt_24xx_handle_els(vha, iocb) == 0)
5115 send_notify_ack = 0;
5116 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005117 default:
5118 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5119 "qla_target(%d): Received unknown immediate "
5120 "notify status %x\n", vha->vp_idx, status);
5121 break;
5122 }
5123
5124 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005125 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5126 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005127}
5128
5129/*
5130 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5131 * This function sends busy to ISP 2xxx or 24xx.
5132 */
Quinn Tran82de8022017-06-13 20:47:17 -07005133static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005134 struct atio_from_isp *atio, uint16_t status)
5135{
Quinn Tran82de8022017-06-13 20:47:17 -07005136 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005137 struct ctio7_to_24xx *ctio24;
5138 struct qla_hw_data *ha = vha->hw;
5139 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005140 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005141 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005142 u16 temp;
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005143 port_id_t id;
5144
5145 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5146 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5147 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5148 id.b.rsvd_1 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005149
Quinn Tran75601512015-12-17 14:57:04 -05005150 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran8ea4faf2018-05-01 09:01:49 -07005151 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
Quinn Tran75601512015-12-17 14:57:04 -05005152 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005153 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005154 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005155 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005156 }
5157 /* Sending marker isn't necessary, since we called from ISR */
5158
Quinn Tran82de8022017-06-13 20:47:17 -07005159 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005160 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005161 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005162 "qla_target(%d): %s failed: unable to allocate "
5163 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005164 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005165 }
5166
Quinn Tran60a9ead2017-06-13 20:47:28 -07005167 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005168 pkt->entry_count = 1;
5169 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5170
5171 ctio24 = (struct ctio7_to_24xx *)pkt;
5172 ctio24->entry_type = CTIO_TYPE7;
5173 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005174 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005175 ctio24->vp_index = vha->vp_idx;
5176 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5177 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5178 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5179 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005180 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005181 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005182 CTIO7_FLAGS_DONT_RET_CTIO;
5183 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005184 /*
5185 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5186 * if the explicit conformation is used.
5187 */
5188 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5189 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005190 /* Memory Barrier */
5191 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005192 if (qpair->reqq_start_iocbs)
5193 qpair->reqq_start_iocbs(qpair);
5194 else
5195 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005196 return 0;
5197}
5198
5199/*
5200 * This routine is used to allocate a command for either a QFull condition
5201 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5202 * out previously.
5203 */
5204static void
5205qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5206 struct atio_from_isp *atio, uint16_t status, int qfull)
5207{
5208 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5209 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005210 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005211 struct se_session *se_sess;
5212 struct qla_tgt_cmd *cmd;
5213 int tag;
Quinn Tran82de8022017-06-13 20:47:17 -07005214 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005215
5216 if (unlikely(tgt->tgt_stop)) {
5217 ql_dbg(ql_dbg_io, vha, 0x300a,
5218 "New command while device %p is shutting down\n", tgt);
5219 return;
5220 }
5221
5222 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5223 vha->hw->tgt.num_qfull_cmds_dropped++;
5224 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005225 vha->qla_stats.stat_max_qfull_cmds_dropped)
5226 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005227 vha->hw->tgt.num_qfull_cmds_dropped;
5228
5229 ql_dbg(ql_dbg_io, vha, 0x3068,
5230 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5231 vha->vp_idx, __func__,
5232 vha->hw->tgt.num_qfull_cmds_dropped);
5233
5234 qlt_chk_exch_leak_thresh_hold(vha);
5235 return;
5236 }
5237
5238 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5239 (vha, atio->u.isp24.fcp_hdr.s_id);
5240 if (!sess)
5241 return;
5242
5243 se_sess = sess->se_sess;
5244
5245 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5246 if (tag < 0)
5247 return;
5248
5249 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5250 if (!cmd) {
5251 ql_dbg(ql_dbg_io, vha, 0x3009,
5252 "qla_target(%d): %s: Allocation of cmd failed\n",
5253 vha->vp_idx, __func__);
5254
5255 vha->hw->tgt.num_qfull_cmds_dropped++;
5256 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005257 vha->qla_stats.stat_max_qfull_cmds_dropped)
5258 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005259 vha->hw->tgt.num_qfull_cmds_dropped;
5260
5261 qlt_chk_exch_leak_thresh_hold(vha);
5262 return;
5263 }
5264
5265 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5266
5267 qlt_incr_num_pend_cmds(vha);
5268 INIT_LIST_HEAD(&cmd->cmd_list);
5269 memcpy(&cmd->atio, atio, sizeof(*atio));
5270
5271 cmd->tgt = vha->vha_tgt.qla_tgt;
5272 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005273 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005274 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005275 cmd->qpair = ha->base_qpair;
Quinn Tran33e79972014-09-25 06:14:55 -04005276
5277 if (qfull) {
5278 cmd->q_full = 1;
5279 /* NOTE: borrowing the state field to carry the status */
5280 cmd->state = status;
5281 } else
5282 cmd->term_exchg = 1;
5283
Quinn Tran82de8022017-06-13 20:47:17 -07005284 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005285 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5286
5287 vha->hw->tgt.num_qfull_cmds_alloc++;
5288 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005289 vha->qla_stats.stat_max_qfull_cmds_alloc)
5290 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005291 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005292 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005293}
5294
5295int
Quinn Tran82de8022017-06-13 20:47:17 -07005296qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005297{
Quinn Tran82de8022017-06-13 20:47:17 -07005298 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005299 struct qla_hw_data *ha = vha->hw;
5300 unsigned long flags;
5301 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005302 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005303 int rc = 0;
5304
5305 if (list_empty(&ha->tgt.q_full_list))
5306 return 0;
5307
5308 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005309 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005310
Quinn Tran82de8022017-06-13 20:47:17 -07005311 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005312 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005313 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005314 return 0;
5315 }
5316
Quinn Tran82de8022017-06-13 20:47:17 -07005317 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5318 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5319
5320 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5321 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005322 if (cmd->q_full)
5323 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005324 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005325 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005326 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005327
5328 if (rc == -ENOMEM)
5329 break;
5330
5331 if (cmd->q_full)
5332 ql_dbg(ql_dbg_io, vha, 0x3006,
5333 "%s: busy sent for ox_id[%04x]\n", __func__,
5334 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5335 else if (cmd->term_exchg)
5336 ql_dbg(ql_dbg_io, vha, 0x3007,
5337 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5338 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5339 else
5340 ql_dbg(ql_dbg_io, vha, 0x3008,
5341 "%s: Unexpected cmd in QFull list %p\n", __func__,
5342 cmd);
5343
5344 list_del(&cmd->cmd_list);
5345 list_add_tail(&cmd->cmd_list, &free_list);
5346
5347 /* piggy back on hardware_lock for protection */
5348 vha->hw->tgt.num_qfull_cmds_alloc--;
5349 }
Quinn Tran82de8022017-06-13 20:47:17 -07005350 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005351
5352 cmd = NULL;
5353
5354 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5355 list_del(&cmd->cmd_list);
5356 /* This cmd was never sent to TCM. There is no need
5357 * to schedule free or call free_cmd
5358 */
5359 qlt_free_cmd(cmd);
5360 }
Quinn Tran82de8022017-06-13 20:47:17 -07005361
5362 if (!list_empty(&q_full_list)) {
5363 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5364 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5365 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5366 }
5367
Quinn Tran33e79972014-09-25 06:14:55 -04005368 return rc;
5369}
5370
5371static void
Quinn Tran82de8022017-06-13 20:47:17 -07005372qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5373 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005374{
5375 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005376 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005377
Quinn Tran82de8022017-06-13 20:47:17 -07005378 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005379 if (rc == -ENOMEM)
5380 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5381}
5382
5383static int
Quinn Tran82de8022017-06-13 20:47:17 -07005384qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5385 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005386{
5387 struct qla_hw_data *ha = vha->hw;
Quinn Tran8b666802017-03-15 09:48:45 -07005388 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005389
5390 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5391 return 0;
5392
Quinn Tran8b666802017-03-15 09:48:45 -07005393 if (!ha_locked)
5394 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005395 qlt_send_busy(qpair, atio, qla_sam_status);
Quinn Tran8b666802017-03-15 09:48:45 -07005396 if (!ha_locked)
5397 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5398
Quinn Tran33e79972014-09-25 06:14:55 -04005399 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005400}
5401
5402/* ha->hardware_lock supposed to be held on entry */
5403/* called via callback from qla2xxx */
5404static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005405 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005406{
5407 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005408 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005409 int rc;
Quinn Tran7cf95f72017-12-28 12:33:28 -08005410 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005411
5412 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005413 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005414 "ATIO pkt, but no tgt (ha %p)", ha);
5415 return;
5416 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005417 /*
5418 * In tgt_stop mode we also should allow all requests to pass.
5419 * Otherwise, some commands can stuck.
5420 */
5421
Quinn Tran2f424b92015-12-17 14:57:07 -05005422 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005423
5424 switch (atio->u.raw.entry_type) {
5425 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005426 if (unlikely(atio->u.isp24.exchange_addr ==
5427 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005428 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005429 "qla_target(%d): ATIO_TYPE7 "
5430 "received with UNKNOWN exchange address, "
5431 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005432 if (!ha_locked)
5433 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005434 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
Quinn Tran2f424b92015-12-17 14:57:07 -05005435 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005436 spin_unlock_irqrestore(&ha->hardware_lock,
5437 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005438 break;
5439 }
Quinn Tran33e79972014-09-25 06:14:55 -04005440
Quinn Tran33e79972014-09-25 06:14:55 -04005441 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005442 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5443 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005444 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005445 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005446 return;
5447 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005448 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005449 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005450 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005451 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005452 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005453 if (!ha_locked)
5454 spin_lock_irqsave(&ha->hardware_lock, flags);
5455 switch (rc) {
5456 case -ENODEV:
5457 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5458 "qla_target: Unable to send command to target\n");
5459 break;
5460 case -EBADF:
5461 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5462 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005463 qlt_send_term_exchange(ha->base_qpair, NULL,
5464 atio, 1, 0);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005465 break;
5466 case -EBUSY:
5467 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5468 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5469 vha->vp_idx);
5470 qlt_send_busy(ha->base_qpair, atio,
5471 tc_sam_status);
5472 break;
5473 default:
5474 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5475 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5476 vha->vp_idx);
5477 qlt_send_busy(ha->base_qpair, atio,
5478 qla_sam_status);
5479 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005480 }
Quinn Tran7cf95f72017-12-28 12:33:28 -08005481 if (!ha_locked)
5482 spin_unlock_irqrestore(&ha->hardware_lock,
5483 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005484 }
5485 break;
5486
5487 case IMMED_NOTIFY_TYPE:
5488 {
5489 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5490 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5491 "qla_target(%d): Received ATIO packet %x "
5492 "with error status %x\n", vha->vp_idx,
5493 atio->u.raw.entry_type,
5494 atio->u.isp2x.entry_status);
5495 break;
5496 }
5497 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005498
5499 if (!ha_locked)
5500 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005501 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005502 if (!ha_locked)
5503 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005504 break;
5505 }
5506
5507 default:
5508 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5509 "qla_target(%d): Received unknown ATIO atio "
5510 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5511 break;
5512 }
5513
Quinn Tran2f424b92015-12-17 14:57:07 -05005514 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005515}
5516
5517/* ha->hardware_lock supposed to be held on entry */
5518/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005519static void qlt_response_pkt(struct scsi_qla_host *vha,
5520 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005521{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005522 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005523
5524 if (unlikely(tgt == NULL)) {
5525 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005526 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5527 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005528 return;
5529 }
5530
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005531 /*
5532 * In tgt_stop mode we also should allow all requests to pass.
5533 * Otherwise, some commands can stuck.
5534 */
5535
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005536 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005537 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005538 case CTIO_TYPE7:
5539 {
5540 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005541 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005542 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5543 entry);
5544 break;
5545 }
5546
5547 case ACCEPT_TGT_IO_TYPE:
5548 {
5549 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5550 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005551 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005552 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005553 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5554 "qla_target(%d): ATIO with error "
5555 "status %x received\n", vha->vp_idx,
5556 le16_to_cpu(atio->u.isp2x.status));
5557 break;
5558 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005559
Quinn Tran82de8022017-06-13 20:47:17 -07005560 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005561 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005562 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005563
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005564 rc = qlt_handle_cmd_for_atio(vha, atio);
5565 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005566 switch (rc) {
5567 case -ENODEV:
5568 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5569 "qla_target: Unable to send command to target\n");
5570 break;
5571 case -EBADF:
5572 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5573 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5574 qlt_send_term_exchange(rsp->qpair, NULL,
5575 atio, 1, 0);
5576 break;
5577 case -EBUSY:
5578 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5579 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5580 vha->vp_idx);
5581 qlt_send_busy(rsp->qpair, atio,
5582 tc_sam_status);
5583 break;
5584 default:
5585 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5586 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5587 vha->vp_idx);
5588 qlt_send_busy(rsp->qpair, atio,
5589 qla_sam_status);
5590 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005591 }
5592 }
5593 }
5594 break;
5595
5596 case CONTINUE_TGT_IO_TYPE:
5597 {
5598 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005599 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005600 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5601 entry);
5602 break;
5603 }
5604
5605 case CTIO_A64_TYPE:
5606 {
5607 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005608 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005609 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5610 entry);
5611 break;
5612 }
5613
5614 case IMMED_NOTIFY_TYPE:
5615 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5616 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5617 break;
5618
5619 case NOTIFY_ACK_TYPE:
5620 if (tgt->notify_ack_expected > 0) {
5621 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5622 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5623 "NOTIFY_ACK seq %08x status %x\n",
5624 le16_to_cpu(entry->u.isp2x.seq_id),
5625 le16_to_cpu(entry->u.isp2x.status));
5626 tgt->notify_ack_expected--;
5627 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005628 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005629 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5630 "qla_target(%d): NOTIFY_ACK "
5631 "failed %x\n", vha->vp_idx,
5632 le16_to_cpu(entry->u.isp2x.status));
5633 }
5634 } else {
5635 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5636 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5637 vha->vp_idx);
5638 }
5639 break;
5640
5641 case ABTS_RECV_24XX:
5642 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5643 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5644 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5645 break;
5646
5647 case ABTS_RESP_24XX:
5648 if (tgt->abts_resp_expected > 0) {
5649 struct abts_resp_from_24xx_fw *entry =
5650 (struct abts_resp_from_24xx_fw *)pkt;
5651 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5652 "ABTS_RESP_24XX: compl_status %x\n",
5653 entry->compl_status);
5654 tgt->abts_resp_expected--;
5655 if (le16_to_cpu(entry->compl_status) !=
5656 ABTS_RESP_COMPL_SUCCESS) {
5657 if ((entry->error_subcode1 == 0x1E) &&
5658 (entry->error_subcode2 == 0)) {
5659 /*
5660 * We've got a race here: aborted
5661 * exchange not terminated, i.e.
5662 * response for the aborted command was
5663 * sent between the abort request was
5664 * received and processed.
5665 * Unfortunately, the firmware has a
5666 * silly requirement that all aborted
5667 * exchanges must be explicitely
5668 * terminated, otherwise it refuses to
5669 * send responses for the abort
5670 * requests. So, we have to
5671 * (re)terminate the exchange and retry
5672 * the abort response.
5673 */
5674 qlt_24xx_retry_term_exchange(vha,
5675 entry);
5676 } else
5677 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5678 "qla_target(%d): ABTS_RESP_24XX "
5679 "failed %x (subcode %x:%x)",
5680 vha->vp_idx, entry->compl_status,
5681 entry->error_subcode1,
5682 entry->error_subcode2);
5683 }
5684 } else {
5685 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5686 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5687 "received\n", vha->vp_idx);
5688 }
5689 break;
5690
5691 default:
5692 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5693 "qla_target(%d): Received unknown response pkt "
5694 "type %x\n", vha->vp_idx, pkt->entry_type);
5695 break;
5696 }
5697
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005698}
5699
5700/*
5701 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5702 */
5703void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5704 uint16_t *mailbox)
5705{
5706 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005707 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005708 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005709
Quinn Tran3a33dc92017-06-02 09:12:04 -07005710 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005711 return;
5712
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005713 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5714 IS_QLA2100(ha))
5715 return;
5716 /*
5717 * In tgt_stop mode we also should allow all requests to pass.
5718 * Otherwise, some commands can stuck.
5719 */
5720
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005721
5722 switch (code) {
5723 case MBA_RESET: /* Reset */
5724 case MBA_SYSTEM_ERR: /* System Error */
5725 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5726 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5727 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5728 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005729 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005730 break;
5731 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5732 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5733 break;
5734
5735 case MBA_LOOP_UP:
5736 {
5737 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005738 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005739 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5740 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5741 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005742 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005743 qlt_send_notify_ack(ha->base_qpair,
5744 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005745 0, 0, 0, 0, 0, 0);
5746 tgt->link_reinit_iocb_pending = 0;
5747 }
5748 break;
5749 }
5750
5751 case MBA_LIP_OCCURRED:
5752 case MBA_LOOP_DOWN:
5753 case MBA_LIP_RESET:
5754 case MBA_RSCN_UPDATE:
5755 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005756 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005757 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5758 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5759 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005760 break;
5761
Quinn Tranead03852017-01-19 22:28:01 -08005762 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005763 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5764 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5765 vha->vp_idx,
5766 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5767 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005768
5769 if (le16_to_cpu(mailbox[3]) == 1) {
5770 /* exchange starvation. */
5771 vha->hw->exch_starvation++;
5772 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005773 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08005774 "Exchange starvation-. Resetting RISC\n");
5775
5776 vha->hw->exch_starvation = 0;
5777 if (IS_P3P_TYPE(vha->hw))
5778 set_bit(FCOE_CTX_RESET_NEEDED,
5779 &vha->dpc_flags);
5780 else
5781 set_bit(ISP_ABORT_NEEDED,
5782 &vha->dpc_flags);
5783 qla2xxx_wake_dpc(vha);
5784 }
5785 }
5786 break;
5787
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005788 case MBA_PORT_UPDATE:
5789 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5790 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005791 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005792 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5793 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5794 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5795
5796 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08005797 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005798 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5799 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08005800 vha->hw->exch_starvation = 0;
5801 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005802 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5803 "Async MB 2: Port Logged Out\n");
5804 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005805 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005806 break;
5807 }
5808
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005809}
5810
5811static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5812 uint16_t loop_id)
5813{
Quinn Tran726b8542017-01-19 22:28:00 -08005814 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005815 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08005816 unsigned long flags;
5817 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005818
Quinn Tran063b36d2017-12-04 14:45:10 -08005819 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005820 if (!fcport) {
5821 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5822 "qla_target(%d): Allocation of tmp FC port failed",
5823 vha->vp_idx);
5824 return NULL;
5825 }
5826
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005827 fcport->loop_id = loop_id;
5828
Quinn Tran15f30a52017-03-15 09:48:52 -07005829 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005830 if (rc != QLA_SUCCESS) {
5831 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5832 "qla_target(%d): Failed to retrieve fcport "
5833 "information -- get_port_database() returned %x "
5834 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5835 kfree(fcport);
5836 return NULL;
5837 }
5838
Quinn Tran726b8542017-01-19 22:28:00 -08005839 del = NULL;
5840 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5841 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5842
5843 if (tfcp) {
5844 tfcp->d_id = fcport->d_id;
5845 tfcp->port_type = fcport->port_type;
5846 tfcp->supported_classes = fcport->supported_classes;
5847 tfcp->flags |= fcport->flags;
Quinn Tran76f9a2d2017-12-04 14:45:11 -08005848 tfcp->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005849
5850 del = fcport;
5851 fcport = tfcp;
5852 } else {
5853 if (vha->hw->current_topology == ISP_CFG_F)
5854 fcport->flags |= FCF_FABRIC_DEVICE;
5855
5856 list_add_tail(&fcport->list, &vha->vp_fcports);
5857 if (!IS_SW_RESV_ADDR(fcport->d_id))
5858 vha->fcport_count++;
5859 fcport->login_gen++;
5860 fcport->disc_state = DSC_LOGIN_COMPLETE;
5861 fcport->login_succ = 1;
5862 newfcport = 1;
5863 }
5864
5865 fcport->deleted = 0;
5866 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5867
5868 switch (vha->host->active_mode) {
5869 case MODE_INITIATOR:
5870 case MODE_DUAL:
5871 if (newfcport) {
5872 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005873 ql_dbg(ql_dbg_disc, vha, 0x20fe,
Quinn Tran726b8542017-01-19 22:28:00 -08005874 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5875 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5876 qla24xx_post_upd_fcport_work(vha, fcport);
5877 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07005878 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08005879 "%s %d %8phC post gpsc fcp_cnt %d\n",
5880 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5881 qla24xx_post_gpsc_work(vha, fcport);
5882 }
5883 }
5884 break;
5885
5886 case MODE_TARGET:
5887 default:
5888 break;
5889 }
5890 if (del)
5891 qla2x00_free_fcport(del);
5892
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005893 return fcport;
5894}
5895
5896/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08005897static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005898 uint8_t *s_id)
5899{
Quinn Tran5d964832017-01-19 22:27:59 -08005900 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005901 fc_port_t *fcport = NULL;
5902 int rc, global_resets;
5903 uint16_t loop_id = 0;
5904
Quinn Tran726b8542017-01-19 22:28:00 -08005905 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5906 /*
5907 * This is Domain Controller, so it should be
5908 * OK to drop SCSI commands from it.
5909 */
5910 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5911 "Unable to find initiator with S_ID %x:%x:%x",
5912 s_id[0], s_id[1], s_id[2]);
5913 return NULL;
5914 }
5915
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005916 mutex_lock(&vha->vha_tgt.tgt_mutex);
5917
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005918retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005919 global_resets =
5920 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005921
5922 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5923 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005924 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5925
Quinn Tran726b8542017-01-19 22:28:00 -08005926 ql_log(ql_log_info, vha, 0xf071,
5927 "qla_target(%d): Unable to find "
5928 "initiator with S_ID %x:%x:%x",
5929 vha->vp_idx, s_id[0], s_id[1],
5930 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005931
5932 if (rc == -ENOENT) {
5933 qlt_port_logo_t logo;
5934 sid_to_portid(s_id, &logo.id);
5935 logo.cmd_count = 1;
5936 qlt_send_first_logo(vha, &logo);
5937 }
5938
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005939 return NULL;
5940 }
5941
5942 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005943 if (!fcport) {
5944 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005945 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005946 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005947
5948 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005949 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005950 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5951 "qla_target(%d): global reset during session discovery "
5952 "(counter was %d, new %d), retrying", vha->vp_idx,
5953 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005954 atomic_read(&vha->vha_tgt.
5955 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005956 goto retry;
5957 }
5958
5959 sess = qlt_create_sess(vha, fcport, true);
5960
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005961 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5962
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005963 return sess;
5964}
5965
5966static void qlt_abort_work(struct qla_tgt *tgt,
5967 struct qla_tgt_sess_work_param *prm)
5968{
5969 struct scsi_qla_host *vha = tgt->vha;
5970 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005971 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005972 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005973 uint32_t be_s_id;
5974 uint8_t s_id[3];
5975 int rc;
5976
Quinn Tran75601512015-12-17 14:57:04 -05005977 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005978
5979 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005980 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005981
5982 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5983 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5984 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5985
5986 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5987 (unsigned char *)&be_s_id);
5988 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005989 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005990
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005991 sess = qlt_make_local_sess(vha, s_id);
5992 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005993
Quinn Tran75601512015-12-17 14:57:04 -05005994 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005995 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005996 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005997 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005998 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005999 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006000 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006001 }
6002
Quinn Tran726b8542017-01-19 22:28:00 -08006003 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006004 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08006005 "%s: kref_get fail %8phC \n",
6006 __func__, sess->port_name);
6007 sess = NULL;
6008 goto out_term2;
6009 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006010 }
6011
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006012 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006013 ha->tgt.tgt_ops->put_sess(sess);
6014 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6015
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006016 if (rc != 0)
6017 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006018 return;
6019
Quinn Tran75601512015-12-17 14:57:04 -05006020out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08006021 if (sess)
6022 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05006023 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006024
6025out_term:
6026 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006027 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6028 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006029 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006030}
6031
6032static void qlt_tmr_work(struct qla_tgt *tgt,
6033 struct qla_tgt_sess_work_param *prm)
6034{
6035 struct atio_from_isp *a = &prm->tm_iocb2;
6036 struct scsi_qla_host *vha = tgt->vha;
6037 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08006038 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006039 unsigned long flags;
6040 uint8_t *s_id = NULL; /* to hide compiler warnings */
6041 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07006042 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07006043 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006044 void *iocb;
6045
Quinn Tran75601512015-12-17 14:57:04 -05006046 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006047
6048 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006049 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006050
6051 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6052 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6053 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006054 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006055
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006056 sess = qlt_make_local_sess(vha, s_id);
6057 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006058
Quinn Tran75601512015-12-17 14:57:04 -05006059 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006060 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006061 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006062 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006063 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006064 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006065 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006066 }
6067
Quinn Tran726b8542017-01-19 22:28:00 -08006068 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006069 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08006070 "%s: kref_get fail %8phC\n",
6071 __func__, sess->port_name);
6072 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006073 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006074 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006075 }
6076
6077 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006078 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006079 unpacked_lun =
6080 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006081
6082 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006083 ha->tgt.tgt_ops->put_sess(sess);
6084 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6085
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006086 if (rc != 0)
6087 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006088 return;
6089
Quinn Tranf159b3c2017-03-15 09:48:47 -07006090out_term2:
6091 if (sess)
6092 ha->tgt.tgt_ops->put_sess(sess);
6093 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006094out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006095 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006096}
6097
6098static void qlt_sess_work_fn(struct work_struct *work)
6099{
6100 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6101 struct scsi_qla_host *vha = tgt->vha;
6102 unsigned long flags;
6103
6104 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6105
6106 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6107 while (!list_empty(&tgt->sess_works_list)) {
6108 struct qla_tgt_sess_work_param *prm = list_entry(
6109 tgt->sess_works_list.next, typeof(*prm),
6110 sess_works_list_entry);
6111
6112 /*
6113 * This work can be scheduled on several CPUs at time, so we
6114 * must delete the entry to eliminate double processing
6115 */
6116 list_del(&prm->sess_works_list_entry);
6117
6118 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6119
6120 switch (prm->type) {
6121 case QLA_TGT_SESS_WORK_ABORT:
6122 qlt_abort_work(tgt, prm);
6123 break;
6124 case QLA_TGT_SESS_WORK_TM:
6125 qlt_tmr_work(tgt, prm);
6126 break;
6127 default:
6128 BUG_ON(1);
6129 break;
6130 }
6131
6132 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6133
6134 kfree(prm);
6135 }
6136 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6137}
6138
6139/* Must be called under tgt_host_action_mutex */
6140int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6141{
6142 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006143 int rc, i;
6144 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006145
6146 if (!QLA_TGT_MODE_ENABLED())
6147 return 0;
6148
Arun Easi33c36c02013-01-30 03:34:41 -05006149 if (!IS_TGT_MODE_CAPABLE(ha)) {
6150 ql_log(ql_log_warn, base_vha, 0xe070,
6151 "This adapter does not support target mode.\n");
6152 return 0;
6153 }
6154
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006155 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006156 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006157
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006158 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006159
6160 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6161 if (!tgt) {
6162 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6163 "Unable to allocate struct qla_tgt\n");
6164 return -ENOMEM;
6165 }
6166
Quinn Trane326d222017-06-13 20:47:18 -07006167 tgt->qphints = kzalloc((ha->max_qpairs + 1) *
6168 sizeof(struct qla_qpair_hint), GFP_KERNEL);
6169 if (!tgt->qphints) {
6170 kfree(tgt);
6171 ql_log(ql_log_warn, base_vha, 0x0197,
6172 "Unable to allocate qpair hints.\n");
6173 return -ENOMEM;
6174 }
6175
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006176 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6177 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6178
Quinn Trane326d222017-06-13 20:47:18 -07006179 rc = btree_init64(&tgt->lun_qpair_map);
6180 if (rc) {
6181 kfree(tgt->qphints);
6182 kfree(tgt);
6183 ql_log(ql_log_info, base_vha, 0x0198,
6184 "Unable to initialize lun_qpair_map btree\n");
6185 return -EIO;
6186 }
6187 h = &tgt->qphints[0];
6188 h->qpair = ha->base_qpair;
6189 INIT_LIST_HEAD(&h->hint_elem);
6190 h->cpuid = ha->base_qpair->cpuid;
6191 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6192
6193 for (i = 0; i < ha->max_qpairs; i++) {
6194 unsigned long flags;
6195
6196 struct qla_qpair *qpair = ha->queue_pair_map[i];
6197 h = &tgt->qphints[i + 1];
6198 INIT_LIST_HEAD(&h->hint_elem);
6199 if (qpair) {
6200 h->qpair = qpair;
6201 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6202 list_add_tail(&h->hint_elem, &qpair->hints_list);
6203 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6204 h->cpuid = qpair->cpuid;
6205 }
6206 }
6207
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006208 tgt->ha = ha;
6209 tgt->vha = base_vha;
6210 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006211 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006212 spin_lock_init(&tgt->sess_work_lock);
6213 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6214 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006215 atomic_set(&tgt->tgt_global_resets_count, 0);
6216
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006217 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006218
6219 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6220 "qla_target(%d): using 64 Bit PCI addressing",
6221 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006222 /* 3 is reserved */
6223 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006224
6225 mutex_lock(&qla_tgt_mutex);
6226 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6227 mutex_unlock(&qla_tgt_mutex);
6228
Quinn Tranf1443ee2017-03-15 09:48:51 -07006229 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6230 ha->tgt.tgt_ops->add_target(base_vha);
6231
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006232 return 0;
6233}
6234
6235/* Must be called under tgt_host_action_mutex */
6236int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6237{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006238 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006239 return 0;
6240
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006241 if (vha->fc_vport) {
6242 qlt_release(vha->vha_tgt.qla_tgt);
6243 return 0;
6244 }
Quinn Tran33e79972014-09-25 06:14:55 -04006245
6246 /* free left over qfull cmds */
6247 qlt_init_term_exchange(vha);
6248
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006249 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6250 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006251 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006252
6253 return 0;
6254}
6255
Quinn Tran482c9dc2017-03-15 09:48:54 -07006256void qlt_remove_target_resources(struct qla_hw_data *ha)
6257{
6258 struct scsi_qla_host *node;
6259 u32 key = 0;
6260
6261 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6262 btree_remove32(&ha->tgt.host_map, key);
6263
6264 btree_destroy32(&ha->tgt.host_map);
6265}
6266
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006267static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6268 unsigned char *b)
6269{
6270 int i;
6271
6272 pr_debug("qla2xxx HW vha->node_name: ");
6273 for (i = 0; i < WWN_SIZE; i++)
6274 pr_debug("%02x ", vha->node_name[i]);
6275 pr_debug("\n");
6276 pr_debug("qla2xxx HW vha->port_name: ");
6277 for (i = 0; i < WWN_SIZE; i++)
6278 pr_debug("%02x ", vha->port_name[i]);
6279 pr_debug("\n");
6280
6281 pr_debug("qla2xxx passed configfs WWPN: ");
6282 put_unaligned_be64(wwpn, b);
6283 for (i = 0; i < WWN_SIZE; i++)
6284 pr_debug("%02x ", b[i]);
6285 pr_debug("\n");
6286}
6287
6288/**
6289 * qla_tgt_lport_register - register lport with external module
6290 *
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006291 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
Bart Van Assche2db62282018-01-23 16:33:51 -08006292 * @phys_wwpn:
6293 * @npiv_wwpn:
6294 * @npiv_wwnn:
6295 * @callback: lport initialization callback for tcm_qla2xxx code
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006296 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006297int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6298 u64 npiv_wwpn, u64 npiv_wwnn,
6299 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006300{
6301 struct qla_tgt *tgt;
6302 struct scsi_qla_host *vha;
6303 struct qla_hw_data *ha;
6304 struct Scsi_Host *host;
6305 unsigned long flags;
6306 int rc;
6307 u8 b[WWN_SIZE];
6308
6309 mutex_lock(&qla_tgt_mutex);
6310 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6311 vha = tgt->vha;
6312 ha = vha->hw;
6313
6314 host = vha->host;
6315 if (!host)
6316 continue;
6317
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006318 if (!(host->hostt->supported_mode & MODE_TARGET))
6319 continue;
6320
6321 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006322 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006323 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6324 host->host_no);
6325 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6326 continue;
6327 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006328 if (tgt->tgt_stop) {
6329 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6330 host->host_no);
6331 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6332 continue;
6333 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006334 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6335
6336 if (!scsi_host_get(host)) {
6337 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6338 "Unable to scsi_host_get() for"
6339 " qla2xxx scsi_host\n");
6340 continue;
6341 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006342 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006343
6344 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6345 scsi_host_put(host);
6346 continue;
6347 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006348 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6349 if (rc != 0)
6350 scsi_host_put(host);
6351
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006352 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006353 return rc;
6354 }
6355 mutex_unlock(&qla_tgt_mutex);
6356
6357 return -ENODEV;
6358}
6359EXPORT_SYMBOL(qlt_lport_register);
6360
6361/**
6362 * qla_tgt_lport_deregister - Degister lport
6363 *
6364 * @vha: Registered scsi_qla_host pointer
6365 */
6366void qlt_lport_deregister(struct scsi_qla_host *vha)
6367{
6368 struct qla_hw_data *ha = vha->hw;
6369 struct Scsi_Host *sh = vha->host;
6370 /*
6371 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6372 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006373 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006374 ha->tgt.tgt_ops = NULL;
6375 /*
6376 * Release the Scsi_Host reference for the underlying qla2xxx host
6377 */
6378 scsi_host_put(sh);
6379}
6380EXPORT_SYMBOL(qlt_lport_deregister);
6381
6382/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006383static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006384{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006385 switch (ql2x_ini_mode) {
6386 case QLA2XXX_INI_MODE_DISABLED:
6387 case QLA2XXX_INI_MODE_EXCLUSIVE:
6388 vha->host->active_mode = MODE_TARGET;
6389 break;
6390 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006391 vha->host->active_mode = MODE_UNKNOWN;
6392 break;
6393 case QLA2XXX_INI_MODE_DUAL:
6394 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006395 break;
6396 default:
6397 break;
6398 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006399}
6400
6401/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006402static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006403{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006404 switch (ql2x_ini_mode) {
6405 case QLA2XXX_INI_MODE_DISABLED:
6406 vha->host->active_mode = MODE_UNKNOWN;
6407 break;
6408 case QLA2XXX_INI_MODE_EXCLUSIVE:
6409 vha->host->active_mode = MODE_INITIATOR;
6410 break;
6411 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006412 case QLA2XXX_INI_MODE_DUAL:
6413 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006414 break;
6415 default:
6416 break;
6417 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006418}
6419
6420/*
6421 * qla_tgt_enable_vha - NO LOCK HELD
6422 *
6423 * host_reset, bring up w/ Target Mode Enabled
6424 */
6425void
6426qlt_enable_vha(struct scsi_qla_host *vha)
6427{
6428 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006429 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006430 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006431 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006432
6433 if (!tgt) {
6434 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6435 "Unable to locate qla_tgt pointer from"
6436 " struct qla_hw_data\n");
6437 dump_stack();
6438 return;
6439 }
6440
6441 spin_lock_irqsave(&ha->hardware_lock, flags);
6442 tgt->tgt_stopped = 0;
6443 qlt_set_mode(vha);
6444 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6445
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006446 if (vha->vp_idx) {
6447 qla24xx_disable_vp(vha);
6448 qla24xx_enable_vp(vha);
6449 } else {
6450 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6451 qla2xxx_wake_dpc(base_vha);
6452 qla2x00_wait_for_hba_online(base_vha);
6453 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006454}
6455EXPORT_SYMBOL(qlt_enable_vha);
6456
6457/*
6458 * qla_tgt_disable_vha - NO LOCK HELD
6459 *
6460 * Disable Target Mode and reset the adapter
6461 */
Joern Engel55a90662014-09-16 16:23:15 -04006462static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006463{
6464 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006465 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006466 unsigned long flags;
6467
6468 if (!tgt) {
6469 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6470 "Unable to locate qla_tgt pointer from"
6471 " struct qla_hw_data\n");
6472 dump_stack();
6473 return;
6474 }
6475
6476 spin_lock_irqsave(&ha->hardware_lock, flags);
6477 qlt_clear_mode(vha);
6478 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6479
6480 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6481 qla2xxx_wake_dpc(vha);
6482 qla2x00_wait_for_hba_online(vha);
6483}
6484
6485/*
6486 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6487 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6488 * members.
6489 */
6490void
6491qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6492{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006493 vha->vha_tgt.qla_tgt = NULL;
6494
6495 mutex_init(&vha->vha_tgt.tgt_mutex);
6496 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006497
6498 qlt_clear_mode(vha);
6499
6500 /*
6501 * NOTE: Currently the value is kept the same for <24xx and
6502 * >=24xx ISPs. If it is necessary to change it,
6503 * the check should be added for specific ISPs,
6504 * assigning the value appropriately.
6505 */
6506 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006507
6508 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006509}
6510
Quinn Trane374f9f2017-12-28 12:33:31 -08006511u8
6512qlt_rff_id(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006513{
Quinn Trane374f9f2017-12-28 12:33:31 -08006514 u8 fc4_feature = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006515 /*
6516 * FC-4 Feature bit 0 indicates target functionality to the name server.
6517 */
6518 if (qla_tgt_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006519 fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006520 } else if (qla_ini_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006521 fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006522 } else if (qla_dual_mode_enabled(vha))
Quinn Trane374f9f2017-12-28 12:33:31 -08006523 fc4_feature = BIT_0 | BIT_1;
6524
6525 return fc4_feature;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006526}
6527
6528/*
6529 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6530 * @ha: HA context
6531 *
6532 * Beginning of ATIO ring has initialization control block already built
6533 * by nvram config routine.
6534 *
6535 * Returns 0 on success.
6536 */
6537void
6538qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6539{
6540 struct qla_hw_data *ha = vha->hw;
6541 uint16_t cnt;
6542 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6543
Quinn Tranead03852017-01-19 22:28:01 -08006544 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006545 return;
6546
6547 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6548 pkt->u.raw.signature = ATIO_PROCESSED;
6549 pkt++;
6550 }
6551
6552}
6553
6554/*
6555 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6556 * @ha: SCSI driver HA context
6557 */
6558void
Quinn Tran2f424b92015-12-17 14:57:07 -05006559qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006560{
6561 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006562 struct atio_from_isp *pkt;
6563 int cnt, i;
6564
Quinn Tranec7193e2017-03-15 09:48:55 -07006565 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006566 return;
6567
Quinn Tran5f355092016-12-23 18:06:11 -08006568 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6569 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006570 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6571 cnt = pkt->u.raw.entry_count;
6572
Quinn Tran5f355092016-12-23 18:06:11 -08006573 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6574 /*
6575 * This packet is corrupted. The header + payload
6576 * can not be trusted. There is no point in passing
6577 * it further up.
6578 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006579 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006580 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6581 pkt->u.isp24.fcp_hdr.s_id,
6582 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6583 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6584
6585 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006586 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6587 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006588 } else {
6589 qlt_24xx_atio_pkt_all_vps(vha,
6590 (struct atio_from_isp *)pkt, ha_locked);
6591 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006592
6593 for (i = 0; i < cnt; i++) {
6594 ha->tgt.atio_ring_index++;
6595 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6596 ha->tgt.atio_ring_index = 0;
6597 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6598 } else
6599 ha->tgt.atio_ring_ptr++;
6600
6601 pkt->u.raw.signature = ATIO_PROCESSED;
6602 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6603 }
6604 wmb();
6605 }
6606
6607 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006608 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006609}
6610
6611void
Arun Easiaa230bc2013-01-30 03:34:39 -05006612qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006613{
6614 struct qla_hw_data *ha = vha->hw;
Quinn Trand2b292c2017-12-28 12:33:17 -08006615 struct qla_msix_entry *msix = &ha->msix_entries[2];
6616 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6617
Arun Easiaa230bc2013-01-30 03:34:39 -05006618 if (!QLA_TGT_MODE_ENABLED())
6619 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006620
Arun Easiaa230bc2013-01-30 03:34:39 -05006621 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6622 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6623 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6624
Quinn Trand2b292c2017-12-28 12:33:17 -08006625 if (ha->flags.msix_enabled) {
6626 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6627 if (IS_QLA2071(ha)) {
6628 /* 4 ports Baker: Enable Interrupt Handshake */
6629 icb->msix_atio = 0;
6630 icb->firmware_options_2 |= BIT_26;
6631 } else {
6632 icb->msix_atio = cpu_to_le16(msix->entry);
6633 icb->firmware_options_2 &= ~BIT_26;
6634 }
6635 ql_dbg(ql_dbg_init, vha, 0xf072,
6636 "Registering ICB vector 0x%x for atio que.\n",
6637 msix->entry);
6638 }
6639 } else {
6640 /* INTx|MSI */
6641 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6642 icb->msix_atio = 0;
6643 icb->firmware_options_2 |= BIT_26;
6644 ql_dbg(ql_dbg_init, vha, 0xf072,
6645 "%s: Use INTx for ATIOQ.\n", __func__);
6646 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006647 }
6648}
6649
6650void
6651qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6652{
6653 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006654 u32 tmp;
Quinn Tran99e1b682017-06-02 09:12:03 -07006655
6656 if (!QLA_TGT_MODE_ENABLED())
6657 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006658
Quinn Tranead03852017-01-19 22:28:01 -08006659 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006660 if (!ha->tgt.saved_set) {
6661 /* We save only once */
6662 ha->tgt.saved_exchange_count = nv->exchange_count;
6663 ha->tgt.saved_firmware_options_1 =
6664 nv->firmware_options_1;
6665 ha->tgt.saved_firmware_options_2 =
6666 nv->firmware_options_2;
6667 ha->tgt.saved_firmware_options_3 =
6668 nv->firmware_options_3;
6669 ha->tgt.saved_set = 1;
6670 }
6671
Quinn Tran99e1b682017-06-02 09:12:03 -07006672 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006673 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006674 else /* dual */
6675 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006676
6677 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006678 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006679
6680 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006681 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006682 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006683
6684 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006685 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006686 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006687 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006688 if (ql2xtgt_tape_enable)
6689 /* Enable FC Tape support */
6690 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6691 else
6692 /* Disable FC Tape support */
6693 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6694
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006695 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006696 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006697
6698 /*
6699 * clear BIT 15 explicitly as we have seen at least
6700 * a couple of instances where this was set and this
6701 * was causing the firmware to not be initialized.
6702 */
6703 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006704 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006705 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08006706
6707 if (IS_QLA25XX(ha)) {
6708 /* Change Loop-prefer to Pt-Pt */
6709 tmp = ~(BIT_4|BIT_5|BIT_6);
6710 nv->firmware_options_2 &= cpu_to_le32(tmp);
6711 tmp = P2P << 4;
6712 nv->firmware_options_2 |= cpu_to_le32(tmp);
6713 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006714 } else {
6715 if (ha->tgt.saved_set) {
6716 nv->exchange_count = ha->tgt.saved_exchange_count;
6717 nv->firmware_options_1 =
6718 ha->tgt.saved_firmware_options_1;
6719 nv->firmware_options_2 =
6720 ha->tgt.saved_firmware_options_2;
6721 nv->firmware_options_3 =
6722 ha->tgt.saved_firmware_options_3;
6723 }
6724 return;
6725 }
6726
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006727 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006728 if (vha->flags.init_done)
6729 fc_host_supported_classes(vha->host) =
6730 FC_COS_CLASS2 | FC_COS_CLASS3;
6731
Bart Van Asschead950362015-07-09 07:24:08 -07006732 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006733 } else {
6734 if (vha->flags.init_done)
6735 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6736
Bart Van Asschead950362015-07-09 07:24:08 -07006737 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006738 }
6739}
6740
6741void
6742qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6743 struct init_cb_24xx *icb)
6744{
6745 struct qla_hw_data *ha = vha->hw;
6746
Quinn Tran481ce732015-12-17 14:57:08 -05006747 if (!QLA_TGT_MODE_ENABLED())
6748 return;
6749
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006750 if (ha->tgt.node_name_set) {
6751 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006752 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006753 }
Quinn Tran481ce732015-12-17 14:57:08 -05006754
6755 /* disable ZIO at start time. */
6756 if (!vha->flags.init_done) {
6757 uint32_t tmp;
6758 tmp = le32_to_cpu(icb->firmware_options_2);
6759 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6760 icb->firmware_options_2 = cpu_to_le32(tmp);
6761 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006762}
6763
Arun Easiaa230bc2013-01-30 03:34:39 -05006764void
6765qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6766{
6767 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006768 u32 tmp;
Arun Easiaa230bc2013-01-30 03:34:39 -05006769
6770 if (!QLA_TGT_MODE_ENABLED())
6771 return;
6772
Quinn Tranead03852017-01-19 22:28:01 -08006773 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006774 if (!ha->tgt.saved_set) {
6775 /* We save only once */
6776 ha->tgt.saved_exchange_count = nv->exchange_count;
6777 ha->tgt.saved_firmware_options_1 =
6778 nv->firmware_options_1;
6779 ha->tgt.saved_firmware_options_2 =
6780 nv->firmware_options_2;
6781 ha->tgt.saved_firmware_options_3 =
6782 nv->firmware_options_3;
6783 ha->tgt.saved_set = 1;
6784 }
6785
Quinn Tran99e1b682017-06-02 09:12:03 -07006786 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006787 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006788 else /* dual */
6789 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05006790
6791 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006792 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006793
6794 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006795 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006796 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006797 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006798 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006799 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006800 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006801 /*
6802 * clear BIT 15 explicitly as we have seen at
6803 * least a couple of instances where this was set
6804 * and this was causing the firmware to not be
6805 * initialized.
6806 */
6807 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006808 if (ql2xtgt_tape_enable)
6809 /* Enable FC tape support */
6810 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6811 else
6812 /* Disable FC tape support */
6813 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6814
Arun Easiaa230bc2013-01-30 03:34:39 -05006815 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006816 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006817 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006818 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08006819
6820 /* Change Loop-prefer to Pt-Pt */
6821 tmp = ~(BIT_4|BIT_5|BIT_6);
6822 nv->firmware_options_2 &= cpu_to_le32(tmp);
6823 tmp = P2P << 4;
6824 nv->firmware_options_2 |= cpu_to_le32(tmp);
Arun Easiaa230bc2013-01-30 03:34:39 -05006825 } else {
6826 if (ha->tgt.saved_set) {
6827 nv->exchange_count = ha->tgt.saved_exchange_count;
6828 nv->firmware_options_1 =
6829 ha->tgt.saved_firmware_options_1;
6830 nv->firmware_options_2 =
6831 ha->tgt.saved_firmware_options_2;
6832 nv->firmware_options_3 =
6833 ha->tgt.saved_firmware_options_3;
6834 }
6835 return;
6836 }
6837
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006838 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006839 if (vha->flags.init_done)
6840 fc_host_supported_classes(vha->host) =
6841 FC_COS_CLASS2 | FC_COS_CLASS3;
6842
Bart Van Asschead950362015-07-09 07:24:08 -07006843 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006844 } else {
6845 if (vha->flags.init_done)
6846 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6847
Bart Van Asschead950362015-07-09 07:24:08 -07006848 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006849 }
6850}
6851
6852void
6853qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6854 struct init_cb_81xx *icb)
6855{
6856 struct qla_hw_data *ha = vha->hw;
6857
6858 if (!QLA_TGT_MODE_ENABLED())
6859 return;
6860
6861 if (ha->tgt.node_name_set) {
6862 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006863 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006864 }
Quinn Tran481ce732015-12-17 14:57:08 -05006865
6866 /* disable ZIO at start time. */
6867 if (!vha->flags.init_done) {
6868 uint32_t tmp;
6869 tmp = le32_to_cpu(icb->firmware_options_2);
6870 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6871 icb->firmware_options_2 = cpu_to_le32(tmp);
6872 }
6873
Arun Easiaa230bc2013-01-30 03:34:39 -05006874}
6875
6876void
6877qlt_83xx_iospace_config(struct qla_hw_data *ha)
6878{
6879 if (!QLA_TGT_MODE_ENABLED())
6880 return;
6881
6882 ha->msix_count += 1; /* For ATIO Q */
6883}
6884
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006885
6886void
6887qlt_modify_vp_config(struct scsi_qla_host *vha,
6888 struct vp_config_entry_24xx *vpmod)
6889{
Quinn Tranead03852017-01-19 22:28:01 -08006890 /* enable target mode. Bit5 = 1 => disable */
6891 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006892 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08006893
Quinn Tranead03852017-01-19 22:28:01 -08006894 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08006895 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006896 vpmod->options_idx1 &= ~BIT_4;
6897}
6898
6899void
6900qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6901{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006902 int rc;
6903
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006904 if (!QLA_TGT_MODE_ENABLED())
6905 return;
6906
Himanshu Madhanic9558862017-10-13 09:34:04 -07006907 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006908 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6909 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6910 } else {
6911 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6912 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6913 }
6914
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006915 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6916 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08006917
6918 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6919 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6920 qlt_unknown_atio_work_fn);
6921
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006922 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006923
6924 rc = btree_init32(&ha->tgt.host_map);
6925 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006926 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006927 "Unable to initialize ha->host_map btree\n");
6928
6929 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006930}
6931
Arun Easiaa230bc2013-01-30 03:34:39 -05006932irqreturn_t
6933qla83xx_msix_atio_q(int irq, void *dev_id)
6934{
6935 struct rsp_que *rsp;
6936 scsi_qla_host_t *vha;
6937 struct qla_hw_data *ha;
6938 unsigned long flags;
6939
6940 rsp = (struct rsp_que *) dev_id;
6941 ha = rsp->hw;
6942 vha = pci_get_drvdata(ha->pdev);
6943
Quinn Tran2f424b92015-12-17 14:57:07 -05006944 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006945
Quinn Tran2f424b92015-12-17 14:57:07 -05006946 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006947
Quinn Tran2f424b92015-12-17 14:57:07 -05006948 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006949
6950 return IRQ_HANDLED;
6951}
6952
Quinn Tran2f424b92015-12-17 14:57:07 -05006953static void
6954qlt_handle_abts_recv_work(struct work_struct *work)
6955{
6956 struct qla_tgt_sess_op *op = container_of(work,
6957 struct qla_tgt_sess_op, work);
6958 scsi_qla_host_t *vha = op->vha;
6959 struct qla_hw_data *ha = vha->hw;
6960 unsigned long flags;
6961
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006962 if (qla2x00_reset_active(vha) ||
6963 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05006964 return;
6965
6966 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6967 qlt_24xx_process_atio_queue(vha, 0);
6968 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6969
6970 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006971 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05006972 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07006973
6974 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05006975}
6976
6977void
Quinn Tran82de8022017-06-13 20:47:17 -07006978qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
6979 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05006980{
6981 struct qla_tgt_sess_op *op;
6982
6983 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6984
6985 if (!op) {
6986 /* do not reach for ATIO queue here. This is best effort err
6987 * recovery at this point.
6988 */
Quinn Tran82de8022017-06-13 20:47:17 -07006989 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05006990 return;
6991 }
6992
6993 memcpy(&op->atio, pkt, sizeof(*pkt));
6994 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006995 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07006996 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05006997 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6998 queue_work(qla_tgt_wq, &op->work);
6999 return;
7000}
7001
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007002int
7003qlt_mem_alloc(struct qla_hw_data *ha)
7004{
7005 if (!QLA_TGT_MODE_ENABLED())
7006 return 0;
7007
7008 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
7009 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
7010 if (!ha->tgt.tgt_vp_map)
7011 return -ENOMEM;
7012
7013 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7014 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7015 &ha->tgt.atio_dma, GFP_KERNEL);
7016 if (!ha->tgt.atio_ring) {
7017 kfree(ha->tgt.tgt_vp_map);
7018 return -ENOMEM;
7019 }
7020 return 0;
7021}
7022
7023void
7024qlt_mem_free(struct qla_hw_data *ha)
7025{
7026 if (!QLA_TGT_MODE_ENABLED())
7027 return;
7028
7029 if (ha->tgt.atio_ring) {
7030 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7031 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7032 ha->tgt.atio_dma);
7033 }
7034 kfree(ha->tgt.tgt_vp_map);
7035}
7036
7037/* vport_slock to be held by the caller */
7038void
7039qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7040{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007041 void *slot;
7042 u32 key;
7043 int rc;
7044
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007045 if (!QLA_TGT_MODE_ENABLED())
7046 return;
7047
Quinn Tran482c9dc2017-03-15 09:48:54 -07007048 key = vha->d_id.b24;
7049
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007050 switch (cmd) {
7051 case SET_VP_IDX:
7052 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7053 break;
7054 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07007055 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7056 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007057 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007058 "Save vha in host_map %p %06x\n", vha, key);
7059 rc = btree_insert32(&vha->hw->tgt.host_map,
7060 key, vha, GFP_ATOMIC);
7061 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007062 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007063 "Unable to insert s_id into host_map: %06x\n",
7064 key);
7065 return;
7066 }
Quinn Tran83548fe2017-06-02 09:12:01 -07007067 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7068 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007069 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007070 break;
7071 case RESET_VP_IDX:
7072 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7073 break;
7074 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07007075 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007076 "clear vha in host_map %p %06x\n", vha, key);
7077 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7078 if (slot)
7079 btree_remove32(&vha->hw->tgt.host_map, key);
7080 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007081 break;
7082 }
7083}
7084
Quinn Tran482c9dc2017-03-15 09:48:54 -07007085void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7086{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007087
7088 if (!vha->d_id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007089 vha->d_id = id;
7090 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007091 } else if (vha->d_id.b24 != id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007092 qlt_update_vp_map(vha, RESET_AL_PA);
7093 vha->d_id = id;
7094 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007095 }
7096}
7097
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007098static int __init qlt_parse_ini_mode(void)
7099{
7100 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7101 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7102 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7103 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7104 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7105 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007106 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7107 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007108 else
7109 return false;
7110
7111 return true;
7112}
7113
7114int __init qlt_init(void)
7115{
7116 int ret;
7117
7118 if (!qlt_parse_ini_mode()) {
7119 ql_log(ql_log_fatal, NULL, 0xe06b,
7120 "qlt_parse_ini_mode() failed\n");
7121 return -EINVAL;
7122 }
7123
7124 if (!QLA_TGT_MODE_ENABLED())
7125 return 0;
7126
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007127 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7128 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7129 qla_tgt_mgmt_cmd), 0, NULL);
7130 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007131 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007132 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007133 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007134 }
7135
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007136 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007137 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7138 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007139
7140 if (!qla_tgt_plogi_cachep) {
7141 ql_log(ql_log_fatal, NULL, 0xe06d,
7142 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7143 ret = -ENOMEM;
7144 goto out_mgmt_cmd_cachep;
7145 }
7146
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007147 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7148 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7149 if (!qla_tgt_mgmt_cmd_mempool) {
7150 ql_log(ql_log_fatal, NULL, 0xe06e,
7151 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7152 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007153 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007154 }
7155
7156 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7157 if (!qla_tgt_wq) {
7158 ql_log(ql_log_fatal, NULL, 0xe06f,
7159 "alloc_workqueue for qla_tgt_wq failed\n");
7160 ret = -ENOMEM;
7161 goto out_cmd_mempool;
7162 }
7163 /*
7164 * Return 1 to signal that initiator-mode is being disabled
7165 */
7166 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7167
7168out_cmd_mempool:
7169 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007170out_plogi_cachep:
7171 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007172out_mgmt_cmd_cachep:
7173 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007174 return ret;
7175}
7176
7177void qlt_exit(void)
7178{
7179 if (!QLA_TGT_MODE_ENABLED())
7180 return;
7181
7182 destroy_workqueue(qla_tgt_wq);
7183 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007184 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007185 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007186}