blob: 4f1621fd5ef2f6601315e3c189d6113ed8797f89 [file] [log] [blame]
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001/*
2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
3 *
4 * based on qla2x00t.c code:
5 *
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
10 *
11 * Forward port and refactoring to modern qla2xxx and target/configfs
12 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -070013 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
18 * of the License.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/types.h>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040029#include <linux/blkdev.h>
30#include <linux/interrupt.h>
31#include <linux/pci.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/workqueue.h>
35#include <asm/unaligned.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_tcq.h>
39#include <target/target_core_base.h>
40#include <target/target_core_fabric.h>
41
42#include "qla_def.h"
43#include "qla_target.h"
44
Arun Easid154f352014-09-25 06:14:48 -040045static int ql2xtgt_tape_enable;
46module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
49
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040050static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51module_param(qlini_mode, charp, S_IRUGO);
52MODULE_PARM_DESC(qlini_mode,
53 "Determines when initiator mode will be enabled. Possible values: "
54 "\"exclusive\" - initiator mode will be enabled on load, "
55 "disabled on enabling target mode and then on disabling target mode "
56 "enabled back; "
57 "\"disabled\" - initiator mode will never be enabled; "
Quinn Tranead03852017-01-19 22:28:01 -080058 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
59 "when ready "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040060 "\"enabled\" (default) - initiator mode will always stay enabled.");
61
Quinn Tran99e1b682017-06-02 09:12:03 -070062static int ql_dm_tgt_ex_pct = 0;
Quinn Tranead03852017-01-19 22:28:01 -080063module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
67 "for Target mode.");
68
Quinn Tran09620ee2017-06-13 20:47:20 -070069int ql2xuctrlirq = 1;
70module_param(ql2xuctrlirq, int, 0644);
71MODULE_PARM_DESC(ql2xuctrlirq,
72 "User to control IRQ placement via smp_affinity."
73 "Valid with qlini_mode=disabled."
74 "1(default): enable");
75
Arun Easiaa230bc2013-01-30 03:34:39 -050076int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040077
Quinn 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;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500148static struct 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;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001531
Quinn Tran3a33dc92017-06-02 09:12:04 -07001532 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1533 !tgt->tgt_stopped)
1534 qlt_stop_phase1(tgt);
1535
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001536 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001537 qlt_stop_phase2(tgt);
1538
Quinn Trane326d222017-06-13 20:47:18 -07001539 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1540 unsigned long flags;
1541
1542 h = &tgt->qphints[i];
1543 if (h->qpair) {
1544 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1545 list_del(&h->hint_elem);
1546 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1547 h->qpair = NULL;
1548 }
1549 }
1550 kfree(tgt->qphints);
1551
1552 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1553 btree_remove64(&tgt->lun_qpair_map, key);
1554
1555 btree_destroy64(&tgt->lun_qpair_map);
1556
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001557 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001558
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001559 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001560 "Release of tgt %p finished\n", tgt);
1561
1562 kfree(tgt);
1563}
1564
1565/* ha->hardware_lock supposed to be held on entry */
1566static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1567 const void *param, unsigned int param_size)
1568{
1569 struct qla_tgt_sess_work_param *prm;
1570 unsigned long flags;
1571
1572 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1573 if (!prm) {
1574 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1575 "qla_target(%d): Unable to create session "
1576 "work, command will be refused", 0);
1577 return -ENOMEM;
1578 }
1579
1580 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1581 "Scheduling work (type %d, prm %p)"
1582 " to find session for param %p (size %d, tgt %p)\n",
1583 type, prm, param, param_size, tgt);
1584
1585 prm->type = type;
1586 memcpy(&prm->tm_iocb, param, param_size);
1587
1588 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1589 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1590 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1591
1592 schedule_work(&tgt->sess_work);
1593
1594 return 0;
1595}
1596
1597/*
1598 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1599 */
Quinn Tran82de8022017-06-13 20:47:17 -07001600static void qlt_send_notify_ack(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001601 struct imm_ntfy_from_isp *ntfy,
1602 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1603 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1604{
Quinn Tran82de8022017-06-13 20:47:17 -07001605 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001606 struct qla_hw_data *ha = vha->hw;
1607 request_t *pkt;
1608 struct nack_to_isp *nack;
1609
Quinn Tranec7193e2017-03-15 09:48:55 -07001610 if (!ha->flags.fw_started)
1611 return;
1612
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001613 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1614
Quinn Tran82de8022017-06-13 20:47:17 -07001615 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001616 if (!pkt) {
1617 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1618 "qla_target(%d): %s failed: unable to allocate "
1619 "request packet\n", vha->vp_idx, __func__);
1620 return;
1621 }
1622
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001623 if (vha->vha_tgt.qla_tgt != NULL)
1624 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001625
1626 pkt->entry_type = NOTIFY_ACK_TYPE;
1627 pkt->entry_count = 1;
1628
1629 nack = (struct nack_to_isp *)pkt;
1630 nack->ox_id = ntfy->ox_id;
1631
Quinn Tran726b8542017-01-19 22:28:00 -08001632 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001633 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1634 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1635 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001636 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001637 }
1638 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1639 nack->u.isp24.status = ntfy->u.isp24.status;
1640 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001641 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001642 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1643 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1644 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1645 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1646 nack->u.isp24.srr_reject_code = srr_reject_code;
1647 nack->u.isp24.srr_reject_code_expl = srr_explan;
1648 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1649
1650 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1651 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1652 vha->vp_idx, nack->u.isp24.status);
1653
Himanshu Madhani63163e02014-09-25 06:14:59 -04001654 /* Memory Barrier */
1655 wmb();
Quinn Tran82de8022017-06-13 20:47:17 -07001656 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001657}
1658
1659/*
1660 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1661 */
Quinn Tran82de8022017-06-13 20:47:17 -07001662static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001663 struct abts_recv_from_24xx *abts, uint32_t status,
1664 bool ids_reversed)
1665{
Quinn Tran82de8022017-06-13 20:47:17 -07001666 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001667 struct qla_hw_data *ha = vha->hw;
1668 struct abts_resp_to_24xx *resp;
1669 uint32_t f_ctl;
1670 uint8_t *p;
1671
1672 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1673 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1674 ha, abts, status);
1675
Quinn Tran82de8022017-06-13 20:47:17 -07001676 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1677 NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001678 if (!resp) {
1679 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1680 "qla_target(%d): %s failed: unable to allocate "
1681 "request packet", vha->vp_idx, __func__);
1682 return;
1683 }
1684
1685 resp->entry_type = ABTS_RESP_24XX;
1686 resp->entry_count = 1;
1687 resp->nport_handle = abts->nport_handle;
1688 resp->vp_index = vha->vp_idx;
1689 resp->sof_type = abts->sof_type;
1690 resp->exchange_address = abts->exchange_address;
1691 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001692 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001693 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1694 F_CTL_SEQ_INITIATIVE);
1695 p = (uint8_t *)&f_ctl;
1696 resp->fcp_hdr_le.f_ctl[0] = *p++;
1697 resp->fcp_hdr_le.f_ctl[1] = *p++;
1698 resp->fcp_hdr_le.f_ctl[2] = *p;
1699 if (ids_reversed) {
1700 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1701 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1702 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1703 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1704 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1705 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1706 } else {
1707 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1708 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1709 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1710 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1711 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1712 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1713 }
1714 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1715 if (status == FCP_TMF_CMPL) {
1716 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1717 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1718 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1719 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1720 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1721 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1722 } else {
1723 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1724 resp->payload.ba_rjt.reason_code =
1725 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1726 /* Other bytes are zero */
1727 }
1728
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001729 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001730
Himanshu Madhani63163e02014-09-25 06:14:59 -04001731 /* Memory Barrier */
1732 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07001733 if (qpair->reqq_start_iocbs)
1734 qpair->reqq_start_iocbs(qpair);
1735 else
1736 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001737}
1738
1739/*
1740 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1741 */
1742static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1743 struct abts_resp_from_24xx_fw *entry)
1744{
1745 struct ctio7_to_24xx *ctio;
1746
1747 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1748 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001749
Quinn Tran82de8022017-06-13 20:47:17 -07001750 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1751 vha->hw->base_qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001752 if (ctio == NULL) {
1753 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1754 "qla_target(%d): %s failed: unable to allocate "
1755 "request packet\n", vha->vp_idx, __func__);
1756 return;
1757 }
1758
1759 /*
1760 * We've got on entrance firmware's response on by us generated
1761 * ABTS response. So, in it ID fields are reversed.
1762 */
1763
1764 ctio->entry_type = CTIO_TYPE7;
1765 ctio->entry_count = 1;
1766 ctio->nport_handle = entry->nport_handle;
1767 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001768 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001769 ctio->vp_index = vha->vp_idx;
1770 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1771 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1772 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1773 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001774 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1775 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001776 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001777
Himanshu Madhani63163e02014-09-25 06:14:59 -04001778 /* Memory Barrier */
1779 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001780 qla2x00_start_iocbs(vha, vha->req);
1781
Quinn Tran82de8022017-06-13 20:47:17 -07001782 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1783 (struct abts_recv_from_24xx *)entry,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001784 FCP_TMF_CMPL, true);
1785}
1786
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001787static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1788{
1789 struct qla_tgt_sess_op *op;
1790 struct qla_tgt_cmd *cmd;
Quinn Tran8b631d82017-06-02 09:11:54 -07001791 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001792
Quinn Tran8b631d82017-06-02 09:11:54 -07001793 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001794 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1795 if (tag == op->atio.u.isp24.exchange_addr) {
1796 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001797 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001798 return 1;
1799 }
1800 }
1801
Quinn Tran41dc5292017-01-19 22:28:03 -08001802 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1803 if (tag == op->atio.u.isp24.exchange_addr) {
1804 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001805 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08001806 return 1;
1807 }
1808 }
1809
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001810 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1811 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001812 cmd->aborted = 1;
Quinn Tran8b631d82017-06-02 09:11:54 -07001813 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001814 return 1;
1815 }
1816 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001817 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001818
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001819 return 0;
1820}
1821
1822/* drop cmds for the given lun
1823 * XXX only looks for cmds on the port through which lun reset was recieved
1824 * XXX does not go through the list of other port (which may have cmds
1825 * for the same lun)
1826 */
1827static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001828 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001829{
1830 struct qla_tgt_sess_op *op;
1831 struct qla_tgt_cmd *cmd;
1832 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001833 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001834
1835 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001836 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001837 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1838 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001839 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001840
1841 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1842 op_lun = scsilun_to_int(
1843 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1844 if (op_key == key && op_lun == lun)
1845 op->aborted = true;
1846 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001847
1848 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1849 uint32_t op_key;
1850 u64 op_lun;
1851
1852 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1853 op_lun = scsilun_to_int(
1854 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1855 if (op_key == key && op_lun == lun)
1856 op->aborted = true;
1857 }
1858
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001859 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1860 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001861 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001862
1863 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1864 cmd_lun = scsilun_to_int(
1865 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1866 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001867 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001868 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001869 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001870}
1871
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001872/* ha->hardware_lock supposed to be held on entry */
1873static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08001874 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001875{
1876 struct qla_hw_data *ha = vha->hw;
1877 struct qla_tgt_mgmt_cmd *mcmd;
1878 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001879
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001880 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1881 /* send TASK_ABORT response immediately */
Linus Torvalds48ea2ce2017-07-13 14:27:32 -07001882 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001883 return 0;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001884 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001885
1886 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1887 "qla_target(%d): task abort (tag=%d)\n",
1888 vha->vp_idx, abts->exchange_addr_to_abort);
1889
1890 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1891 if (mcmd == NULL) {
1892 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1893 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1894 vha->vp_idx, __func__);
1895 return -ENOMEM;
1896 }
1897 memset(mcmd, 0, sizeof(*mcmd));
1898
1899 mcmd->sess = sess;
1900 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001901 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08001902 mcmd->tmr_func = QLA_TGT_ABTS;
Quinn Tran82de8022017-06-13 20:47:17 -07001903 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07001904 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001905
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001906 /*
1907 * LUN is looked up by target-core internally based on the passed
1908 * abts->exchange_addr_to_abort tag.
1909 */
1910 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, 0, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001911 abts->exchange_addr_to_abort);
1912 if (rc != 0) {
1913 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1914 "qla_target(%d): tgt_ops->handle_tmr()"
1915 " failed: %d", vha->vp_idx, rc);
1916 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1917 return -EFAULT;
1918 }
1919
1920 return 0;
1921}
1922
1923/*
1924 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1925 */
1926static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1927 struct abts_recv_from_24xx *abts)
1928{
1929 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001930 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001931 uint32_t tag = abts->exchange_addr_to_abort;
1932 uint8_t s_id[3];
1933 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001934 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001935
1936 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1937 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1938 "qla_target(%d): ABTS: Abort Sequence not "
1939 "supported\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07001940 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1941 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001942 return;
1943 }
1944
1945 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1946 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1947 "qla_target(%d): ABTS: Unknown Exchange "
1948 "Address received\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07001949 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1950 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001951 return;
1952 }
1953
1954 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1955 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1956 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1957 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1958 le32_to_cpu(abts->fcp_hdr_le.parameter));
1959
1960 s_id[0] = abts->fcp_hdr_le.s_id[2];
1961 s_id[1] = abts->fcp_hdr_le.s_id[1];
1962 s_id[2] = abts->fcp_hdr_le.s_id[0];
1963
Quinn Tran75601512015-12-17 14:57:04 -05001964 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001965 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1966 if (!sess) {
1967 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1968 "qla_target(%d): task abort for non-existant session\n",
1969 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001970 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001971 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
Quinn Tran75601512015-12-17 14:57:04 -05001972
1973 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1974
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001975 if (rc != 0) {
Quinn Tran82de8022017-06-13 20:47:17 -07001976 qlt_24xx_send_abts_resp(ha->base_qpair, abts,
1977 FCP_TMF_REJECTED, false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001978 }
1979 return;
1980 }
Quinn Tran75601512015-12-17 14:57:04 -05001981 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1982
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001983
Quinn Tran726b8542017-01-19 22:28:00 -08001984 if (sess->deleted) {
Quinn Tran82de8022017-06-13 20:47:17 -07001985 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1986 false);
Alexei Potashnike52a8b42015-07-14 16:00:48 -04001987 return;
1988 }
1989
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001990 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1991 if (rc != 0) {
1992 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1993 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1994 vha->vp_idx, rc);
Quinn Tran82de8022017-06-13 20:47:17 -07001995 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1996 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001997 return;
1998 }
1999}
2000
2001/*
2002 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2003 */
Quinn Tran82de8022017-06-13 20:47:17 -07002004static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002005 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2006{
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002007 struct scsi_qla_host *ha = mcmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002008 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2009 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002010 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002011
2012 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2013 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2014 ha, atio, resp_code);
2015
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002016
Quinn Tran82de8022017-06-13 20:47:17 -07002017 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002018 if (ctio == NULL) {
2019 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2020 "qla_target(%d): %s failed: unable to allocate "
2021 "request packet\n", ha->vp_idx, __func__);
2022 return;
2023 }
2024
2025 ctio->entry_type = CTIO_TYPE7;
2026 ctio->entry_count = 1;
2027 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2028 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002029 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002030 ctio->vp_index = ha->vp_idx;
2031 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2032 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2033 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2034 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002035 temp = (atio->u.isp24.attr << 9)|
2036 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2037 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002038 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2039 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002040 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002041 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2042 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002043 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002044
Himanshu Madhani63163e02014-09-25 06:14:59 -04002045 /* Memory Barrier */
2046 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002047 if (qpair->reqq_start_iocbs)
2048 qpair->reqq_start_iocbs(qpair);
2049 else
2050 qla2x00_start_iocbs(ha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002051}
2052
2053void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2054{
2055 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2056}
2057EXPORT_SYMBOL(qlt_free_mcmd);
2058
Quinn Tranbe251522017-03-15 09:48:49 -07002059/*
2060 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2061 * reacquire
2062 */
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002063void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07002064 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2065{
2066 struct atio_from_isp *atio = &cmd->atio;
2067 struct ctio7_to_24xx *ctio;
2068 uint16_t temp;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002069 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranbe251522017-03-15 09:48:49 -07002070
2071 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2072 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2073 "sense_key=%02x, asc=%02x, ascq=%02x",
2074 vha, atio, scsi_status, sense_key, asc, ascq);
2075
2076 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2077 if (!ctio) {
2078 ql_dbg(ql_dbg_async, vha, 0x3067,
2079 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2080 vha->host_no, __func__);
2081 goto out;
2082 }
2083
2084 ctio->entry_type = CTIO_TYPE7;
2085 ctio->entry_count = 1;
2086 ctio->handle = QLA_TGT_SKIP_HANDLE;
2087 ctio->nport_handle = cmd->sess->loop_id;
2088 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2089 ctio->vp_index = vha->vp_idx;
2090 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2091 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2092 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2093 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002094 temp = (atio->u.isp24.attr << 9) |
2095 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2096 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002097 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2098 ctio->u.status1.ox_id = cpu_to_le16(temp);
2099 ctio->u.status1.scsi_status =
2100 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2101 ctio->u.status1.response_len = cpu_to_le16(18);
2102 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2103
2104 if (ctio->u.status1.residual != 0)
2105 ctio->u.status1.scsi_status |=
2106 cpu_to_le16(SS_RESIDUAL_UNDER);
2107
2108 /* Response code and sense key */
2109 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2110 (&ctio->u.status1.sense_data)[0]);
2111 /* Additional sense length */
2112 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2113 /* ASC and ASCQ */
2114 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2115 (&ctio->u.status1.sense_data)[3]);
2116
2117 /* Memory Barrier */
2118 wmb();
2119
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002120 if (qpair->reqq_start_iocbs)
2121 qpair->reqq_start_iocbs(qpair);
2122 else
2123 qla2x00_start_iocbs(vha, qpair->req);
2124
Quinn Tranbe251522017-03-15 09:48:49 -07002125out:
2126 return;
2127}
2128
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002129/* callback from target fabric module code */
2130void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2131{
2132 struct scsi_qla_host *vha = mcmd->sess->vha;
2133 struct qla_hw_data *ha = vha->hw;
2134 unsigned long flags;
Quinn Tran82de8022017-06-13 20:47:17 -07002135 struct qla_qpair *qpair = mcmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002136
2137 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2138 "TM response mcmd (%p) status %#x state %#x",
2139 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2140
Quinn Tran82de8022017-06-13 20:47:17 -07002141 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002142
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002143 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002144 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002145 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002146 * previous life, just abort the processing.
2147 */
2148 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002149 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2150 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002151 mcmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002152 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002153 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002154 return;
2155 }
2156
Quinn Tran726b8542017-01-19 22:28:00 -08002157 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2158 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
Quinn Tran41dc5292017-01-19 22:28:03 -08002159 ELS_LOGO ||
2160 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2161 ELS_PRLO ||
2162 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2163 ELS_TPRLO) {
Quinn Tran83548fe2017-06-02 09:12:01 -07002164 ql_dbg(ql_dbg_disc, vha, 0x2106,
Quinn Tran726b8542017-01-19 22:28:00 -08002165 "TM response logo %phC status %#x state %#x",
2166 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2167 mcmd->flags);
2168 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2169 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002170 qlt_send_notify_ack(vha->hw->base_qpair,
2171 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
Quinn Tran726b8542017-01-19 22:28:00 -08002172 }
2173 } else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05002174 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Quinn Tran82de8022017-06-13 20:47:17 -07002175 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002176 mcmd->fc_tm_rsp, false);
2177 else
Quinn Tran82de8022017-06-13 20:47:17 -07002178 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002179 mcmd->fc_tm_rsp);
2180 }
2181 /*
2182 * Make the callback for ->free_mcmd() to queue_work() and invoke
2183 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2184 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2185 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2186 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2187 * qlt_xmit_tm_rsp() returns here..
2188 */
2189 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002190 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002191}
2192EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2193
2194/* No locks */
2195static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2196{
2197 struct qla_tgt_cmd *cmd = prm->cmd;
2198
2199 BUG_ON(cmd->sg_cnt == 0);
2200
2201 prm->sg = (struct scatterlist *)cmd->sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002202 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002203 cmd->sg_cnt, cmd->dma_data_direction);
2204 if (unlikely(prm->seg_cnt == 0))
2205 goto out_err;
2206
2207 prm->cmd->sg_mapped = 1;
2208
Quinn Tranf83adb62014-04-11 16:54:43 -04002209 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2210 /*
2211 * If greater than four sg entries then we need to allocate
2212 * the continuation entries
2213 */
Quinn Tranb5399f72017-06-13 20:47:27 -07002214 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
Quinn Tranf83adb62014-04-11 16:54:43 -04002215 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
Quinn Tranb5399f72017-06-13 20:47:27 -07002216 QLA_TGT_DATASEGS_PER_CMD_24XX,
2217 QLA_TGT_DATASEGS_PER_CONT_24XX);
Quinn Tranf83adb62014-04-11 16:54:43 -04002218 } else {
2219 /* DIF */
2220 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2221 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2222 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2223 prm->tot_dsds = prm->seg_cnt;
2224 } else
2225 prm->tot_dsds = prm->seg_cnt;
2226
2227 if (cmd->prot_sg_cnt) {
2228 prm->prot_sg = cmd->prot_sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002229 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
Quinn Tranf83adb62014-04-11 16:54:43 -04002230 cmd->prot_sg, cmd->prot_sg_cnt,
2231 cmd->dma_data_direction);
2232 if (unlikely(prm->prot_seg_cnt == 0))
2233 goto out_err;
2234
2235 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2236 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2237 /* Dif Bundling not support here */
2238 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2239 cmd->blk_sz);
2240 prm->tot_dsds += prm->prot_seg_cnt;
2241 } else
2242 prm->tot_dsds += prm->prot_seg_cnt;
2243 }
2244 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002245
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002246 return 0;
2247
2248out_err:
Quinn Tran22d84722017-06-13 20:47:25 -07002249 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002250 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2251 0, prm->cmd->sg_cnt);
2252 return -1;
2253}
2254
Joern Engelf9b67212014-09-16 16:23:18 -04002255static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002256{
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002257 struct qla_hw_data *ha;
2258 struct qla_qpair *qpair;
Joern Engelf9b67212014-09-16 16:23:18 -04002259 if (!cmd->sg_mapped)
2260 return;
2261
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002262 qpair = cmd->qpair;
2263
2264 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2265 cmd->dma_data_direction);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002266 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002267
2268 if (cmd->prot_sg_cnt)
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002269 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
Quinn Tranf83adb62014-04-11 16:54:43 -04002270 cmd->dma_data_direction);
2271
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002272 if (!cmd->ctx)
2273 return;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002274 ha = vha->hw;
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002275 if (cmd->ctx_dsd_alloced)
2276 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2277
2278 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002279}
2280
Quinn Tran82de8022017-06-13 20:47:17 -07002281static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002282 uint32_t req_cnt)
2283{
Quinn Trand63b3282017-06-02 09:12:07 -07002284 uint32_t cnt;
Quinn Tran82de8022017-06-13 20:47:17 -07002285 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002286
Quinn Tran82de8022017-06-13 20:47:17 -07002287 if (req->cnt < (req_cnt + 2)) {
Quinn Tranaf7bb382017-06-13 20:47:23 -07002288 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2289 RD_REG_DWORD_RELAXED(req->req_q_out));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002290
Quinn Tran82de8022017-06-13 20:47:17 -07002291 if (req->ring_index < cnt)
2292 req->cnt = cnt - req->ring_index;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002293 else
Quinn Tran82de8022017-06-13 20:47:17 -07002294 req->cnt = req->length - (req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002295
Quinn Tran82de8022017-06-13 20:47:17 -07002296 if (unlikely(req->cnt < (req_cnt + 2)))
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002297 return -EAGAIN;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002298 }
2299
Quinn Tran82de8022017-06-13 20:47:17 -07002300 req->cnt -= req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002301
2302 return 0;
2303}
2304
2305/*
2306 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2307 */
Quinn Tran82de8022017-06-13 20:47:17 -07002308static inline void *qlt_get_req_pkt(struct req_que *req)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002309{
2310 /* Adjust ring index. */
Quinn Tran82de8022017-06-13 20:47:17 -07002311 req->ring_index++;
2312 if (req->ring_index == req->length) {
2313 req->ring_index = 0;
2314 req->ring_ptr = req->ring;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002315 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002316 req->ring_ptr++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002317 }
Quinn Tran82de8022017-06-13 20:47:17 -07002318 return (cont_entry_t *)req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002319}
2320
2321/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002322static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002323{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002324 uint32_t h;
Quinn Tranc5419e22017-06-13 20:47:16 -07002325 int index;
2326 uint8_t found = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07002327 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002328
Quinn Tranc5419e22017-06-13 20:47:16 -07002329 h = req->current_outstanding_cmd;
2330
2331 for (index = 1; index < req->num_outstanding_cmds; index++) {
2332 h++;
2333 if (h == req->num_outstanding_cmds)
2334 h = 1;
2335
2336 if (h == QLA_TGT_SKIP_HANDLE)
2337 continue;
2338
2339 if (!req->outstanding_cmds[h]) {
2340 found = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002341 break;
2342 }
Quinn Tranc5419e22017-06-13 20:47:16 -07002343 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002344
Quinn Tranc5419e22017-06-13 20:47:16 -07002345 if (found) {
2346 req->current_outstanding_cmd = h;
2347 } else {
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002348 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2349 "qla_target(%d): Ran out of empty cmd slots\n",
2350 qpair->vha->vp_idx);
Quinn Tranc5419e22017-06-13 20:47:16 -07002351 h = QLA_TGT_NULL_HANDLE;
2352 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002353
2354 return h;
2355}
2356
2357/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002358static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2359 struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002360{
2361 uint32_t h;
2362 struct ctio7_to_24xx *pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002363 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002364 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002365
Quinn Tran82de8022017-06-13 20:47:17 -07002366 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002367 prm->pkt = pkt;
2368 memset(pkt, 0, sizeof(*pkt));
2369
2370 pkt->entry_type = CTIO_TYPE7;
2371 pkt->entry_count = (uint8_t)prm->req_cnt;
Quinn Tran22d84722017-06-13 20:47:25 -07002372 pkt->vp_index = prm->cmd->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002373
Quinn Tran82de8022017-06-13 20:47:17 -07002374 h = qlt_make_handle(qpair);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002375 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2376 /*
2377 * CTIO type 7 from the firmware doesn't provide a way to
2378 * know the initiator's LOOP ID, hence we can't find
2379 * the session and, so, the command.
2380 */
2381 return -EAGAIN;
Quinn Trane326d222017-06-13 20:47:18 -07002382 } else
2383 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002384
Quinn Tran82de8022017-06-13 20:47:17 -07002385 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2386 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2387 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002388 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002389 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2390 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2391 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2392 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002393 temp = atio->u.isp24.attr << 9;
2394 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002395 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2396 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002397 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2398
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002399 return 0;
2400}
2401
2402/*
2403 * ha->hardware_lock supposed to be held on entry. We have already made sure
2404 * that there is sufficient amount of request entries to not drop it.
2405 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002406static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002407{
2408 int cnt;
2409 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002410
2411 /* Build continuation packets */
2412 while (prm->seg_cnt > 0) {
2413 cont_a64_entry_t *cont_pkt64 =
Quinn Tran82de8022017-06-13 20:47:17 -07002414 (cont_a64_entry_t *)qlt_get_req_pkt(
2415 prm->cmd->qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002416
2417 /*
2418 * Make sure that from cont_pkt64 none of
2419 * 64-bit specific fields used for 32-bit
2420 * addressing. Cast to (cont_entry_t *) for
2421 * that.
2422 */
2423
2424 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2425
2426 cont_pkt64->entry_count = 1;
2427 cont_pkt64->sys_define = 0;
2428
Quinn Tran32d29b42017-06-13 20:47:26 -07002429 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2430 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002431
2432 /* Load continuation entry data segments */
2433 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002434 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002435 cnt++, prm->seg_cnt--) {
2436 *dword_ptr++ =
2437 cpu_to_le32(pci_dma_lo32
2438 (sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002439 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2440 (sg_dma_address(prm->sg)));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002441 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2442
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002443 prm->sg = sg_next(prm->sg);
2444 }
2445 }
2446}
2447
2448/*
2449 * ha->hardware_lock supposed to be held on entry. We have already made sure
2450 * that there is sufficient amount of request entries to not drop it.
2451 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002452static void qlt_load_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002453{
2454 int cnt;
2455 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002456 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2457
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002458 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2459
2460 /* Setup packet address segment pointer */
2461 dword_ptr = pkt24->u.status0.dseg_0_address;
2462
2463 /* Set total data segment count */
2464 if (prm->seg_cnt)
2465 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2466
2467 if (prm->seg_cnt == 0) {
2468 /* No data transfer */
2469 *dword_ptr++ = 0;
2470 *dword_ptr = 0;
2471 return;
2472 }
2473
2474 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002475
2476 /* Load command entry data segments */
2477 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002478 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002479 cnt++, prm->seg_cnt--) {
2480 *dword_ptr++ =
2481 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002482
2483 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2484 sg_dma_address(prm->sg)));
2485
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002486 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2487
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002488 prm->sg = sg_next(prm->sg);
2489 }
2490
Quinn Tran32d29b42017-06-13 20:47:26 -07002491 qlt_load_cont_data_segments(prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002492}
2493
2494static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2495{
2496 return cmd->bufflen > 0;
2497}
2498
Quinn Tranbe251522017-03-15 09:48:49 -07002499static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2500{
2501 struct qla_tgt_cmd *cmd;
2502 struct scsi_qla_host *vha;
2503
2504 /* asc 0x10=dif error */
2505 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2506 cmd = prm->cmd;
2507 vha = cmd->vha;
2508 /* ASCQ */
2509 switch (prm->sense_buffer[13]) {
2510 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002511 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002512 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2513 "se_cmd=%p tag[%x]",
2514 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2515 cmd->atio.u.isp24.exchange_addr);
2516 break;
2517 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002518 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002519 "BE detected APP 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 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002525 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002526 "BE detected REF 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 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002532 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002533 "BE detected Dif ERR: lba[%llx|%lld] len[%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 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002539 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002540 }
2541}
2542
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002543/*
2544 * Called without ha->hardware_lock held
2545 */
2546static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2547 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2548 uint32_t *full_req_cnt)
2549{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002550 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Tran22d84722017-06-13 20:47:25 -07002551 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002552
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002553 prm->cmd = cmd;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002554 prm->tgt = cmd->tgt;
2555 prm->pkt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002556 prm->rq_result = scsi_status;
2557 prm->sense_buffer = &cmd->sense_buffer[0];
2558 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2559 prm->sg = NULL;
2560 prm->seg_cnt = -1;
2561 prm->req_cnt = 1;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002562 prm->residual = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002563 prm->add_status_pkt = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002564 prm->prot_sg = NULL;
2565 prm->prot_seg_cnt = 0;
2566 prm->tot_dsds = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002567
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002568 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2569 if (qlt_pci_map_calc_cnt(prm) != 0)
2570 return -EAGAIN;
2571 }
2572
2573 *full_req_cnt = prm->req_cnt;
2574
2575 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2576 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002577 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002578 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2579 prm->residual, se_cmd->tag,
2580 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2581 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002582 prm->rq_result |= SS_RESIDUAL_UNDER;
2583 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2584 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002585 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002586 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2587 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2588 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002589 prm->rq_result |= SS_RESIDUAL_OVER;
2590 }
2591
2592 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2593 /*
2594 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2595 * ignored in *xmit_response() below
2596 */
2597 if (qlt_has_data(cmd)) {
2598 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002599 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002600 (prm->rq_result != 0))) {
2601 prm->add_status_pkt = 1;
2602 (*full_req_cnt)++;
2603 }
2604 }
2605 }
2606
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002607 return 0;
2608}
2609
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002610static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2611 int sending_sense)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002612{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002613 if (cmd->qpair->enable_class_2)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002614 return 0;
2615
2616 if (sending_sense)
2617 return cmd->conf_compl_supported;
2618 else
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002619 return cmd->qpair->enable_explicit_conf &&
2620 cmd->conf_compl_supported;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002621}
2622
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002623static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2624 struct qla_tgt_prm *prm)
2625{
2626 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2627 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002628 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002629 if (qlt_need_explicit_conf(prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002630 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002631 CTIO7_FLAGS_EXPLICIT_CONFORM |
2632 CTIO7_FLAGS_CONFORM_REQ);
2633 }
2634 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2635 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2636 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2637 int i;
2638
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002639 if (qlt_need_explicit_conf(prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002640 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Quinn Tran22d84722017-06-13 20:47:25 -07002641 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002642 "Skipping EXPLICIT_CONFORM and "
2643 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2644 "non GOOD status\n");
2645 goto skip_explict_conf;
2646 }
Bart Van Asschead950362015-07-09 07:24:08 -07002647 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002648 CTIO7_FLAGS_EXPLICIT_CONFORM |
2649 CTIO7_FLAGS_CONFORM_REQ);
2650 }
2651skip_explict_conf:
2652 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002653 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002654 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002655 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002656 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002657 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002658 ctio->u.status1.sense_length =
2659 cpu_to_le16(prm->sense_buffer_len);
2660 for (i = 0; i < prm->sense_buffer_len/4; i++)
2661 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2662 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002663
2664 qlt_print_dif_err(prm);
2665
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002666 } else {
2667 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002668 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002669 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002670 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002671 ctio->u.status1.sense_length = 0;
2672 memset(ctio->u.status1.sense_data, 0,
2673 sizeof(ctio->u.status1.sense_data));
2674 }
2675
2676 /* Sense with len > 24, is it possible ??? */
2677}
2678
Quinn Tranf83adb62014-04-11 16:54:43 -04002679static inline int
2680qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2681{
Quinn Tranf83adb62014-04-11 16:54:43 -04002682 switch (se_cmd->prot_op) {
2683 case TARGET_PROT_DOUT_INSERT:
2684 case TARGET_PROT_DIN_STRIP:
2685 if (ql2xenablehba_err_chk >= 1)
2686 return 1;
2687 break;
2688 case TARGET_PROT_DOUT_PASS:
2689 case TARGET_PROT_DIN_PASS:
2690 if (ql2xenablehba_err_chk >= 2)
2691 return 1;
2692 break;
2693 case TARGET_PROT_DIN_INSERT:
2694 case TARGET_PROT_DOUT_STRIP:
2695 return 1;
2696 default:
2697 break;
2698 }
2699 return 0;
2700}
2701
Quinn Tranbe251522017-03-15 09:48:49 -07002702static inline int
2703qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002704{
Quinn Tranbe251522017-03-15 09:48:49 -07002705 switch (se_cmd->prot_op) {
2706 case TARGET_PROT_DIN_INSERT:
2707 case TARGET_PROT_DOUT_INSERT:
2708 case TARGET_PROT_DIN_STRIP:
2709 case TARGET_PROT_DOUT_STRIP:
2710 case TARGET_PROT_DIN_PASS:
2711 case TARGET_PROT_DOUT_PASS:
2712 return 1;
2713 default:
2714 return 0;
2715 }
2716 return 0;
2717}
Quinn Tranf83adb62014-04-11 16:54:43 -04002718
Quinn Tranbe251522017-03-15 09:48:49 -07002719/*
2720 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2721 */
2722static void
2723qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2724 uint16_t *pfw_prot_opts)
2725{
2726 struct se_cmd *se_cmd = &cmd->se_cmd;
2727 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2728 scsi_qla_host_t *vha = cmd->tgt->vha;
2729 struct qla_hw_data *ha = vha->hw;
2730 uint32_t t32 = 0;
2731
2732 /*
2733 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002734 * have been immplemented by TCM, before AppTag is avail.
2735 * Look for modesense_handlers[]
2736 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002737 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002738 ctx->app_tag_mask[0] = 0x0;
2739 ctx->app_tag_mask[1] = 0x0;
2740
Quinn Tranbe251522017-03-15 09:48:49 -07002741 if (IS_PI_UNINIT_CAPABLE(ha)) {
2742 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2743 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2744 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2745 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2746 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2747 }
2748
2749 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2750
Quinn Tranf83adb62014-04-11 16:54:43 -04002751 switch (se_cmd->prot_type) {
2752 case TARGET_DIF_TYPE0_PROT:
2753 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002754 * No check for ql2xenablehba_err_chk, as it
2755 * would be an I/O error if hba tag generation
2756 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002757 */
2758 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002759 /* enable ALL bytes of the ref tag */
2760 ctx->ref_tag_mask[0] = 0xff;
2761 ctx->ref_tag_mask[1] = 0xff;
2762 ctx->ref_tag_mask[2] = 0xff;
2763 ctx->ref_tag_mask[3] = 0xff;
2764 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002765 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002766 /*
2767 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2768 * REF tag, and 16 bit app tag.
2769 */
2770 ctx->ref_tag = cpu_to_le32(lba);
2771 if (!qla_tgt_ref_mask_check(se_cmd) ||
2772 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2773 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2774 break;
2775 }
2776 /* enable ALL bytes of the ref tag */
2777 ctx->ref_tag_mask[0] = 0xff;
2778 ctx->ref_tag_mask[1] = 0xff;
2779 ctx->ref_tag_mask[2] = 0xff;
2780 ctx->ref_tag_mask[3] = 0xff;
2781 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002782 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002783 /*
2784 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2785 * tag has to match LBA in CDB + N
2786 */
2787 ctx->ref_tag = cpu_to_le32(lba);
2788 if (!qla_tgt_ref_mask_check(se_cmd) ||
2789 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2790 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2791 break;
2792 }
2793 /* enable ALL bytes of the ref tag */
2794 ctx->ref_tag_mask[0] = 0xff;
2795 ctx->ref_tag_mask[1] = 0xff;
2796 ctx->ref_tag_mask[2] = 0xff;
2797 ctx->ref_tag_mask[3] = 0xff;
2798 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002799 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002800 /* For TYPE 3 protection: 16 bit GUARD only */
2801 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2802 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2803 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2804 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002805 }
2806}
2807
Quinn Tranf83adb62014-04-11 16:54:43 -04002808static inline int
Quinn Tran82de8022017-06-13 20:47:17 -07002809qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
Quinn Tranf83adb62014-04-11 16:54:43 -04002810{
2811 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002812 uint32_t transfer_length = 0;
2813 uint32_t data_bytes;
2814 uint32_t dif_bytes;
2815 uint8_t bundling = 1;
2816 uint8_t *clr_ptr;
2817 struct crc_context *crc_ctx_pkt = NULL;
2818 struct qla_hw_data *ha;
2819 struct ctio_crc2_to_fw *pkt;
2820 dma_addr_t crc_ctx_dma;
2821 uint16_t fw_prot_opts = 0;
2822 struct qla_tgt_cmd *cmd = prm->cmd;
2823 struct se_cmd *se_cmd = &cmd->se_cmd;
2824 uint32_t h;
2825 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07002826 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002827 uint16_t t16;
Quinn Tran82de8022017-06-13 20:47:17 -07002828 scsi_qla_host_t *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04002829
Quinn Tranf83adb62014-04-11 16:54:43 -04002830 ha = vha->hw;
2831
Quinn Tran82de8022017-06-13 20:47:17 -07002832 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
Quinn Tranf83adb62014-04-11 16:54:43 -04002833 prm->pkt = pkt;
2834 memset(pkt, 0, sizeof(*pkt));
2835
Quinn Tran22d84722017-06-13 20:47:25 -07002836 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
Quinn Tranf83adb62014-04-11 16:54:43 -04002837 "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 -07002838 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
Quinn Tranf83adb62014-04-11 16:54:43 -04002839 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2840
2841 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2842 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2843 bundling = 0;
2844
2845 /* Compute dif len and adjust data len to incude protection */
2846 data_bytes = cmd->bufflen;
2847 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2848
2849 switch (se_cmd->prot_op) {
2850 case TARGET_PROT_DIN_INSERT:
2851 case TARGET_PROT_DOUT_STRIP:
2852 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07002853 if (cmd->prot_sg_cnt)
2854 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04002855 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002856 case TARGET_PROT_DIN_STRIP:
2857 case TARGET_PROT_DOUT_INSERT:
2858 case TARGET_PROT_DIN_PASS:
2859 case TARGET_PROT_DOUT_PASS:
2860 transfer_length = data_bytes + dif_bytes;
2861 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002862 default:
2863 BUG();
2864 break;
2865 }
2866
2867 if (!qlt_hba_err_chk_enabled(se_cmd))
2868 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2869 /* HBA error checking enabled */
2870 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2871 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2872 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2873 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2874 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2875 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2876 }
2877
2878 switch (se_cmd->prot_op) {
2879 case TARGET_PROT_DIN_INSERT:
2880 case TARGET_PROT_DOUT_INSERT:
2881 fw_prot_opts |= PO_MODE_DIF_INSERT;
2882 break;
2883 case TARGET_PROT_DIN_STRIP:
2884 case TARGET_PROT_DOUT_STRIP:
2885 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2886 break;
2887 case TARGET_PROT_DIN_PASS:
2888 case TARGET_PROT_DOUT_PASS:
2889 fw_prot_opts |= PO_MODE_DIF_PASS;
2890 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2891 break;
2892 default:/* Normal Request */
2893 fw_prot_opts |= PO_MODE_DIF_PASS;
2894 break;
2895 }
2896
Quinn Tranf83adb62014-04-11 16:54:43 -04002897 /* ---- PKT ---- */
2898 /* Update entry type to indicate Command Type CRC_2 IOCB */
2899 pkt->entry_type = CTIO_CRC2;
2900 pkt->entry_count = 1;
Quinn Tran22d84722017-06-13 20:47:25 -07002901 pkt->vp_index = cmd->vp_idx;
Quinn Tranf83adb62014-04-11 16:54:43 -04002902
Quinn Tran82de8022017-06-13 20:47:17 -07002903 h = qlt_make_handle(qpair);
Quinn Tranf83adb62014-04-11 16:54:43 -04002904 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2905 /*
2906 * CTIO type 7 from the firmware doesn't provide a way to
2907 * know the initiator's LOOP ID, hence we can't find
2908 * the session and, so, the command.
2909 */
2910 return -EAGAIN;
2911 } else
Quinn Tran82de8022017-06-13 20:47:17 -07002912 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002913
Quinn Tran82de8022017-06-13 20:47:17 -07002914 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2915 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07002916 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002917 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002918 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2919 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2920 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2921 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002922
2923 /* silence compile warning */
2924 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2925 pkt->ox_id = cpu_to_le16(t16);
2926
2927 t16 = (atio->u.isp24.attr << 9);
2928 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002929 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2930
2931 /* Set transfer direction */
2932 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002933 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002934 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002935 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002936
Quinn Tranf83adb62014-04-11 16:54:43 -04002937 pkt->dseg_count = prm->tot_dsds;
2938 /* Fibre channel byte count */
2939 pkt->transfer_length = cpu_to_le32(transfer_length);
2940
Quinn Tranf83adb62014-04-11 16:54:43 -04002941 /* ----- CRC context -------- */
2942
2943 /* Allocate CRC context from global pool */
2944 crc_ctx_pkt = cmd->ctx =
2945 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2946
2947 if (!crc_ctx_pkt)
2948 goto crc_queuing_error;
2949
2950 /* Zero out CTX area. */
2951 clr_ptr = (uint8_t *)crc_ctx_pkt;
2952 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2953
2954 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2955 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2956
2957 /* Set handle */
2958 crc_ctx_pkt->handle = pkt->handle;
2959
Quinn Tranbe251522017-03-15 09:48:49 -07002960 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04002961
2962 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2963 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2964 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2965
Quinn Tranf83adb62014-04-11 16:54:43 -04002966 if (!bundling) {
2967 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2968 } else {
2969 /*
2970 * Configure Bundling if we need to fetch interlaving
2971 * protection PCI accesses
2972 */
2973 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2974 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2975 crc_ctx_pkt->u.bundling.dseg_count =
2976 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2977 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2978 }
2979
2980 /* Finish the common fields of CRC pkt */
2981 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2982 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2983 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07002984 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002985
Quinn Tranbe251522017-03-15 09:48:49 -07002986 memset((uint8_t *)&tc, 0 , sizeof(tc));
2987 tc.vha = vha;
2988 tc.blk_sz = cmd->blk_sz;
2989 tc.bufflen = cmd->bufflen;
2990 tc.sg = cmd->sg;
2991 tc.prot_sg = cmd->prot_sg;
2992 tc.ctx = crc_ctx_pkt;
2993 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04002994
2995 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07002996 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04002997
2998 if (!bundling && prm->prot_seg_cnt) {
2999 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003000 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003001 goto crc_queuing_error;
3002 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003003 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003004 goto crc_queuing_error;
3005
3006 if (bundling && prm->prot_seg_cnt) {
3007 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003008 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04003009
3010 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3011 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003012 prm->prot_seg_cnt, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003013 goto crc_queuing_error;
3014 }
3015 return QLA_SUCCESS;
3016
3017crc_queuing_error:
3018 /* Cleanup will be performed by the caller */
Quinn Tran82de8022017-06-13 20:47:17 -07003019 qpair->req->outstanding_cmds[h] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003020
3021 return QLA_FUNCTION_FAILED;
3022}
3023
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003024/*
3025 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3026 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3027 */
3028int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3029 uint8_t scsi_status)
3030{
3031 struct scsi_qla_host *vha = cmd->vha;
Quinn Tran82de8022017-06-13 20:47:17 -07003032 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003033 struct ctio7_to_24xx *pkt;
3034 struct qla_tgt_prm prm;
3035 uint32_t full_req_cnt = 0;
3036 unsigned long flags = 0;
3037 int res;
3038
Quinn Tran726b8542017-01-19 22:28:00 -08003039 if (cmd->sess && cmd->sess->deleted) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003040 cmd->state = QLA_TGT_STATE_PROCESSED;
3041 if (cmd->sess->logout_completed)
3042 /* no need to terminate. FW already freed exchange. */
3043 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3044 else
Quinn Tran82de8022017-06-13 20:47:17 -07003045 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003046 return 0;
3047 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003048
Quinn Tran22d84722017-06-13 20:47:25 -07003049 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
Quinn Tran82de8022017-06-13 20:47:17 -07003050 "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 -04003051 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3052 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
Quinn Tran82de8022017-06-13 20:47:17 -07003053 &cmd->se_cmd, qpair->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003054
3055 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3056 &full_req_cnt);
3057 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003058 return res;
3059 }
3060
Quinn Tran82de8022017-06-13 20:47:17 -07003061 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003062
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003063 if (xmit_type == QLA_TGT_XMIT_STATUS)
Quinn Tran60a9ead2017-06-13 20:47:28 -07003064 qpair->tgt_counters.core_qla_snd_status++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003065 else
Quinn Tran60a9ead2017-06-13 20:47:28 -07003066 qpair->tgt_counters.core_qla_que_buf++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003067
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003068 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003069 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003070 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003071 * previous life, just abort the processing.
3072 */
3073 cmd->state = QLA_TGT_STATE_PROCESSED;
3074 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003075 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003076 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3077 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003078 cmd->reset_count, qpair->chip_reset);
Quinn Tran82de8022017-06-13 20:47:17 -07003079 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04003080 return 0;
3081 }
3082
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003083 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003084 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003085 if (unlikely(res))
3086 goto out_unmap_unlock;
3087
Quinn Tranf83adb62014-04-11 16:54:43 -04003088 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
Quinn Tran82de8022017-06-13 20:47:17 -07003089 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003090 else
Quinn Tran82de8022017-06-13 20:47:17 -07003091 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Quinn Tran810e30b2015-06-10 11:05:20 -04003092 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003093 qpair->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003094 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003095 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003096
3097 pkt = (struct ctio7_to_24xx *)prm.pkt;
3098
3099 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3100 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003101 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003102 CTIO7_FLAGS_STATUS_MODE_0);
3103
Quinn Tranf83adb62014-04-11 16:54:43 -04003104 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003105 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003106
3107 if (prm.add_status_pkt == 0) {
3108 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3109 pkt->u.status0.scsi_status =
3110 cpu_to_le16(prm.rq_result);
3111 pkt->u.status0.residual =
3112 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003113 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003114 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003115 if (qlt_need_explicit_conf(cmd, 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003116 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003117 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003118 CTIO7_FLAGS_EXPLICIT_CONFORM |
3119 CTIO7_FLAGS_CONFORM_REQ);
3120 }
3121 }
3122
3123 } else {
3124 /*
3125 * We have already made sure that there is sufficient
3126 * amount of request entries to not drop HW lock in
3127 * req_pkt().
3128 */
3129 struct ctio7_to_24xx *ctio =
Quinn Tran82de8022017-06-13 20:47:17 -07003130 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3131 qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003132
Quinn Tran22d84722017-06-13 20:47:25 -07003133 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
Arun Easi667024a2014-09-25 06:14:47 -04003134 "Building additional status packet 0x%p.\n",
3135 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003136
Quinn Tranf83adb62014-04-11 16:54:43 -04003137 /*
3138 * T10Dif: ctio_crc2_to_fw overlay ontop of
3139 * ctio7_to_24xx
3140 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003141 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003142 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003143 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003144 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003145 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003146 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003147 CTIO7_FLAGS_DATA_IN);
3148
3149 /* Real finish is ctio_m1's finish */
3150 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003151 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003152 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003153
3154 /* qlt_24xx_init_ctio_to_isp will correct
3155 * all neccessary fields that's part of CTIO7.
3156 * There should be no residual of CTIO-CRC2 data.
3157 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003158 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3159 &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003160 }
3161 } else
3162 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3163
3164
3165 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003166 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003167
Himanshu Madhani63163e02014-09-25 06:14:59 -04003168 /* Memory Barrier */
3169 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003170 if (qpair->reqq_start_iocbs)
3171 qpair->reqq_start_iocbs(qpair);
3172 else
3173 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003174 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003175
3176 return 0;
3177
3178out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003179 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003180 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003181
3182 return res;
3183}
3184EXPORT_SYMBOL(qlt_xmit_response);
3185
3186int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3187{
3188 struct ctio7_to_24xx *pkt;
3189 struct scsi_qla_host *vha = cmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003190 struct qla_tgt *tgt = cmd->tgt;
3191 struct qla_tgt_prm prm;
Quinn Tran82de8022017-06-13 20:47:17 -07003192 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003193 int res = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003194 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003195
3196 memset(&prm, 0, sizeof(prm));
3197 prm.cmd = cmd;
3198 prm.tgt = tgt;
3199 prm.sg = NULL;
3200 prm.req_cnt = 1;
3201
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003202 /* Calculate number of entries and segments required */
3203 if (qlt_pci_map_calc_cnt(&prm) != 0)
3204 return -EAGAIN;
3205
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003206 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003207 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003208 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003209 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003210 * previous life, just abort the processing.
3211 */
3212 cmd->state = QLA_TGT_STATE_NEED_DATA;
3213 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003214 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003215 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3216 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003217 cmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003218 return 0;
3219 }
3220
Quinn Tran82de8022017-06-13 20:47:17 -07003221 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003222 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003223 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003224 if (res != 0)
3225 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003226 if (cmd->se_cmd.prot_op)
Quinn Tran82de8022017-06-13 20:47:17 -07003227 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003228 else
Quinn Tran82de8022017-06-13 20:47:17 -07003229 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003230
Quinn Tran810e30b2015-06-10 11:05:20 -04003231 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003232 qpair->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003233 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003234 }
3235
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003236 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003237 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003238 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003239
3240 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003241 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003242
3243 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003244 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003245
Himanshu Madhani63163e02014-09-25 06:14:59 -04003246 /* Memory Barrier */
3247 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003248 if (qpair->reqq_start_iocbs)
3249 qpair->reqq_start_iocbs(qpair);
3250 else
3251 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003252 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003253
3254 return res;
3255
3256out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003257 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003258 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003259
3260 return res;
3261}
3262EXPORT_SYMBOL(qlt_rdy_to_xfer);
3263
Quinn Tranf83adb62014-04-11 16:54:43 -04003264
3265/*
Quinn Tranbe251522017-03-15 09:48:49 -07003266 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003267 */
Quinn Tranbe251522017-03-15 09:48:49 -07003268static void
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003269qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003270 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003271{
3272 uint8_t *ap = &sts->actual_dif[0];
3273 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003274 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003275 uint8_t scsi_status, sense_key, asc, ascq;
3276 unsigned long flags;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003277 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003278
Quinn Tranbe251522017-03-15 09:48:49 -07003279 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003280
Quinn Tranbe251522017-03-15 09:48:49 -07003281 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3282 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3283 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003284
Quinn Tranbe251522017-03-15 09:48:49 -07003285 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3286 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3287 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003288
Quinn Tranbe251522017-03-15 09:48:49 -07003289 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3290 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003291
Quinn Tranbe251522017-03-15 09:48:49 -07003292 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003293
Quinn Tranbe251522017-03-15 09:48:49 -07003294 /* check appl tag */
3295 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003296 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3297 "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]",
3298 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3299 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3300 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3301 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003302
Quinn Tranbe251522017-03-15 09:48:49 -07003303 cmd->dif_err_code = DIF_ERR_APP;
3304 scsi_status = SAM_STAT_CHECK_CONDITION;
3305 sense_key = ABORTED_COMMAND;
3306 asc = 0x10;
3307 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003308 }
3309
3310 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003311 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003312 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3313 "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] ",
3314 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3315 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3316 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3317 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003318
Quinn Tranbe251522017-03-15 09:48:49 -07003319 cmd->dif_err_code = DIF_ERR_REF;
3320 scsi_status = SAM_STAT_CHECK_CONDITION;
3321 sense_key = ABORTED_COMMAND;
3322 asc = 0x10;
3323 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003324 goto out;
3325 }
3326
Quinn Tranbe251522017-03-15 09:48:49 -07003327 /* check guard */
3328 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003329 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3330 "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]",
3331 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3332 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3333 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3334 cmd->atio.u.isp24.fcp_hdr.ox_id);
3335
Quinn Tranbe251522017-03-15 09:48:49 -07003336 cmd->dif_err_code = DIF_ERR_GRD;
3337 scsi_status = SAM_STAT_CHECK_CONDITION;
3338 sense_key = ABORTED_COMMAND;
3339 asc = 0x10;
3340 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003341 }
3342out:
Quinn Tranbe251522017-03-15 09:48:49 -07003343 switch (cmd->state) {
3344 case QLA_TGT_STATE_NEED_DATA:
3345 /* handle_data will load DIF error code */
3346 cmd->state = QLA_TGT_STATE_DATA_IN;
3347 vha->hw->tgt.tgt_ops->handle_data(cmd);
3348 break;
3349 default:
3350 spin_lock_irqsave(&cmd->cmd_lock, flags);
3351 if (cmd->aborted) {
3352 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3353 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3354 break;
3355 }
3356 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003357
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003358 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3359 ascq);
Quinn Tranbe251522017-03-15 09:48:49 -07003360 /* assume scsi status gets out on the wire.
3361 * Will not wait for completion.
3362 */
3363 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3364 break;
3365 }
3366}
Quinn Tranf83adb62014-04-11 16:54:43 -04003367
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003368/* If hardware_lock held on entry, might drop it, then reaquire */
3369/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003370static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3371 struct imm_ntfy_from_isp *ntfy)
3372{
3373 struct nack_to_isp *nack;
3374 struct qla_hw_data *ha = vha->hw;
3375 request_t *pkt;
3376 int ret = 0;
3377
3378 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3379 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3380
Quinn Tranec7193e2017-03-15 09:48:55 -07003381 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003382 if (pkt == NULL) {
3383 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3384 "qla_target(%d): %s failed: unable to allocate "
3385 "request packet\n", vha->vp_idx, __func__);
3386 return -ENOMEM;
3387 }
3388
3389 pkt->entry_type = NOTIFY_ACK_TYPE;
3390 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003391 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003392
3393 nack = (struct nack_to_isp *)pkt;
3394 nack->ox_id = ntfy->ox_id;
3395
3396 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3397 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3398 nack->u.isp24.flags = ntfy->u.isp24.flags &
3399 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3400 }
3401
3402 /* terminate */
3403 nack->u.isp24.flags |=
3404 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3405
3406 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3407 nack->u.isp24.status = ntfy->u.isp24.status;
3408 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3409 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3410 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3411 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3412 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3413 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3414
3415 qla2x00_start_iocbs(vha, vha->req);
3416 return ret;
3417}
3418
3419static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3420 struct imm_ntfy_from_isp *imm, int ha_locked)
3421{
3422 unsigned long flags = 0;
3423 int rc;
3424
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003425 if (ha_locked) {
3426 rc = __qlt_send_term_imm_notif(vha, imm);
3427
3428#if 0 /* Todo */
3429 if (rc == -ENOMEM)
3430 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003431#else
3432 if (rc) {
3433 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003434#endif
3435 goto done;
3436 }
3437
3438 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3439 rc = __qlt_send_term_imm_notif(vha, imm);
3440
3441#if 0 /* Todo */
3442 if (rc == -ENOMEM)
3443 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3444#endif
3445
3446done:
3447 if (!ha_locked)
3448 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3449}
3450
Quinn Tran83548fe2017-06-02 09:12:01 -07003451/*
3452 * If hardware_lock held on entry, might drop it, then reaquire
3453 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3454 */
Quinn Tran82de8022017-06-13 20:47:17 -07003455static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003456 struct qla_tgt_cmd *cmd,
3457 struct atio_from_isp *atio)
3458{
Quinn Tran82de8022017-06-13 20:47:17 -07003459 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003460 struct ctio7_to_24xx *ctio24;
3461 struct qla_hw_data *ha = vha->hw;
3462 request_t *pkt;
3463 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003464 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003465
Quinn Tran83548fe2017-06-02 09:12:01 -07003466 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003467
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003468 if (cmd)
3469 vha = cmd->vha;
3470
Quinn Tran82de8022017-06-13 20:47:17 -07003471 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003472 if (pkt == NULL) {
3473 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3474 "qla_target(%d): %s failed: unable to allocate "
3475 "request packet\n", vha->vp_idx, __func__);
3476 return -ENOMEM;
3477 }
3478
3479 if (cmd != NULL) {
3480 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3481 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3482 "qla_target(%d): Terminating cmd %p with "
3483 "incorrect state %d\n", vha->vp_idx, cmd,
3484 cmd->state);
3485 } else
3486 ret = 1;
3487 }
3488
Quinn Tran60a9ead2017-06-13 20:47:28 -07003489 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003490 pkt->entry_count = 1;
3491 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3492
3493 ctio24 = (struct ctio7_to_24xx *)pkt;
3494 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003495 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003496 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003497 ctio24->vp_index = vha->vp_idx;
3498 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3499 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3500 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3501 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003502 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3503 CTIO7_FLAGS_TERMINATE;
3504 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003505 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3506 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003507
3508 /* Most likely, it isn't needed */
3509 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3510 &atio->u.isp24.fcp_cmnd.add_cdb[
3511 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3512 if (ctio24->u.status1.residual != 0)
3513 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3514
Himanshu Madhani63163e02014-09-25 06:14:59 -04003515 /* Memory Barrier */
3516 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003517 if (qpair->reqq_start_iocbs)
3518 qpair->reqq_start_iocbs(qpair);
3519 else
3520 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003521 return ret;
3522}
3523
Quinn Tran82de8022017-06-13 20:47:17 -07003524static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003525 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3526 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003527{
Quinn Tran82de8022017-06-13 20:47:17 -07003528 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003529 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003530 int rc;
3531
Quinn Tran82de8022017-06-13 20:47:17 -07003532 /* why use different vha? NPIV */
3533 if (cmd)
3534 vha = cmd->vha;
3535 else
3536 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003537
3538 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003539 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003540 if (rc == -ENOMEM)
3541 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003542 goto done;
3543 }
Quinn Tran82de8022017-06-13 20:47:17 -07003544 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3545 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003546 if (rc == -ENOMEM)
3547 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003548
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003549done:
Quinn Trana07100e2015-12-07 19:48:57 -05003550 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003551 if (cmd->sg_mapped)
3552 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003553 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3554 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003555
3556 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003557 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003558
Quinn Tran7b898542014-04-11 16:54:44 -04003559 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003560}
3561
Quinn Tran33e79972014-09-25 06:14:55 -04003562static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3563{
3564 struct list_head free_list;
3565 struct qla_tgt_cmd *cmd, *tcmd;
3566
3567 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003568 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003569
3570 cmd = tcmd = NULL;
3571 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3572 INIT_LIST_HEAD(&free_list);
3573 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3574
3575 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3576 list_del(&cmd->cmd_list);
3577 /* This cmd was never sent to TCM. There is no need
3578 * to schedule free or call free_cmd
3579 */
3580 qlt_free_cmd(cmd);
3581 vha->hw->tgt.num_qfull_cmds_alloc--;
3582 }
3583 }
3584 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3585}
3586
3587static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3588{
3589 uint32_t total_leaked;
3590
3591 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3592
3593 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3594 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3595
3596 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3597 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003598 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003599
3600 if (IS_P3P_TYPE(vha->hw))
3601 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3602 else
3603 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3604 qla2xxx_wake_dpc(vha);
3605 }
3606
3607}
3608
Quinn Trana07100e2015-12-07 19:48:57 -05003609int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003610{
3611 struct qla_tgt *tgt = cmd->tgt;
3612 struct scsi_qla_host *vha = tgt->vha;
3613 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003614 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003615
3616 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3617 "qla_target(%d): terminating exchange for aborted cmd=%p "
3618 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3619 se_cmd->tag);
3620
Quinn Trana07100e2015-12-07 19:48:57 -05003621 spin_lock_irqsave(&cmd->cmd_lock, flags);
3622 if (cmd->aborted) {
3623 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3624 /*
3625 * It's normal to see 2 calls in this path:
3626 * 1) XFER Rdy completion + CMD_T_ABORT
3627 * 2) TCM TMR - drain_state_list
3628 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003629 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3630 "multiple abort. %p transport_state %x, t_state %x, "
3631 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3632 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003633 return EIO;
3634 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003635 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003636 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003637 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003638
Quinn Tran82de8022017-06-13 20:47:17 -07003639 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003640 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003641}
3642EXPORT_SYMBOL(qlt_abort_cmd);
3643
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003644void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3645{
Quinn Tran5d964832017-01-19 22:27:59 -08003646 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003647
Quinn Tranf83adb62014-04-11 16:54:43 -04003648 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3649 "%s: se_cmd[%p] ox_id %04x\n",
3650 __func__, &cmd->se_cmd,
3651 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003652
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003653 BUG_ON(cmd->cmd_in_wq);
3654
Quinn Trana07100e2015-12-07 19:48:57 -05003655 if (cmd->sg_mapped)
3656 qlt_unmap_sg(cmd->vha, cmd);
3657
Quinn Tran33e79972014-09-25 06:14:55 -04003658 if (!cmd->q_full)
3659 qlt_decr_num_pend_cmds(cmd->vha);
3660
Quinn Tranf83adb62014-04-11 16:54:43 -04003661 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003662 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003663 if (unlikely(cmd->free_sg))
3664 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003665
3666 if (!sess || !sess->se_sess) {
3667 WARN_ON(1);
3668 return;
3669 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003670 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003671 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003672}
3673EXPORT_SYMBOL(qlt_free_cmd);
3674
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003675/*
3676 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3677 */
Quinn Tran82de8022017-06-13 20:47:17 -07003678static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003679 struct qla_tgt_cmd *cmd, uint32_t status)
3680{
3681 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003682 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003683
Quinn Tranbe251522017-03-15 09:48:49 -07003684 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003685 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003686 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3687 "se_cmd=%p tag[%x] op %#x/%s",
3688 cmd->lba, cmd->lba,
3689 cmd->num_blks, &cmd->se_cmd,
3690 cmd->atio.u.isp24.exchange_addr,
3691 cmd->se_cmd.prot_op,
3692 prot_op_str(cmd->se_cmd.prot_op));
3693
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003694 if (ctio != NULL) {
3695 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3696 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003697 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003698 } else
3699 term = 1;
3700
3701 if (term)
Quinn Tran82de8022017-06-13 20:47:17 -07003702 qlt_term_ctio_exchange(qpair, ctio, cmd, status);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003703
3704 return term;
3705}
3706
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003707
3708/* ha->hardware_lock supposed to be held on entry */
3709static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003710 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003711{
3712 struct qla_tgt_cmd *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003713 struct req_que *req;
3714 int qid = GET_QID(handle);
3715 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003716
Quinn Tran82de8022017-06-13 20:47:17 -07003717 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3718 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003719
Quinn Tran82de8022017-06-13 20:47:17 -07003720 if (qid == rsp->req->id) {
3721 req = rsp->req;
3722 } else if (vha->hw->req_q_map[qid]) {
3723 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3724 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3725 vha->vp_idx, rsp->id, handle);
3726 req = vha->hw->req_q_map[qid];
3727 } else {
3728 return NULL;
3729 }
Arun Easi667024a2014-09-25 06:14:47 -04003730
Quinn Tran82de8022017-06-13 20:47:17 -07003731 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003732
Quinn Tran82de8022017-06-13 20:47:17 -07003733 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003734 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003735 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3736 "qla_target(%d): Wrong handle %x received\n",
3737 vha->vp_idx, handle);
3738 return NULL;
3739 }
Quinn Tran82de8022017-06-13 20:47:17 -07003740
3741 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3742 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003743 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003744 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3745 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003746 return NULL;
3747 }
Quinn Tran82de8022017-06-13 20:47:17 -07003748 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003749 } else if (ctio != NULL) {
3750 /* We can't get loop ID from CTIO7 */
3751 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3752 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3753 "support NULL handles\n", vha->vp_idx);
3754 return NULL;
3755 }
3756
3757 return cmd;
3758}
3759
Arun Easic0cb4492014-09-25 06:14:51 -04003760/* hardware_lock should be held by caller. */
Quinn Tranc5419e22017-06-13 20:47:16 -07003761void
Arun Easic0cb4492014-09-25 06:14:51 -04003762qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3763{
3764 struct qla_hw_data *ha = vha->hw;
Arun Easic0cb4492014-09-25 06:14:51 -04003765
3766 if (cmd->sg_mapped)
3767 qlt_unmap_sg(vha, cmd);
3768
Arun Easic0cb4492014-09-25 06:14:51 -04003769 /* TODO: fix debug message type and ids. */
3770 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3771 ql_dbg(ql_dbg_io, vha, 0xff00,
Quinn Tran82de8022017-06-13 20:47:17 -07003772 "HOST-ABORT: state=PROCESSED.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003773 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3774 cmd->write_data_transferred = 0;
3775 cmd->state = QLA_TGT_STATE_DATA_IN;
3776
3777 ql_dbg(ql_dbg_io, vha, 0xff01,
Quinn Tran82de8022017-06-13 20:47:17 -07003778 "HOST-ABORT: state=DATA_IN.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003779
3780 ha->tgt.tgt_ops->handle_data(cmd);
3781 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003782 } else {
3783 ql_dbg(ql_dbg_io, vha, 0xff03,
Quinn Tran82de8022017-06-13 20:47:17 -07003784 "HOST-ABORT: state=BAD(%d).\n",
Arun Easic0cb4492014-09-25 06:14:51 -04003785 cmd->state);
3786 dump_stack();
3787 }
3788
Quinn Tran1eb42f92017-01-19 22:27:55 -08003789 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003790 ha->tgt.tgt_ops->free_cmd(cmd);
3791}
3792
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003793/*
3794 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3795 */
Quinn Tran82de8022017-06-13 20:47:17 -07003796static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3797 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003798{
3799 struct qla_hw_data *ha = vha->hw;
3800 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003801 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07003802 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003803
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003804 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3805 /* That could happen only in case of an error/reset/abort */
3806 if (status != CTIO_SUCCESS) {
3807 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3808 "Intermediate CTIO received"
3809 " (status %x)\n", status);
3810 }
3811 return;
3812 }
3813
Quinn Tran82de8022017-06-13 20:47:17 -07003814 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003815 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003816 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003817
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003818 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003819 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003820
Joern Engelf9b67212014-09-16 16:23:18 -04003821 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003822
3823 if (unlikely(status != CTIO_SUCCESS)) {
3824 switch (status & 0xFFFF) {
3825 case CTIO_LIP_RESET:
3826 case CTIO_TARGET_RESET:
3827 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003828 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003829 case CTIO_TIMEOUT:
3830 case CTIO_INVALID_RX_ID:
3831 /* They are OK */
3832 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3833 "qla_target(%d): CTIO with "
3834 "status %#x received, state %x, se_cmd %p, "
3835 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3836 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3837 status, cmd->state, se_cmd);
3838 break;
3839
3840 case CTIO_PORT_LOGGED_OUT:
3841 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003842 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003843 int logged_out =
3844 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3845
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003846 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003847 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003848 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003849 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003850 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003851
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003852 if (logged_out && cmd->sess) {
3853 /*
3854 * Session is already logged out, but we need
3855 * to notify initiator, who's not aware of this
3856 */
3857 cmd->sess->logout_on_delete = 0;
3858 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07003859 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08003860 "%s %d %8phC post del sess\n",
3861 __func__, __LINE__, cmd->sess->port_name);
3862
3863 qlt_schedule_sess_for_deletion_lock(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003864 }
3865 break;
3866 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003867 case CTIO_DIF_ERROR: {
3868 struct ctio_crc_from_fw *crc =
3869 (struct ctio_crc_from_fw *)ctio;
3870 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07003871 "qla_target(%d): CTIO with DIF_ERROR status %x "
3872 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3873 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003874 vha->vp_idx, status, cmd->state, se_cmd,
3875 *((u64 *)&crc->actual_dif[0]),
3876 *((u64 *)&crc->expected_dif[0]));
3877
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003878 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07003879 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04003880 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003881 default:
3882 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003883 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003884 vha->vp_idx, status, cmd->state, se_cmd);
3885 break;
3886 }
3887
Quinn Tran7b898542014-04-11 16:54:44 -04003888
Quinn Tran193b50b2015-12-17 14:57:03 -05003889 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003890 * cmd is already aborted/terminated, we don't
3891 * need to terminate again. The exchange is already
3892 * cleaned up/freed at FW level. Just cleanup at driver
3893 * level.
3894 */
3895 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003896 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003897 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07003898 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003899 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003900 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003901 }
3902
3903 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003904 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003905 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003906 cmd->state = QLA_TGT_STATE_DATA_IN;
3907
Bart Van Assche52c82822015-07-09 07:23:26 -07003908 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003909 cmd->write_data_transferred = 1;
3910
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003911 ha->tgt.tgt_ops->handle_data(cmd);
3912 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003913 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003914 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003915 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003916 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003917 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003918 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003919 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3920 "qla_target(%d): A command in state (%d) should "
3921 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3922 }
3923
Quinn Tran7b898542014-04-11 16:54:44 -04003924 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003925 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003926 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3927 dump_stack();
3928 }
3929
3930 ha->tgt.tgt_ops->free_cmd(cmd);
3931}
3932
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003933static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3934 uint8_t task_codes)
3935{
3936 int fcp_task_attr;
3937
3938 switch (task_codes) {
3939 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003940 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003941 break;
3942 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003943 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003944 break;
3945 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003946 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003947 break;
3948 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003949 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003950 break;
3951 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003952 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003953 break;
3954 default:
3955 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3956 "qla_target: unknown task code %x, use ORDERED instead\n",
3957 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003958 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003959 break;
3960 }
3961
3962 return fcp_task_attr;
3963}
3964
Quinn Tran5d964832017-01-19 22:27:59 -08003965static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003966 uint8_t *);
3967/*
3968 * Process context for I/O path into tcm_qla2xxx code
3969 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003970static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003971{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003972 scsi_qla_host_t *vha = cmd->vha;
3973 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08003974 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003975 struct atio_from_isp *atio = &cmd->atio;
3976 unsigned char *cdb;
3977 unsigned long flags;
3978 uint32_t data_length;
3979 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003980 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003981
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003982 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003983 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003984
Quinn Tran193b50b2015-12-17 14:57:03 -05003985 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003986 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3987 "cmd with tag %u is aborted\n",
3988 cmd->atio.u.isp24.exchange_addr);
3989 goto out_term;
3990 }
3991
Quinn Trana07100e2015-12-07 19:48:57 -05003992 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003993 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02003994 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003995
3996 if (atio->u.isp24.fcp_cmnd.rddata &&
3997 atio->u.isp24.fcp_cmnd.wrdata) {
3998 bidi = 1;
3999 data_dir = DMA_TO_DEVICE;
4000 } else if (atio->u.isp24.fcp_cmnd.rddata)
4001 data_dir = DMA_FROM_DEVICE;
4002 else if (atio->u.isp24.fcp_cmnd.wrdata)
4003 data_dir = DMA_TO_DEVICE;
4004 else
4005 data_dir = DMA_NONE;
4006
4007 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4008 atio->u.isp24.fcp_cmnd.task_attr);
4009 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4010 &atio->u.isp24.fcp_cmnd.add_cdb[
4011 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4012
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004013 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4014 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004015 if (ret != 0)
4016 goto out_term;
4017 /*
4018 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4019 */
Quinn Tran75601512015-12-17 14:57:04 -05004020 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004021 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004022 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004023 return;
4024
4025out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004026 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004027 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004028 * cmd has not sent to target yet, so pass NULL as the second
4029 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004030 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004031 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004032 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4033 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004034
4035 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004036 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Quinn Tran82de8022017-06-13 20:47:17 -07004037 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004038
4039 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004040 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004041 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004042}
4043
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004044static void qlt_do_work(struct work_struct *work)
4045{
4046 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004047 scsi_qla_host_t *vha = cmd->vha;
4048 unsigned long flags;
4049
4050 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4051 list_del(&cmd->cmd_list);
4052 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004053
4054 __qlt_do_work(cmd);
4055}
4056
Quinn Tran09620ee2017-06-13 20:47:20 -07004057void qlt_clr_qp_table(struct scsi_qla_host *vha)
4058{
4059 unsigned long flags;
4060 struct qla_hw_data *ha = vha->hw;
4061 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4062 void *node;
4063 u64 key = 0;
4064
4065 ql_log(ql_log_info, vha, 0x706c,
4066 "User update Number of Active Qpairs %d\n",
4067 ha->tgt.num_act_qpairs);
4068
4069 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4070
4071 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4072 btree_remove64(&tgt->lun_qpair_map, key);
4073
4074 ha->base_qpair->lun_cnt = 0;
4075 for (key = 0; key < ha->max_qpairs; key++)
4076 if (ha->queue_pair_map[key])
4077 ha->queue_pair_map[key]->lun_cnt = 0;
4078
4079 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4080}
4081
Quinn Trane326d222017-06-13 20:47:18 -07004082static void qlt_assign_qpair(struct scsi_qla_host *vha,
4083 struct qla_tgt_cmd *cmd)
4084{
4085 struct qla_qpair *qpair, *qp;
4086 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4087 struct qla_qpair_hint *h;
4088
4089 if (vha->flags.qpairs_available) {
4090 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4091 if (unlikely(!h)) {
4092 /* spread lun to qpair ratio evently */
4093 int lcnt = 0, rc;
4094 struct scsi_qla_host *base_vha =
4095 pci_get_drvdata(vha->hw->pdev);
4096
4097 qpair = vha->hw->base_qpair;
4098 if (qpair->lun_cnt == 0) {
4099 qpair->lun_cnt++;
4100 h = qla_qpair_to_hint(tgt, qpair);
4101 BUG_ON(!h);
4102 rc = btree_insert64(&tgt->lun_qpair_map,
4103 cmd->unpacked_lun, h, GFP_ATOMIC);
4104 if (rc) {
4105 qpair->lun_cnt--;
4106 ql_log(ql_log_info, vha, 0xd037,
4107 "Unable to insert lun %llx into lun_qpair_map\n",
4108 cmd->unpacked_lun);
4109 }
4110 goto out;
4111 } else {
4112 lcnt = qpair->lun_cnt;
4113 }
4114
4115 h = NULL;
4116 list_for_each_entry(qp, &base_vha->qp_list,
4117 qp_list_elem) {
4118 if (qp->lun_cnt == 0) {
4119 qp->lun_cnt++;
4120 h = qla_qpair_to_hint(tgt, qp);
4121 BUG_ON(!h);
4122 rc = btree_insert64(&tgt->lun_qpair_map,
4123 cmd->unpacked_lun, h, GFP_ATOMIC);
4124 if (rc) {
4125 qp->lun_cnt--;
4126 ql_log(ql_log_info, vha, 0xd038,
4127 "Unable to insert lun %llx into lun_qpair_map\n",
4128 cmd->unpacked_lun);
4129 }
4130 qpair = qp;
4131 goto out;
4132 } else {
4133 if (qp->lun_cnt < lcnt) {
4134 lcnt = qp->lun_cnt;
4135 qpair = qp;
4136 continue;
4137 }
4138 }
4139 }
4140 BUG_ON(!qpair);
4141 qpair->lun_cnt++;
4142 h = qla_qpair_to_hint(tgt, qpair);
4143 BUG_ON(!h);
4144 rc = btree_insert64(&tgt->lun_qpair_map,
4145 cmd->unpacked_lun, h, GFP_ATOMIC);
4146 if (rc) {
4147 qpair->lun_cnt--;
4148 ql_log(ql_log_info, vha, 0xd039,
4149 "Unable to insert lun %llx into lun_qpair_map\n",
4150 cmd->unpacked_lun);
4151 }
4152 }
4153 } else {
4154 h = &tgt->qphints[0];
4155 }
4156out:
4157 cmd->qpair = h->qpair;
4158 cmd->se_cmd.cpuid = h->cpuid;
4159}
4160
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004161static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004162 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004163 struct atio_from_isp *atio)
4164{
4165 struct se_session *se_sess = sess->se_sess;
4166 struct qla_tgt_cmd *cmd;
4167 int tag;
4168
4169 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4170 if (tag < 0)
4171 return NULL;
4172
4173 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4174 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004175 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004176 memcpy(&cmd->atio, atio, sizeof(*atio));
4177 cmd->state = QLA_TGT_STATE_NEW;
4178 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004179 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004180 cmd->vha = vha;
4181 cmd->se_cmd.map_tag = tag;
4182 cmd->sess = sess;
4183 cmd->loop_id = sess->loop_id;
4184 cmd->conf_compl_supported = sess->conf_compl_supported;
4185
Quinn Tran1eb42f92017-01-19 22:27:55 -08004186 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004187 cmd->jiffies_at_alloc = get_jiffies_64();
4188
Quinn Trane326d222017-06-13 20:47:18 -07004189 cmd->unpacked_lun = scsilun_to_int(
4190 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4191 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004192 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004193 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004194
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004195 return cmd;
4196}
4197
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004198static void qlt_create_sess_from_atio(struct work_struct *work)
4199{
4200 struct qla_tgt_sess_op *op = container_of(work,
4201 struct qla_tgt_sess_op, work);
4202 scsi_qla_host_t *vha = op->vha;
4203 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004204 struct fc_port *sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004205 struct qla_tgt_cmd *cmd;
4206 unsigned long flags;
4207 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4208
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004209 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4210 list_del(&op->cmd_list);
4211 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4212
4213 if (op->aborted) {
4214 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4215 "sess_op with tag %u is aborted\n",
4216 op->atio.u.isp24.exchange_addr);
4217 goto out_term;
4218 }
4219
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004220 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004221 "qla_target(%d): Unable to find wwn login"
4222 " (s_id %x:%x:%x), trying to create it manually\n",
4223 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004224
4225 if (op->atio.u.raw.entry_count > 1) {
4226 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004227 "Dropping multy entry atio %p\n", &op->atio);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004228 goto out_term;
4229 }
4230
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004231 sess = qlt_make_local_sess(vha, s_id);
4232 /* sess has an extra creation ref. */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004233
4234 if (!sess)
4235 goto out_term;
4236 /*
4237 * Now obtain a pre-allocated session tag using the original op->atio
4238 * packet header, and dispatch into __qlt_do_work() using the existing
4239 * process context.
4240 */
4241 cmd = qlt_get_tag(vha, sess, &op->atio);
4242 if (!cmd) {
Quinn Tran82de8022017-06-13 20:47:17 -07004243 struct qla_qpair *qpair = ha->base_qpair;
4244
4245 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4246 qlt_send_busy(qpair, &op->atio, SAM_STAT_BUSY);
4247 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4248
4249 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004250 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran82de8022017-06-13 20:47:17 -07004251 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004252 kfree(op);
4253 return;
4254 }
Quinn Tran726b8542017-01-19 22:28:00 -08004255
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004256 /*
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02004257 * __qlt_do_work() will call qlt_put_sess() to release
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004258 * the extra reference taken above by qlt_make_local_sess()
4259 */
4260 __qlt_do_work(cmd);
4261 kfree(op);
4262 return;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004263out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07004264 qlt_send_term_exchange(vha->hw->base_qpair, NULL, &op->atio, 0, 0);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004265 kfree(op);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004266}
4267
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004268/* ha->hardware_lock supposed to be held on entry */
4269static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4270 struct atio_from_isp *atio)
4271{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004272 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004273 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004274 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004275 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004276 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004277
4278 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004279 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004280 "New command while device %p is shutting down\n", tgt);
4281 return -EFAULT;
4282 }
4283
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004284 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4285 if (unlikely(!sess)) {
4286 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4287 GFP_ATOMIC);
4288 if (!op)
4289 return -ENOMEM;
4290
4291 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04004292 op->vha = vha;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004293
Quinn Tran8b631d82017-06-02 09:11:54 -07004294 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004295 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
Quinn Tran8b631d82017-06-02 09:11:54 -07004296 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004297
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004298 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4299 queue_work(qla_tgt_wq, &op->work);
4300 return 0;
4301 }
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004302
4303 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4304 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004305 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004306 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004307 "New command while old session %p is being deleted\n",
4308 sess);
4309 return -EFAULT;
4310 }
4311
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004312 /*
4313 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4314 */
Quinn Tran726b8542017-01-19 22:28:00 -08004315 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004316 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004317 "%s: kref_get fail, %8phC oxid %x \n",
4318 __func__, sess->port_name,
4319 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4320 return -EFAULT;
4321 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004322
4323 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004324 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004325 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004326 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004327 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4328 ha->tgt.tgt_ops->put_sess(sess);
4329 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004330 return -ENOMEM;
4331 }
4332
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004333 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004334 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004335
Quinn Tran726b8542017-01-19 22:28:00 -08004336 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004337 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004338 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004339
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004340 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004341 if (vha->flags.qpairs_available) {
4342 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4343 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004344 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4345 queue_work_on(smp_processor_id(), qla_tgt_wq,
4346 &cmd->work);
4347 else
4348 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4349 &cmd->work);
4350 } else {
4351 queue_work(qla_tgt_wq, &cmd->work);
4352 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004353
Quinn Tran82de8022017-06-13 20:47:17 -07004354 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004355}
4356
4357/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004358static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004359 int fn, void *iocb, int flags)
4360{
4361 struct scsi_qla_host *vha = sess->vha;
4362 struct qla_hw_data *ha = vha->hw;
4363 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004364 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004365 int res;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004366
4367 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4368 if (!mcmd) {
4369 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4370 "qla_target(%d): Allocation of management "
4371 "command failed, some commands and their data could "
4372 "leak\n", vha->vp_idx);
4373 return -ENOMEM;
4374 }
4375 memset(mcmd, 0, sizeof(*mcmd));
4376 mcmd->sess = sess;
4377
4378 if (iocb) {
4379 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4380 sizeof(mcmd->orig_iocb.imm_ntfy));
4381 }
4382 mcmd->tmr_func = fn;
4383 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004384 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07004385 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004386 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004387
4388 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004389 case QLA_TGT_LUN_RESET:
Quinn Tranbe92fc32017-01-19 22:27:54 -08004390 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4391 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004392 }
4393
Quinn Tranbe92fc32017-01-19 22:27:54 -08004394 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004395 if (res != 0) {
4396 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4397 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4398 sess->vha->vp_idx, res);
4399 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4400 return -EFAULT;
4401 }
4402
4403 return 0;
4404}
4405
4406/* ha->hardware_lock supposed to be held on entry */
4407static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4408{
4409 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4410 struct qla_hw_data *ha = vha->hw;
4411 struct qla_tgt *tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004412 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004413 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004414 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004415 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004416
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004417 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004418
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004419 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004420
4421 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004422 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4423 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004424 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4425
Quinn Tranf775bd12017-06-02 09:11:59 -07004426 unpacked_lun =
4427 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004428
4429 if (!sess) {
4430 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4431 "qla_target(%d): task mgmt fn 0x%x for "
4432 "non-existant session\n", vha->vp_idx, fn);
4433 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4434 sizeof(struct atio_from_isp));
4435 }
4436
Quinn Tran726b8542017-01-19 22:28:00 -08004437 if (sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004438 return -EFAULT;
4439
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004440 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4441}
4442
4443/* ha->hardware_lock supposed to be held on entry */
4444static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004445 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004446{
4447 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4448 struct qla_hw_data *ha = vha->hw;
4449 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004450 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004451 int rc;
4452
4453 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4454 if (mcmd == NULL) {
4455 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4456 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4457 vha->vp_idx, __func__);
4458 return -ENOMEM;
4459 }
4460 memset(mcmd, 0, sizeof(*mcmd));
4461
4462 mcmd->sess = sess;
4463 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4464 sizeof(mcmd->orig_iocb.imm_ntfy));
4465
Quinn Tranf775bd12017-06-02 09:11:59 -07004466 unpacked_lun =
4467 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004468 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004469 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004470 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004471
Quinn Tranbe92fc32017-01-19 22:27:54 -08004472 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004473 le16_to_cpu(iocb->u.isp2x.seq_id));
4474 if (rc != 0) {
4475 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4476 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4477 vha->vp_idx, rc);
4478 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4479 return -EFAULT;
4480 }
4481
4482 return 0;
4483}
4484
4485/* ha->hardware_lock supposed to be held on entry */
4486static int qlt_abort_task(struct scsi_qla_host *vha,
4487 struct imm_ntfy_from_isp *iocb)
4488{
4489 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004490 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004491 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004492 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004493
4494 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4495
Quinn Tran75601512015-12-17 14:57:04 -05004496 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004497 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004498 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4499
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004500 if (sess == NULL) {
4501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4502 "qla_target(%d): task abort for unexisting "
4503 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004504 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004505 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4506 }
4507
4508 return __qlt_abort_task(vha, iocb, sess);
4509}
4510
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004511void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4512{
Quinn Tran726b8542017-01-19 22:28:00 -08004513 if (rc != MBS_COMMAND_COMPLETE) {
4514 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4515 "%s: se_sess %p / sess %p from"
4516 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4517 " LOGO failed: %#x\n",
4518 __func__,
4519 fcport->se_sess,
4520 fcport,
4521 fcport->port_name, fcport->loop_id,
4522 fcport->d_id.b.domain, fcport->d_id.b.area,
4523 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004524 }
Quinn Tran726b8542017-01-19 22:28:00 -08004525
4526 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004527}
4528
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004529/*
4530* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4531*
4532* Schedules sessions with matching port_id/loop_id but different wwn for
4533* deletion. Returns existing session with matching wwn if present.
4534* Null otherwise.
4535*/
Quinn Tran726b8542017-01-19 22:28:00 -08004536struct fc_port *
4537qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004538 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004539{
Quinn Tran5d964832017-01-19 22:27:59 -08004540 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004541 uint64_t other_wwn;
4542
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004543 *conflict_sess = NULL;
4544
Quinn Tran5d964832017-01-19 22:27:59 -08004545 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004546
4547 other_wwn = wwn_to_u64(other_sess->port_name);
4548
4549 if (wwn == other_wwn) {
4550 WARN_ON(sess);
4551 sess = other_sess;
4552 continue;
4553 }
4554
4555 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004556 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004557 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004558 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004559 "Invalidating sess %p loop_id %d wwn %llx.\n",
4560 other_sess, other_sess->loop_id, other_wwn);
4561
4562 /*
4563 * logout_on_delete is set by default, but another
4564 * session that has the same s_id/loop_id combo
4565 * might have cleared it when requested this session
4566 * deletion, so don't touch it
4567 */
4568 qlt_schedule_sess_for_deletion(other_sess, true);
4569 } else {
4570 /*
4571 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004572 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004573 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004574 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004575 "Invalidating sess %p loop_id %d wwn %llx.\n",
4576 other_sess, other_sess->loop_id, other_wwn);
4577
4578
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004579 other_sess->keep_nport_handle = 1;
4580 *conflict_sess = other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004581 qlt_schedule_sess_for_deletion(other_sess,
4582 true);
4583 }
4584 continue;
4585 }
4586
4587 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004588 if ((loop_id == other_sess->loop_id) &&
4589 (loop_id != FC_NO_LOOP_ID)) {
4590 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004591 "Invalidating sess %p loop_id %d wwn %llx.\n",
4592 other_sess, other_sess->loop_id, other_wwn);
4593
4594 /* Same loop_id but different s_id
4595 * Ok to kill and logout */
4596 qlt_schedule_sess_for_deletion(other_sess, true);
4597 }
4598 }
4599
4600 return sess;
4601}
4602
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004603/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4604static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4605{
4606 struct qla_tgt_sess_op *op;
4607 struct qla_tgt_cmd *cmd;
4608 uint32_t key;
4609 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004610 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004611
4612 key = (((u32)s_id->b.domain << 16) |
4613 ((u32)s_id->b.area << 8) |
4614 ((u32)s_id->b.al_pa));
4615
Quinn Tran8b631d82017-06-02 09:11:54 -07004616 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004617 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4618 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004619
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004620 if (op_key == key) {
4621 op->aborted = true;
4622 count++;
4623 }
4624 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004625
4626 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4627 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4628 if (op_key == key) {
4629 op->aborted = true;
4630 count++;
4631 }
4632 }
4633
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004634 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4635 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4636 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004637 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004638 count++;
4639 }
4640 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004641 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004642
4643 return count;
4644}
4645
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004646/*
4647 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4648 */
4649static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4650 struct imm_ntfy_from_isp *iocb)
4651{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004652 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004653 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004654 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004655 uint64_t wwn;
4656 port_id_t port_id;
4657 uint16_t loop_id;
4658 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004659 int res = 0;
Quinn Tran5d964832017-01-19 22:27:59 -08004660 struct qlt_plogi_ack_t *pla;
Quinn Tran75601512015-12-17 14:57:04 -05004661 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004662
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004663 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4664
4665 port_id.b.domain = iocb->u.isp24.port_id[2];
4666 port_id.b.area = iocb->u.isp24.port_id[1];
4667 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4668 port_id.b.rsvd_1 = 0;
4669
4670 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4671
Quinn Tran726b8542017-01-19 22:28:00 -08004672 ql_dbg(ql_dbg_disc, vha, 0xf026,
4673 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4674 vha->vp_idx, iocb->u.isp24.port_id[2],
4675 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4676 iocb->u.isp24.status_subcode, loop_id,
4677 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004678
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004679 /* res = 1 means ack at the end of thread
4680 * res = 0 means ack async/later.
4681 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004682 switch (iocb->u.isp24.status_subcode) {
4683 case ELS_PLOGI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004684
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004685 /* Mark all stale commands in qla_tgt_wq for deletion */
4686 abort_cmds_for_s_id(vha, &port_id);
4687
Quinn Tran75601512015-12-17 14:57:04 -05004688 if (wwn) {
4689 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004690 sess = qlt_find_sess_invalidate_other(vha, wwn,
4691 port_id, loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004692 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4693 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004694
Quinn Tran726b8542017-01-19 22:28:00 -08004695 if (IS_SW_RESV_ADDR(port_id)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004696 res = 1;
4697 break;
4698 }
4699
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004700 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4701 if (!pla) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004702 qlt_send_term_imm_notif(vha, iocb, 1);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004703 break;
4704 }
4705
4706 res = 0;
4707
Quinn Tran726b8542017-01-19 22:28:00 -08004708 if (conflict_sess) {
4709 conflict_sess->login_gen++;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004710 qlt_plogi_ack_link(vha, pla, conflict_sess,
Quinn Tran726b8542017-01-19 22:28:00 -08004711 QLT_PLOGI_LINK_CONFLICT);
4712 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004713
Quinn Tran726b8542017-01-19 22:28:00 -08004714 if (!sess) {
4715 pla->ref_count++;
4716 qla24xx_post_newsess_work(vha, &port_id,
4717 iocb->u.isp24.port_name, pla);
4718 res = 0;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004719 break;
Quinn Tran726b8542017-01-19 22:28:00 -08004720 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004721
4722 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
Quinn Tran726b8542017-01-19 22:28:00 -08004723 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4724 sess->d_id = port_id;
4725 sess->login_gen++;
4726
4727 switch (sess->disc_state) {
4728 case DSC_DELETED:
4729 qlt_plogi_ack_unref(vha, pla);
4730 break;
4731
4732 default:
4733 /*
4734 * Under normal circumstances we want to release nport handle
4735 * during LOGO process to avoid nport handle leaks inside FW.
4736 * The exception is when LOGO is done while another PLOGI with
4737 * the same nport handle is waiting as might be the case here.
4738 * Note: there is always a possibily of a race where session
4739 * deletion has already started for other reasons (e.g. ACL
4740 * removal) and now PLOGI arrives:
4741 * 1. if PLOGI arrived in FW after nport handle has been freed,
4742 * FW must have assigned this PLOGI a new/same handle and we
4743 * can proceed ACK'ing it as usual when session deletion
4744 * completes.
4745 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4746 * bit reached it, the handle has now been released. We'll
4747 * get an error when we ACK this PLOGI. Nothing will be sent
4748 * back to initiator. Initiator should eventually retry
4749 * PLOGI and situation will correct itself.
4750 */
4751 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4752 (sess->d_id.b24 == port_id.b24));
4753
Quinn Tran83548fe2017-06-02 09:12:01 -07004754 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4755 "%s %d %8phC post del sess\n",
4756 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004757
4758
4759 qlt_schedule_sess_for_deletion_lock(sess);
4760 break;
4761 }
4762
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004763 break;
4764
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004765 case ELS_PRLI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004766 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4767
Quinn Tran75601512015-12-17 14:57:04 -05004768 if (wwn) {
4769 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004770 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4771 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004772 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4773 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004774
4775 if (conflict_sess) {
4776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4777 "PRLI with conflicting sess %p port %8phC\n",
4778 conflict_sess, conflict_sess->port_name);
4779 qlt_send_term_imm_notif(vha, iocb, 1);
4780 res = 0;
4781 break;
4782 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004783
4784 if (sess != NULL) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004785 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4786 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004787 /*
4788 * Impatient initiator sent PRLI before last
4789 * PLOGI could finish. Will force him to re-try,
4790 * while last one finishes.
4791 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004792 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004793 "sess %p PRLI received, before plogi ack.\n",
4794 sess);
4795 qlt_send_term_imm_notif(vha, iocb, 1);
4796 res = 0;
4797 break;
4798 }
4799
4800 /*
4801 * This shouldn't happen under normal circumstances,
4802 * since we have deleted the old session during PLOGI
4803 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004804 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004805 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4806 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4807
4808 sess->local = 0;
4809 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08004810 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004811 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004812
4813 if (wd3_lo & BIT_7)
4814 sess->conf_compl_supported = 1;
4815
Quinn Tran726b8542017-01-19 22:28:00 -08004816 if ((wd3_lo & BIT_4) == 0)
4817 sess->port_type = FCT_INITIATOR;
4818 else
4819 sess->port_type = FCT_TARGET;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004820 }
4821 res = 1; /* send notify ack */
4822
4823 /* Make session global (not used in fabric mode) */
4824 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004825 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004826 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07004827 "%s %d %8phC post nack\n",
4828 __func__, __LINE__, sess->port_name);
4829 qla24xx_post_nack_work(vha, sess, iocb,
4830 SRB_NACK_PRLI);
4831 res = 0;
4832 } else {
4833 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4834 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4835 qla2xxx_wake_dpc(vha);
4836 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004837 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08004838 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004839 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07004840 "%s %d %8phC post nack\n",
4841 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004842 qla24xx_post_nack_work(vha, sess, iocb,
4843 SRB_NACK_PRLI);
4844 res = 0;
4845 }
4846 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004847 break;
4848
Quinn Tran41dc5292017-01-19 22:28:03 -08004849 case ELS_TPRLO:
4850 if (le16_to_cpu(iocb->u.isp24.flags) &
4851 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4852 loop_id = 0xFFFF;
4853 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4854 res = 1;
4855 break;
4856 }
4857 /* drop through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004858 case ELS_LOGO:
4859 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08004860 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4861 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4862 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4863
4864 if (sess) {
4865 sess->login_gen++;
4866 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08004867 sess->logo_ack_needed = 1;
4868 memcpy(sess->iocb, iocb, IOCB_SIZE);
4869 }
4870
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004871 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08004872
Quinn Tran83548fe2017-06-02 09:12:01 -07004873 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08004874 "%s: logo %llx res %d sess %p ",
4875 __func__, wwn, res, sess);
4876 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08004877 /*
4878 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4879 * for LOGO_ACK & sess delete
4880 */
Quinn Tran726b8542017-01-19 22:28:00 -08004881 BUG_ON(!sess);
4882 res = 0;
4883 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08004884 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08004885 if (sess) {
4886 qlt_schedule_sess_for_deletion_lock(sess);
4887 res = 0;
4888 }
4889 /* else logo will be ack */
4890 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004891 break;
4892 case ELS_PDISC:
4893 case ELS_ADISC:
4894 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004895 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004896 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07004897 qlt_send_notify_ack(ha->base_qpair,
4898 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004899 tgt->link_reinit_iocb_pending = 0;
4900 }
Quinn Tran726b8542017-01-19 22:28:00 -08004901
4902 sess = qla2x00_find_fcport_by_wwpn(vha,
4903 iocb->u.isp24.port_name, 1);
4904 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004905 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08004906 "sess %p lid %d|%d DS %d LS %d\n",
4907 sess, sess->loop_id, loop_id,
4908 sess->disc_state, sess->fw_login_state);
4909 }
4910
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004911 res = 1; /* send notify ack */
4912 break;
4913 }
4914
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004915 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004916 default:
4917 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4918 "qla_target(%d): Unsupported ELS command %x "
4919 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4920 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4921 break;
4922 }
4923
4924 return res;
4925}
4926
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004927/*
4928 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4929 */
4930static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4931 struct imm_ntfy_from_isp *iocb)
4932{
4933 struct qla_hw_data *ha = vha->hw;
4934 uint32_t add_flags = 0;
4935 int send_notify_ack = 1;
4936 uint16_t status;
4937
4938 status = le16_to_cpu(iocb->u.isp2x.status);
4939 switch (status) {
4940 case IMM_NTFY_LIP_RESET:
4941 {
4942 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4943 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4944 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4945 iocb->u.isp24.status_subcode);
4946
4947 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4948 send_notify_ack = 0;
4949 break;
4950 }
4951
4952 case IMM_NTFY_LIP_LINK_REINIT:
4953 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004954 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004955 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4956 "qla_target(%d): LINK REINIT (loop %#x, "
4957 "subcode %x)\n", vha->vp_idx,
4958 le16_to_cpu(iocb->u.isp24.nport_handle),
4959 iocb->u.isp24.status_subcode);
4960 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07004961 qlt_send_notify_ack(ha->base_qpair,
4962 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004963 }
4964 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4965 tgt->link_reinit_iocb_pending = 1;
4966 /*
4967 * QLogic requires to wait after LINK REINIT for possible
4968 * PDISC or ADISC ELS commands
4969 */
4970 send_notify_ack = 0;
4971 break;
4972 }
4973
4974 case IMM_NTFY_PORT_LOGOUT:
4975 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4976 "qla_target(%d): Port logout (loop "
4977 "%#x, subcode %x)\n", vha->vp_idx,
4978 le16_to_cpu(iocb->u.isp24.nport_handle),
4979 iocb->u.isp24.status_subcode);
4980
4981 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4982 send_notify_ack = 0;
4983 /* The sessions will be cleared in the callback, if needed */
4984 break;
4985
4986 case IMM_NTFY_GLBL_TPRLO:
4987 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4988 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4989 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4990 send_notify_ack = 0;
4991 /* The sessions will be cleared in the callback, if needed */
4992 break;
4993
4994 case IMM_NTFY_PORT_CONFIG:
4995 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4996 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4997 status);
4998 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4999 send_notify_ack = 0;
5000 /* The sessions will be cleared in the callback, if needed */
5001 break;
5002
5003 case IMM_NTFY_GLBL_LOGO:
5004 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5005 "qla_target(%d): Link failure detected\n",
5006 vha->vp_idx);
5007 /* I_T nexus loss */
5008 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5009 send_notify_ack = 0;
5010 break;
5011
5012 case IMM_NTFY_IOCB_OVERFLOW:
5013 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5014 "qla_target(%d): Cannot provide requested "
5015 "capability (IOCB overflowed the immediate notify "
5016 "resource count)\n", vha->vp_idx);
5017 break;
5018
5019 case IMM_NTFY_ABORT_TASK:
5020 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5021 "qla_target(%d): Abort Task (S %08x I %#x -> "
5022 "L %#x)\n", vha->vp_idx,
5023 le16_to_cpu(iocb->u.isp2x.seq_id),
5024 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5025 le16_to_cpu(iocb->u.isp2x.lun));
5026 if (qlt_abort_task(vha, iocb) == 0)
5027 send_notify_ack = 0;
5028 break;
5029
5030 case IMM_NTFY_RESOURCE:
5031 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5032 "qla_target(%d): Out of resources, host %ld\n",
5033 vha->vp_idx, vha->host_no);
5034 break;
5035
5036 case IMM_NTFY_MSG_RX:
5037 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5038 "qla_target(%d): Immediate notify task %x\n",
5039 vha->vp_idx, iocb->u.isp2x.task_flags);
5040 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5041 send_notify_ack = 0;
5042 break;
5043
5044 case IMM_NTFY_ELS:
5045 if (qlt_24xx_handle_els(vha, iocb) == 0)
5046 send_notify_ack = 0;
5047 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005048 default:
5049 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5050 "qla_target(%d): Received unknown immediate "
5051 "notify status %x\n", vha->vp_idx, status);
5052 break;
5053 }
5054
5055 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005056 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5057 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005058}
5059
5060/*
5061 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5062 * This function sends busy to ISP 2xxx or 24xx.
5063 */
Quinn Tran82de8022017-06-13 20:47:17 -07005064static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005065 struct atio_from_isp *atio, uint16_t status)
5066{
Quinn Tran82de8022017-06-13 20:47:17 -07005067 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005068 struct ctio7_to_24xx *ctio24;
5069 struct qla_hw_data *ha = vha->hw;
5070 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005071 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005072 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005073 u16 temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005074
Quinn Tran75601512015-12-17 14:57:04 -05005075 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005076 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5077 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05005078 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005079 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005080 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005081 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005082 }
5083 /* Sending marker isn't necessary, since we called from ISR */
5084
Quinn Tran82de8022017-06-13 20:47:17 -07005085 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005086 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005087 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005088 "qla_target(%d): %s failed: unable to allocate "
5089 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005090 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005091 }
5092
Quinn Tran60a9ead2017-06-13 20:47:28 -07005093 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005094 pkt->entry_count = 1;
5095 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5096
5097 ctio24 = (struct ctio7_to_24xx *)pkt;
5098 ctio24->entry_type = CTIO_TYPE7;
5099 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005100 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005101 ctio24->vp_index = vha->vp_idx;
5102 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5103 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5104 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5105 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005106 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005107 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005108 CTIO7_FLAGS_DONT_RET_CTIO;
5109 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005110 /*
5111 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5112 * if the explicit conformation is used.
5113 */
5114 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5115 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005116 /* Memory Barrier */
5117 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005118 if (qpair->reqq_start_iocbs)
5119 qpair->reqq_start_iocbs(qpair);
5120 else
5121 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005122 return 0;
5123}
5124
5125/*
5126 * This routine is used to allocate a command for either a QFull condition
5127 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5128 * out previously.
5129 */
5130static void
5131qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5132 struct atio_from_isp *atio, uint16_t status, int qfull)
5133{
5134 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5135 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005136 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005137 struct se_session *se_sess;
5138 struct qla_tgt_cmd *cmd;
5139 int tag;
Quinn Tran82de8022017-06-13 20:47:17 -07005140 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005141
5142 if (unlikely(tgt->tgt_stop)) {
5143 ql_dbg(ql_dbg_io, vha, 0x300a,
5144 "New command while device %p is shutting down\n", tgt);
5145 return;
5146 }
5147
5148 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5149 vha->hw->tgt.num_qfull_cmds_dropped++;
5150 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005151 vha->qla_stats.stat_max_qfull_cmds_dropped)
5152 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005153 vha->hw->tgt.num_qfull_cmds_dropped;
5154
5155 ql_dbg(ql_dbg_io, vha, 0x3068,
5156 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5157 vha->vp_idx, __func__,
5158 vha->hw->tgt.num_qfull_cmds_dropped);
5159
5160 qlt_chk_exch_leak_thresh_hold(vha);
5161 return;
5162 }
5163
5164 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5165 (vha, atio->u.isp24.fcp_hdr.s_id);
5166 if (!sess)
5167 return;
5168
5169 se_sess = sess->se_sess;
5170
5171 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5172 if (tag < 0)
5173 return;
5174
5175 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5176 if (!cmd) {
5177 ql_dbg(ql_dbg_io, vha, 0x3009,
5178 "qla_target(%d): %s: Allocation of cmd failed\n",
5179 vha->vp_idx, __func__);
5180
5181 vha->hw->tgt.num_qfull_cmds_dropped++;
5182 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005183 vha->qla_stats.stat_max_qfull_cmds_dropped)
5184 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005185 vha->hw->tgt.num_qfull_cmds_dropped;
5186
5187 qlt_chk_exch_leak_thresh_hold(vha);
5188 return;
5189 }
5190
5191 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5192
5193 qlt_incr_num_pend_cmds(vha);
5194 INIT_LIST_HEAD(&cmd->cmd_list);
5195 memcpy(&cmd->atio, atio, sizeof(*atio));
5196
5197 cmd->tgt = vha->vha_tgt.qla_tgt;
5198 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005199 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005200 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005201 cmd->qpair = ha->base_qpair;
Quinn Tran33e79972014-09-25 06:14:55 -04005202
5203 if (qfull) {
5204 cmd->q_full = 1;
5205 /* NOTE: borrowing the state field to carry the status */
5206 cmd->state = status;
5207 } else
5208 cmd->term_exchg = 1;
5209
Quinn Tran82de8022017-06-13 20:47:17 -07005210 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005211 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5212
5213 vha->hw->tgt.num_qfull_cmds_alloc++;
5214 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005215 vha->qla_stats.stat_max_qfull_cmds_alloc)
5216 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005217 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005218 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005219}
5220
5221int
Quinn Tran82de8022017-06-13 20:47:17 -07005222qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005223{
Quinn Tran82de8022017-06-13 20:47:17 -07005224 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005225 struct qla_hw_data *ha = vha->hw;
5226 unsigned long flags;
5227 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005228 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005229 int rc = 0;
5230
5231 if (list_empty(&ha->tgt.q_full_list))
5232 return 0;
5233
5234 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005235 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005236
Quinn Tran82de8022017-06-13 20:47:17 -07005237 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005238 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005239 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005240 return 0;
5241 }
5242
Quinn Tran82de8022017-06-13 20:47:17 -07005243 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5244 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5245
5246 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5247 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005248 if (cmd->q_full)
5249 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005250 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005251 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005252 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005253
5254 if (rc == -ENOMEM)
5255 break;
5256
5257 if (cmd->q_full)
5258 ql_dbg(ql_dbg_io, vha, 0x3006,
5259 "%s: busy sent for ox_id[%04x]\n", __func__,
5260 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5261 else if (cmd->term_exchg)
5262 ql_dbg(ql_dbg_io, vha, 0x3007,
5263 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5264 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5265 else
5266 ql_dbg(ql_dbg_io, vha, 0x3008,
5267 "%s: Unexpected cmd in QFull list %p\n", __func__,
5268 cmd);
5269
5270 list_del(&cmd->cmd_list);
5271 list_add_tail(&cmd->cmd_list, &free_list);
5272
5273 /* piggy back on hardware_lock for protection */
5274 vha->hw->tgt.num_qfull_cmds_alloc--;
5275 }
Quinn Tran82de8022017-06-13 20:47:17 -07005276 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005277
5278 cmd = NULL;
5279
5280 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5281 list_del(&cmd->cmd_list);
5282 /* This cmd was never sent to TCM. There is no need
5283 * to schedule free or call free_cmd
5284 */
5285 qlt_free_cmd(cmd);
5286 }
Quinn Tran82de8022017-06-13 20:47:17 -07005287
5288 if (!list_empty(&q_full_list)) {
5289 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5290 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5291 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5292 }
5293
Quinn Tran33e79972014-09-25 06:14:55 -04005294 return rc;
5295}
5296
5297static void
Quinn Tran82de8022017-06-13 20:47:17 -07005298qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5299 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005300{
5301 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005302 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005303
Quinn Tran82de8022017-06-13 20:47:17 -07005304 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005305 if (rc == -ENOMEM)
5306 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5307}
5308
5309static int
Quinn Tran82de8022017-06-13 20:47:17 -07005310qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5311 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005312{
5313 struct qla_hw_data *ha = vha->hw;
5314 uint16_t status;
Quinn Tran8b666802017-03-15 09:48:45 -07005315 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005316
5317 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5318 return 0;
5319
Quinn Tran8b666802017-03-15 09:48:45 -07005320 if (!ha_locked)
5321 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005322 status = temp_sam_status;
Quinn Tran82de8022017-06-13 20:47:17 -07005323 qlt_send_busy(qpair, atio, status);
Quinn Tran8b666802017-03-15 09:48:45 -07005324 if (!ha_locked)
5325 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5326
Quinn Tran33e79972014-09-25 06:14:55 -04005327 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005328}
5329
5330/* ha->hardware_lock supposed to be held on entry */
5331/* called via callback from qla2xxx */
5332static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005333 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005334{
5335 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005336 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005337 int rc;
Quinn Tran2f424b92015-12-17 14:57:07 -05005338 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005339
5340 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005341 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005342 "ATIO pkt, but no tgt (ha %p)", ha);
5343 return;
5344 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005345 /*
5346 * In tgt_stop mode we also should allow all requests to pass.
5347 * Otherwise, some commands can stuck.
5348 */
5349
Quinn Tran2f424b92015-12-17 14:57:07 -05005350 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005351
5352 switch (atio->u.raw.entry_type) {
5353 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005354 if (unlikely(atio->u.isp24.exchange_addr ==
5355 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005356 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005357 "qla_target(%d): ATIO_TYPE7 "
5358 "received with UNKNOWN exchange address, "
5359 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005360 if (!ha_locked)
5361 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07005362 qlt_send_busy(ha->base_qpair, atio,
5363 SAM_STAT_TASK_SET_FULL);
Quinn Tran2f424b92015-12-17 14:57:07 -05005364 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005365 spin_unlock_irqrestore(&ha->hardware_lock,
5366 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005367 break;
5368 }
Quinn Tran33e79972014-09-25 06:14:55 -04005369
Quinn Tran33e79972014-09-25 06:14:55 -04005370 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005371 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5372 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005373 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005374 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005375 return;
5376 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005377 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005378 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005379 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005380 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005381 if (unlikely(rc != 0)) {
5382 if (rc == -ESRCH) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005383 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005384 spin_lock_irqsave(&ha->hardware_lock,
5385 flags);
Quinn Tran2f424b92015-12-17 14:57:07 -05005386
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005387#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
Quinn Tran82de8022017-06-13 20:47:17 -07005388 qlt_send_busy(ha->base_qpair, atio,
5389 SAM_STAT_BUSY);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005390#else
Quinn Tran82de8022017-06-13 20:47:17 -07005391 qlt_send_term_exchange(ha->base_qpair, NULL,
5392 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005393#endif
Quinn Tran2f424b92015-12-17 14:57:07 -05005394 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005395 spin_unlock_irqrestore(
5396 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005397 } else {
5398 if (tgt->tgt_stop) {
5399 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5400 "qla_target: Unable to send "
5401 "command to target for req, "
5402 "ignoring.\n");
5403 } else {
5404 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5405 "qla_target(%d): Unable to send "
5406 "command to target, sending BUSY "
5407 "status.\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005408 if (!ha_locked)
5409 spin_lock_irqsave(
5410 &ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07005411 qlt_send_busy(ha->base_qpair,
5412 atio, SAM_STAT_BUSY);
Quinn Tran2f424b92015-12-17 14:57:07 -05005413 if (!ha_locked)
5414 spin_unlock_irqrestore(
5415 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005416 }
5417 }
5418 }
5419 break;
5420
5421 case IMMED_NOTIFY_TYPE:
5422 {
5423 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5424 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5425 "qla_target(%d): Received ATIO packet %x "
5426 "with error status %x\n", vha->vp_idx,
5427 atio->u.raw.entry_type,
5428 atio->u.isp2x.entry_status);
5429 break;
5430 }
5431 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005432
5433 if (!ha_locked)
5434 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005435 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005436 if (!ha_locked)
5437 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005438 break;
5439 }
5440
5441 default:
5442 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5443 "qla_target(%d): Received unknown ATIO atio "
5444 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5445 break;
5446 }
5447
Quinn Tran2f424b92015-12-17 14:57:07 -05005448 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005449}
5450
5451/* ha->hardware_lock supposed to be held on entry */
5452/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005453static void qlt_response_pkt(struct scsi_qla_host *vha,
5454 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005455{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005456 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005457
5458 if (unlikely(tgt == NULL)) {
5459 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005460 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5461 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005462 return;
5463 }
5464
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005465 /*
5466 * In tgt_stop mode we also should allow all requests to pass.
5467 * Otherwise, some commands can stuck.
5468 */
5469
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005470 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005471 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005472 case CTIO_TYPE7:
5473 {
5474 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005475 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005476 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5477 entry);
5478 break;
5479 }
5480
5481 case ACCEPT_TGT_IO_TYPE:
5482 {
5483 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5484 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005485 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005486 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005487 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5488 "qla_target(%d): ATIO with error "
5489 "status %x received\n", vha->vp_idx,
5490 le16_to_cpu(atio->u.isp2x.status));
5491 break;
5492 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005493
Quinn Tran82de8022017-06-13 20:47:17 -07005494 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005495 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005496 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005497
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005498 rc = qlt_handle_cmd_for_atio(vha, atio);
5499 if (unlikely(rc != 0)) {
5500 if (rc == -ESRCH) {
5501#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
Quinn Tran82de8022017-06-13 20:47:17 -07005502 qlt_send_busy(rsp->qpair, atio, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005503#else
Quinn Tran82de8022017-06-13 20:47:17 -07005504 qlt_send_term_exchange(rsp->qpair, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005505#endif
5506 } else {
5507 if (tgt->tgt_stop) {
5508 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5509 "qla_target: Unable to send "
5510 "command to target, sending TERM "
5511 "EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005512 qlt_send_term_exchange(rsp->qpair, NULL,
Quinn Trana07100e2015-12-07 19:48:57 -05005513 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005514 } else {
5515 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5516 "qla_target(%d): Unable to send "
5517 "command to target, sending BUSY "
5518 "status\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07005519 qlt_send_busy(rsp->qpair, atio, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005520 }
5521 }
5522 }
5523 }
5524 break;
5525
5526 case CONTINUE_TGT_IO_TYPE:
5527 {
5528 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005529 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005530 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5531 entry);
5532 break;
5533 }
5534
5535 case CTIO_A64_TYPE:
5536 {
5537 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005538 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005539 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5540 entry);
5541 break;
5542 }
5543
5544 case IMMED_NOTIFY_TYPE:
5545 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5546 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5547 break;
5548
5549 case NOTIFY_ACK_TYPE:
5550 if (tgt->notify_ack_expected > 0) {
5551 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5552 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5553 "NOTIFY_ACK seq %08x status %x\n",
5554 le16_to_cpu(entry->u.isp2x.seq_id),
5555 le16_to_cpu(entry->u.isp2x.status));
5556 tgt->notify_ack_expected--;
5557 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005558 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005559 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5560 "qla_target(%d): NOTIFY_ACK "
5561 "failed %x\n", vha->vp_idx,
5562 le16_to_cpu(entry->u.isp2x.status));
5563 }
5564 } else {
5565 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5566 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5567 vha->vp_idx);
5568 }
5569 break;
5570
5571 case ABTS_RECV_24XX:
5572 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5573 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5574 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5575 break;
5576
5577 case ABTS_RESP_24XX:
5578 if (tgt->abts_resp_expected > 0) {
5579 struct abts_resp_from_24xx_fw *entry =
5580 (struct abts_resp_from_24xx_fw *)pkt;
5581 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5582 "ABTS_RESP_24XX: compl_status %x\n",
5583 entry->compl_status);
5584 tgt->abts_resp_expected--;
5585 if (le16_to_cpu(entry->compl_status) !=
5586 ABTS_RESP_COMPL_SUCCESS) {
5587 if ((entry->error_subcode1 == 0x1E) &&
5588 (entry->error_subcode2 == 0)) {
5589 /*
5590 * We've got a race here: aborted
5591 * exchange not terminated, i.e.
5592 * response for the aborted command was
5593 * sent between the abort request was
5594 * received and processed.
5595 * Unfortunately, the firmware has a
5596 * silly requirement that all aborted
5597 * exchanges must be explicitely
5598 * terminated, otherwise it refuses to
5599 * send responses for the abort
5600 * requests. So, we have to
5601 * (re)terminate the exchange and retry
5602 * the abort response.
5603 */
5604 qlt_24xx_retry_term_exchange(vha,
5605 entry);
5606 } else
5607 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5608 "qla_target(%d): ABTS_RESP_24XX "
5609 "failed %x (subcode %x:%x)",
5610 vha->vp_idx, entry->compl_status,
5611 entry->error_subcode1,
5612 entry->error_subcode2);
5613 }
5614 } else {
5615 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5616 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5617 "received\n", vha->vp_idx);
5618 }
5619 break;
5620
5621 default:
5622 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5623 "qla_target(%d): Received unknown response pkt "
5624 "type %x\n", vha->vp_idx, pkt->entry_type);
5625 break;
5626 }
5627
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005628}
5629
5630/*
5631 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5632 */
5633void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5634 uint16_t *mailbox)
5635{
5636 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005637 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005638 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005639
Quinn Tran3a33dc92017-06-02 09:12:04 -07005640 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005641 return;
5642
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005643 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5644 IS_QLA2100(ha))
5645 return;
5646 /*
5647 * In tgt_stop mode we also should allow all requests to pass.
5648 * Otherwise, some commands can stuck.
5649 */
5650
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005651
5652 switch (code) {
5653 case MBA_RESET: /* Reset */
5654 case MBA_SYSTEM_ERR: /* System Error */
5655 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5656 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5657 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5658 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005659 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005660 break;
5661 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5662 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5663 break;
5664
5665 case MBA_LOOP_UP:
5666 {
5667 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005668 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005669 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5670 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5671 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005672 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005673 qlt_send_notify_ack(ha->base_qpair,
5674 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005675 0, 0, 0, 0, 0, 0);
5676 tgt->link_reinit_iocb_pending = 0;
5677 }
5678 break;
5679 }
5680
5681 case MBA_LIP_OCCURRED:
5682 case MBA_LOOP_DOWN:
5683 case MBA_LIP_RESET:
5684 case MBA_RSCN_UPDATE:
5685 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005686 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005687 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5688 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5689 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005690 break;
5691
Quinn Tranead03852017-01-19 22:28:01 -08005692 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005693 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5694 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5695 vha->vp_idx,
5696 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5697 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005698
5699 if (le16_to_cpu(mailbox[3]) == 1) {
5700 /* exchange starvation. */
5701 vha->hw->exch_starvation++;
5702 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005703 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08005704 "Exchange starvation-. Resetting RISC\n");
5705
5706 vha->hw->exch_starvation = 0;
5707 if (IS_P3P_TYPE(vha->hw))
5708 set_bit(FCOE_CTX_RESET_NEEDED,
5709 &vha->dpc_flags);
5710 else
5711 set_bit(ISP_ABORT_NEEDED,
5712 &vha->dpc_flags);
5713 qla2xxx_wake_dpc(vha);
5714 }
5715 }
5716 break;
5717
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005718 case MBA_PORT_UPDATE:
5719 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5720 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005721 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005722 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5723 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5724 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5725
5726 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08005727 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005728 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5729 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08005730 vha->hw->exch_starvation = 0;
5731 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005732 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5733 "Async MB 2: Port Logged Out\n");
5734 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005735 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005736 break;
5737 }
5738
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005739}
5740
5741static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5742 uint16_t loop_id)
5743{
Quinn Tran726b8542017-01-19 22:28:00 -08005744 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005745 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08005746 unsigned long flags;
5747 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005748
5749 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5750 if (!fcport) {
5751 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5752 "qla_target(%d): Allocation of tmp FC port failed",
5753 vha->vp_idx);
5754 return NULL;
5755 }
5756
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005757 fcport->loop_id = loop_id;
5758
Quinn Tran15f30a52017-03-15 09:48:52 -07005759 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005760 if (rc != QLA_SUCCESS) {
5761 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5762 "qla_target(%d): Failed to retrieve fcport "
5763 "information -- get_port_database() returned %x "
5764 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5765 kfree(fcport);
5766 return NULL;
5767 }
5768
Quinn Tran726b8542017-01-19 22:28:00 -08005769 del = NULL;
5770 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5771 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5772
5773 if (tfcp) {
5774 tfcp->d_id = fcport->d_id;
5775 tfcp->port_type = fcport->port_type;
5776 tfcp->supported_classes = fcport->supported_classes;
5777 tfcp->flags |= fcport->flags;
5778
5779 del = fcport;
5780 fcport = tfcp;
5781 } else {
5782 if (vha->hw->current_topology == ISP_CFG_F)
5783 fcport->flags |= FCF_FABRIC_DEVICE;
5784
5785 list_add_tail(&fcport->list, &vha->vp_fcports);
5786 if (!IS_SW_RESV_ADDR(fcport->d_id))
5787 vha->fcport_count++;
5788 fcport->login_gen++;
5789 fcport->disc_state = DSC_LOGIN_COMPLETE;
5790 fcport->login_succ = 1;
5791 newfcport = 1;
5792 }
5793
5794 fcport->deleted = 0;
5795 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5796
5797 switch (vha->host->active_mode) {
5798 case MODE_INITIATOR:
5799 case MODE_DUAL:
5800 if (newfcport) {
5801 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005802 ql_dbg(ql_dbg_disc, vha, 0x20fe,
Quinn Tran726b8542017-01-19 22:28:00 -08005803 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5804 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5805 qla24xx_post_upd_fcport_work(vha, fcport);
5806 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07005807 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08005808 "%s %d %8phC post gpsc fcp_cnt %d\n",
5809 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5810 qla24xx_post_gpsc_work(vha, fcport);
5811 }
5812 }
5813 break;
5814
5815 case MODE_TARGET:
5816 default:
5817 break;
5818 }
5819 if (del)
5820 qla2x00_free_fcport(del);
5821
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005822 return fcport;
5823}
5824
5825/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08005826static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005827 uint8_t *s_id)
5828{
Quinn Tran5d964832017-01-19 22:27:59 -08005829 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005830 fc_port_t *fcport = NULL;
5831 int rc, global_resets;
5832 uint16_t loop_id = 0;
5833
Quinn Tran726b8542017-01-19 22:28:00 -08005834 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5835 /*
5836 * This is Domain Controller, so it should be
5837 * OK to drop SCSI commands from it.
5838 */
5839 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5840 "Unable to find initiator with S_ID %x:%x:%x",
5841 s_id[0], s_id[1], s_id[2]);
5842 return NULL;
5843 }
5844
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005845 mutex_lock(&vha->vha_tgt.tgt_mutex);
5846
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005847retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005848 global_resets =
5849 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005850
5851 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5852 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005853 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5854
Quinn Tran726b8542017-01-19 22:28:00 -08005855 ql_log(ql_log_info, vha, 0xf071,
5856 "qla_target(%d): Unable to find "
5857 "initiator with S_ID %x:%x:%x",
5858 vha->vp_idx, s_id[0], s_id[1],
5859 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005860
5861 if (rc == -ENOENT) {
5862 qlt_port_logo_t logo;
5863 sid_to_portid(s_id, &logo.id);
5864 logo.cmd_count = 1;
5865 qlt_send_first_logo(vha, &logo);
5866 }
5867
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005868 return NULL;
5869 }
5870
5871 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005872 if (!fcport) {
5873 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005874 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005875 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005876
5877 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005878 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005879 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5880 "qla_target(%d): global reset during session discovery "
5881 "(counter was %d, new %d), retrying", vha->vp_idx,
5882 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005883 atomic_read(&vha->vha_tgt.
5884 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005885 goto retry;
5886 }
5887
5888 sess = qlt_create_sess(vha, fcport, true);
5889
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005890 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5891
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005892 return sess;
5893}
5894
5895static void qlt_abort_work(struct qla_tgt *tgt,
5896 struct qla_tgt_sess_work_param *prm)
5897{
5898 struct scsi_qla_host *vha = tgt->vha;
5899 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005900 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005901 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005902 uint32_t be_s_id;
5903 uint8_t s_id[3];
5904 int rc;
5905
Quinn Tran75601512015-12-17 14:57:04 -05005906 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005907
5908 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005909 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005910
5911 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5912 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5913 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5914
5915 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5916 (unsigned char *)&be_s_id);
5917 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005918 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005919
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005920 sess = qlt_make_local_sess(vha, s_id);
5921 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005922
Quinn Tran75601512015-12-17 14:57:04 -05005923 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005924 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005925 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005926 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005927 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005928 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005929 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005930 }
5931
Quinn Tran726b8542017-01-19 22:28:00 -08005932 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005933 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08005934 "%s: kref_get fail %8phC \n",
5935 __func__, sess->port_name);
5936 sess = NULL;
5937 goto out_term2;
5938 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005939 }
5940
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005941 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005942 ha->tgt.tgt_ops->put_sess(sess);
5943 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5944
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005945 if (rc != 0)
5946 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005947 return;
5948
Quinn Tran75601512015-12-17 14:57:04 -05005949out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08005950 if (sess)
5951 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005952 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005953
5954out_term:
5955 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07005956 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
5957 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005958 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005959}
5960
5961static void qlt_tmr_work(struct qla_tgt *tgt,
5962 struct qla_tgt_sess_work_param *prm)
5963{
5964 struct atio_from_isp *a = &prm->tm_iocb2;
5965 struct scsi_qla_host *vha = tgt->vha;
5966 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005967 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005968 unsigned long flags;
5969 uint8_t *s_id = NULL; /* to hide compiler warnings */
5970 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07005971 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07005972 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005973 void *iocb;
5974
Quinn Tran75601512015-12-17 14:57:04 -05005975 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005976
5977 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005978 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005979
5980 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5981 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5982 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005983 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005984
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005985 sess = qlt_make_local_sess(vha, s_id);
5986 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005987
Quinn Tran75601512015-12-17 14:57:04 -05005988 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005989 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005990 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005991 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005992 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005993 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07005994 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005995 }
5996
Quinn Tran726b8542017-01-19 22:28:00 -08005997 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005998 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08005999 "%s: kref_get fail %8phC\n",
6000 __func__, sess->port_name);
6001 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006002 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006003 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006004 }
6005
6006 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006007 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006008 unpacked_lun =
6009 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006010
6011 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006012 ha->tgt.tgt_ops->put_sess(sess);
6013 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6014
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006015 if (rc != 0)
6016 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006017 return;
6018
Quinn Tranf159b3c2017-03-15 09:48:47 -07006019out_term2:
6020 if (sess)
6021 ha->tgt.tgt_ops->put_sess(sess);
6022 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006023out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006024 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006025}
6026
6027static void qlt_sess_work_fn(struct work_struct *work)
6028{
6029 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6030 struct scsi_qla_host *vha = tgt->vha;
6031 unsigned long flags;
6032
6033 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6034
6035 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6036 while (!list_empty(&tgt->sess_works_list)) {
6037 struct qla_tgt_sess_work_param *prm = list_entry(
6038 tgt->sess_works_list.next, typeof(*prm),
6039 sess_works_list_entry);
6040
6041 /*
6042 * This work can be scheduled on several CPUs at time, so we
6043 * must delete the entry to eliminate double processing
6044 */
6045 list_del(&prm->sess_works_list_entry);
6046
6047 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6048
6049 switch (prm->type) {
6050 case QLA_TGT_SESS_WORK_ABORT:
6051 qlt_abort_work(tgt, prm);
6052 break;
6053 case QLA_TGT_SESS_WORK_TM:
6054 qlt_tmr_work(tgt, prm);
6055 break;
6056 default:
6057 BUG_ON(1);
6058 break;
6059 }
6060
6061 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6062
6063 kfree(prm);
6064 }
6065 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6066}
6067
6068/* Must be called under tgt_host_action_mutex */
6069int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6070{
6071 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006072 int rc, i;
6073 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006074
6075 if (!QLA_TGT_MODE_ENABLED())
6076 return 0;
6077
Arun Easi33c36c02013-01-30 03:34:41 -05006078 if (!IS_TGT_MODE_CAPABLE(ha)) {
6079 ql_log(ql_log_warn, base_vha, 0xe070,
6080 "This adapter does not support target mode.\n");
6081 return 0;
6082 }
6083
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006084 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006085 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006086
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006087 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006088
6089 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6090 if (!tgt) {
6091 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6092 "Unable to allocate struct qla_tgt\n");
6093 return -ENOMEM;
6094 }
6095
Quinn Trane326d222017-06-13 20:47:18 -07006096 tgt->qphints = kzalloc((ha->max_qpairs + 1) *
6097 sizeof(struct qla_qpair_hint), GFP_KERNEL);
6098 if (!tgt->qphints) {
6099 kfree(tgt);
6100 ql_log(ql_log_warn, base_vha, 0x0197,
6101 "Unable to allocate qpair hints.\n");
6102 return -ENOMEM;
6103 }
6104
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006105 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6106 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6107
Quinn Trane326d222017-06-13 20:47:18 -07006108 rc = btree_init64(&tgt->lun_qpair_map);
6109 if (rc) {
6110 kfree(tgt->qphints);
6111 kfree(tgt);
6112 ql_log(ql_log_info, base_vha, 0x0198,
6113 "Unable to initialize lun_qpair_map btree\n");
6114 return -EIO;
6115 }
6116 h = &tgt->qphints[0];
6117 h->qpair = ha->base_qpair;
6118 INIT_LIST_HEAD(&h->hint_elem);
6119 h->cpuid = ha->base_qpair->cpuid;
6120 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6121
6122 for (i = 0; i < ha->max_qpairs; i++) {
6123 unsigned long flags;
6124
6125 struct qla_qpair *qpair = ha->queue_pair_map[i];
6126 h = &tgt->qphints[i + 1];
6127 INIT_LIST_HEAD(&h->hint_elem);
6128 if (qpair) {
6129 h->qpair = qpair;
6130 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6131 list_add_tail(&h->hint_elem, &qpair->hints_list);
6132 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6133 h->cpuid = qpair->cpuid;
6134 }
6135 }
6136
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006137 tgt->ha = ha;
6138 tgt->vha = base_vha;
6139 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006140 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006141 spin_lock_init(&tgt->sess_work_lock);
6142 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6143 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006144 atomic_set(&tgt->tgt_global_resets_count, 0);
6145
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006146 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006147
6148 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6149 "qla_target(%d): using 64 Bit PCI addressing",
6150 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006151 /* 3 is reserved */
6152 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006153
6154 mutex_lock(&qla_tgt_mutex);
6155 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6156 mutex_unlock(&qla_tgt_mutex);
6157
Quinn Tranf1443ee2017-03-15 09:48:51 -07006158 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6159 ha->tgt.tgt_ops->add_target(base_vha);
6160
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006161 return 0;
6162}
6163
6164/* Must be called under tgt_host_action_mutex */
6165int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6166{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006167 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006168 return 0;
6169
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006170 if (vha->fc_vport) {
6171 qlt_release(vha->vha_tgt.qla_tgt);
6172 return 0;
6173 }
Quinn Tran33e79972014-09-25 06:14:55 -04006174
6175 /* free left over qfull cmds */
6176 qlt_init_term_exchange(vha);
6177
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006178 mutex_lock(&qla_tgt_mutex);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006179 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006180 mutex_unlock(&qla_tgt_mutex);
6181
6182 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6183 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006184 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006185
6186 return 0;
6187}
6188
Quinn Tran482c9dc2017-03-15 09:48:54 -07006189void qlt_remove_target_resources(struct qla_hw_data *ha)
6190{
6191 struct scsi_qla_host *node;
6192 u32 key = 0;
6193
6194 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6195 btree_remove32(&ha->tgt.host_map, key);
6196
6197 btree_destroy32(&ha->tgt.host_map);
6198}
6199
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006200static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6201 unsigned char *b)
6202{
6203 int i;
6204
6205 pr_debug("qla2xxx HW vha->node_name: ");
6206 for (i = 0; i < WWN_SIZE; i++)
6207 pr_debug("%02x ", vha->node_name[i]);
6208 pr_debug("\n");
6209 pr_debug("qla2xxx HW vha->port_name: ");
6210 for (i = 0; i < WWN_SIZE; i++)
6211 pr_debug("%02x ", vha->port_name[i]);
6212 pr_debug("\n");
6213
6214 pr_debug("qla2xxx passed configfs WWPN: ");
6215 put_unaligned_be64(wwpn, b);
6216 for (i = 0; i < WWN_SIZE; i++)
6217 pr_debug("%02x ", b[i]);
6218 pr_debug("\n");
6219}
6220
6221/**
6222 * qla_tgt_lport_register - register lport with external module
6223 *
6224 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6225 * @wwpn: Passwd FC target WWPN
6226 * @callback: lport initialization callback for tcm_qla2xxx code
6227 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6228 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006229int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6230 u64 npiv_wwpn, u64 npiv_wwnn,
6231 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006232{
6233 struct qla_tgt *tgt;
6234 struct scsi_qla_host *vha;
6235 struct qla_hw_data *ha;
6236 struct Scsi_Host *host;
6237 unsigned long flags;
6238 int rc;
6239 u8 b[WWN_SIZE];
6240
6241 mutex_lock(&qla_tgt_mutex);
6242 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6243 vha = tgt->vha;
6244 ha = vha->hw;
6245
6246 host = vha->host;
6247 if (!host)
6248 continue;
6249
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006250 if (!(host->hostt->supported_mode & MODE_TARGET))
6251 continue;
6252
6253 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006254 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006255 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6256 host->host_no);
6257 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6258 continue;
6259 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006260 if (tgt->tgt_stop) {
6261 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6262 host->host_no);
6263 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6264 continue;
6265 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006266 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6267
6268 if (!scsi_host_get(host)) {
6269 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6270 "Unable to scsi_host_get() for"
6271 " qla2xxx scsi_host\n");
6272 continue;
6273 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006274 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006275
6276 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6277 scsi_host_put(host);
6278 continue;
6279 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006280 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6281 if (rc != 0)
6282 scsi_host_put(host);
6283
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006284 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006285 return rc;
6286 }
6287 mutex_unlock(&qla_tgt_mutex);
6288
6289 return -ENODEV;
6290}
6291EXPORT_SYMBOL(qlt_lport_register);
6292
6293/**
6294 * qla_tgt_lport_deregister - Degister lport
6295 *
6296 * @vha: Registered scsi_qla_host pointer
6297 */
6298void qlt_lport_deregister(struct scsi_qla_host *vha)
6299{
6300 struct qla_hw_data *ha = vha->hw;
6301 struct Scsi_Host *sh = vha->host;
6302 /*
6303 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6304 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006305 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006306 ha->tgt.tgt_ops = NULL;
6307 /*
6308 * Release the Scsi_Host reference for the underlying qla2xxx host
6309 */
6310 scsi_host_put(sh);
6311}
6312EXPORT_SYMBOL(qlt_lport_deregister);
6313
6314/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006315static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006316{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006317 switch (ql2x_ini_mode) {
6318 case QLA2XXX_INI_MODE_DISABLED:
6319 case QLA2XXX_INI_MODE_EXCLUSIVE:
6320 vha->host->active_mode = MODE_TARGET;
6321 break;
6322 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006323 vha->host->active_mode = MODE_UNKNOWN;
6324 break;
6325 case QLA2XXX_INI_MODE_DUAL:
6326 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006327 break;
6328 default:
6329 break;
6330 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006331}
6332
6333/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006334static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006335{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006336 switch (ql2x_ini_mode) {
6337 case QLA2XXX_INI_MODE_DISABLED:
6338 vha->host->active_mode = MODE_UNKNOWN;
6339 break;
6340 case QLA2XXX_INI_MODE_EXCLUSIVE:
6341 vha->host->active_mode = MODE_INITIATOR;
6342 break;
6343 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006344 case QLA2XXX_INI_MODE_DUAL:
6345 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006346 break;
6347 default:
6348 break;
6349 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006350}
6351
6352/*
6353 * qla_tgt_enable_vha - NO LOCK HELD
6354 *
6355 * host_reset, bring up w/ Target Mode Enabled
6356 */
6357void
6358qlt_enable_vha(struct scsi_qla_host *vha)
6359{
6360 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006361 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006362 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006363 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006364
6365 if (!tgt) {
6366 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6367 "Unable to locate qla_tgt pointer from"
6368 " struct qla_hw_data\n");
6369 dump_stack();
6370 return;
6371 }
6372
6373 spin_lock_irqsave(&ha->hardware_lock, flags);
6374 tgt->tgt_stopped = 0;
6375 qlt_set_mode(vha);
6376 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6377
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006378 if (vha->vp_idx) {
6379 qla24xx_disable_vp(vha);
6380 qla24xx_enable_vp(vha);
6381 } else {
6382 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6383 qla2xxx_wake_dpc(base_vha);
6384 qla2x00_wait_for_hba_online(base_vha);
6385 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006386}
6387EXPORT_SYMBOL(qlt_enable_vha);
6388
6389/*
6390 * qla_tgt_disable_vha - NO LOCK HELD
6391 *
6392 * Disable Target Mode and reset the adapter
6393 */
Joern Engel55a90662014-09-16 16:23:15 -04006394static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006395{
6396 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006397 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006398 unsigned long flags;
6399
6400 if (!tgt) {
6401 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6402 "Unable to locate qla_tgt pointer from"
6403 " struct qla_hw_data\n");
6404 dump_stack();
6405 return;
6406 }
6407
6408 spin_lock_irqsave(&ha->hardware_lock, flags);
6409 qlt_clear_mode(vha);
6410 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6411
6412 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6413 qla2xxx_wake_dpc(vha);
6414 qla2x00_wait_for_hba_online(vha);
6415}
6416
6417/*
6418 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6419 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6420 * members.
6421 */
6422void
6423qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6424{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006425 vha->vha_tgt.qla_tgt = NULL;
6426
6427 mutex_init(&vha->vha_tgt.tgt_mutex);
6428 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006429
6430 qlt_clear_mode(vha);
6431
6432 /*
6433 * NOTE: Currently the value is kept the same for <24xx and
6434 * >=24xx ISPs. If it is necessary to change it,
6435 * the check should be added for specific ISPs,
6436 * assigning the value appropriately.
6437 */
6438 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006439
6440 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006441}
6442
6443void
6444qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6445{
6446 /*
6447 * FC-4 Feature bit 0 indicates target functionality to the name server.
6448 */
6449 if (qla_tgt_mode_enabled(vha)) {
Quinn Tran726b8542017-01-19 22:28:00 -08006450 ct_req->req.rff_id.fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006451 } else if (qla_ini_mode_enabled(vha)) {
6452 ct_req->req.rff_id.fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006453 } else if (qla_dual_mode_enabled(vha))
6454 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006455}
6456
6457/*
6458 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6459 * @ha: HA context
6460 *
6461 * Beginning of ATIO ring has initialization control block already built
6462 * by nvram config routine.
6463 *
6464 * Returns 0 on success.
6465 */
6466void
6467qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6468{
6469 struct qla_hw_data *ha = vha->hw;
6470 uint16_t cnt;
6471 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6472
Quinn Tranead03852017-01-19 22:28:01 -08006473 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006474 return;
6475
6476 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6477 pkt->u.raw.signature = ATIO_PROCESSED;
6478 pkt++;
6479 }
6480
6481}
6482
6483/*
6484 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6485 * @ha: SCSI driver HA context
6486 */
6487void
Quinn Tran2f424b92015-12-17 14:57:07 -05006488qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006489{
6490 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006491 struct atio_from_isp *pkt;
6492 int cnt, i;
6493
Quinn Tranec7193e2017-03-15 09:48:55 -07006494 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006495 return;
6496
Quinn Tran5f355092016-12-23 18:06:11 -08006497 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6498 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006499 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6500 cnt = pkt->u.raw.entry_count;
6501
Quinn Tran5f355092016-12-23 18:06:11 -08006502 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6503 /*
6504 * This packet is corrupted. The header + payload
6505 * can not be trusted. There is no point in passing
6506 * it further up.
6507 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006508 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006509 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6510 pkt->u.isp24.fcp_hdr.s_id,
6511 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6512 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6513
6514 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006515 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6516 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006517 } else {
6518 qlt_24xx_atio_pkt_all_vps(vha,
6519 (struct atio_from_isp *)pkt, ha_locked);
6520 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006521
6522 for (i = 0; i < cnt; i++) {
6523 ha->tgt.atio_ring_index++;
6524 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6525 ha->tgt.atio_ring_index = 0;
6526 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6527 } else
6528 ha->tgt.atio_ring_ptr++;
6529
6530 pkt->u.raw.signature = ATIO_PROCESSED;
6531 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6532 }
6533 wmb();
6534 }
6535
6536 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006537 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006538}
6539
6540void
Arun Easiaa230bc2013-01-30 03:34:39 -05006541qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006542{
6543 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05006544 if (!QLA_TGT_MODE_ENABLED())
6545 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006546
Arun Easiaa230bc2013-01-30 03:34:39 -05006547 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6548 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6549 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6550
6551 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6552 struct qla_msix_entry *msix = &ha->msix_entries[2];
6553 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6554
6555 icb->msix_atio = cpu_to_le16(msix->entry);
6556 ql_dbg(ql_dbg_init, vha, 0xf072,
6557 "Registering ICB vector 0x%x for atio que.\n",
6558 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006559 }
6560}
6561
6562void
6563qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6564{
6565 struct qla_hw_data *ha = vha->hw;
Quinn Tran99e1b682017-06-02 09:12:03 -07006566
6567 if (!QLA_TGT_MODE_ENABLED())
6568 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006569
Quinn Tranead03852017-01-19 22:28:01 -08006570 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006571 if (!ha->tgt.saved_set) {
6572 /* We save only once */
6573 ha->tgt.saved_exchange_count = nv->exchange_count;
6574 ha->tgt.saved_firmware_options_1 =
6575 nv->firmware_options_1;
6576 ha->tgt.saved_firmware_options_2 =
6577 nv->firmware_options_2;
6578 ha->tgt.saved_firmware_options_3 =
6579 nv->firmware_options_3;
6580 ha->tgt.saved_set = 1;
6581 }
6582
Quinn Tran99e1b682017-06-02 09:12:03 -07006583 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006584 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006585 else /* dual */
6586 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006587
6588 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006589 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006590
6591 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006592 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006593 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006594
6595 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006596 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006597 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006598 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006599 if (ql2xtgt_tape_enable)
6600 /* Enable FC Tape support */
6601 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6602 else
6603 /* Disable FC Tape support */
6604 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6605
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006606 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006607 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006608
6609 /*
6610 * clear BIT 15 explicitly as we have seen at least
6611 * a couple of instances where this was set and this
6612 * was causing the firmware to not be initialized.
6613 */
6614 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006615 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006616 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006617 } else {
6618 if (ha->tgt.saved_set) {
6619 nv->exchange_count = ha->tgt.saved_exchange_count;
6620 nv->firmware_options_1 =
6621 ha->tgt.saved_firmware_options_1;
6622 nv->firmware_options_2 =
6623 ha->tgt.saved_firmware_options_2;
6624 nv->firmware_options_3 =
6625 ha->tgt.saved_firmware_options_3;
6626 }
6627 return;
6628 }
6629
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006630 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006631 if (vha->flags.init_done)
6632 fc_host_supported_classes(vha->host) =
6633 FC_COS_CLASS2 | FC_COS_CLASS3;
6634
Bart Van Asschead950362015-07-09 07:24:08 -07006635 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006636 } else {
6637 if (vha->flags.init_done)
6638 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6639
Bart Van Asschead950362015-07-09 07:24:08 -07006640 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006641 }
6642}
6643
6644void
6645qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6646 struct init_cb_24xx *icb)
6647{
6648 struct qla_hw_data *ha = vha->hw;
6649
Quinn Tran481ce732015-12-17 14:57:08 -05006650 if (!QLA_TGT_MODE_ENABLED())
6651 return;
6652
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006653 if (ha->tgt.node_name_set) {
6654 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006655 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006656 }
Quinn Tran481ce732015-12-17 14:57:08 -05006657
6658 /* disable ZIO at start time. */
6659 if (!vha->flags.init_done) {
6660 uint32_t tmp;
6661 tmp = le32_to_cpu(icb->firmware_options_2);
6662 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6663 icb->firmware_options_2 = cpu_to_le32(tmp);
6664 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006665}
6666
Arun Easiaa230bc2013-01-30 03:34:39 -05006667void
6668qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6669{
6670 struct qla_hw_data *ha = vha->hw;
6671
6672 if (!QLA_TGT_MODE_ENABLED())
6673 return;
6674
Quinn Tranead03852017-01-19 22:28:01 -08006675 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006676 if (!ha->tgt.saved_set) {
6677 /* We save only once */
6678 ha->tgt.saved_exchange_count = nv->exchange_count;
6679 ha->tgt.saved_firmware_options_1 =
6680 nv->firmware_options_1;
6681 ha->tgt.saved_firmware_options_2 =
6682 nv->firmware_options_2;
6683 ha->tgt.saved_firmware_options_3 =
6684 nv->firmware_options_3;
6685 ha->tgt.saved_set = 1;
6686 }
6687
Quinn Tran99e1b682017-06-02 09:12:03 -07006688 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006689 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006690 else /* dual */
6691 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05006692
6693 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006694 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006695
6696 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006697 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006698 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006699 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006700 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006701 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006702 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006703 /*
6704 * clear BIT 15 explicitly as we have seen at
6705 * least a couple of instances where this was set
6706 * and this was causing the firmware to not be
6707 * initialized.
6708 */
6709 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006710 if (ql2xtgt_tape_enable)
6711 /* Enable FC tape support */
6712 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6713 else
6714 /* Disable FC tape support */
6715 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6716
Arun Easiaa230bc2013-01-30 03:34:39 -05006717 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006718 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006719 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006720 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006721 } else {
6722 if (ha->tgt.saved_set) {
6723 nv->exchange_count = ha->tgt.saved_exchange_count;
6724 nv->firmware_options_1 =
6725 ha->tgt.saved_firmware_options_1;
6726 nv->firmware_options_2 =
6727 ha->tgt.saved_firmware_options_2;
6728 nv->firmware_options_3 =
6729 ha->tgt.saved_firmware_options_3;
6730 }
6731 return;
6732 }
6733
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006734 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006735 if (vha->flags.init_done)
6736 fc_host_supported_classes(vha->host) =
6737 FC_COS_CLASS2 | FC_COS_CLASS3;
6738
Bart Van Asschead950362015-07-09 07:24:08 -07006739 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006740 } else {
6741 if (vha->flags.init_done)
6742 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6743
Bart Van Asschead950362015-07-09 07:24:08 -07006744 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006745 }
6746}
6747
6748void
6749qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6750 struct init_cb_81xx *icb)
6751{
6752 struct qla_hw_data *ha = vha->hw;
6753
6754 if (!QLA_TGT_MODE_ENABLED())
6755 return;
6756
6757 if (ha->tgt.node_name_set) {
6758 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006759 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006760 }
Quinn Tran481ce732015-12-17 14:57:08 -05006761
6762 /* disable ZIO at start time. */
6763 if (!vha->flags.init_done) {
6764 uint32_t tmp;
6765 tmp = le32_to_cpu(icb->firmware_options_2);
6766 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6767 icb->firmware_options_2 = cpu_to_le32(tmp);
6768 }
6769
Arun Easiaa230bc2013-01-30 03:34:39 -05006770}
6771
6772void
6773qlt_83xx_iospace_config(struct qla_hw_data *ha)
6774{
6775 if (!QLA_TGT_MODE_ENABLED())
6776 return;
6777
6778 ha->msix_count += 1; /* For ATIO Q */
6779}
6780
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006781
6782void
6783qlt_modify_vp_config(struct scsi_qla_host *vha,
6784 struct vp_config_entry_24xx *vpmod)
6785{
Quinn Tranead03852017-01-19 22:28:01 -08006786 /* enable target mode. Bit5 = 1 => disable */
6787 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006788 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08006789
Quinn Tranead03852017-01-19 22:28:01 -08006790 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08006791 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006792 vpmod->options_idx1 &= ~BIT_4;
6793}
6794
6795void
6796qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6797{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006798 int rc;
6799
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006800 if (!QLA_TGT_MODE_ENABLED())
6801 return;
6802
Michael Hernandezb7edfa22017-08-23 15:04:56 -07006803 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006804 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6805 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6806 } else {
6807 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6808 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6809 }
6810
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006811 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6812 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08006813
6814 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6815 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6816 qlt_unknown_atio_work_fn);
6817
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006818 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006819
6820 rc = btree_init32(&ha->tgt.host_map);
6821 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006822 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006823 "Unable to initialize ha->host_map btree\n");
6824
6825 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006826}
6827
Arun Easiaa230bc2013-01-30 03:34:39 -05006828irqreturn_t
6829qla83xx_msix_atio_q(int irq, void *dev_id)
6830{
6831 struct rsp_que *rsp;
6832 scsi_qla_host_t *vha;
6833 struct qla_hw_data *ha;
6834 unsigned long flags;
6835
6836 rsp = (struct rsp_que *) dev_id;
6837 ha = rsp->hw;
6838 vha = pci_get_drvdata(ha->pdev);
6839
Quinn Tran2f424b92015-12-17 14:57:07 -05006840 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006841
Quinn Tran2f424b92015-12-17 14:57:07 -05006842 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006843
Quinn Tran2f424b92015-12-17 14:57:07 -05006844 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006845
6846 return IRQ_HANDLED;
6847}
6848
Quinn Tran2f424b92015-12-17 14:57:07 -05006849static void
6850qlt_handle_abts_recv_work(struct work_struct *work)
6851{
6852 struct qla_tgt_sess_op *op = container_of(work,
6853 struct qla_tgt_sess_op, work);
6854 scsi_qla_host_t *vha = op->vha;
6855 struct qla_hw_data *ha = vha->hw;
6856 unsigned long flags;
6857
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006858 if (qla2x00_reset_active(vha) ||
6859 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05006860 return;
6861
6862 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6863 qlt_24xx_process_atio_queue(vha, 0);
6864 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6865
6866 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006867 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05006868 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07006869
6870 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05006871}
6872
6873void
Quinn Tran82de8022017-06-13 20:47:17 -07006874qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
6875 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05006876{
6877 struct qla_tgt_sess_op *op;
6878
6879 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6880
6881 if (!op) {
6882 /* do not reach for ATIO queue here. This is best effort err
6883 * recovery at this point.
6884 */
Quinn Tran82de8022017-06-13 20:47:17 -07006885 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05006886 return;
6887 }
6888
6889 memcpy(&op->atio, pkt, sizeof(*pkt));
6890 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006891 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07006892 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05006893 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6894 queue_work(qla_tgt_wq, &op->work);
6895 return;
6896}
6897
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006898int
6899qlt_mem_alloc(struct qla_hw_data *ha)
6900{
6901 if (!QLA_TGT_MODE_ENABLED())
6902 return 0;
6903
6904 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6905 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6906 if (!ha->tgt.tgt_vp_map)
6907 return -ENOMEM;
6908
6909 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6910 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6911 &ha->tgt.atio_dma, GFP_KERNEL);
6912 if (!ha->tgt.atio_ring) {
6913 kfree(ha->tgt.tgt_vp_map);
6914 return -ENOMEM;
6915 }
6916 return 0;
6917}
6918
6919void
6920qlt_mem_free(struct qla_hw_data *ha)
6921{
6922 if (!QLA_TGT_MODE_ENABLED())
6923 return;
6924
6925 if (ha->tgt.atio_ring) {
6926 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6927 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6928 ha->tgt.atio_dma);
6929 }
6930 kfree(ha->tgt.tgt_vp_map);
6931}
6932
6933/* vport_slock to be held by the caller */
6934void
6935qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6936{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006937 void *slot;
6938 u32 key;
6939 int rc;
6940
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006941 if (!QLA_TGT_MODE_ENABLED())
6942 return;
6943
Quinn Tran482c9dc2017-03-15 09:48:54 -07006944 key = vha->d_id.b24;
6945
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006946 switch (cmd) {
6947 case SET_VP_IDX:
6948 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6949 break;
6950 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07006951 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6952 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006953 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006954 "Save vha in host_map %p %06x\n", vha, key);
6955 rc = btree_insert32(&vha->hw->tgt.host_map,
6956 key, vha, GFP_ATOMIC);
6957 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006958 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006959 "Unable to insert s_id into host_map: %06x\n",
6960 key);
6961 return;
6962 }
Quinn Tran83548fe2017-06-02 09:12:01 -07006963 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6964 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006965 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006966 break;
6967 case RESET_VP_IDX:
6968 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6969 break;
6970 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07006971 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006972 "clear vha in host_map %p %06x\n", vha, key);
6973 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6974 if (slot)
6975 btree_remove32(&vha->hw->tgt.host_map, key);
6976 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006977 break;
6978 }
6979}
6980
Quinn Tran482c9dc2017-03-15 09:48:54 -07006981void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6982{
6983 unsigned long flags;
6984 struct qla_hw_data *ha = vha->hw;
6985
6986 if (!vha->d_id.b24) {
6987 spin_lock_irqsave(&ha->vport_slock, flags);
6988 vha->d_id = id;
6989 qlt_update_vp_map(vha, SET_AL_PA);
6990 spin_unlock_irqrestore(&ha->vport_slock, flags);
6991 } else if (vha->d_id.b24 != id.b24) {
6992 spin_lock_irqsave(&ha->vport_slock, flags);
6993 qlt_update_vp_map(vha, RESET_AL_PA);
6994 vha->d_id = id;
6995 qlt_update_vp_map(vha, SET_AL_PA);
6996 spin_unlock_irqrestore(&ha->vport_slock, flags);
6997 }
6998}
6999
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007000static int __init qlt_parse_ini_mode(void)
7001{
7002 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7003 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7004 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7005 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7006 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7007 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007008 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7009 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007010 else
7011 return false;
7012
7013 return true;
7014}
7015
7016int __init qlt_init(void)
7017{
7018 int ret;
7019
7020 if (!qlt_parse_ini_mode()) {
7021 ql_log(ql_log_fatal, NULL, 0xe06b,
7022 "qlt_parse_ini_mode() failed\n");
7023 return -EINVAL;
7024 }
7025
7026 if (!QLA_TGT_MODE_ENABLED())
7027 return 0;
7028
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007029 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7030 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7031 qla_tgt_mgmt_cmd), 0, NULL);
7032 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007033 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007034 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007035 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007036 }
7037
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007038 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007039 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7040 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007041
7042 if (!qla_tgt_plogi_cachep) {
7043 ql_log(ql_log_fatal, NULL, 0xe06d,
7044 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7045 ret = -ENOMEM;
7046 goto out_mgmt_cmd_cachep;
7047 }
7048
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007049 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7050 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7051 if (!qla_tgt_mgmt_cmd_mempool) {
7052 ql_log(ql_log_fatal, NULL, 0xe06e,
7053 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7054 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007055 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007056 }
7057
7058 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7059 if (!qla_tgt_wq) {
7060 ql_log(ql_log_fatal, NULL, 0xe06f,
7061 "alloc_workqueue for qla_tgt_wq failed\n");
7062 ret = -ENOMEM;
7063 goto out_cmd_mempool;
7064 }
7065 /*
7066 * Return 1 to signal that initiator-mode is being disabled
7067 */
7068 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7069
7070out_cmd_mempool:
7071 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007072out_plogi_cachep:
7073 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007074out_mgmt_cmd_cachep:
7075 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007076 return ret;
7077}
7078
7079void qlt_exit(void)
7080{
7081 if (!QLA_TGT_MODE_ENABLED())
7082 return;
7083
7084 destroy_workqueue(qla_tgt_wq);
7085 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007086 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007087 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007088}