blob: 192554b1536fa0b76b3e20d2bdf725e75892eb70 [file] [log] [blame]
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001/*
2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
3 *
4 * based on qla2x00t.c code:
5 *
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
10 *
11 * Forward port and refactoring to modern qla2xxx and target/configfs
12 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -070013 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
18 * of the License.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/types.h>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040029#include <linux/blkdev.h>
30#include <linux/interrupt.h>
31#include <linux/pci.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/workqueue.h>
35#include <asm/unaligned.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_tcq.h>
39#include <target/target_core_base.h>
40#include <target/target_core_fabric.h>
41
42#include "qla_def.h"
43#include "qla_target.h"
44
Arun Easid154f352014-09-25 06:14:48 -040045static int ql2xtgt_tape_enable;
46module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
49
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040050static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51module_param(qlini_mode, charp, S_IRUGO);
52MODULE_PARM_DESC(qlini_mode,
53 "Determines when initiator mode will be enabled. Possible values: "
54 "\"exclusive\" - initiator mode will be enabled on load, "
55 "disabled on enabling target mode and then on disabling target mode "
56 "enabled back; "
57 "\"disabled\" - initiator mode will never be enabled; "
Quinn Tranead03852017-01-19 22:28:01 -080058 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
59 "when ready "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040060 "\"enabled\" (default) - initiator mode will always stay enabled.");
61
Quinn Tran99e1b682017-06-02 09:12:03 -070062static int ql_dm_tgt_ex_pct = 0;
Quinn Tranead03852017-01-19 22:28:01 -080063module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
67 "for Target mode.");
68
Quinn Tran09620eeb2017-06-13 20:47:20 -070069int ql2xuctrlirq = 1;
70module_param(ql2xuctrlirq, int, 0644);
71MODULE_PARM_DESC(ql2xuctrlirq,
72 "User to control IRQ placement via smp_affinity."
73 "Valid with qlini_mode=disabled."
74 "1(default): enable");
75
Arun Easiaa230bc2013-01-30 03:34:39 -050076int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040077
Quinn Tran33e79972014-09-25 06:14:55 -040078static int temp_sam_status = SAM_STAT_BUSY;
79
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040080/*
81 * From scsi/fc/fc_fcp.h
82 */
83enum fcp_resp_rsp_codes {
84 FCP_TMF_CMPL = 0,
85 FCP_DATA_LEN_INVALID = 1,
86 FCP_CMND_FIELDS_INVALID = 2,
87 FCP_DATA_PARAM_MISMATCH = 3,
88 FCP_TMF_REJECTED = 4,
89 FCP_TMF_FAILED = 5,
90 FCP_TMF_INVALID_LUN = 9,
91};
92
93/*
94 * fc_pri_ta from scsi/fc/fc_fcp.h
95 */
96#define FCP_PTA_SIMPLE 0 /* simple task attribute */
97#define FCP_PTA_HEADQ 1 /* head of queue task attribute */
98#define FCP_PTA_ORDERED 2 /* ordered task attribute */
Masanari Iida6efb3c0a2012-10-26 22:10:54 +090099#define FCP_PTA_ACA 4 /* auto. contingent allegiance */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400100#define FCP_PTA_MASK 7 /* mask for task attribute field */
101#define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
102#define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
103
104/*
105 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
106 * must be called under HW lock and could unlock/lock it inside.
107 * It isn't an issue, since in the current implementation on the time when
108 * those functions are called:
109 *
110 * - Either context is IRQ and only IRQ handler can modify HW data,
111 * including rings related fields,
112 *
113 * - Or access to target mode variables from struct qla_tgt doesn't
114 * cross those functions boundaries, except tgt_stop, which
115 * additionally protected by irq_cmd_count.
116 */
117/* Predefs for callbacks handed to qla2xxx LLD */
118static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500119 struct atio_from_isp *pkt, uint8_t);
Quinn Tran82de8022017-06-13 20:47:17 -0700120static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
121 response_t *pkt);
Quinn Tran5d964832017-01-19 22:27:59 -0800122static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400123 int fn, void *iocb, int flags);
Quinn Tran82de8022017-06-13 20:47:17 -0700124static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
Quinn Trana07100e2015-12-07 19:48:57 -0500125 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
Quinn Tran33e79972014-09-25 06:14:55 -0400126static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
127 struct atio_from_isp *atio, uint16_t status, int qfull);
Joern Engel55a90662014-09-16 16:23:15 -0400128static void qlt_disable_vha(struct scsi_qla_host *vha);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400129static void qlt_clear_tgt_db(struct qla_tgt *tgt);
Quinn Tran82de8022017-06-13 20:47:17 -0700130static void qlt_send_notify_ack(struct qla_qpair *qpair,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400131 struct imm_ntfy_from_isp *ntfy,
132 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
133 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500134static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
135 struct imm_ntfy_from_isp *imm, int ha_locked);
Quinn Tran726b8542017-01-19 22:28:00 -0800136static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
137 fc_port_t *fcport, bool local);
138void qlt_unreg_sess(struct fc_port *sess);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700139static void qlt_24xx_handle_abts(struct scsi_qla_host *,
140 struct abts_recv_from_24xx *);
Quinn Tran82de8022017-06-13 20:47:17 -0700141static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
142 uint16_t);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700143
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400144/*
145 * Global Variables
146 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400147static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
Quinn Tranb5d15312017-08-30 10:16:49 -0700148struct kmem_cache *qla_tgt_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400149static mempool_t *qla_tgt_mgmt_cmd_mempool;
150static struct workqueue_struct *qla_tgt_wq;
151static DEFINE_MUTEX(qla_tgt_mutex);
152static LIST_HEAD(qla_tgt_glist);
153
Quinn Tranbe251522017-03-15 09:48:49 -0700154static const char *prot_op_str(u32 prot_op)
155{
156 switch (prot_op) {
157 case TARGET_PROT_NORMAL: return "NORMAL";
158 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
159 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
160 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
161 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
162 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
163 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
164 default: return "UNKNOWN";
165 }
166}
167
Alexei Potashnikdf673272015-07-14 16:00:46 -0400168/* This API intentionally takes dest as a parameter, rather than returning
169 * int value to avoid caller forgetting to issue wmb() after the store */
170void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
171{
172 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
173 *dest = atomic_inc_return(&base_vha->generation_tick);
174 /* memory barrier */
175 wmb();
176}
177
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400178/* Might release hw lock, then reaquire!! */
179static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
180{
181 /* Send marker if required */
182 if (unlikely(vha->marker_needed != 0)) {
183 int rc = qla2x00_issue_marker(vha, vha_locked);
184 if (rc != QLA_SUCCESS) {
185 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
186 "qla_target(%d): issue_marker() failed\n",
187 vha->vp_idx);
188 }
189 return rc;
190 }
191 return QLA_SUCCESS;
192}
193
194static inline
195struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
196 uint8_t *d_id)
197{
Quinn Tran482c9dc2017-03-15 09:48:54 -0700198 struct scsi_qla_host *host;
199 uint32_t key = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400200
Quinn Tran482c9dc2017-03-15 09:48:54 -0700201 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
202 (vha->d_id.b.al_pa == d_id[2]))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400203 return vha;
204
Quinn Tran482c9dc2017-03-15 09:48:54 -0700205 key = (uint32_t)d_id[0] << 16;
206 key |= (uint32_t)d_id[1] << 8;
207 key |= (uint32_t)d_id[2];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400208
Quinn Tran482c9dc2017-03-15 09:48:54 -0700209 host = btree_lookup32(&vha->hw->tgt.host_map, key);
210 if (!host)
Quinn Tran83548fe2017-06-02 09:12:01 -0700211 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
212 "Unable to find host %06x\n", key);
Quinn Tran482c9dc2017-03-15 09:48:54 -0700213
214 return host;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400215}
216
217static inline
218struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
219 uint16_t vp_idx)
220{
221 struct qla_hw_data *ha = vha->hw;
222
223 if (vha->vp_idx == vp_idx)
224 return vha;
225
226 BUG_ON(ha->tgt.tgt_vp_map == NULL);
227 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
228 return ha->tgt.tgt_vp_map[vp_idx].vha;
229
230 return NULL;
231}
232
Quinn Tran33e79972014-09-25 06:14:55 -0400233static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
234{
235 unsigned long flags;
236
237 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
238
239 vha->hw->tgt.num_pend_cmds++;
Joe Carnucciofc90ada2016-07-06 11:14:23 -0400240 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
241 vha->qla_stats.stat_max_pend_cmds =
Quinn Tran33e79972014-09-25 06:14:55 -0400242 vha->hw->tgt.num_pend_cmds;
243 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
244}
245static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
246{
247 unsigned long flags;
248
249 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
250 vha->hw->tgt.num_pend_cmds--;
251 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
252}
253
Quinn Tran41dc5292017-01-19 22:28:03 -0800254
255static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
Quinn Tran82de8022017-06-13 20:47:17 -0700256 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran41dc5292017-01-19 22:28:03 -0800257{
258 struct qla_tgt_sess_op *u;
259 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
260 unsigned long flags;
261
262 if (tgt->tgt_stop) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700263 ql_dbg(ql_dbg_async, vha, 0x502c,
264 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
265 vha->vp_idx);
Quinn Tran41dc5292017-01-19 22:28:03 -0800266 goto out_term;
267 }
268
269 u = kzalloc(sizeof(*u), GFP_ATOMIC);
Quinn Tran83548fe2017-06-02 09:12:01 -0700270 if (u == NULL)
Quinn Tran41dc5292017-01-19 22:28:03 -0800271 goto out_term;
Quinn Tran41dc5292017-01-19 22:28:03 -0800272
273 u->vha = vha;
274 memcpy(&u->atio, atio, sizeof(*atio));
275 INIT_LIST_HEAD(&u->cmd_list);
276
277 spin_lock_irqsave(&vha->cmd_list_lock, flags);
278 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
279 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
280
281 schedule_delayed_work(&vha->unknown_atio_work, 1);
282
283out:
284 return;
285
286out_term:
Quinn Tran82de8022017-06-13 20:47:17 -0700287 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800288 goto out;
289}
290
291static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
292 uint8_t ha_locked)
293{
294 struct qla_tgt_sess_op *u, *t;
295 scsi_qla_host_t *host;
296 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
297 unsigned long flags;
298 uint8_t queued = 0;
299
300 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
301 if (u->aborted) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700302 ql_dbg(ql_dbg_async, vha, 0x502e,
303 "Freeing unknown %s %p, because of Abort\n",
Quinn Tran41dc5292017-01-19 22:28:03 -0800304 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700305 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
306 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800307 goto abort;
308 }
309
310 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
311 if (host != NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700312 ql_dbg(ql_dbg_async, vha, 0x502f,
313 "Requeuing unknown ATIO_TYPE7 %p\n", u);
Quinn Tran41dc5292017-01-19 22:28:03 -0800314 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
315 } else if (tgt->tgt_stop) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700316 ql_dbg(ql_dbg_async, vha, 0x503a,
317 "Freeing unknown %s %p, because tgt is being stopped\n",
318 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700319 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
320 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800321 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700322 ql_dbg(ql_dbg_async, vha, 0x503d,
323 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
Quinn Tran41dc5292017-01-19 22:28:03 -0800324 if (!queued) {
325 queued = 1;
326 schedule_delayed_work(&vha->unknown_atio_work,
327 1);
328 }
329 continue;
330 }
331
332abort:
333 spin_lock_irqsave(&vha->cmd_list_lock, flags);
334 list_del(&u->cmd_list);
335 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
336 kfree(u);
337 }
338}
339
340void qlt_unknown_atio_work_fn(struct work_struct *work)
341{
342 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
343 struct scsi_qla_host, unknown_atio_work);
344
345 qlt_try_to_dequeue_unknown_atios(vha, 0);
346}
347
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500348static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500349 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400350{
Quinn Tranf83adb62014-04-11 16:54:43 -0400351 ql_dbg(ql_dbg_tgt, vha, 0xe072,
352 "%s: qla_target(%d): type %x ox_id %04x\n",
353 __func__, vha->vp_idx, atio->u.raw.entry_type,
354 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
355
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400356 switch (atio->u.raw.entry_type) {
357 case ATIO_TYPE7:
358 {
359 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
360 atio->u.isp24.fcp_hdr.d_id);
361 if (unlikely(NULL == host)) {
362 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
363 "qla_target(%d): Received ATIO_TYPE7 "
364 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
365 atio->u.isp24.fcp_hdr.d_id[0],
366 atio->u.isp24.fcp_hdr.d_id[1],
367 atio->u.isp24.fcp_hdr.d_id[2]);
Quinn Tran41dc5292017-01-19 22:28:03 -0800368
369
370 qlt_queue_unknown_atio(vha, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400371 break;
372 }
Quinn Tran41dc5292017-01-19 22:28:03 -0800373 if (unlikely(!list_empty(&vha->unknown_atio_list)))
374 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
375
Quinn Tran2f424b92015-12-17 14:57:07 -0500376 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400377 break;
378 }
379
380 case IMMED_NOTIFY_TYPE:
381 {
382 struct scsi_qla_host *host = vha;
383 struct imm_ntfy_from_isp *entry =
384 (struct imm_ntfy_from_isp *)atio;
385
Quinn Tran82de8022017-06-13 20:47:17 -0700386 qlt_issue_marker(vha, ha_locked);
387
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400388 if ((entry->u.isp24.vp_index != 0xFF) &&
389 (entry->u.isp24.nport_handle != 0xFFFF)) {
390 host = qlt_find_host_by_vp_idx(vha,
391 entry->u.isp24.vp_index);
392 if (unlikely(!host)) {
393 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
394 "qla_target(%d): Received "
395 "ATIO (IMMED_NOTIFY_TYPE) "
396 "with unknown vp_index %d\n",
397 vha->vp_idx, entry->u.isp24.vp_index);
398 break;
399 }
400 }
Quinn Tran2f424b92015-12-17 14:57:07 -0500401 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400402 break;
403 }
404
Quinn Tran41dc5292017-01-19 22:28:03 -0800405 case VP_RPT_ID_IOCB_TYPE:
406 qla24xx_report_id_acquisition(vha,
407 (struct vp_rpt_id_entry_24xx *)atio);
408 break;
409
410 case ABTS_RECV_24XX:
411 {
412 struct abts_recv_from_24xx *entry =
413 (struct abts_recv_from_24xx *)atio;
414 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
415 entry->vp_index);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700416 unsigned long flags;
417
Quinn Tran41dc5292017-01-19 22:28:03 -0800418 if (unlikely(!host)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700419 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
Quinn Tran41dc5292017-01-19 22:28:03 -0800420 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
421 "received, with unknown vp_index %d\n",
422 vha->vp_idx, entry->vp_index);
423 break;
424 }
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700425 if (!ha_locked)
426 spin_lock_irqsave(&host->hw->hardware_lock, flags);
427 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
428 if (!ha_locked)
429 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -0800430 break;
Quinn Tran41dc5292017-01-19 22:28:03 -0800431 }
432
433 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
434
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400435 default:
436 ql_dbg(ql_dbg_tgt, vha, 0xe040,
437 "qla_target(%d): Received unknown ATIO atio "
438 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
439 break;
440 }
441
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500442 return false;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400443}
444
Quinn Tran82de8022017-06-13 20:47:17 -0700445void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
446 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400447{
448 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -0400449 case CTIO_CRC2:
450 ql_dbg(ql_dbg_tgt, vha, 0xe073,
451 "qla_target(%d):%s: CRC2 Response pkt\n",
452 vha->vp_idx, __func__);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400453 case CTIO_TYPE7:
454 {
455 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
456 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
457 entry->vp_index);
458 if (unlikely(!host)) {
459 ql_dbg(ql_dbg_tgt, vha, 0xe041,
460 "qla_target(%d): Response pkt (CTIO_TYPE7) "
461 "received, with unknown vp_index %d\n",
462 vha->vp_idx, entry->vp_index);
463 break;
464 }
Quinn Tran82de8022017-06-13 20:47:17 -0700465 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400466 break;
467 }
468
469 case IMMED_NOTIFY_TYPE:
470 {
471 struct scsi_qla_host *host = vha;
472 struct imm_ntfy_from_isp *entry =
473 (struct imm_ntfy_from_isp *)pkt;
474
475 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
476 if (unlikely(!host)) {
477 ql_dbg(ql_dbg_tgt, vha, 0xe042,
478 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
479 "received, with unknown vp_index %d\n",
480 vha->vp_idx, entry->u.isp24.vp_index);
481 break;
482 }
Quinn Tran82de8022017-06-13 20:47:17 -0700483 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400484 break;
485 }
486
487 case NOTIFY_ACK_TYPE:
488 {
489 struct scsi_qla_host *host = vha;
490 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
491
492 if (0xFF != entry->u.isp24.vp_index) {
493 host = qlt_find_host_by_vp_idx(vha,
494 entry->u.isp24.vp_index);
495 if (unlikely(!host)) {
496 ql_dbg(ql_dbg_tgt, vha, 0xe043,
497 "qla_target(%d): Response "
498 "pkt (NOTIFY_ACK_TYPE) "
499 "received, with unknown "
500 "vp_index %d\n", vha->vp_idx,
501 entry->u.isp24.vp_index);
502 break;
503 }
504 }
Quinn Tran82de8022017-06-13 20:47:17 -0700505 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400506 break;
507 }
508
509 case ABTS_RECV_24XX:
510 {
511 struct abts_recv_from_24xx *entry =
512 (struct abts_recv_from_24xx *)pkt;
513 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
514 entry->vp_index);
515 if (unlikely(!host)) {
516 ql_dbg(ql_dbg_tgt, vha, 0xe044,
517 "qla_target(%d): Response pkt "
518 "(ABTS_RECV_24XX) received, with unknown "
519 "vp_index %d\n", vha->vp_idx, entry->vp_index);
520 break;
521 }
Quinn Tran82de8022017-06-13 20:47:17 -0700522 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400523 break;
524 }
525
526 case ABTS_RESP_24XX:
527 {
528 struct abts_resp_to_24xx *entry =
529 (struct abts_resp_to_24xx *)pkt;
530 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
531 entry->vp_index);
532 if (unlikely(!host)) {
533 ql_dbg(ql_dbg_tgt, vha, 0xe045,
534 "qla_target(%d): Response pkt "
535 "(ABTS_RECV_24XX) received, with unknown "
536 "vp_index %d\n", vha->vp_idx, entry->vp_index);
537 break;
538 }
Quinn Tran82de8022017-06-13 20:47:17 -0700539 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400540 break;
541 }
542
543 default:
Quinn Tran82de8022017-06-13 20:47:17 -0700544 qlt_response_pkt(vha, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400545 break;
546 }
547
548}
549
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500550/*
551 * All qlt_plogi_ack_t operations are protected by hardware_lock
552 */
Quinn Tran726b8542017-01-19 22:28:00 -0800553static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
554 struct imm_ntfy_from_isp *ntfy, int type)
555{
556 struct qla_work_evt *e;
557 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
558 if (!e)
559 return QLA_FUNCTION_FAILED;
560
561 e->u.nack.fcport = fcport;
562 e->u.nack.type = type;
563 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
564 return qla2x00_post_work(vha, e);
565}
566
567static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800568void qla2x00_async_nack_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800569{
Quinn Tran726b8542017-01-19 22:28:00 -0800570 struct srb *sp = (struct srb *)s;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800571 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800572 unsigned long flags;
573
Quinn Tran83548fe2017-06-02 09:12:01 -0700574 ql_dbg(ql_dbg_disc, vha, 0x20f2,
575 "Async done-%s res %x %8phC type %d\n",
576 sp->name, res, sp->fcport->port_name, sp->type);
Quinn Tran726b8542017-01-19 22:28:00 -0800577
578 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
579 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -0700580 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran726b8542017-01-19 22:28:00 -0800581
582 switch (sp->type) {
583 case SRB_NACK_PLOGI:
584 sp->fcport->login_gen++;
585 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
586 sp->fcport->logout_on_delete = 1;
Quinn Tran5b334692017-03-15 09:48:48 -0700587 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
Quinn Tran726b8542017-01-19 22:28:00 -0800588 break;
589
590 case SRB_NACK_PRLI:
591 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
592 sp->fcport->deleted = 0;
593
594 if (!sp->fcport->login_succ &&
595 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
596 sp->fcport->login_succ = 1;
597
598 vha->fcport_count++;
599
600 if (!IS_IIDMA_CAPABLE(vha->hw) ||
601 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700602 ql_dbg(ql_dbg_disc, vha, 0x20f3,
603 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
604 __func__, __LINE__,
605 sp->fcport->port_name,
606 vha->fcport_count);
Quinn Tran726b8542017-01-19 22:28:00 -0800607
608 qla24xx_post_upd_fcport_work(vha, sp->fcport);
609 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700610 ql_dbg(ql_dbg_disc, vha, 0x20f5,
611 "%s %d %8phC post gpsc fcp_cnt %d\n",
612 __func__, __LINE__,
613 sp->fcport->port_name,
614 vha->fcport_count);
Quinn Tran726b8542017-01-19 22:28:00 -0800615
616 qla24xx_post_gpsc_work(vha, sp->fcport);
617 }
618 }
619 break;
620
621 case SRB_NACK_LOGO:
622 sp->fcport->login_gen++;
623 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
624 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
625 break;
626 }
627 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
628
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800629 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800630}
631
632int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
633 struct imm_ntfy_from_isp *ntfy, int type)
634{
635 int rval = QLA_FUNCTION_FAILED;
636 srb_t *sp;
637 char *c = NULL;
638
639 fcport->flags |= FCF_ASYNC_SENT;
640 switch (type) {
641 case SRB_NACK_PLOGI:
642 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
643 c = "PLOGI";
644 break;
645 case SRB_NACK_PRLI:
646 fcport->fw_login_state = DSC_LS_PRLI_PEND;
Quinn Tranec7193e2017-03-15 09:48:55 -0700647 fcport->deleted = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800648 c = "PRLI";
649 break;
650 case SRB_NACK_LOGO:
651 fcport->fw_login_state = DSC_LS_LOGO_PEND;
652 c = "LOGO";
653 break;
654 }
655
656 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
657 if (!sp)
658 goto done;
659
660 sp->type = type;
661 sp->name = "nack";
662
663 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
664
665 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
666
667 sp->done = qla2x00_async_nack_sp_done;
668
669 rval = qla2x00_start_sp(sp);
670 if (rval != QLA_SUCCESS)
671 goto done_free_sp;
672
Quinn Tran83548fe2017-06-02 09:12:01 -0700673 ql_dbg(ql_dbg_disc, vha, 0x20f4,
674 "Async-%s %8phC hndl %x %s\n",
675 sp->name, fcport->port_name, sp->handle, c);
Quinn Tran726b8542017-01-19 22:28:00 -0800676
677 return rval;
678
679done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800680 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800681done:
682 fcport->flags &= ~FCF_ASYNC_SENT;
683 return rval;
684}
685
686void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
687{
688 fc_port_t *t;
689 unsigned long flags;
690
691 switch (e->u.nack.type) {
692 case SRB_NACK_PRLI:
693 mutex_lock(&vha->vha_tgt.tgt_mutex);
694 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
695 mutex_unlock(&vha->vha_tgt.tgt_mutex);
696 if (t) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700697 ql_log(ql_log_info, vha, 0xd034,
Quinn Tran726b8542017-01-19 22:28:00 -0800698 "%s create sess success %p", __func__, t);
699 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
700 /* create sess has an extra kref */
701 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
702 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
703 }
704 break;
705 }
706 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
707 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
708}
709
710void qla24xx_delete_sess_fn(struct work_struct *work)
711{
712 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
713 struct qla_hw_data *ha = fcport->vha->hw;
714 unsigned long flags;
715
716 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
717
718 if (fcport->se_sess) {
719 ha->tgt.tgt_ops->shutdown_sess(fcport);
720 ha->tgt.tgt_ops->put_sess(fcport);
721 } else {
722 qlt_unreg_sess(fcport);
723 }
724 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
725}
726
727/*
728 * Called from qla2x00_reg_remote_port()
729 */
730void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
731{
732 struct qla_hw_data *ha = vha->hw;
733 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
734 struct fc_port *sess = fcport;
735 unsigned long flags;
736
737 if (!vha->hw->tgt.tgt_ops)
738 return;
739
740 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
741 if (tgt->tgt_stop) {
742 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
743 return;
744 }
745
746 if (fcport->disc_state == DSC_DELETE_PEND) {
747 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
748 return;
749 }
750
751 if (!sess->se_sess) {
752 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
753
754 mutex_lock(&vha->vha_tgt.tgt_mutex);
755 sess = qlt_create_sess(vha, fcport, false);
756 mutex_unlock(&vha->vha_tgt.tgt_mutex);
757
758 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
759 } else {
760 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
761 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
762 return;
763 }
764
765 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700766 ql_dbg(ql_dbg_disc, vha, 0x2107,
Quinn Tran726b8542017-01-19 22:28:00 -0800767 "%s: kref_get fail sess %8phC \n",
768 __func__, sess->port_name);
769 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
770 return;
771 }
772
773 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
774 "qla_target(%u): %ssession for port %8phC "
775 "(loop ID %d) reappeared\n", vha->vp_idx,
776 sess->local ? "local " : "", sess->port_name, sess->loop_id);
777
778 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
779 "Reappeared sess %p\n", sess);
780
781 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
782 fcport->loop_id,
783 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
784 }
785
786 if (sess && sess->local) {
787 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
788 "qla_target(%u): local session for "
789 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
790 fcport->port_name, sess->loop_id);
791 sess->local = 0;
792 }
793 ha->tgt.tgt_ops->put_sess(sess);
794 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
795}
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500796
797/*
798 * This is a zero-base ref-counting solution, since hardware_lock
799 * guarantees that ref_count is not modified concurrently.
800 * Upon successful return content of iocb is undefined
801 */
Quinn Tran5d964832017-01-19 22:27:59 -0800802static struct qlt_plogi_ack_t *
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500803qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
804 struct imm_ntfy_from_isp *iocb)
805{
Quinn Tran5d964832017-01-19 22:27:59 -0800806 struct qlt_plogi_ack_t *pla;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500807
808 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
809 if (pla->id.b24 == id->b24) {
810 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
Quinn Tran5d964832017-01-19 22:27:59 -0800811 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500812 return pla;
813 }
814 }
815
816 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
817 if (!pla) {
818 ql_dbg(ql_dbg_async, vha, 0x5088,
819 "qla_target(%d): Allocation of plogi_ack failed\n",
820 vha->vp_idx);
821 return NULL;
822 }
823
Quinn Tran5d964832017-01-19 22:27:59 -0800824 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500825 pla->id = *id;
826 list_add_tail(&pla->list, &vha->plogi_ack_list);
827
828 return pla;
829}
830
Quinn Tran726b8542017-01-19 22:28:00 -0800831void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -0800832 struct qlt_plogi_ack_t *pla)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500833{
Quinn Tran5d964832017-01-19 22:27:59 -0800834 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Quinn Tran726b8542017-01-19 22:28:00 -0800835 port_id_t port_id;
836 uint16_t loop_id;
837 fc_port_t *fcport = pla->fcport;
838
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500839 BUG_ON(!pla->ref_count);
840 pla->ref_count--;
841
842 if (pla->ref_count)
843 return;
844
Quinn Tran726b8542017-01-19 22:28:00 -0800845 ql_dbg(ql_dbg_disc, vha, 0x5089,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500846 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
Quinn Tran5d964832017-01-19 22:27:59 -0800847 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
848 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
849 iocb->u.isp24.port_id[0],
850 le16_to_cpu(iocb->u.isp24.nport_handle),
851 iocb->u.isp24.exchange_address, iocb->ox_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800852
853 port_id.b.domain = iocb->u.isp24.port_id[2];
854 port_id.b.area = iocb->u.isp24.port_id[1];
855 port_id.b.al_pa = iocb->u.isp24.port_id[0];
856 port_id.b.rsvd_1 = 0;
857
858 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
859
860 fcport->loop_id = loop_id;
861 fcport->d_id = port_id;
862 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
863
864 list_for_each_entry(fcport, &vha->vp_fcports, list) {
865 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
866 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
867 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
868 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
869 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500870
871 list_del(&pla->list);
872 kmem_cache_free(qla_tgt_plogi_cachep, pla);
873}
874
Quinn Tran726b8542017-01-19 22:28:00 -0800875void
Quinn Tran5d964832017-01-19 22:27:59 -0800876qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
877 struct fc_port *sess, enum qlt_plogi_link_t link)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500878{
Quinn Tran5d964832017-01-19 22:27:59 -0800879 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500880 /* Inc ref_count first because link might already be pointing at pla */
881 pla->ref_count++;
882
Quinn Tran726b8542017-01-19 22:28:00 -0800883 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
884 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
885 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
886 sess, link, sess->port_name,
887 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
888 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
889 pla->ref_count, pla, link);
890
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500891 if (sess->plogi_link[link])
892 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
893
Quinn Tran726b8542017-01-19 22:28:00 -0800894 if (link == QLT_PLOGI_LINK_SAME_WWN)
895 pla->fcport = sess;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500896
897 sess->plogi_link[link] = pla;
898}
899
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500900typedef struct {
901 /* These fields must be initialized by the caller */
902 port_id_t id;
903 /*
904 * number of cmds dropped while we were waiting for
905 * initiator to ack LOGO initialize to 1 if LOGO is
906 * triggered by a command, otherwise, to 0
907 */
908 int cmd_count;
909
910 /* These fields are used by callee */
911 struct list_head list;
912} qlt_port_logo_t;
913
914static void
915qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
916{
917 qlt_port_logo_t *tmp;
918 int res;
919
920 mutex_lock(&vha->vha_tgt.tgt_mutex);
921
922 list_for_each_entry(tmp, &vha->logo_list, list) {
923 if (tmp->id.b24 == logo->id.b24) {
924 tmp->cmd_count += logo->cmd_count;
925 mutex_unlock(&vha->vha_tgt.tgt_mutex);
926 return;
927 }
928 }
929
930 list_add_tail(&logo->list, &vha->logo_list);
931
932 mutex_unlock(&vha->vha_tgt.tgt_mutex);
933
934 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
935
936 mutex_lock(&vha->vha_tgt.tgt_mutex);
937 list_del(&logo->list);
938 mutex_unlock(&vha->vha_tgt.tgt_mutex);
939
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500940 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
941 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
942 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
943 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500944}
945
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400946static void qlt_free_session_done(struct work_struct *work)
947{
Quinn Tran5d964832017-01-19 22:27:59 -0800948 struct fc_port *sess = container_of(work, struct fc_port,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400949 free_work);
950 struct qla_tgt *tgt = sess->tgt;
951 struct scsi_qla_host *vha = sess->vha;
952 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400953 unsigned long flags;
954 bool logout_started = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800955 struct event_arg ea;
Quinn Tran41dc5292017-01-19 22:28:03 -0800956 scsi_qla_host_t *base_vha;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400957
958 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
959 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500960 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400961 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
Quinn Tran37cacc02017-01-19 22:27:58 -0800962 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400963 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500964 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400965
Quinn Tran726b8542017-01-19 22:28:00 -0800966 if (!IS_SW_RESV_ADDR(sess->d_id)) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800967 if (sess->send_els_logo) {
968 qlt_port_logo_t logo;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500969
Quinn Tran41dc5292017-01-19 22:28:03 -0800970 logo.id = sess->d_id;
971 logo.cmd_count = 0;
972 qlt_send_first_logo(vha, &logo);
973 }
974
975 if (sess->logout_on_delete) {
976 int rc;
977
978 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
979 if (rc != QLA_SUCCESS)
980 ql_log(ql_log_warn, vha, 0xf085,
981 "Schedule logo failed sess %p rc %d\n",
982 sess, rc);
983 else
984 logout_started = true;
985 }
Quinn Tran726b8542017-01-19 22:28:00 -0800986 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400987
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400988 /*
989 * Release the target session for FC Nexus from fabric module code.
990 */
991 if (sess->se_sess != NULL)
992 ha->tgt.tgt_ops->free_session(sess);
993
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400994 if (logout_started) {
995 bool traced = false;
996
997 while (!ACCESS_ONCE(sess->logout_completed)) {
998 if (!traced) {
999 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1000 "%s: waiting for sess %p logout\n",
1001 __func__, sess);
1002 traced = true;
1003 }
1004 msleep(100);
1005 }
1006
Quinn Tran726b8542017-01-19 22:28:00 -08001007 ql_dbg(ql_dbg_disc, vha, 0xf087,
Quinn Tran41dc5292017-01-19 22:28:03 -08001008 "%s: sess %p logout completed\n",__func__, sess);
1009 }
1010
1011 if (sess->logo_ack_needed) {
1012 sess->logo_ack_needed = 0;
1013 qla24xx_async_notify_ack(vha, sess,
1014 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001015 }
1016
Quinn Tran726b8542017-01-19 22:28:00 -08001017 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1018 if (sess->se_sess) {
1019 sess->se_sess = NULL;
1020 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1021 tgt->sess_count--;
1022 }
1023
1024 sess->disc_state = DSC_DELETED;
1025 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1026 sess->deleted = QLA_SESS_DELETED;
1027 sess->login_retry = vha->hw->login_retry_count;
1028
1029 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1030 vha->fcport_count--;
1031 sess->login_succ = 0;
1032 }
1033
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001034 if (sess->chip_reset != ha->base_qpair->chip_reset)
Quinn Tran726b8542017-01-19 22:28:00 -08001035 qla2x00_clear_loop_id(sess);
1036
1037 if (sess->conflict) {
1038 sess->conflict->login_pause = 0;
1039 sess->conflict = NULL;
1040 if (!test_bit(UNLOADING, &vha->dpc_flags))
1041 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1042 }
1043
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001044 {
Quinn Tran5d964832017-01-19 22:27:59 -08001045 struct qlt_plogi_ack_t *own =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001046 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Quinn Tran5d964832017-01-19 22:27:59 -08001047 struct qlt_plogi_ack_t *con =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001048 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
Quinn Tran5d964832017-01-19 22:27:59 -08001049 struct imm_ntfy_from_isp *iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001050
1051 if (con) {
Quinn Tran5d964832017-01-19 22:27:59 -08001052 iocb = &con->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001053 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
Quinn Tran5d964832017-01-19 22:27:59 -08001054 "se_sess %p / sess %p port %8phC is gone,"
1055 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1056 sess->se_sess, sess, sess->port_name,
1057 own ? "releasing own PLOGI" : "no own PLOGI pending",
1058 own ? own->ref_count : -1,
1059 iocb->u.isp24.port_name, con->ref_count);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001060 qlt_plogi_ack_unref(vha, con);
Quinn Tran726b8542017-01-19 22:28:00 -08001061 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001062 } else {
1063 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1064 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1065 sess->se_sess, sess, sess->port_name,
1066 own ? "releasing own PLOGI" :
1067 "no own PLOGI pending",
1068 own ? own->ref_count : -1);
1069 }
1070
Quinn Tran726b8542017-01-19 22:28:00 -08001071 if (own) {
1072 sess->fw_login_state = DSC_LS_PLOGI_PEND;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001073 qlt_plogi_ack_unref(vha, own);
Quinn Tran726b8542017-01-19 22:28:00 -08001074 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1075 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001076 }
Quinn Tran5d964832017-01-19 22:27:59 -08001077 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1078
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001079 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
Quinn Tran726b8542017-01-19 22:28:00 -08001080 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1081 sess, sess->port_name, vha->fcport_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001082
Quinn Tran726b8542017-01-19 22:28:00 -08001083 if (tgt && (tgt->sess_count == 0))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001084 wake_up_all(&tgt->waitQ);
Quinn Tran726b8542017-01-19 22:28:00 -08001085
1086 if (vha->fcport_count == 0)
1087 wake_up_all(&vha->fcport_waitQ);
1088
Quinn Tran41dc5292017-01-19 22:28:03 -08001089 base_vha = pci_get_drvdata(ha->pdev);
1090 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1091 return;
1092
Quinn Tran726b8542017-01-19 22:28:00 -08001093 if (!tgt || !tgt->tgt_stop) {
1094 memset(&ea, 0, sizeof(ea));
1095 ea.event = FCME_DELETE_DONE;
1096 ea.fcport = sess;
1097 qla2x00_fcport_event_handler(vha, &ea);
1098 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001099}
1100
Quinn Tran75601512015-12-17 14:57:04 -05001101/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08001102void qlt_unreg_sess(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001103{
1104 struct scsi_qla_host *vha = sess->vha;
1105
Quinn Tran83548fe2017-06-02 09:12:01 -07001106 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
Quinn Tran5d964832017-01-19 22:27:59 -08001107 "%s sess %p for deletion %8phC\n",
1108 __func__, sess, sess->port_name);
1109
Quinn Tran36c78452016-02-04 11:45:18 -05001110 if (sess->se_sess)
1111 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001112
Quinn Tran5d964832017-01-19 22:27:59 -08001113 qla2x00_mark_device_lost(vha, sess, 1, 1);
1114
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001115 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Tran726b8542017-01-19 22:28:00 -08001116 sess->disc_state = DSC_DELETE_PEND;
1117 sess->last_rscn_gen = sess->rscn_gen;
1118 sess->last_login_gen = sess->login_gen;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001119
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001120 if (sess->nvme_flag & NVME_FLAG_REGISTERED)
1121 schedule_work(&sess->nvme_del_work);
1122
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001123 INIT_WORK(&sess->free_work, qlt_free_session_done);
1124 schedule_work(&sess->free_work);
1125}
Quinn Tran5d964832017-01-19 22:27:59 -08001126EXPORT_SYMBOL(qlt_unreg_sess);
Quinn Tran75601512015-12-17 14:57:04 -05001127
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001128static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1129{
1130 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001131 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001132 uint16_t loop_id;
1133 int res = 0;
1134 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -05001135 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001136
1137 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1138 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001139 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -04001140 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -05001141 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -04001142 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001143 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001144 } else {
Quinn Tran75601512015-12-17 14:57:04 -05001145 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001146 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05001147 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001148 }
1149
1150 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1151 "Using sess for qla_tgt_reset: %p\n", sess);
1152 if (!sess) {
1153 res = -ESRCH;
1154 return res;
1155 }
1156
1157 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001158 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1159 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001160 mcmd, loop_id);
1161
Quinn Tranbb1181c2016-12-23 18:06:05 -08001162 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001163}
1164
Quinn Tran726b8542017-01-19 22:28:00 -08001165static void qla24xx_chk_fcp_state(struct fc_port *sess)
1166{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001167 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
Quinn Tran726b8542017-01-19 22:28:00 -08001168 sess->logout_on_delete = 0;
1169 sess->logo_ack_needed = 0;
1170 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1171 sess->scan_state = 0;
1172 }
1173}
1174
Quinn Tran75601512015-12-17 14:57:04 -05001175/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran726b8542017-01-19 22:28:00 -08001176void qlt_schedule_sess_for_deletion(struct fc_port *sess,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001177 bool immediate)
1178{
1179 struct qla_tgt *tgt = sess->tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001180
Quinn Tran726b8542017-01-19 22:28:00 -08001181 if (sess->disc_state == DSC_DELETE_PEND)
1182 return;
1183
1184 if (sess->disc_state == DSC_DELETED) {
1185 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1186 wake_up_all(&tgt->waitQ);
1187 if (sess->vha->fcport_count == 0)
1188 wake_up_all(&sess->vha->fcport_waitQ);
1189
1190 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1191 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001192 return;
1193 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001194
Quinn Tran726b8542017-01-19 22:28:00 -08001195 sess->disc_state = DSC_DELETE_PEND;
1196
1197 if (sess->deleted == QLA_SESS_DELETED)
1198 sess->logout_on_delete = 0;
1199
1200 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1201 qla24xx_chk_fcp_state(sess);
1202
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001203 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1204 "Scheduling sess %p for deletion\n", sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001205
Quinn Tran726b8542017-01-19 22:28:00 -08001206 schedule_work(&sess->del_work);
1207}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001208
Quinn Tran726b8542017-01-19 22:28:00 -08001209void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1210{
1211 unsigned long flags;
1212 struct qla_hw_data *ha = sess->vha->hw;
1213 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1214 qlt_schedule_sess_for_deletion(sess, 1);
1215 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001216}
1217
Quinn Tran75601512015-12-17 14:57:04 -05001218/* ha->tgt.sess_lock supposed to be held on entry */
Joern Engelc5701042014-09-16 16:23:14 -04001219static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001220{
Quinn Tran5d964832017-01-19 22:27:59 -08001221 struct fc_port *sess;
1222 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001223
Quinn Tran5d964832017-01-19 22:27:59 -08001224 list_for_each_entry(sess, &vha->vp_fcports, list) {
1225 if (sess->se_sess)
Quinn Tran726b8542017-01-19 22:28:00 -08001226 qlt_schedule_sess_for_deletion(sess, 1);
Quinn Tran5d964832017-01-19 22:27:59 -08001227 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001228
1229 /* At this point tgt could be already dead */
1230}
1231
1232static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1233 uint16_t *loop_id)
1234{
1235 struct qla_hw_data *ha = vha->hw;
1236 dma_addr_t gid_list_dma;
1237 struct gid_list_info *gid_list;
1238 char *id_iter;
1239 int res, rc, i;
1240 uint16_t entries;
1241
1242 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1243 &gid_list_dma, GFP_KERNEL);
1244 if (!gid_list) {
1245 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1246 "qla_target(%d): DMA Alloc failed of %u\n",
1247 vha->vp_idx, qla2x00_gid_list_size(ha));
1248 return -ENOMEM;
1249 }
1250
1251 /* Get list of logged in devices */
Quinn Tran15f30a52017-03-15 09:48:52 -07001252 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001253 if (rc != QLA_SUCCESS) {
1254 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1255 "qla_target(%d): get_id_list() failed: %x\n",
1256 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001257 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001258 goto out_free_id_list;
1259 }
1260
1261 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001262 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001263 for (i = 0; i < entries; i++) {
1264 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1265 if ((gid->al_pa == s_id[2]) &&
1266 (gid->area == s_id[1]) &&
1267 (gid->domain == s_id[0])) {
1268 *loop_id = le16_to_cpu(gid->loop_id);
1269 res = 0;
1270 break;
1271 }
1272 id_iter += ha->gid_list_info_size;
1273 }
1274
1275out_free_id_list:
1276 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1277 gid_list, gid_list_dma);
1278 return res;
1279}
1280
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001281/*
1282 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1283 * Caller must put it.
1284 */
Quinn Tran5d964832017-01-19 22:27:59 -08001285static struct fc_port *qlt_create_sess(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001286 struct scsi_qla_host *vha,
1287 fc_port_t *fcport,
1288 bool local)
1289{
1290 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -08001291 struct fc_port *sess = fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001292 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001293
Quinn Tran726b8542017-01-19 22:28:00 -08001294 if (vha->vha_tgt.qla_tgt->tgt_stop)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001295 return NULL;
Quinn Tran726b8542017-01-19 22:28:00 -08001296
1297 if (fcport->se_sess) {
1298 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001299 ql_dbg(ql_dbg_disc, vha, 0x20f6,
Quinn Tran726b8542017-01-19 22:28:00 -08001300 "%s: kref_get_unless_zero failed for %8phC\n",
1301 __func__, sess->port_name);
1302 return NULL;
1303 }
1304 return fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001305 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001306 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001307 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001308
Quinn Tran726b8542017-01-19 22:28:00 -08001309 /*
1310 * Under normal circumstances we want to logout from firmware when
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001311 * session eventually ends and release corresponding nport handle.
1312 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
Quinn Tran726b8542017-01-19 22:28:00 -08001313 * code will adjust these flags as necessary.
1314 */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001315 sess->logout_on_delete = 1;
1316 sess->keep_nport_handle = 0;
Quinn Tran726b8542017-01-19 22:28:00 -08001317 sess->logout_completed = 0;
1318
1319 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1320 &fcport->port_name[0], sess) < 0) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001321 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
Quinn Tran726b8542017-01-19 22:28:00 -08001322 "(%d) %8phC check_initiator_node_acl failed\n",
1323 vha->vp_idx, fcport->port_name);
1324 return NULL;
1325 } else {
1326 kref_init(&fcport->sess_kref);
1327 /*
1328 * Take an extra reference to ->sess_kref here to handle
1329 * fc_port access across ->tgt.sess_lock reaquire.
1330 */
1331 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001332 ql_dbg(ql_dbg_disc, vha, 0x20f7,
Quinn Tran726b8542017-01-19 22:28:00 -08001333 "%s: kref_get_unless_zero failed for %8phC\n",
1334 __func__, sess->port_name);
1335 return NULL;
1336 }
1337
1338 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1339 if (!IS_SW_RESV_ADDR(sess->d_id))
1340 vha->vha_tgt.qla_tgt->sess_count++;
1341
1342 qlt_do_generation_tick(vha, &sess->generation);
1343 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1344 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001345
1346 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
Quinn Tran726b8542017-01-19 22:28:00 -08001347 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1348 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1349 vha->vha_tgt.qla_tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001350
1351 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001352 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1353 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1354 vha->vp_idx, local ? "local " : "", fcport->port_name,
Quinn Tran37cacc02017-01-19 22:27:58 -08001355 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1356 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001357
1358 return sess;
1359}
1360
1361/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001362 * max_gen - specifies maximum session generation
1363 * at which this deletion requestion is still valid
1364 */
1365void
1366qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001367{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001368 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08001369 struct fc_port *sess = fcport;
Quinn Tran75601512015-12-17 14:57:04 -05001370 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001371
1372 if (!vha->hw->tgt.tgt_ops)
1373 return;
1374
Roland Dreierb2032fd2015-07-14 16:00:42 -04001375 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001376 return;
1377
Quinn Tran75601512015-12-17 14:57:04 -05001378 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001379 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001380 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001381 return;
1382 }
Quinn Tran5d964832017-01-19 22:27:59 -08001383 if (!sess->se_sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001384 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001385 return;
1386 }
1387
Alexei Potashnikdf673272015-07-14 16:00:46 -04001388 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001389 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001390 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1391 "Ignoring stale deletion request for se_sess %p / sess %p"
1392 " for port %8phC, req_gen %d, sess_gen %d\n",
1393 sess->se_sess, sess, sess->port_name, max_gen,
1394 sess->generation);
1395 return;
1396 }
1397
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001398 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1399
1400 sess->local = 1;
1401 qlt_schedule_sess_for_deletion(sess, false);
Quinn Tran75601512015-12-17 14:57:04 -05001402 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001403}
1404
1405static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1406{
1407 struct qla_hw_data *ha = tgt->ha;
1408 unsigned long flags;
1409 int res;
1410 /*
1411 * We need to protect against race, when tgt is freed before or
1412 * inside wake_up()
1413 */
Quinn Tran726b8542017-01-19 22:28:00 -08001414 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001415 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
Quinn Tran5d964832017-01-19 22:27:59 -08001416 "tgt %p, sess_count=%d\n",
1417 tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001418 res = (tgt->sess_count == 0);
Quinn Tran726b8542017-01-19 22:28:00 -08001419 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001420
1421 return res;
1422}
1423
1424/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001425int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001426{
1427 struct scsi_qla_host *vha = tgt->vha;
1428 struct qla_hw_data *ha = tgt->ha;
1429 unsigned long flags;
1430
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001431 mutex_lock(&qla_tgt_mutex);
1432 if (!vha->fc_vport) {
1433 struct Scsi_Host *sh = vha->host;
1434 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1435 bool npiv_vports;
1436
1437 spin_lock_irqsave(sh->host_lock, flags);
1438 npiv_vports = (fc_host->npiv_vports_inuse);
1439 spin_unlock_irqrestore(sh->host_lock, flags);
1440
1441 if (npiv_vports) {
1442 mutex_unlock(&qla_tgt_mutex);
Quinn Tran3a33dc92017-06-02 09:12:04 -07001443 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1444 "NPIV is in use. Can not stop target\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001445 return -EPERM;
1446 }
1447 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001448 if (tgt->tgt_stop || tgt->tgt_stopped) {
1449 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1450 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001451 mutex_unlock(&qla_tgt_mutex);
1452 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001453 }
1454
Quinn Tran3a33dc92017-06-02 09:12:04 -07001455 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001456 vha->host_no, vha);
1457 /*
1458 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1459 * Lock is needed, because we still can get an incoming packet.
1460 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001461 mutex_lock(&vha->vha_tgt.tgt_mutex);
Quinn Tran75601512015-12-17 14:57:04 -05001462 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001463 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001464 qlt_clear_tgt_db(tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001465 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001466 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001467 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001468
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001469 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1470 "Waiting for sess works (tgt %p)", tgt);
1471 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1472 while (!list_empty(&tgt->sess_works_list)) {
1473 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1474 flush_scheduled_work();
1475 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1476 }
1477 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1478
1479 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001480 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001481
Joe Carnucciob85e0952017-08-23 15:05:11 -07001482 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001483
1484 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001485 if (!ha->flags.host_shutting_down &&
1486 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001487 qlt_disable_vha(vha);
1488
1489 /* Wait for sessions to clear out (just in case) */
Joe Carnucciob85e0952017-08-23 15:05:11 -07001490 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001491 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001492}
1493EXPORT_SYMBOL(qlt_stop_phase1);
1494
1495/* Called by tcm_qla2xxx configfs code */
1496void qlt_stop_phase2(struct qla_tgt *tgt)
1497{
Quinn Tran3a33dc92017-06-02 09:12:04 -07001498 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001499
1500 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001502 "Already in tgt->tgt_stopped state\n");
1503 dump_stack();
1504 return;
1505 }
Quinn Tran3a33dc92017-06-02 09:12:04 -07001506 if (!tgt->tgt_stop) {
1507 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1508 "%s: phase1 stop is not completed\n", __func__);
1509 dump_stack();
1510 return;
1511 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001512
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001513 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001514 tgt->tgt_stop = 0;
1515 tgt->tgt_stopped = 1;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001516 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001517
Quinn Tran3a33dc92017-06-02 09:12:04 -07001518 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001519 tgt);
1520}
1521EXPORT_SYMBOL(qlt_stop_phase2);
1522
1523/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001524static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001525{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001526 scsi_qla_host_t *vha = tgt->vha;
Quinn Trane326d222017-06-13 20:47:18 -07001527 void *node;
1528 u64 key = 0;
1529 u16 i;
1530 struct qla_qpair_hint *h;
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001531 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001532
Quinn Tran3a33dc92017-06-02 09:12:04 -07001533 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1534 !tgt->tgt_stopped)
1535 qlt_stop_phase1(tgt);
1536
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001537 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001538 qlt_stop_phase2(tgt);
1539
Quinn Trane326d222017-06-13 20:47:18 -07001540 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1541 unsigned long flags;
1542
1543 h = &tgt->qphints[i];
1544 if (h->qpair) {
1545 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1546 list_del(&h->hint_elem);
1547 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1548 h->qpair = NULL;
1549 }
1550 }
1551 kfree(tgt->qphints);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001552 mutex_lock(&qla_tgt_mutex);
1553 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1554 mutex_unlock(&qla_tgt_mutex);
Quinn Trane326d222017-06-13 20:47:18 -07001555
1556 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1557 btree_remove64(&tgt->lun_qpair_map, key);
1558
1559 btree_destroy64(&tgt->lun_qpair_map);
1560
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001561 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->remove_target)
1562 ha->tgt.tgt_ops->remove_target(vha);
1563
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001564 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001565
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001566 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001567 "Release of tgt %p finished\n", tgt);
1568
1569 kfree(tgt);
1570}
1571
1572/* ha->hardware_lock supposed to be held on entry */
1573static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1574 const void *param, unsigned int param_size)
1575{
1576 struct qla_tgt_sess_work_param *prm;
1577 unsigned long flags;
1578
1579 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1580 if (!prm) {
1581 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1582 "qla_target(%d): Unable to create session "
1583 "work, command will be refused", 0);
1584 return -ENOMEM;
1585 }
1586
1587 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1588 "Scheduling work (type %d, prm %p)"
1589 " to find session for param %p (size %d, tgt %p)\n",
1590 type, prm, param, param_size, tgt);
1591
1592 prm->type = type;
1593 memcpy(&prm->tm_iocb, param, param_size);
1594
1595 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1596 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1597 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1598
1599 schedule_work(&tgt->sess_work);
1600
1601 return 0;
1602}
1603
1604/*
1605 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1606 */
Quinn Tran82de8022017-06-13 20:47:17 -07001607static void qlt_send_notify_ack(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001608 struct imm_ntfy_from_isp *ntfy,
1609 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1610 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1611{
Quinn Tran82de8022017-06-13 20:47:17 -07001612 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001613 struct qla_hw_data *ha = vha->hw;
1614 request_t *pkt;
1615 struct nack_to_isp *nack;
1616
Quinn Tranec7193e2017-03-15 09:48:55 -07001617 if (!ha->flags.fw_started)
1618 return;
1619
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001620 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1621
Quinn Tran82de8022017-06-13 20:47:17 -07001622 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001623 if (!pkt) {
1624 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1625 "qla_target(%d): %s failed: unable to allocate "
1626 "request packet\n", vha->vp_idx, __func__);
1627 return;
1628 }
1629
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001630 if (vha->vha_tgt.qla_tgt != NULL)
1631 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001632
1633 pkt->entry_type = NOTIFY_ACK_TYPE;
1634 pkt->entry_count = 1;
1635
1636 nack = (struct nack_to_isp *)pkt;
1637 nack->ox_id = ntfy->ox_id;
1638
Quinn Tran726b8542017-01-19 22:28:00 -08001639 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001640 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1641 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1642 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001643 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001644 }
1645 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1646 nack->u.isp24.status = ntfy->u.isp24.status;
1647 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001648 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001649 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1650 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1651 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1652 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1653 nack->u.isp24.srr_reject_code = srr_reject_code;
1654 nack->u.isp24.srr_reject_code_expl = srr_explan;
1655 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1656
1657 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1658 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1659 vha->vp_idx, nack->u.isp24.status);
1660
Himanshu Madhani63163e02014-09-25 06:14:59 -04001661 /* Memory Barrier */
1662 wmb();
Quinn Tran82de8022017-06-13 20:47:17 -07001663 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001664}
1665
1666/*
1667 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1668 */
Quinn Tran82de8022017-06-13 20:47:17 -07001669static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001670 struct abts_recv_from_24xx *abts, uint32_t status,
1671 bool ids_reversed)
1672{
Quinn Tran82de8022017-06-13 20:47:17 -07001673 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001674 struct qla_hw_data *ha = vha->hw;
1675 struct abts_resp_to_24xx *resp;
1676 uint32_t f_ctl;
1677 uint8_t *p;
1678
1679 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1680 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1681 ha, abts, status);
1682
Quinn Tran82de8022017-06-13 20:47:17 -07001683 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1684 NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001685 if (!resp) {
1686 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1687 "qla_target(%d): %s failed: unable to allocate "
1688 "request packet", vha->vp_idx, __func__);
1689 return;
1690 }
1691
1692 resp->entry_type = ABTS_RESP_24XX;
1693 resp->entry_count = 1;
1694 resp->nport_handle = abts->nport_handle;
1695 resp->vp_index = vha->vp_idx;
1696 resp->sof_type = abts->sof_type;
1697 resp->exchange_address = abts->exchange_address;
1698 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001699 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001700 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1701 F_CTL_SEQ_INITIATIVE);
1702 p = (uint8_t *)&f_ctl;
1703 resp->fcp_hdr_le.f_ctl[0] = *p++;
1704 resp->fcp_hdr_le.f_ctl[1] = *p++;
1705 resp->fcp_hdr_le.f_ctl[2] = *p;
1706 if (ids_reversed) {
1707 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1708 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1709 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1710 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1711 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1712 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1713 } else {
1714 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1715 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1716 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1717 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1718 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1719 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1720 }
1721 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1722 if (status == FCP_TMF_CMPL) {
1723 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1724 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1725 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1726 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1727 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1728 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1729 } else {
1730 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1731 resp->payload.ba_rjt.reason_code =
1732 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1733 /* Other bytes are zero */
1734 }
1735
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001736 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001737
Himanshu Madhani63163e02014-09-25 06:14:59 -04001738 /* Memory Barrier */
1739 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07001740 if (qpair->reqq_start_iocbs)
1741 qpair->reqq_start_iocbs(qpair);
1742 else
1743 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001744}
1745
1746/*
1747 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1748 */
1749static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1750 struct abts_resp_from_24xx_fw *entry)
1751{
1752 struct ctio7_to_24xx *ctio;
1753
1754 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1755 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001756
Quinn Tran82de8022017-06-13 20:47:17 -07001757 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1758 vha->hw->base_qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001759 if (ctio == NULL) {
1760 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1761 "qla_target(%d): %s failed: unable to allocate "
1762 "request packet\n", vha->vp_idx, __func__);
1763 return;
1764 }
1765
1766 /*
1767 * We've got on entrance firmware's response on by us generated
1768 * ABTS response. So, in it ID fields are reversed.
1769 */
1770
1771 ctio->entry_type = CTIO_TYPE7;
1772 ctio->entry_count = 1;
1773 ctio->nport_handle = entry->nport_handle;
1774 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001775 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001776 ctio->vp_index = vha->vp_idx;
1777 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1778 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1779 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1780 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001781 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1782 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001783 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001784
Himanshu Madhani63163e02014-09-25 06:14:59 -04001785 /* Memory Barrier */
1786 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001787 qla2x00_start_iocbs(vha, vha->req);
1788
Quinn Tran82de8022017-06-13 20:47:17 -07001789 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1790 (struct abts_recv_from_24xx *)entry,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001791 FCP_TMF_CMPL, true);
1792}
1793
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001794static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1795{
1796 struct qla_tgt_sess_op *op;
1797 struct qla_tgt_cmd *cmd;
Quinn Tran8b631d82017-06-02 09:11:54 -07001798 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001799
Quinn Tran8b631d82017-06-02 09:11:54 -07001800 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001801 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1802 if (tag == op->atio.u.isp24.exchange_addr) {
1803 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001804 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001805 return 1;
1806 }
1807 }
1808
Quinn Tran41dc5292017-01-19 22:28:03 -08001809 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1810 if (tag == op->atio.u.isp24.exchange_addr) {
1811 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001812 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08001813 return 1;
1814 }
1815 }
1816
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001817 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1818 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001819 cmd->aborted = 1;
Quinn Tran8b631d82017-06-02 09:11:54 -07001820 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001821 return 1;
1822 }
1823 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001824 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001825
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001826 return 0;
1827}
1828
1829/* drop cmds for the given lun
1830 * XXX only looks for cmds on the port through which lun reset was recieved
1831 * XXX does not go through the list of other port (which may have cmds
1832 * for the same lun)
1833 */
1834static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001835 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001836{
1837 struct qla_tgt_sess_op *op;
1838 struct qla_tgt_cmd *cmd;
1839 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001840 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001841
1842 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001843 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001844 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1845 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001846 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001847
1848 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1849 op_lun = scsilun_to_int(
1850 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1851 if (op_key == key && op_lun == lun)
1852 op->aborted = true;
1853 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001854
1855 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1856 uint32_t op_key;
1857 u64 op_lun;
1858
1859 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1860 op_lun = scsilun_to_int(
1861 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1862 if (op_key == key && op_lun == lun)
1863 op->aborted = true;
1864 }
1865
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001866 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1867 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001868 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001869
1870 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1871 cmd_lun = scsilun_to_int(
1872 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1873 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001874 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001875 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001876 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001877}
1878
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001879/* ha->hardware_lock supposed to be held on entry */
1880static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08001881 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001882{
1883 struct qla_hw_data *ha = vha->hw;
1884 struct qla_tgt_mgmt_cmd *mcmd;
1885 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001886
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001887 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1888 /* send TASK_ABORT response immediately */
Linus Torvalds48ea2ce2017-07-13 14:27:32 -07001889 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001890 return 0;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001891 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001892
1893 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1894 "qla_target(%d): task abort (tag=%d)\n",
1895 vha->vp_idx, abts->exchange_addr_to_abort);
1896
1897 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1898 if (mcmd == NULL) {
1899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1900 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1901 vha->vp_idx, __func__);
1902 return -ENOMEM;
1903 }
1904 memset(mcmd, 0, sizeof(*mcmd));
1905
1906 mcmd->sess = sess;
1907 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001908 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08001909 mcmd->tmr_func = QLA_TGT_ABTS;
Quinn Tran82de8022017-06-13 20:47:17 -07001910 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07001911 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001912
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001913 /*
1914 * LUN is looked up by target-core internally based on the passed
1915 * abts->exchange_addr_to_abort tag.
1916 */
1917 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, 0, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001918 abts->exchange_addr_to_abort);
1919 if (rc != 0) {
1920 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1921 "qla_target(%d): tgt_ops->handle_tmr()"
1922 " failed: %d", vha->vp_idx, rc);
1923 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1924 return -EFAULT;
1925 }
1926
1927 return 0;
1928}
1929
1930/*
1931 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1932 */
1933static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1934 struct abts_recv_from_24xx *abts)
1935{
1936 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001937 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001938 uint32_t tag = abts->exchange_addr_to_abort;
1939 uint8_t s_id[3];
1940 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001941 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001942
1943 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1944 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1945 "qla_target(%d): ABTS: Abort Sequence not "
1946 "supported\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07001947 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1948 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001949 return;
1950 }
1951
1952 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1953 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1954 "qla_target(%d): ABTS: Unknown Exchange "
1955 "Address received\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07001956 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1957 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001958 return;
1959 }
1960
1961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1962 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1963 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1964 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1965 le32_to_cpu(abts->fcp_hdr_le.parameter));
1966
1967 s_id[0] = abts->fcp_hdr_le.s_id[2];
1968 s_id[1] = abts->fcp_hdr_le.s_id[1];
1969 s_id[2] = abts->fcp_hdr_le.s_id[0];
1970
Quinn Tran75601512015-12-17 14:57:04 -05001971 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001972 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1973 if (!sess) {
1974 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1975 "qla_target(%d): task abort for non-existant session\n",
1976 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001977 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001978 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
Quinn Tran75601512015-12-17 14:57:04 -05001979
1980 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1981
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001982 if (rc != 0) {
Quinn Tran82de8022017-06-13 20:47:17 -07001983 qlt_24xx_send_abts_resp(ha->base_qpair, abts,
1984 FCP_TMF_REJECTED, false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001985 }
1986 return;
1987 }
Quinn Tran75601512015-12-17 14:57:04 -05001988 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1989
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001990
Quinn Tran726b8542017-01-19 22:28:00 -08001991 if (sess->deleted) {
Quinn Tran82de8022017-06-13 20:47:17 -07001992 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1993 false);
Alexei Potashnike52a8b42015-07-14 16:00:48 -04001994 return;
1995 }
1996
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001997 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1998 if (rc != 0) {
1999 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2000 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2001 vha->vp_idx, rc);
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
2008/*
2009 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2010 */
Quinn Tran82de8022017-06-13 20:47:17 -07002011static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002012 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2013{
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002014 struct scsi_qla_host *ha = mcmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002015 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2016 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002017 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002018
2019 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2020 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2021 ha, atio, resp_code);
2022
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002023
Quinn Tran82de8022017-06-13 20:47:17 -07002024 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002025 if (ctio == NULL) {
2026 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2027 "qla_target(%d): %s failed: unable to allocate "
2028 "request packet\n", ha->vp_idx, __func__);
2029 return;
2030 }
2031
2032 ctio->entry_type = CTIO_TYPE7;
2033 ctio->entry_count = 1;
2034 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2035 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002036 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002037 ctio->vp_index = ha->vp_idx;
2038 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2039 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2040 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2041 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002042 temp = (atio->u.isp24.attr << 9)|
2043 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2044 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002045 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2046 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002047 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002048 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2049 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002050 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002051
Himanshu Madhani63163e02014-09-25 06:14:59 -04002052 /* Memory Barrier */
2053 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002054 if (qpair->reqq_start_iocbs)
2055 qpair->reqq_start_iocbs(qpair);
2056 else
2057 qla2x00_start_iocbs(ha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002058}
2059
2060void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2061{
2062 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2063}
2064EXPORT_SYMBOL(qlt_free_mcmd);
2065
Quinn Tranbe251522017-03-15 09:48:49 -07002066/*
2067 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2068 * reacquire
2069 */
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002070void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07002071 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2072{
2073 struct atio_from_isp *atio = &cmd->atio;
2074 struct ctio7_to_24xx *ctio;
2075 uint16_t temp;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002076 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranbe251522017-03-15 09:48:49 -07002077
2078 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2079 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2080 "sense_key=%02x, asc=%02x, ascq=%02x",
2081 vha, atio, scsi_status, sense_key, asc, ascq);
2082
2083 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2084 if (!ctio) {
2085 ql_dbg(ql_dbg_async, vha, 0x3067,
2086 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2087 vha->host_no, __func__);
2088 goto out;
2089 }
2090
2091 ctio->entry_type = CTIO_TYPE7;
2092 ctio->entry_count = 1;
2093 ctio->handle = QLA_TGT_SKIP_HANDLE;
2094 ctio->nport_handle = cmd->sess->loop_id;
2095 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2096 ctio->vp_index = vha->vp_idx;
2097 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2098 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2099 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2100 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002101 temp = (atio->u.isp24.attr << 9) |
2102 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2103 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002104 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2105 ctio->u.status1.ox_id = cpu_to_le16(temp);
2106 ctio->u.status1.scsi_status =
2107 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2108 ctio->u.status1.response_len = cpu_to_le16(18);
2109 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2110
2111 if (ctio->u.status1.residual != 0)
2112 ctio->u.status1.scsi_status |=
2113 cpu_to_le16(SS_RESIDUAL_UNDER);
2114
2115 /* Response code and sense key */
2116 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2117 (&ctio->u.status1.sense_data)[0]);
2118 /* Additional sense length */
2119 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2120 /* ASC and ASCQ */
2121 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2122 (&ctio->u.status1.sense_data)[3]);
2123
2124 /* Memory Barrier */
2125 wmb();
2126
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002127 if (qpair->reqq_start_iocbs)
2128 qpair->reqq_start_iocbs(qpair);
2129 else
2130 qla2x00_start_iocbs(vha, qpair->req);
2131
Quinn Tranbe251522017-03-15 09:48:49 -07002132out:
2133 return;
2134}
2135
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002136/* callback from target fabric module code */
2137void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2138{
2139 struct scsi_qla_host *vha = mcmd->sess->vha;
2140 struct qla_hw_data *ha = vha->hw;
2141 unsigned long flags;
Quinn Tran82de8022017-06-13 20:47:17 -07002142 struct qla_qpair *qpair = mcmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002143
2144 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2145 "TM response mcmd (%p) status %#x state %#x",
2146 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2147
Quinn Tran82de8022017-06-13 20:47:17 -07002148 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002149
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002150 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002151 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002152 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002153 * previous life, just abort the processing.
2154 */
2155 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002156 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2157 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002158 mcmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002159 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002160 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002161 return;
2162 }
2163
Quinn Tran726b8542017-01-19 22:28:00 -08002164 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2165 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
Quinn Tran41dc5292017-01-19 22:28:03 -08002166 ELS_LOGO ||
2167 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2168 ELS_PRLO ||
2169 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2170 ELS_TPRLO) {
Quinn Tran83548fe2017-06-02 09:12:01 -07002171 ql_dbg(ql_dbg_disc, vha, 0x2106,
Quinn Tran726b8542017-01-19 22:28:00 -08002172 "TM response logo %phC status %#x state %#x",
2173 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2174 mcmd->flags);
2175 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2176 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002177 qlt_send_notify_ack(vha->hw->base_qpair,
2178 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
Quinn Tran726b8542017-01-19 22:28:00 -08002179 }
2180 } else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05002181 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Quinn Tran82de8022017-06-13 20:47:17 -07002182 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002183 mcmd->fc_tm_rsp, false);
2184 else
Quinn Tran82de8022017-06-13 20:47:17 -07002185 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002186 mcmd->fc_tm_rsp);
2187 }
2188 /*
2189 * Make the callback for ->free_mcmd() to queue_work() and invoke
2190 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2191 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2192 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2193 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2194 * qlt_xmit_tm_rsp() returns here..
2195 */
2196 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002197 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002198}
2199EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2200
2201/* No locks */
2202static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2203{
2204 struct qla_tgt_cmd *cmd = prm->cmd;
2205
2206 BUG_ON(cmd->sg_cnt == 0);
2207
2208 prm->sg = (struct scatterlist *)cmd->sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002209 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002210 cmd->sg_cnt, cmd->dma_data_direction);
2211 if (unlikely(prm->seg_cnt == 0))
2212 goto out_err;
2213
2214 prm->cmd->sg_mapped = 1;
2215
Quinn Tranf83adb62014-04-11 16:54:43 -04002216 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2217 /*
2218 * If greater than four sg entries then we need to allocate
2219 * the continuation entries
2220 */
Quinn Tranb5399f72017-06-13 20:47:27 -07002221 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
Quinn Tranf83adb62014-04-11 16:54:43 -04002222 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
Quinn Tranb5399f72017-06-13 20:47:27 -07002223 QLA_TGT_DATASEGS_PER_CMD_24XX,
2224 QLA_TGT_DATASEGS_PER_CONT_24XX);
Quinn Tranf83adb62014-04-11 16:54:43 -04002225 } else {
2226 /* DIF */
2227 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2228 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2229 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2230 prm->tot_dsds = prm->seg_cnt;
2231 } else
2232 prm->tot_dsds = prm->seg_cnt;
2233
2234 if (cmd->prot_sg_cnt) {
2235 prm->prot_sg = cmd->prot_sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002236 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
Quinn Tranf83adb62014-04-11 16:54:43 -04002237 cmd->prot_sg, cmd->prot_sg_cnt,
2238 cmd->dma_data_direction);
2239 if (unlikely(prm->prot_seg_cnt == 0))
2240 goto out_err;
2241
2242 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2243 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2244 /* Dif Bundling not support here */
2245 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2246 cmd->blk_sz);
2247 prm->tot_dsds += prm->prot_seg_cnt;
2248 } else
2249 prm->tot_dsds += prm->prot_seg_cnt;
2250 }
2251 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002252
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002253 return 0;
2254
2255out_err:
Quinn Tran22d84722017-06-13 20:47:25 -07002256 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002257 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2258 0, prm->cmd->sg_cnt);
2259 return -1;
2260}
2261
Joern Engelf9b67212014-09-16 16:23:18 -04002262static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002263{
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002264 struct qla_hw_data *ha;
2265 struct qla_qpair *qpair;
Joern Engelf9b67212014-09-16 16:23:18 -04002266 if (!cmd->sg_mapped)
2267 return;
2268
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002269 qpair = cmd->qpair;
2270
2271 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2272 cmd->dma_data_direction);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002273 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002274
2275 if (cmd->prot_sg_cnt)
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002276 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
Quinn Tranf83adb62014-04-11 16:54:43 -04002277 cmd->dma_data_direction);
2278
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002279 if (!cmd->ctx)
2280 return;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002281 ha = vha->hw;
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002282 if (cmd->ctx_dsd_alloced)
2283 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2284
2285 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002286}
2287
Quinn Tran82de8022017-06-13 20:47:17 -07002288static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002289 uint32_t req_cnt)
2290{
Quinn Trand63b3282017-06-02 09:12:07 -07002291 uint32_t cnt;
Quinn Tran82de8022017-06-13 20:47:17 -07002292 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002293
Quinn Tran82de8022017-06-13 20:47:17 -07002294 if (req->cnt < (req_cnt + 2)) {
Quinn Tranaf7bb382017-06-13 20:47:23 -07002295 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2296 RD_REG_DWORD_RELAXED(req->req_q_out));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002297
Quinn Tran82de8022017-06-13 20:47:17 -07002298 if (req->ring_index < cnt)
2299 req->cnt = cnt - req->ring_index;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002300 else
Quinn Tran82de8022017-06-13 20:47:17 -07002301 req->cnt = req->length - (req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002302
Quinn Tran82de8022017-06-13 20:47:17 -07002303 if (unlikely(req->cnt < (req_cnt + 2)))
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002304 return -EAGAIN;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002305 }
2306
Quinn Tran82de8022017-06-13 20:47:17 -07002307 req->cnt -= req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002308
2309 return 0;
2310}
2311
2312/*
2313 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2314 */
Quinn Tran82de8022017-06-13 20:47:17 -07002315static inline void *qlt_get_req_pkt(struct req_que *req)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002316{
2317 /* Adjust ring index. */
Quinn Tran82de8022017-06-13 20:47:17 -07002318 req->ring_index++;
2319 if (req->ring_index == req->length) {
2320 req->ring_index = 0;
2321 req->ring_ptr = req->ring;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002322 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002323 req->ring_ptr++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002324 }
Quinn Tran82de8022017-06-13 20:47:17 -07002325 return (cont_entry_t *)req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002326}
2327
2328/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002329static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002330{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002331 uint32_t h;
Quinn Tranc5419e22017-06-13 20:47:16 -07002332 int index;
2333 uint8_t found = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07002334 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002335
Quinn Tranc5419e22017-06-13 20:47:16 -07002336 h = req->current_outstanding_cmd;
2337
2338 for (index = 1; index < req->num_outstanding_cmds; index++) {
2339 h++;
2340 if (h == req->num_outstanding_cmds)
2341 h = 1;
2342
2343 if (h == QLA_TGT_SKIP_HANDLE)
2344 continue;
2345
2346 if (!req->outstanding_cmds[h]) {
2347 found = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002348 break;
2349 }
Quinn Tranc5419e22017-06-13 20:47:16 -07002350 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002351
Quinn Tranc5419e22017-06-13 20:47:16 -07002352 if (found) {
2353 req->current_outstanding_cmd = h;
2354 } else {
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002355 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2356 "qla_target(%d): Ran out of empty cmd slots\n",
2357 qpair->vha->vp_idx);
Quinn Tranc5419e22017-06-13 20:47:16 -07002358 h = QLA_TGT_NULL_HANDLE;
2359 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002360
2361 return h;
2362}
2363
2364/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002365static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2366 struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002367{
2368 uint32_t h;
2369 struct ctio7_to_24xx *pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002370 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002371 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002372
Quinn Tran82de8022017-06-13 20:47:17 -07002373 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002374 prm->pkt = pkt;
2375 memset(pkt, 0, sizeof(*pkt));
2376
2377 pkt->entry_type = CTIO_TYPE7;
2378 pkt->entry_count = (uint8_t)prm->req_cnt;
Quinn Tran22d84722017-06-13 20:47:25 -07002379 pkt->vp_index = prm->cmd->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002380
Quinn Tran82de8022017-06-13 20:47:17 -07002381 h = qlt_make_handle(qpair);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002382 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2383 /*
2384 * CTIO type 7 from the firmware doesn't provide a way to
2385 * know the initiator's LOOP ID, hence we can't find
2386 * the session and, so, the command.
2387 */
2388 return -EAGAIN;
Quinn Trane326d222017-06-13 20:47:18 -07002389 } else
2390 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002391
Quinn Tran82de8022017-06-13 20:47:17 -07002392 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2393 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2394 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002395 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002396 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2397 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2398 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2399 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002400 temp = atio->u.isp24.attr << 9;
2401 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002402 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2403 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002404 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2405
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002406 return 0;
2407}
2408
2409/*
2410 * ha->hardware_lock supposed to be held on entry. We have already made sure
2411 * that there is sufficient amount of request entries to not drop it.
2412 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002413static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002414{
2415 int cnt;
2416 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002417
2418 /* Build continuation packets */
2419 while (prm->seg_cnt > 0) {
2420 cont_a64_entry_t *cont_pkt64 =
Quinn Tran82de8022017-06-13 20:47:17 -07002421 (cont_a64_entry_t *)qlt_get_req_pkt(
2422 prm->cmd->qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002423
2424 /*
2425 * Make sure that from cont_pkt64 none of
2426 * 64-bit specific fields used for 32-bit
2427 * addressing. Cast to (cont_entry_t *) for
2428 * that.
2429 */
2430
2431 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2432
2433 cont_pkt64->entry_count = 1;
2434 cont_pkt64->sys_define = 0;
2435
Quinn Tran32d29b42017-06-13 20:47:26 -07002436 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2437 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002438
2439 /* Load continuation entry data segments */
2440 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002441 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002442 cnt++, prm->seg_cnt--) {
2443 *dword_ptr++ =
2444 cpu_to_le32(pci_dma_lo32
2445 (sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002446 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2447 (sg_dma_address(prm->sg)));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002448 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2449
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002450 prm->sg = sg_next(prm->sg);
2451 }
2452 }
2453}
2454
2455/*
2456 * ha->hardware_lock supposed to be held on entry. We have already made sure
2457 * that there is sufficient amount of request entries to not drop it.
2458 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002459static void qlt_load_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002460{
2461 int cnt;
2462 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002463 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2464
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002465 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2466
2467 /* Setup packet address segment pointer */
2468 dword_ptr = pkt24->u.status0.dseg_0_address;
2469
2470 /* Set total data segment count */
2471 if (prm->seg_cnt)
2472 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2473
2474 if (prm->seg_cnt == 0) {
2475 /* No data transfer */
2476 *dword_ptr++ = 0;
2477 *dword_ptr = 0;
2478 return;
2479 }
2480
2481 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002482
2483 /* Load command entry data segments */
2484 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002485 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002486 cnt++, prm->seg_cnt--) {
2487 *dword_ptr++ =
2488 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002489
2490 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2491 sg_dma_address(prm->sg)));
2492
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002493 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2494
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002495 prm->sg = sg_next(prm->sg);
2496 }
2497
Quinn Tran32d29b42017-06-13 20:47:26 -07002498 qlt_load_cont_data_segments(prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002499}
2500
2501static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2502{
2503 return cmd->bufflen > 0;
2504}
2505
Quinn Tranbe251522017-03-15 09:48:49 -07002506static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2507{
2508 struct qla_tgt_cmd *cmd;
2509 struct scsi_qla_host *vha;
2510
2511 /* asc 0x10=dif error */
2512 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2513 cmd = prm->cmd;
2514 vha = cmd->vha;
2515 /* ASCQ */
2516 switch (prm->sense_buffer[13]) {
2517 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002518 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002519 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2520 "se_cmd=%p tag[%x]",
2521 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2522 cmd->atio.u.isp24.exchange_addr);
2523 break;
2524 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002525 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002526 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2527 "se_cmd=%p tag[%x]",
2528 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2529 cmd->atio.u.isp24.exchange_addr);
2530 break;
2531 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002532 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002533 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2534 "se_cmd=%p tag[%x]",
2535 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2536 cmd->atio.u.isp24.exchange_addr);
2537 break;
2538 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002539 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002540 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2541 "se_cmd=%p tag[%x]",
2542 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2543 cmd->atio.u.isp24.exchange_addr);
2544 break;
2545 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002546 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002547 }
2548}
2549
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002550/*
2551 * Called without ha->hardware_lock held
2552 */
2553static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2554 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2555 uint32_t *full_req_cnt)
2556{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002557 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Tran22d84722017-06-13 20:47:25 -07002558 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002559
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002560 prm->cmd = cmd;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002561 prm->tgt = cmd->tgt;
2562 prm->pkt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002563 prm->rq_result = scsi_status;
2564 prm->sense_buffer = &cmd->sense_buffer[0];
2565 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2566 prm->sg = NULL;
2567 prm->seg_cnt = -1;
2568 prm->req_cnt = 1;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002569 prm->residual = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002570 prm->add_status_pkt = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002571 prm->prot_sg = NULL;
2572 prm->prot_seg_cnt = 0;
2573 prm->tot_dsds = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002574
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002575 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2576 if (qlt_pci_map_calc_cnt(prm) != 0)
2577 return -EAGAIN;
2578 }
2579
2580 *full_req_cnt = prm->req_cnt;
2581
2582 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2583 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002584 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002585 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2586 prm->residual, se_cmd->tag,
2587 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2588 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002589 prm->rq_result |= SS_RESIDUAL_UNDER;
2590 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2591 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002592 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002593 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2594 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2595 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002596 prm->rq_result |= SS_RESIDUAL_OVER;
2597 }
2598
2599 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2600 /*
2601 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2602 * ignored in *xmit_response() below
2603 */
2604 if (qlt_has_data(cmd)) {
2605 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002606 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002607 (prm->rq_result != 0))) {
2608 prm->add_status_pkt = 1;
2609 (*full_req_cnt)++;
2610 }
2611 }
2612 }
2613
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002614 return 0;
2615}
2616
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002617static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2618 int sending_sense)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002619{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002620 if (cmd->qpair->enable_class_2)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002621 return 0;
2622
2623 if (sending_sense)
2624 return cmd->conf_compl_supported;
2625 else
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002626 return cmd->qpair->enable_explicit_conf &&
2627 cmd->conf_compl_supported;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002628}
2629
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002630static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2631 struct qla_tgt_prm *prm)
2632{
2633 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2634 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002635 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002636 if (qlt_need_explicit_conf(prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002637 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002638 CTIO7_FLAGS_EXPLICIT_CONFORM |
2639 CTIO7_FLAGS_CONFORM_REQ);
2640 }
2641 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2642 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2643 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2644 int i;
2645
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002646 if (qlt_need_explicit_conf(prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002647 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Quinn Tran22d84722017-06-13 20:47:25 -07002648 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002649 "Skipping EXPLICIT_CONFORM and "
2650 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2651 "non GOOD status\n");
2652 goto skip_explict_conf;
2653 }
Bart Van Asschead950362015-07-09 07:24:08 -07002654 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002655 CTIO7_FLAGS_EXPLICIT_CONFORM |
2656 CTIO7_FLAGS_CONFORM_REQ);
2657 }
2658skip_explict_conf:
2659 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002660 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002661 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002662 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002663 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002664 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002665 ctio->u.status1.sense_length =
2666 cpu_to_le16(prm->sense_buffer_len);
2667 for (i = 0; i < prm->sense_buffer_len/4; i++)
2668 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2669 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002670
2671 qlt_print_dif_err(prm);
2672
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002673 } else {
2674 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002675 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002676 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002677 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002678 ctio->u.status1.sense_length = 0;
2679 memset(ctio->u.status1.sense_data, 0,
2680 sizeof(ctio->u.status1.sense_data));
2681 }
2682
2683 /* Sense with len > 24, is it possible ??? */
2684}
2685
Quinn Tranf83adb62014-04-11 16:54:43 -04002686static inline int
2687qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2688{
Quinn Tranf83adb62014-04-11 16:54:43 -04002689 switch (se_cmd->prot_op) {
2690 case TARGET_PROT_DOUT_INSERT:
2691 case TARGET_PROT_DIN_STRIP:
2692 if (ql2xenablehba_err_chk >= 1)
2693 return 1;
2694 break;
2695 case TARGET_PROT_DOUT_PASS:
2696 case TARGET_PROT_DIN_PASS:
2697 if (ql2xenablehba_err_chk >= 2)
2698 return 1;
2699 break;
2700 case TARGET_PROT_DIN_INSERT:
2701 case TARGET_PROT_DOUT_STRIP:
2702 return 1;
2703 default:
2704 break;
2705 }
2706 return 0;
2707}
2708
Quinn Tranbe251522017-03-15 09:48:49 -07002709static inline int
2710qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002711{
Quinn Tranbe251522017-03-15 09:48:49 -07002712 switch (se_cmd->prot_op) {
2713 case TARGET_PROT_DIN_INSERT:
2714 case TARGET_PROT_DOUT_INSERT:
2715 case TARGET_PROT_DIN_STRIP:
2716 case TARGET_PROT_DOUT_STRIP:
2717 case TARGET_PROT_DIN_PASS:
2718 case TARGET_PROT_DOUT_PASS:
2719 return 1;
2720 default:
2721 return 0;
2722 }
2723 return 0;
2724}
Quinn Tranf83adb62014-04-11 16:54:43 -04002725
Quinn Tranbe251522017-03-15 09:48:49 -07002726/*
2727 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2728 */
2729static void
2730qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2731 uint16_t *pfw_prot_opts)
2732{
2733 struct se_cmd *se_cmd = &cmd->se_cmd;
2734 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2735 scsi_qla_host_t *vha = cmd->tgt->vha;
2736 struct qla_hw_data *ha = vha->hw;
2737 uint32_t t32 = 0;
2738
2739 /*
2740 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002741 * have been immplemented by TCM, before AppTag is avail.
2742 * Look for modesense_handlers[]
2743 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002744 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002745 ctx->app_tag_mask[0] = 0x0;
2746 ctx->app_tag_mask[1] = 0x0;
2747
Quinn Tranbe251522017-03-15 09:48:49 -07002748 if (IS_PI_UNINIT_CAPABLE(ha)) {
2749 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2750 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2751 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2752 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2753 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2754 }
2755
2756 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2757
Quinn Tranf83adb62014-04-11 16:54:43 -04002758 switch (se_cmd->prot_type) {
2759 case TARGET_DIF_TYPE0_PROT:
2760 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002761 * No check for ql2xenablehba_err_chk, as it
2762 * would be an I/O error if hba tag generation
2763 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002764 */
2765 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002766 /* enable ALL bytes of the ref tag */
2767 ctx->ref_tag_mask[0] = 0xff;
2768 ctx->ref_tag_mask[1] = 0xff;
2769 ctx->ref_tag_mask[2] = 0xff;
2770 ctx->ref_tag_mask[3] = 0xff;
2771 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002772 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002773 /*
2774 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2775 * REF tag, and 16 bit app tag.
2776 */
2777 ctx->ref_tag = cpu_to_le32(lba);
2778 if (!qla_tgt_ref_mask_check(se_cmd) ||
2779 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2780 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2781 break;
2782 }
2783 /* enable ALL bytes of the ref tag */
2784 ctx->ref_tag_mask[0] = 0xff;
2785 ctx->ref_tag_mask[1] = 0xff;
2786 ctx->ref_tag_mask[2] = 0xff;
2787 ctx->ref_tag_mask[3] = 0xff;
2788 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002789 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002790 /*
2791 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2792 * tag has to match LBA in CDB + N
2793 */
2794 ctx->ref_tag = cpu_to_le32(lba);
2795 if (!qla_tgt_ref_mask_check(se_cmd) ||
2796 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2797 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2798 break;
2799 }
2800 /* enable ALL bytes of the ref tag */
2801 ctx->ref_tag_mask[0] = 0xff;
2802 ctx->ref_tag_mask[1] = 0xff;
2803 ctx->ref_tag_mask[2] = 0xff;
2804 ctx->ref_tag_mask[3] = 0xff;
2805 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002806 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002807 /* For TYPE 3 protection: 16 bit GUARD only */
2808 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2809 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2810 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2811 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002812 }
2813}
2814
Quinn Tranf83adb62014-04-11 16:54:43 -04002815static inline int
Quinn Tran82de8022017-06-13 20:47:17 -07002816qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
Quinn Tranf83adb62014-04-11 16:54:43 -04002817{
2818 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002819 uint32_t transfer_length = 0;
2820 uint32_t data_bytes;
2821 uint32_t dif_bytes;
2822 uint8_t bundling = 1;
2823 uint8_t *clr_ptr;
2824 struct crc_context *crc_ctx_pkt = NULL;
2825 struct qla_hw_data *ha;
2826 struct ctio_crc2_to_fw *pkt;
2827 dma_addr_t crc_ctx_dma;
2828 uint16_t fw_prot_opts = 0;
2829 struct qla_tgt_cmd *cmd = prm->cmd;
2830 struct se_cmd *se_cmd = &cmd->se_cmd;
2831 uint32_t h;
2832 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07002833 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002834 uint16_t t16;
Quinn Tran82de8022017-06-13 20:47:17 -07002835 scsi_qla_host_t *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04002836
Quinn Tranf83adb62014-04-11 16:54:43 -04002837 ha = vha->hw;
2838
Quinn Tran82de8022017-06-13 20:47:17 -07002839 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
Quinn Tranf83adb62014-04-11 16:54:43 -04002840 prm->pkt = pkt;
2841 memset(pkt, 0, sizeof(*pkt));
2842
Quinn Tran22d84722017-06-13 20:47:25 -07002843 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
Quinn Tranf83adb62014-04-11 16:54:43 -04002844 "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 -07002845 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
Quinn Tranf83adb62014-04-11 16:54:43 -04002846 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2847
2848 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2849 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2850 bundling = 0;
2851
2852 /* Compute dif len and adjust data len to incude protection */
2853 data_bytes = cmd->bufflen;
2854 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2855
2856 switch (se_cmd->prot_op) {
2857 case TARGET_PROT_DIN_INSERT:
2858 case TARGET_PROT_DOUT_STRIP:
2859 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07002860 if (cmd->prot_sg_cnt)
2861 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04002862 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002863 case TARGET_PROT_DIN_STRIP:
2864 case TARGET_PROT_DOUT_INSERT:
2865 case TARGET_PROT_DIN_PASS:
2866 case TARGET_PROT_DOUT_PASS:
2867 transfer_length = data_bytes + dif_bytes;
2868 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002869 default:
2870 BUG();
2871 break;
2872 }
2873
2874 if (!qlt_hba_err_chk_enabled(se_cmd))
2875 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2876 /* HBA error checking enabled */
2877 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2878 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2879 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2880 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2881 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2882 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2883 }
2884
2885 switch (se_cmd->prot_op) {
2886 case TARGET_PROT_DIN_INSERT:
2887 case TARGET_PROT_DOUT_INSERT:
2888 fw_prot_opts |= PO_MODE_DIF_INSERT;
2889 break;
2890 case TARGET_PROT_DIN_STRIP:
2891 case TARGET_PROT_DOUT_STRIP:
2892 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2893 break;
2894 case TARGET_PROT_DIN_PASS:
2895 case TARGET_PROT_DOUT_PASS:
2896 fw_prot_opts |= PO_MODE_DIF_PASS;
2897 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2898 break;
2899 default:/* Normal Request */
2900 fw_prot_opts |= PO_MODE_DIF_PASS;
2901 break;
2902 }
2903
Quinn Tranf83adb62014-04-11 16:54:43 -04002904 /* ---- PKT ---- */
2905 /* Update entry type to indicate Command Type CRC_2 IOCB */
2906 pkt->entry_type = CTIO_CRC2;
2907 pkt->entry_count = 1;
Quinn Tran22d84722017-06-13 20:47:25 -07002908 pkt->vp_index = cmd->vp_idx;
Quinn Tranf83adb62014-04-11 16:54:43 -04002909
Quinn Tran82de8022017-06-13 20:47:17 -07002910 h = qlt_make_handle(qpair);
Quinn Tranf83adb62014-04-11 16:54:43 -04002911 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2912 /*
2913 * CTIO type 7 from the firmware doesn't provide a way to
2914 * know the initiator's LOOP ID, hence we can't find
2915 * the session and, so, the command.
2916 */
2917 return -EAGAIN;
2918 } else
Quinn Tran82de8022017-06-13 20:47:17 -07002919 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002920
Quinn Tran82de8022017-06-13 20:47:17 -07002921 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2922 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07002923 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002924 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002925 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2926 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2927 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2928 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002929
2930 /* silence compile warning */
2931 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2932 pkt->ox_id = cpu_to_le16(t16);
2933
2934 t16 = (atio->u.isp24.attr << 9);
2935 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002936 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2937
2938 /* Set transfer direction */
2939 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002940 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002941 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002942 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002943
Quinn Tranf83adb62014-04-11 16:54:43 -04002944 pkt->dseg_count = prm->tot_dsds;
2945 /* Fibre channel byte count */
2946 pkt->transfer_length = cpu_to_le32(transfer_length);
2947
Quinn Tranf83adb62014-04-11 16:54:43 -04002948 /* ----- CRC context -------- */
2949
2950 /* Allocate CRC context from global pool */
2951 crc_ctx_pkt = cmd->ctx =
2952 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2953
2954 if (!crc_ctx_pkt)
2955 goto crc_queuing_error;
2956
2957 /* Zero out CTX area. */
2958 clr_ptr = (uint8_t *)crc_ctx_pkt;
2959 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2960
2961 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2962 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2963
2964 /* Set handle */
2965 crc_ctx_pkt->handle = pkt->handle;
2966
Quinn Tranbe251522017-03-15 09:48:49 -07002967 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04002968
2969 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2970 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2971 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2972
Quinn Tranf83adb62014-04-11 16:54:43 -04002973 if (!bundling) {
2974 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2975 } else {
2976 /*
2977 * Configure Bundling if we need to fetch interlaving
2978 * protection PCI accesses
2979 */
2980 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2981 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2982 crc_ctx_pkt->u.bundling.dseg_count =
2983 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2984 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2985 }
2986
2987 /* Finish the common fields of CRC pkt */
2988 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2989 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2990 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07002991 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002992
Quinn Tranbe251522017-03-15 09:48:49 -07002993 memset((uint8_t *)&tc, 0 , sizeof(tc));
2994 tc.vha = vha;
2995 tc.blk_sz = cmd->blk_sz;
2996 tc.bufflen = cmd->bufflen;
2997 tc.sg = cmd->sg;
2998 tc.prot_sg = cmd->prot_sg;
2999 tc.ctx = crc_ctx_pkt;
3000 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04003001
3002 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07003003 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04003004
3005 if (!bundling && prm->prot_seg_cnt) {
3006 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003007 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003008 goto crc_queuing_error;
3009 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003010 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003011 goto crc_queuing_error;
3012
3013 if (bundling && prm->prot_seg_cnt) {
3014 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003015 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04003016
3017 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3018 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003019 prm->prot_seg_cnt, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003020 goto crc_queuing_error;
3021 }
3022 return QLA_SUCCESS;
3023
3024crc_queuing_error:
3025 /* Cleanup will be performed by the caller */
Quinn Tran82de8022017-06-13 20:47:17 -07003026 qpair->req->outstanding_cmds[h] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003027
3028 return QLA_FUNCTION_FAILED;
3029}
3030
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003031/*
3032 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3033 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3034 */
3035int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3036 uint8_t scsi_status)
3037{
3038 struct scsi_qla_host *vha = cmd->vha;
Quinn Tran82de8022017-06-13 20:47:17 -07003039 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003040 struct ctio7_to_24xx *pkt;
3041 struct qla_tgt_prm prm;
3042 uint32_t full_req_cnt = 0;
3043 unsigned long flags = 0;
3044 int res;
3045
Quinn Tran726b8542017-01-19 22:28:00 -08003046 if (cmd->sess && cmd->sess->deleted) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003047 cmd->state = QLA_TGT_STATE_PROCESSED;
3048 if (cmd->sess->logout_completed)
3049 /* no need to terminate. FW already freed exchange. */
3050 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3051 else
Quinn Tran82de8022017-06-13 20:47:17 -07003052 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003053 return 0;
3054 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003055
Quinn Tran22d84722017-06-13 20:47:25 -07003056 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
Quinn Tran82de8022017-06-13 20:47:17 -07003057 "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 -04003058 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3059 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
Quinn Tran82de8022017-06-13 20:47:17 -07003060 &cmd->se_cmd, qpair->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003061
3062 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3063 &full_req_cnt);
3064 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003065 return res;
3066 }
3067
Quinn Tran82de8022017-06-13 20:47:17 -07003068 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003069
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003070 if (xmit_type == QLA_TGT_XMIT_STATUS)
Quinn Tran60a9ead2017-06-13 20:47:28 -07003071 qpair->tgt_counters.core_qla_snd_status++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003072 else
Quinn Tran60a9ead2017-06-13 20:47:28 -07003073 qpair->tgt_counters.core_qla_que_buf++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003074
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003075 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003076 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003077 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003078 * previous life, just abort the processing.
3079 */
3080 cmd->state = QLA_TGT_STATE_PROCESSED;
3081 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003082 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003083 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3084 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003085 cmd->reset_count, qpair->chip_reset);
Quinn Tran82de8022017-06-13 20:47:17 -07003086 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04003087 return 0;
3088 }
3089
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003090 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003091 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003092 if (unlikely(res))
3093 goto out_unmap_unlock;
3094
Quinn Tranf83adb62014-04-11 16:54:43 -04003095 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
Quinn Tran82de8022017-06-13 20:47:17 -07003096 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003097 else
Quinn Tran82de8022017-06-13 20:47:17 -07003098 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Quinn Tran810e30b2015-06-10 11:05:20 -04003099 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003100 qpair->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003101 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003102 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003103
3104 pkt = (struct ctio7_to_24xx *)prm.pkt;
3105
3106 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3107 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003108 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003109 CTIO7_FLAGS_STATUS_MODE_0);
3110
Quinn Tranf83adb62014-04-11 16:54:43 -04003111 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003112 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003113
3114 if (prm.add_status_pkt == 0) {
3115 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3116 pkt->u.status0.scsi_status =
3117 cpu_to_le16(prm.rq_result);
3118 pkt->u.status0.residual =
3119 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003120 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003121 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003122 if (qlt_need_explicit_conf(cmd, 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003123 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003124 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003125 CTIO7_FLAGS_EXPLICIT_CONFORM |
3126 CTIO7_FLAGS_CONFORM_REQ);
3127 }
3128 }
3129
3130 } else {
3131 /*
3132 * We have already made sure that there is sufficient
3133 * amount of request entries to not drop HW lock in
3134 * req_pkt().
3135 */
3136 struct ctio7_to_24xx *ctio =
Quinn Tran82de8022017-06-13 20:47:17 -07003137 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3138 qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003139
Quinn Tran22d84722017-06-13 20:47:25 -07003140 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
Arun Easi667024a2014-09-25 06:14:47 -04003141 "Building additional status packet 0x%p.\n",
3142 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003143
Quinn Tranf83adb62014-04-11 16:54:43 -04003144 /*
3145 * T10Dif: ctio_crc2_to_fw overlay ontop of
3146 * ctio7_to_24xx
3147 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003148 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003149 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003150 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003151 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003152 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003153 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003154 CTIO7_FLAGS_DATA_IN);
3155
3156 /* Real finish is ctio_m1's finish */
3157 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003158 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003159 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003160
3161 /* qlt_24xx_init_ctio_to_isp will correct
3162 * all neccessary fields that's part of CTIO7.
3163 * There should be no residual of CTIO-CRC2 data.
3164 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003165 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3166 &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003167 }
3168 } else
3169 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3170
3171
3172 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003173 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003174
Himanshu Madhani63163e02014-09-25 06:14:59 -04003175 /* Memory Barrier */
3176 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003177 if (qpair->reqq_start_iocbs)
3178 qpair->reqq_start_iocbs(qpair);
3179 else
3180 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003181 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003182
3183 return 0;
3184
3185out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003186 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003187 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003188
3189 return res;
3190}
3191EXPORT_SYMBOL(qlt_xmit_response);
3192
3193int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3194{
3195 struct ctio7_to_24xx *pkt;
3196 struct scsi_qla_host *vha = cmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003197 struct qla_tgt *tgt = cmd->tgt;
3198 struct qla_tgt_prm prm;
Quinn Tran82de8022017-06-13 20:47:17 -07003199 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003200 int res = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003201 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003202
3203 memset(&prm, 0, sizeof(prm));
3204 prm.cmd = cmd;
3205 prm.tgt = tgt;
3206 prm.sg = NULL;
3207 prm.req_cnt = 1;
3208
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003209 /* Calculate number of entries and segments required */
3210 if (qlt_pci_map_calc_cnt(&prm) != 0)
3211 return -EAGAIN;
3212
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003213 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003214 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003215 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003216 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003217 * previous life, just abort the processing.
3218 */
3219 cmd->state = QLA_TGT_STATE_NEED_DATA;
3220 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003221 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003222 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3223 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003224 cmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003225 return 0;
3226 }
3227
Quinn Tran82de8022017-06-13 20:47:17 -07003228 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003229 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003230 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003231 if (res != 0)
3232 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003233 if (cmd->se_cmd.prot_op)
Quinn Tran82de8022017-06-13 20:47:17 -07003234 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003235 else
Quinn Tran82de8022017-06-13 20:47:17 -07003236 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003237
Quinn Tran810e30b2015-06-10 11:05:20 -04003238 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003239 qpair->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003240 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003241 }
3242
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003243 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003244 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003245 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003246
3247 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003248 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003249
3250 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003251 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003252
Himanshu Madhani63163e02014-09-25 06:14:59 -04003253 /* Memory Barrier */
3254 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003255 if (qpair->reqq_start_iocbs)
3256 qpair->reqq_start_iocbs(qpair);
3257 else
3258 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003259 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003260
3261 return res;
3262
3263out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003264 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003265 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003266
3267 return res;
3268}
3269EXPORT_SYMBOL(qlt_rdy_to_xfer);
3270
Quinn Tranf83adb62014-04-11 16:54:43 -04003271
3272/*
Quinn Tranbe251522017-03-15 09:48:49 -07003273 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003274 */
Quinn Tranbe251522017-03-15 09:48:49 -07003275static void
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003276qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003277 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003278{
3279 uint8_t *ap = &sts->actual_dif[0];
3280 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003281 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003282 uint8_t scsi_status, sense_key, asc, ascq;
3283 unsigned long flags;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003284 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003285
Quinn Tranbe251522017-03-15 09:48:49 -07003286 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003287
Quinn Tranbe251522017-03-15 09:48:49 -07003288 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3289 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3290 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003291
Quinn Tranbe251522017-03-15 09:48:49 -07003292 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3293 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3294 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003295
Quinn Tranbe251522017-03-15 09:48:49 -07003296 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3297 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003298
Quinn Tranbe251522017-03-15 09:48:49 -07003299 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003300
Quinn Tranbe251522017-03-15 09:48:49 -07003301 /* check appl tag */
3302 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003303 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3304 "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]",
3305 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3306 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3307 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3308 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003309
Quinn Tranbe251522017-03-15 09:48:49 -07003310 cmd->dif_err_code = DIF_ERR_APP;
3311 scsi_status = SAM_STAT_CHECK_CONDITION;
3312 sense_key = ABORTED_COMMAND;
3313 asc = 0x10;
3314 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003315 }
3316
3317 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003318 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003319 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3320 "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] ",
3321 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3322 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3323 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3324 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003325
Quinn Tranbe251522017-03-15 09:48:49 -07003326 cmd->dif_err_code = DIF_ERR_REF;
3327 scsi_status = SAM_STAT_CHECK_CONDITION;
3328 sense_key = ABORTED_COMMAND;
3329 asc = 0x10;
3330 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003331 goto out;
3332 }
3333
Quinn Tranbe251522017-03-15 09:48:49 -07003334 /* check guard */
3335 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003336 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3337 "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]",
3338 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3339 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3340 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3341 cmd->atio.u.isp24.fcp_hdr.ox_id);
3342
Quinn Tranbe251522017-03-15 09:48:49 -07003343 cmd->dif_err_code = DIF_ERR_GRD;
3344 scsi_status = SAM_STAT_CHECK_CONDITION;
3345 sense_key = ABORTED_COMMAND;
3346 asc = 0x10;
3347 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003348 }
3349out:
Quinn Tranbe251522017-03-15 09:48:49 -07003350 switch (cmd->state) {
3351 case QLA_TGT_STATE_NEED_DATA:
3352 /* handle_data will load DIF error code */
3353 cmd->state = QLA_TGT_STATE_DATA_IN;
3354 vha->hw->tgt.tgt_ops->handle_data(cmd);
3355 break;
3356 default:
3357 spin_lock_irqsave(&cmd->cmd_lock, flags);
3358 if (cmd->aborted) {
3359 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3360 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3361 break;
3362 }
3363 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003364
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003365 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3366 ascq);
Quinn Tranbe251522017-03-15 09:48:49 -07003367 /* assume scsi status gets out on the wire.
3368 * Will not wait for completion.
3369 */
3370 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3371 break;
3372 }
3373}
Quinn Tranf83adb62014-04-11 16:54:43 -04003374
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003375/* If hardware_lock held on entry, might drop it, then reaquire */
3376/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003377static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3378 struct imm_ntfy_from_isp *ntfy)
3379{
3380 struct nack_to_isp *nack;
3381 struct qla_hw_data *ha = vha->hw;
3382 request_t *pkt;
3383 int ret = 0;
3384
3385 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3386 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3387
Quinn Tranec7193e2017-03-15 09:48:55 -07003388 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003389 if (pkt == NULL) {
3390 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3391 "qla_target(%d): %s failed: unable to allocate "
3392 "request packet\n", vha->vp_idx, __func__);
3393 return -ENOMEM;
3394 }
3395
3396 pkt->entry_type = NOTIFY_ACK_TYPE;
3397 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003398 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003399
3400 nack = (struct nack_to_isp *)pkt;
3401 nack->ox_id = ntfy->ox_id;
3402
3403 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3404 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3405 nack->u.isp24.flags = ntfy->u.isp24.flags &
3406 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3407 }
3408
3409 /* terminate */
3410 nack->u.isp24.flags |=
3411 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3412
3413 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3414 nack->u.isp24.status = ntfy->u.isp24.status;
3415 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3416 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3417 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3418 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3419 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3420 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3421
3422 qla2x00_start_iocbs(vha, vha->req);
3423 return ret;
3424}
3425
3426static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3427 struct imm_ntfy_from_isp *imm, int ha_locked)
3428{
3429 unsigned long flags = 0;
3430 int rc;
3431
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003432 if (ha_locked) {
3433 rc = __qlt_send_term_imm_notif(vha, imm);
3434
3435#if 0 /* Todo */
3436 if (rc == -ENOMEM)
3437 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003438#else
3439 if (rc) {
3440 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003441#endif
3442 goto done;
3443 }
3444
3445 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3446 rc = __qlt_send_term_imm_notif(vha, imm);
3447
3448#if 0 /* Todo */
3449 if (rc == -ENOMEM)
3450 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3451#endif
3452
3453done:
3454 if (!ha_locked)
3455 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3456}
3457
Quinn Tran83548fe2017-06-02 09:12:01 -07003458/*
3459 * If hardware_lock held on entry, might drop it, then reaquire
3460 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3461 */
Quinn Tran82de8022017-06-13 20:47:17 -07003462static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003463 struct qla_tgt_cmd *cmd,
3464 struct atio_from_isp *atio)
3465{
Quinn Tran82de8022017-06-13 20:47:17 -07003466 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003467 struct ctio7_to_24xx *ctio24;
3468 struct qla_hw_data *ha = vha->hw;
3469 request_t *pkt;
3470 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003471 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003472
Quinn Tran83548fe2017-06-02 09:12:01 -07003473 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003474
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003475 if (cmd)
3476 vha = cmd->vha;
3477
Quinn Tran82de8022017-06-13 20:47:17 -07003478 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003479 if (pkt == NULL) {
3480 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3481 "qla_target(%d): %s failed: unable to allocate "
3482 "request packet\n", vha->vp_idx, __func__);
3483 return -ENOMEM;
3484 }
3485
3486 if (cmd != NULL) {
3487 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3488 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3489 "qla_target(%d): Terminating cmd %p with "
3490 "incorrect state %d\n", vha->vp_idx, cmd,
3491 cmd->state);
3492 } else
3493 ret = 1;
3494 }
3495
Quinn Tran60a9ead2017-06-13 20:47:28 -07003496 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003497 pkt->entry_count = 1;
3498 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3499
3500 ctio24 = (struct ctio7_to_24xx *)pkt;
3501 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003502 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003503 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003504 ctio24->vp_index = vha->vp_idx;
3505 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3506 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3507 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3508 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003509 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3510 CTIO7_FLAGS_TERMINATE;
3511 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003512 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3513 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003514
3515 /* Most likely, it isn't needed */
3516 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3517 &atio->u.isp24.fcp_cmnd.add_cdb[
3518 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3519 if (ctio24->u.status1.residual != 0)
3520 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3521
Himanshu Madhani63163e02014-09-25 06:14:59 -04003522 /* Memory Barrier */
3523 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003524 if (qpair->reqq_start_iocbs)
3525 qpair->reqq_start_iocbs(qpair);
3526 else
3527 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003528 return ret;
3529}
3530
Quinn Tran82de8022017-06-13 20:47:17 -07003531static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003532 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3533 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003534{
Quinn Tran82de8022017-06-13 20:47:17 -07003535 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003536 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003537 int rc;
3538
Quinn Tran82de8022017-06-13 20:47:17 -07003539 /* why use different vha? NPIV */
3540 if (cmd)
3541 vha = cmd->vha;
3542 else
3543 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003544
3545 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003546 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003547 if (rc == -ENOMEM)
3548 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003549 goto done;
3550 }
Quinn Tran82de8022017-06-13 20:47:17 -07003551 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3552 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003553 if (rc == -ENOMEM)
3554 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003555
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003556done:
Quinn Trana07100e2015-12-07 19:48:57 -05003557 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003558 if (cmd->sg_mapped)
3559 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003560 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3561 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003562
3563 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003564 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003565
Quinn Tran7b898542014-04-11 16:54:44 -04003566 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003567}
3568
Quinn Tran33e79972014-09-25 06:14:55 -04003569static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3570{
3571 struct list_head free_list;
3572 struct qla_tgt_cmd *cmd, *tcmd;
3573
3574 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003575 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003576
3577 cmd = tcmd = NULL;
3578 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3579 INIT_LIST_HEAD(&free_list);
3580 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3581
3582 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3583 list_del(&cmd->cmd_list);
3584 /* This cmd was never sent to TCM. There is no need
3585 * to schedule free or call free_cmd
3586 */
3587 qlt_free_cmd(cmd);
3588 vha->hw->tgt.num_qfull_cmds_alloc--;
3589 }
3590 }
3591 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3592}
3593
3594static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3595{
3596 uint32_t total_leaked;
3597
3598 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3599
3600 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3601 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3602
3603 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3604 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003605 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003606
3607 if (IS_P3P_TYPE(vha->hw))
3608 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3609 else
3610 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3611 qla2xxx_wake_dpc(vha);
3612 }
3613
3614}
3615
Quinn Trana07100e2015-12-07 19:48:57 -05003616int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003617{
3618 struct qla_tgt *tgt = cmd->tgt;
3619 struct scsi_qla_host *vha = tgt->vha;
3620 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003621 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003622
3623 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3624 "qla_target(%d): terminating exchange for aborted cmd=%p "
3625 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3626 se_cmd->tag);
3627
Quinn Trana07100e2015-12-07 19:48:57 -05003628 spin_lock_irqsave(&cmd->cmd_lock, flags);
3629 if (cmd->aborted) {
3630 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3631 /*
3632 * It's normal to see 2 calls in this path:
3633 * 1) XFER Rdy completion + CMD_T_ABORT
3634 * 2) TCM TMR - drain_state_list
3635 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003636 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3637 "multiple abort. %p transport_state %x, t_state %x, "
3638 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3639 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003640 return EIO;
3641 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003642 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003643 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003644 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003645
Quinn Tran82de8022017-06-13 20:47:17 -07003646 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003647 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003648}
3649EXPORT_SYMBOL(qlt_abort_cmd);
3650
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003651void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3652{
Quinn Tran5d964832017-01-19 22:27:59 -08003653 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003654
Quinn Tranf83adb62014-04-11 16:54:43 -04003655 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3656 "%s: se_cmd[%p] ox_id %04x\n",
3657 __func__, &cmd->se_cmd,
3658 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003659
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003660 BUG_ON(cmd->cmd_in_wq);
3661
Quinn Trana07100e2015-12-07 19:48:57 -05003662 if (cmd->sg_mapped)
3663 qlt_unmap_sg(cmd->vha, cmd);
3664
Quinn Tran33e79972014-09-25 06:14:55 -04003665 if (!cmd->q_full)
3666 qlt_decr_num_pend_cmds(cmd->vha);
3667
Quinn Tranf83adb62014-04-11 16:54:43 -04003668 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003669 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003670 if (unlikely(cmd->free_sg))
3671 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003672
3673 if (!sess || !sess->se_sess) {
3674 WARN_ON(1);
3675 return;
3676 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003677 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003678 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003679}
3680EXPORT_SYMBOL(qlt_free_cmd);
3681
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003682/*
3683 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3684 */
Quinn Tran82de8022017-06-13 20:47:17 -07003685static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003686 struct qla_tgt_cmd *cmd, uint32_t status)
3687{
3688 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003689 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003690
Quinn Tranbe251522017-03-15 09:48:49 -07003691 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003692 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003693 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3694 "se_cmd=%p tag[%x] op %#x/%s",
3695 cmd->lba, cmd->lba,
3696 cmd->num_blks, &cmd->se_cmd,
3697 cmd->atio.u.isp24.exchange_addr,
3698 cmd->se_cmd.prot_op,
3699 prot_op_str(cmd->se_cmd.prot_op));
3700
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003701 if (ctio != NULL) {
3702 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3703 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003704 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003705 } else
3706 term = 1;
3707
3708 if (term)
Quinn Tran82de8022017-06-13 20:47:17 -07003709 qlt_term_ctio_exchange(qpair, ctio, cmd, status);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003710
3711 return term;
3712}
3713
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003714
3715/* ha->hardware_lock supposed to be held on entry */
3716static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003717 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003718{
3719 struct qla_tgt_cmd *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003720 struct req_que *req;
3721 int qid = GET_QID(handle);
3722 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003723
Quinn Tran82de8022017-06-13 20:47:17 -07003724 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3725 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003726
Quinn Tran82de8022017-06-13 20:47:17 -07003727 if (qid == rsp->req->id) {
3728 req = rsp->req;
3729 } else if (vha->hw->req_q_map[qid]) {
3730 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3731 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3732 vha->vp_idx, rsp->id, handle);
3733 req = vha->hw->req_q_map[qid];
3734 } else {
3735 return NULL;
3736 }
Arun Easi667024a2014-09-25 06:14:47 -04003737
Quinn Tran82de8022017-06-13 20:47:17 -07003738 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003739
Quinn Tran82de8022017-06-13 20:47:17 -07003740 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003741 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003742 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3743 "qla_target(%d): Wrong handle %x received\n",
3744 vha->vp_idx, handle);
3745 return NULL;
3746 }
Quinn Tran82de8022017-06-13 20:47:17 -07003747
3748 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3749 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003750 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003751 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3752 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003753 return NULL;
3754 }
Quinn Tran82de8022017-06-13 20:47:17 -07003755 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003756 } else if (ctio != NULL) {
3757 /* We can't get loop ID from CTIO7 */
3758 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3759 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3760 "support NULL handles\n", vha->vp_idx);
3761 return NULL;
3762 }
3763
3764 return cmd;
3765}
3766
Arun Easic0cb4492014-09-25 06:14:51 -04003767/* hardware_lock should be held by caller. */
Quinn Tranc5419e22017-06-13 20:47:16 -07003768void
Arun Easic0cb4492014-09-25 06:14:51 -04003769qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3770{
3771 struct qla_hw_data *ha = vha->hw;
Arun Easic0cb4492014-09-25 06:14:51 -04003772
3773 if (cmd->sg_mapped)
3774 qlt_unmap_sg(vha, cmd);
3775
Arun Easic0cb4492014-09-25 06:14:51 -04003776 /* TODO: fix debug message type and ids. */
3777 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3778 ql_dbg(ql_dbg_io, vha, 0xff00,
Quinn Tran82de8022017-06-13 20:47:17 -07003779 "HOST-ABORT: state=PROCESSED.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003780 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3781 cmd->write_data_transferred = 0;
3782 cmd->state = QLA_TGT_STATE_DATA_IN;
3783
3784 ql_dbg(ql_dbg_io, vha, 0xff01,
Quinn Tran82de8022017-06-13 20:47:17 -07003785 "HOST-ABORT: state=DATA_IN.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003786
3787 ha->tgt.tgt_ops->handle_data(cmd);
3788 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003789 } else {
3790 ql_dbg(ql_dbg_io, vha, 0xff03,
Quinn Tran82de8022017-06-13 20:47:17 -07003791 "HOST-ABORT: state=BAD(%d).\n",
Arun Easic0cb4492014-09-25 06:14:51 -04003792 cmd->state);
3793 dump_stack();
3794 }
3795
Quinn Tran1eb42f92017-01-19 22:27:55 -08003796 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003797 ha->tgt.tgt_ops->free_cmd(cmd);
3798}
3799
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003800/*
3801 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3802 */
Quinn Tran82de8022017-06-13 20:47:17 -07003803static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3804 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003805{
3806 struct qla_hw_data *ha = vha->hw;
3807 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003808 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07003809 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003810
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003811 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3812 /* That could happen only in case of an error/reset/abort */
3813 if (status != CTIO_SUCCESS) {
3814 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3815 "Intermediate CTIO received"
3816 " (status %x)\n", status);
3817 }
3818 return;
3819 }
3820
Quinn Tran82de8022017-06-13 20:47:17 -07003821 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003822 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003823 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003824
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003825 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003826 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003827
Joern Engelf9b67212014-09-16 16:23:18 -04003828 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003829
3830 if (unlikely(status != CTIO_SUCCESS)) {
3831 switch (status & 0xFFFF) {
3832 case CTIO_LIP_RESET:
3833 case CTIO_TARGET_RESET:
3834 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003835 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003836 case CTIO_TIMEOUT:
3837 case CTIO_INVALID_RX_ID:
3838 /* They are OK */
3839 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3840 "qla_target(%d): CTIO with "
3841 "status %#x received, state %x, se_cmd %p, "
3842 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3843 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3844 status, cmd->state, se_cmd);
3845 break;
3846
3847 case CTIO_PORT_LOGGED_OUT:
3848 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003849 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003850 int logged_out =
3851 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3852
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003853 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003854 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003855 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003856 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003857 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003858
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003859 if (logged_out && cmd->sess) {
3860 /*
3861 * Session is already logged out, but we need
3862 * to notify initiator, who's not aware of this
3863 */
3864 cmd->sess->logout_on_delete = 0;
3865 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07003866 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08003867 "%s %d %8phC post del sess\n",
3868 __func__, __LINE__, cmd->sess->port_name);
3869
3870 qlt_schedule_sess_for_deletion_lock(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003871 }
3872 break;
3873 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003874 case CTIO_DIF_ERROR: {
3875 struct ctio_crc_from_fw *crc =
3876 (struct ctio_crc_from_fw *)ctio;
3877 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07003878 "qla_target(%d): CTIO with DIF_ERROR status %x "
3879 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3880 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003881 vha->vp_idx, status, cmd->state, se_cmd,
3882 *((u64 *)&crc->actual_dif[0]),
3883 *((u64 *)&crc->expected_dif[0]));
3884
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003885 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07003886 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04003887 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003888 default:
3889 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003890 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003891 vha->vp_idx, status, cmd->state, se_cmd);
3892 break;
3893 }
3894
Quinn Tran7b898542014-04-11 16:54:44 -04003895
Quinn Tran193b50b2015-12-17 14:57:03 -05003896 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003897 * cmd is already aborted/terminated, we don't
3898 * need to terminate again. The exchange is already
3899 * cleaned up/freed at FW level. Just cleanup at driver
3900 * level.
3901 */
3902 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003903 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003904 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07003905 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003906 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003907 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003908 }
3909
3910 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003911 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003912 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003913 cmd->state = QLA_TGT_STATE_DATA_IN;
3914
Bart Van Assche52c82822015-07-09 07:23:26 -07003915 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003916 cmd->write_data_transferred = 1;
3917
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003918 ha->tgt.tgt_ops->handle_data(cmd);
3919 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003920 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003921 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003922 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003923 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003924 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003925 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003926 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3927 "qla_target(%d): A command in state (%d) should "
3928 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3929 }
3930
Quinn Tran7b898542014-04-11 16:54:44 -04003931 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003932 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003933 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3934 dump_stack();
3935 }
3936
3937 ha->tgt.tgt_ops->free_cmd(cmd);
3938}
3939
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003940static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3941 uint8_t task_codes)
3942{
3943 int fcp_task_attr;
3944
3945 switch (task_codes) {
3946 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003947 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003948 break;
3949 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003950 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003951 break;
3952 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003953 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003954 break;
3955 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003956 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003957 break;
3958 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003959 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003960 break;
3961 default:
3962 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3963 "qla_target: unknown task code %x, use ORDERED instead\n",
3964 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003965 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003966 break;
3967 }
3968
3969 return fcp_task_attr;
3970}
3971
Quinn Tran5d964832017-01-19 22:27:59 -08003972static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003973 uint8_t *);
3974/*
3975 * Process context for I/O path into tcm_qla2xxx code
3976 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003977static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003978{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003979 scsi_qla_host_t *vha = cmd->vha;
3980 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08003981 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003982 struct atio_from_isp *atio = &cmd->atio;
3983 unsigned char *cdb;
3984 unsigned long flags;
3985 uint32_t data_length;
3986 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003987 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003988
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003989 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003990 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003991
Quinn Tran193b50b2015-12-17 14:57:03 -05003992 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003993 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3994 "cmd with tag %u is aborted\n",
3995 cmd->atio.u.isp24.exchange_addr);
3996 goto out_term;
3997 }
3998
Quinn Trana07100e2015-12-07 19:48:57 -05003999 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004000 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004001 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004002
4003 if (atio->u.isp24.fcp_cmnd.rddata &&
4004 atio->u.isp24.fcp_cmnd.wrdata) {
4005 bidi = 1;
4006 data_dir = DMA_TO_DEVICE;
4007 } else if (atio->u.isp24.fcp_cmnd.rddata)
4008 data_dir = DMA_FROM_DEVICE;
4009 else if (atio->u.isp24.fcp_cmnd.wrdata)
4010 data_dir = DMA_TO_DEVICE;
4011 else
4012 data_dir = DMA_NONE;
4013
4014 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4015 atio->u.isp24.fcp_cmnd.task_attr);
4016 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4017 &atio->u.isp24.fcp_cmnd.add_cdb[
4018 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4019
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004020 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4021 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004022 if (ret != 0)
4023 goto out_term;
4024 /*
4025 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4026 */
Quinn Tran75601512015-12-17 14:57:04 -05004027 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004028 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004029 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004030 return;
4031
4032out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004033 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004034 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004035 * cmd has not sent to target yet, so pass NULL as the second
4036 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004037 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004038 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004039 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4040 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004041
4042 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004043 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Quinn Tran82de8022017-06-13 20:47:17 -07004044 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004045
4046 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004047 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004048 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004049}
4050
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004051static void qlt_do_work(struct work_struct *work)
4052{
4053 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004054 scsi_qla_host_t *vha = cmd->vha;
4055 unsigned long flags;
4056
4057 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4058 list_del(&cmd->cmd_list);
4059 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004060
4061 __qlt_do_work(cmd);
4062}
4063
Quinn Tran09620eeb2017-06-13 20:47:20 -07004064void qlt_clr_qp_table(struct scsi_qla_host *vha)
4065{
4066 unsigned long flags;
4067 struct qla_hw_data *ha = vha->hw;
4068 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4069 void *node;
4070 u64 key = 0;
4071
4072 ql_log(ql_log_info, vha, 0x706c,
4073 "User update Number of Active Qpairs %d\n",
4074 ha->tgt.num_act_qpairs);
4075
4076 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4077
4078 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4079 btree_remove64(&tgt->lun_qpair_map, key);
4080
4081 ha->base_qpair->lun_cnt = 0;
4082 for (key = 0; key < ha->max_qpairs; key++)
4083 if (ha->queue_pair_map[key])
4084 ha->queue_pair_map[key]->lun_cnt = 0;
4085
4086 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4087}
4088
Quinn Trane326d222017-06-13 20:47:18 -07004089static void qlt_assign_qpair(struct scsi_qla_host *vha,
4090 struct qla_tgt_cmd *cmd)
4091{
4092 struct qla_qpair *qpair, *qp;
4093 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4094 struct qla_qpair_hint *h;
4095
4096 if (vha->flags.qpairs_available) {
4097 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4098 if (unlikely(!h)) {
4099 /* spread lun to qpair ratio evently */
4100 int lcnt = 0, rc;
4101 struct scsi_qla_host *base_vha =
4102 pci_get_drvdata(vha->hw->pdev);
4103
4104 qpair = vha->hw->base_qpair;
4105 if (qpair->lun_cnt == 0) {
4106 qpair->lun_cnt++;
4107 h = qla_qpair_to_hint(tgt, qpair);
4108 BUG_ON(!h);
4109 rc = btree_insert64(&tgt->lun_qpair_map,
4110 cmd->unpacked_lun, h, GFP_ATOMIC);
4111 if (rc) {
4112 qpair->lun_cnt--;
4113 ql_log(ql_log_info, vha, 0xd037,
4114 "Unable to insert lun %llx into lun_qpair_map\n",
4115 cmd->unpacked_lun);
4116 }
4117 goto out;
4118 } else {
4119 lcnt = qpair->lun_cnt;
4120 }
4121
4122 h = NULL;
4123 list_for_each_entry(qp, &base_vha->qp_list,
4124 qp_list_elem) {
4125 if (qp->lun_cnt == 0) {
4126 qp->lun_cnt++;
4127 h = qla_qpair_to_hint(tgt, qp);
4128 BUG_ON(!h);
4129 rc = btree_insert64(&tgt->lun_qpair_map,
4130 cmd->unpacked_lun, h, GFP_ATOMIC);
4131 if (rc) {
4132 qp->lun_cnt--;
4133 ql_log(ql_log_info, vha, 0xd038,
4134 "Unable to insert lun %llx into lun_qpair_map\n",
4135 cmd->unpacked_lun);
4136 }
4137 qpair = qp;
4138 goto out;
4139 } else {
4140 if (qp->lun_cnt < lcnt) {
4141 lcnt = qp->lun_cnt;
4142 qpair = qp;
4143 continue;
4144 }
4145 }
4146 }
4147 BUG_ON(!qpair);
4148 qpair->lun_cnt++;
4149 h = qla_qpair_to_hint(tgt, qpair);
4150 BUG_ON(!h);
4151 rc = btree_insert64(&tgt->lun_qpair_map,
4152 cmd->unpacked_lun, h, GFP_ATOMIC);
4153 if (rc) {
4154 qpair->lun_cnt--;
4155 ql_log(ql_log_info, vha, 0xd039,
4156 "Unable to insert lun %llx into lun_qpair_map\n",
4157 cmd->unpacked_lun);
4158 }
4159 }
4160 } else {
4161 h = &tgt->qphints[0];
4162 }
4163out:
4164 cmd->qpair = h->qpair;
4165 cmd->se_cmd.cpuid = h->cpuid;
4166}
4167
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004168static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004169 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004170 struct atio_from_isp *atio)
4171{
4172 struct se_session *se_sess = sess->se_sess;
4173 struct qla_tgt_cmd *cmd;
4174 int tag;
4175
4176 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4177 if (tag < 0)
4178 return NULL;
4179
4180 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4181 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004182 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004183 memcpy(&cmd->atio, atio, sizeof(*atio));
4184 cmd->state = QLA_TGT_STATE_NEW;
4185 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004186 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004187 cmd->vha = vha;
4188 cmd->se_cmd.map_tag = tag;
4189 cmd->sess = sess;
4190 cmd->loop_id = sess->loop_id;
4191 cmd->conf_compl_supported = sess->conf_compl_supported;
4192
Quinn Tran1eb42f92017-01-19 22:27:55 -08004193 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004194 cmd->jiffies_at_alloc = get_jiffies_64();
4195
Quinn Trane326d222017-06-13 20:47:18 -07004196 cmd->unpacked_lun = scsilun_to_int(
4197 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4198 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004199 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004200 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004201
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004202 return cmd;
4203}
4204
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004205static void qlt_create_sess_from_atio(struct work_struct *work)
4206{
4207 struct qla_tgt_sess_op *op = container_of(work,
4208 struct qla_tgt_sess_op, work);
4209 scsi_qla_host_t *vha = op->vha;
4210 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004211 struct fc_port *sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004212 struct qla_tgt_cmd *cmd;
4213 unsigned long flags;
4214 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4215
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004216 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4217 list_del(&op->cmd_list);
4218 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4219
4220 if (op->aborted) {
4221 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4222 "sess_op with tag %u is aborted\n",
4223 op->atio.u.isp24.exchange_addr);
4224 goto out_term;
4225 }
4226
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004227 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004228 "qla_target(%d): Unable to find wwn login"
4229 " (s_id %x:%x:%x), trying to create it manually\n",
4230 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004231
4232 if (op->atio.u.raw.entry_count > 1) {
4233 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004234 "Dropping multy entry atio %p\n", &op->atio);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004235 goto out_term;
4236 }
4237
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004238 sess = qlt_make_local_sess(vha, s_id);
4239 /* sess has an extra creation ref. */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004240
4241 if (!sess)
4242 goto out_term;
4243 /*
4244 * Now obtain a pre-allocated session tag using the original op->atio
4245 * packet header, and dispatch into __qlt_do_work() using the existing
4246 * process context.
4247 */
4248 cmd = qlt_get_tag(vha, sess, &op->atio);
4249 if (!cmd) {
Quinn Tran82de8022017-06-13 20:47:17 -07004250 struct qla_qpair *qpair = ha->base_qpair;
4251
4252 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4253 qlt_send_busy(qpair, &op->atio, SAM_STAT_BUSY);
4254 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4255
4256 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004257 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran82de8022017-06-13 20:47:17 -07004258 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004259 kfree(op);
4260 return;
4261 }
Quinn Tran726b8542017-01-19 22:28:00 -08004262
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004263 /*
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02004264 * __qlt_do_work() will call qlt_put_sess() to release
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004265 * the extra reference taken above by qlt_make_local_sess()
4266 */
4267 __qlt_do_work(cmd);
4268 kfree(op);
4269 return;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004270out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07004271 qlt_send_term_exchange(vha->hw->base_qpair, NULL, &op->atio, 0, 0);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004272 kfree(op);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004273}
4274
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004275/* ha->hardware_lock supposed to be held on entry */
4276static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4277 struct atio_from_isp *atio)
4278{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004279 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004280 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004281 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004282 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004283 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004284
4285 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004286 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004287 "New command while device %p is shutting down\n", tgt);
4288 return -EFAULT;
4289 }
4290
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004291 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4292 if (unlikely(!sess)) {
4293 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4294 GFP_ATOMIC);
4295 if (!op)
4296 return -ENOMEM;
4297
4298 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04004299 op->vha = vha;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004300
Quinn Tran8b631d82017-06-02 09:11:54 -07004301 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004302 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
Quinn Tran8b631d82017-06-02 09:11:54 -07004303 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004304
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004305 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4306 queue_work(qla_tgt_wq, &op->work);
4307 return 0;
4308 }
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004309
4310 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4311 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004312 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004313 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004314 "New command while old session %p is being deleted\n",
4315 sess);
4316 return -EFAULT;
4317 }
4318
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004319 /*
4320 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4321 */
Quinn Tran726b8542017-01-19 22:28:00 -08004322 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004323 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004324 "%s: kref_get fail, %8phC oxid %x \n",
4325 __func__, sess->port_name,
4326 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4327 return -EFAULT;
4328 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004329
4330 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004331 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004332 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004333 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004334 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4335 ha->tgt.tgt_ops->put_sess(sess);
4336 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004337 return -ENOMEM;
4338 }
4339
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004340 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004341 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004342
Quinn Tran726b8542017-01-19 22:28:00 -08004343 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004344 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004345 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004346
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004347 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004348 if (vha->flags.qpairs_available) {
4349 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4350 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004351 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4352 queue_work_on(smp_processor_id(), qla_tgt_wq,
4353 &cmd->work);
4354 else
4355 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4356 &cmd->work);
4357 } else {
4358 queue_work(qla_tgt_wq, &cmd->work);
4359 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004360
Quinn Tran82de8022017-06-13 20:47:17 -07004361 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004362}
4363
4364/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004365static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004366 int fn, void *iocb, int flags)
4367{
4368 struct scsi_qla_host *vha = sess->vha;
4369 struct qla_hw_data *ha = vha->hw;
4370 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004371 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004372 int res;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004373
4374 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4375 if (!mcmd) {
4376 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4377 "qla_target(%d): Allocation of management "
4378 "command failed, some commands and their data could "
4379 "leak\n", vha->vp_idx);
4380 return -ENOMEM;
4381 }
4382 memset(mcmd, 0, sizeof(*mcmd));
4383 mcmd->sess = sess;
4384
4385 if (iocb) {
4386 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4387 sizeof(mcmd->orig_iocb.imm_ntfy));
4388 }
4389 mcmd->tmr_func = fn;
4390 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004391 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07004392 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004393 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004394
4395 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004396 case QLA_TGT_LUN_RESET:
Quinn Tranbe92fc32017-01-19 22:27:54 -08004397 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4398 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004399 }
4400
Quinn Tranbe92fc32017-01-19 22:27:54 -08004401 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004402 if (res != 0) {
4403 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4404 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4405 sess->vha->vp_idx, res);
4406 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4407 return -EFAULT;
4408 }
4409
4410 return 0;
4411}
4412
4413/* ha->hardware_lock supposed to be held on entry */
4414static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4415{
4416 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4417 struct qla_hw_data *ha = vha->hw;
4418 struct qla_tgt *tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004419 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004420 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004421 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004422 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004423
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004424 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004425
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004426 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004427
4428 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004429 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4430 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004431 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4432
Quinn Tranf775bd12017-06-02 09:11:59 -07004433 unpacked_lun =
4434 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004435
4436 if (!sess) {
4437 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4438 "qla_target(%d): task mgmt fn 0x%x for "
4439 "non-existant session\n", vha->vp_idx, fn);
4440 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4441 sizeof(struct atio_from_isp));
4442 }
4443
Quinn Tran726b8542017-01-19 22:28:00 -08004444 if (sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004445 return -EFAULT;
4446
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004447 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4448}
4449
4450/* ha->hardware_lock supposed to be held on entry */
4451static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004452 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004453{
4454 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4455 struct qla_hw_data *ha = vha->hw;
4456 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004457 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004458 int rc;
4459
4460 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4461 if (mcmd == NULL) {
4462 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4463 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4464 vha->vp_idx, __func__);
4465 return -ENOMEM;
4466 }
4467 memset(mcmd, 0, sizeof(*mcmd));
4468
4469 mcmd->sess = sess;
4470 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4471 sizeof(mcmd->orig_iocb.imm_ntfy));
4472
Quinn Tranf775bd12017-06-02 09:11:59 -07004473 unpacked_lun =
4474 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004475 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004476 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004477 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004478
Quinn Tranbe92fc32017-01-19 22:27:54 -08004479 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004480 le16_to_cpu(iocb->u.isp2x.seq_id));
4481 if (rc != 0) {
4482 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4483 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4484 vha->vp_idx, rc);
4485 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4486 return -EFAULT;
4487 }
4488
4489 return 0;
4490}
4491
4492/* ha->hardware_lock supposed to be held on entry */
4493static int qlt_abort_task(struct scsi_qla_host *vha,
4494 struct imm_ntfy_from_isp *iocb)
4495{
4496 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004497 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004498 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004499 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004500
4501 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4502
Quinn Tran75601512015-12-17 14:57:04 -05004503 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004504 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004505 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4506
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004507 if (sess == NULL) {
4508 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4509 "qla_target(%d): task abort for unexisting "
4510 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004511 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004512 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4513 }
4514
4515 return __qlt_abort_task(vha, iocb, sess);
4516}
4517
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004518void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4519{
Quinn Tran726b8542017-01-19 22:28:00 -08004520 if (rc != MBS_COMMAND_COMPLETE) {
4521 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4522 "%s: se_sess %p / sess %p from"
4523 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4524 " LOGO failed: %#x\n",
4525 __func__,
4526 fcport->se_sess,
4527 fcport,
4528 fcport->port_name, fcport->loop_id,
4529 fcport->d_id.b.domain, fcport->d_id.b.area,
4530 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004531 }
Quinn Tran726b8542017-01-19 22:28:00 -08004532
4533 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004534}
4535
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004536/*
4537* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4538*
4539* Schedules sessions with matching port_id/loop_id but different wwn for
4540* deletion. Returns existing session with matching wwn if present.
4541* Null otherwise.
4542*/
Quinn Tran726b8542017-01-19 22:28:00 -08004543struct fc_port *
4544qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004545 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004546{
Quinn Tran5d964832017-01-19 22:27:59 -08004547 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004548 uint64_t other_wwn;
4549
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004550 *conflict_sess = NULL;
4551
Quinn Tran5d964832017-01-19 22:27:59 -08004552 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004553
4554 other_wwn = wwn_to_u64(other_sess->port_name);
4555
4556 if (wwn == other_wwn) {
4557 WARN_ON(sess);
4558 sess = other_sess;
4559 continue;
4560 }
4561
4562 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004563 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004564 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004565 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004566 "Invalidating sess %p loop_id %d wwn %llx.\n",
4567 other_sess, other_sess->loop_id, other_wwn);
4568
4569 /*
4570 * logout_on_delete is set by default, but another
4571 * session that has the same s_id/loop_id combo
4572 * might have cleared it when requested this session
4573 * deletion, so don't touch it
4574 */
4575 qlt_schedule_sess_for_deletion(other_sess, true);
4576 } else {
4577 /*
4578 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004579 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004580 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004581 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004582 "Invalidating sess %p loop_id %d wwn %llx.\n",
4583 other_sess, other_sess->loop_id, other_wwn);
4584
4585
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004586 other_sess->keep_nport_handle = 1;
4587 *conflict_sess = other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004588 qlt_schedule_sess_for_deletion(other_sess,
4589 true);
4590 }
4591 continue;
4592 }
4593
4594 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004595 if ((loop_id == other_sess->loop_id) &&
4596 (loop_id != FC_NO_LOOP_ID)) {
4597 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004598 "Invalidating sess %p loop_id %d wwn %llx.\n",
4599 other_sess, other_sess->loop_id, other_wwn);
4600
4601 /* Same loop_id but different s_id
4602 * Ok to kill and logout */
4603 qlt_schedule_sess_for_deletion(other_sess, true);
4604 }
4605 }
4606
4607 return sess;
4608}
4609
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004610/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4611static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4612{
4613 struct qla_tgt_sess_op *op;
4614 struct qla_tgt_cmd *cmd;
4615 uint32_t key;
4616 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004617 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004618
4619 key = (((u32)s_id->b.domain << 16) |
4620 ((u32)s_id->b.area << 8) |
4621 ((u32)s_id->b.al_pa));
4622
Quinn Tran8b631d82017-06-02 09:11:54 -07004623 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004624 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4625 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004626
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004627 if (op_key == key) {
4628 op->aborted = true;
4629 count++;
4630 }
4631 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004632
4633 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4634 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4635 if (op_key == key) {
4636 op->aborted = true;
4637 count++;
4638 }
4639 }
4640
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004641 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4642 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4643 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004644 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004645 count++;
4646 }
4647 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004648 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004649
4650 return count;
4651}
4652
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004653/*
4654 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4655 */
4656static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4657 struct imm_ntfy_from_isp *iocb)
4658{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004659 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004660 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004661 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004662 uint64_t wwn;
4663 port_id_t port_id;
4664 uint16_t loop_id;
4665 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004666 int res = 0;
Quinn Tran5d964832017-01-19 22:27:59 -08004667 struct qlt_plogi_ack_t *pla;
Quinn Tran75601512015-12-17 14:57:04 -05004668 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004669
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004670 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4671
4672 port_id.b.domain = iocb->u.isp24.port_id[2];
4673 port_id.b.area = iocb->u.isp24.port_id[1];
4674 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4675 port_id.b.rsvd_1 = 0;
4676
4677 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4678
Quinn Tran726b8542017-01-19 22:28:00 -08004679 ql_dbg(ql_dbg_disc, vha, 0xf026,
4680 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4681 vha->vp_idx, iocb->u.isp24.port_id[2],
4682 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4683 iocb->u.isp24.status_subcode, loop_id,
4684 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004685
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004686 /* res = 1 means ack at the end of thread
4687 * res = 0 means ack async/later.
4688 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004689 switch (iocb->u.isp24.status_subcode) {
4690 case ELS_PLOGI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004691
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004692 /* Mark all stale commands in qla_tgt_wq for deletion */
4693 abort_cmds_for_s_id(vha, &port_id);
4694
Quinn Tran75601512015-12-17 14:57:04 -05004695 if (wwn) {
4696 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004697 sess = qlt_find_sess_invalidate_other(vha, wwn,
4698 port_id, loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004699 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4700 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004701
Quinn Tran726b8542017-01-19 22:28:00 -08004702 if (IS_SW_RESV_ADDR(port_id)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004703 res = 1;
4704 break;
4705 }
4706
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004707 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4708 if (!pla) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004709 qlt_send_term_imm_notif(vha, iocb, 1);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004710 break;
4711 }
4712
4713 res = 0;
4714
Quinn Tran726b8542017-01-19 22:28:00 -08004715 if (conflict_sess) {
4716 conflict_sess->login_gen++;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004717 qlt_plogi_ack_link(vha, pla, conflict_sess,
Quinn Tran726b8542017-01-19 22:28:00 -08004718 QLT_PLOGI_LINK_CONFLICT);
4719 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004720
Quinn Tran726b8542017-01-19 22:28:00 -08004721 if (!sess) {
4722 pla->ref_count++;
4723 qla24xx_post_newsess_work(vha, &port_id,
4724 iocb->u.isp24.port_name, pla);
4725 res = 0;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004726 break;
Quinn Tran726b8542017-01-19 22:28:00 -08004727 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004728
4729 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
Quinn Tran726b8542017-01-19 22:28:00 -08004730 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4731 sess->d_id = port_id;
4732 sess->login_gen++;
4733
4734 switch (sess->disc_state) {
4735 case DSC_DELETED:
4736 qlt_plogi_ack_unref(vha, pla);
4737 break;
4738
4739 default:
4740 /*
4741 * Under normal circumstances we want to release nport handle
4742 * during LOGO process to avoid nport handle leaks inside FW.
4743 * The exception is when LOGO is done while another PLOGI with
4744 * the same nport handle is waiting as might be the case here.
4745 * Note: there is always a possibily of a race where session
4746 * deletion has already started for other reasons (e.g. ACL
4747 * removal) and now PLOGI arrives:
4748 * 1. if PLOGI arrived in FW after nport handle has been freed,
4749 * FW must have assigned this PLOGI a new/same handle and we
4750 * can proceed ACK'ing it as usual when session deletion
4751 * completes.
4752 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4753 * bit reached it, the handle has now been released. We'll
4754 * get an error when we ACK this PLOGI. Nothing will be sent
4755 * back to initiator. Initiator should eventually retry
4756 * PLOGI and situation will correct itself.
4757 */
4758 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4759 (sess->d_id.b24 == port_id.b24));
4760
Quinn Tran83548fe2017-06-02 09:12:01 -07004761 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4762 "%s %d %8phC post del sess\n",
4763 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004764
4765
4766 qlt_schedule_sess_for_deletion_lock(sess);
4767 break;
4768 }
4769
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004770 break;
4771
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004772 case ELS_PRLI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004773 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4774
Quinn Tran75601512015-12-17 14:57:04 -05004775 if (wwn) {
4776 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004777 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4778 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004779 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4780 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004781
4782 if (conflict_sess) {
4783 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4784 "PRLI with conflicting sess %p port %8phC\n",
4785 conflict_sess, conflict_sess->port_name);
4786 qlt_send_term_imm_notif(vha, iocb, 1);
4787 res = 0;
4788 break;
4789 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004790
4791 if (sess != NULL) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004792 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4793 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004794 /*
4795 * Impatient initiator sent PRLI before last
4796 * PLOGI could finish. Will force him to re-try,
4797 * while last one finishes.
4798 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004799 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004800 "sess %p PRLI received, before plogi ack.\n",
4801 sess);
4802 qlt_send_term_imm_notif(vha, iocb, 1);
4803 res = 0;
4804 break;
4805 }
4806
4807 /*
4808 * This shouldn't happen under normal circumstances,
4809 * since we have deleted the old session during PLOGI
4810 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004811 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004812 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4813 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4814
4815 sess->local = 0;
4816 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08004817 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004818 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004819
4820 if (wd3_lo & BIT_7)
4821 sess->conf_compl_supported = 1;
4822
Quinn Tran726b8542017-01-19 22:28:00 -08004823 if ((wd3_lo & BIT_4) == 0)
4824 sess->port_type = FCT_INITIATOR;
4825 else
4826 sess->port_type = FCT_TARGET;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004827 }
4828 res = 1; /* send notify ack */
4829
4830 /* Make session global (not used in fabric mode) */
4831 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004832 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004833 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07004834 "%s %d %8phC post nack\n",
4835 __func__, __LINE__, sess->port_name);
4836 qla24xx_post_nack_work(vha, sess, iocb,
4837 SRB_NACK_PRLI);
4838 res = 0;
4839 } else {
4840 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4841 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4842 qla2xxx_wake_dpc(vha);
4843 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004844 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08004845 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004846 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07004847 "%s %d %8phC post nack\n",
4848 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004849 qla24xx_post_nack_work(vha, sess, iocb,
4850 SRB_NACK_PRLI);
4851 res = 0;
4852 }
4853 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004854 break;
4855
Quinn Tran41dc5292017-01-19 22:28:03 -08004856 case ELS_TPRLO:
4857 if (le16_to_cpu(iocb->u.isp24.flags) &
4858 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4859 loop_id = 0xFFFF;
4860 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4861 res = 1;
4862 break;
4863 }
4864 /* drop through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004865 case ELS_LOGO:
4866 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08004867 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4868 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4869 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4870
4871 if (sess) {
4872 sess->login_gen++;
4873 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08004874 sess->logo_ack_needed = 1;
4875 memcpy(sess->iocb, iocb, IOCB_SIZE);
4876 }
4877
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004878 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08004879
Quinn Tran83548fe2017-06-02 09:12:01 -07004880 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08004881 "%s: logo %llx res %d sess %p ",
4882 __func__, wwn, res, sess);
4883 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08004884 /*
4885 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4886 * for LOGO_ACK & sess delete
4887 */
Quinn Tran726b8542017-01-19 22:28:00 -08004888 BUG_ON(!sess);
4889 res = 0;
4890 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08004891 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08004892 if (sess) {
4893 qlt_schedule_sess_for_deletion_lock(sess);
4894 res = 0;
4895 }
4896 /* else logo will be ack */
4897 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004898 break;
4899 case ELS_PDISC:
4900 case ELS_ADISC:
4901 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004902 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004903 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07004904 qlt_send_notify_ack(ha->base_qpair,
4905 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004906 tgt->link_reinit_iocb_pending = 0;
4907 }
Quinn Tran726b8542017-01-19 22:28:00 -08004908
4909 sess = qla2x00_find_fcport_by_wwpn(vha,
4910 iocb->u.isp24.port_name, 1);
4911 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004912 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08004913 "sess %p lid %d|%d DS %d LS %d\n",
4914 sess, sess->loop_id, loop_id,
4915 sess->disc_state, sess->fw_login_state);
4916 }
4917
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004918 res = 1; /* send notify ack */
4919 break;
4920 }
4921
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004922 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004923 default:
4924 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4925 "qla_target(%d): Unsupported ELS command %x "
4926 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4927 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4928 break;
4929 }
4930
4931 return res;
4932}
4933
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004934/*
4935 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4936 */
4937static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4938 struct imm_ntfy_from_isp *iocb)
4939{
4940 struct qla_hw_data *ha = vha->hw;
4941 uint32_t add_flags = 0;
4942 int send_notify_ack = 1;
4943 uint16_t status;
4944
4945 status = le16_to_cpu(iocb->u.isp2x.status);
4946 switch (status) {
4947 case IMM_NTFY_LIP_RESET:
4948 {
4949 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4950 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4951 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4952 iocb->u.isp24.status_subcode);
4953
4954 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4955 send_notify_ack = 0;
4956 break;
4957 }
4958
4959 case IMM_NTFY_LIP_LINK_REINIT:
4960 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004961 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004962 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4963 "qla_target(%d): LINK REINIT (loop %#x, "
4964 "subcode %x)\n", vha->vp_idx,
4965 le16_to_cpu(iocb->u.isp24.nport_handle),
4966 iocb->u.isp24.status_subcode);
4967 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07004968 qlt_send_notify_ack(ha->base_qpair,
4969 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004970 }
4971 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4972 tgt->link_reinit_iocb_pending = 1;
4973 /*
4974 * QLogic requires to wait after LINK REINIT for possible
4975 * PDISC or ADISC ELS commands
4976 */
4977 send_notify_ack = 0;
4978 break;
4979 }
4980
4981 case IMM_NTFY_PORT_LOGOUT:
4982 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4983 "qla_target(%d): Port logout (loop "
4984 "%#x, subcode %x)\n", vha->vp_idx,
4985 le16_to_cpu(iocb->u.isp24.nport_handle),
4986 iocb->u.isp24.status_subcode);
4987
4988 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4989 send_notify_ack = 0;
4990 /* The sessions will be cleared in the callback, if needed */
4991 break;
4992
4993 case IMM_NTFY_GLBL_TPRLO:
4994 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4995 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4996 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4997 send_notify_ack = 0;
4998 /* The sessions will be cleared in the callback, if needed */
4999 break;
5000
5001 case IMM_NTFY_PORT_CONFIG:
5002 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5003 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5004 status);
5005 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5006 send_notify_ack = 0;
5007 /* The sessions will be cleared in the callback, if needed */
5008 break;
5009
5010 case IMM_NTFY_GLBL_LOGO:
5011 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5012 "qla_target(%d): Link failure detected\n",
5013 vha->vp_idx);
5014 /* I_T nexus loss */
5015 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5016 send_notify_ack = 0;
5017 break;
5018
5019 case IMM_NTFY_IOCB_OVERFLOW:
5020 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5021 "qla_target(%d): Cannot provide requested "
5022 "capability (IOCB overflowed the immediate notify "
5023 "resource count)\n", vha->vp_idx);
5024 break;
5025
5026 case IMM_NTFY_ABORT_TASK:
5027 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5028 "qla_target(%d): Abort Task (S %08x I %#x -> "
5029 "L %#x)\n", vha->vp_idx,
5030 le16_to_cpu(iocb->u.isp2x.seq_id),
5031 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5032 le16_to_cpu(iocb->u.isp2x.lun));
5033 if (qlt_abort_task(vha, iocb) == 0)
5034 send_notify_ack = 0;
5035 break;
5036
5037 case IMM_NTFY_RESOURCE:
5038 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5039 "qla_target(%d): Out of resources, host %ld\n",
5040 vha->vp_idx, vha->host_no);
5041 break;
5042
5043 case IMM_NTFY_MSG_RX:
5044 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5045 "qla_target(%d): Immediate notify task %x\n",
5046 vha->vp_idx, iocb->u.isp2x.task_flags);
5047 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5048 send_notify_ack = 0;
5049 break;
5050
5051 case IMM_NTFY_ELS:
5052 if (qlt_24xx_handle_els(vha, iocb) == 0)
5053 send_notify_ack = 0;
5054 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005055 default:
5056 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5057 "qla_target(%d): Received unknown immediate "
5058 "notify status %x\n", vha->vp_idx, status);
5059 break;
5060 }
5061
5062 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005063 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5064 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005065}
5066
5067/*
5068 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5069 * This function sends busy to ISP 2xxx or 24xx.
5070 */
Quinn Tran82de8022017-06-13 20:47:17 -07005071static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005072 struct atio_from_isp *atio, uint16_t status)
5073{
Quinn Tran82de8022017-06-13 20:47:17 -07005074 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005075 struct ctio7_to_24xx *ctio24;
5076 struct qla_hw_data *ha = vha->hw;
5077 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005078 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005079 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005080 u16 temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005081
Quinn Tran75601512015-12-17 14:57:04 -05005082 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005083 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5084 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05005085 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005086 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005087 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005088 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005089 }
5090 /* Sending marker isn't necessary, since we called from ISR */
5091
Quinn Tran82de8022017-06-13 20:47:17 -07005092 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005093 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005094 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005095 "qla_target(%d): %s failed: unable to allocate "
5096 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005097 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005098 }
5099
Quinn Tran60a9ead2017-06-13 20:47:28 -07005100 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005101 pkt->entry_count = 1;
5102 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5103
5104 ctio24 = (struct ctio7_to_24xx *)pkt;
5105 ctio24->entry_type = CTIO_TYPE7;
5106 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005107 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005108 ctio24->vp_index = vha->vp_idx;
5109 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5110 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5111 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5112 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005113 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005114 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005115 CTIO7_FLAGS_DONT_RET_CTIO;
5116 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005117 /*
5118 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5119 * if the explicit conformation is used.
5120 */
5121 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5122 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005123 /* Memory Barrier */
5124 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005125 if (qpair->reqq_start_iocbs)
5126 qpair->reqq_start_iocbs(qpair);
5127 else
5128 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005129 return 0;
5130}
5131
5132/*
5133 * This routine is used to allocate a command for either a QFull condition
5134 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5135 * out previously.
5136 */
5137static void
5138qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5139 struct atio_from_isp *atio, uint16_t status, int qfull)
5140{
5141 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5142 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005143 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005144 struct se_session *se_sess;
5145 struct qla_tgt_cmd *cmd;
5146 int tag;
Quinn Tran82de8022017-06-13 20:47:17 -07005147 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005148
5149 if (unlikely(tgt->tgt_stop)) {
5150 ql_dbg(ql_dbg_io, vha, 0x300a,
5151 "New command while device %p is shutting down\n", tgt);
5152 return;
5153 }
5154
5155 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5156 vha->hw->tgt.num_qfull_cmds_dropped++;
5157 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005158 vha->qla_stats.stat_max_qfull_cmds_dropped)
5159 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005160 vha->hw->tgt.num_qfull_cmds_dropped;
5161
5162 ql_dbg(ql_dbg_io, vha, 0x3068,
5163 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5164 vha->vp_idx, __func__,
5165 vha->hw->tgt.num_qfull_cmds_dropped);
5166
5167 qlt_chk_exch_leak_thresh_hold(vha);
5168 return;
5169 }
5170
5171 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5172 (vha, atio->u.isp24.fcp_hdr.s_id);
5173 if (!sess)
5174 return;
5175
5176 se_sess = sess->se_sess;
5177
5178 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5179 if (tag < 0)
5180 return;
5181
5182 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5183 if (!cmd) {
5184 ql_dbg(ql_dbg_io, vha, 0x3009,
5185 "qla_target(%d): %s: Allocation of cmd failed\n",
5186 vha->vp_idx, __func__);
5187
5188 vha->hw->tgt.num_qfull_cmds_dropped++;
5189 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005190 vha->qla_stats.stat_max_qfull_cmds_dropped)
5191 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005192 vha->hw->tgt.num_qfull_cmds_dropped;
5193
5194 qlt_chk_exch_leak_thresh_hold(vha);
5195 return;
5196 }
5197
5198 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5199
5200 qlt_incr_num_pend_cmds(vha);
5201 INIT_LIST_HEAD(&cmd->cmd_list);
5202 memcpy(&cmd->atio, atio, sizeof(*atio));
5203
5204 cmd->tgt = vha->vha_tgt.qla_tgt;
5205 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005206 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005207 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005208 cmd->qpair = ha->base_qpair;
Quinn Tran33e79972014-09-25 06:14:55 -04005209
5210 if (qfull) {
5211 cmd->q_full = 1;
5212 /* NOTE: borrowing the state field to carry the status */
5213 cmd->state = status;
5214 } else
5215 cmd->term_exchg = 1;
5216
Quinn Tran82de8022017-06-13 20:47:17 -07005217 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005218 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5219
5220 vha->hw->tgt.num_qfull_cmds_alloc++;
5221 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005222 vha->qla_stats.stat_max_qfull_cmds_alloc)
5223 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005224 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005225 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005226}
5227
5228int
Quinn Tran82de8022017-06-13 20:47:17 -07005229qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005230{
Quinn Tran82de8022017-06-13 20:47:17 -07005231 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005232 struct qla_hw_data *ha = vha->hw;
5233 unsigned long flags;
5234 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005235 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005236 int rc = 0;
5237
5238 if (list_empty(&ha->tgt.q_full_list))
5239 return 0;
5240
5241 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005242 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005243
Quinn Tran82de8022017-06-13 20:47:17 -07005244 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005245 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005246 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005247 return 0;
5248 }
5249
Quinn Tran82de8022017-06-13 20:47:17 -07005250 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5251 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5252
5253 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5254 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005255 if (cmd->q_full)
5256 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005257 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005258 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005259 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005260
5261 if (rc == -ENOMEM)
5262 break;
5263
5264 if (cmd->q_full)
5265 ql_dbg(ql_dbg_io, vha, 0x3006,
5266 "%s: busy sent for ox_id[%04x]\n", __func__,
5267 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5268 else if (cmd->term_exchg)
5269 ql_dbg(ql_dbg_io, vha, 0x3007,
5270 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5271 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5272 else
5273 ql_dbg(ql_dbg_io, vha, 0x3008,
5274 "%s: Unexpected cmd in QFull list %p\n", __func__,
5275 cmd);
5276
5277 list_del(&cmd->cmd_list);
5278 list_add_tail(&cmd->cmd_list, &free_list);
5279
5280 /* piggy back on hardware_lock for protection */
5281 vha->hw->tgt.num_qfull_cmds_alloc--;
5282 }
Quinn Tran82de8022017-06-13 20:47:17 -07005283 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005284
5285 cmd = NULL;
5286
5287 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5288 list_del(&cmd->cmd_list);
5289 /* This cmd was never sent to TCM. There is no need
5290 * to schedule free or call free_cmd
5291 */
5292 qlt_free_cmd(cmd);
5293 }
Quinn Tran82de8022017-06-13 20:47:17 -07005294
5295 if (!list_empty(&q_full_list)) {
5296 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5297 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5298 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5299 }
5300
Quinn Tran33e79972014-09-25 06:14:55 -04005301 return rc;
5302}
5303
5304static void
Quinn Tran82de8022017-06-13 20:47:17 -07005305qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5306 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005307{
5308 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005309 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005310
Quinn Tran82de8022017-06-13 20:47:17 -07005311 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005312 if (rc == -ENOMEM)
5313 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5314}
5315
5316static int
Quinn Tran82de8022017-06-13 20:47:17 -07005317qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5318 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005319{
5320 struct qla_hw_data *ha = vha->hw;
5321 uint16_t status;
Quinn Tran8b666802017-03-15 09:48:45 -07005322 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005323
5324 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5325 return 0;
5326
Quinn Tran8b666802017-03-15 09:48:45 -07005327 if (!ha_locked)
5328 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005329 status = temp_sam_status;
Quinn Tran82de8022017-06-13 20:47:17 -07005330 qlt_send_busy(qpair, atio, status);
Quinn Tran8b666802017-03-15 09:48:45 -07005331 if (!ha_locked)
5332 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5333
Quinn Tran33e79972014-09-25 06:14:55 -04005334 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005335}
5336
5337/* ha->hardware_lock supposed to be held on entry */
5338/* called via callback from qla2xxx */
5339static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005340 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005341{
5342 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005343 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005344 int rc;
Quinn Tran2f424b92015-12-17 14:57:07 -05005345 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005346
5347 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005348 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005349 "ATIO pkt, but no tgt (ha %p)", ha);
5350 return;
5351 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005352 /*
5353 * In tgt_stop mode we also should allow all requests to pass.
5354 * Otherwise, some commands can stuck.
5355 */
5356
Quinn Tran2f424b92015-12-17 14:57:07 -05005357 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005358
5359 switch (atio->u.raw.entry_type) {
5360 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005361 if (unlikely(atio->u.isp24.exchange_addr ==
5362 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005363 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005364 "qla_target(%d): ATIO_TYPE7 "
5365 "received with UNKNOWN exchange address, "
5366 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005367 if (!ha_locked)
5368 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07005369 qlt_send_busy(ha->base_qpair, atio,
5370 SAM_STAT_TASK_SET_FULL);
Quinn Tran2f424b92015-12-17 14:57:07 -05005371 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005372 spin_unlock_irqrestore(&ha->hardware_lock,
5373 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005374 break;
5375 }
Quinn Tran33e79972014-09-25 06:14:55 -04005376
Quinn Tran33e79972014-09-25 06:14:55 -04005377 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005378 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5379 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005380 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005381 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005382 return;
5383 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005384 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005385 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005386 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005387 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005388 if (unlikely(rc != 0)) {
5389 if (rc == -ESRCH) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005390 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005391 spin_lock_irqsave(&ha->hardware_lock,
5392 flags);
Quinn Tran2f424b92015-12-17 14:57:07 -05005393
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005394#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
Quinn Tran82de8022017-06-13 20:47:17 -07005395 qlt_send_busy(ha->base_qpair, atio,
5396 SAM_STAT_BUSY);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005397#else
Quinn Tran82de8022017-06-13 20:47:17 -07005398 qlt_send_term_exchange(ha->base_qpair, NULL,
5399 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005400#endif
Quinn Tran2f424b92015-12-17 14:57:07 -05005401 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005402 spin_unlock_irqrestore(
5403 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005404 } else {
5405 if (tgt->tgt_stop) {
5406 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5407 "qla_target: Unable to send "
5408 "command to target for req, "
5409 "ignoring.\n");
5410 } else {
5411 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5412 "qla_target(%d): Unable to send "
5413 "command to target, sending BUSY "
5414 "status.\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005415 if (!ha_locked)
5416 spin_lock_irqsave(
5417 &ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07005418 qlt_send_busy(ha->base_qpair,
5419 atio, SAM_STAT_BUSY);
Quinn Tran2f424b92015-12-17 14:57:07 -05005420 if (!ha_locked)
5421 spin_unlock_irqrestore(
5422 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005423 }
5424 }
5425 }
5426 break;
5427
5428 case IMMED_NOTIFY_TYPE:
5429 {
5430 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5431 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5432 "qla_target(%d): Received ATIO packet %x "
5433 "with error status %x\n", vha->vp_idx,
5434 atio->u.raw.entry_type,
5435 atio->u.isp2x.entry_status);
5436 break;
5437 }
5438 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005439
5440 if (!ha_locked)
5441 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005442 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005443 if (!ha_locked)
5444 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005445 break;
5446 }
5447
5448 default:
5449 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5450 "qla_target(%d): Received unknown ATIO atio "
5451 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5452 break;
5453 }
5454
Quinn Tran2f424b92015-12-17 14:57:07 -05005455 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005456}
5457
5458/* ha->hardware_lock supposed to be held on entry */
5459/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005460static void qlt_response_pkt(struct scsi_qla_host *vha,
5461 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005462{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005463 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005464
5465 if (unlikely(tgt == NULL)) {
5466 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005467 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5468 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005469 return;
5470 }
5471
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005472 /*
5473 * In tgt_stop mode we also should allow all requests to pass.
5474 * Otherwise, some commands can stuck.
5475 */
5476
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005477 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005478 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005479 case CTIO_TYPE7:
5480 {
5481 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005482 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005483 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5484 entry);
5485 break;
5486 }
5487
5488 case ACCEPT_TGT_IO_TYPE:
5489 {
5490 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5491 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005492 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005493 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005494 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5495 "qla_target(%d): ATIO with error "
5496 "status %x received\n", vha->vp_idx,
5497 le16_to_cpu(atio->u.isp2x.status));
5498 break;
5499 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005500
Quinn Tran82de8022017-06-13 20:47:17 -07005501 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005502 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005503 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005504
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005505 rc = qlt_handle_cmd_for_atio(vha, atio);
5506 if (unlikely(rc != 0)) {
5507 if (rc == -ESRCH) {
5508#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
Quinn Tran82de8022017-06-13 20:47:17 -07005509 qlt_send_busy(rsp->qpair, atio, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005510#else
Quinn Tran82de8022017-06-13 20:47:17 -07005511 qlt_send_term_exchange(rsp->qpair, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005512#endif
5513 } else {
5514 if (tgt->tgt_stop) {
5515 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5516 "qla_target: Unable to send "
5517 "command to target, sending TERM "
5518 "EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005519 qlt_send_term_exchange(rsp->qpair, NULL,
Quinn Trana07100e2015-12-07 19:48:57 -05005520 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005521 } else {
5522 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5523 "qla_target(%d): Unable to send "
5524 "command to target, sending BUSY "
5525 "status\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07005526 qlt_send_busy(rsp->qpair, atio, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005527 }
5528 }
5529 }
5530 }
5531 break;
5532
5533 case CONTINUE_TGT_IO_TYPE:
5534 {
5535 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005536 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005537 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5538 entry);
5539 break;
5540 }
5541
5542 case CTIO_A64_TYPE:
5543 {
5544 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005545 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005546 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5547 entry);
5548 break;
5549 }
5550
5551 case IMMED_NOTIFY_TYPE:
5552 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5553 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5554 break;
5555
5556 case NOTIFY_ACK_TYPE:
5557 if (tgt->notify_ack_expected > 0) {
5558 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5559 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5560 "NOTIFY_ACK seq %08x status %x\n",
5561 le16_to_cpu(entry->u.isp2x.seq_id),
5562 le16_to_cpu(entry->u.isp2x.status));
5563 tgt->notify_ack_expected--;
5564 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005565 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005566 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5567 "qla_target(%d): NOTIFY_ACK "
5568 "failed %x\n", vha->vp_idx,
5569 le16_to_cpu(entry->u.isp2x.status));
5570 }
5571 } else {
5572 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5573 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5574 vha->vp_idx);
5575 }
5576 break;
5577
5578 case ABTS_RECV_24XX:
5579 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5580 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5581 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5582 break;
5583
5584 case ABTS_RESP_24XX:
5585 if (tgt->abts_resp_expected > 0) {
5586 struct abts_resp_from_24xx_fw *entry =
5587 (struct abts_resp_from_24xx_fw *)pkt;
5588 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5589 "ABTS_RESP_24XX: compl_status %x\n",
5590 entry->compl_status);
5591 tgt->abts_resp_expected--;
5592 if (le16_to_cpu(entry->compl_status) !=
5593 ABTS_RESP_COMPL_SUCCESS) {
5594 if ((entry->error_subcode1 == 0x1E) &&
5595 (entry->error_subcode2 == 0)) {
5596 /*
5597 * We've got a race here: aborted
5598 * exchange not terminated, i.e.
5599 * response for the aborted command was
5600 * sent between the abort request was
5601 * received and processed.
5602 * Unfortunately, the firmware has a
5603 * silly requirement that all aborted
5604 * exchanges must be explicitely
5605 * terminated, otherwise it refuses to
5606 * send responses for the abort
5607 * requests. So, we have to
5608 * (re)terminate the exchange and retry
5609 * the abort response.
5610 */
5611 qlt_24xx_retry_term_exchange(vha,
5612 entry);
5613 } else
5614 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5615 "qla_target(%d): ABTS_RESP_24XX "
5616 "failed %x (subcode %x:%x)",
5617 vha->vp_idx, entry->compl_status,
5618 entry->error_subcode1,
5619 entry->error_subcode2);
5620 }
5621 } else {
5622 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5623 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5624 "received\n", vha->vp_idx);
5625 }
5626 break;
5627
5628 default:
5629 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5630 "qla_target(%d): Received unknown response pkt "
5631 "type %x\n", vha->vp_idx, pkt->entry_type);
5632 break;
5633 }
5634
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005635}
5636
5637/*
5638 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5639 */
5640void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5641 uint16_t *mailbox)
5642{
5643 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005644 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005645 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005646
Quinn Tran3a33dc92017-06-02 09:12:04 -07005647 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005648 return;
5649
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005650 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5651 IS_QLA2100(ha))
5652 return;
5653 /*
5654 * In tgt_stop mode we also should allow all requests to pass.
5655 * Otherwise, some commands can stuck.
5656 */
5657
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005658
5659 switch (code) {
5660 case MBA_RESET: /* Reset */
5661 case MBA_SYSTEM_ERR: /* System Error */
5662 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5663 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5664 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5665 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005666 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005667 break;
5668 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5669 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5670 break;
5671
5672 case MBA_LOOP_UP:
5673 {
5674 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005675 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005676 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5677 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5678 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005679 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005680 qlt_send_notify_ack(ha->base_qpair,
5681 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005682 0, 0, 0, 0, 0, 0);
5683 tgt->link_reinit_iocb_pending = 0;
5684 }
5685 break;
5686 }
5687
5688 case MBA_LIP_OCCURRED:
5689 case MBA_LOOP_DOWN:
5690 case MBA_LIP_RESET:
5691 case MBA_RSCN_UPDATE:
5692 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005693 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005694 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5695 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5696 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005697 break;
5698
Quinn Tranead03852017-01-19 22:28:01 -08005699 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005700 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5701 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5702 vha->vp_idx,
5703 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5704 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005705
5706 if (le16_to_cpu(mailbox[3]) == 1) {
5707 /* exchange starvation. */
5708 vha->hw->exch_starvation++;
5709 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005710 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08005711 "Exchange starvation-. Resetting RISC\n");
5712
5713 vha->hw->exch_starvation = 0;
5714 if (IS_P3P_TYPE(vha->hw))
5715 set_bit(FCOE_CTX_RESET_NEEDED,
5716 &vha->dpc_flags);
5717 else
5718 set_bit(ISP_ABORT_NEEDED,
5719 &vha->dpc_flags);
5720 qla2xxx_wake_dpc(vha);
5721 }
5722 }
5723 break;
5724
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005725 case MBA_PORT_UPDATE:
5726 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5727 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005728 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005729 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5730 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5731 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5732
5733 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08005734 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005735 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5736 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08005737 vha->hw->exch_starvation = 0;
5738 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005739 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5740 "Async MB 2: Port Logged Out\n");
5741 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005742 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005743 break;
5744 }
5745
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005746}
5747
5748static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5749 uint16_t loop_id)
5750{
Quinn Tran726b8542017-01-19 22:28:00 -08005751 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005752 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08005753 unsigned long flags;
5754 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005755
5756 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5757 if (!fcport) {
5758 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5759 "qla_target(%d): Allocation of tmp FC port failed",
5760 vha->vp_idx);
5761 return NULL;
5762 }
5763
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005764 fcport->loop_id = loop_id;
5765
Quinn Tran15f30a52017-03-15 09:48:52 -07005766 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005767 if (rc != QLA_SUCCESS) {
5768 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5769 "qla_target(%d): Failed to retrieve fcport "
5770 "information -- get_port_database() returned %x "
5771 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5772 kfree(fcport);
5773 return NULL;
5774 }
5775
Quinn Tran726b8542017-01-19 22:28:00 -08005776 del = NULL;
5777 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5778 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5779
5780 if (tfcp) {
5781 tfcp->d_id = fcport->d_id;
5782 tfcp->port_type = fcport->port_type;
5783 tfcp->supported_classes = fcport->supported_classes;
5784 tfcp->flags |= fcport->flags;
5785
5786 del = fcport;
5787 fcport = tfcp;
5788 } else {
5789 if (vha->hw->current_topology == ISP_CFG_F)
5790 fcport->flags |= FCF_FABRIC_DEVICE;
5791
5792 list_add_tail(&fcport->list, &vha->vp_fcports);
5793 if (!IS_SW_RESV_ADDR(fcport->d_id))
5794 vha->fcport_count++;
5795 fcport->login_gen++;
5796 fcport->disc_state = DSC_LOGIN_COMPLETE;
5797 fcport->login_succ = 1;
5798 newfcport = 1;
5799 }
5800
5801 fcport->deleted = 0;
5802 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5803
5804 switch (vha->host->active_mode) {
5805 case MODE_INITIATOR:
5806 case MODE_DUAL:
5807 if (newfcport) {
5808 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005809 ql_dbg(ql_dbg_disc, vha, 0x20fe,
Quinn Tran726b8542017-01-19 22:28:00 -08005810 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5811 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5812 qla24xx_post_upd_fcport_work(vha, fcport);
5813 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07005814 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08005815 "%s %d %8phC post gpsc fcp_cnt %d\n",
5816 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5817 qla24xx_post_gpsc_work(vha, fcport);
5818 }
5819 }
5820 break;
5821
5822 case MODE_TARGET:
5823 default:
5824 break;
5825 }
5826 if (del)
5827 qla2x00_free_fcport(del);
5828
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005829 return fcport;
5830}
5831
5832/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08005833static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005834 uint8_t *s_id)
5835{
Quinn Tran5d964832017-01-19 22:27:59 -08005836 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005837 fc_port_t *fcport = NULL;
5838 int rc, global_resets;
5839 uint16_t loop_id = 0;
5840
Quinn Tran726b8542017-01-19 22:28:00 -08005841 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5842 /*
5843 * This is Domain Controller, so it should be
5844 * OK to drop SCSI commands from it.
5845 */
5846 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5847 "Unable to find initiator with S_ID %x:%x:%x",
5848 s_id[0], s_id[1], s_id[2]);
5849 return NULL;
5850 }
5851
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005852 mutex_lock(&vha->vha_tgt.tgt_mutex);
5853
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005854retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005855 global_resets =
5856 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005857
5858 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5859 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005860 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5861
Quinn Tran726b8542017-01-19 22:28:00 -08005862 ql_log(ql_log_info, vha, 0xf071,
5863 "qla_target(%d): Unable to find "
5864 "initiator with S_ID %x:%x:%x",
5865 vha->vp_idx, s_id[0], s_id[1],
5866 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005867
5868 if (rc == -ENOENT) {
5869 qlt_port_logo_t logo;
5870 sid_to_portid(s_id, &logo.id);
5871 logo.cmd_count = 1;
5872 qlt_send_first_logo(vha, &logo);
5873 }
5874
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005875 return NULL;
5876 }
5877
5878 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005879 if (!fcport) {
5880 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005881 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005882 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005883
5884 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005885 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005886 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5887 "qla_target(%d): global reset during session discovery "
5888 "(counter was %d, new %d), retrying", vha->vp_idx,
5889 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005890 atomic_read(&vha->vha_tgt.
5891 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005892 goto retry;
5893 }
5894
5895 sess = qlt_create_sess(vha, fcport, true);
5896
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005897 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5898
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005899 return sess;
5900}
5901
5902static void qlt_abort_work(struct qla_tgt *tgt,
5903 struct qla_tgt_sess_work_param *prm)
5904{
5905 struct scsi_qla_host *vha = tgt->vha;
5906 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005907 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005908 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005909 uint32_t be_s_id;
5910 uint8_t s_id[3];
5911 int rc;
5912
Quinn Tran75601512015-12-17 14:57:04 -05005913 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005914
5915 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005916 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005917
5918 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5919 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5920 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5921
5922 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5923 (unsigned char *)&be_s_id);
5924 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005925 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005926
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005927 sess = qlt_make_local_sess(vha, s_id);
5928 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005929
Quinn Tran75601512015-12-17 14:57:04 -05005930 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005931 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005932 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005933 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005934 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005935 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005936 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005937 }
5938
Quinn Tran726b8542017-01-19 22:28:00 -08005939 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005940 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08005941 "%s: kref_get fail %8phC \n",
5942 __func__, sess->port_name);
5943 sess = NULL;
5944 goto out_term2;
5945 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005946 }
5947
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005948 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005949 ha->tgt.tgt_ops->put_sess(sess);
5950 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5951
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005952 if (rc != 0)
5953 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005954 return;
5955
Quinn Tran75601512015-12-17 14:57:04 -05005956out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08005957 if (sess)
5958 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005959 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005960
5961out_term:
5962 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07005963 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
5964 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005965 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005966}
5967
5968static void qlt_tmr_work(struct qla_tgt *tgt,
5969 struct qla_tgt_sess_work_param *prm)
5970{
5971 struct atio_from_isp *a = &prm->tm_iocb2;
5972 struct scsi_qla_host *vha = tgt->vha;
5973 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005974 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005975 unsigned long flags;
5976 uint8_t *s_id = NULL; /* to hide compiler warnings */
5977 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07005978 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07005979 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005980 void *iocb;
5981
Quinn Tran75601512015-12-17 14:57:04 -05005982 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005983
5984 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005985 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005986
5987 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5988 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5989 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005990 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005991
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005992 sess = qlt_make_local_sess(vha, s_id);
5993 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005994
Quinn Tran75601512015-12-17 14:57:04 -05005995 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005996 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005997 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005998 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005999 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006000 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006001 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006002 }
6003
Quinn Tran726b8542017-01-19 22:28:00 -08006004 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006005 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08006006 "%s: kref_get fail %8phC\n",
6007 __func__, sess->port_name);
6008 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006009 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006010 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006011 }
6012
6013 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006014 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006015 unpacked_lun =
6016 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006017
6018 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006019 ha->tgt.tgt_ops->put_sess(sess);
6020 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6021
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006022 if (rc != 0)
6023 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006024 return;
6025
Quinn Tranf159b3c2017-03-15 09:48:47 -07006026out_term2:
6027 if (sess)
6028 ha->tgt.tgt_ops->put_sess(sess);
6029 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006030out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006031 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006032}
6033
6034static void qlt_sess_work_fn(struct work_struct *work)
6035{
6036 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6037 struct scsi_qla_host *vha = tgt->vha;
6038 unsigned long flags;
6039
6040 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6041
6042 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6043 while (!list_empty(&tgt->sess_works_list)) {
6044 struct qla_tgt_sess_work_param *prm = list_entry(
6045 tgt->sess_works_list.next, typeof(*prm),
6046 sess_works_list_entry);
6047
6048 /*
6049 * This work can be scheduled on several CPUs at time, so we
6050 * must delete the entry to eliminate double processing
6051 */
6052 list_del(&prm->sess_works_list_entry);
6053
6054 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6055
6056 switch (prm->type) {
6057 case QLA_TGT_SESS_WORK_ABORT:
6058 qlt_abort_work(tgt, prm);
6059 break;
6060 case QLA_TGT_SESS_WORK_TM:
6061 qlt_tmr_work(tgt, prm);
6062 break;
6063 default:
6064 BUG_ON(1);
6065 break;
6066 }
6067
6068 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6069
6070 kfree(prm);
6071 }
6072 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6073}
6074
6075/* Must be called under tgt_host_action_mutex */
6076int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6077{
6078 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006079 int rc, i;
6080 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006081
6082 if (!QLA_TGT_MODE_ENABLED())
6083 return 0;
6084
Arun Easi33c36c02013-01-30 03:34:41 -05006085 if (!IS_TGT_MODE_CAPABLE(ha)) {
6086 ql_log(ql_log_warn, base_vha, 0xe070,
6087 "This adapter does not support target mode.\n");
6088 return 0;
6089 }
6090
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006091 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006092 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006093
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006094 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006095
6096 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6097 if (!tgt) {
6098 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6099 "Unable to allocate struct qla_tgt\n");
6100 return -ENOMEM;
6101 }
6102
Quinn Trane326d222017-06-13 20:47:18 -07006103 tgt->qphints = kzalloc((ha->max_qpairs + 1) *
6104 sizeof(struct qla_qpair_hint), GFP_KERNEL);
6105 if (!tgt->qphints) {
6106 kfree(tgt);
6107 ql_log(ql_log_warn, base_vha, 0x0197,
6108 "Unable to allocate qpair hints.\n");
6109 return -ENOMEM;
6110 }
6111
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006112 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6113 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6114
Quinn Trane326d222017-06-13 20:47:18 -07006115 rc = btree_init64(&tgt->lun_qpair_map);
6116 if (rc) {
6117 kfree(tgt->qphints);
6118 kfree(tgt);
6119 ql_log(ql_log_info, base_vha, 0x0198,
6120 "Unable to initialize lun_qpair_map btree\n");
6121 return -EIO;
6122 }
6123 h = &tgt->qphints[0];
6124 h->qpair = ha->base_qpair;
6125 INIT_LIST_HEAD(&h->hint_elem);
6126 h->cpuid = ha->base_qpair->cpuid;
6127 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6128
6129 for (i = 0; i < ha->max_qpairs; i++) {
6130 unsigned long flags;
6131
6132 struct qla_qpair *qpair = ha->queue_pair_map[i];
6133 h = &tgt->qphints[i + 1];
6134 INIT_LIST_HEAD(&h->hint_elem);
6135 if (qpair) {
6136 h->qpair = qpair;
6137 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6138 list_add_tail(&h->hint_elem, &qpair->hints_list);
6139 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6140 h->cpuid = qpair->cpuid;
6141 }
6142 }
6143
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006144 tgt->ha = ha;
6145 tgt->vha = base_vha;
6146 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006147 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006148 spin_lock_init(&tgt->sess_work_lock);
6149 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6150 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006151 atomic_set(&tgt->tgt_global_resets_count, 0);
6152
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006153 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006154
6155 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6156 "qla_target(%d): using 64 Bit PCI addressing",
6157 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006158 /* 3 is reserved */
6159 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006160
6161 mutex_lock(&qla_tgt_mutex);
6162 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6163 mutex_unlock(&qla_tgt_mutex);
6164
Quinn Tranf1443ee2017-03-15 09:48:51 -07006165 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6166 ha->tgt.tgt_ops->add_target(base_vha);
6167
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006168 return 0;
6169}
6170
6171/* Must be called under tgt_host_action_mutex */
6172int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6173{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006174 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006175 return 0;
6176
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006177 if (vha->fc_vport) {
6178 qlt_release(vha->vha_tgt.qla_tgt);
6179 return 0;
6180 }
Quinn Tran33e79972014-09-25 06:14:55 -04006181
6182 /* free left over qfull cmds */
6183 qlt_init_term_exchange(vha);
6184
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006185 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6186 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006187 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006188
6189 return 0;
6190}
6191
Quinn Tran482c9dc2017-03-15 09:48:54 -07006192void qlt_remove_target_resources(struct qla_hw_data *ha)
6193{
6194 struct scsi_qla_host *node;
6195 u32 key = 0;
6196
6197 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6198 btree_remove32(&ha->tgt.host_map, key);
6199
6200 btree_destroy32(&ha->tgt.host_map);
6201}
6202
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006203static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6204 unsigned char *b)
6205{
6206 int i;
6207
6208 pr_debug("qla2xxx HW vha->node_name: ");
6209 for (i = 0; i < WWN_SIZE; i++)
6210 pr_debug("%02x ", vha->node_name[i]);
6211 pr_debug("\n");
6212 pr_debug("qla2xxx HW vha->port_name: ");
6213 for (i = 0; i < WWN_SIZE; i++)
6214 pr_debug("%02x ", vha->port_name[i]);
6215 pr_debug("\n");
6216
6217 pr_debug("qla2xxx passed configfs WWPN: ");
6218 put_unaligned_be64(wwpn, b);
6219 for (i = 0; i < WWN_SIZE; i++)
6220 pr_debug("%02x ", b[i]);
6221 pr_debug("\n");
6222}
6223
6224/**
6225 * qla_tgt_lport_register - register lport with external module
6226 *
6227 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6228 * @wwpn: Passwd FC target WWPN
6229 * @callback: lport initialization callback for tcm_qla2xxx code
6230 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6231 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006232int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6233 u64 npiv_wwpn, u64 npiv_wwnn,
6234 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006235{
6236 struct qla_tgt *tgt;
6237 struct scsi_qla_host *vha;
6238 struct qla_hw_data *ha;
6239 struct Scsi_Host *host;
6240 unsigned long flags;
6241 int rc;
6242 u8 b[WWN_SIZE];
6243
6244 mutex_lock(&qla_tgt_mutex);
6245 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6246 vha = tgt->vha;
6247 ha = vha->hw;
6248
6249 host = vha->host;
6250 if (!host)
6251 continue;
6252
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006253 if (!(host->hostt->supported_mode & MODE_TARGET))
6254 continue;
6255
6256 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006257 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006258 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6259 host->host_no);
6260 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6261 continue;
6262 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006263 if (tgt->tgt_stop) {
6264 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6265 host->host_no);
6266 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6267 continue;
6268 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006269 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6270
6271 if (!scsi_host_get(host)) {
6272 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6273 "Unable to scsi_host_get() for"
6274 " qla2xxx scsi_host\n");
6275 continue;
6276 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006277 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006278
6279 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6280 scsi_host_put(host);
6281 continue;
6282 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006283 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6284 if (rc != 0)
6285 scsi_host_put(host);
6286
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006287 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006288 return rc;
6289 }
6290 mutex_unlock(&qla_tgt_mutex);
6291
6292 return -ENODEV;
6293}
6294EXPORT_SYMBOL(qlt_lport_register);
6295
6296/**
6297 * qla_tgt_lport_deregister - Degister lport
6298 *
6299 * @vha: Registered scsi_qla_host pointer
6300 */
6301void qlt_lport_deregister(struct scsi_qla_host *vha)
6302{
6303 struct qla_hw_data *ha = vha->hw;
6304 struct Scsi_Host *sh = vha->host;
6305 /*
6306 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6307 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006308 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006309 ha->tgt.tgt_ops = NULL;
6310 /*
6311 * Release the Scsi_Host reference for the underlying qla2xxx host
6312 */
6313 scsi_host_put(sh);
6314}
6315EXPORT_SYMBOL(qlt_lport_deregister);
6316
6317/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006318static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006319{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006320 switch (ql2x_ini_mode) {
6321 case QLA2XXX_INI_MODE_DISABLED:
6322 case QLA2XXX_INI_MODE_EXCLUSIVE:
6323 vha->host->active_mode = MODE_TARGET;
6324 break;
6325 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006326 vha->host->active_mode = MODE_UNKNOWN;
6327 break;
6328 case QLA2XXX_INI_MODE_DUAL:
6329 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006330 break;
6331 default:
6332 break;
6333 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006334}
6335
6336/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006337static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006338{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006339 switch (ql2x_ini_mode) {
6340 case QLA2XXX_INI_MODE_DISABLED:
6341 vha->host->active_mode = MODE_UNKNOWN;
6342 break;
6343 case QLA2XXX_INI_MODE_EXCLUSIVE:
6344 vha->host->active_mode = MODE_INITIATOR;
6345 break;
6346 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006347 case QLA2XXX_INI_MODE_DUAL:
6348 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006349 break;
6350 default:
6351 break;
6352 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006353}
6354
6355/*
6356 * qla_tgt_enable_vha - NO LOCK HELD
6357 *
6358 * host_reset, bring up w/ Target Mode Enabled
6359 */
6360void
6361qlt_enable_vha(struct scsi_qla_host *vha)
6362{
6363 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006364 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006365 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006366 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006367
6368 if (!tgt) {
6369 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6370 "Unable to locate qla_tgt pointer from"
6371 " struct qla_hw_data\n");
6372 dump_stack();
6373 return;
6374 }
6375
6376 spin_lock_irqsave(&ha->hardware_lock, flags);
6377 tgt->tgt_stopped = 0;
6378 qlt_set_mode(vha);
6379 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6380
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006381 if (vha->vp_idx) {
6382 qla24xx_disable_vp(vha);
6383 qla24xx_enable_vp(vha);
6384 } else {
6385 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6386 qla2xxx_wake_dpc(base_vha);
6387 qla2x00_wait_for_hba_online(base_vha);
6388 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006389}
6390EXPORT_SYMBOL(qlt_enable_vha);
6391
6392/*
6393 * qla_tgt_disable_vha - NO LOCK HELD
6394 *
6395 * Disable Target Mode and reset the adapter
6396 */
Joern Engel55a90662014-09-16 16:23:15 -04006397static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006398{
6399 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006400 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006401 unsigned long flags;
6402
6403 if (!tgt) {
6404 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6405 "Unable to locate qla_tgt pointer from"
6406 " struct qla_hw_data\n");
6407 dump_stack();
6408 return;
6409 }
6410
6411 spin_lock_irqsave(&ha->hardware_lock, flags);
6412 qlt_clear_mode(vha);
6413 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6414
6415 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6416 qla2xxx_wake_dpc(vha);
6417 qla2x00_wait_for_hba_online(vha);
6418}
6419
6420/*
6421 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6422 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6423 * members.
6424 */
6425void
6426qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6427{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006428 vha->vha_tgt.qla_tgt = NULL;
6429
6430 mutex_init(&vha->vha_tgt.tgt_mutex);
6431 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006432
6433 qlt_clear_mode(vha);
6434
6435 /*
6436 * NOTE: Currently the value is kept the same for <24xx and
6437 * >=24xx ISPs. If it is necessary to change it,
6438 * the check should be added for specific ISPs,
6439 * assigning the value appropriately.
6440 */
6441 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006442
6443 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006444}
6445
6446void
6447qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6448{
6449 /*
6450 * FC-4 Feature bit 0 indicates target functionality to the name server.
6451 */
6452 if (qla_tgt_mode_enabled(vha)) {
Quinn Tran726b8542017-01-19 22:28:00 -08006453 ct_req->req.rff_id.fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006454 } else if (qla_ini_mode_enabled(vha)) {
6455 ct_req->req.rff_id.fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006456 } else if (qla_dual_mode_enabled(vha))
6457 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006458}
6459
6460/*
6461 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6462 * @ha: HA context
6463 *
6464 * Beginning of ATIO ring has initialization control block already built
6465 * by nvram config routine.
6466 *
6467 * Returns 0 on success.
6468 */
6469void
6470qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6471{
6472 struct qla_hw_data *ha = vha->hw;
6473 uint16_t cnt;
6474 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6475
Quinn Tranead03852017-01-19 22:28:01 -08006476 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006477 return;
6478
6479 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6480 pkt->u.raw.signature = ATIO_PROCESSED;
6481 pkt++;
6482 }
6483
6484}
6485
6486/*
6487 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6488 * @ha: SCSI driver HA context
6489 */
6490void
Quinn Tran2f424b92015-12-17 14:57:07 -05006491qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006492{
6493 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006494 struct atio_from_isp *pkt;
6495 int cnt, i;
6496
Quinn Tranec7193e2017-03-15 09:48:55 -07006497 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006498 return;
6499
Quinn Tran5f355092016-12-23 18:06:11 -08006500 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6501 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006502 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6503 cnt = pkt->u.raw.entry_count;
6504
Quinn Tran5f355092016-12-23 18:06:11 -08006505 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6506 /*
6507 * This packet is corrupted. The header + payload
6508 * can not be trusted. There is no point in passing
6509 * it further up.
6510 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006511 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006512 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6513 pkt->u.isp24.fcp_hdr.s_id,
6514 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6515 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6516
6517 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006518 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6519 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006520 } else {
6521 qlt_24xx_atio_pkt_all_vps(vha,
6522 (struct atio_from_isp *)pkt, ha_locked);
6523 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006524
6525 for (i = 0; i < cnt; i++) {
6526 ha->tgt.atio_ring_index++;
6527 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6528 ha->tgt.atio_ring_index = 0;
6529 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6530 } else
6531 ha->tgt.atio_ring_ptr++;
6532
6533 pkt->u.raw.signature = ATIO_PROCESSED;
6534 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6535 }
6536 wmb();
6537 }
6538
6539 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006540 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006541}
6542
6543void
Arun Easiaa230bc2013-01-30 03:34:39 -05006544qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006545{
6546 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05006547 if (!QLA_TGT_MODE_ENABLED())
6548 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006549
Arun Easiaa230bc2013-01-30 03:34:39 -05006550 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6551 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6552 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6553
6554 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6555 struct qla_msix_entry *msix = &ha->msix_entries[2];
6556 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6557
6558 icb->msix_atio = cpu_to_le16(msix->entry);
6559 ql_dbg(ql_dbg_init, vha, 0xf072,
6560 "Registering ICB vector 0x%x for atio que.\n",
6561 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006562 }
6563}
6564
6565void
6566qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6567{
6568 struct qla_hw_data *ha = vha->hw;
Quinn Tran99e1b682017-06-02 09:12:03 -07006569
6570 if (!QLA_TGT_MODE_ENABLED())
6571 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006572
Quinn Tranead03852017-01-19 22:28:01 -08006573 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006574 if (!ha->tgt.saved_set) {
6575 /* We save only once */
6576 ha->tgt.saved_exchange_count = nv->exchange_count;
6577 ha->tgt.saved_firmware_options_1 =
6578 nv->firmware_options_1;
6579 ha->tgt.saved_firmware_options_2 =
6580 nv->firmware_options_2;
6581 ha->tgt.saved_firmware_options_3 =
6582 nv->firmware_options_3;
6583 ha->tgt.saved_set = 1;
6584 }
6585
Quinn Tran99e1b682017-06-02 09:12:03 -07006586 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006587 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006588 else /* dual */
6589 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006590
6591 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006592 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006593
6594 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006595 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006596 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006597
6598 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006599 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006600 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006601 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006602 if (ql2xtgt_tape_enable)
6603 /* Enable FC Tape support */
6604 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6605 else
6606 /* Disable FC Tape support */
6607 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6608
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006609 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006610 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006611
6612 /*
6613 * clear BIT 15 explicitly as we have seen at least
6614 * a couple of instances where this was set and this
6615 * was causing the firmware to not be initialized.
6616 */
6617 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006618 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006619 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006620 } else {
6621 if (ha->tgt.saved_set) {
6622 nv->exchange_count = ha->tgt.saved_exchange_count;
6623 nv->firmware_options_1 =
6624 ha->tgt.saved_firmware_options_1;
6625 nv->firmware_options_2 =
6626 ha->tgt.saved_firmware_options_2;
6627 nv->firmware_options_3 =
6628 ha->tgt.saved_firmware_options_3;
6629 }
6630 return;
6631 }
6632
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006633 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006634 if (vha->flags.init_done)
6635 fc_host_supported_classes(vha->host) =
6636 FC_COS_CLASS2 | FC_COS_CLASS3;
6637
Bart Van Asschead950362015-07-09 07:24:08 -07006638 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006639 } else {
6640 if (vha->flags.init_done)
6641 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6642
Bart Van Asschead950362015-07-09 07:24:08 -07006643 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006644 }
6645}
6646
6647void
6648qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6649 struct init_cb_24xx *icb)
6650{
6651 struct qla_hw_data *ha = vha->hw;
6652
Quinn Tran481ce732015-12-17 14:57:08 -05006653 if (!QLA_TGT_MODE_ENABLED())
6654 return;
6655
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006656 if (ha->tgt.node_name_set) {
6657 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006658 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006659 }
Quinn Tran481ce732015-12-17 14:57:08 -05006660
6661 /* disable ZIO at start time. */
6662 if (!vha->flags.init_done) {
6663 uint32_t tmp;
6664 tmp = le32_to_cpu(icb->firmware_options_2);
6665 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6666 icb->firmware_options_2 = cpu_to_le32(tmp);
6667 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006668}
6669
Arun Easiaa230bc2013-01-30 03:34:39 -05006670void
6671qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6672{
6673 struct qla_hw_data *ha = vha->hw;
6674
6675 if (!QLA_TGT_MODE_ENABLED())
6676 return;
6677
Quinn Tranead03852017-01-19 22:28:01 -08006678 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006679 if (!ha->tgt.saved_set) {
6680 /* We save only once */
6681 ha->tgt.saved_exchange_count = nv->exchange_count;
6682 ha->tgt.saved_firmware_options_1 =
6683 nv->firmware_options_1;
6684 ha->tgt.saved_firmware_options_2 =
6685 nv->firmware_options_2;
6686 ha->tgt.saved_firmware_options_3 =
6687 nv->firmware_options_3;
6688 ha->tgt.saved_set = 1;
6689 }
6690
Quinn Tran99e1b682017-06-02 09:12:03 -07006691 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006692 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006693 else /* dual */
6694 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05006695
6696 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006697 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006698
6699 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006700 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006701 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006702 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006703 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006704 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006705 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006706 /*
6707 * clear BIT 15 explicitly as we have seen at
6708 * least a couple of instances where this was set
6709 * and this was causing the firmware to not be
6710 * initialized.
6711 */
6712 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006713 if (ql2xtgt_tape_enable)
6714 /* Enable FC tape support */
6715 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6716 else
6717 /* Disable FC tape support */
6718 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6719
Arun Easiaa230bc2013-01-30 03:34:39 -05006720 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006721 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006722 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006723 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006724 } else {
6725 if (ha->tgt.saved_set) {
6726 nv->exchange_count = ha->tgt.saved_exchange_count;
6727 nv->firmware_options_1 =
6728 ha->tgt.saved_firmware_options_1;
6729 nv->firmware_options_2 =
6730 ha->tgt.saved_firmware_options_2;
6731 nv->firmware_options_3 =
6732 ha->tgt.saved_firmware_options_3;
6733 }
6734 return;
6735 }
6736
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006737 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006738 if (vha->flags.init_done)
6739 fc_host_supported_classes(vha->host) =
6740 FC_COS_CLASS2 | FC_COS_CLASS3;
6741
Bart Van Asschead950362015-07-09 07:24:08 -07006742 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006743 } else {
6744 if (vha->flags.init_done)
6745 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6746
Bart Van Asschead950362015-07-09 07:24:08 -07006747 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006748 }
6749}
6750
6751void
6752qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6753 struct init_cb_81xx *icb)
6754{
6755 struct qla_hw_data *ha = vha->hw;
6756
6757 if (!QLA_TGT_MODE_ENABLED())
6758 return;
6759
6760 if (ha->tgt.node_name_set) {
6761 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006762 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006763 }
Quinn Tran481ce732015-12-17 14:57:08 -05006764
6765 /* disable ZIO at start time. */
6766 if (!vha->flags.init_done) {
6767 uint32_t tmp;
6768 tmp = le32_to_cpu(icb->firmware_options_2);
6769 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6770 icb->firmware_options_2 = cpu_to_le32(tmp);
6771 }
6772
Arun Easiaa230bc2013-01-30 03:34:39 -05006773}
6774
6775void
6776qlt_83xx_iospace_config(struct qla_hw_data *ha)
6777{
6778 if (!QLA_TGT_MODE_ENABLED())
6779 return;
6780
6781 ha->msix_count += 1; /* For ATIO Q */
6782}
6783
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006784
6785void
6786qlt_modify_vp_config(struct scsi_qla_host *vha,
6787 struct vp_config_entry_24xx *vpmod)
6788{
Quinn Tranead03852017-01-19 22:28:01 -08006789 /* enable target mode. Bit5 = 1 => disable */
6790 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006791 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08006792
Quinn Tranead03852017-01-19 22:28:01 -08006793 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08006794 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006795 vpmod->options_idx1 &= ~BIT_4;
6796}
6797
6798void
6799qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6800{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006801 int rc;
6802
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006803 if (!QLA_TGT_MODE_ENABLED())
6804 return;
6805
Michael Hernandezb7edfa22017-08-23 15:04:56 -07006806 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006807 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6808 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6809 } else {
6810 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6811 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6812 }
6813
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006814 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6815 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08006816
6817 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6818 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6819 qlt_unknown_atio_work_fn);
6820
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006821 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006822
6823 rc = btree_init32(&ha->tgt.host_map);
6824 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006825 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006826 "Unable to initialize ha->host_map btree\n");
6827
6828 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006829}
6830
Arun Easiaa230bc2013-01-30 03:34:39 -05006831irqreturn_t
6832qla83xx_msix_atio_q(int irq, void *dev_id)
6833{
6834 struct rsp_que *rsp;
6835 scsi_qla_host_t *vha;
6836 struct qla_hw_data *ha;
6837 unsigned long flags;
6838
6839 rsp = (struct rsp_que *) dev_id;
6840 ha = rsp->hw;
6841 vha = pci_get_drvdata(ha->pdev);
6842
Quinn Tran2f424b92015-12-17 14:57:07 -05006843 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006844
Quinn Tran2f424b92015-12-17 14:57:07 -05006845 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006846
Quinn Tran2f424b92015-12-17 14:57:07 -05006847 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006848
6849 return IRQ_HANDLED;
6850}
6851
Quinn Tran2f424b92015-12-17 14:57:07 -05006852static void
6853qlt_handle_abts_recv_work(struct work_struct *work)
6854{
6855 struct qla_tgt_sess_op *op = container_of(work,
6856 struct qla_tgt_sess_op, work);
6857 scsi_qla_host_t *vha = op->vha;
6858 struct qla_hw_data *ha = vha->hw;
6859 unsigned long flags;
6860
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006861 if (qla2x00_reset_active(vha) ||
6862 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05006863 return;
6864
6865 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6866 qlt_24xx_process_atio_queue(vha, 0);
6867 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6868
6869 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006870 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05006871 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07006872
6873 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05006874}
6875
6876void
Quinn Tran82de8022017-06-13 20:47:17 -07006877qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
6878 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05006879{
6880 struct qla_tgt_sess_op *op;
6881
6882 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6883
6884 if (!op) {
6885 /* do not reach for ATIO queue here. This is best effort err
6886 * recovery at this point.
6887 */
Quinn Tran82de8022017-06-13 20:47:17 -07006888 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05006889 return;
6890 }
6891
6892 memcpy(&op->atio, pkt, sizeof(*pkt));
6893 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006894 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07006895 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05006896 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6897 queue_work(qla_tgt_wq, &op->work);
6898 return;
6899}
6900
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006901int
6902qlt_mem_alloc(struct qla_hw_data *ha)
6903{
6904 if (!QLA_TGT_MODE_ENABLED())
6905 return 0;
6906
6907 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6908 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6909 if (!ha->tgt.tgt_vp_map)
6910 return -ENOMEM;
6911
6912 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6913 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6914 &ha->tgt.atio_dma, GFP_KERNEL);
6915 if (!ha->tgt.atio_ring) {
6916 kfree(ha->tgt.tgt_vp_map);
6917 return -ENOMEM;
6918 }
6919 return 0;
6920}
6921
6922void
6923qlt_mem_free(struct qla_hw_data *ha)
6924{
6925 if (!QLA_TGT_MODE_ENABLED())
6926 return;
6927
6928 if (ha->tgt.atio_ring) {
6929 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6930 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6931 ha->tgt.atio_dma);
6932 }
6933 kfree(ha->tgt.tgt_vp_map);
6934}
6935
6936/* vport_slock to be held by the caller */
6937void
6938qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6939{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006940 void *slot;
6941 u32 key;
6942 int rc;
6943
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006944 if (!QLA_TGT_MODE_ENABLED())
6945 return;
6946
Quinn Tran482c9dc2017-03-15 09:48:54 -07006947 key = vha->d_id.b24;
6948
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006949 switch (cmd) {
6950 case SET_VP_IDX:
6951 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6952 break;
6953 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07006954 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6955 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006956 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006957 "Save vha in host_map %p %06x\n", vha, key);
6958 rc = btree_insert32(&vha->hw->tgt.host_map,
6959 key, vha, GFP_ATOMIC);
6960 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006961 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006962 "Unable to insert s_id into host_map: %06x\n",
6963 key);
6964 return;
6965 }
Quinn Tran83548fe2017-06-02 09:12:01 -07006966 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6967 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006968 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006969 break;
6970 case RESET_VP_IDX:
6971 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6972 break;
6973 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07006974 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006975 "clear vha in host_map %p %06x\n", vha, key);
6976 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6977 if (slot)
6978 btree_remove32(&vha->hw->tgt.host_map, key);
6979 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006980 break;
6981 }
6982}
6983
Quinn Tran482c9dc2017-03-15 09:48:54 -07006984void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6985{
6986 unsigned long flags;
6987 struct qla_hw_data *ha = vha->hw;
6988
6989 if (!vha->d_id.b24) {
6990 spin_lock_irqsave(&ha->vport_slock, flags);
6991 vha->d_id = id;
6992 qlt_update_vp_map(vha, SET_AL_PA);
6993 spin_unlock_irqrestore(&ha->vport_slock, flags);
6994 } else if (vha->d_id.b24 != id.b24) {
6995 spin_lock_irqsave(&ha->vport_slock, flags);
6996 qlt_update_vp_map(vha, RESET_AL_PA);
6997 vha->d_id = id;
6998 qlt_update_vp_map(vha, SET_AL_PA);
6999 spin_unlock_irqrestore(&ha->vport_slock, flags);
7000 }
7001}
7002
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007003static int __init qlt_parse_ini_mode(void)
7004{
7005 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7006 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7007 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7008 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7009 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7010 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007011 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7012 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007013 else
7014 return false;
7015
7016 return true;
7017}
7018
7019int __init qlt_init(void)
7020{
7021 int ret;
7022
7023 if (!qlt_parse_ini_mode()) {
7024 ql_log(ql_log_fatal, NULL, 0xe06b,
7025 "qlt_parse_ini_mode() failed\n");
7026 return -EINVAL;
7027 }
7028
7029 if (!QLA_TGT_MODE_ENABLED())
7030 return 0;
7031
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007032 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7033 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7034 qla_tgt_mgmt_cmd), 0, NULL);
7035 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007036 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007037 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007038 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007039 }
7040
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007041 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007042 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7043 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007044
7045 if (!qla_tgt_plogi_cachep) {
7046 ql_log(ql_log_fatal, NULL, 0xe06d,
7047 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7048 ret = -ENOMEM;
7049 goto out_mgmt_cmd_cachep;
7050 }
7051
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007052 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7053 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7054 if (!qla_tgt_mgmt_cmd_mempool) {
7055 ql_log(ql_log_fatal, NULL, 0xe06e,
7056 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7057 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007058 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007059 }
7060
7061 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7062 if (!qla_tgt_wq) {
7063 ql_log(ql_log_fatal, NULL, 0xe06f,
7064 "alloc_workqueue for qla_tgt_wq failed\n");
7065 ret = -ENOMEM;
7066 goto out_cmd_mempool;
7067 }
7068 /*
7069 * Return 1 to signal that initiator-mode is being disabled
7070 */
7071 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7072
7073out_cmd_mempool:
7074 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007075out_plogi_cachep:
7076 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007077out_mgmt_cmd_cachep:
7078 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007079 return ret;
7080}
7081
7082void qlt_exit(void)
7083{
7084 if (!QLA_TGT_MODE_ENABLED())
7085 return;
7086
7087 destroy_workqueue(qla_tgt_wq);
7088 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007089 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007090 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007091}