blob: 0d3c3f647f91c2ee1ec92620c5b0cf28d53dc3a4 [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
604 if (!IS_IIDMA_CAPABLE(vha->hw) ||
605 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700606 ql_dbg(ql_dbg_disc, vha, 0x20f3,
607 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
608 __func__, __LINE__,
609 sp->fcport->port_name,
610 vha->fcport_count);
Quinn Tran9cd883f2017-12-28 12:33:24 -0800611 sp->fcport->disc_state = DSC_UPD_FCPORT;
Quinn Tran726b8542017-01-19 22:28:00 -0800612 qla24xx_post_upd_fcport_work(vha, sp->fcport);
613 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700614 ql_dbg(ql_dbg_disc, vha, 0x20f5,
615 "%s %d %8phC post gpsc fcp_cnt %d\n",
616 __func__, __LINE__,
617 sp->fcport->port_name,
618 vha->fcport_count);
Quinn Tran726b8542017-01-19 22:28:00 -0800619
620 qla24xx_post_gpsc_work(vha, sp->fcport);
621 }
622 }
623 break;
624
625 case SRB_NACK_LOGO:
626 sp->fcport->login_gen++;
627 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
628 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
629 break;
630 }
631 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
632
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800633 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800634}
635
636int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
637 struct imm_ntfy_from_isp *ntfy, int type)
638{
639 int rval = QLA_FUNCTION_FAILED;
640 srb_t *sp;
641 char *c = NULL;
642
643 fcport->flags |= FCF_ASYNC_SENT;
644 switch (type) {
645 case SRB_NACK_PLOGI:
646 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
647 c = "PLOGI";
648 break;
649 case SRB_NACK_PRLI:
650 fcport->fw_login_state = DSC_LS_PRLI_PEND;
Quinn Tranec7193e2017-03-15 09:48:55 -0700651 fcport->deleted = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800652 c = "PRLI";
653 break;
654 case SRB_NACK_LOGO:
655 fcport->fw_login_state = DSC_LS_LOGO_PEND;
656 c = "LOGO";
657 break;
658 }
659
660 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
661 if (!sp)
662 goto done;
663
664 sp->type = type;
665 sp->name = "nack";
666
667 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
668
669 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
Quinn Tran2e01d0b2017-12-04 14:44:56 -0800670 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
Quinn Tran726b8542017-01-19 22:28:00 -0800671 sp->done = qla2x00_async_nack_sp_done;
672
673 rval = qla2x00_start_sp(sp);
674 if (rval != QLA_SUCCESS)
675 goto done_free_sp;
676
Quinn Tran83548fe2017-06-02 09:12:01 -0700677 ql_dbg(ql_dbg_disc, vha, 0x20f4,
678 "Async-%s %8phC hndl %x %s\n",
679 sp->name, fcport->port_name, sp->handle, c);
Quinn Tran726b8542017-01-19 22:28:00 -0800680
681 return rval;
682
683done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800684 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800685done:
686 fcport->flags &= ~FCF_ASYNC_SENT;
687 return rval;
688}
689
690void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
691{
692 fc_port_t *t;
693 unsigned long flags;
694
695 switch (e->u.nack.type) {
696 case SRB_NACK_PRLI:
697 mutex_lock(&vha->vha_tgt.tgt_mutex);
698 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
699 mutex_unlock(&vha->vha_tgt.tgt_mutex);
700 if (t) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700701 ql_log(ql_log_info, vha, 0xd034,
Quinn Tran726b8542017-01-19 22:28:00 -0800702 "%s create sess success %p", __func__, t);
703 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
704 /* create sess has an extra kref */
705 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
706 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
707 }
708 break;
709 }
710 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
711 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
712}
713
714void qla24xx_delete_sess_fn(struct work_struct *work)
715{
716 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
717 struct qla_hw_data *ha = fcport->vha->hw;
718 unsigned long flags;
719
720 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
721
722 if (fcport->se_sess) {
723 ha->tgt.tgt_ops->shutdown_sess(fcport);
724 ha->tgt.tgt_ops->put_sess(fcport);
725 } else {
726 qlt_unreg_sess(fcport);
727 }
728 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
729}
730
731/*
732 * Called from qla2x00_reg_remote_port()
733 */
734void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
735{
736 struct qla_hw_data *ha = vha->hw;
737 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
738 struct fc_port *sess = fcport;
739 unsigned long flags;
740
741 if (!vha->hw->tgt.tgt_ops)
742 return;
743
744 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
745 if (tgt->tgt_stop) {
746 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
747 return;
748 }
749
750 if (fcport->disc_state == DSC_DELETE_PEND) {
751 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
752 return;
753 }
754
755 if (!sess->se_sess) {
756 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
757
758 mutex_lock(&vha->vha_tgt.tgt_mutex);
759 sess = qlt_create_sess(vha, fcport, false);
760 mutex_unlock(&vha->vha_tgt.tgt_mutex);
761
762 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
763 } else {
764 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
765 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
766 return;
767 }
768
769 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700770 ql_dbg(ql_dbg_disc, vha, 0x2107,
Quinn Tran726b8542017-01-19 22:28:00 -0800771 "%s: kref_get fail sess %8phC \n",
772 __func__, sess->port_name);
773 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
774 return;
775 }
776
777 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
778 "qla_target(%u): %ssession for port %8phC "
779 "(loop ID %d) reappeared\n", vha->vp_idx,
780 sess->local ? "local " : "", sess->port_name, sess->loop_id);
781
782 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
783 "Reappeared sess %p\n", sess);
784
785 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
786 fcport->loop_id,
787 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
788 }
789
790 if (sess && sess->local) {
791 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
792 "qla_target(%u): local session for "
793 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
794 fcport->port_name, sess->loop_id);
795 sess->local = 0;
796 }
797 ha->tgt.tgt_ops->put_sess(sess);
798 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
799}
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500800
801/*
802 * This is a zero-base ref-counting solution, since hardware_lock
803 * guarantees that ref_count is not modified concurrently.
804 * Upon successful return content of iocb is undefined
805 */
Quinn Tran5d964832017-01-19 22:27:59 -0800806static struct qlt_plogi_ack_t *
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500807qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
808 struct imm_ntfy_from_isp *iocb)
809{
Quinn Tran5d964832017-01-19 22:27:59 -0800810 struct qlt_plogi_ack_t *pla;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500811
812 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
813 if (pla->id.b24 == id->b24) {
814 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
Quinn Tran5d964832017-01-19 22:27:59 -0800815 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500816 return pla;
817 }
818 }
819
820 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
821 if (!pla) {
822 ql_dbg(ql_dbg_async, vha, 0x5088,
823 "qla_target(%d): Allocation of plogi_ack failed\n",
824 vha->vp_idx);
825 return NULL;
826 }
827
Quinn Tran5d964832017-01-19 22:27:59 -0800828 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500829 pla->id = *id;
830 list_add_tail(&pla->list, &vha->plogi_ack_list);
831
832 return pla;
833}
834
Quinn Tran726b8542017-01-19 22:28:00 -0800835void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -0800836 struct qlt_plogi_ack_t *pla)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500837{
Quinn Tran5d964832017-01-19 22:27:59 -0800838 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Quinn Tran726b8542017-01-19 22:28:00 -0800839 port_id_t port_id;
840 uint16_t loop_id;
841 fc_port_t *fcport = pla->fcport;
842
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500843 BUG_ON(!pla->ref_count);
844 pla->ref_count--;
845
846 if (pla->ref_count)
847 return;
848
Quinn Tran726b8542017-01-19 22:28:00 -0800849 ql_dbg(ql_dbg_disc, vha, 0x5089,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500850 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
Quinn Tran5d964832017-01-19 22:27:59 -0800851 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
852 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
853 iocb->u.isp24.port_id[0],
854 le16_to_cpu(iocb->u.isp24.nport_handle),
855 iocb->u.isp24.exchange_address, iocb->ox_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800856
857 port_id.b.domain = iocb->u.isp24.port_id[2];
858 port_id.b.area = iocb->u.isp24.port_id[1];
859 port_id.b.al_pa = iocb->u.isp24.port_id[0];
860 port_id.b.rsvd_1 = 0;
861
862 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
863
864 fcport->loop_id = loop_id;
865 fcport->d_id = port_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800866 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
867 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
868 else
869 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
Quinn Tran726b8542017-01-19 22:28:00 -0800870
871 list_for_each_entry(fcport, &vha->vp_fcports, list) {
872 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
873 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
874 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
875 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
876 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500877
878 list_del(&pla->list);
879 kmem_cache_free(qla_tgt_plogi_cachep, pla);
880}
881
Quinn Tran726b8542017-01-19 22:28:00 -0800882void
Quinn Tran5d964832017-01-19 22:27:59 -0800883qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
884 struct fc_port *sess, enum qlt_plogi_link_t link)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500885{
Quinn Tran5d964832017-01-19 22:27:59 -0800886 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500887 /* Inc ref_count first because link might already be pointing at pla */
888 pla->ref_count++;
889
Quinn Tran726b8542017-01-19 22:28:00 -0800890 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
891 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
892 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
893 sess, link, sess->port_name,
894 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
895 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
896 pla->ref_count, pla, link);
897
Quinn Tran5ef696a2017-12-04 14:45:05 -0800898 if (link == QLT_PLOGI_LINK_CONFLICT) {
899 switch (sess->disc_state) {
900 case DSC_DELETED:
901 case DSC_DELETE_PEND:
902 pla->ref_count--;
903 return;
904 default:
905 break;
906 }
907 }
908
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500909 if (sess->plogi_link[link])
910 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
911
Quinn Tran726b8542017-01-19 22:28:00 -0800912 if (link == QLT_PLOGI_LINK_SAME_WWN)
913 pla->fcport = sess;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500914
915 sess->plogi_link[link] = pla;
916}
917
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500918typedef struct {
919 /* These fields must be initialized by the caller */
920 port_id_t id;
921 /*
922 * number of cmds dropped while we were waiting for
923 * initiator to ack LOGO initialize to 1 if LOGO is
924 * triggered by a command, otherwise, to 0
925 */
926 int cmd_count;
927
928 /* These fields are used by callee */
929 struct list_head list;
930} qlt_port_logo_t;
931
932static void
933qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
934{
935 qlt_port_logo_t *tmp;
936 int res;
937
938 mutex_lock(&vha->vha_tgt.tgt_mutex);
939
940 list_for_each_entry(tmp, &vha->logo_list, list) {
941 if (tmp->id.b24 == logo->id.b24) {
942 tmp->cmd_count += logo->cmd_count;
943 mutex_unlock(&vha->vha_tgt.tgt_mutex);
944 return;
945 }
946 }
947
948 list_add_tail(&logo->list, &vha->logo_list);
949
950 mutex_unlock(&vha->vha_tgt.tgt_mutex);
951
952 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
953
954 mutex_lock(&vha->vha_tgt.tgt_mutex);
955 list_del(&logo->list);
956 mutex_unlock(&vha->vha_tgt.tgt_mutex);
957
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500958 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
959 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
960 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
961 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500962}
963
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400964static void qlt_free_session_done(struct work_struct *work)
965{
Quinn Tran5d964832017-01-19 22:27:59 -0800966 struct fc_port *sess = container_of(work, struct fc_port,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400967 free_work);
968 struct qla_tgt *tgt = sess->tgt;
969 struct scsi_qla_host *vha = sess->vha;
970 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400971 unsigned long flags;
972 bool logout_started = false;
Quinn Tran41dc5292017-01-19 22:28:03 -0800973 scsi_qla_host_t *base_vha;
Quinn Tran9cd883f2017-12-28 12:33:24 -0800974 struct qlt_plogi_ack_t *own =
975 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400976
977 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
978 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500979 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400980 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
Quinn Tran37cacc02017-01-19 22:27:58 -0800981 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400982 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500983 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400984
Quinn Tran726b8542017-01-19 22:28:00 -0800985 if (!IS_SW_RESV_ADDR(sess->d_id)) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800986 if (sess->send_els_logo) {
987 qlt_port_logo_t logo;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500988
Quinn Tran41dc5292017-01-19 22:28:03 -0800989 logo.id = sess->d_id;
990 logo.cmd_count = 0;
991 qlt_send_first_logo(vha, &logo);
992 }
993
Quinn Tranba743f92017-12-04 14:45:12 -0800994 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800995 int rc;
996
Quinn Tran9cd883f2017-12-28 12:33:24 -0800997 if (!own ||
998 (own &&
999 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
1000 rc = qla2x00_post_async_logout_work(vha, sess,
1001 NULL);
1002 if (rc != QLA_SUCCESS)
1003 ql_log(ql_log_warn, vha, 0xf085,
1004 "Schedule logo failed sess %p rc %d\n",
1005 sess, rc);
1006 else
1007 logout_started = true;
1008 } else if (own && (own->iocb.u.isp24.status_subcode ==
1009 ELS_PRLI) && ha->flags.rida_fmt2) {
1010 rc = qla2x00_post_async_prlo_work(vha, sess,
1011 NULL);
1012 if (rc != QLA_SUCCESS)
1013 ql_log(ql_log_warn, vha, 0xf085,
1014 "Schedule PRLO failed sess %p rc %d\n",
1015 sess, rc);
1016 else
1017 logout_started = true;
1018 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001019 }
Quinn Tran726b8542017-01-19 22:28:00 -08001020 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001021
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001022 /*
1023 * Release the target session for FC Nexus from fabric module code.
1024 */
1025 if (sess->se_sess != NULL)
1026 ha->tgt.tgt_ops->free_session(sess);
1027
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001028 if (logout_started) {
1029 bool traced = false;
1030
Mark Rutland6aa7de02017-10-23 14:07:29 -07001031 while (!READ_ONCE(sess->logout_completed)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001032 if (!traced) {
1033 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1034 "%s: waiting for sess %p logout\n",
1035 __func__, sess);
1036 traced = true;
1037 }
1038 msleep(100);
1039 }
1040
Quinn Tran726b8542017-01-19 22:28:00 -08001041 ql_dbg(ql_dbg_disc, vha, 0xf087,
Quinn Tran9cd883f2017-12-28 12:33:24 -08001042 "%s: sess %p logout completed\n", __func__, sess);
Quinn Tran41dc5292017-01-19 22:28:03 -08001043 }
1044
1045 if (sess->logo_ack_needed) {
1046 sess->logo_ack_needed = 0;
1047 qla24xx_async_notify_ack(vha, sess,
1048 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001049 }
1050
Quinn Tran726b8542017-01-19 22:28:00 -08001051 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1052 if (sess->se_sess) {
1053 sess->se_sess = NULL;
1054 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1055 tgt->sess_count--;
1056 }
1057
1058 sess->disc_state = DSC_DELETED;
1059 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1060 sess->deleted = QLA_SESS_DELETED;
1061 sess->login_retry = vha->hw->login_retry_count;
1062
1063 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1064 vha->fcport_count--;
1065 sess->login_succ = 0;
1066 }
1067
Quinn Tranba743f92017-12-04 14:45:12 -08001068 qla2x00_clear_loop_id(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08001069
1070 if (sess->conflict) {
1071 sess->conflict->login_pause = 0;
1072 sess->conflict = NULL;
1073 if (!test_bit(UNLOADING, &vha->dpc_flags))
1074 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1075 }
1076
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001077 {
Quinn Tran5d964832017-01-19 22:27:59 -08001078 struct qlt_plogi_ack_t *con =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001079 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
Quinn Tran5d964832017-01-19 22:27:59 -08001080 struct imm_ntfy_from_isp *iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001081
1082 if (con) {
Quinn Tran5d964832017-01-19 22:27:59 -08001083 iocb = &con->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001084 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
Quinn Tran5d964832017-01-19 22:27:59 -08001085 "se_sess %p / sess %p port %8phC is gone,"
1086 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1087 sess->se_sess, sess, sess->port_name,
1088 own ? "releasing own PLOGI" : "no own PLOGI pending",
1089 own ? own->ref_count : -1,
1090 iocb->u.isp24.port_name, con->ref_count);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001091 qlt_plogi_ack_unref(vha, con);
Quinn Tran726b8542017-01-19 22:28:00 -08001092 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001093 } else {
1094 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1095 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1096 sess->se_sess, sess, sess->port_name,
1097 own ? "releasing own PLOGI" :
1098 "no own PLOGI pending",
1099 own ? own->ref_count : -1);
1100 }
1101
Quinn Tran726b8542017-01-19 22:28:00 -08001102 if (own) {
1103 sess->fw_login_state = DSC_LS_PLOGI_PEND;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001104 qlt_plogi_ack_unref(vha, own);
Quinn Tran726b8542017-01-19 22:28:00 -08001105 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1106 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001107 }
Quinn Tran1ae634e2017-12-28 12:33:44 -08001108
Quinn Tran5d964832017-01-19 22:27:59 -08001109 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1110
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001111 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
Quinn Tran726b8542017-01-19 22:28:00 -08001112 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1113 sess, sess->port_name, vha->fcport_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001114
Quinn Tran726b8542017-01-19 22:28:00 -08001115 if (tgt && (tgt->sess_count == 0))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001116 wake_up_all(&tgt->waitQ);
Quinn Tran726b8542017-01-19 22:28:00 -08001117
1118 if (vha->fcport_count == 0)
1119 wake_up_all(&vha->fcport_waitQ);
1120
Quinn Tran41dc5292017-01-19 22:28:03 -08001121 base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran1ae634e2017-12-28 12:33:44 -08001122
1123 sess->free_pending = 0;
1124
Quinn Tran41dc5292017-01-19 22:28:03 -08001125 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1126 return;
1127
Quinn Trana4239942017-12-28 12:33:26 -08001128 if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1129 switch (vha->host->active_mode) {
1130 case MODE_INITIATOR:
1131 case MODE_DUAL:
1132 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1133 qla2xxx_wake_dpc(vha);
1134 break;
1135 case MODE_TARGET:
1136 default:
1137 /* no-op */
1138 break;
1139 }
Quinn Tran726b8542017-01-19 22:28:00 -08001140 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001141}
1142
Quinn Tran75601512015-12-17 14:57:04 -05001143/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08001144void qlt_unreg_sess(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001145{
1146 struct scsi_qla_host *vha = sess->vha;
Quinn Tran1ae634e2017-12-28 12:33:44 -08001147 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001148
Quinn Tran83548fe2017-06-02 09:12:01 -07001149 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
Quinn Tran5d964832017-01-19 22:27:59 -08001150 "%s sess %p for deletion %8phC\n",
1151 __func__, sess, sess->port_name);
1152
Quinn Tran1ae634e2017-12-28 12:33:44 -08001153 spin_lock_irqsave(&sess->vha->work_lock, flags);
1154 if (sess->free_pending) {
1155 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1156 return;
1157 }
1158 sess->free_pending = 1;
1159 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1160
Quinn Tran36c78452016-02-04 11:45:18 -05001161 if (sess->se_sess)
1162 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001163
Quinn Tran5d964832017-01-19 22:27:59 -08001164 qla2x00_mark_device_lost(vha, sess, 1, 1);
1165
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001166 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Tran726b8542017-01-19 22:28:00 -08001167 sess->disc_state = DSC_DELETE_PEND;
1168 sess->last_rscn_gen = sess->rscn_gen;
1169 sess->last_login_gen = sess->login_gen;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001170
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001171 if (sess->nvme_flag & NVME_FLAG_REGISTERED)
1172 schedule_work(&sess->nvme_del_work);
1173
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001174 INIT_WORK(&sess->free_work, qlt_free_session_done);
1175 schedule_work(&sess->free_work);
1176}
Quinn Tran5d964832017-01-19 22:27:59 -08001177EXPORT_SYMBOL(qlt_unreg_sess);
Quinn Tran75601512015-12-17 14:57:04 -05001178
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001179static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1180{
1181 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001182 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001183 uint16_t loop_id;
1184 int res = 0;
1185 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -05001186 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001187
1188 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1189 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001190 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -04001191 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -05001192 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -04001193 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001194 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001195 } else {
Quinn Tran75601512015-12-17 14:57:04 -05001196 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001197 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05001198 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001199 }
1200
1201 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1202 "Using sess for qla_tgt_reset: %p\n", sess);
1203 if (!sess) {
1204 res = -ESRCH;
1205 return res;
1206 }
1207
1208 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001209 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1210 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001211 mcmd, loop_id);
1212
Quinn Tranbb1181c2016-12-23 18:06:05 -08001213 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001214}
1215
Quinn Tran726b8542017-01-19 22:28:00 -08001216static void qla24xx_chk_fcp_state(struct fc_port *sess)
1217{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001218 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
Quinn Tran726b8542017-01-19 22:28:00 -08001219 sess->logout_on_delete = 0;
1220 sess->logo_ack_needed = 0;
1221 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1222 sess->scan_state = 0;
1223 }
1224}
1225
Quinn Tran75601512015-12-17 14:57:04 -05001226/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran94cff6e2017-12-28 12:33:42 -08001227void qlt_schedule_sess_for_deletion(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001228{
1229 struct qla_tgt *tgt = sess->tgt;
Quinn Trand8630bb2017-12-28 12:33:43 -08001230 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001231
Quinn Tran726b8542017-01-19 22:28:00 -08001232 if (sess->disc_state == DSC_DELETE_PEND)
1233 return;
1234
1235 if (sess->disc_state == DSC_DELETED) {
1236 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1237 wake_up_all(&tgt->waitQ);
1238 if (sess->vha->fcport_count == 0)
1239 wake_up_all(&sess->vha->fcport_waitQ);
1240
1241 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1242 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001243 return;
1244 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001245
Quinn Tran726b8542017-01-19 22:28:00 -08001246 if (sess->deleted == QLA_SESS_DELETED)
1247 sess->logout_on_delete = 0;
1248
Quinn Trand8630bb2017-12-28 12:33:43 -08001249 spin_lock_irqsave(&sess->vha->work_lock, flags);
1250 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1251 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1252 return;
1253 }
Quinn Tran726b8542017-01-19 22:28:00 -08001254 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Trand8630bb2017-12-28 12:33:43 -08001255 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1256
1257 sess->disc_state = DSC_DELETE_PEND;
1258
Quinn Tran726b8542017-01-19 22:28:00 -08001259 qla24xx_chk_fcp_state(sess);
1260
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001261 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1262 "Scheduling sess %p for deletion\n", sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001263
Quinn Tran9cd883f2017-12-28 12:33:24 -08001264 /* use cancel to push work element through before re-queue */
1265 cancel_work_sync(&sess->del_work);
Quinn Trana01c77d2017-12-04 14:44:58 -08001266 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
1267 queue_work(sess->vha->hw->wq, &sess->del_work);
Quinn Tran726b8542017-01-19 22:28:00 -08001268}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001269
Quinn Tran75601512015-12-17 14:57:04 -05001270/* ha->tgt.sess_lock supposed to be held on entry */
Joern Engelc5701042014-09-16 16:23:14 -04001271static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001272{
Quinn Tran5d964832017-01-19 22:27:59 -08001273 struct fc_port *sess;
1274 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001275
Quinn Tran5d964832017-01-19 22:27:59 -08001276 list_for_each_entry(sess, &vha->vp_fcports, list) {
1277 if (sess->se_sess)
Quinn Tran94cff6e2017-12-28 12:33:42 -08001278 qlt_schedule_sess_for_deletion(sess);
Quinn Tran5d964832017-01-19 22:27:59 -08001279 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001280
1281 /* At this point tgt could be already dead */
1282}
1283
1284static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1285 uint16_t *loop_id)
1286{
1287 struct qla_hw_data *ha = vha->hw;
1288 dma_addr_t gid_list_dma;
1289 struct gid_list_info *gid_list;
1290 char *id_iter;
1291 int res, rc, i;
1292 uint16_t entries;
1293
1294 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1295 &gid_list_dma, GFP_KERNEL);
1296 if (!gid_list) {
1297 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1298 "qla_target(%d): DMA Alloc failed of %u\n",
1299 vha->vp_idx, qla2x00_gid_list_size(ha));
1300 return -ENOMEM;
1301 }
1302
1303 /* Get list of logged in devices */
Quinn Tran15f30a52017-03-15 09:48:52 -07001304 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001305 if (rc != QLA_SUCCESS) {
1306 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1307 "qla_target(%d): get_id_list() failed: %x\n",
1308 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001309 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001310 goto out_free_id_list;
1311 }
1312
1313 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001314 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001315 for (i = 0; i < entries; i++) {
1316 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1317 if ((gid->al_pa == s_id[2]) &&
1318 (gid->area == s_id[1]) &&
1319 (gid->domain == s_id[0])) {
1320 *loop_id = le16_to_cpu(gid->loop_id);
1321 res = 0;
1322 break;
1323 }
1324 id_iter += ha->gid_list_info_size;
1325 }
1326
1327out_free_id_list:
1328 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1329 gid_list, gid_list_dma);
1330 return res;
1331}
1332
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001333/*
1334 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1335 * Caller must put it.
1336 */
Quinn Tran5d964832017-01-19 22:27:59 -08001337static struct fc_port *qlt_create_sess(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001338 struct scsi_qla_host *vha,
1339 fc_port_t *fcport,
1340 bool local)
1341{
1342 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -08001343 struct fc_port *sess = fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001344 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001345
Quinn Tran726b8542017-01-19 22:28:00 -08001346 if (vha->vha_tgt.qla_tgt->tgt_stop)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001347 return NULL;
Quinn Tran726b8542017-01-19 22:28:00 -08001348
1349 if (fcport->se_sess) {
1350 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001351 ql_dbg(ql_dbg_disc, vha, 0x20f6,
Quinn Tran726b8542017-01-19 22:28:00 -08001352 "%s: kref_get_unless_zero failed for %8phC\n",
1353 __func__, sess->port_name);
1354 return NULL;
1355 }
1356 return fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001357 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001358 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001359 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001360
Quinn Tran726b8542017-01-19 22:28:00 -08001361 /*
1362 * Under normal circumstances we want to logout from firmware when
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001363 * session eventually ends and release corresponding nport handle.
1364 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
Quinn Tran726b8542017-01-19 22:28:00 -08001365 * code will adjust these flags as necessary.
1366 */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001367 sess->logout_on_delete = 1;
1368 sess->keep_nport_handle = 0;
Quinn Tran726b8542017-01-19 22:28:00 -08001369 sess->logout_completed = 0;
1370
1371 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1372 &fcport->port_name[0], sess) < 0) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001373 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
Quinn Tran726b8542017-01-19 22:28:00 -08001374 "(%d) %8phC check_initiator_node_acl failed\n",
1375 vha->vp_idx, fcport->port_name);
1376 return NULL;
1377 } else {
1378 kref_init(&fcport->sess_kref);
1379 /*
1380 * Take an extra reference to ->sess_kref here to handle
1381 * fc_port access across ->tgt.sess_lock reaquire.
1382 */
1383 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001384 ql_dbg(ql_dbg_disc, vha, 0x20f7,
Quinn Tran726b8542017-01-19 22:28:00 -08001385 "%s: kref_get_unless_zero failed for %8phC\n",
1386 __func__, sess->port_name);
1387 return NULL;
1388 }
1389
1390 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1391 if (!IS_SW_RESV_ADDR(sess->d_id))
1392 vha->vha_tgt.qla_tgt->sess_count++;
1393
1394 qlt_do_generation_tick(vha, &sess->generation);
1395 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1396 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001397
1398 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
Quinn Tran726b8542017-01-19 22:28:00 -08001399 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1400 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1401 vha->vha_tgt.qla_tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001402
1403 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001404 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1405 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1406 vha->vp_idx, local ? "local " : "", fcport->port_name,
Quinn Tran37cacc02017-01-19 22:27:58 -08001407 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1408 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001409
1410 return sess;
1411}
1412
1413/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001414 * max_gen - specifies maximum session generation
1415 * at which this deletion requestion is still valid
1416 */
1417void
1418qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001419{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001420 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08001421 struct fc_port *sess = fcport;
Quinn Tran75601512015-12-17 14:57:04 -05001422 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001423
1424 if (!vha->hw->tgt.tgt_ops)
1425 return;
1426
Roland Dreierb2032fd2015-07-14 16:00:42 -04001427 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001428 return;
1429
Quinn Tran75601512015-12-17 14:57:04 -05001430 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001431 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001432 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001433 return;
1434 }
Quinn Tran5d964832017-01-19 22:27:59 -08001435 if (!sess->se_sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001436 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001437 return;
1438 }
1439
Alexei Potashnikdf673272015-07-14 16:00:46 -04001440 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001441 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001442 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1443 "Ignoring stale deletion request for se_sess %p / sess %p"
1444 " for port %8phC, req_gen %d, sess_gen %d\n",
1445 sess->se_sess, sess, sess->port_name, max_gen,
1446 sess->generation);
1447 return;
1448 }
1449
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001450 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1451
1452 sess->local = 1;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001453 qlt_schedule_sess_for_deletion(sess);
Quinn Tran75601512015-12-17 14:57:04 -05001454 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001455}
1456
1457static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1458{
1459 struct qla_hw_data *ha = tgt->ha;
1460 unsigned long flags;
1461 int res;
1462 /*
1463 * We need to protect against race, when tgt is freed before or
1464 * inside wake_up()
1465 */
Quinn Tran726b8542017-01-19 22:28:00 -08001466 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001467 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
Quinn Tran5d964832017-01-19 22:27:59 -08001468 "tgt %p, sess_count=%d\n",
1469 tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001470 res = (tgt->sess_count == 0);
Quinn Tran726b8542017-01-19 22:28:00 -08001471 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001472
1473 return res;
1474}
1475
1476/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001477int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001478{
1479 struct scsi_qla_host *vha = tgt->vha;
1480 struct qla_hw_data *ha = tgt->ha;
1481 unsigned long flags;
1482
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001483 mutex_lock(&qla_tgt_mutex);
1484 if (!vha->fc_vport) {
1485 struct Scsi_Host *sh = vha->host;
1486 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1487 bool npiv_vports;
1488
1489 spin_lock_irqsave(sh->host_lock, flags);
1490 npiv_vports = (fc_host->npiv_vports_inuse);
1491 spin_unlock_irqrestore(sh->host_lock, flags);
1492
1493 if (npiv_vports) {
1494 mutex_unlock(&qla_tgt_mutex);
Quinn Tran3a33dc92017-06-02 09:12:04 -07001495 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1496 "NPIV is in use. Can not stop target\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001497 return -EPERM;
1498 }
1499 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001500 if (tgt->tgt_stop || tgt->tgt_stopped) {
1501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1502 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001503 mutex_unlock(&qla_tgt_mutex);
1504 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001505 }
1506
Quinn Tran3a33dc92017-06-02 09:12:04 -07001507 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001508 vha->host_no, vha);
1509 /*
1510 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1511 * Lock is needed, because we still can get an incoming packet.
1512 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001513 mutex_lock(&vha->vha_tgt.tgt_mutex);
Quinn Tran75601512015-12-17 14:57:04 -05001514 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001515 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001516 qlt_clear_tgt_db(tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001517 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001518 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001519 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001520
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001521 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1522 "Waiting for sess works (tgt %p)", tgt);
1523 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1524 while (!list_empty(&tgt->sess_works_list)) {
1525 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1526 flush_scheduled_work();
1527 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1528 }
1529 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1530
1531 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001532 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001533
Joe Carnucciob85e0952017-08-23 15:05:11 -07001534 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001535
1536 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001537 if (!ha->flags.host_shutting_down &&
1538 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001539 qlt_disable_vha(vha);
1540
1541 /* Wait for sessions to clear out (just in case) */
Joe Carnucciob85e0952017-08-23 15:05:11 -07001542 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001543 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001544}
1545EXPORT_SYMBOL(qlt_stop_phase1);
1546
1547/* Called by tcm_qla2xxx configfs code */
1548void qlt_stop_phase2(struct qla_tgt *tgt)
1549{
Quinn Tran3a33dc92017-06-02 09:12:04 -07001550 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001551
1552 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001553 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001554 "Already in tgt->tgt_stopped state\n");
1555 dump_stack();
1556 return;
1557 }
Quinn Tran3a33dc92017-06-02 09:12:04 -07001558 if (!tgt->tgt_stop) {
1559 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1560 "%s: phase1 stop is not completed\n", __func__);
1561 dump_stack();
1562 return;
1563 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001564
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001565 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001566 tgt->tgt_stop = 0;
1567 tgt->tgt_stopped = 1;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001568 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001569
Quinn Tran3a33dc92017-06-02 09:12:04 -07001570 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001571 tgt);
1572}
1573EXPORT_SYMBOL(qlt_stop_phase2);
1574
1575/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001576static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001577{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001578 scsi_qla_host_t *vha = tgt->vha;
Quinn Trane326d222017-06-13 20:47:18 -07001579 void *node;
1580 u64 key = 0;
1581 u16 i;
1582 struct qla_qpair_hint *h;
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001583 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001584
Quinn Tran3a33dc92017-06-02 09:12:04 -07001585 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1586 !tgt->tgt_stopped)
1587 qlt_stop_phase1(tgt);
1588
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001589 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001590 qlt_stop_phase2(tgt);
1591
Quinn Trane326d222017-06-13 20:47:18 -07001592 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1593 unsigned long flags;
1594
1595 h = &tgt->qphints[i];
1596 if (h->qpair) {
1597 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1598 list_del(&h->hint_elem);
1599 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1600 h->qpair = NULL;
1601 }
1602 }
1603 kfree(tgt->qphints);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001604 mutex_lock(&qla_tgt_mutex);
1605 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1606 mutex_unlock(&qla_tgt_mutex);
Quinn Trane326d222017-06-13 20:47:18 -07001607
1608 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1609 btree_remove64(&tgt->lun_qpair_map, key);
1610
1611 btree_destroy64(&tgt->lun_qpair_map);
1612
Sawan Chandak3be63b1e2017-12-04 14:45:04 -08001613 if (vha->vp_idx)
1614 if (ha->tgt.tgt_ops &&
1615 ha->tgt.tgt_ops->remove_target &&
1616 vha->vha_tgt.target_lport_ptr)
1617 ha->tgt.tgt_ops->remove_target(vha);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001618
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001619 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001620
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001621 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001622 "Release of tgt %p finished\n", tgt);
1623
1624 kfree(tgt);
1625}
1626
1627/* ha->hardware_lock supposed to be held on entry */
1628static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1629 const void *param, unsigned int param_size)
1630{
1631 struct qla_tgt_sess_work_param *prm;
1632 unsigned long flags;
1633
1634 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1635 if (!prm) {
1636 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1637 "qla_target(%d): Unable to create session "
1638 "work, command will be refused", 0);
1639 return -ENOMEM;
1640 }
1641
1642 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1643 "Scheduling work (type %d, prm %p)"
1644 " to find session for param %p (size %d, tgt %p)\n",
1645 type, prm, param, param_size, tgt);
1646
1647 prm->type = type;
1648 memcpy(&prm->tm_iocb, param, param_size);
1649
1650 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1651 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1652 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1653
1654 schedule_work(&tgt->sess_work);
1655
1656 return 0;
1657}
1658
1659/*
1660 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1661 */
Quinn Tran82de8022017-06-13 20:47:17 -07001662static void qlt_send_notify_ack(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001663 struct imm_ntfy_from_isp *ntfy,
1664 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1665 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1666{
Quinn Tran82de8022017-06-13 20:47:17 -07001667 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001668 struct qla_hw_data *ha = vha->hw;
1669 request_t *pkt;
1670 struct nack_to_isp *nack;
1671
Quinn Tranec7193e2017-03-15 09:48:55 -07001672 if (!ha->flags.fw_started)
1673 return;
1674
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001675 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1676
Quinn Tran82de8022017-06-13 20:47:17 -07001677 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001678 if (!pkt) {
1679 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1680 "qla_target(%d): %s failed: unable to allocate "
1681 "request packet\n", vha->vp_idx, __func__);
1682 return;
1683 }
1684
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001685 if (vha->vha_tgt.qla_tgt != NULL)
1686 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001687
1688 pkt->entry_type = NOTIFY_ACK_TYPE;
1689 pkt->entry_count = 1;
1690
1691 nack = (struct nack_to_isp *)pkt;
1692 nack->ox_id = ntfy->ox_id;
1693
Quinn Tran726b8542017-01-19 22:28:00 -08001694 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001695 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1696 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1697 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001698 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001699 }
1700 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1701 nack->u.isp24.status = ntfy->u.isp24.status;
1702 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001703 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001704 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1705 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1706 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1707 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1708 nack->u.isp24.srr_reject_code = srr_reject_code;
1709 nack->u.isp24.srr_reject_code_expl = srr_explan;
1710 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1711
1712 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1713 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1714 vha->vp_idx, nack->u.isp24.status);
1715
Himanshu Madhani63163e02014-09-25 06:14:59 -04001716 /* Memory Barrier */
1717 wmb();
Quinn Tran82de8022017-06-13 20:47:17 -07001718 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001719}
1720
1721/*
1722 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1723 */
Quinn Tran82de8022017-06-13 20:47:17 -07001724static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001725 struct abts_recv_from_24xx *abts, uint32_t status,
1726 bool ids_reversed)
1727{
Quinn Tran82de8022017-06-13 20:47:17 -07001728 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001729 struct qla_hw_data *ha = vha->hw;
1730 struct abts_resp_to_24xx *resp;
1731 uint32_t f_ctl;
1732 uint8_t *p;
1733
1734 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1735 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1736 ha, abts, status);
1737
Quinn Tran82de8022017-06-13 20:47:17 -07001738 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1739 NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001740 if (!resp) {
1741 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1742 "qla_target(%d): %s failed: unable to allocate "
1743 "request packet", vha->vp_idx, __func__);
1744 return;
1745 }
1746
1747 resp->entry_type = ABTS_RESP_24XX;
1748 resp->entry_count = 1;
1749 resp->nport_handle = abts->nport_handle;
1750 resp->vp_index = vha->vp_idx;
1751 resp->sof_type = abts->sof_type;
1752 resp->exchange_address = abts->exchange_address;
1753 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001754 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001755 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1756 F_CTL_SEQ_INITIATIVE);
1757 p = (uint8_t *)&f_ctl;
1758 resp->fcp_hdr_le.f_ctl[0] = *p++;
1759 resp->fcp_hdr_le.f_ctl[1] = *p++;
1760 resp->fcp_hdr_le.f_ctl[2] = *p;
1761 if (ids_reversed) {
1762 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1763 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1764 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1765 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1766 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1767 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1768 } else {
1769 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1770 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1771 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1772 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1773 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1774 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1775 }
1776 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1777 if (status == FCP_TMF_CMPL) {
1778 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1779 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1780 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1781 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1782 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1783 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1784 } else {
1785 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1786 resp->payload.ba_rjt.reason_code =
1787 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1788 /* Other bytes are zero */
1789 }
1790
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001791 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001792
Himanshu Madhani63163e02014-09-25 06:14:59 -04001793 /* Memory Barrier */
1794 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07001795 if (qpair->reqq_start_iocbs)
1796 qpair->reqq_start_iocbs(qpair);
1797 else
1798 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001799}
1800
1801/*
1802 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1803 */
1804static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1805 struct abts_resp_from_24xx_fw *entry)
1806{
1807 struct ctio7_to_24xx *ctio;
1808
1809 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1810 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001811
Quinn Tran82de8022017-06-13 20:47:17 -07001812 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1813 vha->hw->base_qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001814 if (ctio == NULL) {
1815 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1816 "qla_target(%d): %s failed: unable to allocate "
1817 "request packet\n", vha->vp_idx, __func__);
1818 return;
1819 }
1820
1821 /*
1822 * We've got on entrance firmware's response on by us generated
1823 * ABTS response. So, in it ID fields are reversed.
1824 */
1825
1826 ctio->entry_type = CTIO_TYPE7;
1827 ctio->entry_count = 1;
1828 ctio->nport_handle = entry->nport_handle;
1829 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001830 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001831 ctio->vp_index = vha->vp_idx;
1832 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1833 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1834 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1835 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001836 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1837 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001838 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001839
Himanshu Madhani63163e02014-09-25 06:14:59 -04001840 /* Memory Barrier */
1841 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001842 qla2x00_start_iocbs(vha, vha->req);
1843
Quinn Tran82de8022017-06-13 20:47:17 -07001844 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1845 (struct abts_recv_from_24xx *)entry,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001846 FCP_TMF_CMPL, true);
1847}
1848
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001849static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1850{
1851 struct qla_tgt_sess_op *op;
1852 struct qla_tgt_cmd *cmd;
Quinn Tran8b631d82017-06-02 09:11:54 -07001853 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001854
Quinn Tran8b631d82017-06-02 09:11:54 -07001855 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001856 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1857 if (tag == op->atio.u.isp24.exchange_addr) {
1858 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001859 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001860 return 1;
1861 }
1862 }
1863
Quinn Tran41dc5292017-01-19 22:28:03 -08001864 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1865 if (tag == op->atio.u.isp24.exchange_addr) {
1866 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001867 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08001868 return 1;
1869 }
1870 }
1871
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001872 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1873 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001874 cmd->aborted = 1;
Quinn Tran8b631d82017-06-02 09:11:54 -07001875 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001876 return 1;
1877 }
1878 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001879 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001880
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001881 return 0;
1882}
1883
1884/* drop cmds for the given lun
1885 * XXX only looks for cmds on the port through which lun reset was recieved
1886 * XXX does not go through the list of other port (which may have cmds
1887 * for the same lun)
1888 */
1889static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001890 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001891{
1892 struct qla_tgt_sess_op *op;
1893 struct qla_tgt_cmd *cmd;
1894 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001895 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001896
1897 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001898 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001899 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1900 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001901 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001902
1903 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1904 op_lun = scsilun_to_int(
1905 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1906 if (op_key == key && op_lun == lun)
1907 op->aborted = true;
1908 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001909
1910 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1911 uint32_t op_key;
1912 u64 op_lun;
1913
1914 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1915 op_lun = scsilun_to_int(
1916 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1917 if (op_key == key && op_lun == lun)
1918 op->aborted = true;
1919 }
1920
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001921 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1922 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001923 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001924
1925 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1926 cmd_lun = scsilun_to_int(
1927 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1928 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001929 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001930 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001931 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001932}
1933
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001934/* ha->hardware_lock supposed to be held on entry */
1935static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08001936 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001937{
1938 struct qla_hw_data *ha = vha->hw;
1939 struct qla_tgt_mgmt_cmd *mcmd;
1940 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001941
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001942 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1943 /* send TASK_ABORT response immediately */
Linus Torvalds48ea2ce2017-07-13 14:27:32 -07001944 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001945 return 0;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001946 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001947
1948 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1949 "qla_target(%d): task abort (tag=%d)\n",
1950 vha->vp_idx, abts->exchange_addr_to_abort);
1951
1952 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1953 if (mcmd == NULL) {
1954 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1955 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1956 vha->vp_idx, __func__);
1957 return -ENOMEM;
1958 }
1959 memset(mcmd, 0, sizeof(*mcmd));
1960
1961 mcmd->sess = sess;
1962 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001963 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08001964 mcmd->tmr_func = QLA_TGT_ABTS;
Quinn Tran82de8022017-06-13 20:47:17 -07001965 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07001966 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001967
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001968 /*
1969 * LUN is looked up by target-core internally based on the passed
1970 * abts->exchange_addr_to_abort tag.
1971 */
1972 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, 0, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001973 abts->exchange_addr_to_abort);
1974 if (rc != 0) {
1975 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1976 "qla_target(%d): tgt_ops->handle_tmr()"
1977 " failed: %d", vha->vp_idx, rc);
1978 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1979 return -EFAULT;
1980 }
1981
1982 return 0;
1983}
1984
1985/*
1986 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1987 */
1988static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1989 struct abts_recv_from_24xx *abts)
1990{
1991 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001992 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001993 uint32_t tag = abts->exchange_addr_to_abort;
1994 uint8_t s_id[3];
1995 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001996 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001997
1998 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1999 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2000 "qla_target(%d): ABTS: Abort Sequence not "
2001 "supported\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002002 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2003 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002004 return;
2005 }
2006
2007 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2008 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2009 "qla_target(%d): ABTS: Unknown Exchange "
2010 "Address received\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07002011 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2012 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002013 return;
2014 }
2015
2016 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2017 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2018 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2019 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2020 le32_to_cpu(abts->fcp_hdr_le.parameter));
2021
2022 s_id[0] = abts->fcp_hdr_le.s_id[2];
2023 s_id[1] = abts->fcp_hdr_le.s_id[1];
2024 s_id[2] = abts->fcp_hdr_le.s_id[0];
2025
Quinn Tran75601512015-12-17 14:57:04 -05002026 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002027 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2028 if (!sess) {
2029 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2030 "qla_target(%d): task abort for non-existant session\n",
2031 vha->vp_idx);
Quinn Tran75601512015-12-17 14:57:04 -05002032 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2033
Quinn Tranfb352652017-12-28 12:33:32 -08002034 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2035 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002036 return;
2037 }
Quinn Tran75601512015-12-17 14:57:04 -05002038 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2039
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002040
Quinn Tran726b8542017-01-19 22:28:00 -08002041 if (sess->deleted) {
Quinn Tran82de8022017-06-13 20:47:17 -07002042 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2043 false);
Alexei Potashnike52a8b42015-07-14 16:00:48 -04002044 return;
2045 }
2046
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002047 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2048 if (rc != 0) {
2049 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2050 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2051 vha->vp_idx, rc);
Quinn Tran82de8022017-06-13 20:47:17 -07002052 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2053 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002054 return;
2055 }
2056}
2057
2058/*
2059 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2060 */
Quinn Tran82de8022017-06-13 20:47:17 -07002061static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002062 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2063{
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002064 struct scsi_qla_host *ha = mcmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002065 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2066 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002067 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002068
2069 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2070 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2071 ha, atio, resp_code);
2072
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002073
Quinn Tran82de8022017-06-13 20:47:17 -07002074 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002075 if (ctio == NULL) {
2076 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2077 "qla_target(%d): %s failed: unable to allocate "
2078 "request packet\n", ha->vp_idx, __func__);
2079 return;
2080 }
2081
2082 ctio->entry_type = CTIO_TYPE7;
2083 ctio->entry_count = 1;
2084 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2085 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002086 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002087 ctio->vp_index = ha->vp_idx;
2088 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2089 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2090 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2091 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002092 temp = (atio->u.isp24.attr << 9)|
2093 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2094 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002095 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2096 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002097 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002098 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2099 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002100 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002101
Himanshu Madhani63163e02014-09-25 06:14:59 -04002102 /* Memory Barrier */
2103 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002104 if (qpair->reqq_start_iocbs)
2105 qpair->reqq_start_iocbs(qpair);
2106 else
2107 qla2x00_start_iocbs(ha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002108}
2109
2110void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2111{
2112 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2113}
2114EXPORT_SYMBOL(qlt_free_mcmd);
2115
Quinn Tranbe251522017-03-15 09:48:49 -07002116/*
2117 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2118 * reacquire
2119 */
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002120void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07002121 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2122{
2123 struct atio_from_isp *atio = &cmd->atio;
2124 struct ctio7_to_24xx *ctio;
2125 uint16_t temp;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002126 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranbe251522017-03-15 09:48:49 -07002127
2128 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2129 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2130 "sense_key=%02x, asc=%02x, ascq=%02x",
2131 vha, atio, scsi_status, sense_key, asc, ascq);
2132
2133 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2134 if (!ctio) {
2135 ql_dbg(ql_dbg_async, vha, 0x3067,
2136 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2137 vha->host_no, __func__);
2138 goto out;
2139 }
2140
2141 ctio->entry_type = CTIO_TYPE7;
2142 ctio->entry_count = 1;
2143 ctio->handle = QLA_TGT_SKIP_HANDLE;
2144 ctio->nport_handle = cmd->sess->loop_id;
2145 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2146 ctio->vp_index = vha->vp_idx;
2147 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2148 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2149 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2150 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002151 temp = (atio->u.isp24.attr << 9) |
2152 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2153 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002154 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2155 ctio->u.status1.ox_id = cpu_to_le16(temp);
2156 ctio->u.status1.scsi_status =
2157 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2158 ctio->u.status1.response_len = cpu_to_le16(18);
2159 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2160
2161 if (ctio->u.status1.residual != 0)
2162 ctio->u.status1.scsi_status |=
2163 cpu_to_le16(SS_RESIDUAL_UNDER);
2164
2165 /* Response code and sense key */
2166 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2167 (&ctio->u.status1.sense_data)[0]);
2168 /* Additional sense length */
2169 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2170 /* ASC and ASCQ */
2171 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2172 (&ctio->u.status1.sense_data)[3]);
2173
2174 /* Memory Barrier */
2175 wmb();
2176
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002177 if (qpair->reqq_start_iocbs)
2178 qpair->reqq_start_iocbs(qpair);
2179 else
2180 qla2x00_start_iocbs(vha, qpair->req);
2181
Quinn Tranbe251522017-03-15 09:48:49 -07002182out:
2183 return;
2184}
2185
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002186/* callback from target fabric module code */
2187void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2188{
2189 struct scsi_qla_host *vha = mcmd->sess->vha;
2190 struct qla_hw_data *ha = vha->hw;
2191 unsigned long flags;
Quinn Tran82de8022017-06-13 20:47:17 -07002192 struct qla_qpair *qpair = mcmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002193
2194 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2195 "TM response mcmd (%p) status %#x state %#x",
2196 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2197
Quinn Tran82de8022017-06-13 20:47:17 -07002198 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002199
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002200 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002201 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002202 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002203 * previous life, just abort the processing.
2204 */
2205 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002206 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2207 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002208 mcmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002209 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002210 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002211 return;
2212 }
2213
Quinn Tran726b8542017-01-19 22:28:00 -08002214 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2215 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
Quinn Tran41dc5292017-01-19 22:28:03 -08002216 ELS_LOGO ||
2217 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2218 ELS_PRLO ||
2219 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2220 ELS_TPRLO) {
Quinn Tran83548fe2017-06-02 09:12:01 -07002221 ql_dbg(ql_dbg_disc, vha, 0x2106,
Quinn Tran726b8542017-01-19 22:28:00 -08002222 "TM response logo %phC status %#x state %#x",
2223 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2224 mcmd->flags);
Quinn Trand8630bb2017-12-28 12:33:43 -08002225 qlt_schedule_sess_for_deletion(mcmd->sess);
Quinn Tran726b8542017-01-19 22:28:00 -08002226 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002227 qlt_send_notify_ack(vha->hw->base_qpair,
2228 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
Quinn Tran726b8542017-01-19 22:28:00 -08002229 }
2230 } else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05002231 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Quinn Tran82de8022017-06-13 20:47:17 -07002232 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002233 mcmd->fc_tm_rsp, false);
2234 else
Quinn Tran82de8022017-06-13 20:47:17 -07002235 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002236 mcmd->fc_tm_rsp);
2237 }
2238 /*
2239 * Make the callback for ->free_mcmd() to queue_work() and invoke
2240 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2241 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2242 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2243 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2244 * qlt_xmit_tm_rsp() returns here..
2245 */
2246 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002247 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002248}
2249EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2250
2251/* No locks */
2252static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2253{
2254 struct qla_tgt_cmd *cmd = prm->cmd;
2255
2256 BUG_ON(cmd->sg_cnt == 0);
2257
2258 prm->sg = (struct scatterlist *)cmd->sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002259 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002260 cmd->sg_cnt, cmd->dma_data_direction);
2261 if (unlikely(prm->seg_cnt == 0))
2262 goto out_err;
2263
2264 prm->cmd->sg_mapped = 1;
2265
Quinn Tranf83adb62014-04-11 16:54:43 -04002266 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2267 /*
2268 * If greater than four sg entries then we need to allocate
2269 * the continuation entries
2270 */
Quinn Tranb5399f72017-06-13 20:47:27 -07002271 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
Quinn Tranf83adb62014-04-11 16:54:43 -04002272 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
Quinn Tranb5399f72017-06-13 20:47:27 -07002273 QLA_TGT_DATASEGS_PER_CMD_24XX,
2274 QLA_TGT_DATASEGS_PER_CONT_24XX);
Quinn Tranf83adb62014-04-11 16:54:43 -04002275 } else {
2276 /* DIF */
2277 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2278 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2279 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2280 prm->tot_dsds = prm->seg_cnt;
2281 } else
2282 prm->tot_dsds = prm->seg_cnt;
2283
2284 if (cmd->prot_sg_cnt) {
2285 prm->prot_sg = cmd->prot_sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002286 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
Quinn Tranf83adb62014-04-11 16:54:43 -04002287 cmd->prot_sg, cmd->prot_sg_cnt,
2288 cmd->dma_data_direction);
2289 if (unlikely(prm->prot_seg_cnt == 0))
2290 goto out_err;
2291
2292 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2293 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2294 /* Dif Bundling not support here */
2295 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2296 cmd->blk_sz);
2297 prm->tot_dsds += prm->prot_seg_cnt;
2298 } else
2299 prm->tot_dsds += prm->prot_seg_cnt;
2300 }
2301 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002302
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002303 return 0;
2304
2305out_err:
Quinn Tran22d84722017-06-13 20:47:25 -07002306 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002307 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2308 0, prm->cmd->sg_cnt);
2309 return -1;
2310}
2311
Joern Engelf9b67212014-09-16 16:23:18 -04002312static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002313{
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002314 struct qla_hw_data *ha;
2315 struct qla_qpair *qpair;
Joern Engelf9b67212014-09-16 16:23:18 -04002316 if (!cmd->sg_mapped)
2317 return;
2318
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002319 qpair = cmd->qpair;
2320
2321 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2322 cmd->dma_data_direction);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002323 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002324
2325 if (cmd->prot_sg_cnt)
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002326 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
Quinn Tranf83adb62014-04-11 16:54:43 -04002327 cmd->dma_data_direction);
2328
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002329 if (!cmd->ctx)
2330 return;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002331 ha = vha->hw;
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002332 if (cmd->ctx_dsd_alloced)
2333 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2334
2335 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002336}
2337
Quinn Tran82de8022017-06-13 20:47:17 -07002338static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002339 uint32_t req_cnt)
2340{
Quinn Trand63b3282017-06-02 09:12:07 -07002341 uint32_t cnt;
Quinn Tran82de8022017-06-13 20:47:17 -07002342 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002343
Quinn Tran82de8022017-06-13 20:47:17 -07002344 if (req->cnt < (req_cnt + 2)) {
Quinn Tranaf7bb382017-06-13 20:47:23 -07002345 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2346 RD_REG_DWORD_RELAXED(req->req_q_out));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002347
Quinn Tran82de8022017-06-13 20:47:17 -07002348 if (req->ring_index < cnt)
2349 req->cnt = cnt - req->ring_index;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002350 else
Quinn Tran82de8022017-06-13 20:47:17 -07002351 req->cnt = req->length - (req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002352
Quinn Tran82de8022017-06-13 20:47:17 -07002353 if (unlikely(req->cnt < (req_cnt + 2)))
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002354 return -EAGAIN;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002355 }
2356
Quinn Tran82de8022017-06-13 20:47:17 -07002357 req->cnt -= req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002358
2359 return 0;
2360}
2361
2362/*
2363 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2364 */
Quinn Tran82de8022017-06-13 20:47:17 -07002365static inline void *qlt_get_req_pkt(struct req_que *req)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002366{
2367 /* Adjust ring index. */
Quinn Tran82de8022017-06-13 20:47:17 -07002368 req->ring_index++;
2369 if (req->ring_index == req->length) {
2370 req->ring_index = 0;
2371 req->ring_ptr = req->ring;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002372 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002373 req->ring_ptr++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002374 }
Quinn Tran82de8022017-06-13 20:47:17 -07002375 return (cont_entry_t *)req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002376}
2377
2378/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002379static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002380{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002381 uint32_t h;
Quinn Tranc5419e22017-06-13 20:47:16 -07002382 int index;
2383 uint8_t found = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07002384 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002385
Quinn Tranc5419e22017-06-13 20:47:16 -07002386 h = req->current_outstanding_cmd;
2387
2388 for (index = 1; index < req->num_outstanding_cmds; index++) {
2389 h++;
2390 if (h == req->num_outstanding_cmds)
2391 h = 1;
2392
2393 if (h == QLA_TGT_SKIP_HANDLE)
2394 continue;
2395
2396 if (!req->outstanding_cmds[h]) {
2397 found = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002398 break;
2399 }
Quinn Tranc5419e22017-06-13 20:47:16 -07002400 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002401
Quinn Tranc5419e22017-06-13 20:47:16 -07002402 if (found) {
2403 req->current_outstanding_cmd = h;
2404 } else {
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002405 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2406 "qla_target(%d): Ran out of empty cmd slots\n",
2407 qpair->vha->vp_idx);
Quinn Tranc5419e22017-06-13 20:47:16 -07002408 h = QLA_TGT_NULL_HANDLE;
2409 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002410
2411 return h;
2412}
2413
2414/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002415static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2416 struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002417{
2418 uint32_t h;
2419 struct ctio7_to_24xx *pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002420 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002421 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002422
Quinn Tran82de8022017-06-13 20:47:17 -07002423 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002424 prm->pkt = pkt;
2425 memset(pkt, 0, sizeof(*pkt));
2426
2427 pkt->entry_type = CTIO_TYPE7;
2428 pkt->entry_count = (uint8_t)prm->req_cnt;
Quinn Tran22d84722017-06-13 20:47:25 -07002429 pkt->vp_index = prm->cmd->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002430
Quinn Tran82de8022017-06-13 20:47:17 -07002431 h = qlt_make_handle(qpair);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002432 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2433 /*
2434 * CTIO type 7 from the firmware doesn't provide a way to
2435 * know the initiator's LOOP ID, hence we can't find
2436 * the session and, so, the command.
2437 */
2438 return -EAGAIN;
Quinn Trane326d222017-06-13 20:47:18 -07002439 } else
2440 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002441
Quinn Tran82de8022017-06-13 20:47:17 -07002442 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2443 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2444 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002445 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002446 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2447 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2448 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2449 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002450 temp = atio->u.isp24.attr << 9;
2451 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002452 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2453 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002454 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2455
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002456 return 0;
2457}
2458
2459/*
2460 * ha->hardware_lock supposed to be held on entry. We have already made sure
2461 * that there is sufficient amount of request entries to not drop it.
2462 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002463static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002464{
2465 int cnt;
2466 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002467
2468 /* Build continuation packets */
2469 while (prm->seg_cnt > 0) {
2470 cont_a64_entry_t *cont_pkt64 =
Quinn Tran82de8022017-06-13 20:47:17 -07002471 (cont_a64_entry_t *)qlt_get_req_pkt(
2472 prm->cmd->qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002473
2474 /*
2475 * Make sure that from cont_pkt64 none of
2476 * 64-bit specific fields used for 32-bit
2477 * addressing. Cast to (cont_entry_t *) for
2478 * that.
2479 */
2480
2481 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2482
2483 cont_pkt64->entry_count = 1;
2484 cont_pkt64->sys_define = 0;
2485
Quinn Tran32d29b42017-06-13 20:47:26 -07002486 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2487 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002488
2489 /* Load continuation entry data segments */
2490 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002491 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002492 cnt++, prm->seg_cnt--) {
2493 *dword_ptr++ =
2494 cpu_to_le32(pci_dma_lo32
2495 (sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002496 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2497 (sg_dma_address(prm->sg)));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002498 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2499
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002500 prm->sg = sg_next(prm->sg);
2501 }
2502 }
2503}
2504
2505/*
2506 * ha->hardware_lock supposed to be held on entry. We have already made sure
2507 * that there is sufficient amount of request entries to not drop it.
2508 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002509static void qlt_load_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002510{
2511 int cnt;
2512 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002513 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2514
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002515 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2516
2517 /* Setup packet address segment pointer */
2518 dword_ptr = pkt24->u.status0.dseg_0_address;
2519
2520 /* Set total data segment count */
2521 if (prm->seg_cnt)
2522 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2523
2524 if (prm->seg_cnt == 0) {
2525 /* No data transfer */
2526 *dword_ptr++ = 0;
2527 *dword_ptr = 0;
2528 return;
2529 }
2530
2531 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002532
2533 /* Load command entry data segments */
2534 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002535 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002536 cnt++, prm->seg_cnt--) {
2537 *dword_ptr++ =
2538 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002539
2540 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2541 sg_dma_address(prm->sg)));
2542
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002543 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2544
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002545 prm->sg = sg_next(prm->sg);
2546 }
2547
Quinn Tran32d29b42017-06-13 20:47:26 -07002548 qlt_load_cont_data_segments(prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002549}
2550
2551static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2552{
2553 return cmd->bufflen > 0;
2554}
2555
Quinn Tranbe251522017-03-15 09:48:49 -07002556static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2557{
2558 struct qla_tgt_cmd *cmd;
2559 struct scsi_qla_host *vha;
2560
2561 /* asc 0x10=dif error */
2562 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2563 cmd = prm->cmd;
2564 vha = cmd->vha;
2565 /* ASCQ */
2566 switch (prm->sense_buffer[13]) {
2567 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002568 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002569 "BE detected Guard 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 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002575 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002576 "BE detected APP 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 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002582 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002583 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%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 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002589 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002590 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2591 "se_cmd=%p tag[%x]",
2592 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2593 cmd->atio.u.isp24.exchange_addr);
2594 break;
2595 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002596 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002597 }
2598}
2599
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002600/*
2601 * Called without ha->hardware_lock held
2602 */
2603static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2604 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2605 uint32_t *full_req_cnt)
2606{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002607 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Tran22d84722017-06-13 20:47:25 -07002608 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002609
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002610 prm->cmd = cmd;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002611 prm->tgt = cmd->tgt;
2612 prm->pkt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002613 prm->rq_result = scsi_status;
2614 prm->sense_buffer = &cmd->sense_buffer[0];
2615 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2616 prm->sg = NULL;
2617 prm->seg_cnt = -1;
2618 prm->req_cnt = 1;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002619 prm->residual = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002620 prm->add_status_pkt = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002621 prm->prot_sg = NULL;
2622 prm->prot_seg_cnt = 0;
2623 prm->tot_dsds = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002624
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002625 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2626 if (qlt_pci_map_calc_cnt(prm) != 0)
2627 return -EAGAIN;
2628 }
2629
2630 *full_req_cnt = prm->req_cnt;
2631
2632 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2633 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002634 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002635 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2636 prm->residual, se_cmd->tag,
2637 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2638 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002639 prm->rq_result |= SS_RESIDUAL_UNDER;
2640 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2641 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002642 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002643 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2644 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2645 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002646 prm->rq_result |= SS_RESIDUAL_OVER;
2647 }
2648
2649 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2650 /*
2651 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2652 * ignored in *xmit_response() below
2653 */
2654 if (qlt_has_data(cmd)) {
2655 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002656 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002657 (prm->rq_result != 0))) {
2658 prm->add_status_pkt = 1;
2659 (*full_req_cnt)++;
2660 }
2661 }
2662 }
2663
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002664 return 0;
2665}
2666
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002667static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2668 int sending_sense)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002669{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002670 if (cmd->qpair->enable_class_2)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002671 return 0;
2672
2673 if (sending_sense)
2674 return cmd->conf_compl_supported;
2675 else
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002676 return cmd->qpair->enable_explicit_conf &&
2677 cmd->conf_compl_supported;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002678}
2679
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002680static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2681 struct qla_tgt_prm *prm)
2682{
2683 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2684 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002685 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002686 if (qlt_need_explicit_conf(prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002687 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002688 CTIO7_FLAGS_EXPLICIT_CONFORM |
2689 CTIO7_FLAGS_CONFORM_REQ);
2690 }
2691 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2692 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2693 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2694 int i;
2695
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002696 if (qlt_need_explicit_conf(prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002697 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Quinn Tran22d84722017-06-13 20:47:25 -07002698 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002699 "Skipping EXPLICIT_CONFORM and "
2700 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2701 "non GOOD status\n");
2702 goto skip_explict_conf;
2703 }
Bart Van Asschead950362015-07-09 07:24:08 -07002704 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002705 CTIO7_FLAGS_EXPLICIT_CONFORM |
2706 CTIO7_FLAGS_CONFORM_REQ);
2707 }
2708skip_explict_conf:
2709 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002710 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002711 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002712 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002713 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002714 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002715 ctio->u.status1.sense_length =
2716 cpu_to_le16(prm->sense_buffer_len);
2717 for (i = 0; i < prm->sense_buffer_len/4; i++)
2718 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2719 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002720
2721 qlt_print_dif_err(prm);
2722
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002723 } else {
2724 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002725 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002726 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002727 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002728 ctio->u.status1.sense_length = 0;
2729 memset(ctio->u.status1.sense_data, 0,
2730 sizeof(ctio->u.status1.sense_data));
2731 }
2732
2733 /* Sense with len > 24, is it possible ??? */
2734}
2735
Quinn Tranf83adb62014-04-11 16:54:43 -04002736static inline int
2737qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2738{
Quinn Tranf83adb62014-04-11 16:54:43 -04002739 switch (se_cmd->prot_op) {
2740 case TARGET_PROT_DOUT_INSERT:
2741 case TARGET_PROT_DIN_STRIP:
2742 if (ql2xenablehba_err_chk >= 1)
2743 return 1;
2744 break;
2745 case TARGET_PROT_DOUT_PASS:
2746 case TARGET_PROT_DIN_PASS:
2747 if (ql2xenablehba_err_chk >= 2)
2748 return 1;
2749 break;
2750 case TARGET_PROT_DIN_INSERT:
2751 case TARGET_PROT_DOUT_STRIP:
2752 return 1;
2753 default:
2754 break;
2755 }
2756 return 0;
2757}
2758
Quinn Tranbe251522017-03-15 09:48:49 -07002759static inline int
2760qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002761{
Quinn Tranbe251522017-03-15 09:48:49 -07002762 switch (se_cmd->prot_op) {
2763 case TARGET_PROT_DIN_INSERT:
2764 case TARGET_PROT_DOUT_INSERT:
2765 case TARGET_PROT_DIN_STRIP:
2766 case TARGET_PROT_DOUT_STRIP:
2767 case TARGET_PROT_DIN_PASS:
2768 case TARGET_PROT_DOUT_PASS:
2769 return 1;
2770 default:
2771 return 0;
2772 }
2773 return 0;
2774}
Quinn Tranf83adb62014-04-11 16:54:43 -04002775
Quinn Tranbe251522017-03-15 09:48:49 -07002776/*
2777 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2778 */
2779static void
2780qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2781 uint16_t *pfw_prot_opts)
2782{
2783 struct se_cmd *se_cmd = &cmd->se_cmd;
2784 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2785 scsi_qla_host_t *vha = cmd->tgt->vha;
2786 struct qla_hw_data *ha = vha->hw;
2787 uint32_t t32 = 0;
2788
2789 /*
2790 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002791 * have been immplemented by TCM, before AppTag is avail.
2792 * Look for modesense_handlers[]
2793 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002794 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002795 ctx->app_tag_mask[0] = 0x0;
2796 ctx->app_tag_mask[1] = 0x0;
2797
Quinn Tranbe251522017-03-15 09:48:49 -07002798 if (IS_PI_UNINIT_CAPABLE(ha)) {
2799 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2800 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2801 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2802 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2803 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2804 }
2805
2806 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2807
Quinn Tranf83adb62014-04-11 16:54:43 -04002808 switch (se_cmd->prot_type) {
2809 case TARGET_DIF_TYPE0_PROT:
2810 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002811 * No check for ql2xenablehba_err_chk, as it
2812 * would be an I/O error if hba tag generation
2813 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002814 */
2815 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002816 /* enable ALL bytes of the ref tag */
2817 ctx->ref_tag_mask[0] = 0xff;
2818 ctx->ref_tag_mask[1] = 0xff;
2819 ctx->ref_tag_mask[2] = 0xff;
2820 ctx->ref_tag_mask[3] = 0xff;
2821 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002822 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002823 /*
2824 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2825 * REF tag, and 16 bit app tag.
2826 */
2827 ctx->ref_tag = cpu_to_le32(lba);
2828 if (!qla_tgt_ref_mask_check(se_cmd) ||
2829 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2830 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2831 break;
2832 }
2833 /* enable ALL bytes of the ref tag */
2834 ctx->ref_tag_mask[0] = 0xff;
2835 ctx->ref_tag_mask[1] = 0xff;
2836 ctx->ref_tag_mask[2] = 0xff;
2837 ctx->ref_tag_mask[3] = 0xff;
2838 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002839 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002840 /*
2841 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2842 * tag has to match LBA in CDB + N
2843 */
2844 ctx->ref_tag = cpu_to_le32(lba);
2845 if (!qla_tgt_ref_mask_check(se_cmd) ||
2846 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2847 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2848 break;
2849 }
2850 /* enable ALL bytes of the ref tag */
2851 ctx->ref_tag_mask[0] = 0xff;
2852 ctx->ref_tag_mask[1] = 0xff;
2853 ctx->ref_tag_mask[2] = 0xff;
2854 ctx->ref_tag_mask[3] = 0xff;
2855 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002856 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002857 /* For TYPE 3 protection: 16 bit GUARD only */
2858 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2859 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2860 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2861 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002862 }
2863}
2864
Quinn Tranf83adb62014-04-11 16:54:43 -04002865static inline int
Quinn Tran82de8022017-06-13 20:47:17 -07002866qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
Quinn Tranf83adb62014-04-11 16:54:43 -04002867{
2868 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002869 uint32_t transfer_length = 0;
2870 uint32_t data_bytes;
2871 uint32_t dif_bytes;
2872 uint8_t bundling = 1;
2873 uint8_t *clr_ptr;
2874 struct crc_context *crc_ctx_pkt = NULL;
2875 struct qla_hw_data *ha;
2876 struct ctio_crc2_to_fw *pkt;
2877 dma_addr_t crc_ctx_dma;
2878 uint16_t fw_prot_opts = 0;
2879 struct qla_tgt_cmd *cmd = prm->cmd;
2880 struct se_cmd *se_cmd = &cmd->se_cmd;
2881 uint32_t h;
2882 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07002883 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002884 uint16_t t16;
Quinn Tran82de8022017-06-13 20:47:17 -07002885 scsi_qla_host_t *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04002886
Quinn Tranf83adb62014-04-11 16:54:43 -04002887 ha = vha->hw;
2888
Quinn Tran82de8022017-06-13 20:47:17 -07002889 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
Quinn Tranf83adb62014-04-11 16:54:43 -04002890 prm->pkt = pkt;
2891 memset(pkt, 0, sizeof(*pkt));
2892
Quinn Tran22d84722017-06-13 20:47:25 -07002893 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
Quinn Tranf83adb62014-04-11 16:54:43 -04002894 "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 -07002895 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
Quinn Tranf83adb62014-04-11 16:54:43 -04002896 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2897
2898 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2899 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2900 bundling = 0;
2901
2902 /* Compute dif len and adjust data len to incude protection */
2903 data_bytes = cmd->bufflen;
2904 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2905
2906 switch (se_cmd->prot_op) {
2907 case TARGET_PROT_DIN_INSERT:
2908 case TARGET_PROT_DOUT_STRIP:
2909 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07002910 if (cmd->prot_sg_cnt)
2911 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04002912 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002913 case TARGET_PROT_DIN_STRIP:
2914 case TARGET_PROT_DOUT_INSERT:
2915 case TARGET_PROT_DIN_PASS:
2916 case TARGET_PROT_DOUT_PASS:
2917 transfer_length = data_bytes + dif_bytes;
2918 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002919 default:
2920 BUG();
2921 break;
2922 }
2923
2924 if (!qlt_hba_err_chk_enabled(se_cmd))
2925 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2926 /* HBA error checking enabled */
2927 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2928 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2929 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2930 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2931 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2932 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2933 }
2934
2935 switch (se_cmd->prot_op) {
2936 case TARGET_PROT_DIN_INSERT:
2937 case TARGET_PROT_DOUT_INSERT:
2938 fw_prot_opts |= PO_MODE_DIF_INSERT;
2939 break;
2940 case TARGET_PROT_DIN_STRIP:
2941 case TARGET_PROT_DOUT_STRIP:
2942 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2943 break;
2944 case TARGET_PROT_DIN_PASS:
2945 case TARGET_PROT_DOUT_PASS:
2946 fw_prot_opts |= PO_MODE_DIF_PASS;
2947 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2948 break;
2949 default:/* Normal Request */
2950 fw_prot_opts |= PO_MODE_DIF_PASS;
2951 break;
2952 }
2953
Quinn Tranf83adb62014-04-11 16:54:43 -04002954 /* ---- PKT ---- */
2955 /* Update entry type to indicate Command Type CRC_2 IOCB */
2956 pkt->entry_type = CTIO_CRC2;
2957 pkt->entry_count = 1;
Quinn Tran22d84722017-06-13 20:47:25 -07002958 pkt->vp_index = cmd->vp_idx;
Quinn Tranf83adb62014-04-11 16:54:43 -04002959
Quinn Tran82de8022017-06-13 20:47:17 -07002960 h = qlt_make_handle(qpair);
Quinn Tranf83adb62014-04-11 16:54:43 -04002961 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2962 /*
2963 * CTIO type 7 from the firmware doesn't provide a way to
2964 * know the initiator's LOOP ID, hence we can't find
2965 * the session and, so, the command.
2966 */
2967 return -EAGAIN;
2968 } else
Quinn Tran82de8022017-06-13 20:47:17 -07002969 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002970
Quinn Tran82de8022017-06-13 20:47:17 -07002971 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2972 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07002973 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002974 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002975 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2976 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2977 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2978 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002979
2980 /* silence compile warning */
2981 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2982 pkt->ox_id = cpu_to_le16(t16);
2983
2984 t16 = (atio->u.isp24.attr << 9);
2985 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002986 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2987
2988 /* Set transfer direction */
2989 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002990 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002991 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002992 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002993
Quinn Tranf83adb62014-04-11 16:54:43 -04002994 pkt->dseg_count = prm->tot_dsds;
2995 /* Fibre channel byte count */
2996 pkt->transfer_length = cpu_to_le32(transfer_length);
2997
Quinn Tranf83adb62014-04-11 16:54:43 -04002998 /* ----- CRC context -------- */
2999
3000 /* Allocate CRC context from global pool */
3001 crc_ctx_pkt = cmd->ctx =
3002 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3003
3004 if (!crc_ctx_pkt)
3005 goto crc_queuing_error;
3006
3007 /* Zero out CTX area. */
3008 clr_ptr = (uint8_t *)crc_ctx_pkt;
3009 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
3010
3011 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3012 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3013
3014 /* Set handle */
3015 crc_ctx_pkt->handle = pkt->handle;
3016
Quinn Tranbe251522017-03-15 09:48:49 -07003017 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04003018
3019 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
3020 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
3021 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3022
Quinn Tranf83adb62014-04-11 16:54:43 -04003023 if (!bundling) {
3024 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
3025 } else {
3026 /*
3027 * Configure Bundling if we need to fetch interlaving
3028 * protection PCI accesses
3029 */
3030 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3031 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3032 crc_ctx_pkt->u.bundling.dseg_count =
3033 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3034 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
3035 }
3036
3037 /* Finish the common fields of CRC pkt */
3038 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3039 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3040 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07003041 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003042
Quinn Tranbe251522017-03-15 09:48:49 -07003043 memset((uint8_t *)&tc, 0 , sizeof(tc));
3044 tc.vha = vha;
3045 tc.blk_sz = cmd->blk_sz;
3046 tc.bufflen = cmd->bufflen;
3047 tc.sg = cmd->sg;
3048 tc.prot_sg = cmd->prot_sg;
3049 tc.ctx = crc_ctx_pkt;
3050 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04003051
3052 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07003053 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04003054
3055 if (!bundling && prm->prot_seg_cnt) {
3056 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003057 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003058 goto crc_queuing_error;
3059 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003060 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003061 goto crc_queuing_error;
3062
3063 if (bundling && prm->prot_seg_cnt) {
3064 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003065 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04003066
3067 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3068 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003069 prm->prot_seg_cnt, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003070 goto crc_queuing_error;
3071 }
3072 return QLA_SUCCESS;
3073
3074crc_queuing_error:
3075 /* Cleanup will be performed by the caller */
Quinn Tran82de8022017-06-13 20:47:17 -07003076 qpair->req->outstanding_cmds[h] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003077
3078 return QLA_FUNCTION_FAILED;
3079}
3080
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003081/*
3082 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3083 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3084 */
3085int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3086 uint8_t scsi_status)
3087{
3088 struct scsi_qla_host *vha = cmd->vha;
Quinn Tran82de8022017-06-13 20:47:17 -07003089 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003090 struct ctio7_to_24xx *pkt;
3091 struct qla_tgt_prm prm;
3092 uint32_t full_req_cnt = 0;
3093 unsigned long flags = 0;
3094 int res;
3095
Quinn Tran726b8542017-01-19 22:28:00 -08003096 if (cmd->sess && cmd->sess->deleted) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003097 cmd->state = QLA_TGT_STATE_PROCESSED;
3098 if (cmd->sess->logout_completed)
3099 /* no need to terminate. FW already freed exchange. */
3100 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3101 else
Quinn Tran82de8022017-06-13 20:47:17 -07003102 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003103 return 0;
3104 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003105
Quinn Tran22d84722017-06-13 20:47:25 -07003106 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
Quinn Tran82de8022017-06-13 20:47:17 -07003107 "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 -04003108 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3109 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
Quinn Tran82de8022017-06-13 20:47:17 -07003110 &cmd->se_cmd, qpair->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003111
3112 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3113 &full_req_cnt);
3114 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003115 return res;
3116 }
3117
Quinn Tran82de8022017-06-13 20:47:17 -07003118 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003119
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003120 if (xmit_type == QLA_TGT_XMIT_STATUS)
Quinn Tran60a9ead2017-06-13 20:47:28 -07003121 qpair->tgt_counters.core_qla_snd_status++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003122 else
Quinn Tran60a9ead2017-06-13 20:47:28 -07003123 qpair->tgt_counters.core_qla_que_buf++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003124
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003125 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003126 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003127 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003128 * previous life, just abort the processing.
3129 */
3130 cmd->state = QLA_TGT_STATE_PROCESSED;
3131 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003132 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003133 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3134 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003135 cmd->reset_count, qpair->chip_reset);
Quinn Tran82de8022017-06-13 20:47:17 -07003136 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04003137 return 0;
3138 }
3139
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003140 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003141 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003142 if (unlikely(res))
3143 goto out_unmap_unlock;
3144
Quinn Tranf83adb62014-04-11 16:54:43 -04003145 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
Quinn Tran82de8022017-06-13 20:47:17 -07003146 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003147 else
Quinn Tran82de8022017-06-13 20:47:17 -07003148 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Quinn Tran810e30b2015-06-10 11:05:20 -04003149 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003150 qpair->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003151 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003152 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003153
3154 pkt = (struct ctio7_to_24xx *)prm.pkt;
3155
3156 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3157 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003158 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003159 CTIO7_FLAGS_STATUS_MODE_0);
3160
Quinn Tranf83adb62014-04-11 16:54:43 -04003161 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003162 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003163
3164 if (prm.add_status_pkt == 0) {
3165 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3166 pkt->u.status0.scsi_status =
3167 cpu_to_le16(prm.rq_result);
3168 pkt->u.status0.residual =
3169 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003170 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003171 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003172 if (qlt_need_explicit_conf(cmd, 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003173 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003174 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003175 CTIO7_FLAGS_EXPLICIT_CONFORM |
3176 CTIO7_FLAGS_CONFORM_REQ);
3177 }
3178 }
3179
3180 } else {
3181 /*
3182 * We have already made sure that there is sufficient
3183 * amount of request entries to not drop HW lock in
3184 * req_pkt().
3185 */
3186 struct ctio7_to_24xx *ctio =
Quinn Tran82de8022017-06-13 20:47:17 -07003187 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3188 qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003189
Quinn Tran22d84722017-06-13 20:47:25 -07003190 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
Arun Easi667024a2014-09-25 06:14:47 -04003191 "Building additional status packet 0x%p.\n",
3192 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003193
Quinn Tranf83adb62014-04-11 16:54:43 -04003194 /*
3195 * T10Dif: ctio_crc2_to_fw overlay ontop of
3196 * ctio7_to_24xx
3197 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003198 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003199 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003200 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003201 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003202 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003203 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003204 CTIO7_FLAGS_DATA_IN);
3205
3206 /* Real finish is ctio_m1's finish */
3207 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003208 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003209 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003210
3211 /* qlt_24xx_init_ctio_to_isp will correct
3212 * all neccessary fields that's part of CTIO7.
3213 * There should be no residual of CTIO-CRC2 data.
3214 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003215 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3216 &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003217 }
3218 } else
3219 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3220
3221
3222 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003223 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003224
Himanshu Madhani63163e02014-09-25 06:14:59 -04003225 /* Memory Barrier */
3226 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003227 if (qpair->reqq_start_iocbs)
3228 qpair->reqq_start_iocbs(qpair);
3229 else
3230 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003231 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003232
3233 return 0;
3234
3235out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003236 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003237 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003238
3239 return res;
3240}
3241EXPORT_SYMBOL(qlt_xmit_response);
3242
3243int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3244{
3245 struct ctio7_to_24xx *pkt;
3246 struct scsi_qla_host *vha = cmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003247 struct qla_tgt *tgt = cmd->tgt;
3248 struct qla_tgt_prm prm;
Quinn Tran82de8022017-06-13 20:47:17 -07003249 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003250 int res = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003251 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003252
3253 memset(&prm, 0, sizeof(prm));
3254 prm.cmd = cmd;
3255 prm.tgt = tgt;
3256 prm.sg = NULL;
3257 prm.req_cnt = 1;
3258
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003259 /* Calculate number of entries and segments required */
3260 if (qlt_pci_map_calc_cnt(&prm) != 0)
3261 return -EAGAIN;
3262
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003263 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003264 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003265 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003266 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003267 * previous life, just abort the processing.
3268 */
3269 cmd->state = QLA_TGT_STATE_NEED_DATA;
3270 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003271 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003272 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3273 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003274 cmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003275 return 0;
3276 }
3277
Quinn Tran82de8022017-06-13 20:47:17 -07003278 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003279 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003280 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003281 if (res != 0)
3282 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003283 if (cmd->se_cmd.prot_op)
Quinn Tran82de8022017-06-13 20:47:17 -07003284 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003285 else
Quinn Tran82de8022017-06-13 20:47:17 -07003286 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003287
Quinn Tran810e30b2015-06-10 11:05:20 -04003288 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003289 qpair->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003290 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003291 }
3292
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003293 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003294 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003295 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003296
3297 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003298 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003299
3300 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003301 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003302
Himanshu Madhani63163e02014-09-25 06:14:59 -04003303 /* Memory Barrier */
3304 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003305 if (qpair->reqq_start_iocbs)
3306 qpair->reqq_start_iocbs(qpair);
3307 else
3308 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003309 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003310
3311 return res;
3312
3313out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003314 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003315 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003316
3317 return res;
3318}
3319EXPORT_SYMBOL(qlt_rdy_to_xfer);
3320
Quinn Tranf83adb62014-04-11 16:54:43 -04003321
3322/*
Quinn Tranbe251522017-03-15 09:48:49 -07003323 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003324 */
Quinn Tranbe251522017-03-15 09:48:49 -07003325static void
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003326qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003327 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003328{
3329 uint8_t *ap = &sts->actual_dif[0];
3330 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003331 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003332 uint8_t scsi_status, sense_key, asc, ascq;
3333 unsigned long flags;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003334 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003335
Quinn Tranbe251522017-03-15 09:48:49 -07003336 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003337
Quinn Tranbe251522017-03-15 09:48:49 -07003338 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3339 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3340 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003341
Quinn Tranbe251522017-03-15 09:48:49 -07003342 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3343 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3344 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003345
Quinn Tranbe251522017-03-15 09:48:49 -07003346 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3347 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003348
Quinn Tranbe251522017-03-15 09:48:49 -07003349 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003350
Quinn Tranbe251522017-03-15 09:48:49 -07003351 /* check appl tag */
3352 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003353 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3354 "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]",
3355 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3356 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3357 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3358 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003359
Quinn Tranbe251522017-03-15 09:48:49 -07003360 cmd->dif_err_code = DIF_ERR_APP;
3361 scsi_status = SAM_STAT_CHECK_CONDITION;
3362 sense_key = ABORTED_COMMAND;
3363 asc = 0x10;
3364 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003365 }
3366
3367 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003368 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003369 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3370 "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] ",
3371 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3372 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3373 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3374 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003375
Quinn Tranbe251522017-03-15 09:48:49 -07003376 cmd->dif_err_code = DIF_ERR_REF;
3377 scsi_status = SAM_STAT_CHECK_CONDITION;
3378 sense_key = ABORTED_COMMAND;
3379 asc = 0x10;
3380 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003381 goto out;
3382 }
3383
Quinn Tranbe251522017-03-15 09:48:49 -07003384 /* check guard */
3385 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003386 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3387 "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]",
3388 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3389 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3390 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3391 cmd->atio.u.isp24.fcp_hdr.ox_id);
3392
Quinn Tranbe251522017-03-15 09:48:49 -07003393 cmd->dif_err_code = DIF_ERR_GRD;
3394 scsi_status = SAM_STAT_CHECK_CONDITION;
3395 sense_key = ABORTED_COMMAND;
3396 asc = 0x10;
3397 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003398 }
3399out:
Quinn Tranbe251522017-03-15 09:48:49 -07003400 switch (cmd->state) {
3401 case QLA_TGT_STATE_NEED_DATA:
3402 /* handle_data will load DIF error code */
3403 cmd->state = QLA_TGT_STATE_DATA_IN;
3404 vha->hw->tgt.tgt_ops->handle_data(cmd);
3405 break;
3406 default:
3407 spin_lock_irqsave(&cmd->cmd_lock, flags);
3408 if (cmd->aborted) {
3409 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3410 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3411 break;
3412 }
3413 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003414
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003415 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3416 ascq);
Quinn Tranbe251522017-03-15 09:48:49 -07003417 /* assume scsi status gets out on the wire.
3418 * Will not wait for completion.
3419 */
3420 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3421 break;
3422 }
3423}
Quinn Tranf83adb62014-04-11 16:54:43 -04003424
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003425/* If hardware_lock held on entry, might drop it, then reaquire */
3426/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003427static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3428 struct imm_ntfy_from_isp *ntfy)
3429{
3430 struct nack_to_isp *nack;
3431 struct qla_hw_data *ha = vha->hw;
3432 request_t *pkt;
3433 int ret = 0;
3434
3435 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3436 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3437
Quinn Tranec7193e2017-03-15 09:48:55 -07003438 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003439 if (pkt == NULL) {
3440 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3441 "qla_target(%d): %s failed: unable to allocate "
3442 "request packet\n", vha->vp_idx, __func__);
3443 return -ENOMEM;
3444 }
3445
3446 pkt->entry_type = NOTIFY_ACK_TYPE;
3447 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003448 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003449
3450 nack = (struct nack_to_isp *)pkt;
3451 nack->ox_id = ntfy->ox_id;
3452
3453 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3454 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3455 nack->u.isp24.flags = ntfy->u.isp24.flags &
3456 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3457 }
3458
3459 /* terminate */
3460 nack->u.isp24.flags |=
3461 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3462
3463 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3464 nack->u.isp24.status = ntfy->u.isp24.status;
3465 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3466 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3467 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3468 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3469 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3470 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3471
3472 qla2x00_start_iocbs(vha, vha->req);
3473 return ret;
3474}
3475
3476static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3477 struct imm_ntfy_from_isp *imm, int ha_locked)
3478{
3479 unsigned long flags = 0;
3480 int rc;
3481
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003482 if (ha_locked) {
3483 rc = __qlt_send_term_imm_notif(vha, imm);
3484
3485#if 0 /* Todo */
3486 if (rc == -ENOMEM)
3487 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003488#else
3489 if (rc) {
3490 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003491#endif
3492 goto done;
3493 }
3494
3495 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3496 rc = __qlt_send_term_imm_notif(vha, imm);
3497
3498#if 0 /* Todo */
3499 if (rc == -ENOMEM)
3500 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3501#endif
3502
3503done:
3504 if (!ha_locked)
3505 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3506}
3507
Quinn Tran83548fe2017-06-02 09:12:01 -07003508/*
3509 * If hardware_lock held on entry, might drop it, then reaquire
3510 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3511 */
Quinn Tran82de8022017-06-13 20:47:17 -07003512static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003513 struct qla_tgt_cmd *cmd,
3514 struct atio_from_isp *atio)
3515{
Quinn Tran82de8022017-06-13 20:47:17 -07003516 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003517 struct ctio7_to_24xx *ctio24;
3518 struct qla_hw_data *ha = vha->hw;
3519 request_t *pkt;
3520 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003521 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003522
Quinn Tran83548fe2017-06-02 09:12:01 -07003523 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003524
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003525 if (cmd)
3526 vha = cmd->vha;
3527
Quinn Tran82de8022017-06-13 20:47:17 -07003528 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003529 if (pkt == NULL) {
3530 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3531 "qla_target(%d): %s failed: unable to allocate "
3532 "request packet\n", vha->vp_idx, __func__);
3533 return -ENOMEM;
3534 }
3535
3536 if (cmd != NULL) {
3537 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3538 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3539 "qla_target(%d): Terminating cmd %p with "
3540 "incorrect state %d\n", vha->vp_idx, cmd,
3541 cmd->state);
3542 } else
3543 ret = 1;
3544 }
3545
Quinn Tran60a9ead2017-06-13 20:47:28 -07003546 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003547 pkt->entry_count = 1;
3548 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3549
3550 ctio24 = (struct ctio7_to_24xx *)pkt;
3551 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003552 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003553 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003554 ctio24->vp_index = vha->vp_idx;
3555 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3556 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3557 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3558 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003559 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3560 CTIO7_FLAGS_TERMINATE;
3561 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003562 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3563 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003564
3565 /* Most likely, it isn't needed */
3566 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3567 &atio->u.isp24.fcp_cmnd.add_cdb[
3568 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3569 if (ctio24->u.status1.residual != 0)
3570 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3571
Himanshu Madhani63163e02014-09-25 06:14:59 -04003572 /* Memory Barrier */
3573 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003574 if (qpair->reqq_start_iocbs)
3575 qpair->reqq_start_iocbs(qpair);
3576 else
3577 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003578 return ret;
3579}
3580
Quinn Tran82de8022017-06-13 20:47:17 -07003581static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003582 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3583 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003584{
Quinn Tran82de8022017-06-13 20:47:17 -07003585 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003586 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003587 int rc;
3588
Quinn Tran82de8022017-06-13 20:47:17 -07003589 /* why use different vha? NPIV */
3590 if (cmd)
3591 vha = cmd->vha;
3592 else
3593 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003594
3595 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003596 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003597 if (rc == -ENOMEM)
3598 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003599 goto done;
3600 }
Quinn Tran82de8022017-06-13 20:47:17 -07003601 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3602 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003603 if (rc == -ENOMEM)
3604 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003605
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003606done:
Quinn Trana07100e2015-12-07 19:48:57 -05003607 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003608 if (cmd->sg_mapped)
3609 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003610 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3611 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003612
3613 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003614 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003615
Quinn Tran7b898542014-04-11 16:54:44 -04003616 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003617}
3618
Quinn Tran33e79972014-09-25 06:14:55 -04003619static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3620{
3621 struct list_head free_list;
3622 struct qla_tgt_cmd *cmd, *tcmd;
3623
3624 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003625 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003626
3627 cmd = tcmd = NULL;
3628 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3629 INIT_LIST_HEAD(&free_list);
3630 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3631
3632 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3633 list_del(&cmd->cmd_list);
3634 /* This cmd was never sent to TCM. There is no need
3635 * to schedule free or call free_cmd
3636 */
3637 qlt_free_cmd(cmd);
3638 vha->hw->tgt.num_qfull_cmds_alloc--;
3639 }
3640 }
3641 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3642}
3643
3644static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3645{
3646 uint32_t total_leaked;
3647
3648 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3649
3650 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3651 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3652
3653 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3654 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003655 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003656
3657 if (IS_P3P_TYPE(vha->hw))
3658 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3659 else
3660 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3661 qla2xxx_wake_dpc(vha);
3662 }
3663
3664}
3665
Quinn Trana07100e2015-12-07 19:48:57 -05003666int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003667{
3668 struct qla_tgt *tgt = cmd->tgt;
3669 struct scsi_qla_host *vha = tgt->vha;
3670 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003671 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003672
3673 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3674 "qla_target(%d): terminating exchange for aborted cmd=%p "
3675 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3676 se_cmd->tag);
3677
Quinn Trana07100e2015-12-07 19:48:57 -05003678 spin_lock_irqsave(&cmd->cmd_lock, flags);
3679 if (cmd->aborted) {
3680 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3681 /*
3682 * It's normal to see 2 calls in this path:
3683 * 1) XFER Rdy completion + CMD_T_ABORT
3684 * 2) TCM TMR - drain_state_list
3685 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003686 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3687 "multiple abort. %p transport_state %x, t_state %x, "
3688 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3689 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003690 return EIO;
3691 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003692 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003693 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003694 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003695
Quinn Tran82de8022017-06-13 20:47:17 -07003696 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003697 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003698}
3699EXPORT_SYMBOL(qlt_abort_cmd);
3700
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003701void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3702{
Quinn Tran5d964832017-01-19 22:27:59 -08003703 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003704
Quinn Tranf83adb62014-04-11 16:54:43 -04003705 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3706 "%s: se_cmd[%p] ox_id %04x\n",
3707 __func__, &cmd->se_cmd,
3708 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003709
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003710 BUG_ON(cmd->cmd_in_wq);
3711
Quinn Trana07100e2015-12-07 19:48:57 -05003712 if (cmd->sg_mapped)
3713 qlt_unmap_sg(cmd->vha, cmd);
3714
Quinn Tran33e79972014-09-25 06:14:55 -04003715 if (!cmd->q_full)
3716 qlt_decr_num_pend_cmds(cmd->vha);
3717
Quinn Tranf83adb62014-04-11 16:54:43 -04003718 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003719 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003720 if (unlikely(cmd->free_sg))
3721 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003722
3723 if (!sess || !sess->se_sess) {
3724 WARN_ON(1);
3725 return;
3726 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003727 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003728 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003729}
3730EXPORT_SYMBOL(qlt_free_cmd);
3731
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003732/*
3733 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3734 */
Quinn Tran82de8022017-06-13 20:47:17 -07003735static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003736 struct qla_tgt_cmd *cmd, uint32_t status)
3737{
3738 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003739 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003740
Quinn Tranbe251522017-03-15 09:48:49 -07003741 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003742 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003743 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3744 "se_cmd=%p tag[%x] op %#x/%s",
3745 cmd->lba, cmd->lba,
3746 cmd->num_blks, &cmd->se_cmd,
3747 cmd->atio.u.isp24.exchange_addr,
3748 cmd->se_cmd.prot_op,
3749 prot_op_str(cmd->se_cmd.prot_op));
3750
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003751 if (ctio != NULL) {
3752 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3753 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003754 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003755 } else
3756 term = 1;
3757
3758 if (term)
Quinn Tran82de8022017-06-13 20:47:17 -07003759 qlt_term_ctio_exchange(qpair, ctio, cmd, status);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003760
3761 return term;
3762}
3763
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003764
3765/* ha->hardware_lock supposed to be held on entry */
3766static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003767 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003768{
3769 struct qla_tgt_cmd *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003770 struct req_que *req;
3771 int qid = GET_QID(handle);
3772 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003773
Quinn Tran82de8022017-06-13 20:47:17 -07003774 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3775 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003776
Quinn Tran82de8022017-06-13 20:47:17 -07003777 if (qid == rsp->req->id) {
3778 req = rsp->req;
3779 } else if (vha->hw->req_q_map[qid]) {
3780 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3781 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3782 vha->vp_idx, rsp->id, handle);
3783 req = vha->hw->req_q_map[qid];
3784 } else {
3785 return NULL;
3786 }
Arun Easi667024a2014-09-25 06:14:47 -04003787
Quinn Tran82de8022017-06-13 20:47:17 -07003788 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003789
Quinn Tran82de8022017-06-13 20:47:17 -07003790 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003791 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003792 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3793 "qla_target(%d): Wrong handle %x received\n",
3794 vha->vp_idx, handle);
3795 return NULL;
3796 }
Quinn Tran82de8022017-06-13 20:47:17 -07003797
3798 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3799 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003800 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003801 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3802 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003803 return NULL;
3804 }
Quinn Tran82de8022017-06-13 20:47:17 -07003805 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003806 } else if (ctio != NULL) {
3807 /* We can't get loop ID from CTIO7 */
3808 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3809 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3810 "support NULL handles\n", vha->vp_idx);
3811 return NULL;
3812 }
3813
3814 return cmd;
3815}
3816
Arun Easic0cb4492014-09-25 06:14:51 -04003817/* hardware_lock should be held by caller. */
Quinn Tranc5419e22017-06-13 20:47:16 -07003818void
Arun Easic0cb4492014-09-25 06:14:51 -04003819qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3820{
3821 struct qla_hw_data *ha = vha->hw;
Arun Easic0cb4492014-09-25 06:14:51 -04003822
3823 if (cmd->sg_mapped)
3824 qlt_unmap_sg(vha, cmd);
3825
Arun Easic0cb4492014-09-25 06:14:51 -04003826 /* TODO: fix debug message type and ids. */
3827 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3828 ql_dbg(ql_dbg_io, vha, 0xff00,
Quinn Tran82de8022017-06-13 20:47:17 -07003829 "HOST-ABORT: state=PROCESSED.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003830 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3831 cmd->write_data_transferred = 0;
3832 cmd->state = QLA_TGT_STATE_DATA_IN;
3833
3834 ql_dbg(ql_dbg_io, vha, 0xff01,
Quinn Tran82de8022017-06-13 20:47:17 -07003835 "HOST-ABORT: state=DATA_IN.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003836
3837 ha->tgt.tgt_ops->handle_data(cmd);
3838 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003839 } else {
3840 ql_dbg(ql_dbg_io, vha, 0xff03,
Quinn Tran82de8022017-06-13 20:47:17 -07003841 "HOST-ABORT: state=BAD(%d).\n",
Arun Easic0cb4492014-09-25 06:14:51 -04003842 cmd->state);
3843 dump_stack();
3844 }
3845
Quinn Tran1eb42f92017-01-19 22:27:55 -08003846 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003847 ha->tgt.tgt_ops->free_cmd(cmd);
3848}
3849
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003850/*
3851 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3852 */
Quinn Tran82de8022017-06-13 20:47:17 -07003853static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3854 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003855{
3856 struct qla_hw_data *ha = vha->hw;
3857 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003858 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07003859 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003860
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003861 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3862 /* That could happen only in case of an error/reset/abort */
3863 if (status != CTIO_SUCCESS) {
3864 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3865 "Intermediate CTIO received"
3866 " (status %x)\n", status);
3867 }
3868 return;
3869 }
3870
Quinn Tran82de8022017-06-13 20:47:17 -07003871 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003872 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003873 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003874
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003875 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003876 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003877
Joern Engelf9b67212014-09-16 16:23:18 -04003878 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003879
3880 if (unlikely(status != CTIO_SUCCESS)) {
3881 switch (status & 0xFFFF) {
3882 case CTIO_LIP_RESET:
3883 case CTIO_TARGET_RESET:
3884 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003885 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003886 case CTIO_TIMEOUT:
3887 case CTIO_INVALID_RX_ID:
3888 /* They are OK */
3889 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3890 "qla_target(%d): CTIO with "
3891 "status %#x received, state %x, se_cmd %p, "
3892 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3893 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3894 status, cmd->state, se_cmd);
3895 break;
3896
3897 case CTIO_PORT_LOGGED_OUT:
3898 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003899 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003900 int logged_out =
3901 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3902
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003903 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003904 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003905 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003906 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003907 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003908
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003909 if (logged_out && cmd->sess) {
3910 /*
3911 * Session is already logged out, but we need
3912 * to notify initiator, who's not aware of this
3913 */
3914 cmd->sess->logout_on_delete = 0;
3915 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07003916 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08003917 "%s %d %8phC post del sess\n",
3918 __func__, __LINE__, cmd->sess->port_name);
3919
Quinn Trand8630bb2017-12-28 12:33:43 -08003920 qlt_schedule_sess_for_deletion(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003921 }
3922 break;
3923 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003924 case CTIO_DIF_ERROR: {
3925 struct ctio_crc_from_fw *crc =
3926 (struct ctio_crc_from_fw *)ctio;
3927 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07003928 "qla_target(%d): CTIO with DIF_ERROR status %x "
3929 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3930 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003931 vha->vp_idx, status, cmd->state, se_cmd,
3932 *((u64 *)&crc->actual_dif[0]),
3933 *((u64 *)&crc->expected_dif[0]));
3934
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003935 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07003936 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04003937 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003938 default:
3939 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003940 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003941 vha->vp_idx, status, cmd->state, se_cmd);
3942 break;
3943 }
3944
Quinn Tran7b898542014-04-11 16:54:44 -04003945
Quinn Tran193b50b2015-12-17 14:57:03 -05003946 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003947 * cmd is already aborted/terminated, we don't
3948 * need to terminate again. The exchange is already
3949 * cleaned up/freed at FW level. Just cleanup at driver
3950 * level.
3951 */
3952 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003953 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003954 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07003955 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003956 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003957 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003958 }
3959
3960 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003961 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003962 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003963 cmd->state = QLA_TGT_STATE_DATA_IN;
3964
Bart Van Assche52c82822015-07-09 07:23:26 -07003965 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003966 cmd->write_data_transferred = 1;
3967
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003968 ha->tgt.tgt_ops->handle_data(cmd);
3969 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003970 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003971 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003972 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003973 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003974 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003975 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003976 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3977 "qla_target(%d): A command in state (%d) should "
3978 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3979 }
3980
Quinn Tran7b898542014-04-11 16:54:44 -04003981 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003982 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003983 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3984 dump_stack();
3985 }
3986
3987 ha->tgt.tgt_ops->free_cmd(cmd);
3988}
3989
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003990static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3991 uint8_t task_codes)
3992{
3993 int fcp_task_attr;
3994
3995 switch (task_codes) {
3996 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003997 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003998 break;
3999 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004000 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004001 break;
4002 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004003 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004004 break;
4005 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004006 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004007 break;
4008 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004009 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004010 break;
4011 default:
4012 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4013 "qla_target: unknown task code %x, use ORDERED instead\n",
4014 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08004015 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004016 break;
4017 }
4018
4019 return fcp_task_attr;
4020}
4021
Quinn Tran5d964832017-01-19 22:27:59 -08004022static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004023 uint8_t *);
4024/*
4025 * Process context for I/O path into tcm_qla2xxx code
4026 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004027static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004028{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004029 scsi_qla_host_t *vha = cmd->vha;
4030 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004031 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004032 struct atio_from_isp *atio = &cmd->atio;
4033 unsigned char *cdb;
4034 unsigned long flags;
4035 uint32_t data_length;
4036 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07004037 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004038
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004039 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004040 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004041
Quinn Tran193b50b2015-12-17 14:57:03 -05004042 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004043 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4044 "cmd with tag %u is aborted\n",
4045 cmd->atio.u.isp24.exchange_addr);
4046 goto out_term;
4047 }
4048
Quinn Trana07100e2015-12-07 19:48:57 -05004049 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004050 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004051 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004052
4053 if (atio->u.isp24.fcp_cmnd.rddata &&
4054 atio->u.isp24.fcp_cmnd.wrdata) {
4055 bidi = 1;
4056 data_dir = DMA_TO_DEVICE;
4057 } else if (atio->u.isp24.fcp_cmnd.rddata)
4058 data_dir = DMA_FROM_DEVICE;
4059 else if (atio->u.isp24.fcp_cmnd.wrdata)
4060 data_dir = DMA_TO_DEVICE;
4061 else
4062 data_dir = DMA_NONE;
4063
4064 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4065 atio->u.isp24.fcp_cmnd.task_attr);
4066 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4067 &atio->u.isp24.fcp_cmnd.add_cdb[
4068 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4069
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004070 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4071 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004072 if (ret != 0)
4073 goto out_term;
4074 /*
4075 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4076 */
Quinn Tran75601512015-12-17 14:57:04 -05004077 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004078 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004079 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004080 return;
4081
4082out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004083 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004084 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004085 * cmd has not sent to target yet, so pass NULL as the second
4086 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004087 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004088 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004089 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4090 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004091
4092 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004093 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Quinn Tran82de8022017-06-13 20:47:17 -07004094 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004095
4096 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004097 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004098 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004099}
4100
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004101static void qlt_do_work(struct work_struct *work)
4102{
4103 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004104 scsi_qla_host_t *vha = cmd->vha;
4105 unsigned long flags;
4106
4107 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4108 list_del(&cmd->cmd_list);
4109 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004110
4111 __qlt_do_work(cmd);
4112}
4113
Quinn Tran09620ee2017-06-13 20:47:20 -07004114void qlt_clr_qp_table(struct scsi_qla_host *vha)
4115{
4116 unsigned long flags;
4117 struct qla_hw_data *ha = vha->hw;
4118 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4119 void *node;
4120 u64 key = 0;
4121
4122 ql_log(ql_log_info, vha, 0x706c,
4123 "User update Number of Active Qpairs %d\n",
4124 ha->tgt.num_act_qpairs);
4125
4126 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4127
4128 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4129 btree_remove64(&tgt->lun_qpair_map, key);
4130
4131 ha->base_qpair->lun_cnt = 0;
4132 for (key = 0; key < ha->max_qpairs; key++)
4133 if (ha->queue_pair_map[key])
4134 ha->queue_pair_map[key]->lun_cnt = 0;
4135
4136 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4137}
4138
Quinn Trane326d222017-06-13 20:47:18 -07004139static void qlt_assign_qpair(struct scsi_qla_host *vha,
4140 struct qla_tgt_cmd *cmd)
4141{
4142 struct qla_qpair *qpair, *qp;
4143 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4144 struct qla_qpair_hint *h;
4145
4146 if (vha->flags.qpairs_available) {
4147 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4148 if (unlikely(!h)) {
4149 /* spread lun to qpair ratio evently */
4150 int lcnt = 0, rc;
4151 struct scsi_qla_host *base_vha =
4152 pci_get_drvdata(vha->hw->pdev);
4153
4154 qpair = vha->hw->base_qpair;
4155 if (qpair->lun_cnt == 0) {
4156 qpair->lun_cnt++;
4157 h = qla_qpair_to_hint(tgt, qpair);
4158 BUG_ON(!h);
4159 rc = btree_insert64(&tgt->lun_qpair_map,
4160 cmd->unpacked_lun, h, GFP_ATOMIC);
4161 if (rc) {
4162 qpair->lun_cnt--;
4163 ql_log(ql_log_info, vha, 0xd037,
4164 "Unable to insert lun %llx into lun_qpair_map\n",
4165 cmd->unpacked_lun);
4166 }
4167 goto out;
4168 } else {
4169 lcnt = qpair->lun_cnt;
4170 }
4171
4172 h = NULL;
4173 list_for_each_entry(qp, &base_vha->qp_list,
4174 qp_list_elem) {
4175 if (qp->lun_cnt == 0) {
4176 qp->lun_cnt++;
4177 h = qla_qpair_to_hint(tgt, qp);
4178 BUG_ON(!h);
4179 rc = btree_insert64(&tgt->lun_qpair_map,
4180 cmd->unpacked_lun, h, GFP_ATOMIC);
4181 if (rc) {
4182 qp->lun_cnt--;
4183 ql_log(ql_log_info, vha, 0xd038,
4184 "Unable to insert lun %llx into lun_qpair_map\n",
4185 cmd->unpacked_lun);
4186 }
4187 qpair = qp;
4188 goto out;
4189 } else {
4190 if (qp->lun_cnt < lcnt) {
4191 lcnt = qp->lun_cnt;
4192 qpair = qp;
4193 continue;
4194 }
4195 }
4196 }
4197 BUG_ON(!qpair);
4198 qpair->lun_cnt++;
4199 h = qla_qpair_to_hint(tgt, qpair);
4200 BUG_ON(!h);
4201 rc = btree_insert64(&tgt->lun_qpair_map,
4202 cmd->unpacked_lun, h, GFP_ATOMIC);
4203 if (rc) {
4204 qpair->lun_cnt--;
4205 ql_log(ql_log_info, vha, 0xd039,
4206 "Unable to insert lun %llx into lun_qpair_map\n",
4207 cmd->unpacked_lun);
4208 }
4209 }
4210 } else {
4211 h = &tgt->qphints[0];
4212 }
4213out:
4214 cmd->qpair = h->qpair;
4215 cmd->se_cmd.cpuid = h->cpuid;
4216}
4217
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004218static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004219 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004220 struct atio_from_isp *atio)
4221{
4222 struct se_session *se_sess = sess->se_sess;
4223 struct qla_tgt_cmd *cmd;
4224 int tag;
4225
4226 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4227 if (tag < 0)
4228 return NULL;
4229
4230 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4231 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004232 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004233 memcpy(&cmd->atio, atio, sizeof(*atio));
4234 cmd->state = QLA_TGT_STATE_NEW;
4235 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004236 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004237 cmd->vha = vha;
4238 cmd->se_cmd.map_tag = tag;
4239 cmd->sess = sess;
4240 cmd->loop_id = sess->loop_id;
4241 cmd->conf_compl_supported = sess->conf_compl_supported;
4242
Quinn Tran1eb42f92017-01-19 22:27:55 -08004243 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004244 cmd->jiffies_at_alloc = get_jiffies_64();
4245
Quinn Trane326d222017-06-13 20:47:18 -07004246 cmd->unpacked_lun = scsilun_to_int(
4247 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4248 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004249 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004250 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004251
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004252 return cmd;
4253}
4254
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004255/* ha->hardware_lock supposed to be held on entry */
4256static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4257 struct atio_from_isp *atio)
4258{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004259 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004260 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004261 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004262 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004263 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08004264 port_id_t id;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004265
4266 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004267 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004268 "New command while device %p is shutting down\n", tgt);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004269 return -ENODEV;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004270 }
4271
Quinn Trana4239942017-12-28 12:33:26 -08004272 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4273 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4274 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4275 if (IS_SW_RESV_ADDR(id))
4276 return -EBUSY;
4277
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004278 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 -08004279 if (unlikely(!sess))
4280 return -EFAULT;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004281
4282 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4283 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004284 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004285 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004286 "New command while old session %p is being deleted\n",
4287 sess);
4288 return -EFAULT;
4289 }
4290
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004291 /*
4292 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4293 */
Quinn Tran726b8542017-01-19 22:28:00 -08004294 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004295 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004296 "%s: kref_get fail, %8phC oxid %x \n",
4297 __func__, sess->port_name,
4298 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4299 return -EFAULT;
4300 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004301
4302 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004303 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004304 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004305 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004306 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4307 ha->tgt.tgt_ops->put_sess(sess);
4308 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08004309 return -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004310 }
4311
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004312 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004313 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004314
Quinn Tran726b8542017-01-19 22:28:00 -08004315 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004316 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004317 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004318
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004319 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004320 if (vha->flags.qpairs_available) {
4321 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4322 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004323 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4324 queue_work_on(smp_processor_id(), qla_tgt_wq,
4325 &cmd->work);
4326 else
4327 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4328 &cmd->work);
4329 } else {
4330 queue_work(qla_tgt_wq, &cmd->work);
4331 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004332
Quinn Tran82de8022017-06-13 20:47:17 -07004333 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004334}
4335
4336/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004337static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004338 int fn, void *iocb, int flags)
4339{
4340 struct scsi_qla_host *vha = sess->vha;
4341 struct qla_hw_data *ha = vha->hw;
4342 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004343 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004344 int res;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004345
4346 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4347 if (!mcmd) {
4348 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4349 "qla_target(%d): Allocation of management "
4350 "command failed, some commands and their data could "
4351 "leak\n", vha->vp_idx);
4352 return -ENOMEM;
4353 }
4354 memset(mcmd, 0, sizeof(*mcmd));
4355 mcmd->sess = sess;
4356
4357 if (iocb) {
4358 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4359 sizeof(mcmd->orig_iocb.imm_ntfy));
4360 }
4361 mcmd->tmr_func = fn;
4362 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004363 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07004364 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004365 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004366
4367 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004368 case QLA_TGT_LUN_RESET:
Quinn Tranbe92fc32017-01-19 22:27:54 -08004369 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4370 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004371 }
4372
Quinn Tranbe92fc32017-01-19 22:27:54 -08004373 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004374 if (res != 0) {
4375 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4376 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4377 sess->vha->vp_idx, res);
4378 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4379 return -EFAULT;
4380 }
4381
4382 return 0;
4383}
4384
4385/* ha->hardware_lock supposed to be held on entry */
4386static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4387{
4388 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4389 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004390 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004391 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004392 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004393 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004394
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004395 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004396
4397 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004398 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4399 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004400 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4401
Quinn Tranf775bd12017-06-02 09:11:59 -07004402 unpacked_lun =
4403 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004404
Quinn Tranfb352652017-12-28 12:33:32 -08004405 if (sess == NULL || sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004406 return -EFAULT;
4407
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004408 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4409}
4410
4411/* ha->hardware_lock supposed to be held on entry */
4412static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004413 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004414{
4415 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4416 struct qla_hw_data *ha = vha->hw;
4417 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004418 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004419 int rc;
4420
4421 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4422 if (mcmd == NULL) {
4423 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4424 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4425 vha->vp_idx, __func__);
4426 return -ENOMEM;
4427 }
4428 memset(mcmd, 0, sizeof(*mcmd));
4429
4430 mcmd->sess = sess;
4431 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4432 sizeof(mcmd->orig_iocb.imm_ntfy));
4433
Quinn Tranf775bd12017-06-02 09:11:59 -07004434 unpacked_lun =
4435 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004436 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004437 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004438 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004439
Quinn Tranbe92fc32017-01-19 22:27:54 -08004440 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004441 le16_to_cpu(iocb->u.isp2x.seq_id));
4442 if (rc != 0) {
4443 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4444 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4445 vha->vp_idx, rc);
4446 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4447 return -EFAULT;
4448 }
4449
4450 return 0;
4451}
4452
4453/* ha->hardware_lock supposed to be held on entry */
4454static int qlt_abort_task(struct scsi_qla_host *vha,
4455 struct imm_ntfy_from_isp *iocb)
4456{
4457 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004458 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004459 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004460 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004461
4462 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4463
Quinn Tran75601512015-12-17 14:57:04 -05004464 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004465 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004466 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4467
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004468 if (sess == NULL) {
4469 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4470 "qla_target(%d): task abort for unexisting "
4471 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004472 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004473 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4474 }
4475
4476 return __qlt_abort_task(vha, iocb, sess);
4477}
4478
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004479void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4480{
Quinn Tran726b8542017-01-19 22:28:00 -08004481 if (rc != MBS_COMMAND_COMPLETE) {
4482 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4483 "%s: se_sess %p / sess %p from"
4484 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4485 " LOGO failed: %#x\n",
4486 __func__,
4487 fcport->se_sess,
4488 fcport,
4489 fcport->port_name, fcport->loop_id,
4490 fcport->d_id.b.domain, fcport->d_id.b.area,
4491 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004492 }
Quinn Tran726b8542017-01-19 22:28:00 -08004493
4494 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004495}
4496
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004497/*
4498* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4499*
4500* Schedules sessions with matching port_id/loop_id but different wwn for
4501* deletion. Returns existing session with matching wwn if present.
4502* Null otherwise.
4503*/
Quinn Tran726b8542017-01-19 22:28:00 -08004504struct fc_port *
4505qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004506 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004507{
Quinn Tran5d964832017-01-19 22:27:59 -08004508 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004509 uint64_t other_wwn;
4510
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004511 *conflict_sess = NULL;
4512
Quinn Tran5d964832017-01-19 22:27:59 -08004513 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004514
4515 other_wwn = wwn_to_u64(other_sess->port_name);
4516
4517 if (wwn == other_wwn) {
4518 WARN_ON(sess);
4519 sess = other_sess;
4520 continue;
4521 }
4522
4523 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004524 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004525 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004526 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004527 "Invalidating sess %p loop_id %d wwn %llx.\n",
4528 other_sess, other_sess->loop_id, other_wwn);
4529
4530 /*
4531 * logout_on_delete is set by default, but another
4532 * session that has the same s_id/loop_id combo
4533 * might have cleared it when requested this session
4534 * deletion, so don't touch it
4535 */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004536 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004537 } else {
4538 /*
4539 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004540 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004541 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004542 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004543 "Invalidating sess %p loop_id %d wwn %llx.\n",
4544 other_sess, other_sess->loop_id, other_wwn);
4545
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004546 other_sess->keep_nport_handle = 1;
Quinn Tranba743f92017-12-04 14:45:12 -08004547 if (other_sess->disc_state != DSC_DELETED)
4548 *conflict_sess = other_sess;
Quinn Tran94cff6e2017-12-28 12:33:42 -08004549 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004550 }
4551 continue;
4552 }
4553
4554 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004555 if ((loop_id == other_sess->loop_id) &&
4556 (loop_id != FC_NO_LOOP_ID)) {
4557 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004558 "Invalidating sess %p loop_id %d wwn %llx.\n",
4559 other_sess, other_sess->loop_id, other_wwn);
4560
4561 /* Same loop_id but different s_id
4562 * Ok to kill and logout */
Quinn Tran94cff6e2017-12-28 12:33:42 -08004563 qlt_schedule_sess_for_deletion(other_sess);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004564 }
4565 }
4566
4567 return sess;
4568}
4569
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004570/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4571static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4572{
4573 struct qla_tgt_sess_op *op;
4574 struct qla_tgt_cmd *cmd;
4575 uint32_t key;
4576 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004577 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004578
4579 key = (((u32)s_id->b.domain << 16) |
4580 ((u32)s_id->b.area << 8) |
4581 ((u32)s_id->b.al_pa));
4582
Quinn Tran8b631d82017-06-02 09:11:54 -07004583 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004584 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4585 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004586
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004587 if (op_key == key) {
4588 op->aborted = true;
4589 count++;
4590 }
4591 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004592
4593 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4594 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4595 if (op_key == key) {
4596 op->aborted = true;
4597 count++;
4598 }
4599 }
4600
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004601 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4602 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4603 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004604 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004605 count++;
4606 }
4607 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004608 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004609
4610 return count;
4611}
4612
Quinn Tran9cd883f2017-12-28 12:33:24 -08004613static int qlt_handle_login(struct scsi_qla_host *vha,
4614 struct imm_ntfy_from_isp *iocb)
4615{
4616 struct fc_port *sess = NULL, *conflict_sess = NULL;
4617 uint64_t wwn;
4618 port_id_t port_id;
4619 uint16_t loop_id, wd3_lo;
4620 int res = 0;
4621 struct qlt_plogi_ack_t *pla;
4622 unsigned long flags;
4623
4624 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4625
4626 port_id.b.domain = iocb->u.isp24.port_id[2];
4627 port_id.b.area = iocb->u.isp24.port_id[1];
4628 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4629 port_id.b.rsvd_1 = 0;
4630
4631 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4632
4633 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4634 abort_cmds_for_s_id(vha, &port_id);
4635
4636 if (wwn) {
4637 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4638 sess = qlt_find_sess_invalidate_other(vha, wwn,
4639 port_id, loop_id, &conflict_sess);
4640 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4641 }
4642
4643 if (IS_SW_RESV_ADDR(port_id)) {
4644 res = 1;
4645 goto out;
4646 }
4647
4648 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4649 if (!pla) {
4650 qlt_send_term_imm_notif(vha, iocb, 1);
4651 goto out;
4652 }
4653
4654 if (conflict_sess) {
4655 conflict_sess->login_gen++;
4656 qlt_plogi_ack_link(vha, pla, conflict_sess,
4657 QLT_PLOGI_LINK_CONFLICT);
4658 }
4659
4660 if (!sess) {
4661 pla->ref_count++;
4662 ql_dbg(ql_dbg_disc, vha, 0xffff,
4663 "%s %d %8phC post new sess\n",
4664 __func__, __LINE__, iocb->u.isp24.port_name);
Quinn Trana4239942017-12-28 12:33:26 -08004665 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4666 qla24xx_post_newsess_work(vha, &port_id,
4667 iocb->u.isp24.port_name,
4668 iocb->u.isp24.u.plogi.node_name,
4669 pla, FC4_TYPE_UNKNOWN);
4670 else
4671 qla24xx_post_newsess_work(vha, &port_id,
4672 iocb->u.isp24.port_name, NULL,
4673 pla, FC4_TYPE_UNKNOWN);
4674
Quinn Tran9cd883f2017-12-28 12:33:24 -08004675 goto out;
4676 }
4677
4678 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4679 sess->d_id = port_id;
4680 sess->login_gen++;
4681
4682 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4683 sess->fw_login_state = DSC_LS_PRLI_PEND;
4684 sess->local = 0;
4685 sess->loop_id = loop_id;
4686 sess->d_id = port_id;
4687 sess->fw_login_state = DSC_LS_PRLI_PEND;
4688 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4689
4690 if (wd3_lo & BIT_7)
4691 sess->conf_compl_supported = 1;
4692
4693 if ((wd3_lo & BIT_4) == 0)
4694 sess->port_type = FCT_INITIATOR;
4695 else
4696 sess->port_type = FCT_TARGET;
4697
4698 } else
4699 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4700
4701
4702 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4703 "%s %d %8phC DS %d\n",
4704 __func__, __LINE__, sess->port_name, sess->disc_state);
4705
4706 switch (sess->disc_state) {
4707 case DSC_DELETED:
4708 qlt_plogi_ack_unref(vha, pla);
4709 break;
4710
4711 default:
4712 /*
4713 * Under normal circumstances we want to release nport handle
4714 * during LOGO process to avoid nport handle leaks inside FW.
4715 * The exception is when LOGO is done while another PLOGI with
4716 * the same nport handle is waiting as might be the case here.
4717 * Note: there is always a possibily of a race where session
4718 * deletion has already started for other reasons (e.g. ACL
4719 * removal) and now PLOGI arrives:
4720 * 1. if PLOGI arrived in FW after nport handle has been freed,
4721 * FW must have assigned this PLOGI a new/same handle and we
4722 * can proceed ACK'ing it as usual when session deletion
4723 * completes.
4724 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4725 * bit reached it, the handle has now been released. We'll
4726 * get an error when we ACK this PLOGI. Nothing will be sent
4727 * back to initiator. Initiator should eventually retry
4728 * PLOGI and situation will correct itself.
4729 */
4730 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4731 (sess->d_id.b24 == port_id.b24));
4732
4733 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4734 "%s %d %8phC post del sess\n",
4735 __func__, __LINE__, sess->port_name);
4736
4737
Quinn Trand8630bb2017-12-28 12:33:43 -08004738 qlt_schedule_sess_for_deletion(sess);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004739 break;
4740 }
4741out:
4742 return res;
4743}
4744
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004745/*
4746 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4747 */
4748static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4749 struct imm_ntfy_from_isp *iocb)
4750{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004751 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004752 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004753 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004754 uint64_t wwn;
4755 port_id_t port_id;
4756 uint16_t loop_id;
4757 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004758 int res = 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004759 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004760
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004761 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4762
4763 port_id.b.domain = iocb->u.isp24.port_id[2];
4764 port_id.b.area = iocb->u.isp24.port_id[1];
4765 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4766 port_id.b.rsvd_1 = 0;
4767
4768 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4769
Quinn Tran726b8542017-01-19 22:28:00 -08004770 ql_dbg(ql_dbg_disc, vha, 0xf026,
4771 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4772 vha->vp_idx, iocb->u.isp24.port_id[2],
4773 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4774 iocb->u.isp24.status_subcode, loop_id,
4775 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004776
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004777 /* res = 1 means ack at the end of thread
4778 * res = 0 means ack async/later.
4779 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004780 switch (iocb->u.isp24.status_subcode) {
4781 case ELS_PLOGI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004782 res = qlt_handle_login(vha, iocb);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004783 break;
4784
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004785 case ELS_PRLI:
Quinn Tran9cd883f2017-12-28 12:33:24 -08004786 if (N2N_TOPO(ha)) {
4787 sess = qla2x00_find_fcport_by_wwpn(vha,
4788 iocb->u.isp24.port_name, 1);
4789
4790 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4791 ql_dbg(ql_dbg_disc, vha, 0xffff,
4792 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4793 __func__, __LINE__,
4794 iocb->u.isp24.port_name);
4795 qlt_send_term_imm_notif(vha, iocb, 1);
4796 break;
4797 }
4798
4799 res = qlt_handle_login(vha, iocb);
4800 break;
4801 }
4802
Quinn Trana4239942017-12-28 12:33:26 -08004803 if (IS_SW_RESV_ADDR(port_id)) {
4804 res = 1;
4805 break;
4806 }
4807
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004808 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4809
Quinn Tran75601512015-12-17 14:57:04 -05004810 if (wwn) {
4811 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004812 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4813 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004814 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4815 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004816
4817 if (conflict_sess) {
Quinn Tran5ef696a2017-12-04 14:45:05 -08004818 switch (conflict_sess->disc_state) {
4819 case DSC_DELETED:
4820 case DSC_DELETE_PEND:
4821 break;
4822 default:
4823 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4824 "PRLI with conflicting sess %p port %8phC\n",
4825 conflict_sess, conflict_sess->port_name);
4826 conflict_sess->fw_login_state =
4827 DSC_LS_PORT_UNAVAIL;
4828 qlt_send_term_imm_notif(vha, iocb, 1);
4829 res = 0;
4830 break;
4831 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004832 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004833
4834 if (sess != NULL) {
Quinn Trana4239942017-12-28 12:33:26 -08004835 bool delete = false;
Quinn Tran82abdca2017-12-28 12:33:22 -08004836 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4837 switch (sess->fw_login_state) {
Quinn Trana4239942017-12-28 12:33:26 -08004838 case DSC_LS_PLOGI_PEND:
Quinn Tran82abdca2017-12-28 12:33:22 -08004839 case DSC_LS_PLOGI_COMP:
4840 case DSC_LS_PRLI_COMP:
4841 break;
4842 default:
Quinn Trana4239942017-12-28 12:33:26 -08004843 delete = true;
4844 break;
4845 }
4846
4847 switch (sess->disc_state) {
4848 case DSC_LOGIN_PEND:
4849 case DSC_GPDB:
4850 case DSC_GPSC:
4851 case DSC_UPD_FCPORT:
4852 case DSC_LOGIN_COMPLETE:
4853 case DSC_ADISC:
4854 delete = false;
4855 break;
4856 default:
4857 break;
4858 }
4859
4860 if (delete) {
Quinn Tran82abdca2017-12-28 12:33:22 -08004861 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4862 flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004863 /*
4864 * Impatient initiator sent PRLI before last
4865 * PLOGI could finish. Will force him to re-try,
4866 * while last one finishes.
4867 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004868 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004869 "sess %p PRLI received, before plogi ack.\n",
4870 sess);
4871 qlt_send_term_imm_notif(vha, iocb, 1);
4872 res = 0;
Quinn Tran82abdca2017-12-28 12:33:22 -08004873 spin_lock_irqsave(&tgt->ha->tgt.sess_lock,
4874 flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004875 break;
4876 }
4877
4878 /*
4879 * This shouldn't happen under normal circumstances,
4880 * since we have deleted the old session during PLOGI
4881 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004882 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004883 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4884 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4885
4886 sess->local = 0;
4887 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08004888 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004889 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004890
4891 if (wd3_lo & BIT_7)
4892 sess->conf_compl_supported = 1;
4893
Quinn Tran726b8542017-01-19 22:28:00 -08004894 if ((wd3_lo & BIT_4) == 0)
4895 sess->port_type = FCT_INITIATOR;
4896 else
4897 sess->port_type = FCT_TARGET;
Quinn Tran82abdca2017-12-28 12:33:22 -08004898
4899 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04004900 }
4901 res = 1; /* send notify ack */
4902
4903 /* Make session global (not used in fabric mode) */
4904 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004905 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004906 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07004907 "%s %d %8phC post nack\n",
4908 __func__, __LINE__, sess->port_name);
4909 qla24xx_post_nack_work(vha, sess, iocb,
4910 SRB_NACK_PRLI);
4911 res = 0;
4912 } else {
4913 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4914 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4915 qla2xxx_wake_dpc(vha);
4916 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004917 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08004918 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004919 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07004920 "%s %d %8phC post nack\n",
4921 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004922 qla24xx_post_nack_work(vha, sess, iocb,
4923 SRB_NACK_PRLI);
4924 res = 0;
4925 }
4926 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004927 break;
4928
Quinn Tran41dc5292017-01-19 22:28:03 -08004929 case ELS_TPRLO:
4930 if (le16_to_cpu(iocb->u.isp24.flags) &
4931 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4932 loop_id = 0xFFFF;
4933 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4934 res = 1;
4935 break;
4936 }
Bart Van Assche81881862017-12-07 16:02:46 -08004937 /* fall through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004938 case ELS_LOGO:
4939 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08004940 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4941 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4942 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4943
4944 if (sess) {
4945 sess->login_gen++;
4946 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08004947 sess->logo_ack_needed = 1;
4948 memcpy(sess->iocb, iocb, IOCB_SIZE);
4949 }
4950
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004951 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08004952
Quinn Tran83548fe2017-06-02 09:12:01 -07004953 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08004954 "%s: logo %llx res %d sess %p ",
4955 __func__, wwn, res, sess);
4956 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08004957 /*
4958 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4959 * for LOGO_ACK & sess delete
4960 */
Quinn Tran726b8542017-01-19 22:28:00 -08004961 BUG_ON(!sess);
4962 res = 0;
4963 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08004964 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08004965 if (sess) {
Quinn Trand8630bb2017-12-28 12:33:43 -08004966 qlt_schedule_sess_for_deletion(sess);
Quinn Tran726b8542017-01-19 22:28:00 -08004967 res = 0;
4968 }
4969 /* else logo will be ack */
4970 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004971 break;
4972 case ELS_PDISC:
4973 case ELS_ADISC:
4974 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004975 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004976 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07004977 qlt_send_notify_ack(ha->base_qpair,
4978 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004979 tgt->link_reinit_iocb_pending = 0;
4980 }
Quinn Tran726b8542017-01-19 22:28:00 -08004981
4982 sess = qla2x00_find_fcport_by_wwpn(vha,
4983 iocb->u.isp24.port_name, 1);
4984 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004985 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08004986 "sess %p lid %d|%d DS %d LS %d\n",
4987 sess, sess->loop_id, loop_id,
4988 sess->disc_state, sess->fw_login_state);
4989 }
4990
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004991 res = 1; /* send notify ack */
4992 break;
4993 }
4994
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004995 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004996 default:
4997 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4998 "qla_target(%d): Unsupported ELS command %x "
4999 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5000 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5001 break;
5002 }
5003
Quinn Tran9cd883f2017-12-28 12:33:24 -08005004 ql_dbg(ql_dbg_disc, vha, 0xf026,
5005 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5006 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5007
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005008 return res;
5009}
5010
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005011/*
5012 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5013 */
5014static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5015 struct imm_ntfy_from_isp *iocb)
5016{
5017 struct qla_hw_data *ha = vha->hw;
5018 uint32_t add_flags = 0;
5019 int send_notify_ack = 1;
5020 uint16_t status;
5021
5022 status = le16_to_cpu(iocb->u.isp2x.status);
5023 switch (status) {
5024 case IMM_NTFY_LIP_RESET:
5025 {
5026 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5027 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5028 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5029 iocb->u.isp24.status_subcode);
5030
5031 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5032 send_notify_ack = 0;
5033 break;
5034 }
5035
5036 case IMM_NTFY_LIP_LINK_REINIT:
5037 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005038 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005039 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5040 "qla_target(%d): LINK REINIT (loop %#x, "
5041 "subcode %x)\n", vha->vp_idx,
5042 le16_to_cpu(iocb->u.isp24.nport_handle),
5043 iocb->u.isp24.status_subcode);
5044 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005045 qlt_send_notify_ack(ha->base_qpair,
5046 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005047 }
5048 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5049 tgt->link_reinit_iocb_pending = 1;
5050 /*
5051 * QLogic requires to wait after LINK REINIT for possible
5052 * PDISC or ADISC ELS commands
5053 */
5054 send_notify_ack = 0;
5055 break;
5056 }
5057
5058 case IMM_NTFY_PORT_LOGOUT:
5059 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5060 "qla_target(%d): Port logout (loop "
5061 "%#x, subcode %x)\n", vha->vp_idx,
5062 le16_to_cpu(iocb->u.isp24.nport_handle),
5063 iocb->u.isp24.status_subcode);
5064
5065 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5066 send_notify_ack = 0;
5067 /* The sessions will be cleared in the callback, if needed */
5068 break;
5069
5070 case IMM_NTFY_GLBL_TPRLO:
5071 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5072 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5073 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5074 send_notify_ack = 0;
5075 /* The sessions will be cleared in the callback, if needed */
5076 break;
5077
5078 case IMM_NTFY_PORT_CONFIG:
5079 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5080 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5081 status);
5082 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5083 send_notify_ack = 0;
5084 /* The sessions will be cleared in the callback, if needed */
5085 break;
5086
5087 case IMM_NTFY_GLBL_LOGO:
5088 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5089 "qla_target(%d): Link failure detected\n",
5090 vha->vp_idx);
5091 /* I_T nexus loss */
5092 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5093 send_notify_ack = 0;
5094 break;
5095
5096 case IMM_NTFY_IOCB_OVERFLOW:
5097 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5098 "qla_target(%d): Cannot provide requested "
5099 "capability (IOCB overflowed the immediate notify "
5100 "resource count)\n", vha->vp_idx);
5101 break;
5102
5103 case IMM_NTFY_ABORT_TASK:
5104 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5105 "qla_target(%d): Abort Task (S %08x I %#x -> "
5106 "L %#x)\n", vha->vp_idx,
5107 le16_to_cpu(iocb->u.isp2x.seq_id),
5108 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5109 le16_to_cpu(iocb->u.isp2x.lun));
5110 if (qlt_abort_task(vha, iocb) == 0)
5111 send_notify_ack = 0;
5112 break;
5113
5114 case IMM_NTFY_RESOURCE:
5115 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5116 "qla_target(%d): Out of resources, host %ld\n",
5117 vha->vp_idx, vha->host_no);
5118 break;
5119
5120 case IMM_NTFY_MSG_RX:
5121 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5122 "qla_target(%d): Immediate notify task %x\n",
5123 vha->vp_idx, iocb->u.isp2x.task_flags);
5124 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5125 send_notify_ack = 0;
5126 break;
5127
5128 case IMM_NTFY_ELS:
5129 if (qlt_24xx_handle_els(vha, iocb) == 0)
5130 send_notify_ack = 0;
5131 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005132 default:
5133 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5134 "qla_target(%d): Received unknown immediate "
5135 "notify status %x\n", vha->vp_idx, status);
5136 break;
5137 }
5138
5139 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005140 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5141 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005142}
5143
5144/*
5145 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5146 * This function sends busy to ISP 2xxx or 24xx.
5147 */
Quinn Tran82de8022017-06-13 20:47:17 -07005148static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005149 struct atio_from_isp *atio, uint16_t status)
5150{
Quinn Tran82de8022017-06-13 20:47:17 -07005151 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005152 struct ctio7_to_24xx *ctio24;
5153 struct qla_hw_data *ha = vha->hw;
5154 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005155 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005156 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005157 u16 temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005158
Quinn Tran75601512015-12-17 14:57:04 -05005159 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005160 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5161 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05005162 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005163 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005164 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005165 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005166 }
5167 /* Sending marker isn't necessary, since we called from ISR */
5168
Quinn Tran82de8022017-06-13 20:47:17 -07005169 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005170 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005171 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005172 "qla_target(%d): %s failed: unable to allocate "
5173 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005174 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005175 }
5176
Quinn Tran60a9ead2017-06-13 20:47:28 -07005177 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005178 pkt->entry_count = 1;
5179 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5180
5181 ctio24 = (struct ctio7_to_24xx *)pkt;
5182 ctio24->entry_type = CTIO_TYPE7;
5183 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005184 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005185 ctio24->vp_index = vha->vp_idx;
5186 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5187 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5188 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5189 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005190 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005191 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005192 CTIO7_FLAGS_DONT_RET_CTIO;
5193 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005194 /*
5195 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5196 * if the explicit conformation is used.
5197 */
5198 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5199 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005200 /* Memory Barrier */
5201 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005202 if (qpair->reqq_start_iocbs)
5203 qpair->reqq_start_iocbs(qpair);
5204 else
5205 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005206 return 0;
5207}
5208
5209/*
5210 * This routine is used to allocate a command for either a QFull condition
5211 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5212 * out previously.
5213 */
5214static void
5215qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5216 struct atio_from_isp *atio, uint16_t status, int qfull)
5217{
5218 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5219 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005220 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005221 struct se_session *se_sess;
5222 struct qla_tgt_cmd *cmd;
5223 int tag;
Quinn Tran82de8022017-06-13 20:47:17 -07005224 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005225
5226 if (unlikely(tgt->tgt_stop)) {
5227 ql_dbg(ql_dbg_io, vha, 0x300a,
5228 "New command while device %p is shutting down\n", tgt);
5229 return;
5230 }
5231
5232 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5233 vha->hw->tgt.num_qfull_cmds_dropped++;
5234 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005235 vha->qla_stats.stat_max_qfull_cmds_dropped)
5236 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005237 vha->hw->tgt.num_qfull_cmds_dropped;
5238
5239 ql_dbg(ql_dbg_io, vha, 0x3068,
5240 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5241 vha->vp_idx, __func__,
5242 vha->hw->tgt.num_qfull_cmds_dropped);
5243
5244 qlt_chk_exch_leak_thresh_hold(vha);
5245 return;
5246 }
5247
5248 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5249 (vha, atio->u.isp24.fcp_hdr.s_id);
5250 if (!sess)
5251 return;
5252
5253 se_sess = sess->se_sess;
5254
5255 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5256 if (tag < 0)
5257 return;
5258
5259 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5260 if (!cmd) {
5261 ql_dbg(ql_dbg_io, vha, 0x3009,
5262 "qla_target(%d): %s: Allocation of cmd failed\n",
5263 vha->vp_idx, __func__);
5264
5265 vha->hw->tgt.num_qfull_cmds_dropped++;
5266 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005267 vha->qla_stats.stat_max_qfull_cmds_dropped)
5268 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005269 vha->hw->tgt.num_qfull_cmds_dropped;
5270
5271 qlt_chk_exch_leak_thresh_hold(vha);
5272 return;
5273 }
5274
5275 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5276
5277 qlt_incr_num_pend_cmds(vha);
5278 INIT_LIST_HEAD(&cmd->cmd_list);
5279 memcpy(&cmd->atio, atio, sizeof(*atio));
5280
5281 cmd->tgt = vha->vha_tgt.qla_tgt;
5282 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005283 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005284 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005285 cmd->qpair = ha->base_qpair;
Quinn Tran33e79972014-09-25 06:14:55 -04005286
5287 if (qfull) {
5288 cmd->q_full = 1;
5289 /* NOTE: borrowing the state field to carry the status */
5290 cmd->state = status;
5291 } else
5292 cmd->term_exchg = 1;
5293
Quinn Tran82de8022017-06-13 20:47:17 -07005294 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005295 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5296
5297 vha->hw->tgt.num_qfull_cmds_alloc++;
5298 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005299 vha->qla_stats.stat_max_qfull_cmds_alloc)
5300 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005301 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005302 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005303}
5304
5305int
Quinn Tran82de8022017-06-13 20:47:17 -07005306qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005307{
Quinn Tran82de8022017-06-13 20:47:17 -07005308 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005309 struct qla_hw_data *ha = vha->hw;
5310 unsigned long flags;
5311 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005312 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005313 int rc = 0;
5314
5315 if (list_empty(&ha->tgt.q_full_list))
5316 return 0;
5317
5318 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005319 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005320
Quinn Tran82de8022017-06-13 20:47:17 -07005321 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005322 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005323 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005324 return 0;
5325 }
5326
Quinn Tran82de8022017-06-13 20:47:17 -07005327 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5328 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5329
5330 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5331 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005332 if (cmd->q_full)
5333 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005334 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005335 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005336 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005337
5338 if (rc == -ENOMEM)
5339 break;
5340
5341 if (cmd->q_full)
5342 ql_dbg(ql_dbg_io, vha, 0x3006,
5343 "%s: busy sent for ox_id[%04x]\n", __func__,
5344 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5345 else if (cmd->term_exchg)
5346 ql_dbg(ql_dbg_io, vha, 0x3007,
5347 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5348 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5349 else
5350 ql_dbg(ql_dbg_io, vha, 0x3008,
5351 "%s: Unexpected cmd in QFull list %p\n", __func__,
5352 cmd);
5353
5354 list_del(&cmd->cmd_list);
5355 list_add_tail(&cmd->cmd_list, &free_list);
5356
5357 /* piggy back on hardware_lock for protection */
5358 vha->hw->tgt.num_qfull_cmds_alloc--;
5359 }
Quinn Tran82de8022017-06-13 20:47:17 -07005360 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005361
5362 cmd = NULL;
5363
5364 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5365 list_del(&cmd->cmd_list);
5366 /* This cmd was never sent to TCM. There is no need
5367 * to schedule free or call free_cmd
5368 */
5369 qlt_free_cmd(cmd);
5370 }
Quinn Tran82de8022017-06-13 20:47:17 -07005371
5372 if (!list_empty(&q_full_list)) {
5373 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5374 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5375 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5376 }
5377
Quinn Tran33e79972014-09-25 06:14:55 -04005378 return rc;
5379}
5380
5381static void
Quinn Tran82de8022017-06-13 20:47:17 -07005382qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5383 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005384{
5385 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005386 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005387
Quinn Tran82de8022017-06-13 20:47:17 -07005388 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005389 if (rc == -ENOMEM)
5390 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5391}
5392
5393static int
Quinn Tran82de8022017-06-13 20:47:17 -07005394qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5395 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005396{
5397 struct qla_hw_data *ha = vha->hw;
Quinn Tran8b666802017-03-15 09:48:45 -07005398 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005399
5400 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5401 return 0;
5402
Quinn Tran8b666802017-03-15 09:48:45 -07005403 if (!ha_locked)
5404 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005405 qlt_send_busy(qpair, atio, qla_sam_status);
Quinn Tran8b666802017-03-15 09:48:45 -07005406 if (!ha_locked)
5407 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5408
Quinn Tran33e79972014-09-25 06:14:55 -04005409 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005410}
5411
5412/* ha->hardware_lock supposed to be held on entry */
5413/* called via callback from qla2xxx */
5414static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005415 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005416{
5417 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005418 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005419 int rc;
Quinn Tran7cf95f72017-12-28 12:33:28 -08005420 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005421
5422 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005423 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005424 "ATIO pkt, but no tgt (ha %p)", ha);
5425 return;
5426 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005427 /*
5428 * In tgt_stop mode we also should allow all requests to pass.
5429 * Otherwise, some commands can stuck.
5430 */
5431
Quinn Tran2f424b92015-12-17 14:57:07 -05005432 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005433
5434 switch (atio->u.raw.entry_type) {
5435 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005436 if (unlikely(atio->u.isp24.exchange_addr ==
5437 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005438 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005439 "qla_target(%d): ATIO_TYPE7 "
5440 "received with UNKNOWN exchange address, "
5441 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005442 if (!ha_locked)
5443 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005444 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
Quinn Tran2f424b92015-12-17 14:57:07 -05005445 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005446 spin_unlock_irqrestore(&ha->hardware_lock,
5447 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005448 break;
5449 }
Quinn Tran33e79972014-09-25 06:14:55 -04005450
Quinn Tran33e79972014-09-25 06:14:55 -04005451 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005452 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5453 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005454 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005455 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005456 return;
5457 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005458 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005459 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005460 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005461 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005462 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005463 if (!ha_locked)
5464 spin_lock_irqsave(&ha->hardware_lock, flags);
5465 switch (rc) {
5466 case -ENODEV:
5467 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5468 "qla_target: Unable to send command to target\n");
5469 break;
5470 case -EBADF:
5471 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5472 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005473 qlt_send_term_exchange(ha->base_qpair, NULL,
5474 atio, 1, 0);
Quinn Tran7cf95f72017-12-28 12:33:28 -08005475 break;
5476 case -EBUSY:
5477 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5478 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5479 vha->vp_idx);
5480 qlt_send_busy(ha->base_qpair, atio,
5481 tc_sam_status);
5482 break;
5483 default:
5484 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5485 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5486 vha->vp_idx);
5487 qlt_send_busy(ha->base_qpair, atio,
5488 qla_sam_status);
5489 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005490 }
Quinn Tran7cf95f72017-12-28 12:33:28 -08005491 if (!ha_locked)
5492 spin_unlock_irqrestore(&ha->hardware_lock,
5493 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005494 }
5495 break;
5496
5497 case IMMED_NOTIFY_TYPE:
5498 {
5499 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5500 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5501 "qla_target(%d): Received ATIO packet %x "
5502 "with error status %x\n", vha->vp_idx,
5503 atio->u.raw.entry_type,
5504 atio->u.isp2x.entry_status);
5505 break;
5506 }
5507 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005508
5509 if (!ha_locked)
5510 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005511 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005512 if (!ha_locked)
5513 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005514 break;
5515 }
5516
5517 default:
5518 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5519 "qla_target(%d): Received unknown ATIO atio "
5520 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5521 break;
5522 }
5523
Quinn Tran2f424b92015-12-17 14:57:07 -05005524 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005525}
5526
5527/* ha->hardware_lock supposed to be held on entry */
5528/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005529static void qlt_response_pkt(struct scsi_qla_host *vha,
5530 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005531{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005532 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005533
5534 if (unlikely(tgt == NULL)) {
5535 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005536 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5537 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005538 return;
5539 }
5540
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005541 /*
5542 * In tgt_stop mode we also should allow all requests to pass.
5543 * Otherwise, some commands can stuck.
5544 */
5545
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005546 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005547 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005548 case CTIO_TYPE7:
5549 {
5550 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005551 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005552 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5553 entry);
5554 break;
5555 }
5556
5557 case ACCEPT_TGT_IO_TYPE:
5558 {
5559 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5560 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005561 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005562 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005563 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5564 "qla_target(%d): ATIO with error "
5565 "status %x received\n", vha->vp_idx,
5566 le16_to_cpu(atio->u.isp2x.status));
5567 break;
5568 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005569
Quinn Tran82de8022017-06-13 20:47:17 -07005570 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005571 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005572 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005573
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005574 rc = qlt_handle_cmd_for_atio(vha, atio);
5575 if (unlikely(rc != 0)) {
Quinn Tran7cf95f72017-12-28 12:33:28 -08005576 switch (rc) {
5577 case -ENODEV:
5578 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5579 "qla_target: Unable to send command to target\n");
5580 break;
5581 case -EBADF:
5582 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5583 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5584 qlt_send_term_exchange(rsp->qpair, NULL,
5585 atio, 1, 0);
5586 break;
5587 case -EBUSY:
5588 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5589 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5590 vha->vp_idx);
5591 qlt_send_busy(rsp->qpair, atio,
5592 tc_sam_status);
5593 break;
5594 default:
5595 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5596 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5597 vha->vp_idx);
5598 qlt_send_busy(rsp->qpair, atio,
5599 qla_sam_status);
5600 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005601 }
5602 }
5603 }
5604 break;
5605
5606 case CONTINUE_TGT_IO_TYPE:
5607 {
5608 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005609 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005610 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5611 entry);
5612 break;
5613 }
5614
5615 case CTIO_A64_TYPE:
5616 {
5617 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005618 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005619 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5620 entry);
5621 break;
5622 }
5623
5624 case IMMED_NOTIFY_TYPE:
5625 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5626 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5627 break;
5628
5629 case NOTIFY_ACK_TYPE:
5630 if (tgt->notify_ack_expected > 0) {
5631 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5632 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5633 "NOTIFY_ACK seq %08x status %x\n",
5634 le16_to_cpu(entry->u.isp2x.seq_id),
5635 le16_to_cpu(entry->u.isp2x.status));
5636 tgt->notify_ack_expected--;
5637 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005638 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005639 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5640 "qla_target(%d): NOTIFY_ACK "
5641 "failed %x\n", vha->vp_idx,
5642 le16_to_cpu(entry->u.isp2x.status));
5643 }
5644 } else {
5645 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5646 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5647 vha->vp_idx);
5648 }
5649 break;
5650
5651 case ABTS_RECV_24XX:
5652 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5653 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5654 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5655 break;
5656
5657 case ABTS_RESP_24XX:
5658 if (tgt->abts_resp_expected > 0) {
5659 struct abts_resp_from_24xx_fw *entry =
5660 (struct abts_resp_from_24xx_fw *)pkt;
5661 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5662 "ABTS_RESP_24XX: compl_status %x\n",
5663 entry->compl_status);
5664 tgt->abts_resp_expected--;
5665 if (le16_to_cpu(entry->compl_status) !=
5666 ABTS_RESP_COMPL_SUCCESS) {
5667 if ((entry->error_subcode1 == 0x1E) &&
5668 (entry->error_subcode2 == 0)) {
5669 /*
5670 * We've got a race here: aborted
5671 * exchange not terminated, i.e.
5672 * response for the aborted command was
5673 * sent between the abort request was
5674 * received and processed.
5675 * Unfortunately, the firmware has a
5676 * silly requirement that all aborted
5677 * exchanges must be explicitely
5678 * terminated, otherwise it refuses to
5679 * send responses for the abort
5680 * requests. So, we have to
5681 * (re)terminate the exchange and retry
5682 * the abort response.
5683 */
5684 qlt_24xx_retry_term_exchange(vha,
5685 entry);
5686 } else
5687 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5688 "qla_target(%d): ABTS_RESP_24XX "
5689 "failed %x (subcode %x:%x)",
5690 vha->vp_idx, entry->compl_status,
5691 entry->error_subcode1,
5692 entry->error_subcode2);
5693 }
5694 } else {
5695 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5696 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5697 "received\n", vha->vp_idx);
5698 }
5699 break;
5700
5701 default:
5702 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5703 "qla_target(%d): Received unknown response pkt "
5704 "type %x\n", vha->vp_idx, pkt->entry_type);
5705 break;
5706 }
5707
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005708}
5709
5710/*
5711 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5712 */
5713void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5714 uint16_t *mailbox)
5715{
5716 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005717 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005718 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005719
Quinn Tran3a33dc92017-06-02 09:12:04 -07005720 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005721 return;
5722
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005723 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5724 IS_QLA2100(ha))
5725 return;
5726 /*
5727 * In tgt_stop mode we also should allow all requests to pass.
5728 * Otherwise, some commands can stuck.
5729 */
5730
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005731
5732 switch (code) {
5733 case MBA_RESET: /* Reset */
5734 case MBA_SYSTEM_ERR: /* System Error */
5735 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5736 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5737 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5738 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005739 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005740 break;
5741 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5742 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5743 break;
5744
5745 case MBA_LOOP_UP:
5746 {
5747 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005748 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005749 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5750 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5751 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005752 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005753 qlt_send_notify_ack(ha->base_qpair,
5754 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005755 0, 0, 0, 0, 0, 0);
5756 tgt->link_reinit_iocb_pending = 0;
5757 }
5758 break;
5759 }
5760
5761 case MBA_LIP_OCCURRED:
5762 case MBA_LOOP_DOWN:
5763 case MBA_LIP_RESET:
5764 case MBA_RSCN_UPDATE:
5765 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005766 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005767 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5768 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5769 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005770 break;
5771
Quinn Tranead03852017-01-19 22:28:01 -08005772 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005773 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5774 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5775 vha->vp_idx,
5776 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5777 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005778
5779 if (le16_to_cpu(mailbox[3]) == 1) {
5780 /* exchange starvation. */
5781 vha->hw->exch_starvation++;
5782 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005783 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08005784 "Exchange starvation-. Resetting RISC\n");
5785
5786 vha->hw->exch_starvation = 0;
5787 if (IS_P3P_TYPE(vha->hw))
5788 set_bit(FCOE_CTX_RESET_NEEDED,
5789 &vha->dpc_flags);
5790 else
5791 set_bit(ISP_ABORT_NEEDED,
5792 &vha->dpc_flags);
5793 qla2xxx_wake_dpc(vha);
5794 }
5795 }
5796 break;
5797
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005798 case MBA_PORT_UPDATE:
5799 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5800 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005801 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005802 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5803 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5804 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5805
5806 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08005807 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005808 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5809 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08005810 vha->hw->exch_starvation = 0;
5811 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005812 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5813 "Async MB 2: Port Logged Out\n");
5814 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005815 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005816 break;
5817 }
5818
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005819}
5820
5821static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5822 uint16_t loop_id)
5823{
Quinn Tran726b8542017-01-19 22:28:00 -08005824 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005825 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08005826 unsigned long flags;
5827 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005828
Quinn Tran063b36d2017-12-04 14:45:10 -08005829 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005830 if (!fcport) {
5831 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5832 "qla_target(%d): Allocation of tmp FC port failed",
5833 vha->vp_idx);
5834 return NULL;
5835 }
5836
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005837 fcport->loop_id = loop_id;
5838
Quinn Tran15f30a52017-03-15 09:48:52 -07005839 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005840 if (rc != QLA_SUCCESS) {
5841 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5842 "qla_target(%d): Failed to retrieve fcport "
5843 "information -- get_port_database() returned %x "
5844 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5845 kfree(fcport);
5846 return NULL;
5847 }
5848
Quinn Tran726b8542017-01-19 22:28:00 -08005849 del = NULL;
5850 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5851 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5852
5853 if (tfcp) {
5854 tfcp->d_id = fcport->d_id;
5855 tfcp->port_type = fcport->port_type;
5856 tfcp->supported_classes = fcport->supported_classes;
5857 tfcp->flags |= fcport->flags;
Quinn Tran76f9a2d2017-12-04 14:45:11 -08005858 tfcp->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005859
5860 del = fcport;
5861 fcport = tfcp;
5862 } else {
5863 if (vha->hw->current_topology == ISP_CFG_F)
5864 fcport->flags |= FCF_FABRIC_DEVICE;
5865
5866 list_add_tail(&fcport->list, &vha->vp_fcports);
5867 if (!IS_SW_RESV_ADDR(fcport->d_id))
5868 vha->fcport_count++;
5869 fcport->login_gen++;
5870 fcport->disc_state = DSC_LOGIN_COMPLETE;
5871 fcport->login_succ = 1;
5872 newfcport = 1;
5873 }
5874
5875 fcport->deleted = 0;
5876 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5877
5878 switch (vha->host->active_mode) {
5879 case MODE_INITIATOR:
5880 case MODE_DUAL:
5881 if (newfcport) {
5882 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005883 ql_dbg(ql_dbg_disc, vha, 0x20fe,
Quinn Tran726b8542017-01-19 22:28:00 -08005884 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5885 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5886 qla24xx_post_upd_fcport_work(vha, fcport);
5887 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07005888 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08005889 "%s %d %8phC post gpsc fcp_cnt %d\n",
5890 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5891 qla24xx_post_gpsc_work(vha, fcport);
5892 }
5893 }
5894 break;
5895
5896 case MODE_TARGET:
5897 default:
5898 break;
5899 }
5900 if (del)
5901 qla2x00_free_fcport(del);
5902
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005903 return fcport;
5904}
5905
5906/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08005907static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005908 uint8_t *s_id)
5909{
Quinn Tran5d964832017-01-19 22:27:59 -08005910 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005911 fc_port_t *fcport = NULL;
5912 int rc, global_resets;
5913 uint16_t loop_id = 0;
5914
Quinn Tran726b8542017-01-19 22:28:00 -08005915 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5916 /*
5917 * This is Domain Controller, so it should be
5918 * OK to drop SCSI commands from it.
5919 */
5920 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5921 "Unable to find initiator with S_ID %x:%x:%x",
5922 s_id[0], s_id[1], s_id[2]);
5923 return NULL;
5924 }
5925
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005926 mutex_lock(&vha->vha_tgt.tgt_mutex);
5927
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005928retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005929 global_resets =
5930 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005931
5932 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5933 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005934 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5935
Quinn Tran726b8542017-01-19 22:28:00 -08005936 ql_log(ql_log_info, vha, 0xf071,
5937 "qla_target(%d): Unable to find "
5938 "initiator with S_ID %x:%x:%x",
5939 vha->vp_idx, s_id[0], s_id[1],
5940 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005941
5942 if (rc == -ENOENT) {
5943 qlt_port_logo_t logo;
5944 sid_to_portid(s_id, &logo.id);
5945 logo.cmd_count = 1;
5946 qlt_send_first_logo(vha, &logo);
5947 }
5948
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005949 return NULL;
5950 }
5951
5952 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005953 if (!fcport) {
5954 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005955 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005956 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005957
5958 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005959 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005960 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5961 "qla_target(%d): global reset during session discovery "
5962 "(counter was %d, new %d), retrying", vha->vp_idx,
5963 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005964 atomic_read(&vha->vha_tgt.
5965 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005966 goto retry;
5967 }
5968
5969 sess = qlt_create_sess(vha, fcport, true);
5970
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005971 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5972
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005973 return sess;
5974}
5975
5976static void qlt_abort_work(struct qla_tgt *tgt,
5977 struct qla_tgt_sess_work_param *prm)
5978{
5979 struct scsi_qla_host *vha = tgt->vha;
5980 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005981 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005982 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005983 uint32_t be_s_id;
5984 uint8_t s_id[3];
5985 int rc;
5986
Quinn Tran75601512015-12-17 14:57:04 -05005987 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005988
5989 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005990 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005991
5992 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5993 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5994 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5995
5996 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5997 (unsigned char *)&be_s_id);
5998 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005999 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006000
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006001 sess = qlt_make_local_sess(vha, s_id);
6002 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006003
Quinn Tran75601512015-12-17 14:57:04 -05006004 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006005 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05006006 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006007 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006008 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006009 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05006010 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006011 }
6012
Quinn Tran726b8542017-01-19 22:28:00 -08006013 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006014 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08006015 "%s: kref_get fail %8phC \n",
6016 __func__, sess->port_name);
6017 sess = NULL;
6018 goto out_term2;
6019 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006020 }
6021
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006022 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006023 ha->tgt.tgt_ops->put_sess(sess);
6024 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6025
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006026 if (rc != 0)
6027 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006028 return;
6029
Quinn Tran75601512015-12-17 14:57:04 -05006030out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08006031 if (sess)
6032 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05006033 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006034
6035out_term:
6036 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006037 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6038 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006039 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006040}
6041
6042static void qlt_tmr_work(struct qla_tgt *tgt,
6043 struct qla_tgt_sess_work_param *prm)
6044{
6045 struct atio_from_isp *a = &prm->tm_iocb2;
6046 struct scsi_qla_host *vha = tgt->vha;
6047 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08006048 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006049 unsigned long flags;
6050 uint8_t *s_id = NULL; /* to hide compiler warnings */
6051 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07006052 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07006053 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006054 void *iocb;
6055
Quinn Tran75601512015-12-17 14:57:04 -05006056 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006057
6058 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006059 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006060
6061 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6062 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6063 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05006064 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006065
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006066 sess = qlt_make_local_sess(vha, s_id);
6067 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006068
Quinn Tran75601512015-12-17 14:57:04 -05006069 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006070 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006071 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006072 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006073 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006074 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006075 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006076 }
6077
Quinn Tran726b8542017-01-19 22:28:00 -08006078 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006079 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08006080 "%s: kref_get fail %8phC\n",
6081 __func__, sess->port_name);
6082 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006083 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006084 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006085 }
6086
6087 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006088 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006089 unpacked_lun =
6090 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006091
6092 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006093 ha->tgt.tgt_ops->put_sess(sess);
6094 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6095
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006096 if (rc != 0)
6097 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006098 return;
6099
Quinn Tranf159b3c2017-03-15 09:48:47 -07006100out_term2:
6101 if (sess)
6102 ha->tgt.tgt_ops->put_sess(sess);
6103 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006104out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006105 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006106}
6107
6108static void qlt_sess_work_fn(struct work_struct *work)
6109{
6110 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6111 struct scsi_qla_host *vha = tgt->vha;
6112 unsigned long flags;
6113
6114 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6115
6116 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6117 while (!list_empty(&tgt->sess_works_list)) {
6118 struct qla_tgt_sess_work_param *prm = list_entry(
6119 tgt->sess_works_list.next, typeof(*prm),
6120 sess_works_list_entry);
6121
6122 /*
6123 * This work can be scheduled on several CPUs at time, so we
6124 * must delete the entry to eliminate double processing
6125 */
6126 list_del(&prm->sess_works_list_entry);
6127
6128 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6129
6130 switch (prm->type) {
6131 case QLA_TGT_SESS_WORK_ABORT:
6132 qlt_abort_work(tgt, prm);
6133 break;
6134 case QLA_TGT_SESS_WORK_TM:
6135 qlt_tmr_work(tgt, prm);
6136 break;
6137 default:
6138 BUG_ON(1);
6139 break;
6140 }
6141
6142 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6143
6144 kfree(prm);
6145 }
6146 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6147}
6148
6149/* Must be called under tgt_host_action_mutex */
6150int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6151{
6152 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006153 int rc, i;
6154 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006155
6156 if (!QLA_TGT_MODE_ENABLED())
6157 return 0;
6158
Arun Easi33c36c02013-01-30 03:34:41 -05006159 if (!IS_TGT_MODE_CAPABLE(ha)) {
6160 ql_log(ql_log_warn, base_vha, 0xe070,
6161 "This adapter does not support target mode.\n");
6162 return 0;
6163 }
6164
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006165 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006166 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006167
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006168 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006169
6170 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6171 if (!tgt) {
6172 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6173 "Unable to allocate struct qla_tgt\n");
6174 return -ENOMEM;
6175 }
6176
Quinn Trane326d222017-06-13 20:47:18 -07006177 tgt->qphints = kzalloc((ha->max_qpairs + 1) *
6178 sizeof(struct qla_qpair_hint), GFP_KERNEL);
6179 if (!tgt->qphints) {
6180 kfree(tgt);
6181 ql_log(ql_log_warn, base_vha, 0x0197,
6182 "Unable to allocate qpair hints.\n");
6183 return -ENOMEM;
6184 }
6185
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006186 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6187 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6188
Quinn Trane326d222017-06-13 20:47:18 -07006189 rc = btree_init64(&tgt->lun_qpair_map);
6190 if (rc) {
6191 kfree(tgt->qphints);
6192 kfree(tgt);
6193 ql_log(ql_log_info, base_vha, 0x0198,
6194 "Unable to initialize lun_qpair_map btree\n");
6195 return -EIO;
6196 }
6197 h = &tgt->qphints[0];
6198 h->qpair = ha->base_qpair;
6199 INIT_LIST_HEAD(&h->hint_elem);
6200 h->cpuid = ha->base_qpair->cpuid;
6201 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6202
6203 for (i = 0; i < ha->max_qpairs; i++) {
6204 unsigned long flags;
6205
6206 struct qla_qpair *qpair = ha->queue_pair_map[i];
6207 h = &tgt->qphints[i + 1];
6208 INIT_LIST_HEAD(&h->hint_elem);
6209 if (qpair) {
6210 h->qpair = qpair;
6211 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6212 list_add_tail(&h->hint_elem, &qpair->hints_list);
6213 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6214 h->cpuid = qpair->cpuid;
6215 }
6216 }
6217
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006218 tgt->ha = ha;
6219 tgt->vha = base_vha;
6220 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006221 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006222 spin_lock_init(&tgt->sess_work_lock);
6223 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6224 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006225 atomic_set(&tgt->tgt_global_resets_count, 0);
6226
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006227 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006228
6229 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6230 "qla_target(%d): using 64 Bit PCI addressing",
6231 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006232 /* 3 is reserved */
6233 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006234
6235 mutex_lock(&qla_tgt_mutex);
6236 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6237 mutex_unlock(&qla_tgt_mutex);
6238
Quinn Tranf1443ee2017-03-15 09:48:51 -07006239 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6240 ha->tgt.tgt_ops->add_target(base_vha);
6241
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006242 return 0;
6243}
6244
6245/* Must be called under tgt_host_action_mutex */
6246int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6247{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006248 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006249 return 0;
6250
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006251 if (vha->fc_vport) {
6252 qlt_release(vha->vha_tgt.qla_tgt);
6253 return 0;
6254 }
Quinn Tran33e79972014-09-25 06:14:55 -04006255
6256 /* free left over qfull cmds */
6257 qlt_init_term_exchange(vha);
6258
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006259 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6260 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006261 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006262
6263 return 0;
6264}
6265
Quinn Tran482c9dc2017-03-15 09:48:54 -07006266void qlt_remove_target_resources(struct qla_hw_data *ha)
6267{
6268 struct scsi_qla_host *node;
6269 u32 key = 0;
6270
6271 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6272 btree_remove32(&ha->tgt.host_map, key);
6273
6274 btree_destroy32(&ha->tgt.host_map);
6275}
6276
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006277static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6278 unsigned char *b)
6279{
6280 int i;
6281
6282 pr_debug("qla2xxx HW vha->node_name: ");
6283 for (i = 0; i < WWN_SIZE; i++)
6284 pr_debug("%02x ", vha->node_name[i]);
6285 pr_debug("\n");
6286 pr_debug("qla2xxx HW vha->port_name: ");
6287 for (i = 0; i < WWN_SIZE; i++)
6288 pr_debug("%02x ", vha->port_name[i]);
6289 pr_debug("\n");
6290
6291 pr_debug("qla2xxx passed configfs WWPN: ");
6292 put_unaligned_be64(wwpn, b);
6293 for (i = 0; i < WWN_SIZE; i++)
6294 pr_debug("%02x ", b[i]);
6295 pr_debug("\n");
6296}
6297
6298/**
6299 * qla_tgt_lport_register - register lport with external module
6300 *
6301 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6302 * @wwpn: Passwd FC target WWPN
6303 * @callback: lport initialization callback for tcm_qla2xxx code
6304 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6305 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006306int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6307 u64 npiv_wwpn, u64 npiv_wwnn,
6308 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006309{
6310 struct qla_tgt *tgt;
6311 struct scsi_qla_host *vha;
6312 struct qla_hw_data *ha;
6313 struct Scsi_Host *host;
6314 unsigned long flags;
6315 int rc;
6316 u8 b[WWN_SIZE];
6317
6318 mutex_lock(&qla_tgt_mutex);
6319 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6320 vha = tgt->vha;
6321 ha = vha->hw;
6322
6323 host = vha->host;
6324 if (!host)
6325 continue;
6326
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006327 if (!(host->hostt->supported_mode & MODE_TARGET))
6328 continue;
6329
6330 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006331 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006332 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6333 host->host_no);
6334 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6335 continue;
6336 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006337 if (tgt->tgt_stop) {
6338 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6339 host->host_no);
6340 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6341 continue;
6342 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006343 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6344
6345 if (!scsi_host_get(host)) {
6346 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6347 "Unable to scsi_host_get() for"
6348 " qla2xxx scsi_host\n");
6349 continue;
6350 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006351 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006352
6353 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6354 scsi_host_put(host);
6355 continue;
6356 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006357 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6358 if (rc != 0)
6359 scsi_host_put(host);
6360
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006361 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006362 return rc;
6363 }
6364 mutex_unlock(&qla_tgt_mutex);
6365
6366 return -ENODEV;
6367}
6368EXPORT_SYMBOL(qlt_lport_register);
6369
6370/**
6371 * qla_tgt_lport_deregister - Degister lport
6372 *
6373 * @vha: Registered scsi_qla_host pointer
6374 */
6375void qlt_lport_deregister(struct scsi_qla_host *vha)
6376{
6377 struct qla_hw_data *ha = vha->hw;
6378 struct Scsi_Host *sh = vha->host;
6379 /*
6380 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6381 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006382 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006383 ha->tgt.tgt_ops = NULL;
6384 /*
6385 * Release the Scsi_Host reference for the underlying qla2xxx host
6386 */
6387 scsi_host_put(sh);
6388}
6389EXPORT_SYMBOL(qlt_lport_deregister);
6390
6391/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006392static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006393{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006394 switch (ql2x_ini_mode) {
6395 case QLA2XXX_INI_MODE_DISABLED:
6396 case QLA2XXX_INI_MODE_EXCLUSIVE:
6397 vha->host->active_mode = MODE_TARGET;
6398 break;
6399 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006400 vha->host->active_mode = MODE_UNKNOWN;
6401 break;
6402 case QLA2XXX_INI_MODE_DUAL:
6403 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006404 break;
6405 default:
6406 break;
6407 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006408}
6409
6410/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006411static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006412{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006413 switch (ql2x_ini_mode) {
6414 case QLA2XXX_INI_MODE_DISABLED:
6415 vha->host->active_mode = MODE_UNKNOWN;
6416 break;
6417 case QLA2XXX_INI_MODE_EXCLUSIVE:
6418 vha->host->active_mode = MODE_INITIATOR;
6419 break;
6420 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006421 case QLA2XXX_INI_MODE_DUAL:
6422 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006423 break;
6424 default:
6425 break;
6426 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006427}
6428
6429/*
6430 * qla_tgt_enable_vha - NO LOCK HELD
6431 *
6432 * host_reset, bring up w/ Target Mode Enabled
6433 */
6434void
6435qlt_enable_vha(struct scsi_qla_host *vha)
6436{
6437 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006438 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006439 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006440 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006441
6442 if (!tgt) {
6443 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6444 "Unable to locate qla_tgt pointer from"
6445 " struct qla_hw_data\n");
6446 dump_stack();
6447 return;
6448 }
6449
6450 spin_lock_irqsave(&ha->hardware_lock, flags);
6451 tgt->tgt_stopped = 0;
6452 qlt_set_mode(vha);
6453 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6454
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006455 if (vha->vp_idx) {
6456 qla24xx_disable_vp(vha);
6457 qla24xx_enable_vp(vha);
6458 } else {
6459 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6460 qla2xxx_wake_dpc(base_vha);
6461 qla2x00_wait_for_hba_online(base_vha);
6462 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006463}
6464EXPORT_SYMBOL(qlt_enable_vha);
6465
6466/*
6467 * qla_tgt_disable_vha - NO LOCK HELD
6468 *
6469 * Disable Target Mode and reset the adapter
6470 */
Joern Engel55a90662014-09-16 16:23:15 -04006471static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006472{
6473 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006474 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006475 unsigned long flags;
6476
6477 if (!tgt) {
6478 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6479 "Unable to locate qla_tgt pointer from"
6480 " struct qla_hw_data\n");
6481 dump_stack();
6482 return;
6483 }
6484
6485 spin_lock_irqsave(&ha->hardware_lock, flags);
6486 qlt_clear_mode(vha);
6487 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6488
6489 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6490 qla2xxx_wake_dpc(vha);
6491 qla2x00_wait_for_hba_online(vha);
6492}
6493
6494/*
6495 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6496 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6497 * members.
6498 */
6499void
6500qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6501{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006502 vha->vha_tgt.qla_tgt = NULL;
6503
6504 mutex_init(&vha->vha_tgt.tgt_mutex);
6505 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006506
6507 qlt_clear_mode(vha);
6508
6509 /*
6510 * NOTE: Currently the value is kept the same for <24xx and
6511 * >=24xx ISPs. If it is necessary to change it,
6512 * the check should be added for specific ISPs,
6513 * assigning the value appropriately.
6514 */
6515 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006516
6517 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006518}
6519
Quinn Trane374f9f2017-12-28 12:33:31 -08006520u8
6521qlt_rff_id(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006522{
Quinn Trane374f9f2017-12-28 12:33:31 -08006523 u8 fc4_feature = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006524 /*
6525 * FC-4 Feature bit 0 indicates target functionality to the name server.
6526 */
6527 if (qla_tgt_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006528 fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006529 } else if (qla_ini_mode_enabled(vha)) {
Quinn Trane374f9f2017-12-28 12:33:31 -08006530 fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006531 } else if (qla_dual_mode_enabled(vha))
Quinn Trane374f9f2017-12-28 12:33:31 -08006532 fc4_feature = BIT_0 | BIT_1;
6533
6534 return fc4_feature;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006535}
6536
6537/*
6538 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6539 * @ha: HA context
6540 *
6541 * Beginning of ATIO ring has initialization control block already built
6542 * by nvram config routine.
6543 *
6544 * Returns 0 on success.
6545 */
6546void
6547qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6548{
6549 struct qla_hw_data *ha = vha->hw;
6550 uint16_t cnt;
6551 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6552
Quinn Tranead03852017-01-19 22:28:01 -08006553 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006554 return;
6555
6556 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6557 pkt->u.raw.signature = ATIO_PROCESSED;
6558 pkt++;
6559 }
6560
6561}
6562
6563/*
6564 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6565 * @ha: SCSI driver HA context
6566 */
6567void
Quinn Tran2f424b92015-12-17 14:57:07 -05006568qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006569{
6570 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006571 struct atio_from_isp *pkt;
6572 int cnt, i;
6573
Quinn Tranec7193e2017-03-15 09:48:55 -07006574 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006575 return;
6576
Quinn Tran5f355092016-12-23 18:06:11 -08006577 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6578 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006579 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6580 cnt = pkt->u.raw.entry_count;
6581
Quinn Tran5f355092016-12-23 18:06:11 -08006582 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6583 /*
6584 * This packet is corrupted. The header + payload
6585 * can not be trusted. There is no point in passing
6586 * it further up.
6587 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006588 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006589 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6590 pkt->u.isp24.fcp_hdr.s_id,
6591 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6592 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6593
6594 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006595 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6596 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006597 } else {
6598 qlt_24xx_atio_pkt_all_vps(vha,
6599 (struct atio_from_isp *)pkt, ha_locked);
6600 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006601
6602 for (i = 0; i < cnt; i++) {
6603 ha->tgt.atio_ring_index++;
6604 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6605 ha->tgt.atio_ring_index = 0;
6606 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6607 } else
6608 ha->tgt.atio_ring_ptr++;
6609
6610 pkt->u.raw.signature = ATIO_PROCESSED;
6611 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6612 }
6613 wmb();
6614 }
6615
6616 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006617 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006618}
6619
6620void
Arun Easiaa230bc2013-01-30 03:34:39 -05006621qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006622{
6623 struct qla_hw_data *ha = vha->hw;
Quinn Trand2b292c2017-12-28 12:33:17 -08006624 struct qla_msix_entry *msix = &ha->msix_entries[2];
6625 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6626
Arun Easiaa230bc2013-01-30 03:34:39 -05006627 if (!QLA_TGT_MODE_ENABLED())
6628 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006629
Arun Easiaa230bc2013-01-30 03:34:39 -05006630 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6631 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6632 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6633
Quinn Trand2b292c2017-12-28 12:33:17 -08006634 if (ha->flags.msix_enabled) {
6635 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6636 if (IS_QLA2071(ha)) {
6637 /* 4 ports Baker: Enable Interrupt Handshake */
6638 icb->msix_atio = 0;
6639 icb->firmware_options_2 |= BIT_26;
6640 } else {
6641 icb->msix_atio = cpu_to_le16(msix->entry);
6642 icb->firmware_options_2 &= ~BIT_26;
6643 }
6644 ql_dbg(ql_dbg_init, vha, 0xf072,
6645 "Registering ICB vector 0x%x for atio que.\n",
6646 msix->entry);
6647 }
6648 } else {
6649 /* INTx|MSI */
6650 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6651 icb->msix_atio = 0;
6652 icb->firmware_options_2 |= BIT_26;
6653 ql_dbg(ql_dbg_init, vha, 0xf072,
6654 "%s: Use INTx for ATIOQ.\n", __func__);
6655 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006656 }
6657}
6658
6659void
6660qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6661{
6662 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006663 u32 tmp;
Quinn Tran99e1b682017-06-02 09:12:03 -07006664
6665 if (!QLA_TGT_MODE_ENABLED())
6666 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006667
Quinn Tranead03852017-01-19 22:28:01 -08006668 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006669 if (!ha->tgt.saved_set) {
6670 /* We save only once */
6671 ha->tgt.saved_exchange_count = nv->exchange_count;
6672 ha->tgt.saved_firmware_options_1 =
6673 nv->firmware_options_1;
6674 ha->tgt.saved_firmware_options_2 =
6675 nv->firmware_options_2;
6676 ha->tgt.saved_firmware_options_3 =
6677 nv->firmware_options_3;
6678 ha->tgt.saved_set = 1;
6679 }
6680
Quinn Tran99e1b682017-06-02 09:12:03 -07006681 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006682 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006683 else /* dual */
6684 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006685
6686 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006687 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006688
6689 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006690 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006691 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006692
6693 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006694 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006695 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006696 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006697 if (ql2xtgt_tape_enable)
6698 /* Enable FC Tape support */
6699 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6700 else
6701 /* Disable FC Tape support */
6702 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6703
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006704 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006705 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006706
6707 /*
6708 * clear BIT 15 explicitly as we have seen at least
6709 * a couple of instances where this was set and this
6710 * was causing the firmware to not be initialized.
6711 */
6712 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006713 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006714 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08006715
6716 if (IS_QLA25XX(ha)) {
6717 /* Change Loop-prefer to Pt-Pt */
6718 tmp = ~(BIT_4|BIT_5|BIT_6);
6719 nv->firmware_options_2 &= cpu_to_le32(tmp);
6720 tmp = P2P << 4;
6721 nv->firmware_options_2 |= cpu_to_le32(tmp);
6722 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006723 } else {
6724 if (ha->tgt.saved_set) {
6725 nv->exchange_count = ha->tgt.saved_exchange_count;
6726 nv->firmware_options_1 =
6727 ha->tgt.saved_firmware_options_1;
6728 nv->firmware_options_2 =
6729 ha->tgt.saved_firmware_options_2;
6730 nv->firmware_options_3 =
6731 ha->tgt.saved_firmware_options_3;
6732 }
6733 return;
6734 }
6735
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006736 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006737 if (vha->flags.init_done)
6738 fc_host_supported_classes(vha->host) =
6739 FC_COS_CLASS2 | FC_COS_CLASS3;
6740
Bart Van Asschead950362015-07-09 07:24:08 -07006741 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006742 } else {
6743 if (vha->flags.init_done)
6744 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6745
Bart Van Asschead950362015-07-09 07:24:08 -07006746 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006747 }
6748}
6749
6750void
6751qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6752 struct init_cb_24xx *icb)
6753{
6754 struct qla_hw_data *ha = vha->hw;
6755
Quinn Tran481ce732015-12-17 14:57:08 -05006756 if (!QLA_TGT_MODE_ENABLED())
6757 return;
6758
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006759 if (ha->tgt.node_name_set) {
6760 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006761 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006762 }
Quinn Tran481ce732015-12-17 14:57:08 -05006763
6764 /* disable ZIO at start time. */
6765 if (!vha->flags.init_done) {
6766 uint32_t tmp;
6767 tmp = le32_to_cpu(icb->firmware_options_2);
6768 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6769 icb->firmware_options_2 = cpu_to_le32(tmp);
6770 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006771}
6772
Arun Easiaa230bc2013-01-30 03:34:39 -05006773void
6774qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6775{
6776 struct qla_hw_data *ha = vha->hw;
Quinn Tran9cd883f2017-12-28 12:33:24 -08006777 u32 tmp;
Arun Easiaa230bc2013-01-30 03:34:39 -05006778
6779 if (!QLA_TGT_MODE_ENABLED())
6780 return;
6781
Quinn Tranead03852017-01-19 22:28:01 -08006782 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006783 if (!ha->tgt.saved_set) {
6784 /* We save only once */
6785 ha->tgt.saved_exchange_count = nv->exchange_count;
6786 ha->tgt.saved_firmware_options_1 =
6787 nv->firmware_options_1;
6788 ha->tgt.saved_firmware_options_2 =
6789 nv->firmware_options_2;
6790 ha->tgt.saved_firmware_options_3 =
6791 nv->firmware_options_3;
6792 ha->tgt.saved_set = 1;
6793 }
6794
Quinn Tran99e1b682017-06-02 09:12:03 -07006795 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006796 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006797 else /* dual */
6798 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05006799
6800 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006801 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006802
6803 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006804 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006805 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006806 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006807 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006808 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006809 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006810 /*
6811 * clear BIT 15 explicitly as we have seen at
6812 * least a couple of instances where this was set
6813 * and this was causing the firmware to not be
6814 * initialized.
6815 */
6816 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006817 if (ql2xtgt_tape_enable)
6818 /* Enable FC tape support */
6819 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6820 else
6821 /* Disable FC tape support */
6822 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6823
Arun Easiaa230bc2013-01-30 03:34:39 -05006824 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006825 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006826 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006827 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Quinn Tran9cd883f2017-12-28 12:33:24 -08006828
6829 /* Change Loop-prefer to Pt-Pt */
6830 tmp = ~(BIT_4|BIT_5|BIT_6);
6831 nv->firmware_options_2 &= cpu_to_le32(tmp);
6832 tmp = P2P << 4;
6833 nv->firmware_options_2 |= cpu_to_le32(tmp);
Arun Easiaa230bc2013-01-30 03:34:39 -05006834 } else {
6835 if (ha->tgt.saved_set) {
6836 nv->exchange_count = ha->tgt.saved_exchange_count;
6837 nv->firmware_options_1 =
6838 ha->tgt.saved_firmware_options_1;
6839 nv->firmware_options_2 =
6840 ha->tgt.saved_firmware_options_2;
6841 nv->firmware_options_3 =
6842 ha->tgt.saved_firmware_options_3;
6843 }
6844 return;
6845 }
6846
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006847 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006848 if (vha->flags.init_done)
6849 fc_host_supported_classes(vha->host) =
6850 FC_COS_CLASS2 | FC_COS_CLASS3;
6851
Bart Van Asschead950362015-07-09 07:24:08 -07006852 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006853 } else {
6854 if (vha->flags.init_done)
6855 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6856
Bart Van Asschead950362015-07-09 07:24:08 -07006857 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006858 }
6859}
6860
6861void
6862qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6863 struct init_cb_81xx *icb)
6864{
6865 struct qla_hw_data *ha = vha->hw;
6866
6867 if (!QLA_TGT_MODE_ENABLED())
6868 return;
6869
6870 if (ha->tgt.node_name_set) {
6871 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006872 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006873 }
Quinn Tran481ce732015-12-17 14:57:08 -05006874
6875 /* disable ZIO at start time. */
6876 if (!vha->flags.init_done) {
6877 uint32_t tmp;
6878 tmp = le32_to_cpu(icb->firmware_options_2);
6879 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6880 icb->firmware_options_2 = cpu_to_le32(tmp);
6881 }
6882
Arun Easiaa230bc2013-01-30 03:34:39 -05006883}
6884
6885void
6886qlt_83xx_iospace_config(struct qla_hw_data *ha)
6887{
6888 if (!QLA_TGT_MODE_ENABLED())
6889 return;
6890
6891 ha->msix_count += 1; /* For ATIO Q */
6892}
6893
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006894
6895void
6896qlt_modify_vp_config(struct scsi_qla_host *vha,
6897 struct vp_config_entry_24xx *vpmod)
6898{
Quinn Tranead03852017-01-19 22:28:01 -08006899 /* enable target mode. Bit5 = 1 => disable */
6900 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006901 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08006902
Quinn Tranead03852017-01-19 22:28:01 -08006903 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08006904 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006905 vpmod->options_idx1 &= ~BIT_4;
6906}
6907
6908void
6909qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6910{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006911 int rc;
6912
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006913 if (!QLA_TGT_MODE_ENABLED())
6914 return;
6915
Himanshu Madhanic9558862017-10-13 09:34:04 -07006916 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006917 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6918 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6919 } else {
6920 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6921 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6922 }
6923
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006924 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6925 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08006926
6927 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6928 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6929 qlt_unknown_atio_work_fn);
6930
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006931 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006932
6933 rc = btree_init32(&ha->tgt.host_map);
6934 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006935 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006936 "Unable to initialize ha->host_map btree\n");
6937
6938 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006939}
6940
Arun Easiaa230bc2013-01-30 03:34:39 -05006941irqreturn_t
6942qla83xx_msix_atio_q(int irq, void *dev_id)
6943{
6944 struct rsp_que *rsp;
6945 scsi_qla_host_t *vha;
6946 struct qla_hw_data *ha;
6947 unsigned long flags;
6948
6949 rsp = (struct rsp_que *) dev_id;
6950 ha = rsp->hw;
6951 vha = pci_get_drvdata(ha->pdev);
6952
Quinn Tran2f424b92015-12-17 14:57:07 -05006953 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006954
Quinn Tran2f424b92015-12-17 14:57:07 -05006955 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006956
Quinn Tran2f424b92015-12-17 14:57:07 -05006957 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006958
6959 return IRQ_HANDLED;
6960}
6961
Quinn Tran2f424b92015-12-17 14:57:07 -05006962static void
6963qlt_handle_abts_recv_work(struct work_struct *work)
6964{
6965 struct qla_tgt_sess_op *op = container_of(work,
6966 struct qla_tgt_sess_op, work);
6967 scsi_qla_host_t *vha = op->vha;
6968 struct qla_hw_data *ha = vha->hw;
6969 unsigned long flags;
6970
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006971 if (qla2x00_reset_active(vha) ||
6972 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05006973 return;
6974
6975 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6976 qlt_24xx_process_atio_queue(vha, 0);
6977 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6978
6979 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006980 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05006981 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07006982
6983 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05006984}
6985
6986void
Quinn Tran82de8022017-06-13 20:47:17 -07006987qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
6988 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05006989{
6990 struct qla_tgt_sess_op *op;
6991
6992 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6993
6994 if (!op) {
6995 /* do not reach for ATIO queue here. This is best effort err
6996 * recovery at this point.
6997 */
Quinn Tran82de8022017-06-13 20:47:17 -07006998 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05006999 return;
7000 }
7001
7002 memcpy(&op->atio, pkt, sizeof(*pkt));
7003 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007004 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07007005 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05007006 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7007 queue_work(qla_tgt_wq, &op->work);
7008 return;
7009}
7010
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007011int
7012qlt_mem_alloc(struct qla_hw_data *ha)
7013{
7014 if (!QLA_TGT_MODE_ENABLED())
7015 return 0;
7016
7017 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
7018 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
7019 if (!ha->tgt.tgt_vp_map)
7020 return -ENOMEM;
7021
7022 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7023 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7024 &ha->tgt.atio_dma, GFP_KERNEL);
7025 if (!ha->tgt.atio_ring) {
7026 kfree(ha->tgt.tgt_vp_map);
7027 return -ENOMEM;
7028 }
7029 return 0;
7030}
7031
7032void
7033qlt_mem_free(struct qla_hw_data *ha)
7034{
7035 if (!QLA_TGT_MODE_ENABLED())
7036 return;
7037
7038 if (ha->tgt.atio_ring) {
7039 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7040 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7041 ha->tgt.atio_dma);
7042 }
7043 kfree(ha->tgt.tgt_vp_map);
7044}
7045
7046/* vport_slock to be held by the caller */
7047void
7048qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7049{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007050 void *slot;
7051 u32 key;
7052 int rc;
7053
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007054 if (!QLA_TGT_MODE_ENABLED())
7055 return;
7056
Quinn Tran482c9dc2017-03-15 09:48:54 -07007057 key = vha->d_id.b24;
7058
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007059 switch (cmd) {
7060 case SET_VP_IDX:
7061 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7062 break;
7063 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07007064 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7065 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007066 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007067 "Save vha in host_map %p %06x\n", vha, key);
7068 rc = btree_insert32(&vha->hw->tgt.host_map,
7069 key, vha, GFP_ATOMIC);
7070 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07007071 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007072 "Unable to insert s_id into host_map: %06x\n",
7073 key);
7074 return;
7075 }
Quinn Tran83548fe2017-06-02 09:12:01 -07007076 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7077 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007078 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007079 break;
7080 case RESET_VP_IDX:
7081 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7082 break;
7083 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07007084 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07007085 "clear vha in host_map %p %06x\n", vha, key);
7086 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7087 if (slot)
7088 btree_remove32(&vha->hw->tgt.host_map, key);
7089 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007090 break;
7091 }
7092}
7093
Quinn Tran482c9dc2017-03-15 09:48:54 -07007094void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7095{
Quinn Tran482c9dc2017-03-15 09:48:54 -07007096
7097 if (!vha->d_id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007098 vha->d_id = id;
7099 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007100 } else if (vha->d_id.b24 != id.b24) {
Quinn Tran482c9dc2017-03-15 09:48:54 -07007101 qlt_update_vp_map(vha, RESET_AL_PA);
7102 vha->d_id = id;
7103 qlt_update_vp_map(vha, SET_AL_PA);
Quinn Tran482c9dc2017-03-15 09:48:54 -07007104 }
7105}
7106
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007107static int __init qlt_parse_ini_mode(void)
7108{
7109 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7110 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7111 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7112 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7113 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7114 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007115 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7116 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007117 else
7118 return false;
7119
7120 return true;
7121}
7122
7123int __init qlt_init(void)
7124{
7125 int ret;
7126
7127 if (!qlt_parse_ini_mode()) {
7128 ql_log(ql_log_fatal, NULL, 0xe06b,
7129 "qlt_parse_ini_mode() failed\n");
7130 return -EINVAL;
7131 }
7132
7133 if (!QLA_TGT_MODE_ENABLED())
7134 return 0;
7135
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007136 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7137 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7138 qla_tgt_mgmt_cmd), 0, NULL);
7139 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007140 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007141 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007142 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007143 }
7144
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007145 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007146 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7147 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007148
7149 if (!qla_tgt_plogi_cachep) {
7150 ql_log(ql_log_fatal, NULL, 0xe06d,
7151 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7152 ret = -ENOMEM;
7153 goto out_mgmt_cmd_cachep;
7154 }
7155
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007156 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7157 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7158 if (!qla_tgt_mgmt_cmd_mempool) {
7159 ql_log(ql_log_fatal, NULL, 0xe06e,
7160 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7161 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007162 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007163 }
7164
7165 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7166 if (!qla_tgt_wq) {
7167 ql_log(ql_log_fatal, NULL, 0xe06f,
7168 "alloc_workqueue for qla_tgt_wq failed\n");
7169 ret = -ENOMEM;
7170 goto out_cmd_mempool;
7171 }
7172 /*
7173 * Return 1 to signal that initiator-mode is being disabled
7174 */
7175 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7176
7177out_cmd_mempool:
7178 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007179out_plogi_cachep:
7180 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007181out_mgmt_cmd_cachep:
7182 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007183 return ret;
7184}
7185
7186void qlt_exit(void)
7187{
7188 if (!QLA_TGT_MODE_ENABLED())
7189 return;
7190
7191 destroy_workqueue(qla_tgt_wq);
7192 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007193 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007194 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007195}